netra-sdk 1.10.1 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +164 -1107
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +117 -1060
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -253,10 +253,10 @@ var UsageHttpClient = class extends NetraHttpClient {
|
|
|
253
253
|
user_id: params.userId,
|
|
254
254
|
tenant_id: params.tenantId
|
|
255
255
|
};
|
|
256
|
-
for (const [
|
|
256
|
+
for (const [field, value] of Object.entries(filterMapping)) {
|
|
257
257
|
if (value !== void 0) {
|
|
258
258
|
filters.push({
|
|
259
|
-
field
|
|
259
|
+
field,
|
|
260
260
|
operator: "equals",
|
|
261
261
|
type: "string",
|
|
262
262
|
value
|
|
@@ -465,8 +465,8 @@ var Usage = class {
|
|
|
465
465
|
if (!page) {
|
|
466
466
|
break;
|
|
467
467
|
}
|
|
468
|
-
for (const
|
|
469
|
-
yield
|
|
468
|
+
for (const trace24 of page.traces) {
|
|
469
|
+
yield trace24;
|
|
470
470
|
}
|
|
471
471
|
if (!page.hasNextPage || !page.nextCursor) {
|
|
472
472
|
break;
|
|
@@ -579,7 +579,7 @@ var Usage = class {
|
|
|
579
579
|
};
|
|
580
580
|
|
|
581
581
|
// src/version.ts
|
|
582
|
-
var SDK_VERSION = "1.
|
|
582
|
+
var SDK_VERSION = "1.11.0";
|
|
583
583
|
|
|
584
584
|
// src/config.ts
|
|
585
585
|
var NetraInstruments = /* @__PURE__ */ ((NetraInstruments2) => {
|
|
@@ -1943,13 +1943,13 @@ async function runSingleEvaluator(params) {
|
|
|
1943
1943
|
if (config) {
|
|
1944
1944
|
expectedName = config.name;
|
|
1945
1945
|
}
|
|
1946
|
-
const
|
|
1946
|
+
const context16 = {
|
|
1947
1947
|
input: itemInput,
|
|
1948
1948
|
taskOutput,
|
|
1949
1949
|
expectedOutput,
|
|
1950
1950
|
metadata
|
|
1951
1951
|
};
|
|
1952
|
-
let result = evaluator.evaluate(
|
|
1952
|
+
let result = evaluator.evaluate(context16);
|
|
1953
1953
|
if (result instanceof Promise) {
|
|
1954
1954
|
result = await result;
|
|
1955
1955
|
}
|
|
@@ -3946,14 +3946,14 @@ var SpanIOProcessor = class {
|
|
|
3946
3946
|
if (promptMatch) {
|
|
3947
3947
|
original(key, value);
|
|
3948
3948
|
const idx = parseInt(promptMatch[1], 10);
|
|
3949
|
-
const
|
|
3949
|
+
const field = promptMatch[2];
|
|
3950
3950
|
if (!prompts) prompts = /* @__PURE__ */ new Map();
|
|
3951
3951
|
let entry = prompts.get(idx);
|
|
3952
3952
|
if (!entry) {
|
|
3953
3953
|
entry = {};
|
|
3954
3954
|
prompts.set(idx, entry);
|
|
3955
3955
|
}
|
|
3956
|
-
entry[
|
|
3956
|
+
entry[field] = String(value);
|
|
3957
3957
|
if (!inputLocked() && (inputIsEmpty() || genAiOwnsInput)) {
|
|
3958
3958
|
original("input", buildMessages(prompts));
|
|
3959
3959
|
genAiOwnsInput = true;
|
|
@@ -3964,14 +3964,14 @@ var SpanIOProcessor = class {
|
|
|
3964
3964
|
if (completionMatch) {
|
|
3965
3965
|
original(key, value);
|
|
3966
3966
|
const idx = parseInt(completionMatch[1], 10);
|
|
3967
|
-
const
|
|
3967
|
+
const field = completionMatch[2];
|
|
3968
3968
|
if (!completions) completions = /* @__PURE__ */ new Map();
|
|
3969
3969
|
let entry = completions.get(idx);
|
|
3970
3970
|
if (!entry) {
|
|
3971
3971
|
entry = {};
|
|
3972
3972
|
completions.set(idx, entry);
|
|
3973
3973
|
}
|
|
3974
|
-
entry[
|
|
3974
|
+
entry[field] = String(value);
|
|
3975
3975
|
if (!outputLocked() && (outputIsEmpty() || genAiOwnsOutput)) {
|
|
3976
3976
|
original("output", buildMessages(completions));
|
|
3977
3977
|
genAiOwnsOutput = true;
|
|
@@ -4231,6 +4231,8 @@ var SpanAttributes2 = {
|
|
|
4231
4231
|
LLM_USAGE_COMPLETION_TOKENS: "gen_ai.usage.completion_tokens",
|
|
4232
4232
|
LLM_USAGE_CACHE_READ_INPUT_TOKENS: "gen_ai.usage.cache_read_input_tokens",
|
|
4233
4233
|
LLM_USAGE_CACHE_CREATION_INPUT_TOKENS: "gen_ai.usage.cache_creation_input_tokens",
|
|
4234
|
+
LLM_USAGE_CACHE_CREATION_INPUT_TOKENS_30M: "gen_ai.usage.cache_creation_input_tokens_30m",
|
|
4235
|
+
LLM_USAGE_CACHE_CREATION_INPUT_TOKENS_12H: "gen_ai.usage.cache_creation_input_tokens_12h",
|
|
4234
4236
|
LLM_USAGE_REASONING_TOKENS: "gen_ai.usage.reasoning_tokens",
|
|
4235
4237
|
LLM_USAGE_TOTAL_TOKENS: "llm.usage.total_tokens",
|
|
4236
4238
|
LLM_FREQUENCY_PENALTY: "llm.frequency_penalty",
|
|
@@ -4238,33 +4240,7 @@ var SpanAttributes2 = {
|
|
|
4238
4240
|
LLM_CHAT_STOP_SEQUENCES: "llm.chat.stop_sequences",
|
|
4239
4241
|
LLM_IS_STREAMING: "llm.is_streaming",
|
|
4240
4242
|
LLM_COMPLETIONS: "gen_ai.completion",
|
|
4241
|
-
LLM_PROMPTS: "gen_ai.prompt"
|
|
4242
|
-
// Google GenAI Interactions API. Its request is flat snake_case and its
|
|
4243
|
-
// response is a multi-step Interaction, so it needs names the chat/embedding
|
|
4244
|
-
// surfaces have no equivalent for.
|
|
4245
|
-
LLM_REQUEST_AGENT: "gen_ai.request.agent",
|
|
4246
|
-
LLM_REQUEST_AGENT_CONFIG: "gen_ai.request.agent_config",
|
|
4247
|
-
LLM_REQUEST_SEED: "gen_ai.request.seed",
|
|
4248
|
-
LLM_REQUEST_THINKING_SUMMARIES: "gen_ai.request.thinking_summaries",
|
|
4249
|
-
LLM_REQUEST_TOOL_CHOICE: "llm.request.tool_choice",
|
|
4250
|
-
LLM_REQUEST_TOOLS: "llm.request.functions",
|
|
4251
|
-
LLM_REQUEST_STORE: "gen_ai.request.store",
|
|
4252
|
-
LLM_REQUEST_BACKGROUND: "gen_ai.request.background",
|
|
4253
|
-
LLM_REQUEST_PREVIOUS_INTERACTION_ID: "gen_ai.request.previous_interaction_id",
|
|
4254
|
-
LLM_REQUEST_RESPONSE_FORMAT: "gen_ai.request.response_format",
|
|
4255
|
-
LLM_REQUEST_RESPONSE_MIME_TYPE: "gen_ai.request.response_mime_type",
|
|
4256
|
-
LLM_REQUEST_RESPONSE_MODALITIES: "gen_ai.request.response_modalities",
|
|
4257
|
-
LLM_REQUEST_SERVICE_TIER: "gen_ai.request.service_tier",
|
|
4258
|
-
LLM_RESPONSE_ID: "gen_ai.response.id",
|
|
4259
|
-
LLM_RESPONSE_STATUS: "gen_ai.response.status",
|
|
4260
|
-
LLM_RESPONSE_PREVIOUS_INTERACTION_ID: "gen_ai.response.previous_interaction_id",
|
|
4261
|
-
LLM_RESPONSE_REASONING: "llm.response.reasoning",
|
|
4262
|
-
LLM_INTERACTION_STEP_COUNT: "gen_ai.interaction.step_count",
|
|
4263
|
-
LLM_USAGE_TOOL_USE_TOKENS: "gen_ai.usage.tool_use_tokens",
|
|
4264
|
-
LLM_USAGE_GROUNDING_TOOL_COUNT: "gen_ai.usage.grounding_tool_count",
|
|
4265
|
-
// Set when a stream is consumed through a path we cannot observe
|
|
4266
|
-
// (getReader/pipeTo/pipeThrough/tee), so the span closes instead of leaking.
|
|
4267
|
-
STREAM_UNOBSERVED: "netra.stream.unobserved"
|
|
4243
|
+
LLM_PROMPTS: "gen_ai.prompt"
|
|
4268
4244
|
};
|
|
4269
4245
|
var VALID_NATIVE_TRACING_MODES = /* @__PURE__ */ new Set(["both", "netra", "netra-strict"]);
|
|
4270
4246
|
function parseNativeTracingEnv(name) {
|
|
@@ -4594,6 +4570,19 @@ function setFinishReason(span2, response) {
|
|
|
4594
4570
|
}
|
|
4595
4571
|
}
|
|
4596
4572
|
}
|
|
4573
|
+
var CACHE_WRITE_TTL_ATTRIBUTES = {
|
|
4574
|
+
ttl_30m_tokens: SpanAttributes2.LLM_USAGE_CACHE_CREATION_INPUT_TOKENS_30M,
|
|
4575
|
+
ttl_12h_tokens: SpanAttributes2.LLM_USAGE_CACHE_CREATION_INPUT_TOKENS_12H
|
|
4576
|
+
};
|
|
4577
|
+
function setCacheWriteTtlAttributes(span2, inputTokensDetails) {
|
|
4578
|
+
if (!isDict(inputTokensDetails)) return;
|
|
4579
|
+
const breakdown = inputTokensDetails.cache_write_tokens_details;
|
|
4580
|
+
if (!isDict(breakdown)) return;
|
|
4581
|
+
for (const [field, attribute] of Object.entries(CACHE_WRITE_TTL_ATTRIBUTES)) {
|
|
4582
|
+
const tokens = breakdown[field];
|
|
4583
|
+
if (typeof tokens === "number") span2.setAttribute(attribute, tokens);
|
|
4584
|
+
}
|
|
4585
|
+
}
|
|
4597
4586
|
function setUsageAttributes(span2, response) {
|
|
4598
4587
|
const usage = response.usage ?? response.usage_metadata;
|
|
4599
4588
|
if (!usage) return;
|
|
@@ -4632,6 +4621,10 @@ function setUsageAttributes(span2, response) {
|
|
|
4632
4621
|
Number(cacheCreationTokens)
|
|
4633
4622
|
);
|
|
4634
4623
|
}
|
|
4624
|
+
setCacheWriteTtlAttributes(
|
|
4625
|
+
span2,
|
|
4626
|
+
usage.prompt_tokens_details ?? usage.input_tokens_details
|
|
4627
|
+
);
|
|
4635
4628
|
const reasoningTokens = (usage.completion_tokens_details ?? usage.output_tokens_details)?.reasoning_tokens;
|
|
4636
4629
|
if (reasoningTokens !== void 0) {
|
|
4637
4630
|
span2.setAttribute(
|
|
@@ -5781,6 +5774,15 @@ var BaseInstrumentor = class {
|
|
|
5781
5774
|
}
|
|
5782
5775
|
};
|
|
5783
5776
|
|
|
5777
|
+
// src/instrumentation/google-genai/version.ts
|
|
5778
|
+
var __version__2 = "1.0.0";
|
|
5779
|
+
|
|
5780
|
+
// src/instrumentation/google-genai/types.ts
|
|
5781
|
+
var SPAN_NAMES2 = {
|
|
5782
|
+
CHAT: "google_genai.chat",
|
|
5783
|
+
EMBEDDING: "google_genai.embedding"
|
|
5784
|
+
};
|
|
5785
|
+
|
|
5784
5786
|
// src/instrumentation/google-genai/utils.ts
|
|
5785
5787
|
var LOG_PREFIX = "netra.instrumentation.google_genai";
|
|
5786
5788
|
function setRequestAttributes3(span2, params, requestType) {
|
|
@@ -6163,880 +6165,8 @@ function buildAccumulatedResponse(accumulated) {
|
|
|
6163
6165
|
return response;
|
|
6164
6166
|
}
|
|
6165
6167
|
|
|
6166
|
-
// src/instrumentation/google-genai/
|
|
6168
|
+
// src/instrumentation/google-genai/wrappers.ts
|
|
6167
6169
|
var LOG_PREFIX2 = "netra.instrumentation.google_genai";
|
|
6168
|
-
var CONTENT_TYPES = /* @__PURE__ */ new Set([
|
|
6169
|
-
"text",
|
|
6170
|
-
"image",
|
|
6171
|
-
"audio",
|
|
6172
|
-
"document",
|
|
6173
|
-
"video"
|
|
6174
|
-
]);
|
|
6175
|
-
var SERVER_TOOL_CALL_STEPS = /* @__PURE__ */ new Set([
|
|
6176
|
-
"code_execution_call",
|
|
6177
|
-
"url_context_call",
|
|
6178
|
-
"google_search_call",
|
|
6179
|
-
"google_maps_call",
|
|
6180
|
-
"file_search_call",
|
|
6181
|
-
"mcp_server_tool_call"
|
|
6182
|
-
]);
|
|
6183
|
-
var SERVER_TOOL_RESULT_STEPS = /* @__PURE__ */ new Set([
|
|
6184
|
-
"code_execution_result",
|
|
6185
|
-
"url_context_result",
|
|
6186
|
-
"google_search_result",
|
|
6187
|
-
"google_maps_result",
|
|
6188
|
-
"file_search_result",
|
|
6189
|
-
"mcp_server_tool_result"
|
|
6190
|
-
]);
|
|
6191
|
-
var STEP_TYPES = /* @__PURE__ */ new Set([
|
|
6192
|
-
"user_input",
|
|
6193
|
-
"model_output",
|
|
6194
|
-
"thought",
|
|
6195
|
-
"function_call",
|
|
6196
|
-
"function_result",
|
|
6197
|
-
...SERVER_TOOL_CALL_STEPS,
|
|
6198
|
-
...SERVER_TOOL_RESULT_STEPS
|
|
6199
|
-
]);
|
|
6200
|
-
function field(obj, name) {
|
|
6201
|
-
if (!isDict(obj)) return void 0;
|
|
6202
|
-
return obj[name];
|
|
6203
|
-
}
|
|
6204
|
-
function asArray(value) {
|
|
6205
|
-
if (value === null || value === void 0) return [];
|
|
6206
|
-
return Array.isArray(value) ? value : [value];
|
|
6207
|
-
}
|
|
6208
|
-
function contentText(items) {
|
|
6209
|
-
const parts = [];
|
|
6210
|
-
for (const item of asArray(items)) {
|
|
6211
|
-
const itemType = field(item, "type");
|
|
6212
|
-
if (itemType === "text") {
|
|
6213
|
-
const text = field(item, "text");
|
|
6214
|
-
if (typeof text === "string") parts.push(text);
|
|
6215
|
-
} else if (typeof itemType === "string" && CONTENT_TYPES.has(itemType)) {
|
|
6216
|
-
parts.push(`<${itemType}>`);
|
|
6217
|
-
}
|
|
6218
|
-
}
|
|
6219
|
-
return parts.join("");
|
|
6220
|
-
}
|
|
6221
|
-
function contentModalities(items) {
|
|
6222
|
-
const modalities = [];
|
|
6223
|
-
for (const item of asArray(items)) {
|
|
6224
|
-
const itemType = field(item, "type");
|
|
6225
|
-
if (typeof itemType === "string" && CONTENT_TYPES.has(itemType) && itemType !== "text" && !modalities.includes(itemType)) {
|
|
6226
|
-
modalities.push(itemType);
|
|
6227
|
-
}
|
|
6228
|
-
}
|
|
6229
|
-
return modalities;
|
|
6230
|
-
}
|
|
6231
|
-
function stepMessage(step) {
|
|
6232
|
-
const stepType = field(step, "type");
|
|
6233
|
-
if (typeof stepType !== "string") return null;
|
|
6234
|
-
if (stepType === "user_input") {
|
|
6235
|
-
return {
|
|
6236
|
-
role: "user",
|
|
6237
|
-
content: contentText(field(step, "content")),
|
|
6238
|
-
stepType
|
|
6239
|
-
};
|
|
6240
|
-
}
|
|
6241
|
-
if (stepType === "model_output") {
|
|
6242
|
-
const content = field(step, "content");
|
|
6243
|
-
return {
|
|
6244
|
-
role: "assistant",
|
|
6245
|
-
content: contentText(content),
|
|
6246
|
-
stepType,
|
|
6247
|
-
modalities: contentModalities(content)
|
|
6248
|
-
};
|
|
6249
|
-
}
|
|
6250
|
-
if (stepType === "thought") {
|
|
6251
|
-
return {
|
|
6252
|
-
role: "assistant",
|
|
6253
|
-
content: contentText(field(step, "summary")),
|
|
6254
|
-
stepType
|
|
6255
|
-
};
|
|
6256
|
-
}
|
|
6257
|
-
if (stepType === "function_call") {
|
|
6258
|
-
const toolCall = {
|
|
6259
|
-
name: field(step, "name"),
|
|
6260
|
-
id: field(step, "id"),
|
|
6261
|
-
arguments: field(step, "arguments")
|
|
6262
|
-
};
|
|
6263
|
-
return {
|
|
6264
|
-
role: "assistant",
|
|
6265
|
-
content: safeStringify(toolCall),
|
|
6266
|
-
stepType,
|
|
6267
|
-
toolCall
|
|
6268
|
-
};
|
|
6269
|
-
}
|
|
6270
|
-
if (stepType === "function_result") {
|
|
6271
|
-
const name = field(step, "name");
|
|
6272
|
-
return {
|
|
6273
|
-
role: "tool",
|
|
6274
|
-
content: safeStringify({
|
|
6275
|
-
call_id: field(step, "call_id"),
|
|
6276
|
-
name,
|
|
6277
|
-
result: field(step, "result"),
|
|
6278
|
-
is_error: field(step, "is_error")
|
|
6279
|
-
}),
|
|
6280
|
-
stepType,
|
|
6281
|
-
toolCall: { name }
|
|
6282
|
-
};
|
|
6283
|
-
}
|
|
6284
|
-
if (SERVER_TOOL_CALL_STEPS.has(stepType)) {
|
|
6285
|
-
return {
|
|
6286
|
-
role: "tool",
|
|
6287
|
-
content: safeStringify({
|
|
6288
|
-
name: stepType,
|
|
6289
|
-
arguments: field(step, "arguments")
|
|
6290
|
-
}),
|
|
6291
|
-
stepType,
|
|
6292
|
-
toolCall: { name: stepType }
|
|
6293
|
-
};
|
|
6294
|
-
}
|
|
6295
|
-
if (SERVER_TOOL_RESULT_STEPS.has(stepType)) {
|
|
6296
|
-
return {
|
|
6297
|
-
role: "tool",
|
|
6298
|
-
content: safeStringify({ name: stepType, result: field(step, "result") }),
|
|
6299
|
-
stepType,
|
|
6300
|
-
toolCall: { name: stepType }
|
|
6301
|
-
};
|
|
6302
|
-
}
|
|
6303
|
-
return { role: "assistant", content: safeStringify(step), stepType };
|
|
6304
|
-
}
|
|
6305
|
-
function inputMessages(value) {
|
|
6306
|
-
if (value === null || value === void 0) return [];
|
|
6307
|
-
if (typeof value === "string") {
|
|
6308
|
-
return [{ role: "user", content: value }];
|
|
6309
|
-
}
|
|
6310
|
-
if (Array.isArray(value)) {
|
|
6311
|
-
if (value.length === 0) return [];
|
|
6312
|
-
const types = value.map((item) => field(item, "type"));
|
|
6313
|
-
if (types.every((t) => typeof t === "string" && STEP_TYPES.has(t))) {
|
|
6314
|
-
const messages = [];
|
|
6315
|
-
for (const item of value) {
|
|
6316
|
-
const message = stepMessage(item);
|
|
6317
|
-
if (message && message.stepType !== "thought" && message.content) {
|
|
6318
|
-
messages.push(message);
|
|
6319
|
-
}
|
|
6320
|
-
}
|
|
6321
|
-
return messages;
|
|
6322
|
-
}
|
|
6323
|
-
if (types.every((t) => typeof t === "string" && CONTENT_TYPES.has(t))) {
|
|
6324
|
-
return [{ role: "user", content: contentText(value) }];
|
|
6325
|
-
}
|
|
6326
|
-
return [{ role: "user", content: safeStringify(value) }];
|
|
6327
|
-
}
|
|
6328
|
-
const valueType = field(value, "type");
|
|
6329
|
-
if (typeof valueType === "string" && CONTENT_TYPES.has(valueType)) {
|
|
6330
|
-
return [{ role: "user", content: contentText([value]) }];
|
|
6331
|
-
}
|
|
6332
|
-
if (typeof valueType === "string" && STEP_TYPES.has(valueType)) {
|
|
6333
|
-
const message = stepMessage(value);
|
|
6334
|
-
return message && message.content ? [message] : [];
|
|
6335
|
-
}
|
|
6336
|
-
return [{ role: "user", content: safeStringify(value) }];
|
|
6337
|
-
}
|
|
6338
|
-
function setToolDefinitions(span2, tools) {
|
|
6339
|
-
if (!Array.isArray(tools)) return;
|
|
6340
|
-
tools.forEach((tool, index) => {
|
|
6341
|
-
const toolType = field(tool, "type");
|
|
6342
|
-
const prefix = `${SpanAttributes2.LLM_REQUEST_TOOLS}.${index}`;
|
|
6343
|
-
if (toolType === "function") {
|
|
6344
|
-
const fn = field(tool, "function") ?? tool;
|
|
6345
|
-
span2.setAttribute(`${prefix}.name`, String(field(fn, "name") ?? ""));
|
|
6346
|
-
const description = field(fn, "description");
|
|
6347
|
-
if (description !== void 0) {
|
|
6348
|
-
span2.setAttribute(`${prefix}.description`, String(description));
|
|
6349
|
-
}
|
|
6350
|
-
const parameters = field(fn, "parameters");
|
|
6351
|
-
if (parameters !== void 0) {
|
|
6352
|
-
span2.setAttribute(`${prefix}.parameters`, safeStringify(parameters));
|
|
6353
|
-
}
|
|
6354
|
-
} else if (typeof toolType === "string") {
|
|
6355
|
-
span2.setAttribute(`${prefix}.name`, toolType);
|
|
6356
|
-
}
|
|
6357
|
-
});
|
|
6358
|
-
}
|
|
6359
|
-
function setInteractionRequestAttributes(span2, params) {
|
|
6360
|
-
try {
|
|
6361
|
-
if (!span2.isRecording()) return;
|
|
6362
|
-
span2.setAttribute(SpanAttributes2.LLM_REQUEST_TYPE, "chat");
|
|
6363
|
-
span2.setAttribute(SpanAttributes2.LLM_SYSTEM, "google_genai");
|
|
6364
|
-
if (params.model) {
|
|
6365
|
-
span2.setAttribute(
|
|
6366
|
-
SpanAttributes2.LLM_REQUEST_MODEL,
|
|
6367
|
-
extractModelName(String(params.model))
|
|
6368
|
-
);
|
|
6369
|
-
}
|
|
6370
|
-
if (params.agent !== void 0) {
|
|
6371
|
-
span2.setAttribute(SpanAttributes2.LLM_REQUEST_AGENT, String(params.agent));
|
|
6372
|
-
}
|
|
6373
|
-
if (params.agent_config !== void 0) {
|
|
6374
|
-
span2.setAttribute(
|
|
6375
|
-
SpanAttributes2.LLM_REQUEST_AGENT_CONFIG,
|
|
6376
|
-
safeStringify(params.agent_config)
|
|
6377
|
-
);
|
|
6378
|
-
}
|
|
6379
|
-
const cfg = params.generation_config;
|
|
6380
|
-
if (isDict(cfg)) {
|
|
6381
|
-
if (cfg.temperature !== void 0) {
|
|
6382
|
-
span2.setAttribute(
|
|
6383
|
-
SpanAttributes2.LLM_REQUEST_TEMPERATURE,
|
|
6384
|
-
Number(cfg.temperature)
|
|
6385
|
-
);
|
|
6386
|
-
}
|
|
6387
|
-
if (cfg.top_p !== void 0) {
|
|
6388
|
-
span2.setAttribute(SpanAttributes2.LLM_REQUEST_TOP_P, Number(cfg.top_p));
|
|
6389
|
-
}
|
|
6390
|
-
if (cfg.max_output_tokens !== void 0) {
|
|
6391
|
-
span2.setAttribute(
|
|
6392
|
-
SpanAttributes2.LLM_REQUEST_MAX_TOKENS,
|
|
6393
|
-
Number(cfg.max_output_tokens)
|
|
6394
|
-
);
|
|
6395
|
-
}
|
|
6396
|
-
if (cfg.seed !== void 0) {
|
|
6397
|
-
span2.setAttribute(SpanAttributes2.LLM_REQUEST_SEED, Number(cfg.seed));
|
|
6398
|
-
}
|
|
6399
|
-
if (cfg.stop_sequences !== void 0) {
|
|
6400
|
-
span2.setAttribute(
|
|
6401
|
-
SpanAttributes2.LLM_CHAT_STOP_SEQUENCES,
|
|
6402
|
-
safeStringify(cfg.stop_sequences)
|
|
6403
|
-
);
|
|
6404
|
-
}
|
|
6405
|
-
if (cfg.thinking_level !== void 0) {
|
|
6406
|
-
span2.setAttribute(
|
|
6407
|
-
SpanAttributes2.LLM_REQUEST_REASONING_EFFORT,
|
|
6408
|
-
String(cfg.thinking_level)
|
|
6409
|
-
);
|
|
6410
|
-
}
|
|
6411
|
-
if (cfg.thinking_summaries !== void 0) {
|
|
6412
|
-
span2.setAttribute(
|
|
6413
|
-
SpanAttributes2.LLM_REQUEST_THINKING_SUMMARIES,
|
|
6414
|
-
String(cfg.thinking_summaries)
|
|
6415
|
-
);
|
|
6416
|
-
}
|
|
6417
|
-
if (cfg.tool_choice !== void 0) {
|
|
6418
|
-
span2.setAttribute(
|
|
6419
|
-
SpanAttributes2.LLM_REQUEST_TOOL_CHOICE,
|
|
6420
|
-
safeStringify(cfg.tool_choice)
|
|
6421
|
-
);
|
|
6422
|
-
}
|
|
6423
|
-
}
|
|
6424
|
-
setToolDefinitions(span2, params.tools);
|
|
6425
|
-
if (typeof params.store === "boolean") {
|
|
6426
|
-
span2.setAttribute(SpanAttributes2.LLM_REQUEST_STORE, params.store);
|
|
6427
|
-
}
|
|
6428
|
-
if (typeof params.background === "boolean") {
|
|
6429
|
-
span2.setAttribute(
|
|
6430
|
-
SpanAttributes2.LLM_REQUEST_BACKGROUND,
|
|
6431
|
-
params.background
|
|
6432
|
-
);
|
|
6433
|
-
}
|
|
6434
|
-
if (params.previous_interaction_id !== void 0) {
|
|
6435
|
-
span2.setAttribute(
|
|
6436
|
-
SpanAttributes2.LLM_REQUEST_PREVIOUS_INTERACTION_ID,
|
|
6437
|
-
String(params.previous_interaction_id)
|
|
6438
|
-
);
|
|
6439
|
-
}
|
|
6440
|
-
if (params.response_mime_type !== void 0) {
|
|
6441
|
-
span2.setAttribute(
|
|
6442
|
-
SpanAttributes2.LLM_REQUEST_RESPONSE_MIME_TYPE,
|
|
6443
|
-
String(params.response_mime_type)
|
|
6444
|
-
);
|
|
6445
|
-
}
|
|
6446
|
-
if (params.service_tier !== void 0) {
|
|
6447
|
-
span2.setAttribute(
|
|
6448
|
-
SpanAttributes2.LLM_REQUEST_SERVICE_TIER,
|
|
6449
|
-
String(params.service_tier)
|
|
6450
|
-
);
|
|
6451
|
-
}
|
|
6452
|
-
if (params.response_format !== void 0) {
|
|
6453
|
-
span2.setAttribute(
|
|
6454
|
-
SpanAttributes2.LLM_REQUEST_RESPONSE_FORMAT,
|
|
6455
|
-
safeStringify(params.response_format)
|
|
6456
|
-
);
|
|
6457
|
-
}
|
|
6458
|
-
if (Array.isArray(params.response_modalities)) {
|
|
6459
|
-
span2.setAttribute(
|
|
6460
|
-
SpanAttributes2.LLM_REQUEST_RESPONSE_MODALITIES,
|
|
6461
|
-
params.response_modalities.map((m) => String(m))
|
|
6462
|
-
);
|
|
6463
|
-
}
|
|
6464
|
-
if (!isTraceContentEnabled()) return;
|
|
6465
|
-
const messages = [];
|
|
6466
|
-
if (params.system_instruction !== void 0) {
|
|
6467
|
-
messages.push({
|
|
6468
|
-
role: "system",
|
|
6469
|
-
content: String(params.system_instruction)
|
|
6470
|
-
});
|
|
6471
|
-
}
|
|
6472
|
-
messages.push(...inputMessages(params.input));
|
|
6473
|
-
writePromptAttributes(
|
|
6474
|
-
span2,
|
|
6475
|
-
messages.filter((m) => m.content)
|
|
6476
|
-
);
|
|
6477
|
-
} catch (e) {
|
|
6478
|
-
Logger.error(`${LOG_PREFIX2}: setInteractionRequestAttributes error`, e);
|
|
6479
|
-
}
|
|
6480
|
-
}
|
|
6481
|
-
var MODALITY_USAGE_PREFIXES = {
|
|
6482
|
-
input_tokens_by_modality: "gen_ai.usage.input_tokens_by_modality",
|
|
6483
|
-
output_tokens_by_modality: "gen_ai.usage.output_tokens_by_modality",
|
|
6484
|
-
cached_tokens_by_modality: "gen_ai.usage.cached_tokens_by_modality",
|
|
6485
|
-
tool_use_tokens_by_modality: "gen_ai.usage.tool_use_tokens_by_modality"
|
|
6486
|
-
};
|
|
6487
|
-
function setUsageAttributes3(span2, usage) {
|
|
6488
|
-
if (!isDict(usage)) return;
|
|
6489
|
-
const totals = [
|
|
6490
|
-
["total_input_tokens", SpanAttributes2.LLM_USAGE_PROMPT_TOKENS],
|
|
6491
|
-
["total_tokens", SpanAttributes2.LLM_USAGE_TOTAL_TOKENS],
|
|
6492
|
-
["total_cached_tokens", SpanAttributes2.LLM_USAGE_CACHE_READ_INPUT_TOKENS],
|
|
6493
|
-
["total_thought_tokens", SpanAttributes2.LLM_USAGE_REASONING_TOKENS],
|
|
6494
|
-
["total_tool_use_tokens", SpanAttributes2.LLM_USAGE_TOOL_USE_TOKENS]
|
|
6495
|
-
];
|
|
6496
|
-
for (const [field_, attribute] of totals) {
|
|
6497
|
-
const value = usage[field_];
|
|
6498
|
-
if (typeof value === "number") span2.setAttribute(attribute, value);
|
|
6499
|
-
}
|
|
6500
|
-
let completion = 0;
|
|
6501
|
-
let hasCompletion = false;
|
|
6502
|
-
for (const field_ of ["total_output_tokens", "total_thought_tokens"]) {
|
|
6503
|
-
const value = usage[field_];
|
|
6504
|
-
if (typeof value === "number") {
|
|
6505
|
-
completion += value;
|
|
6506
|
-
hasCompletion = true;
|
|
6507
|
-
}
|
|
6508
|
-
}
|
|
6509
|
-
if (hasCompletion) {
|
|
6510
|
-
span2.setAttribute(SpanAttributes2.LLM_USAGE_COMPLETION_TOKENS, completion);
|
|
6511
|
-
}
|
|
6512
|
-
for (const [field_, prefix] of Object.entries(MODALITY_USAGE_PREFIXES)) {
|
|
6513
|
-
const entries = usage[field_];
|
|
6514
|
-
if (!Array.isArray(entries)) continue;
|
|
6515
|
-
for (const entry of entries) {
|
|
6516
|
-
const modality = field(entry, "modality");
|
|
6517
|
-
const tokens = field(entry, "tokens");
|
|
6518
|
-
if (modality && typeof tokens === "number") {
|
|
6519
|
-
span2.setAttribute(`${prefix}.${String(modality)}`, tokens);
|
|
6520
|
-
}
|
|
6521
|
-
}
|
|
6522
|
-
}
|
|
6523
|
-
const grounding = usage.grounding_tool_count;
|
|
6524
|
-
if (Array.isArray(grounding)) {
|
|
6525
|
-
for (const entry of grounding) {
|
|
6526
|
-
const toolType = field(entry, "type");
|
|
6527
|
-
const count = field(entry, "count");
|
|
6528
|
-
if (toolType && typeof count === "number") {
|
|
6529
|
-
span2.setAttribute(
|
|
6530
|
-
`${SpanAttributes2.LLM_USAGE_GROUNDING_TOOL_COUNT}.${String(toolType)}`,
|
|
6531
|
-
count
|
|
6532
|
-
);
|
|
6533
|
-
}
|
|
6534
|
-
}
|
|
6535
|
-
}
|
|
6536
|
-
}
|
|
6537
|
-
function outputSteps(steps) {
|
|
6538
|
-
if (!Array.isArray(steps)) return [];
|
|
6539
|
-
let firstOutput = 0;
|
|
6540
|
-
steps.forEach((step, index) => {
|
|
6541
|
-
if (field(step, "type") === "user_input") firstOutput = index + 1;
|
|
6542
|
-
});
|
|
6543
|
-
return steps.slice(firstOutput);
|
|
6544
|
-
}
|
|
6545
|
-
function interactionOutputText(steps) {
|
|
6546
|
-
return outputSteps(steps).filter((step) => field(step, "type") === "model_output").map((step) => contentText(field(step, "content"))).join("");
|
|
6547
|
-
}
|
|
6548
|
-
function setInteractionResponseAttributes(span2, interaction) {
|
|
6549
|
-
try {
|
|
6550
|
-
if (!span2.isRecording() || !isDict(interaction)) return;
|
|
6551
|
-
if (interaction.id !== void 0 && interaction.id !== null) {
|
|
6552
|
-
const id = String(interaction.id);
|
|
6553
|
-
span2.setAttribute(SpanAttributes2.LLM_RESPONSE_ID, id);
|
|
6554
|
-
span2.setAttribute("llm.response.id", id);
|
|
6555
|
-
}
|
|
6556
|
-
if (interaction.model !== void 0 && interaction.model !== null) {
|
|
6557
|
-
span2.setAttribute(
|
|
6558
|
-
SpanAttributes2.LLM_RESPONSE_MODEL,
|
|
6559
|
-
String(interaction.model)
|
|
6560
|
-
);
|
|
6561
|
-
}
|
|
6562
|
-
if (interaction.status !== void 0 && interaction.status !== null) {
|
|
6563
|
-
const status = String(interaction.status);
|
|
6564
|
-
span2.setAttribute(SpanAttributes2.LLM_RESPONSE_STATUS, status);
|
|
6565
|
-
span2.setAttribute(SpanAttributes2.LLM_RESPONSE_FINISH_REASON, status);
|
|
6566
|
-
span2.setAttribute("gen_ai.response.finish_reason", status);
|
|
6567
|
-
}
|
|
6568
|
-
if (interaction.previous_interaction_id !== void 0 && interaction.previous_interaction_id !== null) {
|
|
6569
|
-
span2.setAttribute(
|
|
6570
|
-
SpanAttributes2.LLM_RESPONSE_PREVIOUS_INTERACTION_ID,
|
|
6571
|
-
String(interaction.previous_interaction_id)
|
|
6572
|
-
);
|
|
6573
|
-
}
|
|
6574
|
-
setUsageAttributes3(span2, interaction.usage);
|
|
6575
|
-
if (Array.isArray(interaction.steps)) {
|
|
6576
|
-
span2.setAttribute(
|
|
6577
|
-
SpanAttributes2.LLM_INTERACTION_STEP_COUNT,
|
|
6578
|
-
interaction.steps.length
|
|
6579
|
-
);
|
|
6580
|
-
}
|
|
6581
|
-
if (!isTraceContentEnabled()) return;
|
|
6582
|
-
const reasoning = [];
|
|
6583
|
-
const emitted = [];
|
|
6584
|
-
let index = 0;
|
|
6585
|
-
for (const step of outputSteps(interaction.steps)) {
|
|
6586
|
-
const message = stepMessage(step);
|
|
6587
|
-
if (!message) continue;
|
|
6588
|
-
if (message.stepType === "thought") {
|
|
6589
|
-
if (message.content) reasoning.push(message.content);
|
|
6590
|
-
continue;
|
|
6591
|
-
}
|
|
6592
|
-
if (!message.content) continue;
|
|
6593
|
-
const prefix = `${SpanAttributes2.LLM_COMPLETIONS}.${index}`;
|
|
6594
|
-
span2.setAttribute(`${prefix}.role`, message.role);
|
|
6595
|
-
span2.setAttribute(`${prefix}.content`, message.content);
|
|
6596
|
-
span2.setAttribute(`${prefix}.step_type`, message.stepType ?? "");
|
|
6597
|
-
if (message.toolCall) {
|
|
6598
|
-
span2.setAttribute(
|
|
6599
|
-
`${prefix}.tool_calls.0.name`,
|
|
6600
|
-
String(message.toolCall.name ?? "")
|
|
6601
|
-
);
|
|
6602
|
-
if (message.toolCall.id !== void 0) {
|
|
6603
|
-
span2.setAttribute(
|
|
6604
|
-
`${prefix}.tool_calls.0.id`,
|
|
6605
|
-
String(message.toolCall.id)
|
|
6606
|
-
);
|
|
6607
|
-
}
|
|
6608
|
-
if (message.toolCall.arguments !== void 0) {
|
|
6609
|
-
span2.setAttribute(
|
|
6610
|
-
`${prefix}.tool_calls.0.arguments`,
|
|
6611
|
-
safeStringify(message.toolCall.arguments)
|
|
6612
|
-
);
|
|
6613
|
-
}
|
|
6614
|
-
}
|
|
6615
|
-
if (message.modalities && message.modalities.length > 0) {
|
|
6616
|
-
span2.setAttribute(`${prefix}.modality`, message.modalities);
|
|
6617
|
-
}
|
|
6618
|
-
emitted.push(message);
|
|
6619
|
-
index += 1;
|
|
6620
|
-
}
|
|
6621
|
-
if (emitted.length > 0) {
|
|
6622
|
-
span2.setAttribute(
|
|
6623
|
-
"output",
|
|
6624
|
-
JSON.stringify(
|
|
6625
|
-
emitted.map((m) => ({ role: m.role, content: m.content }))
|
|
6626
|
-
)
|
|
6627
|
-
);
|
|
6628
|
-
}
|
|
6629
|
-
if (reasoning.length > 0) {
|
|
6630
|
-
span2.setAttribute(
|
|
6631
|
-
SpanAttributes2.LLM_RESPONSE_REASONING,
|
|
6632
|
-
reasoning.join("")
|
|
6633
|
-
);
|
|
6634
|
-
}
|
|
6635
|
-
} catch (e) {
|
|
6636
|
-
Logger.error(`${LOG_PREFIX2}: setInteractionResponseAttributes error`, e);
|
|
6637
|
-
}
|
|
6638
|
-
}
|
|
6639
|
-
|
|
6640
|
-
// src/instrumentation/google-genai/types.ts
|
|
6641
|
-
var SPAN_NAMES2 = {
|
|
6642
|
-
CHAT: "google_genai.chat",
|
|
6643
|
-
EMBEDDING: "google_genai.embedding",
|
|
6644
|
-
INTERACTION: "google_genai.interaction"
|
|
6645
|
-
};
|
|
6646
|
-
|
|
6647
|
-
// src/instrumentation/google-genai/interactions-wrapper.ts
|
|
6648
|
-
var LOG_PREFIX3 = "netra.instrumentation.google_genai";
|
|
6649
|
-
var NETRA_STREAM = /* @__PURE__ */ Symbol.for("netra.googlegenai.interactions.stream");
|
|
6650
|
-
var INTERACTION_CREATED = "interaction.created";
|
|
6651
|
-
var INTERACTION_COMPLETED = "interaction.completed";
|
|
6652
|
-
var INTERACTION_STATUS_UPDATE = "interaction.status_update";
|
|
6653
|
-
var STEP_START = "step.start";
|
|
6654
|
-
var STEP_DELTA = "step.delta";
|
|
6655
|
-
var ERROR_EVENT = "error";
|
|
6656
|
-
function field2(obj, name) {
|
|
6657
|
-
if (obj === null || obj === void 0) return void 0;
|
|
6658
|
-
return obj[name];
|
|
6659
|
-
}
|
|
6660
|
-
var InteractionStreamAccumulator = class {
|
|
6661
|
-
constructor(span2, startTime) {
|
|
6662
|
-
this.span = span2;
|
|
6663
|
-
this.startTime = startTime;
|
|
6664
|
-
this.finalized = false;
|
|
6665
|
-
this.unobserved = null;
|
|
6666
|
-
this.firstTextRecorded = false;
|
|
6667
|
-
this.stepTypes = /* @__PURE__ */ new Map();
|
|
6668
|
-
this.stepText = /* @__PURE__ */ new Map();
|
|
6669
|
-
this.stepThoughts = /* @__PURE__ */ new Map();
|
|
6670
|
-
this.stepArguments = /* @__PURE__ */ new Map();
|
|
6671
|
-
this.stepMeta = /* @__PURE__ */ new Map();
|
|
6672
|
-
this.completedSteps = null;
|
|
6673
|
-
this.output = "";
|
|
6674
|
-
}
|
|
6675
|
-
processEvent(event) {
|
|
6676
|
-
try {
|
|
6677
|
-
this.reduce(event);
|
|
6678
|
-
} catch (e) {
|
|
6679
|
-
Logger.error(`${LOG_PREFIX3}: interaction event error`, e);
|
|
6680
|
-
}
|
|
6681
|
-
}
|
|
6682
|
-
reduce(event) {
|
|
6683
|
-
this.absorbUsage(event);
|
|
6684
|
-
const eventType = field2(event, "event_type");
|
|
6685
|
-
switch (eventType) {
|
|
6686
|
-
case INTERACTION_CREATED: {
|
|
6687
|
-
const interaction = field2(event, "interaction");
|
|
6688
|
-
this.id = field2(interaction, "id") ?? this.id;
|
|
6689
|
-
this.model = field2(interaction, "model") ?? this.model;
|
|
6690
|
-
this.status = field2(interaction, "status") ?? this.status;
|
|
6691
|
-
break;
|
|
6692
|
-
}
|
|
6693
|
-
case STEP_START: {
|
|
6694
|
-
const index = field2(event, "index");
|
|
6695
|
-
const step = field2(event, "step");
|
|
6696
|
-
if (typeof index !== "number") break;
|
|
6697
|
-
const stepType = field2(step, "type");
|
|
6698
|
-
if (typeof stepType === "string") this.stepTypes.set(index, stepType);
|
|
6699
|
-
if (stepType === "function_call") {
|
|
6700
|
-
this.stepMeta.set(index, {
|
|
6701
|
-
id: field2(step, "id"),
|
|
6702
|
-
name: field2(step, "name")
|
|
6703
|
-
});
|
|
6704
|
-
}
|
|
6705
|
-
break;
|
|
6706
|
-
}
|
|
6707
|
-
case STEP_DELTA:
|
|
6708
|
-
this.reduceDelta(field2(event, "index"), field2(event, "delta"));
|
|
6709
|
-
break;
|
|
6710
|
-
case INTERACTION_STATUS_UPDATE:
|
|
6711
|
-
this.status = field2(event, "status") ?? this.status;
|
|
6712
|
-
break;
|
|
6713
|
-
case INTERACTION_COMPLETED: {
|
|
6714
|
-
const interaction = field2(event, "interaction");
|
|
6715
|
-
this.id = field2(interaction, "id") ?? this.id;
|
|
6716
|
-
this.model = field2(interaction, "model") ?? this.model;
|
|
6717
|
-
this.status = field2(interaction, "status") ?? this.status;
|
|
6718
|
-
const steps = field2(interaction, "steps");
|
|
6719
|
-
if (Array.isArray(steps) && steps.length > 0) this.completedSteps = steps;
|
|
6720
|
-
break;
|
|
6721
|
-
}
|
|
6722
|
-
case ERROR_EVENT:
|
|
6723
|
-
this.error = field2(event, "error");
|
|
6724
|
-
break;
|
|
6725
|
-
}
|
|
6726
|
-
}
|
|
6727
|
-
reduceDelta(index, delta) {
|
|
6728
|
-
if (typeof index !== "number" || delta === null || delta === void 0) return;
|
|
6729
|
-
const deltaType = field2(delta, "type");
|
|
6730
|
-
if (deltaType === "text") {
|
|
6731
|
-
const text = field2(delta, "text");
|
|
6732
|
-
if (typeof text === "string" && text.length > 0) {
|
|
6733
|
-
push(this.stepText, index, text);
|
|
6734
|
-
if (!this.stepTypes.has(index)) this.stepTypes.set(index, "model_output");
|
|
6735
|
-
this.recordFirstText();
|
|
6736
|
-
}
|
|
6737
|
-
} else if (deltaType === "thought_summary") {
|
|
6738
|
-
const text = deltaContentText(field2(delta, "content"));
|
|
6739
|
-
if (text) {
|
|
6740
|
-
push(this.stepThoughts, index, text);
|
|
6741
|
-
if (!this.stepTypes.has(index)) this.stepTypes.set(index, "thought");
|
|
6742
|
-
}
|
|
6743
|
-
} else if (deltaType === "arguments_delta") {
|
|
6744
|
-
const fragment = field2(delta, "arguments") ?? field2(delta, "delta");
|
|
6745
|
-
if (typeof fragment === "string" && fragment.length > 0) {
|
|
6746
|
-
push(this.stepArguments, index, fragment);
|
|
6747
|
-
if (!this.stepTypes.has(index)) this.stepTypes.set(index, "function_call");
|
|
6748
|
-
}
|
|
6749
|
-
} else if (deltaType === "image" || deltaType === "audio" || deltaType === "document" || deltaType === "video") {
|
|
6750
|
-
push(this.stepText, index, `<${deltaType}>`);
|
|
6751
|
-
if (!this.stepTypes.has(index)) this.stepTypes.set(index, "model_output");
|
|
6752
|
-
}
|
|
6753
|
-
}
|
|
6754
|
-
recordFirstText() {
|
|
6755
|
-
if (this.firstTextRecorded) return;
|
|
6756
|
-
this.firstTextRecorded = true;
|
|
6757
|
-
this.span.setAttribute(
|
|
6758
|
-
"gen_ai.performance.time_to_first_token",
|
|
6759
|
-
(Date.now() - this.startTime) / 1e3
|
|
6760
|
-
);
|
|
6761
|
-
}
|
|
6762
|
-
absorbUsage(event) {
|
|
6763
|
-
const metadata = field2(event, "metadata");
|
|
6764
|
-
const candidates = [
|
|
6765
|
-
field2(metadata, "total_usage"),
|
|
6766
|
-
field2(metadata, "usage"),
|
|
6767
|
-
field2(event, "usage"),
|
|
6768
|
-
field2(field2(event, "interaction"), "usage")
|
|
6769
|
-
];
|
|
6770
|
-
for (const candidate of candidates) {
|
|
6771
|
-
if (candidate !== void 0 && candidate !== null) {
|
|
6772
|
-
this.usage = candidate;
|
|
6773
|
-
return;
|
|
6774
|
-
}
|
|
6775
|
-
}
|
|
6776
|
-
}
|
|
6777
|
-
/** Mark the stream as consumed through a path we cannot observe. */
|
|
6778
|
-
abandon(via) {
|
|
6779
|
-
this.unobserved = via;
|
|
6780
|
-
this.finalize();
|
|
6781
|
-
}
|
|
6782
|
-
buildInteraction() {
|
|
6783
|
-
const steps = this.completedSteps && this.stepText.size === 0 ? this.completedSteps : this.buildSteps();
|
|
6784
|
-
return {
|
|
6785
|
-
id: this.id,
|
|
6786
|
-
model: this.model,
|
|
6787
|
-
status: this.status,
|
|
6788
|
-
usage: this.usage,
|
|
6789
|
-
steps
|
|
6790
|
-
};
|
|
6791
|
-
}
|
|
6792
|
-
buildSteps() {
|
|
6793
|
-
const indices = /* @__PURE__ */ new Set([
|
|
6794
|
-
...this.stepTypes.keys(),
|
|
6795
|
-
...this.stepText.keys(),
|
|
6796
|
-
...this.stepThoughts.keys()
|
|
6797
|
-
]);
|
|
6798
|
-
return [...indices].sort((a, b) => a - b).map((index) => {
|
|
6799
|
-
const stepType = this.stepTypes.get(index) ?? "model_output";
|
|
6800
|
-
if (stepType === "thought") {
|
|
6801
|
-
return {
|
|
6802
|
-
type: "thought",
|
|
6803
|
-
summary: [
|
|
6804
|
-
{ type: "text", text: (this.stepThoughts.get(index) ?? []).join("") }
|
|
6805
|
-
]
|
|
6806
|
-
};
|
|
6807
|
-
}
|
|
6808
|
-
if (stepType === "function_call") {
|
|
6809
|
-
const meta = this.stepMeta.get(index) ?? {};
|
|
6810
|
-
return {
|
|
6811
|
-
type: "function_call",
|
|
6812
|
-
id: meta.id,
|
|
6813
|
-
name: meta.name,
|
|
6814
|
-
arguments: parseArguments(
|
|
6815
|
-
(this.stepArguments.get(index) ?? []).join("")
|
|
6816
|
-
)
|
|
6817
|
-
};
|
|
6818
|
-
}
|
|
6819
|
-
return {
|
|
6820
|
-
type: stepType,
|
|
6821
|
-
content: [
|
|
6822
|
-
{ type: "text", text: (this.stepText.get(index) ?? []).join("") }
|
|
6823
|
-
]
|
|
6824
|
-
};
|
|
6825
|
-
});
|
|
6826
|
-
}
|
|
6827
|
-
/** Close the span. Safe to call more than once. */
|
|
6828
|
-
finalize() {
|
|
6829
|
-
if (this.finalized) return;
|
|
6830
|
-
this.finalized = true;
|
|
6831
|
-
try {
|
|
6832
|
-
const interaction = this.buildInteraction();
|
|
6833
|
-
setInteractionResponseAttributes(this.span, interaction);
|
|
6834
|
-
this.output = interactionOutputText(interaction.steps);
|
|
6835
|
-
this.span.setAttribute(
|
|
6836
|
-
SpanAttributes2.LLM_RESPONSE_DURATION,
|
|
6837
|
-
(Date.now() - this.startTime) / 1e3
|
|
6838
|
-
);
|
|
6839
|
-
if (this.unobserved) {
|
|
6840
|
-
this.span.setAttribute(SpanAttributes2.STREAM_UNOBSERVED, this.unobserved);
|
|
6841
|
-
}
|
|
6842
|
-
if (this.error) {
|
|
6843
|
-
this.span.setAttribute(
|
|
6844
|
-
"error.type",
|
|
6845
|
-
String(field2(this.error, "code") ?? "error")
|
|
6846
|
-
);
|
|
6847
|
-
this.span.setStatus({
|
|
6848
|
-
code: SpanStatusCode.ERROR,
|
|
6849
|
-
message: String(field2(this.error, "message") ?? "interaction failed")
|
|
6850
|
-
});
|
|
6851
|
-
} else {
|
|
6852
|
-
this.span.setStatus({ code: SpanStatusCode.OK });
|
|
6853
|
-
}
|
|
6854
|
-
} catch (e) {
|
|
6855
|
-
Logger.error(`${LOG_PREFIX3}: interaction finalize error`, e);
|
|
6856
|
-
} finally {
|
|
6857
|
-
this.span.end();
|
|
6858
|
-
}
|
|
6859
|
-
}
|
|
6860
|
-
fail(error) {
|
|
6861
|
-
if (this.finalized) return;
|
|
6862
|
-
this.span.setStatus({
|
|
6863
|
-
code: SpanStatusCode.ERROR,
|
|
6864
|
-
message: error instanceof Error ? error.message : String(error)
|
|
6865
|
-
});
|
|
6866
|
-
this.span.recordException(error);
|
|
6867
|
-
this.finalize();
|
|
6868
|
-
}
|
|
6869
|
-
};
|
|
6870
|
-
function push(map, index, value) {
|
|
6871
|
-
const existing = map.get(index);
|
|
6872
|
-
if (existing) existing.push(value);
|
|
6873
|
-
else map.set(index, [value]);
|
|
6874
|
-
}
|
|
6875
|
-
function deltaContentText(content) {
|
|
6876
|
-
if (content === null || content === void 0) return "";
|
|
6877
|
-
if (typeof content === "string") return content;
|
|
6878
|
-
const items = Array.isArray(content) ? content : [content];
|
|
6879
|
-
return items.map((item) => String(field2(item, "text") ?? "")).join("");
|
|
6880
|
-
}
|
|
6881
|
-
function parseArguments(raw) {
|
|
6882
|
-
if (!raw) return {};
|
|
6883
|
-
try {
|
|
6884
|
-
return JSON.parse(raw);
|
|
6885
|
-
} catch {
|
|
6886
|
-
return raw;
|
|
6887
|
-
}
|
|
6888
|
-
}
|
|
6889
|
-
function isInteractionStream(value) {
|
|
6890
|
-
return value !== null && value !== void 0 && typeof value[Symbol.asyncIterator] === "function";
|
|
6891
|
-
}
|
|
6892
|
-
function wrapIterator(iterator, accumulator, withContext) {
|
|
6893
|
-
return {
|
|
6894
|
-
async next(value) {
|
|
6895
|
-
try {
|
|
6896
|
-
const result = await withContext(() => iterator.next(value));
|
|
6897
|
-
if (result.done) accumulator.finalize();
|
|
6898
|
-
else accumulator.processEvent(result.value);
|
|
6899
|
-
return result;
|
|
6900
|
-
} catch (e) {
|
|
6901
|
-
accumulator.fail(e);
|
|
6902
|
-
throw e;
|
|
6903
|
-
}
|
|
6904
|
-
},
|
|
6905
|
-
async return(value) {
|
|
6906
|
-
try {
|
|
6907
|
-
const result = await withContext(
|
|
6908
|
-
() => iterator.return?.(value) ?? { done: true, value }
|
|
6909
|
-
);
|
|
6910
|
-
accumulator.finalize();
|
|
6911
|
-
return result;
|
|
6912
|
-
} catch (e) {
|
|
6913
|
-
accumulator.fail(e);
|
|
6914
|
-
throw e;
|
|
6915
|
-
}
|
|
6916
|
-
},
|
|
6917
|
-
async throw(e) {
|
|
6918
|
-
try {
|
|
6919
|
-
const result = await withContext(() => {
|
|
6920
|
-
if (iterator.throw) return iterator.throw(e);
|
|
6921
|
-
throw e;
|
|
6922
|
-
});
|
|
6923
|
-
if (result.done) accumulator.finalize();
|
|
6924
|
-
return result;
|
|
6925
|
-
} catch (err) {
|
|
6926
|
-
accumulator.fail(err);
|
|
6927
|
-
throw err;
|
|
6928
|
-
}
|
|
6929
|
-
}
|
|
6930
|
-
};
|
|
6931
|
-
}
|
|
6932
|
-
var UNOBSERVED_METHODS = /* @__PURE__ */ new Set(["getReader", "pipeTo", "pipeThrough", "tee"]);
|
|
6933
|
-
function instrumentInteractionStream(stream, accumulator, withContext) {
|
|
6934
|
-
if (stream[NETRA_STREAM]) return stream;
|
|
6935
|
-
const makeIterator = (options) => wrapIterator(stream[Symbol.asyncIterator](options), accumulator, withContext);
|
|
6936
|
-
return new Proxy(stream, {
|
|
6937
|
-
get(target, property) {
|
|
6938
|
-
if (property === NETRA_STREAM) return true;
|
|
6939
|
-
if (property === Symbol.asyncIterator || property === "values") {
|
|
6940
|
-
return (options) => makeIterator(options);
|
|
6941
|
-
}
|
|
6942
|
-
if (typeof property === "string" && UNOBSERVED_METHODS.has(property)) {
|
|
6943
|
-
const original = Reflect.get(target, property, target);
|
|
6944
|
-
if (typeof original !== "function") return original;
|
|
6945
|
-
return (...args) => {
|
|
6946
|
-
accumulator.abandon(property);
|
|
6947
|
-
return original.apply(target, args);
|
|
6948
|
-
};
|
|
6949
|
-
}
|
|
6950
|
-
const value = Reflect.get(target, property, target);
|
|
6951
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
6952
|
-
}
|
|
6953
|
-
});
|
|
6954
|
-
}
|
|
6955
|
-
function interactionsWrapper(tracer) {
|
|
6956
|
-
return function wrapper(original) {
|
|
6957
|
-
return function(...args) {
|
|
6958
|
-
if (shouldSuppressInstrumentation()) {
|
|
6959
|
-
return original.apply(this, args);
|
|
6960
|
-
}
|
|
6961
|
-
const params = args[0] ?? {};
|
|
6962
|
-
const currentContext = context.active();
|
|
6963
|
-
return tracer.startActiveSpan(
|
|
6964
|
-
SPAN_NAMES2.INTERACTION,
|
|
6965
|
-
{ kind: SpanKind.CLIENT },
|
|
6966
|
-
(span2) => {
|
|
6967
|
-
const startTime = Date.now();
|
|
6968
|
-
try {
|
|
6969
|
-
setInteractionRequestAttributes(span2, params);
|
|
6970
|
-
const spanContext = trace.setSpan(currentContext, span2);
|
|
6971
|
-
const withContext = (fn) => context.with(spanContext, fn);
|
|
6972
|
-
const suppressedCtx = createSuppressedContext(spanContext);
|
|
6973
|
-
const response = context.with(
|
|
6974
|
-
suppressedCtx,
|
|
6975
|
-
() => original.apply(this, args)
|
|
6976
|
-
);
|
|
6977
|
-
if (!isPromise(response)) {
|
|
6978
|
-
return finishNonStreaming(span2, response, startTime);
|
|
6979
|
-
}
|
|
6980
|
-
return response.then(
|
|
6981
|
-
(value) => {
|
|
6982
|
-
if (isInteractionStream(value)) {
|
|
6983
|
-
span2.setAttribute(SpanAttributes2.LLM_IS_STREAMING, true);
|
|
6984
|
-
const accumulator = new InteractionStreamAccumulator(
|
|
6985
|
-
span2,
|
|
6986
|
-
startTime
|
|
6987
|
-
);
|
|
6988
|
-
return instrumentInteractionStream(
|
|
6989
|
-
value,
|
|
6990
|
-
accumulator,
|
|
6991
|
-
withContext
|
|
6992
|
-
);
|
|
6993
|
-
}
|
|
6994
|
-
return finishNonStreaming(span2, value, startTime);
|
|
6995
|
-
},
|
|
6996
|
-
(error) => {
|
|
6997
|
-
span2.setStatus({
|
|
6998
|
-
code: SpanStatusCode.ERROR,
|
|
6999
|
-
message: error instanceof Error ? error.message : String(error)
|
|
7000
|
-
});
|
|
7001
|
-
span2.recordException(error);
|
|
7002
|
-
span2.end();
|
|
7003
|
-
throw error;
|
|
7004
|
-
}
|
|
7005
|
-
);
|
|
7006
|
-
} catch (error) {
|
|
7007
|
-
span2.setStatus({
|
|
7008
|
-
code: SpanStatusCode.ERROR,
|
|
7009
|
-
message: error instanceof Error ? error.message : String(error)
|
|
7010
|
-
});
|
|
7011
|
-
span2.recordException(error);
|
|
7012
|
-
span2.end();
|
|
7013
|
-
throw error;
|
|
7014
|
-
}
|
|
7015
|
-
}
|
|
7016
|
-
);
|
|
7017
|
-
};
|
|
7018
|
-
};
|
|
7019
|
-
}
|
|
7020
|
-
function finishNonStreaming(span2, value, startTime) {
|
|
7021
|
-
try {
|
|
7022
|
-
span2.setAttribute(SpanAttributes2.LLM_IS_STREAMING, false);
|
|
7023
|
-
setInteractionResponseAttributes(span2, value);
|
|
7024
|
-
span2.setAttribute(
|
|
7025
|
-
SpanAttributes2.LLM_RESPONSE_DURATION,
|
|
7026
|
-
(Date.now() - startTime) / 1e3
|
|
7027
|
-
);
|
|
7028
|
-
span2.setStatus({ code: SpanStatusCode.OK });
|
|
7029
|
-
} catch (e) {
|
|
7030
|
-
Logger.error(`${LOG_PREFIX3}: interaction response error`, e);
|
|
7031
|
-
} finally {
|
|
7032
|
-
span2.end();
|
|
7033
|
-
}
|
|
7034
|
-
return value;
|
|
7035
|
-
}
|
|
7036
|
-
|
|
7037
|
-
// src/instrumentation/google-genai/version.ts
|
|
7038
|
-
var __version__2 = "1.0.0";
|
|
7039
|
-
var LOG_PREFIX4 = "netra.instrumentation.google_genai";
|
|
7040
6170
|
function buildParams(args, self) {
|
|
7041
6171
|
const raw = args[0];
|
|
7042
6172
|
const params = typeof raw === "object" && raw !== null ? { ...raw } : { message: raw };
|
|
@@ -7176,7 +6306,7 @@ function streamWrapperFactory(tracer, spanName, requestType) {
|
|
|
7176
6306
|
(endTime - startTime) / 1e3
|
|
7177
6307
|
);
|
|
7178
6308
|
} catch (e) {
|
|
7179
|
-
Logger.error(`${
|
|
6309
|
+
Logger.error(`${LOG_PREFIX2}: stream finalize error`, e);
|
|
7180
6310
|
}
|
|
7181
6311
|
if (status === "ok") {
|
|
7182
6312
|
span2.setStatus({ code: SpanStatusCode.OK });
|
|
@@ -7205,9 +6335,6 @@ var chatStreamWrapper2 = (tracer) => streamWrapperFactory(tracer, SPAN_NAMES2.CH
|
|
|
7205
6335
|
var embeddingsWrapper = (tracer) => genericWrapperFactory(tracer, SPAN_NAMES2.EMBEDDING, "embedding");
|
|
7206
6336
|
|
|
7207
6337
|
// src/instrumentation/google-genai/index.ts
|
|
7208
|
-
var INTERACTIONS_PATCHED = /* @__PURE__ */ Symbol.for(
|
|
7209
|
-
"netra.googlegenai.interactions.patched"
|
|
7210
|
-
);
|
|
7211
6338
|
var NetraGoogleGenAIInstrumentor = class extends BaseInstrumentor {
|
|
7212
6339
|
constructor() {
|
|
7213
6340
|
super(...arguments);
|
|
@@ -7215,15 +6342,11 @@ var NetraGoogleGenAIInstrumentor = class extends BaseInstrumentor {
|
|
|
7215
6342
|
this.instrumentationVersion = __version__2;
|
|
7216
6343
|
this.packageName = "@google/genai";
|
|
7217
6344
|
this.displayName = "Google GenAI";
|
|
7218
|
-
/** Original `interactions` descriptors, for an exact restore on unpatch. */
|
|
7219
|
-
this.interactionsDescriptors = [];
|
|
7220
|
-
/** Resource prototypes whose `create` we wrapped. */
|
|
7221
|
-
this.patchedInteractionProtos = [];
|
|
7222
6345
|
}
|
|
7223
6346
|
extractClasses(mod) {
|
|
7224
6347
|
const root = mod.default ?? mod;
|
|
7225
6348
|
if (!root.Models) return null;
|
|
7226
|
-
return { Models: root.Models, Chat: root.Chat
|
|
6349
|
+
return { Models: root.Models, Chat: root.Chat };
|
|
7227
6350
|
}
|
|
7228
6351
|
isSameClasses(a, b) {
|
|
7229
6352
|
return a.Models === b.Models;
|
|
@@ -7258,57 +6381,8 @@ var NetraGoogleGenAIInstrumentor = class extends BaseInstrumentor {
|
|
|
7258
6381
|
wrapped = true;
|
|
7259
6382
|
}
|
|
7260
6383
|
}
|
|
7261
|
-
if (this.patchInteractions(classes.GoogleGenAI, tracer)) {
|
|
7262
|
-
wrapped = true;
|
|
7263
|
-
}
|
|
7264
6384
|
return wrapped;
|
|
7265
6385
|
}
|
|
7266
|
-
/**
|
|
7267
|
-
* Redefine the `interactions` accessor so the resource prototype gets patched
|
|
7268
|
-
* on first access.
|
|
7269
|
-
*
|
|
7270
|
-
* shimmer cannot be used on the accessor itself -- it does a plain property
|
|
7271
|
-
* assignment, which would clobber the getter. Nothing is constructed here
|
|
7272
|
-
* either: `new GoogleGenAI({})` writes an API-key warning to the console.
|
|
7273
|
-
*
|
|
7274
|
-
* A client that read `.interactions` before instrumentation finished keeps
|
|
7275
|
-
* its cached resource, but the prototype is shared, so the patch still
|
|
7276
|
-
* applies the next time any instance reads the accessor. Netra.init() is
|
|
7277
|
-
* awaited before user code, so this is an edge case rather than a hole.
|
|
7278
|
-
*/
|
|
7279
|
-
patchInteractions(GoogleGenAI, tracer) {
|
|
7280
|
-
if (!GoogleGenAI?.prototype) return false;
|
|
7281
|
-
const descriptor = Object.getOwnPropertyDescriptor(
|
|
7282
|
-
GoogleGenAI.prototype,
|
|
7283
|
-
"interactions"
|
|
7284
|
-
);
|
|
7285
|
-
if (!descriptor?.get || !descriptor.configurable) return false;
|
|
7286
|
-
const originalGet = descriptor.get;
|
|
7287
|
-
const patchedProtos = this.patchedInteractionProtos;
|
|
7288
|
-
this.interactionsDescriptors.push([GoogleGenAI, descriptor]);
|
|
7289
|
-
Object.defineProperty(GoogleGenAI.prototype, "interactions", {
|
|
7290
|
-
configurable: true,
|
|
7291
|
-
enumerable: descriptor.enumerable,
|
|
7292
|
-
get() {
|
|
7293
|
-
const resource = originalGet.call(this);
|
|
7294
|
-
try {
|
|
7295
|
-
const proto = resource && Object.getPrototypeOf(resource);
|
|
7296
|
-
if (proto && !proto[INTERACTIONS_PATCHED] && typeof proto.create === "function") {
|
|
7297
|
-
Object.defineProperty(proto, INTERACTIONS_PATCHED, {
|
|
7298
|
-
value: true,
|
|
7299
|
-
configurable: true
|
|
7300
|
-
});
|
|
7301
|
-
shimmer.wrap(proto, "create", interactionsWrapper(tracer));
|
|
7302
|
-
patchedProtos.push(proto);
|
|
7303
|
-
}
|
|
7304
|
-
} catch (error) {
|
|
7305
|
-
Logger.error(`Google GenAI: failed to patch interactions: ${error}`);
|
|
7306
|
-
}
|
|
7307
|
-
return resource;
|
|
7308
|
-
}
|
|
7309
|
-
});
|
|
7310
|
-
return true;
|
|
7311
|
-
}
|
|
7312
6386
|
removePatches(classes) {
|
|
7313
6387
|
const modelsProto = classes.Models?.prototype;
|
|
7314
6388
|
if (modelsProto) {
|
|
@@ -7327,25 +6401,6 @@ var NetraGoogleGenAIInstrumentor = class extends BaseInstrumentor {
|
|
|
7327
6401
|
shimmer.unwrap(chatProto, "sendMessageStream");
|
|
7328
6402
|
}
|
|
7329
6403
|
}
|
|
7330
|
-
onUninstrument() {
|
|
7331
|
-
for (const proto of this.patchedInteractionProtos) {
|
|
7332
|
-
try {
|
|
7333
|
-
shimmer.unwrap(proto, "create");
|
|
7334
|
-
delete proto[INTERACTIONS_PATCHED];
|
|
7335
|
-
} catch (error) {
|
|
7336
|
-
Logger.error(`Google GenAI: failed to unwrap interactions.create: ${error}`);
|
|
7337
|
-
}
|
|
7338
|
-
}
|
|
7339
|
-
this.patchedInteractionProtos = [];
|
|
7340
|
-
for (const [GoogleGenAI, descriptor] of this.interactionsDescriptors) {
|
|
7341
|
-
try {
|
|
7342
|
-
Object.defineProperty(GoogleGenAI.prototype, "interactions", descriptor);
|
|
7343
|
-
} catch (error) {
|
|
7344
|
-
Logger.error(`Google GenAI: failed to restore interactions accessor: ${error}`);
|
|
7345
|
-
}
|
|
7346
|
-
}
|
|
7347
|
-
this.interactionsDescriptors = [];
|
|
7348
|
-
}
|
|
7349
6404
|
};
|
|
7350
6405
|
var googleGenAIInstrumentor = new NetraGoogleGenAIInstrumentor();
|
|
7351
6406
|
|
|
@@ -7749,7 +6804,7 @@ function _setEmbeddingResponseAttributes(span2, response) {
|
|
|
7749
6804
|
}
|
|
7750
6805
|
|
|
7751
6806
|
// src/instrumentation/google-generative-ai/wrappers.ts
|
|
7752
|
-
var
|
|
6807
|
+
var LOG_PREFIX3 = "netra.instrumentation.google_generative_ai";
|
|
7753
6808
|
var CHAT_SPAN_NAME = "google_generative_ai.chat";
|
|
7754
6809
|
var EMBEDDING_SPAN_NAME = "google_generative_ai.embedding";
|
|
7755
6810
|
function buildKwargs(instance) {
|
|
@@ -7794,7 +6849,7 @@ function googleGenerativeAIWrapper(tracer, spanName, requestType) {
|
|
|
7794
6849
|
try {
|
|
7795
6850
|
setRequestAttributes4(span2, kwargs, requestType, args[0]);
|
|
7796
6851
|
} catch (e) {
|
|
7797
|
-
Logger.error(`${
|
|
6852
|
+
Logger.error(`${LOG_PREFIX3}:`, e);
|
|
7798
6853
|
}
|
|
7799
6854
|
const startTime = Date.now();
|
|
7800
6855
|
const response = original.apply(this, args);
|
|
@@ -7815,7 +6870,7 @@ function googleGenerativeAIWrapper(tracer, spanName, requestType) {
|
|
|
7815
6870
|
);
|
|
7816
6871
|
}
|
|
7817
6872
|
} catch (e) {
|
|
7818
|
-
Logger.error(`${
|
|
6873
|
+
Logger.error(`${LOG_PREFIX3}:`, e);
|
|
7819
6874
|
}
|
|
7820
6875
|
span2.setStatus({ code: SpanStatusCode.OK });
|
|
7821
6876
|
span2.end();
|
|
@@ -7840,7 +6895,7 @@ function googleGenerativeAIWrapper(tracer, spanName, requestType) {
|
|
|
7840
6895
|
(endTime - startTime) / 1e3
|
|
7841
6896
|
);
|
|
7842
6897
|
} catch (e) {
|
|
7843
|
-
Logger.error(`${
|
|
6898
|
+
Logger.error(`${LOG_PREFIX3}:`, e);
|
|
7844
6899
|
}
|
|
7845
6900
|
span2.setStatus({ code: SpanStatusCode.OK });
|
|
7846
6901
|
span2.end();
|
|
@@ -7882,7 +6937,7 @@ function googleGenerativeAIStreamWrapper(tracer, spanName, requestType) {
|
|
|
7882
6937
|
try {
|
|
7883
6938
|
setRequestAttributes4(span2, kwargs, requestType, args[0]);
|
|
7884
6939
|
} catch (e) {
|
|
7885
|
-
Logger.error(`${
|
|
6940
|
+
Logger.error(`${LOG_PREFIX3}:`, e);
|
|
7886
6941
|
}
|
|
7887
6942
|
const response = original.apply(this, args);
|
|
7888
6943
|
if (!isPromise(response)) {
|
|
@@ -7906,7 +6961,7 @@ function googleGenerativeAIStreamWrapper(tracer, spanName, requestType) {
|
|
|
7906
6961
|
duration
|
|
7907
6962
|
);
|
|
7908
6963
|
} catch (e) {
|
|
7909
|
-
Logger.error(`${
|
|
6964
|
+
Logger.error(`${LOG_PREFIX3}:`, e);
|
|
7910
6965
|
}
|
|
7911
6966
|
span2.setStatus({ code: SpanStatusCode.OK });
|
|
7912
6967
|
span2.end();
|
|
@@ -7942,7 +6997,7 @@ function googleGenerativeAIStreamWrapper(tracer, spanName, requestType) {
|
|
|
7942
6997
|
duration
|
|
7943
6998
|
);
|
|
7944
6999
|
} catch (e) {
|
|
7945
|
-
Logger.error(`${
|
|
7000
|
+
Logger.error(`${LOG_PREFIX3}:`, e);
|
|
7946
7001
|
}
|
|
7947
7002
|
span2.setStatus({ code: SpanStatusCode.OK });
|
|
7948
7003
|
span2.end();
|
|
@@ -10737,11 +9792,11 @@ function setGenerationAttributes(span2, data, systemName) {
|
|
|
10737
9792
|
}
|
|
10738
9793
|
const effectiveOutput = unwrapGenerationOutput(data.output);
|
|
10739
9794
|
if (data.usage) {
|
|
10740
|
-
|
|
9795
|
+
setUsageAttributes3(span2, data.usage);
|
|
10741
9796
|
} else if (effectiveOutput && typeof effectiveOutput === "object" && !Array.isArray(effectiveOutput)) {
|
|
10742
9797
|
const respUsage = effectiveOutput.usage;
|
|
10743
9798
|
if (respUsage && typeof respUsage === "object") {
|
|
10744
|
-
|
|
9799
|
+
setUsageAttributes3(span2, respUsage);
|
|
10745
9800
|
}
|
|
10746
9801
|
}
|
|
10747
9802
|
if (data.input !== void 0) {
|
|
@@ -10780,7 +9835,7 @@ function setResponseAttributes8(span2, agentSpan, systemName) {
|
|
|
10780
9835
|
}
|
|
10781
9836
|
const resp = data._response;
|
|
10782
9837
|
if (data.usage && !(resp && typeof resp === "object" && resp.usage)) {
|
|
10783
|
-
|
|
9838
|
+
setUsageAttributes3(span2, data.usage);
|
|
10784
9839
|
}
|
|
10785
9840
|
}
|
|
10786
9841
|
function setFunctionAttributes(span2, data) {
|
|
@@ -10829,7 +9884,7 @@ function setCustomAttributes(span2, data) {
|
|
|
10829
9884
|
span2.setAttribute(NETRA_CUSTOM_DATA, safeStringify(data.data));
|
|
10830
9885
|
}
|
|
10831
9886
|
}
|
|
10832
|
-
function
|
|
9887
|
+
function setUsageAttributes3(span2, usage) {
|
|
10833
9888
|
const promptTokens = usage.input_tokens ?? usage.prompt_tokens;
|
|
10834
9889
|
const completionTokens = usage.output_tokens ?? usage.completion_tokens;
|
|
10835
9890
|
const totalTokens = usage.total_tokens;
|
|
@@ -10864,6 +9919,7 @@ function setUsageAttributes4(span2, usage) {
|
|
|
10864
9919
|
if (inputDetails.cache_write_tokens !== void 0) {
|
|
10865
9920
|
span2.setAttribute(SpanAttributes2.LLM_USAGE_CACHE_CREATION_INPUT_TOKENS, Number(inputDetails.cache_write_tokens));
|
|
10866
9921
|
}
|
|
9922
|
+
setCacheWriteTtlAttributes(span2, inputDetails);
|
|
10867
9923
|
}
|
|
10868
9924
|
const outputDetails = usage.output_tokens_details ?? usage.completion_tokens_details;
|
|
10869
9925
|
if (outputDetails) {
|
|
@@ -10916,6 +9972,7 @@ function setResponseObjectUsageAttributes(span2, usage) {
|
|
|
10916
9972
|
if (usage.input_tokens_details?.cache_write_tokens !== void 0) {
|
|
10917
9973
|
span2.setAttribute(SpanAttributes2.LLM_USAGE_CACHE_CREATION_INPUT_TOKENS, usage.input_tokens_details.cache_write_tokens);
|
|
10918
9974
|
}
|
|
9975
|
+
setCacheWriteTtlAttributes(span2, usage.input_tokens_details);
|
|
10919
9976
|
if (usage.output_tokens_details?.reasoning_tokens !== void 0) {
|
|
10920
9977
|
span2.setAttribute(SpanAttributes2.LLM_USAGE_REASONING_TOKENS, usage.output_tokens_details.reasoning_tokens);
|
|
10921
9978
|
}
|
|
@@ -12965,17 +12022,17 @@ var BaseTask = class {
|
|
|
12965
12022
|
};
|
|
12966
12023
|
|
|
12967
12024
|
// src/simulation/utils.ts
|
|
12968
|
-
var
|
|
12025
|
+
var LOG_PREFIX4 = "netra.simulation";
|
|
12969
12026
|
var DEFAULT_FILE_DOWNLOAD_TIMEOUT_S = 30;
|
|
12970
12027
|
var MAX_FILE_DOWNLOAD_WORKERS = 8;
|
|
12971
12028
|
var _cachedFileDownloadTimeoutMs = null;
|
|
12972
12029
|
function validateSimulationInputs(datasetId, task2) {
|
|
12973
12030
|
if (!datasetId) {
|
|
12974
|
-
Logger.error(`${
|
|
12031
|
+
Logger.error(`${LOG_PREFIX4}: dataset_id is required`);
|
|
12975
12032
|
return false;
|
|
12976
12033
|
}
|
|
12977
12034
|
if (!(task2 instanceof BaseTask)) {
|
|
12978
|
-
Logger.error(`${
|
|
12035
|
+
Logger.error(`${LOG_PREFIX4}: task must be a BaseTask instance`);
|
|
12979
12036
|
return false;
|
|
12980
12037
|
}
|
|
12981
12038
|
return true;
|
|
@@ -12992,7 +12049,7 @@ function _getFileDownloadTimeout() {
|
|
|
12992
12049
|
return _cachedFileDownloadTimeoutMs;
|
|
12993
12050
|
}
|
|
12994
12051
|
Logger.warn(
|
|
12995
|
-
`${
|
|
12052
|
+
`${LOG_PREFIX4}: Invalid file download timeout '${envVal}', using default ${DEFAULT_FILE_DOWNLOAD_TIMEOUT_S}s`
|
|
12996
12053
|
);
|
|
12997
12054
|
}
|
|
12998
12055
|
_cachedFileDownloadTimeoutMs = DEFAULT_FILE_DOWNLOAD_TIMEOUT_S * 1e3;
|
|
@@ -13021,7 +12078,7 @@ async function _downloadSingleFile(fileData, timeoutMs, signal) {
|
|
|
13021
12078
|
const status = axios.isAxiosError(error) ? error.response?.status : void 0;
|
|
13022
12079
|
const reason = status ? `HTTP ${status}` : error instanceof Error ? error.message : String(error);
|
|
13023
12080
|
Logger.error(
|
|
13024
|
-
`${
|
|
12081
|
+
`${LOG_PREFIX4}: Failed to download file '${fileData.fileName}': ${reason}`
|
|
13025
12082
|
);
|
|
13026
12083
|
throw new Error(
|
|
13027
12084
|
`Failed to download file '${fileData.fileName}': ${reason}`
|
|
@@ -13038,7 +12095,7 @@ function parseFiles(rawFiles) {
|
|
|
13038
12095
|
const downloadUrl = entry.downloadUrl || "";
|
|
13039
12096
|
if (!fileName || !downloadUrl) {
|
|
13040
12097
|
Logger.warn(
|
|
13041
|
-
`${
|
|
12098
|
+
`${LOG_PREFIX4}: Skipping malformed file attachment (missing fileName or downloadUrl)`
|
|
13042
12099
|
);
|
|
13043
12100
|
continue;
|
|
13044
12101
|
}
|
|
@@ -13082,7 +12139,7 @@ async function executeTask2(task2, message, sessionId, rawFiles, setupContext) {
|
|
|
13082
12139
|
}
|
|
13083
12140
|
|
|
13084
12141
|
// src/simulation/client.ts
|
|
13085
|
-
var
|
|
12142
|
+
var LOG_PREFIX5 = "netra.simulation";
|
|
13086
12143
|
var DEFAULT_TIMEOUT = 1e4;
|
|
13087
12144
|
var SimulationHttpClient = class {
|
|
13088
12145
|
constructor(config) {
|
|
@@ -13095,7 +12152,7 @@ var SimulationHttpClient = class {
|
|
|
13095
12152
|
_createClient(config) {
|
|
13096
12153
|
const endpoint = (config.otlpEndpoint || "").trim();
|
|
13097
12154
|
if (!endpoint) {
|
|
13098
|
-
Logger.error(`${
|
|
12155
|
+
Logger.error(`${LOG_PREFIX5}: NETRA_OTLP_ENDPOINT is required`);
|
|
13099
12156
|
return null;
|
|
13100
12157
|
}
|
|
13101
12158
|
const baseURL = this._resolveBaseUrl(endpoint);
|
|
@@ -13117,7 +12174,7 @@ var SimulationHttpClient = class {
|
|
|
13117
12174
|
);
|
|
13118
12175
|
return instance;
|
|
13119
12176
|
} catch (error) {
|
|
13120
|
-
Logger.error(`${
|
|
12177
|
+
Logger.error(`${LOG_PREFIX5}: Failed to create HTTP client:`, error);
|
|
13121
12178
|
return null;
|
|
13122
12179
|
}
|
|
13123
12180
|
}
|
|
@@ -13152,7 +12209,7 @@ var SimulationHttpClient = class {
|
|
|
13152
12209
|
const timeout = parseFloat(timeoutStr);
|
|
13153
12210
|
if (isNaN(timeout)) {
|
|
13154
12211
|
Logger.warn(
|
|
13155
|
-
`${
|
|
12212
|
+
`${LOG_PREFIX5}: Invalid timeout '${timeoutStr}', using default ${DEFAULT_TIMEOUT}ms`
|
|
13156
12213
|
);
|
|
13157
12214
|
return DEFAULT_TIMEOUT;
|
|
13158
12215
|
}
|
|
@@ -13164,9 +12221,9 @@ var SimulationHttpClient = class {
|
|
|
13164
12221
|
* Used in the two-phase flow so that beforeAll/before hooks can run
|
|
13165
12222
|
* before any LLM spend.
|
|
13166
12223
|
*/
|
|
13167
|
-
async initializeRun(name, datasetId,
|
|
12224
|
+
async initializeRun(name, datasetId, context16, hooksMeta) {
|
|
13168
12225
|
if (!this.client) {
|
|
13169
|
-
Logger.error(`${
|
|
12226
|
+
Logger.error(`${LOG_PREFIX5}: Client not initialized`);
|
|
13170
12227
|
return null;
|
|
13171
12228
|
}
|
|
13172
12229
|
try {
|
|
@@ -13174,7 +12231,7 @@ var SimulationHttpClient = class {
|
|
|
13174
12231
|
const payload = {
|
|
13175
12232
|
name,
|
|
13176
12233
|
datasetId,
|
|
13177
|
-
context:
|
|
12234
|
+
context: context16 || {}
|
|
13178
12235
|
};
|
|
13179
12236
|
if (hooksMeta) {
|
|
13180
12237
|
payload.lifecycleHooks = hooksMeta;
|
|
@@ -13184,7 +12241,7 @@ var SimulationHttpClient = class {
|
|
|
13184
12241
|
const responseData = data.data || {};
|
|
13185
12242
|
const items = responseData.items || [];
|
|
13186
12243
|
if (items.length === 0) {
|
|
13187
|
-
Logger.warn(`${
|
|
12244
|
+
Logger.warn(`${LOG_PREFIX5}: No items returned from initialize_run`);
|
|
13188
12245
|
return null;
|
|
13189
12246
|
}
|
|
13190
12247
|
const runId = responseData.id || "";
|
|
@@ -13197,7 +12254,7 @@ var SimulationHttpClient = class {
|
|
|
13197
12254
|
};
|
|
13198
12255
|
} catch (error) {
|
|
13199
12256
|
const errorMsg = this._extractErrorMessage(error);
|
|
13200
|
-
Logger.error(`${
|
|
12257
|
+
Logger.error(`${LOG_PREFIX5}: Failed to initialize run:`, errorMsg);
|
|
13201
12258
|
return null;
|
|
13202
12259
|
}
|
|
13203
12260
|
}
|
|
@@ -13208,7 +12265,7 @@ var SimulationHttpClient = class {
|
|
|
13208
12265
|
*/
|
|
13209
12266
|
async generateFirstTurn(runId, runItemId) {
|
|
13210
12267
|
if (!this.client) {
|
|
13211
|
-
Logger.error(`${
|
|
12268
|
+
Logger.error(`${LOG_PREFIX5}: Client not initialized`);
|
|
13212
12269
|
return null;
|
|
13213
12270
|
}
|
|
13214
12271
|
try {
|
|
@@ -13226,7 +12283,7 @@ var SimulationHttpClient = class {
|
|
|
13226
12283
|
} catch (error) {
|
|
13227
12284
|
const errorMsg = this._extractErrorMessage(error);
|
|
13228
12285
|
Logger.error(
|
|
13229
|
-
`${
|
|
12286
|
+
`${LOG_PREFIX5}: Failed to generate first turn for item ${runItemId}:`,
|
|
13230
12287
|
errorMsg
|
|
13231
12288
|
);
|
|
13232
12289
|
return null;
|
|
@@ -13237,7 +12294,7 @@ var SimulationHttpClient = class {
|
|
|
13237
12294
|
*/
|
|
13238
12295
|
async triggerConversation(message, turnId, sessionId, traceId) {
|
|
13239
12296
|
if (!this.client) {
|
|
13240
|
-
Logger.error(`${
|
|
12297
|
+
Logger.error(`${LOG_PREFIX5}: Client not initialized`);
|
|
13241
12298
|
return null;
|
|
13242
12299
|
}
|
|
13243
12300
|
try {
|
|
@@ -13260,7 +12317,7 @@ var SimulationHttpClient = class {
|
|
|
13260
12317
|
}
|
|
13261
12318
|
const userMessages = responseData.userMessages || [];
|
|
13262
12319
|
if (userMessages.length === 0) {
|
|
13263
|
-
Logger.warn(`${
|
|
12320
|
+
Logger.warn(`${LOG_PREFIX5}: No user messages in continue response`);
|
|
13264
12321
|
return null;
|
|
13265
12322
|
}
|
|
13266
12323
|
const nextMsg = userMessages[0];
|
|
@@ -13273,7 +12330,7 @@ var SimulationHttpClient = class {
|
|
|
13273
12330
|
};
|
|
13274
12331
|
} catch (error) {
|
|
13275
12332
|
const errorMsg = this._extractErrorMessage(error);
|
|
13276
|
-
Logger.error(`${
|
|
12333
|
+
Logger.error(`${LOG_PREFIX5}: Failed to trigger conversation:`, errorMsg);
|
|
13277
12334
|
return null;
|
|
13278
12335
|
}
|
|
13279
12336
|
}
|
|
@@ -13292,17 +12349,17 @@ var SimulationHttpClient = class {
|
|
|
13292
12349
|
*/
|
|
13293
12350
|
async reportFailure(runId, runItemId, error, status = "failed") {
|
|
13294
12351
|
if (!this.client) {
|
|
13295
|
-
Logger.error(`${
|
|
12352
|
+
Logger.error(`${LOG_PREFIX5}: Client not initialized`);
|
|
13296
12353
|
return;
|
|
13297
12354
|
}
|
|
13298
12355
|
try {
|
|
13299
12356
|
const url = `/evaluations/run/${runId}/item/${runItemId}/status`;
|
|
13300
12357
|
const payload = { status, failureReason: error };
|
|
13301
12358
|
await this.client.patch(url, payload);
|
|
13302
|
-
Logger.info(`${
|
|
12359
|
+
Logger.info(`${LOG_PREFIX5}: Reported failure - ${error}`);
|
|
13303
12360
|
} catch (err) {
|
|
13304
12361
|
const errorMsg = this._extractErrorMessage(err);
|
|
13305
|
-
Logger.error(`${
|
|
12362
|
+
Logger.error(`${LOG_PREFIX5}: Failed to report failure:`, errorMsg);
|
|
13306
12363
|
}
|
|
13307
12364
|
}
|
|
13308
12365
|
/**
|
|
@@ -13311,7 +12368,7 @@ var SimulationHttpClient = class {
|
|
|
13311
12368
|
async postRunStatus(runId, status) {
|
|
13312
12369
|
if (!this.client) {
|
|
13313
12370
|
Logger.error(
|
|
13314
|
-
`${
|
|
12371
|
+
`${LOG_PREFIX5}: Client not initialized; cannot post run status`
|
|
13315
12372
|
);
|
|
13316
12373
|
return { success: false };
|
|
13317
12374
|
}
|
|
@@ -13321,14 +12378,14 @@ var SimulationHttpClient = class {
|
|
|
13321
12378
|
const response = await this.client.post(url, payload);
|
|
13322
12379
|
const data = response.data;
|
|
13323
12380
|
if (data && typeof data === "object" && "data" in data) {
|
|
13324
|
-
Logger.info(`${
|
|
12381
|
+
Logger.info(`${LOG_PREFIX5}: Completed test run successfully`);
|
|
13325
12382
|
return data.data || {};
|
|
13326
12383
|
}
|
|
13327
12384
|
return data;
|
|
13328
12385
|
} catch (error) {
|
|
13329
12386
|
const errorMsg = this._extractErrorMessage(error);
|
|
13330
12387
|
Logger.error(
|
|
13331
|
-
`${
|
|
12388
|
+
`${LOG_PREFIX5}: Failed to post run status for run '${runId}':`,
|
|
13332
12389
|
errorMsg
|
|
13333
12390
|
);
|
|
13334
12391
|
return { success: false };
|
|
@@ -13353,7 +12410,7 @@ var SimulationHttpClient = class {
|
|
|
13353
12410
|
};
|
|
13354
12411
|
|
|
13355
12412
|
// src/simulation/hooks.ts
|
|
13356
|
-
var
|
|
12413
|
+
var LOG_PREFIX6 = "netra.simulation";
|
|
13357
12414
|
function describeHooks(hooks) {
|
|
13358
12415
|
const descFn = (fn) => {
|
|
13359
12416
|
if (!fn) return null;
|
|
@@ -13397,11 +12454,11 @@ function describeHooks(hooks) {
|
|
|
13397
12454
|
}
|
|
13398
12455
|
async function runBeforeAll(hooks) {
|
|
13399
12456
|
if (!hooks?.beforeAll) return null;
|
|
13400
|
-
Logger.info(`${
|
|
12457
|
+
Logger.info(`${LOG_PREFIX6}: running beforeAll hook`);
|
|
13401
12458
|
const result = await hooks.beforeAll();
|
|
13402
12459
|
if (result !== null && result !== void 0 && typeof result !== "object") {
|
|
13403
12460
|
Logger.warn(
|
|
13404
|
-
`${
|
|
12461
|
+
`${LOG_PREFIX6}: beforeAll returned ${typeof result} (expected object or null); ignoring value`
|
|
13405
12462
|
);
|
|
13406
12463
|
return null;
|
|
13407
12464
|
}
|
|
@@ -13410,7 +12467,7 @@ async function runBeforeAll(hooks) {
|
|
|
13410
12467
|
async function runBefore(hooks, datasetItemId, sharedContext) {
|
|
13411
12468
|
if (hooks?.before && datasetItemId in hooks.before) {
|
|
13412
12469
|
Logger.info(
|
|
13413
|
-
`${
|
|
12470
|
+
`${LOG_PREFIX6}: running before hook for datasetItemId=${datasetItemId}`
|
|
13414
12471
|
);
|
|
13415
12472
|
const itemHook = hooks.before[datasetItemId];
|
|
13416
12473
|
const result = await itemHook(sharedContext);
|
|
@@ -13420,7 +12477,7 @@ async function runBefore(hooks, datasetItemId, sharedContext) {
|
|
|
13420
12477
|
Object.assign(base, result);
|
|
13421
12478
|
} else {
|
|
13422
12479
|
Logger.warn(
|
|
13423
|
-
`${
|
|
12480
|
+
`${LOG_PREFIX6}: before hook returned ${typeof result} (expected object or null); ignoring value`
|
|
13424
12481
|
);
|
|
13425
12482
|
}
|
|
13426
12483
|
}
|
|
@@ -13431,7 +12488,7 @@ async function runBefore(hooks, datasetItemId, sharedContext) {
|
|
|
13431
12488
|
async function runBeforeEach(hooks, datasetItemId, sharedContext) {
|
|
13432
12489
|
if (!hooks?.beforeEach) return sharedContext;
|
|
13433
12490
|
Logger.info(
|
|
13434
|
-
`${
|
|
12491
|
+
`${LOG_PREFIX6}: running beforeEach hook for datasetItemId=${datasetItemId}`
|
|
13435
12492
|
);
|
|
13436
12493
|
const result = await hooks.beforeEach(sharedContext);
|
|
13437
12494
|
const base = { ...sharedContext || {} };
|
|
@@ -13440,7 +12497,7 @@ async function runBeforeEach(hooks, datasetItemId, sharedContext) {
|
|
|
13440
12497
|
Object.assign(base, result);
|
|
13441
12498
|
} else {
|
|
13442
12499
|
Logger.warn(
|
|
13443
|
-
`${
|
|
12500
|
+
`${LOG_PREFIX6}: beforeEach hook returned ${typeof result} (expected object or null); ignoring value`
|
|
13444
12501
|
);
|
|
13445
12502
|
}
|
|
13446
12503
|
}
|
|
@@ -13449,7 +12506,7 @@ async function runBeforeEach(hooks, datasetItemId, sharedContext) {
|
|
|
13449
12506
|
async function runAfter(hooks, datasetItemId, itemResult, setupContext) {
|
|
13450
12507
|
if (hooks?.after && datasetItemId in hooks.after) {
|
|
13451
12508
|
Logger.info(
|
|
13452
|
-
`${
|
|
12509
|
+
`${LOG_PREFIX6}: running after hook for datasetItemId=${datasetItemId}`
|
|
13453
12510
|
);
|
|
13454
12511
|
await hooks.after[datasetItemId](itemResult, setupContext);
|
|
13455
12512
|
}
|
|
@@ -13457,18 +12514,18 @@ async function runAfter(hooks, datasetItemId, itemResult, setupContext) {
|
|
|
13457
12514
|
async function runAfterEach(hooks, datasetItemId, itemResult, setupContext) {
|
|
13458
12515
|
if (!hooks?.afterEach) return;
|
|
13459
12516
|
Logger.info(
|
|
13460
|
-
`${
|
|
12517
|
+
`${LOG_PREFIX6}: running afterEach hook for datasetItemId=${datasetItemId}`
|
|
13461
12518
|
);
|
|
13462
12519
|
await hooks.afterEach(itemResult, setupContext);
|
|
13463
12520
|
}
|
|
13464
12521
|
async function runAfterAll(hooks, results, sharedContext) {
|
|
13465
12522
|
if (!hooks?.afterAll) return;
|
|
13466
|
-
Logger.info(`${
|
|
12523
|
+
Logger.info(`${LOG_PREFIX6}: running afterAll hook`);
|
|
13467
12524
|
await hooks.afterAll(results, sharedContext);
|
|
13468
12525
|
}
|
|
13469
12526
|
|
|
13470
12527
|
// src/simulation/api.ts
|
|
13471
|
-
var
|
|
12528
|
+
var LOG_PREFIX7 = "netra.simulation";
|
|
13472
12529
|
var SPAN_NAME = "Netra.Simulation.TestRun";
|
|
13473
12530
|
var Simulation = class {
|
|
13474
12531
|
constructor(config) {
|
|
@@ -13492,7 +12549,7 @@ var Simulation = class {
|
|
|
13492
12549
|
name,
|
|
13493
12550
|
datasetId,
|
|
13494
12551
|
task: task2,
|
|
13495
|
-
context:
|
|
12552
|
+
context: context16,
|
|
13496
12553
|
maxConcurrency = 5,
|
|
13497
12554
|
hooks
|
|
13498
12555
|
} = options;
|
|
@@ -13504,7 +12561,7 @@ var Simulation = class {
|
|
|
13504
12561
|
const initResult = await this._client.initializeRun(
|
|
13505
12562
|
name,
|
|
13506
12563
|
datasetId,
|
|
13507
|
-
|
|
12564
|
+
context16,
|
|
13508
12565
|
hooksMeta && Object.keys(hooksMeta).length > 0 ? hooksMeta : null
|
|
13509
12566
|
);
|
|
13510
12567
|
if (!initResult) {
|
|
@@ -13512,7 +12569,7 @@ var Simulation = class {
|
|
|
13512
12569
|
}
|
|
13513
12570
|
const { runId, items } = initResult;
|
|
13514
12571
|
if (!items || items.length === 0) {
|
|
13515
|
-
Logger.error(`${
|
|
12572
|
+
Logger.error(`${LOG_PREFIX7}: No items returned from initialize_run`);
|
|
13516
12573
|
return null;
|
|
13517
12574
|
}
|
|
13518
12575
|
let interrupted = false;
|
|
@@ -13554,7 +12611,7 @@ var Simulation = class {
|
|
|
13554
12611
|
sharedContext = await runBeforeAll(hooks);
|
|
13555
12612
|
} catch (error) {
|
|
13556
12613
|
const errorMsg = `beforeAll hook failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
13557
|
-
Logger.error(`${
|
|
12614
|
+
Logger.error(`${LOG_PREFIX7}: ${errorMsg} \u2014 aborting run (no LLM spent)`);
|
|
13558
12615
|
await this._reportFailures(
|
|
13559
12616
|
runId,
|
|
13560
12617
|
items.map((item) => item.runItemId),
|
|
@@ -13576,7 +12633,7 @@ var Simulation = class {
|
|
|
13576
12633
|
await runAfterAll(hooks, results, null);
|
|
13577
12634
|
} catch (afterAllError) {
|
|
13578
12635
|
const afterAllMsg = `afterAll hook failed: ${afterAllError instanceof Error ? afterAllError.message : String(afterAllError)}`;
|
|
13579
|
-
Logger.error(`${
|
|
12636
|
+
Logger.error(`${LOG_PREFIX7}: ${afterAllMsg}`);
|
|
13580
12637
|
}
|
|
13581
12638
|
await this._client.postRunStatus(runId, "completed");
|
|
13582
12639
|
return results;
|
|
@@ -13599,7 +12656,7 @@ var Simulation = class {
|
|
|
13599
12656
|
setupContexts.set(runItemId, setupContext);
|
|
13600
12657
|
} catch (error) {
|
|
13601
12658
|
const errorMsg = `before hook failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
13602
|
-
Logger.error(`${
|
|
12659
|
+
Logger.error(`${LOG_PREFIX7}: ${errorMsg} for runItemId=${runItemId}`);
|
|
13603
12660
|
await this._client.reportFailure(runId, runItemId, errorMsg, "prescript_failed");
|
|
13604
12661
|
const itemResult = {
|
|
13605
12662
|
runItemId,
|
|
@@ -13615,7 +12672,7 @@ var Simulation = class {
|
|
|
13615
12672
|
}
|
|
13616
12673
|
const simItem = await this._client.generateFirstTurn(runId, runItemId);
|
|
13617
12674
|
if (!simItem) {
|
|
13618
|
-
Logger.warn(`${
|
|
12675
|
+
Logger.warn(`${LOG_PREFIX7}: Failed to generate first turn for item ${runItemId}, marking failed`);
|
|
13619
12676
|
await this._client.reportFailure(runId, runItemId, "Failed to generate first user message");
|
|
13620
12677
|
const itemResult = {
|
|
13621
12678
|
runItemId,
|
|
@@ -13631,7 +12688,7 @@ var Simulation = class {
|
|
|
13631
12688
|
);
|
|
13632
12689
|
await Promise.all(setupPromises);
|
|
13633
12690
|
if (simulationItems.length === 0) {
|
|
13634
|
-
Logger.error(`${
|
|
12691
|
+
Logger.error(`${LOG_PREFIX7}: All items failed during setup/generation`);
|
|
13635
12692
|
const results = {
|
|
13636
12693
|
success: false,
|
|
13637
12694
|
completed: [],
|
|
@@ -13642,12 +12699,12 @@ var Simulation = class {
|
|
|
13642
12699
|
await runAfterAll(hooks, results, sharedContext);
|
|
13643
12700
|
} catch (afterAllError) {
|
|
13644
12701
|
const afterAllMsg = `afterAll hook failed: ${afterAllError instanceof Error ? afterAllError.message : String(afterAllError)}`;
|
|
13645
|
-
Logger.error(`${
|
|
12702
|
+
Logger.error(`${LOG_PREFIX7}: ${afterAllMsg}`);
|
|
13646
12703
|
}
|
|
13647
12704
|
await this._client.postRunStatus(runId, "completed");
|
|
13648
12705
|
return results;
|
|
13649
12706
|
}
|
|
13650
|
-
Logger.info(`${
|
|
12707
|
+
Logger.info(`${LOG_PREFIX7}: Starting simulation with ${simulationItems.length} items`);
|
|
13651
12708
|
try {
|
|
13652
12709
|
const result = await this._runSimulationAsync(
|
|
13653
12710
|
runId,
|
|
@@ -13660,11 +12717,11 @@ var Simulation = class {
|
|
|
13660
12717
|
failedItems
|
|
13661
12718
|
);
|
|
13662
12719
|
const elapsedTime = ((Date.now() - startTime) / 1e3).toFixed(2);
|
|
13663
|
-
Logger.info(`${
|
|
12720
|
+
Logger.info(`${LOG_PREFIX7}: Simulation completed in ${elapsedTime} seconds`);
|
|
13664
12721
|
await this._client.postRunStatus(runId, "completed");
|
|
13665
12722
|
return result;
|
|
13666
12723
|
} catch (error) {
|
|
13667
|
-
Logger.error(`${
|
|
12724
|
+
Logger.error(`${LOG_PREFIX7}: Run simulation failed`);
|
|
13668
12725
|
await this._client.postRunStatus(runId, "failed");
|
|
13669
12726
|
throw error;
|
|
13670
12727
|
}
|
|
@@ -13706,7 +12763,7 @@ var Simulation = class {
|
|
|
13706
12763
|
}
|
|
13707
12764
|
processedCount++;
|
|
13708
12765
|
Logger.info(
|
|
13709
|
-
`${
|
|
12766
|
+
`${LOG_PREFIX7}: ${processedCount}/${runItems.length} processed (run_item_id=${runItem.runItemId})`
|
|
13710
12767
|
);
|
|
13711
12768
|
return result;
|
|
13712
12769
|
})
|
|
@@ -13717,7 +12774,7 @@ var Simulation = class {
|
|
|
13717
12774
|
await runAfterAll(hooks, results, sharedContext);
|
|
13718
12775
|
} catch (error) {
|
|
13719
12776
|
const errorMsg = `afterAll hook failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
13720
|
-
Logger.error(`${
|
|
12777
|
+
Logger.error(`${LOG_PREFIX7}: ${errorMsg}`);
|
|
13721
12778
|
const completed = results.completed;
|
|
13722
12779
|
const successfulIds = completed.map((item) => item.runItemId);
|
|
13723
12780
|
await this._reportFailures(
|
|
@@ -13737,7 +12794,7 @@ var Simulation = class {
|
|
|
13737
12794
|
}
|
|
13738
12795
|
results.success = results.failed.length === 0;
|
|
13739
12796
|
Logger.info(
|
|
13740
|
-
`${
|
|
12797
|
+
`${LOG_PREFIX7}: Completed=${results.completed.length}, Failed=${results.failed.length}`
|
|
13741
12798
|
);
|
|
13742
12799
|
return results;
|
|
13743
12800
|
}
|
|
@@ -13768,14 +12825,14 @@ var Simulation = class {
|
|
|
13768
12825
|
await runAfter(hooks, datasetItemId, itemResult, setupContext);
|
|
13769
12826
|
} catch (error) {
|
|
13770
12827
|
const errorMsg = `after hook failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
13771
|
-
Logger.error(`${
|
|
12828
|
+
Logger.error(`${LOG_PREFIX7}: ${errorMsg} for runItemId=${runItemId}`);
|
|
13772
12829
|
errors.push(errorMsg);
|
|
13773
12830
|
}
|
|
13774
12831
|
try {
|
|
13775
12832
|
await runAfterEach(hooks, datasetItemId, itemResult, setupContext);
|
|
13776
12833
|
} catch (error) {
|
|
13777
12834
|
const errorMsg = `afterEach hook failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
13778
|
-
Logger.error(`${
|
|
12835
|
+
Logger.error(`${LOG_PREFIX7}: ${errorMsg} for runItemId=${runItemId}`);
|
|
13779
12836
|
errors.push(errorMsg);
|
|
13780
12837
|
}
|
|
13781
12838
|
if (errors.length > 0 && itemResult.success) {
|
|
@@ -13808,7 +12865,7 @@ var Simulation = class {
|
|
|
13808
12865
|
const span2 = new SpanWrapper(
|
|
13809
12866
|
SPAN_NAME,
|
|
13810
12867
|
{ [Config.TRACE_ORIGIN_KEY]: Config.TRACE_ORIGIN_EVALUATION },
|
|
13811
|
-
|
|
12868
|
+
LOG_PREFIX7
|
|
13812
12869
|
);
|
|
13813
12870
|
span2.start();
|
|
13814
12871
|
try {
|
|
@@ -13839,7 +12896,7 @@ var Simulation = class {
|
|
|
13839
12896
|
}
|
|
13840
12897
|
if (response.decision === "stop") {
|
|
13841
12898
|
Logger.info(
|
|
13842
|
-
`${
|
|
12899
|
+
`${LOG_PREFIX7}: Completed run_item_id=${runItemId} reason=${response.reason}`
|
|
13843
12900
|
);
|
|
13844
12901
|
const itemResult = {
|
|
13845
12902
|
runItemId,
|
|
@@ -13855,7 +12912,7 @@ var Simulation = class {
|
|
|
13855
12912
|
} catch (error) {
|
|
13856
12913
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
13857
12914
|
Logger.error(
|
|
13858
|
-
`${
|
|
12915
|
+
`${LOG_PREFIX7}: Task failed run_item_id=${runItemId}, turn_id=${turnId}: ${errorMsg}`
|
|
13859
12916
|
);
|
|
13860
12917
|
await this._client.reportFailure(runId, runItemId, errorMsg);
|
|
13861
12918
|
const itemResult = {
|
|
@@ -13872,7 +12929,7 @@ var Simulation = class {
|
|
|
13872
12929
|
}
|
|
13873
12930
|
}
|
|
13874
12931
|
};
|
|
13875
|
-
var
|
|
12932
|
+
var Tracer18 = class {
|
|
13876
12933
|
constructor(cfg, effectiveProvider) {
|
|
13877
12934
|
if (effectiveProvider && typeof effectiveProvider.getTracer === "function") {
|
|
13878
12935
|
this.tracer = effectiveProvider.getTracer(Config.LIBRARY_NAME, Config.LIBRARY_VERSION);
|
|
@@ -13883,8 +12940,8 @@ var Tracer19 = class {
|
|
|
13883
12940
|
};
|
|
13884
12941
|
function spanHasOutput(span2) {
|
|
13885
12942
|
try {
|
|
13886
|
-
for (const
|
|
13887
|
-
const attrs = span2[
|
|
12943
|
+
for (const field of ["attributes", "_attributes"]) {
|
|
12944
|
+
const attrs = span2[field];
|
|
13888
12945
|
if (attrs && typeof attrs === "object" && attrs["output"]) return true;
|
|
13889
12946
|
}
|
|
13890
12947
|
} catch (e) {
|
|
@@ -14087,7 +13144,7 @@ var Netra = class {
|
|
|
14087
13144
|
config.blockInstruments,
|
|
14088
13145
|
config.rootInstruments
|
|
14089
13146
|
);
|
|
14090
|
-
const tracer = new
|
|
13147
|
+
const tracer = new Tracer18(cfg, effectiveProvider);
|
|
14091
13148
|
this._tracer = tracer.tracer;
|
|
14092
13149
|
try {
|
|
14093
13150
|
this.usage = new Usage(cfg);
|