Skip to content

refactor(providers): use extractReasoningFromDelta helper for reasoni…#588

Merged
edelauna merged 5 commits into
Zoo-Code-Org:mainfrom
daewoongoh:refactor/use-extract-reasoning-helper
Jun 15, 2026
Merged

refactor(providers): use extractReasoningFromDelta helper for reasoni…#588
edelauna merged 5 commits into
Zoo-Code-Org:mainfrom
daewoongoh:refactor/use-extract-reasoning-helper

Conversation

@daewoongoh

@daewoongoh daewoongoh commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Related GitHub Issue

#570

Description

This PR replaces duplicated inline reasoning-delta extraction logic across seven providers with the shared extractReasoningFromDelta helper.

Affected providers:

  • deepseek
  • mimo
  • openai
  • opencode-go
  • qwen-code
  • requesty
  • unbound

Previously, each provider had a near-identical streaming check for delta.reasoning_content and emitted reasoning chunks inline. Since src/api/providers/utils/extract-reasoning.ts already provides the canonical extraction behavior and is already used by other OpenAI-compatible providers, this PR brings the remaining duplicated call sites in line with the shared implementation.

Test Procedure

cd src && npx vitest run api/providers/__tests__/

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
    Existing tests cover the changed provider behavior, and the shared helper is already test-covered.
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Screenshots / Videos

N/A

Documentation Updates

  • No documentation updates are required.

Additional Notes

This is a focused provider refactor. It removes duplicated reasoning extraction logic and routes all seven affected providers through the shared extractReasoningFromDelta helper.

The intended behavior for existing reasoning_content chunks is unchanged for normal string payloads. The practical improvement is that these providers now also handle the reasoning fallback supported by the helper, so future or model-specific deltas using that field will stream reasoning text instead of being ignored.

Get in Touch

hehegwk_23849

Summary by CodeRabbit

  • Refactor
    • Streamed “reasoning” text extraction was unified across multiple provider integrations, ensuring consistent emission of reasoning chunks using a shared approach (including correct field precedence and fallback).
  • Tests
    • Added/extended unit tests for each provider to validate streaming reasoning parsing, covering emission, fallback when the primary field is missing, and preference when both reasoning fields are present.

Oh Daewoong and others added 2 commits June 12, 2026 13:18
…ng streams

Replace duplicated reasoning_content extraction logic across 7 providers
(deepseek, mimo, openai, opencode-go, qwen-code, requesty, unbound) with
the shared extractReasoningFromDelta helper. This also adds the OpenRouter-
style 'reasoning' field fallback to each provider for free.
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 7839fe2f-a5cf-446c-b7a0-86069bf3f261

📥 Commits

Reviewing files that changed from the base of the PR and between 5e8e5b1 and 54cb463.

📒 Files selected for processing (7)
  • src/api/providers/__tests__/deepseek.spec.ts
  • src/api/providers/__tests__/mimo.spec.ts
  • src/api/providers/__tests__/openai.spec.ts
  • src/api/providers/__tests__/opencode-go.spec.ts
  • src/api/providers/__tests__/qwen-code-native-tools.spec.ts
  • src/api/providers/__tests__/requesty.spec.ts
  • src/api/providers/__tests__/unbound.spec.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/api/providers/tests/openai.spec.ts
  • src/api/providers/tests/requesty.spec.ts
  • src/api/providers/tests/unbound.spec.ts

📝 Walkthrough

Walkthrough

Provider streaming handlers (DeepSeek, MiMo, OpenAI, Opencode-Go, Qwen-Code, Requesty, Unbound) now import and use extractReasoningFromDelta(delta) to emit { type: "reasoning", text } chunks when present; tests validate extraction, fallback from delta.reasoning_content to delta.reasoning, and precedence rules across all providers.

Changes

Consolidate Reasoning Extraction Helper Adoption

Layer / File(s) Summary
Add shared helper imports
src/api/providers/deepseek.ts, src/api/providers/mimo.ts, src/api/providers/openai.ts, src/api/providers/opencode-go.ts, src/api/providers/qwen-code.ts, src/api/providers/requesty.ts, src/api/providers/unbound.ts
Each provider adds an import for extractReasoningFromDelta from the local utils module.
Update provider streaming handlers to use helper
src/api/providers/deepseek.ts, src/api/providers/mimo.ts, src/api/providers/openai.ts, src/api/providers/opencode-go.ts, src/api/providers/qwen-code.ts, src/api/providers/requesty.ts, src/api/providers/unbound.ts
Streaming handlers now call extractReasoningFromDelta(delta) and yield { type: "reasoning", text } only when non-empty, replacing previous inline checks for delta.reasoning_content.
Add unit tests for extraction and fallback
src/api/providers/__tests__/deepseek.spec.ts, src/api/providers/__tests__/mimo.spec.ts, src/api/providers/__tests__/openai.spec.ts, src/api/providers/__tests__/opencode-go.spec.ts, src/api/providers/__tests__/qwen-code-native-tools.spec.ts, src/api/providers/__tests__/requesty.spec.ts, src/api/providers/__tests__/unbound.spec.ts
New tests across all providers verify reasoning chunk emission from delta.reasoning_content, fallback to delta.reasoning when reasoning_content is absent, and precedence of reasoning_content over reasoning when both are present.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~28 minutes

Possibly related issues

Possibly related PRs

  • Zoo-Code-Org/Zoo-Code#449: Related refactor of streaming delta reasoning handling that centralizes extraction logic using the same helper approach.

Suggested reviewers

  • taltas
  • navedmerchant
  • hannesrudolph
  • JamesRobert20

Poem

🐰 I hopped through deltas, soft and keen,
Shared the thinking that had been unseen.
From scattered checks to one small art,
Reasoning gathered, whole not parts.
Cheers from a rabbit — tidy code, smart! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: refactoring multiple providers to use a shared helper function for reasoning delta extraction instead of duplicated inline logic.
Description check ✅ Passed The description is well-structured, addresses all key template sections including issue link, implementation details, test procedure, and a thorough pre-submission checklist with all items completed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Add streaming tests for openai, requesty, and unbound covering:
- delta.reasoning_content yields a reasoning chunk
- delta.reasoning fallback yields a reasoning chunk when reasoning_content
  is absent (OpenRouter-style)

This raises line coverage on the changed reasoning paths from 50%.

@edelauna edelauna left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this, had one question related to test coverage

Comment on lines +242 to +259
it("falls back to delta.reasoning when reasoning_content is absent", async () => {
mockCreate.mockImplementationOnce(async () => ({
[Symbol.asyncIterator]: async function* () {
yield { choices: [{ delta: { reasoning: "router-style thought" }, index: 0 }] }
yield {
choices: [{ delta: {}, index: 0 }],
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
}
},
}))

const chunks: any[] = []
for await (const chunk of handler.createMessage(systemPrompt, messages)) {
chunks.push(chunk)
}

expect(chunks).toContainEqual({ type: "reasoning", text: "router-style thought" })
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the helper priority rule get exercised here? If a delta has both reasoning_content and reasoning populated, the expected winner is reasoning_content — but that case isn't covered at the provider level. The unit tests for extractReasoningFromDelta cover it, but a provider-level check would catch a future regression where the helper call gets bypassed or re-inlined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I added a provider-level priority test to cover the case where both delta.reasoning_content and delta.reasoning are present. The test asserts that only reasoning_content is emitted as the reasoning chunk, so it should catch regressions where the helper is bypassed or the logic is re-inlined incorrectly.

@edelauna edelauna left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking on this cleanup.

@edelauna edelauna added this pull request to the merge queue Jun 15, 2026
Merged via the queue into Zoo-Code-Org:main with commit 6daa153 Jun 15, 2026
10 checks passed
edelauna pushed a commit that referenced this pull request Jun 17, 2026
#588)

* refactor(providers): use extractReasoningFromDelta helper for reasoning streams

Replace duplicated reasoning_content extraction logic across 7 providers
(deepseek, mimo, openai, opencode-go, qwen-code, requesty, unbound) with
the shared extractReasoningFromDelta helper. This also adds the OpenRouter-
style 'reasoning' field fallback to each provider for free.

* test(providers): cover reasoning_content + reasoning fallback streaming

Add streaming tests for openai, requesty, and unbound covering:
- delta.reasoning_content yields a reasoning chunk
- delta.reasoning fallback yields a reasoning chunk when reasoning_content
  is absent (OpenRouter-style)

This raises line coverage on the changed reasoning paths from 50%.

* test(providers): add reasoning_content streaming tests to deepseek, mimo, opencode-go, qwen-code

---------

Co-authored-by: Oh Daewoong <dw.oh@samsung.com>
nigeldelviero pushed a commit to nigeldelviero/Zoo-Code that referenced this pull request Jun 22, 2026
Zoo-Code-Org#588)

* refactor(providers): use extractReasoningFromDelta helper for reasoning streams

Replace duplicated reasoning_content extraction logic across 7 providers
(deepseek, mimo, openai, opencode-go, qwen-code, requesty, unbound) with
the shared extractReasoningFromDelta helper. This also adds the OpenRouter-
style 'reasoning' field fallback to each provider for free.

* test(providers): cover reasoning_content + reasoning fallback streaming

Add streaming tests for openai, requesty, and unbound covering:
- delta.reasoning_content yields a reasoning chunk
- delta.reasoning fallback yields a reasoning chunk when reasoning_content
  is absent (OpenRouter-style)

This raises line coverage on the changed reasoning paths from 50%.

* test(providers): add reasoning_content streaming tests to deepseek, mimo, opencode-go, qwen-code

---------

Co-authored-by: Oh Daewoong <dw.oh@samsung.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants