The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - verifies isolated native worker processes and their exit events. |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodeOS from "node:os"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | |
| 6 | import { expect, it } from "vite-plus/test"; |
| 7 | |
| 8 | import { loadNativeSpeechModel } from "./native.ts"; |
| 9 | |
| 10 | const fixture = (transcribe: string, maxAudioMs = 0, supportsVocabulary = false) => |
| 11 | `data:text/javascript,${encodeURIComponent(`export const TranscribeModel = { load: async () => ({ capabilities: { supportsStreaming: false, maxAudioMs: ${maxAudioMs}, languages: ["en-US", "de-DE"] }, supports: (feature) => feature === "vocabulary" && ${supportsVocabulary}, transcribe: ${transcribe} }) };`)}`; |
| 12 | |
| 13 | const fixtureWithUnrelatedIpcMessage = () => |
| 14 | `data:text/javascript,${encodeURIComponent(` |
| 15 | process.send?.({ type: "unrelated-control-message" }); |
| 16 | export const TranscribeModel = { |
| 17 | load: async () => ({ capabilities: { supportsStreaming: false }, supports: () => false, transcribe: async () => ({ text: "hello" }) }), |
| 18 | }; |
| 19 | `)}`; |
| 20 | |
| 21 | const streamingFixture = (feed: string, supportsVocabulary = false) => |
| 22 | `data:text/javascript,${encodeURIComponent(` |
| 23 | export const TranscribeModel = { load: async () => ({ |
| 24 | capabilities: { supportsStreaming: true }, |
| 25 | supports: (feature) => feature === "vocabulary" && ${supportsVocabulary}, |
| 26 | createSession: () => ({ |
| 27 | dispose() {}, |
| 28 | stream: async (opts) => ({ |
| 29 | feed: ${feed}, |
| 30 | finalize: async () => {}, |
| 31 | text: { full: opts.vocabulary?.join(" / ") ?? opts.language ?? "hello world", committed: "hello ", tentative: "world" }, |
| 32 | reset() {}, |
| 33 | }), |
| 34 | }), |
| 35 | }) }; |
| 36 | `)}`; |
| 37 | |
| 38 | it("returns transcription from an isolated process and disposes it", async () => { |
| 39 | const model = await loadNativeSpeechModel( |
| 40 | "unused.gguf", |
| 41 | new AbortController().signal, |
| 42 | fixture("async (pcm) => ({ text: String(pcm[0]) })"), |
| 43 | ); |
| 44 | try { |
| 45 | expect(model.languages).toEqual(["en-US", "de-DE"]); |
| 46 | expect( |
| 47 | await model.transcribe( |
| 48 | new Float32Array([0.25]), |
| 49 | { timestamps: "none", language: "en" }, |
| 50 | false, |
| 51 | ), |
| 52 | ).toEqual({ text: "0.25" }); |
| 53 | } finally { |
| 54 | await model.dispose(); |
| 55 | } |
| 56 | }); |
| 57 | |
| 58 | it("probes generic vocabulary support and delivers ordered terms to offline inference", async () => { |
| 59 | for (const supported of [false, true]) { |
| 60 | const model = await loadNativeSpeechModel( |
| 61 | "unused.gguf", |
| 62 | new AbortController().signal, |
| 63 | fixture("async (_pcm, options) => ({ text: JSON.stringify(options) })", 0, supported), |
| 64 | ); |
| 65 | try { |
| 66 | expect(model.supportsVocabulary).toBe(supported); |
| 67 | const options = { |
| 68 | timestamps: "none" as const, |
| 69 | language: "en", |
| 70 | ...(supported ? { vocabulary: ["T3 Code", "GitHub"] } : {}), |
| 71 | }; |
| 72 | const result = await model.transcribe(new Float32Array([0.25]), options, false); |
| 73 | expect(JSON.parse(result.text)).toEqual(options); |
| 74 | } finally { |
| 75 | await model.dispose(); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | it("ignores IPC messages that do not belong to the speech protocol", async () => { |
| 81 | const model = await loadNativeSpeechModel( |
| 82 | "unused.gguf", |
| 83 | new AbortController().signal, |
| 84 | fixtureWithUnrelatedIpcMessage(), |
| 85 | ); |
| 86 | try { |
| 87 | await expect( |
| 88 | model.transcribe(new Float32Array([0.25]), { timestamps: "none", language: "en" }, false), |
| 89 | ).resolves.toEqual({ text: "hello" }); |
| 90 | } finally { |
| 91 | await model.dispose(); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | it("terminates hung native inference when the service shuts down", async () => { |
| 96 | const controller = new AbortController(); |
| 97 | const model = await loadNativeSpeechModel( |
| 98 | "unused.gguf", |
| 99 | controller.signal, |
| 100 | fixture("async () => { while (true) {} }"), |
| 101 | ); |
| 102 | const pending = model.transcribe( |
| 103 | new Float32Array([0.25]), |
| 104 | { |
| 105 | timestamps: "none", |
| 106 | language: "en", |
| 107 | }, |
| 108 | false, |
| 109 | ); |
| 110 | const rejected = expect(pending).rejects.toThrow(); |
| 111 | controller.abort(); |
| 112 | await model.dispose(); |
| 113 | await rejected; |
| 114 | }); |
| 115 | |
| 116 | it("returns streaming previews and final text from an isolated process", async () => { |
| 117 | const model = await loadNativeSpeechModel( |
| 118 | "unused.gguf", |
| 119 | new AbortController().signal, |
| 120 | streamingFixture( |
| 121 | "async () => ({ revision: 1, committedChanged: true, tentativeChanged: true })", |
| 122 | true, |
| 123 | ), |
| 124 | ); |
| 125 | try { |
| 126 | expect(model.supportsStreaming).toBe(true); |
| 127 | await model.begin({ timestamps: "none" }, false); |
| 128 | expect(await model.feed(new Float32Array(512).fill(0.25))).toEqual({ |
| 129 | revision: 1, |
| 130 | text: { committed: "hello ", tentative: "world" }, |
| 131 | }); |
| 132 | expect(await model.finish()).toBe("hello world"); |
| 133 | await model.begin({ timestamps: "none", language: "fr" }, false); |
| 134 | await model.reset(); |
| 135 | await model.begin({ timestamps: "none", language: "fr" }, false); |
| 136 | await model.feed(new Float32Array(512)); |
| 137 | expect(await model.finish()).toBe("fr"); |
| 138 | await model.begin( |
| 139 | { timestamps: "none", language: "fr", vocabulary: ["T3 Code", "GitHub"] }, |
| 140 | false, |
| 141 | ); |
| 142 | await model.feed(new Float32Array(512).fill(0.25)); |
| 143 | expect(await model.finish()).toBe("T3 Code / GitHub"); |
| 144 | } finally { |
| 145 | await model.dispose(); |
| 146 | } |
| 147 | }); |
| 148 | |
| 149 | it("kills a hung streaming feed without waiting for native cleanup", async () => { |
| 150 | const controller = new AbortController(); |
| 151 | const model = await loadNativeSpeechModel( |
| 152 | "unused.gguf", |
| 153 | controller.signal, |
| 154 | streamingFixture("async () => { while (true) {} }"), |
| 155 | ); |
| 156 | await model.begin({ timestamps: "none" }, false); |
| 157 | const rejected = expect(model.feed(new Float32Array(512).fill(0.25))).rejects.toThrow(); |
| 158 | controller.abort(); |
| 159 | await model.dispose(); |
| 160 | await rejected; |
| 161 | }); |
| 162 | |
| 163 | it("skips process exit teardown when the backend dies during native inference", async () => { |
| 164 | const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-speech-orphan-")); |
| 165 | const marker = NodePath.join(directory, "exit-teardown"); |
| 166 | // The exit hook stands in for native atexit teardown; the interval for the threadpool call. |
| 167 | const model = await loadNativeSpeechModel( |
| 168 | "unused.gguf", |
| 169 | new AbortController().signal, |
| 170 | streamingFixture(`async () => { |
| 171 | const { writeFileSync } = process.getBuiltinModule("node:fs"); |
| 172 | process.once("exit", () => writeFileSync(${JSON.stringify(marker)}, "ran")); |
| 173 | setInterval(() => {}, 1 << 30); |
| 174 | process.disconnect(); |
| 175 | return await new Promise(() => {}); |
| 176 | }`), |
| 177 | ); |
| 178 | try { |
| 179 | await model.begin({ timestamps: "none" }, false); |
| 180 | await expect(model.feed(new Float32Array(512).fill(0.25))).rejects.toThrow( |
| 181 | "Speech process exited", |
| 182 | ); |
| 183 | expect( |
| 184 | NodeFS.existsSync(marker), |
| 185 | "orphaned worker ran exit teardown while native inference was in flight", |
| 186 | ).toBe(false); |
| 187 | } finally { |
| 188 | await model.dispose(); |
| 189 | NodeFS.rmSync(directory, { recursive: true, force: true }); |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | const vadFixture = ({ |
| 194 | streaming = false, |
| 195 | failVad = false, |
| 196 | failVadLoad = false, |
| 197 | failAsr = false, |
| 198 | maxAudioMs = 4000, |
| 199 | } = {}) => |
| 200 | `data:text/javascript,${encodeURIComponent(` |
| 201 | const summarize = pcm => String(pcm.length) + ":" + pcm[0].toFixed(2) + ":" + pcm[pcm.length - 1].toFixed(2); |
| 202 | export const TranscribeModel = { load: async (path, options) => { |
| 203 | if (path === "vad.gguf") { |
| 204 | if (${failVadLoad}) throw new Error("VAD unavailable"); |
| 205 | if (options.backend !== "cpu") throw new Error("VAD must use CPU"); |
| 206 | return { vadInfo: { sampleRate: 16000, frameSamples: 512 }, dispose() {}, |
| 207 | createVadSession: ({ nThreads }) => { |
| 208 | if (nThreads !== 1) throw new Error("VAD thread count"); |
| 209 | let pending = []; let at = 0; |
| 210 | const score = final => { |
| 211 | const firstFrame = at; |
| 212 | const probs = []; |
| 213 | while (pending.length >= 512 || final && pending.length) { |
| 214 | probs.push(pending[0] > 0.5 ? 0.9 : 0); |
| 215 | pending.splice(0, 512); at++; |
| 216 | } |
| 217 | return { firstFrame, probs }; |
| 218 | }; |
| 219 | return { dispose() {}, streamReset: async () => { pending = []; at = 0; }, |
| 220 | streamFeed: async pcm => { |
| 221 | if (${failVad} && pcm[0] < 0) throw new Error("VAD fixture failed"); |
| 222 | pending.push(...pcm); return score(false); |
| 223 | }, |
| 224 | streamFlush: async () => score(true), |
| 225 | }; |
| 226 | }, |
| 227 | }; |
| 228 | } |
| 229 | return { backend: "CPU", supports: () => false, |
| 230 | capabilities: { supportsStreaming: ${streaming}, maxAudioMs: ${maxAudioMs} }, |
| 231 | transcribe: async pcm => { if (${failAsr}) throw new Error("ASR fixture failed"); return { text: summarize(pcm) }; }, |
| 232 | createSession: () => { |
| 233 | let all = []; |
| 234 | return { dispose() {}, stream: async () => ({ |
| 235 | feed: async pcm => { all.push(...pcm); return { committedChanged: true }; }, |
| 236 | finalize: async () => {}, reset() { all = []; }, |
| 237 | get text() { const full = summarize(all); return { committed: full, tentative: "", full }; }, |
| 238 | }) }; |
| 239 | }, |
| 240 | }; |
| 241 | }}; |
| 242 | `)}`; |
| 243 | |
| 244 | it("filters silence and preserves partial speech through the production worker and resets between recordings", async () => { |
| 245 | for (const streaming of [false, true]) { |
| 246 | const model = await loadNativeSpeechModel( |
| 247 | "asr.gguf", |
| 248 | new AbortController().signal, |
| 249 | vadFixture({ streaming }), |
| 250 | "cpu", |
| 251 | ); |
| 252 | try { |
| 253 | expect( |
| 254 | await model.transcribe(new Float32Array(2000), { timestamps: "none" }, true, "vad.gguf"), |
| 255 | ).toEqual({ text: "" }); |
| 256 | expect( |
| 257 | await model.transcribe( |
| 258 | new Float32Array([0.8, 0.6]), |
| 259 | { timestamps: "none" }, |
| 260 | true, |
| 261 | "vad.gguf", |
| 262 | ), |
| 263 | ).toEqual({ text: "2:0.80:0.60" }); |
| 264 | expect( |
| 265 | await model.transcribe(new Float32Array(2000), { timestamps: "none" }, false, "vad.gguf"), |
| 266 | ).toEqual({ text: "2000:0.00:0.00" }); |
| 267 | for (const cancel of [true, false]) { |
| 268 | await model.begin({ timestamps: "none" }, true, "vad.gguf"); |
| 269 | await model.feed(new Float32Array(600)); |
| 270 | expect(await model.finish()).toBe(""); |
| 271 | await model.begin({ timestamps: "none" }, true, "vad.gguf"); |
| 272 | await model.feed(new Float32Array(511).fill(0.8)); |
| 273 | if (cancel) await model.reset(); |
| 274 | else expect(await model.finish()).toBe("511:0.80:0.80"); |
| 275 | } |
| 276 | } finally { |
| 277 | await model.dispose(); |
| 278 | } |
| 279 | } |
| 280 | }); |
| 281 | |
| 282 | it("decodes complete stable chunks before Stop and returns the ordered final tail", async () => { |
| 283 | const model = await loadNativeSpeechModel( |
| 284 | "asr.gguf", |
| 285 | new AbortController().signal, |
| 286 | vadFixture(), |
| 287 | "cpu", |
| 288 | ); |
| 289 | try { |
| 290 | await model.begin({ timestamps: "none" }, false, "vad.gguf"); |
| 291 | await model.feed(new Float32Array(16000).fill(0.1)); |
| 292 | await model.feed(new Float32Array(16000).fill(0.2)); |
| 293 | const update = await model.feed(new Float32Array(16000).fill(0.3)); |
| 294 | expect(update.text).toEqual({ committed: "32000:0.10:0.20", tentative: "" }); |
| 295 | await model.feed(new Float32Array([0.4, 0.5])); |
| 296 | expect(await model.finish()).toBe("32000:0.10:0.20 16002:0.30:0.50"); |
| 297 | } finally { |
| 298 | await model.dispose(); |
| 299 | } |
| 300 | }); |
| 301 | |
| 302 | it("uses bounded offline chunks for a native streaming model without opening a session", async () => { |
| 303 | const moduleUrl = `data:text/javascript,${encodeURIComponent(` |
| 304 | export const TranscribeModel = { load: async () => ({ |
| 305 | capabilities: { supportsStreaming: true, maxAudioMs: 4000 }, |
| 306 | supports: () => false, |
| 307 | createSession: () => { throw new Error("Offline inference must not open a stream"); }, |
| 308 | transcribe: async pcm => { |
| 309 | if (pcm.length > 32000) throw new Error("Model input limit exceeded"); |
| 310 | return { text: String(pcm.length) }; |
| 311 | }, |
| 312 | }) }; |
| 313 | `)}`; |
| 314 | const model = await loadNativeSpeechModel("asr.gguf", new AbortController().signal, moduleUrl); |
| 315 | try { |
| 316 | expect( |
| 317 | await model.transcribe(new Float32Array(48002).fill(0.8), { timestamps: "none" }, false), |
| 318 | ).toEqual({ text: "32000 16002" }); |
| 319 | } finally { |
| 320 | await model.dispose(); |
| 321 | } |
| 322 | }); |
| 323 | |
| 324 | it("preserves withheld audio on VAD failure while keeping ASR failures distinct", async () => { |
| 325 | for (const streaming of [false, true]) { |
| 326 | const model = await loadNativeSpeechModel( |
| 327 | "asr.gguf", |
| 328 | new AbortController().signal, |
| 329 | vadFixture({ streaming, failVad: true }), |
| 330 | "cpu", |
| 331 | ); |
| 332 | try { |
| 333 | await model.begin({ timestamps: "none" }, true, "vad.gguf"); |
| 334 | await model.feed(new Float32Array(512).fill(0.1)); |
| 335 | await model.feed(new Float32Array(512).fill(-0.2)); |
| 336 | expect(await model.finish()).toBe("1024:0.10:-0.20"); |
| 337 | await model.begin({ timestamps: "none" }, true, "vad.gguf"); |
| 338 | await model.feed(new Float32Array(512)); |
| 339 | expect(await model.finish()).toBe(""); |
| 340 | } finally { |
| 341 | await model.dispose(); |
| 342 | } |
| 343 | } |
| 344 | const unavailable = await loadNativeSpeechModel( |
| 345 | "asr.gguf", |
| 346 | new AbortController().signal, |
| 347 | vadFixture({ failVadLoad: true }), |
| 348 | "cpu", |
| 349 | ); |
| 350 | try { |
| 351 | expect( |
| 352 | await unavailable.transcribe( |
| 353 | new Float32Array(2000).fill(0.1), |
| 354 | { timestamps: "none" }, |
| 355 | true, |
| 356 | "vad.gguf", |
| 357 | ), |
| 358 | ).toEqual({ text: "2000:0.10:0.10" }); |
| 359 | } finally { |
| 360 | await unavailable.dispose(); |
| 361 | } |
| 362 | const model = await loadNativeSpeechModel( |
| 363 | "asr.gguf", |
| 364 | new AbortController().signal, |
| 365 | vadFixture({ failAsr: true }), |
| 366 | "cpu", |
| 367 | ); |
| 368 | try { |
| 369 | await expect( |
| 370 | model.transcribe(new Float32Array(512).fill(0.8), { timestamps: "none" }, true, "vad.gguf"), |
| 371 | ).rejects.toThrow("ASR fixture failed"); |
| 372 | } finally { |
| 373 | await model.dispose(); |
| 374 | } |
| 375 | }); |