copilot-api-plus 1.4.9 → 1.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{account-manager-D4DftPxS.js → account-manager-DktL5osZ.js} +4 -2
- package/dist/account-manager-DktL5osZ.js.map +1 -0
- package/dist/error-BaXXuCDb.js +3 -0
- package/dist/get-user-Ct5NqLcM.js +3 -0
- package/dist/main.js +5 -5
- package/dist/{token-DYGcLmSO.js → token-B8FDrdsQ.js} +2 -2
- package/dist/{token-DYGcLmSO.js.map → token-B8FDrdsQ.js.map} +1 -1
- package/dist/token-DEcUuJp7.js +4 -0
- package/package.json +1 -1
- package/dist/account-manager-D4DftPxS.js.map +0 -1
- package/dist/error-rdTm4jb1.js +0 -3
- package/dist/get-user-p_Kr8XWd.js +0 -3
- package/dist/token-CsABqA-G.js +0 -4
|
@@ -189,10 +189,12 @@ async function forwardError(c, error) {
|
|
|
189
189
|
consola.warn(`Error occurred: ${rootCause(error)}`);
|
|
190
190
|
consola.debug("HTTP error:", errorJson);
|
|
191
191
|
}
|
|
192
|
+
const isCopilotSessionLimit = error.response.status === 429 && errorText.includes("user_global_rate_limited:pro_plus");
|
|
193
|
+
if (isCopilotSessionLimit) c.header("x-should-retry", "false");
|
|
192
194
|
return c.json({ error: {
|
|
193
195
|
message: errorText,
|
|
194
196
|
type: "error"
|
|
195
|
-
} }, error.response.status);
|
|
197
|
+
} }, isCopilotSessionLimit ? 403 : error.response.status);
|
|
196
198
|
}
|
|
197
199
|
const message = error.message || String(error);
|
|
198
200
|
const cause = error.cause;
|
|
@@ -929,4 +931,4 @@ const accountManager = new AccountManager();
|
|
|
929
931
|
|
|
930
932
|
//#endregion
|
|
931
933
|
export { GITHUB_APP_SCOPES, GITHUB_BASE_URL, GITHUB_CLIENT_ID, HTTPError, PATHS, accountManager, cacheModels, cacheVSCodeVersion, copilotBaseUrl, copilotHeaders, ensurePaths, findModel, forwardError, getAccountDispatcher, getCopilotToken, getCopilotUsage, getGitHubUser, initProxyFromEnv, isAccountProxied, isNullish, isProxyActive, notifyStreamEnd, notifyStreamStart, resetAccountConnections, resetConnections, rootCause, sleep, standardHeaders, state };
|
|
932
|
-
//# sourceMappingURL=account-manager-
|
|
934
|
+
//# sourceMappingURL=account-manager-DktL5osZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-manager-DktL5osZ.js","names":["headers: Record<string, string>","state: State","source: TokenSource","errorText: string","errorJson: unknown","ACCOUNTS_PATH","direct: Agent | undefined","keepaliveTimer: ReturnType<typeof setInterval> | undefined","proxyUrl: string | undefined","connectionRecycleTimer: ReturnType<typeof setInterval> | undefined","fetchOptions: RequestInit","lastError: unknown","response: Response | undefined","error: unknown","err: unknown","data: Array<PersistedAccount>","account: Account"],"sources":["../src/lib/api-config.ts","../src/lib/state.ts","../src/services/copilot/get-models.ts","../src/services/get-vscode-version.ts","../src/lib/utils.ts","../src/lib/error.ts","../src/lib/paths.ts","../src/lib/proxy.ts","../src/services/github/get-copilot-token.ts","../src/services/github/get-copilot-usage.ts","../src/services/github/get-user.ts","../src/lib/account-manager.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\"\n\nexport const standardHeaders = () => ({\n \"content-type\": \"application/json\",\n accept: \"application/json\",\n})\n\nconst COPILOT_VERSION = \"0.38.2\"\nconst EDITOR_PLUGIN_VERSION = `copilot-chat/${COPILOT_VERSION}`\nconst USER_AGENT = `GitHubCopilotChat/${COPILOT_VERSION}`\n\n// Updated to match latest Zed implementation - 2025-10-01 returns Claude models\nconst API_VERSION = \"2025-10-01\"\n\n/**\n * Common interface for anything that can supply Copilot/GitHub credentials.\n *\n * Both `State` and `Account` satisfy this interface, so all header/URL\n * helpers can accept either without an explicit overload.\n */\nexport interface TokenSource {\n copilotToken?: string\n copilotApiEndpoint?: string\n accountType: string\n githubToken?: string\n vsCodeVersion?: string\n machineId?: string\n sessionId?: string\n proxy?: string\n}\n\n// Re-export constants used by other modules for building headers manually\nexport { API_VERSION, EDITOR_PLUGIN_VERSION, USER_AGENT }\n\n// Use the API endpoint from token response if available, otherwise fall back to default\nexport const copilotBaseUrl = (source: TokenSource) => {\n if (source.copilotApiEndpoint) {\n return source.copilotApiEndpoint\n }\n return source.accountType === \"individual\" ?\n \"https://api.githubcopilot.com\"\n : `https://api.${source.accountType}.githubcopilot.com`\n}\nexport const copilotHeaders = (\n source: TokenSource,\n vision: boolean = false,\n) => {\n const headers: Record<string, string> = {\n Authorization: `Bearer ${source.copilotToken}`,\n \"content-type\": standardHeaders()[\"content-type\"],\n \"copilot-integration-id\": \"vscode-chat\",\n \"editor-version\": `vscode/${source.vsCodeVersion}`,\n \"editor-plugin-version\": EDITOR_PLUGIN_VERSION,\n \"user-agent\": USER_AGENT,\n \"openai-intent\": \"conversation-agent\",\n \"x-interaction-type\": \"conversation-agent\",\n \"x-agent-task-id\": randomUUID(),\n \"x-github-api-version\": API_VERSION,\n \"x-request-id\": randomUUID(),\n \"x-vscode-user-agent-library-version\": \"electron-fetch\",\n // Anti-correlation: per-account device identifiers\n ...(source.machineId && { \"vscode-machineid\": source.machineId }),\n ...(source.sessionId && { \"vscode-sessionid\": source.sessionId }),\n }\n\n if (vision) headers[\"copilot-vision-request\"] = \"true\"\n\n return headers\n}\n\nexport const GITHUB_API_BASE_URL = \"https://api.github.com\"\nexport const githubHeaders = (source: TokenSource) => ({\n ...standardHeaders(),\n authorization: `token ${source.githubToken}`,\n \"editor-version\": `vscode/${source.vsCodeVersion}`,\n \"editor-plugin-version\": EDITOR_PLUGIN_VERSION,\n \"user-agent\": USER_AGENT,\n \"x-github-api-version\": API_VERSION,\n \"x-vscode-user-agent-library-version\": \"electron-fetch\",\n})\n\nexport const GITHUB_BASE_URL = \"https://github.com\"\nexport const GITHUB_CLIENT_ID = \"Iv1.b507a08c87ecfe98\"\nexport const GITHUB_APP_SCOPES = [\"read:user\"].join(\" \")\n","import type { ModelsResponse } from \"~/services/copilot/get-models\"\n\nexport interface State {\n githubToken?: string\n copilotToken?: string\n copilotApiEndpoint?: string // API endpoint returned by token response\n\n accountType: string\n models?: ModelsResponse\n vsCodeVersion?: string\n\n manualApprove: boolean\n rateLimitWait: boolean\n showToken: boolean\n\n // Rate limiting configuration\n rateLimitSeconds?: number\n lastRequestTimestamp?: number\n\n // API key authentication\n apiKeys?: Array<string>\n\n // Multi-account mode\n multiAccountEnabled: boolean\n\n /**\n * When true, Anthropic /v1/messages requests are NEVER routed through the\n * native Copilot /v1/messages endpoint — the legacy translation layer\n * (Anthropic → OpenAI chat-completions → Anthropic) is used instead.\n * Default: false (native passthrough enabled by capability).\n */\n disableAnthropicPassthrough: boolean\n\n /**\n * When true, requests that don't specify a `thinking` field will get the\n * model's maximum thinking budget injected automatically (adaptive for\n * adaptive-thinking models, otherwise `enabled` with `max_thinking_budget`).\n *\n * Trade-off:\n * - Pre-2026-06-01 Copilot billing is per-request (token count doesn't\n * change cost), so leaving this on is \"free quality\".\n * - Post-2026-06-01 billing switches to per-token, at which point\n * auto-injection burns tokens. Users on token billing should turn this\n * off (or upgrade to a release that flips the default).\n *\n * Default: true (preserve existing v1.3.x quality-first behavior).\n */\n maxThinking: boolean\n\n // Selected models (from --claude-code setup)\n selectedModel?: string\n selectedSmallModel?: string\n}\n\nexport const state: State = {\n accountType: \"individual\",\n manualApprove: false,\n rateLimitWait: false,\n showToken: false,\n multiAccountEnabled: false,\n disableAnthropicPassthrough: false,\n maxThinking: true,\n}\n","import { accountManager } from \"~/lib/account-manager\"\nimport {\n copilotBaseUrl,\n copilotHeaders,\n type TokenSource,\n} from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\nexport const getModels = async () => {\n // In multi-account mode, use the active account's token\n let source: TokenSource = state\n if (state.multiAccountEnabled && accountManager.hasAccounts()) {\n const account = accountManager.getActiveAccount()\n if (account?.copilotToken) {\n source = {\n copilotToken: account.copilotToken,\n copilotApiEndpoint: account.copilotApiEndpoint,\n accountType: account.accountType,\n githubToken: account.githubToken,\n vsCodeVersion: state.vsCodeVersion,\n }\n }\n }\n\n const url = `${copilotBaseUrl(source)}/models`\n\n const response = await fetch(url, {\n headers: copilotHeaders(source),\n })\n\n if (!response.ok) throw new HTTPError(\"Failed to get models\", response)\n\n const data = (await response.json()) as ModelsResponse\n\n return data\n}\n\nexport interface ModelsResponse {\n data: Array<Model>\n object: string\n}\n\ninterface ModelLimits {\n max_context_window_tokens?: number\n max_output_tokens?: number\n max_prompt_tokens?: number\n max_inputs?: number\n}\n\ninterface ModelSupports {\n tool_calls?: boolean\n parallel_tool_calls?: boolean\n dimensions?: boolean\n // Thinking/reasoning capabilities\n max_thinking_budget?: number\n min_thinking_budget?: number\n adaptive_thinking?: boolean\n /**\n * Per-model whitelist of `output_config.effort` values that Copilot's\n * `/v1/messages` mirror will accept for adaptive-thinking models.\n * As of 2026-05, Copilot caps Opus 4.7 to `[\"medium\"]` and allows\n * Sonnet 4.6 `[\"low\",\"medium\",\"high\"]`. Sending anything outside\n * this list returns 400 \"not supported by model X; supported values: [...]\".\n */\n reasoning_effort?: Array<string>\n}\n\ninterface ModelCapabilities {\n family: string\n limits: ModelLimits\n object: string\n supports: ModelSupports\n tokenizer: string\n type: string\n}\n\nexport interface Model {\n capabilities: ModelCapabilities\n id: string\n model_picker_enabled: boolean\n name: string\n object: string\n preview: boolean\n vendor: string\n version: string\n policy?: {\n state: string\n terms: string\n }\n billing?: {\n is_premium: boolean\n multiplier: number\n restricted_to?: Array<string>\n }\n /**\n * Optional list of native upstream endpoints this model supports.\n * GitHub Copilot's `/models` response advertises this for some models\n * (e.g. Claude family typically includes \"anthropic-messages\") so we\n * can route directly to the native API instead of translating.\n */\n supported_endpoints?: Array<string>\n}\n","const FALLBACK = \"1.104.3\"\n\nexport async function getVSCodeVersion() {\n const controller = new AbortController()\n const timeout = setTimeout(() => {\n controller.abort()\n }, 5000)\n\n try {\n const response = await fetch(\n \"https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=visual-studio-code-bin\",\n {\n signal: controller.signal,\n },\n )\n\n const pkgbuild = await response.text()\n const pkgverRegex = /pkgver=([0-9.]+)/\n const match = pkgbuild.match(pkgverRegex)\n\n if (match) {\n return match[1]\n }\n\n return FALLBACK\n } catch {\n return FALLBACK\n } finally {\n clearTimeout(timeout)\n }\n}\n","import consola from \"consola\"\n\nimport type { Model } from \"~/services/copilot/get-models\"\n\nimport { getModels } from \"~/services/copilot/get-models\"\nimport { getVSCodeVersion } from \"~/services/get-vscode-version\"\n\nimport { state } from \"./state\"\n\nexport const sleep = (ms: number) =>\n new Promise((resolve) => {\n setTimeout(resolve, ms)\n })\n\n/**\n * Extract the root-cause message from an unknown thrown value.\n *\n * If the error wraps another error via the standard `cause` property, the\n * inner message is returned instead — giving a one-line summary of *why*\n * the operation failed without the noise of the full stack trace.\n */\nexport function rootCause(err: unknown): string {\n if (err instanceof Error) {\n return err.cause instanceof Error ? err.cause.message : err.message\n }\n return String(err)\n}\n\nexport const isNullish = (value: unknown): value is null | undefined =>\n value === null || value === undefined\n\nexport async function cacheModels(): Promise<void> {\n const models = await getModels()\n state.models = models\n}\n\nexport const cacheVSCodeVersion = async () => {\n const response = await getVSCodeVersion()\n state.vsCodeVersion = response\n\n consola.info(`Using VSCode version: ${response}`)\n}\n\n/**\n * Find a model in state.models using multi-strategy exact matching.\n *\n * Strategies (in order):\n * 1. Exact match on model ID\n * 2. Strip date suffix (claude-opus-4-6-20251101 → claude-opus-4-6)\n * 3. Dash to dot version (claude-opus-4-5 → claude-opus-4.5)\n * 4. Dot to dash version (claude-opus-4.5 → claude-opus-4-5)\n *\n * No fuzzy/family matching — all strategies produce deterministic exact IDs.\n */\nexport function findModel(modelName: string): Model | undefined {\n const models = state.models?.data\n if (!models || models.length === 0) {\n return undefined\n }\n\n // 1. Exact match\n const exact = models.find((m) => m.id === modelName)\n if (exact) return exact\n\n // 2. Strip date suffix\n const base = modelName.replace(/-\\d{8}$/, \"\")\n if (base !== modelName) {\n const baseMatch = models.find((m) => m.id === base)\n if (baseMatch) return baseMatch\n }\n\n // 3. Dash to dot version (4-5 → 4.5)\n const withDot = base.replace(/-(\\d+)-(\\d+)$/, \"-$1.$2\")\n if (withDot !== base) {\n const dotMatch = models.find((m) => m.id === withDot)\n if (dotMatch) return dotMatch\n }\n\n // 4. Dot to dash version (4.5 → 4-5)\n const withDash = modelName.replace(/(\\d+)\\.(\\d+)/, \"$1-$2\")\n if (withDash !== modelName) {\n const dashMatch = models.find((m) => m.id === withDash)\n if (dashMatch) return dashMatch\n }\n\n return undefined\n}\n","import type { Context } from \"hono\"\nimport type { ContentfulStatusCode } from \"hono/utils/http-status\"\n\nimport consola from \"consola\"\n\nimport { rootCause } from \"~/lib/utils\"\n\nexport class HTTPError extends Error {\n response: Response\n\n constructor(message: string, response: Response) {\n super(message)\n this.response = response\n }\n}\n\nexport async function forwardError(c: Context, error: unknown) {\n if (error instanceof HTTPError) {\n // Try to read error body, but it may already be consumed by the caller\n let errorText: string\n try {\n errorText = await error.response.text()\n } catch {\n // Body already read — fall back to the error message\n errorText = error.message\n }\n\n // 400 errors: concise log, already detailed upstream\n if (error.response.status === 400) {\n // no extra logging, upstream already printed details\n } else {\n let errorJson: unknown\n try {\n errorJson = JSON.parse(errorText)\n } catch {\n errorJson = errorText\n }\n consola.warn(`Error occurred: ${rootCause(error)}`)\n consola.debug(\"HTTP error:\", errorJson)\n }\n\n const isCopilotSessionLimit =\n error.response.status === 429\n && errorText.includes(\"user_global_rate_limited:pro_plus\")\n if (isCopilotSessionLimit) {\n c.header(\"x-should-retry\", \"false\")\n }\n\n return c.json(\n {\n error: {\n message: errorText,\n type: \"error\",\n },\n },\n (isCopilotSessionLimit ? 403 : (\n error.response.status\n )) as ContentfulStatusCode,\n )\n }\n\n // Network errors (fetch failed, TLS disconnect, etc.) — concise log\n const message = (error as Error).message || String(error)\n const cause = (error as { cause?: Error }).cause\n if (cause) {\n consola.error(`${message}: ${cause.message}`)\n } else {\n consola.error(message)\n }\n return c.json(\n {\n error: {\n message: (error as Error).message,\n type: \"error\",\n },\n },\n 500,\n )\n}\n","import fs from \"node:fs/promises\"\nimport os from \"node:os\"\nimport path from \"node:path\"\n\nconst APP_DIR = path.join(os.homedir(), \".local\", \"share\", \"copilot-api-plus\")\n\nconst GITHUB_TOKEN_PATH = path.join(APP_DIR, \"github_token\")\n\nconst ACCOUNTS_PATH = path.join(APP_DIR, \"accounts.json\")\n\nexport const PATHS = {\n APP_DIR,\n DATA_DIR: APP_DIR,\n GITHUB_TOKEN_PATH,\n ACCOUNTS_PATH,\n}\n\nexport async function ensurePaths(): Promise<void> {\n await fs.mkdir(PATHS.APP_DIR, { recursive: true })\n await ensureFile(PATHS.GITHUB_TOKEN_PATH)\n}\n\nasync function ensureFile(filePath: string): Promise<void> {\n try {\n await fs.access(filePath, fs.constants.W_OK)\n } catch {\n await fs.writeFile(filePath, \"\")\n await fs.chmod(filePath, 0o600)\n }\n}\n","import consola from \"consola\"\nimport { getProxyForUrl } from \"proxy-from-env\"\nimport { Agent, ProxyAgent, setGlobalDispatcher, type Dispatcher } from \"undici\"\n\n// Module-level references so that `resetConnections` can swap them out.\n// Initialised by `initProxyFromEnv`; the dispatcher closure captures the\n// *variables* (not their values), so replacing them is enough.\nconst agentOptions = {\n keepAliveTimeout: 300_000,\n keepAliveMaxTimeout: 600_000,\n // Allow HTTP/2 when the target supports it. Inside a CONNECT tunnel\n // the ALPN negotiation and any h2 frames are encrypted traffic — proxy\n // nodes see it as \"data flowing\" and won't kill the connection for\n // being idle.\n allowH2: true,\n // Disable body timeout for SSE: undici's default 300s body timeout kills\n // long-running SSE streams during model thinking phases with no data.\n bodyTimeout: 0,\n connect: {\n timeout: 15_000,\n keepAlive: true,\n keepAliveInitialDelay: 10_000,\n },\n}\nlet direct: Agent | undefined\nlet proxies = new Map<string, ProxyAgent>()\n/** Whether a proxy is actually configured and in use. */\nlet proxyActive = false\n\n/** Whether an environment-level proxy is configured for Copilot URLs. */\nexport function isProxyActive(): boolean {\n return proxyActive\n}\n\n/**\n * Check whether a specific request will be proxied.\n * Account-level proxy takes precedence; falls back to environment proxy.\n */\nexport function isAccountProxied(accountProxy?: string): boolean {\n if (accountProxy) return true\n return proxyActive\n}\n\n// ---------------------------------------------------------------------------\n// Proxy-tunnel keepalive: periodic lightweight requests through the proxy\n// ---------------------------------------------------------------------------\n\n/**\n * Many proxy nodes (especially third-party VPN/airport services) kill\n * CONNECT tunnels that are idle for ~60 s. During long model thinking\n * phases the SSE stream carries no data, which looks \"idle\" to the proxy.\n *\n * This keepalive sends a tiny HEAD request to the Copilot API every 30 s\n * through the SAME connection pool that the SSE stream uses. For\n * per-account connections, this means pinging through each account's own\n * dispatcher, not the global one — so the correct proxy tunnel is kept\n * alive.\n *\n * The keepalive is active ONLY while there are SSE streams in flight\n * (tracked per-account via `activeStreams`).\n */\nlet keepaliveTimer: ReturnType<typeof setInterval> | undefined\nconst KEEPALIVE_INTERVAL_MS = 20_000\nconst KEEPALIVE_URL = \"https://api.individual.githubcopilot.com/\"\n\ninterface ActiveStreamInfo {\n count: number\n accountProxy?: string\n apiBaseUrl?: string\n}\n\n/** Account info attached to streaming responses for keepalive targeting. */\nexport interface StreamAccountInfo {\n accountId?: string\n accountProxy?: string\n apiBaseUrl?: string\n}\n\n/** Track active streams: global (key = \"__global__\") and per-account. */\nconst activeStreams = new Map<string, ActiveStreamInfo>()\n\nfunction startKeepalive(): void {\n if (keepaliveTimer) return\n keepaliveTimer = setInterval(() => {\n // Ping each connection pool that has active streams\n for (const [key, info] of activeStreams) {\n if (info.count <= 0) continue\n // Use the tracked API base URL so the keepalive request goes through\n // the SAME origin (and thus the same HTTP/2 session / CONNECT tunnel)\n // as the active SSE stream.\n const pingUrl = info.apiBaseUrl || KEEPALIVE_URL\n\n if (key === \"__global__\") {\n // Global connection — use standard fetch (goes through global dispatcher)\n fetch(pingUrl, { method: \"HEAD\" }).catch(() => {})\n consola.debug(\n `Proxy keepalive ping sent (global → ${new URL(pingUrl).hostname})`,\n )\n } else {\n // Per-account connection — ping through the account's own dispatcher\n const dispatcher = getAccountDispatcher(key, info.accountProxy)\n fetch(pingUrl, {\n method: \"HEAD\",\n dispatcher: dispatcher as unknown as undefined,\n } as RequestInit).catch(() => {})\n consola.debug(\n `Proxy keepalive ping sent (account ${key.slice(0, 8)} → ${new URL(pingUrl).hostname})`,\n )\n }\n }\n }, KEEPALIVE_INTERVAL_MS)\n // Don't prevent Node from exiting because of this timer.\n keepaliveTimer.unref()\n consola.debug(\n \"Proxy keepalive started (20s interval, targeting active stream origins)\",\n )\n}\n\nfunction stopKeepalive(): void {\n if (keepaliveTimer) {\n clearInterval(keepaliveTimer)\n keepaliveTimer = undefined\n consola.debug(\"Proxy keepalive stopped (no active streams)\")\n }\n}\n\nfunction getTotalStreamCount(): number {\n let total = 0\n for (const info of activeStreams.values()) {\n total += info.count\n }\n return total\n}\n\n/**\n * Call when an SSE stream starts. Activates the proxy-tunnel keepalive\n * if this is the first active stream and a proxy is configured.\n *\n * @param accountInfo If provided, keepalive pings go through this\n * account's own connection pool (not the global one).\n */\nexport function notifyStreamStart(accountInfo?: StreamAccountInfo): void {\n if (!proxyActive) return\n\n const key = accountInfo?.accountId ?? \"__global__\"\n const existing = activeStreams.get(key)\n if (existing) {\n existing.count++\n // Update apiBaseUrl if provided (may differ across streams)\n if (accountInfo?.apiBaseUrl) existing.apiBaseUrl = accountInfo.apiBaseUrl\n } else {\n activeStreams.set(key, {\n count: 1,\n accountProxy: accountInfo?.accountProxy,\n apiBaseUrl: accountInfo?.apiBaseUrl,\n })\n }\n\n if (getTotalStreamCount() === 1) startKeepalive()\n}\n\n/**\n * Call when an SSE stream ends (success or error). Stops the keepalive\n * once no streams are active.\n */\nexport function notifyStreamEnd(accountInfo?: StreamAccountInfo): void {\n if (!proxyActive) return\n\n const key = accountInfo?.accountId ?? \"__global__\"\n const existing = activeStreams.get(key)\n if (existing) {\n existing.count = Math.max(0, existing.count - 1)\n if (existing.count === 0) activeStreams.delete(key)\n }\n\n if (getTotalStreamCount() === 0) stopKeepalive()\n}\n\nexport function initProxyFromEnv(): void {\n if (typeof Bun !== \"undefined\") {\n // Bun's native fetch automatically respects HTTP_PROXY/HTTPS_PROXY.\n // We still need to detect proxy presence so that keepalive and heartbeat\n // logic activates correctly.\n const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy\n const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy\n proxyActive = Boolean(httpProxy || httpsProxy)\n if (proxyActive) {\n consola.debug(\"Bun runtime: proxy detected from environment variables\")\n }\n return\n }\n\n try {\n direct = new Agent(agentOptions)\n proxies = new Map<string, ProxyAgent>()\n\n // We only need a minimal dispatcher that implements `dispatch` at runtime.\n // Typing the object as `Dispatcher` forces TypeScript to require many\n // additional methods. Instead, keep a plain object and cast when passing\n // to `setGlobalDispatcher`.\n const dispatcher = {\n dispatch(\n options: Dispatcher.DispatchOptions,\n handler: Dispatcher.DispatchHandler,\n ) {\n try {\n const origin =\n typeof options.origin === \"string\" ?\n new URL(options.origin)\n : (options.origin as URL)\n const get = getProxyForUrl as unknown as (\n u: string,\n ) => string | undefined\n const raw = get(origin.toString())\n const proxyUrl = raw && raw.length > 0 ? raw : undefined\n if (!proxyUrl) {\n consola.debug(`HTTP proxy bypass: ${origin.hostname}`)\n return (direct as unknown as Dispatcher).dispatch(options, handler)\n }\n let agent = proxies.get(proxyUrl)\n if (!agent) {\n agent = new ProxyAgent({ uri: proxyUrl, ...agentOptions })\n proxies.set(proxyUrl, agent)\n }\n let label = proxyUrl\n try {\n const u = new URL(proxyUrl)\n label = `${u.protocol}//${u.host}`\n } catch {\n /* noop */\n }\n consola.debug(`HTTP proxy route: ${origin.hostname} via ${label}`)\n return (agent as unknown as Dispatcher).dispatch(options, handler)\n } catch {\n return (direct as unknown as Dispatcher).dispatch(options, handler)\n }\n },\n close() {\n for (const agent of proxies.values()) {\n void (agent as unknown as Dispatcher).close()\n }\n // `direct` is always set before the dispatcher is installed.\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return direct!.close()\n },\n destroy() {\n for (const agent of proxies.values()) {\n void (agent as unknown as Dispatcher).destroy()\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return direct!.destroy()\n },\n }\n\n setGlobalDispatcher(dispatcher as unknown as Dispatcher)\n // Only activate proxy keepalive if a proxy is actually configured\n // for at least one relevant URL.\n const get = getProxyForUrl as unknown as (u: string) => string | undefined\n const testUrls = [\n \"https://api.individual.githubcopilot.com/\",\n \"https://api.business.githubcopilot.com/\",\n \"https://api.github.com/\",\n ]\n proxyActive = testUrls.some((u) => {\n const raw = get(u)\n return raw !== undefined && raw.length > 0\n })\n\n if (proxyActive) {\n consola.debug(\n \"Proxy active: undici dispatcher configured with bodyTimeout=0, allowH2=true\",\n )\n } else {\n consola.debug(\n \"HTTP proxy dispatcher installed but no proxy URLs detected\",\n )\n }\n } catch (err) {\n consola.debug(\"Proxy setup skipped:\", err)\n }\n}\n\n/**\n * Destroy all pooled connections (direct + proxy agents) and replace them\n * with fresh instances. The global dispatcher's `dispatch` method captures\n * `direct` and `proxies` by reference, so subsequent requests automatically\n * use the new agents — no need to call `setGlobalDispatcher` again.\n *\n * Call this after a network error to discard stale/half-closed sockets that\n * would otherwise cause every retry to wait ~60 s before timing out.\n *\n * Under the Bun runtime (which doesn't use undici) this is a no-op.\n */\nexport function resetConnections(): void {\n if (typeof Bun !== \"undefined\") return\n if (!direct) return\n\n const oldDirect = direct\n const oldProxies = proxies\n\n direct = new Agent(agentOptions)\n proxies = new Map<string, ProxyAgent>()\n\n // Tear down old agents in the background — errors are non-fatal.\n void (oldDirect as unknown as Dispatcher).close().catch(() => {})\n for (const agent of oldProxies.values()) {\n void (agent as unknown as Dispatcher).close().catch(() => {})\n }\n\n consola.debug(\"Connection pool reset — stale sockets cleared\")\n}\n\n// ---------------------------------------------------------------------------\n// Per-account connection isolation\n// ---------------------------------------------------------------------------\n\n/** Separate connection pools per account to prevent cross-account correlation. */\nconst accountAgents = new Map<string, Agent>()\nconst accountProxyAgents = new Map<string, Map<string, ProxyAgent>>()\n\n/**\n * Get or create an isolated undici Agent for a specific account.\n * Each account gets its own connection pool so that GitHub cannot correlate\n * accounts by shared TCP connections or TLS sessions.\n */\nexport function getAccountDispatcher(\n accountId: string,\n accountProxy?: string,\n): {\n dispatch: (\n options: Dispatcher.DispatchOptions,\n handler: Dispatcher.DispatchHandler,\n ) => boolean\n} {\n // Return a dispatcher that routes through per-account agents\n return {\n dispatch(\n options: Dispatcher.DispatchOptions,\n handler: Dispatcher.DispatchHandler,\n ) {\n try {\n const origin =\n typeof options.origin === \"string\" ?\n new URL(options.origin)\n : (options.origin as URL)\n // Account-level proxy takes precedence over environment proxy\n let proxyUrl: string | undefined\n if (accountProxy) {\n proxyUrl = accountProxy\n } else {\n const get = getProxyForUrl as unknown as (\n u: string,\n ) => string | undefined\n const raw = get(origin.toString())\n proxyUrl = raw && raw.length > 0 ? raw : undefined\n }\n\n if (!proxyUrl) {\n // Direct connection — use per-account agent\n let agent = accountAgents.get(accountId)\n if (!agent) {\n agent = new Agent(agentOptions)\n accountAgents.set(accountId, agent)\n }\n return (agent as unknown as Dispatcher).dispatch(options, handler)\n }\n\n // Proxy connection — use per-account proxy agent\n let proxyMap = accountProxyAgents.get(accountId)\n if (!proxyMap) {\n proxyMap = new Map<string, ProxyAgent>()\n accountProxyAgents.set(accountId, proxyMap)\n }\n let proxyAgent = proxyMap.get(proxyUrl)\n if (!proxyAgent) {\n proxyAgent = new ProxyAgent({ uri: proxyUrl, ...agentOptions })\n proxyMap.set(proxyUrl, proxyAgent)\n }\n return (proxyAgent as unknown as Dispatcher).dispatch(options, handler)\n } catch {\n // Fallback to per-account direct agent\n let agent = accountAgents.get(accountId)\n if (!agent) {\n agent = new Agent(agentOptions)\n accountAgents.set(accountId, agent)\n }\n return (agent as unknown as Dispatcher).dispatch(options, handler)\n }\n },\n }\n}\n\n/**\n * Reset connection pools for a specific account.\n */\nexport function resetAccountConnections(accountId: string): void {\n const oldAgent = accountAgents.get(accountId)\n if (oldAgent) {\n void (oldAgent as unknown as Dispatcher).close().catch(() => {})\n accountAgents.delete(accountId)\n }\n const oldProxies = accountProxyAgents.get(accountId)\n if (oldProxies) {\n for (const agent of oldProxies.values()) {\n void (agent as unknown as Dispatcher).close().catch(() => {})\n }\n accountProxyAgents.delete(accountId)\n }\n}\n\n/**\n * Reset all per-account connection pools.\n */\nexport function resetAllAccountConnections(): void {\n for (const [id] of accountAgents) {\n resetAccountConnections(id)\n }\n}\n\n// ---------------------------------------------------------------------------\n// Periodic connection pool recreation\n// ---------------------------------------------------------------------------\n\nlet connectionRecycleTimer: ReturnType<typeof setInterval> | undefined\n\n/**\n * Start periodic connection pool recreation for all per-account pools.\n * Simulates \"VS Code restart\" behavior — stale connections are replaced\n * with fresh ones at randomized intervals to avoid timing correlation.\n *\n * @param baseIntervalMs Base interval (default 4 hours).\n */\nexport function startConnectionRecycling(\n baseIntervalMs: number = 4 * 60 * 60 * 1000,\n): void {\n stopConnectionRecycling()\n\n connectionRecycleTimer = setInterval(() => {\n // Add ±25% jitter to avoid all accounts recycling at the same time\n const jitter = baseIntervalMs * 0.25 * (Math.random() * 2 - 1)\n setTimeout(\n () => {\n resetAllAccountConnections()\n consola.debug(\"Per-account connection pools recycled\")\n },\n Math.max(0, jitter),\n )\n }, baseIntervalMs)\n\n // Don't prevent Node from exiting\n connectionRecycleTimer.unref()\n consola.debug(\n `Connection pool recycling started (interval: ~${Math.round(baseIntervalMs / 3_600_000)}h)`,\n )\n}\n\n/**\n * Stop periodic connection pool recreation.\n */\nexport function stopConnectionRecycling(): void {\n if (connectionRecycleTimer) {\n clearInterval(connectionRecycleTimer)\n connectionRecycleTimer = undefined\n }\n}\n","import consola from \"consola\"\n\nimport { GITHUB_API_BASE_URL, githubHeaders } from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\n/**\n * Fetch a short-lived Copilot JWT from the GitHub API.\n *\n * @param githubToken Optional explicit GitHub PAT. When provided the request\n * uses this token instead of `state.githubToken` and does\n * **not** mutate global state (so multi-account callers\n * stay side-effect-free).\n */\nexport const getCopilotToken = async (githubToken?: string) => {\n const tokenToUse = githubToken ?? state.githubToken\n const isExplicitToken = githubToken !== undefined\n\n const url = `${GITHUB_API_BASE_URL}/copilot_internal/v2/token`\n const fetchOptions: RequestInit = {\n headers: githubHeaders({\n ...state,\n githubToken: tokenToUse,\n }),\n }\n\n // Retry on transient network errors (TLS disconnect, connection timeout, etc.)\n const maxRetries = 2\n let lastError: unknown\n let response: Response | undefined\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n response = await fetch(url, fetchOptions)\n break\n } catch (error: unknown) {\n lastError = error\n if (attempt < maxRetries) {\n const delay = 1000 * (attempt + 1)\n consola.warn(\n `Token fetch error on attempt ${attempt + 1}/${maxRetries + 1}, retrying in ${delay}ms:`,\n error instanceof Error ? error.message : error,\n )\n await new Promise((r) => setTimeout(r, delay))\n }\n }\n }\n\n if (!response) {\n throw lastError\n }\n\n if (!response.ok) throw new HTTPError(\"Failed to get Copilot token\", response)\n\n const data = (await response.json()) as GetCopilotTokenResponse\n\n // Only write to global state when using the default token (single-account mode).\n // When an explicit githubToken is provided (multi-account), the caller is\n // responsible for storing the endpoint on its own Account object.\n if (!isExplicitToken && data.endpoints?.api) {\n // eslint-disable-next-line require-atomic-updates\n state.copilotApiEndpoint = data.endpoints.api\n }\n\n return data\n}\n\n// Full interface matching Zed's implementation\ninterface GetCopilotTokenResponse {\n expires_at: number\n refresh_in: number\n token: string\n endpoints?: {\n api: string\n \"origin-tracker\"?: string\n proxy?: string\n telemetry?: string\n }\n annotations_enabled?: boolean\n chat_enabled?: boolean\n chat_jetbrains_enabled?: boolean\n code_quote_enabled?: boolean\n codesearch?: boolean\n copilot_ide_agent_chat_gpt4_small_prompt?: boolean\n copilotignore_enabled?: boolean\n individual?: boolean\n sku?: string\n tracking_id?: string\n limited_user_quotas?: unknown // Premium request quotas\n}\n","import { GITHUB_API_BASE_URL, githubHeaders } from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\n/**\n * Fetch the authenticated user's Copilot usage / quota snapshot.\n *\n * @param githubToken Optional explicit GitHub PAT. When provided the request\n * uses this token instead of `state.githubToken`, allowing\n * multi-account callers to query any account without\n * touching global state.\n */\nexport const getCopilotUsage = async (\n githubToken?: string,\n): Promise<CopilotUsageResponse> => {\n const response = await fetch(`${GITHUB_API_BASE_URL}/copilot_internal/user`, {\n headers: githubHeaders({\n ...state,\n githubToken: githubToken ?? state.githubToken,\n }),\n })\n\n if (!response.ok) {\n throw new HTTPError(\"Failed to get Copilot usage\", response)\n }\n\n return (await response.json()) as CopilotUsageResponse\n}\n\nexport interface QuotaDetail {\n entitlement: number\n overage_count: number\n overage_permitted: boolean\n percent_remaining: number\n quota_id: string\n quota_remaining: number\n remaining: number\n unlimited: boolean\n}\n\ninterface QuotaSnapshots {\n chat: QuotaDetail\n completions: QuotaDetail\n premium_interactions: QuotaDetail\n}\n\ninterface CopilotUsageResponse {\n access_type_sku: string\n analytics_tracking_id: string\n assigned_date: string\n can_signup_for_limited: boolean\n chat_enabled: boolean\n copilot_plan: string\n organization_login_list: Array<unknown>\n organization_list: Array<unknown>\n quota_reset_date: string\n quota_snapshots: QuotaSnapshots\n}\n","import { GITHUB_API_BASE_URL, standardHeaders } from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\n/**\n * Fetch the GitHub user profile.\n *\n * @param githubToken Optional explicit token. When omitted, falls back to\n * the global `state.githubToken`. Prefer passing a token\n * explicitly to avoid race conditions in multi-account mode.\n */\nexport async function getGitHubUser(githubToken?: string) {\n const token = githubToken ?? state.githubToken\n const response = await fetch(`${GITHUB_API_BASE_URL}/user`, {\n headers: {\n authorization: `token ${token}`,\n ...standardHeaders(),\n },\n })\n\n if (!response.ok) throw new HTTPError(\"Failed to get GitHub user\", response)\n\n return (await response.json()) as GithubUserResponse\n}\n\n// Trimmed for the sake of simplicity\ninterface GithubUserResponse {\n login: string\n}\n","import consola from \"consola\"\nimport { randomBytes, randomUUID } from \"node:crypto\"\nimport fs from \"node:fs/promises\"\n\nimport { HTTPError } from \"~/lib/error\"\nimport { PATHS } from \"~/lib/paths\"\nimport { startConnectionRecycling, stopConnectionRecycling } from \"~/lib/proxy\"\nimport { rootCause } from \"~/lib/utils\"\nimport { getCopilotToken } from \"~/services/github/get-copilot-token\"\nimport { getCopilotUsage } from \"~/services/github/get-copilot-usage\"\nimport { getGitHubUser } from \"~/services/github/get-user\"\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\nexport type AccountStatus =\n | \"active\"\n | \"exhausted\"\n | \"rate_limited\"\n | \"banned\"\n | \"error\"\n | \"disabled\"\n\nexport interface Account {\n id: string\n label: string\n githubToken: string\n /** Short-lived Copilot JWT – kept in memory only, never persisted. */\n copilotToken?: string\n /** API endpoint extracted from the Copilot token response. */\n copilotApiEndpoint?: string\n accountType: string // \"individual\" | \"business\" | \"enterprise\"\n\n // Status\n status: AccountStatus\n statusMessage?: string\n lastUsedAt?: number\n consecutiveFailures: number\n cooldownUntil?: number\n\n // Quota snapshot (refreshed periodically)\n usage?: {\n premium_remaining: number\n premium_total: number\n chat_remaining: number\n chat_total: number\n /** Whether the upstream account is allowed to exceed its monthly quota. */\n premium_overage_permitted?: boolean\n quotaResetDate: string\n lastCheckedAt: number\n }\n\n // Anti-correlation\n /** Stable per-account machine identifier – persisted to disk. */\n machineId?: string\n /** Runtime-only session identifier – regenerated on every startup. */\n sessionId?: string\n /** Timestamp of the last request sent using this account. */\n lastRequestAt?: number\n /** Optional per-account proxy URL (e.g. \"http://proxy:8080\" or \"socks5://proxy:1080\"). */\n proxy?: string\n\n // Metadata\n githubLogin?: string\n addedAt: number\n}\n\n// ---------------------------------------------------------------------------\n// Persistence helpers\n// ---------------------------------------------------------------------------\n\n/** Fields excluded from the JSON file (short-lived / runtime-only). */\ntype PersistedAccount = Omit<Account, \"copilotToken\" | \"sessionId\">\n\nconst ACCOUNTS_PATH = PATHS.ACCOUNTS_PATH\n\n// ---------------------------------------------------------------------------\n// AccountManager\n// ---------------------------------------------------------------------------\n\nconst COOLDOWN_MS = 60 * 1000 // 60 seconds\n\nexport class AccountManager {\n private accounts: Array<Account> = []\n private refreshInterval?: ReturnType<typeof setInterval>\n private usageInterval?: ReturnType<typeof setInterval>\n\n // Debounced save\n private saveTimer?: ReturnType<typeof setTimeout>\n private savePending = false\n\n /** True if accounts.json existed on disk when loadAccounts() was called. */\n accountsFileExisted = false\n\n // ---------- Persistence ------------------------------------------------\n\n /**\n * Load accounts from the JSON file on disk.\n * Missing file is treated as empty list – not an error.\n */\n async loadAccounts(): Promise<void> {\n try {\n // eslint-disable-next-line unicorn/prefer-json-parse-buffer\n const raw = await fs.readFile(ACCOUNTS_PATH, \"utf8\")\n this.accountsFileExisted = true\n const parsed = JSON.parse(raw) as Array<PersistedAccount>\n this.accounts = parsed.map((a) => ({\n ...a,\n copilotToken: undefined,\n sessionId: randomUUID(),\n machineId: a.machineId || randomBytes(32).toString(\"hex\"),\n }))\n consola.info(`Loaded ${this.accounts.length} account(s) from disk`)\n } catch (err: unknown) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") {\n this.accountsFileExisted = false\n this.accounts = []\n return\n }\n consola.warn(`Failed to load accounts: ${rootCause(err)}`)\n consola.debug(\"Failed to load accounts:\", err)\n this.accounts = []\n }\n }\n\n /**\n * Persist accounts to disk. `copilotToken` is excluded because it is\n * short-lived and will be refreshed on every startup.\n */\n async saveAccounts(): Promise<void> {\n const data: Array<PersistedAccount> = this.accounts.map(\n ({ copilotToken: _dropped, sessionId: _session, ...rest }) => rest,\n )\n try {\n await fs.writeFile(ACCOUNTS_PATH, JSON.stringify(data, null, 2), {\n encoding: \"utf8\",\n mode: 0o600,\n })\n } catch (err) {\n consola.warn(`Failed to save accounts: ${rootCause(err)}`)\n consola.debug(\"Failed to save accounts:\", err)\n }\n }\n\n /** Schedule a debounced save (coalesces rapid status updates). */\n private debouncedSave(): void {\n if (this.saveTimer) return // already scheduled\n this.savePending = true\n this.saveTimer = setTimeout(async () => {\n this.saveTimer = undefined\n if (this.savePending) {\n this.savePending = false\n await this.saveAccounts()\n }\n }, 1_000)\n }\n\n // ---------- CRUD -------------------------------------------------------\n\n /**\n * Add a new account.\n *\n * 1. Validates the GitHub token by fetching user info.\n * 2. Obtains an initial Copilot token.\n * 3. Persists the account to disk.\n */\n async addAccount(\n githubToken: string,\n label: string,\n accountType: string = \"individual\",\n ): Promise<Account> {\n // 1. Validate token – get GitHub login (pass token explicitly, no state mutation)\n const user = await getGitHubUser(githubToken)\n\n // 2. Obtain Copilot token – pass token directly (no state mutation)\n const tokenData = await getCopilotToken(githubToken)\n\n const account: Account = {\n id: randomUUID(),\n label,\n githubToken,\n copilotToken: tokenData.token,\n copilotApiEndpoint: tokenData.endpoints?.api,\n accountType,\n status: \"active\",\n consecutiveFailures: 0,\n githubLogin: user.login,\n machineId: randomBytes(32).toString(\"hex\"),\n sessionId: randomUUID(),\n addedAt: Date.now(),\n }\n\n this.accounts.push(account)\n await this.saveAccounts()\n\n consola.success(`Account added: ${label} (${user.login})`)\n return account\n }\n\n async removeAccount(id: string): Promise<boolean> {\n const idx = this.accounts.findIndex((a) => a.id === id)\n if (idx === -1) return false\n const [removed] = this.accounts.splice(idx, 1)\n await this.saveAccounts()\n consola.info(`Account removed: ${removed.label}`)\n return true\n }\n\n getAccounts(): Array<Account> {\n return this.accounts\n }\n\n getAccountById(id: string): Account | undefined {\n return this.accounts.find((a) => a.id === id)\n }\n\n // ---------- Smart account selection ------------------------------------\n\n /**\n * Pick the best available account.\n *\n * 1. Filter out disabled, banned, exhausted, and accounts still in cooldown.\n * 2. Prefer accounts with more remaining premium quota.\n * 3. Fall back to round-robin (least-recently-used) when quotas are equal\n * or unknown.\n */\n getActiveAccount(): Account | undefined {\n const now = Date.now()\n\n const eligible = this.accounts.filter((a) => {\n if (\n a.status === \"disabled\"\n || a.status === \"banned\"\n || a.status === \"exhausted\"\n ) {\n return false\n }\n if (a.cooldownUntil && a.cooldownUntil > now) return false\n return true\n })\n\n if (eligible.length === 0) {\n // Fallback: if there is exactly one account and it's only cooling down\n // (not banned/disabled/exhausted), return it anyway. With a single\n // account there is nothing to \"switch to\", so blocking all requests\n // for the cooldown period would be a self-inflicted outage.\n if (this.accounts.length === 1) {\n const solo = this.accounts[0]\n if (\n solo.status !== \"disabled\"\n && solo.status !== \"banned\"\n && solo.status !== \"exhausted\"\n ) {\n return solo\n }\n }\n return undefined\n }\n\n eligible.sort((a, b) => {\n const aRemaining = a.usage?.premium_remaining ?? -1\n const bRemaining = b.usage?.premium_remaining ?? -1\n\n // Higher remaining quota first\n if (aRemaining !== bRemaining) return bRemaining - aRemaining\n\n // Equal / unknown → least recently used first (round-robin)\n const aUsed = a.lastUsedAt ?? 0\n const bUsed = b.lastUsedAt ?? 0\n return aUsed - bUsed\n })\n\n return eligible[0]\n }\n\n // ---------- Status management ------------------------------------------\n\n markAccountStatus(id: string, status: AccountStatus, message?: string): void {\n const account = this.getAccountById(id)\n if (!account) return\n\n account.status = status\n account.statusMessage = message\n\n switch (status) {\n case \"rate_limited\":\n case \"error\": {\n // Only apply cooldown when there are other accounts to switch to.\n // With a single account, cooldown would block ALL requests with\n // \"No available accounts\" — effectively a self-inflicted outage.\n if (this.accounts.length > 1) {\n account.cooldownUntil = Date.now() + COOLDOWN_MS\n }\n account.consecutiveFailures += 1\n\n break\n }\n case \"banned\": {\n account.consecutiveFailures += 1\n\n break\n }\n case \"active\": {\n // Recovering from a failure state — reset\n account.consecutiveFailures = 0\n account.cooldownUntil = undefined\n\n break\n }\n // No default\n }\n // \"exhausted\" and \"disabled\" don't touch consecutiveFailures\n\n this.debouncedSave()\n }\n\n markAccountSuccess(id: string): void {\n const account = this.getAccountById(id)\n if (!account) return\n\n account.consecutiveFailures = 0\n account.lastUsedAt = Date.now()\n\n if (account.status === \"error\" || account.status === \"rate_limited\") {\n account.status = \"active\"\n account.statusMessage = undefined\n account.cooldownUntil = undefined\n }\n\n this.debouncedSave()\n }\n\n // ---------- Token & usage refresh (per account) ------------------------\n\n /**\n * Refresh the short-lived Copilot JWT for a single account.\n *\n * Passes the account's GitHub token directly to `getCopilotToken()` so that\n * global `state.githubToken` is never mutated — safe for concurrent use.\n */\n async refreshAccountToken(account: Account): Promise<void> {\n try {\n const data = await getCopilotToken(account.githubToken)\n // eslint-disable-next-line require-atomic-updates\n account.copilotToken = data.token\n if (data.endpoints?.api) {\n // eslint-disable-next-line require-atomic-updates\n account.copilotApiEndpoint = data.endpoints.api\n }\n } catch (err: unknown) {\n if (err instanceof HTTPError && err.response.status === 401) {\n this.markAccountStatus(account.id, \"banned\", \"GitHub token invalid\")\n consola.warn(\n `Account ${account.label}: token invalid, marked as banned`,\n )\n } else {\n consola.warn(\n `Account ${account.label}: failed to refresh Copilot token: ${rootCause(err)}`,\n )\n consola.debug(\n `Account ${account.label}: failed to refresh Copilot token:`,\n err,\n )\n }\n }\n }\n\n /**\n * Refresh the usage / quota snapshot for a single account.\n *\n * Passes the account's GitHub token directly to `getCopilotUsage()` so that\n * global `state.githubToken` is never mutated — safe for concurrent use.\n */\n async refreshAccountUsage(account: Account): Promise<void> {\n try {\n const data = await getCopilotUsage(account.githubToken)\n const snap = data.quota_snapshots\n\n // eslint-disable-next-line require-atomic-updates\n account.usage = {\n premium_remaining: snap.premium_interactions.remaining,\n premium_total: snap.premium_interactions.entitlement,\n chat_remaining: snap.chat.remaining,\n chat_total: snap.chat.entitlement,\n premium_overage_permitted: snap.premium_interactions.overage_permitted,\n quotaResetDate: data.quota_reset_date,\n lastCheckedAt: Date.now(),\n }\n\n const overagePermitted = snap.premium_interactions.overage_permitted\n\n // Transition between active ↔ exhausted.\n // If the upstream account allows overage (paid extra), do NOT mark it\n // exhausted just because remaining went negative — the user can keep\n // making requests and pay per-use. We only flip back from \"exhausted\"\n // to \"active\" automatically; we never auto-flip into \"exhausted\" when\n // overage is permitted.\n if (\n account.usage.premium_remaining <= 0\n && account.status === \"active\"\n && !overagePermitted\n ) {\n this.markAccountStatus(\n account.id,\n \"exhausted\",\n \"Premium quota exhausted\",\n )\n } else if (\n account.status === \"exhausted\"\n && (account.usage.premium_remaining > 0 || overagePermitted)\n ) {\n account.status = \"active\"\n account.statusMessage = undefined\n account.consecutiveFailures = 0\n this.debouncedSave()\n }\n } catch (err) {\n consola.warn(\n `Account ${account.label}: failed to refresh usage: ${rootCause(err)}`,\n )\n consola.debug(`Account ${account.label}: failed to refresh usage:`, err)\n }\n }\n\n // ---------- Background refresh -----------------------------------------\n\n /** Refresh Copilot tokens for all non-disabled accounts. */\n async refreshAllTokens(): Promise<void> {\n const targets = this.accounts.filter((a) => a.status !== \"disabled\")\n await Promise.allSettled(targets.map((a) => this.refreshAccountToken(a)))\n }\n\n /** Refresh usage snapshots for all non-disabled accounts. */\n async refreshAllUsage(): Promise<void> {\n const targets = this.accounts.filter((a) => a.status !== \"disabled\")\n await Promise.allSettled(targets.map((a) => this.refreshAccountUsage(a)))\n }\n\n /**\n * Start periodic background refresh loops.\n *\n * The initial token refresh is awaited to ensure accounts are ready before\n * the first request arrives. Usage refresh runs in the background.\n *\n * @param tokenIntervalMs Token refresh interval (default 25 min).\n * @param usageIntervalMs Usage refresh interval (default 5 min).\n */\n async startBackgroundRefresh(\n tokenIntervalMs: number = 25 * 60 * 1000,\n usageIntervalMs: number = 5 * 60 * 1000,\n ): Promise<void> {\n this.stopBackgroundRefresh()\n\n // Initial refresh — await token refresh so accounts are ready for requests\n await this.refreshAllTokens()\n void this.refreshAllUsage()\n\n this.refreshInterval = setInterval(() => {\n void this.refreshAllTokens()\n }, tokenIntervalMs)\n\n this.usageInterval = setInterval(() => {\n void this.refreshAllUsage()\n }, usageIntervalMs)\n\n consola.debug(\n `Background refresh started (tokens: ${tokenIntervalMs / 60_000}m, usage: ${usageIntervalMs / 60_000}m)`,\n )\n\n // Start periodic connection pool recycling (~4h with jitter)\n startConnectionRecycling()\n }\n\n stopBackgroundRefresh(): void {\n stopConnectionRecycling()\n if (this.refreshInterval) {\n clearInterval(this.refreshInterval)\n this.refreshInterval = undefined\n }\n if (this.usageInterval) {\n clearInterval(this.usageInterval)\n this.usageInterval = undefined\n }\n }\n\n // ---------- Helpers ----------------------------------------------------\n\n get accountCount(): number {\n return this.accounts.length\n }\n\n get activeAccountCount(): number {\n return this.accounts.filter((a) => a.status === \"active\").length\n }\n\n hasAccounts(): boolean {\n return this.accounts.length > 0\n }\n\n // ---------- Legacy migration -------------------------------------------\n\n /**\n * Create an Account entry from the legacy single-account global state.\n * Useful for seamless upgrade from single-account to multi-account mode.\n *\n * Falls back to creating a minimal entry if API validation fails — the\n * background refresh will fill in the missing data later.\n */\n async migrateFromLegacy(\n githubToken: string,\n accountType: string,\n ): Promise<Account> {\n // Check if this token is already registered\n const existing = this.accounts.find((a) => a.githubToken === githubToken)\n if (existing) {\n consola.debug(\"Legacy account already migrated, skipping\")\n return existing\n }\n\n try {\n const account = await this.addAccount(\n githubToken,\n \"Primary (migrated)\",\n accountType,\n )\n consola.success(\"Legacy single-account migrated to multi-account manager\")\n return account\n } catch (error) {\n // API validation failed — create a minimal entry anyway so the account\n // is visible in the management UI. Background token/usage refresh will\n // fill in the missing data.\n consola.warn(\n \"Could not fully validate legacy account, adding with limited info:\",\n error,\n )\n\n const account: Account = {\n id: randomUUID(),\n label: \"Primary (migrated)\",\n githubToken,\n copilotToken: undefined,\n accountType,\n status: \"active\",\n consecutiveFailures: 0,\n machineId: randomBytes(32).toString(\"hex\"),\n sessionId: randomUUID(),\n addedAt: Date.now(),\n }\n\n this.accounts.push(account)\n await this.saveAccounts()\n return account\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Singleton\n// ---------------------------------------------------------------------------\n\nexport const accountManager = new AccountManager()\n"],"mappings":";;;;;;;;;AAEA,MAAa,yB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AAM,UAAU,MAAM,KAAK,WACzB,aACA,sBACA,YACD;AACD,WAAQ,QAAQ,0DAA0D;AAC1E,UAAO;WACA,OAAO;AAId,WAAQ,KACN,sEACA,MACD;GAED,MAAME,UAAmB;IACvB,IAAI,YAAY;IAChB,OAAO;IACP;IACA,cAAc;IACd;IACA,QAAQ;IACR,qBAAqB;IACrB,WAAW,YAAY,GAAG,CAAC,SAAS,MAAM;IAC1C,WAAW,YAAY;IACvB,SAAS,KAAK,KAAK;IACpB;AAED,QAAK,SAAS,KAAK,QAAQ;AAC3B,SAAM,KAAK,cAAc;AACzB,UAAO;;;;AASb,MAAa,iBAAiB,IAAI,gBAAgB"}
|
package/dist/main.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID, HTTPError, PATHS, accountManager, cacheModels, cacheVSCodeVersion, copilotBaseUrl, copilotHeaders, ensurePaths, findModel, forwardError, getAccountDispatcher, getCopilotUsage, initProxyFromEnv, isAccountProxied, isNullish, isProxyActive, notifyStreamEnd, notifyStreamStart, resetAccountConnections, resetConnections, rootCause, sleep, standardHeaders, state } from "./account-manager-
|
|
3
|
-
import { clearGithubToken, getDeviceCode, pollAccessToken, refreshCopilotToken, setupCopilotToken, setupGitHubToken, stopCopilotTokenRefresh } from "./token-
|
|
2
|
+
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID, HTTPError, PATHS, accountManager, cacheModels, cacheVSCodeVersion, copilotBaseUrl, copilotHeaders, ensurePaths, findModel, forwardError, getAccountDispatcher, getCopilotUsage, initProxyFromEnv, isAccountProxied, isNullish, isProxyActive, notifyStreamEnd, notifyStreamStart, resetAccountConnections, resetConnections, rootCause, sleep, standardHeaders, state } from "./account-manager-DktL5osZ.js";
|
|
3
|
+
import { clearGithubToken, getDeviceCode, pollAccessToken, refreshCopilotToken, setupCopilotToken, setupGitHubToken, stopCopilotTokenRefresh } from "./token-B8FDrdsQ.js";
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import { defineCommand, runMain } from "citty";
|
|
6
6
|
import consola from "consola";
|
|
@@ -4556,7 +4556,7 @@ async function validateGitHubToken(token) {
|
|
|
4556
4556
|
state.githubToken = token;
|
|
4557
4557
|
consola.info("Using provided GitHub token");
|
|
4558
4558
|
try {
|
|
4559
|
-
const { getGitHubUser } = await import("./get-user-
|
|
4559
|
+
const { getGitHubUser } = await import("./get-user-Ct5NqLcM.js");
|
|
4560
4560
|
const user = await getGitHubUser();
|
|
4561
4561
|
consola.info(`Logged in as ${user.login}`);
|
|
4562
4562
|
} catch (error) {
|
|
@@ -4618,10 +4618,10 @@ async function runServer(options$1) {
|
|
|
4618
4618
|
try {
|
|
4619
4619
|
await setupCopilotToken();
|
|
4620
4620
|
} catch (error) {
|
|
4621
|
-
const { HTTPError: HTTPError$1 } = await import("./error-
|
|
4621
|
+
const { HTTPError: HTTPError$1 } = await import("./error-BaXXuCDb.js");
|
|
4622
4622
|
if (error instanceof HTTPError$1 && error.response.status === 401) {
|
|
4623
4623
|
consola.error("Failed to get Copilot token - GitHub token may be invalid or Copilot access revoked");
|
|
4624
|
-
const { clearGithubToken: clearGithubToken$1 } = await import("./token-
|
|
4624
|
+
const { clearGithubToken: clearGithubToken$1 } = await import("./token-DEcUuJp7.js");
|
|
4625
4625
|
await clearGithubToken$1();
|
|
4626
4626
|
consola.info("Please restart to re-authenticate");
|
|
4627
4627
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GITHUB_APP_SCOPES, GITHUB_BASE_URL, GITHUB_CLIENT_ID, HTTPError, PATHS, getCopilotToken, getGitHubUser, rootCause, sleep, standardHeaders, state } from "./account-manager-
|
|
1
|
+
import { GITHUB_APP_SCOPES, GITHUB_BASE_URL, GITHUB_CLIENT_ID, HTTPError, PATHS, getCopilotToken, getGitHubUser, rootCause, sleep, standardHeaders, state } from "./account-manager-DktL5osZ.js";
|
|
2
2
|
import consola from "consola";
|
|
3
3
|
import fs from "node:fs/promises";
|
|
4
4
|
|
|
@@ -180,4 +180,4 @@ async function logUser() {
|
|
|
180
180
|
|
|
181
181
|
//#endregion
|
|
182
182
|
export { clearGithubToken, getDeviceCode, pollAccessToken, refreshCopilotToken, setupCopilotToken, setupGitHubToken, stopCopilotTokenRefresh };
|
|
183
|
-
//# sourceMappingURL=token-
|
|
183
|
+
//# sourceMappingURL=token-B8FDrdsQ.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token-DYGcLmSO.js","names":["copilotTokenRefreshTimer: ReturnType<typeof setInterval> | undefined"],"sources":["../src/services/github/get-device-code.ts","../src/services/github/poll-access-token.ts","../src/lib/token.ts"],"sourcesContent":["import {\n GITHUB_APP_SCOPES,\n GITHUB_BASE_URL,\n GITHUB_CLIENT_ID,\n standardHeaders,\n} from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\n\nexport async function getDeviceCode(): Promise<DeviceCodeResponse> {\n const response = await fetch(`${GITHUB_BASE_URL}/login/device/code`, {\n method: \"POST\",\n headers: standardHeaders(),\n body: JSON.stringify({\n client_id: GITHUB_CLIENT_ID,\n scope: GITHUB_APP_SCOPES,\n }),\n })\n\n if (!response.ok) throw new HTTPError(\"Failed to get device code\", response)\n\n return (await response.json()) as DeviceCodeResponse\n}\n\nexport interface DeviceCodeResponse {\n device_code: string\n user_code: string\n verification_uri: string\n expires_in: number\n interval: number\n}\n","import consola from \"consola\"\n\nimport {\n GITHUB_BASE_URL,\n GITHUB_CLIENT_ID,\n standardHeaders,\n} from \"~/lib/api-config\"\nimport { sleep } from \"~/lib/utils\"\n\nimport type { DeviceCodeResponse } from \"./get-device-code\"\n\nexport async function pollAccessToken(\n deviceCode: DeviceCodeResponse,\n): Promise<string> {\n // Interval is in seconds, we need to multiply by 1000 to get milliseconds\n // I'm also adding another second, just to be safe\n const sleepDuration = (deviceCode.interval + 1) * 1000\n consola.debug(`Polling access token with interval of ${sleepDuration}ms`)\n\n // Calculate expiration time - device code expires after expires_in seconds\n const expirationTime = Date.now() + deviceCode.expires_in * 1000\n\n while (Date.now() < expirationTime) {\n const response = await fetch(\n `${GITHUB_BASE_URL}/login/oauth/access_token`,\n {\n method: \"POST\",\n headers: standardHeaders(),\n body: JSON.stringify({\n client_id: GITHUB_CLIENT_ID,\n device_code: deviceCode.device_code,\n grant_type: \"urn:ietf:params:oauth:grant-type:device_code\",\n }),\n },\n )\n\n if (!response.ok) {\n await sleep(sleepDuration)\n consola.error(\"Failed to poll access token:\", await response.text())\n\n continue\n }\n\n const json = (await response.json()) as\n | AccessTokenResponse\n | AccessTokenErrorResponse\n consola.debug(\"Polling access token response:\", json)\n\n if (\"access_token\" in json && json.access_token) {\n return json.access_token\n }\n\n // Handle specific error cases\n if (\"error\" in json) {\n switch (json.error) {\n case \"authorization_pending\": {\n // User hasn't authorized yet, continue polling\n await sleep(sleepDuration)\n continue\n\n break\n }\n case \"slow_down\": {\n // We're polling too fast, increase interval\n await sleep(sleepDuration * 2)\n continue\n\n break\n }\n case \"expired_token\": {\n throw new Error(\"Device code expired. Please try again.\")\n }\n case \"access_denied\": {\n throw new Error(\"Authorization was denied by the user.\")\n }\n default: {\n throw new Error(\n `Authentication failed: ${json.error_description || json.error}`,\n )\n }\n }\n }\n\n await sleep(sleepDuration)\n }\n\n throw new Error(\"Device code expired. Please try again.\")\n}\n\ninterface AccessTokenResponse {\n access_token: string\n token_type: string\n scope: string\n}\n\ninterface AccessTokenErrorResponse {\n error: string\n error_description?: string\n error_uri?: string\n}\n","import consola from \"consola\"\nimport fs from \"node:fs/promises\"\n\nimport { PATHS } from \"~/lib/paths\"\nimport { getCopilotToken } from \"~/services/github/get-copilot-token\"\nimport { getDeviceCode } from \"~/services/github/get-device-code\"\nimport { getGitHubUser } from \"~/services/github/get-user\"\nimport { pollAccessToken } from \"~/services/github/poll-access-token\"\n\nimport { HTTPError } from \"./error\"\nimport { state } from \"./state\"\nimport { rootCause } from \"./utils\"\n\n/** Check if an error is a transient network/connection error */\nfunction isNetworkError(error: unknown): boolean {\n if (!(error instanceof Error)) return false\n const cause = (error as { cause?: { code?: string } }).cause\n const code = cause?.code ?? (error as { code?: string }).code\n return [\n \"ECONNREFUSED\",\n \"ECONNRESET\",\n \"ENOTFOUND\",\n \"ETIMEDOUT\",\n \"UND_ERR_CONNECT_TIMEOUT\",\n \"UND_ERR_SOCKET\",\n ].includes(code ?? \"\")\n}\n\nconst readGithubToken = () => fs.readFile(PATHS.GITHUB_TOKEN_PATH, \"utf8\")\n\nconst writeGithubToken = (token: string) =>\n fs.writeFile(PATHS.GITHUB_TOKEN_PATH, token, { mode: 0o600 })\n\n/**\n * Clear the stored GitHub token from disk and state.\n * This allows the user to logout or re-authenticate.\n */\nexport async function clearGithubToken(): Promise<void> {\n state.githubToken = undefined\n state.copilotToken = undefined\n await fs.writeFile(PATHS.GITHUB_TOKEN_PATH, \"\")\n consola.info(\"GitHub token cleared\")\n}\n\n/** Handle to the single-account Copilot token refresh interval. */\nlet copilotTokenRefreshTimer: ReturnType<typeof setInterval> | undefined\n\n/**\n * Stop the single-account Copilot token refresh interval.\n * Called when multi-account mode is activated to avoid duplicate refreshes.\n */\nexport function stopCopilotTokenRefresh(): void {\n if (copilotTokenRefreshTimer) {\n clearInterval(copilotTokenRefreshTimer)\n copilotTokenRefreshTimer = undefined\n consola.debug(\n \"Single-account Copilot token refresh stopped (multi-account active)\",\n )\n }\n}\n\nexport const setupCopilotToken = async () => {\n const { token, refresh_in } = await getCopilotToken()\n state.copilotToken = token\n\n // Display the Copilot token to the screen\n consola.debug(\"GitHub Copilot Token fetched successfully!\")\n if (state.showToken) {\n consola.info(\"Copilot token:\", token)\n }\n\n const refreshInterval = Math.max((refresh_in - 60) * 1000, 60_000)\n copilotTokenRefreshTimer = setInterval(async () => {\n consola.debug(\"Refreshing Copilot token\")\n try {\n await refreshCopilotToken()\n } catch (error) {\n consola.warn(`Failed to refresh Copilot token: ${rootCause(error)}`)\n consola.debug(\"Failed to refresh Copilot token:\", error)\n\n // If we get a 401, the GitHub token might be invalid\n // Log the error but don't crash - the next API request will fail\n // and the user can restart with valid credentials\n if (error instanceof HTTPError && error.response.status === 401) {\n consola.warn(\n \"GitHub token may have been revoked. Please restart and re-authenticate.\",\n )\n state.copilotToken = undefined\n }\n // Don't throw here - it would cause an unhandled rejection in setInterval\n }\n }, refreshInterval)\n}\n\n/**\n * Refresh the Copilot token on demand (e.g. after a 401 error).\n */\nexport async function refreshCopilotToken(): Promise<void> {\n const { token } = await getCopilotToken()\n state.copilotToken = token\n consola.debug(\"Copilot token refreshed\")\n if (state.showToken) {\n consola.info(\"Refreshed Copilot token:\", token)\n }\n}\n\ninterface SetupGitHubTokenOptions {\n force?: boolean\n}\n\n/**\n * Perform a fresh GitHub authentication flow.\n * Gets a device code and polls for the access token.\n */\nasync function performFreshAuthentication(): Promise<void> {\n consola.info(\"Starting GitHub authentication flow...\")\n const response = await getDeviceCode()\n consola.debug(\"Device code response:\", response)\n\n consola.info(\n `Please enter the code \"${response.user_code}\" in ${response.verification_uri}`,\n )\n\n const token = await pollAccessToken(response)\n await writeGithubToken(token)\n state.githubToken = token\n\n if (state.showToken) {\n consola.info(\"GitHub token:\", token)\n }\n await logUser()\n}\n\nexport async function setupGitHubToken(\n options?: SetupGitHubTokenOptions,\n): Promise<void> {\n try {\n const githubToken = await readGithubToken()\n\n if (githubToken && !options?.force) {\n state.githubToken = githubToken\n if (state.showToken) {\n consola.info(\"GitHub token:\", githubToken)\n }\n\n // Validate the token by checking if we can get the user\n try {\n await logUser()\n return\n } catch (error) {\n // Token is invalid or expired, clear it and re-authenticate\n if (error instanceof HTTPError && error.response.status === 401) {\n consola.warn(\n \"Stored GitHub token is invalid or expired, clearing and re-authenticating...\",\n )\n await clearGithubToken()\n // Fall through to perform fresh authentication\n } else if (isNetworkError(error)) {\n // Network error — token is on disk and might still be valid,\n // we just can't verify right now. Continue without throwing.\n consola.warn(\n `Could not verify GitHub token (network issue): ${rootCause(error)}`,\n )\n consola.debug(\"Network error during token validation:\", error)\n return\n } else {\n throw error\n }\n }\n }\n\n consola.info(\"Not logged in, getting new access token\")\n await performFreshAuthentication()\n } catch (error) {\n if (error instanceof HTTPError) {\n consola.error(\"Failed to get GitHub token:\", await error.response.json())\n throw error\n }\n\n consola.warn(`Failed to get GitHub token: ${rootCause(error)}`)\n consola.debug(\"Failed to get GitHub token:\", error)\n\n // Network errors are non-fatal — the server can still try to start\n // with whatever token is on disk. It will fail on first API call if\n // the token is actually missing/invalid.\n if (isNetworkError(error)) {\n return\n }\n\n throw error\n }\n}\n\nasync function logUser() {\n const user = await getGitHubUser()\n consola.info(`Logged in as ${user.login}`)\n}\n"],"mappings":";;;;;AAQA,eAAsB,gBAA6C;CACjE,MAAM,WAAW,MAAM,MAAM,GAAG,gBAAgB,qBAAqB;EACnE,QAAQ;EACR,SAAS,iBAAiB;EAC1B,MAAM,KAAK,UAAU;GACnB,WAAW;GACX,OAAO;GACR,CAAC;EACH,CAAC;AAEF,KAAI,CAAC,SAAS,GAAI,OAAM,IAAI,UAAU,6BAA6B,SAAS;AAE5E,QAAQ,MAAM,SAAS,MAAM;;;;;ACT/B,eAAsB,gBACpB,YACiB;CAGjB,MAAM,iBAAiB,WAAW,WAAW,KAAK;AAClD,SAAQ,MAAM,yCAAyC,cAAc,IAAI;CAGzE,MAAM,iBAAiB,KAAK,KAAK,GAAG,WAAW,aAAa;AAE5D,QAAO,KAAK,KAAK,GAAG,gBAAgB;EAClC,MAAM,WAAW,MAAM,MACrB,GAAG,gBAAgB,4BACnB;GACE,QAAQ;GACR,SAAS,iBAAiB;GAC1B,MAAM,KAAK,UAAU;IACnB,WAAW;IACX,aAAa,WAAW;IACxB,YAAY;IACb,CAAC;GACH,CACF;AAED,MAAI,CAAC,SAAS,IAAI;AAChB,SAAM,MAAM,cAAc;AAC1B,WAAQ,MAAM,gCAAgC,MAAM,SAAS,MAAM,CAAC;AAEpE;;EAGF,MAAM,OAAQ,MAAM,SAAS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0C,MAAM;AAC9D;UAEA,OAAM;;;AAKZ,UAAQ,KAAK,0CAA0C;AACvD,QAAM,4BAA4B;UAC3B,OAAO;AACd,MAAI,iBAAiB,WAAW;AAC9B,WAAQ,MAAM,+BAA+B,MAAM,MAAM,SAAS,MAAM,CAAC;AACzE,SAAM;;AAGR,UAAQ,KAAK,+BAA+B,UAAU,MAAM,GAAG;AAC/D,UAAQ,MAAM,+BAA+B,MAAM;AAKnD,MAAI,eAAe,MAAM,CACvB;AAGF,QAAM;;;AAIV,eAAe,UAAU;CACvB,MAAM,OAAO,MAAM,eAAe;AAClC,SAAQ,KAAK,gBAAgB,KAAK,QAAQ"}
|
|
1
|
+
{"version":3,"file":"token-B8FDrdsQ.js","names":["copilotTokenRefreshTimer: ReturnType<typeof setInterval> | undefined"],"sources":["../src/services/github/get-device-code.ts","../src/services/github/poll-access-token.ts","../src/lib/token.ts"],"sourcesContent":["import {\n GITHUB_APP_SCOPES,\n GITHUB_BASE_URL,\n GITHUB_CLIENT_ID,\n standardHeaders,\n} from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\n\nexport async function getDeviceCode(): Promise<DeviceCodeResponse> {\n const response = await fetch(`${GITHUB_BASE_URL}/login/device/code`, {\n method: \"POST\",\n headers: standardHeaders(),\n body: JSON.stringify({\n client_id: GITHUB_CLIENT_ID,\n scope: GITHUB_APP_SCOPES,\n }),\n })\n\n if (!response.ok) throw new HTTPError(\"Failed to get device code\", response)\n\n return (await response.json()) as DeviceCodeResponse\n}\n\nexport interface DeviceCodeResponse {\n device_code: string\n user_code: string\n verification_uri: string\n expires_in: number\n interval: number\n}\n","import consola from \"consola\"\n\nimport {\n GITHUB_BASE_URL,\n GITHUB_CLIENT_ID,\n standardHeaders,\n} from \"~/lib/api-config\"\nimport { sleep } from \"~/lib/utils\"\n\nimport type { DeviceCodeResponse } from \"./get-device-code\"\n\nexport async function pollAccessToken(\n deviceCode: DeviceCodeResponse,\n): Promise<string> {\n // Interval is in seconds, we need to multiply by 1000 to get milliseconds\n // I'm also adding another second, just to be safe\n const sleepDuration = (deviceCode.interval + 1) * 1000\n consola.debug(`Polling access token with interval of ${sleepDuration}ms`)\n\n // Calculate expiration time - device code expires after expires_in seconds\n const expirationTime = Date.now() + deviceCode.expires_in * 1000\n\n while (Date.now() < expirationTime) {\n const response = await fetch(\n `${GITHUB_BASE_URL}/login/oauth/access_token`,\n {\n method: \"POST\",\n headers: standardHeaders(),\n body: JSON.stringify({\n client_id: GITHUB_CLIENT_ID,\n device_code: deviceCode.device_code,\n grant_type: \"urn:ietf:params:oauth:grant-type:device_code\",\n }),\n },\n )\n\n if (!response.ok) {\n await sleep(sleepDuration)\n consola.error(\"Failed to poll access token:\", await response.text())\n\n continue\n }\n\n const json = (await response.json()) as\n | AccessTokenResponse\n | AccessTokenErrorResponse\n consola.debug(\"Polling access token response:\", json)\n\n if (\"access_token\" in json && json.access_token) {\n return json.access_token\n }\n\n // Handle specific error cases\n if (\"error\" in json) {\n switch (json.error) {\n case \"authorization_pending\": {\n // User hasn't authorized yet, continue polling\n await sleep(sleepDuration)\n continue\n\n break\n }\n case \"slow_down\": {\n // We're polling too fast, increase interval\n await sleep(sleepDuration * 2)\n continue\n\n break\n }\n case \"expired_token\": {\n throw new Error(\"Device code expired. Please try again.\")\n }\n case \"access_denied\": {\n throw new Error(\"Authorization was denied by the user.\")\n }\n default: {\n throw new Error(\n `Authentication failed: ${json.error_description || json.error}`,\n )\n }\n }\n }\n\n await sleep(sleepDuration)\n }\n\n throw new Error(\"Device code expired. Please try again.\")\n}\n\ninterface AccessTokenResponse {\n access_token: string\n token_type: string\n scope: string\n}\n\ninterface AccessTokenErrorResponse {\n error: string\n error_description?: string\n error_uri?: string\n}\n","import consola from \"consola\"\nimport fs from \"node:fs/promises\"\n\nimport { PATHS } from \"~/lib/paths\"\nimport { getCopilotToken } from \"~/services/github/get-copilot-token\"\nimport { getDeviceCode } from \"~/services/github/get-device-code\"\nimport { getGitHubUser } from \"~/services/github/get-user\"\nimport { pollAccessToken } from \"~/services/github/poll-access-token\"\n\nimport { HTTPError } from \"./error\"\nimport { state } from \"./state\"\nimport { rootCause } from \"./utils\"\n\n/** Check if an error is a transient network/connection error */\nfunction isNetworkError(error: unknown): boolean {\n if (!(error instanceof Error)) return false\n const cause = (error as { cause?: { code?: string } }).cause\n const code = cause?.code ?? (error as { code?: string }).code\n return [\n \"ECONNREFUSED\",\n \"ECONNRESET\",\n \"ENOTFOUND\",\n \"ETIMEDOUT\",\n \"UND_ERR_CONNECT_TIMEOUT\",\n \"UND_ERR_SOCKET\",\n ].includes(code ?? \"\")\n}\n\nconst readGithubToken = () => fs.readFile(PATHS.GITHUB_TOKEN_PATH, \"utf8\")\n\nconst writeGithubToken = (token: string) =>\n fs.writeFile(PATHS.GITHUB_TOKEN_PATH, token, { mode: 0o600 })\n\n/**\n * Clear the stored GitHub token from disk and state.\n * This allows the user to logout or re-authenticate.\n */\nexport async function clearGithubToken(): Promise<void> {\n state.githubToken = undefined\n state.copilotToken = undefined\n await fs.writeFile(PATHS.GITHUB_TOKEN_PATH, \"\")\n consola.info(\"GitHub token cleared\")\n}\n\n/** Handle to the single-account Copilot token refresh interval. */\nlet copilotTokenRefreshTimer: ReturnType<typeof setInterval> | undefined\n\n/**\n * Stop the single-account Copilot token refresh interval.\n * Called when multi-account mode is activated to avoid duplicate refreshes.\n */\nexport function stopCopilotTokenRefresh(): void {\n if (copilotTokenRefreshTimer) {\n clearInterval(copilotTokenRefreshTimer)\n copilotTokenRefreshTimer = undefined\n consola.debug(\n \"Single-account Copilot token refresh stopped (multi-account active)\",\n )\n }\n}\n\nexport const setupCopilotToken = async () => {\n const { token, refresh_in } = await getCopilotToken()\n state.copilotToken = token\n\n // Display the Copilot token to the screen\n consola.debug(\"GitHub Copilot Token fetched successfully!\")\n if (state.showToken) {\n consola.info(\"Copilot token:\", token)\n }\n\n const refreshInterval = Math.max((refresh_in - 60) * 1000, 60_000)\n copilotTokenRefreshTimer = setInterval(async () => {\n consola.debug(\"Refreshing Copilot token\")\n try {\n await refreshCopilotToken()\n } catch (error) {\n consola.warn(`Failed to refresh Copilot token: ${rootCause(error)}`)\n consola.debug(\"Failed to refresh Copilot token:\", error)\n\n // If we get a 401, the GitHub token might be invalid\n // Log the error but don't crash - the next API request will fail\n // and the user can restart with valid credentials\n if (error instanceof HTTPError && error.response.status === 401) {\n consola.warn(\n \"GitHub token may have been revoked. Please restart and re-authenticate.\",\n )\n state.copilotToken = undefined\n }\n // Don't throw here - it would cause an unhandled rejection in setInterval\n }\n }, refreshInterval)\n}\n\n/**\n * Refresh the Copilot token on demand (e.g. after a 401 error).\n */\nexport async function refreshCopilotToken(): Promise<void> {\n const { token } = await getCopilotToken()\n state.copilotToken = token\n consola.debug(\"Copilot token refreshed\")\n if (state.showToken) {\n consola.info(\"Refreshed Copilot token:\", token)\n }\n}\n\ninterface SetupGitHubTokenOptions {\n force?: boolean\n}\n\n/**\n * Perform a fresh GitHub authentication flow.\n * Gets a device code and polls for the access token.\n */\nasync function performFreshAuthentication(): Promise<void> {\n consola.info(\"Starting GitHub authentication flow...\")\n const response = await getDeviceCode()\n consola.debug(\"Device code response:\", response)\n\n consola.info(\n `Please enter the code \"${response.user_code}\" in ${response.verification_uri}`,\n )\n\n const token = await pollAccessToken(response)\n await writeGithubToken(token)\n state.githubToken = token\n\n if (state.showToken) {\n consola.info(\"GitHub token:\", token)\n }\n await logUser()\n}\n\nexport async function setupGitHubToken(\n options?: SetupGitHubTokenOptions,\n): Promise<void> {\n try {\n const githubToken = await readGithubToken()\n\n if (githubToken && !options?.force) {\n state.githubToken = githubToken\n if (state.showToken) {\n consola.info(\"GitHub token:\", githubToken)\n }\n\n // Validate the token by checking if we can get the user\n try {\n await logUser()\n return\n } catch (error) {\n // Token is invalid or expired, clear it and re-authenticate\n if (error instanceof HTTPError && error.response.status === 401) {\n consola.warn(\n \"Stored GitHub token is invalid or expired, clearing and re-authenticating...\",\n )\n await clearGithubToken()\n // Fall through to perform fresh authentication\n } else if (isNetworkError(error)) {\n // Network error — token is on disk and might still be valid,\n // we just can't verify right now. Continue without throwing.\n consola.warn(\n `Could not verify GitHub token (network issue): ${rootCause(error)}`,\n )\n consola.debug(\"Network error during token validation:\", error)\n return\n } else {\n throw error\n }\n }\n }\n\n consola.info(\"Not logged in, getting new access token\")\n await performFreshAuthentication()\n } catch (error) {\n if (error instanceof HTTPError) {\n consola.error(\"Failed to get GitHub token:\", await error.response.json())\n throw error\n }\n\n consola.warn(`Failed to get GitHub token: ${rootCause(error)}`)\n consola.debug(\"Failed to get GitHub token:\", error)\n\n // Network errors are non-fatal — the server can still try to start\n // with whatever token is on disk. It will fail on first API call if\n // the token is actually missing/invalid.\n if (isNetworkError(error)) {\n return\n }\n\n throw error\n }\n}\n\nasync function logUser() {\n const user = await getGitHubUser()\n consola.info(`Logged in as ${user.login}`)\n}\n"],"mappings":";;;;;AAQA,eAAsB,gBAA6C;CACjE,MAAM,WAAW,MAAM,MAAM,GAAG,gBAAgB,qBAAqB;EACnE,QAAQ;EACR,SAAS,iBAAiB;EAC1B,MAAM,KAAK,UAAU;GACnB,WAAW;GACX,OAAO;GACR,CAAC;EACH,CAAC;AAEF,KAAI,CAAC,SAAS,GAAI,OAAM,IAAI,UAAU,6BAA6B,SAAS;AAE5E,QAAQ,MAAM,SAAS,MAAM;;;;;ACT/B,eAAsB,gBACpB,YACiB;CAGjB,MAAM,iBAAiB,WAAW,WAAW,KAAK;AAClD,SAAQ,MAAM,yCAAyC,cAAc,IAAI;CAGzE,MAAM,iBAAiB,KAAK,KAAK,GAAG,WAAW,aAAa;AAE5D,QAAO,KAAK,KAAK,GAAG,gBAAgB;EAClC,MAAM,WAAW,MAAM,MACrB,GAAG,gBAAgB,4BACnB;GACE,QAAQ;GACR,SAAS,iBAAiB;GAC1B,MAAM,KAAK,UAAU;IACnB,WAAW;IACX,aAAa,WAAW;IACxB,YAAY;IACb,CAAC;GACH,CACF;AAED,MAAI,CAAC,SAAS,IAAI;AAChB,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D,WAAM,kBAAkB;eAEf,eAAe,MAAM,EAAE;AAGhC,aAAQ,KACN,kDAAkD,UAAU,MAAM,GACnE;AACD,aAAQ,MAAM,0CAA0C,MAAM;AAC9D;UAEA,OAAM;;;AAKZ,UAAQ,KAAK,0CAA0C;AACvD,QAAM,4BAA4B;UAC3B,OAAO;AACd,MAAI,iBAAiB,WAAW;AAC9B,WAAQ,MAAM,+BAA+B,MAAM,MAAM,SAAS,MAAM,CAAC;AACzE,SAAM;;AAGR,UAAQ,KAAK,+BAA+B,UAAU,MAAM,GAAG;AAC/D,UAAQ,MAAM,+BAA+B,MAAM;AAKnD,MAAI,eAAe,MAAM,CACvB;AAGF,QAAM;;;AAIV,eAAe,UAAU;CACvB,MAAM,OAAO,MAAM,eAAe;AAClC,SAAQ,KAAK,gBAAgB,KAAK,QAAQ"}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"account-manager-D4DftPxS.js","names":["headers: Record<string, string>","state: State","source: TokenSource","errorText: string","errorJson: unknown","ACCOUNTS_PATH","direct: Agent | undefined","keepaliveTimer: ReturnType<typeof setInterval> | undefined","proxyUrl: string | undefined","connectionRecycleTimer: ReturnType<typeof setInterval> | undefined","fetchOptions: RequestInit","lastError: unknown","response: Response | undefined","error: unknown","err: unknown","data: Array<PersistedAccount>","account: Account"],"sources":["../src/lib/api-config.ts","../src/lib/state.ts","../src/services/copilot/get-models.ts","../src/services/get-vscode-version.ts","../src/lib/utils.ts","../src/lib/error.ts","../src/lib/paths.ts","../src/lib/proxy.ts","../src/services/github/get-copilot-token.ts","../src/services/github/get-copilot-usage.ts","../src/services/github/get-user.ts","../src/lib/account-manager.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\"\n\nexport const standardHeaders = () => ({\n \"content-type\": \"application/json\",\n accept: \"application/json\",\n})\n\nconst COPILOT_VERSION = \"0.38.2\"\nconst EDITOR_PLUGIN_VERSION = `copilot-chat/${COPILOT_VERSION}`\nconst USER_AGENT = `GitHubCopilotChat/${COPILOT_VERSION}`\n\n// Updated to match latest Zed implementation - 2025-10-01 returns Claude models\nconst API_VERSION = \"2025-10-01\"\n\n/**\n * Common interface for anything that can supply Copilot/GitHub credentials.\n *\n * Both `State` and `Account` satisfy this interface, so all header/URL\n * helpers can accept either without an explicit overload.\n */\nexport interface TokenSource {\n copilotToken?: string\n copilotApiEndpoint?: string\n accountType: string\n githubToken?: string\n vsCodeVersion?: string\n machineId?: string\n sessionId?: string\n proxy?: string\n}\n\n// Re-export constants used by other modules for building headers manually\nexport { API_VERSION, EDITOR_PLUGIN_VERSION, USER_AGENT }\n\n// Use the API endpoint from token response if available, otherwise fall back to default\nexport const copilotBaseUrl = (source: TokenSource) => {\n if (source.copilotApiEndpoint) {\n return source.copilotApiEndpoint\n }\n return source.accountType === \"individual\" ?\n \"https://api.githubcopilot.com\"\n : `https://api.${source.accountType}.githubcopilot.com`\n}\nexport const copilotHeaders = (\n source: TokenSource,\n vision: boolean = false,\n) => {\n const headers: Record<string, string> = {\n Authorization: `Bearer ${source.copilotToken}`,\n \"content-type\": standardHeaders()[\"content-type\"],\n \"copilot-integration-id\": \"vscode-chat\",\n \"editor-version\": `vscode/${source.vsCodeVersion}`,\n \"editor-plugin-version\": EDITOR_PLUGIN_VERSION,\n \"user-agent\": USER_AGENT,\n \"openai-intent\": \"conversation-agent\",\n \"x-interaction-type\": \"conversation-agent\",\n \"x-agent-task-id\": randomUUID(),\n \"x-github-api-version\": API_VERSION,\n \"x-request-id\": randomUUID(),\n \"x-vscode-user-agent-library-version\": \"electron-fetch\",\n // Anti-correlation: per-account device identifiers\n ...(source.machineId && { \"vscode-machineid\": source.machineId }),\n ...(source.sessionId && { \"vscode-sessionid\": source.sessionId }),\n }\n\n if (vision) headers[\"copilot-vision-request\"] = \"true\"\n\n return headers\n}\n\nexport const GITHUB_API_BASE_URL = \"https://api.github.com\"\nexport const githubHeaders = (source: TokenSource) => ({\n ...standardHeaders(),\n authorization: `token ${source.githubToken}`,\n \"editor-version\": `vscode/${source.vsCodeVersion}`,\n \"editor-plugin-version\": EDITOR_PLUGIN_VERSION,\n \"user-agent\": USER_AGENT,\n \"x-github-api-version\": API_VERSION,\n \"x-vscode-user-agent-library-version\": \"electron-fetch\",\n})\n\nexport const GITHUB_BASE_URL = \"https://github.com\"\nexport const GITHUB_CLIENT_ID = \"Iv1.b507a08c87ecfe98\"\nexport const GITHUB_APP_SCOPES = [\"read:user\"].join(\" \")\n","import type { ModelsResponse } from \"~/services/copilot/get-models\"\n\nexport interface State {\n githubToken?: string\n copilotToken?: string\n copilotApiEndpoint?: string // API endpoint returned by token response\n\n accountType: string\n models?: ModelsResponse\n vsCodeVersion?: string\n\n manualApprove: boolean\n rateLimitWait: boolean\n showToken: boolean\n\n // Rate limiting configuration\n rateLimitSeconds?: number\n lastRequestTimestamp?: number\n\n // API key authentication\n apiKeys?: Array<string>\n\n // Multi-account mode\n multiAccountEnabled: boolean\n\n /**\n * When true, Anthropic /v1/messages requests are NEVER routed through the\n * native Copilot /v1/messages endpoint — the legacy translation layer\n * (Anthropic → OpenAI chat-completions → Anthropic) is used instead.\n * Default: false (native passthrough enabled by capability).\n */\n disableAnthropicPassthrough: boolean\n\n /**\n * When true, requests that don't specify a `thinking` field will get the\n * model's maximum thinking budget injected automatically (adaptive for\n * adaptive-thinking models, otherwise `enabled` with `max_thinking_budget`).\n *\n * Trade-off:\n * - Pre-2026-06-01 Copilot billing is per-request (token count doesn't\n * change cost), so leaving this on is \"free quality\".\n * - Post-2026-06-01 billing switches to per-token, at which point\n * auto-injection burns tokens. Users on token billing should turn this\n * off (or upgrade to a release that flips the default).\n *\n * Default: true (preserve existing v1.3.x quality-first behavior).\n */\n maxThinking: boolean\n\n // Selected models (from --claude-code setup)\n selectedModel?: string\n selectedSmallModel?: string\n}\n\nexport const state: State = {\n accountType: \"individual\",\n manualApprove: false,\n rateLimitWait: false,\n showToken: false,\n multiAccountEnabled: false,\n disableAnthropicPassthrough: false,\n maxThinking: true,\n}\n","import { accountManager } from \"~/lib/account-manager\"\nimport {\n copilotBaseUrl,\n copilotHeaders,\n type TokenSource,\n} from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\nexport const getModels = async () => {\n // In multi-account mode, use the active account's token\n let source: TokenSource = state\n if (state.multiAccountEnabled && accountManager.hasAccounts()) {\n const account = accountManager.getActiveAccount()\n if (account?.copilotToken) {\n source = {\n copilotToken: account.copilotToken,\n copilotApiEndpoint: account.copilotApiEndpoint,\n accountType: account.accountType,\n githubToken: account.githubToken,\n vsCodeVersion: state.vsCodeVersion,\n }\n }\n }\n\n const url = `${copilotBaseUrl(source)}/models`\n\n const response = await fetch(url, {\n headers: copilotHeaders(source),\n })\n\n if (!response.ok) throw new HTTPError(\"Failed to get models\", response)\n\n const data = (await response.json()) as ModelsResponse\n\n return data\n}\n\nexport interface ModelsResponse {\n data: Array<Model>\n object: string\n}\n\ninterface ModelLimits {\n max_context_window_tokens?: number\n max_output_tokens?: number\n max_prompt_tokens?: number\n max_inputs?: number\n}\n\ninterface ModelSupports {\n tool_calls?: boolean\n parallel_tool_calls?: boolean\n dimensions?: boolean\n // Thinking/reasoning capabilities\n max_thinking_budget?: number\n min_thinking_budget?: number\n adaptive_thinking?: boolean\n /**\n * Per-model whitelist of `output_config.effort` values that Copilot's\n * `/v1/messages` mirror will accept for adaptive-thinking models.\n * As of 2026-05, Copilot caps Opus 4.7 to `[\"medium\"]` and allows\n * Sonnet 4.6 `[\"low\",\"medium\",\"high\"]`. Sending anything outside\n * this list returns 400 \"not supported by model X; supported values: [...]\".\n */\n reasoning_effort?: Array<string>\n}\n\ninterface ModelCapabilities {\n family: string\n limits: ModelLimits\n object: string\n supports: ModelSupports\n tokenizer: string\n type: string\n}\n\nexport interface Model {\n capabilities: ModelCapabilities\n id: string\n model_picker_enabled: boolean\n name: string\n object: string\n preview: boolean\n vendor: string\n version: string\n policy?: {\n state: string\n terms: string\n }\n billing?: {\n is_premium: boolean\n multiplier: number\n restricted_to?: Array<string>\n }\n /**\n * Optional list of native upstream endpoints this model supports.\n * GitHub Copilot's `/models` response advertises this for some models\n * (e.g. Claude family typically includes \"anthropic-messages\") so we\n * can route directly to the native API instead of translating.\n */\n supported_endpoints?: Array<string>\n}\n","const FALLBACK = \"1.104.3\"\n\nexport async function getVSCodeVersion() {\n const controller = new AbortController()\n const timeout = setTimeout(() => {\n controller.abort()\n }, 5000)\n\n try {\n const response = await fetch(\n \"https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=visual-studio-code-bin\",\n {\n signal: controller.signal,\n },\n )\n\n const pkgbuild = await response.text()\n const pkgverRegex = /pkgver=([0-9.]+)/\n const match = pkgbuild.match(pkgverRegex)\n\n if (match) {\n return match[1]\n }\n\n return FALLBACK\n } catch {\n return FALLBACK\n } finally {\n clearTimeout(timeout)\n }\n}\n","import consola from \"consola\"\n\nimport type { Model } from \"~/services/copilot/get-models\"\n\nimport { getModels } from \"~/services/copilot/get-models\"\nimport { getVSCodeVersion } from \"~/services/get-vscode-version\"\n\nimport { state } from \"./state\"\n\nexport const sleep = (ms: number) =>\n new Promise((resolve) => {\n setTimeout(resolve, ms)\n })\n\n/**\n * Extract the root-cause message from an unknown thrown value.\n *\n * If the error wraps another error via the standard `cause` property, the\n * inner message is returned instead — giving a one-line summary of *why*\n * the operation failed without the noise of the full stack trace.\n */\nexport function rootCause(err: unknown): string {\n if (err instanceof Error) {\n return err.cause instanceof Error ? err.cause.message : err.message\n }\n return String(err)\n}\n\nexport const isNullish = (value: unknown): value is null | undefined =>\n value === null || value === undefined\n\nexport async function cacheModels(): Promise<void> {\n const models = await getModels()\n state.models = models\n}\n\nexport const cacheVSCodeVersion = async () => {\n const response = await getVSCodeVersion()\n state.vsCodeVersion = response\n\n consola.info(`Using VSCode version: ${response}`)\n}\n\n/**\n * Find a model in state.models using multi-strategy exact matching.\n *\n * Strategies (in order):\n * 1. Exact match on model ID\n * 2. Strip date suffix (claude-opus-4-6-20251101 → claude-opus-4-6)\n * 3. Dash to dot version (claude-opus-4-5 → claude-opus-4.5)\n * 4. Dot to dash version (claude-opus-4.5 → claude-opus-4-5)\n *\n * No fuzzy/family matching — all strategies produce deterministic exact IDs.\n */\nexport function findModel(modelName: string): Model | undefined {\n const models = state.models?.data\n if (!models || models.length === 0) {\n return undefined\n }\n\n // 1. Exact match\n const exact = models.find((m) => m.id === modelName)\n if (exact) return exact\n\n // 2. Strip date suffix\n const base = modelName.replace(/-\\d{8}$/, \"\")\n if (base !== modelName) {\n const baseMatch = models.find((m) => m.id === base)\n if (baseMatch) return baseMatch\n }\n\n // 3. Dash to dot version (4-5 → 4.5)\n const withDot = base.replace(/-(\\d+)-(\\d+)$/, \"-$1.$2\")\n if (withDot !== base) {\n const dotMatch = models.find((m) => m.id === withDot)\n if (dotMatch) return dotMatch\n }\n\n // 4. Dot to dash version (4.5 → 4-5)\n const withDash = modelName.replace(/(\\d+)\\.(\\d+)/, \"$1-$2\")\n if (withDash !== modelName) {\n const dashMatch = models.find((m) => m.id === withDash)\n if (dashMatch) return dashMatch\n }\n\n return undefined\n}\n","import type { Context } from \"hono\"\nimport type { ContentfulStatusCode } from \"hono/utils/http-status\"\n\nimport consola from \"consola\"\n\nimport { rootCause } from \"~/lib/utils\"\n\nexport class HTTPError extends Error {\n response: Response\n\n constructor(message: string, response: Response) {\n super(message)\n this.response = response\n }\n}\n\nexport async function forwardError(c: Context, error: unknown) {\n if (error instanceof HTTPError) {\n // Try to read error body, but it may already be consumed by the caller\n let errorText: string\n try {\n errorText = await error.response.text()\n } catch {\n // Body already read — fall back to the error message\n errorText = error.message\n }\n\n // 400 errors: concise log, already detailed upstream\n if (error.response.status === 400) {\n // no extra logging, upstream already printed details\n } else {\n let errorJson: unknown\n try {\n errorJson = JSON.parse(errorText)\n } catch {\n errorJson = errorText\n }\n consola.warn(`Error occurred: ${rootCause(error)}`)\n consola.debug(\"HTTP error:\", errorJson)\n }\n\n return c.json(\n {\n error: {\n message: errorText,\n type: \"error\",\n },\n },\n error.response.status as ContentfulStatusCode,\n )\n }\n\n // Network errors (fetch failed, TLS disconnect, etc.) — concise log\n const message = (error as Error).message || String(error)\n const cause = (error as { cause?: Error }).cause\n if (cause) {\n consola.error(`${message}: ${cause.message}`)\n } else {\n consola.error(message)\n }\n return c.json(\n {\n error: {\n message: (error as Error).message,\n type: \"error\",\n },\n },\n 500,\n )\n}\n","import fs from \"node:fs/promises\"\nimport os from \"node:os\"\nimport path from \"node:path\"\n\nconst APP_DIR = path.join(os.homedir(), \".local\", \"share\", \"copilot-api-plus\")\n\nconst GITHUB_TOKEN_PATH = path.join(APP_DIR, \"github_token\")\n\nconst ACCOUNTS_PATH = path.join(APP_DIR, \"accounts.json\")\n\nexport const PATHS = {\n APP_DIR,\n DATA_DIR: APP_DIR,\n GITHUB_TOKEN_PATH,\n ACCOUNTS_PATH,\n}\n\nexport async function ensurePaths(): Promise<void> {\n await fs.mkdir(PATHS.APP_DIR, { recursive: true })\n await ensureFile(PATHS.GITHUB_TOKEN_PATH)\n}\n\nasync function ensureFile(filePath: string): Promise<void> {\n try {\n await fs.access(filePath, fs.constants.W_OK)\n } catch {\n await fs.writeFile(filePath, \"\")\n await fs.chmod(filePath, 0o600)\n }\n}\n","import consola from \"consola\"\nimport { getProxyForUrl } from \"proxy-from-env\"\nimport { Agent, ProxyAgent, setGlobalDispatcher, type Dispatcher } from \"undici\"\n\n// Module-level references so that `resetConnections` can swap them out.\n// Initialised by `initProxyFromEnv`; the dispatcher closure captures the\n// *variables* (not their values), so replacing them is enough.\nconst agentOptions = {\n keepAliveTimeout: 300_000,\n keepAliveMaxTimeout: 600_000,\n // Allow HTTP/2 when the target supports it. Inside a CONNECT tunnel\n // the ALPN negotiation and any h2 frames are encrypted traffic — proxy\n // nodes see it as \"data flowing\" and won't kill the connection for\n // being idle.\n allowH2: true,\n // Disable body timeout for SSE: undici's default 300s body timeout kills\n // long-running SSE streams during model thinking phases with no data.\n bodyTimeout: 0,\n connect: {\n timeout: 15_000,\n keepAlive: true,\n keepAliveInitialDelay: 10_000,\n },\n}\nlet direct: Agent | undefined\nlet proxies = new Map<string, ProxyAgent>()\n/** Whether a proxy is actually configured and in use. */\nlet proxyActive = false\n\n/** Whether an environment-level proxy is configured for Copilot URLs. */\nexport function isProxyActive(): boolean {\n return proxyActive\n}\n\n/**\n * Check whether a specific request will be proxied.\n * Account-level proxy takes precedence; falls back to environment proxy.\n */\nexport function isAccountProxied(accountProxy?: string): boolean {\n if (accountProxy) return true\n return proxyActive\n}\n\n// ---------------------------------------------------------------------------\n// Proxy-tunnel keepalive: periodic lightweight requests through the proxy\n// ---------------------------------------------------------------------------\n\n/**\n * Many proxy nodes (especially third-party VPN/airport services) kill\n * CONNECT tunnels that are idle for ~60 s. During long model thinking\n * phases the SSE stream carries no data, which looks \"idle\" to the proxy.\n *\n * This keepalive sends a tiny HEAD request to the Copilot API every 30 s\n * through the SAME connection pool that the SSE stream uses. For\n * per-account connections, this means pinging through each account's own\n * dispatcher, not the global one — so the correct proxy tunnel is kept\n * alive.\n *\n * The keepalive is active ONLY while there are SSE streams in flight\n * (tracked per-account via `activeStreams`).\n */\nlet keepaliveTimer: ReturnType<typeof setInterval> | undefined\nconst KEEPALIVE_INTERVAL_MS = 20_000\nconst KEEPALIVE_URL = \"https://api.individual.githubcopilot.com/\"\n\ninterface ActiveStreamInfo {\n count: number\n accountProxy?: string\n apiBaseUrl?: string\n}\n\n/** Account info attached to streaming responses for keepalive targeting. */\nexport interface StreamAccountInfo {\n accountId?: string\n accountProxy?: string\n apiBaseUrl?: string\n}\n\n/** Track active streams: global (key = \"__global__\") and per-account. */\nconst activeStreams = new Map<string, ActiveStreamInfo>()\n\nfunction startKeepalive(): void {\n if (keepaliveTimer) return\n keepaliveTimer = setInterval(() => {\n // Ping each connection pool that has active streams\n for (const [key, info] of activeStreams) {\n if (info.count <= 0) continue\n // Use the tracked API base URL so the keepalive request goes through\n // the SAME origin (and thus the same HTTP/2 session / CONNECT tunnel)\n // as the active SSE stream.\n const pingUrl = info.apiBaseUrl || KEEPALIVE_URL\n\n if (key === \"__global__\") {\n // Global connection — use standard fetch (goes through global dispatcher)\n fetch(pingUrl, { method: \"HEAD\" }).catch(() => {})\n consola.debug(\n `Proxy keepalive ping sent (global → ${new URL(pingUrl).hostname})`,\n )\n } else {\n // Per-account connection — ping through the account's own dispatcher\n const dispatcher = getAccountDispatcher(key, info.accountProxy)\n fetch(pingUrl, {\n method: \"HEAD\",\n dispatcher: dispatcher as unknown as undefined,\n } as RequestInit).catch(() => {})\n consola.debug(\n `Proxy keepalive ping sent (account ${key.slice(0, 8)} → ${new URL(pingUrl).hostname})`,\n )\n }\n }\n }, KEEPALIVE_INTERVAL_MS)\n // Don't prevent Node from exiting because of this timer.\n keepaliveTimer.unref()\n consola.debug(\n \"Proxy keepalive started (20s interval, targeting active stream origins)\",\n )\n}\n\nfunction stopKeepalive(): void {\n if (keepaliveTimer) {\n clearInterval(keepaliveTimer)\n keepaliveTimer = undefined\n consola.debug(\"Proxy keepalive stopped (no active streams)\")\n }\n}\n\nfunction getTotalStreamCount(): number {\n let total = 0\n for (const info of activeStreams.values()) {\n total += info.count\n }\n return total\n}\n\n/**\n * Call when an SSE stream starts. Activates the proxy-tunnel keepalive\n * if this is the first active stream and a proxy is configured.\n *\n * @param accountInfo If provided, keepalive pings go through this\n * account's own connection pool (not the global one).\n */\nexport function notifyStreamStart(accountInfo?: StreamAccountInfo): void {\n if (!proxyActive) return\n\n const key = accountInfo?.accountId ?? \"__global__\"\n const existing = activeStreams.get(key)\n if (existing) {\n existing.count++\n // Update apiBaseUrl if provided (may differ across streams)\n if (accountInfo?.apiBaseUrl) existing.apiBaseUrl = accountInfo.apiBaseUrl\n } else {\n activeStreams.set(key, {\n count: 1,\n accountProxy: accountInfo?.accountProxy,\n apiBaseUrl: accountInfo?.apiBaseUrl,\n })\n }\n\n if (getTotalStreamCount() === 1) startKeepalive()\n}\n\n/**\n * Call when an SSE stream ends (success or error). Stops the keepalive\n * once no streams are active.\n */\nexport function notifyStreamEnd(accountInfo?: StreamAccountInfo): void {\n if (!proxyActive) return\n\n const key = accountInfo?.accountId ?? \"__global__\"\n const existing = activeStreams.get(key)\n if (existing) {\n existing.count = Math.max(0, existing.count - 1)\n if (existing.count === 0) activeStreams.delete(key)\n }\n\n if (getTotalStreamCount() === 0) stopKeepalive()\n}\n\nexport function initProxyFromEnv(): void {\n if (typeof Bun !== \"undefined\") {\n // Bun's native fetch automatically respects HTTP_PROXY/HTTPS_PROXY.\n // We still need to detect proxy presence so that keepalive and heartbeat\n // logic activates correctly.\n const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy\n const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy\n proxyActive = Boolean(httpProxy || httpsProxy)\n if (proxyActive) {\n consola.debug(\"Bun runtime: proxy detected from environment variables\")\n }\n return\n }\n\n try {\n direct = new Agent(agentOptions)\n proxies = new Map<string, ProxyAgent>()\n\n // We only need a minimal dispatcher that implements `dispatch` at runtime.\n // Typing the object as `Dispatcher` forces TypeScript to require many\n // additional methods. Instead, keep a plain object and cast when passing\n // to `setGlobalDispatcher`.\n const dispatcher = {\n dispatch(\n options: Dispatcher.DispatchOptions,\n handler: Dispatcher.DispatchHandler,\n ) {\n try {\n const origin =\n typeof options.origin === \"string\" ?\n new URL(options.origin)\n : (options.origin as URL)\n const get = getProxyForUrl as unknown as (\n u: string,\n ) => string | undefined\n const raw = get(origin.toString())\n const proxyUrl = raw && raw.length > 0 ? raw : undefined\n if (!proxyUrl) {\n consola.debug(`HTTP proxy bypass: ${origin.hostname}`)\n return (direct as unknown as Dispatcher).dispatch(options, handler)\n }\n let agent = proxies.get(proxyUrl)\n if (!agent) {\n agent = new ProxyAgent({ uri: proxyUrl, ...agentOptions })\n proxies.set(proxyUrl, agent)\n }\n let label = proxyUrl\n try {\n const u = new URL(proxyUrl)\n label = `${u.protocol}//${u.host}`\n } catch {\n /* noop */\n }\n consola.debug(`HTTP proxy route: ${origin.hostname} via ${label}`)\n return (agent as unknown as Dispatcher).dispatch(options, handler)\n } catch {\n return (direct as unknown as Dispatcher).dispatch(options, handler)\n }\n },\n close() {\n for (const agent of proxies.values()) {\n void (agent as unknown as Dispatcher).close()\n }\n // `direct` is always set before the dispatcher is installed.\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return direct!.close()\n },\n destroy() {\n for (const agent of proxies.values()) {\n void (agent as unknown as Dispatcher).destroy()\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return direct!.destroy()\n },\n }\n\n setGlobalDispatcher(dispatcher as unknown as Dispatcher)\n // Only activate proxy keepalive if a proxy is actually configured\n // for at least one relevant URL.\n const get = getProxyForUrl as unknown as (u: string) => string | undefined\n const testUrls = [\n \"https://api.individual.githubcopilot.com/\",\n \"https://api.business.githubcopilot.com/\",\n \"https://api.github.com/\",\n ]\n proxyActive = testUrls.some((u) => {\n const raw = get(u)\n return raw !== undefined && raw.length > 0\n })\n\n if (proxyActive) {\n consola.debug(\n \"Proxy active: undici dispatcher configured with bodyTimeout=0, allowH2=true\",\n )\n } else {\n consola.debug(\n \"HTTP proxy dispatcher installed but no proxy URLs detected\",\n )\n }\n } catch (err) {\n consola.debug(\"Proxy setup skipped:\", err)\n }\n}\n\n/**\n * Destroy all pooled connections (direct + proxy agents) and replace them\n * with fresh instances. The global dispatcher's `dispatch` method captures\n * `direct` and `proxies` by reference, so subsequent requests automatically\n * use the new agents — no need to call `setGlobalDispatcher` again.\n *\n * Call this after a network error to discard stale/half-closed sockets that\n * would otherwise cause every retry to wait ~60 s before timing out.\n *\n * Under the Bun runtime (which doesn't use undici) this is a no-op.\n */\nexport function resetConnections(): void {\n if (typeof Bun !== \"undefined\") return\n if (!direct) return\n\n const oldDirect = direct\n const oldProxies = proxies\n\n direct = new Agent(agentOptions)\n proxies = new Map<string, ProxyAgent>()\n\n // Tear down old agents in the background — errors are non-fatal.\n void (oldDirect as unknown as Dispatcher).close().catch(() => {})\n for (const agent of oldProxies.values()) {\n void (agent as unknown as Dispatcher).close().catch(() => {})\n }\n\n consola.debug(\"Connection pool reset — stale sockets cleared\")\n}\n\n// ---------------------------------------------------------------------------\n// Per-account connection isolation\n// ---------------------------------------------------------------------------\n\n/** Separate connection pools per account to prevent cross-account correlation. */\nconst accountAgents = new Map<string, Agent>()\nconst accountProxyAgents = new Map<string, Map<string, ProxyAgent>>()\n\n/**\n * Get or create an isolated undici Agent for a specific account.\n * Each account gets its own connection pool so that GitHub cannot correlate\n * accounts by shared TCP connections or TLS sessions.\n */\nexport function getAccountDispatcher(\n accountId: string,\n accountProxy?: string,\n): {\n dispatch: (\n options: Dispatcher.DispatchOptions,\n handler: Dispatcher.DispatchHandler,\n ) => boolean\n} {\n // Return a dispatcher that routes through per-account agents\n return {\n dispatch(\n options: Dispatcher.DispatchOptions,\n handler: Dispatcher.DispatchHandler,\n ) {\n try {\n const origin =\n typeof options.origin === \"string\" ?\n new URL(options.origin)\n : (options.origin as URL)\n // Account-level proxy takes precedence over environment proxy\n let proxyUrl: string | undefined\n if (accountProxy) {\n proxyUrl = accountProxy\n } else {\n const get = getProxyForUrl as unknown as (\n u: string,\n ) => string | undefined\n const raw = get(origin.toString())\n proxyUrl = raw && raw.length > 0 ? raw : undefined\n }\n\n if (!proxyUrl) {\n // Direct connection — use per-account agent\n let agent = accountAgents.get(accountId)\n if (!agent) {\n agent = new Agent(agentOptions)\n accountAgents.set(accountId, agent)\n }\n return (agent as unknown as Dispatcher).dispatch(options, handler)\n }\n\n // Proxy connection — use per-account proxy agent\n let proxyMap = accountProxyAgents.get(accountId)\n if (!proxyMap) {\n proxyMap = new Map<string, ProxyAgent>()\n accountProxyAgents.set(accountId, proxyMap)\n }\n let proxyAgent = proxyMap.get(proxyUrl)\n if (!proxyAgent) {\n proxyAgent = new ProxyAgent({ uri: proxyUrl, ...agentOptions })\n proxyMap.set(proxyUrl, proxyAgent)\n }\n return (proxyAgent as unknown as Dispatcher).dispatch(options, handler)\n } catch {\n // Fallback to per-account direct agent\n let agent = accountAgents.get(accountId)\n if (!agent) {\n agent = new Agent(agentOptions)\n accountAgents.set(accountId, agent)\n }\n return (agent as unknown as Dispatcher).dispatch(options, handler)\n }\n },\n }\n}\n\n/**\n * Reset connection pools for a specific account.\n */\nexport function resetAccountConnections(accountId: string): void {\n const oldAgent = accountAgents.get(accountId)\n if (oldAgent) {\n void (oldAgent as unknown as Dispatcher).close().catch(() => {})\n accountAgents.delete(accountId)\n }\n const oldProxies = accountProxyAgents.get(accountId)\n if (oldProxies) {\n for (const agent of oldProxies.values()) {\n void (agent as unknown as Dispatcher).close().catch(() => {})\n }\n accountProxyAgents.delete(accountId)\n }\n}\n\n/**\n * Reset all per-account connection pools.\n */\nexport function resetAllAccountConnections(): void {\n for (const [id] of accountAgents) {\n resetAccountConnections(id)\n }\n}\n\n// ---------------------------------------------------------------------------\n// Periodic connection pool recreation\n// ---------------------------------------------------------------------------\n\nlet connectionRecycleTimer: ReturnType<typeof setInterval> | undefined\n\n/**\n * Start periodic connection pool recreation for all per-account pools.\n * Simulates \"VS Code restart\" behavior — stale connections are replaced\n * with fresh ones at randomized intervals to avoid timing correlation.\n *\n * @param baseIntervalMs Base interval (default 4 hours).\n */\nexport function startConnectionRecycling(\n baseIntervalMs: number = 4 * 60 * 60 * 1000,\n): void {\n stopConnectionRecycling()\n\n connectionRecycleTimer = setInterval(() => {\n // Add ±25% jitter to avoid all accounts recycling at the same time\n const jitter = baseIntervalMs * 0.25 * (Math.random() * 2 - 1)\n setTimeout(\n () => {\n resetAllAccountConnections()\n consola.debug(\"Per-account connection pools recycled\")\n },\n Math.max(0, jitter),\n )\n }, baseIntervalMs)\n\n // Don't prevent Node from exiting\n connectionRecycleTimer.unref()\n consola.debug(\n `Connection pool recycling started (interval: ~${Math.round(baseIntervalMs / 3_600_000)}h)`,\n )\n}\n\n/**\n * Stop periodic connection pool recreation.\n */\nexport function stopConnectionRecycling(): void {\n if (connectionRecycleTimer) {\n clearInterval(connectionRecycleTimer)\n connectionRecycleTimer = undefined\n }\n}\n","import consola from \"consola\"\n\nimport { GITHUB_API_BASE_URL, githubHeaders } from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\n/**\n * Fetch a short-lived Copilot JWT from the GitHub API.\n *\n * @param githubToken Optional explicit GitHub PAT. When provided the request\n * uses this token instead of `state.githubToken` and does\n * **not** mutate global state (so multi-account callers\n * stay side-effect-free).\n */\nexport const getCopilotToken = async (githubToken?: string) => {\n const tokenToUse = githubToken ?? state.githubToken\n const isExplicitToken = githubToken !== undefined\n\n const url = `${GITHUB_API_BASE_URL}/copilot_internal/v2/token`\n const fetchOptions: RequestInit = {\n headers: githubHeaders({\n ...state,\n githubToken: tokenToUse,\n }),\n }\n\n // Retry on transient network errors (TLS disconnect, connection timeout, etc.)\n const maxRetries = 2\n let lastError: unknown\n let response: Response | undefined\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n response = await fetch(url, fetchOptions)\n break\n } catch (error: unknown) {\n lastError = error\n if (attempt < maxRetries) {\n const delay = 1000 * (attempt + 1)\n consola.warn(\n `Token fetch error on attempt ${attempt + 1}/${maxRetries + 1}, retrying in ${delay}ms:`,\n error instanceof Error ? error.message : error,\n )\n await new Promise((r) => setTimeout(r, delay))\n }\n }\n }\n\n if (!response) {\n throw lastError\n }\n\n if (!response.ok) throw new HTTPError(\"Failed to get Copilot token\", response)\n\n const data = (await response.json()) as GetCopilotTokenResponse\n\n // Only write to global state when using the default token (single-account mode).\n // When an explicit githubToken is provided (multi-account), the caller is\n // responsible for storing the endpoint on its own Account object.\n if (!isExplicitToken && data.endpoints?.api) {\n // eslint-disable-next-line require-atomic-updates\n state.copilotApiEndpoint = data.endpoints.api\n }\n\n return data\n}\n\n// Full interface matching Zed's implementation\ninterface GetCopilotTokenResponse {\n expires_at: number\n refresh_in: number\n token: string\n endpoints?: {\n api: string\n \"origin-tracker\"?: string\n proxy?: string\n telemetry?: string\n }\n annotations_enabled?: boolean\n chat_enabled?: boolean\n chat_jetbrains_enabled?: boolean\n code_quote_enabled?: boolean\n codesearch?: boolean\n copilot_ide_agent_chat_gpt4_small_prompt?: boolean\n copilotignore_enabled?: boolean\n individual?: boolean\n sku?: string\n tracking_id?: string\n limited_user_quotas?: unknown // Premium request quotas\n}\n","import { GITHUB_API_BASE_URL, githubHeaders } from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\n/**\n * Fetch the authenticated user's Copilot usage / quota snapshot.\n *\n * @param githubToken Optional explicit GitHub PAT. When provided the request\n * uses this token instead of `state.githubToken`, allowing\n * multi-account callers to query any account without\n * touching global state.\n */\nexport const getCopilotUsage = async (\n githubToken?: string,\n): Promise<CopilotUsageResponse> => {\n const response = await fetch(`${GITHUB_API_BASE_URL}/copilot_internal/user`, {\n headers: githubHeaders({\n ...state,\n githubToken: githubToken ?? state.githubToken,\n }),\n })\n\n if (!response.ok) {\n throw new HTTPError(\"Failed to get Copilot usage\", response)\n }\n\n return (await response.json()) as CopilotUsageResponse\n}\n\nexport interface QuotaDetail {\n entitlement: number\n overage_count: number\n overage_permitted: boolean\n percent_remaining: number\n quota_id: string\n quota_remaining: number\n remaining: number\n unlimited: boolean\n}\n\ninterface QuotaSnapshots {\n chat: QuotaDetail\n completions: QuotaDetail\n premium_interactions: QuotaDetail\n}\n\ninterface CopilotUsageResponse {\n access_type_sku: string\n analytics_tracking_id: string\n assigned_date: string\n can_signup_for_limited: boolean\n chat_enabled: boolean\n copilot_plan: string\n organization_login_list: Array<unknown>\n organization_list: Array<unknown>\n quota_reset_date: string\n quota_snapshots: QuotaSnapshots\n}\n","import { GITHUB_API_BASE_URL, standardHeaders } from \"~/lib/api-config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { state } from \"~/lib/state\"\n\n/**\n * Fetch the GitHub user profile.\n *\n * @param githubToken Optional explicit token. When omitted, falls back to\n * the global `state.githubToken`. Prefer passing a token\n * explicitly to avoid race conditions in multi-account mode.\n */\nexport async function getGitHubUser(githubToken?: string) {\n const token = githubToken ?? state.githubToken\n const response = await fetch(`${GITHUB_API_BASE_URL}/user`, {\n headers: {\n authorization: `token ${token}`,\n ...standardHeaders(),\n },\n })\n\n if (!response.ok) throw new HTTPError(\"Failed to get GitHub user\", response)\n\n return (await response.json()) as GithubUserResponse\n}\n\n// Trimmed for the sake of simplicity\ninterface GithubUserResponse {\n login: string\n}\n","import consola from \"consola\"\nimport { randomBytes, randomUUID } from \"node:crypto\"\nimport fs from \"node:fs/promises\"\n\nimport { HTTPError } from \"~/lib/error\"\nimport { PATHS } from \"~/lib/paths\"\nimport { startConnectionRecycling, stopConnectionRecycling } from \"~/lib/proxy\"\nimport { rootCause } from \"~/lib/utils\"\nimport { getCopilotToken } from \"~/services/github/get-copilot-token\"\nimport { getCopilotUsage } from \"~/services/github/get-copilot-usage\"\nimport { getGitHubUser } from \"~/services/github/get-user\"\n\n// ---------------------------------------------------------------------------\n// Types\n// ---------------------------------------------------------------------------\n\nexport type AccountStatus =\n | \"active\"\n | \"exhausted\"\n | \"rate_limited\"\n | \"banned\"\n | \"error\"\n | \"disabled\"\n\nexport interface Account {\n id: string\n label: string\n githubToken: string\n /** Short-lived Copilot JWT – kept in memory only, never persisted. */\n copilotToken?: string\n /** API endpoint extracted from the Copilot token response. */\n copilotApiEndpoint?: string\n accountType: string // \"individual\" | \"business\" | \"enterprise\"\n\n // Status\n status: AccountStatus\n statusMessage?: string\n lastUsedAt?: number\n consecutiveFailures: number\n cooldownUntil?: number\n\n // Quota snapshot (refreshed periodically)\n usage?: {\n premium_remaining: number\n premium_total: number\n chat_remaining: number\n chat_total: number\n /** Whether the upstream account is allowed to exceed its monthly quota. */\n premium_overage_permitted?: boolean\n quotaResetDate: string\n lastCheckedAt: number\n }\n\n // Anti-correlation\n /** Stable per-account machine identifier – persisted to disk. */\n machineId?: string\n /** Runtime-only session identifier – regenerated on every startup. */\n sessionId?: string\n /** Timestamp of the last request sent using this account. */\n lastRequestAt?: number\n /** Optional per-account proxy URL (e.g. \"http://proxy:8080\" or \"socks5://proxy:1080\"). */\n proxy?: string\n\n // Metadata\n githubLogin?: string\n addedAt: number\n}\n\n// ---------------------------------------------------------------------------\n// Persistence helpers\n// ---------------------------------------------------------------------------\n\n/** Fields excluded from the JSON file (short-lived / runtime-only). */\ntype PersistedAccount = Omit<Account, \"copilotToken\" | \"sessionId\">\n\nconst ACCOUNTS_PATH = PATHS.ACCOUNTS_PATH\n\n// ---------------------------------------------------------------------------\n// AccountManager\n// ---------------------------------------------------------------------------\n\nconst COOLDOWN_MS = 60 * 1000 // 60 seconds\n\nexport class AccountManager {\n private accounts: Array<Account> = []\n private refreshInterval?: ReturnType<typeof setInterval>\n private usageInterval?: ReturnType<typeof setInterval>\n\n // Debounced save\n private saveTimer?: ReturnType<typeof setTimeout>\n private savePending = false\n\n /** True if accounts.json existed on disk when loadAccounts() was called. */\n accountsFileExisted = false\n\n // ---------- Persistence ------------------------------------------------\n\n /**\n * Load accounts from the JSON file on disk.\n * Missing file is treated as empty list – not an error.\n */\n async loadAccounts(): Promise<void> {\n try {\n // eslint-disable-next-line unicorn/prefer-json-parse-buffer\n const raw = await fs.readFile(ACCOUNTS_PATH, \"utf8\")\n this.accountsFileExisted = true\n const parsed = JSON.parse(raw) as Array<PersistedAccount>\n this.accounts = parsed.map((a) => ({\n ...a,\n copilotToken: undefined,\n sessionId: randomUUID(),\n machineId: a.machineId || randomBytes(32).toString(\"hex\"),\n }))\n consola.info(`Loaded ${this.accounts.length} account(s) from disk`)\n } catch (err: unknown) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") {\n this.accountsFileExisted = false\n this.accounts = []\n return\n }\n consola.warn(`Failed to load accounts: ${rootCause(err)}`)\n consola.debug(\"Failed to load accounts:\", err)\n this.accounts = []\n }\n }\n\n /**\n * Persist accounts to disk. `copilotToken` is excluded because it is\n * short-lived and will be refreshed on every startup.\n */\n async saveAccounts(): Promise<void> {\n const data: Array<PersistedAccount> = this.accounts.map(\n ({ copilotToken: _dropped, sessionId: _session, ...rest }) => rest,\n )\n try {\n await fs.writeFile(ACCOUNTS_PATH, JSON.stringify(data, null, 2), {\n encoding: \"utf8\",\n mode: 0o600,\n })\n } catch (err) {\n consola.warn(`Failed to save accounts: ${rootCause(err)}`)\n consola.debug(\"Failed to save accounts:\", err)\n }\n }\n\n /** Schedule a debounced save (coalesces rapid status updates). */\n private debouncedSave(): void {\n if (this.saveTimer) return // already scheduled\n this.savePending = true\n this.saveTimer = setTimeout(async () => {\n this.saveTimer = undefined\n if (this.savePending) {\n this.savePending = false\n await this.saveAccounts()\n }\n }, 1_000)\n }\n\n // ---------- CRUD -------------------------------------------------------\n\n /**\n * Add a new account.\n *\n * 1. Validates the GitHub token by fetching user info.\n * 2. Obtains an initial Copilot token.\n * 3. Persists the account to disk.\n */\n async addAccount(\n githubToken: string,\n label: string,\n accountType: string = \"individual\",\n ): Promise<Account> {\n // 1. Validate token – get GitHub login (pass token explicitly, no state mutation)\n const user = await getGitHubUser(githubToken)\n\n // 2. Obtain Copilot token – pass token directly (no state mutation)\n const tokenData = await getCopilotToken(githubToken)\n\n const account: Account = {\n id: randomUUID(),\n label,\n githubToken,\n copilotToken: tokenData.token,\n copilotApiEndpoint: tokenData.endpoints?.api,\n accountType,\n status: \"active\",\n consecutiveFailures: 0,\n githubLogin: user.login,\n machineId: randomBytes(32).toString(\"hex\"),\n sessionId: randomUUID(),\n addedAt: Date.now(),\n }\n\n this.accounts.push(account)\n await this.saveAccounts()\n\n consola.success(`Account added: ${label} (${user.login})`)\n return account\n }\n\n async removeAccount(id: string): Promise<boolean> {\n const idx = this.accounts.findIndex((a) => a.id === id)\n if (idx === -1) return false\n const [removed] = this.accounts.splice(idx, 1)\n await this.saveAccounts()\n consola.info(`Account removed: ${removed.label}`)\n return true\n }\n\n getAccounts(): Array<Account> {\n return this.accounts\n }\n\n getAccountById(id: string): Account | undefined {\n return this.accounts.find((a) => a.id === id)\n }\n\n // ---------- Smart account selection ------------------------------------\n\n /**\n * Pick the best available account.\n *\n * 1. Filter out disabled, banned, exhausted, and accounts still in cooldown.\n * 2. Prefer accounts with more remaining premium quota.\n * 3. Fall back to round-robin (least-recently-used) when quotas are equal\n * or unknown.\n */\n getActiveAccount(): Account | undefined {\n const now = Date.now()\n\n const eligible = this.accounts.filter((a) => {\n if (\n a.status === \"disabled\"\n || a.status === \"banned\"\n || a.status === \"exhausted\"\n ) {\n return false\n }\n if (a.cooldownUntil && a.cooldownUntil > now) return false\n return true\n })\n\n if (eligible.length === 0) {\n // Fallback: if there is exactly one account and it's only cooling down\n // (not banned/disabled/exhausted), return it anyway. With a single\n // account there is nothing to \"switch to\", so blocking all requests\n // for the cooldown period would be a self-inflicted outage.\n if (this.accounts.length === 1) {\n const solo = this.accounts[0]\n if (\n solo.status !== \"disabled\"\n && solo.status !== \"banned\"\n && solo.status !== \"exhausted\"\n ) {\n return solo\n }\n }\n return undefined\n }\n\n eligible.sort((a, b) => {\n const aRemaining = a.usage?.premium_remaining ?? -1\n const bRemaining = b.usage?.premium_remaining ?? -1\n\n // Higher remaining quota first\n if (aRemaining !== bRemaining) return bRemaining - aRemaining\n\n // Equal / unknown → least recently used first (round-robin)\n const aUsed = a.lastUsedAt ?? 0\n const bUsed = b.lastUsedAt ?? 0\n return aUsed - bUsed\n })\n\n return eligible[0]\n }\n\n // ---------- Status management ------------------------------------------\n\n markAccountStatus(id: string, status: AccountStatus, message?: string): void {\n const account = this.getAccountById(id)\n if (!account) return\n\n account.status = status\n account.statusMessage = message\n\n switch (status) {\n case \"rate_limited\":\n case \"error\": {\n // Only apply cooldown when there are other accounts to switch to.\n // With a single account, cooldown would block ALL requests with\n // \"No available accounts\" — effectively a self-inflicted outage.\n if (this.accounts.length > 1) {\n account.cooldownUntil = Date.now() + COOLDOWN_MS\n }\n account.consecutiveFailures += 1\n\n break\n }\n case \"banned\": {\n account.consecutiveFailures += 1\n\n break\n }\n case \"active\": {\n // Recovering from a failure state — reset\n account.consecutiveFailures = 0\n account.cooldownUntil = undefined\n\n break\n }\n // No default\n }\n // \"exhausted\" and \"disabled\" don't touch consecutiveFailures\n\n this.debouncedSave()\n }\n\n markAccountSuccess(id: string): void {\n const account = this.getAccountById(id)\n if (!account) return\n\n account.consecutiveFailures = 0\n account.lastUsedAt = Date.now()\n\n if (account.status === \"error\" || account.status === \"rate_limited\") {\n account.status = \"active\"\n account.statusMessage = undefined\n account.cooldownUntil = undefined\n }\n\n this.debouncedSave()\n }\n\n // ---------- Token & usage refresh (per account) ------------------------\n\n /**\n * Refresh the short-lived Copilot JWT for a single account.\n *\n * Passes the account's GitHub token directly to `getCopilotToken()` so that\n * global `state.githubToken` is never mutated — safe for concurrent use.\n */\n async refreshAccountToken(account: Account): Promise<void> {\n try {\n const data = await getCopilotToken(account.githubToken)\n // eslint-disable-next-line require-atomic-updates\n account.copilotToken = data.token\n if (data.endpoints?.api) {\n // eslint-disable-next-line require-atomic-updates\n account.copilotApiEndpoint = data.endpoints.api\n }\n } catch (err: unknown) {\n if (err instanceof HTTPError && err.response.status === 401) {\n this.markAccountStatus(account.id, \"banned\", \"GitHub token invalid\")\n consola.warn(\n `Account ${account.label}: token invalid, marked as banned`,\n )\n } else {\n consola.warn(\n `Account ${account.label}: failed to refresh Copilot token: ${rootCause(err)}`,\n )\n consola.debug(\n `Account ${account.label}: failed to refresh Copilot token:`,\n err,\n )\n }\n }\n }\n\n /**\n * Refresh the usage / quota snapshot for a single account.\n *\n * Passes the account's GitHub token directly to `getCopilotUsage()` so that\n * global `state.githubToken` is never mutated — safe for concurrent use.\n */\n async refreshAccountUsage(account: Account): Promise<void> {\n try {\n const data = await getCopilotUsage(account.githubToken)\n const snap = data.quota_snapshots\n\n // eslint-disable-next-line require-atomic-updates\n account.usage = {\n premium_remaining: snap.premium_interactions.remaining,\n premium_total: snap.premium_interactions.entitlement,\n chat_remaining: snap.chat.remaining,\n chat_total: snap.chat.entitlement,\n premium_overage_permitted: snap.premium_interactions.overage_permitted,\n quotaResetDate: data.quota_reset_date,\n lastCheckedAt: Date.now(),\n }\n\n const overagePermitted = snap.premium_interactions.overage_permitted\n\n // Transition between active ↔ exhausted.\n // If the upstream account allows overage (paid extra), do NOT mark it\n // exhausted just because remaining went negative — the user can keep\n // making requests and pay per-use. We only flip back from \"exhausted\"\n // to \"active\" automatically; we never auto-flip into \"exhausted\" when\n // overage is permitted.\n if (\n account.usage.premium_remaining <= 0\n && account.status === \"active\"\n && !overagePermitted\n ) {\n this.markAccountStatus(\n account.id,\n \"exhausted\",\n \"Premium quota exhausted\",\n )\n } else if (\n account.status === \"exhausted\"\n && (account.usage.premium_remaining > 0 || overagePermitted)\n ) {\n account.status = \"active\"\n account.statusMessage = undefined\n account.consecutiveFailures = 0\n this.debouncedSave()\n }\n } catch (err) {\n consola.warn(\n `Account ${account.label}: failed to refresh usage: ${rootCause(err)}`,\n )\n consola.debug(`Account ${account.label}: failed to refresh usage:`, err)\n }\n }\n\n // ---------- Background refresh -----------------------------------------\n\n /** Refresh Copilot tokens for all non-disabled accounts. */\n async refreshAllTokens(): Promise<void> {\n const targets = this.accounts.filter((a) => a.status !== \"disabled\")\n await Promise.allSettled(targets.map((a) => this.refreshAccountToken(a)))\n }\n\n /** Refresh usage snapshots for all non-disabled accounts. */\n async refreshAllUsage(): Promise<void> {\n const targets = this.accounts.filter((a) => a.status !== \"disabled\")\n await Promise.allSettled(targets.map((a) => this.refreshAccountUsage(a)))\n }\n\n /**\n * Start periodic background refresh loops.\n *\n * The initial token refresh is awaited to ensure accounts are ready before\n * the first request arrives. Usage refresh runs in the background.\n *\n * @param tokenIntervalMs Token refresh interval (default 25 min).\n * @param usageIntervalMs Usage refresh interval (default 5 min).\n */\n async startBackgroundRefresh(\n tokenIntervalMs: number = 25 * 60 * 1000,\n usageIntervalMs: number = 5 * 60 * 1000,\n ): Promise<void> {\n this.stopBackgroundRefresh()\n\n // Initial refresh — await token refresh so accounts are ready for requests\n await this.refreshAllTokens()\n void this.refreshAllUsage()\n\n this.refreshInterval = setInterval(() => {\n void this.refreshAllTokens()\n }, tokenIntervalMs)\n\n this.usageInterval = setInterval(() => {\n void this.refreshAllUsage()\n }, usageIntervalMs)\n\n consola.debug(\n `Background refresh started (tokens: ${tokenIntervalMs / 60_000}m, usage: ${usageIntervalMs / 60_000}m)`,\n )\n\n // Start periodic connection pool recycling (~4h with jitter)\n startConnectionRecycling()\n }\n\n stopBackgroundRefresh(): void {\n stopConnectionRecycling()\n if (this.refreshInterval) {\n clearInterval(this.refreshInterval)\n this.refreshInterval = undefined\n }\n if (this.usageInterval) {\n clearInterval(this.usageInterval)\n this.usageInterval = undefined\n }\n }\n\n // ---------- Helpers ----------------------------------------------------\n\n get accountCount(): number {\n return this.accounts.length\n }\n\n get activeAccountCount(): number {\n return this.accounts.filter((a) => a.status === \"active\").length\n }\n\n hasAccounts(): boolean {\n return this.accounts.length > 0\n }\n\n // ---------- Legacy migration -------------------------------------------\n\n /**\n * Create an Account entry from the legacy single-account global state.\n * Useful for seamless upgrade from single-account to multi-account mode.\n *\n * Falls back to creating a minimal entry if API validation fails — the\n * background refresh will fill in the missing data later.\n */\n async migrateFromLegacy(\n githubToken: string,\n accountType: string,\n ): Promise<Account> {\n // Check if this token is already registered\n const existing = this.accounts.find((a) => a.githubToken === githubToken)\n if (existing) {\n consola.debug(\"Legacy account already migrated, skipping\")\n return existing\n }\n\n try {\n const account = await this.addAccount(\n githubToken,\n \"Primary (migrated)\",\n accountType,\n )\n consola.success(\"Legacy single-account migrated to multi-account manager\")\n return account\n } catch (error) {\n // API validation failed — create a minimal entry anyway so the account\n // is visible in the management UI. Background token/usage refresh will\n // fill in the missing data.\n consola.warn(\n \"Could not fully validate legacy account, adding with limited info:\",\n error,\n )\n\n const account: Account = {\n id: randomUUID(),\n label: \"Primary (migrated)\",\n githubToken,\n copilotToken: undefined,\n accountType,\n status: \"active\",\n consecutiveFailures: 0,\n machineId: randomBytes(32).toString(\"hex\"),\n sessionId: randomUUID(),\n addedAt: Date.now(),\n }\n\n this.accounts.push(account)\n await this.saveAccounts()\n return account\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Singleton\n// ---------------------------------------------------------------------------\n\nexport const accountManager = new AccountManager()\n"],"mappings":";;;;;;;;;AAEA,MAAa,yBAAyB;CACpC,gBAAgB;CAChB,QAAQ;CACT;AAED,MAAM,kBAAkB;AACxB,MAAM,wBAAwB,gBAAgB;AAC9C,MAAM,aAAa,qBAAqB;AAGxC,MAAM,cAAc;AAuBpB,MAAa,kBAAkB,WAAwB;AACrD,KAAI,OAAO,mBACT,QAAO,OAAO;AAEhB,QAAO,OAAO,gBAAgB,eAC1B,kCACA,eAAe,OAAO,YAAY;;AAExC,MAAa,kBACX,QACA,SAAkB,UACf;CACH,MAAMA,UAAkC;EACtC,eAAe,UAAU,OAAO;EAChC,gBAAgB,iBAAiB,CAAC;EAClC,0BAA0B;EAC1B,kBAAkB,UAAU,OAAO;EACnC,yBAAyB;EACzB,cAAc;EACd,iBAAiB;EACjB,sBAAsB;EACtB,mBAAmB,YAAY;EAC/B,wBAAwB;EACxB,gBAAgB,YAAY;EAC5B,uCAAuC;EAEvC,GAAI,OAAO,aAAa,EAAE,oBAAoB,OAAO,WAAW;EAChE,GAAI,OAAO,aAAa,EAAE,oBAAoB,OAAO,WAAW;EACjE;AAED,KAAI,OAAQ,SAAQ,4BAA4B;AAEhD,QAAO;;AAGT,MAAa,sBAAsB;AACnC,MAAa,iBAAiB,YAAyB;CACrD,GAAG,iBAAiB;CACpB,eAAe,SAAS,OAAO;CAC/B,kBAAkB,UAAU,OAAO;CACnC,yBAAyB;CACzB,cAAc;CACd,wBAAwB;CACxB,uCAAuC;CACxC;AAED,MAAa,kBAAkB;AAC/B,MAAa,mBAAmB;AAChC,MAAa,oBAAoB,CAAC,YAAY,CAAC,KAAK,IAAI;;;;AC7BxD,MAAaC,QAAe;CAC1B,aAAa;CACb,eAAe;CACf,eAAe;CACf,WAAW;CACX,qBAA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