/** * Cotton Meetings in the object (apps/meetings/CORE.md). * * A recording's audio arrives as every person's file does, sealed on the phone * (`<database id>/meetings/<id>/full.<ext>`, `meetings` among SEALED_KINDS): * the object stores bytes it cannot read, and never transcribes. The words are * made on the owner's own devices — the Mac (pnpm meetings:transcribe --queue) * or the phone on charge — and an outside service only when the owner turned * one on (src/meetings_service.ts, shipped disabled). * * What the object adds is the queue's one shared truth, on the recording's * clear columns (status, transcribe_on, asked_at, attempts, status_why): * * - `GET /v1/meetings/waiting?for=mac|phone|service` (the owner): the * recordings waiting for that transcriber, oldest asked first, with their * audio key and length; nothing sealed is read. * - `meeting_claim { id, by }` (an act): a transcriber takes one; refused 409 * `not_waiting` when another holds it, so the Mac and a phone never both * turn one recording into text. * - `meeting_failed { id, why }`: the transcriber that holds it gives it up, * with a reason the phone words (`meetings` lines `why.*`); the audio stays, * and Turn into text again asks once more. * * Its speakers and lines are written by the transcriber, sealed, as any * device writes rows; the meeting goes `ready` last. */importtype { Engine, Outcome } from"./engine.ts";import { refuse, text } from"./http.ts";importtype { Store, StoredRow } from"./store.ts";exportconst MEETING = "meetings_meeting";exportconst SPEAKER = "meetings_speaker";exportconst LINE = "meetings_line";exporttypeTranscriber = "mac" | "phone" | "service";const TRANSCRIBERS: readonlyTranscriber[] = ["mac", "phone", "service"];/** The reasons a transcriber may give: the phone's `why.*` lines. */exportconst WHYS = ["no_audio", "engine_failed", "too_long", "not_enabled"] asconst;exportinterfaceWaiting { id: string; kind: string; audio: string; duration_ms: number | null; asked_at: string | null; attempts: number;}const textOf = (v: unknown): string => (typeof v === "string" ? v : "");/** Whether a recording asked of `on` may be done by `who`: `any` by every transcriber but the outside service, which only takes what was asked of it. */exportfunction mayDo(on: string, who: Transcriber): boolean {if (who === "service") return on === "service";return on === "" || on === "any" || on === who;}exportfunction transcriberOf(v: string | null): Transcriber {const who = (v ?? "mac").trim() asTranscriber;if (!TRANSCRIBERS.includes(who)) refuse(400, "bad_args", "for is mac, phone or service");return who;}/** The recordings waiting for `who`, oldest asked first. */exportfunction waiting(store: Store, who: Transcriber): Waiting[] {return [...store.scan(MEETING, [{ sql: `json_extract(data, '$."status"') = 'waiting'`, args: [] }])] .filter((row) => mayDo(textOf(row.data.transcribe_on), who) && textOf(row.data.audio) !== "") .sort((a, b) => (textOf(a.data.asked_at) || a.created_at).localeCompare(textOf(b.data.asked_at) || b.created_at) || a.id.localeCompare(b.id)) .map((row) => ({ id: row.id, kind: textOf(row.data.kind) || "meeting", audio: textOf(row.data.audio), duration_ms: typeof row.data.duration_ms === "number" ? row.data.duration_ms : null, asked_at: textOf(row.data.asked_at) || null, attempts: typeof row.data.attempts === "number" ? row.data.attempts : 0, }));}function live(store: Store, id: unknown): StoredRow {if (typeof id !== "string" || !id.trim()) refuse(400, "bad_args", "name the recording: its id");const row = store.get(MEETING, id.trim());if (!row || row.deleted) refuse(404, "no_such_meeting", `no recording with id ${id}`);return row;}/** `meeting_claim { id, by }`: one transcriber takes a waiting recording. */exportfunction meetingClaim(engine: Engine, args: Record<string, unknown>, version: number, outcome: Outcome): { meeting: string; status: string; attempts: number } {const row = live(engine.store, args.id);const by = transcriberOf(text(args.by, "by"));const status = textOf(row.data.status);if (status !== "waiting") refuse(409, "not_waiting", `this recording is ${status || "not waiting"}: another transcriber may hold it`);if (!mayDo(textOf(row.data.transcribe_on), by)) refuse(409, "not_yours", `this recording waits for ${textOf(row.data.transcribe_on)}, not ${by}`);const attempts = (typeof row.data.attempts === "number" ? row.data.attempts : 0) + 1; engine.update(row, { status: "transcribing", attempts, status_why: null, transcribing_on: by }, version, outcome);return { meeting: row.id, status: "transcribing", attempts };}/** `meeting_failed { id, why }`: the transcriber holding it gives it up. */exportfunction meetingFailed(engine: Engine, args: Record<string, unknown>, version: number, outcome: Outcome): { meeting: string; status: string } {const row = live(engine.store, args.id);const why = text(args.why, "why");if (!(WHYS asreadonly string[]).includes(why)) refuse(400, "bad_args", `why is one of ${WHYS.join(", ")}`);if (textOf(row.data.status) !== "transcribing") refuse(409, "not_transcribing", "only a recording being turned into text can fail"); engine.update(row, { status: "failed", status_why: why }, version, outcome);return { meeting: row.id, status: "failed" };}