@xsai/stream-text 0.4.0-beta.10 → 0.4.0-beta.12

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) {
@@ -134,7 +145,7 @@ const streamText = (options) => {
134
145
  }
135
146
  }
136
147
  }));
137
- messages.push({ content: text, role: "assistant", tool_calls });
148
+ messages.push({ content: text, reasoning_content: reasoningText, role: "assistant", tool_calls });
138
149
  if (tool_calls.length !== 0) {
139
150
  for (const toolCall of tool_calls) {
140
151
  if (toolCall == null)
@@ -174,9 +185,11 @@ const streamText = (options) => {
174
185
  await trampoline(async () => doStream());
175
186
  eventCtrl?.close();
176
187
  textCtrl?.close();
188
+ reasoningTextCtrl?.close();
177
189
  } catch (err) {
178
190
  eventCtrl?.error(err);
179
191
  textCtrl?.error(err);
192
+ reasoningTextCtrl?.error(err);
180
193
  resultSteps.reject(err);
181
194
  resultMessages.reject(err);
182
195
  resultUsage.reject(err);
@@ -192,6 +205,7 @@ const streamText = (options) => {
192
205
  return {
193
206
  fullStream: eventStream,
194
207
  messages: resultMessages.promise,
208
+ reasoningTextStream,
195
209
  steps: resultSteps.promise,
196
210
  textStream,
197
211
  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.10",
4
+ "version": "0.4.0-beta.12",
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.10",
33
- "@xsai/shared-chat": "~0.4.0-beta.10"
32
+ "@xsai/shared": "~0.4.0-beta.12",
33
+ "@xsai/shared-chat": "~0.4.0-beta.12"
34
34
  },
35
35
  "devDependencies": {
36
36
  "valibot": "^1.0.0",
37
- "@xsai/tool": "~0.4.0-beta.10"
37
+ "@xsai/tool": "~0.4.0-beta.12"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "pkgroll",