// Metro module 969 (f970). import { GEN_AI_PROMPT_ATTRIBUTE } from "./Module958"; import { setMessagesAttribute, messagesFromParams } from "./Module970"; import { captureException } from "./captureCheckIn"; import { SPAN_STATUS_ERROR } from "./Module840"; import { getClient } from "./Module848"; function $mk67398($env) { return function addContentAttributes(a0, a1) { let r23; const r5 = $env; if ("content" in a1 && Array.isArray(a1.content)) { const setAttributes = a0.setAttributes; const r28 = {}; r28[(0, r5[0])(r5[1][1]).GEN_AI_RESPONSE_TEXT_ATTRIBUTE] = a1.content.map((item) => item.text).filter((item) => !!item).join(""); setAttributes.call(a0, r28); const r42 = []; let r45 = a1.content[Symbol.iterator](); const next = r45.next; while (true) { const r50 = r45 === void 0 ? void 0 : (r23 = next.call(r45)).done ? r45 = void 0 : r23.value; if (r45 === void 0) { if (!(r42.length > 0)) { break; } const setAttributes2 = a0.setAttributes; const r63 = {}; r63[(0, r5[0])(r5[1][1]).GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE] = JSON.stringify(r42); setAttributes2.call(a0, r63); break; } try { if (!(r50.type !== "tool_use" && r50.type !== "server_tool_use")) { r42.push(r50); } } catch (r59) { if (r45 !== void 0 && typeof r45.return === "function") { try { r45.return(); } catch {} } throw r59; } } } if ("completion" in a1) { const setAttributes3 = a0.setAttributes; const r76 = {}; r76[(0, r5[0])(r5[1][1]).GEN_AI_RESPONSE_TEXT_ATTRIBUTE] = a1.completion; setAttributes3.call(a0, r76); } if ("input_tokens" in a1) { const setAttributes4 = a0.setAttributes; const r87 = {}; r87[(0, r5[0])(r5[1][1]).GEN_AI_RESPONSE_TEXT_ATTRIBUTE] = JSON.stringify(a1.input_tokens); setAttributes4.call(a0, r87); } }; } function $mk67400($env) { return (a0, a1) => { let r23; const r4 = $env; const r5 = a0[a1]; const r3 = $env.$parent; const r8 = r3[0]; const r1 = r3[1]; const r18 = r8(r1[2]).buildMethodPath(r4[0], String(a1)); if (typeof r5 === "function" && r8(r1[4]).shouldInstrument(r18)) { const r33 = r4[1]; let env23932 = { $parent: r3 }; env23932[0] = r5; env23932[1] = r18; env23932[2] = a0; env23932[3] = r33; r23 = new Proxy(r5, { apply: (a0, a1, a2) => { let r7, r8, r12, r14, r127, r152, attributes; let c23928_0, c23928_1; L25: { c23928_0 = a0; c23928_1 = a2; r14 = env23932[1]; attributes = {}; r12 = env23932.$parent; r8 = r12[0]; r7 = r12[1]; attributes[r8(r7[1]).GEN_AI_SYSTEM_ATTRIBUTE] = "anthropic"; attributes[r8(r7[1]).GEN_AI_OPERATION_NAME_ATTRIBUTE] = r8(r7[2]).getFinalOperationName(r14); attributes[r8(r7[3]).SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] = "auto.ai.anthropic"; if (a2.length > 0 && (typeof a2[0] === "object" && a2[0] !== null)) { const r50 = a2[0]; if (r50.tools && Array.isArray(r50.tools)) { attributes[r8(r7[1]).GEN_AI_REQUEST_AVAILABLE_TOOLS_ATTRIBUTE] = JSON.stringify(r50.tools); } const GEN_AI_REQUEST_MODEL_ATTRIBUTE = r8(r7[1]).GEN_AI_REQUEST_MODEL_ATTRIBUTE; let model = r50.model; if (model == null) { model = "unknown"; } attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE] = model; if ("temperature" in r50) { attributes[r8(r7[1]).GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE] = r50.temperature; } if ("top_p" in r50) { attributes[r8(r7[1]).GEN_AI_REQUEST_TOP_P_ATTRIBUTE] = r50.top_p; } if ("stream" in r50) { attributes[r8(r7[1]).GEN_AI_REQUEST_STREAM_ATTRIBUTE] = r50.stream; } if ("top_k" in r50) { attributes[r8(r7[1]).GEN_AI_REQUEST_TOP_K_ATTRIBUTE] = r50.top_k; } if ("frequency_penalty" in r50) { attributes[r8(r7[1]).GEN_AI_REQUEST_FREQUENCY_PENALTY_ATTRIBUTE] = r50.frequency_penalty; } if (!("max_tokens" in r50)) { break L25; } attributes[r8(r7[1]).GEN_AI_REQUEST_MAX_TOKENS_ATTRIBUTE] = r50.max_tokens; } else { attributes[r8(r7[1]).GEN_AI_REQUEST_MODEL_ATTRIBUTE] = r14 !== "models.retrieve" ? r14 === "models.get" ? a2[0] : "unknown" : a2[0]; } } let r23 = attributes[r8(r7[1]).GEN_AI_REQUEST_MODEL_ATTRIBUTE]; if (r23 == null) { r23 = "unknown"; } const finalOperationName = r8(r7[2]).getFinalOperationName(r14); const r109 = typeof a2[0] === "object" ? a2[0] : void 0; let c23928_2 = r109; const r114 = Boolean(r109?.stream); const r116 = r14 === "messages.stream"; if (!r114 && !r116) { r127 = r8(r7[7]).startSpan({ name: `${finalOperationName} ${r23}`, op: r8(r7[2]).getSpanOperation(r14), attributes }, (a0) => { let c28696_0 = a0; if (env23932[3].recordInputs && c23928_2) { const r18 = env23932.$parent; const r10 = r18[0]; const r9 = r18[1]; const r11 = r10(r9[4]).messagesFromParams(c23928_2); r10(r9[4]).setMessagesAttribute(a0, r11); if ("prompt" in c23928_2) { const setAttributes = a0.setAttributes; const r27 = {}; r27[r10(r9[1]).GEN_AI_PROMPT_ATTRIBUTE] = JSON.stringify(c23928_2.prompt); setAttributes.call(a0, r27); } } const r37 = env23932.$parent; return (0, r37[0])(r37[1][9]).handleCallbackErrors(() => c23928_0.apply(env23932[2], c23928_1), (a0) => { const r1 = env23932.$parent; (0, r1[0])(r1[1][5]).captureException(a0, { mechanism: { handled: false, type: "auto.ai.anthropic", data: { function: env23932[1] } } }); }, () => {}, (a0) => { const recordOutputs2 = env23932[3].recordOutputs; const r22 = a0 ? typeof a0 === "object" : a0; L23: { if (r22) { const r23 = env23932.$parent; if ("type" in a0 && a0.type === "error") { (0, r23[0])(r23[1][4]).handleResponseError(c28696_0, a0); break L23; } if (recordOutputs2) { $mk67398(r23)(c28696_0, a0); } if ("id" in a0 && "model" in a0) { const r31 = {}; const r32 = r23[0]; const r33 = r23[1]; r31[r32(r33[1]).GEN_AI_RESPONSE_ID_ATTRIBUTE] = a0.id; r31[r32(r33[1]).GEN_AI_RESPONSE_MODEL_ATTRIBUTE] = a0.model; c28696_0.setAttributes(r31); if ("created" in a0 && typeof a0.created === "number") { const r45 = {}; r45[r32(r33[1]).ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE] = new Date(1000 * a0.created).toISOString(); c28696_0.setAttributes(r45); } if ("created_at" in a0 && typeof a0.created_at === "number") { const r61 = {}; r61[r32(r33[1]).ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE] = new Date(1000 * a0.created_at).toISOString(); c28696_0.setAttributes(r61); } if ("usage" in a0 && a0.usage) { r32(r33[2]).setTokenUsageAttributes(c28696_0, a0.usage.input_tokens, a0.usage.output_tokens, a0.usage.cache_creation_input_tokens, a0.usage.cache_read_input_tokens); } } } } }); }); } else { const r128 = env23932[0]; const r129 = env23932[2]; const r130 = env23932[3]; let env23929 = { $parent: r12 }; env23929[0] = r128; env23929[1] = a0; env23929[2] = r129; env23929[3] = a2; env23929[4] = r14; env23929[5] = r109; env23929[6] = r130; let r135 = attributes[r8(r7[1]).GEN_AI_REQUEST_MODEL_ATTRIBUTE]; if (r135 == null) { r135 = "unknown"; } const r139 = { name: `${finalOperationName} ${r135} stream-response`, op: r8(r7[2]).getSpanOperation(r14), attributes }; if (r114 && !r116) { const r145 = r8(r7[7]); let c23930_0 = (0, r12[2])(function*(c31861_0) { let r72; let recordOutputs, c31861_4; let c31861_5 = 0; let c32089_0, c32089_1; let r12 = void 0; L15: { try { c32089_0 = c31861_0; c32089_1 = void 0; c31861_5 = 1; if (env23929[6].recordInputs && env23929[5]) { (0, env23929.$parent[3])(c31861_0, env23929[5]); } const r47 = yield env23929[0].apply(env23929[2], env23929[3]); r12 = void 0; c32089_1 = r47; const r61 = env23929.$parent; const r19 = (0, r61[0])(r61[1][8]); const r66 = c32089_1; const r67 = c32089_0; const recordOutputs2 = env23929[6].recordOutputs; recordOutputs = recordOutputs2; if (recordOutputs2 == null) { recordOutputs = false; } r72 = r19.instrumentAsyncIterableStream(r66, r67, recordOutputs); c31861_5 = 0; } catch ($e) { const r30 = $e; let r7 = r30; c31861_4 = r30; if (c31861_5 !== 0) { while (true) { try { const r81 = c31861_4; c31861_5 = 0; (0, env23929.$parent[4])(r81, c32089_0, env23929[4]); c32089_0 = c31861_0; c32089_1 = void 0; c31861_5 = 1; if (env23929[6].recordInputs && env23929[5]) { (0, env23929.$parent[3])(c31861_0, env23929[5]); } const r107 = yield env23929[0].apply(env23929[2], env23929[3]); r12 = void 0; c32089_1 = r107; const r121 = env23929.$parent; const r125 = (0, r121[0])(r121[1][8]); const r127 = c32089_1; const r128 = c32089_0; const recordOutputs3 = env23929[6].recordOutputs; recordOutputs = recordOutputs3; if (recordOutputs3 == null) { recordOutputs = false; } r72 = r125.instrumentAsyncIterableStream(r127, r128, recordOutputs); c31861_5 = 0; break L15; } catch (r77) { r7 = r77; c31861_4 = r77; if (c31861_5 === 0) { break; } } } } throw r7; } } return r72; }); r152 = r145.startSpanManual(r139, function(a0) { return c23930_0.apply(this, arguments); }); } else { r152 = r8(r7[7]).startSpanManual(r139, (a0) => { let r23; try { const r1 = env23929[6]; if (r1.recordInputs && env23929[5]) { (0, env23929.$parent[3])(a0, env23929[5]); } const r13 = env23929[1].apply(env23929[2], env23929[3]); const r14 = env23929.$parent; const r19 = (0, r14[0])(r14[1][8]); let recordOutputs2 = r1.recordOutputs; if (recordOutputs2 == null) { recordOutputs2 = false; } r23 = r19.instrumentMessageStream(r13, a0, recordOutputs2); } catch ($e) { (0, env23929.$parent[4])($e, a0, env23929[4]); throw new Error("unreachable"); } return r23; }); } r127 = r152; } return r127; } }); } else if (typeof r5 === "function") { r23 = r5.bind(a0); } else { r23 = r5; if (r5) { r23 = r5; if (typeof r23 === "object") { const r24 = r4[1]; let r25 = r18; if (r25 === void 0) { r25 = ""; } let env23931; const r26 = env23931 = { $parent: r3 }; env23931[0] = r25; env23931[1] = r24; r23 = new Proxy(r5, { get: $mk67400(r26) }); } } } return r23; }; } let handleStreamingError; let env798 = {}; const addPrivateRequestAttributes = function addPrivateRequestAttributes(a0, a1) { setMessagesAttribute(a0, messagesFromParams(a1)); if ("prompt" in a1) { const setAttributes = a0.setAttributes; const r17 = {}; r17[GEN_AI_PROMPT_ATTRIBUTE] = JSON.stringify(a1.prompt); setAttributes.call(a0, r17); } }; handleStreamingError = function handleStreamingError(a0, a1, a2) { captureException(a0, { mechanism: { handled: false, type: "auto.ai.anthropic", data: { function: a2 } } }); if (a1.isRecording()) { a1.setStatus({ code: SPAN_STATUS_ERROR, message: "internal_error" }); a1.end(); } throw a0; }; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); export function instrumentAnthropicAiClient(a0, a1) { const r6 = Boolean; const client = getClient(); const r15 = client == null ? void 0 : client.getOptions().sendDefaultPii; let env12599; const recordInputs = r6(r15); const r20 = { recordInputs, recordOutputs: recordInputs, ...a1 }; const r22 = env12599 = { $parent: env798 }; env12599[0] = ""; env12599[1] = r20; return new Proxy(a0, { get: $mk67400(r22) }); }