@xsai/stream-text 0.4.0-beta.2 → 0.4.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.d.ts +2 -1
- package/dist/index.js +22 -13
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -51,8 +51,9 @@ interface StreamTextResult {
|
|
|
51
51
|
messages: Promise<Message[]>;
|
|
52
52
|
steps: Promise<CompletionStep[]>;
|
|
53
53
|
textStream: ReadableStream<string>;
|
|
54
|
+
totalUsage: Promise<undefined | Usage>;
|
|
54
55
|
usage: Promise<undefined | Usage>;
|
|
55
56
|
}
|
|
56
|
-
declare const streamText: (options: StreamTextOptions) =>
|
|
57
|
+
declare const streamText: (options: StreamTextOptions) => StreamTextResult;
|
|
57
58
|
|
|
58
59
|
export { type StreamTextEvent, type StreamTextOptions, type StreamTextResult, streamText };
|
package/dist/index.js
CHANGED
|
@@ -82,14 +82,16 @@ const transformChunk = () => {
|
|
|
82
82
|
});
|
|
83
83
|
};
|
|
84
84
|
|
|
85
|
-
const streamText =
|
|
85
|
+
const streamText = (options) => {
|
|
86
86
|
const steps = [];
|
|
87
87
|
const messages = structuredClone(options.messages);
|
|
88
88
|
const maxSteps = options.maxSteps ?? 1;
|
|
89
89
|
let usage;
|
|
90
|
+
let totalUsage;
|
|
90
91
|
const resultSteps = new DelayedPromise();
|
|
91
92
|
const resultMessages = new DelayedPromise();
|
|
92
93
|
const resultUsage = new DelayedPromise();
|
|
94
|
+
const resultTotalUsage = new DelayedPromise();
|
|
93
95
|
let eventCtrl;
|
|
94
96
|
let textCtrl;
|
|
95
97
|
const eventStream = new ReadableStream({ start: (controller) => eventCtrl = controller });
|
|
@@ -102,16 +104,21 @@ const streamText = async (options) => {
|
|
|
102
104
|
steps.push(step);
|
|
103
105
|
void options.onStepFinish?.(step);
|
|
104
106
|
};
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
107
|
+
const doStream = async () => {
|
|
108
|
+
const { body: stream } = await chat({
|
|
109
|
+
...options,
|
|
110
|
+
maxSteps: void 0,
|
|
111
|
+
messages,
|
|
112
|
+
stream: true,
|
|
113
|
+
streamOptions: options.streamOptions != null ? objCamelToSnake(options.streamOptions) : void 0
|
|
114
|
+
});
|
|
113
115
|
const pushUsage = (u) => {
|
|
114
116
|
usage = u;
|
|
117
|
+
totalUsage = totalUsage ? {
|
|
118
|
+
completion_tokens: totalUsage.completion_tokens + u.completion_tokens,
|
|
119
|
+
prompt_tokens: totalUsage.prompt_tokens + u.prompt_tokens,
|
|
120
|
+
total_tokens: totalUsage.total_tokens + u.total_tokens
|
|
121
|
+
} : { ...u };
|
|
115
122
|
};
|
|
116
123
|
let text = "";
|
|
117
124
|
const pushText = (content) => {
|
|
@@ -122,7 +129,7 @@ const streamText = async (options) => {
|
|
|
122
129
|
const toolCalls = [];
|
|
123
130
|
const toolResults = [];
|
|
124
131
|
let finishReason = "other";
|
|
125
|
-
await
|
|
132
|
+
await stream.pipeThrough(transformChunk()).pipeTo(new WritableStream({
|
|
126
133
|
abort: (reason) => {
|
|
127
134
|
eventCtrl?.error(reason);
|
|
128
135
|
textCtrl?.error(reason);
|
|
@@ -194,12 +201,11 @@ const streamText = async (options) => {
|
|
|
194
201
|
usage
|
|
195
202
|
});
|
|
196
203
|
if (toolCalls.length !== 0 && steps.length < maxSteps)
|
|
197
|
-
return async () =>
|
|
204
|
+
return async () => doStream();
|
|
198
205
|
};
|
|
199
|
-
const stream = await startStream();
|
|
200
206
|
void (async () => {
|
|
201
207
|
try {
|
|
202
|
-
await trampoline(async () =>
|
|
208
|
+
await trampoline(async () => doStream());
|
|
203
209
|
eventCtrl?.close();
|
|
204
210
|
textCtrl?.close();
|
|
205
211
|
} catch (err) {
|
|
@@ -208,10 +214,12 @@ const streamText = async (options) => {
|
|
|
208
214
|
resultSteps.reject(err);
|
|
209
215
|
resultMessages.reject(err);
|
|
210
216
|
resultUsage.reject(err);
|
|
217
|
+
resultTotalUsage.reject(err);
|
|
211
218
|
} finally {
|
|
212
219
|
resultSteps.resolve(steps);
|
|
213
220
|
resultMessages.resolve(messages);
|
|
214
221
|
resultUsage.resolve(usage);
|
|
222
|
+
resultTotalUsage.resolve(totalUsage);
|
|
215
223
|
void options.onFinish?.(steps.at(-1));
|
|
216
224
|
}
|
|
217
225
|
})();
|
|
@@ -220,6 +228,7 @@ const streamText = async (options) => {
|
|
|
220
228
|
messages: resultMessages.promise,
|
|
221
229
|
steps: resultSteps.promise,
|
|
222
230
|
textStream,
|
|
231
|
+
totalUsage: resultTotalUsage.promise,
|
|
223
232
|
usage: resultUsage.promise
|
|
224
233
|
};
|
|
225
234
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/stream-text",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.4.0-beta.
|
|
4
|
+
"version": "0.4.0-beta.3",
|
|
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.4.0-beta.
|
|
32
|
+
"@xsai/shared-chat": "~0.4.0-beta.3"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"valibot": "^1.0.0",
|
|
36
|
-
"@xsai/shared": "~0.4.0-beta.
|
|
37
|
-
"@xsai/tool": "~0.4.0-beta.
|
|
36
|
+
"@xsai/shared": "~0.4.0-beta.3",
|
|
37
|
+
"@xsai/tool": "~0.4.0-beta.3"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "pkgroll",
|