memory-session-summary.mjs
59 lines 2.1 KB
Raw
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge Human 11 hours ago
1 /**
2 * Session summary generation: LLM-powered summarization of recent memory events
3 * into a single session_summary event.
4 *
5 * Uses completeChat (OpenAI → Anthropic → Ollama fallback chain).
6 */
7
8 import { createMemoryManager } from './memory.mjs';
9 import { completeChat } from './llm-complete.mjs';
10
11 const SYSTEM_PROMPT = `You are a session summarizer for a personal knowledge vault system called Knowtation. Given a log of recent agent/user activity events, produce a concise summary covering:
12 1. What was accomplished (searches, writes, exports, imports, index operations)
13 2. Key topics and queries explored
14 3. Decisions made or pending
15 4. Suggested next steps
16
17 Be concise and factual. Focus on actionable takeaways. Output plain text, no markdown headings.`;
18
19 /**
20 * Generate a session summary from recent memory events and store it.
21 * @param {object} config — loadConfig() result
22 * @param {{ since?: string, limit?: number, maxTokens?: number, dryRun?: boolean }} [opts]
23 * @returns {Promise<{ summary: string, event_count: number, id?: string, ts?: string }>}
24 */
25 export async function generateSessionSummary(config, opts = {}) {
26 const mm = createMemoryManager(config);
27 const since = opts.since || new Date(Date.now() - 86_400_000).toISOString();
28 const limit = opts.limit ?? 50;
29
30 const events = mm.list({ since, limit });
31 if (events.length === 0) {
32 return { summary: 'No events to summarize.', event_count: 0 };
33 }
34
35 const eventLines = events.map((e) => {
36 const summary = JSON.stringify(e.data).slice(0, 300);
37 return `[${e.ts}] ${e.type}: ${summary}`;
38 });
39
40 const userPrompt = `Here are ${events.length} recent activity events from the knowledge vault:\n\n${eventLines.join('\n')}\n\nSummarize this session.`;
41
42 const summary = await completeChat(config, {
43 system: SYSTEM_PROMPT,
44 user: userPrompt,
45 maxTokens: opts.maxTokens ?? 512,
46 });
47
48 if (opts.dryRun) {
49 return { summary, event_count: events.length };
50 }
51
52 const result = mm.store('session_summary', {
53 summary_text: summary,
54 event_count: events.length,
55 since,
56 });
57
58 return { summary, event_count: events.length, id: result.id, ts: result.ts };
59 }
File History 1 commit
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge Human 11 hours ago