@codeworksh/aikit 0.12.1 → 0.13.0-canary.100.1.g71d09a02
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/cli.mjs +5 -5
- package/{codex-CLDBTrFm.mjs → codex-DvULd3EJ.mjs} +2 -56
- package/codex-DvULd3EJ.mjs.map +1 -0
- package/{copilot-DvUXC6Kx.mjs → copilot-D9T1jbpu.mjs} +13 -3
- package/copilot-D9T1jbpu.mjs.map +1 -0
- package/{copilot-headers-BLnBaJyp.mjs → copilot-headers-I-AOoDed.mjs} +2 -2
- package/{copilot-headers-BLnBaJyp.mjs.map → copilot-headers-I-AOoDed.mjs.map} +1 -1
- package/{failure-5Wjd-qFH.d.mts → failure-CCCRfhCf.d.mts} +6 -3
- package/{failure-DFQNKc-O.mjs → failure-D0qEqOsO.mjs} +37 -5
- package/failure-D0qEqOsO.mjs.map +1 -0
- package/github-copilot-C75m0xwW.mjs +3 -0
- package/{github-copilot-BcmOr0jd.mjs → github-copilot-CdkIxdQS.mjs} +31 -15
- package/github-copilot-CdkIxdQS.mjs.map +1 -0
- package/index.d.mts +540 -237
- package/index.mjs +566 -103
- package/index.mjs.map +1 -1
- package/interactive-c0deRdi7.mjs +115 -0
- package/interactive-c0deRdi7.mjs.map +1 -0
- package/llm/failure.d.mts +2 -2
- package/llm/failure.mjs +2 -2
- package/{model-Cjp-Dr8V.mjs → model-BZZZAySN.mjs} +564 -617
- package/{model-Cjp-Dr8V.mjs.map → model-BZZZAySN.mjs.map} +1 -1
- package/{model-D3wc9-u4.d.mts → model-Dgf7E5gc.d.mts} +20 -1
- package/{openai-codex-C1Yb02kM.mjs → model-av44O6GQ.mjs} +340 -355
- package/model-av44O6GQ.mjs.map +1 -0
- package/{modelgen-DXMmI80T.mjs → modelgen-f3yv7FiK.mjs} +50 -17
- package/modelgen-f3yv7FiK.mjs.map +1 -0
- package/modelgen.d.mts +1 -1
- package/modelgen.mjs +2 -2
- package/oauth/github/copilot.d.mts +10 -0
- package/oauth/github/copilot.mjs +2 -2
- package/oauth/interactive.d.mts +6 -1
- package/oauth/interactive.mjs +2 -2
- package/oauth/openai/codex.mjs +1 -1
- package/openai-codex-BGNrJzTb.mjs +2 -0
- package/openai-codex-DL0lqVuN.mjs +117 -0
- package/openai-codex-DL0lqVuN.mjs.map +1 -0
- package/package.json +1 -1
- package/{registry-u49t0S4S.mjs → registry-BULNTLAP.mjs} +10 -6
- package/{registry-u49t0S4S.mjs.map → registry-BULNTLAP.mjs.map} +1 -1
- package/codex-CLDBTrFm.mjs.map +0 -1
- package/copilot-DvUXC6Kx.mjs.map +0 -1
- package/failure-DFQNKc-O.mjs.map +0 -1
- package/github-copilot-BcmOr0jd.mjs.map +0 -1
- package/github-copilot-llyP2NK4.mjs +0 -3
- package/interactive-eoNnscGB.mjs +0 -57
- package/interactive-eoNnscGB.mjs.map +0 -1
- package/modelgen-DXMmI80T.mjs.map +0 -1
- package/openai-codex-C1Yb02kM.mjs.map +0 -1
- package/openai-codex-CHxShIN3.mjs +0 -2
package/cli.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import { t as
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { i as promptLine, r as openBrowser } from "./interactive-c0deRdi7.mjs";
|
|
3
|
+
import { r as OpenAICodexOAuthClient, t as JsonOpenAICodexAuthStorage } from "./codex-DvULd3EJ.mjs";
|
|
4
|
+
import { t as ModelgenCommand } from "./modelgen-f3yv7FiK.mjs";
|
|
5
|
+
import { i as JsonGitHubCopilotAuthStorage, r as GitHubCopilotOAuthClient } from "./copilot-D9T1jbpu.mjs";
|
|
6
6
|
import { a as oauthNotice, c as summarizeGitHubCopilot, i as oauthLoginIssue, l as summarizeOpenAICodex, n as checkOAuthProvider, r as formatOAuthSummary, s as oauthRefreshIssue } from "./summary-C_QyCVWa.mjs";
|
|
7
7
|
import yargs from "yargs";
|
|
8
8
|
import { hideBin } from "yargs/helpers";
|
|
9
9
|
//#region package.json
|
|
10
|
-
var version = "0.
|
|
10
|
+
var version = "0.13.0-canary.100.1.g71d09a02";
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/cli/oauth.ts
|
|
13
13
|
async function runOpenAICodexAuth(args) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as pollDeviceCode, n as isObject, r as readEnv, t as JsonAuthStorage } from "./storage-v6qjTQ0v.mjs";
|
|
2
|
-
import
|
|
2
|
+
import { n as oauthSuccessHtml, t as oauthErrorHtml } from "./interactive-c0deRdi7.mjs";
|
|
3
3
|
//#region src/oauth/openai/codex.ts
|
|
4
4
|
/**
|
|
5
5
|
* OpenAI Codex OAuth.
|
|
@@ -203,60 +203,6 @@ async function refreshAccessToken(refreshToken) {
|
|
|
203
203
|
};
|
|
204
204
|
}
|
|
205
205
|
}
|
|
206
|
-
function oauthPage(options) {
|
|
207
|
-
const escapeHtml = (value) => value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
|
|
208
|
-
const details = options.details ? escapeHtml(options.details) : void 0;
|
|
209
|
-
return dedent`
|
|
210
|
-
<!doctype html>
|
|
211
|
-
<html lang="en">
|
|
212
|
-
<head>
|
|
213
|
-
<meta charset="utf-8" />
|
|
214
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
215
|
-
<title>${escapeHtml(options.title)}</title>
|
|
216
|
-
<style>
|
|
217
|
-
html { color-scheme: dark; }
|
|
218
|
-
body {
|
|
219
|
-
margin: 0;
|
|
220
|
-
min-height: 100vh;
|
|
221
|
-
display: grid;
|
|
222
|
-
place-items: center;
|
|
223
|
-
padding: 24px;
|
|
224
|
-
background: #09090b;
|
|
225
|
-
color: #fafafa;
|
|
226
|
-
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
|
227
|
-
text-align: center;
|
|
228
|
-
}
|
|
229
|
-
main { width: 100%; max-width: 560px; }
|
|
230
|
-
h1 { margin: 0 0 10px; font-size: 28px; line-height: 1.15; font-weight: 650; }
|
|
231
|
-
p { margin: 0; line-height: 1.7; color: #a1a1aa; font-size: 15px; }
|
|
232
|
-
.details { margin-top: 16px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; color: #a1a1aa; white-space: pre-wrap; word-break: break-word; }
|
|
233
|
-
</style>
|
|
234
|
-
</head>
|
|
235
|
-
<body>
|
|
236
|
-
<main>
|
|
237
|
-
<h1>${escapeHtml(options.heading)}</h1>
|
|
238
|
-
<p>${escapeHtml(options.message)}</p>
|
|
239
|
-
${details ? `<div class="details">${details}</div>` : ""}
|
|
240
|
-
</main>
|
|
241
|
-
</body>
|
|
242
|
-
</html>
|
|
243
|
-
`;
|
|
244
|
-
}
|
|
245
|
-
function oauthSuccessHtml(message) {
|
|
246
|
-
return oauthPage({
|
|
247
|
-
title: "Authentication successful",
|
|
248
|
-
heading: "Authentication successful",
|
|
249
|
-
message
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
function oauthErrorHtml(message, details) {
|
|
253
|
-
return oauthPage({
|
|
254
|
-
title: "Authentication failed",
|
|
255
|
-
heading: "Authentication failed",
|
|
256
|
-
message,
|
|
257
|
-
...details !== void 0 && { details }
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
206
|
async function startLocalOAuthServer(state, redirectUri) {
|
|
261
207
|
const callback = new URL(redirectUri);
|
|
262
208
|
const port = Number(callback.port || (callback.protocol === "https:" ? 443 : 80));
|
|
@@ -536,4 +482,4 @@ async function getOpenAICodexApiKey(options = {}) {
|
|
|
536
482
|
//#endregion
|
|
537
483
|
export { createOpenAICodexDeviceAuth as a, getOpenAICodexApiKey as c, pollOpenAICodexDeviceAuth as d, refreshOpenAICodexToken as f, createOpenAICodexAuthorizationFlow as i, openAICodexHeaders as l, OPENAI_CODEX_API_KEY_ENV as n, exchangeOpenAICodexAuthorizationCode as o, OpenAICodexOAuthClient as r, getOpenAICodexAccountId as s, JsonOpenAICodexAuthStorage as t, parseOpenAICodexAuthorizationInput as u };
|
|
538
484
|
|
|
539
|
-
//# sourceMappingURL=codex-
|
|
485
|
+
//# sourceMappingURL=codex-DvULd3EJ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codex-DvULd3EJ.mjs","names":[],"sources":["../../src/oauth/openai/codex.ts"],"sourcesContent":["/**\n * OpenAI Codex OAuth.\n *\n * Public interface, by use case:\n *\n * 1. Server OAuth integration (storage-free, bring your own routes):\n * `createOpenAICodexAuthorizationFlow`, `exchangeOpenAICodexAuthorizationCode`,\n * `parseOpenAICodexAuthorizationInput`, `refreshOpenAICodexToken`, `getOpenAICodexAccountId`.\n * 2. CLI / interactive login (localhost callback server, persisted credentials):\n * `OpenAICodexOAuthClient`, `JsonOpenAICodexAuthStorage`, `OpenAICodexAuthStorage`.\n * 3. Application credential access (get key, auto-refresh, request headers):\n * `getOpenAICodexApiKey`, `OpenAICodexOAuthClient.getCredentials/getApiKey/getHeaders`,\n * `openAICodexHeaders`, `OPENAI_CODEX_API_KEY_ENV`.\n *\n * Everything else in this module is an implementation detail and intentionally\n * not exported.\n */\n\nimport { pollDeviceCode } from \"../devcode.ts\";\nimport { oauthErrorHtml, oauthSuccessHtml } from \"../interactive.ts\";\nimport { JsonAuthStorage, isObject, readEnv } from \"../storage.ts\";\n\nconst CODEWORK_OAUTH_CALLBACK_HOST = \"CODEWORK_OAUTH_CALLBACK_HOST\";\n\nconst CLIENT_ID = \"app_EMoamEEZ73f0CkXaXp7hrann\";\nconst AUTHORIZE_URL = \"https://auth.openai.com/oauth/authorize\";\nconst TOKEN_URL = \"https://auth.openai.com/oauth/token\";\nconst DEFAULT_REDIRECT_URI = \"http://localhost:1455/auth/callback\";\n// Device code: no local port, so it works over SSH, in a container, and when\n// something else already holds 1455. The server mints the PKCE verifier and\n// redeems the code against its own callback.\nconst DEVICE_USER_CODE_URL = \"https://auth.openai.com/api/accounts/deviceauth/usercode\";\nconst DEVICE_TOKEN_URL = \"https://auth.openai.com/api/accounts/deviceauth/token\";\nconst DEVICE_VERIFICATION_URI = \"https://auth.openai.com/codex/device\";\nconst DEVICE_REDIRECT_URI = \"https://auth.openai.com/deviceauth/callback\";\nconst DEVICE_TIMEOUT_SECONDS = 15 * 60;\nconst DEFAULT_SCOPE = \"openid profile email offline_access\";\nconst DEFAULT_ORIGINATOR = \"codework\";\nconst DEFAULT_PROVIDER_ID = \"openai-codex\";\nconst JWT_CLAIM_PATH = \"https://api.openai.com/auth\";\nconst REFRESH_SKEW_MS = 60 * 1000;\n\nexport type OpenAICodexOAuthCredentials = {\n\taccess: string;\n\trefresh: string;\n\texpires: number;\n\taccountId: string;\n};\n\nexport type OpenAICodexTokenSuccess = {\n\ttype: \"success\";\n\taccess: string;\n\trefresh: string;\n\texpires: number;\n};\n\nexport type OpenAICodexTokenFailure = {\n\ttype: \"failed\";\n\tmessage: string;\n\tstatus?: number;\n};\n\nexport type OpenAICodexTokenResult = OpenAICodexTokenSuccess | OpenAICodexTokenFailure;\n\nexport type OpenAICodexAuthorizationFlow = {\n\turl: string;\n\tverifier: string;\n\tstate: string;\n\tredirectUri: string;\n};\n\nexport type OpenAICodexAuthorizationOptions = {\n\toriginator?: string;\n\tredirectUri?: string;\n\tscope?: string;\n};\n\nexport type OpenAICodexLoginPrompt = {\n\tmessage: string;\n\tplaceholder?: string;\n\tallowEmpty?: boolean;\n};\n\nexport type OpenAICodexDeviceAuth = {\n\tdeviceAuthId: string;\n\tuserCode: string;\n\tverificationUri: string;\n\tintervalSeconds: number;\n};\n\nexport type OpenAICodexLoginOptions = OpenAICodexAuthorizationOptions & {\n\t/** Use the device-code flow instead of the localhost browser callback. */\n\tdevice?: boolean | undefined;\n\tsignal?: AbortSignal | undefined;\n\tonAuth: (info: { url: string; userCode?: string | undefined; instructions?: string | undefined }) => void;\n\tonPrompt: (prompt: OpenAICodexLoginPrompt) => Promise<string>;\n\tonProgress?: (message: string) => void;\n};\n\nexport interface OpenAICodexAuthStorage {\n\tget(): Promise<OpenAICodexOAuthCredentials | undefined>;\n\tset(credentials: OpenAICodexOAuthCredentials): Promise<void>;\n\tclear(): Promise<void>;\n}\n\nexport type JsonOpenAICodexAuthStorageOptions = {\n\tpath?: string;\n\tproviderId?: string;\n};\n\ntype JwtPayload = {\n\t[JWT_CLAIM_PATH]?: {\n\t\tchatgpt_account_id?: string;\n\t};\n\t[key: string]: unknown;\n};\n\ntype OAuthServerInfo = {\n\tclose: () => Promise<void>;\n\twaitForCode: () => Promise<{ code: string } | null>;\n\t/** Set when the callback port could not be bound; login falls back to pasting. */\n\tlistenError?: Error;\n};\n\ntype TokenEndpointPayload = {\n\taccess_token?: string;\n\trefresh_token?: string;\n\texpires_in?: number;\n};\n\nfunction isOpenAICodexCredentials(value: unknown): value is OpenAICodexOAuthCredentials {\n\tif (!isObject(value)) return false;\n\treturn (\n\t\ttypeof value.access === \"string\" &&\n\t\ttypeof value.refresh === \"string\" &&\n\t\ttypeof value.expires === \"number\" &&\n\t\ttypeof value.accountId === \"string\"\n\t);\n}\n\nfunction redactOAuthSecrets(value: string, secrets: readonly string[] = []): string {\n\tlet redacted = value;\n\tfor (const secret of secrets.filter(Boolean).sort((left, right) => right.length - left.length)) {\n\t\tredacted = redacted.replaceAll(secret, \"[REDACTED]\");\n\t}\n\treturn redacted\n\t\t.replace(\n\t\t\t/((?:\"?(?:access_token|refresh_token|id_token|code|code_verifier)\"?)\\s*[:=]\\s*)(?:\"[^\"]*\"|'[^']*'|[^&\\s,}]+)/gi,\n\t\t\t'$1\"[REDACTED]\"',\n\t\t)\n\t\t.replace(/Bearer\\s+\\S+/gi, \"Bearer [REDACTED]\");\n}\n\nfunction tokenEndpointResult(payload: TokenEndpointPayload, operation: \"exchange\" | \"refresh\"): OpenAICodexTokenResult {\n\tconst missing = [\n\t\t!payload.access_token && \"access_token\",\n\t\t!payload.refresh_token && \"refresh_token\",\n\t\ttypeof payload.expires_in !== \"number\" && \"expires_in\",\n\t].filter((field): field is string => Boolean(field));\n\tif (missing.length > 0) {\n\t\treturn {\n\t\t\ttype: \"failed\",\n\t\t\tmessage: `OpenAI Codex token ${operation} response missing fields: ${missing.join(\", \")}`,\n\t\t};\n\t}\n\n\treturn {\n\t\ttype: \"success\",\n\t\taccess: payload.access_token!,\n\t\trefresh: payload.refresh_token!,\n\t\texpires: Date.now() + payload.expires_in! * 1000,\n\t};\n}\n\nfunction base64UrlEncode(bytes: Uint8Array): string {\n\tlet binary = \"\";\n\tfor (const byte of bytes) {\n\t\tbinary += String.fromCharCode(byte);\n\t}\n\treturn btoa(binary).replaceAll(\"+\", \"-\").replaceAll(\"/\", \"_\").replaceAll(\"=\", \"\");\n}\n\nfunction base64UrlDecode(value: string): string {\n\tconst base64 = value.replaceAll(\"-\", \"+\").replaceAll(\"_\", \"/\");\n\tconst padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, \"=\");\n\treturn atob(padded);\n}\n\ntype WebCrypto = typeof globalThis.crypto;\n\nfunction getCrypto(): WebCrypto {\n\tif (!globalThis.crypto?.getRandomValues || !globalThis.crypto.subtle) {\n\t\tthrow new Error(\"openAI codex OAuth requires web crypto support\");\n\t}\n\treturn globalThis.crypto;\n}\n\nfunction createState(): string {\n\tconst bytes = new Uint8Array(16);\n\tgetCrypto().getRandomValues(bytes);\n\treturn Array.from(bytes, (byte) => byte.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nasync function generatePKCE(): Promise<{\n\tverifier: string;\n\tchallenge: string;\n}> {\n\tconst bytes = new Uint8Array(32);\n\tgetCrypto().getRandomValues(bytes);\n\tconst verifier = base64UrlEncode(bytes);\n\n\tconst data = new TextEncoder().encode(verifier);\n\tconst hash = await getCrypto().subtle.digest(\"SHA-256\", data);\n\tconst challenge = base64UrlEncode(new Uint8Array(hash));\n\n\treturn { verifier, challenge };\n}\n\nfunction authorizationInput(code: string | null | undefined, state: string | null | undefined) {\n\treturn {\n\t\t...(code != null && { code }),\n\t\t...(state != null && { state }),\n\t};\n}\n\nexport function parseOpenAICodexAuthorizationInput(input: string): {\n\tcode?: string;\n\tstate?: string;\n} {\n\tconst value = input.trim();\n\tif (!value) return {};\n\n\ttry {\n\t\tconst url = new URL(value);\n\t\treturn authorizationInput(url.searchParams.get(\"code\"), url.searchParams.get(\"state\"));\n\t} catch {\n\t\t// Not a URL.\n\t}\n\n\tif (value.includes(\"#\")) {\n\t\tconst [code, state] = value.split(\"#\", 2);\n\t\treturn authorizationInput(code, state);\n\t}\n\n\tif (value.includes(\"code=\")) {\n\t\tconst params = new URLSearchParams(value);\n\t\treturn authorizationInput(params.get(\"code\"), params.get(\"state\"));\n\t}\n\n\treturn { code: value };\n}\n\nfunction decodeJwt(token: string): JwtPayload | null {\n\ttry {\n\t\tconst parts = token.split(\".\");\n\t\tif (parts.length !== 3) return null;\n\t\treturn JSON.parse(base64UrlDecode(parts[1] ?? \"\")) as JwtPayload;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nexport function getOpenAICodexAccountId(accessToken: string): string | null {\n\tconst payload = decodeJwt(accessToken);\n\tconst auth = payload?.[JWT_CLAIM_PATH];\n\tconst accountId = auth?.chatgpt_account_id;\n\treturn typeof accountId === \"string\" && accountId.length > 0 ? accountId : null;\n}\n\nexport async function createOpenAICodexAuthorizationFlow(\n\toptions: OpenAICodexAuthorizationOptions = {},\n): Promise<OpenAICodexAuthorizationFlow> {\n\tconst redirectUri = options.redirectUri ?? DEFAULT_REDIRECT_URI;\n\tconst { verifier, challenge } = await generatePKCE();\n\tconst state = createState();\n\n\tconst url = new URL(AUTHORIZE_URL);\n\turl.searchParams.set(\"response_type\", \"code\");\n\turl.searchParams.set(\"client_id\", CLIENT_ID);\n\turl.searchParams.set(\"redirect_uri\", redirectUri);\n\turl.searchParams.set(\"scope\", options.scope ?? DEFAULT_SCOPE);\n\turl.searchParams.set(\"code_challenge\", challenge);\n\turl.searchParams.set(\"code_challenge_method\", \"S256\");\n\turl.searchParams.set(\"state\", state);\n\turl.searchParams.set(\"id_token_add_organizations\", \"true\");\n\turl.searchParams.set(\"codex_cli_simplified_flow\", \"true\");\n\turl.searchParams.set(\"originator\", options.originator ?? DEFAULT_ORIGINATOR);\n\n\treturn { verifier, state, redirectUri, url: url.toString() };\n}\n\nexport async function exchangeOpenAICodexAuthorizationCode(\n\tcode: string,\n\tverifier: string,\n\tredirectUri: string = DEFAULT_REDIRECT_URI,\n): Promise<OpenAICodexTokenResult> {\n\ttry {\n\t\tconst response = await fetch(TOKEN_URL, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: { \"Content-Type\": \"application/x-www-form-urlencoded\" },\n\t\t\tbody: new URLSearchParams({\n\t\t\t\tgrant_type: \"authorization_code\",\n\t\t\t\tclient_id: CLIENT_ID,\n\t\t\t\tcode,\n\t\t\t\tcode_verifier: verifier,\n\t\t\t\tredirect_uri: redirectUri,\n\t\t\t}),\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tconst text = await response.text().catch(() => \"\");\n\t\t\treturn {\n\t\t\t\ttype: \"failed\",\n\t\t\t\tstatus: response.status,\n\t\t\t\tmessage: `OpenAI Codex token exchange failed (${response.status}): ${redactOAuthSecrets(\n\t\t\t\t\ttext || response.statusText,\n\t\t\t\t\t[code, verifier],\n\t\t\t\t)}`,\n\t\t\t};\n\t\t}\n\n\t\treturn tokenEndpointResult((await response.json()) as TokenEndpointPayload, \"exchange\");\n\t} catch (error) {\n\t\treturn {\n\t\t\ttype: \"failed\",\n\t\t\tmessage: `OpenAI Codex token exchange error: ${redactOAuthSecrets(\n\t\t\t\terror instanceof Error ? error.message : String(error),\n\t\t\t\t[code, verifier],\n\t\t\t)}`,\n\t\t};\n\t}\n}\n\nasync function refreshAccessToken(refreshToken: string): Promise<OpenAICodexTokenResult> {\n\ttry {\n\t\tconst response = await fetch(TOKEN_URL, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: { \"Content-Type\": \"application/x-www-form-urlencoded\" },\n\t\t\tbody: new URLSearchParams({\n\t\t\t\tgrant_type: \"refresh_token\",\n\t\t\t\trefresh_token: refreshToken,\n\t\t\t\tclient_id: CLIENT_ID,\n\t\t\t}),\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tconst text = await response.text().catch(() => \"\");\n\t\t\treturn {\n\t\t\t\ttype: \"failed\",\n\t\t\t\tstatus: response.status,\n\t\t\t\tmessage: `OpenAI Codex token refresh failed (${response.status}): ${redactOAuthSecrets(\n\t\t\t\t\ttext || response.statusText,\n\t\t\t\t\t[refreshToken],\n\t\t\t\t)}`,\n\t\t\t};\n\t\t}\n\n\t\treturn tokenEndpointResult((await response.json()) as TokenEndpointPayload, \"refresh\");\n\t} catch (error) {\n\t\treturn {\n\t\t\ttype: \"failed\",\n\t\t\tmessage: `OpenAI Codex token refresh error: ${redactOAuthSecrets(\n\t\t\t\terror instanceof Error ? error.message : String(error),\n\t\t\t\t[refreshToken],\n\t\t\t)}`,\n\t\t};\n\t}\n}\n\nasync function startLocalOAuthServer(state: string, redirectUri: string): Promise<OAuthServerInfo> {\n\t// The port and path are the ones the authorization server will redirect to:\n\t// binding anything else means the callback arrives somewhere we are not.\n\tconst callback = new URL(redirectUri);\n\tconst port = Number(callback.port || (callback.protocol === \"https:\" ? 443 : 80));\n\n\tconst http = await import(\"node:http\").catch(() => undefined);\n\tif (!http) {\n\t\tthrow new Error(\"openAI codex OAuth callback server is only available in Node.js environments\");\n\t}\n\n\tlet settleWait: ((value: { code: string } | null) => void) | undefined;\n\tconst waitForCodePromise = new Promise<{ code: string } | null>((resolve) => {\n\t\tlet settled = false;\n\t\tsettleWait = (value) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tresolve(value);\n\t\t};\n\t});\n\n\tconst server = http.createServer((req, res) => {\n\t\ttry {\n\t\t\tconst url = new URL(req.url || \"\", \"http://localhost\");\n\t\t\tif (url.pathname !== callback.pathname) {\n\t\t\t\tres.statusCode = 404;\n\t\t\t\tres.setHeader(\"Content-Type\", \"text/html; charset=utf-8\");\n\t\t\t\tres.end(oauthErrorHtml(\"Callback route not found.\"));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (url.searchParams.get(\"state\") !== state) {\n\t\t\t\tres.statusCode = 400;\n\t\t\t\tres.setHeader(\"Content-Type\", \"text/html; charset=utf-8\");\n\t\t\t\tres.end(oauthErrorHtml(\"State mismatch.\"));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst code = url.searchParams.get(\"code\");\n\t\t\tif (!code) {\n\t\t\t\tres.statusCode = 400;\n\t\t\t\tres.setHeader(\"Content-Type\", \"text/html; charset=utf-8\");\n\t\t\t\tres.end(oauthErrorHtml(\"Missing authorization code.\"));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tres.statusCode = 200;\n\t\t\tres.setHeader(\"Content-Type\", \"text/html; charset=utf-8\");\n\t\t\tres.end(oauthSuccessHtml(\"OpenAI authentication completed. You can close this window.\"));\n\t\t\tsettleWait?.({ code });\n\t\t} catch (error) {\n\t\t\tres.statusCode = 500;\n\t\t\tres.setHeader(\"Content-Type\", \"text/html; charset=utf-8\");\n\t\t\tres.end(\n\t\t\t\toauthErrorHtml(\n\t\t\t\t\t\"Internal error while processing OAuth callback.\",\n\t\t\t\t\terror instanceof Error ? error.message : String(error),\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t});\n\tconst close = async (): Promise<void> => {\n\t\tif (!server.listening) return;\n\t\tawait new Promise<void>((resolve) => {\n\t\t\tserver.close(() => resolve());\n\t\t\tserver.closeAllConnections();\n\t\t});\n\t};\n\n\treturn new Promise((resolve) => {\n\t\tserver\n\t\t\t.listen(port, readEnv(CODEWORK_OAUTH_CALLBACK_HOST) || \"127.0.0.1\", () => {\n\t\t\t\tresolve({\n\t\t\t\t\tclose,\n\t\t\t\t\twaitForCode: () => waitForCodePromise,\n\t\t\t\t});\n\t\t\t})\n\t\t\t.on(\"error\", (error: Error) => {\n\t\t\t\tsettleWait?.(null);\n\t\t\t\tresolve({\n\t\t\t\t\tclose,\n\t\t\t\t\twaitForCode: async () => null,\n\t\t\t\t\tlistenError: error,\n\t\t\t\t});\n\t\t\t});\n\t});\n}\n\n// Internal: the public login entry point is `OpenAICodexOAuthClient#login`,\n// which persists the credentials. Server integrations should use the\n// storage-free authorization helpers instead of this localhost-callback flow.\nasync function loginOpenAICodex(options: OpenAICodexLoginOptions): Promise<OpenAICodexOAuthCredentials> {\n\tif (options.device) return loginOpenAICodexDeviceCode(options);\n\n\tconst flow = await createOpenAICodexAuthorizationFlow(options);\n\tconst server = await startLocalOAuthServer(flow.state, flow.redirectUri);\n\n\toptions.onAuth({\n\t\turl: flow.url,\n\t\tinstructions: \"Complete the browser login to finish OpenAI Codex authentication.\",\n\t});\n\n\tif (server.listenError) {\n\t\t// The redirect URI is registered against the OAuth client, so the port\n\t\t// cannot move. Whatever holds it -- usually another Codex login already in\n\t\t// progress -- will receive this callback instead of us.\n\t\toptions.onProgress?.(\n\t\t\t`Could not listen on ${flow.redirectUri}: ${server.listenError.message}. ` +\n\t\t\t\t\"Another sign-in is probably already running on that port and would receive the callback. \" +\n\t\t\t\t\"Finish the login in the browser, then paste the redirect URL here.\",\n\t\t);\n\t}\n\n\tlet code: string | undefined;\n\ttry {\n\t\tconst result = await server.waitForCode();\n\t\tif (result?.code) code = result.code;\n\n\t\t// The callback never arrived -- the port was taken, or the browser could\n\t\t// not reach us. Ask for the redirect URL instead.\n\t\tif (!code) {\n\t\t\tconst input = await options.onPrompt({\n\t\t\t\tmessage: \"Paste the authorization code or full redirect URL:\",\n\t\t\t});\n\t\t\tconst parsed = parseOpenAICodexAuthorizationInput(input);\n\t\t\tif (parsed.state && parsed.state !== flow.state) throw new Error(\"state mismatch\");\n\t\t\tcode = parsed.code;\n\t\t}\n\n\t\tif (!code) throw new Error(\"missing authorization code\");\n\n\t\treturn await credentialsFromCode(code, flow.verifier, flow.redirectUri);\n\t} finally {\n\t\tawait server.close();\n\t}\n}\n\n/** The step both flows share: redeem the code, then read the account off the JWT. */\nasync function credentialsFromCode(\n\tcode: string,\n\tverifier: string,\n\tredirectUri: string,\n): Promise<OpenAICodexOAuthCredentials> {\n\tconst tokenResult = await exchangeOpenAICodexAuthorizationCode(code, verifier, redirectUri);\n\tif (tokenResult.type !== \"success\") throw new Error(tokenResult.message);\n\n\tconst accountId = getOpenAICodexAccountId(tokenResult.access);\n\tif (!accountId) throw new Error(\"failed to extract accountId from token\");\n\n\treturn {\n\t\taccess: tokenResult.access,\n\t\trefresh: tokenResult.refresh,\n\t\texpires: tokenResult.expires,\n\t\taccountId,\n\t};\n}\n\n/** Ask for a user code to type at {@link DEVICE_VERIFICATION_URI}. */\nexport async function createOpenAICodexDeviceAuth(\n\toptions: { signal?: AbortSignal | undefined } = {},\n): Promise<OpenAICodexDeviceAuth> {\n\tconst response = await fetch(DEVICE_USER_CODE_URL, {\n\t\tmethod: \"POST\",\n\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\tbody: JSON.stringify({ client_id: CLIENT_ID }),\n\t\tsignal: options.signal ?? null,\n\t});\n\tif (!response.ok) {\n\t\tif (response.status === 404) {\n\t\t\tthrow new Error(\n\t\t\t\t\"OpenAI Codex device code login is not available for this account. Use the browser login instead.\",\n\t\t\t);\n\t\t}\n\t\tconst body = await response.text().catch(() => \"\");\n\t\tthrow new Error(\n\t\t\t`OpenAI Codex device code request failed (${response.status})${body ? `: ${redactOAuthSecrets(body)}` : \"\"}`,\n\t\t);\n\t}\n\n\tconst data: unknown = await response.json();\n\tif (!isObject(data)) throw new Error(\"Invalid OpenAI Codex device code response\");\n\tconst interval = typeof data.interval === \"string\" ? Number(data.interval.trim()) : data.interval;\n\tif (\n\t\ttypeof data.device_auth_id !== \"string\" ||\n\t\ttypeof data.user_code !== \"string\" ||\n\t\ttypeof interval !== \"number\" ||\n\t\t!Number.isFinite(interval) ||\n\t\tinterval < 0\n\t) {\n\t\tthrow new Error(\"Invalid OpenAI Codex device code response fields\");\n\t}\n\n\treturn {\n\t\tdeviceAuthId: data.device_auth_id,\n\t\tuserCode: data.user_code,\n\t\tverificationUri: DEVICE_VERIFICATION_URI,\n\t\tintervalSeconds: interval,\n\t};\n}\n\n/** Wait for the code to be approved; the server returns the code and its verifier. */\nexport async function pollOpenAICodexDeviceAuth(\n\tdevice: OpenAICodexDeviceAuth,\n\toptions: { signal?: AbortSignal | undefined } = {},\n): Promise<{ authorizationCode: string; codeVerifier: string }> {\n\treturn pollDeviceCode({\n\t\tintervalSeconds: device.intervalSeconds,\n\t\texpiresInSeconds: DEVICE_TIMEOUT_SECONDS,\n\t\t...(options.signal !== undefined && { signal: options.signal }),\n\t\tpoll: async () => {\n\t\t\tconst response = await fetch(DEVICE_TOKEN_URL, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({ device_auth_id: device.deviceAuthId, user_code: device.userCode }),\n\t\t\t\tsignal: options.signal ?? null,\n\t\t\t});\n\n\t\t\tif (response.ok) {\n\t\t\t\tconst data: unknown = await response.json();\n\t\t\t\tif (\n\t\t\t\t\t!isObject(data) ||\n\t\t\t\t\ttypeof data.authorization_code !== \"string\" ||\n\t\t\t\t\ttypeof data.code_verifier !== \"string\"\n\t\t\t\t) {\n\t\t\t\t\treturn { status: \"failed\", message: \"Invalid OpenAI Codex device auth token response\" };\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"complete\",\n\t\t\t\t\tvalue: { authorizationCode: data.authorization_code, codeVerifier: data.code_verifier },\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// The endpoint answers 403/404 while the code is still unclaimed.\n\t\t\tif (response.status === 403 || response.status === 404) return { status: \"pending\" };\n\n\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\tlet errorCode: unknown;\n\t\t\ttry {\n\t\t\t\tconst parsed: unknown = JSON.parse(body);\n\t\t\t\tconst error = isObject(parsed) ? parsed.error : undefined;\n\t\t\t\terrorCode = isObject(error) ? error.code : error;\n\t\t\t} catch {\n\t\t\t\t// Not JSON; fall through to the generic failure below.\n\t\t\t}\n\t\t\tif (errorCode === \"deviceauth_authorization_pending\") return { status: \"pending\" };\n\t\t\tif (errorCode === \"slow_down\") return { status: \"slow_down\" };\n\n\t\t\treturn {\n\t\t\t\tstatus: \"failed\",\n\t\t\t\tmessage: `OpenAI Codex device authorization failed (${response.status})${\n\t\t\t\t\tbody ? `: ${redactOAuthSecrets(body)}` : \"\"\n\t\t\t\t}`,\n\t\t\t};\n\t\t},\n\t});\n}\n\nasync function loginOpenAICodexDeviceCode(options: OpenAICodexLoginOptions): Promise<OpenAICodexOAuthCredentials> {\n\tconst device = await createOpenAICodexDeviceAuth(options);\n\toptions.onAuth({\n\t\turl: device.verificationUri,\n\t\tuserCode: device.userCode,\n\t\tinstructions: \"Open the URL and enter the code to finish OpenAI Codex authentication.\",\n\t});\n\n\tconst { authorizationCode, codeVerifier } = await pollOpenAICodexDeviceAuth(device, options);\n\treturn credentialsFromCode(authorizationCode, codeVerifier, DEVICE_REDIRECT_URI);\n}\n\nexport async function refreshOpenAICodexToken(refreshToken: string): Promise<OpenAICodexOAuthCredentials> {\n\tconst result = await refreshAccessToken(refreshToken);\n\tif (result.type !== \"success\") {\n\t\tthrow new Error(result.message);\n\t}\n\n\tconst accountId = getOpenAICodexAccountId(result.access);\n\tif (!accountId) {\n\t\tthrow new Error(\"failed to extract accountId from token\");\n\t}\n\n\treturn {\n\t\taccess: result.access,\n\t\trefresh: result.refresh,\n\t\texpires: result.expires,\n\t\taccountId,\n\t};\n}\n\nexport function openAICodexHeaders(credentials: OpenAICodexOAuthCredentials): Record<string, string> {\n\treturn {\n\t\tAuthorization: `Bearer ${credentials.access}`,\n\t\t\"chatgpt-account-id\": credentials.accountId,\n\t\t\"OpenAI-Beta\": \"responses=experimental\",\n\t};\n}\n\nexport class JsonOpenAICodexAuthStorage implements OpenAICodexAuthStorage {\n\tprivate readonly storage: JsonAuthStorage<OpenAICodexOAuthCredentials>;\n\n\tconstructor(options: JsonOpenAICodexAuthStorageOptions = {}) {\n\t\tthis.storage = new JsonAuthStorage({\n\t\t\tproviderId: options.providerId ?? DEFAULT_PROVIDER_ID,\n\t\t\tisCredentials: isOpenAICodexCredentials,\n\t\t\t...(options.path !== undefined && { path: options.path }),\n\t\t});\n\t}\n\n\tget path(): string {\n\t\treturn this.storage.path;\n\t}\n\n\tget providerId(): string {\n\t\treturn this.storage.providerId;\n\t}\n\n\tget(): Promise<OpenAICodexOAuthCredentials | undefined> {\n\t\treturn this.storage.get();\n\t}\n\n\tset(credentials: OpenAICodexOAuthCredentials): Promise<void> {\n\t\treturn this.storage.set(credentials);\n\t}\n\n\tclear(): Promise<void> {\n\t\treturn this.storage.clear();\n\t}\n}\n\nexport type OpenAICodexOAuthClientOptions = {\n\tstorage?: OpenAICodexAuthStorage;\n\trefreshSkewMs?: number;\n};\n\nexport class OpenAICodexOAuthClient {\n\treadonly storage: OpenAICodexAuthStorage;\n\treadonly refreshSkewMs: number;\n\n\tconstructor(options: OpenAICodexOAuthClientOptions = {}) {\n\t\tthis.storage = options.storage ?? new JsonOpenAICodexAuthStorage();\n\t\tthis.refreshSkewMs = options.refreshSkewMs ?? REFRESH_SKEW_MS;\n\t}\n\n\tasync login(options: OpenAICodexLoginOptions): Promise<OpenAICodexOAuthCredentials> {\n\t\tconst credentials = await loginOpenAICodex(options);\n\t\tawait this.storage.set(credentials);\n\t\treturn credentials;\n\t}\n\n\tasync getCredentials(): Promise<OpenAICodexOAuthCredentials | undefined> {\n\t\tconst credentials = await this.storage.get();\n\t\tif (!credentials) return undefined;\n\n\t\tif (credentials.expires > Date.now() + this.refreshSkewMs) {\n\t\t\treturn credentials;\n\t\t}\n\n\t\tconst refreshed = await refreshOpenAICodexToken(credentials.refresh);\n\t\tawait this.storage.set(refreshed);\n\t\treturn refreshed;\n\t}\n\n\tasync getApiKey(): Promise<string | undefined> {\n\t\treturn (await this.getCredentials())?.access;\n\t}\n\n\tasync getHeaders(): Promise<Record<string, string> | undefined> {\n\t\tconst credentials = await this.getCredentials();\n\t\treturn credentials ? openAICodexHeaders(credentials) : undefined;\n\t}\n\n\tasync logout(): Promise<void> {\n\t\tawait this.storage.clear();\n\t}\n}\n\nexport const OPENAI_CODEX_API_KEY_ENV = \"OPENAI_CODEX_API_KEY\";\n\n/**\n * Resolve the OpenAI Codex API key the same way other provider API keys are\n * resolved: prefer the OPENAI_CODEX_API_KEY environment variable, then fall\n * back to stored OAuth credentials (refreshing them when close to expiry).\n *\n * The result can be passed directly as `apiKey` to the runtime options or to\n * `createOpenAICodex`. Returns undefined when no key is available.\n */\nexport async function getOpenAICodexApiKey(options: OpenAICodexOAuthClientOptions = {}): Promise<string | undefined> {\n\tconst envKey = readEnv(OPENAI_CODEX_API_KEY_ENV);\n\tif (envKey) return envKey;\n\n\treturn new OpenAICodexOAuthClient(options).getApiKey();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAsBA,MAAM,+BAA+B;AAErC,MAAM,YAAY;AAClB,MAAM,gBAAgB;AACtB,MAAM,YAAY;AAClB,MAAM,uBAAuB;AAI7B,MAAM,uBAAuB;AAC7B,MAAM,mBAAmB;AACzB,MAAM,0BAA0B;AAChC,MAAM,sBAAsB;AAC5B,MAAM,yBAAyB;AAC/B,MAAM,gBAAgB;AACtB,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAC5B,MAAM,iBAAiB;AACvB,MAAM,kBAAkB;AA0FxB,SAAS,yBAAyB,OAAsD;CACvF,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO;CAC7B,OACC,OAAO,MAAM,WAAW,YACxB,OAAO,MAAM,YAAY,YACzB,OAAO,MAAM,YAAY,YACzB,OAAO,MAAM,cAAc;AAE7B;AAEA,SAAS,mBAAmB,OAAe,UAA6B,CAAC,GAAW;CACnF,IAAI,WAAW;CACf,KAAK,MAAM,UAAU,QAAQ,OAAO,OAAO,CAAC,CAAC,MAAM,MAAM,UAAU,MAA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OAAO;EAGR,MAAM,YAAY,MAAM,wBAAwB,YAAY,OAAO;EACnE,MAAM,KAAK,QAAQ,IAAI,SAAS;EAChC,OAAO;CACR;CAEA,MAAM,YAAyC;EAC9C,QAAQ,MAAM,KAAK,eAAe,EAAA,EAAI;CACvC;CAEA,MAAM,aAA0D;EAC/D,MAAM,cAAc,MAAM,KAAK,eAAe;EAC9C,OAAO,cAAc,mBAAmB,WAAW,IAAI,KAAA;CACxD;CAEA,MAAM,SAAwB;EAC7B,MAAM,KAAK,QAAQ,MAAM;CAC1B;AACD;AAEA,MAAa,2BAA2B;;;;;;;;;AAUxC,eAAsB,qBAAqB,UAAyC,CAAC,GAAgC;CACpH,MAAM,SAAS,QAAQ,wBAAwB;CAC/C,IAAI,QAAQ,OAAO;CAEnB,OAAO,IAAI,uBAAuB,OAAO,CAAC,CAAC,UAAU;AACtD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as pollDeviceCode, n as isObject, r as readEnv, t as JsonAuthStorage } from "./storage-v6qjTQ0v.mjs";
|
|
2
|
-
import { n as GITHUB_COPILOT_STATIC_HEADERS, t as GITHUB_COPILOT_API_VERSION } from "./copilot-headers-
|
|
2
|
+
import { n as GITHUB_COPILOT_STATIC_HEADERS, t as GITHUB_COPILOT_API_VERSION } from "./copilot-headers-I-AOoDed.mjs";
|
|
3
3
|
//#region src/oauth/github/copilot.ts
|
|
4
4
|
/**
|
|
5
5
|
* GitHub Copilot OAuth.
|
|
@@ -55,6 +55,16 @@ function normalizeGitHubDomain(input) {
|
|
|
55
55
|
function gitHubApiDomain(domain) {
|
|
56
56
|
return domain === DEFAULT_DOMAIN ? "api.github.com" : `api.${domain}`;
|
|
57
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* The catalog models a login can reach, by the `availableModelIds` it recorded at login.
|
|
60
|
+
* A login that recorded none keeps every model.
|
|
61
|
+
*/
|
|
62
|
+
function filterGitHubCopilotModels(models, credentials) {
|
|
63
|
+
const ids = credentials?.availableModelIds;
|
|
64
|
+
if (ids === void 0) return { ...models };
|
|
65
|
+
const available = new Set(ids);
|
|
66
|
+
return Object.fromEntries(Object.entries(models).filter(([, model]) => available.has(model.api?.id ?? model.id)));
|
|
67
|
+
}
|
|
58
68
|
/** The Copilot inference host for a credential or enterprise domain. */
|
|
59
69
|
function gitHubCopilotBaseUrl(options) {
|
|
60
70
|
const apiEndpoint = options?.apiEndpoint;
|
|
@@ -420,6 +430,6 @@ async function getGitHubCopilotApiKey(options = {}) {
|
|
|
420
430
|
return new GitHubCopilotOAuthClient(options).getApiKey();
|
|
421
431
|
}
|
|
422
432
|
//#endregion
|
|
423
|
-
export { createGitHubCopilotDeviceFlow as a, fetchGitHubCopilotUser as c,
|
|
433
|
+
export { createGitHubCopilotDeviceFlow as a, fetchGitHubCopilotUser as c, gitHubApiDomain as d, gitHubCopilotBaseUrl as f, pollGitHubCopilotDeviceToken as g, normalizeGitHubDomain as h, JsonGitHubCopilotAuthStorage as i, filterGitHubCopilotModels as l, gitHubCopilotHeaders as m, GITHUB_COPILOT_PROVIDER_ID as n, enableGitHubCopilotModels as o, gitHubCopilotEntitlementError as p, GitHubCopilotOAuthClient as r, fetchGitHubCopilotModels as s, GITHUB_COPILOT_API_KEY_ENV as t, getGitHubCopilotApiKey as u };
|
|
424
434
|
|
|
425
|
-
//# sourceMappingURL=copilot-
|
|
435
|
+
//# sourceMappingURL=copilot-D9T1jbpu.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copilot-D9T1jbpu.mjs","names":[],"sources":["../../src/oauth/github/copilot.ts"],"sourcesContent":["/**\n * GitHub Copilot OAuth.\n *\n * The device flow yields a plain GitHub OAuth token (`ghu_`/`gho_`), which the\n * Copilot API accepts directly as a Bearer credential — no session-token\n * exchange and no refresh path. `getGitHubCopilotApiKey` resolves a token from\n * `COPILOT_GITHUB_TOKEN` or the codework auth.json store, and nothing else;\n * `GitHubCopilotOAuthClient#login` performs the device flow.\n */\n\nimport {\n\tGITHUB_COPILOT_API_VERSION,\n\tGITHUB_COPILOT_STATIC_HEADERS,\n} from \"../../providers/github-copilot/copilot-headers.ts\";\nimport { pollDeviceCode } from \"../devcode.ts\";\nimport { JsonAuthStorage, isObject, readEnv } from \"../storage.ts\";\n\nexport const GITHUB_COPILOT_API_KEY_ENV = \"COPILOT_GITHUB_TOKEN\";\n\n// The official GitHub Copilot OAuth client id — the same one copilot.vim,\n// copilot.lua, and pi use, so the consent screen shows \"GitHub Copilot\".\nconst CLIENT_ID = \"Iv1.b507a08c87ecfe98\";\nconst SCOPE = \"read:user\";\nconst DEFAULT_DOMAIN = \"github.com\";\nconst DEFAULT_BASE_URL = \"https://api.githubcopilot.com\";\nconst USER_API_VERSION = \"2025-04-01\";\nconst DEFAULT_PROVIDER_ID = \"github-copilot\";\nexport const GITHUB_COPILOT_PROVIDER_ID = DEFAULT_PROVIDER_ID;\nconst DEFAULT_MODELS_RETRY = { maxRetries: 2, maxElapsedMs: 5000 };\n\nexport type GitHubCopilotOAuthCredentials = {\n\t/** GitHub OAuth token (`ghu_`/`gho_`), used directly as the Copilot Bearer credential. */\n\taccess: string;\n\t/** Same token; kept for shape parity with other OAuth credentials. */\n\trefresh: string;\n\t/** Always 0 — GitHub OAuth tokens do not expire; re-login only on persistent 401. */\n\texpires: number;\n\tenterpriseUrl?: string;\n\t/** `endpoints.api` from `/copilot_internal/user` — the plan-specific Copilot API host. */\n\tapiEndpoint?: string;\n\t/** Model ids visible to the account at login time, from `GET {base}/models`. */\n\tavailableModelIds?: string[];\n};\n\nexport type GitHubCopilotDeviceFlow = {\n\tdeviceCode: string;\n\tuserCode: string;\n\tverificationUri: string;\n\tintervalSeconds: number;\n\texpiresInSeconds?: number;\n};\n\nexport type GitHubCopilotUser = {\n\tchatEnabled?: boolean;\n\tcanSignupForLimited?: boolean;\n\tapiEndpoint?: string;\n};\n\nexport type GitHubCopilotRemoteModel = {\n\tid: string;\n\tsupportedEndpoints: string[];\n\tpolicyState?: string;\n\tpickerEnabled: boolean;\n\ttoolCalls: boolean;\n};\n\nexport type GitHubCopilotLoginOptions = {\n\tenterpriseUrl?: string | undefined;\n\t/** Enable `policy.state === \"unconfigured\"` catalog models after login. Off by default — it writes account policy. */\n\tenableModels?: boolean | undefined;\n\tsignal?: AbortSignal | undefined;\n\tfetch?: typeof globalThis.fetch | undefined;\n\tonAuth: (info: { url: string; userCode: string; instructions?: string | undefined }) => void;\n\tonProgress?: ((message: string) => void) | undefined;\n};\n\nexport interface GitHubCopilotAuthStorage {\n\tget(): Promise<GitHubCopilotOAuthCredentials | undefined>;\n\tset(credentials: GitHubCopilotOAuthCredentials): Promise<void>;\n\tclear(): Promise<void>;\n}\n\nexport type JsonGitHubCopilotAuthStorageOptions = {\n\tpath?: string;\n\tproviderId?: string;\n};\n\ntype FetchLike = typeof globalThis.fetch;\n\nfunction fetchWith(options: { fetch?: FetchLike | undefined }): FetchLike {\n\treturn options.fetch ?? globalThis.fetch;\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n\treturn new Promise((resolve, reject) => {\n\t\tif (signal?.aborted) {\n\t\t\treject(new Error(\"GitHub Copilot login cancelled\"));\n\t\t\treturn;\n\t\t}\n\t\tconst timeout = setTimeout(() => {\n\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\tresolve();\n\t\t}, ms);\n\t\tconst onAbort = () => {\n\t\t\tclearTimeout(timeout);\n\t\t\treject(new Error(\"GitHub Copilot login cancelled\"));\n\t\t};\n\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t});\n}\n\nexport function normalizeGitHubDomain(input: string | undefined): string | undefined {\n\tconst trimmed = input?.trim();\n\tif (!trimmed) return undefined;\n\ttry {\n\t\tconst url = trimmed.includes(\"://\") ? new URL(trimmed) : new URL(`https://${trimmed}`);\n\t\treturn url.hostname;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\n/** The `api.` host serving `copilot_internal/*` for a given GitHub domain. */\nexport function gitHubApiDomain(domain: string): string {\n\treturn domain === DEFAULT_DOMAIN ? \"api.github.com\" : `api.${domain}`;\n}\n\n/**\n * The catalog models a login can reach, by the `availableModelIds` it recorded at login.\n * A login that recorded none keeps every model.\n */\nexport function filterGitHubCopilotModels<TModel extends { id: string; api?: { id?: string } }>(\n\tmodels: Readonly<Record<string, TModel>>,\n\tcredentials: Pick<GitHubCopilotOAuthCredentials, \"availableModelIds\"> | undefined,\n): Record<string, TModel> {\n\tconst ids = credentials?.availableModelIds;\n\tif (ids === undefined) return { ...models };\n\tconst available = new Set(ids);\n\t// A settings entry can alias a model; Copilot is asked for its API id.\n\treturn Object.fromEntries(Object.entries(models).filter(([, model]) => available.has(model.api?.id ?? model.id)));\n}\n\n/** The Copilot inference host for a credential or enterprise domain. */\nexport function gitHubCopilotBaseUrl(options?: {\n\tenterpriseUrl?: string | undefined;\n\tapiEndpoint?: string | undefined;\n}): string {\n\tconst apiEndpoint = options?.apiEndpoint;\n\tif (typeof apiEndpoint === \"string\" && apiEndpoint) return apiEndpoint.replace(/\\/+$/, \"\");\n\tconst domain = normalizeGitHubDomain(options?.enterpriseUrl);\n\treturn domain ? `https://copilot-api.${domain}` : DEFAULT_BASE_URL;\n}\n\n/**\n * GitHub reports Copilot access on `/copilot_internal/user`; OAuth itself\n * succeeds for any GitHub account, so this is the only signal that the account\n * can chat.\n */\nexport function gitHubCopilotEntitlementError(user: {\n\tchat_enabled?: boolean | undefined;\n\tcan_signup_for_limited?: boolean | undefined;\n}): string | undefined {\n\tif (user.chat_enabled !== false) return undefined;\n\tif (user.can_signup_for_limited) {\n\t\treturn \"This GitHub account is not signed up for GitHub Copilot. Sign up for Copilot Free at https://github.com/features/copilot/plans and connect again.\";\n\t}\n\treturn \"This GitHub account does not have GitHub Copilot access. It needs an active Copilot subscription or a seat assigned by an organization.\";\n}\n\nexport async function createGitHubCopilotDeviceFlow(\n\toptions: { domain?: string | undefined; fetch?: FetchLike | undefined; signal?: AbortSignal | undefined } = {},\n): Promise<GitHubCopilotDeviceFlow> {\n\tconst domain = options.domain ?? DEFAULT_DOMAIN;\n\tconst response = await fetchWith(options)(`https://${domain}/login/device/code`, {\n\t\tmethod: \"POST\",\n\t\theaders: { Accept: \"application/json\", \"Content-Type\": \"application/json\" },\n\t\tbody: JSON.stringify({ client_id: CLIENT_ID, scope: SCOPE }),\n\t\tsignal: options.signal ?? null,\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub device code request failed (${response.status})`);\n\t}\n\n\tconst data: unknown = await response.json();\n\tif (!isObject(data)) throw new Error(\"Invalid GitHub device code response\");\n\tconst { device_code, user_code, verification_uri, interval, expires_in } = data;\n\tif (\n\t\ttypeof device_code !== \"string\" ||\n\t\ttypeof user_code !== \"string\" ||\n\t\ttypeof verification_uri !== \"string\" ||\n\t\t(interval !== undefined && typeof interval !== \"number\") ||\n\t\t(expires_in !== undefined && typeof expires_in !== \"number\")\n\t) {\n\t\tthrow new Error(\"Invalid GitHub device code response fields\");\n\t}\n\n\t// The verification URI is opened in the user's browser; force it to be a URL\n\t// so `open` cannot be tricked into launching something else.\n\tlet parsed: URL;\n\ttry {\n\t\tparsed = new URL(verification_uri);\n\t} catch {\n\t\tthrow new Error(\"Untrusted verification_uri in GitHub device code response\");\n\t}\n\tif (parsed.protocol !== \"https:\" && parsed.protocol !== \"http:\") {\n\t\tthrow new Error(\"Untrusted verification_uri in GitHub device code response\");\n\t}\n\n\treturn {\n\t\tdeviceCode: device_code,\n\t\tuserCode: user_code,\n\t\tverificationUri: parsed.href,\n\t\tintervalSeconds: typeof interval === \"number\" && interval > 0 ? interval : 5,\n\t\t...(typeof expires_in === \"number\" && { expiresInSeconds: expires_in }),\n\t};\n}\n\nexport async function pollGitHubCopilotDeviceToken(\n\tflow: GitHubCopilotDeviceFlow,\n\toptions: { domain?: string | undefined; fetch?: FetchLike | undefined; signal?: AbortSignal | undefined } = {},\n): Promise<string> {\n\tconst domain = options.domain ?? DEFAULT_DOMAIN;\n\tconst send = fetchWith(options);\n\n\treturn pollDeviceCode<string>({\n\t\tintervalSeconds: flow.intervalSeconds,\n\t\t...(flow.expiresInSeconds !== undefined && { expiresInSeconds: flow.expiresInSeconds }),\n\t\twaitBeforeFirstPoll: true,\n\t\t...(options.signal !== undefined && { signal: options.signal }),\n\t\tpoll: async () => {\n\t\t\tconst response = await send(`https://${domain}/login/oauth/access_token`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: { Accept: \"application/json\", \"Content-Type\": \"application/json\" },\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tclient_id: CLIENT_ID,\n\t\t\t\t\tdevice_code: flow.deviceCode,\n\t\t\t\t\tgrant_type: \"urn:ietf:params:oauth:grant-type:device_code\",\n\t\t\t\t}),\n\t\t\t\tsignal: options.signal ?? null,\n\t\t\t});\n\t\t\tif (!response.ok) {\n\t\t\t\treturn { status: \"failed\", message: `GitHub device token poll failed (${response.status})` };\n\t\t\t}\n\n\t\t\tconst data: unknown = await response.json();\n\t\t\tif (!isObject(data)) return { status: \"failed\", message: \"Invalid GitHub device token response\" };\n\n\t\t\tif (typeof data.access_token === \"string\" && data.access_token) {\n\t\t\t\treturn { status: \"complete\", value: data.access_token };\n\t\t\t}\n\n\t\t\tconst error = typeof data.error === \"string\" ? data.error : undefined;\n\t\t\tif (error === \"authorization_pending\") return { status: \"pending\" };\n\t\t\tif (error === \"slow_down\") {\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"slow_down\",\n\t\t\t\t\t...(typeof data.interval === \"number\" && data.interval > 0 && { intervalSeconds: data.interval }),\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst description = typeof data.error_description === \"string\" ? `: ${data.error_description}` : \"\";\n\t\t\treturn {\n\t\t\t\tstatus: \"failed\",\n\t\t\t\tmessage: `GitHub device authorization failed: ${error ?? \"unknown error\"}${description}`,\n\t\t\t};\n\t\t},\n\t});\n}\n\n/**\n * Look up the account's Copilot entitlement and per-plan API endpoint.\n *\n * A failed or malformed lookup returns `undefined` rather than denying login —\n * only an explicit `chat_enabled: false` answer blocks.\n */\nexport async function fetchGitHubCopilotUser(\n\ttoken: string,\n\toptions: { domain?: string | undefined; fetch?: FetchLike | undefined; signal?: AbortSignal | undefined } = {},\n): Promise<GitHubCopilotUser | undefined> {\n\tconst domain = options.domain ?? DEFAULT_DOMAIN;\n\ttry {\n\t\tconst response = await fetchWith(options)(`https://${gitHubApiDomain(domain)}/copilot_internal/user`, {\n\t\t\theaders: {\n\t\t\t\tAccept: \"application/json\",\n\t\t\t\tAuthorization: `Bearer ${token}`,\n\t\t\t\t\"User-Agent\": GITHUB_COPILOT_STATIC_HEADERS[\"User-Agent\"],\n\t\t\t\t\"X-GitHub-Api-Version\": USER_API_VERSION,\n\t\t\t},\n\t\t\tsignal: options.signal ?? null,\n\t\t});\n\t\tif (!response.ok) return undefined;\n\t\tconst data: unknown = await response.json();\n\t\tif (!isObject(data)) return undefined;\n\t\tconst endpoints = isObject(data.endpoints) ? data.endpoints : undefined;\n\t\treturn {\n\t\t\t...(typeof data.chat_enabled === \"boolean\" && { chatEnabled: data.chat_enabled }),\n\t\t\t...(typeof data.can_signup_for_limited === \"boolean\" && { canSignupForLimited: data.can_signup_for_limited }),\n\t\t\t...(typeof endpoints?.api === \"string\" && endpoints.api && { apiEndpoint: endpoints.api.replace(/\\/+$/, \"\") }),\n\t\t};\n\t} catch (error) {\n\t\tif (options.signal?.aborted) throw error;\n\t\treturn undefined;\n\t}\n}\n\nasync function fetchWithRateLimitRetry(\n\tsend: FetchLike,\n\turl: string,\n\tinit: RequestInit,\n\toptions: { signal?: AbortSignal; maxRetries: number; maxElapsedMs: number },\n): Promise<Response> {\n\tconst deadline = Date.now() + options.maxElapsedMs;\n\tfor (let retry = 0; ; retry++) {\n\t\tconst response = await send(url, { ...init, signal: options.signal ?? null });\n\t\tif (response.status !== 429 || retry >= options.maxRetries) return response;\n\n\t\tconst retryAfter = response.headers.get(\"retry-after\");\n\t\tlet delayMs = 500 * 2 ** retry;\n\t\tif (retryAfter) {\n\t\t\tconst seconds = Number.parseFloat(retryAfter);\n\t\t\tdelayMs = Number.isNaN(seconds) ? Date.parse(retryAfter) - Date.now() : seconds * 1000;\n\t\t\tif (!Number.isFinite(delayMs)) return response;\n\t\t}\n\t\tdelayMs = Math.max(0, delayMs);\n\t\tif (delayMs >= deadline - Date.now()) return response;\n\t\tawait response.body?.cancel();\n\t\tawait sleep(delayMs, options.signal);\n\t}\n}\n\n/**\n * The account-visible Copilot model catalog. `GET {base}/models` is the ground\n * truth for endpoint routing (`supported_endpoints`) and enablement (`policy`,\n * `model_picker_enabled`) — models.dev has no equivalent data.\n */\nexport async function fetchGitHubCopilotModels(\n\ttoken: string,\n\toptions: {\n\t\tbaseURL?: string | undefined;\n\t\tdomain?: string | undefined;\n\t\tfetch?: FetchLike | undefined;\n\t\tsignal?: AbortSignal | undefined;\n\t\tmaxRetries?: number | undefined;\n\t\tmaxElapsedMs?: number | undefined;\n\t} = {},\n): Promise<GitHubCopilotRemoteModel[]> {\n\tconst baseURL = options.baseURL ?? gitHubCopilotBaseUrl({ enterpriseUrl: options.domain });\n\tconst response = await fetchWithRateLimitRetry(\n\t\tfetchWith(options),\n\t\t`${baseURL}/models`,\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAccept: \"application/json\",\n\t\t\t\tAuthorization: `Bearer ${token}`,\n\t\t\t\t...GITHUB_COPILOT_STATIC_HEADERS,\n\t\t\t\t\"X-GitHub-Api-Version\": GITHUB_COPILOT_API_VERSION,\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\t...(options.signal !== undefined && { signal: options.signal }),\n\t\t\tmaxRetries: options.maxRetries ?? DEFAULT_MODELS_RETRY.maxRetries,\n\t\t\tmaxElapsedMs: options.maxElapsedMs ?? DEFAULT_MODELS_RETRY.maxElapsedMs,\n\t\t},\n\t);\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot models request failed (${response.status})`);\n\t}\n\n\tconst data: unknown = await response.json();\n\tconst list = isObject(data) ? data.data : undefined;\n\tif (!Array.isArray(list)) throw new Error(\"Invalid GitHub Copilot models response\");\n\n\treturn list.flatMap((raw) => {\n\t\tif (!isObject(raw) || typeof raw.id !== \"string\") return [];\n\t\tconst capabilities = isObject(raw.capabilities) ? raw.capabilities : undefined;\n\t\tconst supports = isObject(capabilities?.supports) ? capabilities.supports : undefined;\n\t\tconst policy = isObject(raw.policy) ? raw.policy : undefined;\n\t\treturn [\n\t\t\t{\n\t\t\t\tid: raw.id,\n\t\t\t\tsupportedEndpoints: Array.isArray(raw.supported_endpoints)\n\t\t\t\t\t? raw.supported_endpoints.filter((entry): entry is string => typeof entry === \"string\")\n\t\t\t\t\t: [],\n\t\t\t\t...(typeof policy?.state === \"string\" && { policyState: policy.state }),\n\t\t\t\tpickerEnabled: raw.model_picker_enabled === true,\n\t\t\t\ttoolCalls: supports?.tool_calls !== false,\n\t\t\t},\n\t\t];\n\t});\n}\n\n/**\n * Enable models the account has not configured yet (`policy.state ===\n * \"unconfigured\"`). Best effort per model; a rate limit that outlives the\n * retry budget stops the batch.\n */\nexport async function enableGitHubCopilotModels(\n\ttoken: string,\n\tmodelIds: readonly string[],\n\toptions: {\n\t\tbaseURL?: string | undefined;\n\t\tdomain?: string | undefined;\n\t\tfetch?: FetchLike | undefined;\n\t\tsignal?: AbortSignal | undefined;\n\t} = {},\n): Promise<string[]> {\n\tconst baseURL = options.baseURL ?? gitHubCopilotBaseUrl({ enterpriseUrl: options.domain });\n\tconst send = fetchWith(options);\n\tconst enabled: string[] = [];\n\tfor (const modelId of modelIds) {\n\t\ttry {\n\t\t\tconst response = await fetchWithRateLimitRetry(\n\t\t\t\tsend,\n\t\t\t\t`${baseURL}/models/${modelId}/policy`,\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\tAuthorization: `Bearer ${token}`,\n\t\t\t\t\t\t...GITHUB_COPILOT_STATIC_HEADERS,\n\t\t\t\t\t\t\"openai-intent\": \"chat-policy\",\n\t\t\t\t\t\t\"x-interaction-type\": \"chat-policy\",\n\t\t\t\t\t},\n\t\t\t\t\tbody: JSON.stringify({ state: \"enabled\" }),\n\t\t\t\t},\n\t\t\t\t{ ...(options.signal !== undefined && { signal: options.signal }), maxRetries: 2, maxElapsedMs: 5000 },\n\t\t\t);\n\t\t\tif (response.status === 429) break;\n\t\t\tif (response.ok) enabled.push(modelId);\n\t\t} catch (error) {\n\t\t\tif (options.signal?.aborted) throw error;\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn enabled;\n}\n\nfunction isGitHubCopilotCredentials(value: unknown): value is GitHubCopilotOAuthCredentials {\n\tif (!isObject(value)) return false;\n\treturn typeof value.access === \"string\" && typeof value.refresh === \"string\" && typeof value.expires === \"number\";\n}\n\nexport class JsonGitHubCopilotAuthStorage implements GitHubCopilotAuthStorage {\n\tprivate readonly storage: JsonAuthStorage<GitHubCopilotOAuthCredentials>;\n\n\tconstructor(options: JsonGitHubCopilotAuthStorageOptions = {}) {\n\t\tthis.storage = new JsonAuthStorage({\n\t\t\tproviderId: options.providerId ?? DEFAULT_PROVIDER_ID,\n\t\t\tisCredentials: isGitHubCopilotCredentials,\n\t\t\t...(options.path !== undefined && { path: options.path }),\n\t\t});\n\t}\n\n\tget path(): string {\n\t\treturn this.storage.path;\n\t}\n\n\tget providerId(): string {\n\t\treturn this.storage.providerId;\n\t}\n\n\tget(): Promise<GitHubCopilotOAuthCredentials | undefined> {\n\t\treturn this.storage.get();\n\t}\n\n\tset(credentials: GitHubCopilotOAuthCredentials): Promise<void> {\n\t\treturn this.storage.set(credentials);\n\t}\n\n\tclear(): Promise<void> {\n\t\treturn this.storage.clear();\n\t}\n}\n\nexport type GitHubCopilotOAuthClientOptions = {\n\tstorage?: GitHubCopilotAuthStorage;\n};\n\nexport class GitHubCopilotOAuthClient {\n\treadonly storage: GitHubCopilotAuthStorage;\n\n\tconstructor(options: GitHubCopilotOAuthClientOptions = {}) {\n\t\tthis.storage = options.storage ?? new JsonGitHubCopilotAuthStorage();\n\t}\n\n\tasync login(options: GitHubCopilotLoginOptions): Promise<GitHubCopilotOAuthCredentials> {\n\t\tconst enterpriseUrl = options.enterpriseUrl?.trim() || undefined;\n\t\tconst domain = normalizeGitHubDomain(enterpriseUrl) ?? DEFAULT_DOMAIN;\n\t\tif (enterpriseUrl && !normalizeGitHubDomain(enterpriseUrl)) {\n\t\t\tthrow new Error(\"Invalid GitHub Enterprise URL/domain\");\n\t\t}\n\n\t\tconst flow = await createGitHubCopilotDeviceFlow({ domain, fetch: options.fetch, signal: options.signal });\n\t\toptions.onAuth({\n\t\t\turl: flow.verificationUri,\n\t\t\tuserCode: flow.userCode,\n\t\t\tinstructions: \"Complete the GitHub device authorization to finish GitHub Copilot authentication.\",\n\t\t});\n\n\t\tconst access = await pollGitHubCopilotDeviceToken(flow, {\n\t\t\tdomain,\n\t\t\tfetch: options.fetch,\n\t\t\t...(options.signal !== undefined && { signal: options.signal }),\n\t\t});\n\n\t\tconst user = await fetchGitHubCopilotUser(access, { domain, fetch: options.fetch, signal: options.signal });\n\t\tconst denied = user\n\t\t\t? gitHubCopilotEntitlementError({\n\t\t\t\t\tchat_enabled: user.chatEnabled,\n\t\t\t\t\tcan_signup_for_limited: user.canSignupForLimited,\n\t\t\t\t})\n\t\t\t: undefined;\n\t\tif (denied) throw new Error(denied);\n\n\t\tconst apiEndpoint =\n\t\t\tuser?.apiEndpoint ?? gitHubCopilotBaseUrl({ enterpriseUrl: domain === DEFAULT_DOMAIN ? undefined : domain });\n\n\t\toptions.onProgress?.(\"Fetching available Copilot models...\");\n\t\tlet availableModelIds: string[] | undefined;\n\t\tlet unconfigured: string[] = [];\n\t\ttry {\n\t\t\tconst models = await fetchGitHubCopilotModels(access, {\n\t\t\t\tbaseURL: apiEndpoint,\n\t\t\t\tfetch: options.fetch,\n\t\t\t\t...(options.signal !== undefined && { signal: options.signal }),\n\t\t\t});\n\t\t\tconst usable = models.filter((model) => model.toolCalls && model.policyState !== \"disabled\");\n\t\t\tconst pickerIds = usable.filter((model) => model.pickerEnabled).map((model) => model.id);\n\t\t\t// Some Individual accounts report picker=false for every model despite\n\t\t\t// enabled policies; only that endpoint gets the policy fallback.\n\t\t\tconst host = new URL(apiEndpoint).hostname;\n\t\t\tconst policyFallback = pickerIds.length === 0 && host === \"api.individual.githubcopilot.com\";\n\t\t\tavailableModelIds = pickerIds.length\n\t\t\t\t? pickerIds\n\t\t\t\t: policyFallback\n\t\t\t\t\t? usable.filter((model) => model.policyState === \"enabled\").map((model) => model.id)\n\t\t\t\t\t: [];\n\t\t\tunconfigured = usable.filter((model) => model.policyState === \"unconfigured\").map((model) => model.id);\n\t\t} catch (error) {\n\t\t\tif (options.signal?.aborted) throw error;\n\t\t\toptions.onProgress?.(\"Could not fetch the Copilot model catalog; continuing without it.\");\n\t\t}\n\n\t\tif (options.enableModels && unconfigured.length > 0) {\n\t\t\toptions.onProgress?.(\"Enabling Copilot models...\");\n\t\t\tconst enabled = await enableGitHubCopilotModels(access, unconfigured, {\n\t\t\t\tbaseURL: apiEndpoint,\n\t\t\t\tfetch: options.fetch,\n\t\t\t\t...(options.signal !== undefined && { signal: options.signal }),\n\t\t\t});\n\t\t\tavailableModelIds = [...new Set([...(availableModelIds ?? []), ...enabled])];\n\t\t}\n\n\t\tconst credentials: GitHubCopilotOAuthCredentials = {\n\t\t\taccess,\n\t\t\trefresh: access,\n\t\t\texpires: 0,\n\t\t\t...(domain !== DEFAULT_DOMAIN && { enterpriseUrl: domain }),\n\t\t\tapiEndpoint,\n\t\t\t...(availableModelIds !== undefined && availableModelIds.length > 0 && { availableModelIds }),\n\t\t};\n\t\tawait this.storage.set(credentials);\n\t\treturn credentials;\n\t}\n\n\t/** The stored `ghu_` token, returned as-is — it never expires. */\n\tasync getCredentials(): Promise<GitHubCopilotOAuthCredentials | undefined> {\n\t\treturn this.storage.get();\n\t}\n\n\tasync getApiKey(): Promise<string | undefined> {\n\t\treturn (await this.getCredentials())?.access;\n\t}\n\n\tasync getHeaders(): Promise<Record<string, string> | undefined> {\n\t\tconst credentials = await this.getCredentials();\n\t\treturn credentials ? gitHubCopilotHeaders(credentials) : undefined;\n\t}\n\n\tasync logout(): Promise<void> {\n\t\tawait this.storage.clear();\n\t}\n}\n\nexport function gitHubCopilotHeaders(credentials: GitHubCopilotOAuthCredentials): Record<string, string> {\n\treturn {\n\t\tAuthorization: `Bearer ${credentials.access}`,\n\t\t...GITHUB_COPILOT_STATIC_HEADERS,\n\t\t\"X-GitHub-Api-Version\": GITHUB_COPILOT_API_VERSION,\n\t};\n}\n\n/**\n * Resolve the GitHub Copilot API credential: `COPILOT_GITHUB_TOKEN` first, then\n * an explicit `auth --github-copilot` login from codework auth.json.\n *\n * Nothing else is consulted, by design. `GITHUB_TOKEN` and `GH_TOKEN` are\n * usually set for git or `gh` and carry no Copilot access — in GitHub Actions\n * `GITHUB_TOKEN` is a workflow token — so reading them turns a working login\n * into an unexplained 401. Credentials another application stored for itself\n * (an editor's Copilot sign-in) are not consulted either: a token codework was\n * never given should not silently become the identity its runs bill and act as.\n *\n * Returns undefined when nothing is available; the device flow only runs via\n * `GitHubCopilotOAuthClient#login`.\n */\nexport async function getGitHubCopilotApiKey(\n\toptions: GitHubCopilotOAuthClientOptions = {},\n): Promise<string | undefined> {\n\tconst dedicated = readEnv(GITHUB_COPILOT_API_KEY_ENV);\n\tif (dedicated) return dedicated;\n\n\treturn new GitHubCopilotOAuthClient(options).getApiKey();\n}\n"],"mappings":";;;;;;;;;;;;AAiBA,MAAa,6BAA6B;AAI1C,MAAM,YAAY;AAClB,MAAM,QAAQ;AACd,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;AACzB,MAAM,mBAAmB;AACzB,MAAM,sBAAsB;AAC5B,MAAa,6BAA6B;AAC1C,MAAM,uBAAuB;CAAE,YAAY;CAAG,cAAc;AAAK;AA6DjE,SAAS,UAAU,SAAuD;CACzE,OAAO,QAAQ,SA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AK,EAAE,kBAAkB;EAC5F;EACA,MAAM,KAAK,QAAQ,IAAI,WAAW;EAClC,OAAO;CACR;;CAGA,MAAM,iBAAqE;EAC1E,OAAO,KAAK,QAAQ,IAAI;CACzB;CAEA,MAAM,YAAyC;EAC9C,QAAQ,MAAM,KAAK,eAAe,EAAA,EAAI;CACvC;CAEA,MAAM,aAA0D;EAC/D,MAAM,cAAc,MAAM,KAAK,eAAe;EAC9C,OAAO,cAAc,qBAAqB,WAAW,IAAI,KAAA;CAC1D;CAEA,MAAM,SAAwB;EAC7B,MAAM,KAAK,QAAQ,MAAM;CAC1B;AACD;AAEA,SAAgB,qBAAqB,aAAoE;CACxG,OAAO;EACN,eAAe,UAAU,YAAY;EACrC,GAAG;EACH,wBAAwB;CACzB;AACD;;;;;;;;;;;;;;;AAgBA,eAAsB,uBACrB,UAA2C,CAAC,GACd;CAC9B,MAAM,YAAY,QAAQ,0BAA0B;CACpD,IAAI,WAAW,OAAO;CAEtB,OAAO,IAAI,yBAAyB,OAAO,CAAC,CAAC,UAAU;AACxD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { h as isAnthropicAdaptiveThinkingModel } from "./model-
|
|
1
|
+
import { h as isAnthropicAdaptiveThinkingModel } from "./model-BZZZAySN.mjs";
|
|
2
2
|
//#region src/providers/github-copilot/copilot-headers.ts
|
|
3
3
|
/**
|
|
4
4
|
* Copilot request identity.
|
|
@@ -101,4 +101,4 @@ function applyCopilotHeaders(headers, options) {
|
|
|
101
101
|
//#endregion
|
|
102
102
|
export { copilotRequestMetadata as i, GITHUB_COPILOT_STATIC_HEADERS as n, applyCopilotHeaders as r, GITHUB_COPILOT_API_VERSION as t };
|
|
103
103
|
|
|
104
|
-
//# sourceMappingURL=copilot-headers-
|
|
104
|
+
//# sourceMappingURL=copilot-headers-I-AOoDed.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copilot-headers-
|
|
1
|
+
{"version":3,"file":"copilot-headers-I-AOoDed.mjs","names":[],"sources":["../../src/providers/github-copilot/copilot-headers.ts"],"sourcesContent":["/**\n * Copilot request identity.\n *\n * Every inference request must look like it came from the Copilot Chat client:\n * the static headers identify that client, while `x-initiator` and\n * `Copilot-Vision-Request` describe the serialized body.\n */\n\nimport { isAnthropicAdaptiveThinkingModel } from \"../../model/model.ts\";\n\nexport const GITHUB_COPILOT_API_VERSION = \"2026-08-01\";\n\nexport const GITHUB_COPILOT_STATIC_HEADERS = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n} as const;\n\n/** `X-Interaction-Type` is `conversation-{type}`; only `agent` is wired today. */\nexport type GitHubCopilotInteractionType = \"agent\" | \"subagent\" | \"background\" | \"compaction\";\n\n// Copilot rejects these betas on adaptive-thinking Claude models.\nconst ADAPTIVE_UNSUPPORTED_BETAS = new Set([\n\t\"interleaved-thinking-2025-05-14\",\n\t\"fine-grained-tool-streaming-2025-05-14\",\n]);\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\ntype RequestMetadata = { agent: boolean; vision: boolean };\n\nfunction responsesMetadata(input: unknown): RequestMetadata {\n\tconst list = Array.isArray(input) ? input : [];\n\tconst last = list[list.length - 1];\n\tlet vision = false;\n\tfor (const item of list) {\n\t\tif (!isRecord(item) || !Array.isArray(item.content)) continue;\n\t\tif (item.content.some((part) => isRecord(part) && part.type === \"input_image\")) vision = true;\n\t}\n\treturn { agent: isRecord(last) && last.role !== \"user\", vision };\n}\n\nfunction completionsMetadata(messages: unknown): RequestMetadata {\n\tconst list = Array.isArray(messages) ? messages : [];\n\tconst last = list[list.length - 1];\n\tlet vision = false;\n\tfor (const message of list) {\n\t\tif (!isRecord(message) || !Array.isArray(message.content)) continue;\n\t\tif (message.content.some((part) => isRecord(part) && part.type === \"image_url\")) vision = true;\n\t}\n\treturn { agent: isRecord(last) && last.role !== \"user\", vision };\n}\n\nfunction messagesMetadata(messages: unknown): RequestMetadata {\n\tconst list = Array.isArray(messages) ? messages : [];\n\tconst last = list[list.length - 1];\n\tlet vision = false;\n\tfor (const message of list) {\n\t\tif (!isRecord(message) || !Array.isArray(message.content)) continue;\n\t\tfor (const part of message.content) {\n\t\t\tif (!isRecord(part)) continue;\n\t\t\tif (part.type === \"image\") vision = true;\n\t\t\tif (\n\t\t\t\tpart.type === \"tool_result\" &&\n\t\t\t\tArray.isArray(part.content) &&\n\t\t\t\tpart.content.some((inner) => isRecord(inner) && inner.type === \"image\")\n\t\t\t) {\n\t\t\t\tvision = true;\n\t\t\t}\n\t\t}\n\t}\n\n\tconst content = isRecord(last) && Array.isArray(last.content) ? last.content : [];\n\t// A trailing user turn that only carries tool_result parts is still an agent\n\t// step — it answers a tool call, not a human prompt.\n\tconst agent =\n\t\t!isRecord(last) || last.role !== \"user\" || !content.some((part) => isRecord(part) && part.type !== \"tool_result\");\n\treturn { agent, vision };\n}\n\n/** Derive `x-initiator` and `Copilot-Vision-Request` inputs from the serialized body. */\nexport function copilotRequestMetadata(url: string, body: unknown): RequestMetadata {\n\tif (!isRecord(body)) return { agent: false, vision: false };\n\tif (url.includes(\"/responses\")) return responsesMetadata(body.input);\n\tif (Array.isArray(body.messages)) {\n\t\treturn url.includes(\"/completions\") ? completionsMetadata(body.messages) : messagesMetadata(body.messages);\n\t}\n\treturn { agent: false, vision: false };\n}\n\nexport function applyCopilotHeaders(\n\theaders: Headers,\n\toptions: {\n\t\ttoken: string;\n\t\turl: string;\n\t\tbody: unknown;\n\t\tsessionId?: string;\n\t\tinteractionType?: GitHubCopilotInteractionType;\n\t},\n): void {\n\t// Whatever the adapters configured (API key, x-api-key) is replaced by the\n\t// resolved Copilot token; OAuth `ghu_`/`gho_` tokens are Bearer credentials.\n\theaders.delete(\"authorization\");\n\theaders.delete(\"x-api-key\");\n\theaders.set(\"Authorization\", `Bearer ${options.token}`);\n\n\tfor (const [key, value] of Object.entries(GITHUB_COPILOT_STATIC_HEADERS)) {\n\t\theaders.set(key, value);\n\t}\n\theaders.set(\"Openai-Intent\", \"conversation-edits\");\n\theaders.set(\"X-GitHub-Api-Version\", GITHUB_COPILOT_API_VERSION);\n\n\tconst interactionType = `conversation-${options.interactionType ?? \"agent\"}`;\n\theaders.set(\"X-Interaction-Type\", interactionType);\n\tif (options.sessionId) headers.set(\"X-Interaction-Id\", options.sessionId);\n\n\t// `x-initiator` can only escalate to `agent`, never back to `user` — a\n\t// non-agent interaction or an agent-shaped body overrides a declared `user`.\n\tif (interactionType !== \"conversation-agent\") {\n\t\theaders.set(\"x-initiator\", \"agent\");\n\t}\n\tconst metadata = copilotRequestMetadata(options.url, options.body);\n\tif (metadata.agent) {\n\t\theaders.set(\"x-initiator\", \"agent\");\n\t} else if (!headers.has(\"x-initiator\")) {\n\t\theaders.set(\"x-initiator\", \"user\");\n\t}\n\tif (metadata.vision) {\n\t\theaders.set(\"Copilot-Vision-Request\", \"true\");\n\t}\n\n\tconst model = isRecord(options.body) && typeof options.body.model === \"string\" ? options.body.model : undefined;\n\tconst beta = headers.get(\"anthropic-beta\");\n\tif (model && beta && isAnthropicAdaptiveThinkingModel(model)) {\n\t\tconst kept = beta\n\t\t\t.split(\",\")\n\t\t\t.map((entry) => entry.trim())\n\t\t\t.filter((entry) => entry && !ADAPTIVE_UNSUPPORTED_BETAS.has(entry));\n\t\tif (kept.length > 0) {\n\t\t\theaders.set(\"anthropic-beta\", kept.join(\",\"));\n\t\t} else {\n\t\t\theaders.delete(\"anthropic-beta\");\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;AAUA,MAAa,6BAA6B;AAE1C,MAAa,gCAAgC;CAC5C,cAAc;CACd,kBAAkB;CAClB,yBAAyB;CACzB,0BAA0B;AAC3B;AAMA,MAAM,6CAA6B,IAAI,IAAI,CAC1C,mCACA,wCACD,CAAC;AA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AQ,IAAI,eAAe,OAAO;CAEnC,MAAM,WAAW,uBAAuB,QAAQ,KAAK,QAAQ,IAAI;CACjE,IAAI,SAAS,OACZ,QAAQ,IAAI,eAAe,OAAO;MAC5B,IAAI,CAAC,QAAQ,IAAI,aAAa,GACpC,QAAQ,IAAI,eAAe,MAAM;CAElC,IAAI,SAAS,QACZ,QAAQ,IAAI,0BAA0B,MAAM;CAG7C,MAAM,QAAQ,SAAS,QAAQ,IAAI,KAAK,OAAO,QAAQ,KAAK,UAAU,WAAW,QAAQ,KAAK,QAAQ,KAAA;CACtG,MAAM,OAAO,QAAQ,IAAI,gBAAgB;CACzC,IAAI,SAAS,QAAQ,iCAAiC,KAAK,GAAG;EAC7D,MAAM,OAAO,KACX,MAAM,GAAG,CAAC,CACV,KAAK,UAAU,MAAM,KAAK,CAAC,CAAC,CAC5B,QAAQ,UAAU,SAAS,CAAC,2BAA2B,IAAI,KAAK,CAAC;EACnE,IAAI,KAAK,SAAS,GACjB,QAAQ,IAAI,kBAAkB,KAAK,KAAK,GAAG,CAAC;OAE5C,QAAQ,OAAO,gBAAgB;CAEjC;AACD"}
|
|
@@ -233,12 +233,15 @@ type Failure = Static<typeof FailureSchema>;
|
|
|
233
233
|
/** Lightweight guard for failure data crossing package or persistence boundaries. */
|
|
234
234
|
declare function isFailure(value: unknown): value is Failure;
|
|
235
235
|
declare namespace failure_d_exports {
|
|
236
|
-
export { Authentication, Authorization, Configuration, ContentPolicy, Failure, FailureSchema, InvalidRequest, InvalidResponse, ModelUnavailable, Quota, RateLimit, Timeout, Transport, Unavailable, Unknown, fromMessage, isFailure, normalize };
|
|
236
|
+
export { Authentication, Authorization, Configuration, ContentPolicy, Failure, FailureSchema, InvalidRequest, InvalidResponse, ModelUnavailable, Quota, RateLimit, Timeout, Transport, Unavailable, Unknown, fromMessage, isFailure, isStrictToolLimitError, normalize, strictToolLimitMessage };
|
|
237
237
|
}
|
|
238
238
|
/** Normalize AI SDK and provider exceptions without retaining request bodies, response bodies, or headers. */
|
|
239
239
|
declare function normalize(error: unknown): Failure;
|
|
240
|
+
/** The provider's own message when it rejected the request for exceeding its strict-tool limits. */
|
|
241
|
+
declare function strictToolLimitMessage(error: unknown): string | undefined;
|
|
242
|
+
declare function isStrictToolLimitError(error: unknown): boolean;
|
|
240
243
|
/** Best-effort compatibility for terminal messages written before structured failures existed. */
|
|
241
244
|
declare function fromMessage(message: string): Failure;
|
|
242
245
|
//#endregion
|
|
243
|
-
export {
|
|
244
|
-
//# sourceMappingURL=failure-
|
|
246
|
+
export { Timeout as _, strictToolLimitMessage as a, Unknown as b, Configuration as c, FailureSchema as d, InvalidRequest as f, RateLimit as g, Quota as h, normalize as i, ContentPolicy as l, ModelUnavailable as m, fromMessage as n, Authentication as o, InvalidResponse as p, isStrictToolLimitError as r, Authorization as s, failure_d_exports as t, Failure as u, Transport as v, isFailure as x, Unavailable as y };
|
|
247
|
+
//# sourceMappingURL=failure-CCCRfhCf.d.mts.map
|
|
@@ -20,7 +20,9 @@ var failure_exports = /* @__PURE__ */ __exportAll({
|
|
|
20
20
|
Unknown: () => Unknown,
|
|
21
21
|
fromMessage: () => fromMessage,
|
|
22
22
|
isFailure: () => isFailure,
|
|
23
|
-
|
|
23
|
+
isStrictToolLimitError: () => isStrictToolLimitError,
|
|
24
|
+
normalize: () => normalize,
|
|
25
|
+
strictToolLimitMessage: () => strictToolLimitMessage
|
|
24
26
|
});
|
|
25
27
|
const record = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
26
28
|
const nonEmptyString = (value) => typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
|
|
@@ -58,9 +60,17 @@ const retryAfter = (headers) => {
|
|
|
58
60
|
const date = Date.parse(value);
|
|
59
61
|
if (Number.isFinite(date)) return Math.max(0, date - Date.now());
|
|
60
62
|
};
|
|
63
|
+
const parseJson = (text) => {
|
|
64
|
+
if (text === void 0) return void 0;
|
|
65
|
+
try {
|
|
66
|
+
return JSON.parse(text);
|
|
67
|
+
} catch {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
61
71
|
const apiMetadata = (error) => {
|
|
62
|
-
const
|
|
63
|
-
const
|
|
72
|
+
const data = record(error.data) ?? record(parseJson(error.responseBody));
|
|
73
|
+
const code = errorCode(data);
|
|
64
74
|
const nested = record(data?.error);
|
|
65
75
|
const structuredMessage = nonEmptyString(data?.message) ?? nonEmptyString(nested?.message);
|
|
66
76
|
const message = normalizeMessage(structuredMessage ?? (error.responseBody !== void 0 && error.message.includes(error.responseBody) ? `provider request failed${error.statusCode === void 0 ? "" : ` with http ${error.statusCode}`}` : errorMessage(error)));
|
|
@@ -221,6 +231,28 @@ function normalize(error) {
|
|
|
221
231
|
retryable: false
|
|
222
232
|
};
|
|
223
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Claude rejects a request whose strict tools exceed its limits with one of these messages. Observed limits:
|
|
236
|
+
* 20 strict tools, and 16 optional or union-typed parameters across all strict tools.
|
|
237
|
+
*/
|
|
238
|
+
const STRICT_TOOL_LIMIT_MESSAGES = [
|
|
239
|
+
"The compiled grammar is too large",
|
|
240
|
+
"Too many strict tools (",
|
|
241
|
+
"Schemas contains too many parameters with union types ("
|
|
242
|
+
];
|
|
243
|
+
/** The provider's own message when it rejected the request for exceeding its strict-tool limits. */
|
|
244
|
+
function strictToolLimitMessage(error) {
|
|
245
|
+
if (RetryError.isInstance(error)) return strictToolLimitMessage(error.lastError);
|
|
246
|
+
if (!APICallError.isInstance(error) || error.statusCode !== 400) return;
|
|
247
|
+
const data = record(error.data) ?? record(parseJson(error.responseBody));
|
|
248
|
+
const nested = record(data?.error);
|
|
249
|
+
if (nested?.type !== "invalid_request_error") return;
|
|
250
|
+
const message = nonEmptyString(nested.message) ?? error.message;
|
|
251
|
+
return STRICT_TOOL_LIMIT_MESSAGES.some((limit) => message.includes(limit)) ? message : void 0;
|
|
252
|
+
}
|
|
253
|
+
function isStrictToolLimitError(error) {
|
|
254
|
+
return strictToolLimitMessage(error) !== void 0;
|
|
255
|
+
}
|
|
224
256
|
/** Best-effort compatibility for terminal messages written before structured failures existed. */
|
|
225
257
|
function fromMessage(message) {
|
|
226
258
|
const normalized = normalizeMessage(message);
|
|
@@ -243,6 +275,6 @@ function fromMessage(message) {
|
|
|
243
275
|
};
|
|
244
276
|
}
|
|
245
277
|
//#endregion
|
|
246
|
-
export { fromMessage as n,
|
|
278
|
+
export { strictToolLimitMessage as a, normalize as i, fromMessage as n, isStrictToolLimitError as r, failure_exports as t };
|
|
247
279
|
|
|
248
|
-
//# sourceMappingURL=failure-
|
|
280
|
+
//# sourceMappingURL=failure-D0qEqOsO.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failure-D0qEqOsO.mjs","names":[],"sources":["../../src/llm/failure.ts"],"sourcesContent":["import {\n\tAPICallError,\n\tEmptyResponseBodyError,\n\tInvalidArgumentError,\n\tInvalidPromptError,\n\tInvalidResponseDataError,\n\tJSONParseError,\n\tLoadAPIKeyError,\n\tLoadSettingError,\n\tNoContentGeneratedError,\n\tNoSuchModelError,\n\tTypeValidationError,\n\tUnsupportedFunctionalityError,\n} from \"@ai-sdk/provider\";\nimport { RetryError, StreamProviderError } from \"ai\";\nimport type { Failure } from \"./failure/schema.ts\";\n\nexport * from \"./failure/schema.ts\";\n\ntype Metadata = Omit<Failure, \"_tag\" | \"reason\">;\n\nconst record = (value: unknown): Record<string, unknown> | undefined =>\n\ttypeof value === \"object\" && value !== null && !Array.isArray(value)\n\t\t? (value as Record<string, unknown>)\n\t\t: undefined;\n\nconst nonEmptyString = (value: unknown): string | undefined =>\n\ttypeof value === \"string\" && value.trim().length > 0 ? value.trim() : undefined;\n\nconst normalizeMessage = (message: string): string => {\n\tconst trimmed = message.trim().replace(/[.!?]+$/u, \"\");\n\treturn trimmed.replace(/^[A-Za-z]+/u, (word) => word.toLowerCase());\n};\n\nconst errorMessage = (error: unknown): string => {\n\tconst value = record(error);\n\tconst data = record(value?.data);\n\tif (value?.name === \"ProtocolAuthError\") {\n\t\treturn normalizeMessage(nonEmptyString(data?.message) ?? \"provider credentials are missing\");\n\t}\n\tif (error instanceof Error) return normalizeMessage(nonEmptyString(error.message) ?? error.name);\n\treturn normalizeMessage(nonEmptyString(error) ?? \"the provider request failed for an unknown reason\");\n};\n\nconst isProtocolAuthError = (error: unknown): boolean => {\n\tconst value = record(error);\n\treturn value?.name === \"ProtocolAuthError\";\n};\n\nconst errorCode = (data: unknown): string | undefined => {\n\tconst top = record(data);\n\tconst nested = record(top?.error);\n\treturn (\n\t\tnonEmptyString(top?.code) ??\n\t\tnonEmptyString(top?.type) ??\n\t\tnonEmptyString(nested?.code) ??\n\t\tnonEmptyString(nested?.type)\n\t);\n};\n\nconst header = (headers: Record<string, string> | undefined, name: string): string | undefined => {\n\tif (headers === undefined) return undefined;\n\tconst expected = name.toLowerCase();\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (key.toLowerCase() === expected) return nonEmptyString(value);\n\t}\n};\n\n/** The server's requested wait, in the three forms providers send: `retry-after-ms`, seconds, or an HTTP date. */\nconst retryAfter = (headers: Record<string, string> | undefined): number | undefined => {\n\tconst millis = Number(header(headers, \"retry-after-ms\"));\n\tif (Number.isFinite(millis) && millis >= 0) return millis;\n\tconst value = header(headers, \"retry-after\");\n\tif (value === undefined) return undefined;\n\tconst seconds = Number(value);\n\tif (Number.isFinite(seconds) && seconds >= 0) return seconds * 1_000;\n\tconst date = Date.parse(value);\n\tif (Number.isFinite(date)) return Math.max(0, date - Date.now());\n};\n\nconst parseJson = (text: string | undefined): unknown => {\n\tif (text === undefined) return undefined;\n\ttry {\n\t\treturn JSON.parse(text);\n\t} catch {\n\t\treturn undefined;\n\t}\n};\n\nconst apiMetadata = (error: APICallError): Metadata => {\n\t// An adapter fronting another vendor's error shape (Copilot's OpenAI-style errors on the\n\t// Anthropic route) leaves `data` unset; the raw body still carries the message and code.\n\tconst data = record(error.data) ?? record(parseJson(error.responseBody));\n\tconst code = errorCode(data);\n\tconst nested = record(data?.error);\n\tconst structuredMessage = nonEmptyString(data?.message) ?? nonEmptyString(nested?.message);\n\tconst message = normalizeMessage(\n\t\tstructuredMessage ??\n\t\t\t(error.responseBody !== undefined && error.message.includes(error.responseBody)\n\t\t\t\t? `provider request failed${error.statusCode === undefined ? \"\" : ` with http ${error.statusCode}`}`\n\t\t\t\t: errorMessage(error)),\n\t);\n\tconst requestId =\n\t\theader(error.responseHeaders, \"x-request-id\") ??\n\t\theader(error.responseHeaders, \"x-oai-request-id\") ??\n\t\theader(error.responseHeaders, \"request-id\") ??\n\t\theader(error.responseHeaders, \"cf-ray\");\n\tconst retryAfterMs = retryAfter(error.responseHeaders);\n\treturn {\n\t\tmessage,\n\t\tretryable: error.isRetryable,\n\t\t...(error.statusCode === undefined ? {} : { status: error.statusCode }),\n\t\t...(code === undefined ? {} : { code }),\n\t\t...(requestId === undefined ? {} : { requestId }),\n\t\t...(retryAfterMs === undefined ? {} : { retryAfterMs }),\n\t};\n};\n\nconst includes = (value: string | undefined, terms: ReadonlyArray<string>): boolean =>\n\tvalue !== undefined && terms.some((term) => value.toLowerCase().includes(term));\n\n/**\n * Account limits that reset in hours or need a payment, not a backoff. Codes cover OpenAI's\n * `insufficient_quota`, Codex's `subscription_sharing_usage_limit_exceeded` and OpenCode's\n * `GoUsageLimitError`/`FreeUsageLimitError`; the prose covers providers that only say it in words,\n * except when they describe a per-minute window, which is throttling however it is phrased.\n */\nconst QUOTA_CODE_TERMS = [\"quota\", \"billing\", \"credit\", \"usage_limit\", \"usagelimit\"];\nconst QUOTA_MESSAGE =\n\t/usage limit|quota exceeded|insufficient (?:quota|credits?)|credit balance|billing|out of budget|available balance/i;\nconst RATE_LIMIT_CODE_TERMS = [\"rate_limit\", \"ratelimit\"];\nconst RATE_WINDOW_MESSAGE = /per[- ](?:second|minute)|\\b[rt]pm\\b/i;\n\nconst isQuota = (details: Metadata): boolean => {\n\tif (includes(details.code, RATE_LIMIT_CODE_TERMS)) return false;\n\tif (includes(details.code, QUOTA_CODE_TERMS)) return true;\n\treturn QUOTA_MESSAGE.test(details.message) && !RATE_WINDOW_MESSAGE.test(details.message);\n};\n\n/** Classify by provider error code, then HTTP status; shared by request and in-stream failures. */\nconst classify = (statusCode: number | undefined, details: Metadata): Failure => {\n\tif (isQuota(details)) return { _tag: \"Quota\", ...details };\n\tif (\n\t\tincludes(details.code, [\n\t\t\t\"content_filter\",\n\t\t\t\"content_policy\",\n\t\t\t\"safety\",\n\t\t\t\"cyber_policy\",\n\t\t\t\"bio_policy\",\n\t\t\t\"misalignment_policy\",\n\t\t])\n\t) {\n\t\treturn { _tag: \"ContentPolicy\", ...details };\n\t}\n\tif (statusCode === 401) return { _tag: \"Authentication\", reason: \"invalid\", ...details };\n\tif (statusCode === 403) return { _tag: \"Authorization\", ...details };\n\tif (statusCode === 404) return { _tag: \"ModelUnavailable\", ...details };\n\tif (statusCode === 408) return { _tag: \"Timeout\", ...details };\n\tif (statusCode === 429) return { _tag: \"RateLimit\", ...details };\n\tif (statusCode !== undefined && statusCode >= 500) return { _tag: \"Unavailable\", ...details };\n\tif (statusCode === undefined) return { _tag: \"Transport\", ...details };\n\tif (statusCode >= 400 && statusCode < 500) return { _tag: \"InvalidRequest\", ...details };\n\treturn { _tag: \"Unknown\", ...details };\n};\n\n/**\n * A failure the provider reported after the stream started, e.g. OpenAI's\n * `context_length_exceeded` event, which it sends instead of an HTTP 400 at random.\n */\nconst fromStreamProvider = (error: StreamProviderError): Failure => {\n\t// Like apiMetadata: providers put the discriminator in `code`, or only in `type`.\n\tconst code = error.code === undefined ? error.type : String(error.code);\n\treturn classify(error.statusCode, {\n\t\tmessage: normalizeMessage(errorMessage(error)),\n\t\tretryable: error.isRetryable,\n\t\t...(error.statusCode === undefined ? {} : { status: error.statusCode }),\n\t\t...(code === undefined ? {} : { code }),\n\t});\n};\n\nconst resemblesTimeout = (error: unknown): boolean => {\n\tconst value = error instanceof Error ? `${error.name} ${error.message}` : String(error);\n\treturn /timeout|timed out/i.test(value);\n};\n\nconst resemblesTransportFailure = (error: unknown): boolean => {\n\tif (error instanceof TypeError) return true;\n\tconst value = error instanceof Error ? `${error.name} ${error.message}` : String(error);\n\treturn /fetch|network|socket|connection|econn|dns/i.test(value);\n};\n\n/** Normalize AI SDK and provider exceptions without retaining request bodies, response bodies, or headers. */\nexport function normalize(error: unknown): Failure {\n\tif (RetryError.isInstance(error) && error.lastError !== undefined && error.lastError !== error) {\n\t\treturn normalize(error.lastError);\n\t}\n\tif (LoadAPIKeyError.isInstance(error) || isProtocolAuthError(error)) {\n\t\treturn { _tag: \"Authentication\", reason: \"missing\", message: errorMessage(error), retryable: false };\n\t}\n\tif (LoadSettingError.isInstance(error)) {\n\t\treturn { _tag: \"Configuration\", message: errorMessage(error), retryable: false };\n\t}\n\tif (NoSuchModelError.isInstance(error)) {\n\t\treturn { _tag: \"ModelUnavailable\", message: errorMessage(error), retryable: false };\n\t}\n\tif (APICallError.isInstance(error)) return classify(error.statusCode, apiMetadata(error));\n\tif (StreamProviderError.isInstance(error)) return fromStreamProvider(error);\n\tif (\n\t\tInvalidPromptError.isInstance(error) ||\n\t\tInvalidArgumentError.isInstance(error) ||\n\t\tUnsupportedFunctionalityError.isInstance(error)\n\t) {\n\t\treturn { _tag: \"InvalidRequest\", message: errorMessage(error), retryable: false };\n\t}\n\tif (\n\t\tInvalidResponseDataError.isInstance(error) ||\n\t\tJSONParseError.isInstance(error) ||\n\t\tEmptyResponseBodyError.isInstance(error) ||\n\t\tNoContentGeneratedError.isInstance(error) ||\n\t\tTypeValidationError.isInstance(error)\n\t) {\n\t\treturn { _tag: \"InvalidResponse\", message: errorMessage(error), retryable: false };\n\t}\n\tif (resemblesTimeout(error)) return { _tag: \"Timeout\", message: errorMessage(error), retryable: true };\n\tif (resemblesTransportFailure(error)) {\n\t\treturn { _tag: \"Transport\", message: errorMessage(error), retryable: true };\n\t}\n\treturn { _tag: \"Unknown\", message: errorMessage(error), retryable: false };\n}\n\n/**\n * Claude rejects a request whose strict tools exceed its limits with one of these messages. Observed limits:\n * 20 strict tools, and 16 optional or union-typed parameters across all strict tools.\n */\nconst STRICT_TOOL_LIMIT_MESSAGES = [\n\t\"The compiled grammar is too large\",\n\t\"Too many strict tools (\",\n\t\"Schemas contains too many parameters with union types (\",\n];\n\n/** The provider's own message when it rejected the request for exceeding its strict-tool limits. */\nexport function strictToolLimitMessage(error: unknown): string | undefined {\n\tif (RetryError.isInstance(error)) return strictToolLimitMessage(error.lastError);\n\tif (!APICallError.isInstance(error) || error.statusCode !== 400) return;\n\tconst data = record(error.data) ?? record(parseJson(error.responseBody));\n\t// Anthropic's body is `{ type: \"error\", error: { type, message } }`, so the top-level type is not the code.\n\tconst nested = record(data?.error);\n\tif (nested?.type !== \"invalid_request_error\") return;\n\tconst message = nonEmptyString(nested.message) ?? error.message;\n\treturn STRICT_TOOL_LIMIT_MESSAGES.some((limit) => message.includes(limit)) ? message : undefined;\n}\n\nexport function isStrictToolLimitError(error: unknown): boolean {\n\treturn strictToolLimitMessage(error) !== undefined;\n}\n\n/** Best-effort compatibility for terminal messages written before structured failures existed. */\nexport function fromMessage(message: string): Failure {\n\tconst normalized = normalizeMessage(message);\n\tif (/api key/i.test(normalized) && /missing|required|not set/i.test(normalized)) {\n\t\treturn { _tag: \"Authentication\", reason: \"missing\", message: normalized, retryable: false };\n\t}\n\tif (/api key|credential|token/i.test(normalized) && /invalid|expired|unauthorized/i.test(normalized)) {\n\t\treturn {\n\t\t\t_tag: \"Authentication\",\n\t\t\treason: /expired/i.test(normalized) ? \"expired\" : \"invalid\",\n\t\t\tmessage: normalized,\n\t\t\tretryable: false,\n\t\t};\n\t}\n\treturn { _tag: \"Unknown\", message: normalized, retryable: false };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,MAAM,UAAU,UACf,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK,IAC/D,QACD,KAAA;AAEJ,MAAM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B;CAC/D,OAAO,uBAAuB,KAAK,MAAM,KAAA;AAC1C;;AAGA,SAAgB,YAAY,SAA0B;CACrD,MAAM,aAAa,iBAAiB,OAAO;CAC3C,IAAI,WAAW,KAAK,UAAU,KAAK,4BAA4B,KAAK,UAAU,GAC7E,OAAO;EAAE,MAAM;EAAkB,QAAQ;EAAW,SAAS;EAAY,WAAW;CAAM;CAE3F,IAAI,4BAA4B,KAAK,UAAU,KAAK,gCAAgC,KAAK,UAAU,GAClG,OAAO;EACN,MAAM;EACN,QAAQ,WAAW,KAAK,UAAU,IAAI,YAAY;EAClD,SAAS;EACT,WAAW;CACZ;CAED,OAAO;EAAE,MAAM;EAAW,SAAS;EAAY,WAAW;CAAM;AACjE"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as ResponsesLanguageModel } from "./model-av44O6GQ.mjs";
|
|
2
|
+
import { r as applyCopilotHeaders } from "./copilot-headers-I-AOoDed.mjs";
|
|
2
3
|
import { LoadAPIKeyError, NoSuchModelError } from "@ai-sdk/provider";
|
|
3
4
|
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
4
|
-
import { createOpenAI } from "@ai-sdk/openai";
|
|
5
5
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
6
6
|
//#region src/providers/github-copilot/copilot-fetch.ts
|
|
7
7
|
const API_KEY_ENV = "COPILOT_GITHUB_TOKEN";
|
|
@@ -52,15 +52,24 @@ function createCopilotFetch(options = {}) {
|
|
|
52
52
|
*
|
|
53
53
|
* Copilot exposes three inference protocols on one host — OpenAI Chat
|
|
54
54
|
* Completions (`/chat/completions`), OpenAI Responses (`/responses`), and
|
|
55
|
-
* Anthropic Messages (`/v1/messages`) — so this provider
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
55
|
+
* Anthropic Messages (`/v1/messages`) — so this provider routes each model
|
|
56
|
+
* through `api.method`. Chat and Messages use the official AI SDK adapters;
|
|
57
|
+
* Responses uses aikit's own Responses model, which declares tools added
|
|
58
|
+
* mid-conversation in place. Auth and Copilot request headers are applied inside
|
|
59
|
+
* the shared fetch wrapper, which is why the adapters receive a placeholder key:
|
|
60
|
+
* their headers never reach the wire.
|
|
60
61
|
*/
|
|
61
62
|
const GITHUB_COPILOT_DEFAULT_BASE_URL = "https://api.githubcopilot.com";
|
|
62
63
|
const GITHUB_COPILOT_PROVIDER_NAME = "github-copilot";
|
|
63
64
|
const PLACEHOLDER_API_KEY = "github-copilot";
|
|
65
|
+
/** The copilot fetch reads the JSON body for its headers, so it goes uncompressed. */
|
|
66
|
+
const COPILOT_RESPONSES_REQUEST = {
|
|
67
|
+
systemPrompt: "developer",
|
|
68
|
+
maxOutputTokens: true,
|
|
69
|
+
encryptedReasoning: "withEffort",
|
|
70
|
+
compress: false,
|
|
71
|
+
toolDefaults: false
|
|
72
|
+
};
|
|
64
73
|
function createGitHubCopilot(options = {}) {
|
|
65
74
|
const baseURL = (options.baseURL ?? "https://api.githubcopilot.com").replace(/\/+$/, "");
|
|
66
75
|
const fetch = createCopilotFetch({
|
|
@@ -79,12 +88,19 @@ function createGitHubCopilot(options = {}) {
|
|
|
79
88
|
includeUsage: true,
|
|
80
89
|
...options.transformRequestBody !== void 0 && { transformRequestBody: options.transformRequestBody }
|
|
81
90
|
});
|
|
82
|
-
const responses =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
91
|
+
const responses = (modelId) => new ResponsesLanguageModel(modelId, {
|
|
92
|
+
provider: GITHUB_COPILOT_PROVIDER_NAME,
|
|
93
|
+
label: "GitHub Copilot",
|
|
94
|
+
url: `${baseURL}/responses`,
|
|
95
|
+
headers: () => ({
|
|
96
|
+
...options.headers,
|
|
97
|
+
accept: "text/event-stream",
|
|
98
|
+
"content-type": "application/json"
|
|
99
|
+
}),
|
|
100
|
+
fetch,
|
|
101
|
+
request: COPILOT_RESPONSES_REQUEST,
|
|
102
|
+
...options.promptCacheKey !== void 0 && { promptCacheKey: options.promptCacheKey },
|
|
103
|
+
...options.compat !== void 0 && { compat: options.compat }
|
|
88
104
|
});
|
|
89
105
|
const messages = createAnthropic({
|
|
90
106
|
name: GITHUB_COPILOT_PROVIDER_NAME,
|
|
@@ -100,7 +116,7 @@ function createGitHubCopilot(options = {}) {
|
|
|
100
116
|
provider.specificationVersion = "v4";
|
|
101
117
|
provider.languageModel = (modelId) => chat.chatModel(modelId);
|
|
102
118
|
provider.chat = (modelId) => chat.chatModel(modelId);
|
|
103
|
-
provider.responses =
|
|
119
|
+
provider.responses = responses;
|
|
104
120
|
provider.messages = (modelId) => messages.messages(modelId);
|
|
105
121
|
provider.embeddingModel = (modelId) => {
|
|
106
122
|
throw new NoSuchModelError({
|
|
@@ -123,4 +139,4 @@ const githubCopilot = createGitHubCopilot();
|
|
|
123
139
|
//#endregion
|
|
124
140
|
export { createCopilotFetch as a, githubCopilot as i, GITHUB_COPILOT_PROVIDER_NAME as n, createGitHubCopilot as r, GITHUB_COPILOT_DEFAULT_BASE_URL as t };
|
|
125
141
|
|
|
126
|
-
//# sourceMappingURL=github-copilot-
|
|
142
|
+
//# sourceMappingURL=github-copilot-CdkIxdQS.mjs.map
|