@vercel/agent-runs 1.1.0 → 1.1.1
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/CHANGELOG.md +5 -4
- package/README.md +27 -14
- package/dist/src/adapters/ai-sdk-otel.d.ts +9 -0
- package/dist/src/adapters/ai-sdk-otel.d.ts.map +1 -0
- package/dist/src/adapters/ai-sdk-otel.js +77 -0
- package/dist/src/adapters/ai-sdk-otel.js.map +1 -0
- package/dist/src/adapters/ai-sdk-telemetry.d.ts +10 -9
- package/dist/src/adapters/ai-sdk-telemetry.d.ts.map +1 -1
- package/dist/src/adapters/ai-sdk-telemetry.js +149 -38
- package/dist/src/adapters/ai-sdk-telemetry.js.map +1 -1
- package/dist/src/adapters/otel.d.ts +1 -0
- package/dist/src/adapters/otel.d.ts.map +1 -1
- package/dist/src/adapters/otel.js +13 -6
- package/dist/src/adapters/otel.js.map +1 -1
- package/dist/src/agent-run.d.ts.map +1 -1
- package/dist/src/agent-run.js +4 -1
- package/dist/src/agent-run.js.map +1 -1
- package/dist/src/ai-sdk.d.ts +1 -1
- package/dist/src/ai-sdk.d.ts.map +1 -1
- package/dist/src/ai-sdk.js +1 -1
- package/dist/src/ai-sdk.js.map +1 -1
- package/dist/src/core/run-telemetry.d.ts +7 -0
- package/dist/src/core/run-telemetry.d.ts.map +1 -0
- package/dist/src/core/run-telemetry.js +2 -0
- package/dist/src/core/run-telemetry.js.map +1 -0
- package/dist/src/core/scopes.d.ts.map +1 -1
- package/dist/src/core/scopes.js +6 -1
- package/dist/src/core/scopes.js.map +1 -1
- package/package.json +6 -2
- package/src/adapters/ai-sdk-otel.ts +106 -0
- package/src/adapters/ai-sdk-telemetry.ts +192 -40
- package/src/adapters/otel.ts +28 -19
- package/src/agent-run.ts +4 -1
- package/src/ai-sdk.ts +5 -1
- package/src/core/run-telemetry.ts +7 -0
- package/src/core/scopes.ts +6 -1
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { context, trace, ProxyTracerProvider, createContextKey } from "@opentelemetry/api";
|
|
2
|
+
import { registerOTel } from "@vercel/otel";
|
|
3
|
+
import { currentCapture } from "../capture.js";
|
|
4
|
+
import { intersectCapture } from "../core/activation.js";
|
|
5
|
+
import { currentAgentHandoff } from "../core/delegation.js";
|
|
6
|
+
import { runTelemetry } from "../core/run-telemetry.js";
|
|
7
|
+
import type { RunIdentity } from "../core/types.js";
|
|
8
|
+
import type { Operation } from "../operations.js";
|
|
9
|
+
import { aiSdkOpenTelemetry } from "./ai-sdk-otel.js";
|
|
10
|
+
import { telemetryTracerProviders } from "./otel.js";
|
|
1
11
|
import type { Telemetry } from "ai";
|
|
2
12
|
import type { AgentRun, TurnInput } from "../agent-run.js";
|
|
3
13
|
import type {
|
|
@@ -12,43 +22,88 @@ type Event<K extends keyof Telemetry & `on${string}`> = Parameters<NonNullable<T
|
|
|
12
22
|
|
|
13
23
|
/** Options for `aiSdkTelemetry`. */
|
|
14
24
|
export interface AiSdkTelemetryOptions {
|
|
15
|
-
/**
|
|
16
|
-
readonly turn
|
|
25
|
+
/** Optional identity, capture, and placement overrides. Called once per AI SDK call. */
|
|
26
|
+
readonly turn?: AiSdkTurnInput | ((call: { readonly callId: string }) => AiSdkTurnInput);
|
|
17
27
|
}
|
|
18
28
|
|
|
29
|
+
/** Turn overrides; omitted IDs are generated for each call. */
|
|
30
|
+
export type AiSdkTurnInput = Partial<Omit<TurnInput, "identity">> & {
|
|
31
|
+
readonly identity?: Partial<RunIdentity>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
interface ActiveGeneration {
|
|
35
|
+
readonly identity: RunIdentity;
|
|
36
|
+
readonly operation: Operation;
|
|
37
|
+
readonly toolCallId?: string;
|
|
38
|
+
}
|
|
39
|
+
const ACTIVE_GENERATION = createContextKey("agent.tracing.ai-sdk.generation");
|
|
40
|
+
const METADATA_ONLY = { emit: true, recordInputs: false, recordOutputs: false };
|
|
41
|
+
|
|
19
42
|
interface Generation {
|
|
20
|
-
readonly
|
|
21
|
-
turn
|
|
43
|
+
readonly identity: RunIdentity;
|
|
44
|
+
readonly turn: Promise<TurnOperation>;
|
|
22
45
|
attempt?: Promise<AttemptOperation>;
|
|
23
46
|
model?: Promise<ModelOperation>;
|
|
47
|
+
modelFailure?: { readonly error: unknown };
|
|
24
48
|
readonly tools: Map<string, Promise<ToolOperation>>;
|
|
25
49
|
}
|
|
26
50
|
|
|
27
51
|
const ABANDONED = { outcome: "abandoned" } as const;
|
|
28
52
|
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
53
|
+
/** Reuses the run's telemetry for SDK turns and initializes the default OTel provider when needed. */
|
|
54
|
+
export function aiSdkTelemetry(run: AgentRun, options: AiSdkTelemetryOptions = {}): Telemetry {
|
|
55
|
+
const backend = runTelemetry.get(run);
|
|
56
|
+
const provider = trace.getTracerProvider();
|
|
57
|
+
if (
|
|
58
|
+
backend?.usesGlobalProvider &&
|
|
59
|
+
provider instanceof ProxyTracerProvider &&
|
|
60
|
+
provider.getDelegateTracer("agent.tracing") === undefined
|
|
61
|
+
)
|
|
62
|
+
registerOTel({ serviceName: "agent-runs", instrumentations: [] });
|
|
63
|
+
const native = aiSdkOpenTelemetry(
|
|
64
|
+
backend === undefined ? undefined : telemetryTracerProviders.get(backend.telemetry),
|
|
65
|
+
);
|
|
35
66
|
const generations = new Map<string, Generation>();
|
|
36
67
|
|
|
37
|
-
function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
68
|
+
function activeGeneration(): ActiveGeneration | undefined {
|
|
69
|
+
return context.active().getValue(ACTIVE_GENERATION) as ActiveGeneration | undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function turnOverrides(callId: string) {
|
|
73
|
+
return typeof options.turn === "function" ? options.turn({ callId }) : options.turn;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function emitsNativeSpans(callId: string): boolean {
|
|
77
|
+
return (
|
|
78
|
+
turnOverrides(callId)?.capture?.emit !== false &&
|
|
79
|
+
activeGeneration()?.operation.capture.emit !== false &&
|
|
80
|
+
backend?.telemetry.active()?.capture.emit !== false &&
|
|
81
|
+
currentCapture()?.emit !== false &&
|
|
82
|
+
currentAgentHandoff()?.capture.emit !== false
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function executeInGeneration<T>(
|
|
87
|
+
state: Generation,
|
|
88
|
+
operation: Operation,
|
|
89
|
+
execute: () => T,
|
|
90
|
+
toolCallId?: string,
|
|
91
|
+
): T {
|
|
92
|
+
const active: ActiveGeneration = { identity: state.identity, operation, toolCallId };
|
|
93
|
+
return operation.run(() =>
|
|
94
|
+
context.with(context.active().setValue(ACTIVE_GENERATION, active), execute),
|
|
95
|
+
);
|
|
44
96
|
}
|
|
45
97
|
|
|
46
98
|
async function closeStep(state: Generation, failure?: unknown) {
|
|
47
99
|
const result =
|
|
48
100
|
failure === undefined ? ABANDONED : { outcome: "abandoned", failed: true, error: failure };
|
|
49
|
-
|
|
101
|
+
const model = await state.model;
|
|
102
|
+
if (state.modelFailure === undefined) await model?.complete(result);
|
|
103
|
+
else await model?.fail(state.modelFailure.error);
|
|
50
104
|
for (const tool of state.tools.values()) await (await tool).complete(result);
|
|
51
105
|
state.model = undefined;
|
|
106
|
+
state.modelFailure = undefined;
|
|
52
107
|
state.tools.clear();
|
|
53
108
|
const attempt = await state.attempt;
|
|
54
109
|
state.attempt = undefined;
|
|
@@ -65,44 +120,127 @@ export function aiSdkTelemetry(run: AgentRun, options: AiSdkTelemetryOptions): T
|
|
|
65
120
|
generations.delete(callId);
|
|
66
121
|
if (state === undefined) return;
|
|
67
122
|
await closeStep(state, failure);
|
|
68
|
-
|
|
69
|
-
if (turn !== undefined) await end(turn);
|
|
123
|
+
await end(await state.turn);
|
|
70
124
|
}
|
|
71
125
|
|
|
72
126
|
return {
|
|
73
|
-
|
|
74
|
-
|
|
127
|
+
onObjectStepStart: native.telemetry.onObjectStepStart.bind(native.telemetry),
|
|
128
|
+
onObjectStepEnd: native.telemetry.onObjectStepEnd.bind(native.telemetry),
|
|
129
|
+
onEmbedStart: native.telemetry.onEmbedStart.bind(native.telemetry),
|
|
130
|
+
onEmbedEnd: native.telemetry.onEmbedEnd.bind(native.telemetry),
|
|
131
|
+
onRerankStart: native.telemetry.onRerankStart.bind(native.telemetry),
|
|
132
|
+
onRerankEnd: native.telemetry.onRerankEnd.bind(native.telemetry),
|
|
133
|
+
experimental_onDecideStart(event) {
|
|
134
|
+
if (emitsNativeSpans(event.callId)) native.telemetry.experimental_onDecideStart(event);
|
|
135
|
+
},
|
|
136
|
+
experimental_onDecideEnd: native.telemetry.experimental_onDecideEnd.bind(native.telemetry),
|
|
137
|
+
experimental_onDecisionModelCallStart:
|
|
138
|
+
native.telemetry.experimental_onDecisionModelCallStart.bind(native.telemetry),
|
|
139
|
+
experimental_onDecisionModelCallEnd: native.telemetry.experimental_onDecisionModelCallEnd.bind(
|
|
140
|
+
native.telemetry,
|
|
141
|
+
),
|
|
142
|
+
experimental_onStreamTranscriptionStart(event) {
|
|
143
|
+
if (!emitsNativeSpans(event.callId)) return;
|
|
144
|
+
native.telemetry.experimental_onStreamTranscriptionStart({
|
|
145
|
+
...event,
|
|
146
|
+
recordInputs: false,
|
|
147
|
+
recordOutputs: false,
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
experimental_onStreamTranscriptionEnd:
|
|
151
|
+
native.telemetry.experimental_onStreamTranscriptionEnd.bind(native.telemetry),
|
|
152
|
+
async onStart(event: Event<"onStart">) {
|
|
153
|
+
if (!["ai.generateText", "ai.streamText", "ai.harness"].includes(event.operationId)) {
|
|
154
|
+
if (emitsNativeSpans(event.callId))
|
|
155
|
+
native.telemetry.onStart({ ...event, recordInputs: false, recordOutputs: false });
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const inherited = activeGeneration();
|
|
159
|
+
const active = backend?.telemetry.active();
|
|
160
|
+
const overrides = turnOverrides(event.callId);
|
|
161
|
+
const identity: RunIdentity = {
|
|
162
|
+
conversationId:
|
|
163
|
+
overrides?.identity?.conversationId ?? inherited?.identity.conversationId ?? event.callId,
|
|
164
|
+
runId: overrides?.identity?.runId ?? event.callId,
|
|
165
|
+
turnId: overrides?.identity?.turnId ?? event.callId,
|
|
166
|
+
};
|
|
167
|
+
const owner = inherited?.operation.reference ?? active?.reference;
|
|
168
|
+
const host = backend?.telemetry.activeSpan?.();
|
|
169
|
+
const parent =
|
|
170
|
+
owner === undefined || host?.traceId === owner.traceId ? (host ?? owner) : owner;
|
|
171
|
+
const capture = intersectCapture(
|
|
172
|
+
intersectCapture(
|
|
173
|
+
intersectCapture(overrides?.capture ?? METADATA_ONLY, inherited?.operation.capture),
|
|
174
|
+
active?.capture,
|
|
175
|
+
),
|
|
176
|
+
{
|
|
177
|
+
emit: true,
|
|
178
|
+
recordInputs: event.recordInputs !== false,
|
|
179
|
+
recordOutputs: event.recordOutputs !== false,
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
const state: Generation = {
|
|
183
|
+
identity,
|
|
184
|
+
tools: new Map(),
|
|
185
|
+
turn: run.turn({
|
|
186
|
+
sequence: 0,
|
|
187
|
+
agentName: event.functionId,
|
|
188
|
+
framework: { name: "ai-sdk", version: event.aiVersion },
|
|
189
|
+
context: context.active(),
|
|
190
|
+
parent,
|
|
191
|
+
lineage:
|
|
192
|
+
inherited === undefined
|
|
193
|
+
? undefined
|
|
194
|
+
: {
|
|
195
|
+
parentRunId: inherited.identity.runId,
|
|
196
|
+
parentCallId: inherited.toolCallId,
|
|
197
|
+
agentName: overrides?.agentName ?? event.functionId,
|
|
198
|
+
},
|
|
199
|
+
...overrides,
|
|
200
|
+
identity,
|
|
201
|
+
capture,
|
|
202
|
+
}),
|
|
203
|
+
};
|
|
204
|
+
generations.set(event.callId, state);
|
|
205
|
+
const turn = await state.turn;
|
|
206
|
+
native.instrument(turn, () =>
|
|
207
|
+
native.telemetry.onStart({ ...event, recordInputs: false, recordOutputs: false }),
|
|
208
|
+
);
|
|
75
209
|
},
|
|
76
210
|
async onStepStart(event: Event<"onStepStart">) {
|
|
77
|
-
const state =
|
|
211
|
+
const state = generations.get(event.callId);
|
|
212
|
+
if (state === undefined) return;
|
|
78
213
|
await closeStep(state);
|
|
79
|
-
state.turn ??= run.turn({
|
|
80
|
-
startTimeMs: state.startTimeMs,
|
|
81
|
-
...(typeof options.turn === "function"
|
|
82
|
-
? options.turn({ callId: event.callId })
|
|
83
|
-
: options.turn),
|
|
84
|
-
});
|
|
85
214
|
const turn = await state.turn;
|
|
86
215
|
state.attempt = turn.attempt({ stepIndex: event.stepNumber, attempt: 0 });
|
|
216
|
+
native.instrument(await state.attempt, () => native.telemetry.onStepStart(event));
|
|
87
217
|
},
|
|
88
218
|
async onLanguageModelCallStart(event: Event<"onLanguageModelCallStart">) {
|
|
89
219
|
const attempt = await generations.get(event.callId)?.attempt;
|
|
90
220
|
if (attempt === undefined) return;
|
|
91
|
-
generations.get(event.callId)
|
|
221
|
+
const state = generations.get(event.callId)!;
|
|
222
|
+
await (await state.model)?.fail(state.modelFailure?.error);
|
|
223
|
+
state.modelFailure = undefined;
|
|
224
|
+
state.model = attempt.modelCall({
|
|
92
225
|
provider: event.provider,
|
|
93
226
|
modelId: event.modelId,
|
|
94
227
|
messages: event.messages,
|
|
95
228
|
instructions: event.instructions,
|
|
96
229
|
tools: event.tools,
|
|
97
230
|
});
|
|
231
|
+
native.instrument(await state.model, () => native.telemetry.onLanguageModelCallStart(event));
|
|
98
232
|
},
|
|
99
|
-
async executeLanguageModelCall(
|
|
100
|
-
const
|
|
101
|
-
|
|
233
|
+
async executeLanguageModelCall(event) {
|
|
234
|
+
const state = generations.get(event.callId);
|
|
235
|
+
const model = await state?.model;
|
|
236
|
+
if (state === undefined || model === undefined || model.finished) return event.execute();
|
|
102
237
|
try {
|
|
103
|
-
return await
|
|
238
|
+
return await native.telemetry.executeLanguageModelCall({
|
|
239
|
+
...event,
|
|
240
|
+
execute: () => executeInGeneration(state, model, event.execute),
|
|
241
|
+
});
|
|
104
242
|
} catch (error) {
|
|
105
|
-
|
|
243
|
+
state.modelFailure = { error };
|
|
106
244
|
throw error;
|
|
107
245
|
}
|
|
108
246
|
},
|
|
@@ -110,7 +248,9 @@ export function aiSdkTelemetry(run: AgentRun, options: AiSdkTelemetryOptions): T
|
|
|
110
248
|
const state = generations.get(event.callId);
|
|
111
249
|
const model = await state?.model;
|
|
112
250
|
if (state === undefined || model === undefined) return;
|
|
251
|
+
native.withoutNewSpans(() => native.telemetry.onLanguageModelCallEnd(event));
|
|
113
252
|
state.model = undefined;
|
|
253
|
+
state.modelFailure = undefined;
|
|
114
254
|
await model.complete({
|
|
115
255
|
outcome: "completed",
|
|
116
256
|
result: {
|
|
@@ -131,15 +271,24 @@ export function aiSdkTelemetry(run: AgentRun, options: AiSdkTelemetryOptions): T
|
|
|
131
271
|
call.toolCallId,
|
|
132
272
|
attempt.tool({ callId: call.toolCallId, name: call.toolName, arguments: call.input }),
|
|
133
273
|
);
|
|
274
|
+
native.instrument(await state.tools.get(call.toolCallId)!, () =>
|
|
275
|
+
native.telemetry.onToolExecutionStart(event),
|
|
276
|
+
);
|
|
134
277
|
},
|
|
135
|
-
async executeTool(
|
|
136
|
-
const
|
|
137
|
-
|
|
278
|
+
async executeTool(event) {
|
|
279
|
+
const state = generations.get(event.callId);
|
|
280
|
+
const tool = await state?.tools.get(event.toolCallId);
|
|
281
|
+
if (state === undefined || tool === undefined || tool.finished) return event.execute();
|
|
282
|
+
return native.telemetry.executeTool({
|
|
283
|
+
...event,
|
|
284
|
+
execute: () => executeInGeneration(state, tool, event.execute, event.toolCallId),
|
|
285
|
+
});
|
|
138
286
|
},
|
|
139
287
|
async onToolExecutionEnd(event: Event<"onToolExecutionEnd">) {
|
|
140
288
|
const state = generations.get(event.callId);
|
|
141
289
|
const tool = await state?.tools.get(event.toolCall.toolCallId);
|
|
142
290
|
if (state === undefined || tool === undefined) return;
|
|
291
|
+
native.telemetry.onToolExecutionEnd(event);
|
|
143
292
|
state.tools.delete(event.toolCall.toolCallId);
|
|
144
293
|
const output = event.toolOutput;
|
|
145
294
|
const completion =
|
|
@@ -154,9 +303,11 @@ export function aiSdkTelemetry(run: AgentRun, options: AiSdkTelemetryOptions): T
|
|
|
154
303
|
},
|
|
155
304
|
async onStepEnd(event: Event<"onStepEnd">) {
|
|
156
305
|
const state = generations.get(event.callId);
|
|
306
|
+
native.telemetry.onStepEnd(event);
|
|
157
307
|
if (state !== undefined) await closeStep(state);
|
|
158
308
|
},
|
|
159
309
|
async onEnd(event: Event<"onEnd">) {
|
|
310
|
+
native.telemetry.onEnd("totalUsage" in event ? { ...event, usage: event.totalUsage } : event);
|
|
160
311
|
await finish(event.callId, (turn) =>
|
|
161
312
|
turn.complete({
|
|
162
313
|
outcome: "completed",
|
|
@@ -165,15 +316,16 @@ export function aiSdkTelemetry(run: AgentRun, options: AiSdkTelemetryOptions): T
|
|
|
165
316
|
);
|
|
166
317
|
},
|
|
167
318
|
async onAbort(event: Event<"onAbort">) {
|
|
319
|
+
native.telemetry.onAbort(event);
|
|
168
320
|
await finish(event.callId, (turn) => turn.complete({ outcome: "cancelled" }));
|
|
169
321
|
},
|
|
170
322
|
async onError(value: unknown) {
|
|
323
|
+
native.telemetry.onError(value);
|
|
171
324
|
const { callId, error } =
|
|
172
325
|
value !== null && typeof value === "object" && "callId" in value && "error" in value
|
|
173
326
|
? (value as { callId: string; error: unknown })
|
|
174
327
|
: { callId: undefined, error: value };
|
|
175
|
-
|
|
176
|
-
await finish(id, (turn) => turn.fail(error), error);
|
|
328
|
+
if (callId !== undefined) await finish(callId, (turn) => turn.fail(error), error);
|
|
177
329
|
},
|
|
178
330
|
};
|
|
179
331
|
}
|
package/src/adapters/otel.ts
CHANGED
|
@@ -98,6 +98,8 @@ const DEFAULT_MAPPING: OutputMapping = {
|
|
|
98
98
|
link: (_span, link) => ({ "agent.link.type": link.relationship }),
|
|
99
99
|
};
|
|
100
100
|
|
|
101
|
+
export const telemetryTracerProviders = new WeakMap<AgentTelemetry, () => TracerProvider>();
|
|
102
|
+
|
|
101
103
|
function onVercel(): boolean {
|
|
102
104
|
const env = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process
|
|
103
105
|
?.env;
|
|
@@ -131,38 +133,43 @@ export interface OtelTelemetryOptions {
|
|
|
131
133
|
|
|
132
134
|
/** OpenTelemetry implementation of {@link AgentTelemetry}. */
|
|
133
135
|
export function otelTelemetry(options: OtelTelemetryOptions = {}): AgentTelemetry {
|
|
134
|
-
const provider = options.provider ?? trace.getTracerProvider();
|
|
135
|
-
const tracer = provider.getTracer(options.tracerName ?? AGENT_RUNS_SCOPE, options.tracerVersion);
|
|
136
|
+
const provider = () => options.provider ?? trace.getTracerProvider();
|
|
136
137
|
const mapping = options.mapping ?? DEFAULT_MAPPING;
|
|
137
138
|
const idGenerator = options.idGenerator;
|
|
138
139
|
function lifecycle(method: "forceFlush" | "shutdown"): () => Promise<void> {
|
|
139
140
|
return (
|
|
140
141
|
options[method] ??
|
|
141
142
|
(async () => {
|
|
143
|
+
const selected = provider();
|
|
144
|
+
const current =
|
|
145
|
+
(selected as TracerProvider & { getDelegate?(): TracerProvider }).getDelegate?.() ??
|
|
146
|
+
selected;
|
|
142
147
|
const callback = (
|
|
143
|
-
|
|
148
|
+
current as TracerProvider & { forceFlush?(): Promise<void>; shutdown?(): Promise<void> }
|
|
144
149
|
)[method];
|
|
145
150
|
if (callback === undefined)
|
|
146
151
|
throw new Error(`The tracer provider does not support ${method}.`);
|
|
147
|
-
await callback.call(
|
|
152
|
+
await callback.call(current);
|
|
148
153
|
})
|
|
149
154
|
);
|
|
150
155
|
}
|
|
151
156
|
function start(span: PreparedSpan, executionContext?: ExecutionContext): SpanWriter {
|
|
152
|
-
const recorded =
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
157
|
+
const recorded = provider()
|
|
158
|
+
.getTracer(options.tracerName ?? AGENT_RUNS_SCOPE, options.tracerVersion)
|
|
159
|
+
.startSpan(
|
|
160
|
+
span.name,
|
|
161
|
+
{
|
|
162
|
+
attributes: mappedAttributes(mapping, span, span.attributes),
|
|
163
|
+
kind: span.kind === undefined ? undefined : SpanKind[span.kind],
|
|
164
|
+
root: span.root,
|
|
165
|
+
startTime: span.startTimeMs,
|
|
166
|
+
links: span.links?.map((link) => ({
|
|
167
|
+
context: otelReference(link.context),
|
|
168
|
+
attributes: mapping?.link(span, link) ?? { "agent.link.type": link.relationship },
|
|
169
|
+
})),
|
|
170
|
+
},
|
|
171
|
+
parentContext(span.root ? undefined : span.parent, executionContext),
|
|
172
|
+
);
|
|
166
173
|
return {
|
|
167
174
|
reference: portableReference(recorded.spanContext()),
|
|
168
175
|
setAttribute(key, value) {
|
|
@@ -184,7 +191,7 @@ export function otelTelemetry(options: OtelTelemetryOptions = {}): AgentTelemetr
|
|
|
184
191
|
},
|
|
185
192
|
};
|
|
186
193
|
}
|
|
187
|
-
|
|
194
|
+
const telemetry: AgentTelemetry = {
|
|
188
195
|
startSpan(span, { reference, context: host } = {}) {
|
|
189
196
|
if (reference === undefined || idGenerator === undefined) return start(span, host);
|
|
190
197
|
const writer = idGenerator.withTraceId(reference.traceId, () =>
|
|
@@ -237,4 +244,6 @@ export function otelTelemetry(options: OtelTelemetryOptions = {}): AgentTelemetr
|
|
|
237
244
|
forceFlush: lifecycle("forceFlush"),
|
|
238
245
|
shutdown: lifecycle("shutdown"),
|
|
239
246
|
};
|
|
247
|
+
telemetryTracerProviders.set(telemetry, provider);
|
|
248
|
+
return telemetry;
|
|
240
249
|
}
|
package/src/agent-run.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { runTelemetry } from "./core/run-telemetry.js";
|
|
2
3
|
import { otelTelemetry } from "./adapters/otel.js";
|
|
3
4
|
import { aiSdkContentSerializer } from "./adapters/serialization.js";
|
|
4
5
|
import { createScopes, type LiveOperation } from "./core/scopes.js";
|
|
@@ -402,7 +403,7 @@ export function createAgentRun(input: AgentRunOptions): AgentRun {
|
|
|
402
403
|
return operationHandle(live, live.record().data, input.onError);
|
|
403
404
|
}
|
|
404
405
|
|
|
405
|
-
|
|
406
|
+
const run: AgentRun = {
|
|
406
407
|
turn: wrappedOperation(turnOperation, undefined, input.onError),
|
|
407
408
|
decision: wrappedOperation(decision, undefined, input.onError),
|
|
408
409
|
resume,
|
|
@@ -413,4 +414,6 @@ export function createAgentRun(input: AgentRunOptions): AgentRun {
|
|
|
413
414
|
forceFlush: () => lifecycle(() => telemetry.forceFlush()),
|
|
414
415
|
shutdown: () => lifecycle(() => telemetry.shutdown()),
|
|
415
416
|
};
|
|
417
|
+
runTelemetry.set(run, { telemetry, usesGlobalProvider: input.telemetry === undefined });
|
|
418
|
+
return run;
|
|
416
419
|
}
|
package/src/ai-sdk.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
aiSdkTelemetry,
|
|
3
|
+
type AiSdkTelemetryOptions,
|
|
4
|
+
type AiSdkTurnInput,
|
|
5
|
+
} from "./adapters/ai-sdk-telemetry.js";
|
|
2
6
|
export { modelUsage, modelContent } from "./adapters/ai-sdk-payload.js";
|
package/src/core/scopes.ts
CHANGED
|
@@ -347,7 +347,12 @@ export function createScopes(input: {
|
|
|
347
347
|
const container = actualData.type === "step" || actualData.type === "activation";
|
|
348
348
|
for (const next of children.values())
|
|
349
349
|
if (all || !container || !OUTLIVES_ATTEMPT.has(next.type))
|
|
350
|
-
await next.release({
|
|
350
|
+
await next.release({
|
|
351
|
+
failed: result.failed,
|
|
352
|
+
error: result.error,
|
|
353
|
+
errorCode: result.errorCode,
|
|
354
|
+
outcome: "abandoned",
|
|
355
|
+
});
|
|
351
356
|
}
|
|
352
357
|
|
|
353
358
|
async function finish(result: ScopeTerminal): Promise<void> {
|