feat(core): add testProvider method to AsyncAgent

Queued through the serialization queue to safely switch provider,
send a minimal test prompt, and restore the previous provider.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jiayuan Zhang 2026-02-10 23:02:39 +08:00
parent 9290fd1212
commit d7ccbf066e

View file

@ -368,4 +368,37 @@ export class AsyncAgent {
setProvider(providerId: string, modelId?: string): { provider: string; model: string | undefined } {
return this.agent.setProvider(providerId, modelId);
}
/**
* Test a provider connection by temporarily switching, sending a minimal prompt,
* and restoring the previous provider. Queued through the serialization queue.
*/
async testProvider(providerId: string, modelId?: string): Promise<{ ok: boolean; error?: string }> {
return new Promise((resolve) => {
this.queue = this.queue
.then(async () => {
const prev = this.agent.getProviderInfo();
try {
this.agent.setProvider(providerId, modelId);
const result = await this.agent.runInternal('Reply with just the word "OK". No other text.');
if (result.error) {
resolve({ ok: false, error: result.error });
} else {
resolve({ ok: true });
}
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
resolve({ ok: false, error: message });
} finally {
try {
this.agent.setProvider(prev.provider, prev.model);
} catch { /* best effort */ }
}
})
.catch((err) => {
const message = err instanceof Error ? err.message : String(err);
resolve({ ok: false, error: message });
});
});
}
}