@bojackduy/opencode-voice 0.11.2 → 0.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/index.js +1 -1
- package/lib/streaming-stt.js +20 -0
- package/lib/stt-log.js +39 -0
- package/lib/stt.js +221 -19
- package/lib/tts-player.js +356 -0
- package/lib/tts-status.js +266 -0
- package/lib/tts.js +661 -170
- package/lib/v2-compat.js +19 -0
- package/lib/v2-preview.js +141 -21
- package/package.json +1 -1
package/lib/v2-compat.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createV2Preview } from "./v2-preview.js";
|
|
2
|
+
import { createTtsStatus } from "./tts-status.js";
|
|
2
3
|
|
|
3
4
|
// OpenCode v2 compatibility shim.
|
|
4
5
|
//
|
|
@@ -206,6 +207,21 @@ export function adaptV2Context(ctx) {
|
|
|
206
207
|
const disposers = [];
|
|
207
208
|
const preview = createV2Preview(ctx);
|
|
208
209
|
disposers.push(() => preview.clear());
|
|
210
|
+
// v1 has no additive media surface: toasts stay the progress carrier
|
|
211
|
+
// there. v2 gets the persistent now-playing line (same slot pattern as
|
|
212
|
+
// the voice preview); tts.js drives it through api.ttsStatus and degrades
|
|
213
|
+
// to toasts when the surface is absent.
|
|
214
|
+
const ttsStatusReader = { current: () => null };
|
|
215
|
+
const ttsStatusActions = {};
|
|
216
|
+
const ttsStatus = createTtsStatus(ctx, () => ttsStatusReader.current(), ttsStatusActions);
|
|
217
|
+
const setTtsStatusReader = (fn) => {
|
|
218
|
+
ttsStatusReader.current = typeof fn === "function" ? fn : () => null;
|
|
219
|
+
};
|
|
220
|
+
const setTtsStatusActions = (next) => {
|
|
221
|
+
for (const key of Object.keys(ttsStatusActions)) delete ttsStatusActions[key];
|
|
222
|
+
if (next && typeof next === "object") Object.assign(ttsStatusActions, next);
|
|
223
|
+
};
|
|
224
|
+
disposers.push(() => ttsStatus.clear());
|
|
209
225
|
const warned = new Set();
|
|
210
226
|
const warnMessages = [];
|
|
211
227
|
let disposed = false;
|
|
@@ -371,6 +387,9 @@ export function adaptV2Context(ctx) {
|
|
|
371
387
|
// voicePreview uses additive slots without composer editing.
|
|
372
388
|
renderer: ctx.renderer ?? null,
|
|
373
389
|
voicePreview: preview,
|
|
390
|
+
ttsStatus,
|
|
391
|
+
setTtsStatusReader,
|
|
392
|
+
setTtsStatusActions,
|
|
374
393
|
ui: { toast, dialog, DialogSelect },
|
|
375
394
|
event: {
|
|
376
395
|
on: (type, fn) => {
|
package/lib/v2-preview.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
// Host-owned Solid/OpenTUI modules are loaded only when a v2 recording starts.
|
|
2
2
|
// No composer mutation: this bounded display is an additive, documented slot.
|
|
3
|
+
//
|
|
4
|
+
// Renders the live transcript and the live input waveform together: the same
|
|
5
|
+
// 16-column bar style used by the batch recording toast, sourced from the
|
|
6
|
+
// streaming capture's newest PCM tail. The waveform timer lives here so every
|
|
7
|
+
// stop path (`clear()`) cancels it in one place.
|
|
8
|
+
import { sttLog } from "./stt-log.js";
|
|
9
|
+
|
|
10
|
+
const WAVE_CHARS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
|
|
11
|
+
|
|
3
12
|
export function createV2Preview(
|
|
4
13
|
ctx,
|
|
5
14
|
load = async () => {
|
|
@@ -10,59 +19,170 @@ export function createV2Preview(
|
|
|
10
19
|
let generation = 0;
|
|
11
20
|
let active = false;
|
|
12
21
|
let partial = { stableText: "", tentativeText: "" };
|
|
22
|
+
let levels = null;
|
|
23
|
+
let applyLogCount = 0;
|
|
24
|
+
let waveProvider = null;
|
|
25
|
+
let waveTimer = null;
|
|
13
26
|
const nodes = new Set();
|
|
14
27
|
let setContent = null;
|
|
28
|
+
|
|
15
29
|
function display() {
|
|
16
30
|
const stable = partial.stableText || "";
|
|
17
31
|
const tentative = partial.tentativeText || "";
|
|
18
|
-
|
|
19
|
-
|
|
32
|
+
const body =
|
|
33
|
+
`Voice live · ${stable ? `${stable} ` : ""}${tentative ? `[${tentative}]` : stable ? "" : "Listening…"}`.slice(
|
|
34
|
+
-1580,
|
|
35
|
+
);
|
|
36
|
+
const wave = levels
|
|
37
|
+
? `${levels.map((level) => WAVE_CHARS[level] ?? WAVE_CHARS[0]).join("")} `
|
|
38
|
+
: "";
|
|
39
|
+
return `${wave}${body}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function redraw() {
|
|
43
|
+
for (const node of nodes) {
|
|
44
|
+
if (node.isDestroyed) nodes.delete(node);
|
|
45
|
+
else setContent?.(node, display());
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function applyLevels(next) {
|
|
50
|
+
if (!active || !Array.isArray(next) || next.length === 0) return;
|
|
51
|
+
if (applyLogCount < 3) {
|
|
52
|
+
applyLogCount += 1;
|
|
53
|
+
sttLog("preview.levels", `max=${Math.max(...next)} n=${next.length}`);
|
|
54
|
+
}
|
|
55
|
+
levels = next.map((value) => {
|
|
56
|
+
const level = Math.trunc(Number(value));
|
|
57
|
+
if (!Number.isFinite(level)) return 0;
|
|
58
|
+
return Math.max(0, Math.min(WAVE_CHARS.length - 1, level));
|
|
59
|
+
});
|
|
60
|
+
redraw();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function stopWave() {
|
|
64
|
+
if (waveTimer) {
|
|
65
|
+
clearInterval(waveTimer);
|
|
66
|
+
waveTimer = null;
|
|
67
|
+
}
|
|
68
|
+
waveProvider = null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function startWave(provider, intervalMs = 80) {
|
|
72
|
+
stopWave();
|
|
73
|
+
sttLog("preview.wave", active && typeof provider === "function" ? "start" : "rejected");
|
|
74
|
+
if (!active || typeof provider !== "function") return;
|
|
75
|
+
waveProvider = provider;
|
|
76
|
+
const timer = setInterval(
|
|
77
|
+
() => {
|
|
78
|
+
if (!active || !waveProvider) return;
|
|
79
|
+
try {
|
|
80
|
+
const next = waveProvider();
|
|
81
|
+
if (next) applyLevels(next);
|
|
82
|
+
} catch {}
|
|
83
|
+
},
|
|
84
|
+
Math.max(20, Math.floor(Number(intervalMs) || 80)),
|
|
20
85
|
);
|
|
86
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
87
|
+
waveTimer = timer;
|
|
21
88
|
}
|
|
89
|
+
|
|
22
90
|
let unclaims = [];
|
|
23
91
|
function clear() {
|
|
24
92
|
generation += 1;
|
|
25
93
|
active = false;
|
|
26
94
|
partial = { stableText: "", tentativeText: "" };
|
|
95
|
+
levels = null;
|
|
96
|
+
applyLogCount = 0;
|
|
97
|
+
stopWave();
|
|
27
98
|
nodes.clear();
|
|
28
99
|
setContent = null;
|
|
100
|
+
renderCalls = 0;
|
|
29
101
|
for (const unclaim of unclaims.splice(0)) unclaim();
|
|
30
102
|
}
|
|
103
|
+
let renderErrorShown = false;
|
|
104
|
+
let renderCalls = 0;
|
|
105
|
+
function loudRenderError(err) {
|
|
106
|
+
sttLog("preview.render-error", err?.message || String(err));
|
|
107
|
+
if (renderErrorShown) return;
|
|
108
|
+
renderErrorShown = true;
|
|
109
|
+
try {
|
|
110
|
+
ctx.ui?.toast?.show?.({
|
|
111
|
+
message: `Voice live preview failed: ${err?.message || err}`,
|
|
112
|
+
variant: "error",
|
|
113
|
+
duration: 5000,
|
|
114
|
+
});
|
|
115
|
+
} catch {}
|
|
116
|
+
}
|
|
31
117
|
return {
|
|
32
118
|
async begin() {
|
|
33
119
|
clear();
|
|
34
120
|
active = true;
|
|
121
|
+
renderErrorShown = false;
|
|
35
122
|
const mine = generation;
|
|
36
|
-
|
|
123
|
+
let createElement;
|
|
124
|
+
let setProp;
|
|
125
|
+
let insertNode;
|
|
126
|
+
let onCleanup;
|
|
127
|
+
try {
|
|
128
|
+
({ createElement, setProp, insertNode, onCleanup } = await load());
|
|
129
|
+
} catch (loadErr) {
|
|
130
|
+
sttLog("preview.begin-failed", loadErr?.message || String(loadErr));
|
|
131
|
+
loudRenderError(loadErr);
|
|
132
|
+
throw loadErr;
|
|
133
|
+
}
|
|
37
134
|
if (!active || mine !== generation) return false;
|
|
135
|
+
sttLog("preview.begin-ok");
|
|
38
136
|
setContent = (node, value) => setProp(node, "content", value);
|
|
137
|
+
const themeText = ctx.theme?.text;
|
|
138
|
+
const fg =
|
|
139
|
+
typeof themeText === "string"
|
|
140
|
+
? themeText
|
|
141
|
+
: themeText?.base || themeText?.muted || "#eeeeee";
|
|
39
142
|
const render = () => {
|
|
40
143
|
if (!active || mine !== generation) return null;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
144
|
+
renderCalls += 1;
|
|
145
|
+
if (renderCalls === 1) sttLog("preview.render-first");
|
|
146
|
+
try {
|
|
147
|
+
const box = createElement("box");
|
|
148
|
+
setProp(box, "height", 3);
|
|
149
|
+
setProp(box, "flexShrink", 0);
|
|
150
|
+
setProp(box, "width", "100%");
|
|
151
|
+
const node = createElement("text");
|
|
152
|
+
setProp(node, "maxHeight", 5);
|
|
153
|
+
setProp(node, "minHeight", 1);
|
|
154
|
+
setProp(node, "width", "100%");
|
|
155
|
+
setProp(node, "fg", fg);
|
|
156
|
+
setProp(node, "content", display());
|
|
157
|
+
nodes.add(node);
|
|
158
|
+
insertNode(box, node);
|
|
159
|
+
onCleanup?.(() => nodes.delete(node));
|
|
160
|
+
return box;
|
|
161
|
+
} catch (renderErr) {
|
|
162
|
+
loudRenderError(renderErr);
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
54
165
|
};
|
|
55
|
-
|
|
166
|
+
// Vertical slots only: prompt.footer is a horizontal row (zero-width
|
|
167
|
+
// text) and is absent on the home screen.
|
|
168
|
+
unclaims = [
|
|
169
|
+
ctx.ui.slot({ before: "session.composer.top", render }),
|
|
170
|
+
ctx.ui.slot({ before: "home.footer", render }),
|
|
171
|
+
];
|
|
56
172
|
return true;
|
|
57
173
|
},
|
|
58
174
|
applyPartial(p) {
|
|
59
175
|
if (!active) return;
|
|
60
176
|
partial = { stableText: p?.stableText || "", tentativeText: p?.tentativeText || "" };
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
177
|
+
sttLog(
|
|
178
|
+
"preview.partial",
|
|
179
|
+
`stable=${partial.stableText.length} tentative=${partial.tentativeText.length} nodes=${nodes.size}`,
|
|
180
|
+
);
|
|
181
|
+
redraw();
|
|
65
182
|
},
|
|
183
|
+
applyLevels,
|
|
184
|
+
startWave,
|
|
185
|
+
stopWave,
|
|
66
186
|
getTranscript: () => [partial.stableText, partial.tentativeText].filter(Boolean).join(" "),
|
|
67
187
|
finalize(text) {
|
|
68
188
|
clear();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bojackduy/opencode-voice",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.3",
|
|
4
4
|
"description": "Speech-to-text and text-to-speech for OpenCode. Record voice prompts with whisper-cpp, hear responses via Piper TTS, with LLM normalization through any OpenAI-compatible endpoint.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"opencode",
|