//! Recipes — the prompt library (projects-first spec §D10). The port of //! `packages/core/src/recipes.ts`. //! //! ONE declaration, two surfaces: the server projects these onto the MCP wire //! (`prompts/list` and `prompts/get`, so Claude Code renders them as slash //! commands and claude.ai as templates), and Settings → Connected AIs renders //! the same list with a copy affordance for clients without prompt support. A //! second copy of the list is how the screen and the wire would come to //! disagree — which is why this lives in core rather than in the NODE-only //! server package the app can never import, and why it is in `cotton-core` //! here rather than in `cotton-server`. //! //! Language: plain software terms. The TypeScript's `toolLanguage.test.ts` //! sweeps every string here against `RETIRED_TERMS`, exactly as it sweeps the //! tool catalogue; that sweep comes over in full, because both halves of it //! now live in this crate. use std::sync::LazyLock; use regex::Regex; /// One `{{slot}}` a recipe declares. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct RecipeArgument { /// kebab-case-safe identifier; the `{{name}}` slot it fills in the body. pub name: &'static str, pub description: &'static str, /// What stands in when the argument is not given. Every argument is /// optional on the wire and in the app's copied text (the app has no /// argument form), so a fallback is REQUIRED — an argument-less /// `prompts/get` must still read as complete sentences, never as a literal /// `{{slot}}`. pub fallback: &'static str, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Recipe { /// kebab-case; the slash-command name a client renders. pub name: &'static str, pub title: &'static str, pub description: &'static str, pub arguments: &'static [RecipeArgument], /// The prompt itself. `{{argument}}` slots are filled by [`recipe_text`]. pub body: &'static str, } /// The library, in the order a client lists it. /// /// The seventh entry is the journal workstream's, taken verbatim from its spec /// (2026-09-02-cotton-journal-design.md §4) once `append_journal`'s `project` /// argument and `get_project`'s `journal.recent` landed — the two calls the /// body leans on. pub const RECIPES: [Recipe; 7] = [ Recipe { name: "start-session", title: "Start a session", description: "Load the briefing and the project's working context before doing anything else.", arguments: &[ RecipeArgument { name: "project", description: "The project's name or slug; leave it out to use whichever project the briefing matches to this repo.", fallback: "the project we are working on", }, ], body: r#"Start this session against Cotton. 1. Call `brief` with this machine's hostname and, if you are in a repo, its URL. Read it before assuming anything — it carries the owner's standing decisions. 2. Call `get_project` for {{project}} and read the working context it returns: the summary, the linked records, the open threads. 3. Check for open comment threads addressed to assistants, and for todos due today that belong to the project. 4. At the end of every turn, call `log_session` with a short recap and the prompts since the last log."#, }, Recipe { name: "harvest-project", title: "Harvest a project", description: "Pull every thread and attachment about a project from mail and chats, file them, and link them.", arguments: &[ RecipeArgument { name: "project", description: "The project's name or slug.", fallback: "the project we are working on", }, ], body: r#"Harvest everything about {{project}} into Cotton. 1. `get_project` for {{project}} — read what is already linked so you do not file it twice. 2. Search mail and chats for the project: `list_email_threads` and `search_messages` with the project's name, its people and its keywords; read what hits with `read_email_thread` and `read_message_thread`. 3. Link every thread that is genuinely about the project with `link_to_project`, each with a one-line note saying why it belongs. 4. File the attachments worth keeping as documents (`request_document_upload`, then `file_document`) and link those to the project too. 5. Report what you linked, what you filed, and what you skipped."#, }, Recipe { name: "summarize-person", title: "Summarize a person", description: "Refresh a person's summary from everything on record, and say what changed.", arguments: &[ RecipeArgument { name: "person", description: "The person's name.", fallback: "the person we are discussing", }, ], body: r#"Refresh the summary for {{person}}. 1. Find their record with `search_records`, then `get_record` — read the current summary and who wrote it. 2. Read what is newest: their notes, chats, email threads, money balances and shared projects. 3. Write the new summary with `update_summary` (kind `person`) — under 1,200 characters, the most useful facts first. Do not repeat structured fields the record already carries (organisation, role, city). 4. Tell me in one line what changed since the last summary."#, }, Recipe { name: "summarize-project", title: "Summarize a project", description: "Refresh a project's summary, and say what changed.", arguments: &[ RecipeArgument { name: "project", description: "The project's name or slug.", fallback: "the project we are working on", }, ], body: r#"Refresh the summary for {{project}}. 1. `get_project` — read the current summary, who wrote it, and every linked section. 2. Read what arrived since it was last written: threads, documents, todos, notes, sessions. 3. Write it back with `update_project` — what the project is, where it stands, what happens next; under 1,200 characters. 4. Tell me in one or two lines what changed since the previous summary."#, }, Recipe { name: "file-statements", title: "File statements", description: "File the bank statements sitting unread, and reconcile what they show.", arguments: &[], body: r#"File my bank statements. 1. `list_documents` — find statements that are unfiled or failed to read. 2. For each one: `get_document` to read its pages, then `file_document` against the right money account. 3. Compare the statement's period against `list_money_transactions`; record what the statement shows and the account is missing with `record_transaction`. 4. Report per statement: where it was filed, what was added, and anything you could not read."#, }, Recipe { name: "weekly-review", title: "Weekly review", description: "One pass over the week — calendar, todos, projects and money — ending in filed follow-ups.", arguments: &[], body: r#"Run my weekly review. 1. `today_agenda` and `list_events` for the coming week; `list_todos` for what is overdue and what is due soon. 2. `list_projects`, then `get_project` for each active project — note what moved and what stalled. 3. `list_records` for actual transaction rows in the month so far. 4. Give me one tight review: what happened, what is stuck, what is due, what needs a decision — then file the follow-ups as todos with due dates."#, }, Recipe { name: "project-journal", title: "Project journal", description: "At the end of a working day (or when asked), write the day's progress entry for a project.", arguments: &[ RecipeArgument { name: "project", description: "The project's name or slug.", fallback: "the project we are working on", }, ], body: r#"At the end of the working day, write the day's progress entry for {{project}}. 1. `get_project` {{project}}. Read `journal.recent` so today's entry continues the story instead of repeating it. 2. Gather the day: `list_sessions` for sessions naming the project, todos completed today (`list_todos`), and anything the person told you about the day's work (PRs merged, decisions taken, blockers hit). 3. Write 2–5 plain sentences, past tense: what landed, what was decided, what is blocked and on whom. Name PRs and records by their numbers/titles. Never pad, never speculate, never restate the project description. 4. `append_journal({ project: "", text })`. One entry per day — a second call the same day appends a paragraph to the same page, so append only what is new since the last one. 5. Never write to the owner's own journal (no `project` argument) unless the owner asked for exactly that. The owner's daily page is theirs."#, }, ]; /// `/\{\{([a-z][a-z0-9-]*)\}\}/g` — the slot grammar, spelled once. static SLOT: LazyLock = LazyLock::new(|| Regex::new(r"\{\{([a-z][a-z0-9-]*)\}\}").expect("the slot pattern compiles")); /// The recipe's text with its `{{argument}}` slots filled — the ONE renderer, /// used by the server's `prompts/get` and by the app's copy affordance. /// /// A given argument wins; an absent or blank one falls back to the recipe's own /// wording, so the result never carries a literal slot. `args` is a slice of /// pairs rather than a map because the TypeScript's `Record` makes no distinction a caller can act on — a missing key and an /// `undefined` value both fall back — and the first pair with a name wins, the /// way a JavaScript object literal's last duplicate would already have /// collapsed to one. /// /// Blank is JavaScript-blank: the value is trimmed against ECMAScript's /// whitespace class (U+FEFF in, U+0085 out), so a slot filled with a lone BOM /// falls back exactly as it does over there. pub fn recipe_text(recipe: &Recipe, args: &[(&str, &str)]) -> String { SLOT.replace_all(recipe.body, |caps: ®ex::Captures<'_>| { let name = &caps[1]; // An undeclared slot is a broken recipe; `body_slots_and_declared_arguments_agree` // asserts none exists, so this branch is unreachable rather than a // silent fallback. let Some(declared) = recipe.arguments.iter().find(|a| a.name == name) else { return caps[0].to_string(); }; let given = args .iter() .find(|(key, _)| *key == name) .map(|(_, value)| js_trim(value)) .unwrap_or(""); if given.is_empty() { declared.fallback.to_string() } else { given.to_string() } }) .into_owned() } /// ECMAScript's `String.prototype.trim`, not Rust's — the classes differ /// (U+FEFF is whitespace to JavaScript and not to Rust; U+0085 is the other way /// round), and this one decides whether a slot falls back. fn js_trim(text: &str) -> &str { text.trim_matches(cotton_store::protocol::js::is_js_whitespace) } #[cfg(test)] #[path = "recipes_tests.rs"] mod tests;