268 lines
11 KiB
JavaScript
268 lines
11 KiB
JavaScript
// Metro module 963 (f964).
|
|
import { OPENAI_OPERATIONS, OPENAI_RESPONSE_ID_ATTRIBUTE, GEN_AI_RESPONSE_ID_ATTRIBUTE, OPENAI_RESPONSE_MODEL_ATTRIBUTE, GEN_AI_RESPONSE_MODEL_ATTRIBUTE, OPENAI_RESPONSE_TIMESTAMP_ATTRIBUTE, OPENAI_USAGE_PROMPT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, OPENAI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE, GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE, GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE, GEN_AI_CONVERSATION_ID_ATTRIBUTE, GEN_AI_REQUEST_MODEL_ATTRIBUTE, GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE, GEN_AI_REQUEST_TOP_P_ATTRIBUTE, GEN_AI_REQUEST_FREQUENCY_PENALTY_ATTRIBUTE, GEN_AI_REQUEST_PRESENCE_PENALTY_ATTRIBUTE, GEN_AI_REQUEST_STREAM_ATTRIBUTE, GEN_AI_REQUEST_ENCODING_FORMAT_ATTRIBUTE, GEN_AI_REQUEST_DIMENSIONS_ATTRIBUTE } from "./Module958";
|
|
import { INSTRUMENTED_METHODS } from "./Module964";
|
|
const getOperationName = function getOperationName(a0) {
|
|
return a0.includes("chat.completions") ? OPENAI_OPERATIONS.CHAT : a0.includes("responses") ? OPENAI_OPERATIONS.RESPONSES : a0.includes("embeddings") ? OPENAI_OPERATIONS.EMBEDDINGS : a0.includes("conversations") ? OPENAI_OPERATIONS.CONVERSATIONS : a0.split(".").pop() || "unknown";
|
|
};
|
|
Object.defineProperty(exports, Symbol.toStringTag, {
|
|
value: "Module"
|
|
});
|
|
export function addChatCompletionAttributes(a0, a1, a2) {
|
|
const { id, model, created } = a1;
|
|
const setAttributes = a0.setAttributes;
|
|
const r3 = {};
|
|
r3[OPENAI_RESPONSE_ID_ATTRIBUTE] = id;
|
|
r3[GEN_AI_RESPONSE_ID_ATTRIBUTE] = id;
|
|
setAttributes.call(a0, r3);
|
|
const setAttributes2 = a0.setAttributes;
|
|
const r23 = {};
|
|
r23[OPENAI_RESPONSE_MODEL_ATTRIBUTE] = model;
|
|
r23[GEN_AI_RESPONSE_MODEL_ATTRIBUTE] = model;
|
|
setAttributes2.call(a0, r23);
|
|
const setAttributes3 = a0.setAttributes;
|
|
const r32 = {};
|
|
r32[OPENAI_RESPONSE_TIMESTAMP_ATTRIBUTE] = new Date(1000 * created).toISOString();
|
|
setAttributes3.call(a0, r32);
|
|
if (a1.usage) {
|
|
const prompt_tokens = a1.usage.prompt_tokens;
|
|
const completion_tokens = a1.usage.completion_tokens;
|
|
const total_tokens = a1.usage.total_tokens;
|
|
if (prompt_tokens !== void 0) {
|
|
const setAttributes4 = a0.setAttributes;
|
|
const r51 = {};
|
|
r51[OPENAI_USAGE_PROMPT_TOKENS_ATTRIBUTE] = prompt_tokens;
|
|
r51[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] = prompt_tokens;
|
|
setAttributes4.call(a0, r51);
|
|
}
|
|
if (completion_tokens !== void 0) {
|
|
const setAttributes5 = a0.setAttributes;
|
|
const r60 = {};
|
|
r60[OPENAI_USAGE_COMPLETION_TOKENS_ATTRIBUTE] = completion_tokens;
|
|
r60[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] = completion_tokens;
|
|
setAttributes5.call(a0, r60);
|
|
}
|
|
if (total_tokens !== void 0) {
|
|
const setAttributes6 = a0.setAttributes;
|
|
const r69 = {};
|
|
r69[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE] = total_tokens;
|
|
setAttributes6.call(a0, r69);
|
|
}
|
|
}
|
|
if (Array.isArray(a1.choices)) {
|
|
const r84 = a1.choices.map((choice) => choice.finish_reason).filter((item) => item !== null);
|
|
if (r84.length > 0) {
|
|
const setAttributes7 = a0.setAttributes;
|
|
const r88 = {};
|
|
r88[GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE] = JSON.stringify(r84);
|
|
setAttributes7.call(a0, r88);
|
|
}
|
|
if (a2) {
|
|
const r105 = a1.choices.map((choice) => choice.message?.tool_calls).filter((item) => Array.isArray(item) && item.length > 0).flat();
|
|
if (r105.length > 0) {
|
|
const setAttributes8 = a0.setAttributes;
|
|
const r108 = {};
|
|
r108[GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE] = JSON.stringify(r105);
|
|
setAttributes8.call(a0, r108);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
export function addConversationAttributes(a0, { id, created_at }) {
|
|
const setAttributes = a0.setAttributes;
|
|
const r12 = {};
|
|
r12[OPENAI_RESPONSE_ID_ATTRIBUTE] = id;
|
|
r12[GEN_AI_RESPONSE_ID_ATTRIBUTE] = id;
|
|
r12[GEN_AI_CONVERSATION_ID_ATTRIBUTE] = id;
|
|
setAttributes.call(a0, r12);
|
|
if (created_at) {
|
|
const setAttributes2 = a0.setAttributes;
|
|
const r24 = {};
|
|
r24[OPENAI_RESPONSE_TIMESTAMP_ATTRIBUTE] = new Date(1000 * created_at).toISOString();
|
|
setAttributes2.call(a0, r24);
|
|
}
|
|
}
|
|
export function addEmbeddingsAttributes(a0, a1) {
|
|
const setAttributes = a0.setAttributes;
|
|
const r2 = {};
|
|
r2[OPENAI_RESPONSE_MODEL_ATTRIBUTE] = a1.model;
|
|
r2[GEN_AI_RESPONSE_MODEL_ATTRIBUTE] = a1.model;
|
|
setAttributes.call(a0, r2);
|
|
if (a1.usage) {
|
|
const prompt_tokens = a1.usage.prompt_tokens;
|
|
const total_tokens = a1.usage.total_tokens;
|
|
if (prompt_tokens !== void 0) {
|
|
const setAttributes2 = a0.setAttributes;
|
|
const r23 = {};
|
|
r23[OPENAI_USAGE_PROMPT_TOKENS_ATTRIBUTE] = prompt_tokens;
|
|
r23[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] = prompt_tokens;
|
|
setAttributes2.call(a0, r23);
|
|
}
|
|
if (total_tokens !== void 0) {
|
|
const setAttributes3 = a0.setAttributes;
|
|
const r32 = {};
|
|
r32[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE] = total_tokens;
|
|
setAttributes3.call(a0, r32);
|
|
}
|
|
}
|
|
}
|
|
export function addResponsesApiAttributes(a0, a1, a2) {
|
|
const { id, model, created_at } = a1;
|
|
const setAttributes = a0.setAttributes;
|
|
const r3 = {};
|
|
r3[OPENAI_RESPONSE_ID_ATTRIBUTE] = id;
|
|
r3[GEN_AI_RESPONSE_ID_ATTRIBUTE] = id;
|
|
setAttributes.call(a0, r3);
|
|
const setAttributes2 = a0.setAttributes;
|
|
const r24 = {};
|
|
r24[OPENAI_RESPONSE_MODEL_ATTRIBUTE] = model;
|
|
r24[GEN_AI_RESPONSE_MODEL_ATTRIBUTE] = model;
|
|
setAttributes2.call(a0, r24);
|
|
const setAttributes3 = a0.setAttributes;
|
|
const r33 = {};
|
|
r33[OPENAI_RESPONSE_TIMESTAMP_ATTRIBUTE] = new Date(1000 * created_at).toISOString();
|
|
setAttributes3.call(a0, r33);
|
|
if (a1.status) {
|
|
const setAttributes4 = a0.setAttributes;
|
|
const r47 = {};
|
|
r47[GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE] = JSON.stringify([a1.status]);
|
|
setAttributes4.call(a0, r47);
|
|
}
|
|
if (a1.usage) {
|
|
const input_tokens = a1.usage.input_tokens;
|
|
const output_tokens = a1.usage.output_tokens;
|
|
const total_tokens = a1.usage.total_tokens;
|
|
if (input_tokens !== void 0) {
|
|
const setAttributes5 = a0.setAttributes;
|
|
const r63 = {};
|
|
r63[OPENAI_USAGE_PROMPT_TOKENS_ATTRIBUTE] = input_tokens;
|
|
r63[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] = input_tokens;
|
|
setAttributes5.call(a0, r63);
|
|
}
|
|
if (output_tokens !== void 0) {
|
|
const setAttributes6 = a0.setAttributes;
|
|
const r72 = {};
|
|
r72[OPENAI_USAGE_COMPLETION_TOKENS_ATTRIBUTE] = output_tokens;
|
|
r72[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] = output_tokens;
|
|
setAttributes6.call(a0, r72);
|
|
}
|
|
if (total_tokens !== void 0) {
|
|
const setAttributes7 = a0.setAttributes;
|
|
const r81 = {};
|
|
r81[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE] = total_tokens;
|
|
setAttributes7.call(a0, r81);
|
|
}
|
|
}
|
|
if (a2 && Array.isArray(a1.output) && a1.output.length > 0) {
|
|
const r97 = a1.output.filter((item) => (typeof item === "object" ? item !== null : typeof item === "object") && item.type === "function_call");
|
|
if (r97.length > 0) {
|
|
const setAttributes8 = a0.setAttributes;
|
|
const r100 = {};
|
|
r100[GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE] = JSON.stringify(r97);
|
|
setAttributes8.call(a0, r100);
|
|
}
|
|
}
|
|
}
|
|
export function buildMethodPath(a0, a1) {
|
|
return a0 ? `${a0}.${a1}` : a1;
|
|
}
|
|
export function extractRequestParameters(a0) {
|
|
let model = a0.model;
|
|
if (model == null) {
|
|
model = "unknown";
|
|
}
|
|
const r10 = {};
|
|
r10[GEN_AI_REQUEST_MODEL_ATTRIBUTE] = model;
|
|
if ("temperature" in a0) {
|
|
r10[GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE] = a0.temperature;
|
|
}
|
|
if ("top_p" in a0) {
|
|
r10[GEN_AI_REQUEST_TOP_P_ATTRIBUTE] = a0.top_p;
|
|
}
|
|
if ("frequency_penalty" in a0) {
|
|
r10[GEN_AI_REQUEST_FREQUENCY_PENALTY_ATTRIBUTE] = a0.frequency_penalty;
|
|
}
|
|
if ("presence_penalty" in a0) {
|
|
r10[GEN_AI_REQUEST_PRESENCE_PENALTY_ATTRIBUTE] = a0.presence_penalty;
|
|
}
|
|
if ("stream" in a0) {
|
|
r10[GEN_AI_REQUEST_STREAM_ATTRIBUTE] = a0.stream;
|
|
}
|
|
if ("encoding_format" in a0) {
|
|
r10[GEN_AI_REQUEST_ENCODING_FORMAT_ATTRIBUTE] = a0.encoding_format;
|
|
}
|
|
if ("dimensions" in a0) {
|
|
r10[GEN_AI_REQUEST_DIMENSIONS_ATTRIBUTE] = a0.dimensions;
|
|
}
|
|
const r58 = "conversation" in a0 && typeof a0.conversation === "string" ? a0.conversation : "previous_response_id" in a0 ? typeof a0.previous_response_id === "string" ? a0.previous_response_id : void 0 : void 0;
|
|
if (r58) {
|
|
r10[GEN_AI_CONVERSATION_ID_ATTRIBUTE] = r58;
|
|
}
|
|
return r10;
|
|
}
|
|
export { getOperationName };
|
|
export function getSpanOperation(a0) {
|
|
return `gen_ai.${getOperationName(a0)}`;
|
|
}
|
|
export function isChatCompletionChunk(a0) {
|
|
return a0 !== null && typeof a0 === "object" && "object" in a0 && a0.object === "chat.completion.chunk";
|
|
}
|
|
export function isChatCompletionResponse(a0) {
|
|
return a0 !== null && typeof a0 === "object" && "object" in a0 && a0.object === "chat.completion";
|
|
}
|
|
export function isConversationResponse(a0) {
|
|
return a0 !== null && typeof a0 === "object" && "object" in a0 && a0.object === "conversation";
|
|
}
|
|
export function isEmbeddingsResponse(a0) {
|
|
if (a0 !== null && typeof a0 === "object" && "object" in a0) {
|
|
return a0.object === "list" && typeof a0.model === "string" && a0.model.toLowerCase().includes("embedding");
|
|
}
|
|
return false;
|
|
}
|
|
export function isResponsesApiResponse(a0) {
|
|
return a0 !== null && typeof a0 === "object" && "object" in a0 && a0.object === "response";
|
|
}
|
|
export function isResponsesApiStreamEvent(a0) {
|
|
return a0 !== null && typeof a0 === "object" && "type" in a0 && typeof a0.type === "string" && a0.type.startsWith("response.");
|
|
}
|
|
export function setCommonResponseAttributes(a0, a1, a2, a3) {
|
|
const setAttributes = a0.setAttributes;
|
|
const r2 = {};
|
|
r2[OPENAI_RESPONSE_ID_ATTRIBUTE] = a1;
|
|
r2[GEN_AI_RESPONSE_ID_ATTRIBUTE] = a1;
|
|
setAttributes.call(a0, r2);
|
|
const setAttributes2 = a0.setAttributes;
|
|
const r20 = {};
|
|
r20[OPENAI_RESPONSE_MODEL_ATTRIBUTE] = a2;
|
|
r20[GEN_AI_RESPONSE_MODEL_ATTRIBUTE] = a2;
|
|
setAttributes2.call(a0, r20);
|
|
const setAttributes3 = a0.setAttributes;
|
|
const r29 = {};
|
|
r29[OPENAI_RESPONSE_TIMESTAMP_ATTRIBUTE] = new Date(1000 * a3).toISOString();
|
|
setAttributes3.call(a0, r29);
|
|
}
|
|
export function setTokenUsageAttributes(a0, a1, a2, a3) {
|
|
if (a1 !== void 0) {
|
|
const setAttributes = a0.setAttributes;
|
|
const r1 = {};
|
|
r1[OPENAI_USAGE_PROMPT_TOKENS_ATTRIBUTE] = a1;
|
|
r1[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] = a1;
|
|
setAttributes.call(a0, r1);
|
|
}
|
|
if (a2 !== void 0) {
|
|
const setAttributes2 = a0.setAttributes;
|
|
const r17 = {};
|
|
r17[OPENAI_USAGE_COMPLETION_TOKENS_ATTRIBUTE] = a2;
|
|
r17[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] = a2;
|
|
setAttributes2.call(a0, r17);
|
|
}
|
|
if (a3 !== void 0) {
|
|
const setAttributes3 = a0.setAttributes;
|
|
const r29 = {};
|
|
r29[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE] = a3;
|
|
setAttributes3.call(a0, r29);
|
|
}
|
|
}
|
|
export function shouldInstrument(a0) {
|
|
return INSTRUMENTED_METHODS.includes(a0);
|
|
}
|