-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: Refactor OpenRouter provider to use Vercel AI SDK #10778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Replace direct OpenAI SDK usage with @openrouter/ai-sdk-provider - Add 'ai' package for streamText() and generateText() functions - Extract reusable AI SDK conversion utilities to src/api/transform/ai-sdk.ts: - convertToAiSdkMessages(): Anthropic messages to CoreMessage format - convertToolsForAiSdk(): OpenAI tools to AI SDK tool format - processAiSdkStreamPart(): AI SDK stream events to ApiStreamChunk - Update tests to mock AI SDK functions instead of OpenAI client - Add comprehensive tests for the new ai-sdk.ts utility module This follows Vercel's AI SDK provider pattern for standardized LLM integration and enables easier migration of other providers in the future.
Review of commit 5260473. Found 1 issue that needs attention before merging.
Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues. |
| private client: OpenAI | ||
| protected models: ModelRecord = {} | ||
| protected endpoints: ModelRecord = {} | ||
| private readonly providerName = "OpenRouter" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old implementation had a getReasoningDetails() method and currentReasoningDetails accumulator that Task.ts relies on (see line 959 in Task.ts). This method accumulates reasoning_details from the stream (handling reasoning.text, reasoning.summary, and reasoning.encrypted types) and provides them for persistence in API conversation history. This is required for models like Gemini 3 via OpenRouter that use the reasoning.encrypted format - without it, reasoning context won't be preserved across multi-turn conversations, potentially breaking those model integrations.
Fix it with Roo Code or mention @roomote and request a fix.
- Fixed tool result output format to use typed object { type: 'text', value: string }
instead of plain string to satisfy AI SDK validation schema
- Added tool name resolution by building a map of tool call IDs to names
- Updated tests to reflect new output format
Summary
Migrate the OpenRouter provider from the OpenAI SDK to the Vercel AI SDK for a more standardized and maintainable implementation.
Closes COM-502
Problem
The current OpenRouter provider implementation uses the raw OpenAI SDK directly, which requires complex message transformation and reasoning token handling code that is difficult to maintain.
Solution
Refactor the OpenRouter provider to use the
@openrouter/ai-sdk-providerpackage which integrates with Vercel's AI SDK. This provides:streamTextandgenerateTextCoreMessageformatChanges
src/api/transform/ai-sdk.tswith message/tool conversion utilitiessrc/api/providers/openrouter.tsto use AI SDK patternssrc/api/transform/__tests__/ai-sdk.spec.tssrc/api/providers/__tests__/openrouter.spec.tswith new test patternsTesting
tsc --noEmitpasses in bothpackages/typesandsrcImportant
Refactor OpenRouter provider to use Vercel AI SDK, simplifying implementation and improving maintainability.
OpenRouterHandlerinopenrouter.tsto use Vercel AI SDK, replacing OpenAI SDK.createMessageandcompletePromptusingstreamTextandgenerateTextfrom Vercel AI SDK.ai-sdk.tsfor converting messages and tools to AI SDK format and processing stream parts.openrouter.spec.tsto mock Vercel AI SDK functions and verify new behavior.ai-sdk.spec.tsfor conversion utilities.@openrouter/ai-sdk-providerandaitodevDependencies.zodversion across packages.This description was created by
for 5260473. You can customize this summary. It will automatically update as commits are pushed.