(params: {
row: InternalSessionRow | null
position: number
/** Snapshot of every model's queue depth at response time. Only consumed
* by the `queued` variant — active/ended don't need the selector. */
queueDepthByModel: Record<string, number>
graceMs: number
now: Date
})
| 19 | * `{ status: 'none' }` to the client. |
| 20 | */ |
| 21 | export function toSessionStateResponse(params: { |
| 22 | row: InternalSessionRow | null |
| 23 | position: number |
| 24 | /** Snapshot of every model's queue depth at response time. Only consumed |
| 25 | * by the `queued` variant — active/ended don't need the selector. */ |
| 26 | queueDepthByModel: Record<string, number> |
| 27 | graceMs: number |
| 28 | now: Date |
| 29 | }): SessionStateResponse | null { |
| 30 | const { row, position, queueDepthByModel, graceMs, now } = params |
| 31 | if (!row) return null |
| 32 | |
| 33 | if (row.status === 'active' && row.expires_at) { |
| 34 | const expiresAtMs = row.expires_at.getTime() |
| 35 | const nowMs = now.getTime() |
| 36 | if (expiresAtMs > nowMs) { |
| 37 | return { |
| 38 | status: 'active', |
| 39 | accessTier: row.access_tier ?? 'full', |
| 40 | instanceId: row.active_instance_id, |
| 41 | model: row.model, |
| 42 | admittedAt: (row.admitted_at ?? row.created_at).toISOString(), |
| 43 | expiresAt: row.expires_at.toISOString(), |
| 44 | remainingMs: expiresAtMs - nowMs, |
| 45 | ...limitedModeReasonFromRow(row), |
| 46 | } |
| 47 | } |
| 48 | const graceEndsMs = expiresAtMs + graceMs |
| 49 | if (graceEndsMs > nowMs) { |
| 50 | return { |
| 51 | status: 'ended', |
| 52 | accessTier: row.access_tier ?? 'full', |
| 53 | instanceId: row.active_instance_id, |
| 54 | admittedAt: (row.admitted_at ?? row.created_at).toISOString(), |
| 55 | expiresAt: row.expires_at.toISOString(), |
| 56 | gracePeriodEndsAt: new Date(graceEndsMs).toISOString(), |
| 57 | gracePeriodRemainingMs: graceEndsMs - nowMs, |
| 58 | ...limitedModeReasonFromRow(row), |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (row.status === 'queued') { |
| 64 | return { |
| 65 | status: 'queued', |
| 66 | accessTier: row.access_tier ?? 'full', |
| 67 | instanceId: row.active_instance_id, |
| 68 | model: row.model, |
| 69 | position, |
| 70 | queueDepth: queueDepthByModel[row.model] ?? 0, |
| 71 | queueDepthByModel, |
| 72 | estimatedWaitMs: estimateWaitMs({ position }), |
| 73 | queuedAt: row.queued_at.toISOString(), |
| 74 | ...limitedModeReasonFromRow(row), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // active row past the grace window — callers should treat as "no session" and re-queue |
no test coverage detected