@ai-sdk/otel 1.0.0-beta.5 → 1.0.0-beta.51
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 +335 -0
- package/dist/index.d.ts +50 -5
- package/dist/index.js +1131 -100
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/assemble-operation-name.ts +2 -2
- package/src/gen-ai-format-messages.ts +573 -0
- package/src/gen-ai-open-telemetry-integration.ts +924 -0
- package/src/get-base-telemetry-attributes.ts +11 -14
- package/src/index.ts +1 -0
- package/src/open-telemetry-integration.ts +29 -33
- package/src/select-telemetry-attributes.ts +3 -3
- 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,1070 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
// src/gen-ai-open-telemetry-integration.ts
|
|
2
|
+
import {
|
|
3
|
+
context,
|
|
4
|
+
SpanKind,
|
|
5
|
+
SpanStatusCode,
|
|
6
|
+
trace
|
|
7
|
+
} from "@opentelemetry/api";
|
|
8
|
+
|
|
9
|
+
// src/gen-ai-format-messages.ts
|
|
10
|
+
import { convertDataContentToBase64String } from "ai";
|
|
11
|
+
function mapProviderName(provider) {
|
|
12
|
+
const lower = provider.toLowerCase();
|
|
13
|
+
const wellKnownPrefixes = [
|
|
14
|
+
["google.vertex", "gcp.vertex_ai"],
|
|
15
|
+
["google.generative-ai", "gcp.gemini"],
|
|
16
|
+
["google-vertex", "gcp.vertex_ai"],
|
|
17
|
+
["amazon-bedrock", "aws.bedrock"],
|
|
18
|
+
["azure-openai", "azure.ai.openai"],
|
|
19
|
+
["anthropic", "anthropic"],
|
|
20
|
+
["openai", "openai"],
|
|
21
|
+
["azure", "azure.ai.inference"],
|
|
22
|
+
["google", "gcp.gemini"],
|
|
23
|
+
["mistral", "mistral_ai"],
|
|
24
|
+
["cohere", "cohere"],
|
|
25
|
+
["bedrock", "aws.bedrock"],
|
|
26
|
+
["groq", "groq"],
|
|
27
|
+
["deepseek", "deepseek"],
|
|
28
|
+
["perplexity", "perplexity"],
|
|
29
|
+
["xai", "x_ai"]
|
|
30
|
+
];
|
|
31
|
+
for (const [prefix, mapped] of wellKnownPrefixes) {
|
|
32
|
+
if (lower === prefix || lower.startsWith(prefix + ".") || lower.startsWith(prefix + "-")) {
|
|
33
|
+
return mapped;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return provider;
|
|
37
|
+
}
|
|
38
|
+
function mapOperationName(operationId) {
|
|
39
|
+
var _a;
|
|
40
|
+
const mapping = {
|
|
41
|
+
"ai.generateText": "invoke_agent",
|
|
42
|
+
"ai.streamText": "invoke_agent",
|
|
43
|
+
"ai.generateObject": "invoke_agent",
|
|
44
|
+
"ai.streamObject": "invoke_agent",
|
|
45
|
+
"ai.embed": "embeddings",
|
|
46
|
+
"ai.embedMany": "embeddings",
|
|
47
|
+
"ai.rerank": "rerank"
|
|
48
|
+
};
|
|
49
|
+
return (_a = mapping[operationId]) != null ? _a : operationId;
|
|
50
|
+
}
|
|
51
|
+
function formatSystemInstructions(system) {
|
|
52
|
+
if (typeof system === "string") {
|
|
53
|
+
return [{ type: "text", content: system }];
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(system)) {
|
|
56
|
+
return system.map((msg) => ({ type: "text", content: msg.content }));
|
|
57
|
+
}
|
|
58
|
+
return [{ type: "text", content: system.content }];
|
|
59
|
+
}
|
|
60
|
+
function convertMessagePartToSemConv(part) {
|
|
61
|
+
var _a, _b, _c, _d;
|
|
62
|
+
switch (part.type) {
|
|
63
|
+
case "text":
|
|
64
|
+
return { type: "text", content: part.text };
|
|
65
|
+
case "reasoning":
|
|
66
|
+
return { type: "reasoning", content: part.text };
|
|
67
|
+
case "tool-call":
|
|
68
|
+
return {
|
|
69
|
+
type: "tool_call",
|
|
70
|
+
id: (_a = part.toolCallId) != null ? _a : null,
|
|
71
|
+
name: part.toolName,
|
|
72
|
+
arguments: part.input
|
|
73
|
+
};
|
|
74
|
+
case "tool-result": {
|
|
75
|
+
const output = part.output;
|
|
76
|
+
let response;
|
|
77
|
+
if (output) {
|
|
78
|
+
if (output.type === "text" || output.type === "error-text") {
|
|
79
|
+
response = output.value;
|
|
80
|
+
} else if (output.type === "json" || output.type === "error-json") {
|
|
81
|
+
response = output.value;
|
|
82
|
+
} else if (output.type === "execution-denied") {
|
|
83
|
+
response = { denied: true, reason: output.reason };
|
|
84
|
+
} else {
|
|
85
|
+
response = output;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
type: "tool_call_response",
|
|
90
|
+
id: (_b = part.toolCallId) != null ? _b : null,
|
|
91
|
+
response
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
case "file": {
|
|
95
|
+
const data = part.data;
|
|
96
|
+
let content;
|
|
97
|
+
if (data instanceof Uint8Array) {
|
|
98
|
+
content = convertDataContentToBase64String(data);
|
|
99
|
+
} else if (typeof data === "string") {
|
|
100
|
+
if (data.startsWith("http://") || data.startsWith("https://")) {
|
|
101
|
+
return {
|
|
102
|
+
type: "uri",
|
|
103
|
+
modality: getModality(part.mediaType),
|
|
104
|
+
mime_type: (_c = part.mediaType) != null ? _c : null,
|
|
105
|
+
uri: data
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
content = data;
|
|
109
|
+
} else {
|
|
110
|
+
content = String(data);
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
type: "blob",
|
|
114
|
+
modality: getModality(part.mediaType),
|
|
115
|
+
mime_type: (_d = part.mediaType) != null ? _d : null,
|
|
116
|
+
content
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
case "tool-approval-response":
|
|
120
|
+
return {
|
|
121
|
+
type: "tool_approval_response",
|
|
122
|
+
approval_id: part.approvalId,
|
|
123
|
+
approved: part.approved,
|
|
124
|
+
reason: part.reason
|
|
125
|
+
};
|
|
126
|
+
case "custom":
|
|
127
|
+
return { type: "custom", kind: part.kind };
|
|
128
|
+
case "reasoning-file":
|
|
129
|
+
return { type: String(part.type) };
|
|
130
|
+
default: {
|
|
131
|
+
const _exhaustive = part;
|
|
132
|
+
return { type: String(_exhaustive.type) };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function getModality(mediaType) {
|
|
137
|
+
if (!mediaType) return "image";
|
|
138
|
+
if (mediaType.startsWith("image/")) return "image";
|
|
139
|
+
if (mediaType.startsWith("video/")) return "video";
|
|
140
|
+
if (mediaType.startsWith("audio/")) return "audio";
|
|
141
|
+
return "image";
|
|
142
|
+
}
|
|
143
|
+
function formatInputMessages(prompt) {
|
|
144
|
+
return prompt.filter((msg) => msg.role !== "system").map((message) => {
|
|
145
|
+
if (message.role === "system") {
|
|
146
|
+
return {
|
|
147
|
+
role: "system",
|
|
148
|
+
parts: [{ type: "text", content: message.content }]
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const parts = message.content.map(convertMessagePartToSemConv);
|
|
152
|
+
return { role: message.role, parts };
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function formatModelMessages({
|
|
156
|
+
prompt,
|
|
157
|
+
messages
|
|
158
|
+
}) {
|
|
159
|
+
const result = [];
|
|
160
|
+
if (typeof prompt === "string") {
|
|
161
|
+
result.push({
|
|
162
|
+
role: "user",
|
|
163
|
+
parts: [{ type: "text", content: prompt }]
|
|
164
|
+
});
|
|
165
|
+
} else if (Array.isArray(prompt)) {
|
|
166
|
+
for (const msg of prompt) {
|
|
167
|
+
const converted = convertModelMessageToSemConv(msg);
|
|
168
|
+
if (converted) result.push(converted);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (messages) {
|
|
172
|
+
for (const msg of messages) {
|
|
173
|
+
const converted = convertModelMessageToSemConv(msg);
|
|
174
|
+
if (converted) result.push(converted);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
function convertModelMessageToSemConv(msg) {
|
|
180
|
+
if (msg.role === "system") return void 0;
|
|
181
|
+
if (msg.role === "user") {
|
|
182
|
+
if (typeof msg.content === "string") {
|
|
183
|
+
return {
|
|
184
|
+
role: "user",
|
|
185
|
+
parts: [{ type: "text", content: msg.content }]
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const parts = msg.content.map((part) => {
|
|
189
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
190
|
+
switch (part.type) {
|
|
191
|
+
case "text":
|
|
192
|
+
return { type: "text", content: part.text };
|
|
193
|
+
case "image": {
|
|
194
|
+
const data = part.image;
|
|
195
|
+
if (data instanceof URL) {
|
|
196
|
+
return {
|
|
197
|
+
type: "uri",
|
|
198
|
+
modality: "image",
|
|
199
|
+
mime_type: (_a = part.mediaType) != null ? _a : null,
|
|
200
|
+
uri: data.toString()
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (typeof data === "string") {
|
|
204
|
+
if (data.startsWith("http://") || data.startsWith("https://")) {
|
|
205
|
+
return {
|
|
206
|
+
type: "uri",
|
|
207
|
+
modality: "image",
|
|
208
|
+
mime_type: (_b = part.mediaType) != null ? _b : null,
|
|
209
|
+
uri: data
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
type: "blob",
|
|
214
|
+
modality: "image",
|
|
215
|
+
mime_type: (_c = part.mediaType) != null ? _c : null,
|
|
216
|
+
content: data
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
type: "blob",
|
|
221
|
+
modality: "image",
|
|
222
|
+
mime_type: (_d = part.mediaType) != null ? _d : null,
|
|
223
|
+
content: convertDataContentToBase64String(data)
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
case "file": {
|
|
227
|
+
const data = part.data;
|
|
228
|
+
if (data instanceof URL) {
|
|
229
|
+
return {
|
|
230
|
+
type: "uri",
|
|
231
|
+
modality: getModality(part.mediaType),
|
|
232
|
+
mime_type: (_e = part.mediaType) != null ? _e : null,
|
|
233
|
+
uri: data.toString()
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
if (typeof data === "string") {
|
|
237
|
+
if (data.startsWith("http://") || data.startsWith("https://")) {
|
|
238
|
+
return {
|
|
239
|
+
type: "uri",
|
|
240
|
+
modality: getModality(part.mediaType),
|
|
241
|
+
mime_type: (_f = part.mediaType) != null ? _f : null,
|
|
242
|
+
uri: data
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
type: "blob",
|
|
247
|
+
modality: getModality(part.mediaType),
|
|
248
|
+
mime_type: (_g = part.mediaType) != null ? _g : null,
|
|
249
|
+
content: data
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
type: "blob",
|
|
254
|
+
modality: getModality(part.mediaType),
|
|
255
|
+
mime_type: (_h = part.mediaType) != null ? _h : null,
|
|
256
|
+
content: convertDataContentToBase64String(data)
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
default:
|
|
260
|
+
return { type: String(part.type) };
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
return { role: "user", parts };
|
|
264
|
+
}
|
|
265
|
+
if (msg.role === "assistant") {
|
|
266
|
+
if (typeof msg.content === "string") {
|
|
267
|
+
return {
|
|
268
|
+
role: "assistant",
|
|
269
|
+
parts: [{ type: "text", content: msg.content }]
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
const parts = msg.content.map((part) => {
|
|
273
|
+
var _a, _b;
|
|
274
|
+
switch (part.type) {
|
|
275
|
+
case "text":
|
|
276
|
+
return { type: "text", content: part.text };
|
|
277
|
+
case "reasoning":
|
|
278
|
+
return { type: "reasoning", content: part.text };
|
|
279
|
+
case "tool-call":
|
|
280
|
+
return {
|
|
281
|
+
type: "tool_call",
|
|
282
|
+
id: (_a = part.toolCallId) != null ? _a : null,
|
|
283
|
+
name: part.toolName,
|
|
284
|
+
arguments: part.input
|
|
285
|
+
};
|
|
286
|
+
case "tool-result": {
|
|
287
|
+
const output = part.output;
|
|
288
|
+
let response;
|
|
289
|
+
if (output) {
|
|
290
|
+
if (output.type === "text" || output.type === "error-text") {
|
|
291
|
+
response = output.value;
|
|
292
|
+
} else if (output.type === "json" || output.type === "error-json") {
|
|
293
|
+
response = output.value;
|
|
294
|
+
} else if (output.type === "execution-denied") {
|
|
295
|
+
response = { denied: true, reason: output.reason };
|
|
296
|
+
} else {
|
|
297
|
+
response = output;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
type: "tool_call_response",
|
|
302
|
+
id: (_b = part.toolCallId) != null ? _b : null,
|
|
303
|
+
response
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
default:
|
|
307
|
+
return { type: String(part.type) };
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
return { role: "assistant", parts };
|
|
311
|
+
}
|
|
312
|
+
if (msg.role === "tool") {
|
|
313
|
+
const parts = msg.content.map((part) => {
|
|
314
|
+
var _a;
|
|
315
|
+
if (part.type === "tool-result") {
|
|
316
|
+
const output = part.output;
|
|
317
|
+
let response;
|
|
318
|
+
if (output) {
|
|
319
|
+
if (output.type === "text" || output.type === "error-text") {
|
|
320
|
+
response = output.value;
|
|
321
|
+
} else if (output.type === "json" || output.type === "error-json") {
|
|
322
|
+
response = output.value;
|
|
323
|
+
} else if (output.type === "execution-denied") {
|
|
324
|
+
response = { denied: true, reason: output.reason };
|
|
325
|
+
} else {
|
|
326
|
+
response = output;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
type: "tool_call_response",
|
|
331
|
+
id: (_a = part.toolCallId) != null ? _a : null,
|
|
332
|
+
response
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
return { type: String(part.type) };
|
|
336
|
+
});
|
|
337
|
+
return { role: "tool", parts };
|
|
338
|
+
}
|
|
339
|
+
return void 0;
|
|
340
|
+
}
|
|
341
|
+
function formatOutputMessages({
|
|
342
|
+
text,
|
|
343
|
+
reasoning,
|
|
344
|
+
toolCalls,
|
|
345
|
+
files,
|
|
346
|
+
finishReason
|
|
347
|
+
}) {
|
|
348
|
+
const parts = [];
|
|
349
|
+
if (reasoning) {
|
|
350
|
+
for (const r of reasoning) {
|
|
351
|
+
if ("text" in r && r.text) {
|
|
352
|
+
parts.push({ type: "reasoning", content: r.text });
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (text != null && text.length > 0) {
|
|
357
|
+
parts.push({ type: "text", content: text });
|
|
358
|
+
}
|
|
359
|
+
if (toolCalls) {
|
|
360
|
+
for (const tc of toolCalls) {
|
|
361
|
+
parts.push({
|
|
362
|
+
type: "tool_call",
|
|
363
|
+
id: tc.toolCallId,
|
|
364
|
+
name: tc.toolName,
|
|
365
|
+
arguments: tc.input
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (files) {
|
|
370
|
+
for (const file of files) {
|
|
371
|
+
parts.push({
|
|
372
|
+
type: "blob",
|
|
373
|
+
modality: getModality(file.mediaType),
|
|
374
|
+
mime_type: file.mediaType,
|
|
375
|
+
content: file.base64
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return [
|
|
380
|
+
{
|
|
381
|
+
role: "assistant",
|
|
382
|
+
parts,
|
|
383
|
+
finish_reason: mapFinishReason(finishReason)
|
|
384
|
+
}
|
|
385
|
+
];
|
|
386
|
+
}
|
|
387
|
+
function formatObjectOutputMessages({
|
|
388
|
+
objectText,
|
|
389
|
+
finishReason
|
|
390
|
+
}) {
|
|
391
|
+
return [
|
|
392
|
+
{
|
|
393
|
+
role: "assistant",
|
|
394
|
+
parts: [{ type: "text", content: objectText }],
|
|
395
|
+
finish_reason: mapFinishReason(finishReason)
|
|
396
|
+
}
|
|
397
|
+
];
|
|
398
|
+
}
|
|
399
|
+
function mapFinishReason(reason) {
|
|
400
|
+
var _a;
|
|
401
|
+
const mapping = {
|
|
402
|
+
stop: "stop",
|
|
403
|
+
length: "length",
|
|
404
|
+
"content-filter": "content_filter",
|
|
405
|
+
"tool-calls": "tool_call",
|
|
406
|
+
error: "error",
|
|
407
|
+
other: "stop",
|
|
408
|
+
unknown: "stop"
|
|
409
|
+
};
|
|
410
|
+
return (_a = mapping[reason]) != null ? _a : reason;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// src/gen-ai-open-telemetry-integration.ts
|
|
414
|
+
function recordSpanError(span, error) {
|
|
415
|
+
if (error instanceof Error) {
|
|
416
|
+
span.recordException({
|
|
417
|
+
name: error.name,
|
|
418
|
+
message: error.message,
|
|
419
|
+
stack: error.stack
|
|
420
|
+
});
|
|
421
|
+
span.setStatus({
|
|
422
|
+
code: SpanStatusCode.ERROR,
|
|
423
|
+
message: error.message
|
|
424
|
+
});
|
|
425
|
+
} else {
|
|
426
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
function shouldRecord(telemetry) {
|
|
430
|
+
return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
|
|
431
|
+
}
|
|
432
|
+
function selectAttributes(telemetry, attributes) {
|
|
433
|
+
if (!shouldRecord(telemetry)) {
|
|
434
|
+
return {};
|
|
435
|
+
}
|
|
436
|
+
const result = {};
|
|
437
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
438
|
+
if (value == null) continue;
|
|
439
|
+
if (typeof value === "object" && "input" in value && typeof value.input === "function") {
|
|
440
|
+
if ((telemetry == null ? void 0 : telemetry.recordInputs) === false) continue;
|
|
441
|
+
const resolved = value.input();
|
|
442
|
+
if (resolved != null) result[key] = resolved;
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (typeof value === "object" && "output" in value && typeof value.output === "function") {
|
|
446
|
+
if ((telemetry == null ? void 0 : telemetry.recordOutputs) === false) continue;
|
|
447
|
+
const resolved = value.output();
|
|
448
|
+
if (resolved != null) result[key] = resolved;
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
result[key] = value;
|
|
452
|
+
}
|
|
453
|
+
return result;
|
|
454
|
+
}
|
|
455
|
+
var GenAIOpenTelemetryIntegration = class {
|
|
456
|
+
constructor(options = {}) {
|
|
457
|
+
this.callStates = /* @__PURE__ */ new Map();
|
|
458
|
+
var _a;
|
|
459
|
+
this.tracer = (_a = options.tracer) != null ? _a : trace.getTracer("gen_ai");
|
|
460
|
+
}
|
|
461
|
+
getCallState(callId) {
|
|
462
|
+
return this.callStates.get(callId);
|
|
463
|
+
}
|
|
464
|
+
cleanupCallState(callId) {
|
|
465
|
+
this.callStates.delete(callId);
|
|
466
|
+
}
|
|
467
|
+
executeTool({
|
|
468
|
+
callId,
|
|
469
|
+
toolCallId,
|
|
470
|
+
execute
|
|
471
|
+
}) {
|
|
472
|
+
var _a;
|
|
473
|
+
const toolSpanEntry = (_a = this.getCallState(callId)) == null ? void 0 : _a.toolSpans.get(toolCallId);
|
|
474
|
+
if (toolSpanEntry == null) {
|
|
475
|
+
return execute();
|
|
476
|
+
}
|
|
477
|
+
return context.with(toolSpanEntry.context, execute);
|
|
478
|
+
}
|
|
479
|
+
onStart(event) {
|
|
480
|
+
if (event.isEnabled === false) return;
|
|
481
|
+
if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
|
|
482
|
+
this.onEmbedOperationStart(event);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (event.operationId === "ai.rerank") {
|
|
486
|
+
this.onRerankOperationStart(event);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
if (event.operationId === "ai.generateObject" || event.operationId === "ai.streamObject") {
|
|
490
|
+
this.onObjectOperationStart(event);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
this.onGenerateStart(event);
|
|
494
|
+
}
|
|
495
|
+
onGenerateStart(event) {
|
|
496
|
+
var _a;
|
|
497
|
+
const telemetry = {
|
|
498
|
+
isEnabled: event.isEnabled,
|
|
499
|
+
recordInputs: event.recordInputs,
|
|
500
|
+
recordOutputs: event.recordOutputs,
|
|
501
|
+
functionId: event.functionId
|
|
502
|
+
};
|
|
503
|
+
const settings = {
|
|
504
|
+
maxOutputTokens: event.maxOutputTokens,
|
|
505
|
+
temperature: event.temperature,
|
|
506
|
+
topP: event.topP,
|
|
507
|
+
topK: event.topK,
|
|
508
|
+
presencePenalty: event.presencePenalty,
|
|
509
|
+
frequencyPenalty: event.frequencyPenalty,
|
|
510
|
+
stopSequences: event.stopSequences,
|
|
511
|
+
seed: event.seed,
|
|
512
|
+
maxRetries: event.maxRetries
|
|
513
|
+
};
|
|
514
|
+
const providerName = mapProviderName(event.provider);
|
|
515
|
+
const operationName = mapOperationName(event.operationId);
|
|
516
|
+
const attributes = selectAttributes(telemetry, {
|
|
517
|
+
"gen_ai.operation.name": operationName,
|
|
518
|
+
"gen_ai.provider.name": providerName,
|
|
519
|
+
"gen_ai.request.model": event.modelId,
|
|
520
|
+
"gen_ai.agent.name": telemetry.functionId,
|
|
521
|
+
"gen_ai.request.frequency_penalty": event.frequencyPenalty,
|
|
522
|
+
"gen_ai.request.max_tokens": event.maxOutputTokens,
|
|
523
|
+
"gen_ai.request.presence_penalty": event.presencePenalty,
|
|
524
|
+
"gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
|
|
525
|
+
"gen_ai.request.top_k": event.topK,
|
|
526
|
+
"gen_ai.request.top_p": event.topP,
|
|
527
|
+
"gen_ai.request.stop_sequences": event.stopSequences,
|
|
528
|
+
"gen_ai.request.seed": event.seed,
|
|
529
|
+
"gen_ai.system_instructions": event.system ? {
|
|
530
|
+
input: () => JSON.stringify(formatSystemInstructions(event.system))
|
|
531
|
+
} : void 0,
|
|
532
|
+
"gen_ai.input.messages": {
|
|
533
|
+
input: () => JSON.stringify(
|
|
534
|
+
formatModelMessages({
|
|
535
|
+
prompt: event.prompt,
|
|
536
|
+
messages: event.messages
|
|
537
|
+
})
|
|
538
|
+
)
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
const spanName = `${operationName} ${event.modelId}`;
|
|
542
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
543
|
+
attributes,
|
|
544
|
+
kind: SpanKind.INTERNAL
|
|
545
|
+
});
|
|
546
|
+
const rootContext = trace.setSpan(context.active(), rootSpan);
|
|
547
|
+
this.callStates.set(event.callId, {
|
|
548
|
+
operationId: event.operationId,
|
|
549
|
+
telemetry,
|
|
550
|
+
rootSpan,
|
|
551
|
+
rootContext,
|
|
552
|
+
stepSpan: void 0,
|
|
553
|
+
stepContext: void 0,
|
|
554
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
555
|
+
rerankSpan: void 0,
|
|
556
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
557
|
+
settings,
|
|
558
|
+
provider: event.provider,
|
|
559
|
+
modelId: event.modelId
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
onObjectOperationStart(event) {
|
|
563
|
+
var _a;
|
|
564
|
+
const telemetry = {
|
|
565
|
+
isEnabled: event.isEnabled,
|
|
566
|
+
recordInputs: event.recordInputs,
|
|
567
|
+
recordOutputs: event.recordOutputs,
|
|
568
|
+
functionId: event.functionId
|
|
569
|
+
};
|
|
570
|
+
const settings = {
|
|
571
|
+
maxOutputTokens: event.maxOutputTokens,
|
|
572
|
+
temperature: event.temperature,
|
|
573
|
+
topP: event.topP,
|
|
574
|
+
topK: event.topK,
|
|
575
|
+
presencePenalty: event.presencePenalty,
|
|
576
|
+
frequencyPenalty: event.frequencyPenalty,
|
|
577
|
+
seed: event.seed,
|
|
578
|
+
maxRetries: event.maxRetries
|
|
579
|
+
};
|
|
580
|
+
const providerName = mapProviderName(event.provider);
|
|
581
|
+
const operationName = mapOperationName(event.operationId);
|
|
582
|
+
const attributes = selectAttributes(telemetry, {
|
|
583
|
+
"gen_ai.operation.name": operationName,
|
|
584
|
+
"gen_ai.provider.name": providerName,
|
|
585
|
+
"gen_ai.request.model": event.modelId,
|
|
586
|
+
"gen_ai.agent.name": telemetry.functionId,
|
|
587
|
+
"gen_ai.output.type": "json",
|
|
588
|
+
"gen_ai.request.frequency_penalty": event.frequencyPenalty,
|
|
589
|
+
"gen_ai.request.max_tokens": event.maxOutputTokens,
|
|
590
|
+
"gen_ai.request.presence_penalty": event.presencePenalty,
|
|
591
|
+
"gen_ai.request.temperature": (_a = event.temperature) != null ? _a : void 0,
|
|
592
|
+
"gen_ai.request.top_k": event.topK,
|
|
593
|
+
"gen_ai.request.top_p": event.topP,
|
|
594
|
+
"gen_ai.request.seed": event.seed,
|
|
595
|
+
"gen_ai.system_instructions": event.system ? {
|
|
596
|
+
input: () => JSON.stringify(formatSystemInstructions(event.system))
|
|
597
|
+
} : void 0,
|
|
598
|
+
"gen_ai.input.messages": {
|
|
599
|
+
input: () => JSON.stringify(
|
|
600
|
+
formatModelMessages({
|
|
601
|
+
prompt: event.prompt,
|
|
602
|
+
messages: event.messages
|
|
603
|
+
})
|
|
604
|
+
)
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
const spanName = `${operationName} ${event.modelId}`;
|
|
608
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
609
|
+
attributes,
|
|
610
|
+
kind: SpanKind.INTERNAL
|
|
611
|
+
});
|
|
612
|
+
const rootContext = trace.setSpan(context.active(), rootSpan);
|
|
613
|
+
this.callStates.set(event.callId, {
|
|
614
|
+
operationId: event.operationId,
|
|
615
|
+
telemetry,
|
|
616
|
+
rootSpan,
|
|
617
|
+
rootContext,
|
|
618
|
+
stepSpan: void 0,
|
|
619
|
+
stepContext: void 0,
|
|
620
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
621
|
+
rerankSpan: void 0,
|
|
622
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
623
|
+
settings,
|
|
624
|
+
provider: event.provider,
|
|
625
|
+
modelId: event.modelId
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
/** @deprecated */
|
|
629
|
+
onObjectStepStart(event) {
|
|
630
|
+
var _a;
|
|
631
|
+
const state = this.getCallState(event.callId);
|
|
632
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
633
|
+
const { telemetry } = state;
|
|
634
|
+
const providerName = mapProviderName(event.provider);
|
|
635
|
+
const attributes = selectAttributes(telemetry, {
|
|
636
|
+
"gen_ai.operation.name": "chat",
|
|
637
|
+
"gen_ai.provider.name": providerName,
|
|
638
|
+
"gen_ai.request.model": event.modelId,
|
|
639
|
+
"gen_ai.output.type": "json",
|
|
640
|
+
"gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
|
|
641
|
+
"gen_ai.request.max_tokens": state.settings.maxOutputTokens,
|
|
642
|
+
"gen_ai.request.presence_penalty": state.settings.presencePenalty,
|
|
643
|
+
"gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
|
|
644
|
+
"gen_ai.request.top_k": state.settings.topK,
|
|
645
|
+
"gen_ai.request.top_p": state.settings.topP,
|
|
646
|
+
"gen_ai.input.messages": {
|
|
647
|
+
input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
const spanName = `chat ${event.modelId}`;
|
|
651
|
+
state.stepSpan = this.tracer.startSpan(
|
|
652
|
+
spanName,
|
|
653
|
+
{ attributes, kind: SpanKind.CLIENT },
|
|
654
|
+
state.rootContext
|
|
655
|
+
);
|
|
656
|
+
state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
|
|
657
|
+
}
|
|
658
|
+
/** @deprecated */
|
|
659
|
+
onObjectStepFinish(event) {
|
|
660
|
+
const state = this.getCallState(event.callId);
|
|
661
|
+
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
662
|
+
const { telemetry } = state;
|
|
663
|
+
state.stepSpan.setAttributes(
|
|
664
|
+
selectAttributes(telemetry, {
|
|
665
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
666
|
+
"gen_ai.response.id": event.response.id,
|
|
667
|
+
"gen_ai.response.model": event.response.modelId,
|
|
668
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
669
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
670
|
+
"gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
|
|
671
|
+
"gen_ai.output.messages": {
|
|
672
|
+
output: () => {
|
|
673
|
+
try {
|
|
674
|
+
return JSON.stringify(
|
|
675
|
+
formatObjectOutputMessages({
|
|
676
|
+
objectText: event.objectText,
|
|
677
|
+
finishReason: event.finishReason
|
|
678
|
+
})
|
|
679
|
+
);
|
|
680
|
+
} catch (e) {
|
|
681
|
+
return event.objectText;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
})
|
|
686
|
+
);
|
|
687
|
+
state.stepSpan.end();
|
|
688
|
+
state.stepSpan = void 0;
|
|
689
|
+
state.stepContext = void 0;
|
|
690
|
+
}
|
|
691
|
+
onEmbedOperationStart(event) {
|
|
692
|
+
const telemetry = {
|
|
693
|
+
isEnabled: event.isEnabled,
|
|
694
|
+
recordInputs: event.recordInputs,
|
|
695
|
+
recordOutputs: event.recordOutputs,
|
|
696
|
+
functionId: event.functionId
|
|
697
|
+
};
|
|
698
|
+
const settings = {
|
|
699
|
+
maxRetries: event.maxRetries
|
|
700
|
+
};
|
|
701
|
+
const providerName = mapProviderName(event.provider);
|
|
702
|
+
const attributes = selectAttributes(telemetry, {
|
|
703
|
+
"gen_ai.operation.name": "embeddings",
|
|
704
|
+
"gen_ai.provider.name": providerName,
|
|
705
|
+
"gen_ai.request.model": event.modelId
|
|
706
|
+
});
|
|
707
|
+
const spanName = `embeddings ${event.modelId}`;
|
|
708
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
709
|
+
attributes,
|
|
710
|
+
kind: SpanKind.CLIENT
|
|
711
|
+
});
|
|
712
|
+
const rootContext = trace.setSpan(context.active(), rootSpan);
|
|
713
|
+
this.callStates.set(event.callId, {
|
|
714
|
+
operationId: event.operationId,
|
|
715
|
+
telemetry,
|
|
716
|
+
rootSpan,
|
|
717
|
+
rootContext,
|
|
718
|
+
stepSpan: void 0,
|
|
719
|
+
stepContext: void 0,
|
|
720
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
721
|
+
rerankSpan: void 0,
|
|
722
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
723
|
+
settings,
|
|
724
|
+
provider: event.provider,
|
|
725
|
+
modelId: event.modelId
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
onStepStart(event) {
|
|
729
|
+
var _a;
|
|
730
|
+
const state = this.getCallState(event.callId);
|
|
731
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
732
|
+
const { telemetry } = state;
|
|
733
|
+
const providerName = mapProviderName(event.provider);
|
|
734
|
+
const attributes = selectAttributes(telemetry, {
|
|
735
|
+
"gen_ai.operation.name": "chat",
|
|
736
|
+
"gen_ai.provider.name": providerName,
|
|
737
|
+
"gen_ai.request.model": event.modelId,
|
|
738
|
+
"gen_ai.request.frequency_penalty": state.settings.frequencyPenalty,
|
|
739
|
+
"gen_ai.request.max_tokens": state.settings.maxOutputTokens,
|
|
740
|
+
"gen_ai.request.presence_penalty": state.settings.presencePenalty,
|
|
741
|
+
"gen_ai.request.stop_sequences": state.settings.stopSequences,
|
|
742
|
+
"gen_ai.request.temperature": (_a = state.settings.temperature) != null ? _a : void 0,
|
|
743
|
+
"gen_ai.request.top_k": state.settings.topK,
|
|
744
|
+
"gen_ai.request.top_p": state.settings.topP,
|
|
745
|
+
"gen_ai.input.messages": {
|
|
746
|
+
input: () => event.promptMessages ? JSON.stringify(formatInputMessages(event.promptMessages)) : void 0
|
|
747
|
+
},
|
|
748
|
+
"gen_ai.tool.definitions": {
|
|
749
|
+
input: () => event.stepTools ? JSON.stringify(event.stepTools) : void 0
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
const spanName = `chat ${event.modelId}`;
|
|
753
|
+
state.stepSpan = this.tracer.startSpan(
|
|
754
|
+
spanName,
|
|
755
|
+
{ attributes, kind: SpanKind.CLIENT },
|
|
756
|
+
state.rootContext
|
|
757
|
+
);
|
|
758
|
+
state.stepContext = trace.setSpan(state.rootContext, state.stepSpan);
|
|
759
|
+
}
|
|
760
|
+
onToolCallStart(event) {
|
|
761
|
+
const state = this.getCallState(event.callId);
|
|
762
|
+
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
763
|
+
const { telemetry } = state;
|
|
764
|
+
const { toolCall } = event;
|
|
765
|
+
const attributes = selectAttributes(telemetry, {
|
|
766
|
+
"gen_ai.operation.name": "execute_tool",
|
|
767
|
+
"gen_ai.tool.name": toolCall.toolName,
|
|
768
|
+
"gen_ai.tool.call.id": toolCall.toolCallId,
|
|
769
|
+
"gen_ai.tool.type": "function",
|
|
770
|
+
"gen_ai.tool.call.arguments": {
|
|
771
|
+
input: () => JSON.stringify(toolCall.input)
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
const spanName = `execute_tool ${toolCall.toolName}`;
|
|
775
|
+
const toolSpan = this.tracer.startSpan(
|
|
776
|
+
spanName,
|
|
777
|
+
{ attributes, kind: SpanKind.INTERNAL },
|
|
778
|
+
state.stepContext
|
|
779
|
+
);
|
|
780
|
+
const toolContext = trace.setSpan(state.stepContext, toolSpan);
|
|
781
|
+
state.toolSpans.set(toolCall.toolCallId, {
|
|
782
|
+
span: toolSpan,
|
|
783
|
+
context: toolContext
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
onToolCallFinish(event) {
|
|
787
|
+
const state = this.getCallState(event.callId);
|
|
788
|
+
if (!state) return;
|
|
789
|
+
const toolSpanEntry = state.toolSpans.get(event.toolCall.toolCallId);
|
|
790
|
+
if (!toolSpanEntry) return;
|
|
791
|
+
const { span } = toolSpanEntry;
|
|
792
|
+
const { telemetry } = state;
|
|
793
|
+
if (event.success) {
|
|
794
|
+
try {
|
|
795
|
+
span.setAttributes(
|
|
796
|
+
selectAttributes(telemetry, {
|
|
797
|
+
"gen_ai.tool.call.result": {
|
|
798
|
+
output: () => JSON.stringify(event.output)
|
|
799
|
+
}
|
|
800
|
+
})
|
|
801
|
+
);
|
|
802
|
+
} catch (e) {
|
|
803
|
+
}
|
|
804
|
+
} else {
|
|
805
|
+
recordSpanError(span, event.error);
|
|
806
|
+
}
|
|
807
|
+
span.end();
|
|
808
|
+
state.toolSpans.delete(event.toolCall.toolCallId);
|
|
809
|
+
}
|
|
810
|
+
onStepFinish(event) {
|
|
811
|
+
var _a, _b, _c;
|
|
812
|
+
const state = this.getCallState(event.callId);
|
|
813
|
+
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
814
|
+
const { telemetry } = state;
|
|
815
|
+
state.stepSpan.setAttributes(
|
|
816
|
+
selectAttributes(telemetry, {
|
|
817
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
818
|
+
"gen_ai.response.id": event.response.id,
|
|
819
|
+
"gen_ai.response.model": event.response.modelId,
|
|
820
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
821
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
822
|
+
"gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.usage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.usage.cachedInputTokens,
|
|
823
|
+
"gen_ai.usage.cache_creation.input_tokens": (_c = event.usage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
|
|
824
|
+
"gen_ai.output.messages": {
|
|
825
|
+
output: () => {
|
|
826
|
+
var _a2;
|
|
827
|
+
return JSON.stringify(
|
|
828
|
+
formatOutputMessages({
|
|
829
|
+
text: (_a2 = event.text) != null ? _a2 : void 0,
|
|
830
|
+
reasoning: event.reasoning,
|
|
831
|
+
toolCalls: event.toolCalls,
|
|
832
|
+
files: event.files,
|
|
833
|
+
finishReason: event.finishReason
|
|
834
|
+
})
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
})
|
|
839
|
+
);
|
|
840
|
+
state.stepSpan.end();
|
|
841
|
+
state.stepSpan = void 0;
|
|
842
|
+
state.stepContext = void 0;
|
|
843
|
+
}
|
|
844
|
+
onFinish(event) {
|
|
845
|
+
const state = this.getCallState(event.callId);
|
|
846
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
847
|
+
if (state.operationId === "ai.embed" || state.operationId === "ai.embedMany") {
|
|
848
|
+
this.onEmbedOperationFinish(event);
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
if (state.operationId === "ai.rerank") {
|
|
852
|
+
this.onRerankOperationFinish(event);
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
if (state.operationId === "ai.generateObject" || state.operationId === "ai.streamObject") {
|
|
856
|
+
this.onObjectOperationFinish(event);
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
this.onGenerateFinish(event);
|
|
860
|
+
}
|
|
861
|
+
onGenerateFinish(event) {
|
|
862
|
+
var _a, _b, _c;
|
|
863
|
+
const state = this.getCallState(event.callId);
|
|
864
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
865
|
+
const { telemetry } = state;
|
|
866
|
+
state.rootSpan.setAttributes(
|
|
867
|
+
selectAttributes(telemetry, {
|
|
868
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
869
|
+
"gen_ai.usage.input_tokens": event.totalUsage.inputTokens,
|
|
870
|
+
"gen_ai.usage.output_tokens": event.totalUsage.outputTokens,
|
|
871
|
+
"gen_ai.usage.cache_read.input_tokens": (_b = (_a = event.totalUsage.inputTokenDetails) == null ? void 0 : _a.cacheReadTokens) != null ? _b : event.totalUsage.cachedInputTokens,
|
|
872
|
+
"gen_ai.usage.cache_creation.input_tokens": (_c = event.totalUsage.inputTokenDetails) == null ? void 0 : _c.cacheWriteTokens,
|
|
873
|
+
"gen_ai.output.messages": {
|
|
874
|
+
output: () => {
|
|
875
|
+
var _a2;
|
|
876
|
+
return JSON.stringify(
|
|
877
|
+
formatOutputMessages({
|
|
878
|
+
text: (_a2 = event.text) != null ? _a2 : void 0,
|
|
879
|
+
reasoning: event.reasoning,
|
|
880
|
+
toolCalls: event.toolCalls,
|
|
881
|
+
files: event.files,
|
|
882
|
+
finishReason: event.finishReason
|
|
883
|
+
})
|
|
884
|
+
);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
})
|
|
888
|
+
);
|
|
889
|
+
state.rootSpan.end();
|
|
890
|
+
this.cleanupCallState(event.callId);
|
|
891
|
+
}
|
|
892
|
+
onObjectOperationFinish(event) {
|
|
893
|
+
const state = this.getCallState(event.callId);
|
|
894
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
895
|
+
const { telemetry } = state;
|
|
896
|
+
state.rootSpan.setAttributes(
|
|
897
|
+
selectAttributes(telemetry, {
|
|
898
|
+
"gen_ai.response.finish_reasons": [event.finishReason],
|
|
899
|
+
"gen_ai.usage.input_tokens": event.usage.inputTokens,
|
|
900
|
+
"gen_ai.usage.output_tokens": event.usage.outputTokens,
|
|
901
|
+
"gen_ai.usage.cache_read.input_tokens": event.usage.cachedInputTokens,
|
|
902
|
+
"gen_ai.output.messages": {
|
|
903
|
+
output: () => event.object != null ? JSON.stringify(
|
|
904
|
+
formatObjectOutputMessages({
|
|
905
|
+
objectText: JSON.stringify(event.object),
|
|
906
|
+
finishReason: event.finishReason
|
|
907
|
+
})
|
|
908
|
+
) : void 0
|
|
909
|
+
}
|
|
910
|
+
})
|
|
911
|
+
);
|
|
912
|
+
state.rootSpan.end();
|
|
913
|
+
this.cleanupCallState(event.callId);
|
|
914
|
+
}
|
|
915
|
+
onEmbedOperationFinish(event) {
|
|
916
|
+
const state = this.getCallState(event.callId);
|
|
917
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
918
|
+
const { telemetry } = state;
|
|
919
|
+
state.rootSpan.setAttributes(
|
|
920
|
+
selectAttributes(telemetry, {
|
|
921
|
+
"gen_ai.usage.input_tokens": event.usage.tokens
|
|
922
|
+
})
|
|
923
|
+
);
|
|
924
|
+
state.rootSpan.end();
|
|
925
|
+
this.cleanupCallState(event.callId);
|
|
926
|
+
}
|
|
927
|
+
onEmbedStart(event) {
|
|
928
|
+
const state = this.getCallState(event.callId);
|
|
929
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
930
|
+
const { telemetry } = state;
|
|
931
|
+
const providerName = mapProviderName(state.provider);
|
|
932
|
+
const attributes = selectAttributes(telemetry, {
|
|
933
|
+
"gen_ai.operation.name": "embeddings",
|
|
934
|
+
"gen_ai.provider.name": providerName,
|
|
935
|
+
"gen_ai.request.model": state.modelId
|
|
936
|
+
});
|
|
937
|
+
const spanName = `embeddings ${state.modelId}`;
|
|
938
|
+
const embedSpan = this.tracer.startSpan(
|
|
939
|
+
spanName,
|
|
940
|
+
{ attributes, kind: SpanKind.CLIENT },
|
|
941
|
+
state.rootContext
|
|
942
|
+
);
|
|
943
|
+
const embedContext = trace.setSpan(state.rootContext, embedSpan);
|
|
944
|
+
state.embedSpans.set(event.embedCallId, {
|
|
945
|
+
span: embedSpan,
|
|
946
|
+
context: embedContext
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
onEmbedFinish(event) {
|
|
950
|
+
const state = this.getCallState(event.callId);
|
|
951
|
+
if (!state) return;
|
|
952
|
+
const embedSpanEntry = state.embedSpans.get(event.embedCallId);
|
|
953
|
+
if (!embedSpanEntry) return;
|
|
954
|
+
const { span } = embedSpanEntry;
|
|
955
|
+
const { telemetry } = state;
|
|
956
|
+
span.setAttributes(
|
|
957
|
+
selectAttributes(telemetry, {
|
|
958
|
+
"gen_ai.usage.input_tokens": event.usage.tokens
|
|
959
|
+
})
|
|
960
|
+
);
|
|
961
|
+
span.end();
|
|
962
|
+
state.embedSpans.delete(event.embedCallId);
|
|
963
|
+
}
|
|
964
|
+
onRerankOperationStart(event) {
|
|
965
|
+
const telemetry = {
|
|
966
|
+
isEnabled: event.isEnabled,
|
|
967
|
+
recordInputs: event.recordInputs,
|
|
968
|
+
recordOutputs: event.recordOutputs,
|
|
969
|
+
functionId: event.functionId
|
|
970
|
+
};
|
|
971
|
+
const settings = {
|
|
972
|
+
maxRetries: event.maxRetries
|
|
973
|
+
};
|
|
974
|
+
const providerName = mapProviderName(event.provider);
|
|
975
|
+
const attributes = selectAttributes(telemetry, {
|
|
976
|
+
"gen_ai.operation.name": "rerank",
|
|
977
|
+
"gen_ai.provider.name": providerName,
|
|
978
|
+
"gen_ai.request.model": event.modelId
|
|
979
|
+
});
|
|
980
|
+
const spanName = `rerank ${event.modelId}`;
|
|
981
|
+
const rootSpan = this.tracer.startSpan(spanName, {
|
|
982
|
+
attributes,
|
|
983
|
+
kind: SpanKind.CLIENT
|
|
984
|
+
});
|
|
985
|
+
const rootContext = trace.setSpan(context.active(), rootSpan);
|
|
986
|
+
this.callStates.set(event.callId, {
|
|
987
|
+
operationId: event.operationId,
|
|
988
|
+
telemetry,
|
|
989
|
+
rootSpan,
|
|
990
|
+
rootContext,
|
|
991
|
+
stepSpan: void 0,
|
|
992
|
+
stepContext: void 0,
|
|
993
|
+
embedSpans: /* @__PURE__ */ new Map(),
|
|
994
|
+
rerankSpan: void 0,
|
|
995
|
+
toolSpans: /* @__PURE__ */ new Map(),
|
|
996
|
+
settings,
|
|
997
|
+
provider: event.provider,
|
|
998
|
+
modelId: event.modelId
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
onRerankOperationFinish(event) {
|
|
1002
|
+
const state = this.getCallState(event.callId);
|
|
1003
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1004
|
+
state.rootSpan.end();
|
|
1005
|
+
this.cleanupCallState(event.callId);
|
|
1006
|
+
}
|
|
1007
|
+
onRerankStart(event) {
|
|
1008
|
+
const state = this.getCallState(event.callId);
|
|
1009
|
+
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
1010
|
+
const { telemetry } = state;
|
|
1011
|
+
const providerName = mapProviderName(state.provider);
|
|
1012
|
+
const attributes = selectAttributes(telemetry, {
|
|
1013
|
+
"gen_ai.operation.name": "rerank",
|
|
1014
|
+
"gen_ai.provider.name": providerName,
|
|
1015
|
+
"gen_ai.request.model": state.modelId
|
|
1016
|
+
});
|
|
1017
|
+
const spanName = `rerank ${state.modelId}`;
|
|
1018
|
+
const rerankSpan = this.tracer.startSpan(
|
|
1019
|
+
spanName,
|
|
1020
|
+
{ attributes, kind: SpanKind.CLIENT },
|
|
1021
|
+
state.rootContext
|
|
1022
|
+
);
|
|
1023
|
+
const rerankContext = trace.setSpan(state.rootContext, rerankSpan);
|
|
1024
|
+
state.rerankSpan = { span: rerankSpan, context: rerankContext };
|
|
1025
|
+
}
|
|
1026
|
+
onRerankFinish(event) {
|
|
1027
|
+
const state = this.getCallState(event.callId);
|
|
1028
|
+
if (!(state == null ? void 0 : state.rerankSpan)) return;
|
|
1029
|
+
const { span } = state.rerankSpan;
|
|
1030
|
+
span.end();
|
|
1031
|
+
state.rerankSpan = void 0;
|
|
1032
|
+
}
|
|
1033
|
+
onChunk(_event) {
|
|
1034
|
+
}
|
|
1035
|
+
onError(error) {
|
|
1036
|
+
var _a;
|
|
1037
|
+
const event = error;
|
|
1038
|
+
if (!(event == null ? void 0 : event.callId)) return;
|
|
1039
|
+
const state = this.getCallState(event.callId);
|
|
1040
|
+
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
1041
|
+
const actualError = (_a = event.error) != null ? _a : error;
|
|
1042
|
+
if (state.stepSpan) {
|
|
1043
|
+
recordSpanError(state.stepSpan, actualError);
|
|
1044
|
+
state.stepSpan.end();
|
|
1045
|
+
}
|
|
1046
|
+
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
1047
|
+
recordSpanError(embedSpan, actualError);
|
|
1048
|
+
embedSpan.end();
|
|
1049
|
+
}
|
|
1050
|
+
state.embedSpans.clear();
|
|
1051
|
+
if (state.rerankSpan) {
|
|
1052
|
+
recordSpanError(state.rerankSpan.span, actualError);
|
|
1053
|
+
state.rerankSpan.span.end();
|
|
1054
|
+
state.rerankSpan = void 0;
|
|
1055
|
+
}
|
|
1056
|
+
recordSpanError(state.rootSpan, actualError);
|
|
1057
|
+
state.rootSpan.end();
|
|
1058
|
+
this.cleanupCallState(event.callId);
|
|
15
1059
|
}
|
|
16
|
-
return to;
|
|
17
1060
|
};
|
|
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
1061
|
|
|
27
1062
|
// src/open-telemetry-integration.ts
|
|
28
|
-
|
|
1063
|
+
import {
|
|
1064
|
+
context as context2,
|
|
1065
|
+
SpanStatusCode as SpanStatusCode2,
|
|
1066
|
+
trace as trace2
|
|
1067
|
+
} from "@opentelemetry/api";
|
|
29
1068
|
|
|
30
1069
|
// src/assemble-operation-name.ts
|
|
31
1070
|
function assembleOperationName({
|
|
@@ -46,10 +1085,9 @@ function assembleOperationName({
|
|
|
46
1085
|
function getBaseTelemetryAttributes({
|
|
47
1086
|
model,
|
|
48
1087
|
settings,
|
|
49
|
-
|
|
50
|
-
|
|
1088
|
+
headers,
|
|
1089
|
+
context: context3
|
|
51
1090
|
}) {
|
|
52
|
-
var _a;
|
|
53
1091
|
return {
|
|
54
1092
|
"ai.model.provider": model.provider,
|
|
55
1093
|
"ai.model.id": model.modelId,
|
|
@@ -58,16 +1096,13 @@ function getBaseTelemetryAttributes({
|
|
|
58
1096
|
attributes[`ai.settings.${key}`] = value;
|
|
59
1097
|
return attributes;
|
|
60
1098
|
}, {}),
|
|
61
|
-
// add
|
|
62
|
-
...Object.entries(
|
|
63
|
-
(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
},
|
|
69
|
-
{}
|
|
70
|
-
),
|
|
1099
|
+
// add context as attributes:
|
|
1100
|
+
...Object.entries(context3 != null ? context3 : {}).reduce((attributes, [key, value]) => {
|
|
1101
|
+
if (value != void 0) {
|
|
1102
|
+
attributes[`ai.settings.context.${key}`] = value;
|
|
1103
|
+
}
|
|
1104
|
+
return attributes;
|
|
1105
|
+
}, {}),
|
|
71
1106
|
// request headers
|
|
72
1107
|
...Object.entries(headers != null ? headers : {}).reduce((attributes, [key, value]) => {
|
|
73
1108
|
if (value !== void 0) {
|
|
@@ -79,7 +1114,7 @@ function getBaseTelemetryAttributes({
|
|
|
79
1114
|
}
|
|
80
1115
|
|
|
81
1116
|
// src/stringify-for-telemetry.ts
|
|
82
|
-
|
|
1117
|
+
import { convertDataContentToBase64String as convertDataContentToBase64String2 } from "ai";
|
|
83
1118
|
function stringifyForTelemetry(prompt) {
|
|
84
1119
|
return JSON.stringify(
|
|
85
1120
|
prompt.map((message) => ({
|
|
@@ -87,7 +1122,7 @@ function stringifyForTelemetry(prompt) {
|
|
|
87
1122
|
content: typeof message.content === "string" ? message.content : message.content.map(
|
|
88
1123
|
(part) => part.type === "file" ? {
|
|
89
1124
|
...part,
|
|
90
|
-
data: part.data instanceof Uint8Array ? (
|
|
1125
|
+
data: part.data instanceof Uint8Array ? convertDataContentToBase64String2(part.data) : part.data
|
|
91
1126
|
} : part
|
|
92
1127
|
)
|
|
93
1128
|
}))
|
|
@@ -95,7 +1130,7 @@ function stringifyForTelemetry(prompt) {
|
|
|
95
1130
|
}
|
|
96
1131
|
|
|
97
1132
|
// src/open-telemetry-integration.ts
|
|
98
|
-
function
|
|
1133
|
+
function recordSpanError2(span, error) {
|
|
99
1134
|
if (error instanceof Error) {
|
|
100
1135
|
span.recordException({
|
|
101
1136
|
name: error.name,
|
|
@@ -103,18 +1138,18 @@ function recordSpanError(span, error) {
|
|
|
103
1138
|
stack: error.stack
|
|
104
1139
|
});
|
|
105
1140
|
span.setStatus({
|
|
106
|
-
code:
|
|
1141
|
+
code: SpanStatusCode2.ERROR,
|
|
107
1142
|
message: error.message
|
|
108
1143
|
});
|
|
109
1144
|
} else {
|
|
110
|
-
span.setStatus({ code:
|
|
1145
|
+
span.setStatus({ code: SpanStatusCode2.ERROR });
|
|
111
1146
|
}
|
|
112
1147
|
}
|
|
113
|
-
function
|
|
114
|
-
return (telemetry == null ? void 0 : telemetry.isEnabled)
|
|
1148
|
+
function shouldRecord2(telemetry) {
|
|
1149
|
+
return (telemetry == null ? void 0 : telemetry.isEnabled) !== false;
|
|
115
1150
|
}
|
|
116
|
-
function
|
|
117
|
-
if (!
|
|
1151
|
+
function selectAttributes2(telemetry, attributes) {
|
|
1152
|
+
if (!shouldRecord2(telemetry)) {
|
|
118
1153
|
return {};
|
|
119
1154
|
}
|
|
120
1155
|
const result = {};
|
|
@@ -140,7 +1175,7 @@ var OpenTelemetryIntegration = class {
|
|
|
140
1175
|
constructor(options = {}) {
|
|
141
1176
|
this.callStates = /* @__PURE__ */ new Map();
|
|
142
1177
|
var _a;
|
|
143
|
-
this.tracer = (_a = options.tracer) != null ? _a :
|
|
1178
|
+
this.tracer = (_a = options.tracer) != null ? _a : trace2.getTracer("ai");
|
|
144
1179
|
}
|
|
145
1180
|
getCallState(callId) {
|
|
146
1181
|
return this.callStates.get(callId);
|
|
@@ -158,10 +1193,10 @@ var OpenTelemetryIntegration = class {
|
|
|
158
1193
|
if (toolSpanEntry == null) {
|
|
159
1194
|
return execute();
|
|
160
1195
|
}
|
|
161
|
-
return
|
|
1196
|
+
return context2.with(toolSpanEntry.context, execute);
|
|
162
1197
|
}
|
|
163
1198
|
onStart(event) {
|
|
164
|
-
if (event.isEnabled
|
|
1199
|
+
if (event.isEnabled === false) return;
|
|
165
1200
|
if (event.operationId === "ai.embed" || event.operationId === "ai.embedMany") {
|
|
166
1201
|
this.onEmbedOperationStart(event);
|
|
167
1202
|
return;
|
|
@@ -181,8 +1216,7 @@ var OpenTelemetryIntegration = class {
|
|
|
181
1216
|
isEnabled: event.isEnabled,
|
|
182
1217
|
recordInputs: event.recordInputs,
|
|
183
1218
|
recordOutputs: event.recordOutputs,
|
|
184
|
-
functionId: event.functionId
|
|
185
|
-
metadata: event.metadata
|
|
1219
|
+
functionId: event.functionId
|
|
186
1220
|
};
|
|
187
1221
|
const settings = {
|
|
188
1222
|
maxOutputTokens: event.maxOutputTokens,
|
|
@@ -197,11 +1231,11 @@ var OpenTelemetryIntegration = class {
|
|
|
197
1231
|
};
|
|
198
1232
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
199
1233
|
model: { provider: event.provider, modelId: event.modelId },
|
|
200
|
-
telemetry,
|
|
201
1234
|
headers: event.headers,
|
|
202
|
-
settings
|
|
1235
|
+
settings,
|
|
1236
|
+
context: event.runtimeContext
|
|
203
1237
|
});
|
|
204
|
-
const attributes =
|
|
1238
|
+
const attributes = selectAttributes2(telemetry, {
|
|
205
1239
|
...assembleOperationName({
|
|
206
1240
|
operationId: event.operationId,
|
|
207
1241
|
telemetry
|
|
@@ -218,7 +1252,7 @@ var OpenTelemetryIntegration = class {
|
|
|
218
1252
|
}
|
|
219
1253
|
});
|
|
220
1254
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
221
|
-
const rootContext =
|
|
1255
|
+
const rootContext = trace2.setSpan(context2.active(), rootSpan);
|
|
222
1256
|
this.callStates.set(event.callId, {
|
|
223
1257
|
operationId: event.operationId,
|
|
224
1258
|
telemetry,
|
|
@@ -238,8 +1272,7 @@ var OpenTelemetryIntegration = class {
|
|
|
238
1272
|
isEnabled: event.isEnabled,
|
|
239
1273
|
recordInputs: event.recordInputs,
|
|
240
1274
|
recordOutputs: event.recordOutputs,
|
|
241
|
-
functionId: event.functionId
|
|
242
|
-
metadata: event.metadata
|
|
1275
|
+
functionId: event.functionId
|
|
243
1276
|
};
|
|
244
1277
|
const settings = {
|
|
245
1278
|
maxOutputTokens: event.maxOutputTokens,
|
|
@@ -253,11 +1286,11 @@ var OpenTelemetryIntegration = class {
|
|
|
253
1286
|
};
|
|
254
1287
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
255
1288
|
model: { provider: event.provider, modelId: event.modelId },
|
|
256
|
-
telemetry,
|
|
257
1289
|
headers: event.headers,
|
|
258
|
-
settings
|
|
1290
|
+
settings,
|
|
1291
|
+
context: void 0
|
|
259
1292
|
});
|
|
260
|
-
const attributes =
|
|
1293
|
+
const attributes = selectAttributes2(telemetry, {
|
|
261
1294
|
...assembleOperationName({
|
|
262
1295
|
operationId: event.operationId,
|
|
263
1296
|
telemetry
|
|
@@ -276,7 +1309,7 @@ var OpenTelemetryIntegration = class {
|
|
|
276
1309
|
"ai.settings.output": event.output
|
|
277
1310
|
});
|
|
278
1311
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
279
|
-
const rootContext =
|
|
1312
|
+
const rootContext = trace2.setSpan(context2.active(), rootSpan);
|
|
280
1313
|
this.callStates.set(event.callId, {
|
|
281
1314
|
operationId: event.operationId,
|
|
282
1315
|
telemetry,
|
|
@@ -298,7 +1331,7 @@ var OpenTelemetryIntegration = class {
|
|
|
298
1331
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
299
1332
|
const { telemetry } = state;
|
|
300
1333
|
const stepOperationId = state.operationId === "ai.streamObject" ? "ai.streamObject.doStream" : "ai.generateObject.doGenerate";
|
|
301
|
-
const attributes =
|
|
1334
|
+
const attributes = selectAttributes2(telemetry, {
|
|
302
1335
|
...assembleOperationName({
|
|
303
1336
|
operationId: stepOperationId,
|
|
304
1337
|
telemetry
|
|
@@ -321,7 +1354,7 @@ var OpenTelemetryIntegration = class {
|
|
|
321
1354
|
{ attributes },
|
|
322
1355
|
state.rootContext
|
|
323
1356
|
);
|
|
324
|
-
state.stepContext =
|
|
1357
|
+
state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
|
|
325
1358
|
}
|
|
326
1359
|
/** @deprecated */
|
|
327
1360
|
onObjectStepFinish(event) {
|
|
@@ -329,7 +1362,7 @@ var OpenTelemetryIntegration = class {
|
|
|
329
1362
|
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
330
1363
|
const { telemetry } = state;
|
|
331
1364
|
state.stepSpan.setAttributes(
|
|
332
|
-
|
|
1365
|
+
selectAttributes2(telemetry, {
|
|
333
1366
|
"ai.response.finishReason": event.finishReason,
|
|
334
1367
|
"ai.response.object": {
|
|
335
1368
|
output: () => {
|
|
@@ -373,21 +1406,20 @@ var OpenTelemetryIntegration = class {
|
|
|
373
1406
|
isEnabled: event.isEnabled,
|
|
374
1407
|
recordInputs: event.recordInputs,
|
|
375
1408
|
recordOutputs: event.recordOutputs,
|
|
376
|
-
functionId: event.functionId
|
|
377
|
-
metadata: event.metadata
|
|
1409
|
+
functionId: event.functionId
|
|
378
1410
|
};
|
|
379
1411
|
const settings = {
|
|
380
1412
|
maxRetries: event.maxRetries
|
|
381
1413
|
};
|
|
382
1414
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
383
1415
|
model: { provider: event.provider, modelId: event.modelId },
|
|
384
|
-
telemetry,
|
|
385
1416
|
headers: event.headers,
|
|
386
|
-
settings
|
|
1417
|
+
settings,
|
|
1418
|
+
context: void 0
|
|
387
1419
|
});
|
|
388
1420
|
const value = event.value;
|
|
389
1421
|
const isMany = event.operationId === "ai.embedMany";
|
|
390
|
-
const attributes =
|
|
1422
|
+
const attributes = selectAttributes2(telemetry, {
|
|
391
1423
|
...assembleOperationName({
|
|
392
1424
|
operationId: event.operationId,
|
|
393
1425
|
telemetry
|
|
@@ -404,7 +1436,7 @@ var OpenTelemetryIntegration = class {
|
|
|
404
1436
|
}
|
|
405
1437
|
});
|
|
406
1438
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
407
|
-
const rootContext =
|
|
1439
|
+
const rootContext = trace2.setSpan(context2.active(), rootSpan);
|
|
408
1440
|
this.callStates.set(event.callId, {
|
|
409
1441
|
operationId: event.operationId,
|
|
410
1442
|
telemetry,
|
|
@@ -425,7 +1457,7 @@ var OpenTelemetryIntegration = class {
|
|
|
425
1457
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
426
1458
|
const { telemetry } = state;
|
|
427
1459
|
const stepOperationId = state.operationId === "ai.streamText" ? "ai.streamText.doStream" : "ai.generateText.doGenerate";
|
|
428
|
-
const attributes =
|
|
1460
|
+
const attributes = selectAttributes2(telemetry, {
|
|
429
1461
|
...assembleOperationName({
|
|
430
1462
|
operationId: stepOperationId,
|
|
431
1463
|
telemetry
|
|
@@ -460,14 +1492,14 @@ var OpenTelemetryIntegration = class {
|
|
|
460
1492
|
{ attributes },
|
|
461
1493
|
state.rootContext
|
|
462
1494
|
);
|
|
463
|
-
state.stepContext =
|
|
1495
|
+
state.stepContext = trace2.setSpan(state.rootContext, state.stepSpan);
|
|
464
1496
|
}
|
|
465
1497
|
onToolCallStart(event) {
|
|
466
1498
|
const state = this.getCallState(event.callId);
|
|
467
1499
|
if (!(state == null ? void 0 : state.stepContext)) return;
|
|
468
1500
|
const { telemetry } = state;
|
|
469
1501
|
const { toolCall } = event;
|
|
470
|
-
const attributes =
|
|
1502
|
+
const attributes = selectAttributes2(telemetry, {
|
|
471
1503
|
...assembleOperationName({
|
|
472
1504
|
operationId: "ai.toolCall",
|
|
473
1505
|
telemetry
|
|
@@ -483,7 +1515,7 @@ var OpenTelemetryIntegration = class {
|
|
|
483
1515
|
{ attributes },
|
|
484
1516
|
state.stepContext
|
|
485
1517
|
);
|
|
486
|
-
const toolContext =
|
|
1518
|
+
const toolContext = trace2.setSpan(state.stepContext, toolSpan);
|
|
487
1519
|
state.toolSpans.set(toolCall.toolCallId, {
|
|
488
1520
|
span: toolSpan,
|
|
489
1521
|
context: toolContext
|
|
@@ -499,16 +1531,16 @@ var OpenTelemetryIntegration = class {
|
|
|
499
1531
|
if (event.success) {
|
|
500
1532
|
try {
|
|
501
1533
|
span.setAttributes(
|
|
502
|
-
|
|
1534
|
+
selectAttributes2(telemetry, {
|
|
503
1535
|
"ai.toolCall.result": {
|
|
504
1536
|
output: () => JSON.stringify(event.output)
|
|
505
1537
|
}
|
|
506
1538
|
})
|
|
507
1539
|
);
|
|
508
|
-
} catch (
|
|
1540
|
+
} catch (e) {
|
|
509
1541
|
}
|
|
510
1542
|
} else {
|
|
511
|
-
|
|
1543
|
+
recordSpanError2(span, event.error);
|
|
512
1544
|
}
|
|
513
1545
|
span.end();
|
|
514
1546
|
state.toolSpans.delete(event.toolCall.toolCallId);
|
|
@@ -519,7 +1551,7 @@ var OpenTelemetryIntegration = class {
|
|
|
519
1551
|
if (!(state == null ? void 0 : state.stepSpan)) return;
|
|
520
1552
|
const { telemetry } = state;
|
|
521
1553
|
state.stepSpan.setAttributes(
|
|
522
|
-
|
|
1554
|
+
selectAttributes2(telemetry, {
|
|
523
1555
|
"ai.response.finishReason": event.finishReason,
|
|
524
1556
|
"ai.response.text": {
|
|
525
1557
|
output: () => {
|
|
@@ -596,7 +1628,7 @@ var OpenTelemetryIntegration = class {
|
|
|
596
1628
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
597
1629
|
const { telemetry } = state;
|
|
598
1630
|
state.rootSpan.setAttributes(
|
|
599
|
-
|
|
1631
|
+
selectAttributes2(telemetry, {
|
|
600
1632
|
"ai.response.finishReason": event.finishReason,
|
|
601
1633
|
"ai.response.text": {
|
|
602
1634
|
output: () => {
|
|
@@ -646,7 +1678,7 @@ var OpenTelemetryIntegration = class {
|
|
|
646
1678
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
647
1679
|
const { telemetry } = state;
|
|
648
1680
|
state.rootSpan.setAttributes(
|
|
649
|
-
|
|
1681
|
+
selectAttributes2(telemetry, {
|
|
650
1682
|
"ai.response.finishReason": event.finishReason,
|
|
651
1683
|
"ai.response.object": {
|
|
652
1684
|
output: () => event.object != null ? JSON.stringify(event.object) : void 0
|
|
@@ -668,7 +1700,7 @@ var OpenTelemetryIntegration = class {
|
|
|
668
1700
|
const { telemetry } = state;
|
|
669
1701
|
const isMany = state.operationId === "ai.embedMany";
|
|
670
1702
|
state.rootSpan.setAttributes(
|
|
671
|
-
|
|
1703
|
+
selectAttributes2(telemetry, {
|
|
672
1704
|
...isMany ? {
|
|
673
1705
|
"ai.embeddings": {
|
|
674
1706
|
output: () => event.embedding.map((e) => JSON.stringify(e))
|
|
@@ -688,7 +1720,7 @@ var OpenTelemetryIntegration = class {
|
|
|
688
1720
|
const state = this.getCallState(event.callId);
|
|
689
1721
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
690
1722
|
const { telemetry } = state;
|
|
691
|
-
const attributes =
|
|
1723
|
+
const attributes = selectAttributes2(telemetry, {
|
|
692
1724
|
...assembleOperationName({
|
|
693
1725
|
operationId: event.operationId,
|
|
694
1726
|
telemetry
|
|
@@ -703,7 +1735,7 @@ var OpenTelemetryIntegration = class {
|
|
|
703
1735
|
{ attributes },
|
|
704
1736
|
state.rootContext
|
|
705
1737
|
);
|
|
706
|
-
const embedContext =
|
|
1738
|
+
const embedContext = trace2.setSpan(state.rootContext, embedSpan);
|
|
707
1739
|
state.embedSpans.set(event.embedCallId, {
|
|
708
1740
|
span: embedSpan,
|
|
709
1741
|
context: embedContext
|
|
@@ -717,7 +1749,7 @@ var OpenTelemetryIntegration = class {
|
|
|
717
1749
|
const { span } = embedSpanEntry;
|
|
718
1750
|
const { telemetry } = state;
|
|
719
1751
|
span.setAttributes(
|
|
720
|
-
|
|
1752
|
+
selectAttributes2(telemetry, {
|
|
721
1753
|
"ai.embeddings": {
|
|
722
1754
|
output: () => event.embeddings.map((embedding) => JSON.stringify(embedding))
|
|
723
1755
|
},
|
|
@@ -732,19 +1764,18 @@ var OpenTelemetryIntegration = class {
|
|
|
732
1764
|
isEnabled: event.isEnabled,
|
|
733
1765
|
recordInputs: event.recordInputs,
|
|
734
1766
|
recordOutputs: event.recordOutputs,
|
|
735
|
-
functionId: event.functionId
|
|
736
|
-
metadata: event.metadata
|
|
1767
|
+
functionId: event.functionId
|
|
737
1768
|
};
|
|
738
1769
|
const settings = {
|
|
739
1770
|
maxRetries: event.maxRetries
|
|
740
1771
|
};
|
|
741
1772
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
742
1773
|
model: { provider: event.provider, modelId: event.modelId },
|
|
743
|
-
telemetry,
|
|
744
1774
|
headers: event.headers,
|
|
745
|
-
settings
|
|
1775
|
+
settings,
|
|
1776
|
+
context: void 0
|
|
746
1777
|
});
|
|
747
|
-
const attributes =
|
|
1778
|
+
const attributes = selectAttributes2(telemetry, {
|
|
748
1779
|
...assembleOperationName({
|
|
749
1780
|
operationId: event.operationId,
|
|
750
1781
|
telemetry
|
|
@@ -755,7 +1786,7 @@ var OpenTelemetryIntegration = class {
|
|
|
755
1786
|
}
|
|
756
1787
|
});
|
|
757
1788
|
const rootSpan = this.tracer.startSpan(event.operationId, { attributes });
|
|
758
|
-
const rootContext =
|
|
1789
|
+
const rootContext = trace2.setSpan(context2.active(), rootSpan);
|
|
759
1790
|
this.callStates.set(event.callId, {
|
|
760
1791
|
operationId: event.operationId,
|
|
761
1792
|
telemetry,
|
|
@@ -780,7 +1811,7 @@ var OpenTelemetryIntegration = class {
|
|
|
780
1811
|
const state = this.getCallState(event.callId);
|
|
781
1812
|
if (!(state == null ? void 0 : state.rootSpan) || !state.rootContext) return;
|
|
782
1813
|
const { telemetry } = state;
|
|
783
|
-
const attributes =
|
|
1814
|
+
const attributes = selectAttributes2(telemetry, {
|
|
784
1815
|
...assembleOperationName({
|
|
785
1816
|
operationId: event.operationId,
|
|
786
1817
|
telemetry
|
|
@@ -795,7 +1826,7 @@ var OpenTelemetryIntegration = class {
|
|
|
795
1826
|
{ attributes },
|
|
796
1827
|
state.rootContext
|
|
797
1828
|
);
|
|
798
|
-
const rerankContext =
|
|
1829
|
+
const rerankContext = trace2.setSpan(state.rootContext, rerankSpan);
|
|
799
1830
|
state.rerankSpan = { span: rerankSpan, context: rerankContext };
|
|
800
1831
|
}
|
|
801
1832
|
onRerankFinish(event) {
|
|
@@ -804,7 +1835,7 @@ var OpenTelemetryIntegration = class {
|
|
|
804
1835
|
const { span } = state.rerankSpan;
|
|
805
1836
|
const { telemetry } = state;
|
|
806
1837
|
span.setAttributes(
|
|
807
|
-
|
|
1838
|
+
selectAttributes2(telemetry, {
|
|
808
1839
|
"ai.ranking.type": event.documentsType,
|
|
809
1840
|
"ai.ranking": {
|
|
810
1841
|
output: () => event.ranking.map((r) => JSON.stringify(r))
|
|
@@ -843,26 +1874,26 @@ var OpenTelemetryIntegration = class {
|
|
|
843
1874
|
if (!(state == null ? void 0 : state.rootSpan)) return;
|
|
844
1875
|
const actualError = (_a = event.error) != null ? _a : error;
|
|
845
1876
|
if (state.stepSpan) {
|
|
846
|
-
|
|
1877
|
+
recordSpanError2(state.stepSpan, actualError);
|
|
847
1878
|
state.stepSpan.end();
|
|
848
1879
|
}
|
|
849
1880
|
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
850
|
-
|
|
1881
|
+
recordSpanError2(embedSpan, actualError);
|
|
851
1882
|
embedSpan.end();
|
|
852
1883
|
}
|
|
853
1884
|
state.embedSpans.clear();
|
|
854
1885
|
if (state.rerankSpan) {
|
|
855
|
-
|
|
1886
|
+
recordSpanError2(state.rerankSpan.span, actualError);
|
|
856
1887
|
state.rerankSpan.span.end();
|
|
857
1888
|
state.rerankSpan = void 0;
|
|
858
1889
|
}
|
|
859
|
-
|
|
1890
|
+
recordSpanError2(state.rootSpan, actualError);
|
|
860
1891
|
state.rootSpan.end();
|
|
861
1892
|
this.cleanupCallState(event.callId);
|
|
862
1893
|
}
|
|
863
1894
|
};
|
|
864
|
-
|
|
865
|
-
|
|
1895
|
+
export {
|
|
1896
|
+
GenAIOpenTelemetryIntegration,
|
|
866
1897
|
OpenTelemetryIntegration
|
|
867
|
-
}
|
|
1898
|
+
};
|
|
868
1899
|
//# sourceMappingURL=index.js.map
|