/** * Session Manager * * 管理所有活跃的会话 */ import { v4 as uuidv4 } from 'uuid'; import type { Session, CreateSessionInput, Message, SessionStatus } from '../types.js'; export class SessionManager { private sessions: Map = new Map(); private messages: Map = new Map(); /** * 创建新会话 */ create(input: CreateSessionInput = {}): Session { const now = new Date().toISOString(); const session: Session = { id: uuidv4(), name: input.name, workdir: input.workdir || process.cwd(), createdAt: now, updatedAt: now, status: 'idle', messageCount: 0, }; this.sessions.set(session.id, session); this.messages.set(session.id, []); return session; } /** * 获取所有会话 */ list(): Session[] { return Array.from(this.sessions.values()).sort( (a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime() ); } /** * 获取单个会话 */ get(id: string): Session | undefined { return this.sessions.get(id); } /** * 删除会话 */ delete(id: string): boolean { this.messages.delete(id); return this.sessions.delete(id); } /** * 更新会话状态 */ updateStatus(id: string, status: SessionStatus): Session | undefined { const session = this.sessions.get(id); if (!session) return undefined; session.status = status; session.updatedAt = new Date().toISOString(); return session; } /** * 获取会话消息 */ getMessages(sessionId: string): Message[] { return this.messages.get(sessionId) || []; } /** * 添加消息 */ addMessage(sessionId: string, message: Omit): Message | undefined { const session = this.sessions.get(sessionId); if (!session) return undefined; const fullMessage: Message = { ...message, id: uuidv4(), sessionId, createdAt: new Date().toISOString(), }; const messages = this.messages.get(sessionId) || []; messages.push(fullMessage); this.messages.set(sessionId, messages); // 更新会话 session.messageCount = messages.length; session.updatedAt = new Date().toISOString(); return fullMessage; } /** * 获取会话数量 */ count(): number { return this.sessions.size; } /** * 检查会话是否存在 */ exists(id: string): boolean { return this.sessions.has(id); } } // 单例 let instance: SessionManager | null = null; export function getSessionManager(): SessionManager { if (!instance) { instance = new SessionManager(); } return instance; }