@gnahz77/opencode-copilot-multi-auth 0.1.4 → 0.1.5

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # opencode-copilot-multi-auth
2
2
 
3
+ [中文文档](README.zh.md)
4
+
3
5
  Package on npm: https://www.npmjs.com/package/@gnahz77/opencode-copilot-multi-auth
4
6
 
5
7
  This fork replaces the older GitHub Copilot chat-auth flow with the newer Copilot CLI-style OAuth flow and makes `opencode` use the live Copilot model metadata for your account.
@@ -12,7 +14,7 @@ Add the plugin to your `opencode` config:
12
14
  {
13
15
  "$schema": "https://opencode.ai/config.json",
14
16
  "plugin": [
15
- "@gnahz77/opencode-copilot-multi-auth@0.1.4"
17
+ "@gnahz77/opencode-copilot-multi-auth@0.1.5"
16
18
  ]
17
19
  }
18
20
  ```
@@ -25,7 +27,7 @@ If you also want the TUI command support provided by this package, add the same
25
27
  {
26
28
  "$schema": "https://opencode.ai/tui.json",
27
29
  "plugin": [
28
- "@gnahz77/opencode-copilot-multi-auth@0.1.4"
30
+ "@gnahz77/opencode-copilot-multi-auth@0.1.5"
29
31
  ]
30
32
  }
31
33
  ```
package/README.zh.md ADDED
@@ -0,0 +1,218 @@
1
+ # opencode-copilot-multi-auth
2
+
3
+ [English](README.md)
4
+
5
+ npm 上的包主页: https://www.npmjs.com/package/@gnahz77/opencode-copilot-multi-auth
6
+
7
+ 这个分支(fork)将旧的 GitHub Copilot chat-auth 流程替换为更新的 Copilot CLI 风格的 OAuth 流程,并使 `opencode` 使用您的账户实时获取的 Copilot 模型元数据。
8
+
9
+ ## 如何使用
10
+
11
+ 将该插件添加到您的 `opencode` 配置中:
12
+
13
+ ```json
14
+ {
15
+ "$schema": "https://opencode.ai/config.json",
16
+ "plugin": [
17
+ "@gnahz77/opencode-copilot-multi-auth@0.1.5"
18
+ ]
19
+ }
20
+ ```
21
+
22
+ 然后启动 `opencode` 并登录到 `github-copilot` 供应商。该插件处理类似 Copilot CLI 风格的设备授权流程,并在之后重用存储的 GitHub OAuth 令牌。
23
+
24
+ 如果您还希望使用该包提供的 TUI 命令支持,请将相同的插件添加到您的 `tui.json` 或 `tui.jsonc` 中:
25
+
26
+ ```json
27
+ {
28
+ "$schema": "https://opencode.ai/tui.json",
29
+ "plugin": [
30
+ "@gnahz77/opencode-copilot-multi-auth@0.1.5"
31
+ ]
32
+ }
33
+ ```
34
+
35
+ 重新启动 OpenCode TUI 后,您可以打开命令面板并运行 `copilot-usage`,或直接输入 `/copilot-usage`。
36
+ 插件会打开一个弹窗,显示从拆分的本地存储中加载的所有账户:
37
+
38
+ - OAuth/账户数据:`~/.local/share/opencode/copilot-auth.json`
39
+ - 单账户路由策略:`~/.config/opencode/copilot-auth.json`
40
+
41
+ 对于发布前的本地开发,您可以直接加载该文件:
42
+
43
+ ```json
44
+ {
45
+ "$schema": "https://opencode.ai/config.json",
46
+ "plugin": [
47
+ "file:///absolute/path/to/dist/index.js"
48
+ ]
49
+ }
50
+ ```
51
+
52
+ 重要提示:如果文件路径包含 `opencode-copilot-auth`,当前的 `opencode` 构建可能会因为硬编码的插件名称过滤器而跳过加载它。请使用不包含该子字符串的路径。
53
+
54
+ 如果您在开发过程中在本地加载插件并希望使用 `copilot-usage`,请同时将构建的 TUI 入口添加到您的 TUI 配置中:
55
+
56
+ ```json
57
+ {
58
+ "$schema": "https://opencode.ai/tui.json",
59
+ "plugin": [
60
+ "file:///absolute/path/to/dist/tui.js"
61
+ ]
62
+ }
63
+ ```
64
+
65
+ ## `copilot-usage` 命令
66
+
67
+ 此包现在包含一个名为 `copilot-usage` 的 TUI 命令。
68
+
69
+ - 命令面板入口:`copilot-usage`
70
+ - Slash 命令:`/copilot-usage`
71
+
72
+ 它的功能:
73
+
74
+ - 从合并后的本地池中读取每个 Copilot 账户,池子组装自:
75
+ - `~/.local/share/opencode/copilot-auth.json` (OAuth/账户数据)
76
+ - `~/.config/opencode/copilot-auth.json` (路由策略)
77
+ - 使用该账户的 OAuth 令牌,为每个账户调用 GitHub Copilot 的配额端点 `/copilot_internal/user`
78
+ - 打开一个弹窗界面,为每个账户显示:
79
+ - 账户名称
80
+ - 已使用 / 总额度条
81
+ - 使用百分比
82
+
83
+ 注意事项:
84
+
85
+ - 该命令是此包 TUI 目标的一部分,所以只配置 `opencode.json` 是不够的;`tui.json` 必须也加载此插件。
86
+ - 被禁用的账户仍然会显示在弹窗中,并会被标记为已禁用。
87
+ - 如果某个账户加载使用数据失败,弹窗仍会显示其他账户,并在行内包含该账户的错误消息。
88
+
89
+ ## 这个分支有什么改变
90
+
91
+ - 授权流程:使用了类似 Copilot CLI 风格的 OAuth 客户端流程,并直接保留了 GitHub OAuth 令牌。
92
+ - 授权配额:获取 `/copilot_internal/user`,并使用授权提供的 Copilot API 基础 URL。
93
+ - 令牌交换:不再调用 `/copilot_internal/v2/token`。
94
+ - 请求配置:使用较新的 `copilot-developer-cli` 请求头,替代了旧版的聊天配置。
95
+ - 模型元数据:通过插件的 `provider.models` 钩子获取实时的 Copilot `/models` 响应,以便最终的 `opencode` 模型列表来源于 Copilot API 所支持的实时配额。
96
+
97
+ ## 上下文窗口与模型限制
98
+
99
+ 与上游的主要实际区别在于,此分支从 Copilot 动态应用实时的按模型限制,而不是仅依赖静态元数据。
100
+
101
+ 这意味着 `opencode` 可以看到 Copilot 宣告的以下各项的值:
102
+
103
+ - `limit.context`
104
+ - `limit.input`
105
+ - `limit.output`
106
+
107
+ 截至 2026 年 3 月 10 日,此分支使用的实时 GitHub Copilot `/models` 响应暴露了 Copilot CLI 的模型配置。下表比较了实时 Copilot CLI 的上下文窗口与 [`models.dev`](https://models.dev) 上的静态 `github-copilot` 目录。
108
+
109
+ | 模型 | 此分支 (CLI 上下文) | `models.dev` 上下文 | 差异 |
110
+ | ------------------- | ----------------------: | -------------------: | ---------: |
111
+ | `claude-opus-4.6` | 200,000 | 128,000 | +72,000 |
112
+ | `claude-sonnet-4.6` | 200,000 | 128,000 | +72,000 |
113
+ | `claude-haiku-4.5` | 144,000 | 128,000 | +16,000 |
114
+
115
+ 实际收获是,该分支比静态的 `models.dev` 值提供了更大的实时 Claude 上下文窗口。
116
+
117
+ 此分支观察到的例子:
118
+
119
+ - `claude-sonnet-4.6`
120
+ - context window (上下文窗口): `200000`
121
+ - prompt/input limit (提示/输入限制): `168000`
122
+ - output limit (输出限制): `32000`
123
+ - `claude-opus-4.6`
124
+ - context window (上下文窗口): `200000`
125
+ - prompt/input limit (提示/输入限制): `168000`
126
+ - output limit (输出限制): `64000`
127
+ - `claude-haiku-4.5`
128
+ - context window (上下文窗口): `144000`
129
+ - prompt/input limit (提示/输入限制): `128000`
130
+ - output limit (输出限制): `32000`
131
+
132
+ 如果不打这些补丁,`opencode` 可能会因为其使用的初始静态模型目录而显示过期或偏小的限制。
133
+
134
+ ## Claude 思考预算 (thinking budget) 行为
135
+
136
+ 此分支还改变了 Copilot Claude 请求的行为:
137
+
138
+ - 当选择 `thinking` 变体时,它发送 `thinking_budget: 16000`
139
+ - 当未选择变体时,它完全省略 `thinking_budget`
140
+
141
+ 这不同于上游的 `opencode`,上游目前为内置的 `thinking` 变体发送 `thinking_budget: 4000`。
142
+
143
+ 该插件故意不尝试修改 `opencode` 的核心 UI。所以可见的 Claude 变体列表依然由 `opencode` 本身控制;该分支改变的是请求行为,而不是内置变体选择器的标签。
144
+
145
+ ## 发布 (Publishing)
146
+
147
+ ```zsh
148
+ ./script/publish.ts
149
+ ```
150
+
151
+ ## 多账户支持
152
+
153
+ 此分支在分离的本地文件中保存 Copilot 的账户状态:
154
+
155
+ - OAuth 必需的账户数据:`~/.local/share/opencode/copilot-auth.json`
156
+ - 单账户路由策略:`~/.config/opencode/copilot-auth.json`
157
+
158
+ 在启动时,该插件会自动将旧版的单文件存储迁移到这种双文件布局中。
159
+
160
+ 每次成功的 OAuth 登录都会自动更新该池:用一个具有特定作用域部署的新账户登录会追加一条新记录,而在同一部署上用相同的 GitHub 身份再次登录会更新现有记录,而不是创建重复项。
161
+
162
+ 在运行时,插件将这两个文件合并为路由所用的同一内存中 `version: 2` 池结构 (`accounts`)。
163
+
164
+ | 字段 | 含义 |
165
+ | --- | --- |
166
+ | `id` | 与账户记录一起存储的稳定的人类友好标识符。 |
167
+ | `name` | 账户的显示名称。 |
168
+ | `enabled` | 该账户是否能参与自动路由。被禁用的账户依然会保存,但在挑选赢家时会被忽略。 |
169
+ | `priority` | 当多个已启用的账户都可以提供同一个原始模型 ID 的服务时,较低的值优先(即数字越小优先级越高)。 |
170
+ | `allowlist` | 此账户获准提供服务的原始模型 ID 或 `*` 通配符模式。如果不为空,则账户只能提供与这些条目之一匹配的模型。 |
171
+ | `blocklist` | 此账户绝对不能提供服务的原始模型 ID 或 `*` 通配符模式。如果 `allowlist` 和 `blocklist` 均不为空,插件会首先检查 `allowlist`,然后应用 `blocklist`。 |
172
+
173
+ 自动路由是基于 `opencode` 已经使用的原始 Copilot 模型 ID 来工作的。插件根据 `enabled` 过滤合格的账户,然后首先检查 `allowlist`(当不为空时,模型必须匹配其精确条目或通配符模式之一),然后应用 `blocklist`,最后通过最低的 `priority` 挑选出唯一的一个获胜账户(带有一个稳定的基于 key 的平局决胜机制)。模型 ID 本身没有被重写,因此账户标识不会出现在模型 ID 中。
174
+
175
+ 通配符匹配区分大小写,且目前支持用 `*` 表示模式中任意位置的零个或多个字符。例如,`claude-*` 可以匹配 `claude-sonnet-4.6` 和 `claude-opus-4.6`。
176
+
177
+ 示例授权文件 (`~/.local/share/opencode/copilot-auth.json`):
178
+
179
+ ```json
180
+ {
181
+ "version": 2,
182
+ "accounts": [
183
+ {
184
+ "key": "github.com:12345678",
185
+ "deployment": "github.com",
186
+ "domain": "github.com",
187
+ "baseUrl": "https://api.githubcopilot.com",
188
+ "identity": {
189
+ "login": "octocat",
190
+ "userId": 12345678
191
+ },
192
+ "auth": {
193
+ "type": "oauth",
194
+ "refresh": "<oauth token>"
195
+ },
196
+ "createdAt": "2026-04-15T00:00:00.000Z",
197
+ "updatedAt": "2026-04-15T00:00:00.000Z"
198
+ }
199
+ ]
200
+ }
201
+ ```
202
+
203
+ 示例策略文件 (`~/.config/opencode/copilot-auth.json`):
204
+
205
+ ```json
206
+ {
207
+ "version": 2,
208
+ "accounts": [
209
+ {
210
+ "key": "github.com:12345678",
211
+ "enabled": true,
212
+ "priority": 100,
213
+ "allowlist": ["claude-*"],
214
+ "blocklist": []
215
+ }
216
+ ]
217
+ }
218
+ ```
package/dist/auth.d.ts CHANGED
@@ -18,3 +18,4 @@ export declare function buildHeaders(init: RequestInit | undefined, copilotToken
18
18
  export declare function resolveSelectedPoolAccount(pool: AccountPool, accountKey?: string, requestedRawModelId?: string): PoolAccount | null;
19
19
  export declare function refreshSelectedAccountBaseURL(accountKey: string): Promise<PoolAccount>;
20
20
  export declare function fetchWithSelectedAccount(input: RequestInfo | URL, init: RequestInit | undefined, selectedAccount: PoolAccount): Promise<Response>;
21
+ export declare function fetchWithAccountFallback(input: RequestInfo | URL, init: RequestInit | undefined, candidates: PoolAccount[]): Promise<Response>;
package/dist/auth.js CHANGED
@@ -237,3 +237,19 @@ export async function fetchWithSelectedAccount(input, init, selectedAccount) {
237
237
  }
238
238
  return retryResponse;
239
239
  }
240
+ export async function fetchWithAccountFallback(input, init, candidates) {
241
+ if (candidates.length === 0) {
242
+ throw new Error("[opencode-copilot-cli-auth] No eligible accounts available for this model; re-login required");
243
+ }
244
+ let lastError;
245
+ for (const account of candidates) {
246
+ try {
247
+ return await fetchWithSelectedAccount(input, init, account);
248
+ }
249
+ catch (error) {
250
+ lastError = error;
251
+ console.warn(`[opencode-copilot-cli-auth] Account ${account.key ?? "unknown"} failed, trying next:`, error instanceof Error ? error.message : String(error));
252
+ }
253
+ }
254
+ throw lastError ?? new Error("[opencode-copilot-cli-auth] All candidate accounts exhausted; re-login required");
255
+ }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Plugin } from "@opencode-ai/plugin";
2
2
  import { lookupGitHubIdentity } from "./auth.js";
3
- import { deriveAccountKey, getPolicyPath, getPoolPath, migrateLegacyPoolStorageIfNeeded, readPool, resolveWinnerAccount, upsertAccount, writePool } from "./pool.js";
3
+ import { deriveAccountKey, getPolicyPath, getPoolPath, migrateLegacyPoolStorageIfNeeded, readPool, resolveCandidateAccounts, resolveWinnerAccount, upsertAccount, writePool } from "./pool.js";
4
4
  import { injectRoutingHeaders, stripRoutingHeaders } from "./routing.js";
5
- export { getPoolPath, getPolicyPath, readPool, writePool, migrateLegacyPoolStorageIfNeeded, deriveAccountKey, lookupGitHubIdentity, upsertAccount, resolveWinnerAccount, };
5
+ export { getPoolPath, getPolicyPath, readPool, writePool, migrateLegacyPoolStorageIfNeeded, deriveAccountKey, lookupGitHubIdentity, upsertAccount, resolveCandidateAccounts, resolveWinnerAccount, };
6
6
  export { injectRoutingHeaders, stripRoutingHeaders };
7
7
  export declare const CopilotAuthPlugin: Plugin;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import { CLIENT_ID, OAUTH_POLLING_SAFETY_MARGIN_MS, OAUTH_SCOPES, ROUTING_ACCOUNT_KEY_HEADER, ROUTING_SOURCE_HEADER, } from "./constants.js";
2
- import { buildHeaders, fetchEntitlement, fetchWithSelectedAccount, getBaseURL, getCopilotToken, getUrls, lookupGitHubIdentity, resolveSelectedPoolAccount, } from "./auth.js";
2
+ import { buildHeaders, fetchEntitlement, fetchWithAccountFallback, fetchWithSelectedAccount, getBaseURL, getCopilotToken, getUrls, lookupGitHubIdentity, resolveSelectedPoolAccount, } from "./auth.js";
3
3
  import { buildPoolBackedModels, normalizeExistingModels, resolveProviderModels } from "./models.js";
4
- import { deriveAccountKey, getPolicyPath, getPoolPath, migrateLegacyPoolStorageIfNeeded, readPool, resolveWinnerAccount, upsertAccount, writePool, } from "./pool.js";
4
+ import { deriveAccountKey, getPolicyPath, getPoolPath, migrateLegacyPoolStorageIfNeeded, readPool, resolveCandidateAccounts, resolveWinnerAccount, upsertAccount, writePool, } from "./pool.js";
5
5
  import { injectRoutingHeaders, stripRoutingHeaders } from "./routing.js";
6
6
  import { applyBaseURLToRequestInput, getHeader, getConversationMetadata, getRequestedRawModelId, normalizeDomain, resolveClaudeThinkingBudget, } from "./utils.js";
7
- export { getPoolPath, getPolicyPath, readPool, writePool, migrateLegacyPoolStorageIfNeeded, deriveAccountKey, lookupGitHubIdentity, upsertAccount, resolveWinnerAccount, };
7
+ export { getPoolPath, getPolicyPath, readPool, writePool, migrateLegacyPoolStorageIfNeeded, deriveAccountKey, lookupGitHubIdentity, upsertAccount, resolveCandidateAccounts, resolveWinnerAccount, };
8
8
  export { injectRoutingHeaders, stripRoutingHeaders };
9
9
  export const CopilotAuthPlugin = async (input) => {
10
10
  try {
@@ -65,6 +65,15 @@ export const CopilotAuthPlugin = async (input) => {
65
65
  const accountKey = getHeader(init?.headers, ROUTING_ACCOUNT_KEY_HEADER);
66
66
  const requestedRawModelId = getRequestedRawModelId(init);
67
67
  if (pool.accounts.length > 0) {
68
+ if (requestedRawModelId) {
69
+ const candidates = resolveCandidateAccounts(requestedRawModelId, pool);
70
+ const oauthCandidates = candidates.filter((account) => account.auth?.type === "oauth"
71
+ && typeof account.auth.refresh === "string"
72
+ && account.auth.refresh.trim());
73
+ if (oauthCandidates.length > 0) {
74
+ return fetchWithAccountFallback(inputRequest, init, oauthCandidates);
75
+ }
76
+ }
68
77
  const selectedAccount = resolveSelectedPoolAccount(pool, accountKey, requestedRawModelId);
69
78
  if (selectedAccount?.auth?.type === "oauth") {
70
79
  return fetchWithSelectedAccount(inputRequest, init, selectedAccount);
package/dist/pool.d.ts CHANGED
@@ -8,4 +8,5 @@ export declare function writePoolAuthData(pool: AccountPool): void;
8
8
  export declare function migrateLegacyPoolStorageIfNeeded(): void;
9
9
  export declare function deriveAccountKey(deployment: string, userId: number): string;
10
10
  export declare function upsertAccount(pool: AccountPool, accountData: UpsertAccountData): AccountPool;
11
+ export declare function resolveCandidateAccounts(rawModelId: string, pool: AccountPool): PoolAccount[];
11
12
  export declare function resolveWinnerAccount(rawModelId: string, pool: AccountPool): PoolAccount;
package/dist/pool.js CHANGED
@@ -434,7 +434,7 @@ export function upsertAccount(pool, accountData) {
434
434
  accounts: nextAccounts,
435
435
  };
436
436
  }
437
- export function resolveWinnerAccount(rawModelId, pool) {
437
+ export function resolveCandidateAccounts(rawModelId, pool) {
438
438
  const canAccountServeModel = (account) => {
439
439
  const allowlist = normalizeList(account?.allowlist);
440
440
  if (allowlist.length > 0 && !matchesAnyModelIdPattern(allowlist, rawModelId)) {
@@ -443,7 +443,7 @@ export function resolveWinnerAccount(rawModelId, pool) {
443
443
  const blocklist = normalizeList(account?.blocklist);
444
444
  return !matchesAnyModelIdPattern(blocklist, rawModelId);
445
445
  };
446
- const candidates = (Array.isArray(pool?.accounts) ? pool.accounts : [])
446
+ return (Array.isArray(pool?.accounts) ? pool.accounts : [])
447
447
  .filter((account) => account?.enabled !== false)
448
448
  .filter(canAccountServeModel)
449
449
  .sort((left, right) => {
@@ -453,5 +453,7 @@ export function resolveWinnerAccount(rawModelId, pool) {
453
453
  }
454
454
  return String(left?.key ?? "").localeCompare(String(right?.key ?? ""));
455
455
  });
456
- return candidates[0] ?? null;
456
+ }
457
+ export function resolveWinnerAccount(rawModelId, pool) {
458
+ return resolveCandidateAccounts(rawModelId, pool)[0] ?? null;
457
459
  }
package/dist/tui.js CHANGED
@@ -22,7 +22,7 @@ async function showCopilotUsageDialog(api) {
22
22
  }
23
23
  }
24
24
  const tui = async (api) => {
25
- api.command.register(() => [
25
+ api.command?.register(() => [
26
26
  {
27
27
  title: "Copilot Usage",
28
28
  value: "copilot-usage",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gnahz77/opencode-copilot-multi-auth",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",