@xsai/stream-text 0.1.2 → 0.2.0-beta.1
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.d.ts +3 -1
- package/dist/index.js +35 -35
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ToolCall, FinishReason, Usage, ChatOptions, Tool, Message, CompletionToolCall, CompletionToolResult, AssistantMessage } from '@xsai/shared-chat';
|
|
1
|
+
import { ToolCall, FinishReason, Usage, ChatOptions, Tool, Message, StepType, CompletionToolCall, CompletionToolResult, AssistantMessage } from '@xsai/shared-chat';
|
|
2
2
|
|
|
3
3
|
interface StreamTextChunkResult {
|
|
4
4
|
choices: {
|
|
@@ -66,7 +66,9 @@ interface StreamTextResult {
|
|
|
66
66
|
}
|
|
67
67
|
interface StreamTextStep {
|
|
68
68
|
choices: StreamTextChoice[];
|
|
69
|
+
finishReason: FinishReason;
|
|
69
70
|
messages: Message[];
|
|
71
|
+
stepType: StepType;
|
|
70
72
|
toolCalls: CompletionToolCall[];
|
|
71
73
|
toolResults: CompletionToolResult[];
|
|
72
74
|
usage?: Usage;
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ var XSAIError = class extends Error {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
// src/index.ts
|
|
12
|
-
import { chat,
|
|
12
|
+
import { chat, determineStepType, executeTool } from "@xsai/shared-chat";
|
|
13
13
|
|
|
14
14
|
// src/helper.ts
|
|
15
15
|
var CHUNK_HEADER_PREFIX = "data:";
|
|
@@ -54,7 +54,9 @@ var streamText = async (options) => {
|
|
|
54
54
|
const stepOne = async (options2) => {
|
|
55
55
|
const step = {
|
|
56
56
|
choices: [],
|
|
57
|
+
finishReason: "error",
|
|
57
58
|
messages: structuredClone(options2.messages),
|
|
59
|
+
stepType: "initial",
|
|
58
60
|
toolCalls: [],
|
|
59
61
|
toolResults: []
|
|
60
62
|
};
|
|
@@ -65,12 +67,6 @@ var streamText = async (options) => {
|
|
|
65
67
|
if (state.endedToolCallIDs.has(id)) {
|
|
66
68
|
return;
|
|
67
69
|
}
|
|
68
|
-
const toolCall = step.choices[state.index].message.tool_calls[id];
|
|
69
|
-
try {
|
|
70
|
-
toolCall.function.parsed_arguments = JSON.parse(toolCall.function.arguments);
|
|
71
|
-
} catch (error) {
|
|
72
|
-
state.toolCallErrors[id] = error;
|
|
73
|
-
}
|
|
74
70
|
state.endedToolCallIDs.add(id);
|
|
75
71
|
state.currentToolID = null;
|
|
76
72
|
};
|
|
@@ -122,6 +118,7 @@ var streamText = async (options) => {
|
|
|
122
118
|
}
|
|
123
119
|
};
|
|
124
120
|
if (finish_reason !== void 0) {
|
|
121
|
+
step.finishReason = finish_reason;
|
|
125
122
|
choiceSnapshot.finish_reason = finish_reason;
|
|
126
123
|
if (finish_reason === "length") {
|
|
127
124
|
throw new XSAIError("length exceeded");
|
|
@@ -195,34 +192,37 @@ var streamText = async (options) => {
|
|
|
195
192
|
if (state.toolCallResults[id]) {
|
|
196
193
|
return;
|
|
197
194
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
} else {
|
|
222
|
-
state.toolCallErrors[id] = new XSAIError(`tool ${toolCall.function.name} not found`);
|
|
195
|
+
try {
|
|
196
|
+
const { parsedArgs, result, toolName } = await executeTool({
|
|
197
|
+
abortSignal: options2.abortSignal,
|
|
198
|
+
messages: options2.messages,
|
|
199
|
+
toolCall,
|
|
200
|
+
tools: options2.tools
|
|
201
|
+
});
|
|
202
|
+
toolCall.function.parsed_arguments = parsedArgs;
|
|
203
|
+
state.toolCallResults[id] = result;
|
|
204
|
+
step.messages.push({
|
|
205
|
+
content: result,
|
|
206
|
+
role: "tool",
|
|
207
|
+
tool_call_id: id
|
|
208
|
+
});
|
|
209
|
+
step.toolResults.push({
|
|
210
|
+
args: parsedArgs,
|
|
211
|
+
result,
|
|
212
|
+
toolCallId: id,
|
|
213
|
+
toolName
|
|
214
|
+
});
|
|
215
|
+
} catch (error) {
|
|
216
|
+
state.toolCallErrors[id] = error;
|
|
223
217
|
}
|
|
224
218
|
}));
|
|
225
219
|
}));
|
|
220
|
+
step.stepType = determineStepType({
|
|
221
|
+
finishReason: step.finishReason,
|
|
222
|
+
maxSteps,
|
|
223
|
+
stepsLength: steps.length,
|
|
224
|
+
toolCallsLength: step.toolCalls.length
|
|
225
|
+
});
|
|
226
226
|
steps.push(step);
|
|
227
227
|
stepCtrl.enqueue(step);
|
|
228
228
|
options2.onStepFinish?.(step);
|
|
@@ -232,9 +232,9 @@ var streamText = async (options) => {
|
|
|
232
232
|
return async () => stepOne({ ...options2, messages: step.messages });
|
|
233
233
|
};
|
|
234
234
|
const invokeFunctionCalls = async () => {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
235
|
+
let ret = await stepOne(options);
|
|
236
|
+
while (typeof ret === "function" && steps.length < maxSteps)
|
|
237
|
+
ret = await ret();
|
|
238
238
|
options.onFinish?.(steps);
|
|
239
239
|
chunkCtrl.close();
|
|
240
240
|
stepCtrl.close();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/stream-text",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.0-beta.1",
|
|
5
5
|
"description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@xsai/shared": "",
|
|
36
36
|
"@xsai/tool": "",
|
|
37
|
-
"valibot": "^1.0.0
|
|
37
|
+
"valibot": "^1.0.0"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsup",
|