@xsai/stream-text 0.1.2 → 0.1.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.d.ts +3 -1
- package/dist/index.js +36 -29
- package/package.json +1 -1
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
|
};
|
|
@@ -122,6 +124,7 @@ var streamText = async (options) => {
|
|
|
122
124
|
}
|
|
123
125
|
};
|
|
124
126
|
if (finish_reason !== void 0) {
|
|
127
|
+
step.finishReason = finish_reason;
|
|
125
128
|
choiceSnapshot.finish_reason = finish_reason;
|
|
126
129
|
if (finish_reason === "length") {
|
|
127
130
|
throw new XSAIError("length exceeded");
|
|
@@ -195,34 +198,38 @@ var streamText = async (options) => {
|
|
|
195
198
|
if (state.toolCallResults[id]) {
|
|
196
199
|
return;
|
|
197
200
|
}
|
|
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`);
|
|
201
|
+
try {
|
|
202
|
+
const { result } = await executeTool({
|
|
203
|
+
abortSignal: options2.abortSignal,
|
|
204
|
+
messages: options2.messages,
|
|
205
|
+
toolCall,
|
|
206
|
+
tools: options2.tools
|
|
207
|
+
});
|
|
208
|
+
state.toolCallResults[id] = result;
|
|
209
|
+
step.messages.push({
|
|
210
|
+
content: result,
|
|
211
|
+
role: "tool",
|
|
212
|
+
tool_call_id: id
|
|
213
|
+
});
|
|
214
|
+
step.toolResults.push({
|
|
215
|
+
args: toolCall.function.parsed_arguments,
|
|
216
|
+
// TODO: use parsedArgs from executeTool
|
|
217
|
+
result,
|
|
218
|
+
toolCallId: id,
|
|
219
|
+
toolName: toolCall.function.name
|
|
220
|
+
// TODO: use toolName from executeTool
|
|
221
|
+
});
|
|
222
|
+
} catch (error) {
|
|
223
|
+
state.toolCallErrors[id] = error;
|
|
223
224
|
}
|
|
224
225
|
}));
|
|
225
226
|
}));
|
|
227
|
+
step.stepType = determineStepType({
|
|
228
|
+
finishReason: step.finishReason,
|
|
229
|
+
maxSteps,
|
|
230
|
+
stepsLength: steps.length,
|
|
231
|
+
toolCallsLength: step.toolCalls.length
|
|
232
|
+
});
|
|
226
233
|
steps.push(step);
|
|
227
234
|
stepCtrl.enqueue(step);
|
|
228
235
|
options2.onStepFinish?.(step);
|
|
@@ -232,9 +239,9 @@ var streamText = async (options) => {
|
|
|
232
239
|
return async () => stepOne({ ...options2, messages: step.messages });
|
|
233
240
|
};
|
|
234
241
|
const invokeFunctionCalls = async () => {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
242
|
+
let ret = await stepOne(options);
|
|
243
|
+
while (typeof ret === "function" && steps.length < maxSteps)
|
|
244
|
+
ret = await ret();
|
|
238
245
|
options.onFinish?.(steps);
|
|
239
246
|
chunkCtrl.close();
|
|
240
247
|
stepCtrl.close();
|