import { c as R, M as w, m as a, T as M, a as F } from "../tool-usage-meter-CBzE-MaZ.js";
import { b as P, i as z } from "../tool-usage-meter-CBzE-MaZ.js";
import { b as U } from "../telemetry-Bf0GI6nJ.js";
import { d as y } from "../shared-types-CIxCt9tj.js";
let n;
function k(t) {
  n = t;
}
function j(t) {
  return n ? n(t) : E;
}
const E = {
  recordInvoke: () => {
  },
  recordSuccess: () => {
  },
  recordError: () => {
  },
  recordFault: () => {
  },
  recordHistogram: () => {
  },
  reportCountMetrics: () => {
  }
}, u = 200;
let l, m, h, p, b, f, T, c, o = !1;
function B() {
  const t = R.getMeterProvider().getMeter(w.RemoteTool);
  l = t.createCounter("invoked", {
    description: "remote tool invocation count",
    unit: "number"
  }), m = t.createCounter("success", {
    description: "remote tool success count",
    unit: "number"
  }), h = t.createCounter("error", {
    description: "remote tool error count (invalid input)",
    unit: "number"
  }), p = t.createCounter("fault", {
    description: "remote tool fault count (internal failure)",
    unit: "number"
  }), b = t.createCounter("rejected", {
    description: "remote tool rejection count (user rejected)",
    unit: "number"
  }), f = t.createCounter("accepted", {
    description: "remote tool acceptance count (user accepted)",
    unit: "number"
  }), T = t.createCounter("trusted", {
    description: "remote tool trust count (user trusted)",
    unit: "number"
  }), c = t.createHistogram("duration", {
    description: "remote tool duration",
    unit: "ms"
  }), o = !0;
}
function d(t) {
  let e = "Unknown error";
  return t instanceof Error ? e = t.message : typeof t == "string" ? e = t : t && typeof t == "object" && "message" in t && (e = String(t.message)), e.length > u ? e.substring(0, u) + "..." : e;
}
class C {
  startTime;
  attributes;
  constructor(e, r) {
    this.startTime = performance.now(), this.attributes = { toolName: e, ...r }, o && l.add(1, this.attributes);
  }
  /** Create a new recorder and record the initial invocation. */
  static start(e, r) {
    return new C(e, r);
  }
  /** Record success metric and duration. */
  recordSuccess() {
    o && (m.add(1, this.attributes), c.record(performance.now() - this.startTime, this.attributes));
  }
  /** Record error metric (invalid input). */
  recordError(e) {
    if (!o) return;
    const r = a(e), i = d(e), s = { ...this.attributes, errorType: r, errorMessage: i };
    h.add(1, s), c.record(performance.now() - this.startTime, s);
  }
  /** Record fault metric (internal failure). */
  recordFault(e) {
    if (!o) return;
    const r = a(e), i = d(e), s = { ...this.attributes, errorType: r, errorMessage: i };
    p.add(1, s), c.record(performance.now() - this.startTime, s);
  }
  /** Record rejection metric (user rejected). */
  recordRejection() {
    o && (b.add(1, this.attributes), c.record(performance.now() - this.startTime, this.attributes));
  }
  /** Record acceptance metric (user accepted). */
  recordAcceptance() {
    o && f.add(1, this.attributes);
  }
  /** Record trust metric (user trusted). */
  recordTrust() {
    o && T.add(1, this.attributes);
  }
}
const S = "web_fetch";
class g {
  baseRecorder;
  reporter;
  dimensions;
  fetchStartTime;
  constructor(e) {
    this.baseRecorder = e, this.reporter = U(y.Feature, S), this.dimensions = {};
  }
  /** Create a new WebFetch recorder and record the initial invoke. */
  static start() {
    const e = M.start(F.WebFetch);
    return new g(e);
  }
  /** Mark the start of the actual HTTP fetch operation for duration tracking. */
  startFetch() {
    this.fetchStartTime = performance.now();
  }
  /** Record success metric with optional content-level histograms. */
  recordToolSuccess(e) {
    if (e && (this.baseRecorder.recordHistogram("contentSize", e.contentLength, this.dimensions), e.truncated && this.reporter.reportCountMetrics({ webFetchContentTruncated: 1 }, this.dimensions), e.matchCount !== void 0 && (this.reporter.reportCountMetrics(
      { webFetchSelectiveMatches: 1 },
      { ...this.dimensions, matchCount: e.matchCount.toString() }
    ), this.baseRecorder.recordHistogram("matchCount", e.matchCount, this.dimensions))), this.fetchStartTime !== void 0) {
      const r = performance.now() - this.fetchStartTime;
      this.baseRecorder.recordHistogram("fetchTime", r, this.dimensions);
    }
    this.baseRecorder.recordToolSuccess();
  }
  /** Record an invalid input error (e.g. bad URL, user rejection). */
  recordToolError(e) {
    this.baseRecorder.recordToolError(e);
  }
  /** Record an internal tool fault (e.g. network failure, timeout). */
  recordToolFault(e) {
    this.baseRecorder.recordToolFault(e);
  }
  /** Record the fetch mode used and emit a mode usage histogram. */
  recordModeUsed(e) {
    this.dimensions.mode = e, this.reporter.reportCountMetrics({ webFetchModeUsed: 1 }, { mode: e });
    const r = e === "full" ? 1 : e === "truncated" ? 2 : e === "selective" ? 3 : 4;
    this.baseRecorder.recordHistogram("modeUsage", r, { mode: e });
  }
  /** Record when query parameters are stripped from the URL. */
  recordUrlCleaned(e, r) {
    e !== r && this.reporter.reportCountMetrics({ webFetchUrlCleaned: 1 }, { hasQuery: "true" });
  }
  /** Record when an unsafe (non-HTTPS) URL is blocked. */
  recordUnsafeUrlBlocked(e) {
    try {
      const r = new URL(e).protocol;
      this.reporter.reportCountMetrics({ webFetchUnsafeUrlBlocked: 1 }, { urlProtocol: r }), this.baseRecorder.recordHistogram("unsafeUrlAttempts", 1, { protocol: r });
    } catch {
      this.reporter.reportCountMetrics({ webFetchUnsafeUrlBlocked: 1 });
    }
  }
  /** Record when a redirect to an unsafe URL is blocked. */
  recordRedirectBlocked(e, r) {
    try {
      this.reporter.reportCountMetrics(
        { webFetchRedirectBlocked: 1 },
        {
          originalProtocol: new URL(e).protocol,
          redirectProtocol: new URL(r).protocol
        }
      );
    } catch {
      this.reporter.reportCountMetrics({ webFetchRedirectBlocked: 1 });
    }
  }
  /** Record when user accepts a web fetch approval prompt. */
  recordUserAccepted() {
    this.reporter.reportCountMetrics({ webFetchAccepted: 1 }, this.dimensions), this.baseRecorder.recordHistogram("userApproval", 1, { action: "accept" });
  }
  /** Record when user trusts web fetch (auto-approve future requests). */
  recordUserTrusted() {
    this.reporter.reportCountMetrics({ webFetchTrusted: 1 }, this.dimensions), this.baseRecorder.recordHistogram("userApproval", 2, { action: "trust" });
  }
  /** Record when user rejects a web fetch approval prompt. */
  recordUserRejected() {
    this.reporter.reportCountMetrics({ webFetchRejected: 1 }, this.dimensions), this.baseRecorder.recordHistogram("userApproval", 0, { action: "reject" });
  }
}
export {
  C as RemoteToolRecorder,
  F as Tool,
  M as ToolRecorder,
  g as WebFetchRecorder,
  j as createToolTelemetry,
  B as initializeRemoteToolCounters,
  P as initializeToolCounters,
  z as isAbortError,
  a as mapUnknownToErrorType,
  k as setToolTelemetryFactory
};
//# sourceMappingURL=index.js.map
