|
1 | 1 | import { cosmiconfig } from 'cosmiconfig'; |
2 | 2 | import { homedir } from 'node:os'; |
3 | 3 | import { join } from 'node:path'; |
4 | | -import { existsSync } from 'node:fs'; |
| 4 | +import { existsSync, readFileSync } from 'node:fs'; |
5 | 5 | import { JamConfigSchema } from './schema.js'; |
6 | 6 | import { CONFIG_DEFAULTS } from './defaults.js'; |
7 | 7 | import type { JamConfig, CliOverrides, Profile } from './schema.js'; |
@@ -139,9 +139,34 @@ async function isCopilotCliInstalled(): Promise<boolean> { |
139 | 139 | * Detect the best available provider when none is explicitly configured. |
140 | 140 | * Priority: VSCode Copilot proxy > Copilot CLI > Anthropic > OpenAI > null (keep ollama default) |
141 | 141 | */ |
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 { |
144 | 148 | 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 |
145 | 170 | return { provider: 'copilot' }; |
146 | 171 | } |
147 | 172 |
|
|
0 commit comments