Skip to content

Commit 48f6c2c

Browse files
committed
fix: add CometAPI fetcher and update modelCache
1 parent 38a6d24 commit 48f6c2c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ModelRecord } from "../../../shared/api"
2+
import { COMETAPI_MODELS } from "@roo-code/types"
3+
4+
/**
5+
* Fetch CometAPI models from the API
6+
* @param apiKey - The API key for CometAPI
7+
* @param baseUrl - The base URL for CometAPI (optional)
8+
* @returns The models from CometAPI
9+
*/
10+
export async function getCometAPIModels(apiKey?: string, baseUrl?: string): Promise<ModelRecord> {
11+
const url = `${baseUrl || "https://api.cometapi.com/v1"}/models`
12+
13+
try {
14+
if (!apiKey) {
15+
// Return fallback models if no API key is provided
16+
return COMETAPI_MODELS
17+
}
18+
19+
const response = await fetch(url, {
20+
headers: {
21+
Authorization: `Bearer ${apiKey}`,
22+
Accept: "application/json",
23+
},
24+
})
25+
26+
if (!response.ok) {
27+
console.warn(`Failed to fetch CometAPI models: ${response.status} ${response.statusText}`)
28+
// Return fallback models on error
29+
return COMETAPI_MODELS
30+
}
31+
32+
const data = await response.json()
33+
34+
// Transform the API response to match our ModelRecord format
35+
const models: ModelRecord = {}
36+
37+
if (data.data && Array.isArray(data.data)) {
38+
for (const model of data.data) {
39+
// Use fallback model info if available, otherwise create basic info
40+
const fallbackInfo = COMETAPI_MODELS[model.id]
41+
models[model.id] = fallbackInfo || {
42+
maxTokens: model.max_tokens || 8192,
43+
contextWindow: model.context_length || 128000,
44+
supportsImages: model.supports_images || false,
45+
supportsPromptCache: false,
46+
inputPrice: model.pricing?.prompt || 0,
47+
outputPrice: model.pricing?.completion || 0,
48+
description: model.description || model.id,
49+
}
50+
}
51+
}
52+
53+
// If no models were returned, use fallback models
54+
return Object.keys(models).length > 0 ? models : COMETAPI_MODELS
55+
} catch (error) {
56+
console.error("Error fetching CometAPI models:", error)
57+
// Return fallback models on error
58+
return COMETAPI_MODELS
59+
}
60+
}

src/api/providers/fetchers/modelCache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { getOllamaModels } from "./ollama"
2020
import { getLMStudioModels } from "./lmstudio"
2121
import { getIOIntelligenceModels } from "./io-intelligence"
2222
import { getDeepInfraModels } from "./deepinfra"
23+
import { getCometAPIModels } from "./cometapi"
2324
const memoryCache = new NodeCache({ stdTTL: 5 * 60, checkperiod: 5 * 60 })
2425

2526
async function writeModels(router: RouterName, data: ModelRecord) {
@@ -89,6 +90,9 @@ export const getModels = async (options: GetModelsOptions): Promise<ModelRecord>
8990
case "vercel-ai-gateway":
9091
models = await getVercelAiGatewayModels()
9192
break
93+
case "cometapi":
94+
models = await getCometAPIModels(options.apiKey, options.baseUrl)
95+
break
9296
default: {
9397
// Ensures router is exhaustively checked if RouterName is a strict union
9498
const exhaustiveCheck: never = provider

0 commit comments

Comments
 (0)