@ai-sdk/otel 1.0.0-beta.12 → 1.0.0-beta.123
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 +891 -0
- package/README.md +119 -0
- package/dist/index.d.ts +139 -22
- package/dist/index.js +1729 -165
- package/dist/index.js.map +1 -1
- package/package.json +16 -13
- package/src/assemble-operation-name.ts +2 -2
- package/src/gen-ai-format-messages.ts +644 -0
- package/src/get-base-telemetry-attributes.ts +8 -15
- package/src/get-tracer.ts +1 -1
- package/src/index.ts +7 -1
- package/src/{open-telemetry-integration.ts → legacy-open-telemetry.ts} +200 -144
- package/src/mock-tracer.ts +1 -1
- package/src/noop-tracer.ts +1 -1
- package/src/open-telemetry.ts +1310 -0
- package/src/record-span.ts +3 -4
- package/src/select-attributes.ts +57 -0
- package/src/select-telemetry-attributes.ts +3 -3
- package/src/stringify-for-telemetry.ts +23 -5
- package/src/supplemental-attributes.ts +201 -0
- package/dist/index.d.mts +0 -52
- package/dist/index.mjs +0 -845
- package/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,31 +1,1559 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// src/open-telemetry.ts
|
|
2
|
+
import {
|
|
3
|
+
context as context2,
|
|
4
|
+
SpanKind,
|
|
5
|
+
trace
|
|
6
|
+
} from "@opentelemetry/api";
|
|
7
|
+
|
|
8
|
+
// src/gen-ai-format-messages.ts
|
|
9
|
+
import { convertDataContentToBase64String } from "ai";
|
|
10
|
+
function mapProviderName(provider) {
|
|
11
|
+
const lower = provider.toLowerCase();
|
|
12
|
+
const wellKnownPrefixes = [
|
|
13
|
+
["google.vertex", "gcp.vertex_ai"],
|
|
14
|
+
["google.generative-ai", "gcp.gemini"],
|
|
15
|
+
["google-vertex", "gcp.vertex_ai"],
|
|
16
|
+
["amazon-bedrock", "aws.bedrock"],
|
|
17
|
+
["azure-openai", "azure.ai.openai"],
|
|
18
|
+
["anthropic", "anthropic"],
|
|
19
|
+
["openai", "openai"],
|
|
20
|
+
["azure", "azure.ai.inference"],
|
|
21
|
+
["google", "gcp.gemini"],
|
|
22
|
+
["mistral", "mistral_ai"],
|
|
23
|
+
["cohere", "cohere"],
|
|
24
|
+
["bedrock", "aws.bedrock"],
|
|
25
|
+
["groq", "groq"],
|
|
26
|
+
["deepseek", "deepseek"],
|
|
27
|
+
["perplexity", "perplexity"],
|
|
28
|
+
["xai", "x_ai"]
|
|
29
|
+
];
|
|
30
|
+
for (const [prefix, mapped] of wellKnownPrefixes) {
|
|
31
|
+
if (lower === prefix || lower.startsWith(prefix + ".") || lower.startsWith(prefix + "-")) {
|
|
32
|
+
return mapped;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return provider;
|
|
36
|
+
}
|
|
37
|
+
function mapOperationName(operationId) {
|
|
38
|
+
var _a;
|
|
39
|
+
const mapping = {
|
|
40
|
+
"ai.generateText": "invoke_agent",
|
|
41
|
+
"ai.streamText": "invoke_agent",
|
|
42
|
+
"ai.generateObject": "invoke_agent",
|
|
43
|
+
"ai.streamObject": "invoke_agent",
|
|
44
|
+
"ai.embed": "embeddings",
|
|
45
|
+
"ai.embedMany": "embeddings",
|
|
46
|
+
"ai.rerank": "rerank"
|
|
47
|
+
};
|
|
48
|
+
return (_a = mapping[operationId]) != null ? _a : operationId;
|
|
49
|
+
}
|
|
50
|
+
function formatSystemInstructions(system) {
|
|
51
|
+
if (typeof system === "string") {
|
|
52
|
+
return [{ type: "text", content: system }];
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(system)) {
|
|
55
|
+
return system.map((msg) => ({ type: "text", content: msg.content }));
|
|
56
|
+
}
|
|
57
|
+
return [{ type: "text", content: system.content }];
|
|
58
|
+
}
|
|
59
|
+
function convertMessagePartToSemConv(part) {
|
|
60
|
+
var _a, _b, _c, _d, _e;
|
|
61
|
+
switch (part.type) {
|
|
62
|
+
case "text":
|
|
63
|
+
return { type: "text", content: part.text };
|
|
64
|
+
case "reasoning":
|
|
65
|
+
return { type: "reasoning", content: part.text };
|
|
66
|
+
case "tool-call":
|
|
67
|
+
return {
|
|
68
|
+
type: "tool_call",
|
|
69
|
+
id: (_a = part.toolCallId) != null ? _a : null,
|
|
70
|
+
name: part.toolName,
|
|
71
|
+
arguments: part.input
|
|
72
|
+
};
|
|
73
|
+
case "tool-result": {
|
|
74
|
+
const output = part.output;
|
|
75
|
+
let response;
|
|
76
|
+
if (output) {
|
|
77
|
+
if (output.type === "text" || output.type === "error-text") {
|
|
78
|
+
response = output.value;
|
|
79
|
+
} else if (output.type === "json" || output.type === "error-json") {
|
|
80
|
+
response = output.value;
|
|
81
|
+
} else if (output.type === "execution-denied") {
|
|
82
|
+
response = { denied: true, reason: output.reason };
|
|
83
|
+
} else {
|
|
84
|
+
response = output;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
type: "tool_call_response",
|
|
89
|
+
id: (_b = part.toolCallId) != null ? _b : null,
|
|
90
|
+
response
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
case "file": {
|
|
94
|
+
const rawData = part.data;
|
|
95
|
+
const data = (() => {
|
|
96
|
+
if (typeof rawData === "object" && rawData !== null && !(rawData instanceof URL) && !(rawData instanceof Uint8Array) && !(rawData instanceof ArrayBuffer) && "type" in rawData) {
|
|
97
|
+
switch (rawData.type) {
|
|
98
|
+
case "data":
|
|
99
|
+
return rawData.data;
|
|
100
|
+
case "url":
|
|
101
|
+
return rawData.url;
|
|
102
|
+
case "text":
|
|
103
|
+
return rawData.text;
|
|
104
|
+
default:
|
|
105
|
+
return "";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return rawData;
|
|
109
|
+
})();
|
|
110
|
+
let content;
|
|
111
|
+
if (data instanceof Uint8Array) {
|
|
112
|
+
content = convertDataContentToBase64String(data);
|
|
113
|
+
} else if (typeof data === "string") {
|
|
114
|
+
if (data.startsWith("http://") || data.startsWith("https://")) {
|
|
115
|
+
return {
|
|
116
|
+
type: "uri",
|
|
117
|
+
modality: getModality(part.mediaType),
|
|
118
|
+
mime_type: (_c = part.mediaType) != null ? _c : null,
|
|
119
|
+
uri: data
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
content = data;
|
|
123
|
+
} else if (data instanceof URL) {
|
|
124
|
+
return {
|
|
125
|
+
type: "uri",
|
|
126
|
+
modality: getModality(part.mediaType),
|
|
127
|
+
mime_type: (_d = part.mediaType) != null ? _d : null,
|
|
128
|
+
uri: data.toString()
|
|
129
|
+
};
|
|
130
|
+
} else {
|
|
131
|
+
content = String(data);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
type: "blob",
|
|
135
|
+
modality: getModality(part.mediaType),
|
|
136
|
+
mime_type: (_e = part.mediaType) != null ? _e : null,
|
|
137
|
+
content
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
case "tool-approval-response":
|
|
141
|
+
return {
|
|
142
|
+
type: "tool_approval_response",
|
|
143
|
+
approval_id: part.approvalId,
|
|
144
|
+
approved: part.approved,
|
|
145
|
+
reason: part.reason
|
|
146
|
+
};
|
|
147
|
+
case "custom":
|
|
148
|
+
return { type: "custom", kind: part.kind };
|
|
149
|
+
case "reasoning-file":
|
|
150
|
+
return { type: String(part.type) };
|
|
151
|
+
default: {
|
|
152
|
+
const _exhaustive = part;
|
|
153
|
+
return { type: String(_exhaustive.type) };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function getModality(mediaType) {
|
|
158
|
+
if (!mediaType) return "image";
|
|
159
|
+
if (mediaType.startsWith("image/")) return "image";
|
|
160
|
+
if (mediaType.startsWith("video/")) return "video";
|
|
161
|
+
if (mediaType.startsWith("audio/")) return "audio";
|
|
162
|
+
return "image";
|
|
163
|
+
}
|
|
164
|
+
function formatInputMessages(prompt) {
|
|
165
|
+
return prompt.filter((msg) => msg.role !== "system").map((message) => {
|
|
166
|
+
if (message.role === "system") {
|
|
167
|
+
return {
|
|
168
|
+
role: "system",
|
|
169
|
+
parts: [{ type: "text", content: message.content }]
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
const parts = message.content.map(convertMessagePartToSemConv);
|
|
173
|
+
return { role: message.role, parts };
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
function formatModelMessages({
|
|
177
|
+
prompt,
|
|
178
|
+
messages
|
|
179
|
+
}) {
|
|
180
|
+
const result = [];
|
|
181
|
+
if (typeof prompt === "string") {
|
|
182
|
+
result.push({
|
|
183
|
+
role: "user",
|
|
184
|
+
parts: [{ type: "text", content: prompt }]
|
|
185
|
+
});
|
|
186
|
+
} else if (Array.isArray(prompt)) {
|
|
187
|
+
for (const msg of prompt) {
|
|
188
|
+
const converted = convertModelMessageToSemConv(msg);
|
|
189
|
+
if (converted) result.push(converted);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (messages) {
|
|
193
|
+
for (const msg of messages) {
|
|
194
|
+
const converted = convertModelMessageToSemConv(msg);
|
|
195
|
+
if (converted) result.push(converted);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
200
|
+
function convertModelMessageToSemConv(msg) {
|
|
201
|
+
if (msg.role === "system") return void 0;
|
|
202
|
+
if (msg.role === "user") {
|
|
203
|
+
if (typeof msg.content === "string") {
|
|
204
|
+
return {
|
|
205
|
+
role: "user",
|
|
206
|
+
parts: [{ type: "text", content: msg.content }]
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
const parts = msg.content.map((part) => {
|
|
210
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
211
|
+
switch (part.type) {
|
|
212
|
+
case "text":
|
|
213
|
+
return { type: "text", content: part.text };
|
|
214
|
+
case "image": {
|
|
215
|
+
const data = part.image;
|
|
216
|
+
if (data instanceof URL) {
|
|
217
|
+
return {
|
|
218
|
+
type: "uri",
|
|
219
|
+
modality: "image",
|
|
220
|
+
mime_type: (_a = part.mediaType) != null ? _a : null,
|
|
221
|
+
uri: data.toString()
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if (typeof data === "string") {
|
|
225
|
+
if (data.startsWith("http://") || data.startsWith("https://")) {
|
|
226
|
+
return {
|
|
227
|
+
type: "uri",
|
|
228
|
+
modality: "image",
|
|
229
|
+
mime_type: (_b = part.mediaType) != null ? _b : null,
|
|
230
|
+
uri: data
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
type: "blob",
|
|
235
|
+
modality: "image",
|
|
236
|
+
mime_type: (_c = part.mediaType) != null ? _c : null,
|
|
237
|
+
content: data
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
type: "blob",
|
|
242
|
+
modality: "image",
|
|
243
|
+
mime_type: (_d = part.mediaType) != null ? _d : null,
|
|
244
|
+
content: convertDataContentToBase64String(data)
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
case "file": {
|
|
248
|
+
const rawData = part.data;
|
|
249
|
+
const data = (() => {
|
|
250
|
+
if (typeof rawData === "object" && rawData !== null && !(rawData instanceof URL) && !(rawData instanceof Uint8Array) && !(rawData instanceof ArrayBuffer) && "type" in rawData) {
|
|
251
|
+
switch (rawData.type) {
|
|
252
|
+
case "data":
|
|
253
|
+
return rawData.data;
|
|
254
|
+
case "url":
|
|
255
|
+
return rawData.url;
|
|
256
|
+
case "text":
|
|
257
|
+
return rawData.text;
|
|
258
|
+
default:
|
|
259
|
+
return "";
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return rawData;
|
|
263
|
+
})();
|
|
264
|
+
if (data instanceof URL) {
|
|
265
|
+
return {
|
|
266
|
+
type: "uri",
|
|
267
|
+
modality: getModality(part.mediaType),
|
|
268
|
+
mime_type: (_e = part.mediaType) != null ? _e : null,
|
|
269
|
+
uri: data.toString()
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
if (typeof data === "string") {
|
|
273
|
+
if (data.startsWith("http://") || data.startsWith("https://")) {
|
|
274
|
+
return {
|
|
275
|
+
type: "uri",
|
|
276
|
+
modality: getModality(part.mediaType),
|
|
277
|
+
mime_type: (_f = part.mediaType) != null ? _f : null,
|
|
278
|
+
uri: data
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return {
|
|
282
|
+
type: "blob",
|
|
283
|
+
modality: getModality(part.mediaType),
|
|
284
|
+
mime_type: (_g = part.mediaType) != null ? _g : null,
|
|
285
|
+
content: data
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
type: "blob",
|
|
290
|
+
modality: getModality(part.mediaType),
|
|
291
|
+
mime_type: (_h = part.mediaType) != null ? _h : null,
|
|
292
|
+
content: convertDataContentToBase64String(data)
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
default:
|
|
296
|
+
return { type: String(part.type) };
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
return { role: "user", parts };
|
|
300
|
+
}
|
|
301
|
+
if (msg.role === "assistant") {
|
|
302
|
+
if (typeof msg.content === "string") {
|
|
303
|
+
return {
|
|
304
|
+
role: "assistant",
|
|
305
|
+
parts: [{ type: "text", content: msg.content }]
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
const parts = msg.content.map((part) => {
|
|
309
|
+
var _a, _b;
|
|
310
|
+
switch (part.type) {
|
|
311
|
+
case "text":
|
|
312
|
+
return { type: "text", content: part.text };
|
|
313
|
+
case "reasoning":
|
|
314
|
+
return { type: "reasoning", content: part.text };
|
|
315
|
+
case "tool-call":
|
|
316
|
+
return {
|
|
317
|
+
type: "tool_call",
|
|
318
|
+
id: (_a = part.toolCallId) != null ? _a : null,
|
|
319
|
+
name: part.toolName,
|
|
320
|
+
arguments: part.input
|
|
321
|
+
};
|
|
322
|
+
case "tool-result": {
|
|
323
|
+
const output = part.output;
|
|
324
|
+
let response;
|
|
325
|
+
if (output) {
|
|
326
|
+
if (output.type === "text" || output.type === "error-text") {
|
|
327
|
+
response = output.value;
|
|
328
|
+
} else if (output.type === "json" || output.type === "error-json") {
|
|
329
|
+
response = output.value;
|
|
330
|
+
} else if (output.type === "execution-denied") {
|
|
331
|
+
response = { denied: true, reason: output.reason };
|
|
332
|
+
} else {
|
|
333
|
+
response = output;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
type: "tool_call_response",
|
|
338
|
+
id: (_b = part.toolCallId) != null ? _b : null,
|
|
339
|
+
response
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
default:
|
|
343
|
+
return { type: String(part.type) };
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
return { role: "assistant", parts };
|
|
347
|
+
}
|
|
348
|
+
if (msg.role === "tool") {
|
|
349
|
+
const parts = msg.content.map((part) => {
|
|
350
|
+
var _a;
|
|
351
|
+
if (part.type === "tool-result") {
|
|
352
|
+
const output = part.output;
|
|
353
|
+
let response;
|
|
354
|
+
if (output) {
|
|
355
|
+
if (output.type === "text" || output.type === "error-text") {
|
|
356
|
+
response = output.value;
|
|
357
|
+
} else if (output.type === "json" || output.type === "error-json") {
|
|
358
|
+
response = output.value;
|
|
359
|
+
} else if (output.type === "execution-denied") {
|
|
360
|
+
response = { denied: true, reason: output.reason };
|
|
361
|
+
} else {
|
|
362
|
+
response = output;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
type: "tool_call_response",
|
|
367
|
+
id: (_a = part.toolCallId) != null ? _a : null,
|
|
368
|
+
response
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
return { type: String(part.type) };
|
|
372
|
+
});
|
|
373
|
+
return { role: "tool", parts };
|
|
374
|
+
}
|
|
375
|
+
return void 0;
|
|
376
|
+
}
|
|
377
|
+
function formatOutputMessages({
|
|
378
|
+
text,
|
|
379
|
+
reasoning,
|
|
380
|
+
toolCalls,
|
|
381
|
+
files,
|
|
382
|
+
finishReason
|
|
383
|
+
}) {
|
|
384
|
+
const parts = [];
|
|
385
|
+
if (reasoning) {
|
|
386
|
+
for (const r of reasoning) {
|
|
387
|
+
if ("text" in r && r.text) {
|
|
388
|
+
parts.push({ type: "reasoning", content: r.text });
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (text != null && text.length > 0) {
|
|
393
|
+
parts.push({ type: "text", content: text });
|
|
394
|
+
}
|
|
395
|
+
if (toolCalls) {
|
|
396
|
+
for (const tc of toolCalls) {
|
|
397
|
+
parts.push({
|
|
398
|
+
type: "tool_call",
|
|
399
|
+
id: tc.toolCallId,
|
|
400
|
+
name: tc.toolName,
|
|
401
|
+
arguments: tc.input
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (files) {
|
|
406
|
+
for (const file of files) {
|
|
407
|
+
parts.push({
|
|
408
|
+
type: "blob",
|
|
409
|
+
modality: getModality(file.mediaType),
|
|
410
|
+
mime_type: file.mediaType,
|
|
411
|
+
content: file.base64
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return [
|
|
416
|
+
{
|
|
417
|
+
role: "assistant",
|
|
418
|
+
parts,
|
|
419
|
+
finish_reason: mapFinishReason(finishReason)
|
|
420
|
+
}
|
|
421
|
+
];
|
|
422
|
+
}
|
|
423
|
+
function formatObjectOutputMessages({
|
|
424
|
+
objectText,
|
|
425
|
+
finishReason
|
|
426
|
+
}) {
|
|
427
|
+
return [
|
|
428
|
+
{
|
|
429
|
+
role: "assistant",
|
|
430
|
+
parts: [{ type: "text", content: objectText }],
|
|
431
|
+
finish_reason: mapFinishReason(finishReason)
|
|
432
|
+
}
|
|
433
|
+
];
|
|
434
|
+
}
|
|
435
|
+
function mapFinishReason(reason) {
|
|
436
|
+
var _a;
|
|
437
|
+
const mapping = {
|
|
438
|
+
stop: "stop",
|
|
439
|
+
length: "length",
|
|
440
|
+
"content-filter": "content_filter",
|
|
441
|
+
"tool-calls": "tool_call",
|
|
442
|
+
error: "error",
|
|
443
|
+
other: "stop",
|
|
444
|
+
unknown: "stop"
|
|
445
|
+
};
|
|
446
|
+
return (_a = mapping[reason]) != null ? _a : reason;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/record-span.ts
|
|
450
|
+
import {
|
|
451
|
+
SpanStatusCode,
|
|
452
|
+
context
|
|
453
|
+
} from "@opentelemetry/api";
|
|
454
|
+
function recordErrorOnSpan(span, error) {
|
|
455
|
+
if (error instanceof Error) {
|
|
456
|
+
span.recordException({
|
|
457
|
+
name: error.name,
|
|
458
|
+
message: error.message,
|
|
459
|
+
stack: error.stack
|
|
460
|
+
});
|
|
461
|
+
span.setStatus({
|
|
462
|
+
code: SpanStatusCode.ERROR,
|
|
463
|
+
message: error.message
|
|
464
|
+
});
|
|
465
|
+
} else {
|
|
466
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// src/select-attributes.ts
|
|
471
|
+
function shouldRecord(telemetry) {
|
|
472
|
+
return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
|
|
473
|
+
}
|
|
474
|
+
function selectAttributes(telemetry, attributes) {
|
|
475
|
+
if (!shouldRecord(telemetry)) {
|
|
476
|
+
return {};
|
|
477
|
+
}
|
|
478
|
+
const result = {};
|
|
479
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
480
|
+
if (value == null) continue;
|
|
481
|
+
if (typeof value === "object" && "input" in value && typeof value.input === "function") {
|
|
482
|
+
if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
|
|
483
|
+
const resolved = value.input();
|
|
484
|
+
if (resolved != null) result[key] = resolved;
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (typeof value === "object" && "output" in value && typeof value.output === "function") {
|
|
488
|
+
if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
|
|
489
|
+
const resolved = value.output();
|
|
490
|
+
if (resolved != null) result[key] = resolved;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
result[key] = value;
|
|
494
|
+
}
|
|
495
|
+
return result;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// src/supplemental-attributes.ts
|
|
499
|
+
var disabledSupplementalAttributes = {
|
|
500
|
+
usage: false,
|
|
501
|
+
providerMetadata: false,
|
|
502
|
+
embedding: false,
|
|
503
|
+
reranking: false,
|
|
504
|
+
runtimeContext: false,
|
|
505
|
+
headers: false,
|
|
506
|
+
toolChoice: false,
|
|
507
|
+
schema: false
|
|
9
508
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
509
|
+
function normalizeSupplementalAttributes(options) {
|
|
510
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
511
|
+
return {
|
|
512
|
+
...disabledSupplementalAttributes,
|
|
513
|
+
usage: (_a = options.usage) != null ? _a : false,
|
|
514
|
+
providerMetadata: (_b = options.providerMetadata) != null ? _b : false,
|
|
515
|
+
embedding: (_c = options.embedding) != null ? _c : false,
|
|
516
|
+
reranking: (_d = options.reranking) != null ? _d : false,
|
|
517
|
+
runtimeContext: (_e = options.runtimeContext) != null ? _e : false,
|
|
518
|
+
headers: (_f = options.headers) != null ? _f : false,
|
|
519
|
+
toolChoice: (_g = options.toolChoice) != null ? _g : false,
|
|
520
|
+
schema: (_h = options.schema) != null ? _h : false
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
function getRuntimeContextAttributes(context4) {
|
|
524
|
+
const attributes = {};
|
|
525
|
+
for (const [key, value] of Object.entries(context4 != null ? context4 : {})) {
|
|
526
|
+
addRuntimeContextAttribute(attributes, `ai.settings.context.${key}`, value);
|
|
527
|
+
}
|
|
528
|
+
return attributes;
|
|
529
|
+
}
|
|
530
|
+
function addRuntimeContextAttribute(attributes, key, value) {
|
|
531
|
+
if (value == null) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (Array.isArray(value) || typeof value !== "object") {
|
|
535
|
+
attributes[key] = value;
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
for (const [nestedKey, nestedValue] of Object.entries(value)) {
|
|
539
|
+
addRuntimeContextAttribute(attributes, `${key}.${nestedKey}`, nestedValue);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function getHeaderAttributes(headers) {
|
|
543
|
+
return Object.fromEntries(
|
|
544
|
+
Object.entries(headers != null ? headers : {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.request.headers.${key}`, value])
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
function getDetailedUsageAttributes(usage) {
|
|
548
|
+
var _a, _b, _c;
|
|
549
|
+
return {
|
|
550
|
+
"ai.usage.inputTokenDetails.noCacheTokens": (_a = usage.inputTokenDetails) == null ? void 0 : _a.noCacheTokens,
|
|
551
|
+
"ai.usage.outputTokenDetails.textTokens": (_b = usage.outputTokenDetails) == null ? void 0 : _b.textTokens,
|
|
552
|
+
"ai.usage.outputTokenDetails.reasoningTokens": (_c = usage.outputTokenDetails) == null ? void 0 : _c.reasoningTokens
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
function selectSupplementalAttributes(telemetry, enabledAttributes, attributes) {
|
|
556
|
+
const result = {};
|
|
557
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
558
|
+
if (!enabledAttributes[key] || value == null) {
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
Object.assign(result, selectAttributes(telemetry, value));
|
|
562
|
+
}
|
|
563
|
+
return result;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// src/open-telemetry.ts
|
|
567
|
+
var OpenTelemetry = class {
|
|
568
|
+
constructor(options = {}) {
|
|
569
|
+
this.callStates = /* @__PURE__ */ new Map();
|
|
570
|
+
var _a;
|
|
571
|
+
this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("gen_ai");
|
|
572
|
+
this.supplementalAttributes = normalizeSupplementalAttributes(options);
|
|
573
|
+
this.enrichSpan = options.enrichSpan;
|
|
574
|
+
}
|
|
575
|
+
getCallState(callId) {
|
|
576
|
+
return this.callStates.get(callId);
|
|
577
|
+
}
|
|
578
|
+
cleanupCallState(callId) {
|
|
579
|
+
this.callStates.delete(callId);
|
|
580
|
+
}
|
|
581
|
+
getSpanAttributes({
|
|
582
|
+
attributes,
|
|
583
|
+
spanType,
|
|
584
|
+
operationId,
|
|
585
|
+
callId,
|
|
586
|
+
runtimeContext
|
|
587
|
+
}) {
|
|
588
|
+
var _a;
|
|
589
|
+
let customAttributes;
|
|
590
|
+
try {
|
|
591
|
+
customAttributes = (_a = this.enrichSpan) == null ? void 0 : _a.call(this, {
|
|
592
|
+
spanType,
|
|
593
|
+
operationId,
|
|
594
|
+
callId,
|
|
595
|
+
runtimeContext
|
|
596
|
+
});
|
|
597
|
+
} catch (e) {
|
|
598
|
+
customAttributes = void 0;
|
|
599
|
+
}
|
|
600
|
+
return {
|
|
601
|
+
...customAttributes,
|
|
602
|
+
...attributes
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
executeTool({
|
|
606
|
+
callId,
|
|
607
|
+
toolCallId,
|
|
608
|
+
execute
|
|
609
|
+
}) {
|
|
610
|
+
var _a;
|
|
611
|
+
const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
|
|
612
|
+
if (toolSpanEntry == null) {
|
|
613
|
+
return execute();
|
|
614
|
+
}
|
|
615
|
+
return context2.with(toolSpanEntry.context, execute);
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Runs the provider `doGenerate`/`doStream` call with the active model-call
|
|
619
|
+
* context.
|
|
620
|
+
*/
|
|
621
|
+
executeLanguageModelCall({
|
|
622
|
+
callId,
|
|
623
|
+
execute
|
|
624
|
+
}) {
|
|
625
|
+
var _a;
|
|
626
|
+
const state = this.getCallState(callId);
|
|
627
|
+
const modelCallContext = (_a = state == null ? void 0 : state.inferenceContext) != null ? _a : state == null ? void 0 : state.stepContext;
|
|
628
|
+
if (modelCallContext == null) {
|
|
629
|
+
return execute();
|
|
630
|
+
}
|
|
631
|
+
return context2.with(modelCallContext, execute);
|
|
632
|
+
}
|
|
633
|
+
onStart(event) {
|
|
634
|
+
if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
|
|
635
|
+
this.onEmbedOperationStart(event);
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
if (event.operationId === "ai.rerank") {
|
|
639
|
+
this.onRerankOperationStart(
|
|
640
|
+
event
|
|
641
|
+
);
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
|
|
645
|
+
this.onObjectOperationStart(
|
|
646
|
+
event
|
|
647
|
+
);
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
this.onGenerateStart(event);
|
|
651
|
+
}
|
|
652
|
+
onGenerateStart(event) {
|
|
653
|
+
var _a;
|
|
654
|
+
const telemetry = {
|
|
655
|
+
recordInputs: event.recordInputs,
|
|
656
|
+
recordOutputs: event.recordOutputs,
|
|
657
|
+
functionId: event.functionId
|
|
658
|
+
};
|
|
659
|
+
const settings = {
|
|
660
|
+
maxOutputTokens: event.maxOutputTokens,
|
|
661
|
+
temperature: event.temperature,
|
|
662
|
+
topP: event.topP,
|
|
663
|
+
topK: event.topK,
|
|
664
|
+
presencePenalty: event.presencePenalty,
|
|
665
|
+
frequencyPenalty: event.frequencyPenalty,
|
|
666
|
+
stopSequences: event.stopSequences,
|
|
667
|
+
seed: event.seed,
|
|
668
|
+
maxRetries: event.maxRetries
|
|
669
|
+
};
|
|
670
|
+
const providerName = mapProviderName(event.provider);
|
|
671
|
+
const operationName = mapOperationName(event.operationId);
|
|
672
|
+
const runtimeContext = event.runtimeContext;
|
|
673
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
674
|
+
telemetry,
|
|
675
|
+
this.supplementalAttributes,
|
|
676
|
+
{
|
|
677
|
+
runtimeContext: getRuntimeContextAttributes(runtimeContext),
|
|
678
|
+
headers: getHeaderAttributes(event.headers)
|
|
679
|
+
}
|
|
680
|
+
);
|
|
681
|
+
const attributes = selectAttributes(telemetry, {
|
|
682
|
+
"gen_ai.operation.name": operationName,
|
|
683
|
+
"gen_ai.provider.name": providerName,
|
|
684
|
+
"gen_ai.request.model": event.modelId,
|
|
685
|
+
"gen_ai.agent.name": telemetry.functionId,
|
|
686
|
+
"gen_ai.request.frequency_penalty": event.frequencyPenalty,
|
|
687
|
+
"gen_ai.request.max_tokens": event.maxOutputTokens,
|
|
688
|
+
"gen_ai.request.presence_penalty": event.presencePenalty,
|
|
689
|
+
"gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
|
|
690
|
+
"gen_ai.request.top_k": event.topK,
|
|
691
|
+
"gen_ai.request.top_p": event.topP,
|
|
692
|
+
"gen_ai.request.stop_sequences": event.stopSequences,
|
|
693
|
+
"gen_ai.request.seed": event.seed,
|
|
694
|
+
"gen_ai.system_instructions": event.instructions ? {
|
|
695
|
+
input: () => JSON.stringify(formatSystemInstructions(event.instructions))
|
|
696
|
+
} : void 0,
|
|
697
|
+
"gen_ai.input.messages": {
|
|
698
|
+
input: () => JSON.stringify(
|
|
699
|
+
formatModelMessages({
|
|
700
|
+
prompt: void 0,
|
|
701
|
+
messages: event.messages
|
|
702
|
+
})
|
|
703
|
+
)
|
|
704
|
+
},
|
|
705
|
+
...baseSupplementalAttributes
|
|
706
|
+
});
|
|
707
|
+
const spanName = `${operationName} ${event.modelId}`;
|
|
708
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
709
|
+
attributes: this.getSpanAttributes({
|
|
710
|
+
attributes,
|
|
711
|
+
spanType: "operation",
|
|
712
|
+
operationId: event.operationId,
|
|
713
|
+
callId: event.callId,
|
|
714
|
+
runtimeContext
|
|
715
|
+
}),
|
|
716
|
+
kind: SpanKind.INTERNAL
|
|
717
|
+
});
|
|
718
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
719
|
+
this.callStates.set(event.callId, {
|
|
720
|
+
operationId: event.operationId,
|
|
721
|
+
telemetry,
|
|
722
|
+
rootSpan,
|
|
723
|
+
rootContext,
|
|
724
|
+
stepSpan: void 0,
|
|
725
|
+
stepContext: void 0,
|
|
726
|
+
inferenceSpan: void 0,
|
|
727
|
+
inferenceContext: void 0,
|
|
728
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
729
|
+
rerankSpan: void 0,
|
|
730
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
731
|
+
settings,
|
|
732
|
+
provider: event.provider,
|
|
733
|
+
modelId: event.modelId,
|
|
734
|
+
runtimeContext,
|
|
735
|
+
baseSupplementalAttributes
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
onObjectOperationStart(event) {
|
|
739
|
+
var _a;
|
|
740
|
+
const telemetry = {
|
|
741
|
+
recordInputs: event.recordInputs,
|
|
742
|
+
recordOutputs: event.recordOutputs,
|
|
743
|
+
functionId: event.functionId
|
|
744
|
+
};
|
|
745
|
+
const settings = {
|
|
746
|
+
maxOutputTokens: event.maxOutputTokens,
|
|
747
|
+
temperature: event.temperature,
|
|
748
|
+
topP: event.topP,
|
|
749
|
+
topK: event.topK,
|
|
750
|
+
presencePenalty: event.presencePenalty,
|
|
751
|
+
frequencyPenalty: event.frequencyPenalty,
|
|
752
|
+
seed: event.seed,
|
|
753
|
+
maxRetries: event.maxRetries
|
|
754
|
+
};
|
|
755
|
+
const providerName = mapProviderName(event.provider);
|
|
756
|
+
const operationName = mapOperationName(event.operationId);
|
|
757
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
758
|
+
telemetry,
|
|
759
|
+
this.supplementalAttributes,
|
|
760
|
+
{
|
|
761
|
+
headers: getHeaderAttributes(event.headers)
|
|
762
|
+
}
|
|
763
|
+
);
|
|
764
|
+
const attributes = selectAttributes(telemetry, {
|
|
765
|
+
"gen_ai.operation.name": operationName,
|
|
766
|
+
"gen_ai.provider.name": providerName,
|
|
767
|
+
"gen_ai.request.model": event.modelId,
|
|
768
|
+
"gen_ai.agent.name": telemetry.functionId,
|
|
769
|
+
"gen_ai.output.type": "json",
|
|
770
|
+
"gen_ai.request.frequency_penalty": event.frequencyPenalty,
|
|
771
|
+
"gen_ai.request.max_tokens": event.maxOutputTokens,
|
|
772
|
+
"gen_ai.request.presence_penalty": event.presencePenalty,
|
|
773
|
+
"gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
|
|
774
|
+
"gen_ai.request.top_k": event.topK,
|
|
775
|
+
"gen_ai.request.top_p": event.topP,
|
|
776
|
+
"gen_ai.request.seed": event.seed,
|
|
777
|
+
"gen_ai.system_instructions": event.system ? {
|
|
778
|
+
input: () => JSON.stringify(formatSystemInstructions(event.system))
|
|
779
|
+
} : void 0,
|
|
780
|
+
"gen_ai.input.messages": {
|
|
781
|
+
input: () => JSON.stringify(
|
|
782
|
+
formatModelMessages({
|
|
783
|
+
prompt: event.prompt,
|
|
784
|
+
messages: event.messages
|
|
785
|
+
})
|
|
786
|
+
)
|
|
787
|
+
},
|
|
788
|
+
...baseSupplementalAttributes,
|
|
789
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
790
|
+
schema: {
|
|
791
|
+
"ai.schema": event.schema ? { input: () => JSON.stringify(event.schema) } : void 0,
|
|
792
|
+
"ai.schema.name": event.schemaName,
|
|
793
|
+
"ai.schema.description": event.schemaDescription,
|
|
794
|
+
"ai.settings.output": event.output
|
|
795
|
+
}
|
|
796
|
+
})
|
|
797
|
+
});
|
|
798
|
+
const spanName = `${operationName} ${event.modelId}`;
|
|
799
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
800
|
+
attributes: this.getSpanAttributes({
|
|
801
|
+
attributes,
|
|
802
|
+
spanType: "operation",
|
|
803
|
+
operationId: event.operationId,
|
|
804
|
+
callId: event.callId,
|
|
805
|
+
runtimeContext: void 0
|
|
806
|
+
}),
|
|
807
|
+
kind: SpanKind.INTERNAL
|
|
808
|
+
});
|
|
809
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
810
|
+
this.callStates.set(event.callId, {
|
|
811
|
+
operationId: event.operationId,
|
|
812
|
+
telemetry,
|
|
813
|
+
rootSpan,
|
|
814
|
+
rootContext,
|
|
815
|
+
stepSpan: void 0,
|
|
816
|
+
stepContext: void 0,
|
|
817
|
+
inferenceSpan: void 0,
|
|
818
|
+
inferenceContext: void 0,
|
|
819
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
820
|
+
rerankSpan: void 0,
|
|
821
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
822
|
+
settings,
|
|
823
|
+
provider: event.provider,
|
|
824
|
+
modelId: event.modelId,
|
|
825
|
+
runtimeContext: void 0,
|
|
826
|
+
baseSupplementalAttributes
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
/** @deprecated */
|
|
830
|
+
onObjectStepStart(event) {
|
|
831
|
+
var _a;
|
|
832
|
+
const state = this.getCallState(event.callId);
|
|
833
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
834
|
+
const { telemetry } = state;
|
|
835
|
+
const providerName = mapProviderName(event.provider);
|
|
836
|
+
const attributes = selectAttributes(telemetry, {
|
|
837
|
+
"gen_ai.operation.name": "chat",
|
|
838
|
+
"gen_ai.provider.name": providerName,
|
|
839
|
+
"gen_ai.request.model": event.modelId,
|
|
840
|
+
"gen_ai.output.type": "json",
|
|
841
|
+
"gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
|
|
842
|
+
"gen_ai.request.max_tokens": state.settings.maxOutputTokens,
|
|
843
|
+
"gen_ai.request.presence_penalty": state.settings.presencePenalty,
|
|
844
|
+
"gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
|
|
845
|
+
"gen_ai.request.top_k": state.settings.topK,
|
|
846
|
+
"gen_ai.request.top_p": state.settings.topP,
|
|
847
|
+
"gen_ai.input.messages": {
|
|
848
|
+
input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
|
|
849
|
+
},
|
|
850
|
+
...state.baseSupplementalAttributes
|
|
851
|
+
});
|
|
852
|
+
const spanName = `chat ${event.modelId}`;
|
|
853
|
+
state.inferenceSpan = this.tracer.startSpan(
|
|
854
|
+
spanName,
|
|
855
|
+
{
|
|
856
|
+
attributes: this.getSpanAttributes({
|
|
857
|
+
attributes,
|
|
858
|
+
spanType: "languageModel",
|
|
859
|
+
operationId: state.operationId,
|
|
860
|
+
callId: event.callId,
|
|
861
|
+
runtimeContext: state.runtimeContext
|
|
862
|
+
}),
|
|
863
|
+
kind: SpanKind.CLIENT
|
|
864
|
+
},
|
|
865
|
+
state.rootContext
|
|
866
|
+
);
|
|
867
|
+
state.inferenceContext = trace.setSpan(
|
|
868
|
+
state.rootContext,
|
|
869
|
+
state.inferenceSpan
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
/** @deprecated */
|
|
873
|
+
onObjectStepEnd(event) {
|
|
874
|
+
var _a;
|
|
875
|
+
const state = this.getCallState(event.callId);
|
|
876
|
+
if (!(state == null ? void 0 : state.inferenceSpan)) return;
|
|
877
|
+
const { telemetry } = state;
|
|
878
|
+
state.inferenceSpan.setAttributes(
|
|
879
|
+
selectAttributes(telemetry, {
|
|
880
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
881
|
+
"gen_ai.response.id": event.response.id,
|
|
882
|
+
"gen_ai.response.model": event.response.modelId,
|
|
883
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
884
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
885
|
+
"gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
|
|
886
|
+
"gen_ai.output.messages": {
|
|
887
|
+
output: () => {
|
|
888
|
+
try {
|
|
889
|
+
return JSON.stringify(
|
|
890
|
+
formatObjectOutputMessages({
|
|
891
|
+
objectText: event.objectText,
|
|
892
|
+
finishReason: event.finishReason
|
|
893
|
+
})
|
|
894
|
+
);
|
|
895
|
+
} catch (e) {
|
|
896
|
+
return event.objectText;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
},
|
|
900
|
+
...selectSupplementalAttributes(
|
|
901
|
+
telemetry,
|
|
902
|
+
this.supplementalAttributes,
|
|
903
|
+
{
|
|
904
|
+
providerMetadata: {
|
|
905
|
+
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
|
|
906
|
+
},
|
|
907
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
908
|
+
}
|
|
909
|
+
)
|
|
910
|
+
})
|
|
911
|
+
);
|
|
912
|
+
state.inferenceSpan.end();
|
|
913
|
+
state.inferenceSpan = void 0;
|
|
914
|
+
state.inferenceContext = void 0;
|
|
915
|
+
}
|
|
916
|
+
onEmbedOperationStart(event) {
|
|
917
|
+
const telemetry = {
|
|
918
|
+
recordInputs: event.recordInputs,
|
|
919
|
+
recordOutputs: event.recordOutputs,
|
|
920
|
+
functionId: event.functionId
|
|
921
|
+
};
|
|
922
|
+
const providerName = mapProviderName(event.provider);
|
|
923
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
924
|
+
telemetry,
|
|
925
|
+
this.supplementalAttributes,
|
|
926
|
+
{
|
|
927
|
+
headers: getHeaderAttributes(event.headers)
|
|
928
|
+
}
|
|
929
|
+
);
|
|
930
|
+
const value = event.value;
|
|
931
|
+
const isMany = event.operationId === "ai.embedMany";
|
|
932
|
+
const attributes = selectAttributes(telemetry, {
|
|
933
|
+
"gen_ai.operation.name": "embeddings",
|
|
934
|
+
"gen_ai.provider.name": providerName,
|
|
935
|
+
"gen_ai.request.model": event.modelId,
|
|
936
|
+
...baseSupplementalAttributes,
|
|
937
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
938
|
+
embedding: isMany ? {
|
|
939
|
+
"ai.values": {
|
|
940
|
+
input: () => value.map((v) => JSON.stringify(v))
|
|
941
|
+
}
|
|
942
|
+
} : {
|
|
943
|
+
"ai.value": {
|
|
944
|
+
input: () => JSON.stringify(value)
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
})
|
|
948
|
+
});
|
|
949
|
+
const spanName = `embeddings ${event.modelId}`;
|
|
950
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
951
|
+
attributes: this.getSpanAttributes({
|
|
952
|
+
attributes,
|
|
953
|
+
spanType: "operation",
|
|
954
|
+
operationId: event.operationId,
|
|
955
|
+
callId: event.callId,
|
|
956
|
+
runtimeContext: void 0
|
|
957
|
+
}),
|
|
958
|
+
kind: SpanKind.CLIENT
|
|
959
|
+
});
|
|
960
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
961
|
+
this.callStates.set(event.callId, {
|
|
962
|
+
operationId: event.operationId,
|
|
963
|
+
telemetry,
|
|
964
|
+
rootSpan,
|
|
965
|
+
rootContext,
|
|
966
|
+
stepSpan: void 0,
|
|
967
|
+
stepContext: void 0,
|
|
968
|
+
inferenceSpan: void 0,
|
|
969
|
+
inferenceContext: void 0,
|
|
970
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
971
|
+
rerankSpan: void 0,
|
|
972
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
973
|
+
settings: { maxRetries: event.maxRetries },
|
|
974
|
+
provider: event.provider,
|
|
975
|
+
modelId: event.modelId,
|
|
976
|
+
runtimeContext: void 0,
|
|
977
|
+
baseSupplementalAttributes
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
onStepStart(event) {
|
|
981
|
+
const state = this.getCallState(event.callId);
|
|
982
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
983
|
+
const { telemetry } = state;
|
|
984
|
+
state.runtimeContext = event.runtimeContext;
|
|
985
|
+
const stepAttributes = selectAttributes(telemetry, {
|
|
986
|
+
"gen_ai.operation.name": "agent_step",
|
|
987
|
+
...state.baseSupplementalAttributes,
|
|
988
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
989
|
+
toolChoice: {
|
|
990
|
+
"ai.prompt.toolChoice": {
|
|
991
|
+
input: () => event.stepToolChoice != null ? JSON.stringify(event.stepToolChoice) : void 0
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
})
|
|
995
|
+
});
|
|
996
|
+
state.stepSpan = this.tracer.startSpan(
|
|
997
|
+
`step ${event.steps.length + 1}`,
|
|
998
|
+
{
|
|
999
|
+
attributes: this.getSpanAttributes({
|
|
1000
|
+
attributes: stepAttributes,
|
|
1001
|
+
spanType: "step",
|
|
1002
|
+
operationId: state.operationId,
|
|
1003
|
+
callId: event.callId,
|
|
1004
|
+
runtimeContext: state.runtimeContext
|
|
1005
|
+
}),
|
|
1006
|
+
kind: SpanKind.INTERNAL
|
|
1007
|
+
},
|
|
1008
|
+
state.rootContext
|
|
1009
|
+
);
|
|
1010
|
+
state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
|
|
1011
|
+
}
|
|
1012
|
+
onLanguageModelCallStart(event) {
|
|
1013
|
+
var _a;
|
|
1014
|
+
const state = this.getCallState(event.callId);
|
|
1015
|
+
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
1016
|
+
const { telemetry } = state;
|
|
1017
|
+
const providerName = mapProviderName(event.provider);
|
|
1018
|
+
const inferenceAttributes = selectAttributes(telemetry, {
|
|
1019
|
+
"gen_ai.operation.name": "chat",
|
|
1020
|
+
"gen_ai.provider.name": providerName,
|
|
1021
|
+
"gen_ai.request.model": event.modelId,
|
|
1022
|
+
"gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
|
|
1023
|
+
"gen_ai.request.max_tokens": state.settings.maxOutputTokens,
|
|
1024
|
+
"gen_ai.request.presence_penalty": state.settings.presencePenalty,
|
|
1025
|
+
"gen_ai.request.stop_sequences": state.settings.stopSequences,
|
|
1026
|
+
"gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
|
|
1027
|
+
"gen_ai.request.top_k": state.settings.topK,
|
|
1028
|
+
"gen_ai.request.top_p": state.settings.topP,
|
|
1029
|
+
"gen_ai.input.messages": {
|
|
1030
|
+
input: () => {
|
|
1031
|
+
const formattedMessages = formatModelMessages({
|
|
1032
|
+
prompt: void 0,
|
|
1033
|
+
messages: event.messages
|
|
1034
|
+
});
|
|
1035
|
+
return formattedMessages.length > 0 ? JSON.stringify(formattedMessages) : void 0;
|
|
1036
|
+
}
|
|
1037
|
+
},
|
|
1038
|
+
"gen_ai.tool.definitions": {
|
|
1039
|
+
input: () => event.tools ? JSON.stringify(event.tools) : void 0
|
|
1040
|
+
}
|
|
1041
|
+
});
|
|
1042
|
+
state.inferenceSpan = this.tracer.startSpan(
|
|
1043
|
+
`chat ${event.modelId}`,
|
|
1044
|
+
{
|
|
1045
|
+
attributes: this.getSpanAttributes({
|
|
1046
|
+
attributes: inferenceAttributes,
|
|
1047
|
+
spanType: "languageModel",
|
|
1048
|
+
operationId: state.operationId,
|
|
1049
|
+
callId: event.callId,
|
|
1050
|
+
runtimeContext: state.runtimeContext
|
|
1051
|
+
}),
|
|
1052
|
+
kind: SpanKind.CLIENT
|
|
1053
|
+
},
|
|
1054
|
+
state.stepContext
|
|
1055
|
+
);
|
|
1056
|
+
state.inferenceContext = trace.setSpan(
|
|
1057
|
+
state.stepContext,
|
|
1058
|
+
state.inferenceSpan
|
|
1059
|
+
);
|
|
1060
|
+
}
|
|
1061
|
+
onLanguageModelCallEnd(event) {
|
|
1062
|
+
var _a, _b;
|
|
1063
|
+
const state = this.getCallState(event.callId);
|
|
1064
|
+
if (!(state == null ? void 0 : state.inferenceSpan)) return;
|
|
1065
|
+
const { telemetry } = state;
|
|
1066
|
+
state.inferenceSpan.setAttributes(
|
|
1067
|
+
selectAttributes(telemetry, {
|
|
1068
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
1069
|
+
"gen_ai.response.id": event.responseId,
|
|
1070
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
1071
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
1072
|
+
"gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
|
|
1073
|
+
"gen_ai.usage.cache_creation.input_tokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheWriteTokens,
|
|
1074
|
+
"gen_ai.output.messages": {
|
|
1075
|
+
output: () => JSON.stringify(
|
|
1076
|
+
formatOutputMessages({
|
|
1077
|
+
text: event.content.filter((p) => p.type === "text").map((p) => p.text).join("") || void 0,
|
|
1078
|
+
reasoning: event.content.filter((p) => p.type === "reasoning"),
|
|
1079
|
+
toolCalls: event.content.filter((p) => p.type === "tool-call"),
|
|
1080
|
+
files: event.content.filter((p) => p.type === "file").map((p) => p.file),
|
|
1081
|
+
finishReason: event.finishReason
|
|
1082
|
+
})
|
|
1083
|
+
)
|
|
1084
|
+
},
|
|
1085
|
+
...selectSupplementalAttributes(
|
|
1086
|
+
telemetry,
|
|
1087
|
+
this.supplementalAttributes,
|
|
1088
|
+
{
|
|
1089
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
1090
|
+
}
|
|
1091
|
+
)
|
|
1092
|
+
})
|
|
1093
|
+
);
|
|
1094
|
+
state.inferenceSpan.end();
|
|
1095
|
+
state.inferenceSpan = void 0;
|
|
1096
|
+
state.inferenceContext = void 0;
|
|
1097
|
+
}
|
|
1098
|
+
onToolExecutionStart(event) {
|
|
1099
|
+
const state = this.getCallState(event.callId);
|
|
1100
|
+
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
1101
|
+
const { telemetry } = state;
|
|
1102
|
+
const { toolCall } = event;
|
|
1103
|
+
const attributes = selectAttributes(telemetry, {
|
|
1104
|
+
"gen_ai.operation.name": "execute_tool",
|
|
1105
|
+
"gen_ai.tool.name": toolCall.toolName,
|
|
1106
|
+
"gen_ai.tool.call.id": toolCall.toolCallId,
|
|
1107
|
+
"gen_ai.tool.type": "function",
|
|
1108
|
+
"gen_ai.tool.call.arguments": {
|
|
1109
|
+
input: () => JSON.stringify(toolCall.input)
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
const spanName = `execute_tool ${toolCall.toolName}`;
|
|
1113
|
+
const toolSpan = this.tracer.startSpan(
|
|
1114
|
+
spanName,
|
|
1115
|
+
{
|
|
1116
|
+
attributes: this.getSpanAttributes({
|
|
1117
|
+
attributes,
|
|
1118
|
+
spanType: "tool",
|
|
1119
|
+
operationId: state.operationId,
|
|
1120
|
+
callId: event.callId,
|
|
1121
|
+
runtimeContext: state.runtimeContext
|
|
1122
|
+
}),
|
|
1123
|
+
kind: SpanKind.INTERNAL
|
|
1124
|
+
},
|
|
1125
|
+
state.stepContext
|
|
1126
|
+
);
|
|
1127
|
+
const toolContext = trace.setSpan(state.stepContext, toolSpan);
|
|
1128
|
+
state.toolSpans.set(toolCall.toolCallId, {
|
|
1129
|
+
span: toolSpan,
|
|
1130
|
+
context: toolContext
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
onToolExecutionEnd(event) {
|
|
1134
|
+
const state = this.getCallState(event.callId);
|
|
1135
|
+
if (!state) return;
|
|
1136
|
+
const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
|
|
1137
|
+
if (!toolSpanEntry) return;
|
|
1138
|
+
const { span } = toolSpanEntry;
|
|
1139
|
+
const { telemetry } = state;
|
|
1140
|
+
const { toolOutput } = event;
|
|
1141
|
+
if (toolOutput.type === "tool-result") {
|
|
1142
|
+
try {
|
|
1143
|
+
span.setAttributes(
|
|
1144
|
+
selectAttributes(telemetry, {
|
|
1145
|
+
"gen_ai.tool.call.result": {
|
|
1146
|
+
output: () => JSON.stringify(toolOutput.output)
|
|
1147
|
+
}
|
|
1148
|
+
})
|
|
1149
|
+
);
|
|
1150
|
+
} catch (e) {
|
|
1151
|
+
}
|
|
1152
|
+
} else {
|
|
1153
|
+
recordErrorOnSpan(span, toolOutput.error);
|
|
1154
|
+
}
|
|
1155
|
+
span.end();
|
|
1156
|
+
state.toolSpans.delete(event.toolCall.toolCallId);
|
|
1157
|
+
}
|
|
1158
|
+
onStepEnd(event) {
|
|
1159
|
+
const state = this.getCallState(event.callId);
|
|
1160
|
+
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
1161
|
+
const { telemetry } = state;
|
|
1162
|
+
state.stepSpan.setAttributes(
|
|
1163
|
+
selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1164
|
+
providerMetadata: {
|
|
1165
|
+
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
|
|
1166
|
+
},
|
|
1167
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
1168
|
+
})
|
|
1169
|
+
);
|
|
1170
|
+
state.stepSpan.end();
|
|
1171
|
+
state.stepSpan = void 0;
|
|
1172
|
+
state.stepContext = void 0;
|
|
1173
|
+
}
|
|
1174
|
+
/** @deprecated Use `onStepEnd` instead. */
|
|
1175
|
+
onStepFinish(event) {
|
|
1176
|
+
this.onStepEnd(event);
|
|
1177
|
+
}
|
|
1178
|
+
onEnd(event) {
|
|
1179
|
+
const state = this.getCallState(event.callId);
|
|
1180
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1181
|
+
if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
|
|
1182
|
+
this.onEmbedOperationEnd(event);
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
if (state.operationId === "ai.rerank") {
|
|
1186
|
+
this.onRerankOperationEnd(event);
|
|
1187
|
+
return;
|
|
1188
|
+
}
|
|
1189
|
+
if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
|
|
1190
|
+
this.onObjectOperationEnd(event);
|
|
1191
|
+
return;
|
|
1192
|
+
}
|
|
1193
|
+
this.onGenerateEnd(event);
|
|
1194
|
+
}
|
|
1195
|
+
onGenerateEnd(event) {
|
|
1196
|
+
var _a, _b;
|
|
1197
|
+
const state = this.getCallState(event.callId);
|
|
1198
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1199
|
+
const { telemetry } = state;
|
|
1200
|
+
state.rootSpan.setAttributes(
|
|
1201
|
+
selectAttributes(telemetry, {
|
|
1202
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
1203
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
1204
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
1205
|
+
"gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
|
|
1206
|
+
"gen_ai.usage.cache_creation.input_tokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheWriteTokens,
|
|
1207
|
+
"gen_ai.output.messages": {
|
|
1208
|
+
output: () => {
|
|
1209
|
+
var _a2;
|
|
1210
|
+
return JSON.stringify(
|
|
1211
|
+
formatOutputMessages({
|
|
1212
|
+
text: (_a2 = event.text) != null ? _a2 : void 0,
|
|
1213
|
+
reasoning: event.finalStep.reasoning,
|
|
1214
|
+
toolCalls: event.toolCalls,
|
|
1215
|
+
files: event.files,
|
|
1216
|
+
finishReason: event.finishReason
|
|
1217
|
+
})
|
|
1218
|
+
);
|
|
1219
|
+
}
|
|
1220
|
+
},
|
|
1221
|
+
...selectSupplementalAttributes(
|
|
1222
|
+
telemetry,
|
|
1223
|
+
this.supplementalAttributes,
|
|
1224
|
+
{
|
|
1225
|
+
providerMetadata: {
|
|
1226
|
+
"ai.response.providerMetadata": event.finalStep.providerMetadata ? JSON.stringify(event.finalStep.providerMetadata) : void 0
|
|
1227
|
+
},
|
|
1228
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
1229
|
+
}
|
|
1230
|
+
)
|
|
1231
|
+
})
|
|
1232
|
+
);
|
|
1233
|
+
state.rootSpan.end();
|
|
1234
|
+
this.cleanupCallState(event.callId);
|
|
1235
|
+
}
|
|
1236
|
+
onObjectOperationEnd(event) {
|
|
1237
|
+
var _a;
|
|
1238
|
+
const state = this.getCallState(event.callId);
|
|
1239
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1240
|
+
const { telemetry } = state;
|
|
1241
|
+
state.rootSpan.setAttributes(
|
|
1242
|
+
selectAttributes(telemetry, {
|
|
1243
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
1244
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
1245
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
1246
|
+
"gen_ai.usage.cache_read.input_tokens": (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens,
|
|
1247
|
+
"gen_ai.output.messages": {
|
|
1248
|
+
output: () => event.object != null ? JSON.stringify(
|
|
1249
|
+
formatObjectOutputMessages({
|
|
1250
|
+
objectText: JSON.stringify(event.object),
|
|
1251
|
+
finishReason: event.finishReason
|
|
1252
|
+
})
|
|
1253
|
+
) : void 0
|
|
1254
|
+
},
|
|
1255
|
+
...selectSupplementalAttributes(
|
|
1256
|
+
telemetry,
|
|
1257
|
+
this.supplementalAttributes,
|
|
1258
|
+
{
|
|
1259
|
+
providerMetadata: {
|
|
1260
|
+
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
|
|
1261
|
+
},
|
|
1262
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
1263
|
+
}
|
|
1264
|
+
)
|
|
1265
|
+
})
|
|
1266
|
+
);
|
|
1267
|
+
state.rootSpan.end();
|
|
1268
|
+
this.cleanupCallState(event.callId);
|
|
1269
|
+
}
|
|
1270
|
+
onEmbedOperationEnd(event) {
|
|
1271
|
+
const state = this.getCallState(event.callId);
|
|
1272
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1273
|
+
const { telemetry } = state;
|
|
1274
|
+
const isMany = state.operationId === "ai.embedMany";
|
|
1275
|
+
state.rootSpan.setAttributes(
|
|
1276
|
+
selectAttributes(telemetry, {
|
|
1277
|
+
"gen_ai.usage.input_tokens": event.usage.tokens,
|
|
1278
|
+
...selectSupplementalAttributes(
|
|
1279
|
+
telemetry,
|
|
1280
|
+
this.supplementalAttributes,
|
|
1281
|
+
{
|
|
1282
|
+
embedding: isMany ? {
|
|
1283
|
+
"ai.embeddings": {
|
|
1284
|
+
output: () => event.embedding.map(
|
|
1285
|
+
(e) => JSON.stringify(e)
|
|
1286
|
+
)
|
|
1287
|
+
}
|
|
1288
|
+
} : {
|
|
1289
|
+
"ai.embedding": {
|
|
1290
|
+
output: () => JSON.stringify(event.embedding)
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
)
|
|
1295
|
+
})
|
|
1296
|
+
);
|
|
1297
|
+
state.rootSpan.end();
|
|
1298
|
+
this.cleanupCallState(event.callId);
|
|
1299
|
+
}
|
|
1300
|
+
onEmbedStart(event) {
|
|
1301
|
+
const state = this.getCallState(event.callId);
|
|
1302
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
1303
|
+
const { telemetry } = state;
|
|
1304
|
+
const providerName = mapProviderName(state.provider);
|
|
1305
|
+
const attributes = selectAttributes(telemetry, {
|
|
1306
|
+
"gen_ai.operation.name": "embeddings",
|
|
1307
|
+
"gen_ai.provider.name": providerName,
|
|
1308
|
+
"gen_ai.request.model": state.modelId,
|
|
1309
|
+
...state.baseSupplementalAttributes,
|
|
1310
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1311
|
+
embedding: {
|
|
1312
|
+
"ai.values": {
|
|
1313
|
+
input: () => event.values.map((v) => JSON.stringify(v))
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
})
|
|
1317
|
+
});
|
|
1318
|
+
const spanName = `embeddings ${state.modelId}`;
|
|
1319
|
+
const embedSpan = this.tracer.startSpan(
|
|
1320
|
+
spanName,
|
|
1321
|
+
{
|
|
1322
|
+
attributes: this.getSpanAttributes({
|
|
1323
|
+
attributes,
|
|
1324
|
+
spanType: "embedding",
|
|
1325
|
+
operationId: state.operationId,
|
|
1326
|
+
callId: event.callId,
|
|
1327
|
+
runtimeContext: state.runtimeContext
|
|
1328
|
+
}),
|
|
1329
|
+
kind: SpanKind.CLIENT
|
|
1330
|
+
},
|
|
1331
|
+
state.rootContext
|
|
1332
|
+
);
|
|
1333
|
+
const embedContext = trace.setSpan(state.rootContext, embedSpan);
|
|
1334
|
+
state.embedSpans.set(event.embedCallId, {
|
|
1335
|
+
span: embedSpan,
|
|
1336
|
+
context: embedContext
|
|
1337
|
+
});
|
|
1338
|
+
}
|
|
1339
|
+
onEmbedEnd(event) {
|
|
1340
|
+
const state = this.getCallState(event.callId);
|
|
1341
|
+
if (!state) return;
|
|
1342
|
+
const embedSpanEntry = state.embedSpans.get(event.embedCallId);
|
|
1343
|
+
if (!embedSpanEntry) return;
|
|
1344
|
+
const { span } = embedSpanEntry;
|
|
1345
|
+
const { telemetry } = state;
|
|
1346
|
+
span.setAttributes(
|
|
1347
|
+
selectAttributes(telemetry, {
|
|
1348
|
+
"gen_ai.usage.input_tokens": event.usage.tokens,
|
|
1349
|
+
...selectSupplementalAttributes(
|
|
1350
|
+
telemetry,
|
|
1351
|
+
this.supplementalAttributes,
|
|
1352
|
+
{
|
|
1353
|
+
embedding: {
|
|
1354
|
+
"ai.embeddings": {
|
|
1355
|
+
output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
)
|
|
1360
|
+
})
|
|
1361
|
+
);
|
|
1362
|
+
span.end();
|
|
1363
|
+
state.embedSpans.delete(event.embedCallId);
|
|
1364
|
+
}
|
|
1365
|
+
onRerankOperationStart(event) {
|
|
1366
|
+
const telemetry = {
|
|
1367
|
+
recordInputs: event.recordInputs,
|
|
1368
|
+
recordOutputs: event.recordOutputs,
|
|
1369
|
+
functionId: event.functionId
|
|
1370
|
+
};
|
|
1371
|
+
const providerName = mapProviderName(event.provider);
|
|
1372
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
1373
|
+
telemetry,
|
|
1374
|
+
this.supplementalAttributes,
|
|
1375
|
+
{
|
|
1376
|
+
headers: getHeaderAttributes(event.headers)
|
|
1377
|
+
}
|
|
1378
|
+
);
|
|
1379
|
+
const attributes = selectAttributes(telemetry, {
|
|
1380
|
+
"gen_ai.operation.name": "rerank",
|
|
1381
|
+
"gen_ai.provider.name": providerName,
|
|
1382
|
+
"gen_ai.request.model": event.modelId,
|
|
1383
|
+
...baseSupplementalAttributes,
|
|
1384
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1385
|
+
reranking: {
|
|
1386
|
+
"ai.documents": {
|
|
1387
|
+
input: () => event.documents.map((d) => JSON.stringify(d))
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
})
|
|
1391
|
+
});
|
|
1392
|
+
const spanName = `rerank ${event.modelId}`;
|
|
1393
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
1394
|
+
attributes: this.getSpanAttributes({
|
|
1395
|
+
attributes,
|
|
1396
|
+
spanType: "operation",
|
|
1397
|
+
operationId: event.operationId,
|
|
1398
|
+
callId: event.callId,
|
|
1399
|
+
runtimeContext: void 0
|
|
1400
|
+
}),
|
|
1401
|
+
kind: SpanKind.CLIENT
|
|
1402
|
+
});
|
|
1403
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
1404
|
+
this.callStates.set(event.callId, {
|
|
1405
|
+
operationId: event.operationId,
|
|
1406
|
+
telemetry,
|
|
1407
|
+
rootSpan,
|
|
1408
|
+
rootContext,
|
|
1409
|
+
stepSpan: void 0,
|
|
1410
|
+
stepContext: void 0,
|
|
1411
|
+
inferenceSpan: void 0,
|
|
1412
|
+
inferenceContext: void 0,
|
|
1413
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
1414
|
+
rerankSpan: void 0,
|
|
1415
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
1416
|
+
settings: { maxRetries: event.maxRetries },
|
|
1417
|
+
provider: event.provider,
|
|
1418
|
+
modelId: event.modelId,
|
|
1419
|
+
runtimeContext: void 0,
|
|
1420
|
+
baseSupplementalAttributes
|
|
1421
|
+
});
|
|
1422
|
+
}
|
|
1423
|
+
onRerankOperationEnd(event) {
|
|
1424
|
+
const state = this.getCallState(event.callId);
|
|
1425
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1426
|
+
state.rootSpan.end();
|
|
1427
|
+
this.cleanupCallState(event.callId);
|
|
1428
|
+
}
|
|
1429
|
+
onRerankStart(event) {
|
|
1430
|
+
const state = this.getCallState(event.callId);
|
|
1431
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
1432
|
+
const { telemetry } = state;
|
|
1433
|
+
const providerName = mapProviderName(state.provider);
|
|
1434
|
+
const attributes = selectAttributes(telemetry, {
|
|
1435
|
+
"gen_ai.operation.name": "rerank",
|
|
1436
|
+
"gen_ai.provider.name": providerName,
|
|
1437
|
+
"gen_ai.request.model": state.modelId,
|
|
1438
|
+
...state.baseSupplementalAttributes,
|
|
1439
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1440
|
+
reranking: {
|
|
1441
|
+
"ai.documents": {
|
|
1442
|
+
input: () => event.documents.map((d) => JSON.stringify(d))
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
})
|
|
1446
|
+
});
|
|
1447
|
+
const spanName = `rerank ${state.modelId}`;
|
|
1448
|
+
const rerankSpan = this.tracer.startSpan(
|
|
1449
|
+
spanName,
|
|
1450
|
+
{
|
|
1451
|
+
attributes: this.getSpanAttributes({
|
|
1452
|
+
attributes,
|
|
1453
|
+
spanType: "reranking",
|
|
1454
|
+
operationId: state.operationId,
|
|
1455
|
+
callId: event.callId,
|
|
1456
|
+
runtimeContext: state.runtimeContext
|
|
1457
|
+
}),
|
|
1458
|
+
kind: SpanKind.CLIENT
|
|
1459
|
+
},
|
|
1460
|
+
state.rootContext
|
|
1461
|
+
);
|
|
1462
|
+
const rerankContext = trace.setSpan(state.rootContext, rerankSpan);
|
|
1463
|
+
state.rerankSpan = { span: rerankSpan, context: rerankContext };
|
|
1464
|
+
}
|
|
1465
|
+
onRerankEnd(event) {
|
|
1466
|
+
const state = this.getCallState(event.callId);
|
|
1467
|
+
if (!(state == null ? void 0 : state.rerankSpan)) return;
|
|
1468
|
+
const { span } = state.rerankSpan;
|
|
1469
|
+
const { telemetry } = state;
|
|
1470
|
+
span.setAttributes(
|
|
1471
|
+
selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1472
|
+
reranking: {
|
|
1473
|
+
"ai.ranking.type": event.documentsType,
|
|
1474
|
+
"ai.ranking": {
|
|
1475
|
+
output: () => event.ranking.map((r) => JSON.stringify(r))
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
})
|
|
1479
|
+
);
|
|
1480
|
+
span.end();
|
|
1481
|
+
state.rerankSpan = void 0;
|
|
1482
|
+
}
|
|
1483
|
+
onAbort(event) {
|
|
1484
|
+
const state = this.getCallState(event.callId);
|
|
1485
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1486
|
+
for (const { span: toolSpan } of state.toolSpans.values()) {
|
|
1487
|
+
toolSpan.end();
|
|
1488
|
+
}
|
|
1489
|
+
state.toolSpans.clear();
|
|
1490
|
+
if (state.inferenceSpan) {
|
|
1491
|
+
state.inferenceSpan.end();
|
|
1492
|
+
state.inferenceSpan = void 0;
|
|
1493
|
+
state.inferenceContext = void 0;
|
|
1494
|
+
}
|
|
1495
|
+
if (state.stepSpan) {
|
|
1496
|
+
state.stepSpan.end();
|
|
1497
|
+
state.stepSpan = void 0;
|
|
1498
|
+
state.stepContext = void 0;
|
|
1499
|
+
}
|
|
1500
|
+
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
1501
|
+
embedSpan.end();
|
|
1502
|
+
}
|
|
1503
|
+
state.embedSpans.clear();
|
|
1504
|
+
if (state.rerankSpan) {
|
|
1505
|
+
state.rerankSpan.span.end();
|
|
1506
|
+
state.rerankSpan = void 0;
|
|
1507
|
+
}
|
|
1508
|
+
state.rootSpan.end();
|
|
1509
|
+
this.cleanupCallState(event.callId);
|
|
1510
|
+
}
|
|
1511
|
+
onError(error) {
|
|
1512
|
+
var _a;
|
|
1513
|
+
const event = error;
|
|
1514
|
+
if (!(event == null ? void 0 : event.callId)) return;
|
|
1515
|
+
const state = this.getCallState(event.callId);
|
|
1516
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1517
|
+
const actualError = (_a = event.error) != null ? _a : error;
|
|
1518
|
+
for (const { span: toolSpan } of state.toolSpans.values()) {
|
|
1519
|
+
recordErrorOnSpan(toolSpan, actualError);
|
|
1520
|
+
toolSpan.end();
|
|
1521
|
+
}
|
|
1522
|
+
state.toolSpans.clear();
|
|
1523
|
+
if (state.inferenceSpan) {
|
|
1524
|
+
recordErrorOnSpan(state.inferenceSpan, actualError);
|
|
1525
|
+
state.inferenceSpan.end();
|
|
1526
|
+
state.inferenceSpan = void 0;
|
|
1527
|
+
state.inferenceContext = void 0;
|
|
1528
|
+
}
|
|
1529
|
+
if (state.stepSpan) {
|
|
1530
|
+
recordErrorOnSpan(state.stepSpan, actualError);
|
|
1531
|
+
state.stepSpan.end();
|
|
1532
|
+
state.stepSpan = void 0;
|
|
1533
|
+
state.stepContext = void 0;
|
|
1534
|
+
}
|
|
1535
|
+
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
1536
|
+
recordErrorOnSpan(embedSpan, actualError);
|
|
1537
|
+
embedSpan.end();
|
|
1538
|
+
}
|
|
1539
|
+
state.embedSpans.clear();
|
|
1540
|
+
if (state.rerankSpan) {
|
|
1541
|
+
recordErrorOnSpan(state.rerankSpan.span, actualError);
|
|
1542
|
+
state.rerankSpan.span.end();
|
|
1543
|
+
state.rerankSpan = void 0;
|
|
1544
|
+
}
|
|
1545
|
+
recordErrorOnSpan(state.rootSpan, actualError);
|
|
1546
|
+
state.rootSpan.end();
|
|
1547
|
+
this.cleanupCallState(event.callId);
|
|
15
1548
|
}
|
|
16
|
-
return to;
|
|
17
1549
|
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
OpenTelemetryIntegration: () => OpenTelemetryIntegration
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(index_exports);
|
|
26
1550
|
|
|
27
|
-
// src/open-telemetry
|
|
28
|
-
|
|
1551
|
+
// src/legacy-open-telemetry.ts
|
|
1552
|
+
import {
|
|
1553
|
+
context as context3,
|
|
1554
|
+
SpanStatusCode as SpanStatusCode2,
|
|
1555
|
+
trace as trace2
|
|
1556
|
+
} from "@opentelemetry/api";
|
|
29
1557
|
|
|
30
1558
|
// src/assemble-operation-name.ts
|
|
31
1559
|
function assembleOperationName({
|
|
@@ -46,10 +1574,9 @@ function assembleOperationName({
|
|
|
46
1574
|
function getBaseTelemetryAttributes({
|
|
47
1575
|
model,
|
|
48
1576
|
settings,
|
|
49
|
-
|
|
50
|
-
|
|
1577
|
+
headers,
|
|
1578
|
+
context: context4
|
|
51
1579
|
}) {
|
|
52
|
-
var _a;
|
|
53
1580
|
return {
|
|
54
1581
|
"ai.model.provider": model.provider,
|
|
55
1582
|
"ai.model.id": model.modelId,
|
|
@@ -58,16 +1585,8 @@ function getBaseTelemetryAttributes({
|
|
|
58
1585
|
attributes[`ai.settings.${key}`] = value;
|
|
59
1586
|
return attributes;
|
|
60
1587
|
}, {}),
|
|
61
|
-
// add
|
|
62
|
-
...
|
|
63
|
-
(attributes, [key, value]) => {
|
|
64
|
-
if (value != void 0) {
|
|
65
|
-
attributes[`ai.telemetry.metadata.${key}`] = value;
|
|
66
|
-
}
|
|
67
|
-
return attributes;
|
|
68
|
-
},
|
|
69
|
-
{}
|
|
70
|
-
),
|
|
1588
|
+
// add context as attributes:
|
|
1589
|
+
...getRuntimeContextAttributes(context4),
|
|
71
1590
|
// request headers
|
|
72
1591
|
...Object.entries(headers != null ? headers : {}).reduce((attributes, [key, value]) => {
|
|
73
1592
|
if (value !== void 0) {
|
|
@@ -79,7 +1598,7 @@ function getBaseTelemetryAttributes({
|
|
|
79
1598
|
}
|
|
80
1599
|
|
|
81
1600
|
// src/stringify-for-telemetry.ts
|
|
82
|
-
|
|
1601
|
+
import { convertDataContentToBase64String as convertDataContentToBase64String2 } from "ai";
|
|
83
1602
|
function stringifyForTelemetry(prompt) {
|
|
84
1603
|
return JSON.stringify(
|
|
85
1604
|
prompt.map((message) => ({
|
|
@@ -87,14 +1606,26 @@ function stringifyForTelemetry(prompt) {
|
|
|
87
1606
|
content: typeof message.content === "string" ? message.content : message.content.map(
|
|
88
1607
|
(part) => part.type === "file" ? {
|
|
89
1608
|
...part,
|
|
90
|
-
data:
|
|
1609
|
+
data: serializeFileData(part.data)
|
|
91
1610
|
} : part
|
|
92
1611
|
)
|
|
93
1612
|
}))
|
|
94
1613
|
);
|
|
95
1614
|
}
|
|
1615
|
+
function serializeFileData(data) {
|
|
1616
|
+
switch (data.type) {
|
|
1617
|
+
case "data":
|
|
1618
|
+
return data.data instanceof Uint8Array ? convertDataContentToBase64String2(data.data) : data.data;
|
|
1619
|
+
case "url":
|
|
1620
|
+
return data.url.toString();
|
|
1621
|
+
case "reference":
|
|
1622
|
+
return data.reference;
|
|
1623
|
+
case "text":
|
|
1624
|
+
return data.text;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
96
1627
|
|
|
97
|
-
// src/open-telemetry
|
|
1628
|
+
// src/legacy-open-telemetry.ts
|
|
98
1629
|
function recordSpanError(span, error) {
|
|
99
1630
|
if (error instanceof Error) {
|
|
100
1631
|
span.recordException({
|
|
@@ -103,18 +1634,18 @@ function recordSpanError(span, error) {
|
|
|
103
1634
|
stack: error.stack
|
|
104
1635
|
});
|
|
105
1636
|
span.setStatus({
|
|
106
|
-
code:
|
|
1637
|
+
code: SpanStatusCode2.ERROR,
|
|
107
1638
|
message: error.message
|
|
108
1639
|
});
|
|
109
1640
|
} else {
|
|
110
|
-
span.setStatus({ code:
|
|
1641
|
+
span.setStatus({ code: SpanStatusCode2.ERROR });
|
|
111
1642
|
}
|
|
112
1643
|
}
|
|
113
|
-
function
|
|
114
|
-
return (telemetry == null ? void 0 : telemetry.isEnabled)
|
|
1644
|
+
function shouldRecord2(telemetry) {
|
|
1645
|
+
return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
|
|
115
1646
|
}
|
|
116
|
-
function
|
|
117
|
-
if (!
|
|
1647
|
+
function selectAttributes2(telemetry, attributes) {
|
|
1648
|
+
if (!shouldRecord2(telemetry)) {
|
|
118
1649
|
return {};
|
|
119
1650
|
}
|
|
120
1651
|
const result = {};
|
|
@@ -136,11 +1667,11 @@ function selectAttributes(telemetry, attributes) {
|
|
|
136
1667
|
}
|
|
137
1668
|
return result;
|
|
138
1669
|
}
|
|
139
|
-
var
|
|
1670
|
+
var LegacyOpenTelemetry = class {
|
|
140
1671
|
constructor(options = {}) {
|
|
141
1672
|
this.callStates = /* @__PURE__ */ new Map();
|
|
142
1673
|
var _a;
|
|
143
|
-
this.tracer = (_a = options.tracer) != null ? _a :
|
|
1674
|
+
this.tracer = (_a = options.tracer) != null ? _a : trace2.getTracer("ai");
|
|
144
1675
|
}
|
|
145
1676
|
getCallState(callId) {
|
|
146
1677
|
return this.callStates.get(callId);
|
|
@@ -158,31 +1689,47 @@ var OpenTelemetryIntegration = class {
|
|
|
158
1689
|
if (toolSpanEntry == null) {
|
|
159
1690
|
return execute();
|
|
160
1691
|
}
|
|
161
|
-
return
|
|
1692
|
+
return context3.with(toolSpanEntry.context, execute);
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* Runs the provider `doGenerate`/`doStream` call with the active legacy
|
|
1696
|
+
* model-call context.
|
|
1697
|
+
*/
|
|
1698
|
+
executeLanguageModelCall({
|
|
1699
|
+
callId,
|
|
1700
|
+
execute
|
|
1701
|
+
}) {
|
|
1702
|
+
var _a;
|
|
1703
|
+
const stepContext = (_a = this.getCallState(callId)) == null ? void 0 : _a.stepContext;
|
|
1704
|
+
if (stepContext == null) {
|
|
1705
|
+
return execute();
|
|
1706
|
+
}
|
|
1707
|
+
return context3.with(stepContext, execute);
|
|
162
1708
|
}
|
|
163
1709
|
onStart(event) {
|
|
164
|
-
if (event.isEnabled !== true) return;
|
|
165
1710
|
if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
|
|
166
1711
|
this.onEmbedOperationStart(event);
|
|
167
1712
|
return;
|
|
168
1713
|
}
|
|
169
1714
|
if (event.operationId === "ai.rerank") {
|
|
170
|
-
this.onRerankOperationStart(
|
|
1715
|
+
this.onRerankOperationStart(
|
|
1716
|
+
event
|
|
1717
|
+
);
|
|
171
1718
|
return;
|
|
172
1719
|
}
|
|
173
1720
|
if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
|
|
174
|
-
this.onObjectOperationStart(
|
|
1721
|
+
this.onObjectOperationStart(
|
|
1722
|
+
event
|
|
1723
|
+
);
|
|
175
1724
|
return;
|
|
176
1725
|
}
|
|
177
1726
|
this.onGenerateStart(event);
|
|
178
1727
|
}
|
|
179
1728
|
onGenerateStart(event) {
|
|
180
1729
|
const telemetry = {
|
|
181
|
-
isEnabled: event.isEnabled,
|
|
182
1730
|
recordInputs: event.recordInputs,
|
|
183
1731
|
recordOutputs: event.recordOutputs,
|
|
184
|
-
functionId: event.functionId
|
|
185
|
-
metadata: event.metadata
|
|
1732
|
+
functionId: event.functionId
|
|
186
1733
|
};
|
|
187
1734
|
const settings = {
|
|
188
1735
|
maxOutputTokens: event.maxOutputTokens,
|
|
@@ -197,11 +1744,11 @@ var OpenTelemetryIntegration = class {
|
|
|
197
1744
|
};
|
|
198
1745
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
199
1746
|
model: { provider: event.provider, modelId: event.modelId },
|
|
200
|
-
telemetry,
|
|
201
1747
|
headers: event.headers,
|
|
202
|
-
settings
|
|
1748
|
+
settings,
|
|
1749
|
+
context: event.runtimeContext
|
|
203
1750
|
});
|
|
204
|
-
const attributes =
|
|
1751
|
+
const attributes = selectAttributes2(telemetry, {
|
|
205
1752
|
...assembleOperationName({
|
|
206
1753
|
operationId: event.operationId,
|
|
207
1754
|
telemetry
|
|
@@ -211,14 +1758,13 @@ var OpenTelemetryIntegration = class {
|
|
|
211
1758
|
"ai.model.id": event.modelId,
|
|
212
1759
|
"ai.prompt": {
|
|
213
1760
|
input: () => JSON.stringify({
|
|
214
|
-
system: event.
|
|
215
|
-
prompt: event.prompt,
|
|
1761
|
+
system: event.instructions,
|
|
216
1762
|
messages: event.messages
|
|
217
1763
|
})
|
|
218
1764
|
}
|
|
219
1765
|
});
|
|
220
1766
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
221
|
-
const rootContext =
|
|
1767
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
222
1768
|
this.callStates.set(event.callId, {
|
|
223
1769
|
operationId: event.operationId,
|
|
224
1770
|
telemetry,
|
|
@@ -235,11 +1781,9 @@ var OpenTelemetryIntegration = class {
|
|
|
235
1781
|
}
|
|
236
1782
|
onObjectOperationStart(event) {
|
|
237
1783
|
const telemetry = {
|
|
238
|
-
isEnabled: event.isEnabled,
|
|
239
1784
|
recordInputs: event.recordInputs,
|
|
240
1785
|
recordOutputs: event.recordOutputs,
|
|
241
|
-
functionId: event.functionId
|
|
242
|
-
metadata: event.metadata
|
|
1786
|
+
functionId: event.functionId
|
|
243
1787
|
};
|
|
244
1788
|
const settings = {
|
|
245
1789
|
maxOutputTokens: event.maxOutputTokens,
|
|
@@ -253,11 +1797,11 @@ var OpenTelemetryIntegration = class {
|
|
|
253
1797
|
};
|
|
254
1798
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
255
1799
|
model: { provider: event.provider, modelId: event.modelId },
|
|
256
|
-
telemetry,
|
|
257
1800
|
headers: event.headers,
|
|
258
|
-
settings
|
|
1801
|
+
settings,
|
|
1802
|
+
context: void 0
|
|
259
1803
|
});
|
|
260
|
-
const attributes =
|
|
1804
|
+
const attributes = selectAttributes2(telemetry, {
|
|
261
1805
|
...assembleOperationName({
|
|
262
1806
|
operationId: event.operationId,
|
|
263
1807
|
telemetry
|
|
@@ -276,7 +1820,7 @@ var OpenTelemetryIntegration = class {
|
|
|
276
1820
|
"ai.settings.output": event.output
|
|
277
1821
|
});
|
|
278
1822
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
279
|
-
const rootContext =
|
|
1823
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
280
1824
|
this.callStates.set(event.callId, {
|
|
281
1825
|
operationId: event.operationId,
|
|
282
1826
|
telemetry,
|
|
@@ -298,7 +1842,7 @@ var OpenTelemetryIntegration = class {
|
|
|
298
1842
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
299
1843
|
const { telemetry } = state;
|
|
300
1844
|
const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
|
|
301
|
-
const attributes =
|
|
1845
|
+
const attributes = selectAttributes2(telemetry, {
|
|
302
1846
|
...assembleOperationName({
|
|
303
1847
|
operationId: stepOperationId,
|
|
304
1848
|
telemetry
|
|
@@ -321,15 +1865,16 @@ var OpenTelemetryIntegration = class {
|
|
|
321
1865
|
{ attributes },
|
|
322
1866
|
state.rootContext
|
|
323
1867
|
);
|
|
324
|
-
state.stepContext =
|
|
1868
|
+
state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
|
|
325
1869
|
}
|
|
326
1870
|
/** @deprecated */
|
|
327
|
-
|
|
1871
|
+
onObjectStepEnd(event) {
|
|
1872
|
+
var _a, _b;
|
|
328
1873
|
const state = this.getCallState(event.callId);
|
|
329
1874
|
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
330
1875
|
const { telemetry } = state;
|
|
331
1876
|
state.stepSpan.setAttributes(
|
|
332
|
-
|
|
1877
|
+
selectAttributes2(telemetry, {
|
|
333
1878
|
"ai.response.finishReason": event.finishReason,
|
|
334
1879
|
"ai.response.object": {
|
|
335
1880
|
output: () => {
|
|
@@ -347,8 +1892,8 @@ var OpenTelemetryIntegration = class {
|
|
|
347
1892
|
"ai.usage.inputTokens": event.usage.inputTokens,
|
|
348
1893
|
"ai.usage.outputTokens": event.usage.outputTokens,
|
|
349
1894
|
"ai.usage.totalTokens": event.usage.totalTokens,
|
|
350
|
-
"ai.usage.reasoningTokens": event.usage.reasoningTokens,
|
|
351
|
-
"ai.usage.cachedInputTokens": event.usage.
|
|
1895
|
+
"ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
|
|
1896
|
+
"ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
|
|
352
1897
|
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
353
1898
|
"gen_ai.response.id": event.response.id,
|
|
354
1899
|
"gen_ai.response.model": event.response.modelId,
|
|
@@ -370,24 +1915,22 @@ var OpenTelemetryIntegration = class {
|
|
|
370
1915
|
}
|
|
371
1916
|
onEmbedOperationStart(event) {
|
|
372
1917
|
const telemetry = {
|
|
373
|
-
isEnabled: event.isEnabled,
|
|
374
1918
|
recordInputs: event.recordInputs,
|
|
375
1919
|
recordOutputs: event.recordOutputs,
|
|
376
|
-
functionId: event.functionId
|
|
377
|
-
metadata: event.metadata
|
|
1920
|
+
functionId: event.functionId
|
|
378
1921
|
};
|
|
379
1922
|
const settings = {
|
|
380
1923
|
maxRetries: event.maxRetries
|
|
381
1924
|
};
|
|
382
1925
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
383
1926
|
model: { provider: event.provider, modelId: event.modelId },
|
|
384
|
-
telemetry,
|
|
385
1927
|
headers: event.headers,
|
|
386
|
-
settings
|
|
1928
|
+
settings,
|
|
1929
|
+
context: void 0
|
|
387
1930
|
});
|
|
388
1931
|
const value = event.value;
|
|
389
1932
|
const isMany = event.operationId === "ai.embedMany";
|
|
390
|
-
const attributes =
|
|
1933
|
+
const attributes = selectAttributes2(telemetry, {
|
|
391
1934
|
...assembleOperationName({
|
|
392
1935
|
operationId: event.operationId,
|
|
393
1936
|
telemetry
|
|
@@ -404,7 +1947,7 @@ var OpenTelemetryIntegration = class {
|
|
|
404
1947
|
}
|
|
405
1948
|
});
|
|
406
1949
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
407
|
-
const rootContext =
|
|
1950
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
408
1951
|
this.callStates.set(event.callId, {
|
|
409
1952
|
operationId: event.operationId,
|
|
410
1953
|
telemetry,
|
|
@@ -425,7 +1968,7 @@ var OpenTelemetryIntegration = class {
|
|
|
425
1968
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
426
1969
|
const { telemetry } = state;
|
|
427
1970
|
const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
|
|
428
|
-
const attributes =
|
|
1971
|
+
const attributes = selectAttributes2(telemetry, {
|
|
429
1972
|
...assembleOperationName({
|
|
430
1973
|
operationId: stepOperationId,
|
|
431
1974
|
telemetry
|
|
@@ -460,14 +2003,14 @@ var OpenTelemetryIntegration = class {
|
|
|
460
2003
|
{ attributes },
|
|
461
2004
|
state.rootContext
|
|
462
2005
|
);
|
|
463
|
-
state.stepContext =
|
|
2006
|
+
state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
|
|
464
2007
|
}
|
|
465
|
-
|
|
2008
|
+
onToolExecutionStart(event) {
|
|
466
2009
|
const state = this.getCallState(event.callId);
|
|
467
2010
|
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
468
2011
|
const { telemetry } = state;
|
|
469
2012
|
const { toolCall } = event;
|
|
470
|
-
const attributes =
|
|
2013
|
+
const attributes = selectAttributes2(telemetry, {
|
|
471
2014
|
...assembleOperationName({
|
|
472
2015
|
operationId: "ai.toolCall",
|
|
473
2016
|
telemetry
|
|
@@ -483,43 +2026,45 @@ var OpenTelemetryIntegration = class {
|
|
|
483
2026
|
{ attributes },
|
|
484
2027
|
state.stepContext
|
|
485
2028
|
);
|
|
486
|
-
const toolContext =
|
|
2029
|
+
const toolContext = trace2.setSpan(state.stepContext, toolSpan);
|
|
487
2030
|
state.toolSpans.set(toolCall.toolCallId, {
|
|
488
2031
|
span: toolSpan,
|
|
489
2032
|
context: toolContext
|
|
490
2033
|
});
|
|
491
2034
|
}
|
|
492
|
-
|
|
2035
|
+
onToolExecutionEnd(event) {
|
|
493
2036
|
const state = this.getCallState(event.callId);
|
|
494
2037
|
if (!state) return;
|
|
495
2038
|
const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
|
|
496
2039
|
if (!toolSpanEntry) return;
|
|
497
2040
|
const { span } = toolSpanEntry;
|
|
498
2041
|
const { telemetry } = state;
|
|
499
|
-
|
|
2042
|
+
const { toolOutput } = event;
|
|
2043
|
+
if (toolOutput.type === "tool-result") {
|
|
500
2044
|
try {
|
|
501
2045
|
span.setAttributes(
|
|
502
|
-
|
|
2046
|
+
selectAttributes2(telemetry, {
|
|
503
2047
|
"ai.toolCall.result": {
|
|
504
|
-
output: () => JSON.stringify(
|
|
2048
|
+
output: () => JSON.stringify(toolOutput.output)
|
|
505
2049
|
}
|
|
506
2050
|
})
|
|
507
2051
|
);
|
|
508
|
-
} catch (
|
|
2052
|
+
} catch (e) {
|
|
509
2053
|
}
|
|
510
2054
|
} else {
|
|
511
|
-
recordSpanError(span,
|
|
2055
|
+
recordSpanError(span, toolOutput.error);
|
|
512
2056
|
}
|
|
513
2057
|
span.end();
|
|
514
2058
|
state.toolSpans.delete(event.toolCall.toolCallId);
|
|
515
2059
|
}
|
|
516
|
-
|
|
517
|
-
var _a, _b, _c, _d, _e;
|
|
2060
|
+
onStepEnd(event) {
|
|
2061
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
518
2062
|
const state = this.getCallState(event.callId);
|
|
519
2063
|
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
520
2064
|
const { telemetry } = state;
|
|
2065
|
+
const isStreamText = state.operationId === "ai.streamText";
|
|
521
2066
|
state.stepSpan.setAttributes(
|
|
522
|
-
|
|
2067
|
+
selectAttributes2(telemetry, {
|
|
523
2068
|
"ai.response.finishReason": event.finishReason,
|
|
524
2069
|
"ai.response.text": {
|
|
525
2070
|
output: () => {
|
|
@@ -552,16 +2097,19 @@ var OpenTelemetryIntegration = class {
|
|
|
552
2097
|
"ai.response.model": event.response.modelId,
|
|
553
2098
|
"ai.response.timestamp": event.response.timestamp.toISOString(),
|
|
554
2099
|
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0,
|
|
2100
|
+
"ai.response.msToFirstChunk": isStreamText ? event.performance.timeToFirstOutputMs : void 0,
|
|
2101
|
+
"ai.response.msToFinish": isStreamText ? event.performance.responseTimeMs : void 0,
|
|
2102
|
+
"ai.response.avgOutputTokensPerSecond": isStreamText ? event.performance.effectiveOutputTokensPerSecond : void 0,
|
|
555
2103
|
"ai.usage.inputTokens": event.usage.inputTokens,
|
|
556
2104
|
"ai.usage.outputTokens": event.usage.outputTokens,
|
|
557
2105
|
"ai.usage.totalTokens": event.usage.totalTokens,
|
|
558
|
-
"ai.usage.reasoningTokens": event.usage.reasoningTokens,
|
|
559
|
-
"ai.usage.cachedInputTokens": event.usage.
|
|
560
|
-
"ai.usage.inputTokenDetails.noCacheTokens": (
|
|
561
|
-
"ai.usage.inputTokenDetails.cacheReadTokens": (
|
|
562
|
-
"ai.usage.inputTokenDetails.cacheWriteTokens": (
|
|
563
|
-
"ai.usage.outputTokenDetails.textTokens": (
|
|
564
|
-
"ai.usage.outputTokenDetails.reasoningTokens": (
|
|
2106
|
+
"ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
|
|
2107
|
+
"ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
|
|
2108
|
+
"ai.usage.inputTokenDetails.noCacheTokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.noCacheTokens,
|
|
2109
|
+
"ai.usage.inputTokenDetails.cacheReadTokens": (_d = event.usage.inputTokenDetails) == null ? void 0 : _d.cacheReadTokens,
|
|
2110
|
+
"ai.usage.inputTokenDetails.cacheWriteTokens": (_e = event.usage.inputTokenDetails) == null ? void 0 : _e.cacheWriteTokens,
|
|
2111
|
+
"ai.usage.outputTokenDetails.textTokens": (_f = event.usage.outputTokenDetails) == null ? void 0 : _f.textTokens,
|
|
2112
|
+
"ai.usage.outputTokenDetails.reasoningTokens": (_g = event.usage.outputTokenDetails) == null ? void 0 : _g.reasoningTokens,
|
|
565
2113
|
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
566
2114
|
"gen_ai.response.id": event.response.id,
|
|
567
2115
|
"gen_ai.response.model": event.response.modelId,
|
|
@@ -569,34 +2117,49 @@ var OpenTelemetryIntegration = class {
|
|
|
569
2117
|
"gen_ai.usage.output_tokens": event.usage.outputTokens
|
|
570
2118
|
})
|
|
571
2119
|
);
|
|
2120
|
+
if (isStreamText && event.performance.timeToFirstOutputMs != null) {
|
|
2121
|
+
state.stepSpan.addEvent("ai.stream.firstChunk", {
|
|
2122
|
+
"ai.response.msToFirstChunk": event.performance.timeToFirstOutputMs
|
|
2123
|
+
});
|
|
2124
|
+
}
|
|
2125
|
+
if (isStreamText) {
|
|
2126
|
+
state.stepSpan.addEvent("ai.stream.finish", {
|
|
2127
|
+
"ai.response.msToFinish": event.performance.responseTimeMs,
|
|
2128
|
+
"ai.response.avgOutputTokensPerSecond": event.performance.effectiveOutputTokensPerSecond
|
|
2129
|
+
});
|
|
2130
|
+
}
|
|
572
2131
|
state.stepSpan.end();
|
|
573
2132
|
state.stepSpan = void 0;
|
|
574
2133
|
state.stepContext = void 0;
|
|
575
2134
|
}
|
|
576
|
-
|
|
2135
|
+
/** @deprecated Use `onStepEnd` instead. */
|
|
2136
|
+
onStepFinish(event) {
|
|
2137
|
+
this.onStepEnd(event);
|
|
2138
|
+
}
|
|
2139
|
+
onEnd(event) {
|
|
577
2140
|
const state = this.getCallState(event.callId);
|
|
578
2141
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
579
2142
|
if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
|
|
580
|
-
this.
|
|
2143
|
+
this.onEmbedOperationEnd(event);
|
|
581
2144
|
return;
|
|
582
2145
|
}
|
|
583
2146
|
if (state.operationId === "ai.rerank") {
|
|
584
|
-
this.
|
|
2147
|
+
this.onRerankOperationEnd(event);
|
|
585
2148
|
return;
|
|
586
2149
|
}
|
|
587
2150
|
if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
|
|
588
|
-
this.
|
|
2151
|
+
this.onObjectOperationEnd(event);
|
|
589
2152
|
return;
|
|
590
2153
|
}
|
|
591
|
-
this.
|
|
2154
|
+
this.onGenerateEnd(event);
|
|
592
2155
|
}
|
|
593
|
-
|
|
594
|
-
var _a, _b, _c, _d, _e;
|
|
2156
|
+
onGenerateEnd(event) {
|
|
2157
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
595
2158
|
const state = this.getCallState(event.callId);
|
|
596
2159
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
597
2160
|
const { telemetry } = state;
|
|
598
2161
|
state.rootSpan.setAttributes(
|
|
599
|
-
|
|
2162
|
+
selectAttributes2(telemetry, {
|
|
600
2163
|
"ai.response.finishReason": event.finishReason,
|
|
601
2164
|
"ai.response.text": {
|
|
602
2165
|
output: () => {
|
|
@@ -605,7 +2168,7 @@ var OpenTelemetryIntegration = class {
|
|
|
605
2168
|
}
|
|
606
2169
|
},
|
|
607
2170
|
"ai.response.reasoning": {
|
|
608
|
-
output: () => event.reasoning.length > 0 ? event.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
|
|
2171
|
+
output: () => event.finalStep.reasoning.length > 0 ? event.finalStep.reasoning.filter((part) => "text" in part).map((part) => part.text).join("\n") : void 0
|
|
609
2172
|
},
|
|
610
2173
|
"ai.response.toolCalls": {
|
|
611
2174
|
output: () => event.toolCalls.length > 0 ? JSON.stringify(
|
|
@@ -625,28 +2188,29 @@ var OpenTelemetryIntegration = class {
|
|
|
625
2188
|
}))
|
|
626
2189
|
) : void 0
|
|
627
2190
|
},
|
|
628
|
-
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0,
|
|
629
|
-
"ai.usage.inputTokens": event.
|
|
630
|
-
"ai.usage.outputTokens": event.
|
|
631
|
-
"ai.usage.totalTokens": event.
|
|
632
|
-
"ai.usage.reasoningTokens": event.
|
|
633
|
-
"ai.usage.cachedInputTokens": event.
|
|
634
|
-
"ai.usage.inputTokenDetails.noCacheTokens": (
|
|
635
|
-
"ai.usage.inputTokenDetails.cacheReadTokens": (
|
|
636
|
-
"ai.usage.inputTokenDetails.cacheWriteTokens": (
|
|
637
|
-
"ai.usage.outputTokenDetails.textTokens": (
|
|
638
|
-
"ai.usage.outputTokenDetails.reasoningTokens": (
|
|
2191
|
+
"ai.response.providerMetadata": event.finalStep.providerMetadata ? JSON.stringify(event.finalStep.providerMetadata) : void 0,
|
|
2192
|
+
"ai.usage.inputTokens": event.usage.inputTokens,
|
|
2193
|
+
"ai.usage.outputTokens": event.usage.outputTokens,
|
|
2194
|
+
"ai.usage.totalTokens": event.usage.totalTokens,
|
|
2195
|
+
"ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
|
|
2196
|
+
"ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens,
|
|
2197
|
+
"ai.usage.inputTokenDetails.noCacheTokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.noCacheTokens,
|
|
2198
|
+
"ai.usage.inputTokenDetails.cacheReadTokens": (_d = event.usage.inputTokenDetails) == null ? void 0 : _d.cacheReadTokens,
|
|
2199
|
+
"ai.usage.inputTokenDetails.cacheWriteTokens": (_e = event.usage.inputTokenDetails) == null ? void 0 : _e.cacheWriteTokens,
|
|
2200
|
+
"ai.usage.outputTokenDetails.textTokens": (_f = event.usage.outputTokenDetails) == null ? void 0 : _f.textTokens,
|
|
2201
|
+
"ai.usage.outputTokenDetails.reasoningTokens": (_g = event.usage.outputTokenDetails) == null ? void 0 : _g.reasoningTokens
|
|
639
2202
|
})
|
|
640
2203
|
);
|
|
641
2204
|
state.rootSpan.end();
|
|
642
2205
|
this.cleanupCallState(event.callId);
|
|
643
2206
|
}
|
|
644
|
-
|
|
2207
|
+
onObjectOperationEnd(event) {
|
|
2208
|
+
var _a, _b;
|
|
645
2209
|
const state = this.getCallState(event.callId);
|
|
646
2210
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
647
2211
|
const { telemetry } = state;
|
|
648
2212
|
state.rootSpan.setAttributes(
|
|
649
|
-
|
|
2213
|
+
selectAttributes2(telemetry, {
|
|
650
2214
|
"ai.response.finishReason": event.finishReason,
|
|
651
2215
|
"ai.response.object": {
|
|
652
2216
|
output: () => event.object != null ? JSON.stringify(event.object) : void 0
|
|
@@ -655,20 +2219,20 @@ var OpenTelemetryIntegration = class {
|
|
|
655
2219
|
"ai.usage.inputTokens": event.usage.inputTokens,
|
|
656
2220
|
"ai.usage.outputTokens": event.usage.outputTokens,
|
|
657
2221
|
"ai.usage.totalTokens": event.usage.totalTokens,
|
|
658
|
-
"ai.usage.reasoningTokens": event.usage.reasoningTokens,
|
|
659
|
-
"ai.usage.cachedInputTokens": event.usage.
|
|
2222
|
+
"ai.usage.reasoningTokens": (_a = event.usage.outputTokenDetails) == null ? void 0 : _a.reasoningTokens,
|
|
2223
|
+
"ai.usage.cachedInputTokens": (_b = event.usage.inputTokenDetails) == null ? void 0 : _b.cacheReadTokens
|
|
660
2224
|
})
|
|
661
2225
|
);
|
|
662
2226
|
state.rootSpan.end();
|
|
663
2227
|
this.cleanupCallState(event.callId);
|
|
664
2228
|
}
|
|
665
|
-
|
|
2229
|
+
onEmbedOperationEnd(event) {
|
|
666
2230
|
const state = this.getCallState(event.callId);
|
|
667
2231
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
668
2232
|
const { telemetry } = state;
|
|
669
2233
|
const isMany = state.operationId === "ai.embedMany";
|
|
670
2234
|
state.rootSpan.setAttributes(
|
|
671
|
-
|
|
2235
|
+
selectAttributes2(telemetry, {
|
|
672
2236
|
...isMany ? {
|
|
673
2237
|
"ai.embeddings": {
|
|
674
2238
|
output: () => event.embedding.map((e) => JSON.stringify(e))
|
|
@@ -688,7 +2252,7 @@ var OpenTelemetryIntegration = class {
|
|
|
688
2252
|
const state = this.getCallState(event.callId);
|
|
689
2253
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
690
2254
|
const { telemetry } = state;
|
|
691
|
-
const attributes =
|
|
2255
|
+
const attributes = selectAttributes2(telemetry, {
|
|
692
2256
|
...assembleOperationName({
|
|
693
2257
|
operationId: event.operationId,
|
|
694
2258
|
telemetry
|
|
@@ -703,13 +2267,13 @@ var OpenTelemetryIntegration = class {
|
|
|
703
2267
|
{ attributes },
|
|
704
2268
|
state.rootContext
|
|
705
2269
|
);
|
|
706
|
-
const embedContext =
|
|
2270
|
+
const embedContext = trace2.setSpan(state.rootContext, embedSpan);
|
|
707
2271
|
state.embedSpans.set(event.embedCallId, {
|
|
708
2272
|
span: embedSpan,
|
|
709
2273
|
context: embedContext
|
|
710
2274
|
});
|
|
711
2275
|
}
|
|
712
|
-
|
|
2276
|
+
onEmbedEnd(event) {
|
|
713
2277
|
const state = this.getCallState(event.callId);
|
|
714
2278
|
if (!state) return;
|
|
715
2279
|
const embedSpanEntry = state.embedSpans.get(event.embedCallId);
|
|
@@ -717,7 +2281,7 @@ var OpenTelemetryIntegration = class {
|
|
|
717
2281
|
const { span } = embedSpanEntry;
|
|
718
2282
|
const { telemetry } = state;
|
|
719
2283
|
span.setAttributes(
|
|
720
|
-
|
|
2284
|
+
selectAttributes2(telemetry, {
|
|
721
2285
|
"ai.embeddings": {
|
|
722
2286
|
output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
|
|
723
2287
|
},
|
|
@@ -729,22 +2293,20 @@ var OpenTelemetryIntegration = class {
|
|
|
729
2293
|
}
|
|
730
2294
|
onRerankOperationStart(event) {
|
|
731
2295
|
const telemetry = {
|
|
732
|
-
isEnabled: event.isEnabled,
|
|
733
2296
|
recordInputs: event.recordInputs,
|
|
734
2297
|
recordOutputs: event.recordOutputs,
|
|
735
|
-
functionId: event.functionId
|
|
736
|
-
metadata: event.metadata
|
|
2298
|
+
functionId: event.functionId
|
|
737
2299
|
};
|
|
738
2300
|
const settings = {
|
|
739
2301
|
maxRetries: event.maxRetries
|
|
740
2302
|
};
|
|
741
2303
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
742
2304
|
model: { provider: event.provider, modelId: event.modelId },
|
|
743
|
-
telemetry,
|
|
744
2305
|
headers: event.headers,
|
|
745
|
-
settings
|
|
2306
|
+
settings,
|
|
2307
|
+
context: void 0
|
|
746
2308
|
});
|
|
747
|
-
const attributes =
|
|
2309
|
+
const attributes = selectAttributes2(telemetry, {
|
|
748
2310
|
...assembleOperationName({
|
|
749
2311
|
operationId: event.operationId,
|
|
750
2312
|
telemetry
|
|
@@ -755,7 +2317,7 @@ var OpenTelemetryIntegration = class {
|
|
|
755
2317
|
}
|
|
756
2318
|
});
|
|
757
2319
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
758
|
-
const rootContext =
|
|
2320
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
759
2321
|
this.callStates.set(event.callId, {
|
|
760
2322
|
operationId: event.operationId,
|
|
761
2323
|
telemetry,
|
|
@@ -770,7 +2332,7 @@ var OpenTelemetryIntegration = class {
|
|
|
770
2332
|
settings
|
|
771
2333
|
});
|
|
772
2334
|
}
|
|
773
|
-
|
|
2335
|
+
onRerankOperationEnd(event) {
|
|
774
2336
|
const state = this.getCallState(event.callId);
|
|
775
2337
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
776
2338
|
state.rootSpan.end();
|
|
@@ -780,7 +2342,7 @@ var OpenTelemetryIntegration = class {
|
|
|
780
2342
|
const state = this.getCallState(event.callId);
|
|
781
2343
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
782
2344
|
const { telemetry } = state;
|
|
783
|
-
const attributes =
|
|
2345
|
+
const attributes = selectAttributes2(telemetry, {
|
|
784
2346
|
...assembleOperationName({
|
|
785
2347
|
operationId: event.operationId,
|
|
786
2348
|
telemetry
|
|
@@ -795,16 +2357,16 @@ var OpenTelemetryIntegration = class {
|
|
|
795
2357
|
{ attributes },
|
|
796
2358
|
state.rootContext
|
|
797
2359
|
);
|
|
798
|
-
const rerankContext =
|
|
2360
|
+
const rerankContext = trace2.setSpan(state.rootContext, rerankSpan);
|
|
799
2361
|
state.rerankSpan = { span: rerankSpan, context: rerankContext };
|
|
800
2362
|
}
|
|
801
|
-
|
|
2363
|
+
onRerankEnd(event) {
|
|
802
2364
|
const state = this.getCallState(event.callId);
|
|
803
2365
|
if (!(state == null ? void 0 : state.rerankSpan)) return;
|
|
804
2366
|
const { span } = state.rerankSpan;
|
|
805
2367
|
const { telemetry } = state;
|
|
806
2368
|
span.setAttributes(
|
|
807
|
-
|
|
2369
|
+
selectAttributes2(telemetry, {
|
|
808
2370
|
"ai.ranking.type": event.documentsType,
|
|
809
2371
|
"ai.ranking": {
|
|
810
2372
|
output: () => event.ranking.map((r) => JSON.stringify(r))
|
|
@@ -814,26 +2376,28 @@ var OpenTelemetryIntegration = class {
|
|
|
814
2376
|
span.end();
|
|
815
2377
|
state.rerankSpan = void 0;
|
|
816
2378
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
2379
|
+
onAbort(event) {
|
|
2380
|
+
const state = this.getCallState(event.callId);
|
|
2381
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
2382
|
+
for (const { span: toolSpan } of state.toolSpans.values()) {
|
|
2383
|
+
toolSpan.end();
|
|
822
2384
|
}
|
|
823
|
-
|
|
824
|
-
|
|
2385
|
+
state.toolSpans.clear();
|
|
2386
|
+
if (state.stepSpan) {
|
|
2387
|
+
state.stepSpan.end();
|
|
2388
|
+
state.stepSpan = void 0;
|
|
2389
|
+
state.stepContext = void 0;
|
|
825
2390
|
}
|
|
826
|
-
const
|
|
827
|
-
|
|
828
|
-
const attributes = Object.fromEntries(
|
|
829
|
-
Object.entries(
|
|
830
|
-
(_a = chunk.attributes) != null ? _a : {}
|
|
831
|
-
).filter(([, value]) => value != null)
|
|
832
|
-
);
|
|
833
|
-
state.stepSpan.addEvent(chunk.type, attributes);
|
|
834
|
-
if (Object.keys(attributes).length > 0) {
|
|
835
|
-
state.stepSpan.setAttributes(attributes);
|
|
2391
|
+
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
2392
|
+
embedSpan.end();
|
|
836
2393
|
}
|
|
2394
|
+
state.embedSpans.clear();
|
|
2395
|
+
if (state.rerankSpan) {
|
|
2396
|
+
state.rerankSpan.span.end();
|
|
2397
|
+
state.rerankSpan = void 0;
|
|
2398
|
+
}
|
|
2399
|
+
state.rootSpan.end();
|
|
2400
|
+
this.cleanupCallState(event.callId);
|
|
837
2401
|
}
|
|
838
2402
|
onError(error) {
|
|
839
2403
|
var _a;
|
|
@@ -861,8 +2425,8 @@ var OpenTelemetryIntegration = class {
|
|
|
861
2425
|
this.cleanupCallState(event.callId);
|
|
862
2426
|
}
|
|
863
2427
|
};
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
2428
|
+
export {
|
|
2429
|
+
LegacyOpenTelemetry,
|
|
2430
|
+
OpenTelemetry
|
|
2431
|
+
};
|
|
868
2432
|
//# sourceMappingURL=index.js.map
|