dankvoice: speak long answers as sequential sentence-sized requests
agent_end awaited one whole-answer synthesis; long answers hit the harness 30s extension-handler timeout (handler timed out after 30000ms) and the single frank-tts request was the stall. Port the built-in speak-aloud vocalizer's utterance segmenter (fHe in dist/cli.js) so segmentation matches the known-working stock path: [.!?…]-run boundaries, abbreviation/decimal/inline-code guards, fenced code and tables swallowed, markup cleaned. Play segment N to player exit before synthesizing N+1 (audio never stacks); fire-and-forget the chain so utterances outlive the handler timeout; a run token retires superseded chains. DANKVOICE_MAX becomes a per-answer budget of whole segments; stripForSpeech is gone. Refs #23 follow-up.
This commit is contained in:
+355
-41
@@ -28,14 +28,21 @@
|
||||
* DANKVOICE_CONFIG settings file (default ~/.omp/cache/dankvoice.json)
|
||||
* DANKVOICE_PLAYER player binary, skipping detection
|
||||
* DANKVOICE_VOICE voice name ("" = server default)
|
||||
* DANKVOICE_MAX spoken-character cap per utterance (default 700)
|
||||
* DANKVOICE_MAX spoken-character budget per answer (default 700)
|
||||
* DANKVOICE_AUTO=1 start with auto-speak armed
|
||||
*
|
||||
* Utterances are synthesized to a temp WAV in $TMPDIR; the file is removed
|
||||
* when its player process exits. A new utterance interrupts the previous one
|
||||
* instead of queueing behind it. Output routing: a saved sink becomes
|
||||
* `pw-play --target <sink>` or `paplay --device=<sink>` (ffplay has no device
|
||||
* concept and ignores it); with no saved sink the player's own default applies.
|
||||
* Long answers are cut into speakable segments by a state machine ported
|
||||
* from omp's built-in vocalizer: strong boundaries on [.!?…] runs followed
|
||||
* by whitespace (so "!!!!!" and "..." stay one boundary), guards for
|
||||
* abbreviations and decimals, code fences and tables swallowed, markdown
|
||||
* cleaned. Each segment is synthesized to a temp WAV in $TMPDIR and played
|
||||
* to completion before the next request goes out — requests are strictly
|
||||
* sequential, so audio never stacks. The chain runs detached from the event
|
||||
* handler, so an utterance may outlive the harness handler timeout. A new
|
||||
* utterance interrupts the previous one. Output routing: a saved sink
|
||||
* becomes `pw-play --target <sink>` or `paplay --device=<sink>` (ffplay has
|
||||
* no device concept and ignores it); with no saved sink the player's own
|
||||
* default applies.
|
||||
*/
|
||||
|
||||
import { spawn, spawnSync, type ChildProcess } from "node:child_process";
|
||||
@@ -185,25 +192,305 @@ export function playerFor(command: string, sink: string): Player {
|
||||
return { command, args: ["--target", sink] };
|
||||
}
|
||||
|
||||
/** What a speech engine should say: prose only, no fences, no markup, bounded. */
|
||||
export function stripForSpeech(text: string, maxChars: number = DEFAULT_MAX_CHARS): string {
|
||||
let out = text;
|
||||
out = out.replace(/```[\s\S]*?```/g, " ");
|
||||
out = out.replace(/~~~[\s\S]*?~~~/g, " ");
|
||||
out = out.replace(/`([^`]*)`/g, "$1");
|
||||
out = out.replace(/!\[[^\]]*\]\([^)]*\)/g, " ");
|
||||
out = out.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1");
|
||||
out = out.replace(/^\s{0,3}#{1,6}\s+/gm, "");
|
||||
out = out.replace(/^\s*[-*+]\s+/gm, "");
|
||||
out = out.replace(/^\s*\|[-: |]+\|\s*$/gm, " ");
|
||||
out = out.replace(/\|/g, " ");
|
||||
out = out.replace(/[*_]{1,3}([^*_]+)[*_]{1,3}/g, "$1");
|
||||
out = out.replace(/\s+/g, " ").trim();
|
||||
if (out.length <= maxChars) return out;
|
||||
const window = out.slice(0, maxChars);
|
||||
const cut = Math.max(window.lastIndexOf(". "), window.lastIndexOf("! "), window.lastIndexOf("? "));
|
||||
const clipped = cut > maxChars / 2 ? window.slice(0, cut + 1) : window.replace(/[,;:]\s*\S*$/, "");
|
||||
return `${clipped.trim()} …`;
|
||||
// ------------------------------------------------------------ segmenting
|
||||
//
|
||||
// Ported from omp's built-in speak-aloud vocalizer (the `fHe` segmenter and
|
||||
// its helpers in dist/cli.js). The same boundary rules as the stock path,
|
||||
// kept deliberately: push() takes text and returns the segments that are
|
||||
// complete, flush() ends the message.
|
||||
|
||||
const SENTENCE_END = /[.!?\u2026]+[)\]"'\u00BB\u201D\u2019]*\s/g;
|
||||
const WEAK_BREAK = /[,;:\u2014\u2013]\s/g;
|
||||
const ABBREVIATION = /(?:^|\s)(?:e\.g|i\.e|etc|vs|Mr|Mrs|Ms|Dr|St|No)\.$/i;
|
||||
const LINE_HEAD = /^(?:#{1,6}|[-*+]|-{2,}|\*{2,}|_{2,}|\d{1,3}|\d{1,3}[.)]|>+|`{1,2}|~{1,2})$/;
|
||||
const HORIZONTAL_RULE = /^(?:-{3,}|\*{3,}|_{3,})\s*$/;
|
||||
|
||||
const IMAGE_LINK = /!\[([^\]]*)\]\(([^()]*)\)/g;
|
||||
const LINK = /\[([^\]]+)\]\(([^()]*)\)/g;
|
||||
const BRACKETED_URL = /<(https?:\/\/[^\s>]+)>/g;
|
||||
const BARE_URL = /\bhttps?:\/\/[^\s<>()"'\]]+|\bwww\.[\w-]+(?:\.[\w-]+)+[^\s<>()"'\]]*/g;
|
||||
const INLINE_CODE = /`{1,2}([^`]+)`{1,2}/g;
|
||||
const BOLD_MARKUP = /\*\*|__|~~/g;
|
||||
const STAR = /\*(?=\S)|(?<=\S)\*/g;
|
||||
const UNDERSCORE = /(^|\s)_+|_+(?=\s|$)/g;
|
||||
const HTML_TAG = /<\/?[a-zA-Z][^<>]*>/g;
|
||||
const EMPHASIS_RUN = /(^|\s)[-*_]{3,}(?=\s|$)/g;
|
||||
const DEEP_PATH = /(^|[\s("'`])((?:~|\.{1,2})?\/?[\w.@+-]+(?:\/[\w.@+-]+){2,}\/?)/g;
|
||||
const HAS_WORD = /[\p{L}\p{N}]/u;
|
||||
|
||||
/** Speak a URL by host and a file path by its last segment. */
|
||||
export function spokenDomain(url: string): string {
|
||||
return url.replace(/^[a-z][\w+.-]*:\/\//i, "").replace(/^www\./i, "").replace(/[/?#].*$/, "");
|
||||
}
|
||||
|
||||
/** Final per-segment cleanup; empty when nothing speakable survived. */
|
||||
export function cleanForSpeech(text: string): string {
|
||||
const out = text
|
||||
.replace(IMAGE_LINK, "$1")
|
||||
.replace(LINK, "$1")
|
||||
.replace(BRACKETED_URL, (_match, url: string) => spokenDomain(url))
|
||||
.replace(BARE_URL, (url: string) => spokenDomain(url))
|
||||
.replace(INLINE_CODE, "$1")
|
||||
.replace(BOLD_MARKUP, "")
|
||||
.replace(STAR, "")
|
||||
.replace(UNDERSCORE, "$1")
|
||||
.replace(HTML_TAG, " ")
|
||||
.replace(EMPHASIS_RUN, "$1")
|
||||
.replace(DEEP_PATH, (_match, pre: string, path: string) => {
|
||||
const parts = path.split("/").filter((part) => part.length > 0);
|
||||
return pre + (parts[parts.length - 1] ?? path);
|
||||
})
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
return HAS_WORD.test(out) ? out : "";
|
||||
}
|
||||
|
||||
function sentenceBoundary(text: string, minChars: number): number {
|
||||
SENTENCE_END.lastIndex = 0;
|
||||
for (let match = SENTENCE_END.exec(text); match; match = SENTENCE_END.exec(text)) {
|
||||
const end = match.index + match[0].length;
|
||||
if (end < minChars) continue;
|
||||
if (ABBREVIATION.test(text.slice(0, end).trimEnd())) continue;
|
||||
if ((text.slice(0, end).match(/`/g)?.length ?? 0) % 2 !== 0) continue;
|
||||
return end;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function firstWeakBreak(text: string, minChars: number): number {
|
||||
WEAK_BREAK.lastIndex = 0;
|
||||
for (let match = WEAK_BREAK.exec(text); match; match = WEAK_BREAK.exec(text)) {
|
||||
const end = match.index + match[0].length;
|
||||
if (end >= minChars) return end;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function lastWeakBreakWithin(text: string, minChars: number, maxChars: number): number {
|
||||
WEAK_BREAK.lastIndex = 0;
|
||||
let found = -1;
|
||||
for (let match = WEAK_BREAK.exec(text); match; match = WEAK_BREAK.exec(text)) {
|
||||
const end = match.index + match[0].length;
|
||||
if (end > maxChars) break;
|
||||
if (end >= minChars) found = end;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
function wordWrapCut(text: string, maxChars: number): number {
|
||||
const space = text.lastIndexOf(" ", maxChars);
|
||||
return space > 0 ? space + 1 : Math.min(maxChars, text.length);
|
||||
}
|
||||
|
||||
type LineHead =
|
||||
| { kind: "swallow" }
|
||||
| { kind: "fence"; fence: string }
|
||||
| { kind: "marker"; spoken: string }
|
||||
| { kind: "prose"; text: string }
|
||||
| { kind: "undecided" };
|
||||
|
||||
/** Classify a line start: tables and fences swallow, list markers get spoken. */
|
||||
export function classifyLineHead(head: string): LineHead {
|
||||
if (head === "|") return { kind: "swallow" };
|
||||
if (/^(?:`{3}|~{3})/.test(head)) return { kind: "fence", fence: head.slice(0, 3) };
|
||||
if (/^#{1,6}[ \t]/.test(head)) return { kind: "marker", spoken: "" };
|
||||
if (/^[-*+][ \t]/.test(head)) return { kind: "marker", spoken: "" };
|
||||
const ordered = /^(\d{1,3})[.)][ \t]/.exec(head);
|
||||
if (ordered) return { kind: "marker", spoken: `${ordered[1]}, ` };
|
||||
if (/^>+/.test(head) && !/^>+$/.test(head)) return { kind: "prose", text: head.replace(/^>+[ \t]?/, "") };
|
||||
if (LINE_HEAD.test(head)) return { kind: "undecided" };
|
||||
return { kind: "prose", text: head };
|
||||
}
|
||||
|
||||
type SegmenterState = "linestart" | "prose" | "swallow" | "code";
|
||||
|
||||
/** The built-in vocalizer's segmenter: streamed text in, speakable segments out. */
|
||||
export class SpeechSegmenter {
|
||||
#state: SegmenterState = "linestart";
|
||||
#head = "";
|
||||
#fence = "";
|
||||
#fenceLine = "";
|
||||
#resume: SegmenterState = "linestart";
|
||||
#buffer = "";
|
||||
#started = false;
|
||||
|
||||
push(chunk: string): string[] {
|
||||
const out: string[] = [];
|
||||
for (const char of chunk) this.#feed(char, out);
|
||||
this.#split(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
flush(): string[] {
|
||||
const out: string[] = [];
|
||||
if (this.#state === "linestart" && this.#head.length > 0 && !HORIZONTAL_RULE.test(this.#head)) this.#buffer += this.#head;
|
||||
this.#head = "";
|
||||
this.#state = "linestart";
|
||||
this.#emit(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Streaming idle flush: only cut a tail that already looks finished. */
|
||||
flushIdle(): string[] {
|
||||
const out: string[] = [];
|
||||
const tail = this.#buffer.trimEnd();
|
||||
if (!/[.!?\u2026][)\]"'\u00BB\u201D\u2019]*$/.test(tail) && tail.length < 24) return out;
|
||||
this.#emit(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
#feed(char: string, out: string[]): void {
|
||||
switch (this.#state) {
|
||||
case "linestart":
|
||||
this.#feedLineStart(char, out);
|
||||
return;
|
||||
case "prose":
|
||||
if (char === "\n") this.#endLine(out);
|
||||
else this.#buffer += char;
|
||||
return;
|
||||
case "swallow":
|
||||
if (char === "\n") this.#state = this.#resume;
|
||||
return;
|
||||
case "code":
|
||||
this.#feedCode(char);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#feedLineStart(char: string, out: string[]): void {
|
||||
if (char === "\n") {
|
||||
const head = this.#head;
|
||||
this.#head = "";
|
||||
if (head.length > 0 && !HORIZONTAL_RULE.test(head)) this.#buffer += head;
|
||||
this.#endLine(out);
|
||||
return;
|
||||
}
|
||||
this.#head += char;
|
||||
const classified = classifyLineHead(this.#head);
|
||||
if (classified.kind === "undecided") {
|
||||
if (this.#head.length > 8) {
|
||||
this.#buffer += this.#head;
|
||||
this.#head = "";
|
||||
this.#state = "prose";
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.#head = "";
|
||||
switch (classified.kind) {
|
||||
case "prose":
|
||||
this.#buffer += classified.text;
|
||||
this.#state = "prose";
|
||||
return;
|
||||
case "marker":
|
||||
this.#buffer += classified.spoken;
|
||||
this.#state = "prose";
|
||||
return;
|
||||
case "swallow":
|
||||
this.#state = "swallow";
|
||||
this.#resume = "linestart";
|
||||
return;
|
||||
case "fence":
|
||||
this.#fence = classified.fence;
|
||||
this.#fenceLine = "";
|
||||
this.#state = "swallow";
|
||||
this.#resume = "code";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#feedCode(char: string): void {
|
||||
if (char === "\n") {
|
||||
this.#fenceLine = "";
|
||||
return;
|
||||
}
|
||||
if (this.#fenceLine.length < 3) {
|
||||
this.#fenceLine += char;
|
||||
if (this.#fenceLine === this.#fence) {
|
||||
this.#state = "swallow";
|
||||
this.#resume = "linestart";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endLine(out: string[]): void {
|
||||
this.#state = "linestart";
|
||||
this.#emit(out);
|
||||
}
|
||||
|
||||
#emit(out: string[]): void {
|
||||
this.#split(out);
|
||||
const segment = this.#buffer;
|
||||
this.#buffer = "";
|
||||
this.#pushSegment(segment, out);
|
||||
}
|
||||
|
||||
#split(out: string[]): void {
|
||||
for (;;) {
|
||||
const buffer = this.#buffer;
|
||||
const boundary = sentenceBoundary(buffer, this.#started ? 24 : 12);
|
||||
if (boundary !== -1 && boundary <= 280) {
|
||||
this.#cut(boundary, out);
|
||||
continue;
|
||||
}
|
||||
if (!this.#started && buffer.length >= 40) {
|
||||
const weak = firstWeakBreak(buffer, 12);
|
||||
if (weak !== -1 && weak <= 140) {
|
||||
this.#cut(weak, out);
|
||||
continue;
|
||||
}
|
||||
if (buffer.length >= 140) {
|
||||
this.#cut(wordWrapCut(buffer, 140), out);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (this.#started && buffer.length >= 160) {
|
||||
const weak = lastWeakBreakWithin(buffer, 24, 160);
|
||||
if (weak !== -1) {
|
||||
this.#cut(weak, out);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (buffer.length > 280) {
|
||||
const weak = lastWeakBreakWithin(buffer, 24, 280);
|
||||
this.#cut(weak !== -1 ? weak : wordWrapCut(buffer, 280), out);
|
||||
continue;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#cut(end: number, out: string[]): void {
|
||||
const head = this.#buffer.slice(0, end);
|
||||
this.#buffer = this.#buffer.slice(end);
|
||||
this.#pushSegment(head, out);
|
||||
}
|
||||
|
||||
#pushSegment(raw: string, out: string[]): void {
|
||||
const cleaned = cleanForSpeech(raw);
|
||||
if (!cleaned) return;
|
||||
out.push(cleaned);
|
||||
this.#started = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** A whole message to speakable segments: the segmenter run start to end. */
|
||||
export function segmentsForSpeech(text: string): string[] {
|
||||
const segmenter = new SpeechSegmenter();
|
||||
const segments = segmenter.push(text);
|
||||
for (const tail of segmenter.flush()) segments.push(tail);
|
||||
return segments;
|
||||
}
|
||||
|
||||
/** The leading whole segments that fit the user's spoken-character budget. */
|
||||
export function budgetSegments(segments: string[], maxChars: number): string[] {
|
||||
const chosen: string[] = [];
|
||||
let used = 0;
|
||||
for (const segment of segments) {
|
||||
if (used + segment.length > maxChars) break;
|
||||
used += segment.length;
|
||||
chosen.push(segment);
|
||||
}
|
||||
return chosen;
|
||||
}
|
||||
|
||||
/** The text the assistant last actually said; tool-only messages do not count. */
|
||||
@@ -408,14 +695,16 @@ function startPlayback(player: Player, file: string, dir: string): ChildProcess
|
||||
|
||||
export default function dankvoice(pi: ExtensionAPI) {
|
||||
const cfg = loadConfig();
|
||||
const state: { auto: boolean; voice: string; child?: ChildProcess; lastCtx?: ExtensionContext } = {
|
||||
const state: { auto: boolean; voice: string; child?: ChildProcess; run: number; lastCtx?: ExtensionContext } = {
|
||||
auto: cfg.auto,
|
||||
voice: cfg.voice,
|
||||
run: 0,
|
||||
};
|
||||
const voiceLabel = () => state.voice || "server default";
|
||||
const reflectStatus = () => state.lastCtx?.ui.setStatus("dankvoice", state.auto ? `🔊 ${voiceLabel()}` : undefined);
|
||||
|
||||
const stop = () => {
|
||||
state.run += 1; // retire the live chain; its loop stops at the next gate
|
||||
if (state.child && state.child.exitCode === null) state.child.kill("SIGTERM");
|
||||
state.child = undefined;
|
||||
};
|
||||
@@ -455,9 +744,12 @@ export default function dankvoice(pi: ExtensionAPI) {
|
||||
persistSettings(ctx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Speak one answer as strictly sequential requests: synthesize a segment,
|
||||
* play it to the end, only then ask for the next — audio never stacks.
|
||||
* Superseded chains (new utterance, /say stop, disarm) exit at the gates.
|
||||
*/
|
||||
async function speak(ctx: ExtensionContext, text: string) {
|
||||
const spoken = stripForSpeech(text, cfg.maxChars);
|
||||
if (!spoken) return;
|
||||
if (!cfg.target) {
|
||||
ctx.ui.notify(missingTarget(), "error");
|
||||
return;
|
||||
@@ -466,19 +758,36 @@ export default function dankvoice(pi: ExtensionAPI) {
|
||||
ctx.ui.notify("dankvoice: no audio player found (pw-play, paplay or ffplay)", "error");
|
||||
return;
|
||||
}
|
||||
const target = cfg.target;
|
||||
const player = cfg.player;
|
||||
const segments = budgetSegments(segmentsForSpeech(text), cfg.maxChars);
|
||||
if (segments.length === 0) return;
|
||||
stop();
|
||||
const dir = mkdtempSync(join(tmpdir(), "dankvoice-"));
|
||||
try {
|
||||
const file = await synthesize(cfg.target, speechBody(cfg.target.model, spoken, state.voice), dir, SYNTH_TIMEOUT_MS);
|
||||
state.child = startPlayback(cfg.player, file, dir);
|
||||
} catch (error) {
|
||||
stop();
|
||||
const run = state.run;
|
||||
for (const segment of segments) {
|
||||
if (state.run !== run) return;
|
||||
const dir = mkdtempSync(join(tmpdir(), "dankvoice-"));
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// ignore
|
||||
const file = await synthesize(target, speechBody(target.model, segment, state.voice), dir, SYNTH_TIMEOUT_MS);
|
||||
if (state.run !== run) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
return;
|
||||
}
|
||||
await new Promise<void>((resolve) => {
|
||||
const child = startPlayback(player, file, dir);
|
||||
state.child = child;
|
||||
child.once("exit", () => resolve());
|
||||
child.once("error", () => resolve());
|
||||
});
|
||||
} catch (error) {
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// temp files are not worth a toast
|
||||
}
|
||||
ctx.ui.notify(`dankvoice: ${error instanceof Error ? error.message : String(error)}`, "error");
|
||||
return;
|
||||
}
|
||||
ctx.ui.notify(`dankvoice: ${error instanceof Error ? error.message : String(error)}`, "error");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -603,13 +912,13 @@ export default function dankvoice(pi: ExtensionAPI) {
|
||||
);
|
||||
return;
|
||||
case "speak":
|
||||
await speak(ctx, action.text);
|
||||
void speak(ctx, action.text).catch((error) => ctx.ui.notify(`dankvoice: ${error instanceof Error ? error.message : String(error)}`, "error"));
|
||||
return;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
pi.on("agent_end", async (event, ctx) => {
|
||||
pi.on("agent_end", (event, ctx) => {
|
||||
state.lastCtx = ctx;
|
||||
if (!state.auto) return;
|
||||
// `willContinue` means the harness already scheduled a continuation
|
||||
@@ -617,7 +926,12 @@ export default function dankvoice(pi: ExtensionAPI) {
|
||||
// settle, so narrating it would speak an answer the model is about to redo.
|
||||
if (event.willContinue) return;
|
||||
if (ctx.mode === "print" || ctx.mode === "json") return;
|
||||
await speak(ctx, lastAssistantText(event.messages));
|
||||
// Fire-and-forget: a multi-segment utterance legitimately outlives the
|
||||
// harness's handler timeout, and blocking here would only produce a
|
||||
// spurious "handler timed out" toast while playback continues anyway.
|
||||
void speak(ctx, lastAssistantText(event.messages)).catch((error) =>
|
||||
ctx.ui.notify(`dankvoice: ${error instanceof Error ? error.message : String(error)}`, "error"),
|
||||
);
|
||||
});
|
||||
|
||||
pi.on("session_shutdown", () => {
|
||||
|
||||
Reference in New Issue
Block a user