Skip to content

Commit 7726e8e

Browse files
icecrasher321waleedlatif1
authored andcommitted
feat(openrouter): add open router to model block
1 parent 7c73f5f commit 7726e8e

File tree

8 files changed

+640
-1
lines changed

8 files changed

+640
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { type NextRequest, NextResponse } from 'next/server'
2+
import { createLogger } from '@/lib/logs/console/logger'
3+
4+
const logger = createLogger('OpenRouterModelsAPI')
5+
6+
export const dynamic = 'force-dynamic'
7+
8+
export async function GET(_request: NextRequest) {
9+
try {
10+
const response = await fetch('https://openrouter.ai/api/v1/models', {
11+
headers: { 'Content-Type': 'application/json' },
12+
cache: 'no-store',
13+
})
14+
15+
if (!response.ok) {
16+
logger.warn('Failed to fetch OpenRouter models', {
17+
status: response.status,
18+
statusText: response.statusText,
19+
})
20+
return NextResponse.json({ models: [] })
21+
}
22+
23+
const data = await response.json()
24+
const models = Array.isArray(data?.data)
25+
? Array.from(
26+
new Set(
27+
data.data
28+
.map((m: any) => m?.id)
29+
.filter((id: unknown): id is string => typeof id === 'string' && id.length > 0)
30+
.map((id: string) => `openrouter/${id}`)
31+
)
32+
)
33+
: []
34+
35+
logger.info('Successfully fetched OpenRouter models', {
36+
count: models.length,
37+
})
38+
39+
return NextResponse.json({ models })
40+
} catch (error) {
41+
logger.error('Error fetching OpenRouter models', {
42+
error: error instanceof Error ? error.message : 'Unknown error',
43+
})
44+
return NextResponse.json({ models: [] })
45+
}
46+
}

apps/sim/blocks/blocks/agent.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const getCurrentOllamaModels = () => {
2121
}
2222

2323
import { useOllamaStore } from '@/stores/ollama/store'
24+
import { useOpenRouterStore } from '@/stores/openrouter/store'
2425
import type { ToolResponse } from '@/tools/types'
2526

2627
const logger = createLogger('AgentBlock')
@@ -159,8 +160,9 @@ Create a system prompt appropriately detailed for the request, using clear langu
159160
required: true,
160161
options: () => {
161162
const ollamaModels = useOllamaStore.getState().models
163+
const openrouterModels = useOpenRouterStore.getState().models
162164
const baseModels = Object.keys(getBaseModelProviders())
163-
const allModels = [...baseModels, ...ollamaModels]
165+
const allModels = Array.from(new Set([...baseModels, ...ollamaModels, ...openrouterModels]))
164166

165167
return allModels.map((model) => {
166168
const icon = getProviderIcon(model)

apps/sim/providers/models.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ export interface ProviderDefinition {
6262
* Comprehensive provider definitions, single source of truth
6363
*/
6464
export const PROVIDER_DEFINITIONS: Record<string, ProviderDefinition> = {
65+
openrouter: {
66+
id: 'openrouter',
67+
name: 'OpenRouter',
68+
description: 'Unified access to many models via OpenRouter',
69+
defaultModel: '',
70+
modelPatterns: [/^openrouter\//],
71+
// Use OpenAIIcon for now; can add a specific OpenRouter icon later
72+
icon: OpenAIIcon,
73+
models: [],
74+
},
6575
openai: {
6676
id: 'openai',
6777
name: 'OpenAI',
@@ -859,6 +869,21 @@ export function updateOllamaModels(models: string[]): void {
859869
}))
860870
}
861871

872+
/**
873+
* Update OpenRouter models dynamically
874+
*/
875+
export function updateOpenRouterModels(models: string[]): void {
876+
PROVIDER_DEFINITIONS.openrouter.models = models.map((modelId) => ({
877+
id: modelId,
878+
pricing: {
879+
input: 0,
880+
output: 0,
881+
updatedAt: new Date().toISOString().split('T')[0],
882+
},
883+
capabilities: {},
884+
}))
885+
}
886+
862887
/**
863888
* Embedding model pricing - separate from chat models
864889
*/

0 commit comments

Comments
 (0)