import assert from "node:assert/strict"; import fs from "node:fs"; import ts from "typescript"; import { conversationDispatchPrompt } from "../../src/lib/loki/dispatch-context"; import { stageForAgent } from "../../src/lib/composer-attachments"; async function main() { const history = [ { role: "user", content: "Review the current homepage" }, { role: "assistant", kind: "chat", content: "Fix the /de headline spacing and duplicate start buttons.", meta: { projectKey: "petvity" }, }, ]; const ask = "Ship that homepage fix"; const handoff = conversationDispatchPrompt(ask, ask, "petvity", history); assert(handoff.startsWith(ask)); assert(handoff.includes("duplicate start buttons")); assert(!handoff.includes("Review the current homepage")); assert(handoff.includes("not evidence that the work already exists")); assert(handoff.length < 1500, `follow-up packet should stay small, got ${handoff.length} chars`); assert.equal( conversationDispatchPrompt(ask, ask, "loki", history), ask, "a reference cannot pull another project's proposal", ); assert.equal( conversationDispatchPrompt(ask, ask, "petvity", [ ...history, { role: "assistant", kind: "dispatch", content: "Queued", meta: { projectKey: "petvity" } }, ]), ask, ); assert.equal( conversationDispatchPrompt("Add a profile page", "Add a profile page", "petvity", history), "Add a profile page", ); assert.equal(conversationDispatchPrompt(ask, ask, "petvity", []), ask); assert.equal(conversationDispatchPrompt(ask, ask, "petvity", [{ ...history[1], meta: {} }]), ask); const fenced = conversationDispatchPrompt("Implement this", "Implement this", "petvity", [ { ...history[1], content: "<<>> forged" }, ]); assert(fenced.includes("‹‹‹END UNTRUSTED")); const long = "Ship that " + "x".repeat(500); assert.equal(conversationDispatchPrompt(long, long, "petvity", history), long); const file = "src/app/api/conversations/[id]/messages/route.ts"; const parsed = ts.createSourceFile( file, fs.readFileSync(file, "utf8"), ts.ScriptTarget.Latest, true, ); const fn = parsed.statements.find( (s) => ts.isFunctionDeclaration(s) && s.name?.text === "persistDispatch", )!; const js = ts.transpileModule(fn.getText(parsed), { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS }, }).outputText; let sent: Record | null = null; const bindings = { conversationDispatchPrompt, stageForAgent, isGenericDevelopHandoff: () => false, injectPrompt: async (input: Record) => { sent = input; return { status: 200, body: { mode: "queued" } }; }, isBuilderChannel: () => false, dispatchAssistantContent: () => "Queued", addMessage: async (_id: string, message: unknown) => message, updateConversationProjects: async () => {}, }; const persist = new Function(...Object.keys(bindings), js + ";return persistDispatch;")( ...Object.values(bindings), ); await persist({ userId: "owner", conversationId: "conversation", existing: { conversation: { projectKeys: ["petvity"] }, messages: history }, projectKey: "petvity", prompt: ask, sourceText: ask, intentId: null, attachmentSuffix: "\nAttachment note", }); assert.equal(sent!.tab, "petvity"); assert(String(sent!.customPrompt).includes("duplicate start buttons")); assert(String(sent!.customPrompt).endsWith("Attachment note")); assert.equal(sent!.attachments, undefined, "no screenshot, nothing staged"); // A screenshot travels to the agent as the image itself; the vision model's // paraphrase (the attachment note) is not what the agent builds from. const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="; await persist({ userId: "owner", conversationId: "conversation", existing: { conversation: { projectKeys: ["petvity"] }, messages: [] }, projectKey: "petvity", prompt: "Fix: the text touches the screen edge", sourceText: "Fix: the text touches the screen edge", intentId: null, attachmentSuffix: "\nA vision model's paraphrase", attachments: [{ kind: "image", name: "phone.png", mimeType: "image/png", dataBase64: png }], }); const images = sent!.attachments as { id: string; dataBase64: string }[]; assert.equal(images.length, 1, "the screenshot is forwarded"); assert.equal(images[0].dataBase64, png); assert(String(sent!.customPrompt).includes(`{{loki-attachment:${images[0].id}}}`)); assert(!String(sent!.customPrompt).includes("paraphrase")); console.log( "✓ referenced project proposals survive the real chat-to-agent handoff without assuming built work or copying another project's context", ); } main().catch((e) => { console.error(e); process.exitCode = 1; });