Skip to content

Leslie-ller/opencode-copilot-account-switcher

 
 

Repository files navigation

OpenCode GitHub Copilot Account Switcher

npm version npm downloads License: MPL-2.0

中文 | English


中文

OpenCode 中管理并切换多个 GitHub Copilot 账号。本插件基于官方 github-copilot provider,补充账号管理、配额查看和几项 Copilot 工作流增强能力,无需修改模型配置

默认能力与开关:

  • Guided Loop Safety — 默认开启;帮助一次 premium request 更容易连续工作更久,并减少真正需要你输入前的汇报打断
  • Copilot Network Retry — 默认关闭;用于处理可重试的网络或证书类失败
  • Synthetic Agent Initiator — 默认关闭;实验性开关,会偏离 upstream 当前稳定行为,发送或覆盖 x-initiator=agent,不保证平台一定不计费,且存在滥用判定与意外计费风险
  • Copilot Status Slash Command — 默认开启;实验性 /copilot-status workaround,只把 TUI 作为主支持面,Web/App 不承诺一致体验
  • Copilot Inject Slash Command — 默认开启;/copilot-inject 为无参数强干预开关,触发后会在下一次非 question 工具输出中注入 marker,强制模型立即走 question
  • Wait Tool — 默认开启;提供 wait 工具(最短 30 秒)用于后台等待并返回 started/waited/now

功能一览

  • 多账号管理 — 添加多个 Copilot 账号,随时切换
  • 配额查询 — 查看每个账号的剩余额度
  • 导入认证 — 可从 OpenCode 认证存储导入
  • Guided Loop Safety — 默认开启;仅对 Copilot 生效的双通道交互约束(question 负责强交互、notify 负责纯进度),推动非阻塞工作持续执行并减少无谓打断
  • Copilot Network Retry — 默认关闭;把可重试的 Copilot 网络或 TLS 失败归一化成 OpenCode 原生重试链路可识别的形态
  • Synthetic Agent Initiator — 默认关闭;实验性开关,会偏离 upstream 稳定行为,发送或覆盖 x-initiator=agent,并伴随计费/滥用风险
  • /copilot-status — 默认开启;实验性 slash command,会先弹出“正在拉取”toast,再弹出 quota 结果或错误 toast
  • /copilot-inject — 默认开启;无参数实验性强干预开关,下一次非 question 工具输出会自动附加 marker,要求模型立刻调用 question
  • wait 工具 — 默认开启;最短等待 30 秒,返回开始时间、等待秒数和当前时间
  • 无需模型配置 — 使用官方 provider,无需改模型

安装

面向人类用户

选项 A:让 LLM 帮你安装

把下面这段话丢给任意 LLM(OpenCode / Claude Code / Cursor 等):

请按以下说明安装 opencode-copilot-account-switcher 插件:https://raw.githubusercontent.com/jiwangyihao/opencode-copilot-account-switcher/master/README.md

选项 B:手动安装

  1. 在配置文件中添加插件 ~/.config/opencode/opencode.json

    {
      "$schema": "https://opencode.ai/config.json",
      "plugin": ["opencode-copilot-account-switcher"]
    }
  2. 重启 OpenCode(会自动从 npm 下载插件)。

  3. 登录 GitHub Copilot

    opencode auth login --provider github-copilot
面向 LLM 智能体

步骤指引

  1. 打开 OpenCode 配置文件 ~/.config/opencode/opencode.json

    说明:该路径在所有平台通用;Windows 上 ~ 会解析为用户目录(例如 C:\Users\YourName)。

  2. plugin 数组中添加插件:

    {
      "$schema": "https://opencode.ai/config.json",
      "plugin": ["opencode-copilot-account-switcher"]
    }
  3. 重启 OpenCode,使其自动拉取 npm 包。

验证

opencode auth login --provider github-copilot

使用方式

在 Copilot 认证流程中运行:

opencode auth login --provider github-copilot

会出现交互式菜单(方向键 + 回车):

  • 添加账号
  • 从 auth.json 导入
  • 检查配额
  • Guided Loop Safety 开关 — 通过提示词约束双通道交互:强交互内容必须走 question,纯进度优先走 notifynotify 不可用时纯进度静默继续
  • Copilot Network Retry 开关 — 默认关闭;仅影响 Copilot 请求的 fetch 路径,只处理可重试的网络/证书类失败
  • Synthetic Agent Initiator 开关 — 默认关闭;实验性开关,发送或覆盖 x-initiator=agent,会偏离 upstream 稳定行为,且不保证平台一定不计费
  • /copilot-status — 默认开启;实验性 slash command,会先弹出“正在拉取”toast,再弹出 quota 结果或错误 toast
  • /copilot-inject — 默认开启;无参数实验性强干预开关,会在下一次非 question 工具输出注入 marker,要求模型立刻调用 question
  • 切换账号
  • 删除账号
  • 全部删除

Guided Loop Safety 现在默认开启。实际使用中,它可以让一次 request 更容易连续工作好几个小时:当 question 工具在当前会话中可用且被允许时,所有需要你介入的强交互内容(决策、缺失输入、等待态、最终交接、无安全工作可继续)必须通过它完成;纯进度、阶段切换和“仍在工作中”状态优先通过 notify 发送,若 notify 不可用则静默继续,避免把纯进度错误升级成打断式提问;若路由不确定则默认使用 question。另外,策略层要求用户可见交互仅走 question/notify,避免普通 assistant 直出文本打断流程。

实验性 /copilot-inject

  • 默认:开启
  • 触发方式:在正常对话输入 /copilot-inject无参数
  • 行为:命令触发后会提示“将在模型下次调用工具的时候要求模型立刻调用提问工具”;下一次任意非 question 工具真实输出会被附加如下 marker,并再次 toast 提示“已要求模型立刻调用提问工具”
[COPILOT_INJECT_V1_BEGIN]
立即调用 question 工具并等待用户指示;在收到用户新指示前,不要继续执行后续任务。
[COPILOT_INJECT_V1_END]
  • 清除时机:模型实际调用 question 后,inject armed 状态自动清除
  • 作用范围:仅当前插件实例内存,不写入存储文件,不跨实例共享

wait 工具

  • 默认:开启
  • 用法:wait({ seconds })seconds 可省略,最小值固定为 30 秒
  • 返回格式:started: <ISO>; waited: <N>s; now: <ISO>

如果你在切换 Copilot 账号后遇到瞬时 TLS/网络失败,或者遇到由旧 session item ID 残留引起的 input[*].id too long 错误,也可以在同一菜单中开启 Copilot Network Retry。它默认关闭。开启后,插件会先保留 upstream 官方 loader 生成的 baseURL、认证头和 fetch 行为,只在最后一跳 Copilot fetch 路径上做最小包装,把可重试的网络类失败归一化成 OpenCode 已有重试链路能识别的形态;对于明确命中的 input[*].id too long 400,还会回写命中的 session part,避免旧 item ID 持续污染后续重试。

实验性 /copilot-status

  • 默认:开启
  • 性质:实验特性 / workaround,不是稳定公开 API
  • 主支持面:TUI-first;Web/App 只保留风险说明,不承诺一致体验
  • 触发方式:在正常对话里输入 /copilot-status
  • 预期反馈:先显示“正在拉取 Copilot quota...” toast,再显示成功或失败结果 toast

如果你想关闭这个实验特性,可编辑账号存储文件 ~/.config/opencode/copilot-accounts.json,加入或修改:

{
  "experimentalStatusSlashCommandEnabled": false
}

关闭后:

  • OpenCode 配置里不再注入 /copilot-status
  • 即使手动输入 /copilot-status,插件也不会再进入该 workaround 执行链

Copilot Network Retry

  • 默认:关闭
  • 作用范围:仅影响 auth.loader 返回的官方 Copilot 请求 fetch 路径
  • 用途:有限处理 failed to fetchECONNRESETunknown certificateself signed certificate 等可重试网络/证书类失败
  • 实现策略:尽量保留官方 loader 行为,再把可重试失败归一化给 OpenCode 原生重试链路判断是否重试
  • 风险提示:因为插件仍然包裹了官方 fetch 路径,若 upstream 后续内部实现变化,仍可能产生行为漂移

Synthetic Agent Initiator

  • 默认:关闭
  • 作用:发送或覆盖 x-initiator=agent,用于提前启用 upstream 开发中的 synthetic initiator 语义
  • 与 upstream 当前稳定代码的关系:开启后,请求行为会与 upstream 当前稳定代码不一致;这是基于上游开发中语义的提前启用方案,不是 upstream 稳定默认行为
  • 计费相关说明:平台可能更倾向于把压缩后继续工作、以及其他自动生成的 synthetic 提示消息排除在 premium request 计费范围之外,但这不是保证;实际是否计费、如何计费,始终由平台决定,请不要把它理解为“必然不计费”
  • 风险提示:该行为可能更容易被平台判定为滥用;也可能因为上游实现、平台策略或服务端校验变化而失效,甚至产生意外计费
  • 上游参考:

Upstream 同步机制

仓库中提交了一份 upstream 快照 src/upstream/copilot-plugin.snapshot.ts,并提供同步/校验脚本 scripts/sync-copilot-upstream.mjs

常用命令:

npm run sync:copilot-snapshot -- --source <file-or-url> --upstream-commit <sha> --sync-date <YYYY-MM-DD>
npm run check:copilot-sync -- --source <file-or-url> --upstream-commit <sha> --sync-date <YYYY-MM-DD>

该脚本会生成或校验仓库中提交的 snapshot,并要求在更新正式 snapshot 时显式提供 upstream commit 与同步日期,用来尽早发现与官方 opencode copilot.ts 的行为漂移。


存储位置

账号信息保存于:

~/.config/opencode/copilot-accounts.json

常见问题

需要改模型配置吗? 不需要。本插件只做账号管理,继续使用官方 github-copilot provider。

会替换官方 provider 吗? 不会。它只是在官方 provider 基础上增加账号切换和配额查询。

Copilot Network Retry 会替代 OpenCode 自己的重试逻辑吗? 不会。插件的目标是把可重试的 Copilot 网络/TLS 失败归一化成 OpenCode 已识别的可重试错误形态,真正的是否重试与如何退避仍由 OpenCode 原生链路决定。


English

Manage and switch between multiple GitHub Copilot accounts in OpenCode. The plugin builds on the official github-copilot provider to add account management, quota visibility, and a few Copilot workflow enhancements, with no model reconfiguration required.

Default behavior and optional switches:

  • Guided Loop Safety — enabled by default; helps a single premium request stay productive longer with fewer report interruptions before it truly needs user input
  • Copilot Network Retry — optional and off by default; handles retryable network or certificate-style failures
  • Synthetic Agent Initiator — optional and off by default; experimental switch that diverges from stable upstream behavior, sends or overrides x-initiator=agent, does not guarantee non-billable treatment, and carries abuse or unexpected-billing risk
  • Copilot Status Slash Command — enabled by default; experimental /copilot-status workaround with TUI-first support and no cross-client UX guarantee
  • Copilot Inject Slash Command — enabled by default; no-arg /copilot-inject force-intervention switch that injects a marker into the next non-question tool output and drives immediate question
  • Wait Tool — enabled by default; provides wait (minimum 30s) and returns started/waited/now

What You Get

  • Multi-account support — add multiple Copilot accounts and switch anytime
  • Quota check — view remaining quota per account
  • Auth import — import Copilot tokens from OpenCode auth storage
  • Guided Loop Safety — enabled by default; a Copilot-only dual-channel policy (question for strong interaction, notify for pure progress) that keeps non-blocked work moving and reduces avoidable interruptions
  • Copilot Network Retry — optional and off by default; normalizes retryable Copilot network or TLS failures so OpenCode's native retry path can handle them
  • Synthetic Agent Initiator — optional and off by default; experimental switch that diverges from stable upstream behavior, sends or overrides x-initiator=agent, and carries billing/abuse risk
  • /copilot-status — enabled by default; experimental slash command that shows a loading toast first and then a quota result or error toast
  • /copilot-inject — enabled by default; no-arg force-intervention command that appends a marker to the next non-question tool output to force immediate question
  • wait tool — enabled by default; minimum wait is 30 seconds and response contains start/wait/current timestamps
  • Zero model config — no model changes required (official provider only)

Installation

For Humans

Option A: Let an LLM do it

Paste this into any LLM agent (OpenCode, Claude Code, Cursor, etc.):

Install the opencode-copilot-account-switcher plugin by following: https://raw.githubusercontent.com/jiwangyihao/opencode-copilot-account-switcher/master/README.md

Option B: Manual setup

  1. Add the plugin to ~/.config/opencode/opencode.json:

    {
      "$schema": "https://opencode.ai/config.json",
      "plugin": ["opencode-copilot-account-switcher"]
    }
  2. Restart OpenCode (it will auto-download the plugin from npm).

  3. Login to GitHub Copilot:

    opencode auth login --provider github-copilot
For LLM Agents

Step-by-Step Instructions

  1. Edit the OpenCode configuration file at ~/.config/opencode/opencode.json

    Note: This path works on all platforms. On Windows, ~ resolves to your user home directory (e.g., C:\Users\YourName).

  2. Add the plugin to the plugin array:

    {
      "$schema": "https://opencode.ai/config.json",
      "plugin": ["opencode-copilot-account-switcher"]
    }
  3. Restart OpenCode so it fetches the plugin automatically from npm.

Verification

opencode auth login --provider github-copilot

Usage

Run inside the GitHub Copilot auth flow:

opencode auth login --provider github-copilot

You will see an interactive menu. Use the built-in language switch action if you want to swap between Chinese and English labels.

  • Add account
  • Import from auth.json
  • Check quota
  • Guided Loop Safety toggle — enforces dual-channel interaction: strong-interaction content must use question, while pure progress prefers notify and stays silent if notify is unavailable
  • Copilot Network Retry toggle — off by default; only affects the Copilot fetch path for retryable network/certificate failures
  • Synthetic Agent Initiator toggle — off by default; experimental switch that sends or overrides x-initiator=agent, diverges from stable upstream behavior, and does not guarantee non-billable treatment
  • /copilot-status — enabled by default; experimental slash command workaround that shows a loading toast first and then a quota result or error toast
  • /copilot-inject — enabled by default; no-arg force-intervention command that injects a marker into the next non-question tool output and requires immediate question
  • Switch account
  • Delete account
  • Delete all

Guided Loop Safety is enabled by default. In practice, this can keep one request productive for hours: when question is available and permitted, all strong-interaction content (decisions, missing required input, explicit waiting states, final handoff, and no-safe-work-left states) must use it. Pure progress updates and phase changes should use notify; if notify is unavailable, pure progress stays silent and work continues instead of being escalated into interrupting questions. If routing is uncertain, default to question. The policy also constrains user-visible interaction channels to question/notify, avoiding ordinary plain-text assistant interruptions.

Experimental /copilot-inject

  • Default: enabled
  • Trigger: type /copilot-inject in chat (no arguments)
  • Behavior: it first shows a toast saying it will require immediate question on the model's next tool call; then on the next real output from any non-question tool, it appends the marker below and shows another toast saying question is now required:
[COPILOT_INJECT_V1_BEGIN]
立即调用 question 工具并等待用户指示;在收到用户新指示前,不要继续执行后续任务。
[COPILOT_INJECT_V1_END]
  • Clear condition: armed inject state is auto-cleared as soon as question executes
  • Scope: instance memory only; not persisted to store and not shared across plugin instances

wait Tool

  • Default: enabled
  • Usage: wait({ seconds }); seconds is optional and clamped to minimum 30
  • Output shape: started: <ISO>; waited: <N>s; now: <ISO>

If you switch Copilot accounts and then hit transient TLS/network failures or input[*].id too long errors caused by stale session item IDs, enable Copilot Network Retry from the same menu. It is off by default. When enabled, the plugin keeps the official Copilot header/baseURL behavior from the upstream loader, only wraps the final Copilot fetch path, and converts retryable network-like failures into a shape that OpenCode already treats as retryable. It also repairs the matched session part after an input[*].id too long 400 so later retries can recover instead of repeatedly failing on stale item IDs.

You can also enable Synthetic Agent Initiator from the same menu. It is off by default. From a user perspective, it changes the request marker by sending or overriding x-initiator=agent so requests follow an early version of upstream's still-in-development synthetic initiator semantics instead of the current stable upstream behavior; it does not change who makes the final billing decision, and it does not guarantee the platform will treat those requests as non-billable.

Experimental /copilot-status

  • Default: enabled
  • Nature: experimental / workaround, not a stable public plugin command API
  • Support scope: TUI-first; Web/App behavior is explicitly not guaranteed to match
  • Trigger: enter /copilot-status in a normal chat session
  • Expected feedback: first a Fetching Copilot quota... toast, then a success or error toast

To disable this experimental feature, edit ~/.config/opencode/copilot-accounts.json and add or set:

{
  "experimentalStatusSlashCommandEnabled": false
}

After disabling it:

  • OpenCode config injection no longer includes /copilot-status
  • Even manual /copilot-status input will no longer enter the plugin workaround execution chain

Copilot Network Retry

  • Default: disabled
  • Scope: only the official Copilot request fetch path returned by auth.loader
  • Purpose: limited handling for retryable network and certificate-style failures such as failed to fetch, ECONNRESET, unknown certificate, or self signed certificate
  • Strategy: preserve official loader behavior, then normalize retryable failures so OpenCode's native retry pipeline can decide whether and when to retry
  • Risk: because the plugin still wraps the official fetch path, upstream internal behavior may change over time and drift is possible

Synthetic Agent Initiator

  • Default: disabled
  • Effect: sends or overrides x-initiator=agent to enable upstream's in-progress synthetic initiator semantics early
  • Relation to stable upstream: when enabled, request behavior intentionally differs from the current stable upstream code path; this is an early-adoption path based on upstream work in progress, not the stable upstream default
  • Billing note: compressed continue-working flows and other automatically generated synthetic prompt messages may be more likely to fall outside premium request billing, but that is not guaranteed; the platform decides whether and how billing applies, so do not treat this as "guaranteed non-billable"
  • Risk: this behavior may be more likely to be treated as abuse, may stop working as upstream/platform behavior changes, and may also lead to unexpected billing
  • Upstream references:

Upstream Sync

The repository includes a committed upstream snapshot at src/upstream/copilot-plugin.snapshot.ts plus a sync/check script at scripts/sync-copilot-upstream.mjs.

Useful commands:

npm run sync:copilot-snapshot -- --source <file-or-url> --upstream-commit <sha> --sync-date <YYYY-MM-DD>
npm run check:copilot-sync -- --source <file-or-url> --upstream-commit <sha> --sync-date <YYYY-MM-DD>

The script generates or checks the committed snapshot, requires upstream metadata for repository snapshot updates, and helps catch drift from the official opencode copilot.ts implementation.


Storage

Accounts are stored in:

~/.config/opencode/copilot-accounts.json

FAQ

Do I need to change model configurations? No. This plugin only manages accounts and works with the official github-copilot provider.

Does it replace the official provider? No. It uses the official provider and only adds account switching + quota checks.

Does Copilot Network Retry replace OpenCode's retry logic? No. The plugin keeps retry policy inside OpenCode by normalizing retryable Copilot network/TLS failures into a shape that OpenCode already recognizes as retryable.


License

MPL-2.0 License. See LICENSE for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • JavaScript 61.6%
  • TypeScript 38.4%