-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(cli): fall back to built-in models.dev catalog when fetch fails #2416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
7Sageer
merged 1 commit into
MoonshotAI:main
from
mangeshraut712:fix/catalog-builtin-fallback
Jul 31, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Fall back to the built-in models.dev catalog snapshot when the public catalog is unreachable, so Known third-party provider import still works offline or in blocked networks. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { | ||
| DEFAULT_CATALOG_URL, | ||
| fetchCatalog, | ||
| loadBuiltInCatalog, | ||
| type Catalog, | ||
| type FetchCatalogOptions, | ||
| } from '@moonshot-ai/kimi-code-sdk'; | ||
|
|
||
| import { BUILT_IN_CATALOG_JSON } from '#/built-in-catalog'; | ||
|
|
||
| export interface FetchCatalogOrBuiltInResult { | ||
| readonly catalog: Catalog; | ||
| /** True when the network fetch failed and the release-build snapshot was used. */ | ||
| readonly fromBuiltIn: boolean; | ||
| } | ||
|
|
||
| export interface FetchCatalogOrBuiltInOptions extends FetchCatalogOptions { | ||
| /** | ||
| * Override the built-in snapshot JSON (tests). Defaults to the tsdown-injected | ||
| * `__KIMI_CODE_BUILT_IN_CATALOG__` constant. | ||
| */ | ||
| readonly builtInJson?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches a models.dev-style catalog, falling back to the release-build | ||
| * snapshot when the public default URL is unreachable. | ||
| * | ||
| * Custom `--url` overrides never fall back — a private registry must fail | ||
| * loudly rather than silently substitute models.dev. User abort | ||
| * (`signal.aborted`) also skips the fallback so Cancel stays Cancel. | ||
| */ | ||
| export async function fetchCatalogOrBuiltIn( | ||
| url: string, | ||
| options: FetchCatalogOrBuiltInOptions = {}, | ||
| ): Promise<FetchCatalogOrBuiltInResult> { | ||
| try { | ||
| const catalog = await fetchCatalog(url, options); | ||
| return { catalog, fromBuiltIn: false }; | ||
| } catch (error) { | ||
| if (options.signal?.aborted) throw error; | ||
| if (isAbortError(error)) throw error; | ||
| if (url !== DEFAULT_CATALOG_URL) throw error; | ||
| const builtIn = loadBuiltInCatalog(options.builtInJson ?? BUILT_IN_CATALOG_JSON); | ||
| if (builtIn === undefined) throw error; | ||
| return { catalog: builtIn, fromBuiltIn: true }; | ||
| } | ||
| } | ||
|
|
||
| function isAbortError(error: unknown): boolean { | ||
| return ( | ||
| (typeof DOMException !== 'undefined' && | ||
| error instanceof DOMException && | ||
| error.name === 'AbortError') || | ||
| (error instanceof Error && error.name === 'AbortError') | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { DEFAULT_CATALOG_URL, CatalogFetchError } from '@moonshot-ai/kimi-code-sdk'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { fetchCatalogOrBuiltIn } from '#/utils/catalog-fetch'; | ||
|
|
||
| const BUILT_IN = JSON.stringify({ | ||
| anthropic: { | ||
| id: 'anthropic', | ||
| name: 'Anthropic', | ||
| models: { 'claude-test': { id: 'claude-test', limit: { context: 200000 } } }, | ||
| }, | ||
| }); | ||
|
|
||
| function jsonResponse(body: unknown, status = 200): Response { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
|
|
||
| describe('fetchCatalogOrBuiltIn', () => { | ||
| it('returns the network catalog when models.dev is reachable', async () => { | ||
| const network = { openai: { id: 'openai', models: {} } }; | ||
| const fetchImpl = vi.fn(async () => jsonResponse(network)); | ||
|
|
||
| const result = await fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { | ||
| fetchImpl: fetchImpl as unknown as typeof fetch, | ||
| builtInJson: BUILT_IN, | ||
| }); | ||
|
|
||
| expect(result.fromBuiltIn).toBe(false); | ||
| expect(result.catalog).toEqual(network); | ||
| }); | ||
|
|
||
| it('falls back to the built-in snapshot when the default URL fetch fails', async () => { | ||
| const fetchImpl = vi.fn(async () => jsonResponse('no', 503)); | ||
|
|
||
| const result = await fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { | ||
| fetchImpl: fetchImpl as unknown as typeof fetch, | ||
| builtInJson: BUILT_IN, | ||
| }); | ||
|
|
||
| expect(result.fromBuiltIn).toBe(true); | ||
| expect(result.catalog).toEqual(JSON.parse(BUILT_IN)); | ||
| }); | ||
|
|
||
| it('does not fall back for a custom catalog URL', async () => { | ||
| const fetchImpl = vi.fn(async () => jsonResponse('no', 500)); | ||
|
|
||
| await expect( | ||
| fetchCatalogOrBuiltIn('https://example.test/private.json', { | ||
| fetchImpl: fetchImpl as unknown as typeof fetch, | ||
| builtInJson: BUILT_IN, | ||
| }), | ||
| ).rejects.toBeInstanceOf(CatalogFetchError); | ||
| }); | ||
|
|
||
| it('does not fall back when the caller aborted the request', async () => { | ||
| const controller = new AbortController(); | ||
| controller.abort(); | ||
| const fetchImpl = vi.fn(async () => { | ||
| throw new DOMException('Aborted', 'AbortError'); | ||
| }); | ||
|
|
||
| await expect( | ||
| fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { | ||
| signal: controller.signal, | ||
| fetchImpl: fetchImpl as unknown as typeof fetch, | ||
| builtInJson: BUILT_IN, | ||
| }), | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('rethrows when fetch fails and no built-in snapshot is available', async () => { | ||
| const fetchImpl = vi.fn(async () => jsonResponse('no', 500)); | ||
|
|
||
| await expect( | ||
| fetchCatalogOrBuiltIn(DEFAULT_CATALOG_URL, { | ||
| fetchImpl: fetchImpl as unknown as typeof fetch, | ||
| builtInJson: '', | ||
| }), | ||
| ).rejects.toBeInstanceOf(CatalogFetchError); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the default models.dev fetch fails, this returns the embedded snapshot as if it were the live catalog, but that snapshot is generated by
apps/kimi-code/scripts/update-catalog.mjswithKEEP_MODELomitting fields thatcatalogProviderModelsconsumes, including per-modelprovideroverrides,reasoning_options, andstatus. In blocked/offline networks this can misconfigure imported gateway models that need per-model protocol/baseUrl overrides, or surface models the live catalog path would filter. Please keep the fields used by the normalizer in the snapshot, or normalize the catalog before embedding it.Useful? React with 👍 / 👎.