diff options
| author | christine betts <[email protected]> | 2025-08-14 18:09:19 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-14 18:09:19 +0000 |
| commit | af93a10a92423184b30c8ef0f5a8e9e70abff167 (patch) | |
| tree | 2b94b9d8ac40ba026b9d2c40685988f621f575dd /packages/core/src/ide/ide-client.ts | |
| parent | ec7b84191f539c1a3f890fc994dd62b68d3232fd (diff) | |
[ide-mode] Write port to file in ide-server (#5811)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Diffstat (limited to 'packages/core/src/ide/ide-client.ts')
| -rw-r--r-- | packages/core/src/ide/ide-client.ts | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/packages/core/src/ide/ide-client.ts b/packages/core/src/ide/ide-client.ts index fe605eb2..94107f21 100644 --- a/packages/core/src/ide/ide-client.ts +++ b/packages/core/src/ide/ide-client.ts @@ -5,7 +5,6 @@ */ import * as fs from 'node:fs'; -import * as path from 'node:path'; import { detectIde, DetectedIde, getIdeInfo } from '../ide/detect-ide.js'; import { ideContext, @@ -15,8 +14,11 @@ import { CloseDiffResponseSchema, DiffUpdateResult, } from '../ide/ideContext.js'; +import { getIdeProcessId } from './process-utils.js'; import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; +import * as os from 'node:os'; +import * as path from 'node:path'; const logger = { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -95,12 +97,27 @@ export class IdeClient { return; } - const port = this.getPortFromEnv(); - if (!port) { - return; + const portFromFile = await this.getPortFromFile(); + if (portFromFile) { + const connected = await this.establishConnection(portFromFile); + if (connected) { + return; + } } - await this.establishConnection(port); + const portFromEnv = this.getPortFromEnv(); + if (portFromEnv) { + const connected = await this.establishConnection(portFromEnv); + if (connected) { + return; + } + } + + this.setState( + IDEConnectionStatus.Disconnected, + `Failed to connect to IDE companion extension for ${this.currentIdeDisplayName}. Please ensure the extension is running and try restarting your terminal. To install the extension, run /ide install.`, + true, + ); } /** @@ -264,16 +281,26 @@ export class IdeClient { private getPortFromEnv(): string | undefined { const port = process.env['GEMINI_CLI_IDE_SERVER_PORT']; if (!port) { - this.setState( - IDEConnectionStatus.Disconnected, - `Failed to connect to IDE companion extension for ${this.currentIdeDisplayName}. Please ensure the extension is running and try restarting your terminal. To install the extension, run /ide install.`, - true, - ); return undefined; } return port; } + private async getPortFromFile(): Promise<string | undefined> { + try { + const ideProcessId = await getIdeProcessId(); + const portFile = path.join( + os.tmpdir(), + `gemini-ide-server-${ideProcessId}.json`, + ); + const portFileContents = await fs.promises.readFile(portFile, 'utf8'); + const port = JSON.parse(portFileContents).port; + return port.toString(); + } catch (_) { + return undefined; + } + } + private registerClientHandlers() { if (!this.client) { return; @@ -328,7 +355,7 @@ export class IdeClient { ); } - private async establishConnection(port: string) { + private async establishConnection(port: string): Promise<boolean> { let transport: StreamableHTTPClientTransport | undefined; try { this.client = new Client({ @@ -342,12 +369,8 @@ export class IdeClient { await this.client.connect(transport); this.registerClientHandlers(); this.setState(IDEConnectionStatus.Connected); + return true; } catch (_error) { - this.setState( - IDEConnectionStatus.Disconnected, - `Failed to connect to IDE companion extension for ${this.currentIdeDisplayName}. Please ensure the extension is running and try restarting your terminal. To install the extension, run /ide install.`, - true, - ); if (transport) { try { await transport.close(); @@ -355,6 +378,7 @@ export class IdeClient { logger.debug('Failed to close transport:', closeError); } } + return false; } } } |
