@uselemma/tracing 7.14.0 → 7.15.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/README.md +73 -17
- package/dist/client.d.ts +49 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +204 -73
- package/dist/client.js.map +1 -1
- package/dist/coding-agent-backlog.d.ts +81 -0
- package/dist/coding-agent-backlog.d.ts.map +1 -0
- package/dist/coding-agent-backlog.js +239 -0
- package/dist/coding-agent-backlog.js.map +1 -0
- package/dist/coding-agent-delivery.d.ts +31 -0
- package/dist/coding-agent-delivery.d.ts.map +1 -0
- package/dist/coding-agent-delivery.js +97 -0
- package/dist/coding-agent-delivery.js.map +1 -0
- package/dist/coding-agent-queue.d.ts +74 -0
- package/dist/coding-agent-queue.d.ts.map +1 -0
- package/dist/coding-agent-queue.js +230 -0
- package/dist/coding-agent-queue.js.map +1 -0
- package/dist/coding-agent.d.ts +14 -0
- package/dist/coding-agent.d.ts.map +1 -1
- package/dist/coding-agent.js +52 -15
- package/dist/coding-agent.js.map +1 -1
- package/dist/debug-mode.d.ts.map +1 -1
- package/dist/debug-mode.js +4 -4
- package/dist/debug-mode.js.map +1 -1
- package/dist/delivery.d.ts +50 -7
- package/dist/delivery.d.ts.map +1 -1
- package/dist/delivery.js +117 -21
- package/dist/delivery.js.map +1 -1
- package/dist/env.d.ts +8 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +18 -0
- package/dist/env.js.map +1 -0
- package/dist/error-message.d.ts +10 -0
- package/dist/error-message.d.ts.map +1 -1
- package/dist/error-message.js +14 -0
- package/dist/error-message.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/integration-client.d.ts +20 -0
- package/dist/integration-client.d.ts.map +1 -0
- package/dist/integration-client.js +47 -0
- package/dist/integration-client.js.map +1 -0
- package/dist/langchain-context.d.ts +7 -0
- package/dist/langchain-context.d.ts.map +1 -0
- package/dist/langchain-context.js +28 -0
- package/dist/langchain-context.js.map +1 -0
- package/dist/langchain-eviction.d.ts +42 -0
- package/dist/langchain-eviction.d.ts.map +1 -0
- package/dist/langchain-eviction.js +56 -0
- package/dist/langchain-eviction.js.map +1 -0
- package/dist/langchain.d.ts +31 -13
- package/dist/langchain.d.ts.map +1 -1
- package/dist/langchain.js +74 -109
- package/dist/langchain.js.map +1 -1
- package/dist/mastra.d.ts +9 -11
- package/dist/mastra.d.ts.map +1 -1
- package/dist/mastra.js +40 -25
- package/dist/mastra.js.map +1 -1
- package/dist/openai-agents.d.ts +8 -0
- package/dist/openai-agents.d.ts.map +1 -1
- package/dist/openai-agents.js +17 -14
- package/dist/openai-agents.js.map +1 -1
- package/dist/serialize.d.ts +17 -0
- package/dist/serialize.d.ts.map +1 -0
- package/dist/serialize.js +143 -0
- package/dist/serialize.js.map +1 -0
- package/dist/stream-result.d.ts +33 -0
- package/dist/stream-result.d.ts.map +1 -0
- package/dist/stream-result.js +135 -0
- package/dist/stream-result.js.map +1 -0
- package/dist/vercel-ai-events.d.ts +308 -0
- package/dist/vercel-ai-events.d.ts.map +1 -0
- package/dist/vercel-ai-events.js +340 -0
- package/dist/vercel-ai-events.js.map +1 -0
- package/dist/vercel-ai-run.d.ts +171 -0
- package/dist/vercel-ai-run.d.ts.map +1 -0
- package/dist/vercel-ai-run.js +650 -0
- package/dist/vercel-ai-run.js.map +1 -0
- package/dist/vercel-ai.d.ts +36 -108
- package/dist/vercel-ai.d.ts.map +1 -1
- package/dist/vercel-ai.js +185 -882
- package/dist/vercel-ai.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/examples/vercel-ai-v6.ts +2 -2
- package/examples/vercel-ai-v7.ts +14 -16
- package/package.json +1 -1
package/dist/vercel-ai.js
CHANGED
|
@@ -1,924 +1,227 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CANCELLED_METADATA_KEY = void 0;
|
|
3
4
|
exports.vercelAI = vercelAI;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
5
|
+
const integration_client_1 = require("./integration-client");
|
|
6
|
+
const vercel_ai_events_1 = require("./vercel-ai-events");
|
|
7
|
+
const vercel_ai_run_1 = require("./vercel-ai-run");
|
|
8
|
+
var vercel_ai_run_2 = require("./vercel-ai-run");
|
|
9
|
+
Object.defineProperty(exports, "CANCELLED_METADATA_KEY", { enumerable: true, get: function () { return vercel_ai_run_2.CANCELLED_METADATA_KEY; } });
|
|
10
|
+
const CONCURRENT_V6_WARNING = "@uselemma/tracing: vercelAI() received an AI SDK 6 run while another was in flight. AI SDK 6 sends no call ID, so the two runs' spans get mixed into one trace and some are dropped. Create one vercelAI() per concurrent AI SDK 6 call, or use AI SDK 7.";
|
|
11
|
+
/** The callbacks a call's events reach after its onStart. */
|
|
12
|
+
const CALL_EVENT_CALLBACKS = {
|
|
13
|
+
onStepStart: true,
|
|
14
|
+
onStepEnd: true,
|
|
15
|
+
onStepFinish: true,
|
|
16
|
+
onLanguageModelCallStart: true,
|
|
17
|
+
onLanguageModelCallEnd: true,
|
|
18
|
+
onToolExecutionStart: true,
|
|
19
|
+
onToolCallStart: true,
|
|
20
|
+
onToolExecutionEnd: true,
|
|
21
|
+
onToolCallFinish: true,
|
|
22
|
+
onFinish: true,
|
|
23
|
+
onEnd: true,
|
|
24
|
+
onAbort: true,
|
|
25
|
+
onError: true,
|
|
11
26
|
};
|
|
12
|
-
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
?
|
|
16
|
-
: new Date();
|
|
17
|
-
}
|
|
18
|
-
function subtractMs(endedAt, durationMs) {
|
|
19
|
-
return typeof durationMs === "number"
|
|
20
|
-
? new Date(endedAt.getTime() - durationMs)
|
|
21
|
-
: endedAt;
|
|
22
|
-
}
|
|
23
|
-
function v7StepKey(callId, stepNumber) {
|
|
24
|
-
return `${callId}:${stepNumber}`;
|
|
25
|
-
}
|
|
26
|
-
function isV7StepStart(event) {
|
|
27
|
-
return "callId" in event && "provider" in event && "modelId" in event;
|
|
28
|
-
}
|
|
29
|
-
function stringifyContent(content) {
|
|
30
|
-
const text = content
|
|
31
|
-
.map((part) => {
|
|
32
|
-
if (!part || typeof part !== "object")
|
|
33
|
-
return "";
|
|
34
|
-
if (part.type !== "text")
|
|
35
|
-
return "";
|
|
36
|
-
return String(part.text ?? "");
|
|
37
|
-
})
|
|
38
|
-
.join("");
|
|
39
|
-
if (text)
|
|
40
|
-
return text;
|
|
41
|
-
return JSON.stringify(content);
|
|
42
|
-
}
|
|
43
|
-
function structuredAssistantOutput(text, content) {
|
|
44
|
-
if (typeof text === "string")
|
|
45
|
-
return text;
|
|
46
|
-
if (!content)
|
|
47
|
-
return undefined;
|
|
48
|
-
const hasNonText = content.some((part) => part &&
|
|
49
|
-
typeof part === "object" &&
|
|
50
|
-
part.type !== "text");
|
|
51
|
-
if (hasNonText)
|
|
52
|
-
return content;
|
|
53
|
-
return stringifyContent(content);
|
|
54
|
-
}
|
|
55
|
-
function errorToolFields(error) {
|
|
56
|
-
return { error, status: "ERROR" };
|
|
57
|
-
}
|
|
58
|
-
function toolOutput(event) {
|
|
59
|
-
if (event.toolOutput.type === "tool-error") {
|
|
60
|
-
return errorToolFields(event.toolOutput.error);
|
|
61
|
-
}
|
|
62
|
-
const softError = (0, tool_result_1.toolResultError)(event.toolOutput.output);
|
|
63
|
-
if (softError) {
|
|
64
|
-
return errorToolFields(softError);
|
|
65
|
-
}
|
|
66
|
-
return { output: event.toolOutput.output };
|
|
67
|
-
}
|
|
68
|
-
function v6ToolOutput(event) {
|
|
69
|
-
if (!event.success) {
|
|
70
|
-
return errorToolFields(event.error);
|
|
71
|
-
}
|
|
72
|
-
const softError = (0, tool_result_1.toolResultError)(event.output);
|
|
73
|
-
if (softError) {
|
|
74
|
-
return errorToolFields(softError);
|
|
75
|
-
}
|
|
76
|
-
return { output: event.output };
|
|
77
|
-
}
|
|
78
|
-
function resolveDurationMs(startedAt, endedAt, reportedMs) {
|
|
79
|
-
const wallMs = Math.max(0, endedAt.getTime() - startedAt.getTime());
|
|
80
|
-
if (typeof reportedMs !== "number")
|
|
81
|
-
return wallMs;
|
|
82
|
-
// Prefer wall clock when it is longer so children that finished inside the
|
|
83
|
-
// step cannot outlast a too-short reported model-only duration.
|
|
84
|
-
return Math.max(reportedMs, wallMs);
|
|
85
|
-
}
|
|
86
|
-
function messageContent(message) {
|
|
87
|
-
if (!message || typeof message !== "object")
|
|
88
|
-
return message;
|
|
89
|
-
const record = message;
|
|
90
|
-
if ("content" in record)
|
|
91
|
-
return record.content;
|
|
92
|
-
return message;
|
|
93
|
-
}
|
|
94
|
-
function lookupString(sources, keys) {
|
|
95
|
-
for (const source of sources) {
|
|
96
|
-
if (!source)
|
|
97
|
-
continue;
|
|
98
|
-
for (const key of keys) {
|
|
99
|
-
const value = source[key];
|
|
100
|
-
if (typeof value === "string" && value)
|
|
101
|
-
return value;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return undefined;
|
|
105
|
-
}
|
|
106
|
-
function instructionText(value) {
|
|
107
|
-
if (typeof value === "string" && value)
|
|
108
|
-
return value;
|
|
109
|
-
if (Array.isArray(value)) {
|
|
110
|
-
const parts = value
|
|
111
|
-
.map((part) => instructionText(part))
|
|
112
|
-
.filter((part) => Boolean(part));
|
|
113
|
-
return parts.length > 0 ? parts.join("\n") : undefined;
|
|
114
|
-
}
|
|
115
|
-
if (!value || typeof value !== "object")
|
|
116
|
-
return undefined;
|
|
117
|
-
const content = value.content;
|
|
118
|
-
return typeof content === "string" && content ? content : undefined;
|
|
119
|
-
}
|
|
120
|
-
/** Prefer AI SDK 7 `instructions`; fall back to deprecated `system`. */
|
|
121
|
-
function siblingSystem(event) {
|
|
122
|
-
if (!event || typeof event !== "object")
|
|
123
|
-
return undefined;
|
|
124
|
-
const record = event;
|
|
125
|
-
return instructionText(record.instructions) ?? instructionText(record.system);
|
|
126
|
-
}
|
|
127
|
-
function firstMessageRole(messages) {
|
|
128
|
-
const first = messages[0];
|
|
129
|
-
if (!first || typeof first !== "object")
|
|
130
|
-
return undefined;
|
|
131
|
-
return first.role;
|
|
132
|
-
}
|
|
133
|
-
function firstMessageIsSystem(messages, system) {
|
|
134
|
-
const first = messages[0];
|
|
135
|
-
return Boolean(first &&
|
|
136
|
-
typeof first === "object" &&
|
|
137
|
-
first.role === "system" &&
|
|
138
|
-
first.content === system);
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Use this event's instructions/system. If it has none, trust a leading
|
|
142
|
-
* system message instead of stamping a stale onStart playbook.
|
|
143
|
-
*/
|
|
144
|
-
function resolveSystemForMessages(event, messages, fallbackSystem) {
|
|
145
|
-
const sibling = siblingSystem(event);
|
|
146
|
-
if (sibling)
|
|
147
|
-
return sibling;
|
|
148
|
-
if (Array.isArray(messages) && firstMessageRole(messages) === "system") {
|
|
149
|
-
return undefined;
|
|
150
|
-
}
|
|
151
|
-
return fallbackSystem;
|
|
152
|
-
}
|
|
153
|
-
/** Prepend a sibling `system` string onto a message list unless already present. */
|
|
154
|
-
function withSystemMessage(system, messages) {
|
|
155
|
-
if (!Array.isArray(messages))
|
|
156
|
-
return undefined;
|
|
157
|
-
if (!system || firstMessageIsSystem(messages, system))
|
|
158
|
-
return messages;
|
|
159
|
-
return [{ role: "system", content: system }, ...messages];
|
|
160
|
-
}
|
|
161
|
-
/** Normalize v6 system/prompt/messages into a chat message list when possible. */
|
|
162
|
-
function v6NormalizedMessages(event, fallbackSystem) {
|
|
163
|
-
const system = resolveSystemForMessages(event, "messages" in event ? event.messages : undefined, fallbackSystem);
|
|
164
|
-
if (Array.isArray(event.messages)) {
|
|
165
|
-
return withSystemMessage(system, event.messages);
|
|
166
|
-
}
|
|
167
|
-
const messages = [];
|
|
168
|
-
if (system) {
|
|
169
|
-
messages.push({ role: "system", content: system });
|
|
170
|
-
}
|
|
171
|
-
if ("prompt" in event && event.prompt !== undefined) {
|
|
172
|
-
if (Array.isArray(event.prompt)) {
|
|
173
|
-
return messages.length > 0
|
|
174
|
-
? [...messages, ...event.prompt]
|
|
175
|
-
: event.prompt;
|
|
176
|
-
}
|
|
177
|
-
if (typeof event.prompt === "string") {
|
|
178
|
-
messages.push({ role: "user", content: event.prompt });
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return messages.length > 0 ? messages : undefined;
|
|
182
|
-
}
|
|
183
|
-
function v6Input(event, fallbackSystem) {
|
|
184
|
-
const normalized = v6NormalizedMessages(event, fallbackSystem);
|
|
185
|
-
if (normalized)
|
|
186
|
-
return normalized;
|
|
187
|
-
if ("prompt" in event)
|
|
188
|
-
return event.prompt;
|
|
189
|
-
return undefined;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Prefer the current user turn for the Lemma root input.
|
|
193
|
-
* Accepts a bare string, message array, or prompt/system start payload.
|
|
194
|
-
*/
|
|
195
|
-
function rootTraceInput(input) {
|
|
196
|
-
if (typeof input === "string")
|
|
197
|
-
return input;
|
|
198
|
-
if (!Array.isArray(input) || input.length === 0)
|
|
199
|
-
return input;
|
|
200
|
-
for (let i = input.length - 1; i >= 0; i--) {
|
|
201
|
-
const message = input[i];
|
|
202
|
-
if (!message || typeof message !== "object")
|
|
203
|
-
continue;
|
|
204
|
-
const role = message.role;
|
|
205
|
-
if (role === "user")
|
|
206
|
-
return messageContent(message);
|
|
207
|
-
}
|
|
208
|
-
return messageContent(input[input.length - 1]);
|
|
209
|
-
}
|
|
210
|
-
function eventTraceInput(event) {
|
|
211
|
-
if (!event)
|
|
212
|
-
return undefined;
|
|
213
|
-
if ("messages" in event || "prompt" in event || "system" in event) {
|
|
214
|
-
if (!("callId" in event) || "model" in event) {
|
|
215
|
-
return v6Input(event);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if ("messages" in event && event.messages)
|
|
219
|
-
return event.messages;
|
|
220
|
-
if ("prompt" in event)
|
|
221
|
-
return event.prompt;
|
|
222
|
-
return undefined;
|
|
223
|
-
}
|
|
224
|
-
function eventMetadata(event) {
|
|
225
|
-
return event && "metadata" in event ? (event.metadata ?? undefined) : undefined;
|
|
226
|
-
}
|
|
227
|
-
function traceName(options, event) {
|
|
228
|
-
if (options.agentName)
|
|
229
|
-
return options.agentName;
|
|
230
|
-
const functionId = event && "functionId" in event && typeof event.functionId === "string"
|
|
231
|
-
? event.functionId
|
|
27
|
+
/** The call ID a `generateText` result's steps carry on AI SDK 7. */
|
|
28
|
+
function resultCallId(result) {
|
|
29
|
+
const steps = typeof result === "object" && result !== null
|
|
30
|
+
? result.steps
|
|
232
31
|
: undefined;
|
|
233
|
-
return
|
|
234
|
-
}
|
|
235
|
-
function v6Output(event) {
|
|
236
|
-
return structuredAssistantOutput(event.text, event.content);
|
|
237
|
-
}
|
|
238
|
-
function endOutput(event) {
|
|
239
|
-
return structuredAssistantOutput(event.text, event.content);
|
|
240
|
-
}
|
|
241
|
-
/** Extract usage from a finish/end event when the AI SDK exposes it. */
|
|
242
|
-
function eventUsage(event) {
|
|
243
|
-
return (0, usage_1.normalizeTokenUsage)(event.usage) ?? (0, usage_1.normalizeTokenUsage)(event.totalUsage);
|
|
244
|
-
}
|
|
245
|
-
function withIntegrationAttrs(attributes) {
|
|
246
|
-
return { ...INTEGRATION_ATTRS, ...(attributes ?? {}) };
|
|
32
|
+
return Array.isArray(steps) ? (0, vercel_ai_events_1.runKey)(steps[0]) : undefined;
|
|
247
33
|
}
|
|
248
34
|
function vercelAI(options = {}) {
|
|
249
|
-
|
|
35
|
+
const getLemma = (0, integration_client_1.integrationClientGetter)("vercelAI()", options);
|
|
250
36
|
const pending = new Set();
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
let
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
let
|
|
260
|
-
let
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
latestV6GenerationId = undefined;
|
|
279
|
-
managedTrace = undefined;
|
|
280
|
-
recordedV6Step = false;
|
|
281
|
-
sawV7StepZero = false;
|
|
282
|
-
runStartedAt = undefined;
|
|
283
|
-
runError = undefined;
|
|
284
|
-
endingTrace = undefined;
|
|
285
|
-
deliverOwnedTrace = undefined;
|
|
286
|
-
}
|
|
287
|
-
function trackGeneration(handle) {
|
|
288
|
-
generationHandlesById.set(handle.id, handle);
|
|
289
|
-
}
|
|
290
|
-
function coverParent(parentId, endedAt) {
|
|
291
|
-
if (!parentId)
|
|
292
|
-
return;
|
|
293
|
-
generationHandlesById.get(parentId)?.ensureEndedAt(endedAt);
|
|
294
|
-
}
|
|
295
|
-
function getLemma() {
|
|
296
|
-
lemma ??= new client_1.Lemma({
|
|
297
|
-
apiKey: options.apiKey,
|
|
298
|
-
projectId: options.projectId,
|
|
299
|
-
baseUrl: options.baseUrl,
|
|
300
|
-
fetch: options.fetch,
|
|
301
|
-
release: options.release,
|
|
302
|
-
});
|
|
303
|
-
return lemma;
|
|
304
|
-
}
|
|
305
|
-
function resolveThreadId(metadata) {
|
|
306
|
-
const key = options.threadIdKey ?? "threadId";
|
|
307
|
-
return lookupString([metadata, options.metadata], [key]);
|
|
308
|
-
}
|
|
309
|
-
function resolveUserId(metadata) {
|
|
310
|
-
const key = options.userIdKey ?? "userId";
|
|
311
|
-
return lookupString([metadata, options.metadata], [key]);
|
|
312
|
-
}
|
|
313
|
-
function mergedMetadata(metadata) {
|
|
314
|
-
return {
|
|
315
|
-
...options.metadata,
|
|
316
|
-
...(metadata ?? {}),
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
function beginActivity() {
|
|
320
|
-
if (phase === "idle") {
|
|
321
|
-
phase = "active";
|
|
322
|
-
runStartedAt = new Date();
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
function beginNewRun() {
|
|
326
|
-
if (phase === "active" || phase === "ending") {
|
|
327
|
-
throw new Error(CONCURRENT_REUSE_ERROR);
|
|
328
|
-
}
|
|
329
|
-
resetRunState();
|
|
330
|
-
phase = "active";
|
|
331
|
-
runStartedAt = new Date();
|
|
332
|
-
}
|
|
333
|
-
function hasExplicitEndableTrace() {
|
|
334
|
-
const trace = options.trace;
|
|
335
|
-
return Boolean(trace && typeof trace.end === "function");
|
|
336
|
-
}
|
|
337
|
-
function applyIdentity(trace, metadata) {
|
|
338
|
-
const threadId = resolveThreadId(metadata);
|
|
339
|
-
const userId = resolveUserId(metadata);
|
|
340
|
-
if (threadId)
|
|
341
|
-
trace.threadId(threadId);
|
|
342
|
-
if (userId)
|
|
343
|
-
trace.userId(userId);
|
|
344
|
-
}
|
|
345
|
-
function resolveTrace(event) {
|
|
346
|
-
// Trailing AI SDK callbacks can arrive while terminal delivery is in flight.
|
|
347
|
-
// Attach them to the still-open trace instead of throwing.
|
|
348
|
-
if (phase === "ending") {
|
|
349
|
-
const trace = options.trace ?? managedTrace;
|
|
350
|
-
return trace
|
|
351
|
-
? {
|
|
352
|
-
trace,
|
|
353
|
-
source: options.trace ? "explicit" : "managed",
|
|
354
|
-
}
|
|
355
|
-
: null;
|
|
356
|
-
}
|
|
357
|
-
beginActivity();
|
|
358
|
-
const metadata = eventMetadata(event);
|
|
359
|
-
if (options.trace) {
|
|
360
|
-
applyIdentity(options.trace, metadata);
|
|
361
|
-
return { trace: options.trace, source: "explicit" };
|
|
362
|
-
}
|
|
363
|
-
if (!managedTrace) {
|
|
364
|
-
const rawInput = eventTraceInput(event);
|
|
365
|
-
managedTrace = getLemma().trace({
|
|
366
|
-
name: traceName(options, event),
|
|
367
|
-
input: rootTraceInput(rawInput),
|
|
368
|
-
metadata: mergedMetadata(metadata),
|
|
369
|
-
threadId: resolveThreadId(metadata),
|
|
370
|
-
userId: resolveUserId(metadata),
|
|
371
|
-
startedAt: runStartedAt,
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
else {
|
|
375
|
-
applyIdentity(managedTrace, metadata);
|
|
376
|
-
if (event) {
|
|
377
|
-
const rawInput = eventTraceInput(event);
|
|
378
|
-
if (rawInput !== undefined) {
|
|
379
|
-
// Keep root input as the latest known current-turn value.
|
|
380
|
-
managedTrace.input(rootTraceInput(rawInput));
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
return { trace: managedTrace, source: "managed" };
|
|
385
|
-
}
|
|
386
|
-
function trackPending(promise) {
|
|
387
|
-
pending.add(promise);
|
|
388
|
-
void promise.finally(() => pending.delete(promise));
|
|
389
|
-
return promise;
|
|
390
|
-
}
|
|
391
|
-
function ensureManagedTrace(event) {
|
|
392
|
-
if (options.trace || managedTrace)
|
|
393
|
-
return;
|
|
394
|
-
beginActivity();
|
|
395
|
-
managedTrace = getLemma().trace({
|
|
396
|
-
name: traceName(options, event),
|
|
397
|
-
metadata: mergedMetadata(eventMetadata(event)),
|
|
398
|
-
threadId: resolveThreadId(eventMetadata(event)),
|
|
399
|
-
userId: resolveUserId(eventMetadata(event)),
|
|
400
|
-
startedAt: runStartedAt,
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
async function endOwnedTrace(event) {
|
|
404
|
-
if (phase === "idle")
|
|
405
|
-
return;
|
|
406
|
-
if (phase === "ending")
|
|
407
|
-
return;
|
|
408
|
-
phase = "ending";
|
|
409
|
-
const trace = options.trace;
|
|
410
|
-
let ownedTrace;
|
|
411
|
-
let explicit = false;
|
|
412
|
-
if (trace && typeof trace.end === "function") {
|
|
413
|
-
explicit = true;
|
|
414
|
-
ownedTrace = {
|
|
415
|
-
end: trace.end.bind(trace),
|
|
416
|
-
fail: trace.fail.bind(trace),
|
|
417
|
-
output: trace.output.bind(trace),
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
else {
|
|
421
|
-
ensureManagedTrace(event);
|
|
422
|
-
if (managedTrace) {
|
|
423
|
-
ownedTrace = managedTrace;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
const identityTrace = (options.trace ?? managedTrace);
|
|
427
|
-
if (identityTrace) {
|
|
428
|
-
applyIdentity(identityTrace, eventMetadata(event));
|
|
429
|
-
}
|
|
430
|
-
const eventError = "error" in event && event.error != null
|
|
431
|
-
? (0, error_message_1.describeError)(event.error)
|
|
432
|
-
: undefined;
|
|
433
|
-
if (eventError && !runError) {
|
|
434
|
-
runError = eventError;
|
|
435
|
-
}
|
|
436
|
-
const endedAt = new Date();
|
|
437
|
-
const startedAt = runStartedAt ?? endedAt;
|
|
438
|
-
const durationMs = Math.max(0, endedAt.getTime() - startedAt.getTime());
|
|
439
|
-
const successOutput = endOutput(event);
|
|
440
|
-
endingTrace = ownedTrace;
|
|
441
|
-
// Delay the HTTP send until after generateText returns so optional
|
|
442
|
-
// recordResult can stamp usage onto the same generation spans. A
|
|
443
|
-
// macrotask fallback still delivers if the caller never waits for ingest.
|
|
444
|
-
let delivered = false;
|
|
445
|
-
const deliver = async () => {
|
|
446
|
-
if (delivered)
|
|
447
|
-
return;
|
|
448
|
-
delivered = true;
|
|
449
|
-
try {
|
|
450
|
-
if (!ownedTrace)
|
|
451
|
-
return;
|
|
452
|
-
const terminalError = runError;
|
|
453
|
-
// Explicit handles already own startedAt from construction; only pass
|
|
454
|
-
// durationMs for managed traces we started with runStartedAt.
|
|
455
|
-
const timing = explicit ? { endedAt } : { durationMs, endedAt };
|
|
456
|
-
if (terminalError) {
|
|
457
|
-
ownedTrace.fail(terminalError);
|
|
458
|
-
ownedTrace.output(undefined);
|
|
459
|
-
await ownedTrace.end(timing);
|
|
460
|
-
return;
|
|
37
|
+
// Runs in flight by AI SDK 7 callId; AI SDK 6 runs share UNKEYED_RUN.
|
|
38
|
+
const runs = new Map();
|
|
39
|
+
let runsOpened = 0;
|
|
40
|
+
// The run each failure's error or stop's reason belongs to, so a fail()
|
|
41
|
+
// that repeats it (from a catch block, even after the run was sent) is
|
|
42
|
+
// never attributed to another run. A string reason can't key a WeakMap,
|
|
43
|
+
// so the latest one is kept on its own.
|
|
44
|
+
const claims = new WeakMap();
|
|
45
|
+
let lastPrimitiveClaim;
|
|
46
|
+
let warnedConcurrentV6 = false;
|
|
47
|
+
function trackPending(delivery) {
|
|
48
|
+
pending.add(delivery);
|
|
49
|
+
void delivery.finally(() => pending.delete(delivery));
|
|
50
|
+
}
|
|
51
|
+
function newRun(key) {
|
|
52
|
+
runsOpened += 1;
|
|
53
|
+
const run = new vercel_ai_run_1.VercelAIRun({
|
|
54
|
+
options,
|
|
55
|
+
getLemma,
|
|
56
|
+
trackPending,
|
|
57
|
+
release: () => {
|
|
58
|
+
if (runs.get(key) === run)
|
|
59
|
+
runs.delete(key);
|
|
60
|
+
},
|
|
61
|
+
claim: (value) => {
|
|
62
|
+
if (typeof value === "object" && value !== null) {
|
|
63
|
+
claims.set(value, run);
|
|
461
64
|
}
|
|
462
|
-
if (
|
|
463
|
-
|
|
65
|
+
else if (value !== undefined) {
|
|
66
|
+
lastPrimitiveClaim = { value, run };
|
|
464
67
|
}
|
|
465
|
-
|
|
466
|
-
await ownedTrace.end({
|
|
467
|
-
output: successOutput,
|
|
468
|
-
...timing,
|
|
469
|
-
});
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
finally {
|
|
473
|
-
// Fully reset so the same integration object can be reused sequentially.
|
|
474
|
-
resetRunState();
|
|
475
|
-
}
|
|
476
|
-
};
|
|
477
|
-
deliverOwnedTrace = deliver;
|
|
478
|
-
trackPending(new Promise((resolve, reject) => {
|
|
479
|
-
(0, schedule_1.scheduleMacrotask)(() => {
|
|
480
|
-
deliver().then(resolve, reject);
|
|
481
|
-
});
|
|
482
|
-
}));
|
|
483
|
-
}
|
|
484
|
-
function recordResult(result) {
|
|
485
|
-
const trace = (options.trace ?? managedTrace);
|
|
486
|
-
if (!trace)
|
|
487
|
-
return 0;
|
|
488
|
-
return trace.applyGenerationUsage(result);
|
|
489
|
-
}
|
|
490
|
-
function recordV6Generation(event, stored) {
|
|
491
|
-
const resolved = resolveTrace(stored?.event);
|
|
492
|
-
if (!resolved)
|
|
493
|
-
return;
|
|
494
|
-
const { trace } = resolved;
|
|
495
|
-
const startedAt = stored?.startedAt ?? new Date();
|
|
496
|
-
const endedAt = new Date();
|
|
497
|
-
const durationMs = resolveDurationMs(startedAt, endedAt, undefined);
|
|
498
|
-
const id = crypto.randomUUID();
|
|
499
|
-
const name = typeof options.generationName === "function"
|
|
500
|
-
? options.generationName(event)
|
|
501
|
-
: (options.generationName ?? "vercel-ai-generation");
|
|
502
|
-
const output = v6Output(event);
|
|
503
|
-
const generationError = "error" in event && event.error != null
|
|
504
|
-
? (0, error_message_1.describeError)(event.error)
|
|
505
|
-
: undefined;
|
|
506
|
-
const fallbackSystem = siblingSystem(v6Starts.at(-1)?.event);
|
|
507
|
-
const generation = {
|
|
508
|
-
name,
|
|
509
|
-
input: stored?.event && v6Input(stored.event, fallbackSystem),
|
|
510
|
-
output: generationError ? undefined : output,
|
|
511
|
-
metadata: options.metadata,
|
|
512
|
-
attributes: withIntegrationAttrs(),
|
|
513
|
-
model: event.model.modelId,
|
|
514
|
-
startedAt,
|
|
515
|
-
endedAt,
|
|
516
|
-
durationMs,
|
|
517
|
-
llmProvider: event.model.provider,
|
|
518
|
-
llmInputMessages: stored?.event
|
|
519
|
-
? v6NormalizedMessages(stored.event, fallbackSystem)
|
|
520
|
-
: undefined,
|
|
521
|
-
llmOutputMessages: output === undefined || generationError
|
|
522
|
-
? undefined
|
|
523
|
-
: [{ role: "assistant", content: output }],
|
|
524
|
-
llmTools: stored?.event.tools,
|
|
525
|
-
status: generationError ? "ERROR" : undefined,
|
|
526
|
-
error: generationError,
|
|
527
|
-
usage: eventUsage(event),
|
|
528
|
-
};
|
|
529
|
-
if (stored?.handle) {
|
|
530
|
-
stored.handle.end(generation);
|
|
531
|
-
latestV6GenerationId = stored.handle.id;
|
|
532
|
-
trackGeneration(stored.handle);
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
trace.recordGeneration({
|
|
536
|
-
id,
|
|
537
|
-
...generation,
|
|
68
|
+
},
|
|
538
69
|
});
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
70
|
+
runs.set(key, run);
|
|
71
|
+
return run;
|
|
72
|
+
}
|
|
73
|
+
/** The event's run, opened when this is the run's first event. */
|
|
74
|
+
function openRun(event) {
|
|
75
|
+
const key = (0, vercel_ai_events_1.runKey)(event);
|
|
76
|
+
return runs.get(key) ?? newRun(key);
|
|
77
|
+
}
|
|
78
|
+
function existingRun(event) {
|
|
79
|
+
return runs.get((0, vercel_ai_events_1.runKey)(event));
|
|
80
|
+
}
|
|
81
|
+
/** A terminal event with no run still ends a trace handle passed in. */
|
|
82
|
+
function terminalRun(event) {
|
|
83
|
+
return (existingRun(event) ??
|
|
84
|
+
((0, vercel_ai_run_1.endableTrace)(options) ? newRun((0, vercel_ai_events_1.runKey)(event)) : undefined));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The run of an integration that has only ever opened one: the
|
|
88
|
+
* per-request pattern, where a caller-side call can belong to no other run.
|
|
89
|
+
* After a second run, even one that started after the first was sent, a
|
|
90
|
+
* call without a call ID may come from another request.
|
|
91
|
+
*/
|
|
92
|
+
function onlyRunEver() {
|
|
93
|
+
return runsOpened === 1 ? runs.values().next().value : undefined;
|
|
94
|
+
}
|
|
95
|
+
function claimant(value) {
|
|
96
|
+
if (typeof value === "object" && value !== null)
|
|
97
|
+
return claims.get(value);
|
|
98
|
+
const primitive = lastPrimitiveClaim;
|
|
99
|
+
return primitive && primitive.value === value ? primitive.run : undefined;
|
|
100
|
+
}
|
|
101
|
+
/** The run an operation's start event opens; none for an overlapping v6 run. */
|
|
102
|
+
function startRun(event, signal) {
|
|
103
|
+
const key = (0, vercel_ai_events_1.runKey)(event);
|
|
104
|
+
const current = runs.get(key);
|
|
105
|
+
current?.stopIfAborted();
|
|
106
|
+
if (current?.phase === "active") {
|
|
107
|
+
// AI SDK 6 runs share a key, and nothing tells them apart. An AI SDK 7
|
|
108
|
+
// call ID repeats only when the integration is passed twice for a call.
|
|
109
|
+
if (key === vercel_ai_events_1.UNKEYED_RUN && !warnedConcurrentV6) {
|
|
110
|
+
console.warn(CONCURRENT_V6_WARNING);
|
|
111
|
+
warnedConcurrentV6 = true;
|
|
545
112
|
}
|
|
546
|
-
sawV7StepZero = true;
|
|
547
|
-
}
|
|
548
|
-
const resolved = resolveTrace(event);
|
|
549
|
-
if (!resolved)
|
|
550
|
-
return;
|
|
551
|
-
const { trace } = resolved;
|
|
552
|
-
const name = typeof options.generationName === "function"
|
|
553
|
-
? options.generationName({
|
|
554
|
-
callId: event.callId,
|
|
555
|
-
provider: event.provider,
|
|
556
|
-
modelId: event.modelId,
|
|
557
|
-
content: [],
|
|
558
|
-
performance: {},
|
|
559
|
-
})
|
|
560
|
-
: (options.generationName ?? "vercel-ai-generation");
|
|
561
|
-
const startedAt = new Date();
|
|
562
|
-
const messages = withSystemMessage(resolveSystemForMessages(event, event.messages, siblingSystem(v6Starts.at(-1)?.event)), event.messages);
|
|
563
|
-
const handle = trace.startGeneration({
|
|
564
|
-
name,
|
|
565
|
-
input: messages,
|
|
566
|
-
metadata: options.metadata,
|
|
567
|
-
attributes: withIntegrationAttrs(),
|
|
568
|
-
model: event.modelId,
|
|
569
|
-
startedAt,
|
|
570
|
-
llmProvider: event.provider,
|
|
571
|
-
llmInputMessages: messages,
|
|
572
|
-
llmTools: event.tools,
|
|
573
|
-
});
|
|
574
|
-
const stored = { event, startedAt, handle };
|
|
575
|
-
v7Steps.set(v7StepKey(event.callId, event.stepNumber), stored);
|
|
576
|
-
generationSpanIdsByCallId.set(event.callId, handle.id);
|
|
577
|
-
trackGeneration(handle);
|
|
578
|
-
}
|
|
579
|
-
function startV6Generation(event, key) {
|
|
580
|
-
const resolved = resolveTrace(event);
|
|
581
|
-
if (!resolved)
|
|
582
113
|
return undefined;
|
|
583
|
-
const { trace } = resolved;
|
|
584
|
-
const startedAt = new Date();
|
|
585
|
-
const name = typeof options.generationName === "function"
|
|
586
|
-
? options.generationName({
|
|
587
|
-
stepNumber: "stepNumber" in event ? event.stepNumber : 0,
|
|
588
|
-
model: event.model,
|
|
589
|
-
})
|
|
590
|
-
: (options.generationName ?? "vercel-ai-generation");
|
|
591
|
-
const fallbackSystem = siblingSystem(v6Starts.at(-1)?.event);
|
|
592
|
-
const input = v6Input(event, fallbackSystem);
|
|
593
|
-
const handle = trace.startGeneration({
|
|
594
|
-
name,
|
|
595
|
-
input,
|
|
596
|
-
metadata: options.metadata,
|
|
597
|
-
attributes: withIntegrationAttrs(),
|
|
598
|
-
model: event.model.modelId,
|
|
599
|
-
startedAt,
|
|
600
|
-
llmProvider: event.model.provider,
|
|
601
|
-
llmInputMessages: v6NormalizedMessages(event, fallbackSystem),
|
|
602
|
-
llmTools: event.tools,
|
|
603
|
-
});
|
|
604
|
-
latestV6GenerationId = handle.id;
|
|
605
|
-
trackGeneration(handle);
|
|
606
|
-
return { event, startedAt, handle, key };
|
|
607
|
-
}
|
|
608
|
-
function endV7Generation(event) {
|
|
609
|
-
const key = v7StepKey(event.callId, event.stepNumber);
|
|
610
|
-
const stored = v7Steps.get(key);
|
|
611
|
-
v7Steps.delete(key);
|
|
612
|
-
if (!stored)
|
|
613
|
-
return;
|
|
614
|
-
const endedAt = new Date();
|
|
615
|
-
const durationMs = resolveDurationMs(stored.startedAt, endedAt, event.performance?.stepTimeMs ?? event.performance?.responseTimeMs);
|
|
616
|
-
const output = structuredAssistantOutput(event.text, event.content);
|
|
617
|
-
const generationError = event.error != null ? (0, error_message_1.describeError)(event.error) : undefined;
|
|
618
|
-
for (const toolCall of event.toolCalls ?? []) {
|
|
619
|
-
if (toolCall.toolCallId) {
|
|
620
|
-
generationSpanIdsByToolCallId.set(toolCall.toolCallId, stored.handle.id);
|
|
621
|
-
}
|
|
622
114
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
llmOutputMessages: output === undefined || generationError
|
|
630
|
-
? undefined
|
|
631
|
-
: [{ role: "assistant", content: output }],
|
|
632
|
-
status: generationError ? "ERROR" : undefined,
|
|
633
|
-
error: generationError,
|
|
634
|
-
usage: eventUsage(event),
|
|
635
|
-
});
|
|
636
|
-
}
|
|
637
|
-
function resolveToolParentId(callId, toolCallId) {
|
|
638
|
-
return ((toolCallId && generationSpanIdsByToolCallId.get(toolCallId)) ||
|
|
639
|
-
(callId ? generationSpanIdsByCallId.get(callId) : undefined) ||
|
|
640
|
-
latestV6GenerationId);
|
|
115
|
+
// A run still waiting for its delivery macrotask is complete; send it
|
|
116
|
+
// now so it can't merge with this one.
|
|
117
|
+
void current?.deliver();
|
|
118
|
+
const run = newRun(key);
|
|
119
|
+
run.start(event, signal);
|
|
120
|
+
return run;
|
|
641
121
|
}
|
|
642
122
|
const integration = {
|
|
123
|
+
onStart(event) {
|
|
124
|
+
startRun(event);
|
|
125
|
+
},
|
|
126
|
+
onStepStart(event) {
|
|
127
|
+
openRun(event).stepStart(event);
|
|
128
|
+
},
|
|
129
|
+
// AI SDK 7 sends each step end to onStepEnd and to the deprecated
|
|
130
|
+
// onStepFinish; the first ends the step's open generation and the second
|
|
131
|
+
// finds nothing left to end.
|
|
132
|
+
onStepEnd(event) {
|
|
133
|
+
existingRun(event)?.stepEnd(event);
|
|
134
|
+
},
|
|
135
|
+
onStepFinish(event) {
|
|
136
|
+
existingRun(event)?.stepEnd(event);
|
|
137
|
+
},
|
|
643
138
|
onLanguageModelCallStart(event) {
|
|
644
|
-
|
|
645
|
-
generationSpanIdsByCallId.size > 0 &&
|
|
646
|
-
!generationSpanIdsByCallId.has(event.callId)) {
|
|
647
|
-
const belongsToActiveStep = [...v7Steps.values()].some((step) => step.event.callId === event.callId);
|
|
648
|
-
// Multi-step v7 registers the next callId on step start before the
|
|
649
|
-
// model callback. Any other new callId is concurrent reuse.
|
|
650
|
-
if (!belongsToActiveStep) {
|
|
651
|
-
throw new Error(CONCURRENT_REUSE_ERROR);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
const resolved = resolveTrace(event);
|
|
655
|
-
if (!resolved)
|
|
656
|
-
return;
|
|
657
|
-
const { trace } = resolved;
|
|
658
|
-
if (generationSpanIdsByCallId.has(event.callId)) {
|
|
659
|
-
modelCalls.set(event.callId, { event, startedAt: new Date() });
|
|
660
|
-
return;
|
|
661
|
-
}
|
|
662
|
-
const startedAt = new Date();
|
|
663
|
-
const name = typeof options.generationName === "function"
|
|
664
|
-
? options.generationName({
|
|
665
|
-
callId: event.callId,
|
|
666
|
-
provider: event.provider,
|
|
667
|
-
modelId: event.modelId,
|
|
668
|
-
content: [],
|
|
669
|
-
performance: {},
|
|
670
|
-
})
|
|
671
|
-
: (options.generationName ?? "vercel-ai-generation");
|
|
672
|
-
const handle = trace.startGeneration({
|
|
673
|
-
name,
|
|
674
|
-
input: event.messages,
|
|
675
|
-
metadata: options.metadata,
|
|
676
|
-
attributes: withIntegrationAttrs(),
|
|
677
|
-
model: event.modelId,
|
|
678
|
-
startedAt,
|
|
679
|
-
llmProvider: event.provider,
|
|
680
|
-
llmInputMessages: event.messages,
|
|
681
|
-
llmTools: event.tools,
|
|
682
|
-
});
|
|
683
|
-
modelCalls.set(event.callId, { event, startedAt, handle });
|
|
684
|
-
generationSpanIdsByCallId.set(event.callId, handle.id);
|
|
685
|
-
trackGeneration(handle);
|
|
139
|
+
openRun(event).modelCallStart(event);
|
|
686
140
|
},
|
|
687
141
|
onLanguageModelCallEnd(event) {
|
|
688
|
-
|
|
689
|
-
return;
|
|
690
|
-
const stored = modelCalls.get(event.callId);
|
|
691
|
-
modelCalls.delete(event.callId);
|
|
692
|
-
if (!stored?.handle)
|
|
693
|
-
return;
|
|
694
|
-
const endedAt = new Date();
|
|
695
|
-
const durationMs = resolveDurationMs(stored.startedAt, endedAt, event.performance.responseTimeMs);
|
|
696
|
-
const output = structuredAssistantOutput(undefined, event.content);
|
|
697
|
-
stored.handle.end({
|
|
698
|
-
output,
|
|
699
|
-
model: event.modelId,
|
|
700
|
-
durationMs,
|
|
701
|
-
endedAt: addMs(stored.startedAt, durationMs),
|
|
702
|
-
llmProvider: event.provider,
|
|
703
|
-
llmOutputMessages: output === undefined
|
|
704
|
-
? undefined
|
|
705
|
-
: [{ role: "assistant", content: output }],
|
|
706
|
-
usage: eventUsage(event),
|
|
707
|
-
});
|
|
142
|
+
existingRun(event)?.modelCallEnd(event);
|
|
708
143
|
},
|
|
144
|
+
// A tool event with no run in flight trails a run already sent.
|
|
709
145
|
onToolExecutionStart(event) {
|
|
710
|
-
|
|
711
|
-
if (!resolved)
|
|
712
|
-
return;
|
|
713
|
-
const { trace } = resolved;
|
|
714
|
-
const parentId = resolveToolParentId(event.callId, event.toolCall.toolCallId);
|
|
715
|
-
const name = typeof options.toolName === "function"
|
|
716
|
-
? options.toolName({
|
|
717
|
-
...event,
|
|
718
|
-
toolExecutionMs: undefined,
|
|
719
|
-
toolOutput: { type: "tool-result" },
|
|
720
|
-
})
|
|
721
|
-
: (options.toolName ?? event.toolCall.toolName);
|
|
722
|
-
const startedAt = new Date();
|
|
723
|
-
const handle = trace.startTool({
|
|
724
|
-
name,
|
|
725
|
-
parentId,
|
|
726
|
-
input: event.toolCall.input,
|
|
727
|
-
metadata: options.metadata,
|
|
728
|
-
attributes: withIntegrationAttrs(),
|
|
729
|
-
startedAt,
|
|
730
|
-
toolName: event.toolCall.toolName,
|
|
731
|
-
});
|
|
732
|
-
if (event.toolCall.toolCallId) {
|
|
733
|
-
toolExecutions.set(event.toolCall.toolCallId, {
|
|
734
|
-
handle,
|
|
735
|
-
startedAt,
|
|
736
|
-
parentId,
|
|
737
|
-
});
|
|
738
|
-
}
|
|
146
|
+
existingRun(event)?.toolStart(event);
|
|
739
147
|
},
|
|
740
148
|
onToolCallStart(event) {
|
|
741
|
-
|
|
149
|
+
existingRun(event)?.toolStart(event);
|
|
742
150
|
},
|
|
743
151
|
onToolExecutionEnd(event) {
|
|
744
|
-
|
|
745
|
-
if (!resolved)
|
|
746
|
-
return;
|
|
747
|
-
const { trace } = resolved;
|
|
748
|
-
const name = typeof options.toolName === "function"
|
|
749
|
-
? options.toolName(event)
|
|
750
|
-
: (options.toolName ?? event.toolCall.toolName);
|
|
751
|
-
const storedTool = event.toolCall.toolCallId
|
|
752
|
-
? toolExecutions.get(event.toolCall.toolCallId)
|
|
753
|
-
: undefined;
|
|
754
|
-
if (event.toolCall.toolCallId) {
|
|
755
|
-
toolExecutions.delete(event.toolCall.toolCallId);
|
|
756
|
-
}
|
|
757
|
-
if (storedTool) {
|
|
758
|
-
const endedAt = addMs(storedTool.startedAt, event.toolExecutionMs);
|
|
759
|
-
const durationMs = resolveDurationMs(storedTool.startedAt, endedAt, event.toolExecutionMs);
|
|
760
|
-
storedTool.handle.end({
|
|
761
|
-
durationMs,
|
|
762
|
-
endedAt: addMs(storedTool.startedAt, durationMs),
|
|
763
|
-
toolName: event.toolCall.toolName,
|
|
764
|
-
...toolOutput(event),
|
|
765
|
-
});
|
|
766
|
-
coverParent(storedTool.parentId, addMs(storedTool.startedAt, durationMs));
|
|
767
|
-
return;
|
|
768
|
-
}
|
|
769
|
-
const endedAt = new Date();
|
|
770
|
-
const startedAt = subtractMs(endedAt, event.toolExecutionMs);
|
|
771
|
-
const durationMs = resolveDurationMs(startedAt, endedAt, event.toolExecutionMs);
|
|
772
|
-
const parentId = resolveToolParentId(event.callId, event.toolCall.toolCallId);
|
|
773
|
-
trace.recordTool({
|
|
774
|
-
name,
|
|
775
|
-
parentId,
|
|
776
|
-
toolName: event.toolCall.toolName,
|
|
777
|
-
input: event.toolCall.input,
|
|
778
|
-
metadata: options.metadata,
|
|
779
|
-
attributes: withIntegrationAttrs(),
|
|
780
|
-
durationMs,
|
|
781
|
-
startedAt: subtractMs(endedAt, durationMs),
|
|
782
|
-
endedAt,
|
|
783
|
-
...toolOutput(event),
|
|
784
|
-
});
|
|
785
|
-
coverParent(parentId, endedAt);
|
|
152
|
+
existingRun(event)?.toolEnd(event);
|
|
786
153
|
},
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
recordedV6Step = false;
|
|
790
|
-
const key = `start:${v6Starts.length}:${Date.now()}`;
|
|
791
|
-
v6Starts.push({ event, startedAt: new Date(), key });
|
|
792
|
-
resolveTrace(event);
|
|
793
|
-
},
|
|
794
|
-
onStepStart(event) {
|
|
795
|
-
if (isV7StepStart(event)) {
|
|
796
|
-
startV7Generation(event);
|
|
797
|
-
return;
|
|
798
|
-
}
|
|
799
|
-
const key = `step:${event.stepNumber}`;
|
|
800
|
-
if (v6Steps.has(key)) {
|
|
801
|
-
// Duplicate step start for the same step number — ignore.
|
|
802
|
-
return;
|
|
803
|
-
}
|
|
804
|
-
const stored = startV6Generation(event, key);
|
|
805
|
-
if (stored)
|
|
806
|
-
v6Steps.set(key, stored);
|
|
807
|
-
},
|
|
808
|
-
onStepEnd(event) {
|
|
809
|
-
if (phase !== "active" && phase !== "ending")
|
|
810
|
-
return;
|
|
811
|
-
endV7Generation(event);
|
|
154
|
+
onToolCallFinish(event) {
|
|
155
|
+
existingRun(event)?.v6ToolEnd(event);
|
|
812
156
|
},
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
return;
|
|
816
|
-
recordedV6Step = true;
|
|
817
|
-
const key = `step:${event.stepNumber}`;
|
|
818
|
-
const stored = v6Steps.get(key);
|
|
819
|
-
v6Steps.delete(key);
|
|
820
|
-
recordV6Generation(event, stored);
|
|
157
|
+
onFinish(event) {
|
|
158
|
+
terminalRun(event)?.finish(event);
|
|
821
159
|
},
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
// Terminal-only path for an explicit trace handle with no prior events.
|
|
825
|
-
if (!hasExplicitEndableTrace())
|
|
826
|
-
return;
|
|
827
|
-
beginActivity();
|
|
828
|
-
}
|
|
829
|
-
if (phase === "active") {
|
|
830
|
-
if (recordedV6Step) {
|
|
831
|
-
v6Starts.shift();
|
|
832
|
-
recordedV6Step = false;
|
|
833
|
-
}
|
|
834
|
-
else {
|
|
835
|
-
recordV6Generation(event, v6Starts.shift());
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
await endOwnedTrace(event);
|
|
160
|
+
onEnd(event) {
|
|
161
|
+
terminalRun(event)?.end(event);
|
|
839
162
|
},
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
beginActivity();
|
|
845
|
-
}
|
|
846
|
-
await endOwnedTrace(event);
|
|
163
|
+
// Neither handler awaits delivery: the AI SDK awaits them before it
|
|
164
|
+
// closes the stream or rethrows to the caller.
|
|
165
|
+
onAbort(event) {
|
|
166
|
+
terminalRun(event)?.abort(event.reason);
|
|
847
167
|
},
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
if (!resolved)
|
|
851
|
-
return;
|
|
852
|
-
const { trace } = resolved;
|
|
853
|
-
const name = typeof options.toolName === "function"
|
|
854
|
-
? options.toolName(event)
|
|
855
|
-
: (options.toolName ?? event.toolCall.toolName);
|
|
856
|
-
const storedTool = event.toolCall.toolCallId
|
|
857
|
-
? toolExecutions.get(event.toolCall.toolCallId)
|
|
858
|
-
: undefined;
|
|
859
|
-
if (event.toolCall.toolCallId) {
|
|
860
|
-
toolExecutions.delete(event.toolCall.toolCallId);
|
|
861
|
-
}
|
|
862
|
-
if (storedTool) {
|
|
863
|
-
const endedAt = addMs(storedTool.startedAt, event.durationMs);
|
|
864
|
-
const durationMs = resolveDurationMs(storedTool.startedAt, endedAt, event.durationMs);
|
|
865
|
-
const toolEndedAt = addMs(storedTool.startedAt, durationMs);
|
|
866
|
-
storedTool.handle.end({
|
|
867
|
-
durationMs,
|
|
868
|
-
endedAt: toolEndedAt,
|
|
869
|
-
toolName: event.toolCall.toolName,
|
|
870
|
-
...v6ToolOutput(event),
|
|
871
|
-
});
|
|
872
|
-
coverParent(storedTool.parentId, toolEndedAt);
|
|
873
|
-
return;
|
|
874
|
-
}
|
|
875
|
-
const endedAt = new Date();
|
|
876
|
-
const durationMs = resolveDurationMs(subtractMs(endedAt, event.durationMs), endedAt, event.durationMs);
|
|
877
|
-
const startedAt = subtractMs(endedAt, durationMs);
|
|
878
|
-
const parentId = resolveToolParentId(undefined, event.toolCall.toolCallId);
|
|
879
|
-
trace.recordTool({
|
|
880
|
-
name,
|
|
881
|
-
parentId,
|
|
882
|
-
toolName: event.toolCall.toolName,
|
|
883
|
-
input: event.toolCall.input,
|
|
884
|
-
metadata: options.metadata,
|
|
885
|
-
attributes: withIntegrationAttrs(),
|
|
886
|
-
durationMs,
|
|
887
|
-
startedAt,
|
|
888
|
-
endedAt,
|
|
889
|
-
...v6ToolOutput(event),
|
|
890
|
-
});
|
|
891
|
-
coverParent(parentId, endedAt);
|
|
168
|
+
onError(event) {
|
|
169
|
+
openRun(event).stop((0, vercel_ai_events_1.reportedError)(event));
|
|
892
170
|
},
|
|
893
171
|
async fail(error) {
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
beginActivity();
|
|
904
|
-
}
|
|
905
|
-
await endOwnedTrace({ error });
|
|
172
|
+
// An error no run claims is sent as its own failed trace; a stop no run
|
|
173
|
+
// claims has nothing to end, unless a trace handle was passed in.
|
|
174
|
+
const run = claimant(error) ??
|
|
175
|
+
runs.get(vercel_ai_events_1.UNKEYED_RUN) ??
|
|
176
|
+
onlyRunEver() ??
|
|
177
|
+
((0, vercel_ai_events_1.isCallerStop)(error) && !(0, vercel_ai_run_1.endableTrace)(options)
|
|
178
|
+
? undefined
|
|
179
|
+
: newRun(vercel_ai_events_1.UNKEYED_RUN));
|
|
180
|
+
run?.stop(error);
|
|
906
181
|
// Failures have no operation result to wait for — deliver now.
|
|
907
|
-
|
|
908
|
-
await deliverOwnedTrace();
|
|
182
|
+
await run?.deliver();
|
|
909
183
|
},
|
|
910
|
-
recordResult
|
|
184
|
+
recordResult(result) {
|
|
185
|
+
const key = resultCallId(result);
|
|
186
|
+
// A result naming an operation belongs to that run alone.
|
|
187
|
+
const run = key
|
|
188
|
+
? runs.get(key)
|
|
189
|
+
: (runs.get(vercel_ai_events_1.UNKEYED_RUN) ?? onlyRunEver());
|
|
190
|
+
if (run)
|
|
191
|
+
return run.recordResult(result);
|
|
192
|
+
return options.trace?.applyGenerationUsage(result) ?? 0;
|
|
193
|
+
},
|
|
194
|
+
// Each ended run's delivery waits a macrotask for its caller's
|
|
195
|
+
// recordResult(); flushing early would send other requests' runs first.
|
|
911
196
|
async flush() {
|
|
912
|
-
if (deliverOwnedTrace)
|
|
913
|
-
await deliverOwnedTrace();
|
|
914
197
|
await Promise.all(Array.from(pending));
|
|
915
198
|
},
|
|
916
199
|
async shutdown() {
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
}
|
|
200
|
+
for (const run of [...runs.values()])
|
|
201
|
+
run.close();
|
|
920
202
|
await integration.flush();
|
|
921
|
-
|
|
203
|
+
},
|
|
204
|
+
withAbortSignal(signal) {
|
|
205
|
+
// The runs this call's onStart opened. A run its signal ended can be
|
|
206
|
+
// sent while the AI SDK still reports the call, and those events must
|
|
207
|
+
// not open another run for the same call ID.
|
|
208
|
+
const opened = new Map();
|
|
209
|
+
const callbacks = {
|
|
210
|
+
onStart(event) {
|
|
211
|
+
const run = startRun(event, signal);
|
|
212
|
+
if (run)
|
|
213
|
+
opened.set((0, vercel_ai_events_1.runKey)(event), run);
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
for (const name of Object.keys(CALL_EVENT_CALLBACKS)) {
|
|
217
|
+
const callback = integration[name];
|
|
218
|
+
callbacks[name] = (event) => {
|
|
219
|
+
if (opened.get((0, vercel_ai_events_1.runKey)(event))?.phase !== "sent") {
|
|
220
|
+
return callback(event);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
return callbacks;
|
|
922
225
|
},
|
|
923
226
|
};
|
|
924
227
|
return integration;
|