From 8d32a06b5c2b2ab3d10677f0edc708fd5d385a15 Mon Sep 17 00:00:00 2001 From: Jiang Bohan Date: Mon, 9 Feb 2026 13:51:45 +0800 Subject: [PATCH] fix(agent): validate API key before calling PiAgentCore.prompt() getApiKey errors thrown inside PiAgentCore's internal async context result in UnhandledPromiseRejection instead of propagating to the caller. Return a graceful error early so AsyncAgent can emit it through the subscriber mechanism to the UI. Co-Authored-By: Claude Opus 4.6 --- src/agent/runner.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/agent/runner.ts b/src/agent/runner.ts index c6fa4566..7d63bfe5 100644 --- a/src/agent/runner.ts +++ b/src/agent/runner.ts @@ -364,6 +364,14 @@ export class Agent { await this.ensureInitialized(); this.output.state.lastAssistantText = ""; + // Early validation: check API key before calling PiAgentCore.prompt(), + // because getApiKey errors thrown inside PiAgentCore's internal async + // context result in UnhandledPromiseRejection instead of propagating. + if (!this.currentApiKey) { + const errorMsg = `No API key configured for provider: ${this.resolvedProvider}. Please configure a provider in Agent Settings.`; + return { text: "", error: errorMsg }; + } + const canRotate = !this.pinnedProfile && this.profileCandidates.length > 1; let lastError: unknown;