diff --git a/front_end/panels/ai_chat/core/AgentService.ts b/front_end/panels/ai_chat/core/AgentService.ts index 8dd765f4524..eb141ee6ba6 100644 --- a/front_end/panels/ai_chat/core/AgentService.ts +++ b/front_end/panels/ai_chat/core/AgentService.ts @@ -274,6 +274,17 @@ export class AgentService extends Common.ObjectWrapper.ObjectWrapper<{ return this.#isInitialized; } + /** + * Resets the initialization state to allow re-initialization with new configuration. + * This is useful when configuration overrides are set (e.g., API keys from request payload). + */ + resetInitialization(): void { + this.#isInitialized = false; + this.#graph = undefined; + this.#apiKey = null; + logger.info('AgentService initialization state reset'); + } + /** * Gets the current state of the agent */ @@ -383,8 +394,8 @@ export class AgentService extends Common.ObjectWrapper.ObjectWrapper<{ // In AUTOMATED_MODE, ensure the graph is initialized even without API key if (BUILD_CONFIG.AUTOMATED_MODE && !this.#graph) { const config = this.#configManager.getConfiguration(); - // Initialize with empty API key in AUTOMATED_MODE - will be overridden by request - await this.initialize('', config.mainModel, config.miniModel || '', config.nanoModel || ''); + // Initialize with API key from config (includes overrides set by EvaluationAgent) + await this.initialize(config.apiKey || '', config.mainModel, config.miniModel || '', config.nanoModel || ''); } // In normal mode, check if graph needs reinitialization (e.g., after config change) diff --git a/front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.ts b/front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.ts index 992a44681ec..2170339a8e1 100644 --- a/front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.ts +++ b/front_end/panels/ai_chat/evaluation/remote/EvaluationAgent.ts @@ -395,7 +395,11 @@ export class EvaluationAgent { }); hasSetEarlyOverride = true; - logger.info('DEBUG: Early override set successfully', { + // Reset AgentService to force re-initialization with new configuration + const agentService = AgentService.getInstance(); + agentService.resetInitialization(); + + logger.info('DEBUG: Early override set and AgentService reset successfully', { evaluationId: params.evaluationId }); } else {