feat(memory): thread summary and PA prompt through compaction result
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { Message } from '../models/types.js';
|
||||
import type { AgentOrchestrator } from '../backends/native/orchestrator.js';
|
||||
import type { MemoryStore } from '../memory/store.js';
|
||||
import { COMPACTION_SYSTEM_PROMPT, MEMORY_EXTRACTION_PROMPT } from '../backends/native/prompts.js';
|
||||
import { COMPACTION_SYSTEM_PROMPT, MEMORY_EXTRACTION_PROMPT, buildCompactionPrompt } from '../backends/native/prompts.js';
|
||||
import { estimateMessageTokens } from './tokens.js';
|
||||
import { getMessageText } from '../models/media.js';
|
||||
import { selectImportantMessages } from './weighting.js';
|
||||
@@ -43,6 +43,8 @@ export interface CompactionResult {
|
||||
tokensBefore: number;
|
||||
/** Estimated tokens after compaction. */
|
||||
tokensAfter: number;
|
||||
/** The raw summary text produced by the compaction model (populated when compaction ran). */
|
||||
summary?: string;
|
||||
}
|
||||
|
||||
export const DEFAULT_COMPACTION_CONFIG: CompactionConfig = {
|
||||
@@ -66,6 +68,8 @@ export async function compactHistory(opts: {
|
||||
config: CompactionConfig;
|
||||
memoryStore?: MemoryStore;
|
||||
autoExtract?: boolean;
|
||||
usePersonalAssistantPrompt?: boolean;
|
||||
memoryExtractionNamespace?: string;
|
||||
}): Promise<CompactionResult> {
|
||||
const { messages, orchestrator, config } = opts;
|
||||
|
||||
@@ -116,10 +120,14 @@ export async function compactHistory(opts: {
|
||||
|
||||
const tier = orchestrator.getDelegationTier('compaction');
|
||||
|
||||
const systemPrompt = opts.usePersonalAssistantPrompt
|
||||
? buildCompactionPrompt({ personalAssistant: true })
|
||||
: COMPACTION_SYSTEM_PROMPT;
|
||||
|
||||
const result = await orchestrator.delegate({
|
||||
task: 'compaction',
|
||||
tier,
|
||||
systemPrompt: COMPACTION_SYSTEM_PROMPT,
|
||||
systemPrompt,
|
||||
message: formattedConversation,
|
||||
maxTokens: config.summaryMaxTokens,
|
||||
});
|
||||
@@ -144,8 +152,9 @@ export async function compactHistory(opts: {
|
||||
// Only write if the extraction produced meaningful content
|
||||
const extractedContent = extraction.content.trim();
|
||||
if (extractedContent.length > 0 && !extractedContent.toLowerCase().includes('no facts')) {
|
||||
opts.memoryStore.write('global', extractedContent, 'append');
|
||||
console.log(`[Flynn:memory] Extracted ${extractedContent.length} chars of facts to global memory`);
|
||||
const extractionNs = opts.memoryExtractionNamespace ?? 'global';
|
||||
opts.memoryStore.write(extractionNs, extractedContent, 'append');
|
||||
console.log(`[Flynn:memory] Extracted ${extractedContent.length} chars of facts to ${extractionNs} memory`);
|
||||
}
|
||||
} catch (error) {
|
||||
// Memory extraction is best-effort — don't fail compaction if it errors
|
||||
@@ -158,5 +167,6 @@ export async function compactHistory(opts: {
|
||||
compactedCount: toSummarize.length,
|
||||
tokensBefore: estimateMessageTokens(messages),
|
||||
tokensAfter: estimateMessageTokens([...preservedMessages, summaryMessage, ...toKeep]),
|
||||
summary: result.content,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user