import type { CreateSessionInput, SessionId, SessionInfo, MessageInfo, MessagePart, ProjectId, PermissionRuleset, TodoItem, SessionStorePort, MessageWithParts, } from "@zcode/contracts"; export function createRecordingSubagentSessionStore(): SessionStorePort { const sessions = new Map(); const entries = new Map< string, Parameters>[0] >(); const messagesBySession = new Map(); const partsByMessage = new Map(); const permissionsByProject = new Map(); const todosBySession = new Map(); const store = { async saveSessionEntry( entry: Parameters>[0], ) { entries.set(entry.id, entry); }, async sessionEntries({ sessionID, type, }: Parameters>[0]) { return [...entries.values()].filter( (entry) => entry.sessionID === sessionID && (!type || entry.type === type), ); }, async createSession(input: CreateSessionInput): Promise { const now = Date.now(); const session: SessionInfo = { id: input.id, projectID: input.projectID, workspaceID: input.workspaceID, parentID: input.parentID, traceID: input.traceID, taskType: input.taskType ?? "interactive", slug: input.slug, directory: input.directory, path: input.path, title: input.title, titleSource: input.titleSource, titleMessageID: input.titleMessageID, version: input.version, shareURL: input.shareURL, permission: input.permission, time: { created: input.time?.created ?? now, updated: input.time?.updated ?? input.time?.created ?? now, }, }; sessions.set(session.id, session); return session; }, async updateSession( input: Parameters[0], ): Promise { const session = sessions.get(input.id); if (!session) { throw new Error(`Unknown session: ${input.id}`); } const updated: SessionInfo = { ...session, ...(input.title !== undefined ? { title: input.title } : {}), ...(input.titleSource !== undefined ? { titleSource: input.titleSource } : {}), ...(input.titleMessageID !== undefined && input.titleMessageID !== null ? { titleMessageID: input.titleMessageID } : {}), time: { ...session.time, updated: Date.now(), }, }; sessions.set(updated.id, updated); return updated; }, async getSession(sessionID: SessionId): Promise { return sessions.get(sessionID) ?? null; }, async listSessions(): Promise { return [...sessions.values()]; }, async saveMessage(input: MessageInfo): Promise { const messages = messagesBySession.get(input.sessionID) ?? []; messages.push(input); messagesBySession.set(input.sessionID, messages); }, async removeMessage(input: Parameters[0]): Promise { const messages = messagesBySession.get(input.sessionID) ?? []; messagesBySession.set( input.sessionID, messages.filter((message) => message.id !== input.messageID), ); partsByMessage.delete(input.messageID); }, async savePart(input: MessagePart): Promise { const parts = partsByMessage.get(input.messageID) ?? []; parts.push(input); partsByMessage.set(input.messageID, parts); }, async removePart(input: Parameters[0]): Promise { const parts = partsByMessage.get(input.messageID) ?? []; partsByMessage.set( input.messageID, parts.filter((part) => part.id !== input.partID), ); }, async messages(input): Promise { return (messagesBySession.get(input.sessionID) ?? []).map((message) => ({ info: message, parts: partsByMessage.get(message.id) ?? [], })); }, async getProjectPermission(projectID: ProjectId): Promise { return permissionsByProject.get(projectID) ?? null; }, async saveProjectPermission(input: { permission: PermissionRuleset; projectID: ProjectId; }): Promise { permissionsByProject.set(input.projectID, input.permission); return input.permission; }, async readTodos(input: { sessionID: SessionId }) { return todosBySession.get(input.sessionID) ?? []; }, async updateTodos(input: { sessionID: SessionId; todos: TodoItem[] }) { todosBySession.set(input.sessionID, input.todos); }, }; return store as unknown as SessionStorePort; }