(
method: 'POST' | 'GET' | 'DELETE',
token: string,
opts: { instanceId?: string; model?: string; signal?: AbortSignal } = {},
)
| 56 | } |
| 57 | |
| 58 | async function callSession( |
| 59 | method: 'POST' | 'GET' | 'DELETE', |
| 60 | token: string, |
| 61 | opts: { instanceId?: string; model?: string; signal?: AbortSignal } = {}, |
| 62 | ): Promise<FreebuffSessionServerResponse> { |
| 63 | const headers: Record<string, string> = { Authorization: `Bearer ${token}` } |
| 64 | if (method === 'GET' && opts.instanceId) { |
| 65 | headers[FREEBUFF_INSTANCE_HEADER] = opts.instanceId |
| 66 | } |
| 67 | if (method === 'POST' && opts.model) { |
| 68 | headers[FREEBUFF_MODEL_HEADER] = opts.model |
| 69 | } |
| 70 | const resp = await fetch(sessionEndpoint(), { |
| 71 | method, |
| 72 | headers, |
| 73 | signal: opts.signal, |
| 74 | }) |
| 75 | // 404 = endpoint not deployed on this server (older web build). Treat as |
| 76 | // "waiting room disabled" so a newer CLI against an older server still |
| 77 | // works, rather than stranding users in a waiting room forever. |
| 78 | if (resp.status === 404) { |
| 79 | return { status: 'disabled' } |
| 80 | } |
| 81 | // 403 with a country_blocked or banned body is a terminal signal, not an |
| 82 | // error — the server rejects non-allowlist countries and banned accounts up |
| 83 | // front (see session _handlers.ts) so they don't wait through the queue only |
| 84 | // to be rejected at chat time. The 403 status (rather than 200) is |
| 85 | // deliberate: older CLIs that don't know these statuses treat them as a |
| 86 | // generic error and back off on the 10s error-retry cadence instead of |
| 87 | // tight-polling an unrecognized 200 body. |
| 88 | if (resp.status === 403) { |
| 89 | const body = (await resp |
| 90 | .json() |
| 91 | .catch(() => null)) as FreebuffSessionServerResponse | null |
| 92 | if ( |
| 93 | body && |
| 94 | (body.status === 'country_blocked' || body.status === 'banned') |
| 95 | ) { |
| 96 | return body |
| 97 | } |
| 98 | } |
| 99 | // 409 from POST means the selected model cannot be joined right now, either |
| 100 | // because an active session is locked to another model or because a |
| 101 | // Surface model-switch conflicts and temporary model availability closures |
| 102 | // as non-throw states. |
| 103 | if (resp.status === 409 && method === 'POST') { |
| 104 | const body = (await resp |
| 105 | .json() |
| 106 | .catch(() => null)) as FreebuffSessionServerResponse | null |
| 107 | if ( |
| 108 | body && |
| 109 | (body.status === 'model_locked' || body.status === 'model_unavailable') |
| 110 | ) { |
| 111 | return body |
| 112 | } |
| 113 | } |
| 114 | // 429 from POST is the per-model session-quota reject (e.g. too many DeepSeek |
| 115 | // sessions in the last 12h). Terminal for the current poll — the CLI shows |
no test coverage detected