// // PostsModelMicropubSummary.swift // Museion // // SummaryPrompt.swift's model half — #208: PostsModel.summarize drafts // a first-person summary of the OPEN piece on a fresh one-shot // chat-profile engine and hands the text to the caller. The write is // NOT here — the send stamps the frontmatter `summary:` line // (PostFrontmatter, slice 1). // import Foundation @MainActor extension PostsModel { /// #208: draft a first-person summary of the OPEN piece on a fresh /// one-shot chat-profile engine. Flush-first (the engine reads /// disk); the guards are the shared open-piece rail's. Returns the /// cleaned reply, or nil with `lastError` set naming the blocker /// (nothing running / not ready / no open piece / the engine said /// nothing). Writes NOTHING — the text is the caller's to place. /// `summarizing` is live while the turn runs: the pass bar's Cancel /// ends it (the Blind Read posture) and a Pass refuses meanwhile. func summarize(mentor: MentorSessionModel) async -> String? { guard let (id, _, home) = guardOpenPieceRun("a summary", mentor: mentor) else { return nil } guard flushOrAbort(id) else { return nil } // House posture (blindRead): the run is firing — a stale guard // refusal no longer owns the error strip. lastError = nil // brief-28: the guard answered the session's OWN library; disk is // the truth post-flush — ONE read feeds the prompt AND the title // (the scan snapshot can lag a just-flushed heading). guard let dir = PostStore.folder(of: id, home: home), let raw = try? String(contentsOf: dir.appendingPathComponent("post.md"), encoding: .utf8) else { lastError = "Couldn't read the piece before summarizing." return nil } let body = PostFrontmatter.split(raw).body guard !body.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { lastError = "This piece has no text to summarize." return nil } summarizing = true defer { summarizing = false } let pieceTitle = Post.title(of: raw) let reply = await mentor.runSummary( prompt: SummaryPrompt.prompt(title: pieceTitle, body: body), pieceTitle: pieceTitle, ) guard let summary = reply.map(SummaryPrompt.cleaned), !summary.isEmpty else { lastError = "The engine didn't return a summary — try again." return nil } return summary } }