@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 CHANGED
@@ -16,6 +16,9 @@ type StreamTextEvent = (CompletionToolCall & {
16
16
  finishReason: FinishReason;
17
17
  type: 'finish';
18
18
  usage?: Usage;
19
+ } | {
20
+ text: string;
21
+ type: 'reasoning-delta';
19
22
  } | {
20
23
  text: string;
21
24
  type: 'text-delta';
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 chat({
119
- ...options,
120
- maxSteps: void 0,
121
- messages,
122
- stream: true,
123
- streamOptions: options.streamOptions != null ? objCamelToSnake(options.streamOptions) : void 0
124
- }).then(
125
- async (res) => res.body.pipeThrough(transformChunk()).pipeTo(new WritableStream({
126
- abort: (reason) => {
127
- eventCtrl?.error(reason);
128
- textCtrl?.error(reason);
129
- },
130
- close: () => {
131
- },
132
- write: (chunk) => {
133
- if (chunk.usage)
134
- pushUsage(chunk.usage);
135
- if (chunk.choices == null || chunk.choices.length === 0)
136
- return;
137
- const choice = chunk.choices[0];
138
- if (choice.finish_reason != null)
139
- finishReason = choice.finish_reason;
140
- if (choice.delta.tool_calls?.length === 0 || choice.delta.tool_calls == null) {
141
- if (choice.delta.content != null) {
142
- pushEvent({ text: choice.delta.content, type: "text-delta" });
143
- pushText(choice.delta.content);
144
- } else if (choice.delta.refusal != null) {
145
- pushEvent({ error: choice.delta.refusal, type: "error" });
146
- } else if (choice.finish_reason != null) {
147
- pushEvent({ finishReason: choice.finish_reason, type: "finish", usage });
148
- }
149
- } else {
150
- for (const toolCall of choice.delta.tool_calls) {
151
- const { index } = toolCall;
152
- if (!tool_calls.at(index)) {
153
- tool_calls[index] = toolCall;
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
- try {
198
- await trampoline(async () => startStream());
199
- eventCtrl?.close();
200
- textCtrl?.close();
201
- } catch (err) {
202
- eventCtrl?.error(err);
203
- textCtrl?.error(err);
204
- resultSteps.reject(err);
205
- resultMessages.reject(err);
206
- resultUsage.reject(err);
207
- } finally {
208
- resultSteps.resolve(steps);
209
- resultMessages.resolve(messages);
210
- resultUsage.resolve(usage);
211
- void options.onFinish?.(steps.at(-1));
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.3.5",
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.3.5"
32
+ "@xsai/shared-chat": "~0.4.0-beta.2"
33
33
  },
34
34
  "devDependencies": {
35
35
  "valibot": "^1.0.0",
36
- "@xsai/shared": "~0.3.5",
37
- "@xsai/tool": "~0.3.5"
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",