| 66 | } |
| 67 | |
| 68 | export class SessionManager { |
| 69 | private sessions = new Map<string, Session>(); |
| 70 | private storage = new AsyncLocalStorage<Session>(); |
| 71 | private persistencePath: string | null = null; |
| 72 | |
| 73 | configurePersistence(filePath?: string | null): void { |
| 74 | this.persistencePath = filePath || null; |
| 75 | this.sessions.clear(); |
| 76 | |
| 77 | if (!this.persistencePath || !existsSync(this.persistencePath)) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | try { |
| 82 | const parsed = JSON.parse(readFileSync(this.persistencePath, 'utf8')); |
| 83 | const records = Array.isArray(parsed?.sessions) ? parsed.sessions as PersistedSessionRecord[] : []; |
| 84 | |
| 85 | for (const record of records) { |
| 86 | if (!record?.id || !record?.agentId) continue; |
| 87 | this.sessions.set(record.id, { |
| 88 | id: record.id, |
| 89 | parentId: record.parentId, |
| 90 | agentId: record.agentId, |
| 91 | messages: Array.isArray(record.messages) ? record.messages : [], |
| 92 | context: record.context || {}, |
| 93 | metadata: record.metadata || {}, |
| 94 | abort: new AbortController(), |
| 95 | createdAt: record.createdAt || Date.now(), |
| 96 | updatedAt: record.updatedAt || Date.now(), |
| 97 | }); |
| 98 | } |
| 99 | } catch (error) { |
| 100 | console.warn(`Failed to load persisted sessions from ${this.persistencePath}:`, error); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | create(agentId: string, parentId?: string): Session { |
| 105 | const session: Session = { |
| 106 | id: randomUUID(), |
| 107 | parentId, |
| 108 | agentId, |
| 109 | messages: [], |
| 110 | context: {}, |
| 111 | metadata: {}, |
| 112 | abort: new AbortController(), |
| 113 | createdAt: Date.now(), |
| 114 | updatedAt: Date.now() |
| 115 | }; |
| 116 | |
| 117 | this.sessions.set(session.id, session); |
| 118 | this.persist(); |
| 119 | return session; |
| 120 | } |
| 121 | |
| 122 | get(id: string): Session | undefined { |
| 123 | return this.sessions.get(id); |
| 124 | } |
| 125 |
nothing calls this directly
no outgoing calls
no test coverage detected