@xsai-ext/telemetry 0.5.0-beta.3 → 0.5.0-beta.5
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 +17 -12
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { embed as embed$1, clean, embedMany as embedMany$1, trampoline, resolvePrepareStep, chat, responseJSON, normalizeChatCompletionUsage, InvalidResponseError, stepCountAtLeast, executeTool, shouldStop, DelayedPromise, objCamelToSnake
|
|
1
|
+
import { embed as embed$1, clean, embedMany as embedMany$1, trampoline, resolvePrepareStep, chat, responseJSON, normalizeChatCompletionUsage, computeTotalUsage, InvalidResponseError, stepCountAtLeast, executeTool, shouldStop, DelayedPromise, objCamelToSnake } from 'xsai';
|
|
2
2
|
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.5";
|
|
7
7
|
var pkg = {
|
|
8
8
|
version: version};
|
|
9
9
|
|
|
@@ -150,16 +150,17 @@ const generateText = async (options) => {
|
|
|
150
150
|
toolChoice: stepOptions.toolChoice
|
|
151
151
|
}, tracer), async (span) => chat({
|
|
152
152
|
...options2,
|
|
153
|
-
maxSteps: void 0,
|
|
154
153
|
messages: stepOptions.input,
|
|
155
154
|
model: stepOptions.model,
|
|
156
155
|
steps: void 0,
|
|
157
|
-
stopWhen: void 0,
|
|
158
156
|
stream: false,
|
|
159
|
-
|
|
157
|
+
telemetry: void 0,
|
|
158
|
+
toolChoice: stepOptions.toolChoice,
|
|
159
|
+
totalUsage: void 0
|
|
160
160
|
}).then(responseJSON).then(async (res) => {
|
|
161
161
|
const { choices } = res;
|
|
162
162
|
const usage = normalizeChatCompletionUsage(res.usage);
|
|
163
|
+
const totalUsage = computeTotalUsage(options2.totalUsage, usage);
|
|
163
164
|
if (!choices?.length) {
|
|
164
165
|
const responseBody = JSON.stringify(res);
|
|
165
166
|
throw new InvalidResponseError(`No choices returned, response body: ${responseBody}`, {
|
|
@@ -223,13 +224,15 @@ const generateText = async (options) => {
|
|
|
223
224
|
text: step.text,
|
|
224
225
|
toolCalls: step.toolCalls,
|
|
225
226
|
toolResults: step.toolResults,
|
|
227
|
+
totalUsage,
|
|
226
228
|
usage: step.usage
|
|
227
229
|
};
|
|
228
230
|
} else {
|
|
229
231
|
return async () => rawGenerateText({
|
|
230
232
|
...options2,
|
|
231
233
|
messages,
|
|
232
|
-
steps
|
|
234
|
+
steps,
|
|
235
|
+
totalUsage
|
|
233
236
|
});
|
|
234
237
|
}
|
|
235
238
|
}));
|
|
@@ -253,6 +256,7 @@ const streamText = (options) => {
|
|
|
253
256
|
const resultUsage = new DelayedPromise();
|
|
254
257
|
const resultTotalUsage = new DelayedPromise();
|
|
255
258
|
const [eventStream, eventCtrl] = createControlledStream();
|
|
259
|
+
const [fullStream, fullCtrl] = createControlledStream();
|
|
256
260
|
const [textStream, textCtrl] = createControlledStream();
|
|
257
261
|
const [reasoningTextStream, reasoningTextCtrl] = createControlledStream();
|
|
258
262
|
const pushEvent = (stepEvent) => {
|
|
@@ -281,12 +285,11 @@ const streamText = (options) => {
|
|
|
281
285
|
}, tracer), async (span) => {
|
|
282
286
|
const { body: stream } = await chat({
|
|
283
287
|
...options,
|
|
284
|
-
maxSteps: void 0,
|
|
285
288
|
messages: stepOptions.input,
|
|
286
289
|
model: stepOptions.model,
|
|
287
|
-
stopWhen: void 0,
|
|
288
290
|
stream: true,
|
|
289
291
|
streamOptions: options.streamOptions != null ? objCamelToSnake(options.streamOptions) : void 0,
|
|
292
|
+
telemetry: void 0,
|
|
290
293
|
toolChoice: stepOptions.toolChoice,
|
|
291
294
|
tools
|
|
292
295
|
});
|
|
@@ -313,12 +316,13 @@ const streamText = (options) => {
|
|
|
313
316
|
let finishReason = "other";
|
|
314
317
|
await stream.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream()).pipeThrough(new JsonMessageTransformStream()).pipeTo(new WritableStream({
|
|
315
318
|
abort: (reason) => {
|
|
316
|
-
errorControllers(reason, eventCtrl, textCtrl, reasoningTextCtrl);
|
|
319
|
+
errorControllers(reason, eventCtrl, fullCtrl, textCtrl, reasoningTextCtrl);
|
|
317
320
|
},
|
|
318
321
|
close: () => {
|
|
319
322
|
},
|
|
320
323
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
321
324
|
write: (chunk) => {
|
|
325
|
+
fullCtrl.current?.enqueue(chunk);
|
|
322
326
|
pushUsage(chunk.usage);
|
|
323
327
|
if (chunk.choices == null || chunk.choices.length === 0)
|
|
324
328
|
return;
|
|
@@ -448,21 +452,22 @@ const streamText = (options) => {
|
|
|
448
452
|
finalError ??= err;
|
|
449
453
|
}
|
|
450
454
|
if (finalError != null) {
|
|
451
|
-
errorControllers(finalError, eventCtrl, textCtrl, reasoningTextCtrl);
|
|
455
|
+
errorControllers(finalError, eventCtrl, fullCtrl, textCtrl, reasoningTextCtrl);
|
|
452
456
|
resultSteps.reject(finalError);
|
|
453
457
|
resultMessages.reject(finalError);
|
|
454
458
|
resultUsage.reject(finalError);
|
|
455
459
|
resultTotalUsage.reject(finalError);
|
|
456
460
|
return;
|
|
457
461
|
}
|
|
458
|
-
closeControllers(eventCtrl, textCtrl, reasoningTextCtrl);
|
|
462
|
+
closeControllers(eventCtrl, fullCtrl, textCtrl, reasoningTextCtrl);
|
|
459
463
|
resultSteps.resolve(steps);
|
|
460
464
|
resultMessages.resolve(messages);
|
|
461
465
|
resultUsage.resolve(usage);
|
|
462
466
|
resultTotalUsage.resolve(totalUsage);
|
|
463
467
|
})();
|
|
464
468
|
return {
|
|
465
|
-
|
|
469
|
+
eventStream,
|
|
470
|
+
fullStream,
|
|
466
471
|
messages: resultMessages.promise,
|
|
467
472
|
reasoningTextStream,
|
|
468
473
|
steps: resultSteps.promise,
|
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.5",
|
|
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
|
-
"xsai": "0.5.0-beta.
|
|
34
|
-
"
|
|
33
|
+
"@xsai/shared-stream": "0.5.0-beta.5",
|
|
34
|
+
"xsai": "0.5.0-beta.5"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@langfuse/otel": "^5.1.0",
|