import { app } from 'electron'; import { registerProvider, type AIProvider } from './provider'; // Dynamic import type — @github/copilot-sdk is ESM-only type CopilotClientType = import('@github/copilot-sdk').CopilotClient; let client: CopilotClientType | null = null; let isReady = false; const copilotProvider: AIProvider = { name: 'copilot', displayName: 'GitHub Copilot', usesExternalAuth: true, async initialize(): Promise { try { // Stop existing client if re-initializing if (client) { // eslint-disable-next-line @typescript-eslint/no-empty-function await client.stop().catch(() => {}); client = null; } const { CopilotClient } = await import('@github/copilot-sdk'); // No githubToken — uses stored OAuth credentials from Copilot CLI // (authenticate first with `copilot auth login`) client = new CopilotClient({ autoStart: true, autoRestart: true, logLevel: 'warning', }); await client.start(); isReady = true; console.log('[AI] CopilotClient started (using CLI OAuth credentials)'); return true; } catch (err) { console.error('[AI] Failed to start CopilotClient:', err); client = null; isReady = false; return false; } }, isReady(): boolean { return isReady && client !== null; }, }; /** Get the CopilotClient instance (null if not initialized). */ export function getCopilotClient(): CopilotClientType | null { return client; } // Clean shutdown on app quit app.on('before-quit', () => { if (client) { client.stop().catch((err: unknown) => console.error('[AI] Error stopping CopilotClient:', err)); } }); registerProvider(copilotProvider);