@xsai/stream-text 0.3.5 → 0.4.0-beta.2
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 -0
- package/dist/index.js +66 -61
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -102,7 +102,14 @@ const streamText = async (options) => {
|
|
|
102
102
|
steps.push(step);
|
|
103
103
|
void options.onStepFinish?.(step);
|
|
104
104
|
};
|
|
105
|
-
const startStream = async () => {
|
|
105
|
+
const startStream = async () => chat({
|
|
106
|
+
...options,
|
|
107
|
+
maxSteps: void 0,
|
|
108
|
+
messages,
|
|
109
|
+
stream: true,
|
|
110
|
+
streamOptions: options.streamOptions != null ? objCamelToSnake(options.streamOptions) : void 0
|
|
111
|
+
}).then((res) => res.body);
|
|
112
|
+
const handleStream = async (stream2) => {
|
|
106
113
|
const pushUsage = (u) => {
|
|
107
114
|
usage = u;
|
|
108
115
|
};
|
|
@@ -115,52 +122,47 @@ const streamText = async (options) => {
|
|
|
115
122
|
const toolCalls = [];
|
|
116
123
|
const toolResults = [];
|
|
117
124
|
let finishReason = "other";
|
|
118
|
-
await
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (choice.delta.
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
pushEvent({ toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-streaming-start" });
|
|
155
|
-
} else {
|
|
156
|
-
tool_calls[index].function.arguments += toolCall.function.arguments;
|
|
157
|
-
pushEvent({ argsTextDelta: toolCall.function.arguments, toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-delta" });
|
|
158
|
-
}
|
|
125
|
+
await stream2.pipeThrough(transformChunk()).pipeTo(new WritableStream({
|
|
126
|
+
abort: (reason) => {
|
|
127
|
+
eventCtrl?.error(reason);
|
|
128
|
+
textCtrl?.error(reason);
|
|
129
|
+
},
|
|
130
|
+
close: () => {
|
|
131
|
+
},
|
|
132
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
133
|
+
write: (chunk) => {
|
|
134
|
+
if (chunk.usage)
|
|
135
|
+
pushUsage(chunk.usage);
|
|
136
|
+
if (chunk.choices == null || chunk.choices.length === 0)
|
|
137
|
+
return;
|
|
138
|
+
const choice = chunk.choices[0];
|
|
139
|
+
if (choice.delta.reasoning_content != null)
|
|
140
|
+
pushEvent({ text: choice.delta.reasoning_content, type: "reasoning-delta" });
|
|
141
|
+
if (choice.finish_reason != null)
|
|
142
|
+
finishReason = choice.finish_reason;
|
|
143
|
+
if (choice.delta.tool_calls?.length === 0 || choice.delta.tool_calls == null) {
|
|
144
|
+
if (choice.delta.content != null) {
|
|
145
|
+
pushEvent({ text: choice.delta.content, type: "text-delta" });
|
|
146
|
+
pushText(choice.delta.content);
|
|
147
|
+
} else if (choice.delta.refusal != null) {
|
|
148
|
+
pushEvent({ error: choice.delta.refusal, type: "error" });
|
|
149
|
+
} else if (choice.finish_reason != null) {
|
|
150
|
+
pushEvent({ finishReason: choice.finish_reason, type: "finish", usage });
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
for (const toolCall of choice.delta.tool_calls) {
|
|
154
|
+
const { index } = toolCall;
|
|
155
|
+
if (!tool_calls.at(index)) {
|
|
156
|
+
tool_calls[index] = toolCall;
|
|
157
|
+
pushEvent({ toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-streaming-start" });
|
|
158
|
+
} else {
|
|
159
|
+
tool_calls[index].function.arguments += toolCall.function.arguments;
|
|
160
|
+
pushEvent({ argsTextDelta: toolCall.function.arguments, toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-delta" });
|
|
159
161
|
}
|
|
160
162
|
}
|
|
161
163
|
}
|
|
162
|
-
}
|
|
163
|
-
);
|
|
164
|
+
}
|
|
165
|
+
}));
|
|
164
166
|
messages.push({ content: text, role: "assistant", tool_calls });
|
|
165
167
|
if (tool_calls.length !== 0) {
|
|
166
168
|
for (const toolCall of tool_calls) {
|
|
@@ -192,24 +194,27 @@ const streamText = async (options) => {
|
|
|
192
194
|
usage
|
|
193
195
|
});
|
|
194
196
|
if (toolCalls.length !== 0 && steps.length < maxSteps)
|
|
195
|
-
return async () => startStream();
|
|
197
|
+
return async () => handleStream(await startStream());
|
|
196
198
|
};
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
199
|
+
const stream = await startStream();
|
|
200
|
+
void (async () => {
|
|
201
|
+
try {
|
|
202
|
+
await trampoline(async () => handleStream(stream));
|
|
203
|
+
eventCtrl?.close();
|
|
204
|
+
textCtrl?.close();
|
|
205
|
+
} catch (err) {
|
|
206
|
+
eventCtrl?.error(err);
|
|
207
|
+
textCtrl?.error(err);
|
|
208
|
+
resultSteps.reject(err);
|
|
209
|
+
resultMessages.reject(err);
|
|
210
|
+
resultUsage.reject(err);
|
|
211
|
+
} finally {
|
|
212
|
+
resultSteps.resolve(steps);
|
|
213
|
+
resultMessages.resolve(messages);
|
|
214
|
+
resultUsage.resolve(usage);
|
|
215
|
+
void options.onFinish?.(steps.at(-1));
|
|
216
|
+
}
|
|
217
|
+
})();
|
|
213
218
|
return {
|
|
214
219
|
fullStream: eventStream,
|
|
215
220
|
messages: resultMessages.promise,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/stream-text",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0-beta.2",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@xsai/shared-chat": "~0.
|
|
32
|
+
"@xsai/shared-chat": "~0.4.0-beta.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"valibot": "^1.0.0",
|
|
36
|
-
"@xsai/shared": "~0.
|
|
37
|
-
"@xsai/tool": "~0.
|
|
36
|
+
"@xsai/shared": "~0.4.0-beta.2",
|
|
37
|
+
"@xsai/tool": "~0.4.0-beta.2"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "pkgroll",
|