@micdrop/gradium 1.1.2 → 1.2.0
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 +10 -10
- package/dist/index.d.mts +24 -31
- package/dist/index.d.ts +24 -31
- package/dist/index.js +277 -404
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +282 -396
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/GradiumSTT.ts","../src/closeSocket.ts","../src/types.ts","../src/GradiumTTS.ts"],"sourcesContent":["import { STT } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket from 'ws'\nimport { closeSocket } from './closeSocket'\nimport {\n DEFAULT_INPUT_FORMAT,\n DEFAULT_LANGUAGE,\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumASRJsonConfig,\n GradiumASRResponse,\n GradiumASRSetupMessage,\n GradiumInputFormat,\n GradiumSTTOptions,\n} from './types'\n\n/**\n * Gradium Real-time STT (ASR)\n *\n * @see https://docs.gradium.ai/guides/speech-to-text\n */\n\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\n// Each frame of the transcription model lasts 80 ms\nconst FRAME_DURATION_MS = 80\nconst DEFAULT_DELAY_IN_FRAMES = 10\n\nexport class GradiumSTT extends STT {\n private socket?: WebSocket\n private initPromise: Promise<void>\n private reconnectTimeout?: NodeJS.Timeout\n private connectionTimeout?: NodeJS.Timeout\n private transcriptionTimeout?: NodeJS.Timeout\n private audioChunksPending: Buffer[] = [] // Store audio chunks to send them again if reconnecting\n private transcript = '' // Accumulated text segments for the current utterance\n private flushId = 0\n private delayInFrames = DEFAULT_DELAY_IN_FRAMES // Given by the server when ready\n private retryCount = 0\n private destroyed = false\n\n constructor(private options: GradiumSTTOptions) {\n super()\n\n // Setup WebSocket connection\n this.initPromise = this.connect()\n }\n\n transcribe(audioStream: Readable) {\n let started = false\n\n // Read audio stream and send to Gradium\n audioStream.on('data', async (chunk: Buffer) => {\n // New utterance: start from a clean transcript for this stream, but only\n // once it carries something. Voice detection opens a stream on any noise\n // and many of them never carry a word, and clearing here rather than on\n // entry keeps those from wiping the transcript of the utterance before,\n // whose flush may still be on its way back.\n if (!started) {\n started = true\n this.transcript = ''\n }\n this.audioChunksPending.push(chunk)\n await this.initPromise\n this.sendAudioChunk(chunk)\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // pending segments and confirms with a matching \"flushed\" message.\n audioStream.on('end', async () => {\n await this.initPromise\n if (this.audioChunksPending.length === 0) return\n this.sendTrailingSilence()\n const flushId = ++this.flushId\n this.sendFlush(flushId)\n\n // Timeout transcription if no \"flushed\" confirmation is received\n this.transcriptionTimeout = setTimeout(() => {\n this.transcriptionTimeout = undefined\n this.log('Transcription timeout')\n this.emitTranscript()\n }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT)\n })\n }\n\n destroy() {\n super.destroy()\n this.destroyed = true\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n if (this.transcriptionTimeout) {\n clearTimeout(this.transcriptionTimeout)\n this.transcriptionTimeout = undefined\n }\n if (this.connectionTimeout) {\n clearTimeout(this.connectionTimeout)\n this.connectionTimeout = undefined\n }\n if (this.socket) closeSocket(this.socket)\n this.socket = undefined\n }\n\n private connect(): Promise<void> {\n return this.initWS().catch((error) => {\n console.error('[GradiumSTT] Connection error:', error)\n this.reconnect()\n })\n }\n\n private getEndpoint() {\n const region = this.options.region ?? DEFAULT_REGION\n return `wss://${region}.api.gradium.ai/api/speech/asr`\n }\n\n private async initWS(): Promise<void> {\n return new Promise<void>((resolve, reject) => {\n const socket = new WebSocket(this.getEndpoint(), {\n headers: {\n 'x-api-key': this.options.apiKey,\n },\n })\n this.socket = socket\n\n const timeout = setTimeout(() => {\n this.log('Connection timeout')\n closeSocket(socket)\n this.socket = undefined\n reject(new Error('WebSocket connection timeout'))\n }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT)\n this.connectionTimeout = timeout\n\n socket.addEventListener('open', () => {\n clearTimeout(timeout)\n this.sendSetup()\n this.log('Connection opened')\n resolve()\n })\n\n socket.addEventListener('error', (error) => {\n clearTimeout(timeout)\n this.log('WebSocket error:', error)\n reject(new Error('WebSocket connection error'))\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n clearTimeout(timeout)\n this.socket?.removeAllListeners()\n this.socket = undefined\n\n if (code !== 1000) {\n this.reconnect()\n } else {\n this.log('Connection closed', { code, reason })\n }\n })\n\n socket.addEventListener('message', (event) => {\n try {\n this.handleMessage(JSON.parse(event.data.toString()))\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private buildJsonConfig(): GradiumASRJsonConfig {\n const config: GradiumASRJsonConfig = { ...this.options.jsonConfig }\n if (config.language === undefined) {\n config.language = this.options.language ?? DEFAULT_LANGUAGE\n }\n return config\n }\n\n private sendSetup() {\n this.socket?.send(\n JSON.stringify({\n type: 'setup',\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,\n json_config: this.buildJsonConfig(),\n } satisfies GradiumASRSetupMessage)\n )\n this.log('Sent setup')\n }\n\n private sendAudioChunk(chunk: Buffer) {\n this.socket?.send(\n JSON.stringify({\n type: 'audio',\n audio: chunk.toString('base64'),\n })\n )\n }\n\n /**\n * Follows the speech with as much silence as the transcription lags behind.\n *\n * The model writes each word a few frames after hearing it, and a flush only\n * processes the audio already sent. With nothing after the last word, the\n * server confirms the flush before writing that word, which then opens the\n * next transcript.\n */\n private sendTrailingSilence() {\n const sampleRate = pcmSampleRate(\n this.options.inputFormat ?? DEFAULT_INPUT_FORMAT\n )\n // Silence is a run of zeros in raw PCM only\n if (!sampleRate) return\n const durationMs = this.delayInFrames * FRAME_DURATION_MS\n const samples = Math.round((sampleRate * durationMs) / 1000)\n const silence = Buffer.alloc(samples * 2)\n this.audioChunksPending.push(silence)\n this.sendAudioChunk(silence)\n this.log(`Sent ${durationMs}ms of silence`)\n }\n\n private sendFlush(flushId: number) {\n this.socket?.send(JSON.stringify({ type: 'flush', flush_id: flushId }))\n this.log(`Sent flush (flush_id=${flushId})`)\n }\n\n private emitTranscript() {\n const transcript = this.transcript.replace(/\\s+/g, ' ').trim()\n this.log(`Received transcript: \"${transcript}\"`)\n this.emit('Transcript', transcript)\n // Reset state and clear timeout\n this.transcript = ''\n this.audioChunksPending.length = 0\n if (this.transcriptionTimeout) {\n clearTimeout(this.transcriptionTimeout)\n this.transcriptionTimeout = undefined\n }\n }\n\n private handleMessage(message: GradiumASRResponse) {\n switch (message.type) {\n case 'ready':\n this.log('Server ready')\n // The server accepted the setup, so the connection is usable\n this.retryCount = 0\n if (typeof message.delay_in_frames === 'number') {\n this.delayInFrames = message.delay_in_frames\n }\n break\n\n case 'text':\n if (typeof message.text === 'string') {\n this.transcript += (this.transcript ? ' ' : '') + message.text\n this.log(`Received text segment: \"${message.text}\"`)\n }\n break\n\n case 'end_text':\n // Segment finalized, nothing to accumulate (text already received)\n break\n\n case 'flushed':\n // The flush for the current utterance completed: all pending segments\n // have been received, emit the full transcript.\n if (message.flush_id === this.flushId) {\n this.emitTranscript()\n }\n break\n\n case 'step':\n // Voice activity detection frames, ignored\n break\n\n case 'end_of_stream':\n this.log('End of stream')\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n }\n\n private reconnect() {\n if (this.destroyed) return\n this.retryCount++\n if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {\n this.log('Max retries reached, giving up')\n this.emit('Failed', this.audioChunksPending)\n return\n }\n\n this.initPromise = new Promise((resolve) => {\n this.log('Reconnecting...')\n this.reconnectTimeout = setTimeout(() => {\n this.reconnectTimeout = undefined\n // The count starts over once the server accepts the session, since it\n // opens the connection before checking the setup\n this.initWS()\n .then(() => {\n // Resend audio chunks if reconnecting during transcription. Setup\n // is sent on open and the server replays the whole utterance, so\n // reset the accumulated segments to avoid duplicating them.\n if (this.audioChunksPending.length > 0) {\n this.log('Sending audio chunks again')\n this.transcript = ''\n this.audioChunksPending.forEach((chunk) =>\n this.sendAudioChunk(chunk)\n )\n }\n })\n .then(resolve)\n .catch((error) => {\n this.log('Reconnection error:', error)\n this.reconnect()\n })\n }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY)\n })\n }\n}\n\n/** Sample rate of a raw PCM input format, undefined for an encoded one */\nfunction pcmSampleRate(format: GradiumInputFormat): number | undefined {\n if (format === 'pcm') return 24000\n const match = format.match(/^pcm_(\\d+)$/)\n return match ? Number(match[1]) : undefined\n}\n","import WebSocket from 'ws'\n\n/**\n * Closes a socket without hearing from it again.\n *\n * A socket closed while still connecting emits an error, and an error nobody\n * listens to crashes the process, so a listener that ignores it stays.\n */\nexport function closeSocket(socket: WebSocket) {\n socket.removeAllListeners()\n socket.on('error', () => {})\n if (\n socket.readyState === WebSocket.OPEN ||\n socket.readyState === WebSocket.CONNECTING\n ) {\n socket.close(1000)\n }\n}\n","export type GradiumOutputFormat =\n | 'pcm'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n\nexport type GradiumInputFormat =\n | 'pcm'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n | 'pcm_48000'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n\nexport type GradiumRegion = 'eu' | 'us'\n\nexport interface GradiumTTSOptions {\n apiKey: string\n voiceId: string\n modelName?: string\n outputFormat?: GradiumOutputFormat\n region?: GradiumRegion\n jsonConfig?: GradiumJsonConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nexport interface GradiumJsonConfig {\n padding_bonus?: number // Speed control: -4.0 to 4.0\n temp?: number // Temperature: 0 to 1.4, default 0.7\n cfg_coef?: number // Voice similarity: 1.0 to 4.0, default 2.0\n}\n\nexport const DEFAULT_MODEL_NAME = 'default'\nexport const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat = 'pcm_16000'\nexport const DEFAULT_INPUT_FORMAT: GradiumInputFormat = 'pcm_16000'\nexport const DEFAULT_REGION: GradiumRegion = 'eu'\n// Gradium refuses a transcription without a language, \"any\" detects it\nexport const DEFAULT_LANGUAGE = 'any'\n\n// Speech-to-text (ASR) types\n\nexport interface GradiumASRJsonConfig {\n language?: string\n target_language?: string\n delay_in_frames?: number // 0 to 80, each frame = 80ms\n temp?: number\n padding_bonus?: number\n}\n\nexport interface GradiumSTTOptions {\n apiKey: string\n modelName?: string\n inputFormat?: GradiumInputFormat\n language?: string\n region?: GradiumRegion\n jsonConfig?: GradiumASRJsonConfig\n connectionTimeout?: number\n transcriptionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\n// Client -> Server ASR messages\n\nexport interface GradiumASRSetupMessage {\n type: 'setup'\n model_name: string\n input_format: string\n json_config?: GradiumASRJsonConfig\n}\n\nexport interface GradiumAudioMessage {\n type: 'audio'\n audio: string // Base64 encoded audio data\n}\n\nexport interface GradiumFlushMessage {\n type: 'flush'\n flush_id: number\n}\n\n// Server -> Client ASR messages\n\nexport type GradiumASRResponse =\n | GradiumReadyResponse\n | GradiumTextResponse\n | GradiumEndTextResponse\n | GradiumStepResponse\n | GradiumFlushedResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumTextResponse {\n type: 'text'\n text: string\n start_s?: number\n stream_id?: number\n}\n\nexport interface GradiumEndTextResponse {\n type: 'end_text'\n stop_s?: number\n stream_id?: number\n}\n\nexport interface GradiumStepResponse {\n type: 'step'\n vad?: Array<{ horizon_s: number; inactivity_prob: number }>\n step_idx?: number\n step_duration_s?: number\n total_duration_s?: number\n}\n\nexport interface GradiumFlushedResponse {\n type: 'flushed'\n flush_id: number\n}\n\n// Client -> Server messages\n\nexport interface GradiumSetupMessage {\n type: 'setup'\n voice_id: string\n model_name: string\n output_format: string\n close_ws_on_eos?: boolean\n json_config?: GradiumJsonConfig\n client_req_id?: string\n}\n\nexport interface GradiumTextMessage {\n type: 'text'\n text: string\n client_req_id?: string\n}\n\nexport interface GradiumEosMessage {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\n// Server -> Client messages\n\nexport type GradiumResponse =\n | GradiumReadyResponse\n | GradiumAudioResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumReadyResponse {\n type: 'ready'\n request_id?: string\n delay_in_frames?: number // Frames of 80ms the transcription lags behind the audio\n}\n\nexport interface GradiumAudioResponse {\n type: 'audio'\n audio: string // Base64 encoded PCM data\n client_req_id?: string\n}\n\nexport interface GradiumEosResponse {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\nexport interface GradiumErrorResponse {\n type: 'error'\n message: string\n code?: number\n}\n","import { TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport WebSocket from 'ws'\nimport { closeSocket } from './closeSocket'\nimport {\n DEFAULT_MODEL_NAME,\n DEFAULT_OUTPUT_FORMAT,\n DEFAULT_REGION,\n GradiumAudioResponse,\n GradiumEosMessage,\n GradiumResponse,\n GradiumSetupMessage,\n GradiumTextMessage,\n GradiumTTSOptions,\n} from './types'\n\n// API Reference: https://gradium.ai/api_docs.html\n\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\n\nexport class GradiumTTS extends TTS {\n private socket?: WebSocket\n private initPromise!: Promise<void>\n private counter = 0\n // Bumped by every speak() and every cancel(), so a request claimed late can\n // tell whether it is still the one that should be heard. Kept apart from\n // `counter`, which numbers the audio requests and must only move when there\n // is something to synthesize.\n private generation = 0\n private isProcessing = false\n private reconnectTimeout?: NodeJS.Timeout\n private connectionTimeout?: NodeJS.Timeout\n private textSent = ''\n private textBuffer = ''\n private retryCount = 0\n private destroyed = false\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n this.connect()\n }\n\n private connect() {\n if (this.destroyed) return\n this.initPromise = this.initWS().catch((error) => {\n console.error('[GradiumTTS] Connection error:', error)\n this.reconnect()\n })\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let counter = 0\n let clientReqId = ''\n\n // Claiming the request is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current id and anything\n // older is dropped. A stream that never carries a word, which is what an\n // answer skipped by a tool or by onBeforeAnswer hands over, would then cut\n // the assistant off mid-sentence for nothing.\n const claimRequest = () => {\n if (counter) return true\n // Cancelled, or superseded by another speak(), before the first word.\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n clientReqId = counter.toString()\n this.isProcessing = true\n this.textSent = ''\n this.textBuffer = ''\n\n // Each speak() is its own multiplexed request: send a fresh setup stamped\n // with the request's client_req_id before the first text chunk, so the\n // server can bind the voice to this session. Multiplexing is required to\n // reuse the connection across utterances (sequential mode only works for\n // the first request on a socket). Queued from the first data handler\n // before that handler awaits, so the setup microtask still precedes the\n // first transcript microtask.\n this.initPromise.then(() => {\n if (counter !== this.counter) return\n this.sendSetup(clientReqId)\n })\n return true\n }\n\n textStream.on('data', async (chunk: Buffer) => {\n if (!claimRequest()) return\n if (counter !== this.counter) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n this.textSent += text\n\n await this.initPromise\n if (counter !== this.counter) return\n\n // Buffer text and only send complete words (flush on last space)\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n this.textBuffer += text\n } else {\n this.sendTranscript(\n this.textBuffer + text.slice(0, spaceIndex + 1),\n clientReqId\n )\n this.textBuffer = text.slice(spaceIndex + 1)\n }\n })\n\n textStream.on('error', (error) => {\n if (!counter) return\n this.log('Error in text stream, ending audio stream', error)\n this.isProcessing = false\n })\n\n textStream.on('end', async () => {\n // Nothing was ever said, so there is no request to close and no audio to\n // wait for.\n if (!counter || counter !== this.counter) return\n await this.initPromise\n if (counter !== this.counter) return\n\n // Send remaining buffered text\n if (this.textBuffer.trim()) {\n this.sendTranscript(this.textBuffer, clientReqId)\n this.textBuffer = ''\n }\n\n // Signal end of text input for this request\n this.socket?.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: clientReqId,\n } satisfies GradiumEosMessage)\n )\n this.log('Sent end_of_stream')\n })\n }\n\n cancel() {\n this.generation++\n if (!this.isProcessing) return\n this.log('Cancel')\n this.isProcessing = false\n this.textSent = ''\n this.textBuffer = ''\n\n // Increment counter to ignore messages from previous calls\n this.counter++\n\n // A pending reconnect would open its own socket later: cancel it so we own\n // the single fresh connection created below.\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n\n // A socket still connecting keeps its connection timeout, which would\n // otherwise fire on the connection opened below.\n if (this.connectionTimeout) {\n clearTimeout(this.connectionTimeout)\n this.connectionTimeout = undefined\n }\n\n // Closing the socket cancels the in-flight synthesis server-side. Detach the\n // old socket first so its close handler can't run and strand the next\n // request (a speak() racing right after cancel would otherwise flip\n // isProcessing back to true and make the handler skip reconnection). Then\n // open a fresh connection immediately, so a speak() that follows awaits the\n // new initPromise and lands its setup and text on the new socket.\n const socket = this.socket\n this.socket = undefined\n if (socket) closeSocket(socket)\n this.connect()\n }\n\n destroy() {\n // Set first, so the cancel() that super.destroy() runs opens no new\n // connection\n this.destroyed = true\n super.destroy()\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout)\n this.reconnectTimeout = undefined\n }\n if (this.connectionTimeout) {\n clearTimeout(this.connectionTimeout)\n this.connectionTimeout = undefined\n }\n if (this.socket) closeSocket(this.socket)\n this.socket = undefined\n this.isProcessing = false\n }\n\n private getEndpoint() {\n const region = this.options.region ?? DEFAULT_REGION\n return `wss://${region}.api.gradium.ai/api/speech/tts`\n }\n\n private async initWS() {\n return new Promise<void>((resolve, reject) => {\n const socket = new WebSocket(this.getEndpoint(), {\n headers: {\n 'x-api-key': this.options.apiKey,\n },\n })\n this.socket = socket\n\n const timeout = setTimeout(() => {\n this.log('Connection timeout')\n closeSocket(socket)\n this.socket = undefined\n reject(new Error('WebSocket connection timeout'))\n }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT)\n this.connectionTimeout = timeout\n\n socket.addEventListener('open', () => {\n clearTimeout(timeout)\n this.log('Connection opened')\n // The setup is now sent per-request from speak(); we resolve as soon as\n // the socket is open so callers can start streaming text immediately.\n resolve()\n })\n\n socket.addEventListener('error', (error) => {\n clearTimeout(timeout)\n this.log('WebSocket error:', error)\n reject(new Error('WebSocket connection error'))\n })\n\n socket.addEventListener('close', ({ code, reason }) => {\n clearTimeout(timeout)\n this.socket?.removeAllListeners()\n this.socket = undefined\n\n if (code !== 1000) {\n this.reconnect()\n } else {\n this.log('Connection closed', { code, reason })\n // Reconnect for next usage\n if (!this.isProcessing) {\n this.reconnect()\n }\n }\n })\n\n socket.addEventListener('message', (event) => {\n try {\n const message: GradiumResponse = JSON.parse(event.data.toString())\n\n switch (message.type) {\n case 'ready':\n // The server emits one ready per per-request setup. We already\n // resolved initPromise on socket open, so this is purely\n // informational. Logged so it shows up in debug traces.\n this.log('Server ready', message.request_id ?? '')\n // The server accepted the voice, so the connection is usable\n this.retryCount = 0\n break\n\n case 'audio':\n this.processAudioMessage(message)\n break\n\n case 'end_of_stream':\n // Ignore if from a previous request\n if (\n 'client_req_id' in message &&\n message.client_req_id !== this.counter.toString()\n )\n return\n this.log('Audio ended')\n this.isProcessing = false\n this.textSent = ''\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n } catch {\n this.log('Error parsing message', event.data)\n }\n })\n })\n }\n\n private sendSetup(clientReqId: string) {\n this.socket?.send(\n JSON.stringify({\n type: 'setup',\n voice_id: this.options.voiceId,\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,\n close_ws_on_eos: false,\n json_config: this.options.jsonConfig,\n client_req_id: clientReqId,\n } satisfies GradiumSetupMessage)\n )\n this.log(`Sent setup (client_req_id=${clientReqId})`)\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n this.socket?.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n this.log(`Sent transcript: \"${text}\"`)\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (message.client_req_id !== this.counter.toString()) return\n\n const chunk = Buffer.from(message.audio, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n }\n\n private reconnect() {\n if (this.destroyed) return\n this.retryCount++\n if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY)) {\n this.log('Max retries reached, giving up')\n this.emit('Failed', [this.textSent])\n return\n }\n\n this.initPromise = new Promise((resolve) => {\n this.log('Reconnecting...')\n this.reconnectTimeout = setTimeout(() => {\n this.reconnectTimeout = undefined\n // The count starts over once the server accepts the session, since it\n // opens the connection before checking the setup\n this.initWS()\n .then(() => {\n // Resend text if reconnecting during processing. Setup must come\n // first because the new socket has no session yet, and it carries\n // the same client_req_id as the in-flight request.\n if (this.textSent.length > 0) {\n const clientReqId = this.counter.toString()\n this.log('Sending text chunks again')\n this.sendSetup(clientReqId)\n this.sendTranscript(this.textSent, clientReqId)\n }\n })\n .then(resolve)\n .catch((error) => {\n this.log('Reconnection error:', error)\n this.reconnect()\n })\n }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY)\n })\n }\n}\n"],"mappings":";AAAA,SAAS,WAAW;AAEpB,OAAOA,gBAAe;;;ACFtB,OAAO,eAAe;AAQf,SAAS,YAAY,QAAmB;AAC7C,SAAO,mBAAmB;AAC1B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AAQ,kBAAkB,KAAK,QAAQ,SAAS,EAAG;AAEvD,UAAM,QAAQ,OAAO,KAAK,QAAQ,OAAO,QAAQ;AACjD,SAAK,IAAI,yBAAyB,MAAM,MAAM,SAAS;AACvD,SAAK,KAAK,SAAS,KAAK;AAAA,EAC1B;AAAA,EAEQ,YAAY;AAClB,QAAI,KAAK,UAAW;AACpB,SAAK;AACL,QAAI,KAAK,cAAc,KAAK,QAAQ,YAAYE,qBAAoB;AAClE,WAAK,IAAI,gCAAgC;AACzC,WAAK,KAAK,UAAU,CAAC,KAAK,QAAQ,CAAC;AACnC;AAAA,IACF;AAEA,SAAK,cAAc,IAAI,QAAQ,CAAC,YAAY;AAC1C,WAAK,IAAI,iBAAiB;AAC1B,WAAK,mBAAmB,WAAW,MAAM;AACvC,aAAK,mBAAmB;AAGxB,aAAK,OAAO,EACT,KAAK,MAAM;AAIV,cAAI,KAAK,SAAS,SAAS,GAAG;AAC5B,kBAAM,cAAc,KAAK,QAAQ,SAAS;AAC1C,iBAAK,IAAI,2BAA2B;AACpC,iBAAK,UAAU,WAAW;AAC1B,iBAAK,eAAe,KAAK,UAAU,WAAW;AAAA,UAChD;AAAA,QACF,CAAC,EACA,KAAK,OAAO,EACZ,MAAM,CAAC,UAAU;AAChB,eAAK,IAAI,uBAAuB,KAAK;AACrC,eAAK,UAAU;AAAA,QACjB,CAAC;AAAA,MACL,GAAG,KAAK,QAAQ,cAAcD,oBAAmB;AAAA,IACnD,CAAC;AAAA,EACH;AACF;","names":["WebSocket","WebSocket","WebSocket","DEFAULT_CONNECTION_TIMEOUT","DEFAULT_RETRY_DELAY","DEFAULT_MAX_RETRY","WebSocket"]}
|
|
1
|
+
{"version":3,"sources":["../src/GradiumSTT.ts","../src/types.ts","../src/GradiumTTS.ts"],"sourcesContent":["import {\n ReconnectingSocket,\n ReconnectingSocketHandshake,\n STT,\n} from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_INPUT_FORMAT,\n DEFAULT_LANGUAGE,\n DEFAULT_MODEL_NAME,\n DEFAULT_REGION,\n GradiumASRJsonConfig,\n GradiumASRResponse,\n GradiumASRSetupMessage,\n GradiumInputFormat,\n GradiumSTTOptions,\n} from './types'\n\n/**\n * Gradium Real-time STT (ASR)\n *\n * @see https://docs.gradium.ai/guides/speech-to-text\n */\n\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\n// Each frame of the transcription model lasts 80 ms\nconst FRAME_DURATION_MS = 80\nconst DEFAULT_DELAY_IN_FRAMES = 10\n\n/**\n * One utterance, from its first audio chunk to its transcript.\n *\n * It is capturing while its audio stream runs, then flushing once the stream\n * ended and the flush went out, and done once its transcript is emitted, by\n * the matching `flushed` or by its own timeout.\n */\ninterface Utterance {\n /** Audio sent so far, sent again whole on a new connection */\n audio: Buffer[]\n /** Text segments received for this utterance */\n transcript: string\n /** Set when the audio stream ends and the flush goes out */\n flushId?: number\n timeout?: ReturnType<typeof setTimeout>\n /** The transcript went out, on `flushed` or on timeout */\n done: boolean\n}\n\nexport class GradiumSTT extends STT {\n private socket: ReconnectingSocket\n // Utterances in the order their audio went out. The server answers in that\n // order too, so every text segment belongs to the first one not flushed yet\n private utterances: Utterance[] = []\n private flushId = 0\n private delayInFrames = DEFAULT_DELAY_IN_FRAMES // Given by the server when ready\n\n constructor(private options: GradiumSTTOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/asr`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n // The server opens the connection before checking the setup, so the\n // connection only counts as open once it answers ready\n handshake: this.handshake,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n transcribe(audioStream: Readable) {\n let utterance: Utterance | undefined\n\n // Read audio stream and send to Gradium\n audioStream.on('data', (chunk: Buffer) => {\n // The utterance starts with its first chunk. Voice detection opens a\n // stream on any noise and many of them never carry a word, so they leave\n // no utterance behind.\n if (!utterance) {\n utterance = { audio: [], transcript: '', done: false }\n this.utterances.push(utterance)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n }\n if (utterance.done) return\n utterance.audio.push(chunk)\n // Without a connection the audio waits, and goes out whole on open\n if (this.sendAudioChunk(chunk)) {\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n }\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // pending segments and confirms with a matching \"flushed\" message.\n audioStream.on('end', () => {\n if (!utterance || utterance.done) return\n const current = utterance\n current.audio.push(this.trailingSilence())\n current.flushId = ++this.flushId\n if (this.socket.isOpen) this.sendEnd(current)\n\n // Timeout transcription if no \"flushed\" confirmation is received\n current.timeout = setTimeout(() => {\n current.timeout = undefined\n this.log('Transcription timeout')\n this.emitTranscript(current)\n }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT)\n })\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n }\n\n private buildJsonConfig(): GradiumASRJsonConfig {\n const config: GradiumASRJsonConfig = { ...this.options.jsonConfig }\n if (config.language === undefined) {\n config.language = this.options.language ?? DEFAULT_LANGUAGE\n }\n return config\n }\n\n /** Sends the setup, and waits for the server to accept it */\n private handshake: ReconnectingSocketHandshake = {\n start: (socket) => {\n socket.send(\n JSON.stringify({\n type: 'setup',\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n input_format: this.options.inputFormat ?? DEFAULT_INPUT_FORMAT,\n json_config: this.buildJsonConfig(),\n } satisfies GradiumASRSetupMessage)\n )\n this.log('Sent setup')\n },\n read: (data) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return false\n }\n if (message.type === 'error') {\n throw new Error(`Setup refused: ${message.message}`)\n }\n if (message.type !== 'ready') return false\n this.log('Server ready')\n if (typeof message.delay_in_frames === 'number') {\n this.delayInFrames = message.delay_in_frames\n }\n return true\n },\n }\n\n private sendAudioChunk(chunk: Buffer) {\n return this.socket.send(\n JSON.stringify({\n type: 'audio',\n audio: chunk.toString('base64'),\n })\n )\n }\n\n /**\n * Silence as long as the transcription lags behind, to follow the speech.\n *\n * The model writes each word a few frames after hearing it, and a flush only\n * processes the audio already sent. With nothing after the last word, the\n * server confirms the flush before writing that word, which then opens the\n * next transcript.\n */\n private trailingSilence(): Buffer {\n const sampleRate = pcmSampleRate(\n this.options.inputFormat ?? DEFAULT_INPUT_FORMAT\n )\n // Silence is a run of zeros in raw PCM only\n if (!sampleRate) return Buffer.alloc(0)\n const durationMs = this.delayInFrames * FRAME_DURATION_MS\n const samples = Math.round((sampleRate * durationMs) / 1000)\n return Buffer.alloc(samples * 2)\n }\n\n /** Sends the trailing silence, already the last audio chunk, and the flush */\n private sendEnd(utterance: Utterance) {\n const silence = utterance.audio[utterance.audio.length - 1]\n if (silence.length) this.sendAudioChunk(silence)\n this.socket.send(\n JSON.stringify({ type: 'flush', flush_id: utterance.flushId })\n )\n this.log(`Sent flush (flush_id=${utterance.flushId})`)\n }\n\n private emitTranscript(utterance: Utterance) {\n if (utterance.done) return\n utterance.done = true\n clearTimeout(utterance.timeout)\n utterance.timeout = undefined\n // Its audio is no longer needed, but the utterance stays in the queue until\n // its flush returns, so the late segments of a timed out utterance don't\n // land in the next one\n utterance.audio = []\n const transcript = utterance.transcript.replace(/\\s+/g, ' ').trim()\n this.log(`Received transcript: \"${transcript}\"`)\n this.emit('Transcript', transcript)\n }\n\n // A new connection knows nothing of the utterances under way: the audio read\n // before the first connection opened, or lost with the previous one, goes out\n // again whole, and the server transcribes it from the start\n private onOpen = () => {\n // A new session never confirms the flushes of the previous one\n this.utterances = this.utterances.filter((utterance) => !utterance.done)\n if (this.utterances.length === 0) return\n this.log('Sending audio chunks again')\n for (const utterance of this.utterances) {\n utterance.transcript = ''\n if (utterance.flushId === undefined) {\n utterance.audio.forEach((chunk) => this.sendAudioChunk(chunk))\n } else {\n // The trailing silence goes out with the flush\n utterance.audio\n .slice(0, -1)\n .forEach((chunk) => this.sendAudioChunk(chunk))\n this.sendEnd(utterance)\n }\n }\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumASRResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'text': {\n const utterance = this.utterances[0]\n if (typeof message.text === 'string' && utterance) {\n utterance.transcript +=\n (utterance.transcript ? ' ' : '') + message.text\n this.log(`Received text segment: \"${message.text}\"`)\n }\n break\n }\n\n case 'flushed': {\n // Every segment of the flushed utterance arrived. Those before it in\n // the queue are flushed too, since the server answers in order.\n const index = this.utterances.findIndex(\n (utterance) => utterance.flushId === message.flush_id\n )\n if (index === -1) break\n const flushed = this.utterances.splice(0, index + 1)\n flushed.forEach((utterance) => this.emitTranscript(utterance))\n break\n }\n\n case 'end_text':\n // Segment finalized, nothing to accumulate (text already received)\n break\n\n case 'step':\n // Voice activity detection frames, ignored\n break\n\n case 'end_of_stream':\n this.log('End of stream')\n break\n\n case 'error':\n this.log('Error:', message.message, message.code)\n break\n }\n }\n\n private onFailed = () => {\n // The audio of every utterance still waiting for its transcript, for a\n // fallback to transcribe it instead\n const pending = this.utterances.filter((utterance) => !utterance.done)\n this.utterances.forEach((utterance) => clearTimeout(utterance.timeout))\n this.utterances = []\n this.emit(\n 'Failed',\n pending.flatMap((utterance) => utterance.audio)\n )\n }\n}\n\n/** Sample rate of a raw PCM input format, undefined for an encoded one */\nfunction pcmSampleRate(format: GradiumInputFormat): number | undefined {\n if (format === 'pcm') return 24000\n const match = format.match(/^pcm_(\\d+)$/)\n return match ? Number(match[1]) : undefined\n}\n","export type GradiumOutputFormat =\n | 'pcm'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n\nexport type GradiumInputFormat =\n | 'pcm'\n | 'pcm_8000'\n | 'pcm_16000'\n | 'pcm_24000'\n | 'pcm_48000'\n | 'wav'\n | 'opus'\n | 'ulaw_8000'\n | 'alaw_8000'\n\nexport type GradiumRegion = 'eu' | 'us'\n\nexport interface GradiumTTSOptions {\n apiKey: string\n voiceId: string\n modelName?: string\n outputFormat?: GradiumOutputFormat\n region?: GradiumRegion\n jsonConfig?: GradiumJsonConfig\n connectionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\nexport interface GradiumJsonConfig {\n padding_bonus?: number // Speed control: -4.0 to 4.0\n temp?: number // Temperature: 0 to 1.4, default 0.7\n cfg_coef?: number // Voice similarity: 1.0 to 4.0, default 2.0\n}\n\nexport const DEFAULT_MODEL_NAME = 'default'\nexport const DEFAULT_OUTPUT_FORMAT: GradiumOutputFormat = 'pcm_16000'\nexport const DEFAULT_INPUT_FORMAT: GradiumInputFormat = 'pcm_16000'\nexport const DEFAULT_REGION: GradiumRegion = 'eu'\n// Gradium refuses a transcription without a language, \"any\" detects it\nexport const DEFAULT_LANGUAGE = 'any'\n\n// Speech-to-text (ASR) types\n\nexport interface GradiumASRJsonConfig {\n language?: string\n target_language?: string\n delay_in_frames?: number // 0 to 80, each frame = 80ms\n temp?: number\n padding_bonus?: number\n}\n\nexport interface GradiumSTTOptions {\n apiKey: string\n modelName?: string\n inputFormat?: GradiumInputFormat\n language?: string\n region?: GradiumRegion\n jsonConfig?: GradiumASRJsonConfig\n connectionTimeout?: number\n transcriptionTimeout?: number\n retryDelay?: number\n maxRetry?: number\n}\n\n// Client -> Server ASR messages\n\nexport interface GradiumASRSetupMessage {\n type: 'setup'\n model_name: string\n input_format: string\n json_config?: GradiumASRJsonConfig\n}\n\nexport interface GradiumAudioMessage {\n type: 'audio'\n audio: string // Base64 encoded audio data\n}\n\nexport interface GradiumFlushMessage {\n type: 'flush'\n flush_id: number\n}\n\n// Server -> Client ASR messages\n\nexport type GradiumASRResponse =\n | GradiumReadyResponse\n | GradiumTextResponse\n | GradiumEndTextResponse\n | GradiumStepResponse\n | GradiumFlushedResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumTextResponse {\n type: 'text'\n text: string\n start_s?: number\n stream_id?: number\n}\n\nexport interface GradiumEndTextResponse {\n type: 'end_text'\n stop_s?: number\n stream_id?: number\n}\n\nexport interface GradiumStepResponse {\n type: 'step'\n vad?: Array<{ horizon_s: number; inactivity_prob: number }>\n step_idx?: number\n step_duration_s?: number\n total_duration_s?: number\n}\n\nexport interface GradiumFlushedResponse {\n type: 'flushed'\n flush_id: number\n}\n\n// Client -> Server messages\n\nexport interface GradiumSetupMessage {\n type: 'setup'\n voice_id: string\n model_name: string\n output_format: string\n close_ws_on_eos?: boolean\n json_config?: GradiumJsonConfig\n client_req_id?: string\n}\n\nexport interface GradiumTextMessage {\n type: 'text'\n text: string\n client_req_id?: string\n}\n\nexport interface GradiumEosMessage {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\n// Server -> Client messages\n\nexport type GradiumResponse =\n | GradiumReadyResponse\n | GradiumAudioResponse\n | GradiumEosResponse\n | GradiumErrorResponse\n\nexport interface GradiumReadyResponse {\n type: 'ready'\n request_id?: string\n delay_in_frames?: number // Frames of 80ms the transcription lags behind the audio\n}\n\nexport interface GradiumAudioResponse {\n type: 'audio'\n audio: string // Base64 encoded PCM data\n client_req_id?: string\n}\n\nexport interface GradiumEosResponse {\n type: 'end_of_stream'\n client_req_id?: string\n}\n\nexport interface GradiumErrorResponse {\n type: 'error'\n message: string\n code?: number\n}\n","import { ReconnectingSocket, TTS } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport { RawData } from 'ws'\nimport {\n DEFAULT_MODEL_NAME,\n DEFAULT_OUTPUT_FORMAT,\n DEFAULT_REGION,\n GradiumAudioResponse,\n GradiumEosMessage,\n GradiumResponse,\n GradiumSetupMessage,\n GradiumTextMessage,\n GradiumTTSOptions,\n} from './types'\n\n// API Reference: https://gradium.ai/api_docs.html\n\n/**\n * The utterance Gradium is synthesizing. While there is one the TTS is\n * speaking, and when there is none it is idle: every way out of a request\n * (end of stream, server error, cancel, text stream error, give up) clears it,\n * so nothing about it outlives it.\n */\ninterface GradiumRequest {\n id: string\n /** Complete words received so far, sent again whole on a new connection */\n words: string\n /** Start of a word, held until a space completes it or the stream ends */\n partial: string\n /** The text stream ended, so Gradium was told no more text follows */\n ended: boolean\n}\n\nexport class GradiumTTS extends TTS {\n private socket: ReconnectingSocket\n // Numbers the audio requests, and only moves when there is something to say\n private counter = 0\n // Bumped by every speak() and every cancel(), so a request claimed late can\n // tell whether it is still the one that should be heard\n private generation = 0\n private request?: GradiumRequest\n\n constructor(private readonly options: GradiumTTSOptions) {\n super()\n\n const region = options.region ?? DEFAULT_REGION\n this.socket = new ReconnectingSocket({\n url: `wss://${region}.api.gradium.ai/api/speech/tts`,\n clientOptions: { headers: { 'x-api-key': options.apiKey } },\n connectionTimeout: options.connectionTimeout,\n retryDelay: options.retryDelay,\n maxRetry: options.maxRetry,\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let request: GradiumRequest | undefined\n\n // Claiming the request is deferred until there is something to say.\n //\n // Taking the next id right away would silence the utterance still playing,\n // because incoming audio is matched against the current id and anything\n // older is dropped. A stream that never carries a word, which is what an\n // answer skipped by a tool or by onBeforeAnswer hands over, would then cut\n // the assistant off mid-sentence for nothing.\n const claimRequest = () => {\n if (request) return request\n // Cancelled, or superseded by another speak(), before the first word\n if (this.generation !== generation) return\n request = {\n id: (++this.counter).toString(),\n words: '',\n partial: '',\n ended: false,\n }\n this.request = request\n // Each speak() is its own multiplexed request, opened by a setup stamped\n // with its client_req_id, so the server binds the voice to it.\n // Multiplexing is what lets the connection serve several utterances\n // (sequential mode only works for the first request on a socket). Without\n // a connection, the setup goes out on open.\n this.sendSetup(request.id)\n // A connection that gave up gets a new chance with a new utterance\n this.socket.connect()\n return request\n }\n\n textStream.on('data', (chunk: Buffer) => {\n const request = claimRequest()\n if (!request || this.request !== request) return\n const text = chunk.toString('utf-8').replace(/[\\r\\n ]+/g, ' ')\n\n // Only complete words go out, up to the last space\n const spaceIndex = text.lastIndexOf(' ')\n if (spaceIndex === -1) {\n request.partial += text\n return\n }\n const words = request.partial + text.slice(0, spaceIndex + 1)\n request.partial = text.slice(spaceIndex + 1)\n request.words += words\n // Without a connection the words wait, and go out whole on open\n this.sendTranscript(words, request.id)\n })\n\n textStream.on('error', (error) => {\n if (!request || this.request !== request) return\n this.log('Error in text stream, cancelling the audio', error)\n this.stopRequest()\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is no request to close and no audio to\n // wait for\n if (!request || this.request !== request) return\n request.ended = true\n this.sendEnd(request)\n })\n }\n\n /**\n * Stops the current utterance. Gradium can only stop a synthesis by closing\n * its connection, so a new one opens right away.\n */\n cancel() {\n this.generation++\n if (!this.request) return\n this.log('Cancel')\n this.stopRequest()\n }\n\n destroy() {\n // Destroyed first, so the cancel() that super.destroy() runs opens no new\n // connection\n this.socket.destroy()\n super.destroy()\n this.request = undefined\n }\n\n /** Forgets the current request, and closes the connection that carries it */\n private stopRequest() {\n this.request = undefined\n // Free of charge on the retry budget, and a speak() that follows lands its\n // setup and text on the new connection\n this.socket.restart()\n }\n\n private sendSetup(clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'setup',\n voice_id: this.options.voiceId,\n model_name: this.options.modelName ?? DEFAULT_MODEL_NAME,\n output_format: this.options.outputFormat ?? DEFAULT_OUTPUT_FORMAT,\n close_ws_on_eos: false,\n json_config: this.options.jsonConfig,\n client_req_id: clientReqId,\n } satisfies GradiumSetupMessage)\n )\n if (sent) this.log(`Sent setup (client_req_id=${clientReqId})`)\n }\n\n private sendTranscript(text: string, clientReqId: string) {\n const sent = this.socket.send(\n JSON.stringify({\n type: 'text',\n text,\n client_req_id: clientReqId,\n } satisfies GradiumTextMessage)\n )\n if (sent) this.log(`Sent transcript: \"${text}\"`)\n }\n\n /** Sends the last partial word, then the end of the text */\n private sendEnd(request: GradiumRequest) {\n if (request.partial.trim()) {\n this.sendTranscript(request.partial, request.id)\n }\n const sent = this.socket.send(\n JSON.stringify({\n type: 'end_of_stream',\n client_req_id: request.id,\n } satisfies GradiumEosMessage)\n )\n if (sent) this.log('Sent end_of_stream')\n }\n\n // A new connection has no session for the request under way: its setup and\n // its words go out again, the setup first, with the same client_req_id\n private onOpen = () => {\n const request = this.request\n if (!request) return\n this.log('Sending the request again')\n this.sendSetup(request.id)\n if (request.words) this.sendTranscript(request.words, request.id)\n if (request.ended) this.sendEnd(request)\n }\n\n private onMessage = (data: RawData) => {\n let message: GradiumResponse\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Error parsing message', data)\n return\n }\n\n switch (message.type) {\n case 'ready':\n // The server emits one ready per setup, informational only\n this.log('Server ready', message.request_id ?? '')\n break\n\n case 'audio':\n this.processAudioMessage(message)\n break\n\n case 'end_of_stream':\n // Ignore if from a previous request\n if (\n !this.request ||\n (message.client_req_id !== undefined &&\n message.client_req_id !== this.request.id)\n )\n return\n this.log('Audio ended')\n this.request = undefined\n break\n\n case 'error':\n // The connection carries one request at a time, since cancel() closes\n // it, so the error is about the current one, which gets no more audio.\n // Its text is handed over, so a FallbackTTS can speak it instead, and\n // a fresh connection keeps the next request clear of it.\n this.log('Error:', message.message, message.code)\n if (!this.request) break\n this.onFailed()\n this.socket.restart()\n break\n }\n }\n\n private processAudioMessage(message: GradiumAudioResponse) {\n // Ignore messages from previous requests\n if (!this.request || message.client_req_id !== this.request.id) return\n\n const chunk = Buffer.from(message.audio, 'base64')\n this.log(`Received audio chunk (${chunk.length} bytes)`)\n this.emit('Audio', chunk)\n }\n\n private onFailed = () => {\n const request = this.request\n this.request = undefined\n const text = request ? request.words + request.partial : ''\n this.emit('Failed', text ? [text] : [])\n }\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EAEA;AAAA,OACK;;;ACqCA,IAAM,qBAAqB;AAC3B,IAAM,wBAA6C;AACnD,IAAM,uBAA2C;AACjD,IAAM,iBAAgC;AAEtC,IAAM,mBAAmB;;;ADrBhC,IAAM,gCAAgC;AAEtC,IAAM,oBAAoB;AAC1B,IAAM,0BAA0B;AAqBzB,IAAM,aAAN,cAAyB,IAAI;AAAA;AAAA,EAQlC,YAAoB,SAA4B;AAC9C,UAAM;AADY;AAJpB;AAAA;AAAA,SAAQ,aAA0B,CAAC;AACnC,SAAQ,UAAU;AAClB,SAAQ,gBAAgB;AA+ExB;AAAA,SAAQ,YAAyC;AAAA,MAC/C,OAAO,CAAC,WAAW;AACjB,eAAO;AAAA,UACL,KAAK,UAAU;AAAA,YACb,MAAM;AAAA,YACN,YAAY,KAAK,QAAQ,aAAa;AAAA,YACtC,cAAc,KAAK,QAAQ,eAAe;AAAA,YAC1C,aAAa,KAAK,gBAAgB;AAAA,UACpC,CAAkC;AAAA,QACpC;AACA,aAAK,IAAI,YAAY;AAAA,MACvB;AAAA,MACA,MAAM,CAAC,SAAS;AACd,YAAI;AACJ,YAAI;AACF,oBAAU,KAAK,MAAM,KAAK,SAAS,CAAC;AAAA,QACtC,QAAQ;AACN,eAAK,IAAI,yBAAyB,IAAI;AACtC,iBAAO;AAAA,QACT;AACA,YAAI,QAAQ,SAAS,SAAS;AAC5B,gBAAM,IAAI,MAAM,kBAAkB,QAAQ,OAAO,EAAE;AAAA,QACrD;AACA,YAAI,QAAQ,SAAS,QAAS,QAAO;AACrC,aAAK,IAAI,cAAc;AACvB,YAAI,OAAO,QAAQ,oBAAoB,UAAU;AAC/C,eAAK,gBAAgB,QAAQ;AAAA,QAC/B;AACA,eAAO;AAAA,MACT;AAAA,IACF;AAyDA;AAAA;AAAA;AAAA,SAAQ,SAAS,MAAM;A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QAAQ,OAAO,KAAK,QAAQ,OAAO,QAAQ;AACjD,SAAK,IAAI,yBAAyB,MAAM,MAAM,SAAS;AACvD,SAAK,KAAK,SAAS,KAAK;AAAA,EAC1B;AAQF;","names":["ReconnectingSocket","ReconnectingSocket","request"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@micdrop/gradium",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Gradium TTS implementation for @micdrop/server",
|
|
5
5
|
"author": "Godefroy de Compreignac",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"ws": "^8.19.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@micdrop/server": "
|
|
42
|
+
"@micdrop/server": ">=2.10.0"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/ws": "^8.18.1",
|