"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var model_usage_exports = {};
__export(model_usage_exports, {
  ModelUsageCollector: () => ModelUsageCollector,
  filterZeroValues: () => filterZeroValues
});
module.exports = __toCommonJS(model_usage_exports);
function filterZeroValues(usage) {
  const result = {};
  for (const [key, value] of Object.entries(usage)) {
    if (value !== 0 && value !== 0) {
      result[key] = value;
    }
  }
  return result;
}
class ModelUsageCollector {
  llmUsage = /* @__PURE__ */ new Map();
  ttsUsage = /* @__PURE__ */ new Map();
  sttUsage = /* @__PURE__ */ new Map();
  interruptionUsage = /* @__PURE__ */ new Map();
  eotUsage = /* @__PURE__ */ new Map();
  /** Extract provider and model from metrics metadata. */
  extractProviderModel(metrics) {
    let provider = "";
    let model = "";
    if (metrics.metadata) {
      provider = metrics.metadata.modelProvider || "";
      model = metrics.metadata.modelName || "";
    }
    return [provider, model];
  }
  /** Get or create an LLMModelUsage for the given provider/model combination. */
  getLLMUsage(provider, model) {
    const key = `${provider}:${model}`;
    let usage = this.llmUsage.get(key);
    if (!usage) {
      usage = {
        type: "llm_usage",
        provider,
        model,
        inputTokens: 0,
        inputCachedTokens: 0,
        inputAudioTokens: 0,
        inputCachedAudioTokens: 0,
        inputTextTokens: 0,
        inputCachedTextTokens: 0,
        inputImageTokens: 0,
        inputCachedImageTokens: 0,
        outputTokens: 0,
        outputAudioTokens: 0,
        outputTextTokens: 0,
        sessionDurationMs: 0
      };
      this.llmUsage.set(key, usage);
    }
    return usage;
  }
  /** Get or create a TTSModelUsage for the given provider/model combination. */
  getTTSUsage(provider, model) {
    const key = `${provider}:${model}`;
    let usage = this.ttsUsage.get(key);
    if (!usage) {
      usage = {
        type: "tts_usage",
        provider,
        model,
        inputTokens: 0,
        outputTokens: 0,
        charactersCount: 0,
        audioDurationMs: 0
      };
      this.ttsUsage.set(key, usage);
    }
    return usage;
  }
  /** Get or create an STTModelUsage for the given provider/model combination. */
  getSTTUsage(provider, model) {
    const key = `${provider}:${model}`;
    let usage = this.sttUsage.get(key);
    if (!usage) {
      usage = {
        type: "stt_usage",
        provider,
        model,
        inputTokens: 0,
        outputTokens: 0,
        audioDurationMs: 0
      };
      this.sttUsage.set(key, usage);
    }
    return usage;
  }
  getInterruptionUsage(provider, model) {
    const key = `${provider}:${model}`;
    let usage = this.interruptionUsage.get(key);
    if (!usage) {
      usage = {
        type: "interruption_usage",
        provider,
        model,
        totalRequests: 0
      };
      this.interruptionUsage.set(key, usage);
    }
    return usage;
  }
  getEotUsage(provider, model) {
    const key = `${provider}:${model}`;
    let usage = this.eotUsage.get(key);
    if (!usage) {
      usage = {
        type: "eot_usage",
        provider,
        model,
        totalRequests: 0
      };
      this.eotUsage.set(key, usage);
    }
    return usage;
  }
  /** Collect metrics and aggregate usage by model/provider. */
  collect(metrics) {
    var _a, _b, _c;
    if (metrics.type === "llm_metrics") {
      const [provider, model] = this.extractProviderModel(metrics);
      const usage = this.getLLMUsage(provider, model);
      usage.inputTokens += metrics.promptTokens;
      usage.inputCachedTokens += metrics.promptCachedTokens;
      usage.outputTokens += metrics.completionTokens;
    } else if (metrics.type === "realtime_model_metrics") {
      const [provider, model] = this.extractProviderModel(metrics);
      const usage = this.getLLMUsage(provider, model);
      usage.inputTokens += metrics.inputTokens;
      usage.inputCachedTokens += metrics.inputTokenDetails.cachedTokens;
      usage.inputTextTokens += metrics.inputTokenDetails.textTokens;
      usage.inputCachedTextTokens += ((_a = metrics.inputTokenDetails.cachedTokensDetails) == null ? void 0 : _a.textTokens) ?? 0;
      usage.inputImageTokens += metrics.inputTokenDetails.imageTokens;
      usage.inputCachedImageTokens += ((_b = metrics.inputTokenDetails.cachedTokensDetails) == null ? void 0 : _b.imageTokens) ?? 0;
      usage.inputAudioTokens += metrics.inputTokenDetails.audioTokens;
      usage.inputCachedAudioTokens += ((_c = metrics.inputTokenDetails.cachedTokensDetails) == null ? void 0 : _c.audioTokens) ?? 0;
      usage.outputTextTokens += metrics.outputTokenDetails.textTokens;
      usage.outputAudioTokens += metrics.outputTokenDetails.audioTokens;
      usage.outputTokens += metrics.outputTokens;
      usage.sessionDurationMs += metrics.sessionDurationMs ?? 0;
    } else if (metrics.type === "tts_metrics") {
      const [provider, model] = this.extractProviderModel(metrics);
      const ttsUsage = this.getTTSUsage(provider, model);
      ttsUsage.inputTokens += metrics.inputTokens ?? 0;
      ttsUsage.outputTokens += metrics.outputTokens ?? 0;
      ttsUsage.charactersCount += metrics.charactersCount;
      ttsUsage.audioDurationMs += metrics.audioDurationMs;
    } else if (metrics.type === "stt_metrics") {
      const [provider, model] = this.extractProviderModel(metrics);
      const sttUsage = this.getSTTUsage(provider, model);
      sttUsage.inputTokens += metrics.inputTokens ?? 0;
      sttUsage.outputTokens += metrics.outputTokens ?? 0;
      sttUsage.audioDurationMs += metrics.audioDurationMs;
    } else if (metrics.type === "interruption_metrics") {
      const [provider, model] = this.extractProviderModel(metrics);
      const usage = this.getInterruptionUsage(provider, model);
      usage.totalRequests += metrics.numRequests;
    } else if (metrics.type === "eot_inference_metrics") {
      const [provider, model] = this.extractProviderModel(metrics);
      const usage = this.getEotUsage(provider, model);
      usage.totalRequests += metrics.numRequests;
    }
  }
  flatten() {
    const result = [];
    for (const u of this.llmUsage.values()) {
      result.push({ ...u });
    }
    for (const u of this.ttsUsage.values()) {
      result.push({ ...u });
    }
    for (const u of this.sttUsage.values()) {
      result.push({ ...u });
    }
    for (const u of this.interruptionUsage.values()) {
      result.push({ ...u });
    }
    for (const u of this.eotUsage.values()) {
      result.push({ ...u });
    }
    return result;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ModelUsageCollector,
  filterZeroValues
});
//# sourceMappingURL=model_usage.cjs.map