From d7ccbf066e2f0a61bbd6ed4019bc3b15993c32f7 Mon Sep 17 00:00:00 2001 From: Jiayuan Zhang Date: Tue, 10 Feb 2026 23:02:39 +0800 Subject: [PATCH] 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 --- packages/core/src/agent/async-agent.ts | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/core/src/agent/async-agent.ts b/packages/core/src/agent/async-agent.ts index 80cd45c6..30e1f94d 100644 --- a/packages/core/src/agent/async-agent.ts +++ b/packages/core/src/agent/async-agent.ts @@ -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 }); + }); + }); + } }