From accb53d99375818caa5c0bcae18e0939fdebe49c Mon Sep 17 00:00:00 2001 From: Wisley Alves Date: Wed, 18 Nov 2026 17:25:38 -0300 Subject: [PATCH] fix(voz): resposta de outra aba espera a leitura atual acabar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No modo "quando a pergunta foi ditada", a resposta da segunda aba cortava a leitura da primeira. Agora as leituras automáticas entram numa fila; parar a voz na mão (ou começar a ditar) esvazia a fila, e sair da aba só corta a leitura dela. Co-Authored-By: Claude Opus 5.5 --- src/App.vue | 13 ++++++++++--- src/lib/queue.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib/queue.ts | 15 +++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/lib/queue.test.ts create mode 100644 src/lib/queue.ts diff --git a/src/App.vue b/src/App.vue index bae33e3..dd4345a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -16,6 +16,7 @@ import TasksMenu from "./components/TasksMenu.vue"; import SettingsPanel from "./components/SettingsPanel.vue"; import { loadSettings, pickFiles, shellQuote, speakClaudeReply, speakStop, voiceCancel, voiceStart, voiceStop } from "./lib/settings"; import { voiceCommand } from "./lib/voice"; +import { serialQueue } from "./lib/queue"; import { listen } from "@tauri-apps/api/event"; import { getCurrentWebview } from "@tauri-apps/api/webview"; import { anon } from "./state/anon"; @@ -499,13 +500,17 @@ let speakSeq = 0; /** Aba sendo lida: sair dela cancela a leitura. */ let speakingTab: number | undefined; +/** Leituras automáticas, uma de cada vez. */ +const speechQueue = serialQueue(); + /** Claude da aba terminou: lê sozinho, conforme "Ler a resposta" nas configurações. */ async function speakReply(tab: Tab) { const dictated = dictatedTabs.delete(tab.id); const speech = (await loadSettings().catch(() => null))?.speech; if (!speech || speech.mode === "off") return; if (speech.mode === "dictated" ? !dictated : !isActive(tab)) return; - await speakTab(tab, false); + // Na fila: a resposta de outra aba espera a que está sendo lida acabar, em vez de cortá-la. + await speechQueue.add(() => speakTab(tab, false)); } /** Lê a última resposta do Claude da aba. `manual`: pedido pelo ▶, avisa se não tem o que ler. */ @@ -534,7 +539,9 @@ function listenReply() { else if (tab) flash("Voz: não tem Claude rodando nesta aba."); } -function stopSpeaking() { +/** `keepQueue`: só corta a leitura atual (saiu da aba); as outras respostas na fila continuam. */ +function stopSpeaking(keepQueue = false) { + if (!keepQueue) speechQueue.clear(); speakSeq++; speakingTab = undefined; voice.value = "idle"; @@ -545,7 +552,7 @@ function stopSpeaking() { watch( () => activeTab.value?.id, (id) => { - if (voice.value === "speaking" && speakingTab !== undefined && speakingTab !== id) stopSpeaking(); + if (voice.value === "speaking" && speakingTab !== undefined && speakingTab !== id) stopSpeaking(true); }, ); diff --git a/src/lib/queue.test.ts b/src/lib/queue.test.ts new file mode 100644 index 0000000..98db7c3 --- /dev/null +++ b/src/lib/queue.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from "vitest"; +import { serialQueue } from "./queue"; + +const tick = () => new Promise((r) => setTimeout(r, 0)); + +describe("serialQueue", () => { + it("a segunda só começa quando a primeira termina", async () => { + const q = serialQueue(); + const log: string[] = []; + let finishFirst!: () => void; + q.add(() => (log.push("a começa"), new Promise((r) => (finishFirst = () => (log.push("a termina"), r()))))); + const done = q.add(async () => void log.push("b começa")); + await tick(); + expect(log).toEqual(["a começa"]); + finishFirst(); + await done; + expect(log).toEqual(["a começa", "a termina", "b começa"]); + }); + + it("clear descarta as que esperavam, mas aceita as novas", async () => { + const q = serialQueue(); + const log: string[] = []; + let finish!: () => void; + q.add(() => new Promise((r) => (finish = r))); + q.add(async () => void log.push("descartada")); + await tick(); + q.clear(); + finish(); + await q.add(async () => void log.push("nova")); + expect(log).toEqual(["nova"]); + }); + + it("uma tarefa que falha não trava a fila", async () => { + const q = serialQueue(); + const log: string[] = []; + q.add(() => Promise.reject(new Error("x"))); + await q.add(async () => void log.push("seguiu")); + expect(log).toEqual(["seguiu"]); + }); +}); diff --git a/src/lib/queue.ts b/src/lib/queue.ts new file mode 100644 index 0000000..bdbd9c3 --- /dev/null +++ b/src/lib/queue.ts @@ -0,0 +1,15 @@ +/** Fila de tarefas uma depois da outra; `clear` descarta as que ainda não começaram. */ +export function serialQueue() { + let tail: Promise = Promise.resolve(); + let generation = 0; + return { + add(task: () => Promise): Promise { + const gen = generation; + tail = tail.then(() => (gen === generation ? task().then(() => {}, () => {}) : undefined)); + return tail; + }, + clear() { + generation++; + }, + }; +}