//! `project.conversation` — the phone opening a project's standing//! conversation, or finding the one that is already there.//!//! **A METHOD and not a tool**, for `session.create`'s reason exactly: what it//! does is put a session on one of the owner's Macs, and starting an agent on//! somebody's Mac is the OWNER's own act. The assistant-facing form of the same//! write is `open_project_conversation`, which a session already inside the//! household may call; this is the phone's, through the same `Ctx`, the same//! writer and the same outbox as every other phone write.//!//! **THE WRITE IS `ensure_conversation`'s AND NOBODY ELSE'S**//! (`cotton_tools::session_birth`, Cotton note "The project conversation — one standing thread per project").//! This module is the METHOD: it turns the phone's `slug` into the project's//! id, wraps the refusal in a `MethodError`, and hands the write over. The//! conversation's id is DERIVED from the project, so a phone and a Mac opening//! one in the same minute arrive at the same row rather than at a rejection.//!//! **IDEMPOTENT, and the phone leans on it.** The project page asks for the//! conversation when the composer is first used, so the answer for a project//! that already has one must be that row and not a second — which is what//! `created: false` says.use serde_json::{Map, Value};use cotton_tools::mcp::dispatch::Ctx;use cotton_tools::mcp::handlers::common::maybe_row;use cotton_tools::mcp::read_db::Query;use cotton_tools::session_birth::ensure_conversation;usecrate::handle::CottonHandle;usecrate::methods::MethodError;/// The method's name on the boundary.pubconst METHOD: &str = "project.conversation";/// `project.conversation {slug}` — the project's conversation, opened if this/// is the first time anyone asked.////// Answers `{ id, state, kind, created }`: the row id every screen addresses,/// the state a card draws (`queued` the moment it is opened, `ended` between/// two messages — never a final state), and whether this call was the one that/// opened it.pubfn conversation(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {let slug = request .get("slug") .and_then(Value::as_str) .map(str::trim) .filter(|slug| !slug.is_empty()) .ok_or_else(|| MethodError::new("bad_request", "a conversation needs `slug`".to_string()))? .to_string(); handle .client() .write_rows(METHOD, &mut |ctx| write_conversation(ctx, &slug)) .map(crate::authoring::own_answer) .map_err(|reason| MethodError::new("write_refused", reason))}/// The write itself, so a test can drive it over a `Ctx` without a handle.////// The SLUG is resolved here rather than inside `ensure_conversation`, which/// takes the project's id: a slug is what a screen and a deep link hold, an id/// is what the row carries, and one translation in one place is the whole of/// what this layer adds.pubfn write_conversation(ctx: &mutCtx<'_>, slug: &str) -> Result<Value, String> {let project = maybe_row( ctx, &Query::on("cotton_memory_projects") .columns(&["id", "slug"]) .eq("slug", slug.to_string()), ) .map_err(|failure| failure.to_string())? .ok_or_else(|| format!("no project with slug {slug}"))?;let project_id = project .get("id") .and_then(Value::as_str) .unwrap_or_default() .to_string(); ensure_conversation(ctx, &project_id)}/// The conversation a project already has, for a READ to carry.////// `None` for a project nobody has opened one on, which is the honest answer/// and what the project page draws its composer's empty state from. It never/// writes: a page being LOOKED at must not put a session on a Mac.////// **A FAILURE IS CARRIED, WITH ITS SENTENCE AND ITS MAC.** `state = failed`/// alone would leave the page drawing the owner's message as waiting for/// ever; `error` is what the claim wrote when it could not start the agent/// and `machine` is where — the two facts a person needs to read the page as/// "it did not start, and here is why" rather than "it is still coming".pubfn conversation_of( driver: &cotton_store::driver::Driver, project_id: &str,) -> Result<Option<Value>, String> {let row = driver .get("SELECT id, state, last_active_at, error, machine FROM cotton_sessions \ WHERE project_id = ? AND kind = 'conversation'", &[rusqlite::types::Value::Text(project_id.to_string())], ) .map_err(|error| error.to_string())?;Ok(row.map(|row| {letmut out = Map::new();for (key, column) in [ ("id", "id"), ("state", "state"), ("lastActiveAt", "last_active_at"), ("error", "error"), ("machine", "machine"), ] { out.insert( key.to_string(), row.text(column).map_or(Value::Null, Value::from), ); }Value::Object(out) }))}#[cfg(test)]#[path = "conversation_tests.rs"]mod tests;