// Metro module 962 (f963). import { GEN_AI_SYSTEM_ATTRIBUTE, GEN_AI_OPERATION_NAME_ATTRIBUTE, GEN_AI_REQUEST_AVAILABLE_TOOLS_ATTRIBUTE, GEN_AI_REQUEST_MODEL_ATTRIBUTE, GEN_AI_REQUEST_MESSAGES_ATTRIBUTE, GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE } from "./Module958"; import { getOperationName, extractRequestParameters } from "./getOperationName"; import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from "./Module839"; import { getTruncatedJsonString } from "./Module960"; import { getClient } from "./Module848"; import { HermesBuiltin, $createGenerator } from "../_hermes/prelude"; function $mk67383($env) { return (a0, a1) => { let r23; const r2 = $env; const r7 = a0[a1]; const r3 = $env.$parent; const r8 = r3[0]; const r1 = r3[1]; const r18 = r8(r1[2]).buildMethodPath(r2[0], String(a1)); if (typeof r7 === "function" && r8(r1[2]).shouldInstrument(r18)) { const r33 = r2[1]; let env23919 = { $parent: r3 }; env23919[0] = r7; env23919[1] = r18; env23919[2] = a0; env23919[3] = r33; let c23920_0 = (0, r3[2])(function*(...c28694_0) { let r104; let c28694_1, c28694_2; L16: { let env31176; c28694_2 = void 0; const r13 = env31176 = { $parent: env23919 }; c28694_1 = r13; env31176[0] = c28694_0; env31176[1] = void 0; env31176[2] = void 0; env31176[3] = void 0; env31176[4] = void 0; yield void 0; const r57 = env23919.$parent; env31176[1] = (0, r57[3])(env31176[0], env23919[1]); env31176[2] = env31176[1][(0, r57[0])(r57[1][1]).GEN_AI_REQUEST_MODEL_ATTRIBUTE] || "unknown"; const r70 = env23919.$parent; env31176[3] = (0, r70[0])(r70[1][2]).getOperationName(env23919[1]); env31176[4] = env31176[0][0]; if (env31176[4]) { if (typeof env31176[4] === "object" && env31176[4].stream === true) { const r110 = c28694_1; const r112 = env23919.$parent; r104 = (0, r112[0])(r112[1][5]).startSpanManual({ name: `${env31176[3]} ${env31176[2]} stream-response`, op: (0, r112[0])(r112[1][2]).getSpanOperation(env23919[1]), attributes: env31176[1] }, $mk117564(r110)()); break L16; } } const r82 = c28694_1; const r17 = env23919.$parent; r104 = (0, r17[0])(r17[1][5]).startSpan({ name: `${env31176[3]} ${env31176[2]}`, op: (0, r17[0])(r17[1][2]).getSpanOperation(env23919[1]), attributes: env31176[1] }, $mk117565(r82)()); } return r104; }); r23 = function instrumentedMethod() { return c23920_0.apply(this, arguments); }; } else if (typeof r7 === "function") { r23 = r7.bind(a0); } else { r23 = r7; if (r7) { r23 = r7; if (typeof r23 === "object") { const r24 = r2[1]; let r25 = r18; if (r25 === void 0) { r25 = ""; } let env23918; const r26 = env23918 = { $parent: r3 }; env23918[0] = r25; env23918[1] = r24; r23 = new Proxy(r7, { get: $mk67383(r26) }); } } } return r23; }; } function $mk117564($env) { return function() { let env31859 = { $parent: $env }; env31859[0] = (0, $env.$parent.$parent[2])(function(c32087_0) { let recordOutputs, c32087_2, c32087_3, c32087_4; let c32087_6 = 0; let c32087_7 = 0; let c32087_5 = 0; return $createGenerator(function(a0) { let value2, value3; const r0 = c32087_7; if (r0 === 2) { c32087_7 = 3; HermesBuiltin.throwTypeError("Generator functions may not be called on executing generators"); throw new Error("unreachable"); } const value = arguments[1]; const r11 = env31859; const r10 = env31859.$parent.$parent; if (r0 === 3) { if (a0 === 1) { throw value; } if (a0 === 2) { return { value, done: true }; } return { value: void 0, done: true }; } while (true) { L23: { L21: { L11: { try { c32087_7 = 2; const r27 = c32087_6; if (r27 === 0) { if (a0 === 1) { c32087_7 = 3; throw value; } if (a0 === 2) { c32087_7 = 3; break L23; } let env32154; c32087_3 = r11; const r70 = env32154 = {}; c32087_2 = r70; env32154[0] = c32087_0; env32154[1] = void 0; c32087_5 = 1; if (r10[3].recordInputs && env31859.$parent[4]) { (0, env31859.$parent.$parent.$parent[4])(c32087_0, env31859.$parent[4]); } value3 = r10[0].apply(r10[2], env31859.$parent[0]); c32087_6 = 2; c32087_7 = 1; break L21; } if (r27 === 1) { const r21 = c32087_3; const r45 = c32087_4; c32087_5 = 0; env32154[2] = r45; const r46 = env32154[0]; const r48 = r21.$parent.$parent.$parent; r46.setStatus({ code: (0, r48[0])(r48[1][7]).SPAN_STATUS_ERROR, message: "internal_error" }); (0, r48[0])(r48[1][8]).captureException(env32154[2], { mechanism: { handled: false, type: "auto.ai.openai.stream", data: { function: r21.$parent.$parent[1] } } }); env32154[0].end(); throw env32154[2]; } if (a0 === 1) { c32087_7 = 3; throw value; } if (a0 === 2) { c32087_5 = 0; c32087_7 = 3; break L11; } const r28 = c32087_3; env32154[1] = value; const r16 = r28.$parent.$parent.$parent; const r18 = (0, r16[0])(r16[1][6]); const r32 = env32154[1]; const r33 = env32154[0]; const recordOutputs2 = r28.$parent.$parent[3].recordOutputs; recordOutputs = recordOutputs2; if (recordOutputs2 == null) { recordOutputs = false; } value2 = r18.instrumentStream(r32, r33, recordOutputs); c32087_5 = 0; c32087_7 = 3; } catch (r91) { c32087_4 = r91; if (c32087_5 === 0) { c32087_7 = 3; throw r91; } c32087_6 = 1; continue; } return { value: value2, done: true }; } return { value, done: true }; } return { value: value3, done: false }; } return { value, done: true }; } }); }); return function(a0) { return env31859[0].apply(this, arguments); }; }; } function $mk117565($env) { return function() { let env31860 = { $parent: $env }; env31860[0] = (0, $env.$parent.$parent[2])(function(c32088_0) { let c32088_1, c32088_2, c32088_3; let c32088_5 = 0; let c32088_6 = 0; let c32088_4 = 0; return $createGenerator(function(a0) { let value2, value3; const r0 = c32088_6; if (r0 === 2) { c32088_6 = 3; HermesBuiltin.throwTypeError("Generator functions may not be called on executing generators"); throw new Error("unreachable"); } const value = arguments[1]; const r10 = env31860; const r9 = env31860.$parent.$parent.$parent; const r8 = env31860.$parent.$parent; if (r0 === 3) { if (a0 === 1) { throw value; } if (a0 === 2) { return { value, done: true }; } return { value: void 0, done: true }; } while (true) { L21: { L19: { L9: { try { c32088_6 = 2; const r26 = c32088_5; if (r26 === 0) { if (a0 === 1) { c32088_6 = 3; throw value; } if (a0 === 2) { c32088_6 = 3; break L21; } let env32155; c32088_2 = r10; const r53 = env32155 = { $parent: r9 }; c32088_1 = r53; env32155[0] = c32088_0; env32155[1] = void 0; c32088_4 = 1; if (r8[3].recordInputs && env31860.$parent[4]) { (0, r9[4])(c32088_0, env31860.$parent[4]); } value3 = r8[0].apply(r8[2], env31860.$parent[0]); c32088_5 = 2; c32088_6 = 1; break L19; } if (r26 === 1) { const r20 = c32088_2; const r40 = c32088_3; c32088_4 = 0; env32155[2] = r40; const r41 = r20.$parent.$parent.$parent; (0, r41[0])(r41[1][8]).captureException(env32155[2], { mechanism: { handled: false, type: "auto.ai.openai", data: { function: r20.$parent.$parent[1] } } }); throw env32155[2]; } if (a0 === 1) { c32088_6 = 3; throw value; } if (a0 === 2) { c32088_4 = 0; c32088_6 = 3; break L9; } const r27 = c32088_1; const r14 = c32088_2; env32155[1] = value; const r17 = env32155[0]; const r16 = env32155[1]; const recordOutputs2 = r14.$parent.$parent[3].recordOutputs; $mk119071(r27.$parent)(r17, r16, recordOutputs2); value2 = env32155[1]; c32088_4 = 0; c32088_6 = 3; } catch (r73) { c32088_3 = r73; if (c32088_4 === 0) { c32088_6 = 3; throw r73; } c32088_5 = 1; continue; } return { value: value2, done: true }; } return { value, done: true }; } return { value: value3, done: false }; } return { value, done: true }; } }); }); return function(a0) { return env31860[0].apply(this, arguments); }; }; } function $mk119071($env) { return function addResponseAttributes(a0, a1, a2) { if (a1 && typeof a1 === "object") { const r5 = $env; const r6 = r5[0]; const r12 = r5[1]; const isChatCompletionResponse = r6(r12[2]).isChatCompletionResponse(a1); const r16 = r6(r12[2]); if (isChatCompletionResponse) { r16.addChatCompletionAttributes(a0, a1, a2); if (a2) { if (a1.choices?.length) { const r48 = a1.choices.map((choice) => choice.message?.content || ""); const setAttributes2 = a0.setAttributes; const r50 = {}; r50[r6(r12[1]).GEN_AI_RESPONSE_TEXT_ATTRIBUTE] = JSON.stringify(r48); setAttributes2.call(a0, r50); } } } else { const isResponsesApiResponse = r16.isResponsesApiResponse(a1); const r19 = r6(r12[2]); if (isResponsesApiResponse) { r19.addResponsesApiAttributes(a0, a1, a2); if (a2 ? a1.output_text : a2) { const setAttributes = a0.setAttributes; const r34 = {}; r34[r6(r12[1]).GEN_AI_RESPONSE_TEXT_ATTRIBUTE] = a1.output_text; setAttributes.call(a0, r34); } } else { const isEmbeddingsResponse = r19.isEmbeddingsResponse(a1); const r22 = r6(r12[2]); if (isEmbeddingsResponse) { r22.addEmbeddingsAttributes(a0, a1); } else if (r22.isConversationResponse(a1)) { r6(r12[2]).addConversationAttributes(a0, a1); } } } } }; } let addRequestAttributes; let env793 = {}; const extractRequestAttributes = function extractRequestAttributes(a0, a1) { const r4 = {}; r4[GEN_AI_SYSTEM_ATTRIBUTE] = "openai"; r4[GEN_AI_OPERATION_NAME_ATTRIBUTE] = getOperationName(a1); r4[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] = "auto.ai.openai"; if (a0.length > 0 && (typeof a0[0] === "object" && a0[0] !== null)) { const r36 = a0[0]; const r12 = [...Array.isArray(r36.tools) ? r36.tools : [], ...r36.web_search_options && typeof r36.web_search_options === "object" ? [{ type: "web_search_options", ...r36.web_search_options }] : []]; const r57 = r12.length > 0 ? JSON.stringify(r12) : void 0; if (r57) { r4[GEN_AI_REQUEST_AVAILABLE_TOOLS_ATTRIBUTE] = r57; } Object.assign(r4, extractRequestParameters(r36)); } else { r4[GEN_AI_REQUEST_MODEL_ATTRIBUTE] = "unknown"; } return r4; }; addRequestAttributes = function addRequestAttributes(a0, a1) { const r11 = "input" in a1 ? a1.input : "messages" in a1 ? a1.messages : void 0; const isArray = Array.isArray(r11); const r5 = isArray ? r11.length : void 0; if (r11 && r5 !== 0) { a0.setAttribute(GEN_AI_REQUEST_MESSAGES_ATTRIBUTE, getTruncatedJsonString(r11)); if (r5) { a0.setAttribute(GEN_AI_REQUEST_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE, r5); } } }; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); export function instrumentOpenAiClient(a0, a1) { const r6 = Boolean; const client = getClient(); const r15 = client == null ? void 0 : client.getOptions().sendDefaultPii; let env12596; const recordInputs = r6(r15); const r20 = { recordInputs, recordOutputs: recordInputs, ...a1 }; const r22 = env12596 = { $parent: env793 }; env12596[0] = ""; env12596[1] = r20; return new Proxy(a0, { get: $mk67383(r22) }); }