memory-session-summary.mjs
59 lines 2.1 KB
Raw
sha256:c2dbf04d56308f3bbf2d06e6d2eb022b8948b1e827195fe525a44e5e18d5f9c0 feat(auth): Phase B Connect cloud agent (RFC 8628) + Hermes… Human minor ⚠ breaking 11 days 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:93bcf8f9bd56d8c5b9339f4ec73b9ebd66571398d56262d38eedc2cfa9db9882 fix(test): align Band B landing assertion with desktop MCP … Human 11 days ago