// // SummaryPrompt.swift // Museion // // #208: the AI summary — a first-person teaser a fresh one-shot engine // on the museion profile drafts from the open piece. The prompt and the // reply cleanup live here, pure; the model half is PostsModel.summarize // (PostsModelMicropubSummary.swift). The summary's home is the // frontmatter `summary:` line the SEND stamps — this half only drafts // text for the publish sheet's field. // import Foundation /// #208: the AI summary's prompt vocabulary — the title and body travel /// inline, the ask is a first-person teaser, and the reply is the /// summary text and nothing else. nonisolated enum SummaryPrompt { /// The ask: a first-person teaser that entices the reader to read /// the full piece. Title and body travel inline; the reply is the /// summary text and nothing else. static func prompt(title: String, body: String) -> String { "Summary — a first-person teaser for the piece below.\n\n" + "Title: \(title)\n\n\(body)\n\n" + "Write a short summary in first person that entices the reader to read the full piece. " + "Reply with the summary text only — no preamble, no quotes, no markdown heading." } /// The engine's reply as the field's text: trimmed; a reply that /// wraps its answer in a leading label ("Summary: …") drops the /// label; surrounding double quotes strip; "" when blank. Internal /// newlines collapse to one space through the codec's own /// `summaryValue` — the sink is line-bounded and the FIELD shows /// one line too. static func cleaned(_ reply: String) -> String { var text = reply.trimmingCharacters(in: .whitespacesAndNewlines) // ONE leading label, case-insensitive; the colon is required so a // reply that merely opens with the word "Summary" keeps it. if let label = text.range(of: #"^summary:\s*"#, options: [.regularExpression, .caseInsensitive]) { text.removeSubrange(label) } // ONE pair of surrounding double quotes — a WRAPPER, not prose: a // reply whose inner text still holds a `"` is a teaser that opens // and closes on quoted words, and its edge quotes are the writer's. if Self.isQuotedWrapper(text) { text = String(text.dropFirst().dropLast()) } // The codec's round-trip, not just its render: `summaryValue` // quotes a quote-bearing value for the line-bounded sink, and // the raw-line read gives it back — so the FIELD shows exactly // the text the wire will carry (no escapes ever reach the eyes). // Fenced: the read takes the block `split` finds, like disk. return PostFrontmatter.summary(inText: "---\nsummary: \(PostFrontmatter.summaryValue(text))\n---\n") } /// Edge quotes around text that holds no other `"` — a WRAPPER the /// model put around its reply, not a teaser opening on a quoted word. private static func isQuotedWrapper(_ text: String) -> Bool { text.count >= 2 && text.hasPrefix("\"") && text.hasSuffix("\"") && !text.dropFirst().dropLast().contains("\"") } }