Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(desktop): rename a speaker in the transcript being renamed
Elian Dorandf8f01f1f33d
diff --git a/.claude/skills/working-with-transcription/SKILL.md b/.claude/skills/working-with-transcription/SKILL.mdindex b83f65d809..bd9c9c4f34 100644--- a/.claude/skills/working-with-transcription/SKILL.md+++ b/.claude/skills/working-with-transcription/SKILL.md@@ -47,7 +47,7 @@ The main process opens the capture window, starts the transcription process andNothing the transcription process imports may reach `@triliumnext/core` or `@triliumnext/server` (the data directory module creates directories when it loads): it gets its paths and a `Logger` from the main process.| `packages/commons/src/lib/transcript.ts` | The `Transcript` format (`version: 1`, `startedAt`, `speakers`, `utterances`), `parseTranscript()`, speaker keys and `setSpeakerName()`; `cleanUtteranceText()`, the filler and stutter cleanup || Cleanup | The stored transcript stays verbatim. `TranscriptView` shows each utterance through `cleanUtteranceText()` while the synced option `transcriptCleanupEnabled` (default on, the "Hide filler words" switch above the transcript) holds, and leaves out utterances it empties. The rules are English-only for stutters (a function-word list) and keep numbers, comma-separated repeats and "that that"/"it it" |-| Speaker names | While its note is being transcribed, `TranscriptView` renames through `electronApi.transcription.renameSpeaker()`: `AttachmentSink`, the only writer then, applies `setSpeakerName()` to the stored names in one transaction, so no utterance is overwritten. Otherwise it saves the stored transcript whole through the generic `POST notes/:noteId/attachments?matchBy=attachmentId`; attachments get no route of their own. `AttachmentSink` re-reads `speakers` before each rewrite, so a rename from another window or device survives the next utterance |+| Speaker names | While a recording writes the transcript, `TranscriptView` renames through `electronApi.transcription.renameSpeaker(attachmentId, …)` (by attachment, since a note can hold an older transcript beside the running one): `AttachmentSink`, the only writer then, applies `setSpeakerName()` to the stored names in one transaction, so no utterance is overwritten. Otherwise it saves the stored transcript whole through the generic `POST notes/:noteId/attachments?matchBy=attachmentId`; attachments get no route of their own. `AttachmentSink` re-reads `speakers` before each rewrite, so a rename from another window or device survives the next utterance || `apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx` | Turns, clock times, the "Speaking…" line, fade-in || `apps/client/src/widgets/launch_bar/RecordMeetingButton.tsx` | The launcher and `useTranscriptionState()` |diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsxindex 0e34126acd..cfc7d147c1 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx@@ -334,7 +334,7 @@ describe("TranscriptView", () => {input?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));});await vi.waitFor(() => expect(renameSpeaker)- .toHaveBeenCalledExactlyOnceWith("meeting", "system:1", "Alexandra"));+ .toHaveBeenCalledExactlyOnceWith("transcript1", "system:1", "Alexandra"));await new Promise((resolve) => setTimeout(resolve));expect(h.post).toHaveBeenCalledTimes(posted);expect(getBlob).toHaveBeenCalledOnce();diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsxindex c404611888..9ca887c5f1 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx@@ -234,13 +234,13 @@ function SpeakerName({ speakerKey, name, defaultName, onRename }: {}/**- * Names a speaker. While this note is being transcribed, the recording applies the name, since it- * rewrites the transcript after every utterance and a whole-transcript save here could overwrite- * one. Otherwise the transcript as stored now is saved with the name.+ * Names a speaker. While a recording writes this transcript, the recording applies the name,+ * since it rewrites the transcript after every utterance and a whole-transcript save here could+ * overwrite one. Otherwise the transcript as stored now is saved with the name.*/async function renameSpeaker(attachment: FAttachment, key: string, name: string) {const { attachmentId, ownerId, role, mime, title } = attachment;- if (await window.electronApi?.transcription?.renameSpeaker(ownerId, key, name)) {+ if (await window.electronApi?.transcription?.renameSpeaker(attachmentId, key, name)) {return;}const latest = parseTranscript((await attachment.getBlob())?.content);diff --git a/apps/desktop/src/preload.spec.ts b/apps/desktop/src/preload.spec.tsindex 307dbb9ac4..b728f122e4 100644--- a/apps/desktop/src/preload.spec.ts+++ b/apps/desktop/src/preload.spec.ts@@ -728,14 +728,14 @@ describe("preload script", () => {await transcription().start("meeting");await transcription().stop();await transcription().getState();- await transcription().renameSpeaker("meeting", "system:1", "Alexandra");+ await transcription().renameSpeaker("transcript1", "system:1", "Alexandra");expect(ipcRendererInvoked).toEqual([{ channel: "transcription-start", args: [ "meeting" ] },{ channel: "transcription-stop", args: [] },{ channel: "transcription-get-state", args: [] },{channel: "transcription-rename-speaker",- args: [ "meeting", "system:1", "Alexandra" ]+ args: [ "transcript1", "system:1", "Alexandra" ]}]);});diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.tsindex dc35e0e7f0..f1c67665a4 100644--- a/apps/desktop/src/preload.ts+++ b/apps/desktop/src/preload.ts@@ -264,8 +264,8 @@ contextBridge.exposeInMainWorld("electronApi", {getState() {return ipcRenderer.invoke("transcription-get-state");},- renameSpeaker(noteId: string, key: string, name: string) {- return ipcRenderer.invoke("transcription-rename-speaker", noteId, key, name);+ renameSpeaker(attachmentId: string, key: string, name: string) {+ return ipcRenderer.invoke("transcription-rename-speaker", attachmentId, key, name);},onStateChanged(callback: (state: TranscriptionState) => void) {const listener = (_event: unknown, state: TranscriptionState) => callback(state);diff --git a/apps/desktop/src/services/transcription/attachment_sink.ts b/apps/desktop/src/services/transcription/attachment_sink.tsindex dc4dcbe149..272916e849 100644--- a/apps/desktop/src/services/transcription/attachment_sink.ts+++ b/apps/desktop/src/services/transcription/attachment_sink.ts@@ -15,7 +15,8 @@ import type { TranscriptSink } from "./session";*/export class AttachmentSink implements TranscriptSink {private transcript: Transcript | null = null;- private attachmentId: string | null = null;+ /** The transcript attachment, once {@link open} has created it. */+ attachmentId: string | null = null;constructor(private readonly noteId: string) {}diff --git a/apps/desktop/src/services/transcription/index.spec.ts b/apps/desktop/src/services/transcription/index.spec.tsindex f826813ca8..53e2560b48 100644--- a/apps/desktop/src/services/transcription/index.spec.ts+++ b/apps/desktop/src/services/transcription/index.spec.ts@@ -85,6 +85,7 @@ vi.mock("./attachment_sink", () => ({write = h.sink.write;save = h.sink.save;renameSpeaker = h.sink.renameSpeaker;+ attachmentId = "transcript1";constructor(readonly noteId: string) {}}@@ -271,25 +272,26 @@ describe("transcription IPC", () => {it("renames a speaker through the recording that writes the transcript, until it stops", async () => {const invoke = await loadService();- const rename = (noteId: unknown, key: unknown, name: unknown) =>- invoke("transcription-rename-speaker", noteId, key, name);- expect(await rename("meeting", "system:1", "Alexandra")).toBe(false);+ const rename = (attachmentId: unknown, key: unknown, name: unknown) =>+ invoke("transcription-rename-speaker", attachmentId, key, name);+ expect(await rename("transcript1", "system:1", "Alexandra")).toBe(false);await invoke("transcription-start", "meeting");- expect(await rename("meeting", "system:1", "Alexandra")).toBe(true);+ expect(await rename("transcript1", "system:1", "Alexandra")).toBe(true);expect(h.sink.renameSpeaker).toHaveBeenCalledExactlyOnceWith("system:1", "Alexandra");- expect(await rename("other", "system:1", "Bob")).toBe(false);- expect(await rename("meeting", "system:1", 42)).toBe(false);+ // An older transcript of the same note is not the one the recording writes.+ expect(await rename("olderTranscript", "system:1", "Bob")).toBe(false);+ expect(await rename("transcript1", "system:1", 42)).toBe(false);let finishStopping = () => {};h.worker.stop.mockImplementationOnce(() => new Promise<void>((resolve) => {finishStopping = resolve;}));const stopped = invoke("transcription-stop");- expect(await rename("meeting", "microphone", "Elian")).toBe(true);+ expect(await rename("transcript1", "microphone", "Elian")).toBe(true);finishStopping();await stopped;- expect(await rename("meeting", "microphone", "Elian")).toBe(false);+ expect(await rename("transcript1", "microphone", "Elian")).toBe(false);expect(h.sink.renameSpeaker).toHaveBeenCalledTimes(2);});diff --git a/apps/desktop/src/services/transcription/index.ts b/apps/desktop/src/services/transcription/index.tsindex 366b3a9a5a..28c3003a88 100644--- a/apps/desktop/src/services/transcription/index.ts+++ b/apps/desktop/src/services/transcription/index.ts@@ -29,7 +29,7 @@ interface Recording {let state = IDLE;let activeRecording: Recording | null = null;/** The transcript the running recording writes, until its stop has written the last utterance. */-let writer: { noteId: string; sink: AttachmentSink } | null = null;+let writer: AttachmentSink | null = null;/** The running stop, while the speech already captured is still being transcribed. */let stopping: Promise<void> | null = null;/**@@ -44,8 +44,8 @@ export function setupTranscriptionHandlers() {ipcMain.handle("transcription-start", (_event, noteId: unknown) => startRecording(noteId));ipcMain.handle("transcription-stop", () => stopRecording());ipcMain.handle("transcription-get-state", () => getState());- ipcMain.handle("transcription-rename-speaker",- (_event, noteId: unknown, key: unknown, name: unknown) => renameSpeaker(noteId, key, name));+ ipcMain.handle("transcription-rename-speaker", (_event, attachmentId: unknown, key: unknown,+ name: unknown) => renameSpeaker(attachmentId, key, name));app.on("before-quit", (event) => finishBeforeQuit(event));events.subscribe(events.ENTER_PROTECTED_SESSION, () => saveUnsaved());}@@ -88,7 +88,7 @@ export async function startRecording(noteId: unknown): Promise<string> {recordingDirectory: coreUtils.isDev() ? getRecordingDirectory(Date.now(), noteId) : null});sink.open(startedAt);- writer = { noteId, sink };+ writer = sink;const { port1, port2 } = new MessageChannelMain();recording.worker.connectCapture(port2);const sources = await recording.capture.start(port1);@@ -136,17 +136,17 @@ export function getState() {}/**- * Names a speaker of the transcript the recording of `noteId` writes, through the sink, which is- * its only writer while it runs.+ * Names a speaker of the transcript attachment `attachmentId` through the running recording's+ * sink, when that is the transcript it writes: the sink is then its only writer.*- * @returns `false` when no recording of `noteId` runs, or the arguments are not strings.+ * @returns `false` when no recording writes `attachmentId`, or the arguments are not strings.*/-export function renameSpeaker(noteId: unknown, key: unknown, name: unknown) {+export function renameSpeaker(attachmentId: unknown, key: unknown, name: unknown) {const isValid = typeof key === "string" && typeof name === "string";- if (!writer || writer.noteId !== noteId || !isValid) {+ if (!writer || writer.attachmentId !== attachmentId || !isValid) {return false;}- writer.sink.renameSpeaker(key, name);+ writer.renameSpeaker(key, name);return true;}diff --git a/packages/commons/src/lib/electron_api_interface.ts b/packages/commons/src/lib/electron_api_interface.tsindex 3d89d189ee..3cc90cfba3 100644--- a/packages/commons/src/lib/electron_api_interface.ts+++ b/packages/commons/src/lib/electron_api_interface.ts@@ -749,13 +749,13 @@ export interface ElectronTranscriptionApi {getState(): Promise<TranscriptionState>;/**- * Names a speaker of the transcript the recording of `noteId` writes, or restores the default- * name when `name` is blank. The recording applies it, so no utterance it writes meanwhile is- * lost.+ * Names a speaker of the transcript attachment `attachmentId`, or restores the default name+ * when `name` is blank, if a running recording writes that transcript. The recording applies+ * it, so no utterance it writes meanwhile is lost.*- * @returns `false` when no recording of `noteId` runs; the transcript can then be saved whole.+ * @returns `false` when no recording writes `attachmentId`; it can then be saved whole.*/- renameSpeaker(noteId: string, key: string, name: string): Promise<boolean>;+ renameSpeaker(attachmentId: string, key: string, name: string): Promise<boolean>;/*** Calls `callback` every time the state changes, in any window.