@bojackduy/opencode-voice 0.11.1 → 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/lib/v2-compat.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createV2Preview } from "./v2-preview.js";
2
+ import { createTtsStatus } from "./tts-status.js";
2
3
 
3
- // OpenCode v2 compatibility shim (submit-only mode).
4
+ // OpenCode v2 compatibility shim.
4
5
  //
5
6
  // WHY this file exists: v2's TUI plugin `Context` renamed or replaced every
6
7
  // v1 `TuiPluginApi` surface this plugin uses (see the drift table in the
@@ -10,12 +11,15 @@ import { createV2Preview } from "./v2-preview.js";
10
11
  // presence, never version strings), and every surface with no v2 path
11
12
  // degrades loudly (warn log + honest toast), never silently.
12
13
  //
13
- // Non-negotiable product decision (user-accepted): v2 2.0.26 has no
14
- // plugin-reachable append-to-composer API (`tui.prompt.append` exists only
15
- // as a server→TUI event type, not a client method). So on v2, STT
16
- // transcribes and SUBMITS immediately via `session.prompt`. The adapted api
17
- // passes `renderer: null` so `appendTranscription` skips the focused-input
18
- // insert path and lands on the client shim below, which submits.
14
+ // Composer editing on v2: there is no plugin-reachable append-to-composer
15
+ // call (`tui.prompt.append` exists only as a server→TUI event type, not a
16
+ // client method), but `Context.renderer` IS the live `CliRenderer`. So the
17
+ // adapted api forwards it, and the shared `insertIntoFocusedInput` helper
18
+ // types at the cursor of whatever editable field currently has focus — the
19
+ // session composer, a dialog search box, or another plugin's input. Only an
20
+ // explicit submit (`/stt-submit`, `sttAutoSubmit`, conversation) presses
21
+ // Enter. With no editable field focused, text is retained in a recovery
22
+ // dialog instead of being sent as a prompt.
19
23
 
20
24
  // Event renames with no same-named v2 equivalent. Values are the v2 event
21
25
  // type to subscribe to instead (both verified present in the v2
@@ -203,6 +207,21 @@ export function adaptV2Context(ctx) {
203
207
  const disposers = [];
204
208
  const preview = createV2Preview(ctx);
205
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());
206
225
  const warned = new Set();
207
226
  const warnMessages = [];
208
227
  let disposed = false;
@@ -301,6 +320,20 @@ export function adaptV2Context(ctx) {
301
320
  return task;
302
321
  }
303
322
 
323
+ // Reached only when no focused renderable accepts text. Never redirect the
324
+ // transcript into a chat prompt the user did not ask for: keep it visible
325
+ // in a recovery dialog and let the caller report `retained`.
326
+ function retainTranscript(text) {
327
+ const captured = String(text || "");
328
+ try {
329
+ void ctx.ui.dialog.alert({
330
+ title: "Voice transcript — no text field focused",
331
+ message: captured,
332
+ });
333
+ } catch {}
334
+ return { data: null, retained: true };
335
+ }
336
+
304
337
  // `Object.create` (not spread) so client methods keep their `this`.
305
338
  // v2 `session.message` is a `{get}` namespace, not the v1 callable, so
306
339
  // the v1 call shape `client.session.message({sessionID, messageID})` is
@@ -323,14 +356,21 @@ export function adaptV2Context(ctx) {
323
356
  app: { value: { log: async () => {} } },
324
357
  tui: {
325
358
  value: {
326
- submitOnly: true,
327
- // Submit-only: append-for-edit is unimplementable on v2, so append
328
- // becomes submit. Accepts both v1 call shapes ({body:{text}} and
329
- // {text}) used by stt.js. The `submitted` marker lets the caller's
330
- // toast report a submit instead of an edit-step append.
359
+ // Marker for v1-shaped callers: this host has no composer-append API
360
+ // and dispatches prompts through `session.prompt` below (an explicit
361
+ // submit may resolve after the reply stream has started).
362
+ v2: true,
363
+ // Called only after `insertIntoFocusedInput` found no editable field.
364
+ // Accepts both v1 call shapes ({body:{text}} and {text}) used by
365
+ // stt.js. Explicit submits still go out via `session.prompt`; plain
366
+ // appends are retained visibly instead of being sent.
331
367
  appendPrompt: async (input) => {
332
- await submitTextAsPrompt(input?.body?.text ?? input?.text ?? "", input?.isCurrent);
333
- return { data: null, submitted: true };
368
+ const text = input?.body?.text ?? input?.text ?? "";
369
+ if (input?.submit === true) {
370
+ await submitTextAsPrompt(text, input?.isCurrent);
371
+ return { data: null, submitted: true };
372
+ }
373
+ return retainTranscript(text);
334
374
  },
335
375
  submitPrompt: async () => {},
336
376
  },
@@ -341,12 +381,15 @@ export function adaptV2Context(ctx) {
341
381
 
342
382
  const api = {
343
383
  client,
344
- // Submit-only enforcement: with no renderer, `appendTranscription`
345
- // skips `insertIntoFocusedInput` and lands on the client shim above,
346
- // which submits via `session.prompt`. Streaming display is separate:
384
+ // The live CliRenderer: `currentFocusedRenderable` lets the shared
385
+ // `insertIntoFocusedInput` helper type into the focused composer, dialog
386
+ // search box, or plugin input. Streaming display is separate:
347
387
  // voicePreview uses additive slots without composer editing.
348
- renderer: null,
388
+ renderer: ctx.renderer ?? null,
349
389
  voicePreview: preview,
390
+ ttsStatus,
391
+ setTtsStatusReader,
392
+ setTtsStatusActions,
350
393
  ui: { toast, dialog, DialogSelect },
351
394
  event: {
352
395
  on: (type, fn) => {
@@ -527,13 +570,44 @@ export function adaptV2Context(ctx) {
527
570
  },
528
571
  };
529
572
 
530
- // v1 reads `api.state.provider` as an array (`resolveVoiceProviderModel`
531
- // does `api?.state?.provider || []` then `.find`); v2 lists per location.
532
- // A getter keeps the array shape without touching callers.
573
+ // v1 reads `api.state.provider` as an array of `{models, env, options}`
574
+ // (`voice-model.js` / `llm-client.js`). v2 splits those across collections:
575
+ // providers carry NO models (they live in `location.model`), auth env names
576
+ // live on the provider's integration method `{type:"env", names}`, and the
577
+ // endpoint is `settings.baseURL` (`options` in v1). Recombining them here is
578
+ // what keeps /voice-model from seeing every provider as model-less.
533
579
  Object.defineProperty(api.state, "provider", {
534
580
  get() {
535
581
  try {
536
- return ctx.data.location.provider.list() || [];
582
+ const providers = ctx.data.location.provider.list() || [];
583
+ const models = ctx.data.location.model?.list?.() || [];
584
+ const integrations = ctx.data.location.integration?.list?.() || [];
585
+ const envByIntegration = new Map();
586
+ for (const integration of integrations) {
587
+ const names = (integration?.methods || [])
588
+ .filter((method) => method?.type === "env")
589
+ .flatMap((method) => method?.names || []);
590
+ if (names.length > 0) envByIntegration.set(integration.id, names);
591
+ }
592
+ const modelsByProvider = new Map();
593
+ for (const model of models) {
594
+ const providerID = model?.providerID;
595
+ if (!providerID) continue;
596
+ const list = modelsByProvider.get(providerID);
597
+ if (list) list.push(model);
598
+ else modelsByProvider.set(providerID, [model]);
599
+ }
600
+ return providers.map((provider) => {
601
+ const own = modelsByProvider.get(provider.id) || [];
602
+ return {
603
+ ...provider,
604
+ models: Object.fromEntries(own.map((model) => [model.id, model])),
605
+ env:
606
+ envByIntegration.get(provider.integrationID ?? provider.id) ??
607
+ (Array.isArray(provider.env) ? provider.env : []),
608
+ options: provider.settings || {},
609
+ };
610
+ });
537
611
  } catch {
538
612
  return [];
539
613
  }
@@ -554,7 +628,6 @@ export function adaptV2Context(ctx) {
554
628
  return {
555
629
  api,
556
630
  cleanup,
557
- submitOnly: true,
558
631
  warnMessages,
559
632
  onWarn: (sink) => void (warnSink = sink),
560
633
  };
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
- return `Voice live · ${stable ? `${stable} ` : ""}${tentative ? `[${tentative}]` : stable ? "" : "Listening…"}`.slice(
19
- -1600,
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
- const { createElement, setProp, insertNode, onCleanup } = await load();
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
- const box = createElement("box");
42
- setProp(box, "height", 3);
43
- setProp(box, "flexShrink", 0);
44
- const node = createElement("text");
45
- setProp(node, "maxHeight", 5);
46
- setProp(node, "minHeight", 1);
47
- setProp(node, "width", "100%");
48
- setProp(node, "fg", ctx.theme?.text || "#eeeeee");
49
- setProp(node, "content", display());
50
- nodes.add(node);
51
- insertNode(box, node);
52
- onCleanup?.(() => nodes.delete(node));
53
- return box;
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
- unclaims = [ctx.ui.slot({ before: "prompt.footer", render })];
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
- for (const node of nodes) {
62
- if (node.isDestroyed) nodes.delete(node);
63
- else setContent?.(node, display());
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.1",
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",