/** * Shared utilities for compaction and branch summarization. */ import type { AgentMessage } from "@f5-sales-demo/pi-agent-core"; import type { Message } from "@f5-sales-demo/pi-ai"; import { prompt } from "@f5-sales-demo/pi-utils"; import fileOperationsTemplate from "../../prompts/system/file-operations.md" with { type: "text" }; import summarizationSystemPrompt from "../../prompts/system/summarization-system.md" with { type: "text" }; // ============================================================================ // File Operation Tracking // ============================================================================ export interface FileOperations { read: Set; written: Set; edited: Set; } export function createFileOps(): FileOperations { return { read: new Set(), written: new Set(), edited: new Set(), }; } /** * Extract file operations from tool calls in an assistant message. */ export function extractFileOpsFromMessage(message: AgentMessage, fileOps: FileOperations): void { if (message.role !== "assistant") return; if (!("content" in message) || !Array.isArray(message.content)) return; for (const block of message.content) { if (typeof block !== "object" || block === null) continue; if (!("type" in block) || block.type !== "toolCall") continue; if (!("arguments" in block) || !("name" in block)) continue; const args = block.arguments as Record | undefined; if (!args) continue; const path = typeof args.path === "string" ? args.path : undefined; if (!path) continue; switch (block.name) { case "read": fileOps.read.add(path); break; case "write": fileOps.written.add(path); break; case "edit": fileOps.edited.add(path); break; } } } /** * Compute final file lists from file operations. * Returns readFiles (files only read, not modified) and modifiedFiles. */ export function computeFileLists(fileOps: FileOperations): { readFiles: string[]; modifiedFiles: string[] } { const modified = new Set([...fileOps.edited, ...fileOps.written]); const readOnly = [...fileOps.read].filter(f => !modified.has(f)).sort(); const modifiedFiles = [...modified].sort(); return { readFiles: readOnly, modifiedFiles }; } /** * Format file operations as XML tags for summary. */ const FILE_OPERATION_SUMMARY_LIMIT = 20; function truncateFileList(files: string[]): string[] { if (files.length <= FILE_OPERATION_SUMMARY_LIMIT) return files; const omitted = files.length - FILE_OPERATION_SUMMARY_LIMIT; return [...files.slice(0, FILE_OPERATION_SUMMARY_LIMIT), `… (${omitted} more files omitted)`]; } function stripFileOperationTags(summary: string): string { const withoutReadFiles = summary.replace(/[\s\S]*?<\/read-files>\s*/g, ""); const withoutModifiedFiles = withoutReadFiles.replace(/[\s\S]*?<\/modified-files>\s*/g, ""); return withoutModifiedFiles.trimEnd(); } export function formatFileOperations(readFiles: string[], modifiedFiles: string[]): string { if (readFiles.length === 0 && modifiedFiles.length === 0) return ""; return prompt.render(fileOperationsTemplate, { readFiles: truncateFileList(readFiles), modifiedFiles: truncateFileList(modifiedFiles), }); } export function upsertFileOperations(summary: string, readFiles: string[], modifiedFiles: string[]): string { const baseSummary = stripFileOperationTags(summary); const fileOperations = formatFileOperations(readFiles, modifiedFiles); if (!fileOperations) return baseSummary; if (!baseSummary) return fileOperations; return `${baseSummary}\n\n${fileOperations}`; } // ============================================================================ // Message Serialization // ============================================================================ /** Maximum characters for a tool result in serialized summaries. */ const TOOL_RESULT_MAX_CHARS = 2000; /** * Truncate text to a maximum character length for summarization. * Keeps the beginning and appends a truncation marker. */ function truncateForSummary(text: string, maxChars: number): string { if (text.length <= maxChars) return text; const truncatedChars = text.length - maxChars; return `${text.slice(0, maxChars)}\n\n[... ${truncatedChars} more characters truncated]`; } /** * Serialize LLM messages to text for summarization. * This prevents the model from treating it as a conversation to continue. * Call convertToLlm() first to handle custom message types. */ export function serializeConversation(messages: Message[]): string { const parts: string[] = []; for (const msg of messages) { if (msg.role === "user") { const content = typeof msg.content === "string" ? msg.content : msg.content .filter((c): c is { type: "text"; text: string } => c.type === "text") .map(c => c.text) .join(""); if (content) parts.push(`[User]: ${content}`); } else if (msg.role === "assistant") { const textParts: string[] = []; const thinkingParts: string[] = []; const toolCalls: string[] = []; for (const block of msg.content) { if (block.type === "text") { textParts.push(block.text); } else if (block.type === "thinking") { thinkingParts.push(block.thinking); } else if (block.type === "toolCall") { const args = block.arguments as Record; const argsStr = Object.entries(args) .map(([k, v]) => `${k}=${JSON.stringify(v)}`) .join(", "); toolCalls.push(`${block.name}(${argsStr})`); } } if (thinkingParts.length > 0) { parts.push(`[Assistant thinking]: ${thinkingParts.join("\n")}`); } if (textParts.length > 0) { parts.push(`[Assistant]: ${textParts.join("\n")}`); } if (toolCalls.length > 0) { parts.push(`[Assistant tool calls]: ${toolCalls.join("; ")}`); } } else if (msg.role === "toolResult") { const content = msg.content .filter((c): c is { type: "text"; text: string } => c.type === "text") .map(c => c.text) .join(""); if (content) { parts.push(`[Tool result]: ${truncateForSummary(content, TOOL_RESULT_MAX_CHARS)}`); } } } return parts.join("\n\n"); } // ============================================================================ // Summarization System Prompt // ============================================================================ export const SUMMARIZATION_SYSTEM_PROMPT = prompt.render(summarizationSystemPrompt);