fix(opencode): keep model switching runtime-hot

This commit is contained in:
2026-08-21 01:26:20 +08:00
parent 605fef417f
commit cce772299a
10 changed files with 256 additions and 88 deletions

View File

@@ -511,6 +511,7 @@ function expectedUserModelProxyConfig(
async function rebindLocalProxyHostApiTokenIfNeeded(
expectedBaseUrl: string,
markPending: () => void,
runtimeAlreadyUsesCurrentHostApiToken: boolean,
signal?: AbortSignal,
): Promise<boolean> {
const providerService = getProviderService();
@@ -532,14 +533,31 @@ async function rebindLocalProxyHostApiTokenIfNeeded(
return false;
}
markPending();
await providerService.updateAccount(
NIANCODE_USER_MODEL_ACCOUNT_ID,
account,
currentHostApiToken,
);
signal?.throwIfAborted();
return true;
if (!runtimeAlreadyUsesCurrentHostApiToken) {
markPending();
}
const markPendingOnAbort = runtimeAlreadyUsesCurrentHostApiToken
? () => markPending()
: undefined;
if (markPendingOnAbort) {
signal?.addEventListener('abort', markPendingOnAbort, { once: true });
}
try {
await providerService.updateAccount(
NIANCODE_USER_MODEL_ACCOUNT_ID,
account,
currentHostApiToken,
);
signal?.throwIfAborted();
} catch (error) {
markPending();
throw error;
} finally {
if (markPendingOnAbort) {
signal?.removeEventListener('abort', markPendingOnAbort);
}
}
return !runtimeAlreadyUsesCurrentHostApiToken;
}
async function runtimeUsesExpectedUserModelProxy(
@@ -607,7 +625,12 @@ async function ensureRuntimeUserModelProxyConfig(
try {
if (
expectedConfig.usesLocalProxy
&& await rebindLocalProxyHostApiTokenIfNeeded(expectedConfig.baseUrl, markPending, signal)
&& await rebindLocalProxyHostApiTokenIfNeeded(
expectedConfig.baseUrl,
markPending,
ctx.opencodeManager.getRuntimeGenerationProvenance() === 'fresh',
signal,
)
) {
if (!allowRestart) {
markPending();

View File

@@ -361,10 +361,13 @@ export async function importCurrentUserModelConfig(
createdAt: existing?.createdAt ?? now,
updatedAt: now,
};
const shouldRestartRuntime = importedProviderRuntimeShapeChanged(existing, account)
|| await importedProviderApiKeyChanged(providerService, existing, accountApiKey);
signal.throwIfAborted();
const runtimeIsActive = ctx.opencodeManager.getStatus().state !== 'stopped';
const runtimeAlreadyUsesLocalProxyApiKey = useLocalAiProxy
&& ctx.opencodeManager.getRuntimeGenerationProvenance?.() === 'fresh';
const shouldRestartRuntime = importedProviderRuntimeShapeChanged(existing, account)
|| (!runtimeAlreadyUsesLocalProxyApiKey
&& await importedProviderApiKeyChanged(providerService, existing, accountApiKey));
signal.throwIfAborted();
const refreshAlreadyPending = lease.isRefreshPending();
const runtimeRefreshRequired = runtimeIsActive
&& (shouldRestartRuntime || refreshAlreadyPending);