@hxnnxs/opencode-voice 0.2.2 → 0.2.4

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/wav.js ADDED
@@ -0,0 +1,91 @@
1
+ import fs from "node:fs";
2
+
3
+ const RIFF_HEADER_BYTES = 12;
4
+ const CHUNK_HEADER_BYTES = 8;
5
+ const UINT32_MAX = 0xffffffff;
6
+
7
+ function readExactly(fd, buffer, position) {
8
+ return fs.readSync(fd, buffer, 0, buffer.length, position) === buffer.length;
9
+ }
10
+
11
+ function validChunkTail(fd, offset, end) {
12
+ const header = Buffer.alloc(CHUNK_HEADER_BYTES);
13
+ while (offset + CHUNK_HEADER_BYTES <= end) {
14
+ if (!readExactly(fd, header, offset)) return false;
15
+ if (!/^[\x20-\x7e]{4}$/.test(header.toString("latin1", 0, 4))) return false;
16
+ const size = header.readUInt32LE(4);
17
+ offset += CHUNK_HEADER_BYTES + size + (size % 2);
18
+ }
19
+ return offset === end;
20
+ }
21
+
22
+ export function repairUnfinalizedWavHeader(file) {
23
+ let fd;
24
+ try {
25
+ fd = fs.openSync(file, "r+");
26
+ } catch {
27
+ return false;
28
+ }
29
+
30
+ try {
31
+ const fileSize = fs.fstatSync(fd).size;
32
+ if (fileSize < RIFF_HEADER_BYTES || fileSize - 8 > UINT32_MAX) return false;
33
+
34
+ const riffHeader = Buffer.alloc(RIFF_HEADER_BYTES);
35
+ if (!readExactly(fd, riffHeader, 0)) return false;
36
+ if (riffHeader.toString("ascii", 0, 4) !== "RIFF" || riffHeader.toString("ascii", 8, 12) !== "WAVE") return false;
37
+
38
+ const declaredRiffSize = riffHeader.readUInt32LE(4);
39
+ let chunkOffset = RIFF_HEADER_BYTES;
40
+
41
+ while (chunkOffset + CHUNK_HEADER_BYTES <= fileSize) {
42
+ const chunkHeader = Buffer.alloc(CHUNK_HEADER_BYTES);
43
+ if (!readExactly(fd, chunkHeader, chunkOffset)) return false;
44
+
45
+ const chunkId = chunkHeader.toString("ascii", 0, 4);
46
+ const declaredChunkSize = chunkHeader.readUInt32LE(4);
47
+ const chunkDataOffset = chunkOffset + CHUNK_HEADER_BYTES;
48
+
49
+ if (chunkId === "data") {
50
+ const availableDataSize = fileSize - chunkDataOffset;
51
+ const declaredEnd = declaredRiffSize + 8;
52
+ const dataEnd = chunkDataOffset + declaredChunkSize + (declaredChunkSize % 2);
53
+ // A complete RIFF may contain other chunks after data, or bytes outside
54
+ // the RIFF form. Do not mistake either for recorded PCM.
55
+ const validForm = dataEnd <= declaredEnd && declaredEnd <= fileSize &&
56
+ validChunkTail(fd, dataEnd, declaredEnd);
57
+ if (validForm && (declaredChunkSize !== 0 || dataEnd === fileSize ||
58
+ validChunkTail(fd, dataEnd, fileSize))) return false;
59
+
60
+ const repairRiffSize = declaredRiffSize !== fileSize - 8;
61
+ const repairDataSize = declaredChunkSize > availableDataSize ||
62
+ (dataEnd < fileSize && !validChunkTail(fd, dataEnd, fileSize));
63
+ if (!repairRiffSize && !repairDataSize) return false;
64
+
65
+ const size = Buffer.alloc(4);
66
+ if (repairDataSize) {
67
+ size.writeUInt32LE(availableDataSize);
68
+ fs.writeSync(fd, size, 0, size.length, chunkOffset + 4);
69
+ }
70
+ if (repairRiffSize) {
71
+ size.writeUInt32LE(fileSize - 8);
72
+ fs.writeSync(fd, size, 0, size.length, 4);
73
+ }
74
+ return true;
75
+ }
76
+
77
+ const paddedChunkSize = declaredChunkSize + (declaredChunkSize % 2);
78
+ const nextChunkOffset = chunkDataOffset + paddedChunkSize;
79
+ if (nextChunkOffset > fileSize) return false;
80
+ chunkOffset = nextChunkOffset;
81
+ }
82
+
83
+ return false;
84
+ } catch {
85
+ return false;
86
+ } finally {
87
+ try {
88
+ fs.closeSync(fd);
89
+ } catch {}
90
+ }
91
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hxnnxs/opencode-voice",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Local voice input plugin for OpenCode",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "import": "./index.js"
20
20
  },
21
21
  "./tui": {
22
- "import": "./index.js"
22
+ "import": "./tui.js"
23
23
  },
24
24
  "./runtime": {
25
25
  "import": "./lib/engine.js"
@@ -33,6 +33,7 @@
33
33
  "bin/",
34
34
  "lib/",
35
35
  "index.js",
36
+ "tui.js",
36
37
  "CHANGELOG.md",
37
38
  "CONTRIBUTING.md",
38
39
  "PUBLISHING.md",
@@ -42,7 +43,8 @@
42
43
  "LICENSE"
43
44
  ],
44
45
  "scripts": {
45
- "check": "node --check index.js && node --check lib/models.js && node --check lib/handy-model-catalog.js && node --check lib/download.js && node --check lib/engine.js && node --check lib/engines.js && node --check bin/opencode-voice.js && node --check scripts/package-engine-asset.mjs && node --check scripts/build-engine-registry.mjs && node --check scripts/update-handy-model-catalog.mjs",
46
+ "check": "node --check index.js && node --check tui.js && node --check lib/models.js && node --check lib/handy-model-catalog.js && node --check lib/download.js && node --check lib/engine.js && node --check lib/engines.js && node --check lib/wav.js && node --check bin/opencode-voice.js && node --check scripts/package-engine-asset.mjs && node --check scripts/build-engine-registry.mjs && node --check scripts/update-handy-model-catalog.mjs && npm test",
47
+ "test": "node --test",
46
48
  "update-handy-model-catalog": "node scripts/update-handy-model-catalog.mjs",
47
49
  "prepack": "npm run check"
48
50
  },
@@ -54,11 +56,15 @@
54
56
  "provenance": true
55
57
  },
56
58
  "peerDependencies": {
57
- "@opencode-ai/plugin": ">=1.17.4 <2"
59
+ "@opencode-ai/plugin": ">=1.17.4 <2",
60
+ "@opencode/plugin": ">=2.0.26 <3"
58
61
  },
59
62
  "peerDependenciesMeta": {
60
63
  "@opencode-ai/plugin": {
61
64
  "optional": true
65
+ },
66
+ "@opencode/plugin": {
67
+ "optional": true
62
68
  }
63
69
  },
64
70
  "keywords": [
package/tui.js ADDED
@@ -0,0 +1,108 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import legacyPlugin, { setupVoice } from "./index.js";
5
+
6
+ function legacySettings() {
7
+ try {
8
+ const state = process.env.XDG_STATE_HOME || path.join(os.homedir(), ".local", "state");
9
+ const kv = JSON.parse(fs.readFileSync(path.join(state, "opencode", "kv.json"), "utf8"));
10
+ return Object.fromEntries(Object.entries(kv).filter(([key]) => key.startsWith("voice.")));
11
+ } catch {
12
+ return {};
13
+ }
14
+ }
15
+
16
+ // Translate the shared dialog callbacks to V2's promise-based dialogs.
17
+ export function createApi(context) {
18
+ const [settings, update] = context.storage.store("settings", { initial: legacySettings() });
19
+ const dialog = context.ui.dialog;
20
+ let size = "medium";
21
+ let revision = 0;
22
+ let disposed = false;
23
+ const report = (error) => context.ui.toast.show({ title: "Voice", message: String(error), variant: "error" });
24
+
25
+ function present(result, callback) {
26
+ const current = revision;
27
+ void result.then((value) => {
28
+ if (!disposed && current === revision) return callback?.(value);
29
+ }).catch(report);
30
+ }
31
+
32
+ return {
33
+ kv: {
34
+ get: (key, fallback) => settings[key] ?? fallback,
35
+ set: (key, value) => update((draft) => { draft[key] = value; }),
36
+ },
37
+ ui: {
38
+ toast: (options) => { if (!disposed) context.ui.toast.show(options); },
39
+ dialog: {
40
+ setSize(value) { size = value; },
41
+ replace(render) {
42
+ if (disposed) return;
43
+ revision++;
44
+ render();
45
+ dialog.set({ size });
46
+ },
47
+ clear() {
48
+ if (disposed) return;
49
+ revision++;
50
+ dialog.clear();
51
+ },
52
+ },
53
+ DialogAlert({ onConfirm, ...options }) {
54
+ present(dialog.alert(options), onConfirm);
55
+ },
56
+ DialogSelect({ onSelect, footer, options, ...input }) {
57
+ present(dialog.select({
58
+ ...input,
59
+ options: options.map(({ value, title, description, details, category, disabled }) => ({
60
+ value, title, description: description || details?.join(" · "), category, disabled, footer,
61
+ })),
62
+ }), (value) => {
63
+ if (value !== undefined) return onSelect({ value });
64
+ });
65
+ },
66
+ DialogPrompt({ onConfirm, onCancel, ...options }) {
67
+ present(dialog.prompt(options), (value) => value === undefined ? onCancel?.() : onConfirm(value));
68
+ },
69
+ },
70
+ client: {
71
+ tui: {
72
+ appendPrompt({ text }) {
73
+ // V2 no longer exposes client.tui. Use the focused prompt editor and
74
+ // its normal content-change handler, leaving existing draft text intact.
75
+ const input = context.renderer.currentFocusedRenderable;
76
+ const route = context.ui.router.current();
77
+ const canSubmit = context.keymap.commands().some((command) => command.id === "prompt.submit");
78
+ if (disposed || !["home", "session"].includes(route.type) ||
79
+ context.keymap.mode.current() !== "base" || !canSubmit ||
80
+ !input?.focused || input.isDestroyed || typeof input.insertText !== "function" ||
81
+ typeof input.gotoBufferEnd !== "function") {
82
+ throw new Error("Focus the OpenCode prompt before inserting the transcription.");
83
+ }
84
+ input.gotoBufferEnd();
85
+ input.insertText(text);
86
+ context.renderer.requestRender();
87
+ },
88
+ submitPrompt() {
89
+ if (!disposed) context.keymap.dispatch("prompt.submit");
90
+ },
91
+ },
92
+ },
93
+ dispose() {
94
+ disposed = true;
95
+ revision++;
96
+ },
97
+ };
98
+ }
99
+
100
+ // Plugin.define is an identity helper in V2. Exporting its definition shape
101
+ // directly also lets V1 load this entrypoint without importing the V2 SDK.
102
+ export default {
103
+ id: legacyPlugin.id,
104
+ tui: legacyPlugin.tui,
105
+ setup(context) {
106
+ return setupVoice(createApi(context), context.options || {}, context);
107
+ },
108
+ };