// The report file an agent writes (release 27, S5): `write_report`, // `edit_report` and `read_report` — the plan's Write/Edit tools, for the // iteration's sweep.md only. Every change is kept in the iterations store as // it happens, so a run cut short leaves its report as far as it got. import type { AgentTool } from "../../../llm"; import { writeIterationFile } from "../../iterations"; import type { StorePaths } from "./types"; export type ReportFile = { text(): string; set(md: string): Promise; tools: AgentTool[]; }; export function reportFile(o: { article: string; n: number; paths?: StorePaths; initial?: string; readOnly?: boolean }): ReportFile { let md = o.initial ?? ""; let written = md.length > 0; const save = async (next: string) => { md = next; written = true; await writeIterationFile(o.article, o.n, "sweep.md", md, o.paths); }; const tools: AgentTool[] = [ { name: "write_report", description: "Replace the whole report file (sweep.md) with this markdown.", inputSchema: { type: "object", properties: { markdown: { type: "string" } }, required: ["markdown"] }, run: async (input: { markdown?: unknown }) => { if (typeof input?.markdown !== "string" || !input.markdown.trim()) throw new Error("write_report needs the markdown"); await save(input.markdown); return `wrote sweep.md (${input.markdown.length} chars)`; }, }, { name: "edit_report", description: "Replace one exact, unique passage of the report with new text (old_text must occur once).", inputSchema: { type: "object", properties: { old_text: { type: "string" }, new_text: { type: "string" } }, required: ["old_text", "new_text"], }, run: async (input: { old_text?: unknown; new_text?: unknown }) => { const from = typeof input?.old_text === "string" ? input.old_text : ""; const to = typeof input?.new_text === "string" ? input.new_text : null; if (!from || to === null) throw new Error("edit_report needs old_text and new_text"); const count = md.split(from).length - 1; if (count !== 1) throw new Error(`old_text occurs ${count} times in the report — give a unique passage`); await save(md.replace(from, () => to)); return "edited"; }, }, { name: "read_report", description: "Read the report file as it is now.", inputSchema: { type: "object", properties: {} }, run: async () => (written ? md : "(no report yet)"), }, ]; return { text: () => md, set: save, tools: o.readOnly ? tools.filter((t) => t.name === "read_report") : tools, }; }