( deps: ProviderDeps, url: string, opts: AddOptions, )
| 70 | } |
| 71 | |
| 72 | export async function handleProviderAdd( |
| 73 | deps: ProviderDeps, |
| 74 | url: string, |
| 75 | opts: AddOptions, |
| 76 | ): Promise<void> { |
| 77 | const apiKey = resolveApiKey(opts.apiKey, deps.env); |
| 78 | if (apiKey === undefined) { |
| 79 | deps.stderr.write( |
| 80 | 'Missing API key. Pass --api-key <key> or set KIMI_REGISTRY_API_KEY.\n', |
| 81 | ); |
| 82 | deps.exit(1); |
| 83 | } |
| 84 | |
| 85 | const trimmedUrl = url.trim(); |
| 86 | if (trimmedUrl.length === 0) { |
| 87 | deps.stderr.write('Registry URL is required.\n'); |
| 88 | deps.exit(1); |
| 89 | } |
| 90 | |
| 91 | const source: CustomRegistrySource = { |
| 92 | kind: 'apiJson', |
| 93 | url: trimmedUrl, |
| 94 | apiKey, |
| 95 | }; |
| 96 | |
| 97 | const harness = deps.getHarness(); |
| 98 | await harness.ensureConfigFile(); |
| 99 | |
| 100 | let entries: Awaited<ReturnType<typeof fetchCustomRegistry>>; |
| 101 | try { |
| 102 | entries = await fetchCustomRegistry(source); |
| 103 | } catch (error) { |
| 104 | const suffix = error instanceof CustomRegistryApiError ? ` (HTTP ${String(error.status)})` : ''; |
| 105 | deps.stderr.write(`Failed to fetch registry${suffix}: ${errorMessage(error)}\n`); |
| 106 | deps.exit(1); |
| 107 | } |
| 108 | |
| 109 | const entryList = Object.values(entries); |
| 110 | if (entryList.length === 0) { |
| 111 | deps.stderr.write(`Registry at ${trimmedUrl} contained no usable providers.\n`); |
| 112 | deps.exit(1); |
| 113 | } |
| 114 | |
| 115 | // `harness.removeProvider` reloads the config from disk on each call (see |
| 116 | // `core-impl.ts removeKimiProvider`), so calling it inside the apply loop |
| 117 | // would discard providers we already applied in memory but have not yet |
| 118 | // persisted. Drop every stale id up front in a single batch instead, then |
| 119 | // apply against the resulting fresh config. |
| 120 | let config = await harness.getConfig(); |
| 121 | const staleIds = entryList |
| 122 | .filter((entry) => config.providers[entry.id] !== undefined) |
| 123 | .map((entry) => entry.id); |
| 124 | for (const id of staleIds) { |
| 125 | config = await harness.removeProvider(id); |
| 126 | } |
| 127 | |
| 128 | const addedProviderIds: string[] = []; |
| 129 | let modelCount = 0; |
no test coverage detected