@ai-sdk/otel 1.0.0-beta.6 → 1.0.0-beta.60
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 +445 -0
- package/dist/index.d.ts +101 -18
- package/dist/index.js +1471 -109
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
- package/src/assemble-operation-name.ts +2 -2
- package/src/gen-ai-format-messages.ts +644 -0
- package/src/get-base-telemetry-attributes.ts +12 -15
- package/src/get-tracer.ts +1 -1
- package/src/index.ts +2 -1
- package/src/{open-telemetry-integration.ts → legacy-open-telemetry.ts} +102 -97
- package/src/mock-tracer.ts +1 -1
- package/src/noop-tracer.ts +1 -1
- package/src/open-telemetry.ts +1126 -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 +151 -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,1390 @@
|
|
|
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
|
+
return Object.fromEntries(
|
|
525
|
+
Object.entries(context4 != null ? context4 : {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.settings.context.${key}`, value])
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
function getHeaderAttributes(headers) {
|
|
529
|
+
return Object.fromEntries(
|
|
530
|
+
Object.entries(headers != null ? headers : {}).filter(([, value]) => value != null).map(([key, value]) => [`ai.request.headers.${key}`, value])
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
function getDetailedUsageAttributes(usage) {
|
|
534
|
+
var _a, _b, _c;
|
|
535
|
+
return {
|
|
536
|
+
"ai.usage.inputTokenDetails.noCacheTokens": (_a = usage.inputTokenDetails) == null ? void 0 : _a.noCacheTokens,
|
|
537
|
+
"ai.usage.outputTokenDetails.textTokens": (_b = usage.outputTokenDetails) == null ? void 0 : _b.textTokens,
|
|
538
|
+
"ai.usage.outputTokenDetails.reasoningTokens": (_c = usage.outputTokenDetails) == null ? void 0 : _c.reasoningTokens
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
function selectSupplementalAttributes(telemetry, enabledAttributes, attributes) {
|
|
542
|
+
const result = {};
|
|
543
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
544
|
+
if (!enabledAttributes[key] || value == null) {
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
Object.assign(result, selectAttributes(telemetry, value));
|
|
548
|
+
}
|
|
549
|
+
return result;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// src/open-telemetry.ts
|
|
553
|
+
var OpenTelemetry = class {
|
|
554
|
+
constructor(options = {}) {
|
|
555
|
+
this.callStates = /* @__PURE__ */ new Map();
|
|
556
|
+
var _a;
|
|
557
|
+
this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("gen_ai");
|
|
558
|
+
this.supplementalAttributes = normalizeSupplementalAttributes(options);
|
|
559
|
+
}
|
|
560
|
+
getCallState(callId) {
|
|
561
|
+
return this.callStates.get(callId);
|
|
562
|
+
}
|
|
563
|
+
cleanupCallState(callId) {
|
|
564
|
+
this.callStates.delete(callId);
|
|
565
|
+
}
|
|
566
|
+
executeTool({
|
|
567
|
+
callId,
|
|
568
|
+
toolCallId,
|
|
569
|
+
execute
|
|
570
|
+
}) {
|
|
571
|
+
var _a;
|
|
572
|
+
const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
|
|
573
|
+
if (toolSpanEntry == null) {
|
|
574
|
+
return execute();
|
|
575
|
+
}
|
|
576
|
+
return context2.with(toolSpanEntry.context, execute);
|
|
577
|
+
}
|
|
578
|
+
onStart(event) {
|
|
579
|
+
if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
|
|
580
|
+
this.onEmbedOperationStart(event);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
if (event.operationId === "ai.rerank") {
|
|
584
|
+
this.onRerankOperationStart(
|
|
585
|
+
event
|
|
586
|
+
);
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
|
|
590
|
+
this.onObjectOperationStart(
|
|
591
|
+
event
|
|
592
|
+
);
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
this.onGenerateStart(event);
|
|
596
|
+
}
|
|
597
|
+
onGenerateStart(event) {
|
|
598
|
+
var _a;
|
|
599
|
+
const telemetry = {
|
|
600
|
+
recordInputs: event.recordInputs,
|
|
601
|
+
recordOutputs: event.recordOutputs,
|
|
602
|
+
functionId: event.functionId
|
|
603
|
+
};
|
|
604
|
+
const settings = {
|
|
605
|
+
maxOutputTokens: event.maxOutputTokens,
|
|
606
|
+
temperature: event.temperature,
|
|
607
|
+
topP: event.topP,
|
|
608
|
+
topK: event.topK,
|
|
609
|
+
presencePenalty: event.presencePenalty,
|
|
610
|
+
frequencyPenalty: event.frequencyPenalty,
|
|
611
|
+
stopSequences: event.stopSequences,
|
|
612
|
+
seed: event.seed,
|
|
613
|
+
maxRetries: event.maxRetries
|
|
614
|
+
};
|
|
615
|
+
const providerName = mapProviderName(event.provider);
|
|
616
|
+
const operationName = mapOperationName(event.operationId);
|
|
617
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
618
|
+
telemetry,
|
|
619
|
+
this.supplementalAttributes,
|
|
620
|
+
{
|
|
621
|
+
runtimeContext: getRuntimeContextAttributes(
|
|
622
|
+
event.runtimeContext
|
|
623
|
+
),
|
|
624
|
+
headers: getHeaderAttributes(event.headers)
|
|
625
|
+
}
|
|
626
|
+
);
|
|
627
|
+
const attributes = selectAttributes(telemetry, {
|
|
628
|
+
"gen_ai.operation.name": operationName,
|
|
629
|
+
"gen_ai.provider.name": providerName,
|
|
630
|
+
"gen_ai.request.model": event.modelId,
|
|
631
|
+
"gen_ai.agent.name": telemetry.functionId,
|
|
632
|
+
"gen_ai.request.frequency_penalty": event.frequencyPenalty,
|
|
633
|
+
"gen_ai.request.max_tokens": event.maxOutputTokens,
|
|
634
|
+
"gen_ai.request.presence_penalty": event.presencePenalty,
|
|
635
|
+
"gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
|
|
636
|
+
"gen_ai.request.top_k": event.topK,
|
|
637
|
+
"gen_ai.request.top_p": event.topP,
|
|
638
|
+
"gen_ai.request.stop_sequences": event.stopSequences,
|
|
639
|
+
"gen_ai.request.seed": event.seed,
|
|
640
|
+
"gen_ai.system_instructions": event.system ? {
|
|
641
|
+
input: () => JSON.stringify(formatSystemInstructions(event.system))
|
|
642
|
+
} : void 0,
|
|
643
|
+
"gen_ai.input.messages": {
|
|
644
|
+
input: () => JSON.stringify(
|
|
645
|
+
formatModelMessages({
|
|
646
|
+
prompt: void 0,
|
|
647
|
+
messages: event.messages
|
|
648
|
+
})
|
|
649
|
+
)
|
|
650
|
+
},
|
|
651
|
+
...baseSupplementalAttributes
|
|
652
|
+
});
|
|
653
|
+
const spanName = `${operationName} ${event.modelId}`;
|
|
654
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
655
|
+
attributes,
|
|
656
|
+
kind: SpanKind.INTERNAL
|
|
657
|
+
});
|
|
658
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
659
|
+
this.callStates.set(event.callId, {
|
|
660
|
+
operationId: event.operationId,
|
|
661
|
+
telemetry,
|
|
662
|
+
rootSpan,
|
|
663
|
+
rootContext,
|
|
664
|
+
stepSpan: void 0,
|
|
665
|
+
stepContext: void 0,
|
|
666
|
+
inferenceSpan: void 0,
|
|
667
|
+
inferenceContext: void 0,
|
|
668
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
669
|
+
rerankSpan: void 0,
|
|
670
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
671
|
+
settings,
|
|
672
|
+
provider: event.provider,
|
|
673
|
+
modelId: event.modelId,
|
|
674
|
+
baseSupplementalAttributes
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
onObjectOperationStart(event) {
|
|
678
|
+
var _a;
|
|
679
|
+
const telemetry = {
|
|
680
|
+
recordInputs: event.recordInputs,
|
|
681
|
+
recordOutputs: event.recordOutputs,
|
|
682
|
+
functionId: event.functionId
|
|
683
|
+
};
|
|
684
|
+
const settings = {
|
|
685
|
+
maxOutputTokens: event.maxOutputTokens,
|
|
686
|
+
temperature: event.temperature,
|
|
687
|
+
topP: event.topP,
|
|
688
|
+
topK: event.topK,
|
|
689
|
+
presencePenalty: event.presencePenalty,
|
|
690
|
+
frequencyPenalty: event.frequencyPenalty,
|
|
691
|
+
seed: event.seed,
|
|
692
|
+
maxRetries: event.maxRetries
|
|
693
|
+
};
|
|
694
|
+
const providerName = mapProviderName(event.provider);
|
|
695
|
+
const operationName = mapOperationName(event.operationId);
|
|
696
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
697
|
+
telemetry,
|
|
698
|
+
this.supplementalAttributes,
|
|
699
|
+
{
|
|
700
|
+
headers: getHeaderAttributes(event.headers)
|
|
701
|
+
}
|
|
702
|
+
);
|
|
703
|
+
const attributes = selectAttributes(telemetry, {
|
|
704
|
+
"gen_ai.operation.name": operationName,
|
|
705
|
+
"gen_ai.provider.name": providerName,
|
|
706
|
+
"gen_ai.request.model": event.modelId,
|
|
707
|
+
"gen_ai.agent.name": telemetry.functionId,
|
|
708
|
+
"gen_ai.output.type": "json",
|
|
709
|
+
"gen_ai.request.frequency_penalty": event.frequencyPenalty,
|
|
710
|
+
"gen_ai.request.max_tokens": event.maxOutputTokens,
|
|
711
|
+
"gen_ai.request.presence_penalty": event.presencePenalty,
|
|
712
|
+
"gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
|
|
713
|
+
"gen_ai.request.top_k": event.topK,
|
|
714
|
+
"gen_ai.request.top_p": event.topP,
|
|
715
|
+
"gen_ai.request.seed": event.seed,
|
|
716
|
+
"gen_ai.system_instructions": event.system ? {
|
|
717
|
+
input: () => JSON.stringify(formatSystemInstructions(event.system))
|
|
718
|
+
} : void 0,
|
|
719
|
+
"gen_ai.input.messages": {
|
|
720
|
+
input: () => JSON.stringify(
|
|
721
|
+
formatModelMessages({
|
|
722
|
+
prompt: event.prompt,
|
|
723
|
+
messages: event.messages
|
|
724
|
+
})
|
|
725
|
+
)
|
|
726
|
+
},
|
|
727
|
+
...baseSupplementalAttributes,
|
|
728
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
729
|
+
schema: {
|
|
730
|
+
"ai.schema": event.schema ? { input: () => JSON.stringify(event.schema) } : void 0,
|
|
731
|
+
"ai.schema.name": event.schemaName,
|
|
732
|
+
"ai.schema.description": event.schemaDescription,
|
|
733
|
+
"ai.settings.output": event.output
|
|
734
|
+
}
|
|
735
|
+
})
|
|
736
|
+
});
|
|
737
|
+
const spanName = `${operationName} ${event.modelId}`;
|
|
738
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
739
|
+
attributes,
|
|
740
|
+
kind: SpanKind.INTERNAL
|
|
741
|
+
});
|
|
742
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
743
|
+
this.callStates.set(event.callId, {
|
|
744
|
+
operationId: event.operationId,
|
|
745
|
+
telemetry,
|
|
746
|
+
rootSpan,
|
|
747
|
+
rootContext,
|
|
748
|
+
stepSpan: void 0,
|
|
749
|
+
stepContext: void 0,
|
|
750
|
+
inferenceSpan: void 0,
|
|
751
|
+
inferenceContext: void 0,
|
|
752
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
753
|
+
rerankSpan: void 0,
|
|
754
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
755
|
+
settings,
|
|
756
|
+
provider: event.provider,
|
|
757
|
+
modelId: event.modelId,
|
|
758
|
+
baseSupplementalAttributes
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
/** @deprecated */
|
|
762
|
+
onObjectStepStart(event) {
|
|
763
|
+
var _a;
|
|
764
|
+
const state = this.getCallState(event.callId);
|
|
765
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
766
|
+
const { telemetry } = state;
|
|
767
|
+
const providerName = mapProviderName(event.provider);
|
|
768
|
+
const attributes = selectAttributes(telemetry, {
|
|
769
|
+
"gen_ai.operation.name": "chat",
|
|
770
|
+
"gen_ai.provider.name": providerName,
|
|
771
|
+
"gen_ai.request.model": event.modelId,
|
|
772
|
+
"gen_ai.output.type": "json",
|
|
773
|
+
"gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
|
|
774
|
+
"gen_ai.request.max_tokens": state.settings.maxOutputTokens,
|
|
775
|
+
"gen_ai.request.presence_penalty": state.settings.presencePenalty,
|
|
776
|
+
"gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
|
|
777
|
+
"gen_ai.request.top_k": state.settings.topK,
|
|
778
|
+
"gen_ai.request.top_p": state.settings.topP,
|
|
779
|
+
"gen_ai.input.messages": {
|
|
780
|
+
input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
|
|
781
|
+
},
|
|
782
|
+
...state.baseSupplementalAttributes
|
|
783
|
+
});
|
|
784
|
+
const spanName = `chat ${event.modelId}`;
|
|
785
|
+
state.inferenceSpan = this.tracer.startSpan(
|
|
786
|
+
spanName,
|
|
787
|
+
{ attributes, kind: SpanKind.CLIENT },
|
|
788
|
+
state.rootContext
|
|
789
|
+
);
|
|
790
|
+
state.inferenceContext = trace.setSpan(
|
|
791
|
+
state.rootContext,
|
|
792
|
+
state.inferenceSpan
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
/** @deprecated */
|
|
796
|
+
onObjectStepFinish(event) {
|
|
797
|
+
const state = this.getCallState(event.callId);
|
|
798
|
+
if (!(state == null ? void 0 : state.inferenceSpan)) return;
|
|
799
|
+
const { telemetry } = state;
|
|
800
|
+
state.inferenceSpan.setAttributes(
|
|
801
|
+
selectAttributes(telemetry, {
|
|
802
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
803
|
+
"gen_ai.response.id": event.response.id,
|
|
804
|
+
"gen_ai.response.model": event.response.modelId,
|
|
805
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
806
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
807
|
+
"gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
|
|
808
|
+
"gen_ai.output.messages": {
|
|
809
|
+
output: () => {
|
|
810
|
+
try {
|
|
811
|
+
return JSON.stringify(
|
|
812
|
+
formatObjectOutputMessages({
|
|
813
|
+
objectText: event.objectText,
|
|
814
|
+
finishReason: event.finishReason
|
|
815
|
+
})
|
|
816
|
+
);
|
|
817
|
+
} catch (e) {
|
|
818
|
+
return event.objectText;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
},
|
|
822
|
+
...selectSupplementalAttributes(
|
|
823
|
+
telemetry,
|
|
824
|
+
this.supplementalAttributes,
|
|
825
|
+
{
|
|
826
|
+
providerMetadata: {
|
|
827
|
+
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
|
|
828
|
+
},
|
|
829
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
830
|
+
}
|
|
831
|
+
)
|
|
832
|
+
})
|
|
833
|
+
);
|
|
834
|
+
state.inferenceSpan.end();
|
|
835
|
+
state.inferenceSpan = void 0;
|
|
836
|
+
state.inferenceContext = void 0;
|
|
837
|
+
}
|
|
838
|
+
onEmbedOperationStart(event) {
|
|
839
|
+
const telemetry = {
|
|
840
|
+
recordInputs: event.recordInputs,
|
|
841
|
+
recordOutputs: event.recordOutputs,
|
|
842
|
+
functionId: event.functionId
|
|
843
|
+
};
|
|
844
|
+
const providerName = mapProviderName(event.provider);
|
|
845
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
846
|
+
telemetry,
|
|
847
|
+
this.supplementalAttributes,
|
|
848
|
+
{
|
|
849
|
+
headers: getHeaderAttributes(event.headers)
|
|
850
|
+
}
|
|
851
|
+
);
|
|
852
|
+
const value = event.value;
|
|
853
|
+
const isMany = event.operationId === "ai.embedMany";
|
|
854
|
+
const attributes = selectAttributes(telemetry, {
|
|
855
|
+
"gen_ai.operation.name": "embeddings",
|
|
856
|
+
"gen_ai.provider.name": providerName,
|
|
857
|
+
"gen_ai.request.model": event.modelId,
|
|
858
|
+
...baseSupplementalAttributes,
|
|
859
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
860
|
+
embedding: isMany ? {
|
|
861
|
+
"ai.values": {
|
|
862
|
+
input: () => value.map((v) => JSON.stringify(v))
|
|
863
|
+
}
|
|
864
|
+
} : {
|
|
865
|
+
"ai.value": {
|
|
866
|
+
input: () => JSON.stringify(value)
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
})
|
|
870
|
+
});
|
|
871
|
+
const spanName = `embeddings ${event.modelId}`;
|
|
872
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
873
|
+
attributes,
|
|
874
|
+
kind: SpanKind.CLIENT
|
|
875
|
+
});
|
|
876
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
877
|
+
this.callStates.set(event.callId, {
|
|
878
|
+
operationId: event.operationId,
|
|
879
|
+
telemetry,
|
|
880
|
+
rootSpan,
|
|
881
|
+
rootContext,
|
|
882
|
+
stepSpan: void 0,
|
|
883
|
+
stepContext: void 0,
|
|
884
|
+
inferenceSpan: void 0,
|
|
885
|
+
inferenceContext: void 0,
|
|
886
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
887
|
+
rerankSpan: void 0,
|
|
888
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
889
|
+
settings: { maxRetries: event.maxRetries },
|
|
890
|
+
provider: event.provider,
|
|
891
|
+
modelId: event.modelId,
|
|
892
|
+
baseSupplementalAttributes
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
onStepStart(event) {
|
|
896
|
+
const state = this.getCallState(event.callId);
|
|
897
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
898
|
+
const { telemetry } = state;
|
|
899
|
+
const stepAttributes = selectAttributes(telemetry, {
|
|
900
|
+
"gen_ai.operation.name": "agent_step",
|
|
901
|
+
...state.baseSupplementalAttributes,
|
|
902
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
903
|
+
toolChoice: {
|
|
904
|
+
"ai.prompt.toolChoice": {
|
|
905
|
+
input: () => event.stepToolChoice != null ? JSON.stringify(event.stepToolChoice) : void 0
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
})
|
|
909
|
+
});
|
|
910
|
+
state.stepSpan = this.tracer.startSpan(
|
|
911
|
+
`step ${event.steps.length + 1}`,
|
|
912
|
+
{ attributes: stepAttributes, kind: SpanKind.INTERNAL },
|
|
913
|
+
state.rootContext
|
|
914
|
+
);
|
|
915
|
+
state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
|
|
916
|
+
}
|
|
917
|
+
onLanguageModelCallStart(event) {
|
|
918
|
+
var _a;
|
|
919
|
+
const state = this.getCallState(event.callId);
|
|
920
|
+
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
921
|
+
const { telemetry } = state;
|
|
922
|
+
const providerName = mapProviderName(event.provider);
|
|
923
|
+
const inferenceAttributes = selectAttributes(telemetry, {
|
|
924
|
+
"gen_ai.operation.name": "chat",
|
|
925
|
+
"gen_ai.provider.name": providerName,
|
|
926
|
+
"gen_ai.request.model": event.modelId,
|
|
927
|
+
"gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
|
|
928
|
+
"gen_ai.request.max_tokens": state.settings.maxOutputTokens,
|
|
929
|
+
"gen_ai.request.presence_penalty": state.settings.presencePenalty,
|
|
930
|
+
"gen_ai.request.stop_sequences": state.settings.stopSequences,
|
|
931
|
+
"gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
|
|
932
|
+
"gen_ai.request.top_k": state.settings.topK,
|
|
933
|
+
"gen_ai.request.top_p": state.settings.topP,
|
|
934
|
+
"gen_ai.input.messages": {
|
|
935
|
+
input: () => {
|
|
936
|
+
const formattedMessages = formatModelMessages({
|
|
937
|
+
prompt: void 0,
|
|
938
|
+
messages: event.messages
|
|
939
|
+
});
|
|
940
|
+
return formattedMessages.length > 0 ? JSON.stringify(formattedMessages) : void 0;
|
|
941
|
+
}
|
|
942
|
+
},
|
|
943
|
+
"gen_ai.tool.definitions": {
|
|
944
|
+
input: () => event.tools ? JSON.stringify(event.tools) : void 0
|
|
945
|
+
}
|
|
946
|
+
});
|
|
947
|
+
state.inferenceSpan = this.tracer.startSpan(
|
|
948
|
+
`chat ${event.modelId}`,
|
|
949
|
+
{ attributes: inferenceAttributes, kind: SpanKind.CLIENT },
|
|
950
|
+
state.stepContext
|
|
951
|
+
);
|
|
952
|
+
state.inferenceContext = trace.setSpan(
|
|
953
|
+
state.stepContext,
|
|
954
|
+
state.inferenceSpan
|
|
955
|
+
);
|
|
956
|
+
}
|
|
957
|
+
onLanguageModelCallEnd(event) {
|
|
958
|
+
var _a, _b, _c;
|
|
959
|
+
const state = this.getCallState(event.callId);
|
|
960
|
+
if (!(state == null ? void 0 : state.inferenceSpan)) return;
|
|
961
|
+
const { telemetry } = state;
|
|
962
|
+
state.inferenceSpan.setAttributes(
|
|
963
|
+
selectAttributes(telemetry, {
|
|
964
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
965
|
+
"gen_ai.response.id": event.responseId,
|
|
966
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
967
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
968
|
+
"gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.usage.cachedInputTokens,
|
|
969
|
+
"gen_ai.usage.cache_creation.input_tokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
|
|
970
|
+
"gen_ai.output.messages": {
|
|
971
|
+
output: () => JSON.stringify(
|
|
972
|
+
formatOutputMessages({
|
|
973
|
+
text: event.content.filter((p) => p.type === "text").map((p) => p.text).join("") || void 0,
|
|
974
|
+
reasoning: event.content.filter((p) => p.type === "reasoning"),
|
|
975
|
+
toolCalls: event.content.filter((p) => p.type === "tool-call"),
|
|
976
|
+
files: event.content.filter((p) => p.type === "file").map((p) => p.file),
|
|
977
|
+
finishReason: event.finishReason
|
|
978
|
+
})
|
|
979
|
+
)
|
|
980
|
+
},
|
|
981
|
+
...selectSupplementalAttributes(
|
|
982
|
+
telemetry,
|
|
983
|
+
this.supplementalAttributes,
|
|
984
|
+
{
|
|
985
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
986
|
+
}
|
|
987
|
+
)
|
|
988
|
+
})
|
|
989
|
+
);
|
|
990
|
+
state.inferenceSpan.end();
|
|
991
|
+
state.inferenceSpan = void 0;
|
|
992
|
+
state.inferenceContext = void 0;
|
|
993
|
+
}
|
|
994
|
+
onToolExecutionStart(event) {
|
|
995
|
+
const state = this.getCallState(event.callId);
|
|
996
|
+
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
997
|
+
const { telemetry } = state;
|
|
998
|
+
const { toolCall } = event;
|
|
999
|
+
const attributes = selectAttributes(telemetry, {
|
|
1000
|
+
"gen_ai.operation.name": "execute_tool",
|
|
1001
|
+
"gen_ai.tool.name": toolCall.toolName,
|
|
1002
|
+
"gen_ai.tool.call.id": toolCall.toolCallId,
|
|
1003
|
+
"gen_ai.tool.type": "function",
|
|
1004
|
+
"gen_ai.tool.call.arguments": {
|
|
1005
|
+
input: () => JSON.stringify(toolCall.input)
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
const spanName = `execute_tool ${toolCall.toolName}`;
|
|
1009
|
+
const toolSpan = this.tracer.startSpan(
|
|
1010
|
+
spanName,
|
|
1011
|
+
{ attributes, kind: SpanKind.INTERNAL },
|
|
1012
|
+
state.stepContext
|
|
1013
|
+
);
|
|
1014
|
+
const toolContext = trace.setSpan(state.stepContext, toolSpan);
|
|
1015
|
+
state.toolSpans.set(toolCall.toolCallId, {
|
|
1016
|
+
span: toolSpan,
|
|
1017
|
+
context: toolContext
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
onToolExecutionEnd(event) {
|
|
1021
|
+
const state = this.getCallState(event.callId);
|
|
1022
|
+
if (!state) return;
|
|
1023
|
+
const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
|
|
1024
|
+
if (!toolSpanEntry) return;
|
|
1025
|
+
const { span } = toolSpanEntry;
|
|
1026
|
+
const { telemetry } = state;
|
|
1027
|
+
const { toolOutput } = event;
|
|
1028
|
+
if (toolOutput.type === "tool-result") {
|
|
1029
|
+
try {
|
|
1030
|
+
span.setAttributes(
|
|
1031
|
+
selectAttributes(telemetry, {
|
|
1032
|
+
"gen_ai.tool.call.result": {
|
|
1033
|
+
output: () => JSON.stringify(toolOutput.output)
|
|
1034
|
+
}
|
|
1035
|
+
})
|
|
1036
|
+
);
|
|
1037
|
+
} catch (e) {
|
|
1038
|
+
}
|
|
1039
|
+
} else {
|
|
1040
|
+
recordErrorOnSpan(span, toolOutput.error);
|
|
1041
|
+
}
|
|
1042
|
+
span.end();
|
|
1043
|
+
state.toolSpans.delete(event.toolCall.toolCallId);
|
|
1044
|
+
}
|
|
1045
|
+
onStepFinish(event) {
|
|
1046
|
+
const state = this.getCallState(event.callId);
|
|
1047
|
+
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
1048
|
+
const { telemetry } = state;
|
|
1049
|
+
state.stepSpan.setAttributes(
|
|
1050
|
+
selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1051
|
+
providerMetadata: {
|
|
1052
|
+
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
|
|
1053
|
+
},
|
|
1054
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
1055
|
+
})
|
|
1056
|
+
);
|
|
1057
|
+
state.stepSpan.end();
|
|
1058
|
+
state.stepSpan = void 0;
|
|
1059
|
+
state.stepContext = void 0;
|
|
1060
|
+
}
|
|
1061
|
+
onFinish(event) {
|
|
1062
|
+
const state = this.getCallState(event.callId);
|
|
1063
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1064
|
+
if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
|
|
1065
|
+
this.onEmbedOperationFinish(event);
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
if (state.operationId === "ai.rerank") {
|
|
1069
|
+
this.onRerankOperationFinish(event);
|
|
1070
|
+
return;
|
|
1071
|
+
}
|
|
1072
|
+
if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
|
|
1073
|
+
this.onObjectOperationFinish(event);
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
this.onGenerateFinish(event);
|
|
1077
|
+
}
|
|
1078
|
+
onGenerateFinish(event) {
|
|
1079
|
+
var _a, _b, _c;
|
|
1080
|
+
const state = this.getCallState(event.callId);
|
|
1081
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1082
|
+
const { telemetry } = state;
|
|
1083
|
+
state.rootSpan.setAttributes(
|
|
1084
|
+
selectAttributes(telemetry, {
|
|
1085
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
1086
|
+
"gen_ai.usage.input_tokens": event.totalUsage.inputTokens,
|
|
1087
|
+
"gen_ai.usage.output_tokens": event.totalUsage.outputTokens,
|
|
1088
|
+
"gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.totalUsage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.totalUsage.cachedInputTokens,
|
|
1089
|
+
"gen_ai.usage.cache_creation.input_tokens": (_c = event.totalUsage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
|
|
1090
|
+
"gen_ai.output.messages": {
|
|
1091
|
+
output: () => {
|
|
1092
|
+
var _a2;
|
|
1093
|
+
return JSON.stringify(
|
|
1094
|
+
formatOutputMessages({
|
|
1095
|
+
text: (_a2 = event.text) != null ? _a2 : void 0,
|
|
1096
|
+
reasoning: event.reasoning,
|
|
1097
|
+
toolCalls: event.toolCalls,
|
|
1098
|
+
files: event.files,
|
|
1099
|
+
finishReason: event.finishReason
|
|
1100
|
+
})
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
},
|
|
1104
|
+
...selectSupplementalAttributes(
|
|
1105
|
+
telemetry,
|
|
1106
|
+
this.supplementalAttributes,
|
|
1107
|
+
{
|
|
1108
|
+
providerMetadata: {
|
|
1109
|
+
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
|
|
1110
|
+
},
|
|
1111
|
+
usage: getDetailedUsageAttributes(event.totalUsage)
|
|
1112
|
+
}
|
|
1113
|
+
)
|
|
1114
|
+
})
|
|
1115
|
+
);
|
|
1116
|
+
state.rootSpan.end();
|
|
1117
|
+
this.cleanupCallState(event.callId);
|
|
1118
|
+
}
|
|
1119
|
+
onObjectOperationFinish(event) {
|
|
1120
|
+
const state = this.getCallState(event.callId);
|
|
1121
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1122
|
+
const { telemetry } = state;
|
|
1123
|
+
state.rootSpan.setAttributes(
|
|
1124
|
+
selectAttributes(telemetry, {
|
|
1125
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
1126
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
1127
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
1128
|
+
"gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
|
|
1129
|
+
"gen_ai.output.messages": {
|
|
1130
|
+
output: () => event.object != null ? JSON.stringify(
|
|
1131
|
+
formatObjectOutputMessages({
|
|
1132
|
+
objectText: JSON.stringify(event.object),
|
|
1133
|
+
finishReason: event.finishReason
|
|
1134
|
+
})
|
|
1135
|
+
) : void 0
|
|
1136
|
+
},
|
|
1137
|
+
...selectSupplementalAttributes(
|
|
1138
|
+
telemetry,
|
|
1139
|
+
this.supplementalAttributes,
|
|
1140
|
+
{
|
|
1141
|
+
providerMetadata: {
|
|
1142
|
+
"ai.response.providerMetadata": event.providerMetadata ? JSON.stringify(event.providerMetadata) : void 0
|
|
1143
|
+
},
|
|
1144
|
+
usage: getDetailedUsageAttributes(event.usage)
|
|
1145
|
+
}
|
|
1146
|
+
)
|
|
1147
|
+
})
|
|
1148
|
+
);
|
|
1149
|
+
state.rootSpan.end();
|
|
1150
|
+
this.cleanupCallState(event.callId);
|
|
1151
|
+
}
|
|
1152
|
+
onEmbedOperationFinish(event) {
|
|
1153
|
+
const state = this.getCallState(event.callId);
|
|
1154
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1155
|
+
const { telemetry } = state;
|
|
1156
|
+
const isMany = state.operationId === "ai.embedMany";
|
|
1157
|
+
state.rootSpan.setAttributes(
|
|
1158
|
+
selectAttributes(telemetry, {
|
|
1159
|
+
"gen_ai.usage.input_tokens": event.usage.tokens,
|
|
1160
|
+
...selectSupplementalAttributes(
|
|
1161
|
+
telemetry,
|
|
1162
|
+
this.supplementalAttributes,
|
|
1163
|
+
{
|
|
1164
|
+
embedding: isMany ? {
|
|
1165
|
+
"ai.embeddings": {
|
|
1166
|
+
output: () => event.embedding.map(
|
|
1167
|
+
(e) => JSON.stringify(e)
|
|
1168
|
+
)
|
|
1169
|
+
}
|
|
1170
|
+
} : {
|
|
1171
|
+
"ai.embedding": {
|
|
1172
|
+
output: () => JSON.stringify(event.embedding)
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
)
|
|
1177
|
+
})
|
|
1178
|
+
);
|
|
1179
|
+
state.rootSpan.end();
|
|
1180
|
+
this.cleanupCallState(event.callId);
|
|
1181
|
+
}
|
|
1182
|
+
onEmbedStart(event) {
|
|
1183
|
+
const state = this.getCallState(event.callId);
|
|
1184
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
1185
|
+
const { telemetry } = state;
|
|
1186
|
+
const providerName = mapProviderName(state.provider);
|
|
1187
|
+
const attributes = selectAttributes(telemetry, {
|
|
1188
|
+
"gen_ai.operation.name": "embeddings",
|
|
1189
|
+
"gen_ai.provider.name": providerName,
|
|
1190
|
+
"gen_ai.request.model": state.modelId,
|
|
1191
|
+
...state.baseSupplementalAttributes,
|
|
1192
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1193
|
+
embedding: {
|
|
1194
|
+
"ai.values": {
|
|
1195
|
+
input: () => event.values.map((v) => JSON.stringify(v))
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
})
|
|
1199
|
+
});
|
|
1200
|
+
const spanName = `embeddings ${state.modelId}`;
|
|
1201
|
+
const embedSpan = this.tracer.startSpan(
|
|
1202
|
+
spanName,
|
|
1203
|
+
{ attributes, kind: SpanKind.CLIENT },
|
|
1204
|
+
state.rootContext
|
|
1205
|
+
);
|
|
1206
|
+
const embedContext = trace.setSpan(state.rootContext, embedSpan);
|
|
1207
|
+
state.embedSpans.set(event.embedCallId, {
|
|
1208
|
+
span: embedSpan,
|
|
1209
|
+
context: embedContext
|
|
1210
|
+
});
|
|
1211
|
+
}
|
|
1212
|
+
onEmbedFinish(event) {
|
|
1213
|
+
const state = this.getCallState(event.callId);
|
|
1214
|
+
if (!state) return;
|
|
1215
|
+
const embedSpanEntry = state.embedSpans.get(event.embedCallId);
|
|
1216
|
+
if (!embedSpanEntry) return;
|
|
1217
|
+
const { span } = embedSpanEntry;
|
|
1218
|
+
const { telemetry } = state;
|
|
1219
|
+
span.setAttributes(
|
|
1220
|
+
selectAttributes(telemetry, {
|
|
1221
|
+
"gen_ai.usage.input_tokens": event.usage.tokens,
|
|
1222
|
+
...selectSupplementalAttributes(
|
|
1223
|
+
telemetry,
|
|
1224
|
+
this.supplementalAttributes,
|
|
1225
|
+
{
|
|
1226
|
+
embedding: {
|
|
1227
|
+
"ai.embeddings": {
|
|
1228
|
+
output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
)
|
|
1233
|
+
})
|
|
1234
|
+
);
|
|
1235
|
+
span.end();
|
|
1236
|
+
state.embedSpans.delete(event.embedCallId);
|
|
1237
|
+
}
|
|
1238
|
+
onRerankOperationStart(event) {
|
|
1239
|
+
const telemetry = {
|
|
1240
|
+
recordInputs: event.recordInputs,
|
|
1241
|
+
recordOutputs: event.recordOutputs,
|
|
1242
|
+
functionId: event.functionId
|
|
1243
|
+
};
|
|
1244
|
+
const providerName = mapProviderName(event.provider);
|
|
1245
|
+
const baseSupplementalAttributes = selectSupplementalAttributes(
|
|
1246
|
+
telemetry,
|
|
1247
|
+
this.supplementalAttributes,
|
|
1248
|
+
{
|
|
1249
|
+
headers: getHeaderAttributes(event.headers)
|
|
1250
|
+
}
|
|
1251
|
+
);
|
|
1252
|
+
const attributes = selectAttributes(telemetry, {
|
|
1253
|
+
"gen_ai.operation.name": "rerank",
|
|
1254
|
+
"gen_ai.provider.name": providerName,
|
|
1255
|
+
"gen_ai.request.model": event.modelId,
|
|
1256
|
+
...baseSupplementalAttributes,
|
|
1257
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1258
|
+
reranking: {
|
|
1259
|
+
"ai.documents": {
|
|
1260
|
+
input: () => event.documents.map((d) => JSON.stringify(d))
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
})
|
|
1264
|
+
});
|
|
1265
|
+
const spanName = `rerank ${event.modelId}`;
|
|
1266
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
1267
|
+
attributes,
|
|
1268
|
+
kind: SpanKind.CLIENT
|
|
1269
|
+
});
|
|
1270
|
+
const rootContext = trace.setSpan(context2.active(), rootSpan);
|
|
1271
|
+
this.callStates.set(event.callId, {
|
|
1272
|
+
operationId: event.operationId,
|
|
1273
|
+
telemetry,
|
|
1274
|
+
rootSpan,
|
|
1275
|
+
rootContext,
|
|
1276
|
+
stepSpan: void 0,
|
|
1277
|
+
stepContext: void 0,
|
|
1278
|
+
inferenceSpan: void 0,
|
|
1279
|
+
inferenceContext: void 0,
|
|
1280
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
1281
|
+
rerankSpan: void 0,
|
|
1282
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
1283
|
+
settings: { maxRetries: event.maxRetries },
|
|
1284
|
+
provider: event.provider,
|
|
1285
|
+
modelId: event.modelId,
|
|
1286
|
+
baseSupplementalAttributes
|
|
1287
|
+
});
|
|
1288
|
+
}
|
|
1289
|
+
onRerankOperationFinish(event) {
|
|
1290
|
+
const state = this.getCallState(event.callId);
|
|
1291
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1292
|
+
state.rootSpan.end();
|
|
1293
|
+
this.cleanupCallState(event.callId);
|
|
1294
|
+
}
|
|
1295
|
+
onRerankStart(event) {
|
|
1296
|
+
const state = this.getCallState(event.callId);
|
|
1297
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
1298
|
+
const { telemetry } = state;
|
|
1299
|
+
const providerName = mapProviderName(state.provider);
|
|
1300
|
+
const attributes = selectAttributes(telemetry, {
|
|
1301
|
+
"gen_ai.operation.name": "rerank",
|
|
1302
|
+
"gen_ai.provider.name": providerName,
|
|
1303
|
+
"gen_ai.request.model": state.modelId,
|
|
1304
|
+
...state.baseSupplementalAttributes,
|
|
1305
|
+
...selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1306
|
+
reranking: {
|
|
1307
|
+
"ai.documents": {
|
|
1308
|
+
input: () => event.documents.map((d) => JSON.stringify(d))
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
})
|
|
1312
|
+
});
|
|
1313
|
+
const spanName = `rerank ${state.modelId}`;
|
|
1314
|
+
const rerankSpan = this.tracer.startSpan(
|
|
1315
|
+
spanName,
|
|
1316
|
+
{ attributes, kind: SpanKind.CLIENT },
|
|
1317
|
+
state.rootContext
|
|
1318
|
+
);
|
|
1319
|
+
const rerankContext = trace.setSpan(state.rootContext, rerankSpan);
|
|
1320
|
+
state.rerankSpan = { span: rerankSpan, context: rerankContext };
|
|
1321
|
+
}
|
|
1322
|
+
onRerankFinish(event) {
|
|
1323
|
+
const state = this.getCallState(event.callId);
|
|
1324
|
+
if (!(state == null ? void 0 : state.rerankSpan)) return;
|
|
1325
|
+
const { span } = state.rerankSpan;
|
|
1326
|
+
const { telemetry } = state;
|
|
1327
|
+
span.setAttributes(
|
|
1328
|
+
selectSupplementalAttributes(telemetry, this.supplementalAttributes, {
|
|
1329
|
+
reranking: {
|
|
1330
|
+
"ai.ranking.type": event.documentsType,
|
|
1331
|
+
"ai.ranking": {
|
|
1332
|
+
output: () => event.ranking.map((r) => JSON.stringify(r))
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
})
|
|
1336
|
+
);
|
|
1337
|
+
span.end();
|
|
1338
|
+
state.rerankSpan = void 0;
|
|
1339
|
+
}
|
|
1340
|
+
onChunk(_event) {
|
|
1341
|
+
}
|
|
1342
|
+
onError(error) {
|
|
1343
|
+
var _a;
|
|
1344
|
+
const event = error;
|
|
1345
|
+
if (!(event == null ? void 0 : event.callId)) return;
|
|
1346
|
+
const state = this.getCallState(event.callId);
|
|
1347
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1348
|
+
const actualError = (_a = event.error) != null ? _a : error;
|
|
1349
|
+
for (const { span: toolSpan } of state.toolSpans.values()) {
|
|
1350
|
+
recordErrorOnSpan(toolSpan, actualError);
|
|
1351
|
+
toolSpan.end();
|
|
1352
|
+
}
|
|
1353
|
+
state.toolSpans.clear();
|
|
1354
|
+
if (state.inferenceSpan) {
|
|
1355
|
+
recordErrorOnSpan(state.inferenceSpan, actualError);
|
|
1356
|
+
state.inferenceSpan.end();
|
|
1357
|
+
state.inferenceSpan = void 0;
|
|
1358
|
+
state.inferenceContext = void 0;
|
|
1359
|
+
}
|
|
1360
|
+
if (state.stepSpan) {
|
|
1361
|
+
recordErrorOnSpan(state.stepSpan, actualError);
|
|
1362
|
+
state.stepSpan.end();
|
|
1363
|
+
state.stepSpan = void 0;
|
|
1364
|
+
state.stepContext = void 0;
|
|
1365
|
+
}
|
|
1366
|
+
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
1367
|
+
recordErrorOnSpan(embedSpan, actualError);
|
|
1368
|
+
embedSpan.end();
|
|
1369
|
+
}
|
|
1370
|
+
state.embedSpans.clear();
|
|
1371
|
+
if (state.rerankSpan) {
|
|
1372
|
+
recordErrorOnSpan(state.rerankSpan.span, actualError);
|
|
1373
|
+
state.rerankSpan.span.end();
|
|
1374
|
+
state.rerankSpan = void 0;
|
|
1375
|
+
}
|
|
1376
|
+
recordErrorOnSpan(state.rootSpan, actualError);
|
|
1377
|
+
state.rootSpan.end();
|
|
1378
|
+
this.cleanupCallState(event.callId);
|
|
15
1379
|
}
|
|
16
|
-
return to;
|
|
17
1380
|
};
|
|
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
1381
|
|
|
27
|
-
// src/open-telemetry
|
|
28
|
-
|
|
1382
|
+
// src/legacy-open-telemetry.ts
|
|
1383
|
+
import {
|
|
1384
|
+
context as context3,
|
|
1385
|
+
SpanStatusCode as SpanStatusCode2,
|
|
1386
|
+
trace as trace2
|
|
1387
|
+
} from "@opentelemetry/api";
|
|
29
1388
|
|
|
30
1389
|
// src/assemble-operation-name.ts
|
|
31
1390
|
function assembleOperationName({
|
|
@@ -46,10 +1405,9 @@ function assembleOperationName({
|
|
|
46
1405
|
function getBaseTelemetryAttributes({
|
|
47
1406
|
model,
|
|
48
1407
|
settings,
|
|
49
|
-
|
|
50
|
-
|
|
1408
|
+
headers,
|
|
1409
|
+
context: context4
|
|
51
1410
|
}) {
|
|
52
|
-
var _a;
|
|
53
1411
|
return {
|
|
54
1412
|
"ai.model.provider": model.provider,
|
|
55
1413
|
"ai.model.id": model.modelId,
|
|
@@ -58,16 +1416,13 @@ function getBaseTelemetryAttributes({
|
|
|
58
1416
|
attributes[`ai.settings.${key}`] = value;
|
|
59
1417
|
return attributes;
|
|
60
1418
|
}, {}),
|
|
61
|
-
// add
|
|
62
|
-
...Object.entries(
|
|
63
|
-
(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
},
|
|
69
|
-
{}
|
|
70
|
-
),
|
|
1419
|
+
// add context as attributes:
|
|
1420
|
+
...Object.entries(context4 != null ? context4 : {}).reduce((attributes, [key, value]) => {
|
|
1421
|
+
if (value != void 0) {
|
|
1422
|
+
attributes[`ai.settings.context.${key}`] = value;
|
|
1423
|
+
}
|
|
1424
|
+
return attributes;
|
|
1425
|
+
}, {}),
|
|
71
1426
|
// request headers
|
|
72
1427
|
...Object.entries(headers != null ? headers : {}).reduce((attributes, [key, value]) => {
|
|
73
1428
|
if (value !== void 0) {
|
|
@@ -79,7 +1434,7 @@ function getBaseTelemetryAttributes({
|
|
|
79
1434
|
}
|
|
80
1435
|
|
|
81
1436
|
// src/stringify-for-telemetry.ts
|
|
82
|
-
|
|
1437
|
+
import { convertDataContentToBase64String as convertDataContentToBase64String2 } from "ai";
|
|
83
1438
|
function stringifyForTelemetry(prompt) {
|
|
84
1439
|
return JSON.stringify(
|
|
85
1440
|
prompt.map((message) => ({
|
|
@@ -87,14 +1442,26 @@ function stringifyForTelemetry(prompt) {
|
|
|
87
1442
|
content: typeof message.content === "string" ? message.content : message.content.map(
|
|
88
1443
|
(part) => part.type === "file" ? {
|
|
89
1444
|
...part,
|
|
90
|
-
data:
|
|
1445
|
+
data: serializeFileData(part.data)
|
|
91
1446
|
} : part
|
|
92
1447
|
)
|
|
93
1448
|
}))
|
|
94
1449
|
);
|
|
95
1450
|
}
|
|
1451
|
+
function serializeFileData(data) {
|
|
1452
|
+
switch (data.type) {
|
|
1453
|
+
case "data":
|
|
1454
|
+
return data.data instanceof Uint8Array ? convertDataContentToBase64String2(data.data) : data.data;
|
|
1455
|
+
case "url":
|
|
1456
|
+
return data.url.toString();
|
|
1457
|
+
case "reference":
|
|
1458
|
+
return data.reference;
|
|
1459
|
+
case "text":
|
|
1460
|
+
return data.text;
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
96
1463
|
|
|
97
|
-
// src/open-telemetry
|
|
1464
|
+
// src/legacy-open-telemetry.ts
|
|
98
1465
|
function recordSpanError(span, error) {
|
|
99
1466
|
if (error instanceof Error) {
|
|
100
1467
|
span.recordException({
|
|
@@ -103,18 +1470,18 @@ function recordSpanError(span, error) {
|
|
|
103
1470
|
stack: error.stack
|
|
104
1471
|
});
|
|
105
1472
|
span.setStatus({
|
|
106
|
-
code:
|
|
1473
|
+
code: SpanStatusCode2.ERROR,
|
|
107
1474
|
message: error.message
|
|
108
1475
|
});
|
|
109
1476
|
} else {
|
|
110
|
-
span.setStatus({ code:
|
|
1477
|
+
span.setStatus({ code: SpanStatusCode2.ERROR });
|
|
111
1478
|
}
|
|
112
1479
|
}
|
|
113
|
-
function
|
|
114
|
-
return (telemetry == null ? void 0 : telemetry.isEnabled)
|
|
1480
|
+
function shouldRecord2(telemetry) {
|
|
1481
|
+
return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
|
|
115
1482
|
}
|
|
116
|
-
function
|
|
117
|
-
if (!
|
|
1483
|
+
function selectAttributes2(telemetry, attributes) {
|
|
1484
|
+
if (!shouldRecord2(telemetry)) {
|
|
118
1485
|
return {};
|
|
119
1486
|
}
|
|
120
1487
|
const result = {};
|
|
@@ -136,11 +1503,11 @@ function selectAttributes(telemetry, attributes) {
|
|
|
136
1503
|
}
|
|
137
1504
|
return result;
|
|
138
1505
|
}
|
|
139
|
-
var
|
|
1506
|
+
var LegacyOpenTelemetry = class {
|
|
140
1507
|
constructor(options = {}) {
|
|
141
1508
|
this.callStates = /* @__PURE__ */ new Map();
|
|
142
1509
|
var _a;
|
|
143
|
-
this.tracer = (_a = options.tracer) != null ? _a :
|
|
1510
|
+
this.tracer = (_a = options.tracer) != null ? _a : trace2.getTracer("ai");
|
|
144
1511
|
}
|
|
145
1512
|
getCallState(callId) {
|
|
146
1513
|
return this.callStates.get(callId);
|
|
@@ -158,31 +1525,32 @@ var OpenTelemetryIntegration = class {
|
|
|
158
1525
|
if (toolSpanEntry == null) {
|
|
159
1526
|
return execute();
|
|
160
1527
|
}
|
|
161
|
-
return
|
|
1528
|
+
return context3.with(toolSpanEntry.context, execute);
|
|
162
1529
|
}
|
|
163
1530
|
onStart(event) {
|
|
164
|
-
if (event.isEnabled !== true) return;
|
|
165
1531
|
if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
|
|
166
1532
|
this.onEmbedOperationStart(event);
|
|
167
1533
|
return;
|
|
168
1534
|
}
|
|
169
1535
|
if (event.operationId === "ai.rerank") {
|
|
170
|
-
this.onRerankOperationStart(
|
|
1536
|
+
this.onRerankOperationStart(
|
|
1537
|
+
event
|
|
1538
|
+
);
|
|
171
1539
|
return;
|
|
172
1540
|
}
|
|
173
1541
|
if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
|
|
174
|
-
this.onObjectOperationStart(
|
|
1542
|
+
this.onObjectOperationStart(
|
|
1543
|
+
event
|
|
1544
|
+
);
|
|
175
1545
|
return;
|
|
176
1546
|
}
|
|
177
1547
|
this.onGenerateStart(event);
|
|
178
1548
|
}
|
|
179
1549
|
onGenerateStart(event) {
|
|
180
1550
|
const telemetry = {
|
|
181
|
-
isEnabled: event.isEnabled,
|
|
182
1551
|
recordInputs: event.recordInputs,
|
|
183
1552
|
recordOutputs: event.recordOutputs,
|
|
184
|
-
functionId: event.functionId
|
|
185
|
-
metadata: event.metadata
|
|
1553
|
+
functionId: event.functionId
|
|
186
1554
|
};
|
|
187
1555
|
const settings = {
|
|
188
1556
|
maxOutputTokens: event.maxOutputTokens,
|
|
@@ -197,11 +1565,11 @@ var OpenTelemetryIntegration = class {
|
|
|
197
1565
|
};
|
|
198
1566
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
199
1567
|
model: { provider: event.provider, modelId: event.modelId },
|
|
200
|
-
telemetry,
|
|
201
1568
|
headers: event.headers,
|
|
202
|
-
settings
|
|
1569
|
+
settings,
|
|
1570
|
+
context: event.runtimeContext
|
|
203
1571
|
});
|
|
204
|
-
const attributes =
|
|
1572
|
+
const attributes = selectAttributes2(telemetry, {
|
|
205
1573
|
...assembleOperationName({
|
|
206
1574
|
operationId: event.operationId,
|
|
207
1575
|
telemetry
|
|
@@ -212,13 +1580,12 @@ var OpenTelemetryIntegration = class {
|
|
|
212
1580
|
"ai.prompt": {
|
|
213
1581
|
input: () => JSON.stringify({
|
|
214
1582
|
system: event.system,
|
|
215
|
-
prompt: event.prompt,
|
|
216
1583
|
messages: event.messages
|
|
217
1584
|
})
|
|
218
1585
|
}
|
|
219
1586
|
});
|
|
220
1587
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
221
|
-
const rootContext =
|
|
1588
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
222
1589
|
this.callStates.set(event.callId, {
|
|
223
1590
|
operationId: event.operationId,
|
|
224
1591
|
telemetry,
|
|
@@ -235,11 +1602,9 @@ var OpenTelemetryIntegration = class {
|
|
|
235
1602
|
}
|
|
236
1603
|
onObjectOperationStart(event) {
|
|
237
1604
|
const telemetry = {
|
|
238
|
-
isEnabled: event.isEnabled,
|
|
239
1605
|
recordInputs: event.recordInputs,
|
|
240
1606
|
recordOutputs: event.recordOutputs,
|
|
241
|
-
functionId: event.functionId
|
|
242
|
-
metadata: event.metadata
|
|
1607
|
+
functionId: event.functionId
|
|
243
1608
|
};
|
|
244
1609
|
const settings = {
|
|
245
1610
|
maxOutputTokens: event.maxOutputTokens,
|
|
@@ -253,11 +1618,11 @@ var OpenTelemetryIntegration = class {
|
|
|
253
1618
|
};
|
|
254
1619
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
255
1620
|
model: { provider: event.provider, modelId: event.modelId },
|
|
256
|
-
telemetry,
|
|
257
1621
|
headers: event.headers,
|
|
258
|
-
settings
|
|
1622
|
+
settings,
|
|
1623
|
+
context: void 0
|
|
259
1624
|
});
|
|
260
|
-
const attributes =
|
|
1625
|
+
const attributes = selectAttributes2(telemetry, {
|
|
261
1626
|
...assembleOperationName({
|
|
262
1627
|
operationId: event.operationId,
|
|
263
1628
|
telemetry
|
|
@@ -276,7 +1641,7 @@ var OpenTelemetryIntegration = class {
|
|
|
276
1641
|
"ai.settings.output": event.output
|
|
277
1642
|
});
|
|
278
1643
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
279
|
-
const rootContext =
|
|
1644
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
280
1645
|
this.callStates.set(event.callId, {
|
|
281
1646
|
operationId: event.operationId,
|
|
282
1647
|
telemetry,
|
|
@@ -298,7 +1663,7 @@ var OpenTelemetryIntegration = class {
|
|
|
298
1663
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
299
1664
|
const { telemetry } = state;
|
|
300
1665
|
const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
|
|
301
|
-
const attributes =
|
|
1666
|
+
const attributes = selectAttributes2(telemetry, {
|
|
302
1667
|
...assembleOperationName({
|
|
303
1668
|
operationId: stepOperationId,
|
|
304
1669
|
telemetry
|
|
@@ -321,7 +1686,7 @@ var OpenTelemetryIntegration = class {
|
|
|
321
1686
|
{ attributes },
|
|
322
1687
|
state.rootContext
|
|
323
1688
|
);
|
|
324
|
-
state.stepContext =
|
|
1689
|
+
state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
|
|
325
1690
|
}
|
|
326
1691
|
/** @deprecated */
|
|
327
1692
|
onObjectStepFinish(event) {
|
|
@@ -329,7 +1694,7 @@ var OpenTelemetryIntegration = class {
|
|
|
329
1694
|
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
330
1695
|
const { telemetry } = state;
|
|
331
1696
|
state.stepSpan.setAttributes(
|
|
332
|
-
|
|
1697
|
+
selectAttributes2(telemetry, {
|
|
333
1698
|
"ai.response.finishReason": event.finishReason,
|
|
334
1699
|
"ai.response.object": {
|
|
335
1700
|
output: () => {
|
|
@@ -370,24 +1735,22 @@ var OpenTelemetryIntegration = class {
|
|
|
370
1735
|
}
|
|
371
1736
|
onEmbedOperationStart(event) {
|
|
372
1737
|
const telemetry = {
|
|
373
|
-
isEnabled: event.isEnabled,
|
|
374
1738
|
recordInputs: event.recordInputs,
|
|
375
1739
|
recordOutputs: event.recordOutputs,
|
|
376
|
-
functionId: event.functionId
|
|
377
|
-
metadata: event.metadata
|
|
1740
|
+
functionId: event.functionId
|
|
378
1741
|
};
|
|
379
1742
|
const settings = {
|
|
380
1743
|
maxRetries: event.maxRetries
|
|
381
1744
|
};
|
|
382
1745
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
383
1746
|
model: { provider: event.provider, modelId: event.modelId },
|
|
384
|
-
telemetry,
|
|
385
1747
|
headers: event.headers,
|
|
386
|
-
settings
|
|
1748
|
+
settings,
|
|
1749
|
+
context: void 0
|
|
387
1750
|
});
|
|
388
1751
|
const value = event.value;
|
|
389
1752
|
const isMany = event.operationId === "ai.embedMany";
|
|
390
|
-
const attributes =
|
|
1753
|
+
const attributes = selectAttributes2(telemetry, {
|
|
391
1754
|
...assembleOperationName({
|
|
392
1755
|
operationId: event.operationId,
|
|
393
1756
|
telemetry
|
|
@@ -404,7 +1767,7 @@ var OpenTelemetryIntegration = class {
|
|
|
404
1767
|
}
|
|
405
1768
|
});
|
|
406
1769
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
407
|
-
const rootContext =
|
|
1770
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
408
1771
|
this.callStates.set(event.callId, {
|
|
409
1772
|
operationId: event.operationId,
|
|
410
1773
|
telemetry,
|
|
@@ -425,7 +1788,7 @@ var OpenTelemetryIntegration = class {
|
|
|
425
1788
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
426
1789
|
const { telemetry } = state;
|
|
427
1790
|
const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
|
|
428
|
-
const attributes =
|
|
1791
|
+
const attributes = selectAttributes2(telemetry, {
|
|
429
1792
|
...assembleOperationName({
|
|
430
1793
|
operationId: stepOperationId,
|
|
431
1794
|
telemetry
|
|
@@ -460,14 +1823,14 @@ var OpenTelemetryIntegration = class {
|
|
|
460
1823
|
{ attributes },
|
|
461
1824
|
state.rootContext
|
|
462
1825
|
);
|
|
463
|
-
state.stepContext =
|
|
1826
|
+
state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
|
|
464
1827
|
}
|
|
465
|
-
|
|
1828
|
+
onToolExecutionStart(event) {
|
|
466
1829
|
const state = this.getCallState(event.callId);
|
|
467
1830
|
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
468
1831
|
const { telemetry } = state;
|
|
469
1832
|
const { toolCall } = event;
|
|
470
|
-
const attributes =
|
|
1833
|
+
const attributes = selectAttributes2(telemetry, {
|
|
471
1834
|
...assembleOperationName({
|
|
472
1835
|
operationId: "ai.toolCall",
|
|
473
1836
|
telemetry
|
|
@@ -483,32 +1846,33 @@ var OpenTelemetryIntegration = class {
|
|
|
483
1846
|
{ attributes },
|
|
484
1847
|
state.stepContext
|
|
485
1848
|
);
|
|
486
|
-
const toolContext =
|
|
1849
|
+
const toolContext = trace2.setSpan(state.stepContext, toolSpan);
|
|
487
1850
|
state.toolSpans.set(toolCall.toolCallId, {
|
|
488
1851
|
span: toolSpan,
|
|
489
1852
|
context: toolContext
|
|
490
1853
|
});
|
|
491
1854
|
}
|
|
492
|
-
|
|
1855
|
+
onToolExecutionEnd(event) {
|
|
493
1856
|
const state = this.getCallState(event.callId);
|
|
494
1857
|
if (!state) return;
|
|
495
1858
|
const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
|
|
496
1859
|
if (!toolSpanEntry) return;
|
|
497
1860
|
const { span } = toolSpanEntry;
|
|
498
1861
|
const { telemetry } = state;
|
|
499
|
-
|
|
1862
|
+
const { toolOutput } = event;
|
|
1863
|
+
if (toolOutput.type === "tool-result") {
|
|
500
1864
|
try {
|
|
501
1865
|
span.setAttributes(
|
|
502
|
-
|
|
1866
|
+
selectAttributes2(telemetry, {
|
|
503
1867
|
"ai.toolCall.result": {
|
|
504
|
-
output: () => JSON.stringify(
|
|
1868
|
+
output: () => JSON.stringify(toolOutput.output)
|
|
505
1869
|
}
|
|
506
1870
|
})
|
|
507
1871
|
);
|
|
508
|
-
} catch (
|
|
1872
|
+
} catch (e) {
|
|
509
1873
|
}
|
|
510
1874
|
} else {
|
|
511
|
-
recordSpanError(span,
|
|
1875
|
+
recordSpanError(span, toolOutput.error);
|
|
512
1876
|
}
|
|
513
1877
|
span.end();
|
|
514
1878
|
state.toolSpans.delete(event.toolCall.toolCallId);
|
|
@@ -519,7 +1883,7 @@ var OpenTelemetryIntegration = class {
|
|
|
519
1883
|
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
520
1884
|
const { telemetry } = state;
|
|
521
1885
|
state.stepSpan.setAttributes(
|
|
522
|
-
|
|
1886
|
+
selectAttributes2(telemetry, {
|
|
523
1887
|
"ai.response.finishReason": event.finishReason,
|
|
524
1888
|
"ai.response.text": {
|
|
525
1889
|
output: () => {
|
|
@@ -596,7 +1960,7 @@ var OpenTelemetryIntegration = class {
|
|
|
596
1960
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
597
1961
|
const { telemetry } = state;
|
|
598
1962
|
state.rootSpan.setAttributes(
|
|
599
|
-
|
|
1963
|
+
selectAttributes2(telemetry, {
|
|
600
1964
|
"ai.response.finishReason": event.finishReason,
|
|
601
1965
|
"ai.response.text": {
|
|
602
1966
|
output: () => {
|
|
@@ -646,7 +2010,7 @@ var OpenTelemetryIntegration = class {
|
|
|
646
2010
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
647
2011
|
const { telemetry } = state;
|
|
648
2012
|
state.rootSpan.setAttributes(
|
|
649
|
-
|
|
2013
|
+
selectAttributes2(telemetry, {
|
|
650
2014
|
"ai.response.finishReason": event.finishReason,
|
|
651
2015
|
"ai.response.object": {
|
|
652
2016
|
output: () => event.object != null ? JSON.stringify(event.object) : void 0
|
|
@@ -668,7 +2032,7 @@ var OpenTelemetryIntegration = class {
|
|
|
668
2032
|
const { telemetry } = state;
|
|
669
2033
|
const isMany = state.operationId === "ai.embedMany";
|
|
670
2034
|
state.rootSpan.setAttributes(
|
|
671
|
-
|
|
2035
|
+
selectAttributes2(telemetry, {
|
|
672
2036
|
...isMany ? {
|
|
673
2037
|
"ai.embeddings": {
|
|
674
2038
|
output: () => event.embedding.map((e) => JSON.stringify(e))
|
|
@@ -688,7 +2052,7 @@ var OpenTelemetryIntegration = class {
|
|
|
688
2052
|
const state = this.getCallState(event.callId);
|
|
689
2053
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
690
2054
|
const { telemetry } = state;
|
|
691
|
-
const attributes =
|
|
2055
|
+
const attributes = selectAttributes2(telemetry, {
|
|
692
2056
|
...assembleOperationName({
|
|
693
2057
|
operationId: event.operationId,
|
|
694
2058
|
telemetry
|
|
@@ -703,7 +2067,7 @@ var OpenTelemetryIntegration = class {
|
|
|
703
2067
|
{ attributes },
|
|
704
2068
|
state.rootContext
|
|
705
2069
|
);
|
|
706
|
-
const embedContext =
|
|
2070
|
+
const embedContext = trace2.setSpan(state.rootContext, embedSpan);
|
|
707
2071
|
state.embedSpans.set(event.embedCallId, {
|
|
708
2072
|
span: embedSpan,
|
|
709
2073
|
context: embedContext
|
|
@@ -717,7 +2081,7 @@ var OpenTelemetryIntegration = class {
|
|
|
717
2081
|
const { span } = embedSpanEntry;
|
|
718
2082
|
const { telemetry } = state;
|
|
719
2083
|
span.setAttributes(
|
|
720
|
-
|
|
2084
|
+
selectAttributes2(telemetry, {
|
|
721
2085
|
"ai.embeddings": {
|
|
722
2086
|
output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
|
|
723
2087
|
},
|
|
@@ -729,22 +2093,20 @@ var OpenTelemetryIntegration = class {
|
|
|
729
2093
|
}
|
|
730
2094
|
onRerankOperationStart(event) {
|
|
731
2095
|
const telemetry = {
|
|
732
|
-
isEnabled: event.isEnabled,
|
|
733
2096
|
recordInputs: event.recordInputs,
|
|
734
2097
|
recordOutputs: event.recordOutputs,
|
|
735
|
-
functionId: event.functionId
|
|
736
|
-
metadata: event.metadata
|
|
2098
|
+
functionId: event.functionId
|
|
737
2099
|
};
|
|
738
2100
|
const settings = {
|
|
739
2101
|
maxRetries: event.maxRetries
|
|
740
2102
|
};
|
|
741
2103
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
742
2104
|
model: { provider: event.provider, modelId: event.modelId },
|
|
743
|
-
telemetry,
|
|
744
2105
|
headers: event.headers,
|
|
745
|
-
settings
|
|
2106
|
+
settings,
|
|
2107
|
+
context: void 0
|
|
746
2108
|
});
|
|
747
|
-
const attributes =
|
|
2109
|
+
const attributes = selectAttributes2(telemetry, {
|
|
748
2110
|
...assembleOperationName({
|
|
749
2111
|
operationId: event.operationId,
|
|
750
2112
|
telemetry
|
|
@@ -755,7 +2117,7 @@ var OpenTelemetryIntegration = class {
|
|
|
755
2117
|
}
|
|
756
2118
|
});
|
|
757
2119
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
758
|
-
const rootContext =
|
|
2120
|
+
const rootContext = trace2.setSpan(context3.active(), rootSpan);
|
|
759
2121
|
this.callStates.set(event.callId, {
|
|
760
2122
|
operationId: event.operationId,
|
|
761
2123
|
telemetry,
|
|
@@ -780,7 +2142,7 @@ var OpenTelemetryIntegration = class {
|
|
|
780
2142
|
const state = this.getCallState(event.callId);
|
|
781
2143
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
782
2144
|
const { telemetry } = state;
|
|
783
|
-
const attributes =
|
|
2145
|
+
const attributes = selectAttributes2(telemetry, {
|
|
784
2146
|
...assembleOperationName({
|
|
785
2147
|
operationId: event.operationId,
|
|
786
2148
|
telemetry
|
|
@@ -795,7 +2157,7 @@ var OpenTelemetryIntegration = class {
|
|
|
795
2157
|
{ attributes },
|
|
796
2158
|
state.rootContext
|
|
797
2159
|
);
|
|
798
|
-
const rerankContext =
|
|
2160
|
+
const rerankContext = trace2.setSpan(state.rootContext, rerankSpan);
|
|
799
2161
|
state.rerankSpan = { span: rerankSpan, context: rerankContext };
|
|
800
2162
|
}
|
|
801
2163
|
onRerankFinish(event) {
|
|
@@ -804,7 +2166,7 @@ var OpenTelemetryIntegration = class {
|
|
|
804
2166
|
const { span } = state.rerankSpan;
|
|
805
2167
|
const { telemetry } = state;
|
|
806
2168
|
span.setAttributes(
|
|
807
|
-
|
|
2169
|
+
selectAttributes2(telemetry, {
|
|
808
2170
|
"ai.ranking.type": event.documentsType,
|
|
809
2171
|
"ai.ranking": {
|
|
810
2172
|
output: () => event.ranking.map((r) => JSON.stringify(r))
|
|
@@ -861,8 +2223,8 @@ var OpenTelemetryIntegration = class {
|
|
|
861
2223
|
this.cleanupCallState(event.callId);
|
|
862
2224
|
}
|
|
863
2225
|
};
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
2226
|
+
export {
|
|
2227
|
+
LegacyOpenTelemetry,
|
|
2228
|
+
OpenTelemetry
|
|
2229
|
+
};
|
|
868
2230
|
//# sourceMappingURL=index.js.map
|