Spaces:
Running
Running
File size: 11,615 Bytes
780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 780d695 98c9143 | 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | import { randomUUID } from "node:crypto"
import { COMPACT_REQUEST, type CompactType } from "~/lib/compact"
import type { State } from "./state"
import { getCachedOpencodeVersion } from "./opencode"
import { requestContext } from "./request-context"
export const isOpencodeOauthApp = (): boolean => {
return process.env.COPILOT_API_OAUTH_APP?.trim() === "opencode"
}
export const normalizeDomain = (input: string): string => {
return input
.trim()
.replace(/^https?:\/\//u, "")
.replace(/\/+$/u, "")
}
export const getEnterpriseDomain = (): string | null => {
const raw = (process.env.COPILOT_API_ENTERPRISE_URL ?? "").trim()
if (!raw) return null
const normalized = normalizeDomain(raw)
return normalized || null
}
export const getGitHubBaseUrl = (): string => {
const resolvedDomain = getEnterpriseDomain()
return resolvedDomain ? `https://${resolvedDomain}` : GITHUB_BASE_URL
}
export const getGitHubApiBaseUrl = (): string => {
const resolvedDomain = getEnterpriseDomain()
return resolvedDomain ? `https://api.${resolvedDomain}` : GITHUB_API_BASE_URL
}
const getOpencodeOauthHeaders = (): Record<string, string> => {
return {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": getOpencodeVersion(),
}
}
const getOpencodeLLMHeaders = (): Record<string, string> => {
return {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": OPENCODE_LLM_USER_AGENT,
}
}
const normalizeOpencodeUserAgent = (userAgent: string): string => {
const candidate = userAgent.trim()
const opencodeProduct = candidate.match(/^opencode\/[^\s,]+/u)?.[0]
if (!opencodeProduct || candidate.includes(`, ${opencodeProduct}`)) {
return candidate
}
return `${candidate}, ${opencodeProduct}`
}
export const getOauthUrls = (): {
deviceCodeUrl: string
accessTokenUrl: string
} => {
const githubBaseUrl = getGitHubBaseUrl()
return {
deviceCodeUrl: `${githubBaseUrl}/login/device/code`,
accessTokenUrl: `${githubBaseUrl}/login/oauth/access_token`,
}
}
interface OauthAppConfig {
clientId: string
headers: Record<string, string>
scope: string
}
export const getOauthAppConfig = (): OauthAppConfig => {
if (isOpencodeOauthApp()) {
return {
clientId: OPENCODE_GITHUB_CLIENT_ID,
headers: getOpencodeOauthHeaders(),
scope: GITHUB_APP_SCOPES,
}
}
return {
clientId: GITHUB_CLIENT_ID,
headers: standardHeaders(),
scope: GITHUB_APP_SCOPES,
}
}
export const prepareForCompact = (
headers: Record<string, string>,
compactType?: CompactType,
) => {
if (compactType) {
headers["x-initiator"] = "agent"
if (!isOpencodeOauthApp() && compactType === COMPACT_REQUEST) {
headers["x-interaction-type"] = "conversation-compaction"
headers["openai-intent"] = "conversation-agent"
}
}
}
export const prepareInteractionHeaders = (
sessionId: string | undefined,
isSubagent: boolean,
headers: Record<string, string>,
) => {
const sendInteractionHeaders = !isOpencodeOauthApp()
if (isSubagent) {
headers["x-initiator"] = "agent"
if (sendInteractionHeaders) {
headers["x-interaction-type"] = "conversation-subagent"
}
}
if (sessionId && sendInteractionHeaders) {
headers["x-interaction-id"] = sessionId
}
}
export const standardHeaders = () => ({
"content-type": "application/json",
accept: "application/json",
})
export const getOpencodeVersion = () => {
const version = getCachedOpencodeVersion()
if (version) {
return "opencode/" + version
}
return OPENCODE_VERSION
}
const OPENCODE_VERSION = "opencode/1.14.29"
const OPENCODE_LLM_USER_AGENT =
"opencode/1.14.29 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.13, opencode/1.14.29"
const COPILOT_VERSION = "0.47.1"
const EDITOR_PLUGIN_VERSION = `copilot-chat/${COPILOT_VERSION}`
const USER_AGENT = `GitHubCopilotChat/${COPILOT_VERSION}`
const CLAUDE_AGENT_USER_AGENT =
"vscode_claude_code/2.1.112 (external, sdk-ts, agent-sdk/0.2.112)"
const COPILOT_WEBSOCKET_VERSION = COPILOT_VERSION
const EDITOR_WEBSOCKET_PLUGIN_VERSION = `copilot-chat/${COPILOT_WEBSOCKET_VERSION}`
const API_VERSION = "2026-01-09"
const WEBSOCKET_API_VERSION = API_VERSION
export const copilotBaseUrl = (state: State) => {
const enterpriseDomain = getEnterpriseDomain()
if (enterpriseDomain) {
return `https://copilot-api.${enterpriseDomain}`
}
if (isOpencodeOauthApp()) {
return "https://api.githubcopilot.com"
}
if (state.copilotApiUrl) {
return state.copilotApiUrl
}
return state.accountType === "individual" ?
"https://api.githubcopilot.com"
: `https://api.${state.accountType}.githubcopilot.com`
}
export const prepareMessageProxyHeaders = (headers: Record<string, string>) => {
if (isOpencodeOauthApp()) {
return
}
// vscode copilot claude agent regenerates request id for
// each request, keeping it consistent
const requestIdValue = randomUUID()
headers["x-agent-task-id"] = requestIdValue
headers["x-request-id"] = requestIdValue
// Consistent with vscode copilot claude agent
headers["x-interaction-type"] = "messages-proxy"
headers["openai-intent"] = "messages-proxy"
headers["user-agent"] = CLAUDE_AGENT_USER_AGENT
delete headers["copilot-integration-id"]
}
export const githubUserHeaders = (state: State): Record<string, string> => {
if (isOpencodeOauthApp()) {
return {
Authorization: `Bearer ${state.githubToken}`,
"User-Agent": getOpencodeVersion(),
}
}
return {
accept: "application/vnd.github+json",
authorization: `token ${state.githubToken}`,
"user-agent": USER_AGENT,
"x-github-api-version": "2022-11-28",
"x-vscode-user-agent-library-version": "electron-fetch",
}
}
export const copilotModelsHeaders = (state: State) => {
if (isOpencodeOauthApp()) {
return {
Authorization: `Bearer ${state.copilotToken}`,
"User-Agent": getOpencodeVersion(),
}
}
const headers = githubCopilotHeaders(state)
headers["x-interaction-type"] = "model-access"
headers["openai-intent"] = "model-access"
delete headers["x-interaction-id"]
delete headers["content-type"]
return headers
}
export const copilotHeaders = (
state: State,
requestId?: string,
vision: boolean = false,
) => {
if (isOpencodeOauthApp()) {
const headers: Record<string, string> = {
Authorization: `Bearer ${state.copilotToken}`,
...getOpencodeLLMHeaders(),
"Openai-Intent": "conversation-edits",
}
const store = requestContext.getStore()
const userAgent = store?.userAgent.trim()
// Real opencode traffic already carries a versioned opencode/* UA,
// so prefer the inbound header to keep upstream behavior aligned.
if (userAgent?.startsWith("opencode/")) {
headers["User-Agent"] = normalizeOpencodeUserAgent(userAgent)
}
if (store?.sessionAffinity) {
headers["x-session-affinity"] = store.sessionAffinity
}
if (store?.parentSessionId) {
headers["x-parent-session-id"] = store.parentSessionId
}
if (vision) headers["Copilot-Vision-Request"] = "true"
return headers
}
return githubCopilotHeaders(state, requestId, vision)
}
export const copilotWebSocketHeaders = (
preparedHeaders: Record<string, string>,
) => {
if (isOpencodeOauthApp()) {
return omitHeader(preparedHeaders, "x-initiator")
}
const requestId =
getPreparedHeader(preparedHeaders, "x-request-id") ?? randomUUID()
const source = createHeaderResolver(preparedHeaders)
const headers: Record<string, string> = {
Authorization: source("authorization"),
"X-Request-Id": requestId,
"OpenAI-Intent": source("openai-intent", "conversation-agent"),
"X-GitHub-Api-Version": source(
"x-github-api-version",
WEBSOCKET_API_VERSION,
),
"X-Interaction-Id": source("x-interaction-id", requestId),
"X-Interaction-Type": source("x-interaction-type", "conversation-agent"),
"X-Agent-Task-Id": source("x-agent-task-id", requestId),
}
setPreparedHeader(
headers,
"VScode-SessionId",
preparedHeaders,
"vscode-sessionid",
)
setPreparedHeader(
headers,
"VScode-MachineId",
preparedHeaders,
"vscode-machineid",
)
Object.assign(headers, {
"Editor-Device-Id": source("editor-device-id"),
"Editor-Plugin-Version": source(
"editor-plugin-version",
EDITOR_WEBSOCKET_PLUGIN_VERSION,
),
"Editor-Version": source("editor-version"),
"Copilot-Integration-Id": source("copilot-integration-id", "vscode-chat"),
})
setPreparedHeader(
headers,
"Copilot-Vision-Request",
preparedHeaders,
"copilot-vision-request",
)
headers["user-agent"] = "node"
return headers
}
const createHeaderResolver =
(headers: Record<string, string>) =>
(headerName: string, fallback: string = ""): string =>
getPreparedHeader(headers, headerName) ?? fallback
const getPreparedHeader = (
headers: Record<string, string>,
headerName: string,
): string | undefined => {
const normalizedHeaderName = headerName.toLowerCase()
const match = Object.entries(headers).find(
([key]) => key.toLowerCase() === normalizedHeaderName,
)
return match?.[1]
}
const setPreparedHeader = (
target: Record<string, string>,
targetHeaderName: string,
source: Record<string, string>,
sourceHeaderName: string,
): void => {
const value = getPreparedHeader(source, sourceHeaderName)
if (value) {
target[targetHeaderName] = value
}
}
const omitHeader = (
headers: Record<string, string>,
headerName: string,
): Record<string, string> => {
const normalizedHeaderName = headerName.toLowerCase()
return Object.fromEntries(
Object.entries(headers).filter(
([key]) => key.toLowerCase() !== normalizedHeaderName,
),
)
}
const githubCopilotHeaders = (
state: State,
requestId?: string,
vision: boolean = false,
) => {
const requestIdValue = requestId ?? randomUUID()
const headers: Record<string, string> = {
Authorization: `Bearer ${state.copilotToken}`,
"content-type": standardHeaders()["content-type"],
"copilot-integration-id": "vscode-chat",
"editor-device-id": state.vsCodeDeviceId,
"editor-version": `vscode/${state.vsCodeVersion}`,
"editor-plugin-version": EDITOR_PLUGIN_VERSION,
"user-agent": USER_AGENT,
"openai-intent": "conversation-agent",
"x-github-api-version": API_VERSION,
"x-request-id": requestIdValue,
"x-vscode-user-agent-library-version": "electron-fetch",
"x-agent-task-id": requestIdValue,
"x-interaction-type": "conversation-agent",
}
if (vision) headers["copilot-vision-request"] = "true"
if (state.macMachineId) {
headers["vscode-machineid"] = state.macMachineId
}
if (state.vsCodeSessionId) {
headers["vscode-sessionid"] = state.vsCodeSessionId
}
return headers
}
export const GITHUB_API_BASE_URL = "https://api.github.com"
export const githubHeaders = (state: State): Record<string, string> => {
if (isOpencodeOauthApp()) {
return {
Authorization: `Bearer ${state.githubToken}`,
...getOpencodeOauthHeaders(),
}
}
return {
authorization: `token ${state.githubToken}`,
"user-agent": USER_AGENT,
"x-github-api-version": "2025-04-01",
"x-vscode-user-agent-library-version": "electron-fetch",
}
}
export const GITHUB_BASE_URL = "https://github.com"
export const GITHUB_CLIENT_ID = "Iv1.b507a08c87ecfe98"
export const GITHUB_APP_SCOPES = ["read:user"].join(" ")
export const OPENCODE_GITHUB_CLIENT_ID = "Ov23li8tweQw6odWQebz"
|