| 181 | } |
| 182 | |
| 183 | export async function requireSession(request: Request): Promise<Session> { |
| 184 | const sessionId = |
| 185 | request.headers.get("x-agent-kanban-session")?.trim() ?? |
| 186 | getCookie(request, "agent-kanban-session")?.trim() |
| 187 | if (!sessionId) { |
| 188 | throw new MissingCursorApiKeyError() |
| 189 | } |
| 190 | |
| 191 | const session = sessions.get(sessionId) |
| 192 | if (session) { |
| 193 | return session |
| 194 | } |
| 195 | |
| 196 | const restored = await restoreSession(sessionId) |
| 197 | const restoredSession = sessions.get(restored.id) |
| 198 | if (!restoredSession) { |
| 199 | throw new UnknownSessionError() |
| 200 | } |
| 201 | |
| 202 | return restoredSession |
| 203 | } |
| 204 | |
| 205 | function getCookie(request: Request, name: string) { |
| 206 | const cookies = request.headers.get("cookie") ?? "" |