@xsai-ext/telemetry 0.5.0-beta.2 → 0.5.0-beta.3
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 +42 -55
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { embed as embed$1, clean, embedMany as embedMany$1, trampoline,
|
|
1
|
+
import { embed as embed$1, clean, embedMany as embedMany$1, trampoline, resolvePrepareStep, chat, responseJSON, normalizeChatCompletionUsage, InvalidResponseError, stepCountAtLeast, executeTool, shouldStop, DelayedPromise, objCamelToSnake, computeTotalUsage } 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.3";
|
|
7
7
|
var pkg = {
|
|
8
8
|
version: version};
|
|
9
9
|
|
|
@@ -135,8 +135,8 @@ const generateText = async (options) => {
|
|
|
135
135
|
const rawGenerateText = async (options2) => {
|
|
136
136
|
const messages = options2.steps == null ? structuredClone(options2.messages) : options2.messages;
|
|
137
137
|
const steps = options2.steps ?? [];
|
|
138
|
-
const stepOptions = await
|
|
139
|
-
messages,
|
|
138
|
+
const stepOptions = await resolvePrepareStep({
|
|
139
|
+
input: messages,
|
|
140
140
|
model: options2.model,
|
|
141
141
|
prepareStep: options2.prepareStep,
|
|
142
142
|
stepNumber: steps.length,
|
|
@@ -145,20 +145,21 @@ const generateText = async (options) => {
|
|
|
145
145
|
});
|
|
146
146
|
return recordSpan(chatSpan({
|
|
147
147
|
...options2,
|
|
148
|
-
messages: stepOptions.
|
|
148
|
+
messages: stepOptions.input,
|
|
149
149
|
model: stepOptions.model,
|
|
150
150
|
toolChoice: stepOptions.toolChoice
|
|
151
151
|
}, tracer), async (span) => chat({
|
|
152
152
|
...options2,
|
|
153
153
|
maxSteps: void 0,
|
|
154
|
-
messages: stepOptions.
|
|
154
|
+
messages: stepOptions.input,
|
|
155
155
|
model: stepOptions.model,
|
|
156
156
|
steps: void 0,
|
|
157
157
|
stopWhen: void 0,
|
|
158
158
|
stream: false,
|
|
159
159
|
toolChoice: stepOptions.toolChoice
|
|
160
160
|
}).then(responseJSON).then(async (res) => {
|
|
161
|
-
const { choices
|
|
161
|
+
const { choices } = res;
|
|
162
|
+
const usage = normalizeChatCompletionUsage(res.usage);
|
|
162
163
|
if (!choices?.length) {
|
|
163
164
|
const responseBody = JSON.stringify(res);
|
|
164
165
|
throw new InvalidResponseError(`No choices returned, response body: ${responseBody}`, {
|
|
@@ -182,13 +183,17 @@ const generateText = async (options) => {
|
|
|
182
183
|
tools: options2.tools
|
|
183
184
|
}))
|
|
184
185
|
);
|
|
185
|
-
for (const { completionToolCall, completionToolResult,
|
|
186
|
+
for (const { completionToolCall, completionToolResult, result } of results) {
|
|
186
187
|
toolCalls.push(completionToolCall);
|
|
187
188
|
toolResults.push(completionToolResult);
|
|
188
|
-
messages.push(
|
|
189
|
+
messages.push({
|
|
190
|
+
content: result,
|
|
191
|
+
role: "tool",
|
|
192
|
+
tool_call_id: completionToolCall.toolCallId
|
|
193
|
+
});
|
|
189
194
|
}
|
|
190
195
|
}
|
|
191
|
-
const
|
|
196
|
+
const step = {
|
|
192
197
|
finishReason,
|
|
193
198
|
text: Array.isArray(message.content) ? message.content.filter((m) => m.type === "text").map((m) => m.text).join("\n") : message.content,
|
|
194
199
|
toolCalls,
|
|
@@ -196,25 +201,16 @@ const generateText = async (options) => {
|
|
|
196
201
|
usage
|
|
197
202
|
};
|
|
198
203
|
const stop = shouldStop(stopWhen, {
|
|
199
|
-
messages,
|
|
200
|
-
step
|
|
201
|
-
steps: [...steps,
|
|
204
|
+
input: messages,
|
|
205
|
+
step,
|
|
206
|
+
steps: [...steps, step]
|
|
202
207
|
});
|
|
203
208
|
const willContinue = toolCalls.length > 0 && !stop;
|
|
204
|
-
const step = {
|
|
205
|
-
...stopStep,
|
|
206
|
-
stepType: determineStepType({
|
|
207
|
-
finishReason,
|
|
208
|
-
stepsLength: steps.length,
|
|
209
|
-
toolCallsLength: toolCalls.length,
|
|
210
|
-
willContinue
|
|
211
|
-
})
|
|
212
|
-
};
|
|
213
209
|
steps.push(step);
|
|
214
210
|
span.setAttributes({
|
|
215
211
|
"gen_ai.response.finish_reasons": [step.finishReason],
|
|
216
|
-
"gen_ai.usage.input_tokens": step.usage.
|
|
217
|
-
"gen_ai.usage.output_tokens": step.usage.
|
|
212
|
+
"gen_ai.usage.input_tokens": step.usage.inputTokens,
|
|
213
|
+
"gen_ai.usage.output_tokens": step.usage.outputTokens
|
|
218
214
|
});
|
|
219
215
|
if (options2.onStepFinish)
|
|
220
216
|
await options2.onStepFinish(step);
|
|
@@ -269,8 +265,8 @@ const streamText = (options) => {
|
|
|
269
265
|
};
|
|
270
266
|
const tools = options.tools != null && options.tools.length > 0 ? options.tools.map((tool) => wrapTool(tool, tracer)) : void 0;
|
|
271
267
|
const doStream = async () => {
|
|
272
|
-
const stepOptions = await
|
|
273
|
-
messages,
|
|
268
|
+
const stepOptions = await resolvePrepareStep({
|
|
269
|
+
input: messages,
|
|
274
270
|
model: options.model,
|
|
275
271
|
prepareStep: options.prepareStep,
|
|
276
272
|
stepNumber: steps.length,
|
|
@@ -279,14 +275,14 @@ const streamText = (options) => {
|
|
|
279
275
|
});
|
|
280
276
|
return recordSpan(chatSpan({
|
|
281
277
|
...options,
|
|
282
|
-
messages: stepOptions.
|
|
278
|
+
messages: stepOptions.input,
|
|
283
279
|
model: stepOptions.model,
|
|
284
280
|
toolChoice: stepOptions.toolChoice
|
|
285
281
|
}, tracer), async (span) => {
|
|
286
282
|
const { body: stream } = await chat({
|
|
287
283
|
...options,
|
|
288
284
|
maxSteps: void 0,
|
|
289
|
-
messages: stepOptions.
|
|
285
|
+
messages: stepOptions.input,
|
|
290
286
|
model: stepOptions.model,
|
|
291
287
|
stopWhen: void 0,
|
|
292
288
|
stream: true,
|
|
@@ -295,12 +291,10 @@ const streamText = (options) => {
|
|
|
295
291
|
tools
|
|
296
292
|
});
|
|
297
293
|
const pushUsage = (u) => {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
total_tokens: totalUsage.total_tokens + u.total_tokens
|
|
303
|
-
} : u;
|
|
294
|
+
if (u == null)
|
|
295
|
+
return;
|
|
296
|
+
usage = normalizeChatCompletionUsage(u);
|
|
297
|
+
totalUsage = computeTotalUsage(totalUsage, usage);
|
|
304
298
|
};
|
|
305
299
|
let text = "";
|
|
306
300
|
let reasoningText;
|
|
@@ -309,8 +303,7 @@ const streamText = (options) => {
|
|
|
309
303
|
text += content;
|
|
310
304
|
};
|
|
311
305
|
const pushReasoningText = (reasoningContent) => {
|
|
312
|
-
|
|
313
|
-
reasoningText = "";
|
|
306
|
+
reasoningText ??= "";
|
|
314
307
|
reasoningTextCtrl.current?.enqueue(reasoningContent);
|
|
315
308
|
reasoningText += reasoningContent;
|
|
316
309
|
};
|
|
@@ -326,8 +319,7 @@ const streamText = (options) => {
|
|
|
326
319
|
},
|
|
327
320
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
328
321
|
write: (chunk) => {
|
|
329
|
-
|
|
330
|
-
pushUsage(chunk.usage);
|
|
322
|
+
pushUsage(chunk.usage);
|
|
331
323
|
if (chunk.choices == null || chunk.choices.length === 0)
|
|
332
324
|
return;
|
|
333
325
|
const choice = chunk.choices[0];
|
|
@@ -400,10 +392,14 @@ const streamText = (options) => {
|
|
|
400
392
|
tools
|
|
401
393
|
}))
|
|
402
394
|
);
|
|
403
|
-
for (const { completionToolCall, completionToolResult,
|
|
395
|
+
for (const { completionToolCall, completionToolResult, result } of results) {
|
|
404
396
|
toolCalls.push(completionToolCall);
|
|
405
397
|
toolResults.push(completionToolResult);
|
|
406
|
-
messages.push(
|
|
398
|
+
messages.push({
|
|
399
|
+
content: result,
|
|
400
|
+
role: "tool",
|
|
401
|
+
tool_call_id: completionToolCall.toolCallId
|
|
402
|
+
});
|
|
407
403
|
pushEvent({ ...completionToolCall, type: "tool-call" });
|
|
408
404
|
pushEvent({ ...completionToolResult, type: "tool-result" });
|
|
409
405
|
}
|
|
@@ -414,7 +410,7 @@ const streamText = (options) => {
|
|
|
414
410
|
usage
|
|
415
411
|
});
|
|
416
412
|
}
|
|
417
|
-
const
|
|
413
|
+
const step = {
|
|
418
414
|
finishReason,
|
|
419
415
|
text,
|
|
420
416
|
toolCalls,
|
|
@@ -422,26 +418,17 @@ const streamText = (options) => {
|
|
|
422
418
|
usage
|
|
423
419
|
};
|
|
424
420
|
const stop = shouldStop(stopWhen, {
|
|
425
|
-
messages,
|
|
426
|
-
step
|
|
427
|
-
steps: [...steps,
|
|
421
|
+
input: messages,
|
|
422
|
+
step,
|
|
423
|
+
steps: [...steps, step]
|
|
428
424
|
});
|
|
429
425
|
const willContinue = toolCalls.length > 0 && !stop;
|
|
430
|
-
const step = {
|
|
431
|
-
...stopStep,
|
|
432
|
-
stepType: determineStepType({
|
|
433
|
-
finishReason,
|
|
434
|
-
stepsLength: steps.length,
|
|
435
|
-
toolCallsLength: toolCalls.length,
|
|
436
|
-
willContinue
|
|
437
|
-
})
|
|
438
|
-
};
|
|
439
426
|
pushStep(step);
|
|
440
427
|
span.setAttributes({
|
|
441
428
|
"gen_ai.response.finish_reasons": [step.finishReason],
|
|
442
429
|
...step.usage && {
|
|
443
|
-
"gen_ai.usage.input_tokens": step.usage.
|
|
444
|
-
"gen_ai.usage.output_tokens": step.usage.
|
|
430
|
+
"gen_ai.usage.input_tokens": step.usage.inputTokens,
|
|
431
|
+
"gen_ai.usage.output_tokens": step.usage.outputTokens
|
|
445
432
|
}
|
|
446
433
|
});
|
|
447
434
|
if (willContinue)
|
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.3",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,15 +29,15 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@opentelemetry/api": "^1.9.
|
|
33
|
-
"
|
|
34
|
-
"xsai": "
|
|
32
|
+
"@opentelemetry/api": "^1.9.1",
|
|
33
|
+
"xsai": "0.5.0-beta.3",
|
|
34
|
+
"@xsai/shared-stream": "0.5.0-beta.3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@langfuse/otel": "^
|
|
38
|
-
"@opentelemetry/sdk-trace-base": "^2.
|
|
39
|
-
"@opentelemetry/sdk-trace-node": "^2.
|
|
40
|
-
"zod": "^4.
|
|
37
|
+
"@langfuse/otel": "^5.1.0",
|
|
38
|
+
"@opentelemetry/sdk-trace-base": "^2.6.1",
|
|
39
|
+
"@opentelemetry/sdk-trace-node": "^2.6.1",
|
|
40
|
+
"zod": "^4.3.6"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "pkgroll",
|