/**
 * Prometheus metrics factory for SDK agents.
 *
 * Promoted from forge/src/agents/shared/metrics.ts (Vague 1.B.4).
 *
 * prom-client is an optional peer dependency. If absent, all metrics are
 * no-op stubs so consumers that do not use Prometheus can still import this
 * module without errors.
 *
 * @public @since 0.18.0
 */
import { createRequire } from "node:module";
// ─── Lazy prom-client loader ──────────────────────────────────────────────────
let _prom;
/**
 * Load prom-client synchronously once, cache result.
 * Returns null if prom-client is not installed.
 */
function loadPromClient() {
    if (_prom !== undefined)
        return _prom;
    try {
        const req = createRequire(import.meta.url);
        _prom = req("prom-client");
    }
    catch {
        _prom = null;
    }
    return _prom;
}
// ─── No-op stubs ──────────────────────────────────────────────────────────────
/** Minimal no-op counter. Safe to call `.inc()` / `.labels()` without effect. */
function noopCounter() {
    const stub = {
        inc: () => undefined,
        labels: () => stub,
        reset: () => undefined,
        remove: () => undefined,
    };
    return stub;
}
/** Minimal no-op histogram. Safe to call `.observe()` / `.startTimer()` without effect. */
function noopHistogram() {
    const stub = {
        observe: () => undefined,
        startTimer: () => () => 0,
        labels: () => stub,
        reset: () => undefined,
        remove: () => undefined,
        zero: () => undefined,
    };
    return stub;
}
// ─── Factory ──────────────────────────────────────────────────────────────────
/**
 * Create a set of standard Prometheus metrics for an SDK agent.
 *
 * If prom-client is not installed (optional peer dependency), returns no-op
 * stubs so the call site does not need to handle `null`.
 *
 * @example
 * ```ts
 * import { createAgentMetrics } from "@vauban-org/agent-sdk/metrics";
 *
 * const m = createAgentMetrics({ prefix: "forge_" });
 * m.cycleStarted.inc({ agent_id: "my-agent" });
 * ```
 *
 * @public
 */
export function createAgentMetrics(opts = {}) {
    const prefix = opts.prefix ?? "agent_";
    const prom = loadPromClient();
    if (prom === null) {
        return {
            cycleStarted: noopCounter(),
            cycleCompleted: noopCounter(),
            cycleDuration: noopHistogram(),
            hitlRequested: noopCounter(),
            hitlResolved: noopCounter(),
            llmCallCount: noopCounter(),
            llmTokensUsed: noopCounter(),
            llmCostUsd: noopCounter(),
        };
    }
    const registry = opts.registry ?? new prom.Registry();
    function counter(name, help, labelNames) {
        return new prom.Counter({
            name: `${prefix}${name}`,
            help,
            labelNames,
            registers: [registry],
        });
    }
    function histogram(name, help, labelNames, buckets) {
        return new prom.Histogram({
            name: `${prefix}${name}`,
            help,
            labelNames,
            buckets: buckets ?? prom.linearBuckets(0, 500, 12),
            registers: [registry],
        });
    }
    return {
        cycleStarted: counter("cycle_started_total", "OODA cycles started", ["agent_id"]),
        cycleCompleted: counter("cycle_completed_total", "OODA cycles completed", ["agent_id", "outcome"]),
        cycleDuration: histogram("cycle_duration_ms", "OODA cycle duration in milliseconds", ["agent_id", "phase"]),
        hitlRequested: counter("hitl_requested_total", "HITL requests sent", ["agent_id"]),
        hitlResolved: counter("hitl_resolved_total", "HITL requests resolved", ["agent_id", "decision"]),
        llmCallCount: counter("llm_calls_total", "LLM API calls", ["agent_id", "provider"]),
        llmTokensUsed: counter("llm_tokens_total", "LLM tokens consumed", ["agent_id", "provider", "direction"]),
        llmCostUsd: counter("llm_cost_usd_total", "Cumulative LLM cost in USD", ["agent_id", "provider"]),
    };
}
//# sourceMappingURL=create-agent-metrics.js.map