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(transcription): log why an audio source failed to start
Elian Doran7f636ce75b02
diff --git a/apps/desktop/src/services/transcription/capture.spec.ts b/apps/desktop/src/services/transcription/capture.spec.tsindex fcef2a134a..2e98dc8084 100644--- a/apps/desktop/src/services/transcription/capture.spec.ts+++ b/apps/desktop/src/services/transcription/capture.spec.ts@@ -17,7 +17,8 @@ class FakeWindow {webContents = Object.assign(new EventEmitter(), {ipc: new EventEmitter(),postMessage: vi.fn(),- executeJavaScript: vi.fn((call: string) => h.pageCalls[call]?.() ?? Promise.resolve(call))+ executeJavaScript: vi.fn((call: string) =>+ h.pageCalls[call]?.() ?? Promise.resolve({ tracks: call }))});constructor() {@@ -184,14 +185,16 @@ describe("CaptureWindow", () => {});it("runs the sources that start, and fails when none does", async () => {- h.pageCalls["captureSystemAudio()"] = () => Promise.reject(new Error("NotAllowedError"));+ h.pageCalls["run(captureSystemAudio)"] = () =>+ Promise.resolve({ error: "NotAllowedError: Permission denied" });h.windows.length = 0;const capture = new CaptureWindow({ onLost: vi.fn() });expect(await capture.start("port" as never)).toEqual([ "microphone" ]);expect(h.log.error).toHaveBeenCalledWith(- "Transcription: system unavailable: NotAllowedError");+ "Transcription: system unavailable: NotAllowedError: Permission denied");- h.pageCalls["captureMicrophone()"] = () => Promise.reject(new Error("NotFoundError"));+ h.pageCalls["run(captureMicrophone)"] = () =>+ Promise.reject(new Error("render frame was disposed"));await expect(new CaptureWindow({ onLost: vi.fn() }).start("port" as never)).rejects.toThrow("No audio source could be captured.");expect(h.windows[1].destroy).toHaveBeenCalledOnce();diff --git a/apps/desktop/src/services/transcription/capture.ts b/apps/desktop/src/services/transcription/capture.tsindex 61f205bdbe..db4135f8e4 100644--- a/apps/desktop/src/services/transcription/capture.ts+++ b/apps/desktop/src/services/transcription/capture.ts@@ -27,10 +27,10 @@ const VAD_WINDOW_SAMPLES = 512;/** How long {@link CaptureWindow.stop} waits for the page to post what it still holds. */const FINISH_TIMEOUT_MS = 1000;-/** The page function that starts each source. */+/** The page call that starts each source. */const SOURCE_STARTERS = [- [ "microphone", "captureMicrophone()" ],- [ "system", "captureSystemAudio()" ]+ [ "microphone", "run(captureMicrophone)" ],+ [ "system", "run(captureSystemAudio)" ]] as const;/**@@ -69,8 +69,12 @@ export class CaptureWindow {try {// getDisplayMedia() requires a user gesture, which the hidden window only gets// this way.- const tracks = await window.webContents.executeJavaScript(call, true);- getLog().info(`Transcription: capturing ${source} from ${tracks}.`);+ const result: StarterResult =+ await window.webContents.executeJavaScript(call, true);+ if ("error" in result) {+ throw new Error(result.error);+ }+ getLog().info(`Transcription: capturing ${source} from ${result.tracks}.`);sources.push(source);this.running.add(source);} catch (e) {@@ -121,6 +125,12 @@ export class CaptureWindow {}}+/**+ * What `run()` in {@link CAPTURE_PAGE} resolves to. The page reports a failure as text because+ * `executeJavaScript()` rejects with an object that has lost the `DOMException`'s message.+ */+type StarterResult = { tracks: string } | { error: string };+/*** The window runs only {@link CAPTURE_PAGE}, a local file, so it gets Node integration to reach* `ipcRenderer`, which delivers the port, without a preload. Its in-memory session grants the@@ -249,6 +259,14 @@ async function finishCapture() {(await portReceived).postMessage({ end: true });}+async function run(starter) {+ try {+ return { tracks: await starter() };+ } catch (e) {+ return { error: e instanceof Error ? e.name + ": " + e.message : String(e) };+ }+}+function describe(tracks) {return tracks.map((track) => track.label).join(", ");}@@ -278,8 +296,6 @@ async function captureSystemAudio() {const media = await navigator.mediaDevices.getDisplayMedia({video: true,audio: { echoCancellation: false, noiseSuppression: false, autoGainControl: false }- }).catch((e) => {- throw new Error(e.name + ": " + e.message);});for (const track of media.getVideoTracks()) {track.stop();