// Force strict mode and setup for ESM
"use strict";
import {
  context,
  createContextKey,
  init_esm
} from "./chunk-TARRHLLG.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/telemetry/gen-ai-request.ts
init_esbuild_shims();
init_esm();
var requestObserverKey = createContextKey(
  "qwen-code.gen-ai-request-observer"
);
function ownValue(record, key) {
  return Object.hasOwn(record, key) ? record[key] : void 0;
}
__name(ownValue, "ownValue");
function finiteNumber(value) {
  return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
__name(finiteNumber, "finiteNumber");
function safeInteger(value) {
  return typeof value === "number" && Number.isSafeInteger(value) ? value : void 0;
}
__name(safeInteger, "safeInteger");
function stopSequences(value, allowSingleString) {
  if (allowSingleString && typeof value === "string") return [value];
  if (!Array.isArray(value) || !value.every((item) => typeof item === "string"))
    return void 0;
  return [...value];
}
__name(stopSequences, "stopSequences");
function outputBudget(record) {
  const values = ["max_tokens", "max_completion_tokens", "max_new_tokens"].map((key) => ownValue(record, key)).filter((value) => value !== void 0 && value !== null);
  if (values.length === 0) return void 0;
  const integers = values.map(safeInteger);
  if (integers.some((value) => value === void 0)) return void 0;
  const first = integers[0];
  return integers.every((value) => value === first) ? first : void 0;
}
__name(outputBudget, "outputBudget");
function assignNumber(attributes, key, value) {
  const number = finiteNumber(value);
  if (number !== void 0) attributes[key] = number;
}
__name(assignNumber, "assignNumber");
function assignInteger(attributes, key, value) {
  const integer = safeInteger(value);
  if (integer !== void 0) attributes[key] = integer;
}
__name(assignInteger, "assignInteger");
function assignStopSequences(attributes, value, allowSingleString) {
  const sequences = stopSequences(value, allowSingleString);
  if (sequences !== void 0)
    attributes["gen_ai.request.stop_sequences"] = sequences;
}
__name(assignStopSequences, "assignStopSequences");
function extractOpenAiRequestAttributes(request) {
  const record = request;
  const attributes = {};
  const choiceCount = safeInteger(ownValue(record, "n"));
  if (choiceCount !== void 0 && choiceCount !== 1) {
    attributes["gen_ai.request.choice.count"] = choiceCount;
  }
  const maxTokens = outputBudget(record);
  if (maxTokens !== void 0) {
    attributes["gen_ai.request.max_tokens"] = maxTokens;
  }
  assignNumber(
    attributes,
    "gen_ai.request.temperature",
    ownValue(record, "temperature")
  );
  assignNumber(attributes, "gen_ai.request.top_p", ownValue(record, "top_p"));
  assignNumber(
    attributes,
    "gen_ai.request.frequency_penalty",
    ownValue(record, "frequency_penalty")
  );
  assignNumber(
    attributes,
    "gen_ai.request.presence_penalty",
    ownValue(record, "presence_penalty")
  );
  assignStopSequences(attributes, ownValue(record, "stop"), true);
  return attributes;
}
__name(extractOpenAiRequestAttributes, "extractOpenAiRequestAttributes");
function extractAnthropicRequestAttributes(request) {
  const record = request;
  const attributes = {};
  assignInteger(
    attributes,
    "gen_ai.request.max_tokens",
    ownValue(record, "max_tokens")
  );
  assignNumber(
    attributes,
    "gen_ai.request.temperature",
    ownValue(record, "temperature")
  );
  assignNumber(attributes, "gen_ai.request.top_p", ownValue(record, "top_p"));
  assignStopSequences(attributes, ownValue(record, "stop_sequences"), false);
  return attributes;
}
__name(extractAnthropicRequestAttributes, "extractAnthropicRequestAttributes");
function extractGeminiRequestAttributes(request) {
  const record = request;
  const config = ownValue(record, "config");
  if (typeof config !== "object" || config === null) return {};
  const configRecord = config;
  const attributes = {};
  const choiceCount = safeInteger(ownValue(configRecord, "candidateCount"));
  if (choiceCount !== void 0 && choiceCount !== 1) {
    attributes["gen_ai.request.choice.count"] = choiceCount;
  }
  assignInteger(
    attributes,
    "gen_ai.request.max_tokens",
    ownValue(configRecord, "maxOutputTokens")
  );
  assignNumber(
    attributes,
    "gen_ai.request.temperature",
    ownValue(configRecord, "temperature")
  );
  assignNumber(
    attributes,
    "gen_ai.request.top_p",
    ownValue(configRecord, "topP")
  );
  assignNumber(
    attributes,
    "gen_ai.request.frequency_penalty",
    ownValue(configRecord, "frequencyPenalty")
  );
  assignNumber(
    attributes,
    "gen_ai.request.presence_penalty",
    ownValue(configRecord, "presencePenalty")
  );
  assignStopSequences(
    attributes,
    ownValue(configRecord, "stopSequences"),
    false
  );
  return attributes;
}
__name(extractGeminiRequestAttributes, "extractGeminiRequestAttributes");
function createGenAiRequestObserverContext(parent, span) {
  try {
    if (!span.isRecording()) return parent;
  } catch {
    return parent;
  }
  let consumed = false;
  const observer = /* @__PURE__ */ __name((attributes) => {
    if (consumed) return;
    consumed = true;
    try {
      span.setAttributes(attributes);
    } catch {
    }
  }, "observer");
  try {
    return parent.setValue(requestObserverKey, observer);
  } catch {
    return parent;
  }
}
__name(createGenAiRequestObserverContext, "createGenAiRequestObserverContext");
function reportRequest(request, extract, requestContext) {
  let observer;
  try {
    observer = requestContext.getValue(requestObserverKey);
  } catch {
    return;
  }
  if (typeof observer !== "function") return;
  let attributes = {};
  try {
    attributes = extract(request);
  } catch {
  }
  try {
    observer(attributes);
  } catch {
  }
}
__name(reportRequest, "reportRequest");
function reportActiveRequest(request, extract, requestContext) {
  try {
    reportRequest(request, extract, requestContext ?? context.active());
  } catch {
  }
}
__name(reportActiveRequest, "reportActiveRequest");
function reportOpenAiRequest(request, requestContext) {
  reportActiveRequest(request, extractOpenAiRequestAttributes, requestContext);
}
__name(reportOpenAiRequest, "reportOpenAiRequest");
function reportAnthropicRequest(request, requestContext) {
  reportActiveRequest(
    request,
    extractAnthropicRequestAttributes,
    requestContext
  );
}
__name(reportAnthropicRequest, "reportAnthropicRequest");
function reportGeminiRequest(request, requestContext) {
  reportActiveRequest(request, extractGeminiRequestAttributes, requestContext);
}
__name(reportGeminiRequest, "reportGeminiRequest");

export {
  createGenAiRequestObserverContext,
  reportOpenAiRequest,
  reportAnthropicRequest,
  reportGeminiRequest
};
/**
 * @license
 * Copyright 2026 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
