import * as path from "node:path"; import type { ExtensionFactory } from "@oh-my-pi/pi-coding-agent"; export interface FieldNotesWriteBroker { /** Real host broker: resolve only after exact bytes are durable at the supplied destination. */ write(destination: string, content: string): Promise; } /** Host supplies the already-canonical allowlisted destination and a real broker. No elevated writer is bundled. */ export function createFieldNotesFallback(destination: string, broker: FieldNotesWriteBroker): ExtensionFactory { if (!path.isAbsolute(destination)) throw new Error("The host must supply an absolute, canonical destination."); return pi => { pi.registerFileWriteFallback(async (request, ctx) => { if (request.sessionId !== ctx.sessionManager.getSessionId() || request.dst !== destination) return false; await broker.write(request.dst, request.content); return true; }); }; }