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