Skip to content

Commit 062c93b

Browse files
committed
feat: read Copilot bridge port from ~/.jam/copilot-bridge.json as env var fallback
1 parent e47b828 commit 062c93b

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/config/loader.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { cosmiconfig } from 'cosmiconfig';
22
import { homedir } from 'node:os';
33
import { join } from 'node:path';
4-
import { existsSync } from 'node:fs';
4+
import { existsSync, readFileSync } from 'node:fs';
55
import { JamConfigSchema } from './schema.js';
66
import { CONFIG_DEFAULTS } from './defaults.js';
77
import type { JamConfig, CliOverrides, Profile } from './schema.js';
@@ -139,9 +139,34 @@ async function isCopilotCliInstalled(): Promise<boolean> {
139139
* Detect the best available provider when none is explicitly configured.
140140
* Priority: VSCode Copilot proxy > Copilot CLI > Anthropic > OpenAI > null (keep ollama default)
141141
*/
142-
async function detectBestProvider(): Promise<{ provider: string; model?: string } | null> {
143-
// 1. VSCode Copilot proxy (JAM_VSCODE_LM_PORT)
142+
/**
143+
* Read the Copilot bridge port from ~/.jam/copilot-bridge.json.
144+
* The VSCode extension writes this file when the proxy starts.
145+
* Falls back to JAM_VSCODE_LM_PORT env var.
146+
*/
147+
function readBridgePort(): string | null {
144148
if (process.env['JAM_VSCODE_LM_PORT']) {
149+
return process.env['JAM_VSCODE_LM_PORT'];
150+
}
151+
try {
152+
const bridgePath = join(homedir(), '.jam', 'copilot-bridge.json');
153+
const data = JSON.parse(readFileSync(bridgePath, 'utf-8')) as { port?: number; pid?: number };
154+
if (data.port) {
155+
// Verify the VSCode process is still running
156+
if (data.pid) {
157+
try { process.kill(data.pid, 0); } catch { return null; } // process gone
158+
}
159+
return String(data.port);
160+
}
161+
} catch { /* file doesn't exist or invalid */ }
162+
return null;
163+
}
164+
165+
async function detectBestProvider(): Promise<{ provider: string; model?: string } | null> {
166+
// 1. VSCode Copilot proxy (env var or bridge file)
167+
const bridgePort = readBridgePort();
168+
if (bridgePort) {
169+
process.env['JAM_VSCODE_LM_PORT'] = bridgePort; // propagate for downstream use
145170
return { provider: 'copilot' };
146171
}
147172

0 commit comments

Comments
 (0)