| 100 | return { |
| 101 | apiKey: "", |
| 102 | async fetch(request: RequestInfo | URL, init?: RequestInit) { |
| 103 | const info = await getAuth() |
| 104 | if (info.type !== "oauth") return fetch(request, init) |
| 105 | |
| 106 | const url = request instanceof URL ? request.href : typeof request === "string" ? request : request.url |
| 107 | const { isVision, isAgent } = iife(() => { |
| 108 | try { |
| 109 | const body = typeof init?.body === "string" ? JSON.parse(init.body) : init?.body |
| 110 | |
| 111 | // Completions API |
| 112 | if (body?.messages && url.includes("completions")) { |
| 113 | const last = body.messages[body.messages.length - 1] |
| 114 | return { |
| 115 | isVision: body.messages.some( |
| 116 | (msg: any) => |
| 117 | Array.isArray(msg.content) && msg.content.some((part: any) => part.type === "image_url"), |
| 118 | ), |
| 119 | isAgent: last?.role !== "user" || imgMsg(last), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Responses API |
| 124 | if (body?.input) { |
| 125 | const last = body.input[body.input.length - 1] |
| 126 | return { |
| 127 | isVision: body.input.some( |
| 128 | (item: any) => |
| 129 | Array.isArray(item?.content) && item.content.some((part: any) => part.type === "input_image"), |
| 130 | ), |
| 131 | isAgent: last?.role !== "user" || imgMsg(last), |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Messages API |
| 136 | if (body?.messages) { |
| 137 | const last = body.messages[body.messages.length - 1] |
| 138 | const hasNonToolCalls = |
| 139 | Array.isArray(last?.content) && last.content.some((part: any) => part?.type !== "tool_result") |
| 140 | return { |
| 141 | isVision: body.messages.some( |
| 142 | (item: any) => |
| 143 | Array.isArray(item?.content) && |
| 144 | item.content.some( |
| 145 | (part: any) => |
| 146 | part?.type === "image" || |
| 147 | // images can be nested inside tool_result content |
| 148 | (part?.type === "tool_result" && |
| 149 | Array.isArray(part?.content) && |
| 150 | part.content.some((nested: any) => nested?.type === "image")), |
| 151 | ), |
| 152 | ), |
| 153 | isAgent: !(last?.role === "user" && hasNonToolCalls) || imgMsg(last), |
| 154 | } |
| 155 | } |
| 156 | } catch {} |
| 157 | return { isVision: false, isAgent: false } |
| 158 | }) |
| 159 | |