11import { Anthropic } from "@anthropic-ai/sdk"
2- import OpenAI from "openai"
32import { createOpenRouter } from "@openrouter/ai-sdk-provider"
43import { streamText , generateText } from "ai"
54
98 openRouterDefaultModelId ,
109 openRouterDefaultModelInfo ,
1110 OPENROUTER_DEFAULT_PROVIDER_NAME ,
12- OPEN_ROUTER_PROMPT_CACHING_MODELS ,
1311 DEEP_SEEK_DEFAULT_TEMPERATURE ,
1412 ApiProviderError ,
1513} from "@roo-code/types"
@@ -18,15 +16,7 @@ import { TelemetryService } from "@roo-code/telemetry"
1816import type { ApiHandlerOptions } from "../../shared/api"
1917import { calculateApiCostOpenAI } from "../../shared/cost"
2018
21- import {
22- convertToOpenAiMessages ,
23- sanitizeGeminiMessages ,
24- consolidateReasoningDetails ,
25- type ReasoningDetail ,
26- } from "../transform/openai-format"
27- import { convertToR1Format } from "../transform/r1-format"
28- import { addCacheBreakpoints as addAnthropicCacheBreakpoints } from "../transform/caching/anthropic"
29- import { addCacheBreakpoints as addGeminiCacheBreakpoints } from "../transform/caching/gemini"
19+ import { type ReasoningDetail } from "../transform/openai-format"
3020import { getModelParams } from "../transform/model-params"
3121import { convertToAiSdkMessages , convertToolsForAiSdk , processAiSdkStreamPart } from "../transform/ai-sdk"
3222
@@ -77,17 +67,13 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
7767 private createOpenRouterProvider ( options ?: {
7868 reasoning ?: { effort ?: string ; max_tokens ?: number ; exclude ?: boolean }
7969 headers ?: Record < string , string >
80- openAiMessages ?: OpenAI . Chat . ChatCompletionMessageParam [ ]
8170 } ) {
8271 const apiKey = this . options . openRouterApiKey ?? "not-provided"
8372 const baseURL = this . options . openRouterBaseUrl || "https://openrouter.ai/api/v1"
8473 const extraBody : Record < string , unknown > = { }
8574 if ( options ?. reasoning ) {
8675 extraBody . reasoning = options . reasoning
8776 }
88- if ( options ?. openAiMessages ) {
89- extraBody . messages = options . openAiMessages
90- }
9177 return createOpenRouter ( {
9278 apiKey,
9379 baseURL,
@@ -142,59 +128,6 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
142128 }
143129 }
144130
145- private buildOpenAiMessages (
146- systemPrompt : string ,
147- messages : Anthropic . Messages . MessageParam [ ] ,
148- modelId : string ,
149- ) : OpenAI . Chat . ChatCompletionMessageParam [ ] | undefined {
150- const isR1 = modelId . startsWith ( "deepseek/deepseek-r1" ) || modelId === "perplexity/sonar-reasoning"
151- const isGemini = modelId . startsWith ( "google/gemini" )
152- const needsCaching = OPEN_ROUTER_PROMPT_CACHING_MODELS . has ( modelId )
153- if ( ! isR1 && ! isGemini && ! needsCaching ) {
154- return undefined
155- }
156- let openAiMessages : OpenAI . Chat . ChatCompletionMessageParam [ ]
157- if ( isR1 ) {
158- openAiMessages = convertToR1Format ( [ { role : "user" , content : systemPrompt } , ...messages ] )
159- } else {
160- openAiMessages = [ { role : "system" , content : systemPrompt } , ...convertToOpenAiMessages ( messages ) ]
161- }
162- if ( isGemini ) {
163- openAiMessages = sanitizeGeminiMessages ( openAiMessages , modelId )
164- openAiMessages = openAiMessages . map ( ( msg ) => {
165- if ( msg . role === "assistant" ) {
166- const toolCalls = ( msg as any ) . tool_calls as any [ ] | undefined
167- const existingDetails = ( msg as any ) . reasoning_details as any [ ] | undefined
168- if ( toolCalls && toolCalls . length > 0 ) {
169- const hasEncrypted = existingDetails ?. some ( ( d ) => d . type === "reasoning.encrypted" ) ?? false
170- if ( ! hasEncrypted ) {
171- const fakeEncrypted = {
172- type : "reasoning.encrypted" ,
173- data : "skip_thought_signature_validator" ,
174- id : toolCalls [ 0 ] . id ,
175- format : "google-gemini-v1" ,
176- index : 0 ,
177- }
178- return {
179- ...msg ,
180- reasoning_details : [ ...( existingDetails ?? [ ] ) , fakeEncrypted ] ,
181- }
182- }
183- }
184- }
185- return msg
186- } )
187- }
188- if ( needsCaching ) {
189- if ( modelId . startsWith ( "google/" ) ) {
190- addGeminiCacheBreakpoints ( systemPrompt , openAiMessages )
191- } else {
192- addAnthropicCacheBreakpoints ( systemPrompt , openAiMessages )
193- }
194- }
195- return openAiMessages
196- }
197-
198131 override async * createMessage (
199132 systemPrompt : string ,
200133 messages : Anthropic . Messages . MessageParam [ ] ,
@@ -216,12 +149,9 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
216149 ? { "x-anthropic-beta" : "fine-grained-tool-streaming-2025-05-14" }
217150 : undefined
218151
219- const openAiMessages = this . buildOpenAiMessages ( systemPrompt , messages , modelId )
220- const openrouter = this . createOpenRouterProvider ( { reasoning, headers, openAiMessages } )
152+ const aiSdkMessages = convertToAiSdkMessages ( messages )
221153
222- const coreMessages = openAiMessages
223- ? convertToAiSdkMessages ( [ { role : "user" , content : "." } ] )
224- : convertToAiSdkMessages ( messages )
154+ const openrouter = this . createOpenRouterProvider ( { reasoning, headers } )
225155
226156 const tools = convertToolsForAiSdk ( metadata ?. tools )
227157
@@ -250,8 +180,8 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
250180 try {
251181 const result = streamText ( {
252182 model : openrouter . chat ( modelId ) ,
253- ... ( openAiMessages ? { } : { system : systemPrompt } ) ,
254- messages : coreMessages ,
183+ system : systemPrompt ,
184+ messages : aiSdkMessages ,
255185 maxOutputTokens : maxTokens && maxTokens > 0 ? maxTokens : undefined ,
256186 temperature,
257187 topP,
@@ -261,7 +191,7 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
261191 } )
262192
263193 for await ( const part of result . fullStream ) {
264- if ( part . type === "reasoning-delta" ) {
194+ if ( part . type === "reasoning-delta" && part . text !== "[REDACTED]" ) {
265195 accumulatedReasoningText += part . text
266196 }
267197 yield * processAiSdkStreamPart ( part )
@@ -283,7 +213,7 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
283213 | undefined
284214
285215 if ( providerReasoningDetails && providerReasoningDetails . length > 0 ) {
286- this . currentReasoningDetails = consolidateReasoningDetails ( providerReasoningDetails )
216+ this . currentReasoningDetails = providerReasoningDetails
287217 }
288218
289219 const usage = await result . usage
0 commit comments