UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

175 lines (174 loc) • 6.93 kB
/** * Shared utilities for compaction and branch summarization. */ import { contentText, } from "@earendil-works/pi-ai"; import { extractPatchedPaths } from "../extensions/builtin/gpt-apply-patch/index.js"; export function createFileOps() { return { read: new Set(), written: new Set(), edited: new Set(), }; } /** * Extract file operations from tool calls in an assistant message, or from the nested calls * recorded on a tool result. */ export function extractFileOpsFromMessage(message, fileOps) { if (message.role === "toolResult") { // Calls made from codemode scripts are recorded on the script's result. for (const call of message.nestedCalls?.calls ?? []) addFileOp(call.name, call.arguments, fileOps); return; } 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; addFileOp(block.name, block.arguments, fileOps); } } function addFileOp(toolName, args, fileOps) { if (!args) return; switch (toolName) { case "read": if (typeof args.path === "string") fileOps.read.add(args.path); break; case "write": if (typeof args.path === "string") fileOps.written.add(args.path); break; case "edit": if (typeof args.path === "string") fileOps.edited.add(args.path); break; case "apply_patch": if (typeof args.input !== "string") return; for (const patchedPath of extractPatchedPaths(args.input)) { fileOps.edited.add(patchedPath); } break; } } /** * Compute final file lists from file operations. * Returns readFiles (files only read, not modified) and modifiedFiles. */ export function computeFileLists(fileOps) { 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. */ export function formatFileOperations(readFiles, modifiedFiles) { const sections = []; if (readFiles.length > 0) { sections.push(`<read-files>\n${readFiles.join("\n")}\n</read-files>`); } if (modifiedFiles.length > 0) { sections.push(`<modified-files>\n${modifiedFiles.join("\n")}\n</modified-files>`); } if (sections.length === 0) return ""; return `\n\n${sections.join("\n\n")}`; } // ============================================================================ // 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, maxChars) { if (text.length <= maxChars) return text; const truncatedChars = text.length - maxChars; return `${text.slice(0, maxChars)}\n\n[... ${truncatedChars} more characters truncated]`; } function isPortableSummaryContentBlock(block) { return block.type !== "providerNative"; } /** * Extract text from content that may retain provider-native replay blocks. * * Provider-native blocks must remain on the original assistant message for * same-provider replay, but are not part of pi-ai's portable `contentText` * contract. Normalize a filtered copy for summarization instead of casting or * mutating the persisted message. */ export function contentTextForSummary(content, separator = "\n") { if (typeof content === "string") return content; return contentText(content.filter(isPortableSummaryContentBlock), separator); } /** * 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. * * Tool results are truncated to keep the summarization request within * reasonable token budgets. Full content is not needed for summarization. */ export function serializeConversation(messages) { const parts = []; for (const msg of messages) { if (msg.role === "user") { const content = contentTextForSummary(msg.content, ""); if (content) parts.push(`[User]: ${content}`); } else if (msg.role === "assistant") { const thinkingParts = []; const toolCalls = []; for (const block of msg.content) { if (block.type === "thinking") { thinkingParts.push(block.thinking); } else if (block.type === "toolCall") { const args = block.arguments; 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 (msg.content.some((block) => block.type === "text")) { parts.push(`[Assistant]: ${contentTextForSummary(msg.content)}`); } if (toolCalls.length > 0) { parts.push(`[Assistant tool calls]: ${toolCalls.join("; ")}`); } } else if (msg.role === "toolResult") { const content = contentTextForSummary(msg.content, ""); 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 = `You are a context summarization assistant. Your task is to read a conversation between a user and an AI assistant, then produce a structured summary following the exact format specified. Do NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary.`; //# sourceMappingURL=utils.js.map