Skip to content

Commit b941b65

Browse files
committed
PR feedback
1 parent 60e900c commit b941b65

File tree

1 file changed

+57
-55
lines changed
  • src/api/providers/fetchers

1 file changed

+57
-55
lines changed

src/api/providers/fetchers/roo.ts

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,74 +24,76 @@ export async function getRooModels(baseUrl: string, apiKey?: string): Promise<Mo
2424
}
2525

2626
// Construct the models endpoint URL
27-
// Strip trailing /v1 if present to avoid /v1/v1/models
28-
const normalizedBase = baseUrl.endsWith("/v1") ? baseUrl.slice(0, -3) : baseUrl
27+
// Strip trailing /v1 or /v1/ to avoid /v1/v1/models
28+
const normalizedBase = baseUrl.replace(/\/?v1\/?$/, "")
2929
const url = `${normalizedBase}/v1/models`
3030

3131
// Use fetch with AbortController for better timeout handling
3232
const controller = new AbortController()
3333
const timeoutId = setTimeout(() => controller.abort(), 10000)
3434

35-
const response = await fetch(url, {
36-
headers,
37-
signal: controller.signal,
38-
})
35+
try {
36+
const response = await fetch(url, {
37+
headers,
38+
signal: controller.signal,
39+
})
3940

40-
clearTimeout(timeoutId)
41-
42-
if (!response.ok) {
43-
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
44-
}
41+
if (!response.ok) {
42+
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
43+
}
4544

46-
const data = await response.json()
47-
const models: ModelRecord = {}
45+
const data = await response.json()
46+
const models: ModelRecord = {}
4847

49-
// Validate response against schema
50-
const parsed = RooModelsResponseSchema.safeParse(data)
48+
// Validate response against schema
49+
const parsed = RooModelsResponseSchema.safeParse(data)
5150

52-
if (!parsed.success) {
53-
console.error("Error fetching Roo Code Cloud models: Unexpected response format", data)
54-
console.error("Validation errors:", parsed.error.format())
55-
throw new Error("Failed to fetch Roo Code Cloud models: Unexpected response format.")
56-
}
51+
if (!parsed.success) {
52+
console.error("Error fetching Roo Code Cloud models: Unexpected response format", data)
53+
console.error("Validation errors:", parsed.error.format())
54+
throw new Error("Failed to fetch Roo Code Cloud models: Unexpected response format.")
55+
}
5756

58-
// Process the validated model data
59-
for (const model of parsed.data.data) {
60-
const modelId = model.id
61-
62-
if (!modelId) continue
63-
64-
// Extract model data from the validated API response
65-
// All required fields are guaranteed by the schema
66-
const contextWindow = model.context_window
67-
const maxTokens = model.max_tokens
68-
const tags = model.tags || []
69-
const pricing = model.pricing
70-
71-
// Determine if the model supports images based on tags
72-
const supportsImages = tags.includes("vision")
73-
74-
// Parse pricing (API returns strings, convert to numbers)
75-
const inputPrice = parseFloat(pricing.input)
76-
const outputPrice = parseFloat(pricing.output)
77-
const cacheReadPrice = pricing.input_cache_read ? parseFloat(pricing.input_cache_read) : undefined
78-
const cacheWritePrice = pricing.input_cache_write ? parseFloat(pricing.input_cache_write) : undefined
79-
80-
models[modelId] = {
81-
maxTokens,
82-
contextWindow,
83-
supportsImages,
84-
supportsPromptCache: Boolean(cacheReadPrice !== undefined),
85-
inputPrice,
86-
outputPrice,
87-
cacheWritesPrice: cacheWritePrice,
88-
cacheReadsPrice: cacheReadPrice,
89-
description: model.description || model.name,
90-
deprecated: model.deprecated || false,
57+
// Process the validated model data
58+
for (const model of parsed.data.data) {
59+
const modelId = model.id
60+
61+
if (!modelId) continue
62+
63+
// Extract model data from the validated API response
64+
// All required fields are guaranteed by the schema
65+
const contextWindow = model.context_window
66+
const maxTokens = model.max_tokens
67+
const tags = model.tags || []
68+
const pricing = model.pricing
69+
70+
// Determine if the model supports images based on tags
71+
const supportsImages = tags.includes("vision")
72+
73+
// Parse pricing (API returns strings, convert to numbers)
74+
const inputPrice = parseFloat(pricing.input)
75+
const outputPrice = parseFloat(pricing.output)
76+
const cacheReadPrice = pricing.input_cache_read ? parseFloat(pricing.input_cache_read) : undefined
77+
const cacheWritePrice = pricing.input_cache_write ? parseFloat(pricing.input_cache_write) : undefined
78+
79+
models[modelId] = {
80+
maxTokens,
81+
contextWindow,
82+
supportsImages,
83+
supportsPromptCache: Boolean(cacheReadPrice !== undefined),
84+
inputPrice,
85+
outputPrice,
86+
cacheWritesPrice: cacheWritePrice,
87+
cacheReadsPrice: cacheReadPrice,
88+
description: model.description || model.name,
89+
deprecated: model.deprecated || false,
90+
}
9191
}
92-
}
9392

94-
return models
93+
return models
94+
} finally {
95+
clearTimeout(timeoutId)
96+
}
9597
} catch (error: any) {
9698
console.error("Error fetching Roo Code Cloud models:", error.message ? error.message : error)
9799

0 commit comments

Comments
 (0)