fix(qoder): address review findings

Correctness:
- testUtils: drop checkExpiry so the userinfo URL probe actually runs (revoked
  tokens used to look "active" until local 30-day expiry passed)
- auth.parseExpiry: handle numeric expiresAt, swap parseInt before Date.parse
  so "2026" doesn't get interpreted as year-2026, treat expires_in:0 as
  already-expired instead of fabricating a 30-day default
- providers.mapTokens: synthesize email from userId when fetchUserInfo fails
  so OAuth dedup works (re-logins no longer accumulate "Account N" rows)

SSE wrapper:
- wrapQoderSSE: add !doneEmitted guard on success branch (chunks could leak
  past [DONE] when an error envelope shared a TCP packet with a valid one)
- flush(): finalize TextDecoder + drain trailing buffer so the chunk carrying
  finish_reason is delivered when upstream closes without a final \n
- sanitize literal \n inside inner OpenAI body so SSE framing stays intact

Robustness:
- executor: wrap buildCosyHeaders in try/catch so a missing accessToken
  returns 401 (re-auth) instead of bubbling as 500
- executor: short-circuit on missing accessToken before signing
- executor: plumb proxyOptions/signal through buildQoderRequestBody so
  proxy-only networks can fetch the model_config catalog
- qoderModels: dedupe concurrent first-time misses with an in-flight Promise
  map (parallel chat windows now do 1 upstream fetch instead of N)
- qoderModels: check signal.aborted before addEventListener so a pre-aborted
  parent signal cancels the inner fetch immediately
- auth: AbortController + 15s timeout on pollDeviceToken / fetchUserInfo to
  prevent hung sockets when openapi.qoder.sh stalls mid-response

UX:
- OAuthModal: derive polling deadline from device-code expires_in (qoder
  publishes 300s; the previous fixed 120s caused timeouts when users took
  more than 2 minutes on the consent page)

Cleanup:
- delete src/lib/oauth/services/qoder.js — referenced removed config fields
  (clientId/clientSecret/tokenUrl/authorizeUrl) and was re-exported from
  services/index.js, so any future caller would TypeError on first use
This commit is contained in:
Simon Shi
2026-05-29 17:36:27 +07:00
committed by decolua
parent a6fd84691b
commit 620b59ca0b
8 changed files with 219 additions and 318 deletions
+18 -4
View File
@@ -86,12 +86,16 @@ export default function OAuthModal({ isOpen, provider, providerInfo, onSuccess,
}, [authData, onSuccess]);
// Poll for device code token
const startPolling = useCallback(async (deviceCode, codeVerifier, interval, extraData) => {
const startPolling = useCallback(async (deviceCode, codeVerifier, interval, extraData, deadlineMs) => {
pollingAbortRef.current = false;
setPolling(true);
const maxAttempts = 60;
// Honor the upstream's expires_in when supplied (qoder sets 300s) so we
// don't time out earlier than the device code itself. Default 120s
// matches the prior behavior for providers that don't surface a value.
const startedAt = Date.now();
const deadline = startedAt + (Number.isFinite(deadlineMs) && deadlineMs > 0 ? deadlineMs : 120_000);
for (let i = 0; i < maxAttempts; i++) {
while (Date.now() < deadline) {
// Check if polling should be aborted
if (pollingAbortRef.current) {
console.log("[OAuthModal] Polling aborted");
@@ -193,7 +197,17 @@ export default function OAuthModal({ isOpen, provider, providerInfo, onSuccess,
_qoderVerifier: data.codeVerifier,
}
: null;
startPolling(data.device_code, data.codeVerifier, data.interval || 5, extraData);
startPolling(
data.device_code,
data.codeVerifier,
data.interval || 5,
extraData,
// Use the upstream's expires_in if present so we don't time out
// before the device code itself (qoder gives 300s).
Number.isFinite(data.expires_in) && data.expires_in > 0
? data.expires_in * 1000
: undefined,
);
return;
}