/** * Session Memory Service * * Manages session lifecycle for agents: startup briefings, session summaries, * and the project-level agents index. Coordinates with AgentMemoryService and * ProjectMemoryService to assemble the context an agent needs when it starts * a new work session and to persist what it learned when the session ends. * * Storage locations: * - Session summaries: ~/.crewly/agents/{agentId}/sessions/ * - Agents index: {projectPath}/.crewly/agents-index.json * * @module services/memory/session-memory.service */ import type { StartupBriefing } from '../../types/memory.types.js'; /** * Service for managing agent session lifecycle * * Follows singleton pattern for consistent state management. * Coordinates AgentMemoryService and ProjectMemoryService to build * startup briefings and persist session summaries. * * @example * ```typescript * const sessionMemory = SessionMemoryService.getInstance(); * await sessionMemory.onSessionStart('dev-001', 'developer', '/projects/app'); * * // ... agent works ... * * await sessionMemory.onSessionEnd('dev-001', 'developer', '/projects/app', 'Implemented auth'); * ``` */ export declare class SessionMemoryService { private static instance; private readonly logger; /** * Creates a new SessionMemoryService instance * * Private constructor -- use {@link getInstance} to obtain the singleton. */ private constructor(); /** * Gets the singleton instance of SessionMemoryService * * @returns The singleton SessionMemoryService instance */ static getInstance(): SessionMemoryService; /** * Clears the singleton instance (useful for testing) */ static clearInstance(): void; /** * Returns the absolute path to the Crewly home directory (~/.crewly) * * @returns Absolute path to Crewly home */ private getAgentmuxHome; /** * Returns the sessions archive directory for a given agent * * @param agentId - The agent's unique identifier * @returns Absolute path to the agent's sessions directory * * @example * ```typescript * // Returns: ~/.crewly/agents/dev-001/sessions * this.getSessionsDir('dev-001'); * ``` */ private getSessionsDir; /** * Safely reads a file and returns its contents as a string, or null if the * file does not exist or cannot be read. * * @param filePath - Absolute path to the file * @returns File contents or null on any error */ private safeReadFile; /** * Truncates a string to a maximum number of characters, appending an * ellipsis marker when truncation occurs. * * @param text - The input string * @param maxChars - Maximum number of characters to retain * @returns Truncated string (or original if within limit) */ private truncate; /** * Called when an agent session starts * * Initializes both agent-level and project-level memory stores (if not * already initialized) and updates the project agents index so that the * orchestrator knows which agents have worked on a project. * * @param agentId - Unique identifier for the agent * @param role - Agent's role (e.g., 'developer', 'qa', 'pm') * @param projectPath - Absolute path to the project the agent is working on * * @example * ```typescript * await sessionMemory.onSessionStart('backend-dev-001', 'developer', '/home/user/project'); * ``` */ onSessionStart(agentId: string, role: string, projectPath: string): Promise; /** * Called when an agent session ends * * Persists a timestamped session summary markdown file under the agent's * sessions directory and also writes a `latest-summary.md` symlink-equivalent * so that the next startup briefing can quickly locate the most recent summary. * * @param agentId - Unique identifier for the agent * @param role - Agent's role * @param projectPath - Absolute path to the project * @param summary - Optional human-readable summary of what the agent accomplished * * @example * ```typescript * await sessionMemory.onSessionEnd( * 'backend-dev-001', * 'developer', * '/home/user/project', * 'Implemented user authentication with JWT tokens and added integration tests.', * ); * ``` */ onSessionEnd(agentId: string, role: string, projectPath: string, summary?: string): Promise; /** * Generates a startup briefing for an agent that is about to begin a session * * Aggregates context from multiple sources: * - The agent's most recent session summary * - Agent-level knowledge, preferences, and performance data * - Project-level patterns, decisions, and gotchas * - Today's daily log, active goals, and learning files * * All reads are best-effort: a missing file simply produces a null or empty * field rather than an error. * * @param agentId - Unique identifier for the agent * @param role - Agent's role * @param projectPath - Absolute path to the project * @returns A {@link StartupBriefing} containing all available context * * @example * ```typescript * const briefing = await sessionMemory.generateStartupBriefing( * 'backend-dev-001', * 'developer', * '/home/user/project', * ); * const markdown = sessionMemory.formatBriefingAsMarkdown(briefing); * ``` */ generateStartupBriefing(agentId: string, role: string, projectPath: string): Promise; /** * Formats a {@link StartupBriefing} into a markdown string suitable for * prompt injection * * Only includes sections that have non-null, non-empty content. Long * sections are truncated to keep the prompt within reasonable limits. * * @param briefing - The startup briefing to format * @returns A markdown string ready for injection into the agent's system prompt * * @example * ```typescript * const briefing = await sessionMemory.generateStartupBriefing(id, role, path); * const md = sessionMemory.formatBriefingAsMarkdown(briefing); * // md is a string like "## Your Previous Knowledge\n\n### Last Session\n..." * ``` */ formatBriefingAsMarkdown(briefing: StartupBriefing): string; /** * Updates the project-level agents index with an agent's latest activity * * The agents index lives at `{projectPath}/.crewly/agents-index.json` and * tracks every agent that has worked on the project, along with the timestamp * of their most recent activity. * * If the agent already exists in the index, only the `lastActive` and `role` * fields are updated. Otherwise a new entry is appended. * * @param projectPath - Absolute path to the project * @param agentId - The agent's unique identifier * @param role - Agent's role * * @throws Rethrows file I/O errors other than ENOENT * * @example * ```typescript * await sessionMemory.updateAgentsIndex('/home/user/project', 'dev-001', 'developer'); * ``` */ updateAgentsIndex(projectPath: string, agentId: string, role: string): Promise; } //# sourceMappingURL=session-memory.service.d.ts.map