Open History to see the last commit
1/** Both Kimi Coding regions are explicitly documented in the same native page. */
2import { cachedDocs, docsReport, docsRun, tryDocs } from './model-facts.ts'
3import { classifyOpenCodeRoute, routeOnlyChatSpec } from './opencode-routes.ts'
4import { fetchText, sha256Text } from './types.ts'
5import type { ProviderConfig } from './types.ts'
6import {
7 KIMI_CODE_MODELS_URL,
8 kimiCodeChatRoute,
9 parseKimiCodeModels,
10} from './kimi-code-docs.ts'
11import type { KimiCodeRegion } from './kimi-code-docs.ts'
12
13export function kimiCodeProvider(
14 id: string,
15 displayName: string,
16 region: KimiCodeRegion,
17): ProviderConfig {
18 return {
19 id,
20 displayName,
21 specSourceUrl: KIMI_CODE_MODELS_URL,
22 modelsEndpoint: KIMI_CODE_MODELS_URL,
23 defaultDerivation: 'docs-derived',
24 bindSyncedRoutesOnly: true,
25 // The page states the route, not its request schema (issue #716).
26 bindStoredRoutesWithoutSchemas: true,
27 async listModels(_env, kv) {
28 const doc = await cachedDocs(kv, KIMI_CODE_MODELS_URL, async () => {
29 const html = await fetchText(KIMI_CODE_MODELS_URL)
30 return { html, hash: await sha256Text(html) }
31 })
32 const source = {
33 derivation: 'docs-derived' as const,
34 sourceUrl: KIMI_CODE_MODELS_URL,
35 sourceHash: doc.hash,
36 }
37 const run = docsRun()
38 const route = await tryDocs(run, KIMI_CODE_MODELS_URL, async () =>
39 kimiCodeChatRoute(doc.html, region),
40 )
41 return {
42 models: parseKimiCodeModels(doc.html, source).map((model) =>
43 route
44 ? {
45 ...model,
46 schemaEndpointId: route,
47 factSources: {
48 ...model.factSources,
49 schemaEndpointId: {
50 ...source,
51 path: `Base URLs; Chat Completions; ${region} base URL + the protocol's /chat/completions`,
52 },
53 },
54 absent: { pricing: 'cleared' as const },
55 }
56 : {
57 ...model,
58 absent: {
59 pricing: 'cleared' as const,
60 schemaEndpointId: 'unavailable' as const,
61 },
62 },
63 ),
64 docsFailures: docsReport(run),
65 }
66 },
67 async fetchSpec() {
68 const html = await fetchText(KIMI_CODE_MODELS_URL)
69 const hash = await sha256Text(html)
70 parseKimiCodeModels(html, {
71 derivation: 'docs-derived',
72 sourceUrl: KIMI_CODE_MODELS_URL,
73 sourceHash: hash,
74 })
75 return routeOnlyChatSpec([kimiCodeChatRoute(html, region)], {
76 url: KIMI_CODE_MODELS_URL,
77 hash,
78 })
79 },
80 classify: classifyOpenCodeRoute,
81 }
82}