Commit Graph
2 Commits
Author SHA1 Message Date
yetone 53d4813743 copilot 0.1.1: a refused token exchange marks the sign-in, a quota read GitHub answers clears it (#618)
jinpy666 found idle Copilot slots saying "sign-in has expired — sign in
again" while their OAuth token still traded at
/copilot_internal/v2/token. Two things in the plugin kept the mark wrong:

- The loader's token exchange threw a plain "Token refresh failed: N"
  for every failure, so a refresh GitHub really refused (401) never told
  magpie the sign-in was gone. It now carries signIn "expired" on a 401
  only; 403, 429 and 5xx carry nothing, as before.
- Every quota read said signIn "kept", so a read GitHub answered with
  the OAuth token never cleared a mark. It now says "renewed" on any 200
  with a JSON object (readable quota or not). A 401 still says
  "expired"; every other failure still "kept". Accounts marked before
  this are cleared on their next quota read.

magpie's side (a bare 401 on a stored access token with a refresh token
makes the token due instead of marking the account) ships in magpie
itself.

Run (mote, bun 1.4.2, at this commit): `bun test` in packages/copilot,
42 pass; `bun scripts/check.mjs copilot` ok.
Broken by hand: with index.mjs and usage.mjs back at 0.1.0 the two new
tests fail (the exchange's 401 has no signIn; the 200 read says kept).
Not tried: a real Copilot account, a real 401 from the exchange.
2026-11-02 11:46:50 +08:00
Kurt Lee 67407cf00f copilot 0.1.0: GitHub Copilot with premium request and credit quota (#411)
New package @magpie-community/opencode-copilot-auth 0.1.0 (provider id
github-copilot), from kurtleee. It adapts opencode-copilot-auth 0.0.12 (MIT):
GitHub.com and GHE.com device sign-in, Copilot token renewal, request
signing with initiator and vision headers. Client versions match magpie's
native Copilot (vscode/1.140.0, copilot-chat/0.68.0). Host headers are
read as a Headers object, which is how magpie's host.js passes them. When the
sign-in isn't OAuth, the request goes on to plain fetch.

It adds magpie's auth.usage hook. The hook reads GET
api.<host>/copilot_internal/user with the GitHub OAuth token
(redirect: "error", 15s timeout) and turns the reply into windows the way
magpie's built-in Copilot reader does. It reads only the chat, completions
and premium_interactions snapshots. Credits are fractional, and string
counters are accepted. An exhausted unlimited pool counts as 100% used.
Managed seats block when spent, and personal seats with overage are aside.
credits_used is never divided by the entitlement. An unlimited pool's
credits_used goes in the display, aside and with no limit. Data that can't
be read is unknown, and only HTTP 401 expires the sign-in.

Run at head c7fb3abd, merged onto main 6a2ade35 (Bun 1.4.2, sandbox HOME
and TMPDIR, HTTP(S)_PROXY pointed at a closed port):
- packages/copilot bun test: 40 pass, 0 fail. Full repo bun test: 2838
  pass, 3 skip, 0 fail. bun scripts/check.mjs: all ok.
- A probe sent fetch to a local Bun server, using magpie's real Copilot
  quota bytes (internal/provider/copilot_usage_test.go). Max token billing:
  "0.1 / 20,000 credits used". Enterprise pool spent: 100%, blocking. Pro in
  overage: 300/300, aside. 401: signIn expired. A GHE sign-in reads
  api.<host>. The loader with a host-style Headers object renewed through
  v2/token with Bearer <oauth>. The chat request went out with content-type,
  accept and the extra header kept. Incoming authorization and x-api-key
  were replaced. Vision and X-Initiator were set, and the renewed token was
  saved under github-copilot.
- PR CI (approved at c7fb3abd): test and on-npm pass.

Not tried: a real GitHub or Enterprise account. Not changed: the device
flow treats slow_down as a failure. This comes from upstream.
2026-10-27 16:04:15 +08:00