Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(speech): cloud engine + pinned multipart contract (desktop-dictation Task 5)

enes6d0988571da2
diff --git a/src/core/speech/cloud-speech.test.ts b/src/core/speech/cloud-speech.test.ts
new file mode 100644
index 000000000..63396c2ea
--- /dev/null
+++ b/src/core/speech/cloud-speech.test.ts
@@ -0,0 +1,48 @@
+import { describe, expect, it } from 'vitest'
+import { buildTranscribeRequest, cloudTranscribe } from './cloud-speech'
+
+const wav = new Uint8Array([0x52, 0x49, 0x46, 0x46]) // "RIFF"
+
+describe('buildTranscribeRequest', () => {
+ it('builds the exact multipart body the iOS contract pins', () => {
+ const { url, init } = buildTranscribeRequest({
+ apiBase: 'https://api.example.dev', wav, locale: 'tr-TR', token: 'tok.sig', boundary: 'BOUNDARY',
+ })
+ expect(url).toBe('https://api.example.dev/v1/transcribe')
+ expect(init.method).toBe('POST')
+ expect((init.headers as Record<string, string>).Authorization).toBe('Bearer tok.sig')
+ expect((init.headers as Record<string, string>)['Content-Type']).toBe('multipart/form-data; boundary=BOUNDARY')
+ const body = Buffer.from(init.body as Uint8Array).toString('latin1')
+ expect(body).toBe(
+ '--BOUNDARY\r\nContent-Disposition: form-data; name="locale"\r\n\r\ntr-TR\r\n' +
+ '--BOUNDARY\r\nContent-Disposition: form-data; name="audio"; filename="dictation.wav"\r\n' +
+ 'Content-Type: audio/wav\r\n\r\nRIFF\r\n--BOUNDARY--\r\n',
+ )
+ })
+
+ it('omits Authorization without a token', () => {
+ const { init } = buildTranscribeRequest({ apiBase: 'x', wav, locale: 'en', token: null })
+ expect((init.headers as Record<string, string>).Authorization).toBeUndefined()
+ })
+})
+
+describe('cloudTranscribe', () => {
+ const pcm = new Float32Array(160)
+ const respond = (status: number, body: unknown): typeof fetch =>
+ (async () => ({ ok: status === 200, status, json: async () => body }) as unknown as Response) as typeof fetch
+
+ it('maps 404 to the not-available message', async () => {
+ await expect(cloudTranscribe(pcm, { apiBase: 'x', locale: 'en', token: null, fetchFn: respond(404, {}) }))
+ .rejects.toThrow("Cloud transcription isn't available yet.")
+ })
+
+ it('surfaces the server error field', async () => {
+ await expect(cloudTranscribe(pcm, { apiBase: 'x', locale: 'en', token: 't', fetchFn: respond(429, { error: 'rate limited' }) }))
+ .rejects.toThrow('rate limited')
+ })
+
+ it('returns trimmed text on 200', async () => {
+ await expect(cloudTranscribe(pcm, { apiBase: 'x', locale: 'en', token: 't', fetchFn: respond(200, { text: ' hi ' }) }))
+ .resolves.toBe('hi')
+ })
+})
diff --git a/src/core/speech/cloud-speech.ts b/src/core/speech/cloud-speech.ts
new file mode 100644
index 000000000..26446261d
--- /dev/null
+++ b/src/core/speech/cloud-speech.ts
@@ -0,0 +1,47 @@
+import { randomUUID } from 'node:crypto'
+import { pcmToWav } from './pcm'
+
+/** Cloud dictation against the nodeterm backend — the SAME wire contract the
+ * iOS Cloud engine speaks (multipart WAV + locale, Bearer license token), so
+ * the one future /v1/transcribe endpoint serves both platforms. Until it
+ * exists, every call maps 404 to a friendly "not available yet". */
+export function buildTranscribeRequest(opts: {
+ apiBase: string
+ wav: Uint8Array
+ locale: string
+ token: string | null
+ boundary?: string
+}): { url: string; init: RequestInit } {
+ const boundary = opts.boundary ?? randomUUID()
+ const headers: Record<string, string> = {
+ 'Content-Type': `multipart/form-data; boundary=${boundary}`,
+ }
+ if (opts.token) headers.Authorization = `Bearer ${opts.token}`
+ const enc = (s: string) => Buffer.from(s, 'latin1')
+ const body = Buffer.concat([
+ enc(`--${boundary}\r\nContent-Disposition: form-data; name="locale"\r\n\r\n${opts.locale}\r\n`),
+ enc(`--${boundary}\r\nContent-Disposition: form-data; name="audio"; filename="dictation.wav"\r\nContent-Type: audio/wav\r\n\r\n`),
+ Buffer.from(opts.wav),
+ enc(`\r\n--${boundary}--\r\n`),
+ ])
+ return { url: `${opts.apiBase}/v1/transcribe`, init: { method: 'POST', headers, body } }
+}
+
+export async function cloudTranscribe(
+ pcm: Float32Array,
+ opts: { apiBase: string; locale: string; token: string | null; fetchFn?: typeof fetch },
+): Promise<string> {
+ const fetchFn = opts.fetchFn ?? fetch
+ const { url, init } = buildTranscribeRequest({
+ apiBase: opts.apiBase, wav: pcmToWav(pcm), locale: opts.locale, token: opts.token,
+ })
+ const res = await fetchFn(url, init)
+ if (res.status === 404) throw new Error("Cloud transcription isn't available yet.")
+ if (res.status !== 200) {
+ const body = await res.json().catch(() => null) as { error?: string } | null
+ throw new Error(body?.error ?? `Cloud transcription failed (${res.status}).`)
+ }
+ const body = await res.json().catch(() => null) as { text?: string } | null
+ if (typeof body?.text !== 'string') throw new Error('Cloud transcription returned an unexpected response.')
+ return body.text.trim()
+}