Files
museion/Museion/SummaryPrompt.swift
T
AlexKucera 36393a9e8e feat(microblog): post summary for Micro.blog
A piece's `summary:` frontmatter line now travels to Micro.blog
beside `category:`. A first send carries the summary when it is not
empty; a re-send replaces the post's summary only when the piece has
the line (opt-in by key; an empty value clears it; no line leaves the
blog's). The same rule as categories, so a piece that never gained
the line cannot blank a summary set on Micro.blog itself.

The publish sheet gains a Summary field and an AI summary button. The
button drafts a first-person teaser from the open piece on a fresh
one-shot engine on the chat profile (the Blind Read's rail — the
resident session is never touched); the reply fills the field for the
writer to edit, and nothing is written until the send stamps the
line. The sheet is modal, so while its own turn runs the button is
the Stop; a Pass or Blind Read that owns the rail disables the button
with the reason on hover and refuses from behind it.

The summary is read off the RAW frontmatter line, never through the
lenient decode — which trims every edge quote — and the read walks
the block the way the lenient reader does. A `split`-based read finds
no block in a mixed-ending file while the lenient reader still sees
the key, and the empty value it answers would have been sent over a
live post's summary.

Reviewed twice (two-axis during the build, three-reviewer
finish-audit after): the mixed-ending read, an unfenced body line
claiming the key, the uncancellable modal turn, and the wrapper rule
eating a teaser's edge quotes were found and fixed before this
commit. Every guard was watched fail on its own pin. Rail 814/814;
SwiftFormat and SwiftLint clean.

Docs: AGENTS.md bullets and learnings, the GLOSSARY term, CHANGELOG
[Unreleased], PRD S6.1, and the devlog.

Closes #208
2026-12-05 12:32:38 +01:00

63 lines
2.9 KiB
Swift

//
// 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("\"")
}
}