@@ -2,14 +2,15 @@ import { remixAILogger } from '../../helpers/logger'
22import { ChatAnthropic } from '@langchain/anthropic'
33import { ChatMistralAI } from '@langchain/mistralai'
44import { ChatOpenAI } from '@langchain/openai'
5+ import { ChatOpenRouter } from '@langchain/openrouter'
56import { ChatOllama } from '@langchain/ollama'
67import { ChatBedrockConverse } from '@langchain/aws'
78import { BaseChatModel } from '@langchain/core/language_models/chat_models'
89import { HTTPClient } from '@mistralai/mistralai/lib/http.js'
910import { endpointUrls } from '@remix-endpoints-helper'
1011import { ModelSelection , IUserApiKeyConfig } from '../../types/deepagent'
1112import { DAPP_MAX_TOKENS } from './constants'
12- import { getRemixAuthHeader } from '../auth'
13+ import { getRemixAuthHeader , getRemixAccessToken } from '../auth'
1314import { discoverOllamaHost , getBestAvailableModel , getModelCapabilities } from '../local/ollama'
1415
1516const AI_DEBUG = ( ( ) => {
@@ -460,39 +461,58 @@ export async function createModelInstance(
460461 case 'openrouter' : {
461462 const useDirectApi = ! ! ( userApiKeys ?. useOwnKeys && userApiKeys ?. openrouterApiKey )
462463 remixAILogger . log ( `[ModelFactory] Creating OpenRouter model: ${ modelId } ${ useDirectApi ? ' (direct API)' : ' (proxy)' } ` )
464+ // Own key → talk to OpenRouter directly through its dedicated LangChain SDK.
465+ if ( useDirectApi ) {
466+ return wrapModelForDebug ( new ChatOpenRouter ( {
467+ apiKey : userApiKeys ! . openrouterApiKey as string ,
468+ model : modelId ,
469+ temperature : 0.7 ,
470+ maxTokens : maxTokens ,
471+ maxRetries : 0 ,
472+ } ) , `openrouter/${ modelId } ` )
473+ }
474+ // No key → route through the Remix proxy (OpenAI-compatible endpoint).
463475 return wrapModelForDebug ( new ChatOpenAI ( {
464- apiKey : useDirectApi ? ( userApiKeys ! . openrouterApiKey as string ) : 'proxy-handled' ,
476+ apiKey : 'proxy-handled' ,
465477 model : modelId ,
466478 temperature : 0.7 ,
467479 maxTokens : maxTokens ,
468480 streaming : true ,
469481 maxRetries : 0 ,
470- ...( useDirectApi
471- ? {
472- configuration : {
473- baseURL : 'https://openrouter.ai/api/v1'
474- }
475- }
476- : {
477- configuration : {
478- baseURL : `${ endpointUrls . langchain } /openrouter` ,
479- fetch : authedFetch
480- }
481- } )
482+ configuration : {
483+ baseURL : `${ endpointUrls . langchain } /openrouter` ,
484+ fetch : authedFetch
485+ }
482486 } ) , `openrouter/${ modelId } ` )
487+ }
488+
483489 case 'bedrock' : {
484490 const bedrockBearerToken = userApiKeys ?. bedrockBearerToken ?. trim ( )
485- if ( ! bedrockBearerToken ) {
486- throw new Error ( '[ModelFactory] AWS Bedrock models requires a Bedrock API key. Add it under Settings → Bring Your Own API Keys.' )
487- }
488-
489491 const region = DEFAULT_BEDROCK_REGION
490492 const bedrockModelId = resolveBedrockModelId ( modelId , region )
491- remixAILogger . log ( `[ModelFactory] Creating AWS Bedrock model: ${ bedrockModelId } @ ${ region } ` )
493+ const useDirectApi = ! ! bedrockBearerToken
494+
495+ if ( useDirectApi ) {
496+ remixAILogger . log ( `[ModelFactory] Creating AWS Bedrock model: ${ bedrockModelId } @ ${ region } (direct API)` )
497+ return wrapModelForDebug ( patchBedrockBindTools ( new ChatBedrockConverse ( {
498+ model : bedrockModelId ,
499+ region,
500+ bedrockBearerToken,
501+ } ) ) , `bedrock/${ bedrockModelId } ` )
502+ }
503+
504+ // Proxy mode: no user-provided key. Route the Bedrock Converse calls through
505+ const remixToken = getRemixAccessToken ( )
506+ if ( ! remixToken ) {
507+ throw new Error ( '[ModelFactory] AWS Bedrock requires you to be signed in to use the Remix proxy, or add your own Bedrock API key under Settings → Bring Your Own API Keys.' )
508+ }
509+ const proxyEndpointHost = `${ endpointUrls . langchain } /bedrock` . replace ( / ^ h t t p s ? : \/ \/ / , '' )
510+ remixAILogger . log ( `[ModelFactory] Creating AWS Bedrock model: ${ bedrockModelId } @ ${ region } (proxy)` )
492511 return wrapModelForDebug ( patchBedrockBindTools ( new ChatBedrockConverse ( {
493512 model : bedrockModelId ,
494513 region,
495- bedrockBearerToken,
514+ bedrockBearerToken : remixToken ,
515+ endpointHost : proxyEndpointHost ,
496516 } ) ) , `bedrock/${ bedrockModelId } ` )
497517 }
498518
0 commit comments