import "../chunk-NSBPE2FW.js";

// src/telemetry/schema.ts
var TELEMETRY_SCHEMA_VERSION = 1;

// src/telemetry/client.ts
var TelemetryClient = class {
  constructor(sink, defaultSource) {
    this.sink = sink;
    this.defaultSource = defaultSource;
  }
  sink;
  defaultSource;
  emit(args) {
    const envelope = {
      schemaVersion: TELEMETRY_SCHEMA_VERSION,
      envelopeId: makeEnvelopeId(),
      runId: args.runId,
      timestamp: (/* @__PURE__ */ new Date()).toISOString(),
      source: args.source ?? this.defaultSource,
      kind: args.kind,
      ok: args.ok,
      durationMs: args.durationMs,
      data: args.data ?? {},
      metrics: args.metrics ?? {},
      ...args.parentRunId ? { parentRunId: args.parentRunId } : {},
      ...args.model ? { model: args.model } : {},
      ...args.tags ? { tags: args.tags } : {},
      ...args.error ? { error: args.error } : {}
    };
    try {
      this.sink.emit(envelope);
    } catch {
    }
  }
  async close() {
    await this.sink.close?.();
  }
};
function makeEnvelopeId() {
  if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
    return crypto.randomUUID();
  }
  return `env-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
}
var SECRET_FLAGS = /* @__PURE__ */ new Set(["--api-key", "--bearer", "--token", "--password"]);
function sanitiseArgv(argv) {
  const out = [];
  for (let i = 0; i < argv.length; i++) {
    const a = argv[i];
    if (SECRET_FLAGS.has(a)) {
      out.push(a, "<redacted>");
      i++;
      continue;
    }
    if (/^(?:--api-key|--bearer|--token|--password)=/.test(a)) {
      out.push(a.replace(/=.*$/, "=<redacted>"));
      continue;
    }
    out.push(a);
  }
  return out;
}

// src/telemetry/sink-fetch.ts
var HttpTelemetrySink = class {
  constructor(endpoint, bearer) {
    this.endpoint = endpoint;
    this.bearer = bearer;
  }
  endpoint;
  bearer;
  inflight = /* @__PURE__ */ new Set();
  emit(envelope) {
    const body = JSON.stringify(envelope);
    const headers = { "content-type": "application/json" };
    if (this.bearer) headers.authorization = `Bearer ${this.bearer}`;
    const promise = fetch(this.endpoint, { method: "POST", headers, body }).then(() => void 0).catch(() => void 0);
    this.inflight.add(promise);
    promise.finally(() => this.inflight.delete(promise));
  }
  async close() {
    await Promise.allSettled(Array.from(this.inflight));
  }
};
var FanoutTelemetrySink = class {
  constructor(sinks) {
    this.sinks = sinks;
  }
  sinks;
  emit(envelope) {
    for (const sink of this.sinks) {
      try {
        const result = sink.emit(envelope);
        if (result && typeof result.catch === "function") {
          ;
          result.catch(() => void 0);
        }
      } catch {
      }
    }
  }
  async close() {
    await Promise.allSettled(this.sinks.map((s) => Promise.resolve(s.close?.())));
  }
};
var NullTelemetrySink = class {
  emit() {
  }
};
var InMemoryTelemetrySink = class {
  envelopes = [];
  emit(envelope) {
    this.envelopes.push(envelope);
  }
  clear() {
    this.envelopes.length = 0;
  }
};
export {
  FanoutTelemetrySink,
  HttpTelemetrySink,
  InMemoryTelemetrySink,
  NullTelemetrySink,
  SECRET_FLAGS,
  TELEMETRY_SCHEMA_VERSION,
  TelemetryClient,
  sanitiseArgv
};
//# sourceMappingURL=index.js.map