| 175 | * In tests, each test creates its own store for isolation. |
| 176 | */ |
| 177 | export function createSessionStore(): SessionStore { |
| 178 | const sessions = new Map<string, SessionState>() |
| 179 | |
| 180 | function get(id: string): SessionState { |
| 181 | const existing = sessions.get(id) |
| 182 | if (existing) return existing |
| 183 | |
| 184 | const state = createSessionState() |
| 185 | sessions.set(id, state) |
| 186 | return state |
| 187 | } |
| 188 | |
| 189 | function has(id: string): boolean { |
| 190 | return sessions.has(id) |
| 191 | } |
| 192 | |
| 193 | function remove(id: string): void { |
| 194 | sessions.delete(id) |
| 195 | } |
| 196 | |
| 197 | function incrementTurn(id: string): number { |
| 198 | const session = get(id) |
| 199 | session.turnCount++ |
| 200 | return session.turnCount |
| 201 | } |
| 202 | |
| 203 | function setModel(id: string, provider?: string, model?: string): void { |
| 204 | const session = get(id) |
| 205 | if (provider) session.provider = provider |
| 206 | if (model) session.model = model |
| 207 | } |
| 208 | |
| 209 | function setPrompt(id: string, prompt: string): void { |
| 210 | if (!prompt) return |
| 211 | const session = get(id) |
| 212 | session.lastPrompt = prompt |
| 213 | } |
| 214 | |
| 215 | function startTool(id: string, callId: string, now?: number): void { |
| 216 | const session = get(id) |
| 217 | session.toolStartTimes.set(callId, now ?? Date.now()) |
| 218 | } |
| 219 | |
| 220 | function endTool(id: string, callId: string, now?: number): number | undefined { |
| 221 | const session = sessions.get(id) |
| 222 | if (!session) return undefined |
| 223 | |
| 224 | const start = session.toolStartTimes.get(callId) |
| 225 | if (start === undefined) return undefined |
| 226 | |
| 227 | session.toolStartTimes.delete(callId) |
| 228 | return (now ?? Date.now()) - start |
| 229 | } |
| 230 | |
| 231 | function size(): number { |
| 232 | return sessions.size |
| 233 | } |
| 234 | |