/** * A recording's summary by the person's own assistant (owner via the coordinator, 23 Nov 2026: Cotton has no model * of its own; assets/design/chat/DECISIONS.md "Recordings"). * * A recording (meetings_meeting, its meetings_speaker voices, meetings_line words, meetings_action to-dos) is sealed, * so these read and write inside Your Claude's grant, the Recordings group, as every sealed tool does: * * - `recordings_to_summarise`: the recordings with words and no summary yet, or whose summary was asked for again; * - `recording_read`: one recording's speakers and lines in order, and the transcript as one text; * - `recording_summary_write`: the summary and the actions written back onto the recording, laid out as the Mac's * local model's are (the shared rule `meetings.summary.land`): the `summary` and `actions` columns, one * meetings_action row per action, `summarised_by` the assistant's name. An action he already added to his to-dos * stays as it was. The phone draws the summary when it arrives. * * The Mac's summariser (crates/cotton-mac/src/summarise.rs) writes the same columns and is unchanged. */ import { rule } from "../../rules/index.ts"; import { fail, optionalString, requireString, type Data, type Handler, type ToolContext } from "../context.ts"; import { resolveTable } from "./records/table.ts"; import { deleteRecord } from "./records/write.ts"; import { outsideText } from "../outside.ts"; import { make, num, patch, str, type Row } from "./story.ts"; import { openStored, windowFor } from "./window.ts"; export const MEETING = "meetings_meeting"; const SPEAKER = "meetings_speaker"; const LINE = "meetings_line"; const ACTION = "meetings_action"; const NO_RECORDING = "There is no such recording: recordings_to_summarise lists them."; /** A recording's live rows in `table`, opened in this assistant's window. */ async function partsOf(ctx: ToolContext, table: string, meeting: string, tool: string): Promise { const t = resolveTable(ctx, table); const keys = await windowFor(ctx, t, tool); const out: Row[] = []; for (const stored of ctx.where(t.id, "meeting", meeting)) { if (t.binned(stored) !== null) continue; const { row } = await openStored(ctx, t, keys, stored); out.push({ ...row.data, id: row.id, created_at: row.created_at }); } return out; } async function recordingOf(ctx: ToolContext, id: string, tool: string): Promise { const t = resolveTable(ctx, MEETING); const keys = await windowFor(ctx, t, tool); const held = ctx.get(t.id, id); if (!held || t.binned(held) !== null) fail(NO_RECORDING); const { row } = await openStored(ctx, t, keys, held); return { ...row.data, id: row.id, created_at: row.created_at }; } /** Has words and wants a summary: none yet, or one asked for again since the last. */ function wanting(data: Data): boolean { if (str(data, "status") !== "ready" || num(data, "lines") === 0) return false; const at = str(data, "summarised_at"); return !at || str(data, "summary_asked_at") > at; } function clock(ms: number): string { const s = Math.max(0, Math.floor(ms / 1000)); const h = Math.floor(s / 3600); const mm = String(Math.floor((s % 3600) / 60)).padStart(h ? 2 : 1, "0"); const ss = String(s % 60).padStart(2, "0"); return h ? `${h}:${mm}:${ss}` : `${mm}:${ss}`; } function about(r: Row): Data { return { id: r.id, title: str(r, "title") || null, kind: str(r, "kind") || null, source: str(r, "source") || "room", started_at: str(r, "started_at") || null, duration_ms: num(r, "duration_ms"), languages: str(r, "languages") || null, lines: num(r, "lines") }; } async function toSummarise(ctx: ToolContext, args: Data): Promise { const tool = "recordings_to_summarise"; const most = typeof args.limit === "number" && args.limit > 0 ? Math.min(Math.floor(args.limit), 100) : 20; const t = resolveTable(ctx, MEETING); const keys = await windowFor(ctx, t, tool); const held = [...ctx.scan(t.id)].filter((row) => t.binned(row) === null && wanting(row.data)); held.sort((a, b) => str(b.data, "started_at").localeCompare(str(a.data, "started_at"))); const recordings: Data[] = []; for (const stored of held.slice(0, most)) { const { row } = await openStored(ctx, t, keys, stored); recordings.push(about({ ...row.data, id: row.id })); } return { recordings, more: held.length > most }; } async function read(ctx: ToolContext, args: Data): Promise { const tool = "recording_read"; const recording = await recordingOf(ctx, requireString(args, "id"), tool); const speakers = (await partsOf(ctx, SPEAKER, recording.id, tool)).sort((a, b) => str(a, "label").localeCompare(str(b, "label"), undefined, { numeric: true })); const lines = (await partsOf(ctx, LINE, recording.id, tool)).sort((a, b) => num(a, "position") - num(b, "position") || num(a, "start_ms") - num(b, "start_ms")); const actions = (await partsOf(ctx, ACTION, recording.id, tool)).sort((a, b) => num(a, "position") - num(b, "position")); const voice = new Map(speakers.map((s) => [s.id, s])); const said = lines.map((l, at) => { const s = voice.get(str(l, "speaker")); const label = s ? str(s, "label") : ""; const name = s ? str(s, "name").trim() : ""; return { n: at + 1, at: clock(num(l, "start_ms")), start_ms: num(l, "start_ms"), speaker: label || null, name: name || null, language: str(l, "language") || null, text: str(l, "text") }; }); const transcript = said.map((l) => `${l.n} [${l.at}] ${l.name ? `${l.name} (${l.speaker})` : (l.speaker ?? "?")}: ${l.text}`).join("\n"); return { recording: { ...about(recording), summary: str(recording, "summary") || null, actions: str(recording, "actions") || null, summarised_by: str(recording, "summarised_by") || null, summarised_at: str(recording, "summarised_at") || null }, speakers: speakers.map((s) => ({ label: str(s, "label"), name: str(s, "name").trim() || null })), lines: said, transcript, // AUDIT C 4: what was said in the room is everyone's speech, not the owner's instructions (src/mcp/outside.ts). outside: { ...outsideText("the people speaking in the recording"), columns: ["lines", "transcript"] }, actions: actions.map((a) => ({ id: a.id, words: str(a, "words"), who: str(a, "who") || null, due: str(a, "due") || null, added_to_todos: !!str(a, "todo") })), }; } interface Landed { summary: string; actions: string; fresh: { words: string; who?: string | null; due?: string | null; due_said?: string | null; at_ms?: number | null }[]; } async function summaryWrite(ctx: ToolContext, args: Data): Promise { const tool = "recording_summary_write"; const recording = await recordingOf(ctx, requireString(args, "id"), tool); if (str(recording, "status") !== "ready") fail("This recording has no words yet: it can be summarised once it is transcribed."); const points = Array.isArray(args.points) ? args.points.filter((p): p is string => typeof p === "string" && p.trim() !== "") : []; if (!points.length) fail("points is required: the summary, one point a string"); const asked = Array.isArray(args.actions) ? args.actions : []; const actions = asked.map((a, at) => { if (!a || typeof a !== "object" || typeof (a as Data).words !== "string") fail(`actions[${at}] needs its words`); const x = a as Data; const opt = (k: string): string | null => (typeof x[k] === "string" && (x[k] as string).trim() ? (x[k] as string).trim() : null); const due = opt("due"); if (due && !/^\d{4}-\d{2}-\d{2}$/.test(due)) fail(`actions[${at}].due is a day, YYYY-MM-DD`); return { words: (x.words as string).trim(), who: opt("who"), due, due_said: opt("due_said"), at_ms: typeof x.at_ms === "number" ? Math.max(0, Math.floor(x.at_ms)) : null }; }); const by = (optionalString(args, "by") ?? "").trim() || "assistant"; if (by.toLowerCase().startsWith("mac:")) fail("by is the assistant's own name: mac: is the Mac's summariser"); const speakers = await partsOf(ctx, SPEAKER, recording.id, tool); const old = await partsOf(ctx, ACTION, recording.id, tool); const kept = old.filter((a) => str(a, "todo")); const landed = rule("meetings.summary.land", { points, actions, voices: speakers.map((s) => ({ label: str(s, "label"), ...(str(s, "name").trim() ? { name: str(s, "name").trim() } : {}) })), kept: kept.map((k) => ({ words: str(k, "words"), who: str(k, "who") || null })), }); // As the Mac lands it: the old actions go except those he added to his to-dos, the new come in order, the // recording's columns last. for (const gone of old.filter((a) => !str(a, "todo"))) deleteRecord(ctx, { table: ACTION, id: gone.id }); let position = kept.length; for (const a of landed.fresh) { position += 1; const speaker = a.who ? (speakers.find((s) => str(s, "label") === a.who)?.id ?? null) : null; await make(ctx, ACTION, { meeting: recording.id, position, words: a.words, who: a.who ?? null, speaker, due: a.due ?? null, due_said: a.due_said ?? null, at_ms: a.at_ms ?? null, made_by: by }); } await patch(ctx, MEETING, recording.id, { summary: landed.summary, actions: landed.actions || null, summarised_by: by, summarised_at: ctx.now, summary_why: null }); return { recording: recording.id, summary: landed.summary, actions: landed.actions, made: landed.fresh.length, kept: kept.length, said: "Written onto the recording: the phone shows it at its next sync." }; } export const RECORDINGS_HANDLERS: Record = { recordings_to_summarise: toSummarise, recording_read: read, recording_summary_write: summaryWrite, };