@xsai/stream-text 0.4.0-beta.9 → 0.4.0

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
@@ -50,6 +50,7 @@ interface StreamTextOptions extends ChatOptions {
50
50
  interface StreamTextResult {
51
51
  fullStream: ReadableStream<StreamTextEvent>;
52
52
  messages: Promise<Message[]>;
53
+ reasoningTextStream: ReadableStream<string>;
53
54
  steps: Promise<CompletionStep[]>;
54
55
  textStream: ReadableStream<string>;
55
56
  totalUsage: Promise<undefined | Usage>;
package/dist/index.js CHANGED
@@ -52,8 +52,10 @@ const streamText = (options) => {
52
52
  const resultTotalUsage = new DelayedPromise();
53
53
  let eventCtrl;
54
54
  let textCtrl;
55
+ let reasoningTextCtrl;
55
56
  const eventStream = new ReadableStream({ start: (controller) => eventCtrl = controller });
56
57
  const textStream = new ReadableStream({ start: (controller) => textCtrl = controller });
58
+ const reasoningTextStream = new ReadableStream({ start: (controller) => reasoningTextCtrl = controller });
57
59
  const pushEvent = (stepEvent) => {
58
60
  eventCtrl?.enqueue(stepEvent);
59
61
  void options.onEvent?.(stepEvent);
@@ -79,10 +81,17 @@ const streamText = (options) => {
79
81
  } : { ...u };
80
82
  };
81
83
  let text = "";
84
+ let reasoningText;
82
85
  const pushText = (content) => {
83
86
  textCtrl?.enqueue(content);
84
87
  text += content;
85
88
  };
89
+ const pushReasoningText = (reasoningContent) => {
90
+ if (reasoningText == null)
91
+ reasoningText = "";
92
+ reasoningTextCtrl?.enqueue(reasoningContent);
93
+ reasoningText += reasoningContent;
94
+ };
86
95
  const tool_calls = [];
87
96
  const toolCalls = [];
88
97
  const toolResults = [];
@@ -101,8 +110,10 @@ const streamText = (options) => {
101
110
  if (chunk.choices == null || chunk.choices.length === 0)
102
111
  return;
103
112
  const choice = chunk.choices[0];
104
- if (choice.delta.reasoning_content != null)
113
+ if (choice.delta.reasoning_content != null) {
105
114
  pushEvent({ text: choice.delta.reasoning_content, type: "reasoning-delta" });
115
+ pushReasoningText(choice.delta.reasoning_content);
116
+ }
106
117
  if (choice.finish_reason != null)
107
118
  finishReason = choice.finish_reason;
108
119
  if (choice.delta.tool_calls?.length === 0 || choice.delta.tool_calls == null) {
@@ -122,19 +133,29 @@ const streamText = (options) => {
122
133
  ...toolCall,
123
134
  function: {
124
135
  ...toolCall.function,
125
- arguments: toolCall.function.arguments
136
+ arguments: toolCall.function.arguments ?? ""
126
137
  }
127
138
  };
128
139
  pushEvent({ toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-streaming-start" });
129
140
  } else {
130
141
  tool_calls[index].function.arguments += toolCall.function.arguments;
131
- pushEvent({ argsTextDelta: toolCall.function.arguments, toolCallId: toolCall.id, toolName: toolCall.function.name ?? tool_calls[index].function.name, type: "tool-call-delta" });
142
+ pushEvent({
143
+ argsTextDelta: toolCall.function.arguments,
144
+ toolCallId: toolCall.id,
145
+ toolName: toolCall.function.name ?? tool_calls[index].function.name,
146
+ type: "tool-call-delta"
147
+ });
132
148
  }
133
149
  }
134
150
  }
135
151
  }
136
152
  }));
137
- messages.push({ content: text, role: "assistant", tool_calls });
153
+ messages.push({
154
+ content: text,
155
+ reasoning_content: reasoningText,
156
+ role: "assistant",
157
+ tool_calls: tool_calls.length > 0 ? tool_calls : void 0
158
+ });
138
159
  if (tool_calls.length !== 0) {
139
160
  for (const toolCall of tool_calls) {
140
161
  if (toolCall == null)
@@ -174,9 +195,11 @@ const streamText = (options) => {
174
195
  await trampoline(async () => doStream());
175
196
  eventCtrl?.close();
176
197
  textCtrl?.close();
198
+ reasoningTextCtrl?.close();
177
199
  } catch (err) {
178
200
  eventCtrl?.error(err);
179
201
  textCtrl?.error(err);
202
+ reasoningTextCtrl?.error(err);
180
203
  resultSteps.reject(err);
181
204
  resultMessages.reject(err);
182
205
  resultUsage.reject(err);
@@ -192,6 +215,7 @@ const streamText = (options) => {
192
215
  return {
193
216
  fullStream: eventStream,
194
217
  messages: resultMessages.promise,
218
+ reasoningTextStream,
195
219
  steps: resultSteps.promise,
196
220
  textStream,
197
221
  totalUsage: resultTotalUsage.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.0-beta.9",
4
+ "version": "0.4.0",
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": "~0.4.0-beta.9",
33
- "@xsai/shared-chat": "~0.4.0-beta.9"
32
+ "@xsai/shared": "~0.4.0",
33
+ "@xsai/shared-chat": "~0.4.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "valibot": "^1.0.0",
37
- "@xsai/tool": "~0.4.0-beta.9"
37
+ "@xsai/tool": "~0.4.0"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "pkgroll",