Skip to content

Commit ac0b8f2

Browse files
committed
refactor(providers): canonicalize model router calls
1 parent 3219853 commit ac0b8f2

9 files changed

Lines changed: 111 additions & 62 deletions

File tree

src/api/providers/fetchers/lmstudio.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import axios from "axios"
22
import { LLM, LLMInfo, LLMInstanceInfo, LMStudioClient } from "@lmstudio/sdk"
33

4-
import { type ModelInfo, lMStudioDefaultModelInfo } from "@roo-code/types"
4+
import { type ModelInfo, lMStudioDefaultModelInfo, providerIdentifiers } from "@roo-code/types"
55

66
import { flushModels, getModels } from "./modelCache"
77

@@ -19,7 +19,7 @@ export const forceFullModelDetailsLoad = async (baseUrl: string, modelId: string
1919
const client = new LMStudioClient({ baseUrl: lmsUrl })
2020
await client.llm.model(modelId)
2121
// Flush and refresh cache to get updated model details
22-
await flushModels({ provider: "lmstudio", baseUrl }, true)
22+
await flushModels({ provider: providerIdentifiers.lmstudio, baseUrl }, true)
2323

2424
// Mark this model as having full details loaded.
2525
modelsWithLoadedDetails.add(modelId)

src/api/providers/fetchers/modelEndpointCache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from "fs/promises"
44
import NodeCache from "node-cache"
55
import sanitize from "sanitize-filename"
66

7-
import type { ModelRecord } from "@roo-code/types"
7+
import { providerIdentifiers, type ModelRecord } from "@roo-code/types"
88

99
import { ContextProxy } from "../../../core/config/ContextProxy"
1010
import { RouterName } from "../../../shared/api"
@@ -44,7 +44,7 @@ export const getModelEndpoints = async ({
4444
}): Promise<ModelRecord> => {
4545
// OpenRouter is the only provider that supports model endpoints, but you
4646
// can see how we'd extend this to other providers in the future.
47-
if (router !== "openrouter" || !modelId || !endpoint) {
47+
if (router !== providerIdentifiers.openrouter || !modelId || !endpoint) {
4848
return {}
4949
}
5050

@@ -61,7 +61,7 @@ export const getModelEndpoints = async ({
6161
// Copy model-level capabilities from the parent model to each endpoint
6262
// These are capabilities that don't vary by provider (tools, reasoning, etc.)
6363
if (Object.keys(modelProviders).length > 0) {
64-
const parentModels = await getModels({ provider: "openrouter" })
64+
const parentModels = await getModels({ provider: providerIdentifiers.openrouter })
6565
const parentModel = parentModels[modelId]
6666

6767
if (parentModel) {

src/api/providers/kimi-code.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
KIMI_CODE_BASE_URL,
55
kimiCodeDefaultModelId,
66
kimiCodeDefaultModelInfo,
7+
providerIdentifiers,
78
type ModelInfo,
89
type ModelRecord,
910
} from "@roo-code/types"
@@ -63,7 +64,7 @@ export class KimiCodeHandler extends OpenAiHandler {
6364
if (!this.modelDiscoveryAttempted) {
6465
this.modelDiscoveryAttempted = true
6566
try {
66-
this.models = await getModels({ provider: "kimi-code", apiKey: accessToken })
67+
this.models = await getModels({ provider: providerIdentifiers.kimiCode, apiKey: accessToken })
6768
} catch (error) {
6869
// Model discovery is best-effort; preserve the configured ID and fallback metadata.
6970
console.debug("[KimiCode] Model discovery failed; using fallback model metadata", {

src/api/providers/lm-studio.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { Anthropic } from "@anthropic-ai/sdk"
22
import OpenAI from "openai"
33
import axios from "axios"
44

5-
import { type ModelInfo, openAiModelInfoSaneDefaults, LMSTUDIO_DEFAULT_TEMPERATURE } from "@roo-code/types"
5+
import {
6+
type ModelInfo,
7+
openAiModelInfoSaneDefaults,
8+
LMSTUDIO_DEFAULT_TEMPERATURE,
9+
providerIdentifiers,
10+
} from "@roo-code/types"
611

712
import type { ApiHandlerOptions } from "../../shared/api"
813

@@ -171,7 +176,7 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
171176

172177
override getModel(): { id: string; info: ModelInfo } {
173178
const models = getModelsFromCache({
174-
provider: "lmstudio",
179+
provider: providerIdentifiers.lmstudio,
175180
baseUrl: this.options.lmStudioBaseUrl,
176181
})
177182
if (models && this.options.lmStudioModelId && models[this.options.lmStudioModelId]) {

src/api/providers/openrouter.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
OPENROUTER_DEFAULT_PROVIDER_NAME,
1111
OPEN_ROUTER_PROMPT_CACHING_MODELS,
1212
DEEP_SEEK_DEFAULT_TEMPERATURE,
13+
providerIdentifiers,
1314
} from "@roo-code/types"
1415
import { TelemetryService } from "@roo-code/telemetry"
1516

@@ -164,9 +165,9 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
164165
private async loadDynamicModels(): Promise<void> {
165166
try {
166167
const [models, endpoints] = await Promise.all([
167-
getModels({ provider: "openrouter" }),
168+
getModels({ provider: providerIdentifiers.openrouter }),
168169
getModelEndpoints({
169-
router: "openrouter",
170+
router: providerIdentifiers.openrouter,
170171
modelId: this.options.openRouterModelId,
171172
endpoint: this.options.openRouterSpecificProvider,
172173
}),
@@ -535,9 +536,9 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
535536

536537
public async fetchModel() {
537538
const [models, endpoints] = await Promise.all([
538-
getModels({ provider: "openrouter" }),
539+
getModels({ provider: providerIdentifiers.openrouter }),
539540
getModelEndpoints({
540-
router: "openrouter",
541+
router: providerIdentifiers.openrouter,
541542
modelId: this.options.openRouterModelId,
542543
endpoint: this.options.openRouterSpecificProvider,
543544
}),

src/api/providers/poe.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type ModelInfo,
1010
type ReasoningEffortExtended,
1111
ApiProviderError,
12+
providerIdentifiers,
1213
} from "@roo-code/types"
1314
import { TelemetryService } from "@roo-code/telemetry"
1415

@@ -39,7 +40,7 @@ export class PoeHandler extends BaseProvider implements SingleCompletionHandler
3940
override getModel() {
4041
const id = this.options.apiModelId ?? poeDefaultModelId
4142
const cached = getModelsFromCache({
42-
provider: "poe",
43+
provider: providerIdentifiers.poe,
4344
apiKey: this.options.poeApiKey,
4445
baseUrl: this.options.poeBaseUrl,
4546
})

src/api/providers/requesty.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22
import OpenAI from "openai"
33

4-
import { type ModelInfo, type ModelRecord, requestyDefaultModelId, requestyDefaultModelInfo } from "@roo-code/types"
4+
import {
5+
type ModelInfo,
6+
type ModelRecord,
7+
providerIdentifiers,
8+
requestyDefaultModelId,
9+
requestyDefaultModelInfo,
10+
} from "@roo-code/types"
511

612
import type { ApiHandlerOptions } from "../../shared/api"
713
import { calculateApiCostOpenAI } from "../../shared/cost"
@@ -74,7 +80,7 @@ export class RequestyHandler extends BaseProvider implements SingleCompletionHan
7480
}
7581

7682
public async fetchModel() {
77-
this.models = await getModels({ provider: "requesty", baseUrl: this.baseURL })
83+
this.models = await getModels({ provider: providerIdentifiers.requesty, baseUrl: this.baseURL })
7884
return this.getModel()
7985
}
8086

src/api/providers/unbound.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22
import OpenAI from "openai"
33

4-
import { type ModelInfo, type ModelRecord, unboundDefaultModelId, unboundDefaultModelInfo } from "@roo-code/types"
4+
import {
5+
type ModelInfo,
6+
type ModelRecord,
7+
providerIdentifiers,
8+
unboundDefaultModelId,
9+
unboundDefaultModelInfo,
10+
} from "@roo-code/types"
511

612
import type { ApiHandlerOptions } from "../../shared/api"
713
import { calculateApiCostOpenAI } from "../../shared/cost"
@@ -68,7 +74,10 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand
6874
}
6975

7076
public async fetchModel() {
71-
this.models = await getModels({ provider: "unbound", apiKey: this.options.unboundApiKey })
77+
this.models = await getModels({
78+
provider: providerIdentifiers.unbound,
79+
apiKey: this.options.unboundApiKey,
80+
})
7281
return this.getModel()
7382
}
7483

0 commit comments

Comments
 (0)