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
feat(attachments): break a long turn into paragraphs at long pauses
Elian Doran4ca382132561
diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.cssindex be36c42c7d..32f6a378b6 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.css@@ -38,4 +38,8 @@margin: 0;white-space: pre-wrap;}++ p + p {+ margin-top: 0.5rem;+ }}diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsxindex 3abb99c681..b949fc4776 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.spec.tsx@@ -32,9 +32,21 @@ describe("transcript helpers", () => {{ source: "system", offsetMs: 5000, text: "Yes." },{ source: "microphone", offsetMs: 7000, text: "Good." }])).toEqual([- { source: "microphone", offsetMs: 1000, text: "Hello. Can you hear me?" },- { source: "system", offsetMs: 5000, text: "Yes." },- { source: "microphone", offsetMs: 7000, text: "Good." }+ { source: "microphone", offsetMs: 1000, paragraphs: [ "Hello. Can you hear me?" ] },+ { source: "system", offsetMs: 5000, paragraphs: [ "Yes." ] },+ { source: "microphone", offsetMs: 7000, paragraphs: [ "Good." ] }+ ]);+ });++ it("starts a new paragraph within a turn after a long pause", () => {+ const turns = groupIntoTurns([+ { source: "system", offsetMs: 0, durationMs: 8000, text: "First point," },+ { source: "system", offsetMs: 8100, durationMs: 3000, text: "continued." },+ { source: "system", offsetMs: 12_600, durationMs: 2000, text: "Short pause." },+ { source: "system", offsetMs: 17_000, durationMs: 2000, text: "Second point." }+ ]);+ expect(turns.map((turn) => turn.paragraphs)).toEqual([+ [ "First point, continued. Short pause.", "Second point." ]]);});@@ -51,8 +63,9 @@ describe("TranscriptView", () => {const getBlob = vi.fn().mockResolvedValueOnce({ content: transcript([]) }).mockResolvedValueOnce({ content: transcript([- { source: "microphone", offsetMs: 1500, text: "Can you hear me?" },- { source: "system", offsetMs: 3000, text: "Yes." }+ { source: "microphone", offsetMs: 1500, durationMs: 1000, text: "Can you hear me?" },+ { source: "microphone", offsetMs: 6000, durationMs: 1000, text: "Hello?" },+ { source: "system", offsetMs: 8000, text: "Yes." }]) });const attachment = { attachmentId: "transcript1", getBlob } as unknown as FAttachment;const component = new Component();@@ -74,9 +87,10 @@ describe("TranscriptView", () => {.map((turn) => turn.textContent);await vi.waitFor(() => expect(turns()).toHaveLength(2));expect(turns()).toEqual([- "transcript.speaker_me0:01Can you hear me?",- "transcript.speaker_others0:03Yes."+ "transcript.speaker_me0:01Can you hear me?Hello?",+ "transcript.speaker_others0:08Yes."]);+ expect(container.querySelectorAll(".transcript-microphone p")).toHaveLength(2);});it("follows new turns for a reader at the end, not for one who scrolled up", async () => {diff --git a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsxindex 13c4e9d4c8..0488da7c16 100644--- a/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx+++ b/apps/client/src/widgets/type_widgets/transcript/TranscriptView.tsx@@ -11,6 +11,9 @@ import NoItems from "../../react/NoItems";/** How close to the end, in pixels, still counts as reading the newest turn. */const FOLLOW_THRESHOLD = 16;+/** How long a pause within one speaker's turn starts a new paragraph. */+const PARAGRAPH_PAUSE_MS = 2000;+/*** Shows a `transcript` attachment as the conversation it records, one paragraph per speaker turn.* Follows the attachment as a running recording rewrites it, and keeps a reader who is at the end@@ -67,18 +70,18 @@ export default function TranscriptView({ attachment }: { attachment: FAttachment<span className="transcript-speaker">{speakerName(turn.source)}</span><span className="transcript-time">{formatOffset(turn.offsetMs)}</span></div>- <p>{turn.text}</p>+ {turn.paragraphs.map((paragraph, index) => <p key={index}>{paragraph}</p>)}</div>))}</div>);}-/** Consecutive utterances from one source, read as one paragraph. */+/** Consecutive utterances from one source, split into paragraphs at long pauses. */export interface TranscriptTurn {source: TranscriptSource;offsetMs: number;- text: string;+ paragraphs: string[];}/** Reads a transcript attachment's content, or returns `null` for content of another shape. */@@ -96,16 +99,24 @@ export function parseTranscript(content: string | null): Transcript | null {}}-/** Joins each run of utterances from one source into a turn, timed by its first utterance. */+/**+ * Joins each run of utterances from one source into a turn, timed by its first utterance, and+ * starts a new paragraph after a pause of {@link PARAGRAPH_PAUSE_MS} or more. An utterance without+ * a `durationMs` never starts one, since the pause before the next is unknown.+ */export function groupIntoTurns(utterances: TranscriptUtterance[]): TranscriptTurn[] {const turns: TranscriptTurn[] = [];- for (const { source, offsetMs, text } of utterances) {+ let previousEndMs: number | null = null;+ for (const { source, offsetMs, durationMs, text } of utterances) {const last = turns.at(-1);- if (last?.source === source) {- last.text += ` ${text}`;+ if (last?.source !== source) {+ turns.push({ source, offsetMs, paragraphs: [ text ] });+ } else if (previousEndMs !== null && offsetMs - previousEndMs >= PARAGRAPH_PAUSE_MS) {+ last.paragraphs.push(text);} else {- turns.push({ source, offsetMs, text });+ last.paragraphs[last.paragraphs.length - 1] += ` ${text}`;}+ previousEndMs = durationMs === undefined ? null : offsetMs + durationMs;}return turns;}diff --git a/apps/desktop/src/services/transcription/session.spec.ts b/apps/desktop/src/services/transcription/session.spec.tsindex 9e32f57034..ed084847cd 100644--- a/apps/desktop/src/services/transcription/session.spec.ts+++ b/apps/desktop/src/services/transcription/session.spec.ts@@ -72,7 +72,7 @@ beforeEach(() => {});describe("RecordingSession", () => {- it("labels each source's utterances with their offset from the recording start", async () => {+ it("labels each source's utterances with their offset from the recording start and length", async () => {const { session, capture, engine, sink, deliver, written } = createHarness();await session.start();expect(sink.open).toHaveBeenCalledExactlyOnceWith(1000);@@ -87,8 +87,8 @@ describe("RecordingSession", () => {expect(engine.createSegmenter).toHaveBeenCalledTimes(2);expect(written).toEqual([- { source: "microphone", offsetMs: 1500, text: "utterance 1" },- { source: "system", offsetMs: 3000, text: "utterance 2" }+ { source: "microphone", offsetMs: 1500, durationMs: 1000, text: "utterance 1" },+ { source: "system", offsetMs: 3000, durationMs: 1000, text: "utterance 2" }]);});@@ -110,7 +110,7 @@ describe("RecordingSession", () => {expect(capture.stop).toHaveBeenCalledOnce();expect(sink.write).toHaveBeenCalledExactlyOnceWith({- source: "microphone", offsetMs: 1000, text: "utterance 3"+ source: "microphone", offsetMs: 1000, durationMs: 1000, text: "utterance 3"});expect(sink.close).toHaveBeenCalledOnce();expect(sink.write.mock.invocationCallOrder[0])diff --git a/apps/desktop/src/services/transcription/session.ts b/apps/desktop/src/services/transcription/session.tsindex e63b098148..4da6ea0409 100644--- a/apps/desktop/src/services/transcription/session.ts+++ b/apps/desktop/src/services/transcription/session.ts@@ -90,9 +90,10 @@ export class RecordingSession {return;}+ const durationMs = samples.length / SAMPLE_RATE * 1000;for (const sink of this.options.sinks) {try {- await sink.write({ source, offsetMs, text });+ await sink.write({ source, offsetMs, durationMs, text });} catch (e) {getLog().error(`Transcription: a sink failed: ${e}`);}diff --git a/packages/commons/src/lib/transcript.ts b/packages/commons/src/lib/transcript.tsindex 541f9e319f..a49f90becb 100644--- a/packages/commons/src/lib/transcript.ts+++ b/packages/commons/src/lib/transcript.ts@@ -9,6 +9,8 @@ export interface TranscriptUtterance {source: TranscriptSource;/** Milliseconds from the start of the recording to the start of the utterance. */offsetMs: number;+ /** Length of the utterance in milliseconds. Absent from transcripts recorded before it was kept. */+ durationMs?: number;text: string;}