@nick3/copilot-api 1.4.2 → 1.4.4

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.
Files changed (27) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +50 -12
  3. package/dist/{accounts-manager-CtZD9BhK.js → accounts-manager-BvbpPVp1.js} +7 -4
  4. package/dist/accounts-manager-BvbpPVp1.js.map +1 -0
  5. package/dist/admin/assets/index-Y2SvOXge.js +57 -0
  6. package/dist/admin/assets/{index-CvffOmW7.css → index-geiCIixE.css} +1 -1
  7. package/dist/admin/index.html +2 -2
  8. package/dist/{auth-QjAXKuWY.js → auth-C_MhaDzM.js} +3 -3
  9. package/dist/{auth-QjAXKuWY.js.map → auth-C_MhaDzM.js.map} +1 -1
  10. package/dist/{check-usage-DXujADko.js → check-usage-Cw1fo31i.js} +4 -4
  11. package/dist/{check-usage-DXujADko.js.map → check-usage-Cw1fo31i.js.map} +1 -1
  12. package/dist/{get-copilot-token-BMypymQn.js → get-copilot-token-CbzXMTE5.js} +2 -2
  13. package/dist/{get-copilot-token-BMypymQn.js.map → get-copilot-token-CbzXMTE5.js.map} +1 -1
  14. package/dist/main.js +3 -3
  15. package/dist/{poll-access-token-BqarRUZn.js → poll-access-token-BMaaEFhO.js} +2 -2
  16. package/dist/{poll-access-token-BqarRUZn.js.map → poll-access-token-BMaaEFhO.js.map} +1 -1
  17. package/dist/{server-CKtDlwHW.js → server-BKsjCvqp.js} +45 -6
  18. package/dist/server-BKsjCvqp.js.map +1 -0
  19. package/dist/{start-CQKD78BE.js → start-cJXCnfYv.js} +35 -31
  20. package/dist/start-cJXCnfYv.js.map +1 -0
  21. package/dist/{utils-Ce30L8HS.js → utils-DkUv9TDj.js} +6 -2
  22. package/dist/{utils-Ce30L8HS.js.map → utils-DkUv9TDj.js.map} +1 -1
  23. package/package.json +1 -1
  24. package/dist/accounts-manager-CtZD9BhK.js.map +0 -1
  25. package/dist/admin/assets/index-BB9SaCFS.js +0 -57
  26. package/dist/server-CKtDlwHW.js.map +0 -1
  27. package/dist/start-CQKD78BE.js.map +0 -1
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Erick Christian Purwanto
3
+ Copyright (c) 2025-, Erick Christian Purwanto, and a number of other contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -24,7 +24,22 @@ English | [中文](./README_CN.md)
24
24
  ---
25
25
 
26
26
  > [!NOTE]
27
- > [opencode](https://github.com/sst/opencode) already ships with a built-in GitHub Copilot provider, so you may not need this project for basic usage. This proxy is still useful if you want OpenCode to talk to Copilot through `@ai-sdk/anthropic`, preserve Anthropic Messages semantics for tool use, prefer the native Messages API over plain Chat Completions for Claude-family models, use `gpt-5.4` phase-aware commentary, or fine-tune premium-request usage with small-model fallbacks.
27
+ > [opencode](https://github.com/sst/opencode) already ships with a built-in GitHub Copilot provider, so you may not need this project for basic usage. This proxy is still useful if you want OpenCode to talk to Copilot through `@ai-sdk/anthropic`, preserve Anthropic Messages semantics for tool use, prefer the native Messages API over Chat Completions API for Claude-family models, use gpt phase-aware commentary, or optimize premium requests.
28
+
29
+ ---
30
+ ## Important Notes
31
+
32
+ > [!IMPORTANT]
33
+ > **Before using, please be aware of the following:**
34
+ >
35
+ > 1. **Claude Code model ID configuration:** When using with Claude Code, please configure the model ID as `claude-opus-4-6` or `claude-opus-4.6` (without the `[1m]` suffix, exceeding GitHub Copilot's context window limit too much may lead to being banned).
36
+ >
37
+ > 2. **Recommend for Opencode:** When using with opencode, we recommend starting with the opencode OAuth app. This approach behaves identically to opencode's built-in GitHub Copilot provider with no Terms of Service risk:
38
+ > ```sh
39
+ > npx @nick3/copilot-api@latest --oauth-app=opencode start
40
+ > ```
41
+ >
42
+ > 3. **Disable multi agent when using codex:** If you're using codex via GitHub Copilot, it's recommended to disable the multi agent feature. Currently, GitHub Copilot charges based on the last message being a user role when using codex, and the billing logic has not been adjusted.
28
43
 
29
44
  ---
30
45
 
@@ -38,10 +53,10 @@ Compared with routing everything through plain Chat Completions compatibility, t
38
53
 
39
54
  - **OpenAI & Anthropic Compatibility**: Exposes GitHub Copilot as an OpenAI-compatible (`/v1/responses`, `/v1/chat/completions`, `/v1/models`, `/v1/embeddings`) and Anthropic-compatible (`/v1/messages`) API.
40
55
  - **Anthropic-First Routing for Claude Models**: When a model supports Copilot's native `/v1/messages` endpoint, the proxy prefers it over `/responses` or `/chat/completions`, preserving Anthropic-style `tool_use` / `tool_result` flows and more Claude-native behavior.
41
- - **Fewer Unnecessary Premium Requests**: Reduces wasted premium usage by routing warmup and compact/background requests to `smallModel`, merging `tool_result` follow-ups back into the tool flow, and treating resumed tool turns as continuation traffic instead of fresh premium interactions.
56
+ - **Fewer Unnecessary Premium Requests**: Reduces wasted premium usage by routing warmup requests to `smallModel`, merging `tool_result` follow-ups back into the tool flow, and treating resumed tool turns as continuation traffic instead of fresh premium interactions.
42
57
  - **Phase-Aware `gpt-5.4` and `gpt-5.3-codex`**: These models can emit user-friendly commentary before deeper reasoning or tool use, so long-running coding actions are easier to understand instead of appearing as a sudden tool burst.
43
58
  - **Claude Native Beta Support**: On the Messages API path, supports Anthropic-native capabilities such as `interleaved-thinking`, `advanced-tool-use`, and `context-management`, which are difficult or unavailable through plain Chat Completions compatibility.
44
- - **Subagent Marker Integration**: Optional Claude Code and opencode plugins can inject `__SUBAGENT_MARKER__...` and propagate `x-session-id` so subagent traffic keeps the correct root session and agent/user semantics.
59
+ - **Subagent Marker Integration**: Claude Code and opencode plugins can inject `__SUBAGENT_MARKER__...` and propagate `x-session-id` so subagent traffic keeps the correct root session and agent/user semantics.
45
60
  - **OpenCode via `@ai-sdk/anthropic`**: Point OpenCode at this proxy as an Anthropic provider so Anthropic Messages semantics, premium-request optimizations, and Claude-native behavior are preserved end to end.
46
61
  - **Claude Code Integration**: Easily configure and launch [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) to use Copilot as its backend with a simple command-line flag (`--claude-code`).
47
62
  - **Usage Dashboard**: A web-based dashboard to monitor your Copilot API usage, view quotas, and see detailed statistics.
@@ -56,6 +71,8 @@ Compared with routing everything through plain Chat Completions compatibility, t
56
71
  - **GitHub Enterprise Support**: Connect to GHE.com by setting `COPILOT_API_ENTERPRISE_URL` environment variable (e.g., `company.ghe.com`) or using `--enterprise-url=company.ghe.com` command line option.
57
72
  - **Custom Data Directory**: Change the default data directory (where tokens and config are stored) by setting `COPILOT_API_HOME` environment variable or using `--api-home=/path/to/dir` command line option.
58
73
  - **Multi-Provider Anthropic Proxy Routes**: Add global provider configs and call external Anthropic-compatible APIs via `/:provider/v1/messages` and `/:provider/v1/models`.
74
+ - **Accurate Claude Token Counting**: Optionally forward `/v1/messages/count_tokens` requests for Claude models to Anthropic's free token counting endpoint for exact counts instead of GPT tokenizer estimation.
75
+ - **GPT Context Management**: Configurable context compaction for long-running GPT conversations via `responsesApiContextManagementModels`, reducing unnecessary premium requests when approaching token limits. See [Configuration](#configuration-configjson) for details.
59
76
 
60
77
  ## Better Agent Semantics
61
78
 
@@ -76,7 +93,6 @@ Supported `anthropic-beta` values are filtered and forwarded on the native Messa
76
93
  The proxy includes request-accounting safeguards designed for tool-heavy coding workflows:
77
94
 
78
95
  - tool-less warmup or probe requests can be forced onto `smallModel` so background checks do not spend premium usage;
79
- - compact/background requests can be downgraded to `smallModel` automatically;
80
96
  - mixed `tool_result` + reminder text blocks are merged back into the `tool_result` flow instead of being counted like fresh user turns;
81
97
  - `x-initiator` is derived from the latest message or item, not stale assistant history.
82
98
 
@@ -94,7 +110,24 @@ For subagent-based clients, this project can preserve root session context and c
94
110
 
95
111
  The marker flow uses `__SUBAGENT_MARKER__...` inside a `<system-reminder>` block together with root `x-session-id` propagation. When a marker is detected, the proxy can keep the parent session identity, infer `x-initiator: agent`, and tag the interaction as subagent traffic instead of a fresh top-level request.
96
112
 
97
- Optional marker producers are included for both Claude Code and opencode; see [Subagent Marker Integration](#subagent-marker-integration-optional) below for setup details.
113
+ Plugin integrations are included for both Claude Code and opencode; see [Plugin Integrations](#plugin-integrations) below for setup details.
114
+
115
+ ### Accurate Claude token counting
116
+
117
+ By default, `/v1/messages/count_tokens` estimates Claude token counts using the GPT `o200k_base` tokenizer with a 1.15x multiplier. This consistently underestimates actual Claude token usage, which can cause tools like Claude Code to compact too late and hit "prompt token count exceeds limit" errors.
118
+
119
+ When an Anthropic API key is configured, the proxy forwards Claude model token counting requests to [Anthropic's real `/v1/messages/count_tokens` endpoint](https://docs.anthropic.com/en/docs/build-with-claude/token-counting) instead. This returns exact counts and eliminates the estimation mismatch. Non-Claude models and failures fall back to the GPT tokenizer estimation automatically.
120
+
121
+ **Setup:**
122
+
123
+ 1. Create an Anthropic API account at [console.anthropic.com](https://console.anthropic.com) and add a minimum $5 credit balance (required to activate the API key, but the token counting endpoint itself is free)
124
+ 2. Create an API key from Settings > API Keys
125
+ 3. Configure the key via **one** of:
126
+ - `config.json`: set `"anthropicApiKey": "sk-ant-..."`
127
+ - Environment variable: `ANTHROPIC_API_KEY=sk-ant-...`
128
+
129
+ > [!NOTE]
130
+ > Anthropic's `/v1/messages/count_tokens` endpoint is **free** (no per-token cost). It is rate-limited to 100 RPM at Tier 1. The $5 credit purchase is only needed to activate API access — the token counting calls themselves cost nothing.
98
131
 
99
132
  ## Demo
100
133
 
@@ -355,6 +388,7 @@ The `<target>` can be either the account ID (GitHub username) or a 1-based index
355
388
  - **messageStartInputTokensFallback:** When `true`, the Anthropic streaming translation layer estimates `message_start.input_tokens` when upstream stream events do not provide it. Defaults to `false`.
356
389
  - **modelRefreshIntervalHours:** Interval for refreshing account model lists in the background. Set to `0` to disable refresh. Defaults to `24`.
357
390
  - **useMessagesApi:** When `true` (default), Claude-family models that support Copilot's native `/v1/messages` endpoint may use the Messages API path. Set to `false` to skip the Messages API candidate and fall back to `/responses` (if supported) or `/chat/completions`.
391
+ - **anthropicApiKey:** Optional Anthropic API key used for accurate Claude token counting (see [Accurate Claude Token Counting](#accurate-claude-token-counting) below). Can also be set via the `ANTHROPIC_API_KEY` environment variable. If not set, token counting falls back to GPT tokenizer estimation.
358
392
 
359
393
  Edit this file to customize prompts or swap in your own fast model. If you edit it manually, restart the server (or call `GET /api/admin/config`) so the cached config is refreshed. Changes made through the Admin UI/API are validated, written to disk, and applied immediately; unknown keys are rejected.
360
394
 
@@ -758,7 +792,6 @@ Here is an example `.claude/settings.json` file:
758
792
  "ANTHROPIC_DEFAULT_HAIKU_MODEL": "gpt-5-mini",
759
793
  "DISABLE_NON_ESSENTIAL_MODEL_CALLS": "1",
760
794
  "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
761
- "BASH_MAX_TIMEOUT_MS": "600000",
762
795
  "CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
763
796
  "CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION": "false"
764
797
  },
@@ -783,13 +816,13 @@ You can find more options here: [Claude Code settings](https://docs.anthropic.co
783
816
 
784
817
  You can also read more about IDE integration here: [Add Claude Code to your IDE](https://docs.anthropic.com/en/docs/claude-code/ide-integrations)
785
818
 
786
- ## Subagent Marker Integration (Optional)
819
+ ## Plugin Integrations
787
820
 
788
- This project supports `x-initiator: agent` for subagent-originated requests and can preserve the root session identity with `x-session-id` when a subagent marker is present.
821
+ Plugin integrations are available for Claude Code and opencode.
789
822
 
790
- #### Claude Code plugin producer (marketplace-based)
823
+ #### Claude Code plugin integration (marketplace-based)
791
824
 
792
- The marker producer is packaged as a Claude Code plugin named `claude-plugin`.
825
+ The Claude Code integration is packaged as a plugin named `claude-plugin`.
793
826
 
794
827
  - Marketplace catalog in this repository: `.claude-plugin/marketplace.json`
795
828
  - Plugin source in this repository: `claude-plugin`
@@ -808,9 +841,14 @@ Install the plugin from the marketplace:
808
841
 
809
842
  After installation, the plugin injects `__SUBAGENT_MARKER__...` on `SubagentStart`, and this proxy uses it to infer `x-initiator: agent`.
810
843
 
811
- #### Opencode plugin producer
844
+ The plugin also registers a `UserPromptSubmit` hook that returns `{"continue": true}`, and it can inject `SessionStart` reminder rules through environment variables:
845
+
846
+ - `CLAUDE_PLUGIN_ENABLE_QUESTION_RULES=1` enables the two `question`-tool reminders.
847
+ - `CLAUDE_PLUGIN_ENABLE_NO_BACKGROUND_AGENTS_RULE=1` enables the `run_in_background: true` avoidance reminder for agent hooks.
848
+
849
+ #### Opencode plugin
812
850
 
813
- The marker producer is packaged as an opencode plugin located at `.opencode/plugins/subagent-marker.js`.
851
+ The subagent marker producer is packaged as an opencode plugin located at `.opencode/plugins/subagent-marker.js`.
814
852
 
815
853
  **Installation:**
816
854
 
@@ -1,7 +1,7 @@
1
1
  import { PATHS } from "./paths-DoT4SZ8f.js";
2
2
  import { addAccountToRegistry, hasLegacyToken, hasRegistry, listAccountsFromRegistry, loadAccountToken, readLegacyToken, saveAccountToken } from "./accounts-registry-c7rs5Ed9.js";
3
- import { HTTPError, getCopilotUsage, getGitHubUser, getModels } from "./utils-Ce30L8HS.js";
4
- import { getCopilotToken } from "./get-copilot-token-BMypymQn.js";
3
+ import { HTTPError, getCopilotUsage, getGitHubUser, getModels } from "./utils-DkUv9TDj.js";
4
+ import { getCopilotToken } from "./get-copilot-token-CbzXMTE5.js";
5
5
  import consola from "consola";
6
6
  import fs from "node:fs";
7
7
 
@@ -369,6 +369,9 @@ function getProviderConfig(name) {
369
369
  function isMessagesApiEnabled() {
370
370
  return getConfig().useMessagesApi ?? true;
371
371
  }
372
+ function getAnthropicApiKey() {
373
+ return getConfig().anthropicApiKey ?? process.env.ANTHROPIC_API_KEY ?? void 0;
374
+ }
372
375
 
373
376
  //#endregion
374
377
  //#region src/lib/accounts-manager-auth.ts
@@ -1175,5 +1178,5 @@ var AccountsManager = class {
1175
1178
  const accountsManager = new AccountsManager();
1176
1179
 
1177
1180
  //#endregion
1178
- export { PROVIDER_TYPE_ANTHROPIC, accountsManager, getAliasTargetSet, getConfig, getExtraPromptForModel, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getProviderConfig, getReasoningEffortForModel, getSmallModel, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isMessagesApiEnabled, isResponsesApiContextManagementModel, mergeConfigWithDefaults, shouldCompactUseSmallModel };
1179
- //# sourceMappingURL=accounts-manager-CtZD9BhK.js.map
1181
+ export { PROVIDER_TYPE_ANTHROPIC, accountsManager, getAliasTargetSet, getAnthropicApiKey, getConfig, getExtraPromptForModel, getModelAliases, getModelAliasesInfo, getModelRefreshIntervalMs, getProviderConfig, getReasoningEffortForModel, getSmallModel, isForceAgentEnabled, isFreeModelLoadBalancingEnabled, isMessageStartInputTokensFallbackEnabled, isMessagesApiEnabled, isResponsesApiContextManagementModel, mergeConfigWithDefaults, shouldCompactUseSmallModel };
1182
+ //# sourceMappingURL=accounts-manager-BvbpPVp1.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accounts-manager-BvbpPVp1.js","names":["defaultConfig: AppConfig","cachedConfig: AppConfig | null","normalizedTarget","normalized: ModelAliasInfoMap","normalized: ModelAliasMap","runtime: AccountRuntime","accounts: Array<AccountRuntime>","overageFallback:\n | {\n account: AccountRuntime\n model: Model\n endpoint: string\n costUnits: number\n }\n | undefined","statuses: Array<{\n id: string\n entitlement?: number\n remaining?: number\n unlimited?: boolean\n overagePermitted?: boolean\n failed?: boolean\n failureReason?: string\n }>","allAccounts: Array<AccountRuntime>","added: Array<string>","removed: Array<string>","updated: Array<string>","changes: Array<string>"],"sources":["../src/lib/config.ts","../src/lib/accounts-manager-auth.ts","../src/lib/accounts-manager-quota.ts","../src/lib/accounts-manager.ts"],"sourcesContent":["import consola from \"consola\"\nimport fs from \"node:fs\"\n\nimport { PATHS } from \"./paths\"\n\nexport interface AppConfig {\n auth?: {\n apiKeys?: Array<string>\n }\n providers?: Record<string, ProviderConfig>\n extraPrompts?: Record<string, string>\n smallModel?: string\n freeModelLoadBalancing?: boolean\n /** @deprecated */\n apiKey?: string\n responsesApiContextManagementModels?: Array<string>\n modelReasoningEfforts?: Record<\n string,\n \"none\" | \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\"\n >\n modelAliases?: Record<string, { target: string; allowOriginal?: boolean }>\n allowOriginalModelNamesForAliases?: boolean\n useFunctionApplyPatch?: boolean\n forceAgent?: boolean\n compactUseSmallModel?: boolean\n messageStartInputTokensFallback?: boolean\n modelRefreshIntervalHours?: number\n useMessagesApi?: boolean\n anthropicApiKey?: string\n}\n\nexport interface ModelConfig {\n temperature?: number\n topP?: number\n topK?: number\n}\n\nexport const PROVIDER_TYPE_ANTHROPIC = \"anthropic\" as const\n\nexport type ProviderType = typeof PROVIDER_TYPE_ANTHROPIC\n\nexport interface ProviderConfig {\n type?: string\n enabled?: boolean\n baseUrl?: string\n apiKey?: string\n models?: Record<string, ModelConfig>\n adjustInputTokens?: boolean\n}\n\nexport interface ResolvedProviderConfig {\n name: string\n type: ProviderType\n baseUrl: string\n apiKey: string\n models?: Record<string, ModelConfig>\n adjustInputTokens?: boolean\n}\n\nconst gpt5ExplorationPrompt = `## Exploration and reading files\n- **Think first.** Before any tool call, decide ALL files/resources you will need.\n- **Batch everything.** If you need multiple files (even from different places), read them together.\n- **multi_tool_use.parallel** Use multi_tool_use.parallel to parallelize tool calls and only this.\n- **Only make sequential calls if you truly cannot know the next file without seeing a result first.**\n- **Workflow:** (a) plan all needed reads → (b) issue one parallel batch → (c) analyze results → (d) repeat if new, unpredictable reads arise.`\n\nconst gpt5CommentaryPrompt = `# Working with the user\n\nYou interact with the user through a terminal. You have 2 ways of communicating with the users: \n- Share intermediary updates in \\`commentary\\` channel. \n- After you have completed all your work, send a message to the \\`final\\` channel. \n\n## Intermediary updates\n\n- Intermediary updates go to the \\`commentary\\` channel.\n- User updates are short updates while you are working, they are NOT final answers.\n- You use 1-2 sentence user updates to communicate progress and new information to the user as you are doing work.\n- Do not begin responses with conversational interjections or meta commentary. Avoid openers such as acknowledgements (“Done —”, “Got it”, “Great question, ”) or framing phrases.\n- You provide user updates frequently, every 20s.\n- Before exploring or doing substantial work, you start with a user update acknowledging the request and explaining your first step. You should include your understanding of the user request and explain what you will do. Avoid commenting on the request or using starters such as \"Got it -\" or \"Understood -\" etc.\n- When exploring, e.g. searching, reading files, you provide user updates as you go, every 20s, explaining what context you are gathering and what you've learned. Vary your sentence structure when providing these updates to avoid sounding repetitive - in particular, don't start each sentence the same way.\n- After you have sufficient context, and the work is substantial, you provide a longer plan (this is the only user update that may be longer than 2 sentences and can contain formatting).\n- Before performing file edits of any kind, you provide updates explaining what edits you are making.\n- As you are thinking, you very frequently provide updates even if not taking any actions, informing the user of your progress. You interrupt your thinking and send multiple updates in a row if thinking for more than 100 words.\n- Tone of your updates MUST match your personality.`\n\nconst defaultConfig: AppConfig = {\n auth: {\n apiKeys: [],\n },\n providers: {},\n extraPrompts: {\n \"gpt-5-mini\": gpt5ExplorationPrompt,\n \"gpt-5.3-codex\": gpt5CommentaryPrompt,\n \"gpt-5.4-mini\": gpt5CommentaryPrompt,\n \"gpt-5.4\": gpt5CommentaryPrompt,\n },\n smallModel: \"gpt-5-mini\",\n freeModelLoadBalancing: true,\n responsesApiContextManagementModels: [],\n modelReasoningEfforts: {\n \"gpt-5-mini\": \"low\",\n \"gpt-5.3-codex\": \"xhigh\",\n \"gpt-5.4-mini\": \"xhigh\",\n \"gpt-5.4\": \"xhigh\",\n },\n allowOriginalModelNamesForAliases: false,\n useFunctionApplyPatch: true,\n compactUseSmallModel: true,\n messageStartInputTokensFallback: false,\n modelRefreshIntervalHours: 24,\n useMessagesApi: true,\n}\n\nlet cachedConfig: AppConfig | null = null\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value)\n}\n\nfunction normalizeAuthApiKeys(value: unknown): Array<string> {\n if (!Array.isArray(value)) return []\n\n return [\n ...new Set(\n value\n .filter((item): item is string => typeof item === \"string\")\n .map((item) => item.trim())\n .filter((item) => item.length > 0),\n ),\n ]\n}\n\nfunction normalizeModelRefreshIntervalHours(\n value: unknown,\n): number | undefined {\n if (typeof value !== \"number\") return undefined\n if (!Number.isFinite(value)) return undefined\n if (value < 0) return undefined\n return value\n}\n\nfunction ensureConfigFile(): void {\n try {\n fs.accessSync(PATHS.CONFIG_PATH, fs.constants.R_OK)\n return\n } catch {\n // Fall through to try creating the default config file.\n }\n\n try {\n fs.mkdirSync(PATHS.APP_DIR, { recursive: true })\n fs.writeFileSync(\n PATHS.CONFIG_PATH,\n `${JSON.stringify(defaultConfig, null, 2)}\\n`,\n \"utf8\",\n )\n try {\n fs.chmodSync(PATHS.CONFIG_PATH, 0o600)\n } catch {\n // Ignore chmod errors (e.g. unsupported filesystem).\n }\n } catch {\n // Best-effort only: if we can't create the file, reads will fall back to defaults.\n }\n}\n\nfunction readConfigFromDisk(): AppConfig {\n ensureConfigFile()\n try {\n const raw = fs.readFileSync(PATHS.CONFIG_PATH, \"utf8\")\n if (!raw.trim()) {\n fs.writeFileSync(\n PATHS.CONFIG_PATH,\n `${JSON.stringify(defaultConfig, null, 2)}\\n`,\n \"utf8\",\n )\n return defaultConfig\n }\n return JSON.parse(raw) as AppConfig\n } catch (error) {\n consola.error(\"Failed to read config file, using default config\", error)\n return defaultConfig\n }\n}\n\nfunction mergeDefaultConfig(config: AppConfig): {\n mergedConfig: AppConfig\n changed: boolean\n} {\n const extraPrompts = config.extraPrompts ?? {}\n const defaultExtraPrompts = defaultConfig.extraPrompts ?? {}\n const modelReasoningEfforts = config.modelReasoningEfforts ?? {}\n const defaultModelReasoningEfforts = defaultConfig.modelReasoningEfforts ?? {}\n\n const missingExtraPromptModels = Object.keys(defaultExtraPrompts).filter(\n (model) => !Object.hasOwn(extraPrompts, model),\n )\n\n const missingReasoningEffortModels = Object.keys(\n defaultModelReasoningEfforts,\n ).filter((model) => !Object.hasOwn(modelReasoningEfforts, model))\n\n const hasExtraPromptChanges = missingExtraPromptModels.length > 0\n const hasReasoningEffortChanges = missingReasoningEffortModels.length > 0\n\n if (!hasExtraPromptChanges && !hasReasoningEffortChanges) {\n return { mergedConfig: config, changed: false }\n }\n\n return {\n mergedConfig: {\n ...config,\n extraPrompts: {\n ...defaultExtraPrompts,\n ...extraPrompts,\n },\n modelReasoningEfforts: {\n ...defaultModelReasoningEfforts,\n ...modelReasoningEfforts,\n },\n },\n changed: true,\n }\n}\n\nfunction mergeDefaultAuth(config: AppConfig): {\n mergedConfig: AppConfig\n changed: boolean\n} {\n const authConfig = isPlainObject(config.auth) ? config.auth : undefined\n const rawApiKeys =\n Array.isArray(authConfig?.apiKeys) ? authConfig.apiKeys : undefined\n const normalizedApiKeys = normalizeAuthApiKeys(rawApiKeys)\n const nextAuth = { apiKeys: normalizedApiKeys }\n\n if (authConfig && JSON.stringify(authConfig) === JSON.stringify(nextAuth)) {\n return { mergedConfig: config, changed: false }\n }\n\n return {\n mergedConfig: {\n ...config,\n auth: nextAuth,\n },\n changed: true,\n }\n}\n\nfunction mergeDefaultFreeModelLoadBalancing(config: AppConfig): {\n mergedConfig: AppConfig\n changed: boolean\n} {\n if (typeof config.freeModelLoadBalancing === \"boolean\") {\n return { mergedConfig: config, changed: false }\n }\n\n return {\n mergedConfig: {\n ...config,\n freeModelLoadBalancing: defaultConfig.freeModelLoadBalancing ?? true,\n },\n changed: true,\n }\n}\n\nfunction mergeDefaultModelRefreshInterval(config: AppConfig): {\n mergedConfig: AppConfig\n changed: boolean\n} {\n const normalized = normalizeModelRefreshIntervalHours(\n config.modelRefreshIntervalHours,\n )\n\n if (normalized !== undefined) {\n return { mergedConfig: config, changed: false }\n }\n\n return {\n mergedConfig: {\n ...config,\n modelRefreshIntervalHours: defaultConfig.modelRefreshIntervalHours ?? 24,\n },\n changed: true,\n }\n}\n\ntype ConfigMergeResult = {\n mergedConfig: AppConfig\n changed: boolean\n}\n\ntype ConfigMergeFn = (config: AppConfig) => ConfigMergeResult\n\nfunction applyConfigMerges(\n config: AppConfig,\n mergeFns: ReadonlyArray<ConfigMergeFn>,\n): ConfigMergeResult {\n return mergeFns.reduce<ConfigMergeResult>(\n (acc, mergeFn) => {\n const result = mergeFn(acc.mergedConfig)\n return {\n mergedConfig: result.mergedConfig,\n changed: acc.changed || result.changed,\n }\n },\n { mergedConfig: config, changed: false },\n )\n}\n\nexport function mergeConfigWithDefaults(): AppConfig {\n const config = readConfigFromDisk()\n\n const { mergedConfig, changed } = applyConfigMerges(config, [\n mergeDefaultAuth,\n mergeDefaultConfig,\n mergeDefaultFreeModelLoadBalancing,\n mergeDefaultModelRefreshInterval,\n ])\n\n if (changed) {\n try {\n fs.writeFileSync(\n PATHS.CONFIG_PATH,\n `${JSON.stringify(mergedConfig, null, 2)}\\n`,\n \"utf8\",\n )\n } catch (writeError) {\n consola.warn(\"Failed to write merged config defaults\", writeError)\n }\n }\n\n cachedConfig = mergedConfig\n return mergedConfig\n}\n\nexport function getConfig(): AppConfig {\n cachedConfig ??= readConfigFromDisk()\n return cachedConfig\n}\n\ntype ModelAliasSpec = {\n target: string\n allowOriginal?: boolean\n}\n\ntype ModelAliasMap = Record<string, string>\n\ntype ModelAliasInfoMap = Record<string, ModelAliasSpec>\n\ntype ModelAliasRawMap = Record<string, unknown>\n\nfunction normalizeAliasKey(value: string): string | null {\n const trimmed = value.trim().toLowerCase()\n return trimmed.length > 0 ? trimmed : null\n}\n\nfunction normalizeAliasTarget(value: string): string | null {\n const trimmed = value.trim()\n return trimmed.length > 0 ? trimmed : null\n}\n\nfunction normalizeAliasSpec(value: unknown): ModelAliasSpec | null {\n if (typeof value === \"string\") {\n const normalizedTarget = normalizeAliasTarget(value)\n return normalizedTarget ? { target: normalizedTarget } : null\n }\n if (!value || typeof value !== \"object\") {\n return null\n }\n\n const targetValue = (value as { target?: unknown }).target\n if (typeof targetValue !== \"string\") {\n return null\n }\n\n const normalizedTarget = normalizeAliasTarget(targetValue)\n if (!normalizedTarget) {\n return null\n }\n\n const allowOriginalValue = (value as { allowOriginal?: unknown })\n .allowOriginal\n const allowOriginal =\n typeof allowOriginalValue === \"boolean\" ? allowOriginalValue : undefined\n return { target: normalizedTarget, allowOriginal }\n}\n\nexport function getModelAliasesInfo(): ModelAliasInfoMap {\n const config = getConfig()\n const raw = (config.modelAliases ?? {}) as ModelAliasRawMap\n const normalized: ModelAliasInfoMap = {}\n\n for (const [alias, rawSpec] of Object.entries(raw)) {\n const normalizedAlias = normalizeAliasKey(alias)\n const normalizedSpec = normalizeAliasSpec(rawSpec)\n if (!normalizedAlias || !normalizedSpec) {\n continue\n }\n if (!Object.hasOwn(normalized, normalizedAlias)) {\n normalized[normalizedAlias] = normalizedSpec\n }\n }\n\n return normalized\n}\n\nexport function getModelAliases(): ModelAliasMap {\n const info = getModelAliasesInfo()\n const normalized: ModelAliasMap = {}\n\n for (const [alias, spec] of Object.entries(info)) {\n normalized[alias] = spec.target\n }\n\n return normalized\n}\n\nexport function resolveModelAlias(modelId: string): string {\n const normalized = normalizeAliasKey(modelId)\n if (!normalized) return modelId\n const aliases = getModelAliases()\n return aliases[normalized] ?? modelId\n}\n\nexport function isOriginalModelNameAllowedForAliases(): boolean {\n const config = getConfig()\n return config.allowOriginalModelNamesForAliases ?? false\n}\n\nexport function getAliasTargetSet(): Set<string> {\n const aliases = getModelAliasesInfo()\n const allowOriginalDefault = isOriginalModelNameAllowedForAliases()\n const targetAllowMap = new Map<string, boolean>()\n\n for (const { target, allowOriginal } of Object.values(aliases)) {\n const normalizedTarget = target.toLowerCase()\n const effectiveAllow = allowOriginal ?? allowOriginalDefault\n const currentAllow = targetAllowMap.get(normalizedTarget)\n if (currentAllow === true) {\n continue\n }\n if (effectiveAllow) {\n targetAllowMap.set(normalizedTarget, true)\n } else if (currentAllow === undefined) {\n targetAllowMap.set(normalizedTarget, false)\n }\n }\n\n const blockedTargets = new Set<string>()\n for (const [target, allowed] of targetAllowMap.entries()) {\n if (!allowed) {\n blockedTargets.add(target)\n }\n }\n\n return blockedTargets\n}\n\nexport function isOriginalModelNameAllowedForTarget(modelId: string): boolean {\n const normalized = normalizeAliasKey(modelId)\n if (!normalized) return true\n const blockedTargets = getAliasTargetSet()\n return !blockedTargets.has(normalized)\n}\n\nexport function getPreferredAliasForTarget(modelId: string): string | null {\n const aliases = getModelAliases()\n const aliasKeys = getAliasKeysForTarget(modelId, aliases)\n return aliasKeys[0] ?? null\n}\n\nfunction getAliasKeysForTarget(\n target: string,\n aliases: ModelAliasMap,\n): Array<string> {\n const normalizedTarget = target.toLowerCase()\n return Object.entries(aliases)\n .filter(([, model]) => model.toLowerCase() === normalizedTarget)\n .map(([alias]) => alias)\n .sort()\n}\n\nfunction getAliasFallbackValue<T extends string>(\n record: Record<string, T> | undefined,\n modelId: string,\n aliases: ModelAliasMap,\n): T | undefined {\n if (!record) return undefined\n\n const aliasKeys = getAliasKeysForTarget(modelId, aliases)\n if (aliasKeys.length === 0) return undefined\n\n const recordByAlias = new Map<string, T>()\n for (const [key, value] of Object.entries(record)) {\n const normalized = normalizeAliasKey(key)\n if (normalized) {\n recordByAlias.set(normalized, value)\n }\n }\n\n for (const alias of aliasKeys) {\n const value = recordByAlias.get(alias)\n if (value !== undefined) {\n return value\n }\n }\n\n return undefined\n}\n\nexport function getExtraPromptForModel(model: string): string {\n const config = getConfig()\n const direct = config.extraPrompts?.[model]\n if (direct !== undefined) return direct\n\n const aliases = getModelAliases()\n const fallback = getAliasFallbackValue(config.extraPrompts, model, aliases)\n return fallback ?? \"\"\n}\n\nexport function getSmallModel(): string {\n const config = getConfig()\n const model = config.smallModel ?? \"gpt-5-mini\"\n if (isOriginalModelNameAllowedForTarget(model)) {\n return model\n }\n\n return getPreferredAliasForTarget(model) ?? model\n}\n\nexport function isFreeModelLoadBalancingEnabled(): boolean {\n const config = getConfig()\n return config.freeModelLoadBalancing ?? true\n}\n\nexport function getModelRefreshIntervalHours(): number {\n const config = getConfig()\n const normalized = normalizeModelRefreshIntervalHours(\n config.modelRefreshIntervalHours,\n )\n return normalized ?? defaultConfig.modelRefreshIntervalHours ?? 24\n}\n\nexport function getModelRefreshIntervalMs(): number {\n const hours = getModelRefreshIntervalHours()\n if (!Number.isFinite(hours) || hours <= 0) return 0\n return hours * 60 * 60 * 1000\n}\n\nexport function isMessageStartInputTokensFallbackEnabled(): boolean {\n const config = getConfig()\n return config.messageStartInputTokensFallback ?? false\n}\n\nexport function shouldCompactUseSmallModel(): boolean {\n const config = getConfig()\n return config.compactUseSmallModel ?? true\n}\n\nexport function getResponsesApiContextManagementModels(): Array<string> {\n const config = getConfig()\n return (\n config.responsesApiContextManagementModels\n ?? defaultConfig.responsesApiContextManagementModels\n ?? []\n )\n}\n\nexport function isResponsesApiContextManagementModel(model: string): boolean {\n return getResponsesApiContextManagementModels().includes(model)\n}\n\nexport function getReasoningEffortForModel(\n model: string,\n): \"none\" | \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" {\n const config = getConfig()\n const direct = config.modelReasoningEfforts?.[model]\n if (direct !== undefined) return direct\n\n const aliases = getModelAliases()\n const fallback = getAliasFallbackValue(\n config.modelReasoningEfforts,\n model,\n aliases,\n )\n return fallback ?? \"high\"\n}\n\nexport function isForceAgentEnabled(): boolean {\n const config = getConfig()\n return config.forceAgent ?? false\n}\n\nexport function normalizeProviderBaseUrl(url: string): string {\n return url.trim().replace(/\\/+$/u, \"\")\n}\n\nexport function getProviderConfig(name: string): ResolvedProviderConfig | null {\n const providerName = name.trim()\n if (!providerName) {\n return null\n }\n\n const config = getConfig()\n const provider = config.providers?.[providerName]\n if (!provider) {\n return null\n }\n\n if (provider.enabled === false) {\n return null\n }\n\n const type = provider.type ?? PROVIDER_TYPE_ANTHROPIC\n if (type !== PROVIDER_TYPE_ANTHROPIC) {\n consola.warn(\n `Provider ${providerName} is ignored because only anthropic type is supported`,\n )\n return null\n }\n\n const baseUrl = normalizeProviderBaseUrl(provider.baseUrl ?? \"\")\n const apiKey = (provider.apiKey ?? \"\").trim()\n if (!baseUrl || !apiKey) {\n consola.warn(\n `Provider ${providerName} is enabled but missing baseUrl or apiKey`,\n )\n return null\n }\n\n return {\n name: providerName,\n type: PROVIDER_TYPE_ANTHROPIC,\n baseUrl,\n apiKey,\n models: provider.models,\n adjustInputTokens: provider.adjustInputTokens,\n }\n}\n\nexport function listEnabledProviders(): Array<string> {\n const config = getConfig()\n const providerNames = Object.keys(config.providers ?? {})\n return providerNames.filter((name) => getProviderConfig(name) !== null)\n}\n\nexport function isMessagesApiEnabled(): boolean {\n const config = getConfig()\n return config.useMessagesApi ?? true\n}\n\nexport function getAnthropicApiKey(): string | undefined {\n const config = getConfig()\n return config.anthropicApiKey ?? process.env.ANTHROPIC_API_KEY ?? undefined\n}\n","import consola from \"consola\"\n\nimport type {\n AccountContext,\n AccountRuntime,\n AccountType,\n} from \"~/lib/types/account\"\nimport type { QuotaDetail } from \"~/services/github/get-copilot-usage\"\n\nexport type AuthSnapshot = Readonly<{\n githubToken: string\n accountType: AccountType\n}>\n\nexport const takeAuthSnapshot = (account: AccountRuntime): AuthSnapshot => ({\n githubToken: account.githubToken,\n accountType: account.accountType,\n})\n\nexport const isAuthSnapshotCurrent = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n): boolean =>\n account.githubToken === snapshot.githubToken\n && account.accountType === snapshot.accountType\n\nexport const isSameAuthSnapshot = (\n a: AuthSnapshot | undefined,\n b: AuthSnapshot,\n): boolean => {\n if (!a) return false\n return a.githubToken === b.githubToken && a.accountType === b.accountType\n}\n\nexport const toAccountContextFromSnapshot = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n copilotToken?: string,\n): AccountContext => ({\n githubToken: snapshot.githubToken,\n copilotToken,\n accountType: snapshot.accountType,\n vsCodeVersion: account.vsCodeVersion,\n})\n\nexport const applyCopilotTokenIfCurrent = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n copilotToken: string,\n): boolean => {\n if (!isAuthSnapshotCurrent(account, snapshot)) {\n return false\n }\n\n account.copilotToken = copilotToken\n return true\n}\n\nexport const applyModelsIfCurrent = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n models: AccountRuntime[\"models\"],\n): boolean => {\n if (!isAuthSnapshotCurrent(account, snapshot)) {\n return false\n }\n\n account.models = models\n return true\n}\n\nexport const applyTokenRefreshSuccessIfCurrent = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n token: string,\n): boolean => {\n if (!isAuthSnapshotCurrent(account, snapshot)) {\n return false\n }\n\n account.copilotToken = token\n account.failed = false\n account.failureReason = undefined\n return true\n}\n\nexport const applyTokenRefreshFailureIfCurrent = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n error: unknown,\n): boolean => {\n if (!isAuthSnapshotCurrent(account, snapshot)) {\n return false\n }\n\n account.failed = true\n account.failureReason = String(error)\n return true\n}\n\nexport const applyQuotaRefreshSuccessIfCurrent = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n premium: QuotaDetail,\n): boolean => {\n if (!isAuthSnapshotCurrent(account, snapshot)) {\n return false\n }\n\n account.premiumEntitlement = premium.entitlement\n account.premiumRemaining = premium.remaining\n account.unlimited = premium.unlimited\n account.overagePermitted = premium.overage_permitted\n account.lastQuotaFetch = Date.now()\n account.failed = false\n account.failureReason = undefined\n return true\n}\n\nexport const setAccountFailedState = (\n account: AccountRuntime,\n reason: string,\n): void => {\n account.failed = true\n account.failureReason = reason\n consola.warn(`Account ${account.id} marked as failed: ${reason}`)\n}\n\nexport const applyUnauthorizedIfCurrent = (\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n reason: string,\n): boolean => {\n if (!isAuthSnapshotCurrent(account, snapshot)) {\n return false\n }\n\n setAccountFailedState(account, reason)\n return true\n}\n","import type { AccountRuntime } from \"~/lib/types/account\"\nimport type { Model } from \"~/services/copilot/get-models\"\n\nexport type QuotaReservation = Readonly<{ id: symbol }>\n\nexport const getCostUnits = (model: Model): number => {\n // Per user decision: missing billing => treat as free (costUnits = 0)\n const billing = model.billing\n if (!billing) {\n return 0\n }\n\n if (billing.is_premium !== true) {\n return 0\n }\n\n const multiplier = billing.multiplier\n if (\n typeof multiplier !== \"number\"\n || !Number.isFinite(multiplier)\n || multiplier <= 0\n ) {\n return 1\n }\n\n return multiplier\n}\n\nexport const getEffectivePremiumRemaining = (\n account: AccountRuntime,\n): number | undefined => {\n if (account.premiumRemaining === undefined) {\n return undefined\n }\n\n const reserved = account.premiumReserved ?? 0\n return account.premiumRemaining - reserved\n}\n\nexport const reservePremiumUnits = (\n account: AccountRuntime,\n units: number,\n): QuotaReservation | undefined => {\n if (units <= 0) {\n return undefined\n }\n\n const id = Symbol(\"quotaReservation\")\n\n if (!account.premiumReservations) {\n account.premiumReservations = new Map()\n }\n\n account.premiumReservations.set(id, units)\n account.premiumReserved = (account.premiumReserved ?? 0) + units\n\n return { id }\n}\n\nexport const releasePremiumReservation = (\n account: AccountRuntime,\n reservation?: QuotaReservation,\n): void => {\n if (!reservation) {\n return\n }\n\n const reservations = account.premiumReservations\n if (!reservations) {\n return\n }\n\n const reservedUnits = reservations.get(reservation.id)\n if (reservedUnits === undefined) {\n return\n }\n\n reservations.delete(reservation.id)\n\n const nextReserved = (account.premiumReserved ?? 0) - reservedUnits\n account.premiumReserved = Math.max(0, nextReserved)\n\n if (reservations.size === 0) {\n account.premiumReservations = undefined\n }\n}\n","/* eslint-disable max-lines */\nimport consola from \"consola\"\nimport fs from \"node:fs\"\n\nimport type {\n AccountContext,\n AccountRuntime,\n AccountType,\n} from \"~/lib/types/account\"\n\nimport { resolveModelAlias } from \"~/lib/config\"\nimport { HTTPError } from \"~/lib/error\"\nimport { getModels, type Model } from \"~/services/copilot/get-models\"\nimport { getCopilotToken } from \"~/services/github/get-copilot-token\"\nimport { getCopilotUsage } from \"~/services/github/get-copilot-usage\"\nimport { getGitHubUser } from \"~/services/github/get-user\"\n\nimport {\n applyCopilotTokenIfCurrent,\n applyModelsIfCurrent,\n applyQuotaRefreshSuccessIfCurrent,\n applyTokenRefreshFailureIfCurrent,\n applyTokenRefreshSuccessIfCurrent,\n applyUnauthorizedIfCurrent,\n isAuthSnapshotCurrent,\n isSameAuthSnapshot,\n setAccountFailedState,\n takeAuthSnapshot,\n toAccountContextFromSnapshot,\n type AuthSnapshot,\n} from \"./accounts-manager-auth\"\nimport {\n getCostUnits,\n getEffectivePremiumRemaining,\n releasePremiumReservation,\n reservePremiumUnits,\n type QuotaReservation,\n} from \"./accounts-manager-quota\"\nimport {\n hasLegacyToken,\n hasRegistry,\n listAccountsFromRegistry,\n loadAccountToken,\n readLegacyToken,\n saveAccountToken,\n addAccountToRegistry,\n} from \"./accounts-registry\"\nimport { PATHS } from \"./paths\"\n\n/** Quota cache TTL in milliseconds (45 seconds) for pre-request selection. */\nconst QUOTA_CACHE_TTL = 45 * 1000\n\n/** Debounce delay for registry reload in milliseconds */\nconst RELOAD_DEBOUNCE_MS = 500\n\n/** Registry watcher restart initial delay in milliseconds */\nconst WATCHER_RESTART_INITIAL_DELAY_MS = 1000\n/** Registry watcher restart max delay in milliseconds */\nconst WATCHER_RESTART_MAX_DELAY_MS = 60 * 1000\n\nexport interface AccountRequestCandidate {\n modelId: string\n endpoint: string\n}\n\nexport type { QuotaReservation } from \"./accounts-manager-quota\"\n\nexport type SelectAccountForRequestFailureReason =\n | \"NO_ACCOUNTS\"\n | \"MODEL_NOT_SUPPORTED\"\n | \"NO_QUOTA\"\n\ntype SelectAccountForRequestSuccess = {\n ok: true\n account: AccountRuntime\n selectedModel: Model\n endpoint: string\n costUnits: number\n reservation?: QuotaReservation\n}\n\nexport type SelectAccountForRequestResult =\n | SelectAccountForRequestSuccess\n | {\n ok: false\n reason: SelectAccountForRequestFailureReason\n }\n\n/** Manages multiple GitHub Copilot accounts at runtime. */\nexport class AccountsManager {\n private accounts: Map<string, AccountRuntime> = new Map()\n private accountOrder: Array<string> = []\n private temporaryAccount?: AccountRuntime\n private vsCodeVersion?: string\n private freeModelCursor = 0\n private freeModelLoadBalancingEnabled = true\n\n private quotaRefreshSnapshotByAccount = new WeakMap<\n AccountRuntime,\n AuthSnapshot\n >()\n private modelsRefreshSnapshotByAccount = new WeakMap<\n AccountRuntime,\n AuthSnapshot\n >()\n private tokenRefreshEnabledAccounts = new WeakSet<AccountRuntime>()\n private modelsRefreshTimer?: ReturnType<typeof setTimeout>\n private modelsRefreshIntervalMs = 0\n\n // Registry file watcher for hot reload\n private registryWatcher?: fs.FSWatcher\n private reloadDebounceTimer?: ReturnType<typeof setTimeout>\n private registryWatcherRestartTimer?: ReturnType<typeof setTimeout>\n private registryWatcherRestartDelayMs = WATCHER_RESTART_INITIAL_DELAY_MS\n private isReloading = false\n\n /** Initialize accounts manager (load registry, migrate legacy token). */\n async initialize(vsCodeVersion?: string): Promise<void> {\n this.vsCodeVersion = vsCodeVersion\n\n // Check if we need to migrate legacy token\n const hasReg = await hasRegistry()\n const hasLegacy = await hasLegacyToken()\n\n if (!hasReg && hasLegacy) {\n await this.migrateLegacyToken()\n }\n\n // Load accounts from registry\n const accountMetas = await listAccountsFromRegistry()\n\n for (const meta of accountMetas) {\n const token = await loadAccountToken(meta.id)\n if (!token) {\n consola.warn(`No token found for account ${meta.id}, skipping`)\n continue\n }\n\n const runtime: AccountRuntime = {\n ...meta,\n githubToken: token,\n vsCodeVersion: this.vsCodeVersion,\n }\n\n this.accounts.set(meta.id, runtime)\n this.accountOrder.push(meta.id)\n }\n\n // Initialize Copilot tokens for all accounts\n for (const account of this.accounts.values()) {\n try {\n await this.initializeAccount(account)\n } catch (error) {\n consola.error(`Failed to initialize account ${account.id}:`, error)\n account.failed = true\n account.failureReason = String(error)\n }\n }\n\n consola.info(`Loaded ${this.accounts.size} account(s)`)\n\n // Start watching the registry file for hot reload\n this.startRegistryWatcher()\n }\n\n setFreeModelLoadBalancingEnabled(enabled: boolean): void {\n this.freeModelLoadBalancingEnabled = enabled\n }\n\n setModelsRefreshIntervalMs(intervalMs: number): void {\n this.modelsRefreshIntervalMs =\n Number.isFinite(intervalMs) && intervalMs > 0 ? intervalMs : 0\n this.scheduleModelsRefresh()\n }\n\n private computeTokenRefreshDelayMs(refreshInSeconds: number): number {\n return Math.max((refreshInSeconds - 60) * 1000, 1000)\n }\n\n private shouldContinueTokenRefresh(\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n ): boolean {\n return (\n this.tokenRefreshEnabledAccounts.has(account)\n && isAuthSnapshotCurrent(account, snapshot)\n )\n }\n\n private async runTokenRefreshTick(\n account: AccountRuntime,\n snapshot: AuthSnapshot,\n refreshInSeconds: number,\n ): Promise<void> {\n if (!this.shouldContinueTokenRefresh(account, snapshot)) {\n return\n }\n\n try {\n const ctx = toAccountContextFromSnapshot(account, snapshot)\n const { token, refresh_in } = await getCopilotToken(ctx)\n\n if (!this.shouldContinueTokenRefresh(account, snapshot)) {\n return\n }\n\n const applied = applyTokenRefreshSuccessIfCurrent(\n account,\n snapshot,\n token,\n )\n if (!applied) {\n return\n }\n\n consola.debug(`Refreshed token for account ${account.id}`)\n\n // Schedule next refresh using the new refresh interval.\n if (!this.shouldContinueTokenRefresh(account, snapshot)) {\n return\n }\n this.startTokenRefresh(account, refresh_in)\n } catch (error) {\n consola.error(`Failed to refresh token for ${account.id}:`, error)\n\n if (!this.shouldContinueTokenRefresh(account, snapshot)) {\n return\n }\n\n applyTokenRefreshFailureIfCurrent(account, snapshot, error)\n\n // Retry using the previous refresh interval (best effort).\n if (!this.shouldContinueTokenRefresh(account, snapshot)) {\n return\n }\n this.startTokenRefresh(account, refreshInSeconds)\n }\n }\n\n private finalizeQuotaRefreshPromise(\n account: AccountRuntime,\n promise: Promise<void>,\n ): void {\n if (account.quotaRefreshPromise !== promise) {\n return\n }\n\n account.isRefreshingQuota = false\n account.quotaRefreshPromise = undefined\n this.quotaRefreshSnapshotByAccount.delete(account)\n }\n\n /** Initialize a single account. */\n private async initializeAccount(account: AccountRuntime): Promise<void> {\n const snapshot = takeAuthSnapshot(account)\n\n try {\n // Get Copilot token\n const tokenCtx = toAccountContextFromSnapshot(account, snapshot)\n const { token, refresh_in } = await getCopilotToken(tokenCtx)\n\n if (!applyCopilotTokenIfCurrent(account, snapshot, token)) {\n return\n }\n\n // Start token refresh timer\n this.startTokenRefresh(account, refresh_in)\n\n // Get models\n const modelsCtx = toAccountContextFromSnapshot(account, snapshot, token)\n const models = await getModels(modelsCtx)\n\n if (!applyModelsIfCurrent(account, snapshot, models)) {\n return\n }\n account.lastModelsFetch = Date.now()\n\n // Refresh quota\n await this.refreshQuota(account)\n\n consola.debug(`Account ${account.id} initialized`)\n } catch (error) {\n // Ignore stale results if registry hot reload changed auth.\n if (!isAuthSnapshotCurrent(account, snapshot)) {\n return\n }\n\n throw error\n }\n }\n\n /** Migrate legacy github_token to the new multi-account system. */\n private async migrateLegacyToken(): Promise<void> {\n const token = await readLegacyToken()\n if (!token) return\n\n try {\n // Get user info to determine the account ID\n const user = await getGitHubUser({\n githubToken: token,\n accountType: \"individual\",\n })\n const id = user.login\n\n // Save token to new location\n await saveAccountToken(id, token)\n\n // Add to registry\n await addAccountToRegistry({\n id,\n accountType: \"individual\",\n addedAt: Date.now(),\n })\n\n consola.info(`Migrated legacy token to account: ${id}`)\n } catch (error) {\n consola.error(\"Failed to migrate legacy token:\", error)\n }\n }\n\n /** Start token refresh timer for an account. */\n private startTokenRefresh(\n account: AccountRuntime,\n refreshInSeconds: number,\n ): void {\n // Stop existing timer if any\n this.stopTokenRefresh(account)\n\n this.tokenRefreshEnabledAccounts.add(account)\n\n const snapshot = takeAuthSnapshot(account)\n const delayMs = this.computeTokenRefreshDelayMs(refreshInSeconds)\n\n account.refreshTimer = setTimeout(() => {\n void this.runTokenRefreshTick(account, snapshot, refreshInSeconds)\n }, delayMs)\n }\n\n /** Stop token refresh timer for an account. */\n private stopTokenRefresh(account: AccountRuntime): void {\n this.tokenRefreshEnabledAccounts.delete(account)\n\n if (account.refreshTimer) {\n clearTimeout(account.refreshTimer)\n account.refreshTimer = undefined\n }\n }\n\n /** Stop all token refresh timers. */\n private stopAllTokenRefresh(): void {\n for (const account of this.accounts.values()) {\n this.stopTokenRefresh(account)\n }\n if (this.temporaryAccount) {\n this.stopTokenRefresh(this.temporaryAccount)\n }\n }\n\n private scheduleModelsRefresh(): void {\n this.stopModelsRefresh()\n\n if (!this.modelsRefreshIntervalMs || this.modelsRefreshIntervalMs <= 0) {\n return\n }\n\n this.modelsRefreshTimer = setTimeout(() => {\n void this.runModelsRefreshTick()\n }, this.modelsRefreshIntervalMs)\n }\n\n private stopModelsRefresh(): void {\n if (this.modelsRefreshTimer) {\n clearTimeout(this.modelsRefreshTimer)\n this.modelsRefreshTimer = undefined\n }\n }\n\n private async runModelsRefreshTick(): Promise<void> {\n try {\n await this.refreshAllModels()\n } catch (error) {\n consola.error(\"Failed to refresh models:\", error)\n } finally {\n this.scheduleModelsRefresh()\n }\n }\n\n private finalizeModelsRefreshPromise(\n account: AccountRuntime,\n promise: Promise<void>,\n ): void {\n if (account.modelsRefreshPromise !== promise) {\n return\n }\n\n account.isRefreshingModels = false\n account.modelsRefreshPromise = undefined\n this.modelsRefreshSnapshotByAccount.delete(account)\n }\n\n private async refreshModels(account: AccountRuntime): Promise<void> {\n if (!account.copilotToken) {\n consola.debug(\n `Skip model refresh for ${account.id}: missing Copilot token`,\n )\n return\n }\n\n const snapshot = takeAuthSnapshot(account)\n\n if (account.modelsRefreshPromise) {\n const existingSnapshot = this.modelsRefreshSnapshotByAccount.get(account)\n if (isSameAuthSnapshot(existingSnapshot, snapshot)) {\n await account.modelsRefreshPromise\n return\n }\n }\n\n account.isRefreshingModels = true\n\n const ctx = toAccountContextFromSnapshot(\n account,\n snapshot,\n account.copilotToken,\n )\n\n const promise = (async () => {\n try {\n const models = await getModels(ctx)\n const applied = applyModelsIfCurrent(account, snapshot, models)\n if (applied) {\n account.lastModelsFetch = Date.now()\n }\n } catch (error) {\n if (error instanceof HTTPError && error.response.status === 401) {\n applyUnauthorizedIfCurrent(account, snapshot, \"Unauthorized (401)\")\n return\n }\n\n consola.error(`Failed to refresh models for ${account.id}:`, error)\n }\n })()\n\n account.modelsRefreshPromise = promise\n this.modelsRefreshSnapshotByAccount.set(account, snapshot)\n\n void promise.finally(() => {\n this.finalizeModelsRefreshPromise(account, promise)\n })\n\n await promise\n }\n\n private async refreshAllModels(): Promise<void> {\n const accounts: Array<AccountRuntime> = []\n\n if (this.temporaryAccount) {\n accounts.push(this.temporaryAccount)\n }\n\n for (const id of this.accountOrder) {\n const account = this.accounts.get(id)\n if (account) {\n accounts.push(account)\n }\n }\n\n if (accounts.length === 0) {\n return\n }\n\n await Promise.allSettled(\n accounts.map((account) => this.refreshModels(account)),\n )\n }\n\n /** Refresh quota information for an account. */\n async refreshQuota(account: AccountRuntime): Promise<void> {\n const snapshot = takeAuthSnapshot(account)\n\n if (account.quotaRefreshPromise) {\n const existingSnapshot = this.quotaRefreshSnapshotByAccount.get(account)\n if (isSameAuthSnapshot(existingSnapshot, snapshot)) {\n await account.quotaRefreshPromise\n return\n }\n }\n\n account.isRefreshingQuota = true\n\n const ctx = toAccountContextFromSnapshot(account, snapshot)\n const promise = (async () => {\n try {\n const usage = await getCopilotUsage(ctx)\n const premium = usage.quota_snapshots.premium_interactions\n applyQuotaRefreshSuccessIfCurrent(account, snapshot, premium)\n } catch (error) {\n if (error instanceof HTTPError && error.response.status === 401) {\n applyUnauthorizedIfCurrent(account, snapshot, \"Unauthorized (401)\")\n return\n }\n\n consola.error(`Failed to refresh quota for ${account.id}:`, error)\n // Don't mark as failed for non-401 quota refresh errors\n }\n })()\n\n account.quotaRefreshPromise = promise\n this.quotaRefreshSnapshotByAccount.set(account, snapshot)\n\n void promise.finally(() => {\n this.finalizeQuotaRefreshPromise(account, promise)\n })\n\n await promise\n }\n\n /** Check if quota cache is expired. */\n private isQuotaCacheExpired(account: AccountRuntime): boolean {\n if (!account.lastQuotaFetch) return true\n return Date.now() - account.lastQuotaFetch > QUOTA_CACHE_TTL\n }\n\n private isAccountFailed(account: AccountRuntime): boolean {\n return account.failed === true\n }\n\n private useOverageFallback(fallback: {\n account: AccountRuntime\n model: Model\n endpoint: string\n costUnits: number\n }): SelectAccountForRequestSuccess {\n const reservation = reservePremiumUnits(\n fallback.account,\n fallback.costUnits,\n )\n return {\n ok: true,\n account: fallback.account,\n selectedModel: fallback.model,\n endpoint: fallback.endpoint,\n costUnits: fallback.costUnits,\n reservation,\n }\n }\n\n private isModelSupportedForEndpoint(model: Model, endpoint: string): boolean {\n if (endpoint === \"/responses\") {\n return model.supported_endpoints?.includes(endpoint) ?? false\n }\n\n const supported = model.supported_endpoints\n if (!supported) {\n return true\n }\n\n return supported.includes(endpoint)\n }\n\n private pickSupportedCandidate(\n account: AccountRuntime,\n candidates: Array<AccountRequestCandidate>,\n ): { candidate: AccountRequestCandidate; model: Model } | null {\n const models = account.models?.data\n if (!models) {\n return null\n }\n\n for (const candidate of candidates) {\n const model = models.find((m) => m.id === candidate.modelId)\n if (!model) {\n continue\n }\n\n if (!this.isModelSupportedForEndpoint(model, candidate.endpoint)) {\n continue\n }\n\n return { candidate, model }\n }\n\n return null\n }\n\n private selectFreeAccountForRequest(\n orderedAccounts: Array<AccountRuntime>,\n candidates: Array<AccountRequestCandidate>,\n ): SelectAccountForRequestResult {\n const count = orderedAccounts.length\n const start = this.freeModelCursor % count\n\n let supportedCandidateFound = false\n\n for (let i = 0; i < count; i++) {\n const idx = (start + i) % count\n const account = orderedAccounts[idx]\n if (this.isAccountFailed(account)) {\n continue\n }\n\n const supported = this.pickSupportedCandidate(account, candidates)\n if (!supported) {\n continue\n }\n\n supportedCandidateFound = true\n\n const { candidate, model } = supported\n const costUnits = getCostUnits(model)\n\n // Defensive: free path should only be used for free models.\n if (costUnits > 0) {\n continue\n }\n\n this.freeModelCursor = (idx + 1) % count\n\n return {\n ok: true,\n account,\n selectedModel: model,\n endpoint: candidate.endpoint,\n costUnits,\n }\n }\n\n if (!supportedCandidateFound) {\n return { ok: false, reason: \"MODEL_NOT_SUPPORTED\" }\n }\n\n return { ok: false, reason: \"NO_QUOTA\" }\n }\n\n // eslint-disable-next-line complexity -- overage fallback adds necessary branching\n private async selectAccountForCandidates(\n orderedAccounts: Array<AccountRuntime>,\n candidates: Array<AccountRequestCandidate>,\n ): Promise<SelectAccountForRequestResult> {\n if (orderedAccounts.length === 0) {\n return { ok: false, reason: \"NO_ACCOUNTS\" }\n }\n\n let supportedCandidateFound = false\n let overageFallback:\n | {\n account: AccountRuntime\n model: Model\n endpoint: string\n costUnits: number\n }\n | undefined\n\n for (const account of orderedAccounts) {\n if (this.isAccountFailed(account)) {\n continue\n }\n\n const supported = this.pickSupportedCandidate(account, candidates)\n if (!supported) {\n continue\n }\n\n supportedCandidateFound = true\n\n const { candidate, model } = supported\n const costUnits = getCostUnits(model)\n\n if (costUnits <= 0) {\n if (this.freeModelLoadBalancingEnabled) {\n // Free model: RR load balancing across accounts (including temporaryAccount).\n return this.selectFreeAccountForRequest(orderedAccounts, candidates)\n }\n\n // Free model: sequential routing (same ordering strategy as premium models).\n return {\n ok: true,\n account,\n selectedModel: model,\n endpoint: candidate.endpoint,\n costUnits,\n }\n }\n\n if (!account.unlimited && this.isQuotaCacheExpired(account)) {\n await this.refreshQuota(account)\n }\n\n if (this.isAccountFailed(account)) {\n continue\n }\n\n if (account.unlimited) {\n return {\n ok: true,\n account,\n selectedModel: model,\n endpoint: candidate.endpoint,\n costUnits,\n }\n }\n\n // Check if account has sufficient quota.\n const effectiveRemaining = getEffectivePremiumRemaining(account)\n if (effectiveRemaining !== undefined && effectiveRemaining < costUnits) {\n // Insufficient quota - store as overage fallback if permitted, but keep\n // looking for accounts with quota to avoid unnecessary overage charges.\n if (account.overagePermitted && !overageFallback) {\n overageFallback = {\n account,\n model,\n endpoint: candidate.endpoint,\n costUnits,\n }\n }\n continue\n }\n\n const reservation = reservePremiumUnits(account, costUnits)\n\n return {\n ok: true,\n account,\n selectedModel: model,\n endpoint: candidate.endpoint,\n costUnits,\n reservation,\n }\n }\n\n if (!supportedCandidateFound) {\n return { ok: false, reason: \"MODEL_NOT_SUPPORTED\" }\n }\n\n // No account with quota found - use overage fallback if available.\n return overageFallback ?\n this.useOverageFallback(overageFallback)\n : { ok: false, reason: \"NO_QUOTA\" }\n }\n\n /**\n * Select an available account for a specific request (model + endpoint).\n * Uses reservation to avoid oversubscribing premium quota under concurrency.\n */\n async selectAccountForRequest(\n candidates: Array<AccountRequestCandidate>,\n ): Promise<SelectAccountForRequestResult> {\n if (candidates.length === 0) {\n throw new Error(\"selectAccountForRequest requires at least one candidate\")\n }\n\n const orderedAccounts = [\n ...(this.temporaryAccount ? [this.temporaryAccount] : []),\n ...this.accountOrder\n .map((id) => this.accounts.get(id))\n .filter((account): account is AccountRuntime => account !== undefined),\n ]\n const primary = await this.selectAccountForCandidates(\n orderedAccounts,\n candidates,\n )\n if (primary.ok || primary.reason !== \"MODEL_NOT_SUPPORTED\") {\n return primary\n }\n\n const aliasCandidates = candidates.map((candidate) => {\n const modelId = resolveModelAlias(candidate.modelId)\n if (modelId === candidate.modelId) return candidate\n return { ...candidate, modelId }\n })\n const aliasChanged = aliasCandidates.some(\n (candidate, index) => candidate.modelId !== candidates[index].modelId,\n )\n if (!aliasChanged) {\n return primary\n }\n\n return this.selectAccountForCandidates(orderedAccounts, aliasCandidates)\n }\n\n /**\n * Finalize quota after a request completes.\n * This releases any in-flight reservation and refreshes the actual quota from the API.\n */\n async finalizeQuota(\n account: AccountRuntime,\n reservation?: QuotaReservation,\n ): Promise<void> {\n releasePremiumReservation(account, reservation)\n\n try {\n await this.refreshQuota(account)\n } catch (error) {\n consola.debug(`Failed to finalize quota for ${account.id}:`, error)\n }\n }\n\n /**\n * Mark an account as failed.\n */\n markAccountFailed(id: string, reason: string): void {\n const account = this.accounts.get(id)\n if (account) {\n setAccountFailedState(account, reason)\n return\n }\n\n if (this.temporaryAccount && this.temporaryAccount.id === id) {\n setAccountFailedState(this.temporaryAccount, reason)\n }\n }\n\n /**\n * Get status of all accounts.\n */\n getAccountStatus(): Array<{\n id: string\n entitlement?: number\n remaining?: number\n unlimited?: boolean\n overagePermitted?: boolean\n failed?: boolean\n failureReason?: string\n }> {\n const statuses: Array<{\n id: string\n entitlement?: number\n remaining?: number\n unlimited?: boolean\n overagePermitted?: boolean\n failed?: boolean\n failureReason?: string\n }> = []\n\n if (this.temporaryAccount) {\n statuses.push({\n id: \"(temporary)\",\n entitlement: this.temporaryAccount.premiumEntitlement,\n remaining: this.temporaryAccount.premiumRemaining,\n unlimited: this.temporaryAccount.unlimited,\n overagePermitted: this.temporaryAccount.overagePermitted,\n failed: this.temporaryAccount.failed,\n failureReason: this.temporaryAccount.failureReason,\n })\n }\n\n for (const id of this.accountOrder) {\n const account = this.accounts.get(id)\n if (account) {\n statuses.push({\n id: account.id,\n entitlement: account.premiumEntitlement,\n remaining: account.premiumRemaining,\n unlimited: account.unlimited,\n overagePermitted: account.overagePermitted,\n failed: account.failed,\n failureReason: account.failureReason,\n })\n }\n }\n\n return statuses\n }\n\n /**\n * Set a temporary account from a GitHub token (--github-token).\n * This account takes priority over registered accounts.\n */\n async setTemporaryAccount(\n githubToken: string,\n accountType: AccountType,\n ): Promise<void> {\n const runtime: AccountRuntime = {\n id: \"(temporary)\",\n accountType,\n addedAt: Date.now(),\n githubToken,\n vsCodeVersion: this.vsCodeVersion,\n }\n\n try {\n await this.initializeAccount(runtime)\n this.temporaryAccount = runtime\n consola.info(\"Temporary account initialized\")\n } catch (error) {\n consola.error(\"Failed to initialize temporary account:\", error)\n throw error\n }\n }\n\n /**\n * Check if any accounts are available.\n */\n hasAccounts(): boolean {\n return this.accounts.size > 0 || this.temporaryAccount !== undefined\n }\n\n /**\n * Get the first available account's models.\n * Used for caching models in legacy compatibility mode.\n */\n getFirstAccountModels(): AccountRuntime[\"models\"] {\n if (this.temporaryAccount?.models) {\n return this.temporaryAccount.models\n }\n\n for (const id of this.accountOrder) {\n const account = this.accounts.get(id)\n if (account?.models) {\n return account.models\n }\n }\n\n return undefined\n }\n\n /**\n * Get account context by index.\n * Index 0 is the temporary account (if exists), otherwise the first registered account.\n * Returns null if index is out of bounds.\n */\n getAccountContextByIndex(index: number): AccountContext | null {\n // Build the same order as getAccountStatus()\n const allAccounts: Array<AccountRuntime> = []\n\n if (this.temporaryAccount) {\n allAccounts.push(this.temporaryAccount)\n }\n\n for (const id of this.accountOrder) {\n const account = this.accounts.get(id)\n if (account) {\n allAccounts.push(account)\n }\n }\n\n if (index < 0 || index >= allAccounts.length) {\n return null\n }\n\n return this.toAccountContext(allAccounts[index])\n }\n\n /**\n * Get the total number of accounts (including temporary).\n */\n getAccountCount(): number {\n return (this.temporaryAccount ? 1 : 0) + this.accountOrder.length\n }\n\n /**\n * Convert AccountRuntime to AccountContext for service calls.\n */\n private toAccountContext(account: AccountRuntime): AccountContext {\n return {\n githubToken: account.githubToken,\n copilotToken: account.copilotToken,\n accountType: account.accountType,\n vsCodeVersion: account.vsCodeVersion,\n }\n }\n\n /**\n * Start watching the registry file for changes.\n * Enables hot reload of accounts when the file is modified.\n */\n private startRegistryWatcher(): void {\n // Stop existing watcher if any\n this.stopRegistryWatcher()\n\n try {\n this.registryWatcher = fs.watch(\n PATHS.ACCOUNTS_REGISTRY_PATH,\n (eventType) => {\n // Only react to 'change' events (file content modified)\n if (eventType === \"change\") {\n this.scheduleReload()\n }\n },\n )\n\n // Successful start: reset restart backoff.\n this.registryWatcherRestartDelayMs = WATCHER_RESTART_INITIAL_DELAY_MS\n if (this.registryWatcherRestartTimer) {\n clearTimeout(this.registryWatcherRestartTimer)\n this.registryWatcherRestartTimer = undefined\n }\n\n // Handle watcher errors (e.g., file deleted)\n this.registryWatcher.on(\"error\", (error) => {\n consola.debug(\"Registry watcher error:\", error)\n\n const delayMs = this.registryWatcherRestartDelayMs\n this.registryWatcherRestartDelayMs = Math.min(\n this.registryWatcherRestartDelayMs * 2,\n WATCHER_RESTART_MAX_DELAY_MS,\n )\n\n // Close broken watcher to avoid repeated error events.\n this.stopRegistryWatcher()\n\n // Try to restart the watcher after a delay (with backoff)\n this.registryWatcherRestartTimer = setTimeout(() => {\n this.registryWatcherRestartTimer = undefined\n this.startRegistryWatcher()\n }, delayMs)\n\n consola.debug(`Restarting registry watcher in ${delayMs}ms`)\n })\n\n consola.debug(\"Started registry file watcher\")\n } catch (error) {\n consola.warn(\"Failed to start registry watcher:\", error)\n }\n }\n\n /**\n * Schedule a registry reload with debouncing.\n */\n private scheduleReload(): void {\n // Clear existing timer\n if (this.reloadDebounceTimer) {\n clearTimeout(this.reloadDebounceTimer)\n }\n\n // Schedule reload after debounce delay\n this.reloadDebounceTimer = setTimeout(() => {\n void this.reloadRegistry()\n }, RELOAD_DEBOUNCE_MS)\n }\n\n /**\n * Reload the registry and perform incremental updates.\n * Adds new accounts, removes deleted ones, and reinitializes existing accounts\n * when token/accountType changes.\n */\n private async reloadRegistry(): Promise<void> {\n // Prevent concurrent reloads\n if (this.isReloading) {\n return\n }\n this.isReloading = true\n\n try {\n const newMetas = await listAccountsFromRegistry()\n const newIds = new Set(newMetas.map((m) => m.id))\n const currentIds = new Set(this.accountOrder)\n\n // Track changes for logging\n const added: Array<string> = []\n const removed: Array<string> = []\n const updated: Array<string> = []\n\n this.removeDeletedAccounts(currentIds, newIds, removed)\n\n // Add new accounts (newIds - currentIds)\n for (const meta of newMetas) {\n if (!currentIds.has(meta.id)) {\n await this.addNewAccount(meta, added)\n }\n }\n\n // Update existing accounts when meta/token changed\n await this.reinitializeUpdatedAccounts(newMetas, currentIds, updated)\n\n // Update accountOrder to reflect new order\n this.accountOrder = newMetas\n .map((m) => m.id)\n .filter((id) => this.accounts.has(id))\n\n // Reset free-model RR cursor on account list/order changes.\n this.freeModelCursor = 0\n\n this.logRegistryReloadChanges(added, removed, updated)\n } catch (error) {\n consola.error(\"Failed to reload registry:\", error)\n this.shutdown()\n process.exit(1)\n } finally {\n this.isReloading = false\n }\n }\n\n private removeDeletedAccounts(\n currentIds: Set<string>,\n newIds: Set<string>,\n removed: Array<string>,\n ): void {\n for (const id of currentIds) {\n if (!newIds.has(id)) {\n const account = this.accounts.get(id)\n if (!account) {\n continue\n }\n\n this.stopTokenRefresh(account)\n this.accounts.delete(id)\n removed.push(id)\n }\n }\n }\n\n private async reinitializeUpdatedAccounts(\n newMetas: Array<{ id: string; accountType: AccountType; addedAt: number }>,\n currentIds: Set<string>,\n updated: Array<string>,\n ): Promise<void> {\n for (const meta of newMetas) {\n if (!currentIds.has(meta.id)) {\n continue\n }\n\n const account = this.accounts.get(meta.id)\n if (!account) {\n continue\n }\n\n const token = await loadAccountToken(meta.id)\n if (!token) {\n consola.warn(`No token found for account ${meta.id}, skipping update`)\n continue\n }\n\n const accountTypeChanged = account.accountType !== meta.accountType\n const tokenChanged = account.githubToken !== token\n const addedAtChanged = account.addedAt !== meta.addedAt\n\n // Keep runtime metadata in sync with the registry.\n if (accountTypeChanged) {\n account.accountType = meta.accountType\n }\n if (addedAtChanged) {\n account.addedAt = meta.addedAt\n }\n if (tokenChanged) {\n account.githubToken = token\n }\n\n if (!accountTypeChanged && !tokenChanged) {\n continue\n }\n\n try {\n await this.initializeAccount(account)\n updated.push(meta.id)\n } catch (error) {\n consola.error(\n `Failed to reinitialize account ${meta.id} after update:`,\n error,\n )\n account.failed = true\n account.failureReason = String(error)\n updated.push(`${meta.id} (failed)`)\n }\n }\n }\n\n private logRegistryReloadChanges(\n added: Array<string>,\n removed: Array<string>,\n updated: Array<string>,\n ): void {\n if (added.length === 0 && removed.length === 0 && updated.length === 0) {\n return\n }\n\n const changes: Array<string> = []\n if (added.length > 0) {\n changes.push(`added: ${added.join(\", \")}`)\n }\n if (removed.length > 0) {\n changes.push(`removed: ${removed.join(\", \")}`)\n }\n if (updated.length > 0) {\n changes.push(`updated: ${updated.join(\", \")}`)\n }\n\n consola.info(\n `Registry reloaded (${changes.join(\"; \")}). Total: ${this.accounts.size} account(s)`,\n )\n }\n\n /**\n * Helper to add a new account during reload.\n */\n private async addNewAccount(\n meta: { id: string; accountType: AccountType; addedAt: number },\n added: Array<string>,\n ): Promise<void> {\n const token = await loadAccountToken(meta.id)\n if (!token) {\n consola.warn(`No token found for new account ${meta.id}, skipping`)\n return\n }\n\n const runtime: AccountRuntime = {\n ...meta,\n githubToken: token,\n vsCodeVersion: this.vsCodeVersion,\n }\n\n try {\n await this.initializeAccount(runtime)\n this.accounts.set(meta.id, runtime)\n added.push(meta.id)\n } catch (error) {\n consola.error(`Failed to initialize new account ${meta.id}:`, error)\n runtime.failed = true\n runtime.failureReason = String(error)\n this.accounts.set(meta.id, runtime)\n added.push(`${meta.id} (failed)`)\n }\n }\n\n /**\n * Stop the registry file watcher.\n */\n private stopRegistryWatcher(): void {\n if (this.reloadDebounceTimer) {\n clearTimeout(this.reloadDebounceTimer)\n this.reloadDebounceTimer = undefined\n }\n if (this.registryWatcherRestartTimer) {\n clearTimeout(this.registryWatcherRestartTimer)\n this.registryWatcherRestartTimer = undefined\n }\n if (this.registryWatcher) {\n this.registryWatcher.close()\n this.registryWatcher = undefined\n }\n }\n\n /**\n * Shutdown the manager and clean up resources.\n */\n shutdown(): void {\n this.stopRegistryWatcher()\n this.stopAllTokenRefresh()\n this.stopModelsRefresh()\n this.accounts.clear()\n this.accountOrder = []\n this.temporaryAccount = undefined\n }\n}\n\n/** Singleton instance of AccountsManager */\nexport const accountsManager = new AccountsManager()\n"],"mappings":";;;;;;;;AAqCA,MAAa,0BAA0B;AAsBvC,MAAM,wBAAwB;;;;;;AAO9B,MAAM,uBAAuB;;;;;;;;;;;;;;;;;;;AAoB7B,MAAMA,gBAA2B;CAC/B,MAAM,EACJ,SAAS,EAAE,EACZ;CACD,WAAW,EAAE;CACb,cAAc;EACZ,cAAc;EACd,iBAAiB;EACjB,gBAAgB;EAChB,WAAW;EACZ;CACD,YAAY;CACZ,wBAAwB;CACxB,qCAAqC,EAAE;CACvC,uBAAuB;EACrB,cAAc;EACd,iBAAiB;EACjB,gBAAgB;EAChB,WAAW;EACZ;CACD,mCAAmC;CACnC,uBAAuB;CACvB,sBAAsB;CACtB,iCAAiC;CACjC,2BAA2B;CAC3B,gBAAgB;CACjB;AAED,IAAIC,eAAiC;AAErC,SAAS,cAAc,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