@xsai/stream-text 0.4.0-beta.8 → 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/README.md ADDED
@@ -0,0 +1 @@
1
+ https://xsai.js.org/docs/packages/stream/text
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) {
@@ -118,17 +129,33 @@ const streamText = (options) => {
118
129
  for (const toolCall of choice.delta.tool_calls) {
119
130
  const { index } = toolCall;
120
131
  if (!tool_calls.at(index)) {
121
- tool_calls[index] = toolCall;
132
+ tool_calls[index] = {
133
+ ...toolCall,
134
+ function: {
135
+ ...toolCall.function,
136
+ arguments: toolCall.function.arguments ?? ""
137
+ }
138
+ };
122
139
  pushEvent({ toolCallId: toolCall.id, toolName: toolCall.function.name, type: "tool-call-streaming-start" });
123
140
  } else {
124
141
  tool_calls[index].function.arguments += toolCall.function.arguments;
125
- pushEvent({ argsTextDelta: toolCall.function.arguments, toolCallId: toolCall.id, toolName: toolCall.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
+ });
126
148
  }
127
149
  }
128
150
  }
129
151
  }
130
152
  }));
131
- 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
+ });
132
159
  if (tool_calls.length !== 0) {
133
160
  for (const toolCall of tool_calls) {
134
161
  if (toolCall == null)
@@ -168,9 +195,11 @@ const streamText = (options) => {
168
195
  await trampoline(async () => doStream());
169
196
  eventCtrl?.close();
170
197
  textCtrl?.close();
198
+ reasoningTextCtrl?.close();
171
199
  } catch (err) {
172
200
  eventCtrl?.error(err);
173
201
  textCtrl?.error(err);
202
+ reasoningTextCtrl?.error(err);
174
203
  resultSteps.reject(err);
175
204
  resultMessages.reject(err);
176
205
  resultUsage.reject(err);
@@ -186,6 +215,7 @@ const streamText = (options) => {
186
215
  return {
187
216
  fullStream: eventStream,
188
217
  messages: resultMessages.promise,
218
+ reasoningTextStream,
189
219
  steps: resultSteps.promise,
190
220
  textStream,
191
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.8",
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.8",
33
- "@xsai/shared-chat": "~0.4.0-beta.8"
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.8"
37
+ "@xsai/tool": "~0.4.0"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "pkgroll",