-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathkimi-code.ts
More file actions
51 lines (45 loc) · 1.87 KB
/
Copy pathkimi-code.ts
File metadata and controls
51 lines (45 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { z } from "zod"
import { KIMI_CODE_BASE_URL, getKimiCodeModelInfo, type ModelInfo, type ModelRecord } from "@roo-code/types"
export const kimiCodeModelSchema = z.object({
id: z.string().min(1),
context_length: z.number().positive().optional(),
max_tokens: z.number().int().positive().optional(),
supports_reasoning: z.boolean().optional(),
supports_image_in: z.boolean().optional(),
display_name: z.string().optional(),
})
const kimiCodeModelsResponseSchema = z.object({ data: z.array(kimiCodeModelSchema) })
const KIMI_CODE_MODELS_TIMEOUT_MS = 10_000
export function mapKimiCodeModel(model: z.infer<typeof kimiCodeModelSchema>): ModelInfo {
const profile = getKimiCodeModelInfo(model.id)
return {
...profile,
contextWindow: model.context_length ?? profile.contextWindow,
maxTokens: model.max_tokens ?? profile.maxTokens,
supportsImages: model.supports_image_in ?? profile.supportsImages,
displayName: model.display_name,
}
}
export async function getKimiCodeModels(apiKey?: string): Promise<ModelRecord> {
if (!apiKey) throw new Error("Kimi Code authentication is required to fetch models")
const controller = new AbortController()
const timeout = setTimeout(
() => controller.abort(new Error("Kimi Code models request timed out")),
KIMI_CODE_MODELS_TIMEOUT_MS,
)
try {
const response = await fetch(`${KIMI_CODE_BASE_URL}/models`, {
headers: { Accept: "application/json", Authorization: `Bearer ${apiKey}` },
signal: controller.signal,
})
if (!response.ok) {
const error = new Error(`Kimi Code models request failed: ${response.status} ${response.statusText}`)
;(error as Error & { status?: number }).status = response.status
throw error
}
const parsed = kimiCodeModelsResponseSchema.parse(await response.json())
return Object.fromEntries(parsed.data.map((model) => [model.id, mapKimiCodeModel(model)]))
} finally {
clearTimeout(timeout)
}
}