180 lines
6.0 KiB
JavaScript
180 lines
6.0 KiB
JavaScript
// Metro module 982 (f983).
|
|
import { GEN_AI_RESPONSE_MODEL_ATTRIBUTE, GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE, GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE, GEN_AI_RESPONSE_TEXT_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE } from "./Module958";
|
|
import { normalizeLangChainMessages } from "./asString";
|
|
let extractTokenUsageFromMessage, extractModelMetadata;
|
|
const extractToolCalls = function extractToolCalls(a0) {
|
|
if (a0 && a0.length !== 0) {
|
|
const r25 = [];
|
|
for (const item of a0) {
|
|
if (item && typeof item === "object") {
|
|
let tool_calls = item.tool_calls;
|
|
const r33 = tool_calls;
|
|
if (tool_calls) {
|
|
tool_calls = Array.isArray(r33);
|
|
}
|
|
if (tool_calls) {
|
|
r25.push.apply(r25, [...r33]);
|
|
}
|
|
}
|
|
}
|
|
return r25.length > 0 ? r25 : null;
|
|
}
|
|
return null;
|
|
};
|
|
extractTokenUsageFromMessage = function extractTokenUsageFromMessage(a0) {
|
|
if (a0.usage_metadata && typeof a0.usage_metadata === "object") {
|
|
const usage_metadata = a0.usage_metadata;
|
|
return {
|
|
inputTokens: typeof usage_metadata.input_tokens === "number" ? usage_metadata.input_tokens : 0,
|
|
outputTokens: typeof usage_metadata.output_tokens === "number" ? usage_metadata.output_tokens : 0,
|
|
totalTokens: typeof usage_metadata.total_tokens === "number" ? usage_metadata.total_tokens : 0
|
|
};
|
|
}
|
|
const response_metadata = a0.response_metadata;
|
|
let totalTokens2 = 0;
|
|
let outputTokens = 0;
|
|
let inputTokens = 0;
|
|
if (response_metadata) {
|
|
const response_metadata2 = a0.response_metadata;
|
|
totalTokens2 = 0;
|
|
outputTokens = 0;
|
|
inputTokens = 0;
|
|
if (typeof response_metadata2 === "object") {
|
|
const response_metadata3 = a0.response_metadata;
|
|
const tokenUsage = response_metadata3.tokenUsage;
|
|
totalTokens2 = 0;
|
|
outputTokens = 0;
|
|
inputTokens = 0;
|
|
if (tokenUsage) {
|
|
const tokenUsage3 = response_metadata3.tokenUsage;
|
|
totalTokens2 = 0;
|
|
outputTokens = 0;
|
|
inputTokens = 0;
|
|
if (typeof tokenUsage3 === "object") {
|
|
const tokenUsage2 = response_metadata3.tokenUsage;
|
|
const r15 = typeof tokenUsage2.promptTokens === "number" ? tokenUsage2.promptTokens : 0;
|
|
const r16 = typeof tokenUsage2.completionTokens === "number" ? tokenUsage2.completionTokens : 0;
|
|
const totalTokens = tokenUsage2.totalTokens;
|
|
totalTokens2 = 0;
|
|
outputTokens = r16;
|
|
inputTokens = r15;
|
|
if (typeof totalTokens === "number") {
|
|
totalTokens2 = tokenUsage2.totalTokens;
|
|
outputTokens = r16;
|
|
inputTokens = r15;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
inputTokens,
|
|
outputTokens,
|
|
totalTokens: totalTokens2
|
|
};
|
|
};
|
|
extractModelMetadata = function extractModelMetadata(a0, a1) {
|
|
if (a1.response_metadata && typeof a1.response_metadata === "object") {
|
|
const response_metadata = a1.response_metadata;
|
|
if (response_metadata.model_name && typeof response_metadata.model_name === "string") {
|
|
a0.setAttribute(GEN_AI_RESPONSE_MODEL_ATTRIBUTE, response_metadata.model_name);
|
|
}
|
|
if (response_metadata.finish_reason && typeof response_metadata.finish_reason === "string") {
|
|
a0.setAttribute(GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE, [response_metadata.finish_reason]);
|
|
}
|
|
}
|
|
};
|
|
Object.defineProperty(exports, Symbol.toStringTag, {
|
|
value: "Module"
|
|
});
|
|
export { extractModelMetadata };
|
|
export { extractTokenUsageFromMessage };
|
|
export { extractToolCalls };
|
|
export function extractToolsFromCompiledGraph(a0) {
|
|
const builder = a0.builder;
|
|
const r3 = builder == null;
|
|
let r5 = void 0;
|
|
if (!r3) {
|
|
const nodes = builder.nodes;
|
|
const r9 = nodes == null;
|
|
r5 = void 0;
|
|
if (!r9) {
|
|
const tools = nodes.tools;
|
|
r5 = tools == null ? void 0 : tools.runnable?.tools;
|
|
}
|
|
}
|
|
if (r5) {
|
|
const builder2 = a0.builder;
|
|
const r15 = builder2 == null;
|
|
let r7 = void 0;
|
|
if (!r15) {
|
|
const nodes2 = builder2.nodes;
|
|
const r17 = nodes2 == null;
|
|
r7 = void 0;
|
|
if (!r17) {
|
|
const tools2 = nodes2.tools;
|
|
r7 = tools2 == null ? void 0 : tools2.runnable?.tools;
|
|
}
|
|
}
|
|
return r7 ? Array.isArray(r7) ? r7.length !== 0 ? r7.map((item) => ({
|
|
name: item.lc_kwargs?.name,
|
|
description: item.lc_kwargs?.description,
|
|
schema: item.lc_kwargs?.schema
|
|
})) : null : null : null;
|
|
}
|
|
return null;
|
|
}
|
|
export function setResponseAttributes(a0, a1, a2) {
|
|
let r25;
|
|
const messages = a2?.messages;
|
|
if (messages && Array.isArray(messages)) {
|
|
let r30 = a1?.length;
|
|
if (r30 == null) {
|
|
r30 = 0;
|
|
}
|
|
const r32 = messages.length > r30 ? messages.slice(r30) : [];
|
|
if (r32.length !== 0) {
|
|
const r13 = extractToolCalls(r32);
|
|
if (r13) {
|
|
a0.setAttribute(GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE, JSON.stringify(r13));
|
|
}
|
|
a0.setAttribute(GEN_AI_RESPONSE_TEXT_ATTRIBUTE, JSON.stringify(normalizeLangChainMessages(r32)));
|
|
let r60 = 0;
|
|
let r61 = 0;
|
|
let r62 = 0;
|
|
let r64 = r32[Symbol.iterator]();
|
|
const next = r64.next;
|
|
while (true) {
|
|
const r67 = r64 === void 0 ? void 0 : (r25 = next.call(r64)).done ? r64 = void 0 : r25.value;
|
|
if (r64 === void 0) {
|
|
if (r60 > 0) {
|
|
a0.setAttribute(GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, r60);
|
|
}
|
|
if (r61 > 0) {
|
|
a0.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, r61);
|
|
}
|
|
if (!(r62 > 0)) {
|
|
break;
|
|
}
|
|
a0.setAttribute(GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE, r62);
|
|
break;
|
|
}
|
|
try {
|
|
const r70 = extractTokenUsageFromMessage(r67);
|
|
r60 = r60 + r70.inputTokens;
|
|
r61 = r61 + r70.outputTokens;
|
|
r62 = r62 + r70.totalTokens;
|
|
extractModelMetadata(a0, r67);
|
|
} catch (r79) {
|
|
if (r64 !== void 0 && typeof r64.return === "function") {
|
|
try {
|
|
r64.return();
|
|
} catch {}
|
|
}
|
|
throw r79;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|