From a8984a9b30d153219cf517a7dc77d65ea3ef0965 Mon Sep 17 00:00:00 2001 From: Kumbham Ajay Goud Date: Mon, 4 Aug 2025 03:33:01 +0530 Subject: Fix: Preserve conversation history when changing auth methods via /auth (#5216) Co-authored-by: Jacob Richman --- packages/core/src/config/config.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'packages/core/src/config/config.ts') diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index b2d5f387..e94e8421 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -47,6 +47,7 @@ import { ClearcutLogger } from '../telemetry/clearcut-logger/clearcut-logger.js' import { shouldAttemptBrowserLaunch } from '../utils/browser.js'; import { MCPOAuthConfig } from '../mcp/oauth-provider.js'; import { IdeClient } from '../ide/ide-client.js'; +import type { Content } from '@google/genai'; // Re-export OAuth config type export type { MCPOAuthConfig }; @@ -332,13 +333,30 @@ export class Config { } async refreshAuth(authMethod: AuthType) { - this.contentGeneratorConfig = createContentGeneratorConfig( + // Save the current conversation history before creating a new client + let existingHistory: Content[] = []; + if (this.geminiClient && this.geminiClient.isInitialized()) { + existingHistory = this.geminiClient.getHistory(); + } + + // Create new content generator config + const newContentGeneratorConfig = createContentGeneratorConfig( this, authMethod, ); - this.geminiClient = new GeminiClient(this); - await this.geminiClient.initialize(this.contentGeneratorConfig); + // Create and initialize new client in local variable first + const newGeminiClient = new GeminiClient(this); + await newGeminiClient.initialize(newContentGeneratorConfig); + + // Only assign to instance properties after successful initialization + this.contentGeneratorConfig = newContentGeneratorConfig; + this.geminiClient = newGeminiClient; + + // Restore the conversation history to the new client + if (existingHistory.length > 0) { + this.geminiClient.setHistory(existingHistory); + } // Reset the session flag since we're explicitly changing auth and using default model this.inFallbackMode = false; -- cgit v1.2.3