UNPKG

@code-yeongyu/senpi

Version:

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

94 lines • 4 kB
import { getLatestTodoStateFromBranchEntries } from "../todotools/todo-storage.js"; const MAX_TEXT_CHARS = 1500; const MAX_FILES = 20; const MAX_TODOS = 20; const MAX_TODO_CHARS = 200; const PATH_TOOLS = new Set(["read", "edit", "write", "multiedit", "ls"]); const PATCH_FILE_LINE = /^\*\*\* (?:Update|Add|Delete) File: (.+)$/gm; function trim(text, max = MAX_TEXT_CHARS) { const flat = text.trim(); return flat.length > max ? `${flat.slice(0, max)}...` : flat; } function textOf(content) { if (typeof content === "string") return content; if (!Array.isArray(content)) return ""; return content .filter((part) => part?.type === "text" && typeof part.text === "string") .map((part) => part.text) .join("\n"); } function pathsOfCall(name, args) { if (typeof args !== "object" || args === null) return []; const record = args; if (PATH_TOOLS.has(name)) { const path = record.path ?? record.file_path; return typeof path === "string" ? [path] : []; } const patch = record.patch ?? record.input; if (name === "apply_patch" && typeof patch === "string") { return [...patch.matchAll(PATCH_FILE_LINE)].map((match) => match[1].trim()); } return []; } function touchedFiles(entries) { const seen = new Set(); for (let index = entries.length - 1; index >= 0 && seen.size < MAX_FILES; index -= 1) { const entry = entries[index]; if (entry.type !== "message" || entry.message.role !== "assistant") continue; const parts = Array.isArray(entry.message.content) ? entry.message.content : []; for (const part of [...parts].reverse()) { if (part.type !== "toolCall") continue; for (const path of pathsOfCall(part.name, part.arguments)) { if (seen.size >= MAX_FILES) break; seen.add(path); } } } return [...seen]; } function lastText(entries, role) { for (let index = entries.length - 1; index >= 0; index -= 1) { const entry = entries[index]; if (entry.type !== "message" || entry.message.role !== role) continue; const text = textOf(entry.message.content); if (text.trim().length > 0) return trim(text); } return ""; } const STATUS_LABEL = { in_progress: "in progress", pending: "open" }; export function buildHandoffBrief(input) { const lines = [ `Handoff from an earlier session${input.previousSessionFile ? ` (${input.previousSessionFile})` : ""}.`, `That session had grown to about ${Math.round(input.contextTokens / 1000)}k tokens, so only this brief carries over. Read the old session file if you need more detail.`, ]; if (input.goal && input.goal.status === "active") lines.push("", `Goal: ${trim(input.goal.objective)}`); const open = getLatestTodoStateFromBranchEntries(input.entries).phases.flatMap((phase) => phase.tasks .filter((task) => task.status === "in_progress" || task.status === "pending") .map((task) => `- [${STATUS_LABEL[task.status]}] ${phase.name}: ${trim(task.content, MAX_TODO_CHARS)}`)); if (open.length > 0) { lines.push("", "Open todos:", ...open.slice(0, MAX_TODOS)); if (open.length > MAX_TODOS) lines.push(`- and ${open.length - MAX_TODOS} more`); } const ask = lastText(input.entries, "user"); if (ask) lines.push("", "Last request:", ask); const answer = lastText(input.entries, "assistant"); if (answer) lines.push("", "Last answer:", answer); const files = touchedFiles(input.entries); if (files.length > 0) lines.push("", "Files touched, most recent first:", ...files.map((path) => `- ${path}`)); lines.push("", `Working directory: ${input.cwd}${input.modelLabel ? `. Model: ${input.modelLabel}` : ""}.`); return lines.join("\n"); } //# sourceMappingURL=brief.js.map