Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/cli/src/ui/hooks/useProviderUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {
ALL_PROVIDERS,
applyProviderInstallPlan,
buildInstallPlan,
buildProviderTemplate,
computeModelListVersion,
computeProviderTemplateVersion,
getDefaultModelIds,
PROVIDER_METADATA_NS,
providerMatchesCredentials,
Expand Down Expand Up @@ -205,8 +204,7 @@ function findAllPendingUpdates(
if (!metadata.version) continue;

const baseUrl = metadata.baseUrl || resolveBaseUrl(provider);
const currentTemplate = buildProviderTemplate(provider, baseUrl);
const currentVersion = computeModelListVersion(currentTemplate);
const currentVersion = computeProviderTemplateVersion(provider, baseUrl);

if (metadata.version === currentVersion) continue;
if (metadata.ignoredVersion === currentVersion) continue;
Expand Down
90 changes: 90 additions & 0 deletions packages/core/src/providers/__tests__/provider-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ import {
findExistingProviderModels,
findProviderByCredentials,
getDefaultModelIds,
PROVIDER_METADATA_NS,
resolveBaseUrl,
shouldShowStep,
providerMatchesCredentials,
type ProviderConfig,
} from '@qwen-code/qwen-code-core';
// Imported from source: the invariant below must be checked against the
// current implementation, not a previously built `dist`.
import {
buildInstallPlan as buildInstallPlanSrc,
computeProviderTemplateVersion,
resolveMetadataKey,
} from '../provider-config.js';
import { ALL_PROVIDERS as ALL_PROVIDERS_SRC } from '../all-providers.js';
import {
TOKEN_PLAN_CHINA_BASE_URL,
TOKEN_PLAN_ENV_KEY,
Expand Down Expand Up @@ -916,3 +925,84 @@ describe('resolveMetadataKey dotted-id guard', () => {
expect(() => resolveMetadataKeySrc(config)).toThrow(/must not contain/);
});
});

describe('stored provider version matches the launch-time check', () => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
// The prompt in useProviderUpdates clears only when the version recorded by
// an install/update equals the one recomputed at launch from the built-in
// template. Assert that invariant for every built-in provider, with a custom
// model installed — the case that used to make the two diverge forever.
const providersWithMetadata = ALL_PROVIDERS_SRC.filter((provider) =>
resolveMetadataKey(provider),
);

it('covers every built-in provider that records metadata', () => {
expect(providersWithMetadata.length).toBeGreaterThan(0);
});

for (const provider of providersWithMetadata) {
const metadataKey = resolveMetadataKey(provider)!;

it(`holds for ${metadataKey} when a custom model is installed`, () => {
const baseUrl = resolveBaseUrl(provider);
const defaultIds = getDefaultModelIds(provider);
const plan = buildInstallPlanSrc(provider, {
baseUrl,
apiKey: 'sk-test',
// A user-added id plus a built-in that upstream renamed away: both
// reach the plan as "custom" ids and must not affect the version.
modelIds: [...defaultIds, 'user-added-model', 'renamed-away-builtin'],
});

const stored =
plan.providerState?.[`${PROVIDER_METADATA_NS}.${metadataKey}`];
const launchVersion = computeProviderTemplateVersion(provider, baseUrl);

expect(stored?.['version']).toBe(launchVersion);
// The plan still carries the custom ids — only the version ignores them.
expect(plan.modelProviders?.[0]?.models.map((m) => m.id)).toEqual(
expect.arrayContaining(['user-added-model', 'renamed-away-builtin']),
);
});
}
});

describe('computeProviderTemplateVersion', () => {
it('equals hashing the built-in template by hand', () => {
const config = makeConfig();
const baseUrl = resolveBaseUrl(config);
expect(computeProviderTemplateVersion(config, baseUrl)).toBe(
computeModelListVersion(buildProviderTemplate(config, baseUrl)),
);
});

it('ignores installed custom models', () => {
const config = makeConfig();
const baseUrl = resolveBaseUrl(config);
const withCustom = buildInstallPlanSrc(config, {
baseUrl,
apiKey: 'sk-test',
modelIds: [...getDefaultModelIds(config), 'extra-model'],
});
const withoutCustom = buildInstallPlanSrc(config, {
baseUrl,
apiKey: 'sk-test',
modelIds: getDefaultModelIds(config),
});
const key = `${PROVIDER_METADATA_NS}.${resolveMetadataKey(config)}`;
expect(withCustom.providerState?.[key]?.['version']).toBe(
withoutCustom.providerState?.[key]?.['version'],
);
});

it('changes when the built-in template changes', () => {
const baseUrl = 'https://api.test.com/v1';
const before = computeProviderTemplateVersion(makeConfig(), baseUrl);
const after = computeProviderTemplateVersion(
makeConfig({
models: [{ id: 'model-a-renamed', contextWindowSize: 8192 }],
}),
baseUrl,
);
expect(after).not.toBe(before);
});
});
1 change: 1 addition & 0 deletions packages/core/src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
buildInstallPlan,
buildProviderTemplate,
computeModelListVersion,
computeProviderTemplateVersion,
findExistingProviderModels,
getDefaultBaseUrlForProtocol,
getDefaultModelIds,
Expand Down
21 changes: 18 additions & 3 deletions packages/core/src/providers/provider-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ export const PROVIDER_METADATA_NS = 'providerMetadata';
function resolveProviderState(
config: ProviderConfig,
baseUrl: string,
models: ProviderModelConfig[],
): ProviderInstallState | undefined {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
const key = resolveMetadataKey(config);
if (key) {
return {
[`${PROVIDER_METADATA_NS}.${key}`]: {
version: computeModelListVersion(models),
version: computeProviderTemplateVersion(config, baseUrl),
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
baseUrl,
},
};
Expand Down Expand Up @@ -287,7 +286,7 @@ export function buildInstallPlan(
...(ownsModel ? { ownsModel } : {}),
},
],
providerState: resolveProviderState(config, inputs.baseUrl, models),
providerState: resolveProviderState(config, inputs.baseUrl),
};
}

Expand All @@ -299,6 +298,22 @@ export function computeModelListVersion(models: ProviderModelConfig[]): string {
return createHash('sha256').update(JSON.stringify(models)).digest('hex');
}

/**
* Version of a provider's built-in template (default models only).
*
* The version recorded at install time and the one recomputed at launch to
* detect a pending update must hash the same input, or the update prompt can
* never clear. Both sides call this instead of composing
* `buildProviderTemplate` + `computeModelListVersion` by hand, so the two
* cannot drift apart again.
*/
export function computeProviderTemplateVersion(
config: ProviderConfig,
baseUrl: string,
): string {
return computeModelListVersion(buildProviderTemplate(config, baseUrl));
}

/**
* Default base URLs per protocol, used as placeholder/fallback when the user
* doesn't supply one for a custom provider. Kept in core so the CLI flow
Expand Down
Loading