/** * What the generic verbs answer: the record object and the change object, in * the Rust key order (tables/mod.rs `Stored::to_json`, `change_json`), and a * change's receipt in the kit's `receipts` words (records/receipt.rs). * * A record's `byline` the Rust answers carried is null: no row here stores * who wrote it (the change log does, src/changes.ts). A change's `id` is the * change the log kept for the write, to take it back with undo. */ import type { Data, ToolContext } from "../../context.ts"; import type { StoredRow } from "../../../store.ts"; import { outsideOf } from "../../outside.ts"; import { isSensitiveColumn, keyAsWords, lowerFirst, type Column, type Table } from "./table.ts"; /** A row's values as a read answers them: the definition's column order, an empty value left out, no sealed or hashed column. */ export function readData(t: Table, data: Data): Data { const out: Data = {}; for (const c of t.columns) { if (isSensitiveColumn(c.key)) continue; if (c.computed !== undefined && c.computed !== null) continue; const v = data[c.key]; if (v === null || v === undefined) continue; out[c.key] = c.type === "account_number" && typeof v === "string" ? ending(v) : v; } return out; } /** An account number is only ever read as its last four digits (`account_number_ending`). */ function ending(v: string): string { return v.replace(/[^0-9]/g, "").slice(-4); } export function recordJson(t: Table, row: StoredRow): Data { const data = readData(t, row.data); // AUDIT C 4: text another person wrote is marked as outside content (src/mcp/outside.ts). const outside = outsideOf(t.id, data); return { table: t.id, id: row.id, data, created_at: row.created_at, updated_at: row.updated_at, deleted_at: t.binned(row), byline: null, ...(outside ? { outside } : {}), }; } export type Verb = "create" | "update" | "delete"; export function bylineOf(ctx: ToolContext): string { return ctx.caller.name.trim() ? ctx.caller.name : "assistant"; } /** Where a write left its record: a delete in the bin or gone, an update that took it back out of the bin. */ export type Lands = "bin" | "gone" | "back"; export function changeJson(ctx: ToolContext, t: Table, verb: Verb, recordId: string, before: Data | null, after: Data | null, now: Data | null, lands: Lands = "gone"): Data { const shownBefore = before ? project(t, before) : null; const shownAfter = after ? project(t, after) : null; // The write's own change is the first its writes logged: the record's, before any its automations wrote. const id = ctx.trail.ids[0] ?? null; return { id, table: t.id, record_id: recordId, verb, byline: bylineOf(ctx), before: shownBefore, after: shownAfter, created_at: ctx.now, undone_by: null, undoable: id !== null, reason: id === null ? "the write changed no record" : null, receipt: receipt(t, verb, before ?? {}, after ?? {}, now, lands), }; } /** A change's before and after are a read too: no sealed column, an account number by its ending. */ export function project(t: Table, data: Data): Data { const out: Data = {}; for (const [k, v] of Object.entries(data)) { if (isSensitiveColumn(k)) continue; const c = t.column(k); out[k] = c?.type === "account_number" && typeof v === "string" ? ending(v) : v; } return out; } // ── receipts (records/receipt.rs) ───────────────────────────────────────── interface Receipt { kind: string; said: string; words: { name: string; before: string | null; after: string | null }[]; } function plain(kind: string, said: string): Receipt { return { kind, said, words: [] }; } const NAME_LENGTH = 60; /** Words as a sentence says them: the first line, cut at 60 letters. */ function saidWords(v: unknown): string | null { if (typeof v !== "string") return null; const line = v.trim().split("\n")[0]!.trim(); if (!line) return null; const letters = [...line]; return letters.length > NAME_LENGTH ? `${letters.slice(0, NAME_LENGTH).join("")}…` : line; } function trimmed(v: unknown): string | null { return typeof v === "string" && v.trim() ? v.trim() : null; } function isWordsColumn(c: Column | undefined): boolean { return !!c && ["text", "longtext", "url", "place"].includes(c.type) && c.secret !== true; } /** The name a receipt calls a record by: its title's first line, else what it is ("a lane"). Never its id. */ export function recordName(t: Table, data: Data): string { const titled = t.column(t.title); const own = titled && ["text", "longtext", "place", "account_number"].includes(titled.type) ? saidWords(data[t.title]) : null; let name: string; if (own) name = own; else { const n = lowerFirst(t.name); name = `${/^[aeiou]/.test(n) ? "an" : "a"} ${n}`; } const kept = name.endsWith(".") ? name.slice(0, -1) : null; return kept !== null && !kept.endsWith(".") && kept.trim() ? kept.trimEnd() : name; } function isSet(v: unknown): boolean { return !(v === null || v === undefined || v === false || v === ""); } function receipt(t: Table, verb: Verb, before: Data, after: Data, now: Data | null, lands: Lands): Receipt { if (verb === "create") return plain("added", `Added ${recordName(t, now ?? after)}`); if (verb === "delete") return plain("deleted", lands === "bin" ? `Moved ${recordName(t, before)} to the bin` : `Deleted ${recordName(t, before)}`); const name = recordName(t, { ...before, ...after, ...(now ?? {}) }); if (lands === "back") return plain("put_back", `Put back ${name} from the bin`); const columns = t.columns.filter((c) => c.key !== t.order && (c.key in before || c.key in after) && JSON.stringify(before[c.key] ?? null) !== JSON.stringify(after[c.key] ?? null)); const held = (data: Data, c: Column) => data[c.key] ?? null; const archive = t.archive; const archived = archive && "column" in archive ? columns.find((c) => c.key === archive.column) : undefined; if (archived) return isSet(held(after, archived)) ? plain("archived", `Archived ${name}`) : plain("put_back", `Put back ${name}`); const longtexts = columns.filter((c) => c.type === "longtext"); if (longtexts.length === 1) { const was = held(before, longtexts[0]!); const is = held(after, longtexts[0]!); if (typeof was === "string" && typeof is === "string" && was.trim() && is.startsWith(was.trimEnd())) { const added = is.slice(was.trimEnd().length); if (added.startsWith("\n") && added.trim()) return plain("appended", `Added ${added.trim()}`); } } const isState = (c: Column) => c.type === "bool" || (c.type === "choice" && c.multiple !== true); const states = columns.filter(isState); const ridesAlong = columns.filter((c) => !isState(c)).every((c) => c.type === "date"); if (states.length === 1 && ridesAlong) { const c = states[0]!; const is = held(after, c); if (c.type === "bool") { const slip = t.slip; const label = lowerFirst(t.label(c)); const toggle = slip.toggle === c.key && slip.toggle_style === "switch"; if (toggle) return plain("switched", isSet(is) ? `Turned on ${name}` : `Turned off ${name}`); return plain("switched", isSet(is) ? `Marked ${name} ${label}` : `Marked ${name} not ${label}`); } if (typeof is === "string" && is) { const slip = t.slip; const caption = slip.caption as { status?: unknown } | undefined; const answers = slip.answers as { column?: unknown } | undefined; const stateColumn = slip.status === c.key || caption?.status === c.key || answers?.column === c.key; return plain("switched", stateColumn ? `${keyAsWords(is)} ${name}` : `Marked ${name} ${lowerFirst(keyAsWords(is))}`); } } const words = columns.filter(isWordsColumn).map((c) => ({ name: t.label(c), before: trimmed(held(before, c)), after: trimmed(held(after, c)) })); const edited = words.length > 0 && words.length === columns.length; return { kind: edited ? "edited" : "changed", said: edited ? `Edited ${name}` : `Changed ${name}`, words }; }