Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Stop the voice pane's setup wait on terminal states and speak each reply once

Fei2411879c65109463
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 07736272..b8e2e4a6 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -11009,6 +11009,24 @@ async function speechCatalog(speech) {
const provider = catalog.providers.find((item) => item.id === catalog.selection.providerId);
return { catalog, usable: USABLE.has(provider?.preparation?.phase) };
}
+async function requestPreparation(speech, providerId) {
+ try {
+ const result = await speech.prepare(providerId);
+ return result?.ok !== false;
+ } catch {
+ return false;
+ }
+}
+function preparationStep(catalog) {
+ const preparation = catalog.providers.find((item) => item.id === catalog.selection.providerId)?.preparation;
+ if (preparation === void 0) return { state: "done", failed: false };
+ if (USABLE.has(preparation.phase)) return { state: "usable" };
+ if (preparation.phase === "failed" || preparation.phase === "cancelled" || preparation.phase === "unprepared") {
+ return { state: "done", failed: preparation.phase === "failed" };
+ }
+ const pct = preparation.phase === "downloading" && preparation.totalBytes ? Math.floor(preparation.completedBytes / preparation.totalBytes * 100) : void 0;
+ return { state: "waiting", pct };
+}
function waveBytes(samples, rate = 16e3) {
const bytes = new Uint8Array(44 + samples.length * 2);
const view = new DataView(bytes.buffer);
@@ -14048,6 +14066,25 @@ var VOICE_LABELS = {
unsupported: "Voice chat is not available in this browser"
};
var CAPTURE_NOTES = { blocked: "Microphone access is blocked", unavailable: "Voice chat is not available in this browser", "too-long": "That was too long to recognize" };
+function watchReply(read, watch, consume) {
+ let off = () => {
+ };
+ let consumed = false;
+ const attempt = () => {
+ if (consumed) return;
+ const text = read();
+ if (text === void 0) return;
+ consumed = true;
+ off();
+ consume(text);
+ };
+ off = watch(attempt);
+ attempt();
+ return () => {
+ consumed = true;
+ off();
+ };
+}
function useMicLevel(enabled) {
const [level, setLevel] = (0, import_react34.useState)(0);
(0, import_react34.useEffect)(() => {
@@ -14244,22 +14281,18 @@ function VoiceMode({ sessionId, roster, actions, useSessions, useSessionStatus }
window.speechSynthesis.cancel();
window.speechSynthesis.speak(utterance);
};
- let off = () => {
- };
- const attempt = () => {
- const text = actions.closingReply(sessionId, turn2);
- if (text === void 0) return;
- off();
- if (text === null) {
- waiting.current = null;
- setPhase("listening");
- return;
+ return watchReply(
+ () => actions.closingReply(sessionId, turn2),
+ (listener) => actions.watchTurns(sessionId, listener),
+ (text) => {
+ if (text === null) {
+ waiting.current = null;
+ setPhase("listening");
+ return;
+ }
+ speak(text);
}
- speak(text);
- };
- off = actions.watchTurns(sessionId, attempt);
- attempt();
- return () => off();
+ );
}, [phase, running, lastReply]);
(0, import_react34.useEffect)(() => () => window.speechSynthesis?.cancel(), []);
const micLevel = useMicLevel(!speech && phase === "listening" && dictation.listening);
@@ -14281,21 +14314,20 @@ function VoiceMode({ sessionId, roster, actions, useSessions, useSessionStatus }
let timer;
const tick = async () => {
try {
- const { catalog: next, usable } = await speechCatalog(speech);
+ const { catalog: next } = await speechCatalog(speech);
if (stopped) return;
setCatalog(next);
- if (usable) {
+ const step = preparationStep(next);
+ if (step.state === "usable") {
setPhase("listening");
return;
}
- const provider = next.providers.find((item) => item.id === next.selection.providerId);
- const preparation = provider?.preparation;
- if (preparation?.phase === "failed") {
+ if (step.state === "done") {
setPhase("setup");
- setNote(t("Couldn't prepare voice input"));
+ if (step.failed) setNote(t("Couldn't prepare voice input"));
return;
}
- setNote(preparation?.phase === "downloading" && preparation.totalBytes ? t("Downloading the speech model… {pct}%", { pct: Math.floor(preparation.completedBytes / preparation.totalBytes * 100) }) : "");
+ setNote(step.pct === void 0 ? "" : t("Downloading the speech model… {pct}%", { pct: step.pct }));
} catch {
if (!stopped) {
setPhase("setup");
@@ -14319,9 +14351,11 @@ function VoiceMode({ sessionId, roster, actions, useSessions, useSessionStatus }
}
turn.current += 1;
setPhase("preparing");
- void Promise.resolve(openSetup.prepare()).catch(() => {
- setPhase("setup");
- setNote(t("Couldn't prepare voice input"));
+ void Promise.resolve(openSetup.prepare()).then((started) => {
+ if (started === false) {
+ setPhase("setup");
+ setNote(t("Couldn't prepare voice input"));
+ }
});
};
const caption = phase === "speaking" ? said : heard;
@@ -20939,7 +20973,7 @@ function apply(ctx) {
const navigation = ctx.get("pluginNavigation");
if (navigation !== void 0) return { navigate: () => navigation.openBundle(VOICE_BUNDLE) };
const speech = ctx.get("remote.speech");
- return speech === void 0 || providerId === void 0 ? null : { prepare: () => speech.prepare(providerId) };
+ return speech === void 0 || providerId === void 0 ? null : { prepare: () => requestPreparation(speech, providerId) };
};
actions.closingReply = (sessionId, turn) => {
try {
diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.js
index e481fb57..2a44df68 100644
--- a/ds-bot/src/client/index.js
+++ b/ds-bot/src/client/index.js
@@ -15,7 +15,7 @@ import { ComposerPlus, ComposerVoice, menuIcons } from './composer.js'
import { CHATS_SOURCE, atSources } from './at-menu.js'
import { BtwDock, BtwTurnTail } from './btw.js'
import { SIDE_CSS, sideSource } from './side-chat.js'
-import { VOICE_BUNDLE, closingReplyText } from './speech.js'
+import { VOICE_BUNDLE, closingReplyText, requestPreparation } from './speech.js'
import { sameVersion } from './dsh-offer.js'
import { QuestionCard, QuestionDock } from './questions.js'
import { AnsweredReply, WidgetDock, WIDGET_CSS } from './widget-cell.js'
@@ -641,7 +641,7 @@ export function apply(ctx) {
const navigation = ctx.get('pluginNavigation')
if (navigation !== undefined) return { navigate: () => navigation.openBundle(VOICE_BUNDLE) }
const speech = ctx.get('remote.speech')
- return speech === undefined || providerId === undefined ? null : { prepare: () => speech.prepare(providerId) }
+ return speech === undefined || providerId === undefined ? null : { prepare: () => requestPreparation(speech, providerId) }
}
// The full text the voice chat reads aloud: the turn's closing Assistant
// message, not the turn outline's bounded preview. undefined while the chat
diff --git a/ds-bot/src/client/speech.js b/ds-bot/src/client/speech.js
index 91821ff2..e2a1ed6e 100644
--- a/ds-bot/src/client/speech.js
+++ b/ds-bot/src/client/speech.js
@@ -12,6 +12,35 @@ export async function speechCatalog(speech) {
return { catalog, usable: USABLE.has(provider?.preparation?.phase) }
}
+// Ask the provider to prepare. A refused request resolves as a result, not a
+// rejection, so both shapes come back as false: no preparation task started.
+export async function requestPreparation(speech, providerId) {
+ try {
+ const result = await speech.prepare(providerId)
+ return result?.ok !== false
+ } catch {
+ return false
+ }
+}
+
+// One catalog poll while the voice pane waits on preparation: 'usable' starts
+// listening; 'done' is a terminal state that left the provider unusable —
+// cancelled, failed, back at unprepared, or the provider gone — where the
+// setup step shows again and can be retried; anything else keeps waiting,
+// 'pct' carrying download progress when the provider reports byte totals.
+export function preparationStep(catalog) {
+ const preparation = catalog.providers.find(item => item.id === catalog.selection.providerId)?.preparation
+ if (preparation === undefined) return { state: 'done', failed: false }
+ if (USABLE.has(preparation.phase)) return { state: 'usable' }
+ if (preparation.phase === 'failed' || preparation.phase === 'cancelled' || preparation.phase === 'unprepared') {
+ return { state: 'done', failed: preparation.phase === 'failed' }
+ }
+ const pct = preparation.phase === 'downloading' && preparation.totalBytes
+ ? Math.floor((preparation.completedBytes / preparation.totalBytes) * 100)
+ : undefined
+ return { state: 'waiting', pct }
+}
+
export function waveBytes(samples, rate = 16000) {
const bytes = new Uint8Array(44 + samples.length * 2)
const view = new DataView(bytes.buffer)
diff --git a/ds-bot/src/client/voice.js b/ds-bot/src/client/voice.js
index 39a67938..219e9387 100644
--- a/ds-bot/src/client/voice.js
+++ b/ds-bot/src/client/voice.js
@@ -6,7 +6,7 @@ import { speechApi, useDictation } from './composer.js'
import { Shimmer } from './cells.js'
import { useEscape, usePaneLeft } from './overlay-hooks.js'
import { t } from './i18n.js'
-import { openCapture, speechCatalog, transcribeWave, turnDetector } from './speech.js'
+import { openCapture, preparationStep, speechCatalog, transcribeWave, turnDetector } from './speech.js'
// Voice chat: listen, send what the user said, read the Bot's answer aloud, listen
// again. With DSH's Voice Input plugin on, its recognizer hears the user: one
@@ -20,6 +20,26 @@ const VOICE_LABELS = {
}
const CAPTURE_NOTES = { blocked: 'Microphone access is blocked', unavailable: 'Voice chat is not available in this browser', 'too-long': 'That was too long to recognize' }
+// Reads the reply until it lands, then consumes it once. The chat target can
+// publish synchronously inside `watch`, before it returns the disposer, so the
+// consumed flag — not the disposer — is what stops a second pass: a reply
+// already spoken is never spoken again.
+export function watchReply(read, watch, consume) {
+ let off = () => {}
+ let consumed = false
+ const attempt = () => {
+ if (consumed) return
+ const text = read()
+ if (text === undefined) return
+ consumed = true
+ off()
+ consume(text)
+ }
+ off = watch(attempt)
+ attempt()
+ return () => { consumed = true; off() }
+}
+
function useMicLevel(enabled) {
const [level, setLevel] = useState(0)
useEffect(() => {
@@ -202,17 +222,13 @@ export function VoiceMode({ sessionId, roster, actions, useSessions, useSessionS
// The outline commits at turn/end but only holds a bounded preview; the
// closing Assistant text can trail it, so the answer is read through the
// chat view's own changes until it lands (or the Session goes away).
- let off = () => {}
- const attempt = () => {
- const text = actions.closingReply(sessionId, turn)
- if (text === undefined) return
- off()
- if (text === null) { waiting.current = null; setPhase('listening'); return }
- speak(text)
- }
- off = actions.watchTurns(sessionId, attempt)
- attempt()
- return () => off()
+ return watchReply(
+ () => actions.closingReply(sessionId, turn),
+ listener => actions.watchTurns(sessionId, listener),
+ (text) => {
+ if (text === null) { waiting.current = null; setPhase('listening'); return }
+ speak(text)
+ })
}, [phase, running, lastReply])
useEffect(() => () => window.speechSynthesis?.cancel(), [])
const micLevel = useMicLevel(!speech && phase === 'listening' && dictation.listening)
@@ -233,16 +249,17 @@ export function VoiceMode({ sessionId, roster, actions, useSessions, useSessionS
let timer
const tick = async () => {
try {
- const { catalog: next, usable } = await speechCatalog(speech)
+ const { catalog: next } = await speechCatalog(speech)
if (stopped) return
setCatalog(next)
- if (usable) { setPhase('listening'); return }
- const provider = next.providers.find(item => item.id === next.selection.providerId)
- const preparation = provider?.preparation
- if (preparation?.phase === 'failed') { setPhase('setup'); setNote(t("Couldn't prepare voice input")); return }
- setNote(preparation?.phase === 'downloading' && preparation.totalBytes
- ? t('Downloading the speech model… {pct}%', { pct: Math.floor((preparation.completedBytes / preparation.totalBytes) * 100) })
- : '')
+ const step = preparationStep(next)
+ if (step.state === 'usable') { setPhase('listening'); return }
+ if (step.state === 'done') {
+ setPhase('setup')
+ if (step.failed) setNote(t("Couldn't prepare voice input"))
+ return
+ }
+ setNote(step.pct === undefined ? '' : t('Downloading the speech model… {pct}%', { pct: step.pct }))
} catch { if (!stopped) { setPhase('setup'); return } }
timer = setTimeout(tick, 1500)
}
@@ -254,7 +271,9 @@ export function VoiceMode({ sessionId, roster, actions, useSessions, useSessionS
if (openSetup?.navigate) { close(); openSetup.navigate(); return }
turn.current += 1
setPhase('preparing')
- void Promise.resolve(openSetup.prepare()).catch(() => { setPhase('setup'); setNote(t("Couldn't prepare voice input")) })
+ void Promise.resolve(openSetup.prepare()).then((started) => {
+ if (started === false) { setPhase('setup'); setNote(t("Couldn't prepare voice input")) }
+ })
}
const caption = phase === 'speaking' ? said : heard
const shownNote = note || dictation.note
diff --git a/ds-bot/test/speech.test.js b/ds-bot/test/speech.test.js
index 8de176b4..bb00c29c 100644
--- a/ds-bot/test/speech.test.js
+++ b/ds-bot/test/speech.test.js
@@ -1,6 +1,6 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
-import { base64Of, closingReplyText, speechCatalog, transcribeWave, turnDetector, waveBytes } from '../src/client/speech.js'
+import { base64Of, closingReplyText, preparationStep, requestPreparation, speechCatalog, transcribeWave, turnDetector, waveBytes } from '../src/client/speech.js'
const catalog = (phase, extra = {}) => ({
providers: [{ id: 'sensevoice', preparation: { phase } }],
@@ -38,6 +38,29 @@ test('speechCatalog is usable only when the selected provider is ready', async (
await assert.rejects(speechCatalog({ catalog: async () => ({ ok: false, error: { message: 'down' } }) }), /down/)
})
+test('requestPreparation is false on a refused request and on a throw', async () => {
+ let asked = 0
+ const speech = { prepare: async () => { asked += 1; return { ok: true } } }
+ assert.equal(await requestPreparation(speech, 'sensevoice'), true)
+ assert.equal(asked, 1)
+ // The remote reports refusal as a result, not a rejection: the pane must
+ // leave 'preparing' or it would wait on a task that never started.
+ assert.equal(await requestPreparation({ prepare: async () => ({ ok: false, error: { message: 'unavailable' } }) }), false)
+ assert.equal(await requestPreparation({ prepare: async () => { throw new Error('gone') } }), false)
+})
+
+test('preparationStep maps every catalog phase to the pane step', () => {
+ for (const phase of ['ready', 'standby', 'waking']) assert.equal(preparationStep(catalog(phase)).state, 'usable', phase)
+ for (const phase of ['downloading', 'checking', 'loading', 'cancelling']) assert.equal(preparationStep(catalog(phase)).state, 'waiting', phase)
+ // Every terminal non-usable state ends the wait so the setup step can retry.
+ for (const phase of ['cancelled', 'unprepared']) assert.deepEqual(preparationStep(catalog(phase)), { state: 'done', failed: false }, phase)
+ assert.deepEqual(preparationStep(catalog('failed')), { state: 'done', failed: true })
+ // The selected provider gone from the catalog is done too.
+ assert.deepEqual(preparationStep({ providers: [], selection: { providerId: 'sensevoice', language: 'auto' } }), { state: 'done', failed: false })
+ assert.deepEqual(preparationStep(catalog('downloading', { providers: [{ id: 'sensevoice', preparation: { phase: 'downloading', completedBytes: 25, totalBytes: 100 } }] })), { state: 'waiting', pct: 25 })
+ assert.deepEqual(preparationStep(catalog('downloading')), { state: 'waiting', pct: undefined })
+})
+
test('transcribeWave sends the selection and returns trimmed text', async () => {
let sent
const speech = { transcribe: async (request) => { sent = request; return { ok: true, value: { text: ' 你好 ' } } } }
diff --git a/ds-bot/test/voice.test.js b/ds-bot/test/voice.test.js
new file mode 100644
index 00000000..fb72a2c1
--- /dev/null
+++ b/ds-bot/test/voice.test.js
@@ -0,0 +1,57 @@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { runInNewContext } from 'node:vm'
+import { dirname } from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { build } from 'esbuild'
+
+// voice.js imports react; the client tests bundle it and stub react, as the
+// other client tests do.
+const bundle = await build({
+ stdin: { contents: "export { watchReply } from './src/client/voice.js'", resolveDir: dirname(dirname(fileURLToPath(import.meta.url))) },
+ bundle: true, format: 'cjs', platform: 'node', write: false, logLevel: 'silent',
+ external: ['react', 'react-dom', '@deepseek-ai/dsh-client-ui-primitives'],
+})
+const module = { exports: {} }
+const platform = {
+ react: {
+ createElement: () => null, Fragment: 'fragment',
+ useState: initial => [initial, () => {}], useEffect: () => {}, useMemo: make => make(), useCallback: fn => fn,
+ useRef: current => ({ current }), useId: () => 'id', useSyncExternalStore: (sub, get) => get(),
+ createContext: initial => ({ value: initial, Provider: () => null }), useContext: ctx => ctx.value,
+ },
+ 'react-dom': { createPortal: node => node },
+ '@deepseek-ai/dsh-client-ui-primitives': { MarkdownText: () => null },
+}
+runInNewContext(bundle.outputFiles[0].text, { module, exports: module.exports, require: name => platform[name] ?? {} })
+const { watchReply } = module.exports
+
+test('a reply published synchronously during subscribe is spoken once', () => {
+ const consumed = []
+ // DSH's target publishes on subscribe, before watch() returns the disposer.
+ const watch = (listener) => { listener(); return () => {} }
+ const off = watchReply(() => 'the answer', watch, text => consumed.push(text))
+ assert.deepEqual(consumed, ['the answer'])
+ off()
+})
+
+test('watchReply waits for the reply, speaks it once, then stops', () => {
+ const consumed = []
+ const calls = []
+ let text
+ const listeners = new Set()
+ const off = watchReply(
+ () => text,
+ (listener) => { listeners.add(listener); return () => listeners.delete(listener) },
+ (value) => { calls.push('consume'); consumed.push(value) })
+ assert.deepEqual(consumed, [])
+ text = 'first'
+ for (const listener of [...listeners]) listener()
+ assert.deepEqual(consumed, ['first'])
+ assert.equal(listeners.size, 0)
+ // A later publish must not speak the same reply again.
+ text = 'again'
+ assert.deepEqual(consumed, ['first'])
+ assert.equal(calls.length, 1)
+ off()
+})