kimi-code/packages/oauth/src/refreshProviderModels.ts
liruifengv 2bf7ed22d7
feat(auth): split model readiness from sign-in state in /api/v1/auth (#3293)
* feat(auth): split model readiness from sign-in state in /api/v1/auth

GET /api/v1/auth now reports models_ready (the default model resolves
against the configured catalog, providerless and env-injected models
included) instead of the compound ready flag, and no longer carries
default_model — config values are served by /config alone. The v1
summary schema follows.

OAuth managed-model refreshes now heal a lost default model: the
refresh snapshot includes defaultModel, so an unchanged catalog with
a missing default still lands the write-back branch and re-selects
one. The refresh also rebases onto a fresh config read after the
remote fetch, so a model or thinking change made during the fetch is
no longer overwritten. The shared discovery refresh path (scheduler,
POST /providers/{id}:refresh) heals the default the same way.

Config changes are now published to WS clients on every write path:
a debounced+trailing publisher bridges IConfigService section changes
to ConfigChanged with camelCase changedFields and a full config
projection, and the broadcaster forwards event.config.changed and
event.model_catalog.changed (both previously published but never
delivered). All three event types are registered in the event unions,
so session_event parsing and AsyncAPI describe them.

BREAKING CHANGE: GET /api/v1/auth drops the ready and default_model
fields in favor of models_ready; event.config.changed's changedFields
is now camelCase domain names instead of the raw snake_case request
keys (v1 summary schema follows).

* fix(kap-server): expose the session model in session list projections

GET /api/v1/sessions hardcoded agent_config.model to '' and the v2
projection had no model field at all, so clients could only learn a
session's model via the post-select /status read — which races the WS
replay and often never lands. SessionFacts now carries the live
session's model (same source as the snapshot route), toWireSession
emits it, and the v2 activity domain gains a nullable model field.

* fix(kap-server): gate prompt submission on the effective session model

The submit gate called ensureReady() with no override, so it only ever
validated config.default_model: a session with a bound model (or a
prompt carrying one) was rejected with 40113 whenever default_model was
missing or dangling. Pass the effective model (request model, then the
agent profile's bound model, falling back to default_model inside
ensureReady) on both the prompt submit and btw routes.

* fix(agent-core-v2): honor defaultProvider in model readiness resolution

resolveModelForReady stopped at the flat baseUrl fallback, so a model
that omits provider/providerId and relies on the configured
defaultProvider resolved at runtime (ModelCatalog.resolveProviderContext
falls back to it) while /api/v1/auth reported models_ready:false and the
send gate rejected the prompt. Mirror the runtime order (providerId ->
provider -> defaultProvider -> flat baseUrl) and pass the configured
default provider from both readiness callers.

* fix(kap-server): redact inline model credentials from config responses

toConfigResponse only redacted the providers section, so a model's
inline apiKey/oauth rode GET /config verbatim and, via the new
event.config.changed publisher, every WS connection plus the persistent
event journal. Project the models section the same way: strip
credential fields and report has_api_key.

* fix(agent-core-v2): honor defaultProvider in ensureReady credential checks

The readiness phase learned the defaultProvider fallback, but the
credential phase right after still derived the provider only from the
model's explicit fields: a model omitting provider/providerId passed
readiness yet missed the default provider's apiKey/OAuth material and
prompts failed with auth.token_missing. Mirror the same provider chain
(providerId -> provider -> defaultProvider) when resolving credentials.

* fix(kap-server): validate the model a profile bind will select at the prompt gate

The gate validated the session's current model even for a prompt that
switches profile without a model — but bind falls back to defaultModel
in that case, so a stale session model drew a misleading 40113 before
bind could run. Gate on bind's selection order instead: the request's
explicit model, then the default on a profile switch, then the session's
bound model.

* fix(kap-server): redact inline service credentials from config responses

The earlier redaction covered providers and models, but toConfigResponse
still passed the services section through verbatim: inline or
env-injected apiKey, oauth references, and credential-bearing
customHeaders rode GET /config and, via the event.config.changed
publisher, every WS connection plus the persistent event journal.
Project services the same way: strip apiKey/oauth into has_api_key and
report only the header names as custom_header_keys (the MCP
envKeys/headerKeys convention).

* fix(kap-server): keep unlisted config domains through event validation

The config.changed broadcaster returned the zod-parsed config, which
strips domains absent from configResponseSchema (mcp, identity,
model_catalog, image, tools, token_counting): changedFields named them
while the advertised full snapshot no longer matched GET /api/v1/config.
Make the response projection passthrough (defineRoute validates only
requests, so REST responses are unaffected).

* fix(agent-core-v2): use the exact configured key for model readiness lookups

resolveModelForReady trimmed the model id before the models-table lookup
while ModelCatalog and ensureReady use the configured string as an exact
record key: a whitespace-padded default_model was reported ready and then
crashed the submit gate with an internal error instead of 40113, and a
legitimate key containing spaces was reported dangling. Trim only rejects
blank values now; the lookup always uses the raw key.

* fix(protocol): keep unlisted config domains in the shared event projection

The shared configResponseSchema stripped domains it does not enumerate
(mcp, identity, model_catalog, image, tools, token_counting, subagent,
secondary_model), so event.config.changed parsed through agentEventSchema
named them in changedFields while omitting their values. Make the shared
projection passthrough like the kap-server-local one.

* fix(agent-core-v2): use the exact default_provider key in readiness checks

The defaultProvider fallback trimmed the configured value before the
providers-table lookup while ProviderService and ModelCatalog use the
configured string verbatim: a whitespace-padded default_provider could
build successfully yet report not-ready (40113), or report ready for a
provider runtime resolution cannot find. Trim only rejects blank values;
the lookup uses the raw key.

* chore: sync web dist from code-app

Rebuild the bundled web UI against this branch's /auth contract (models_ready, no ready/default_model): the previous bundle still read the old fields and stayed in the not-ready flow against this server.

code-app: 000d2594ff3e95b553be326126bab3f939b62944

* Revert "chore: sync web dist from code-app"

This reverts commit 9400a24a03863b3e8b780dda251540f824f02f3a.

* fix(oauth): rebase the default selection after the refresh fetch

A provider refresh snapshots the config before the remote catalog fetch;
when the user selects a default model while the fetch is in flight, the
stale snapshot's empty default made an otherwise unchanged catalog enter
the write path and the self-heal persisted the generated default over the
user's newer selection. Each branch now re-reads and rebases the
default/thinking selection after its fetch, before cloning, comparing,
or writing.

* style(kap-server): pass optional custom_header_keys without conditional spread
2026-08-28 15:53:56 +08:00

779 lines
28 KiB
TypeScript

import {
applyCustomRegistryProvider,
fetchCustomRegistry,
removeCustomRegistryProvider,
type CustomRegistrySource,
} from './custom-registry';
import {
applyManagedApiKeyProviderModels,
applyManagedKimiCodeConfig,
fetchManagedKimiCodeModels,
KIMI_CODE_PLATFORM_ID,
KIMI_CODE_PROVIDER_NAME,
resolveKimiCodeRuntimeAuth,
type ManagedKimiConfigShape,
type ManagedKimiModelAlias,
type ManagedKimiOAuthRef,
} from './managed-kimi-code';
import { isManagedKimiCodeBaseUrl } from './managed-usage';
import {
applyOpenPlatformConfig,
fetchOpenPlatformModels,
filterModelsByPrefix,
getOpenPlatformById,
isOpenPlatformId,
} from './open-platform';
import { isRecord } from './utils';
/**
* Host capabilities the refresh orchestrator needs. Intentionally typed against
* {@link ManagedKimiConfigShape} (the oauth package's own minimal config shape)
* rather than the SDK's full `KimiConfig`, so this module has no dependency on
* `agent-core` / the SDK and can be reused by both the CLI and the daemon.
*/
export interface RefreshProviderHost {
getConfig(): Promise<ManagedKimiConfigShape>;
removeProvider(providerId: string): Promise<ManagedKimiConfigShape>;
setConfig(patch: ManagedKimiConfigShape): Promise<ManagedKimiConfigShape>;
resolveOAuthToken(providerName: string, oauthRef?: ManagedKimiOAuthRef): Promise<string>;
/**
* Product User-Agent sent on custom-registry (api.json) fetches, e.g.
* `kimi-code-cli/1.2.3`. When omitted the fetch falls back to the runtime
* default (`User-Agent: node`).
*/
readonly userAgent?: string;
}
export interface ProviderChange {
readonly providerId: string;
/** User-facing name when available. */
readonly providerName: string;
readonly added: number;
readonly removed: number;
}
export interface RefreshResult {
/** Providers whose model list actually changed. */
readonly changed: readonly ProviderChange[];
/** Providers whose model list stayed identical after refresh. */
readonly unchanged: readonly string[];
readonly failed: ReadonlyArray<{ readonly provider: string; readonly reason: string }>;
}
export type RefreshProviderScope = 'all' | 'oauth';
export interface RefreshProviderOptions {
readonly scope?: RefreshProviderScope;
/**
* Refresh only this provider. When set, managed / open-platform branches
* skip every other provider; for a custom-registry provider the registry
* group it belongs to is fetched but only the target entry is applied.
*/
readonly providerId?: string;
}
interface ProviderView {
readonly type?: string;
readonly baseUrl?: string;
readonly apiKey?: string;
readonly oauth?: ManagedKimiOAuthRef;
readonly source?: unknown;
readonly env?: unknown;
}
/**
* Mirrors the runtime credential resolution for `type: 'kimi'` providers
* (`providerApiKey` in agent-core's provider-manager): the inline `apiKey`
* wins, with `env.KIMI_API_KEY` as the documented config-file fallback.
*/
function resolveProviderApiKey(provider: ProviderView): string | undefined {
if (typeof provider.apiKey === 'string' && provider.apiKey.length > 0) {
return provider.apiKey;
}
if (isRecord(provider.env)) {
const fromEnv = provider.env['KIMI_API_KEY'];
if (typeof fromEnv === 'string' && fromEnv.length > 0) return fromEnv;
}
return undefined;
}
function readProvider(
config: ManagedKimiConfigShape,
providerId: string,
): ProviderView | undefined {
const provider = config.providers[providerId];
if (provider === undefined) return undefined;
return provider as ProviderView;
}
function readModel(
config: ManagedKimiConfigShape,
alias: string,
): ManagedKimiModelAlias | undefined {
const model = config.models?.[alias];
if (model === undefined) return undefined;
return model as ManagedKimiModelAlias;
}
function readCustomRegistrySource(provider: ProviderView): CustomRegistrySource | undefined {
const source = provider.source;
if (typeof source !== 'object' || source === null) return undefined;
const candidate = source as Record<string, unknown>;
if (candidate['kind'] !== 'apiJson') return undefined;
const url = candidate['url'];
const apiKey = candidate['apiKey'];
if (typeof url !== 'string' || url.length === 0) return undefined;
if (typeof apiKey !== 'string') return undefined;
return { kind: 'apiJson', url, apiKey };
}
function customRegistrySourceKey(source: CustomRegistrySource): string {
return JSON.stringify([source.url]);
}
function customRegistrySourceCredentialKey(source: CustomRegistrySource): string {
return JSON.stringify([source.url, source.apiKey]);
}
async function fetchCustomRegistryFromSources(
sources: readonly CustomRegistrySource[],
userAgent?: string,
): Promise<{
readonly entries: Awaited<ReturnType<typeof fetchCustomRegistry>>;
readonly source: CustomRegistrySource;
}> {
let lastError: unknown;
for (const source of sources) {
try {
return {
entries: await fetchCustomRegistry(source, { userAgent }),
source,
};
} catch (error) {
lastError = error;
}
}
if (lastError instanceof Error) throw lastError;
if (typeof lastError === 'string') throw new Error(lastError);
throw new Error('No custom registry sources configured.');
}
function collectModelIdsForAliases(
config: ManagedKimiConfigShape,
aliasKeys: ReadonlySet<string>,
): Set<string> {
const ids = new Set<string>();
for (const aliasKey of aliasKeys) {
const alias = readModel(config, aliasKey);
if (alias !== undefined && alias.model.length > 0) {
ids.add(alias.model);
}
}
return ids;
}
function providerAliasKeys(config: ManagedKimiConfigShape, providerId: string): Set<string> {
const keys = new Set<string>();
for (const [alias, raw] of Object.entries(config.models ?? {})) {
if ((raw as ManagedKimiModelAlias).provider === providerId) keys.add(alias);
}
return keys;
}
function generatedProviderAliasKeys(
config: ManagedKimiConfigShape,
providerId: string,
aliasPrefix: string,
): Set<string> {
const keys = new Set<string>();
for (const [alias, raw] of Object.entries(config.models ?? {})) {
const model = raw as ManagedKimiModelAlias;
if (model.provider === providerId && alias.startsWith(aliasPrefix)) {
keys.add(alias);
}
}
return keys;
}
function computeChanges(oldIds: Set<string>, newIds: Set<string>): { added: number; removed: number } {
let added = 0;
for (const id of newIds) {
if (!oldIds.has(id)) added++;
}
let removed = 0;
for (const id of oldIds) {
if (!newIds.has(id)) removed++;
}
return { added, removed };
}
interface ProviderModelSnapshot {
readonly alias: string;
readonly model: ManagedKimiModelAlias;
}
// Compare the full model metadata for the relevant aliases, not just model IDs:
// a registry can change capabilities (e.g. enabling reasoning) without changing
// any model ID. Spreading the whole alias keeps this in sync with the schema
// automatically; only `capabilities` needs normalizing because its order is not
// meaningful. `defaultModel` joins the snapshot so a lost selection flips the
// provider to changed and the re-selected default is written back.
function providerModelSnapshot(
config: ManagedKimiConfigShape,
providerId: string,
aliasKeys: ReadonlySet<string>,
): string {
const snapshots: ProviderModelSnapshot[] = [];
for (const alias of aliasKeys) {
const model = readModel(config, alias);
if (model === undefined || model.provider !== providerId) continue;
snapshots.push({
alias,
model: {
...model,
capabilities: model.capabilities === undefined ? undefined : model.capabilities.toSorted(),
},
});
}
snapshots.sort((a, b) => a.alias.localeCompare(b.alias));
return JSON.stringify({ defaultModel: config.defaultModel ?? null, models: snapshots });
}
function providerModelsEqual(
config: ManagedKimiConfigShape,
nextConfig: ManagedKimiConfigShape,
providerId: string,
aliasKeys: ReadonlySet<string>,
): boolean {
return (
providerModelSnapshot(config, providerId, aliasKeys) ===
providerModelSnapshot(nextConfig, providerId, aliasKeys)
);
}
function providerConfigSnapshot(config: ManagedKimiConfigShape, providerId: string): string {
return JSON.stringify(config.providers[providerId] ?? null);
}
function providerConfigEqual(
config: ManagedKimiConfigShape,
nextConfig: ManagedKimiConfigShape,
providerId: string,
): boolean {
return providerConfigSnapshot(config, providerId) === providerConfigSnapshot(nextConfig, providerId);
}
function providerRefreshAliasKeys(
config: ManagedKimiConfigShape,
nextConfig: ManagedKimiConfigShape,
providerId: string,
aliasPrefix: string,
): Set<string> {
const keys = generatedProviderAliasKeys(config, providerId, aliasPrefix);
for (const key of providerAliasKeys(nextConfig, providerId)) keys.add(key);
return keys;
}
function preserveUserProviderAliases(
config: ManagedKimiConfigShape,
providerId: string,
refreshedAliasKeys: ReadonlySet<string>,
): Record<string, ManagedKimiModelAlias> {
const preserved: Record<string, ManagedKimiModelAlias> = {};
for (const [alias, raw] of Object.entries(config.models ?? {})) {
const model = raw as ManagedKimiModelAlias;
if (model.provider !== providerId || refreshedAliasKeys.has(alias)) continue;
preserved[alias] = structuredClone(model);
}
return preserved;
}
function restoreProviderAliases(
config: ManagedKimiConfigShape,
aliases: Record<string, ManagedKimiModelAlias>,
): void {
if (Object.keys(aliases).length === 0) return;
config.models = {
...config.models,
...aliases,
};
}
function restoreDefaultSelection(
config: ManagedKimiConfigShape,
defaultModel: string | undefined,
defaultEnabled: boolean | undefined,
): void {
if (defaultModel === undefined || readModel(config, defaultModel) === undefined) return;
config.defaultModel = defaultModel;
// A refresh may have just learned that the default model cannot disable
// thinking — never restore a stale thinking-off selection onto it.
const capabilities = readModel(config, defaultModel)?.capabilities ?? [];
const enabled = capabilities.includes('always_thinking') ? true : defaultEnabled;
if (enabled !== undefined) {
config.thinking = { ...config.thinking, enabled };
}
}
async function rebaseSelectionAfterFetch(
host: RefreshProviderHost,
config: ManagedKimiConfigShape,
): Promise<ManagedKimiConfigShape> {
const fresh = await host.getConfig();
return { ...config, defaultModel: fresh.defaultModel, thinking: fresh.thinking };
}
// `apply*` may leave `defaultModel` pointing at an alias that no longer exists
// (e.g. the previously-selected model was dropped from the registry). The host's
// `setConfig` deep-merge cannot clear a key, so the matching `removeProvider`
// call handles disk cleanup while this drops the dangling reference in memory.
function clampDanglingDefault(config: ManagedKimiConfigShape): void {
if (config.defaultModel !== undefined && readModel(config, config.defaultModel) === undefined) {
config.defaultModel = undefined;
config.thinking = undefined;
}
}
function clearDefaultThinkingWhenDefaultRemoved(
config: ManagedKimiConfigShape,
previousDefaultModel: string | undefined,
): void {
if (previousDefaultModel !== undefined && config.defaultModel === undefined) {
config.thinking = undefined;
}
}
function pickDefaultModel(
config: ManagedKimiConfigShape,
providerId: string,
models: Array<{ id: string }>,
): string {
const firstModel = models[0];
if (firstModel === undefined) return '';
const existingDefault = config.defaultModel;
if (existingDefault !== undefined) {
const alias = readModel(config, existingDefault);
if (alias !== undefined && alias.provider === providerId) {
const stillAvailable = models.find((m) => m.id === alias.model);
if (stillAvailable !== undefined) {
return stillAvailable.id;
}
}
}
return firstModel.id;
}
/**
* Refresh remote model metadata for the configured providers and persist any
* changes through the host. Handles four provider kinds, in order:
*
* 1. Managed Kimi Code (OAuth) — `GET /models` against the runtime endpoint.
* 2. Open platforms (moonshot-cn, moonshot-ai, …) — platform catalog fetch.
* 2.5. Managed-endpoint API-key providers — hand-written `type: 'kimi'`
* providers (including a hand-written `managed:kimi-code` without an oauth
* ref) whose baseUrl is exactly the managed Kimi Code endpoint; refreshed
* via `GET /models` with the configured API key as Bearer. Only model
* aliases are merged; the provider record is user-owned and never
* rewritten.
* 3. Custom registries (models.dev-style, keyed by `provider.source`).
*
* Each branch diffs old vs new and only writes when something actually changed
* (`removeProvider` then `setConfig`). Failures are collected per-provider and
* never abort the whole refresh. Pass `providerId` to scope the refresh to a
* single provider; pass `scope: 'oauth'` to refresh only the managed provider.
*/
export async function refreshProviderModels(
host: RefreshProviderHost,
options: RefreshProviderOptions = {},
): Promise<RefreshResult> {
const changed: ProviderChange[] = [];
const unchanged: string[] = [];
const failed: Array<{ provider: string; reason: string }> = [];
const scope = options.scope ?? 'all';
const targetId = options.providerId;
let config = await host.getConfig();
// ---------------------------------------------------------------------------
// 1. Managed Kimi Code (OAuth)
// ---------------------------------------------------------------------------
const managedProvider = readProvider(config, KIMI_CODE_PROVIDER_NAME);
const managedWanted = targetId === undefined || targetId === KIMI_CODE_PROVIDER_NAME;
if (
managedWanted &&
managedProvider !== undefined &&
managedProvider.type === 'kimi' &&
managedProvider.oauth !== undefined
) {
try {
const auth = resolveKimiCodeRuntimeAuth({
configuredBaseUrl: managedProvider.baseUrl,
configuredOAuthRef: managedProvider.oauth,
});
const accessToken = await host.resolveOAuthToken(KIMI_CODE_PROVIDER_NAME, auth.oauthRef);
const models = await fetchManagedKimiCodeModels({
accessToken,
baseUrl: auth.baseUrl,
});
if (models.length > 0) {
config = await rebaseSelectionAfterFetch(host, config);
const next = structuredClone(config);
applyManagedKimiCodeConfig(next, {
models,
baseUrl: auth.baseUrl,
oauthKey: auth.oauthRef.key,
oauthHost: auth.oauthRef.oauthHost,
preserveDefaultModel: true,
});
const refreshedAliasKeys = providerRefreshAliasKeys(
config,
next,
KIMI_CODE_PROVIDER_NAME,
`${KIMI_CODE_PLATFORM_ID}/`,
);
restoreProviderAliases(
next,
preserveUserProviderAliases(config, KIMI_CODE_PROVIDER_NAME, refreshedAliasKeys),
);
restoreDefaultSelection(next, config.defaultModel, config.thinking?.enabled);
clampDanglingDefault(next);
clearDefaultThinkingWhenDefaultRemoved(next, config.defaultModel);
if (providerModelsEqual(config, next, KIMI_CODE_PROVIDER_NAME, refreshedAliasKeys)) {
unchanged.push(KIMI_CODE_PROVIDER_NAME);
} else {
const { added, removed } = computeChanges(
collectModelIdsForAliases(config, refreshedAliasKeys),
collectModelIdsForAliases(next, refreshedAliasKeys),
);
await host.removeProvider(KIMI_CODE_PROVIDER_NAME);
config = await host.setConfig({
providers: next.providers,
models: next.models,
defaultModel: next.defaultModel,
thinking: next.thinking,
});
changed.push({
providerId: KIMI_CODE_PROVIDER_NAME,
providerName: 'Kimi Code',
added,
removed,
});
}
}
} catch (error) {
failed.push({
provider: KIMI_CODE_PROVIDER_NAME,
reason: error instanceof Error ? error.message : String(error),
});
}
}
// The oauth scope stops here, but a targeted refresh of the managed provider
// must fall through: branch 2 no-ops on a non-open-platform id, branch 2.5
// handles a hand-written `managed:kimi-code` that carries an API key instead
// of an oauth ref, and branch 3 no-ops when no registry group contains it.
if (scope === 'oauth') {
return { changed, unchanged, failed };
}
// ---------------------------------------------------------------------------
// 2. Open Platforms (moonshot-cn, moonshot-ai, …)
// ---------------------------------------------------------------------------
const openPlatformIds = Object.keys(config.providers).filter((id) => isOpenPlatformId(id));
for (const providerId of openPlatformIds) {
if (targetId !== undefined && targetId !== providerId) continue;
const platform = getOpenPlatformById(providerId);
if (platform === undefined) continue;
const providerConfig = readProvider(config, providerId);
if (providerConfig === undefined) continue;
const apiKey = providerConfig.apiKey;
if (typeof apiKey !== 'string' || apiKey.length === 0) continue;
try {
let models = await fetchOpenPlatformModels(platform, apiKey);
models = filterModelsByPrefix(models, platform);
if (models.length === 0) continue;
config = await rebaseSelectionAfterFetch(host, config);
const selectedModelId = pickDefaultModel(config, providerId, models);
const selectedModel = models.find((m) => m.id === selectedModelId);
if (selectedModel === undefined) continue;
const next = structuredClone(config);
applyOpenPlatformConfig(next, {
platform,
models,
selectedModel,
thinking: false,
apiKey,
});
const refreshedAliasKeys = providerRefreshAliasKeys(
config,
next,
providerId,
`${providerId}/`,
);
restoreProviderAliases(next, preserveUserProviderAliases(config, providerId, refreshedAliasKeys));
restoreDefaultSelection(next, config.defaultModel, config.thinking?.enabled);
clampDanglingDefault(next);
clearDefaultThinkingWhenDefaultRemoved(next, config.defaultModel);
if (providerModelsEqual(config, next, providerId, refreshedAliasKeys)) {
unchanged.push(providerId);
} else {
const { added, removed } = computeChanges(
collectModelIdsForAliases(config, refreshedAliasKeys),
collectModelIdsForAliases(next, refreshedAliasKeys),
);
await host.removeProvider(providerId);
config = await host.setConfig({
providers: next.providers,
models: next.models,
defaultModel: next.defaultModel,
thinking: next.thinking,
});
changed.push({
providerId,
providerName: platform.name,
added,
removed,
});
}
} catch (error) {
failed.push({
provider: providerId,
reason: error instanceof Error ? error.message : String(error),
});
}
}
// ---------------------------------------------------------------------------
// 2.5. Managed-endpoint API-key providers (hand-configured distributed keys)
// ---------------------------------------------------------------------------
// A hand-written `type: 'kimi'` provider whose baseUrl is exactly the managed
// Kimi Code endpoint, carrying an API key (inline or via `env.KIMI_API_KEY`)
// instead of an oauth ref, gets its model list refreshed from
// `{baseUrl}/models` just like the OAuth branch. Strict baseUrl matching
// keeps proxies / gateways with an untrusted `/models` schema out.
for (const providerId of Object.keys(config.providers)) {
if (isOpenPlatformId(providerId)) continue;
if (targetId !== undefined && targetId !== providerId) continue;
const provider = readProvider(config, providerId);
if (provider === undefined) continue;
if (provider.type !== 'kimi') continue;
if (provider.oauth !== undefined) continue;
if (readCustomRegistrySource(provider) !== undefined) continue;
if (!isManagedKimiCodeBaseUrl(provider.baseUrl)) continue;
const apiKey = resolveProviderApiKey(provider);
if (apiKey === undefined) continue;
try {
const models = await fetchManagedKimiCodeModels({
accessToken: apiKey,
baseUrl: provider.baseUrl,
credentialKind: 'apiKey',
});
if (models.length === 0) continue;
config = await rebaseSelectionAfterFetch(host, config);
// A hand-written `managed:kimi-code` shares the OAuth branch's
// `kimi-code/` alias prefix so the two shapes merge cleanly if the user
// later logs in via OAuth; ordinary providers use their own id.
const aliasPrefix =
providerId === KIMI_CODE_PROVIDER_NAME ? `${KIMI_CODE_PLATFORM_ID}/` : `${providerId}/`;
const next = structuredClone(config);
applyManagedApiKeyProviderModels(next, providerId, models, aliasPrefix);
const refreshedAliasKeys = providerRefreshAliasKeys(config, next, providerId, aliasPrefix);
restoreProviderAliases(
next,
preserveUserProviderAliases(config, providerId, refreshedAliasKeys),
);
restoreDefaultSelection(next, config.defaultModel, config.thinking?.enabled);
clampDanglingDefault(next);
clearDefaultThinkingWhenDefaultRemoved(next, config.defaultModel);
if (providerModelsEqual(config, next, providerId, refreshedAliasKeys)) {
unchanged.push(providerId);
} else {
const { added, removed } = computeChanges(
collectModelIdsForAliases(config, refreshedAliasKeys),
collectModelIdsForAliases(next, refreshedAliasKeys),
);
await host.removeProvider(providerId);
config = await host.setConfig({
providers: next.providers,
models: next.models,
defaultModel: next.defaultModel,
thinking: next.thinking,
// The v1 `removeProvider` RPC clears `defaultProvider` when it points
// at this provider; the clone still holds the original value, so
// write it back — a refresh must not silently drop the fallback.
defaultProvider: next['defaultProvider'],
});
changed.push({
providerId,
providerName: providerId,
added,
removed,
});
}
} catch (error) {
failed.push({
provider: providerId,
reason: error instanceof Error ? error.message : String(error),
});
}
}
// ---------------------------------------------------------------------------
// 3. Custom Registry providers (grouped by URL, with API-key candidates)
// ---------------------------------------------------------------------------
const customSources = new Map<
string,
{
readonly sources: CustomRegistrySource[];
readonly sourceKeys: Set<string>;
readonly providerIds: string[];
}
>();
for (const providerId of Object.keys(config.providers)) {
if (providerId === KIMI_CODE_PROVIDER_NAME) continue;
if (isOpenPlatformId(providerId)) continue;
const provider = readProvider(config, providerId);
if (provider === undefined) continue;
const source = readCustomRegistrySource(provider);
if (source === undefined) continue;
const key = customRegistrySourceKey(source);
const sourceKey = customRegistrySourceCredentialKey(source);
const entry = customSources.get(key);
if (entry !== undefined) {
if (!entry.sourceKeys.has(sourceKey)) {
entry.sources.push(source);
entry.sourceKeys.add(sourceKey);
}
entry.providerIds.push(providerId);
} else {
customSources.set(key, {
sources: [source],
sourceKeys: new Set([sourceKey]),
providerIds: [providerId],
});
}
}
for (const { sources, providerIds } of customSources.values()) {
// When scoped to a single provider, only refresh the registry group it
// belongs to and only apply the target entry (siblings under the same URL
// are left untouched).
if (targetId !== undefined && !providerIds.includes(targetId)) continue;
try {
const { entries, source } = await fetchCustomRegistryFromSources(sources, host.userAgent);
config = await rebaseSelectionAfterFetch(host, config);
// Build the whole batch on one clone so that several changed providers
// from the same source do not overwrite each other's aliases, and so the
// config we compare is exactly the config we persist.
const next = structuredClone(config);
const changedProviders: Array<{
readonly providerId: string;
readonly providerName: string;
readonly added: number;
readonly removed: number;
}> = [];
const providersToRemoveBeforeSet = new Set<string>();
let hasUnreportedConfigChange = false;
const remoteEntries = Object.values(entries);
const remoteEntriesByProviderId = new Map(
remoteEntries.map((entry) => [entry.id, entry]),
);
const providerIdsToSync = new Set(providerIds);
// Only pull in newly-appeared providers from the registry when running an
// unscoped refresh; a scoped refresh must not add siblings.
if (targetId === undefined) {
for (const entry of remoteEntries) providerIdsToSync.add(entry.id);
}
for (const providerId of providerIdsToSync) {
if (targetId !== undefined && providerId !== targetId) continue;
const entry = remoteEntriesByProviderId.get(providerId);
if (entry === undefined) {
const oldIds = collectModelIdsForAliases(config, providerAliasKeys(config, providerId));
removeCustomRegistryProvider(next, providerId);
changedProviders.push({
providerId,
providerName: providerId,
added: 0,
removed: oldIds.size,
});
providersToRemoveBeforeSet.add(providerId);
continue;
}
const existed = config.providers[providerId] !== undefined;
applyCustomRegistryProvider(next, entry, source);
const refreshedAliasKeys = providerRefreshAliasKeys(config, next, providerId, `${providerId}/`);
if (existed) {
restoreProviderAliases(next, preserveUserProviderAliases(config, providerId, refreshedAliasKeys));
}
if (
existed &&
providerModelsEqual(config, next, providerId, refreshedAliasKeys) &&
providerConfigEqual(config, next, providerId)
) {
unchanged.push(providerId);
} else if (existed && providerModelsEqual(config, next, providerId, refreshedAliasKeys)) {
unchanged.push(providerId);
providersToRemoveBeforeSet.add(providerId);
hasUnreportedConfigChange = true;
} else {
const { added, removed } = computeChanges(
collectModelIdsForAliases(config, refreshedAliasKeys),
collectModelIdsForAliases(next, refreshedAliasKeys),
);
changedProviders.push({
providerId,
providerName: entry.name || providerId,
added,
removed,
});
if (existed) providersToRemoveBeforeSet.add(providerId);
}
}
if (changedProviders.length > 0 || hasUnreportedConfigChange) {
restoreDefaultSelection(next, config.defaultModel, config.thinking?.enabled);
clampDanglingDefault(next);
clearDefaultThinkingWhenDefaultRemoved(next, config.defaultModel);
for (const providerId of providersToRemoveBeforeSet) {
await host.removeProvider(providerId);
}
config = await host.setConfig({
providers: next.providers,
models: next.models,
defaultModel: next.defaultModel,
thinking: next.thinking,
});
for (const change of changedProviders) {
changed.push({
providerId: change.providerId,
providerName: change.providerName,
added: change.added,
removed: change.removed,
});
}
}
} catch (error) {
const reportedIds = targetId !== undefined ? [targetId] : providerIds;
for (const providerId of reportedIds) {
failed.push({
provider: providerId,
reason: error instanceof Error ? error.message : String(error),
});
}
}
}
return { changed, unchanged, failed };
}