Repository navigation
fix(core): ask to reconnect ChatGPT when its refresh token is rejected - #65289
Conversation
There was a problem hiding this comment.
The change looks sound to me. When OpenAI rejects a refresh with invalid_grant, the new message replaces the bare 400. It reaches users unchanged through Integration.AuthorizationError. I couldn't run it end to end, because that needs a real ChatGPT sign-in whose token has been revoked. Instead I ran refresh() against a local server. Without the fix, the invalid_grant case fails with "Request failed: 400". With the fix, the new message appears, and other 400s and 500s keep their current error. The package's tests and tsc -b pass.
The export with a default URL follows the pattern revoke already uses. The invalid_grant check matches the existing handling in opencode.ts. The test is small and uses a local server, not the real network.
Public API: unchanged. refresh is a newly exported function inside core only.
No blocking concerns.
Related: #55211
There was a problem hiding this comment.
🟡 Changes recommended
The reconnect-message branch incorrectly handles non-400 responses containing invalid_grant, masking server errors as expired sign-ins instead of preserving the existing HTTP error.
1 open issue
What changed in this PR
Improves ChatGPT OAuth refresh errors in core so users are told to reconnect when their refresh token is rejected, addressing the unclear prompt failures reported in #60133. Authorization-code exchange behavior and connection status remain unchanged.
Changes:
- Recognizes
invalid_grantresponses during refresh and supplies an actionable reconnect message. - Adds local-server tests for rejected refresh tokens and other HTTP failures.
| File | Description |
|---|---|
packages/core/test/plugin/provider-chatgpt.test.ts |
Tests reconnect messaging and fallback HTTP errors. |
packages/core/src/plugin/provider/chatgpt.ts |
Decodes refresh rejection responses and provides the reconnect message. |
Review effort: Balanced
| if (!response.ok) throw new Error(`Request failed: ${response.status}`) | ||
| return response.json() as Promise<A> | ||
| if (response.ok) return response.json() as Promise<A> | ||
| if (rejected && Option.isSome(decodeInvalidGrant(await response.text()))) throw new Error(rejected) |

Issue for this PR
Closes #60133
Type of change
What does this PR do?
When a ChatGPT sign-in's refresh token has been revoked or has expired, OpenAI's token endpoint answers 400 with
{"error":"invalid_grant"}.request()dropped the body, so every prompt failed with "Request failed: 400" and nothing told the user to reconnect.refresh()now turns that response into "This ChatGPT sign-in has expired or was revoked. Connect again." Onlyinvalid_granton the refresh grant is matched. Other failures, and the authorization-code exchange, keep their current errors.This does not add a "needs sign in" status for the connection. The refresh runs in core rather than in the plugin, so where that status would be set needs a decision and is left out of this PR.
How did you verify your code works?
provider-chatgpt.test.tsthat runsrefresh()against a local server.invalid_grantgives the new message; a 400 with a different error and a 500 still give "Request failed: ". The test fails without the fix.bun run test test/plugin/provider-chatgpt.test.tsandbun typecheckinpackages/core, andbun run checkat the root.{"error":"invalid_grant"}. I have not tested against a real sign-in whose token was revoked.Screenshots / recordings
Not a UI change.
Checklist
Added by the opencode bot (AI-generated).
Related: #55211