forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-config.ts
More file actions
72 lines (61 loc) · 2.57 KB
/
api-config.ts
File metadata and controls
72 lines (61 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { randomUUID } from "node:crypto"
import type { State } from "./state"
export const standardHeaders = () => ({
"content-type": "application/json",
accept: "application/json",
})
const COPILOT_VERSION_FALLBACK = "0.45.1"
const copilotVersion = (state: State) =>
state.copilotChatVersion ?? COPILOT_VERSION_FALLBACK
const editorPluginVersion = (state: State) =>
`copilot-chat/${copilotVersion(state)}`
const userAgent = (state: State) => `GitHubCopilotChat/${copilotVersion(state)}`
const API_VERSION = "2025-10-01"
export const copilotBaseUrl = (state: State) => {
if (state.copilotApiEndpoint) return state.copilotApiEndpoint
return state.accountType === "individual" ?
"https://api.githubcopilot.com"
: `https://api.${state.accountType}.githubcopilot.com`
}
export const copilotHeaders = (state: State, vision: boolean = false) => {
const headers: Record<string, string> = {
Authorization: `Bearer ${state.copilotToken}`,
"content-type": standardHeaders()["content-type"],
"copilot-integration-id": "vscode-chat",
"editor-version": `vscode/${state.vsCodeVersion}`,
"editor-plugin-version": editorPluginVersion(state),
"user-agent": userAgent(state),
"openai-intent": "conversation-panel",
"x-github-api-version": API_VERSION,
"x-request-id": randomUUID(),
"x-vscode-user-agent-library-version": "electron-fetch",
}
if (vision) headers["copilot-vision-request"] = "true"
return headers
}
export const githubHeaders = (state: State) => ({
...standardHeaders(),
authorization: `token ${state.githubToken}`,
"editor-version": `vscode/${state.vsCodeVersion}`,
"editor-plugin-version": editorPluginVersion(state),
"user-agent": userAgent(state),
"x-github-api-version": API_VERSION,
"x-vscode-user-agent-library-version": "electron-fetch",
})
export const GITHUB_CLIENT_ID = "01ab8ac9400c4e429b23"
export const GITHUB_APP_SCOPES = ["read:user"].join(" ")
/**
* GitHub Copilot upstream rejects `user` strings longer than 64 characters
* with `invalid_request_body`. Claude Code's `metadata.user_id` (forwarded as
* `user`) is typically far longer (account+session composite). Truncate to
* stay within the limit while preserving the prefix, which is generally the
* stable per-user portion.
*/
export const COPILOT_USER_MAX_LENGTH = 64
export function clampUserField<T extends { user?: string | null }>(
payload: T,
): T {
if (typeof payload.user !== "string") return payload
if (payload.user.length <= COPILOT_USER_MAX_LENGTH) return payload
return { ...payload, user: payload.user.slice(0, COPILOT_USER_MAX_LENGTH) }
}