toad-eye 2.4.3 → 2.4.5
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/core/tracer.js
CHANGED
|
@@ -3,7 +3,7 @@ import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
|
3
3
|
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
4
4
|
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
5
5
|
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
|
|
6
|
-
import { TraceIdRatioBasedSampler, ParentBasedSampler, } from "@opentelemetry/sdk-trace-node";
|
|
6
|
+
import { TraceIdRatioBasedSampler, ParentBasedSampler, BatchSpanProcessor, } from "@opentelemetry/sdk-trace-node";
|
|
7
7
|
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
8
8
|
import { initMetrics, resetMetrics } from "./metrics.js";
|
|
9
9
|
import { resetCustomPricing } from "./pricing.js";
|
|
@@ -74,9 +74,11 @@ export function initObservability(config) {
|
|
|
74
74
|
exporter: metricExporter,
|
|
75
75
|
exportIntervalMillis: isCloudMode ? 10_000 : 5_000,
|
|
76
76
|
});
|
|
77
|
-
//
|
|
77
|
+
// When custom SpanProcessors are needed (e.g., Vercel AI SDK),
|
|
78
|
+
// we must also include a BatchSpanProcessor for trace export —
|
|
79
|
+
// NodeSDK won't create one automatically when spanProcessors is provided.
|
|
78
80
|
const spanProcessors = config.instrument?.includes("ai")
|
|
79
|
-
? [new ToadEyeAISpanProcessor()]
|
|
81
|
+
? [new BatchSpanProcessor(traceExporter), new ToadEyeAISpanProcessor()]
|
|
80
82
|
: [];
|
|
81
83
|
// SDK-side head sampling (default: 1.0 = send everything to Collector)
|
|
82
84
|
const sdkRate = config.sampling?.sdkRate ?? 1.0;
|
|
@@ -50,6 +50,23 @@ const messagesCreate = {
|
|
|
50
50
|
if (event.type === "content_block_delta" && event.delta?.text) {
|
|
51
51
|
acc.completion += event.delta.text;
|
|
52
52
|
}
|
|
53
|
+
// Tool use: content_block_start with type "tool_use"
|
|
54
|
+
if (event.type === "content_block_start" &&
|
|
55
|
+
event.content_block?.type === "tool_use") {
|
|
56
|
+
acc.toolCalls.push({
|
|
57
|
+
name: event.content_block.name ?? "",
|
|
58
|
+
arguments: "",
|
|
59
|
+
id: event.content_block.id,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
// Tool use: input_json_delta accumulates arguments
|
|
63
|
+
if (event.type === "content_block_delta" &&
|
|
64
|
+
event.delta?.type === "input_json_delta" &&
|
|
65
|
+
event.delta.partial_json) {
|
|
66
|
+
const last = acc.toolCalls[acc.toolCalls.length - 1];
|
|
67
|
+
if (last)
|
|
68
|
+
last.arguments += event.delta.partial_json;
|
|
69
|
+
}
|
|
53
70
|
if (event.type === "message_start" && event.message?.usage) {
|
|
54
71
|
acc.inputTokens = event.message.usage.input_tokens ?? 0;
|
|
55
72
|
}
|
|
@@ -33,6 +33,7 @@ async function* wrapAsyncIterable(stream, accumulate, onFirstChunk, onComplete,
|
|
|
33
33
|
completion: "",
|
|
34
34
|
inputTokens: 0,
|
|
35
35
|
outputTokens: 0,
|
|
36
|
+
toolCalls: [],
|
|
36
37
|
};
|
|
37
38
|
let firstChunk = true;
|
|
38
39
|
let completed = false;
|
|
@@ -66,6 +67,14 @@ function createStreamingHandler(
|
|
|
66
67
|
original, patch, providerName) {
|
|
67
68
|
const tracer = trace.getTracer(INSTRUMENTATION_NAME);
|
|
68
69
|
return async function handleStreaming(thisArg, body, rest) {
|
|
70
|
+
// OpenAI does not send usage in streaming chunks by default.
|
|
71
|
+
// Auto-inject stream_options to get token counts in the final chunk.
|
|
72
|
+
if (providerName === "openai") {
|
|
73
|
+
const b = body;
|
|
74
|
+
if (!b["stream_options"]) {
|
|
75
|
+
b["stream_options"] = { include_usage: true };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
69
78
|
// Pass thisArg so extractRequest can access instance properties (e.g., Gemini model name)
|
|
70
79
|
const req = patch.extractRequest(body, thisArg);
|
|
71
80
|
const start = performance.now();
|
|
@@ -132,6 +141,11 @@ original, patch, providerName) {
|
|
|
132
141
|
const duration = performance.now() - start;
|
|
133
142
|
const cost = calculateCost(effectiveModel, acc.inputTokens, acc.outputTokens);
|
|
134
143
|
const processedCompletion = processContent(acc.completion);
|
|
144
|
+
// Record tool calls from streaming chunks
|
|
145
|
+
if (acc.toolCalls.length > 0) {
|
|
146
|
+
span.setAttribute(GEN_AI_ATTRS.TOOL_NAME, acc.toolCalls.map((t) => t.name).join(", "));
|
|
147
|
+
span.setAttribute("gen_ai.tool.call.count", acc.toolCalls.length);
|
|
148
|
+
}
|
|
135
149
|
span.setAttributes({
|
|
136
150
|
...(processedCompletion !== undefined && {
|
|
137
151
|
[GEN_AI_ATTRS.COMPLETION]: processedCompletion,
|
|
@@ -66,6 +66,18 @@ const generateContentStream = {
|
|
|
66
66
|
catch {
|
|
67
67
|
// text() may throw if content is blocked
|
|
68
68
|
}
|
|
69
|
+
// Gemini function calls in streaming
|
|
70
|
+
const parts = c?.candidates?.[0]?.content?.parts;
|
|
71
|
+
if (parts) {
|
|
72
|
+
for (const part of parts) {
|
|
73
|
+
if (part.functionCall) {
|
|
74
|
+
acc.toolCalls.push({
|
|
75
|
+
name: part.functionCall.name,
|
|
76
|
+
arguments: JSON.stringify(part.functionCall.args ?? {}),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
69
81
|
if (c?.usageMetadata) {
|
|
70
82
|
acc.inputTokens = c.usageMetadata.promptTokenCount ?? acc.inputTokens;
|
|
71
83
|
acc.outputTokens =
|
|
@@ -47,9 +47,26 @@ const chatCompletions = {
|
|
|
47
47
|
isStreaming: (body) => !!body?.stream,
|
|
48
48
|
accumulateChunk: (acc, chunk) => {
|
|
49
49
|
const c = chunk;
|
|
50
|
-
const delta = c?.choices?.[0]?.delta
|
|
51
|
-
if (delta)
|
|
52
|
-
acc.completion += delta;
|
|
50
|
+
const delta = c?.choices?.[0]?.delta;
|
|
51
|
+
if (delta?.content)
|
|
52
|
+
acc.completion += delta.content;
|
|
53
|
+
// Accumulate tool calls from streaming chunks
|
|
54
|
+
if (delta?.tool_calls) {
|
|
55
|
+
for (const tc of delta.tool_calls) {
|
|
56
|
+
const existing = acc.toolCalls[tc.index];
|
|
57
|
+
if (existing) {
|
|
58
|
+
if (tc.function?.arguments)
|
|
59
|
+
existing.arguments += tc.function.arguments;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
acc.toolCalls[tc.index] = {
|
|
63
|
+
name: tc.function?.name ?? "",
|
|
64
|
+
arguments: tc.function?.arguments ?? "",
|
|
65
|
+
id: tc.id,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
53
70
|
if (c?.usage) {
|
|
54
71
|
acc.inputTokens = c.usage.prompt_tokens ?? acc.inputTokens;
|
|
55
72
|
acc.outputTokens = c.usage.completion_tokens ?? acc.outputTokens;
|
|
@@ -6,11 +6,18 @@ export interface Instrumentation {
|
|
|
6
6
|
/** Restore original methods. */
|
|
7
7
|
disable(): void;
|
|
8
8
|
}
|
|
9
|
+
/** Tool call accumulated from streaming chunks. */
|
|
10
|
+
export interface AccumulatedToolCall {
|
|
11
|
+
name: string;
|
|
12
|
+
arguments: string;
|
|
13
|
+
id?: string | undefined;
|
|
14
|
+
}
|
|
9
15
|
/** Accumulated stream data — only primitives, no raw SDK objects. */
|
|
10
16
|
export interface StreamAccumulator {
|
|
11
17
|
completion: string;
|
|
12
18
|
inputTokens: number;
|
|
13
19
|
outputTokens: number;
|
|
20
|
+
toolCalls: AccumulatedToolCall[];
|
|
14
21
|
}
|
|
15
22
|
/** Describes a single method to monkey-patch on an SDK prototype. */
|
|
16
23
|
export interface PatchTarget {
|