@xsai-ext/telemetry 0.5.0-beta.5 → 0.5.0-beta.6
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/dist/index.js +39 -25
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ export * from 'xsai';
|
|
|
3
3
|
import { trace, SpanStatusCode } from '@opentelemetry/api';
|
|
4
4
|
import { createControlledStream, errorControllers, closeControllers, EventSourceParserStream, JsonMessageTransformStream } from '@xsai/shared-stream';
|
|
5
5
|
|
|
6
|
-
var version = "0.5.0-beta.
|
|
6
|
+
var version = "0.5.0-beta.6";
|
|
7
7
|
var pkg = {
|
|
8
8
|
version: version};
|
|
9
9
|
|
|
@@ -259,9 +259,9 @@ const streamText = (options) => {
|
|
|
259
259
|
const [fullStream, fullCtrl] = createControlledStream();
|
|
260
260
|
const [textStream, textCtrl] = createControlledStream();
|
|
261
261
|
const [reasoningTextStream, reasoningTextCtrl] = createControlledStream();
|
|
262
|
-
const pushEvent = (
|
|
263
|
-
eventCtrl.current?.enqueue(
|
|
264
|
-
void options.onEvent?.(
|
|
262
|
+
const pushEvent = (event) => {
|
|
263
|
+
eventCtrl.current?.enqueue(event);
|
|
264
|
+
void options.onEvent?.(event);
|
|
265
265
|
};
|
|
266
266
|
const pushStep = (step) => {
|
|
267
267
|
steps.push(step);
|
|
@@ -314,6 +314,9 @@ const streamText = (options) => {
|
|
|
314
314
|
const toolCalls = [];
|
|
315
315
|
const toolResults = [];
|
|
316
316
|
let finishReason = "other";
|
|
317
|
+
let reasoningStarted = false;
|
|
318
|
+
let textStarted = false;
|
|
319
|
+
pushEvent({ type: "step.start" });
|
|
317
320
|
await stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(new JsonMessageTransformStream()).pipeTo(new WritableStream({
|
|
318
321
|
abort: (reason) => {
|
|
319
322
|
errorControllers(reason, eventCtrl, fullCtrl, textCtrl, reasoningTextCtrl);
|
|
@@ -330,24 +333,39 @@ const streamText = (options) => {
|
|
|
330
333
|
if (choice.delta.reasoning != null) {
|
|
331
334
|
if (reasoningField !== "reasoning")
|
|
332
335
|
reasoningField = "reasoning";
|
|
333
|
-
|
|
336
|
+
if (!reasoningStarted) {
|
|
337
|
+
reasoningStarted = true;
|
|
338
|
+
pushEvent({ type: "reasoning.start" });
|
|
339
|
+
}
|
|
340
|
+
pushEvent({ delta: choice.delta.reasoning, type: "reasoning.delta" });
|
|
334
341
|
pushReasoningText(choice.delta.reasoning);
|
|
335
342
|
} else if (choice.delta.reasoning_content != null) {
|
|
336
343
|
if (reasoningField !== "reasoning_content")
|
|
337
344
|
reasoningField = "reasoning_content";
|
|
338
|
-
|
|
345
|
+
if (!reasoningStarted) {
|
|
346
|
+
reasoningStarted = true;
|
|
347
|
+
pushEvent({ type: "reasoning.start" });
|
|
348
|
+
}
|
|
349
|
+
pushEvent({ delta: choice.delta.reasoning_content, type: "reasoning.delta" });
|
|
339
350
|
pushReasoningText(choice.delta.reasoning_content);
|
|
340
351
|
}
|
|
341
352
|
if (choice.finish_reason != null)
|
|
342
353
|
finishReason = choice.finish_reason;
|
|
343
354
|
if (choice.delta.tool_calls?.length === 0 || choice.delta.tool_calls == null) {
|
|
344
355
|
if (choice.delta.content != null) {
|
|
345
|
-
|
|
356
|
+
if (!textStarted) {
|
|
357
|
+
textStarted = true;
|
|
358
|
+
pushEvent({ type: "text.start" });
|
|
359
|
+
}
|
|
360
|
+
pushEvent({ delta: choice.delta.content, type: "text.delta" });
|
|
346
361
|
pushText(choice.delta.content);
|
|
347
362
|
} else if (choice.delta.refusal != null) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
363
|
+
if (!textStarted) {
|
|
364
|
+
textStarted = true;
|
|
365
|
+
pushEvent({ type: "text.start" });
|
|
366
|
+
}
|
|
367
|
+
pushEvent({ delta: choice.delta.refusal, type: "text.delta" });
|
|
368
|
+
pushText(choice.delta.refusal);
|
|
351
369
|
}
|
|
352
370
|
} else {
|
|
353
371
|
for (const toolCall of choice.delta.tool_calls) {
|
|
@@ -363,21 +381,22 @@ const streamText = (options) => {
|
|
|
363
381
|
pushEvent({
|
|
364
382
|
toolCallId: toolCall.id,
|
|
365
383
|
toolName: toolCall.function.name,
|
|
366
|
-
type: "tool-call
|
|
384
|
+
type: "tool-call.start"
|
|
367
385
|
});
|
|
386
|
+
if (toolCall.function.arguments != null && toolCall.function.arguments.length > 0)
|
|
387
|
+
pushEvent({ delta: toolCall.function.arguments, type: "tool-call.delta" });
|
|
368
388
|
} else {
|
|
369
389
|
tool_calls[index].function.arguments += toolCall.function.arguments;
|
|
370
|
-
pushEvent({
|
|
371
|
-
argsTextDelta: toolCall.function.arguments,
|
|
372
|
-
toolCallId: toolCall.id,
|
|
373
|
-
toolName: toolCall.function.name ?? tool_calls[index].function.name,
|
|
374
|
-
type: "tool-call-delta"
|
|
375
|
-
});
|
|
390
|
+
pushEvent({ delta: toolCall.function.arguments, type: "tool-call.delta" });
|
|
376
391
|
}
|
|
377
392
|
}
|
|
378
393
|
}
|
|
379
394
|
}
|
|
380
395
|
}));
|
|
396
|
+
if (reasoningStarted)
|
|
397
|
+
pushEvent({ content: reasoningText ?? "", type: "reasoning.done" });
|
|
398
|
+
if (textStarted)
|
|
399
|
+
pushEvent({ content: text, type: "text.done" });
|
|
381
400
|
const message = {
|
|
382
401
|
...reasoningField != null ? { [reasoningField]: reasoningText } : {},
|
|
383
402
|
content: text,
|
|
@@ -404,15 +423,9 @@ const streamText = (options) => {
|
|
|
404
423
|
role: "tool",
|
|
405
424
|
tool_call_id: completionToolCall.toolCallId
|
|
406
425
|
});
|
|
407
|
-
pushEvent({ ...completionToolCall, type: "tool-call" });
|
|
408
|
-
pushEvent({ ...completionToolResult, type: "tool-result" });
|
|
426
|
+
pushEvent({ ...completionToolCall, type: "tool-call.done" });
|
|
427
|
+
pushEvent({ ...completionToolResult, type: "tool-result.done" });
|
|
409
428
|
}
|
|
410
|
-
} else {
|
|
411
|
-
pushEvent({
|
|
412
|
-
finishReason,
|
|
413
|
-
type: "finish",
|
|
414
|
-
usage
|
|
415
|
-
});
|
|
416
429
|
}
|
|
417
430
|
const step = {
|
|
418
431
|
finishReason,
|
|
@@ -428,6 +441,7 @@ const streamText = (options) => {
|
|
|
428
441
|
});
|
|
429
442
|
const willContinue = toolCalls.length > 0 && !stop;
|
|
430
443
|
pushStep(step);
|
|
444
|
+
pushEvent({ type: "step.done", usage });
|
|
431
445
|
span.setAttributes({
|
|
432
446
|
"gen_ai.response.finish_reasons": [step.finishReason],
|
|
433
447
|
...step.usage && {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai-ext/telemetry",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.0-beta.
|
|
4
|
+
"version": "0.5.0-beta.6",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@opentelemetry/api": "^1.9.1",
|
|
33
|
-
"
|
|
34
|
-
"xsai": "0.5.0-beta.
|
|
33
|
+
"xsai": "0.5.0-beta.6",
|
|
34
|
+
"@xsai/shared-stream": "0.5.0-beta.6"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@langfuse/otel": "^5.1.0",
|