feat: support openrouter reasoning_details #3605
Open
+172
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
This PR implements support for OpenRouter's reasoning_details feature, which allows preserving reasoning traces when making API requests. According to OpenRouter's documentation, reasoning_details can be passed back in subsequent API requests to maintain context of the model's reasoning process.
This enhancement enables the system to capture, consolidate, and preserve reasoning traces from models that support this feature, improving the quality and continuity of AI-assisted coding sessions.
Implementation
The implementation spans several layers of the application:
1. Stream Type Definition (
src/api/transform/stream.ts)ApiStreamReasoningDetailsChunktype to handle reasoning_details chunks from the API stream2. OpenRouter Provider (
src/api/providers/openrouter.ts)reasoning_detailsfrom the delta streamshouldSkipReasoningForModel()utility to skip reasoning for specific models (e.g., Grok-4 which only shows "thinking" without useful information)3. Message Transformation (
src/api/transform/openai-format.ts)consolidateReasoningDetails()function that:convertToOpenAiMessages()to extract reasoning_details from message parts and attach them to assistant messagesReasoningDetailobjects4. Task Orchestration (
src/core/task/Task.ts)reasoningDetailsarray to collect reasoning_details during streamingreasoning_detailsstream chunks5. Model Utilities (
src/utils/model-utils.ts)shouldSkipReasoningForModel()functionTrade-offs and Design Decisions
@ts-ignorecomments for reasoning_details properties since they're not part of the standard Anthropic types but are OpenRouter-specific extensionsScreenshots
This is an internal API enhancement without visible UI changes. The reasoning preservation happens behind the scenes to improve model context.
How to Test
Set up OpenRouter with a reasoning-enabled model:
Execute a multi-turn conversation:
Verify reasoning preservation:
Task.tsaround line 2103)Test Grok-4 filtering:
Test backward compatibility: