@agent-inspect/ai-sdk 6.31.34 → 6.31.36
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/package.json +2 -2
- package/dist/index.cjs +0 -642
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -122
- package/dist/index.d.ts +0 -122
- package/dist/index.mjs +0 -640
- package/dist/index.mjs.map +0 -1
package/dist/index.mjs
DELETED
|
@@ -1,640 +0,0 @@
|
|
|
1
|
-
import { bindTelemetryIntegration } from 'ai';
|
|
2
|
-
import { createAdapterPreviewCapture } from 'agent-inspect/advanced';
|
|
3
|
-
import { fileWriter } from 'agent-inspect/writers';
|
|
4
|
-
|
|
5
|
-
// packages/ai-sdk/src/index.ts
|
|
6
|
-
|
|
7
|
-
// packages/ai-sdk/src/usage.ts
|
|
8
|
-
function nonNegativeFinite(value) {
|
|
9
|
-
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
10
|
-
}
|
|
11
|
-
function isUsageRecord(value) {
|
|
12
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
13
|
-
}
|
|
14
|
-
function summarizeAiSdkUsage(usage) {
|
|
15
|
-
if (!usage) return void 0;
|
|
16
|
-
let input = nonNegativeFinite(usage.inputTokens) ?? nonNegativeFinite(usage.input);
|
|
17
|
-
let output = nonNegativeFinite(usage.outputTokens) ?? nonNegativeFinite(usage.output);
|
|
18
|
-
let total = nonNegativeFinite(usage.totalTokens) ?? nonNegativeFinite(usage.total);
|
|
19
|
-
let cached = nonNegativeFinite(usage.cachedInputTokens) ?? nonNegativeFinite(usage.cached);
|
|
20
|
-
let cacheWrite = nonNegativeFinite(usage.cacheWrite);
|
|
21
|
-
let reasoning = nonNegativeFinite(usage.reasoning);
|
|
22
|
-
if (isUsageRecord(usage.inputTokens)) {
|
|
23
|
-
input = nonNegativeFinite(usage.inputTokens.total) ?? input;
|
|
24
|
-
cached = nonNegativeFinite(usage.inputTokens.cacheRead) ?? nonNegativeFinite(usage.inputTokens.cache_read) ?? cached;
|
|
25
|
-
cacheWrite = nonNegativeFinite(usage.inputTokens.cacheWrite) ?? nonNegativeFinite(usage.inputTokens.cache_write) ?? nonNegativeFinite(usage.inputTokens.cacheCreation) ?? cacheWrite;
|
|
26
|
-
}
|
|
27
|
-
if (isUsageRecord(usage.outputTokens)) {
|
|
28
|
-
output = nonNegativeFinite(usage.outputTokens.total) ?? output;
|
|
29
|
-
reasoning = nonNegativeFinite(usage.outputTokens.reasoning) ?? nonNegativeFinite(usage.outputTokens.reasoningTokens) ?? reasoning;
|
|
30
|
-
}
|
|
31
|
-
if (isUsageRecord(usage.inputTokenDetails)) {
|
|
32
|
-
cached = nonNegativeFinite(usage.inputTokenDetails.cacheReadTokens) ?? nonNegativeFinite(usage.inputTokenDetails.cacheRead) ?? cached;
|
|
33
|
-
cacheWrite = nonNegativeFinite(usage.inputTokenDetails.cacheWriteTokens) ?? nonNegativeFinite(usage.inputTokenDetails.cacheWrite) ?? cacheWrite;
|
|
34
|
-
}
|
|
35
|
-
if (isUsageRecord(usage.outputTokenDetails)) {
|
|
36
|
-
reasoning = nonNegativeFinite(usage.outputTokenDetails.reasoningTokens) ?? nonNegativeFinite(usage.outputTokenDetails.reasoning) ?? reasoning;
|
|
37
|
-
}
|
|
38
|
-
const tokenUsage = {};
|
|
39
|
-
if (input !== void 0) tokenUsage.input = input;
|
|
40
|
-
if (output !== void 0) tokenUsage.output = output;
|
|
41
|
-
if (total !== void 0) tokenUsage.total = total;
|
|
42
|
-
if (cached !== void 0) tokenUsage.cached = cached;
|
|
43
|
-
if (cacheWrite !== void 0) tokenUsage.cacheWrite = cacheWrite;
|
|
44
|
-
if (reasoning !== void 0) tokenUsage.reasoning = reasoning;
|
|
45
|
-
return Object.keys(tokenUsage).length > 0 ? tokenUsage : void 0;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// packages/ai-sdk/src/index.ts
|
|
49
|
-
var AI_SDK_SOURCE = {
|
|
50
|
-
type: "ai-sdk",
|
|
51
|
-
name: "@agent-inspect/ai-sdk",
|
|
52
|
-
version: "experimental"
|
|
53
|
-
};
|
|
54
|
-
function nowIso() {
|
|
55
|
-
return (/* @__PURE__ */ new Date()).toISOString();
|
|
56
|
-
}
|
|
57
|
-
function createId(prefix) {
|
|
58
|
-
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
|
59
|
-
return `${prefix}_${crypto.randomUUID()}`;
|
|
60
|
-
}
|
|
61
|
-
return `${prefix}_${Date.now()}_${Math.random().toString(16).slice(2)}`;
|
|
62
|
-
}
|
|
63
|
-
function normalizeError(error) {
|
|
64
|
-
if (error instanceof Error && error.message.trim() !== "") {
|
|
65
|
-
return error.message;
|
|
66
|
-
}
|
|
67
|
-
if (typeof error === "string" && error.trim() !== "") {
|
|
68
|
-
return error;
|
|
69
|
-
}
|
|
70
|
-
return "Unknown AI SDK integration error";
|
|
71
|
-
}
|
|
72
|
-
function durationMs(startedAt, endedAt) {
|
|
73
|
-
const started = Date.parse(startedAt);
|
|
74
|
-
const ended = Date.parse(endedAt);
|
|
75
|
-
if (!Number.isFinite(started) || !Number.isFinite(ended)) return void 0;
|
|
76
|
-
return Math.max(0, ended - started);
|
|
77
|
-
}
|
|
78
|
-
function countRecordKeys(value) {
|
|
79
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
80
|
-
return void 0;
|
|
81
|
-
}
|
|
82
|
-
return Object.keys(value).length;
|
|
83
|
-
}
|
|
84
|
-
function normalizeMaxPreviewChars(value) {
|
|
85
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
86
|
-
return void 0;
|
|
87
|
-
}
|
|
88
|
-
return Math.floor(value);
|
|
89
|
-
}
|
|
90
|
-
function summarizeUnknown(value) {
|
|
91
|
-
if (value === null) return { type: "null" };
|
|
92
|
-
if (Array.isArray(value)) return { type: "array", itemCount: value.length };
|
|
93
|
-
if (typeof value === "object") {
|
|
94
|
-
return { type: "object", keyCount: countRecordKeys(value) ?? 0 };
|
|
95
|
-
}
|
|
96
|
-
if (typeof value === "string") {
|
|
97
|
-
return { type: "string", length: value.length };
|
|
98
|
-
}
|
|
99
|
-
const valueType = typeof value;
|
|
100
|
-
if (valueType === "number" || valueType === "boolean" || valueType === "bigint") {
|
|
101
|
-
return { type: valueType };
|
|
102
|
-
}
|
|
103
|
-
if (valueType === "undefined") return { type: "undefined" };
|
|
104
|
-
return { type: "unknown" };
|
|
105
|
-
}
|
|
106
|
-
function summarizeError(error) {
|
|
107
|
-
if (error instanceof Error) {
|
|
108
|
-
return {
|
|
109
|
-
name: error.name || "Error",
|
|
110
|
-
message: error.message
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
if (typeof error === "string") {
|
|
114
|
-
return {
|
|
115
|
-
message: error
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
return {
|
|
119
|
-
message: "Unknown AI SDK tool error"
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
function summarizeModel(model) {
|
|
123
|
-
return {
|
|
124
|
-
provider: model?.provider,
|
|
125
|
-
modelId: model?.modelId
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
function summarizeFinishReason(finishReason) {
|
|
129
|
-
if (typeof finishReason === "string") {
|
|
130
|
-
return { finishReason };
|
|
131
|
-
}
|
|
132
|
-
if (typeof finishReason !== "object" || finishReason === null) {
|
|
133
|
-
return {};
|
|
134
|
-
}
|
|
135
|
-
const value = finishReason;
|
|
136
|
-
return {
|
|
137
|
-
finishReason: typeof value.unified === "string" ? value.unified : void 0,
|
|
138
|
-
rawFinishReason: typeof value.raw === "string" ? value.raw : void 0
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
var AgentInspectAiSdkTelemetryIntegration = class {
|
|
142
|
-
constructor(options) {
|
|
143
|
-
this.options = options;
|
|
144
|
-
this.requestedCapture = options.capture ?? "metadata-only";
|
|
145
|
-
this.preview = createAdapterPreviewCapture({
|
|
146
|
-
capture: this.requestedCapture,
|
|
147
|
-
redactionProfile: options.redactionProfile,
|
|
148
|
-
maxPreviewChars: options.maxPreviewChars,
|
|
149
|
-
onDiagnostic: options.onDiagnostic
|
|
150
|
-
});
|
|
151
|
-
this.effectiveCapture = this.preview.capture;
|
|
152
|
-
this.writer = options.writer ?? (options.traceDir ? fileWriter({ dir: options.traceDir }) : void 0);
|
|
153
|
-
this.recordCaptureOptionWarnings();
|
|
154
|
-
}
|
|
155
|
-
options;
|
|
156
|
-
writer;
|
|
157
|
-
requestedCapture;
|
|
158
|
-
preview;
|
|
159
|
-
effectiveCapture;
|
|
160
|
-
diagnostics = {
|
|
161
|
-
writeFailures: 0,
|
|
162
|
-
lifecycleWarnings: 0,
|
|
163
|
-
flushFailures: 0,
|
|
164
|
-
closeFailures: 0
|
|
165
|
-
};
|
|
166
|
-
activeRun;
|
|
167
|
-
suspendedReason;
|
|
168
|
-
getDiagnostics() {
|
|
169
|
-
return { ...this.diagnostics, capture: this.preview.getDiagnostics() };
|
|
170
|
-
}
|
|
171
|
-
/** Bounded preview attributes for one lifecycle event (empty unless enabled). */
|
|
172
|
-
previewAttributes(fields) {
|
|
173
|
-
return this.preview.applyPreviewFields({}, fields);
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* AI SDK 6 audit (6.29.3): callbacks do not expose a stable run/step UUID on
|
|
177
|
-
* all lifecycle hooks (only `toolCallId` is stable). Overlapping generations
|
|
178
|
-
* therefore cannot be multiplexed safely — abandon and terminalize instead.
|
|
179
|
-
*/
|
|
180
|
-
async terminalizeActiveRun(reason) {
|
|
181
|
-
const run = this.activeRun;
|
|
182
|
-
if (!run) return;
|
|
183
|
-
const endedAt = nowIso();
|
|
184
|
-
const status = reason === "abandoned-overlap" ? "error" : "unknown";
|
|
185
|
-
for (const tool of run.tools.values()) {
|
|
186
|
-
await this.write({
|
|
187
|
-
schemaVersion: "0.2",
|
|
188
|
-
eventId: tool.eventId,
|
|
189
|
-
runId: run.runId,
|
|
190
|
-
parentId: tool.parentId,
|
|
191
|
-
kind: "TOOL",
|
|
192
|
-
name: tool.toolName,
|
|
193
|
-
status,
|
|
194
|
-
timestamp: endedAt,
|
|
195
|
-
startedAt: tool.startedAt,
|
|
196
|
-
endedAt,
|
|
197
|
-
durationMs: durationMs(tool.startedAt, endedAt),
|
|
198
|
-
confidence: "explicit",
|
|
199
|
-
source: AI_SDK_SOURCE,
|
|
200
|
-
attributes: {
|
|
201
|
-
legacyEvent: "step_completed",
|
|
202
|
-
lifecycle: reason,
|
|
203
|
-
toolName: tool.toolName
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
for (const [stepNumber, step] of run.steps.entries()) {
|
|
208
|
-
await this.write({
|
|
209
|
-
schemaVersion: "0.2",
|
|
210
|
-
eventId: step.eventId,
|
|
211
|
-
runId: run.runId,
|
|
212
|
-
parentId: step.parentId,
|
|
213
|
-
kind: "LLM",
|
|
214
|
-
name: `ai-sdk-step-${stepNumber}`,
|
|
215
|
-
status,
|
|
216
|
-
timestamp: endedAt,
|
|
217
|
-
startedAt: step.startedAt,
|
|
218
|
-
endedAt,
|
|
219
|
-
durationMs: durationMs(step.startedAt, endedAt),
|
|
220
|
-
confidence: "explicit",
|
|
221
|
-
source: AI_SDK_SOURCE,
|
|
222
|
-
attributes: {
|
|
223
|
-
legacyEvent: "step_completed",
|
|
224
|
-
lifecycle: reason,
|
|
225
|
-
stepNumber
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
await this.write({
|
|
230
|
-
schemaVersion: "0.2",
|
|
231
|
-
eventId: run.eventId,
|
|
232
|
-
runId: run.runId,
|
|
233
|
-
kind: "RUN",
|
|
234
|
-
name: run.name,
|
|
235
|
-
status,
|
|
236
|
-
timestamp: endedAt,
|
|
237
|
-
startedAt: run.startedAt,
|
|
238
|
-
endedAt,
|
|
239
|
-
durationMs: durationMs(run.startedAt, endedAt),
|
|
240
|
-
confidence: "explicit",
|
|
241
|
-
source: AI_SDK_SOURCE,
|
|
242
|
-
attributes: {
|
|
243
|
-
legacyEvent: "run_completed",
|
|
244
|
-
lifecycle: reason,
|
|
245
|
-
...summarizeModel(run.model)
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
run.steps.clear();
|
|
249
|
-
run.tools.clear();
|
|
250
|
-
this.activeRun = void 0;
|
|
251
|
-
}
|
|
252
|
-
async onStart(event) {
|
|
253
|
-
await this.handleLifecycle("onStart", async () => {
|
|
254
|
-
if (this.activeRun || this.suspendedReason) {
|
|
255
|
-
if (this.activeRun) {
|
|
256
|
-
await this.terminalizeActiveRun("abandoned-overlap");
|
|
257
|
-
}
|
|
258
|
-
this.suspendedReason = "Overlapping AI SDK generation ignored; create one agentInspect() integration per concurrent generation.";
|
|
259
|
-
this.recordLifecycleWarning(this.suspendedReason);
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
const startedAt = nowIso();
|
|
263
|
-
const runId = createId("ai_sdk_run");
|
|
264
|
-
const eventId = createId("event");
|
|
265
|
-
const name = this.options.runName ?? event.functionId ?? event.model.modelId ?? "ai-sdk-generation";
|
|
266
|
-
this.activeRun = {
|
|
267
|
-
runId,
|
|
268
|
-
eventId,
|
|
269
|
-
name,
|
|
270
|
-
startedAt,
|
|
271
|
-
model: event.model,
|
|
272
|
-
steps: /* @__PURE__ */ new Map(),
|
|
273
|
-
tools: /* @__PURE__ */ new Map()
|
|
274
|
-
};
|
|
275
|
-
await this.write({
|
|
276
|
-
schemaVersion: "0.2",
|
|
277
|
-
eventId,
|
|
278
|
-
runId,
|
|
279
|
-
kind: "RUN",
|
|
280
|
-
name,
|
|
281
|
-
status: "running",
|
|
282
|
-
timestamp: startedAt,
|
|
283
|
-
startedAt,
|
|
284
|
-
confidence: "explicit",
|
|
285
|
-
source: AI_SDK_SOURCE,
|
|
286
|
-
attributes: {
|
|
287
|
-
legacyEvent: "run_started",
|
|
288
|
-
...summarizeModel(event.model),
|
|
289
|
-
functionId: event.functionId,
|
|
290
|
-
capture: this.effectiveCapture,
|
|
291
|
-
requestedCapture: this.requestedCapture === this.effectiveCapture ? void 0 : this.requestedCapture,
|
|
292
|
-
previewCaptureSupported: true,
|
|
293
|
-
// Preview rows report the effective bound/profile, which the
|
|
294
|
-
// `share` and `strict` profiles can lower below the request.
|
|
295
|
-
redactionProfile: this.preview.previewEnabled ? this.preview.redactionProfile : this.options.redactionProfile,
|
|
296
|
-
maxPreviewChars: this.preview.previewEnabled ? this.preview.maxPreviewChars : normalizeMaxPreviewChars(this.options.maxPreviewChars),
|
|
297
|
-
recordInputsRequired: false,
|
|
298
|
-
recordOutputsRequired: false,
|
|
299
|
-
toolCount: countRecordKeys(event.tools),
|
|
300
|
-
hasPrompt: event.prompt !== void 0,
|
|
301
|
-
hasMessages: event.messages !== void 0,
|
|
302
|
-
metadataKeyCount: countRecordKeys(event.metadata),
|
|
303
|
-
...this.previewAttributes({
|
|
304
|
-
input: event.prompt ?? event.messages
|
|
305
|
-
})
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
async onStepStart(event) {
|
|
311
|
-
await this.handleLifecycle("onStepStart", async () => {
|
|
312
|
-
const run = this.getActiveRun("onStepStart");
|
|
313
|
-
if (!run) return;
|
|
314
|
-
const startedAt = nowIso();
|
|
315
|
-
const stepEventId = createId("event");
|
|
316
|
-
run.steps.set(event.stepNumber, {
|
|
317
|
-
eventId: stepEventId,
|
|
318
|
-
parentId: run.eventId,
|
|
319
|
-
startedAt
|
|
320
|
-
});
|
|
321
|
-
await this.write({
|
|
322
|
-
schemaVersion: "0.2",
|
|
323
|
-
eventId: stepEventId,
|
|
324
|
-
runId: run.runId,
|
|
325
|
-
parentId: run.eventId,
|
|
326
|
-
kind: "LLM",
|
|
327
|
-
name: `ai-sdk-step-${event.stepNumber}`,
|
|
328
|
-
status: "running",
|
|
329
|
-
timestamp: startedAt,
|
|
330
|
-
startedAt,
|
|
331
|
-
confidence: "explicit",
|
|
332
|
-
source: AI_SDK_SOURCE,
|
|
333
|
-
attributes: {
|
|
334
|
-
legacyEvent: "step_started",
|
|
335
|
-
stepId: stepEventId,
|
|
336
|
-
...summarizeModel(event.model),
|
|
337
|
-
functionId: event.functionId,
|
|
338
|
-
stepNumber: event.stepNumber,
|
|
339
|
-
priorStepCount: event.steps.length,
|
|
340
|
-
activeToolCount: event.activeTools?.length,
|
|
341
|
-
toolCount: countRecordKeys(event.tools),
|
|
342
|
-
messageCount: event.messages.length,
|
|
343
|
-
metadataKeyCount: countRecordKeys(event.metadata),
|
|
344
|
-
...this.previewAttributes({ input: event.messages })
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
async onStepFinish(event) {
|
|
350
|
-
await this.handleLifecycle("onStepFinish", async () => {
|
|
351
|
-
const run = this.getActiveRun("onStepFinish");
|
|
352
|
-
if (!run) return;
|
|
353
|
-
const endedAt = nowIso();
|
|
354
|
-
const activeStep = run.steps.get(event.stepNumber);
|
|
355
|
-
if (!activeStep) {
|
|
356
|
-
this.recordLifecycleWarning(
|
|
357
|
-
`onStepFinish ignored because step ${event.stepNumber} has no matching start callback.`
|
|
358
|
-
);
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
await this.write({
|
|
362
|
-
schemaVersion: "0.2",
|
|
363
|
-
eventId: activeStep.eventId,
|
|
364
|
-
runId: run.runId,
|
|
365
|
-
parentId: activeStep.parentId,
|
|
366
|
-
kind: "LLM",
|
|
367
|
-
name: `ai-sdk-step-${event.stepNumber}`,
|
|
368
|
-
status: "ok",
|
|
369
|
-
timestamp: endedAt,
|
|
370
|
-
startedAt: activeStep.startedAt,
|
|
371
|
-
endedAt,
|
|
372
|
-
durationMs: durationMs(activeStep.startedAt, endedAt),
|
|
373
|
-
confidence: "explicit",
|
|
374
|
-
source: AI_SDK_SOURCE,
|
|
375
|
-
attributes: {
|
|
376
|
-
legacyEvent: "step_completed",
|
|
377
|
-
stepId: activeStep.eventId,
|
|
378
|
-
...summarizeModel(event.model),
|
|
379
|
-
functionId: event.functionId,
|
|
380
|
-
stepNumber: event.stepNumber,
|
|
381
|
-
...summarizeFinishReason(event.finishReason),
|
|
382
|
-
warningCount: event.warnings?.length ?? 0,
|
|
383
|
-
contentPartCount: event.content.length,
|
|
384
|
-
toolCallCount: event.toolCalls.length,
|
|
385
|
-
toolResultCount: event.toolResults.length,
|
|
386
|
-
responseId: event.response.id,
|
|
387
|
-
responseModelId: event.response.modelId,
|
|
388
|
-
responseTimestamp: event.response.timestamp?.toISOString(),
|
|
389
|
-
metadataKeyCount: countRecordKeys(event.metadata),
|
|
390
|
-
...this.previewAttributes({ output: event.text })
|
|
391
|
-
},
|
|
392
|
-
tokenUsage: summarizeAiSdkUsage(event.usage),
|
|
393
|
-
outputSummary: {
|
|
394
|
-
contentPartCount: event.content.length,
|
|
395
|
-
textLength: event.text.length,
|
|
396
|
-
reasoningPartCount: event.reasoning.length,
|
|
397
|
-
fileCount: event.files.length,
|
|
398
|
-
sourceCount: event.sources.length
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
run.steps.delete(event.stepNumber);
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
async onToolCallStart(event) {
|
|
405
|
-
await this.handleLifecycle("onToolCallStart", async () => {
|
|
406
|
-
const run = this.getActiveRun("onToolCallStart");
|
|
407
|
-
if (!run) return;
|
|
408
|
-
const startedAt = nowIso();
|
|
409
|
-
const eventId = createId("event");
|
|
410
|
-
const toolCall = event.toolCall;
|
|
411
|
-
const step = event.stepNumber === void 0 ? void 0 : run.steps.get(event.stepNumber);
|
|
412
|
-
const parentId = step?.eventId ?? run.eventId;
|
|
413
|
-
if (event.stepNumber === void 0 || !step) {
|
|
414
|
-
this.recordLifecycleWarning(
|
|
415
|
-
"onToolCallStart attached tool to run because the AI SDK callback did not expose a matching active step."
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
run.tools.set(toolCall.toolCallId, {
|
|
419
|
-
eventId,
|
|
420
|
-
parentId,
|
|
421
|
-
startedAt,
|
|
422
|
-
toolName: toolCall.toolName
|
|
423
|
-
});
|
|
424
|
-
await this.write({
|
|
425
|
-
schemaVersion: "0.2",
|
|
426
|
-
eventId,
|
|
427
|
-
runId: run.runId,
|
|
428
|
-
parentId,
|
|
429
|
-
kind: "TOOL",
|
|
430
|
-
name: toolCall.toolName,
|
|
431
|
-
status: "running",
|
|
432
|
-
timestamp: startedAt,
|
|
433
|
-
startedAt,
|
|
434
|
-
confidence: "explicit",
|
|
435
|
-
source: AI_SDK_SOURCE,
|
|
436
|
-
attributes: {
|
|
437
|
-
legacyEvent: "step_started",
|
|
438
|
-
stepId: eventId,
|
|
439
|
-
...summarizeModel(event.model),
|
|
440
|
-
functionId: event.functionId,
|
|
441
|
-
stepNumber: event.stepNumber,
|
|
442
|
-
toolCallId: toolCall.toolCallId,
|
|
443
|
-
toolName: toolCall.toolName,
|
|
444
|
-
dynamic: toolCall.dynamic === true,
|
|
445
|
-
invalid: toolCall.invalid === true,
|
|
446
|
-
providerExecuted: toolCall.providerExecuted === true,
|
|
447
|
-
messageCount: event.messages.length,
|
|
448
|
-
metadataKeyCount: countRecordKeys(event.metadata),
|
|
449
|
-
providerMetadataKeyCount: countRecordKeys(toolCall.providerMetadata),
|
|
450
|
-
toolMetadataKeyCount: countRecordKeys(toolCall.toolMetadata),
|
|
451
|
-
...this.previewAttributes({ input: toolCall.input })
|
|
452
|
-
},
|
|
453
|
-
inputSummary: summarizeUnknown(toolCall.input),
|
|
454
|
-
error: toolCall.invalid ? summarizeError(toolCall.error) : void 0
|
|
455
|
-
});
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
async onToolCallFinish(event) {
|
|
459
|
-
await this.handleLifecycle("onToolCallFinish", async () => {
|
|
460
|
-
const run = this.getActiveRun("onToolCallFinish");
|
|
461
|
-
if (!run) return;
|
|
462
|
-
const endedAt = nowIso();
|
|
463
|
-
const toolCall = event.toolCall;
|
|
464
|
-
const activeTool = run.tools.get(toolCall.toolCallId);
|
|
465
|
-
if (!activeTool) {
|
|
466
|
-
this.recordLifecycleWarning(
|
|
467
|
-
`onToolCallFinish ignored because tool call ${toolCall.toolCallId} has no matching start callback.`
|
|
468
|
-
);
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
await this.write({
|
|
472
|
-
schemaVersion: "0.2",
|
|
473
|
-
eventId: activeTool.eventId,
|
|
474
|
-
runId: run.runId,
|
|
475
|
-
parentId: activeTool.parentId,
|
|
476
|
-
kind: "TOOL",
|
|
477
|
-
name: activeTool.toolName,
|
|
478
|
-
status: event.success ? "ok" : "error",
|
|
479
|
-
timestamp: endedAt,
|
|
480
|
-
startedAt: activeTool.startedAt,
|
|
481
|
-
endedAt,
|
|
482
|
-
durationMs: event.durationMs,
|
|
483
|
-
confidence: "explicit",
|
|
484
|
-
source: AI_SDK_SOURCE,
|
|
485
|
-
attributes: {
|
|
486
|
-
legacyEvent: "step_completed",
|
|
487
|
-
stepId: activeTool.eventId,
|
|
488
|
-
...summarizeModel(event.model),
|
|
489
|
-
functionId: event.functionId,
|
|
490
|
-
stepNumber: event.stepNumber,
|
|
491
|
-
toolCallId: toolCall.toolCallId,
|
|
492
|
-
toolName: toolCall.toolName,
|
|
493
|
-
dynamic: toolCall.dynamic === true,
|
|
494
|
-
invalid: toolCall.invalid === true,
|
|
495
|
-
providerExecuted: toolCall.providerExecuted === true,
|
|
496
|
-
messageCount: event.messages.length,
|
|
497
|
-
metadataKeyCount: countRecordKeys(event.metadata),
|
|
498
|
-
providerMetadataKeyCount: countRecordKeys(toolCall.providerMetadata),
|
|
499
|
-
toolMetadataKeyCount: countRecordKeys(toolCall.toolMetadata),
|
|
500
|
-
...this.previewAttributes({
|
|
501
|
-
input: toolCall.input,
|
|
502
|
-
...event.success ? { output: event.output } : {}
|
|
503
|
-
})
|
|
504
|
-
},
|
|
505
|
-
outputSummary: event.success ? summarizeUnknown(event.output) : void 0,
|
|
506
|
-
error: event.success ? void 0 : summarizeError(event.error)
|
|
507
|
-
});
|
|
508
|
-
run.tools.delete(toolCall.toolCallId);
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
async onFinish(event) {
|
|
512
|
-
await this.handleLifecycle("onFinish", async () => {
|
|
513
|
-
if (this.suspendedReason) {
|
|
514
|
-
this.suspendedReason = void 0;
|
|
515
|
-
return;
|
|
516
|
-
}
|
|
517
|
-
const run = this.getActiveRun("onFinish");
|
|
518
|
-
if (!run) return;
|
|
519
|
-
const endedAt = nowIso();
|
|
520
|
-
await this.write({
|
|
521
|
-
schemaVersion: "0.2",
|
|
522
|
-
eventId: run.eventId,
|
|
523
|
-
runId: run.runId,
|
|
524
|
-
kind: "RUN",
|
|
525
|
-
name: run.name,
|
|
526
|
-
status: "ok",
|
|
527
|
-
timestamp: endedAt,
|
|
528
|
-
startedAt: run.startedAt,
|
|
529
|
-
endedAt,
|
|
530
|
-
durationMs: durationMs(run.startedAt, endedAt),
|
|
531
|
-
confidence: "explicit",
|
|
532
|
-
source: AI_SDK_SOURCE,
|
|
533
|
-
attributes: {
|
|
534
|
-
legacyEvent: "run_completed",
|
|
535
|
-
...summarizeModel(event.model ?? run.model),
|
|
536
|
-
functionId: event.functionId,
|
|
537
|
-
...summarizeFinishReason(event.finishReason),
|
|
538
|
-
stepCount: event.steps.length,
|
|
539
|
-
warningCount: event.warnings?.length ?? 0,
|
|
540
|
-
toolCallCount: event.toolCalls.length,
|
|
541
|
-
toolResultCount: event.toolResults.length,
|
|
542
|
-
metadataKeyCount: countRecordKeys(event.metadata),
|
|
543
|
-
...this.previewAttributes({ output: event.text })
|
|
544
|
-
},
|
|
545
|
-
tokenUsage: summarizeAiSdkUsage(event.totalUsage),
|
|
546
|
-
outputSummary: {
|
|
547
|
-
contentPartCount: event.content.length,
|
|
548
|
-
textLength: event.text.length,
|
|
549
|
-
reasoningPartCount: event.reasoning.length,
|
|
550
|
-
fileCount: event.files.length,
|
|
551
|
-
sourceCount: event.sources.length
|
|
552
|
-
}
|
|
553
|
-
});
|
|
554
|
-
this.activeRun = void 0;
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
getWriterStats() {
|
|
558
|
-
return this.writer?.getStats?.();
|
|
559
|
-
}
|
|
560
|
-
async flush() {
|
|
561
|
-
try {
|
|
562
|
-
await this.writer?.flush?.();
|
|
563
|
-
} catch (error) {
|
|
564
|
-
this.diagnostics.flushFailures += 1;
|
|
565
|
-
this.diagnostics.lastError = normalizeError(error);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
async close() {
|
|
569
|
-
try {
|
|
570
|
-
if (this.activeRun) {
|
|
571
|
-
await this.terminalizeActiveRun("abandoned-close");
|
|
572
|
-
}
|
|
573
|
-
await this.writer?.close?.();
|
|
574
|
-
} catch (error) {
|
|
575
|
-
this.diagnostics.closeFailures += 1;
|
|
576
|
-
this.diagnostics.lastError = normalizeError(error);
|
|
577
|
-
} finally {
|
|
578
|
-
this.activeRun = void 0;
|
|
579
|
-
this.suspendedReason = void 0;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
getActiveRun(callbackName) {
|
|
583
|
-
if (this.suspendedReason) {
|
|
584
|
-
this.recordLifecycleWarning(
|
|
585
|
-
`${callbackName} ignored while integration is suspended: ${this.suspendedReason}`
|
|
586
|
-
);
|
|
587
|
-
return void 0;
|
|
588
|
-
}
|
|
589
|
-
if (this.activeRun) return this.activeRun;
|
|
590
|
-
this.recordLifecycleWarning(
|
|
591
|
-
`${callbackName} ignored because no active AI SDK generation was started.`
|
|
592
|
-
);
|
|
593
|
-
return void 0;
|
|
594
|
-
}
|
|
595
|
-
async handleLifecycle(callbackName, run) {
|
|
596
|
-
try {
|
|
597
|
-
await run();
|
|
598
|
-
} catch (error) {
|
|
599
|
-
this.recordLifecycleWarning(`${callbackName}: ${normalizeError(error)}`);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
recordLifecycleWarning(message) {
|
|
603
|
-
this.diagnostics.lifecycleWarnings += 1;
|
|
604
|
-
this.diagnostics.lastWarning = message;
|
|
605
|
-
}
|
|
606
|
-
recordCaptureOptionWarnings() {
|
|
607
|
-
if (this.effectiveCapture === "preview") return;
|
|
608
|
-
const previewOnlyOptions = [];
|
|
609
|
-
if (this.options.redactionProfile !== void 0) previewOnlyOptions.push("redactionProfile");
|
|
610
|
-
if (this.options.maxPreviewChars !== void 0) previewOnlyOptions.push("maxPreviewChars");
|
|
611
|
-
if (previewOnlyOptions.length > 0) {
|
|
612
|
-
this.recordLifecycleWarning(
|
|
613
|
-
`AI SDK preview-only options have no effect in metadata-only capture: ${previewOnlyOptions.join(", ")}.`
|
|
614
|
-
);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
async write(event) {
|
|
618
|
-
if (!this.writer) return;
|
|
619
|
-
try {
|
|
620
|
-
await this.writer.write(event);
|
|
621
|
-
} catch (error) {
|
|
622
|
-
this.diagnostics.writeFailures += 1;
|
|
623
|
-
this.diagnostics.lastError = normalizeError(error);
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
};
|
|
627
|
-
function agentInspect(options = {}) {
|
|
628
|
-
const integration = new AgentInspectAiSdkTelemetryIntegration(options);
|
|
629
|
-
return {
|
|
630
|
-
...bindTelemetryIntegration(integration),
|
|
631
|
-
getDiagnostics: () => integration.getDiagnostics(),
|
|
632
|
-
getWriterStats: () => integration.getWriterStats(),
|
|
633
|
-
flush: () => integration.flush(),
|
|
634
|
-
close: () => integration.close()
|
|
635
|
-
};
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
export { agentInspect };
|
|
639
|
-
//# sourceMappingURL=index.mjs.map
|
|
640
|
-
//# sourceMappingURL=index.mjs.map
|