(input: RequestInfo, init?: RequestInit)
| 71 | baseURL, |
| 72 | apiKey: "", |
| 73 | async fetch(input: RequestInfo, init?: RequestInit): Promise<Response> { |
| 74 | const currentInfo = await getAuth() |
| 75 | if (currentInfo.type !== "oauth") return fetch(input, init) |
| 76 | |
| 77 | const currentEnterpriseUrl = (currentInfo as any).enterpriseUrl as string | undefined |
| 78 | const domain = currentEnterpriseUrl ? normalizeDomain(currentEnterpriseUrl) : "github.com" |
| 79 | const urls = getUrls(domain) |
| 80 | |
| 81 | let accessToken = currentInfo.access |
| 82 | |
| 83 | // Refresh token if expired or missing |
| 84 | if (!accessToken || currentInfo.expires < Date.now()) { |
| 85 | const response = await fetch(urls.COPILOT_API_KEY_URL, { |
| 86 | headers: { |
| 87 | Accept: "application/json", |
| 88 | Authorization: `Bearer ${currentInfo.refresh}`, |
| 89 | ...HEADERS, |
| 90 | }, |
| 91 | }) |
| 92 | |
| 93 | if (!response.ok) { |
| 94 | throw new Error(`Token refresh failed: ${response.status}`) |
| 95 | } |
| 96 | |
| 97 | const tokenData = await response.json() |
| 98 | accessToken = tokenData.token |
| 99 | } |
| 100 | |
| 101 | let isAgentCall = false |
| 102 | let isVisionRequest = false |
| 103 | |
| 104 | try { |
| 105 | const body = typeof init?.body === "string" ? JSON.parse(init.body) : init?.body |
| 106 | |
| 107 | if (body?.messages) { |
| 108 | if (body.messages.length > 0) { |
| 109 | const lastMessage = body.messages[body.messages.length - 1] |
| 110 | isAgentCall = lastMessage.role && ["tool", "assistant"].includes(lastMessage.role) |
| 111 | } |
| 112 | isVisionRequest = body.messages.some( |
| 113 | (msg: any) => |
| 114 | Array.isArray(msg.content) && msg.content.some((part: any) => part.type === "image_url"), |
| 115 | ) |
| 116 | } |
| 117 | |
| 118 | if (body?.input) { |
| 119 | const lastInput = body.input[body.input.length - 1] |
| 120 | const isAssistant = lastInput?.role === "assistant" |
| 121 | const hasAgentType = lastInput?.type |
| 122 | ? RESPONSES_API_ALTERNATE_INPUT_TYPES.includes(lastInput.type) |
| 123 | : false |
| 124 | isAgentCall = isAssistant || hasAgentType |
| 125 | |
| 126 | isVisionRequest = |
| 127 | Array.isArray(lastInput?.content) && |
| 128 | lastInput.content.some((part: any) => part.type === "input_image") |
| 129 | } |
| 130 | } catch {} |
no test coverage detected