Skip to content

Commit 4e8fa09

Browse files
authored
fix: yield reasoning chunks before content chunks in providers (Zoo-Code-Org#1462)
This prevents the content truncation bug in Task.ts when reasoning and content are delivered in the same streaming chunk.
1 parent 22cc416 commit 4e8fa09

16 files changed

Lines changed: 123 additions & 59 deletions

‎src/api/providers/__tests__/base-openai-compatible-provider.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,25 @@ describe("BaseOpenAiCompatibleProvider", () => {
236236
// Should yield reasoning with spaces (only pure whitespace is filtered)
237237
expect(chunks).toEqual([{ type: "reasoning", text: " content with spaces " }])
238238
})
239+
240+
it("should yield reasoning chunks BEFORE text chunks when both are present in the exact same delta", async () => {
241+
mockCreate.mockImplementationOnce(() =>
242+
asyncStreamFrom([
243+
{
244+
choices: [{ delta: { reasoning_content: "thinking...", content: "answer" } }],
245+
},
246+
]),
247+
)
248+
249+
const stream = handler.createMessage("system prompt", [])
250+
const chunks = await collectStream(stream)
251+
252+
const contentChunks = chunks.filter((c) => c.type === "reasoning" || c.type === "text")
253+
expect(contentChunks).toEqual([
254+
{ type: "reasoning", text: "thinking..." },
255+
{ type: "text", text: "answer" },
256+
])
257+
})
239258
})
240259

241260
describe("Basic functionality", () => {

‎src/api/providers/__tests__/lite-llm.spec.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,31 @@ describe("LiteLLMHandler", () => {
731731
expect(textChunk).toMatchObject({ type: "text", text: "The answer is 42." })
732732
})
733733

734+
it("should yield reasoning chunks BEFORE text chunks when both are present in the exact same delta", async () => {
735+
const mockStream = asyncStreamFrom([
736+
{
737+
choices: [{ delta: { reasoning_content: "thinking...", content: "answer" } }],
738+
usage: { prompt_tokens: 10, completion_tokens: 10 },
739+
},
740+
])
741+
742+
mockCreate.mockReturnValue({
743+
withResponse: vi.fn().mockResolvedValue({ data: mockStream }),
744+
})
745+
746+
const generator = handler.createMessage("system", [{ role: "user", content: "Test simultaneous." }])
747+
const results = await collectStream(generator)
748+
749+
// Filter out usage chunks to focus on ordering of content
750+
const contentResults = results.filter((r) => r.type === "reasoning" || r.type === "text")
751+
752+
// The order is strictly enforced here
753+
expect(contentResults).toEqual([
754+
{ type: "reasoning", text: "thinking..." },
755+
{ type: "text", text: "answer" },
756+
])
757+
})
758+
734759
it("should yield reasoning chunks from reasoning delta field", async () => {
735760
const mockStream = asyncStreamFrom([
736761
{

‎src/api/providers/__tests__/nanogpt.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ describe("NanoGptHandler", () => {
123123
new NanoGptHandler({ nanoGptModelId: "model:thinking" }).createMessage("sys", messages),
124124
)
125125
expect(chunks).toEqual([
126-
{ type: "text", text: "answer" },
127126
{ type: "reasoning", text: "modern" },
127+
{ type: "text", text: "answer" },
128128
{ type: "reasoning", text: "legacy" },
129129
{ type: "tool_call_partial", index: 0, id: "call-1", name: "read_file", arguments: '{"path":' },
130130
{ type: "tool_call_partial", index: 1, id: "call-2", name: "search_files", arguments: '{"query":' },

‎src/api/providers/__tests__/openai.spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,26 @@ describe("OpenAiHandler", () => {
658658
expect(callArgs.max_completion_tokens).toBe(4096)
659659
})
660660

661+
it("should yield reasoning chunks BEFORE text chunks when both are present in the exact same delta", async () => {
662+
mockCreate.mockImplementationOnce(() =>
663+
asyncStreamFrom([
664+
{
665+
choices: [{ delta: { reasoning_content: "thinking...", content: "answer" } }],
666+
usage: { prompt_tokens: 10, completion_tokens: 10 },
667+
},
668+
]),
669+
)
670+
671+
const stream = handler.createMessage("system prompt", [])
672+
const chunks = await collectStream(stream)
673+
674+
const contentChunks = chunks.filter((c) => c.type === "reasoning" || c.type === "text")
675+
expect(contentChunks).toEqual([
676+
{ type: "reasoning", text: "thinking..." },
677+
{ type: "text", text: "answer" },
678+
])
679+
})
680+
661681
describe("TagMatcher reasoning tags", () => {
662682
it("should treat stray closing tag as plain text when no tag is open", async () => {
663683
mockCreate.mockImplementationOnce(() =>

‎src/api/providers/base-openai-compatible-provider.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,17 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
141141
const delta = chunk.choices?.[0]?.delta
142142
const finishReason = chunk.choices?.[0]?.finish_reason
143143

144+
const reasoningText = extractReasoningFromDelta(delta)
145+
if (reasoningText) {
146+
yield { type: "reasoning", text: reasoningText }
147+
}
148+
144149
if (delta?.content) {
145150
for (const processedChunk of matcher.update(delta.content)) {
146151
yield processedChunk
147152
}
148153
}
149154

150-
const reasoningText = extractReasoningFromDelta(delta)
151-
if (reasoningText) {
152-
yield { type: "reasoning", text: reasoningText }
153-
}
154-
155155
// Emit raw tool call chunks - NativeToolCallParser handles state management
156156
if (delta?.tool_calls) {
157157
for (const toolCall of delta.tool_calls) {

‎src/api/providers/deepseek.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ export class DeepSeekHandler extends OpenAiHandler {
166166
for await (const chunk of stream) {
167167
const delta = chunk.choices?.[0]?.delta ?? {}
168168

169+
// Handle reasoning_content from DeepSeek's interleaved thinking
170+
// This is the proper way DeepSeek sends thinking content in streaming
171+
const reasoningText = extractReasoningFromDelta(delta)
172+
if (reasoningText) {
173+
yield { type: "reasoning", text: reasoningText }
174+
}
175+
169176
// Handle regular text content
170177
if (delta.content) {
171178
yield {
@@ -174,13 +181,6 @@ export class DeepSeekHandler extends OpenAiHandler {
174181
}
175182
}
176183

177-
// Handle reasoning_content from DeepSeek's interleaved thinking
178-
// This is the proper way DeepSeek sends thinking content in streaming
179-
const reasoningText = extractReasoningFromDelta(delta)
180-
if (reasoningText) {
181-
yield { type: "reasoning", text: reasoningText }
182-
}
183-
184184
// Handle tool calls
185185
if (delta.tool_calls) {
186186
for (const toolCall of delta.tool_calls) {

‎src/api/providers/kenari.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ export class KenariHandler extends RouterProvider implements SingleCompletionHan
8282
for await (const chunk of completion) {
8383
const delta = chunk.choices[0]?.delta
8484

85-
if (delta?.content) {
86-
yield { type: "text", text: delta.content }
87-
}
88-
8985
// Several Kenari models (GLM, DeepSeek) stream reasoning via reasoning_content,
9086
// with an OpenRouter-style `reasoning` fallback; the shared helper handles both.
9187
const reasoningText = extractReasoningFromDelta(delta)
9288
if (reasoningText) {
9389
yield { type: "reasoning", text: reasoningText }
9490
}
9591

92+
if (delta?.content) {
93+
yield { type: "text", text: delta.content }
94+
}
95+
9696
// Emit raw tool call chunks - NativeToolCallParser handles state management.
9797
if (delta?.tool_calls) {
9898
for (const toolCall of delta.tool_calls) {

‎src/api/providers/lite-llm.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,15 @@ export class LiteLLMHandler extends RouterProvider implements SingleCompletionHa
257257
const delta = chunk.choices[0]?.delta
258258
const usage = chunk.usage as LiteLLMUsage
259259

260-
if (delta?.content) {
261-
yield { type: "text", text: delta.content }
262-
}
263-
264260
const reasoningText = extractReasoningFromDelta(delta)
265261
if (reasoningText) {
266262
yield { type: "reasoning", text: reasoningText }
267263
}
268264

265+
if (delta?.content) {
266+
yield { type: "text", text: delta.content }
267+
}
268+
269269
// Handle tool calls in stream - emit partial chunks for NativeToolCallParser
270270
if (delta?.tool_calls) {
271271
for (const toolCall of delta.tool_calls) {

‎src/api/providers/lm-studio.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,6 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
123123
const delta = chunk.choices[0]?.delta
124124
const finishReason = chunk.choices[0]?.finish_reason
125125

126-
if (delta?.content) {
127-
assistantText += delta.content
128-
for (const processedChunk of matcher.update(delta.content)) {
129-
yield processedChunk
130-
}
131-
}
132-
133126
// Reasoning models served by LM Studio (Qwen3, DeepSeek-R1, QwQ, ...) stream
134127
// their thinking in a dedicated `reasoning_content`/`reasoning` delta field
135128
// rather than as <think> tags inside `content`, so TagMatcher never sees it.
@@ -139,6 +132,13 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
139132
yield { type: "reasoning", text: reasoningText }
140133
}
141134

135+
if (delta?.content) {
136+
assistantText += delta.content
137+
for (const processedChunk of matcher.update(delta.content)) {
138+
yield processedChunk
139+
}
140+
}
141+
142142
// Handle tool calls in stream - emit partial chunks for NativeToolCallParser
143143
if (delta?.tool_calls) {
144144
for (const toolCall of delta.tool_calls) {

‎src/api/providers/mimo.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,18 @@ export class MimoHandler extends OpenAiHandler {
122122
}
123123
: delta
124124

125+
const reasoningText = extractReasoningFromDelta(delta)
126+
if (reasoningText) {
127+
yield { type: "reasoning", text: reasoningText }
128+
}
129+
125130
if (delta.content) {
126131
yield {
127132
type: "text",
128133
text: delta.content,
129134
}
130135
}
131136

132-
const reasoningText = extractReasoningFromDelta(delta)
133-
if (reasoningText) {
134-
yield { type: "reasoning", text: reasoningText }
135-
}
136-
137137
yield* this.processToolCalls(sanitizedDelta, finishReason, activeToolCallIds)
138138

139139
if (chunk.usage) {

0 commit comments

Comments
 (0)