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

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
@@ -46,14 +46,17 @@ const streamText = (options) => {
46
46
  const maxSteps = options.maxSteps ?? 1;
47
47
  let usage;
48
48
  let totalUsage;
49
+ let reasoningField;
49
50
  const resultSteps = new DelayedPromise();
50
51
  const resultMessages = new DelayedPromise();
51
52
  const resultUsage = new DelayedPromise();
52
53
  const resultTotalUsage = new DelayedPromise();
53
54
  let eventCtrl;
54
55
  let textCtrl;
56
+ let reasoningTextCtrl;
55
57
  const eventStream = new ReadableStream({ start: (controller) => eventCtrl = controller });
56
58
  const textStream = new ReadableStream({ start: (controller) => textCtrl = controller });
59
+ const reasoningTextStream = new ReadableStream({ start: (controller) => reasoningTextCtrl = controller });
57
60
  const pushEvent = (stepEvent) => {
58
61
  eventCtrl?.enqueue(stepEvent);
59
62
  void options.onEvent?.(stepEvent);
@@ -76,13 +79,20 @@ const streamText = (options) => {
76
79
  completion_tokens: totalUsage.completion_tokens + u.completion_tokens,
77
80
  prompt_tokens: totalUsage.prompt_tokens + u.prompt_tokens,
78
81
  total_tokens: totalUsage.total_tokens + u.total_tokens
79
- } : { ...u };
82
+ } : u;
80
83
  };
81
84
  let text = "";
85
+ let reasoningText;
82
86
  const pushText = (content) => {
83
87
  textCtrl?.enqueue(content);
84
88
  text += content;
85
89
  };
90
+ const pushReasoningText = (reasoningContent) => {
91
+ if (reasoningText == null)
92
+ reasoningText = "";
93
+ reasoningTextCtrl?.enqueue(reasoningContent);
94
+ reasoningText += reasoningContent;
95
+ };
86
96
  const tool_calls = [];
87
97
  const toolCalls = [];
88
98
  const toolResults = [];
@@ -101,8 +111,17 @@ const streamText = (options) => {
101
111
  if (chunk.choices == null || chunk.choices.length === 0)
102
112
  return;
103
113
  const choice = chunk.choices[0];
104
- if (choice.delta.reasoning_content != null)
114
+ if (choice.delta.reasoning != null) {
115
+ if (reasoningField !== "reasoning")
116
+ reasoningField = "reasoning";
117
+ pushEvent({ text: choice.delta.reasoning, type: "reasoning-delta" });
118
+ pushReasoningText(choice.delta.reasoning);
119
+ } else if (choice.delta.reasoning_content != null) {
120
+ if (reasoningField !== "reasoning_content")
121
+ reasoningField = "reasoning_content";
105
122
  pushEvent({ text: choice.delta.reasoning_content, type: "reasoning-delta" });
123
+ pushReasoningText(choice.delta.reasoning_content);
124
+ }
106
125
  if (choice.finish_reason != null)
107
126
  finishReason = choice.finish_reason;
108
127
  if (choice.delta.tool_calls?.length === 0 || choice.delta.tool_calls == null) {
@@ -122,19 +141,29 @@ const streamText = (options) => {
122
141
  ...toolCall,
123
142
  function: {
124
143
  ...toolCall.function,
125
- arguments: toolCall.function.arguments
144
+ arguments: toolCall.function.arguments ?? ""
126
145
  }
127
146
  };
128
147
  pushEvent({ toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-streaming-start" });
129
148
  } else {
130
149
  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" });
150
+ pushEvent({
151
+ argsTextDelta: toolCall.function.arguments,
152
+ toolCallId: toolCall.id,
153
+ toolName: toolCall.function.name ?? tool_calls[index].function.name,
154
+ type: "tool-call-delta"
155
+ });
132
156
  }
133
157
  }
134
158
  }
135
159
  }
136
160
  }));
137
- messages.push({ content: text, role: "assistant", tool_calls });
161
+ messages.push({
162
+ ...reasoningField != null ? { [reasoningField]: reasoningText } : {},
163
+ content: text,
164
+ role: "assistant",
165
+ tool_calls: tool_calls.length > 0 ? tool_calls : void 0
166
+ });
138
167
  if (tool_calls.length !== 0) {
139
168
  for (const toolCall of tool_calls) {
140
169
  if (toolCall == null)
@@ -174,9 +203,11 @@ const streamText = (options) => {
174
203
  await trampoline(async () => doStream());
175
204
  eventCtrl?.close();
176
205
  textCtrl?.close();
206
+ reasoningTextCtrl?.close();
177
207
  } catch (err) {
178
208
  eventCtrl?.error(err);
179
209
  textCtrl?.error(err);
210
+ reasoningTextCtrl?.error(err);
180
211
  resultSteps.reject(err);
181
212
  resultMessages.reject(err);
182
213
  resultUsage.reject(err);
@@ -192,6 +223,7 @@ const streamText = (options) => {
192
223
  return {
193
224
  fullStream: eventStream,
194
225
  messages: resultMessages.promise,
226
+ reasoningTextStream,
195
227
  steps: resultSteps.promise,
196
228
  textStream,
197
229
  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.1",
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.1",
33
+ "@xsai/shared-chat": "~0.4.1"
34
34
  },
35
35
  "devDependencies": {
36
36
  "valibot": "^1.0.0",
37
- "@xsai/tool": "~0.4.0-beta.9"
37
+ "@xsai/tool": "~0.4.1"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "pkgroll",