| 123 | * code straight to OpenCode's localhost callback. |
| 124 | */ |
| 125 | export const completeOAuthConsent = async ( |
| 126 | home: OpenCodeHome, |
| 127 | email: string, |
| 128 | sinceIndex: number, |
| 129 | ): Promise<void> => { |
| 130 | const deadline = Date.now() + 30_000; |
| 131 | while (Date.now() < deadline) { |
| 132 | const url = home.openedUrls()[sinceIndex]; |
| 133 | if (url) { |
| 134 | const response = await fetch(`${url}&login_hint=${encodeURIComponent(email)}`); |
| 135 | if (!response.ok) throw new Error(`consent redirect chain failed (${response.status})`); |
| 136 | return; |
| 137 | } |
| 138 | await new Promise((tick) => setTimeout(tick, 250)); |
| 139 | } |
| 140 | throw new Error("opencode never opened an authorization URL"); |
| 141 | }; |