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.
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
export const HEADERS = {
|
|
"User-Agent": "GitHubCopilotChat/0.68.0",
|
|
"Editor-Version": "vscode/1.140.0",
|
|
"Editor-Plugin-Version": "copilot-chat/0.68.0",
|
|
"Copilot-Integration-Id": "vscode-chat",
|
|
};
|
|
|
|
export function normalizeDomain(value) {
|
|
if (typeof value !== "string" || !value.trim()) {
|
|
throw new Error("GitHub host is required");
|
|
}
|
|
const input = value.trim();
|
|
const url = new URL(input.includes("://") ? input : `https://${input}`);
|
|
if (
|
|
url.protocol !== "https:" || url.username || url.password || url.port ||
|
|
url.search || url.hash || url.pathname !== "/" ||
|
|
!/^[a-z0-9-]+(?:\.[a-z0-9-]+)+$/i.test(url.hostname)
|
|
) {
|
|
throw new Error("Expected an HTTPS GitHub host without credentials, port or path");
|
|
}
|
|
return url.hostname;
|
|
}
|
|
|
|
export function getUrls(domain = "github.com") {
|
|
const host = normalizeDomain(domain);
|
|
const api = `https://api.${host}`;
|
|
return {
|
|
DEVICE_CODE_URL: `https://${host}/login/device/code`,
|
|
ACCESS_TOKEN_URL: `https://${host}/login/oauth/access_token`,
|
|
COPILOT_API_KEY_URL: `${api}/copilot_internal/v2/token`,
|
|
USER_URL: `${api}/copilot_internal/user`,
|
|
};
|
|
}
|