@dtelecom/agents-js 0.1.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/LICENSE +190 -0
- package/README.md +153 -0
- package/dist/chunk-6OWWB2X7.mjs +17 -0
- package/dist/chunk-6OWWB2X7.mjs.map +1 -0
- package/dist/chunk-BN7PIFNJ.mjs +54 -0
- package/dist/chunk-BN7PIFNJ.mjs.map +1 -0
- package/dist/chunk-RQKGHAFV.mjs +412 -0
- package/dist/chunk-RQKGHAFV.mjs.map +1 -0
- package/dist/index.d.mts +343 -0
- package/dist/index.d.ts +343 -0
- package/dist/index.js +1750 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1201 -0
- package/dist/index.mjs.map +1 -0
- package/dist/providers/index.d.mts +142 -0
- package/dist/providers/index.d.ts +142 -0
- package/dist/providers/index.js +613 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/index.mjs +524 -0
- package/dist/providers/index.mjs.map +1 -0
- package/dist/room-memory-VAREPHY6.mjs +8 -0
- package/dist/room-memory-VAREPHY6.mjs.map +1 -0
- package/dist/types-Cs5uUoTC.d.mts +259 -0
- package/dist/types-Cs5uUoTC.d.ts +259 -0
- package/package.json +89 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/providers/index.ts","../../src/providers/deepgram-stt.ts","../../src/core/base-stt-stream.ts","../../src/utils/logger.ts","../../src/providers/openrouter-llm.ts","../../src/providers/cartesia-tts.ts"],"sourcesContent":["export { DeepgramSTT } from './deepgram-stt';\nexport type { DeepgramSTTOptions } from './deepgram-stt';\n\nexport { OpenRouterLLM } from './openrouter-llm';\nexport type { OpenRouterLLMOptions } from './openrouter-llm';\n\nexport { CartesiaTTS } from './cartesia-tts';\nexport type { CartesiaTTSOptions } from './cartesia-tts';\n","/**\n * DeepgramSTT — real-time streaming STT via Deepgram WebSocket API.\n *\n * Protocol:\n * - Connect to wss://api.deepgram.com/v1/listen?... with config as query params\n * - Auth via Authorization header: \"Token <apiKey>\"\n * - Send audio as binary WebSocket frames (PCM16 16kHz mono)\n * - Receive JSON: { type: \"Results\", channel: { alternatives: [{ transcript }] }, is_final, speech_final }\n * - Send KeepAlive every 5s when no audio is being sent\n * - Send CloseStream to gracefully shut down\n *\n * End-of-utterance strategy:\n * Buffer all is_final=true transcripts. Emit the buffered utterance as a\n * single final TranscriptionResult when speech_final=true OR UtteranceEnd\n * arrives. Interim results (is_final=false) are emitted immediately for\n * real-time feedback.\n */\n\nimport WebSocket from 'ws';\nimport { BaseSTTStream } from '../core/base-stt-stream';\nimport type { STTPlugin, STTStream, STTStreamOptions, TranscriptionResult } from '../core/types';\nimport { createLogger } from '../utils/logger';\n\nconst log = createLogger('DeepgramSTT');\n\nconst DEEPGRAM_WS_URL = 'wss://api.deepgram.com/v1/listen';\nconst KEEPALIVE_INTERVAL_MS = 5_000;\n\nexport interface DeepgramSTTOptions {\n apiKey: string;\n /** Deepgram model (default: 'nova-3') */\n model?: string;\n /** Language code (default: 'en') */\n language?: string;\n /** Enable interim results (default: true) */\n interimResults?: boolean;\n /** Enable punctuation (default: true) */\n punctuate?: boolean;\n /** Endpointing in ms (default: 300). Set to false to disable. */\n endpointing?: number | false;\n /** Keywords to boost recognition (e.g. ['dTelecom:5', 'WebRTC:3']) */\n keywords?: string[];\n /** Enable smart formatting (default: false) */\n smartFormat?: boolean;\n /** Utterance end timeout in ms (default: 1000). Requires interimResults. */\n utteranceEndMs?: number;\n}\n\nexport class DeepgramSTT implements STTPlugin {\n private readonly options: Required<Pick<DeepgramSTTOptions, 'apiKey'>> & DeepgramSTTOptions;\n\n constructor(options: DeepgramSTTOptions) {\n if (!options.apiKey) {\n throw new Error('DeepgramSTT requires an apiKey');\n }\n this.options = options;\n }\n\n createStream(options?: STTStreamOptions): STTStream {\n const language = options?.language ?? this.options.language ?? 'en';\n return new DeepgramSTTStream(this.options, language);\n }\n}\n\nclass DeepgramSTTStream extends BaseSTTStream {\n private ws: WebSocket | null = null;\n private readonly apiKey: string;\n private readonly wsUrl: string;\n private _ready = false;\n private _closed = false;\n private pendingAudio: Buffer[] = [];\n private keepAliveTimer: ReturnType<typeof setInterval> | null = null;\n private lastAudioSentAt = 0;\n /** Buffer of is_final=true transcripts for the current utterance */\n private utteranceBuffer: string[] = [];\n /** Timestamp of the last non-empty interim result (approximates end of speech) */\n private lastInterimAt = 0;\n\n constructor(options: DeepgramSTTOptions, language: string) {\n super();\n this.apiKey = options.apiKey;\n this.wsUrl = buildWsUrl(options, language);\n this.connect();\n }\n\n sendAudio(pcm16: Buffer): void {\n if (this._closed) return;\n\n if (!this._ready) {\n this.pendingAudio.push(pcm16);\n return;\n }\n\n if (this.ws?.readyState === WebSocket.OPEN) {\n this.ws.send(pcm16);\n this.lastAudioSentAt = performance.now();\n }\n }\n\n async close(): Promise<void> {\n if (this._closed) return;\n this._closed = true;\n this._ready = false;\n this.pendingAudio = [];\n this.stopKeepAlive();\n\n if (this.ws?.readyState === WebSocket.OPEN) {\n // Graceful shutdown — ask server to flush remaining audio\n try {\n this.ws.send(JSON.stringify({ type: 'CloseStream' }));\n } catch {\n // Ignore send errors during shutdown\n }\n }\n\n if (this.ws) {\n this.ws.close();\n this.ws = null;\n }\n\n log.debug('DeepgramSTT stream closed');\n }\n\n private connect(): void {\n log.debug(`Connecting to Deepgram: ${this.wsUrl.replace(/token=[^&]+/, 'token=***')}`);\n\n this.ws = new WebSocket(this.wsUrl, {\n headers: {\n Authorization: `Token ${this.apiKey}`,\n },\n });\n\n this.ws.on('open', () => {\n log.info('Deepgram WebSocket connected');\n this._ready = true;\n\n // Flush pending audio\n for (const buf of this.pendingAudio) {\n if (this.ws?.readyState === WebSocket.OPEN) {\n this.ws.send(buf);\n }\n }\n this.pendingAudio = [];\n\n this.startKeepAlive();\n });\n\n this.ws.on('message', (data) => {\n try {\n const msg = JSON.parse(data.toString());\n this.handleMessage(msg);\n } catch (err) {\n log.error('Failed to parse Deepgram message:', err);\n }\n });\n\n this.ws.on('error', (err) => {\n log.error('Deepgram WebSocket error:', err);\n this.emit('error', err instanceof Error ? err : new Error(String(err)));\n });\n\n this.ws.on('close', (code, reason) => {\n log.debug(`Deepgram WebSocket closed: ${code} ${reason.toString()}`);\n this._ready = false;\n this.stopKeepAlive();\n\n // Reconnect if not intentionally closed\n if (!this._closed) {\n log.info('Deepgram connection lost, reconnecting in 1s...');\n setTimeout(() => {\n if (!this._closed) this.connect();\n }, 1000);\n }\n });\n }\n\n private handleMessage(msg: Record<string, unknown>): void {\n const type = msg.type as string;\n\n if (type === 'Results') {\n this.handleResults(msg);\n } else if (type === 'UtteranceEnd') {\n this.flushUtterance();\n } else if (type === 'Metadata') {\n log.debug('Deepgram session metadata received');\n } else if (type === 'SpeechStarted') {\n log.debug('Speech started detected');\n }\n }\n\n private handleResults(msg: Record<string, unknown>): void {\n const channel = msg.channel as { alternatives?: Array<{ transcript?: string; confidence?: number }> } | undefined;\n const transcript = channel?.alternatives?.[0]?.transcript ?? '';\n const confidence = channel?.alternatives?.[0]?.confidence;\n const isFinal = msg.is_final as boolean ?? false;\n const speechFinal = msg.speech_final as boolean ?? false;\n\n if (!transcript) return;\n\n if (!isFinal) {\n // Interim result — emit immediately for real-time feedback.\n // Include any buffered finals as prefix so the UI shows the full utterance.\n this.lastInterimAt = performance.now();\n const fullInterim = this.utteranceBuffer.length > 0\n ? this.utteranceBuffer.join(' ') + ' ' + transcript\n : transcript;\n this.emit('transcription', {\n text: fullInterim,\n isFinal: false,\n confidence: confidence ?? undefined,\n } satisfies TranscriptionResult);\n return;\n }\n\n // is_final=true — buffer this segment\n this.utteranceBuffer.push(transcript);\n\n if (speechFinal) {\n // End of utterance — emit the complete buffered transcript\n this.flushUtterance();\n }\n }\n\n /** Emit the buffered utterance as a single final transcription result. */\n private flushUtterance(): void {\n if (this.utteranceBuffer.length === 0) return;\n\n const now = performance.now();\n const fullText = this.utteranceBuffer.join(' ');\n this.utteranceBuffer = [];\n\n // sttDuration = time from last interim (≈ end of speech) to now (final result)\n // This includes endpointing delay + STT processing + network\n const sttDuration = this.lastInterimAt > 0 ? now - this.lastInterimAt : undefined;\n\n if (sttDuration !== undefined) {\n log.info(`stt_final: ${sttDuration.toFixed(0)}ms \"${fullText.slice(0, 50)}\"`);\n }\n\n this.lastInterimAt = 0;\n\n this.emit('transcription', {\n text: fullText,\n isFinal: true,\n sttDuration,\n } satisfies TranscriptionResult);\n }\n\n private startKeepAlive(): void {\n this.stopKeepAlive();\n this.keepAliveTimer = setInterval(() => {\n if (this.ws?.readyState === WebSocket.OPEN) {\n this.ws.send(JSON.stringify({ type: 'KeepAlive' }));\n }\n }, KEEPALIVE_INTERVAL_MS);\n }\n\n private stopKeepAlive(): void {\n if (this.keepAliveTimer) {\n clearInterval(this.keepAliveTimer);\n this.keepAliveTimer = null;\n }\n }\n}\n\n/** Build the Deepgram WebSocket URL with query parameters. */\nfunction buildWsUrl(options: DeepgramSTTOptions, language: string): string {\n const params = new URLSearchParams();\n\n params.set('model', options.model ?? 'nova-3');\n params.set('language', language);\n params.set('encoding', 'linear16');\n params.set('sample_rate', '16000');\n params.set('channels', '1');\n params.set('interim_results', String(options.interimResults ?? true));\n params.set('punctuate', String(options.punctuate ?? true));\n\n if (options.endpointing === false) {\n params.set('endpointing', 'false');\n } else {\n params.set('endpointing', String(options.endpointing ?? 300));\n }\n\n if (options.smartFormat) {\n params.set('smart_format', 'true');\n }\n\n if (options.utteranceEndMs !== undefined) {\n params.set('utterance_end_ms', String(options.utteranceEndMs));\n } else if (options.interimResults !== false) {\n // Default utterance_end_ms when interim results are enabled\n params.set('utterance_end_ms', '1000');\n }\n\n if (options.keywords?.length) {\n for (const kw of options.keywords) {\n params.append('keywords', kw);\n }\n }\n\n return `${DEEPGRAM_WS_URL}?${params.toString()}`;\n}\n","import { EventEmitter } from 'events';\nimport type { STTStream, TranscriptionResult } from './types';\n\n/**\n * Abstract base class for STT streams.\n * Provides typed EventEmitter interface for transcription events.\n * Provider implementations should extend this class.\n */\nexport abstract class BaseSTTStream extends EventEmitter implements STTStream {\n abstract sendAudio(pcm16: Buffer): void;\n abstract close(): Promise<void>;\n\n override on(event: 'transcription', cb: (result: TranscriptionResult) => void): this;\n override on(event: 'error', cb: (error: Error) => void): this;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n override on(event: string, cb: (...args: any[]) => void): this {\n return super.on(event, cb);\n }\n\n override emit(event: 'transcription', result: TranscriptionResult): boolean;\n override emit(event: 'error', error: Error): boolean;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n override emit(event: string | symbol, ...args: any[]): boolean {\n return super.emit(event, ...args);\n }\n}\n","export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';\n\nconst LEVELS: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n silent: 4,\n};\n\n/** Default to 'debug' if DEBUG env var matches our namespace */\nfunction detectLevel(): LogLevel {\n const debug = typeof process !== 'undefined' && process.env?.DEBUG;\n if (debug && (debug === '*' || debug.includes('@dtelecom/agents'))) {\n return 'debug';\n }\n return 'info';\n}\n\nlet globalLevel: LogLevel = detectLevel();\n\nexport function setLogLevel(level: LogLevel): void {\n globalLevel = level;\n}\n\nexport function getLogLevel(): LogLevel {\n return globalLevel;\n}\n\nexport interface Logger {\n debug(...args: unknown[]): void;\n info(...args: unknown[]): void;\n warn(...args: unknown[]): void;\n error(...args: unknown[]): void;\n}\n\nfunction timestamp(): string {\n const d = new Date();\n const h = String(d.getHours()).padStart(2, '0');\n const m = String(d.getMinutes()).padStart(2, '0');\n const s = String(d.getSeconds()).padStart(2, '0');\n const ms = String(d.getMilliseconds()).padStart(3, '0');\n return `${h}:${m}:${s}.${ms}`;\n}\n\nexport function createLogger(tag: string): Logger {\n const prefix = `[@dtelecom/agents:${tag}]`;\n return {\n debug(...args: unknown[]) {\n if (LEVELS[globalLevel] <= LEVELS.debug) console.debug(timestamp(), prefix, ...args);\n },\n info(...args: unknown[]) {\n if (LEVELS[globalLevel] <= LEVELS.info) console.info(timestamp(), prefix, ...args);\n },\n warn(...args: unknown[]) {\n if (LEVELS[globalLevel] <= LEVELS.warn) console.warn(timestamp(), prefix, ...args);\n },\n error(...args: unknown[]) {\n if (LEVELS[globalLevel] <= LEVELS.error) console.error(timestamp(), prefix, ...args);\n },\n };\n}\n","/**\n * OpenRouterLLM — streaming LLM via OpenRouter (OpenAI-compatible API).\n *\n * Uses native fetch() with SSE parsing for streaming responses.\n * No SDK dependency — just HTTP.\n */\n\nimport type { LLMPlugin, LLMChunk, Message } from '../core/types';\nimport { createLogger } from '../utils/logger';\n\nconst log = createLogger('OpenRouterLLM');\n\nconst OPENROUTER_URL = 'https://openrouter.ai/api/v1/chat/completions';\n\nexport interface OpenRouterLLMOptions {\n apiKey: string;\n /** Model identifier (e.g. 'openai/gpt-4o', 'anthropic/claude-sonnet-4') */\n model: string;\n /** Max tokens in response (default: 512) */\n maxTokens?: number;\n /** Sampling temperature 0-2 (default: 0.7) */\n temperature?: number;\n /** OpenRouter provider routing preferences */\n providerRouting?: {\n /** Sort providers by metric (e.g. 'latency') */\n sort?: string;\n /** Pin to specific providers in order */\n order?: string[];\n /** Allow fallback to other providers if pinned ones fail */\n allowFallbacks?: boolean;\n };\n}\n\nexport class OpenRouterLLM implements LLMPlugin {\n private readonly apiKey: string;\n private readonly model: string;\n private readonly maxTokens: number;\n private readonly temperature: number;\n private readonly provider?: { sort?: string; order?: string[]; allow_fallbacks?: boolean };\n\n constructor(options: OpenRouterLLMOptions) {\n if (!options.apiKey) {\n throw new Error('OpenRouterLLM requires an apiKey');\n }\n this.apiKey = options.apiKey;\n this.model = options.model;\n this.maxTokens = options.maxTokens ?? 512;\n this.temperature = options.temperature ?? 0.7;\n\n if (options.providerRouting) {\n this.provider = {\n sort: options.providerRouting.sort,\n order: options.providerRouting.order,\n allow_fallbacks: options.providerRouting.allowFallbacks,\n };\n }\n }\n\n /**\n * Warm up the LLM by sending the system prompt and a short message.\n * Primes the HTTP/TLS connection and model loading on the provider side.\n */\n async warmup(systemPrompt: string): Promise<void> {\n log.info('Warming up LLM connection...');\n const start = performance.now();\n\n const messages: Message[] = [\n { role: 'system', content: systemPrompt },\n { role: 'user', content: 'Hello' },\n ];\n\n try {\n const gen = this.chat(messages);\n for await (const chunk of gen) {\n if (chunk.type === 'done') break;\n }\n log.info(`LLM warmup complete in ${(performance.now() - start).toFixed(0)}ms`);\n } catch (err) {\n log.warn('LLM warmup failed (non-fatal):', err);\n }\n }\n\n async *chat(messages: Message[], signal?: AbortSignal): AsyncGenerator<LLMChunk> {\n const body: Record<string, unknown> = {\n model: this.model,\n messages,\n max_tokens: this.maxTokens,\n temperature: this.temperature,\n stream: true,\n };\n if (this.provider) {\n body.provider = this.provider;\n }\n\n log.debug(`LLM request: model=${this.model}, messages=${messages.length}`);\n\n const response = await fetch(OPENROUTER_URL, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(body),\n signal,\n });\n\n if (!response.ok) {\n const errorText = await response.text();\n throw new Error(`OpenRouter API error ${response.status}: ${errorText}`);\n }\n\n if (!response.body) {\n throw new Error('OpenRouter response has no body');\n }\n\n // Parse SSE stream\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n let buffer = '';\n\n try {\n while (true) {\n // Check abort before blocking on read — prevents hanging when signal\n // was fired while we were yielding tokens to the pipeline\n if (signal?.aborted) break;\n\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed || !trimmed.startsWith('data: ')) continue;\n\n const data = trimmed.slice(6);\n if (data === '[DONE]') {\n yield { type: 'done' };\n return;\n }\n\n try {\n const parsed = JSON.parse(data);\n const choice = parsed.choices?.[0];\n if (!choice) continue;\n\n const delta = choice.delta;\n if (delta?.content) {\n yield { type: 'token', token: delta.content };\n }\n\n // Usage stats in the final chunk\n if (parsed.usage) {\n yield {\n type: 'done',\n usage: {\n promptTokens: parsed.usage.prompt_tokens,\n completionTokens: parsed.usage.completion_tokens,\n },\n };\n return;\n }\n } catch {\n // Skip malformed JSON chunks\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n\n yield { type: 'done' };\n }\n}\n","/**\n * CartesiaTTS — real-time streaming TTS via Cartesia WebSocket API.\n *\n * Protocol:\n * - Connect to wss://api.cartesia.ai/tts/websocket?api_key=...&cartesia_version=...\n * - Send JSON: { model_id, transcript, voice: { mode: \"id\", id }, output_format, context_id }\n * - Receive JSON: { type: \"chunk\", data: \"<base64 PCM>\" } — audio data\n * - Receive JSON: { type: \"done\", context_id } — synthesis complete\n * - Audio is base64-encoded PCM16 LE at the requested sample rate\n *\n * Uses a persistent WebSocket connection to avoid per-sentence handshake overhead.\n * Each synthesize() call uses a unique context_id for multiplexing.\n */\n\nimport WebSocket from 'ws';\nimport type { TTSPlugin } from '../core/types';\nimport { createLogger } from '../utils/logger';\n\nconst log = createLogger('CartesiaTTS');\n\nconst CARTESIA_WS_BASE = 'wss://api.cartesia.ai/tts/websocket';\nconst DEFAULT_API_VERSION = '2024-06-10';\nconst DEFAULT_MODEL = 'sonic-3';\n/** Pipeline operates at 48kHz — matches Opus/WebRTC native rate, no resampling */\nconst DEFAULT_SAMPLE_RATE = 48000;\n/** Reconnect after idle timeout (Cartesia closes after 5 min idle) */\nconst RECONNECT_DELAY_MS = 1000;\n\nexport interface CartesiaTTSOptions {\n apiKey: string;\n /** Cartesia voice ID */\n voiceId: string;\n /** Model ID (default: 'sonic-3') */\n modelId?: string;\n /** Output sample rate in Hz (default: 16000) */\n sampleRate?: number;\n /** API version (default: '2024-06-10') */\n apiVersion?: string;\n /** Language code (default: 'en') */\n language?: string;\n /** Speech speed multiplier, 0.6-1.5 (default: 1.0). Sonic-3 only. */\n speed?: number;\n /** Emotion string (e.g. 'friendly', 'calm'). Sonic-3 only. */\n emotion?: string;\n}\n\n/** Per-context state for tracking an in-flight synthesis. */\ninterface ContextState {\n chunks: Buffer[];\n done: boolean;\n error: Error | null;\n wake: (() => void) | null;\n}\n\nexport class CartesiaTTS implements TTSPlugin {\n private readonly apiKey: string;\n private readonly voiceId: string;\n private readonly modelId: string;\n private readonly sampleRate: number;\n private readonly apiVersion: string;\n private readonly language?: string;\n private readonly speed: number | undefined;\n private readonly emotion: string | undefined;\n\n private ws: WebSocket | null = null;\n private _connected = false;\n private connectPromise: Promise<void> | null = null;\n /** Active contexts keyed by context_id */\n private contexts = new Map<string, ContextState>();\n private contextCounter = 0;\n\n constructor(options: CartesiaTTSOptions) {\n if (!options.apiKey) {\n throw new Error('CartesiaTTS requires an apiKey');\n }\n if (!options.voiceId) {\n throw new Error('CartesiaTTS requires a voiceId');\n }\n this.apiKey = options.apiKey;\n this.voiceId = options.voiceId;\n this.modelId = options.modelId ?? DEFAULT_MODEL;\n this.sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;\n this.apiVersion = options.apiVersion ?? DEFAULT_API_VERSION;\n this.language = options.language;\n this.speed = options.speed;\n this.emotion = options.emotion;\n }\n\n /** Pre-connect the WebSocket so first synthesize() doesn't pay connection cost. */\n async warmup(): Promise<void> {\n log.info('Warming up TTS connection...');\n const start = performance.now();\n try {\n await this.ensureConnection();\n log.info(`TTS warmup complete in ${(performance.now() - start).toFixed(0)}ms`);\n } catch (err) {\n log.warn('TTS warmup failed (non-fatal):', err);\n }\n }\n\n async *synthesize(text: string, signal?: AbortSignal): AsyncGenerator<Buffer> {\n log.debug(`Synthesizing: \"${text.slice(0, 60)}\"`);\n\n await this.ensureConnection();\n\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Cartesia WebSocket not connected');\n }\n\n const contextId = `ctx-${++this.contextCounter}-${Date.now()}`;\n const ctx: ContextState = { chunks: [], done: false, error: null, wake: null };\n this.contexts.set(contextId, ctx);\n\n // Build request\n const request: Record<string, unknown> = {\n model_id: this.modelId,\n transcript: text,\n voice: { mode: 'id', id: this.voiceId },\n output_format: {\n container: 'raw',\n encoding: 'pcm_s16le',\n sample_rate: this.sampleRate,\n },\n context_id: contextId,\n continue: false,\n };\n\n if (this.language) {\n request.language = this.language;\n }\n\n // Sonic-3 generation config\n if (this.speed !== undefined || this.emotion !== undefined) {\n const genConfig: Record<string, unknown> = {};\n if (this.speed !== undefined) genConfig.speed = this.speed;\n if (this.emotion !== undefined) genConfig.emotion = this.emotion;\n request.generation_config = genConfig;\n }\n\n // Handle abort — cancel the context on the server\n const onAbort = () => {\n ctx.done = true;\n ctx.wake?.();\n // Send cancel to server so it stops generating\n if (this.ws?.readyState === WebSocket.OPEN) {\n try {\n this.ws.send(JSON.stringify({ context_id: contextId, cancel: true }));\n } catch {\n // Ignore send errors during cancellation\n }\n }\n };\n signal?.addEventListener('abort', onAbort, { once: true });\n\n // Send synthesis request\n this.ws.send(JSON.stringify(request));\n\n // Yield audio chunks as they arrive\n try {\n while (true) {\n if (signal?.aborted) break;\n if (ctx.error) throw ctx.error;\n\n if (ctx.chunks.length > 0) {\n yield ctx.chunks.shift()!;\n continue;\n }\n\n if (ctx.done) break;\n\n // Wait for next chunk or done signal\n await new Promise<void>((resolve) => {\n ctx.wake = resolve;\n });\n ctx.wake = null;\n }\n\n // Drain remaining chunks\n while (ctx.chunks.length > 0) {\n yield ctx.chunks.shift()!;\n }\n } finally {\n signal?.removeEventListener('abort', onAbort);\n this.contexts.delete(contextId);\n }\n }\n\n /** Ensure the persistent WebSocket is connected. */\n private ensureConnection(): Promise<void> {\n if (this._connected && this.ws?.readyState === WebSocket.OPEN) {\n return Promise.resolve();\n }\n\n // Deduplicate concurrent connection attempts\n if (this.connectPromise) return this.connectPromise;\n\n this.connectPromise = new Promise<void>((resolve, reject) => {\n const url = `${CARTESIA_WS_BASE}?api_key=${this.apiKey}&cartesia_version=${this.apiVersion}`;\n log.debug('Connecting to Cartesia...');\n\n this.ws = new WebSocket(url);\n\n this.ws.on('open', () => {\n this._connected = true;\n this.connectPromise = null;\n log.info('Cartesia WebSocket connected');\n resolve();\n });\n\n this.ws.on('message', (data) => {\n try {\n const msg = JSON.parse(data.toString());\n this.handleMessage(msg);\n } catch (err) {\n log.error('Failed to parse Cartesia message:', err);\n }\n });\n\n this.ws.on('error', (err) => {\n const error = err instanceof Error ? err : new Error(String(err));\n log.error('Cartesia WebSocket error:', error);\n // Propagate error to all active contexts\n for (const ctx of this.contexts.values()) {\n ctx.error = error;\n ctx.wake?.();\n }\n this._connected = false;\n this.connectPromise = null;\n reject(error);\n });\n\n this.ws.on('close', (code, reason) => {\n log.debug(`Cartesia WebSocket closed: ${code} ${reason.toString()}`);\n this._connected = false;\n this.connectPromise = null;\n // Mark all active contexts as done\n for (const ctx of this.contexts.values()) {\n ctx.done = true;\n ctx.wake?.();\n }\n });\n });\n\n return this.connectPromise;\n }\n\n private handleMessage(msg: Record<string, unknown>): void {\n const contextId = msg.context_id as string | undefined;\n if (!contextId) return;\n\n const ctx = this.contexts.get(contextId);\n if (!ctx) return; // Stale context — already cleaned up\n\n const type = msg.type as string;\n\n if (type === 'chunk') {\n const b64 = msg.data as string;\n if (b64) {\n const pcm = Buffer.from(b64, 'base64');\n ctx.chunks.push(pcm);\n ctx.wake?.();\n }\n } else if (type === 'done') {\n log.debug(`Cartesia synthesis done for ${contextId} (${ctx.chunks.length} chunks pending)`);\n ctx.done = true;\n ctx.wake?.();\n } else if (type === 'error') {\n const errorMsg = msg.error as string ?? 'Unknown Cartesia error';\n log.error(`Cartesia error for ${contextId}: ${errorMsg}`);\n ctx.error = new Error(`Cartesia TTS error: ${errorMsg}`);\n ctx.wake?.();\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkBA,gBAAsB;;;AClBtB,oBAA6B;AAQtB,IAAe,gBAAf,cAAqC,2BAAkC;AAAA;AAAA,EAOnE,GAAG,OAAe,IAAoC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KAAK;AAAA,MACd,CAAC;AAED,WAAK,GAAG,GAAG,SAAS,CAAC,MAAM,WAAW;AACpC,QAAAA,KAAI,MAAM,8BAA8B,IAAI,IAAI,OAAO,SAAS,CAAC,EAAE;AACnE,aAAK,aAAa;AAClB,aAAK,iBAAiB;AAEtB,mBAAW,OAAO,KAAK,SAAS,OAAO,GAAG;AACxC,cAAI,OAAO;AACX,cAAI,OAAO;AAAA,QACb;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAED,WAAO,KAAK;AAAA,EACd;AAAA,EAEQ,cAAc,KAAoC;AACxD,UAAM,YAAY,IAAI;AACtB,QAAI,CAAC,UAAW;AAEhB,UAAM,MAAM,KAAK,SAAS,IAAI,SAAS;AACvC,QAAI,CAAC,IAAK;AAEV,UAAM,OAAO,IAAI;AAEjB,QAAI,SAAS,SAAS;AACpB,YAAM,MAAM,IAAI;AAChB,UAAI,KAAK;AACP,cAAM,MAAM,OAAO,KAAK,KAAK,QAAQ;AACrC,YAAI,OAAO,KAAK,GAAG;AACnB,YAAI,OAAO;AAAA,MACb;AAAA,IACF,WAAW,SAAS,QAAQ;AAC1B,MAAAA,KAAI,MAAM,+BAA+B,SAAS,KAAK,IAAI,OAAO,MAAM,kBAAkB;AAC1F,UAAI,OAAO;AACX,UAAI,OAAO;AAAA,IACb,WAAW,SAAS,SAAS;AAC3B,YAAM,WAAW,IAAI,SAAmB;AACxC,MAAAA,KAAI,MAAM,sBAAsB,SAAS,KAAK,QAAQ,EAAE;AACxD,UAAI,QAAQ,IAAI,MAAM,uBAAuB,QAAQ,EAAE;AACvD,UAAI,OAAO;AAAA,IACb;AAAA,EACF;AACF;","names":["WebSocket","log","import_ws","log","log","WebSocket"]}
|
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseSTTStream
|
|
3
|
+
} from "../chunk-6OWWB2X7.mjs";
|
|
4
|
+
import {
|
|
5
|
+
createLogger
|
|
6
|
+
} from "../chunk-BN7PIFNJ.mjs";
|
|
7
|
+
|
|
8
|
+
// src/providers/deepgram-stt.ts
|
|
9
|
+
import WebSocket from "ws";
|
|
10
|
+
var log = createLogger("DeepgramSTT");
|
|
11
|
+
var DEEPGRAM_WS_URL = "wss://api.deepgram.com/v1/listen";
|
|
12
|
+
var KEEPALIVE_INTERVAL_MS = 5e3;
|
|
13
|
+
var DeepgramSTT = class {
|
|
14
|
+
options;
|
|
15
|
+
constructor(options) {
|
|
16
|
+
if (!options.apiKey) {
|
|
17
|
+
throw new Error("DeepgramSTT requires an apiKey");
|
|
18
|
+
}
|
|
19
|
+
this.options = options;
|
|
20
|
+
}
|
|
21
|
+
createStream(options) {
|
|
22
|
+
const language = options?.language ?? this.options.language ?? "en";
|
|
23
|
+
return new DeepgramSTTStream(this.options, language);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var DeepgramSTTStream = class extends BaseSTTStream {
|
|
27
|
+
ws = null;
|
|
28
|
+
apiKey;
|
|
29
|
+
wsUrl;
|
|
30
|
+
_ready = false;
|
|
31
|
+
_closed = false;
|
|
32
|
+
pendingAudio = [];
|
|
33
|
+
keepAliveTimer = null;
|
|
34
|
+
lastAudioSentAt = 0;
|
|
35
|
+
/** Buffer of is_final=true transcripts for the current utterance */
|
|
36
|
+
utteranceBuffer = [];
|
|
37
|
+
/** Timestamp of the last non-empty interim result (approximates end of speech) */
|
|
38
|
+
lastInterimAt = 0;
|
|
39
|
+
constructor(options, language) {
|
|
40
|
+
super();
|
|
41
|
+
this.apiKey = options.apiKey;
|
|
42
|
+
this.wsUrl = buildWsUrl(options, language);
|
|
43
|
+
this.connect();
|
|
44
|
+
}
|
|
45
|
+
sendAudio(pcm16) {
|
|
46
|
+
if (this._closed) return;
|
|
47
|
+
if (!this._ready) {
|
|
48
|
+
this.pendingAudio.push(pcm16);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
52
|
+
this.ws.send(pcm16);
|
|
53
|
+
this.lastAudioSentAt = performance.now();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async close() {
|
|
57
|
+
if (this._closed) return;
|
|
58
|
+
this._closed = true;
|
|
59
|
+
this._ready = false;
|
|
60
|
+
this.pendingAudio = [];
|
|
61
|
+
this.stopKeepAlive();
|
|
62
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
63
|
+
try {
|
|
64
|
+
this.ws.send(JSON.stringify({ type: "CloseStream" }));
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (this.ws) {
|
|
69
|
+
this.ws.close();
|
|
70
|
+
this.ws = null;
|
|
71
|
+
}
|
|
72
|
+
log.debug("DeepgramSTT stream closed");
|
|
73
|
+
}
|
|
74
|
+
connect() {
|
|
75
|
+
log.debug(`Connecting to Deepgram: ${this.wsUrl.replace(/token=[^&]+/, "token=***")}`);
|
|
76
|
+
this.ws = new WebSocket(this.wsUrl, {
|
|
77
|
+
headers: {
|
|
78
|
+
Authorization: `Token ${this.apiKey}`
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
this.ws.on("open", () => {
|
|
82
|
+
log.info("Deepgram WebSocket connected");
|
|
83
|
+
this._ready = true;
|
|
84
|
+
for (const buf of this.pendingAudio) {
|
|
85
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
86
|
+
this.ws.send(buf);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
this.pendingAudio = [];
|
|
90
|
+
this.startKeepAlive();
|
|
91
|
+
});
|
|
92
|
+
this.ws.on("message", (data) => {
|
|
93
|
+
try {
|
|
94
|
+
const msg = JSON.parse(data.toString());
|
|
95
|
+
this.handleMessage(msg);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
log.error("Failed to parse Deepgram message:", err);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
this.ws.on("error", (err) => {
|
|
101
|
+
log.error("Deepgram WebSocket error:", err);
|
|
102
|
+
this.emit("error", err instanceof Error ? err : new Error(String(err)));
|
|
103
|
+
});
|
|
104
|
+
this.ws.on("close", (code, reason) => {
|
|
105
|
+
log.debug(`Deepgram WebSocket closed: ${code} ${reason.toString()}`);
|
|
106
|
+
this._ready = false;
|
|
107
|
+
this.stopKeepAlive();
|
|
108
|
+
if (!this._closed) {
|
|
109
|
+
log.info("Deepgram connection lost, reconnecting in 1s...");
|
|
110
|
+
setTimeout(() => {
|
|
111
|
+
if (!this._closed) this.connect();
|
|
112
|
+
}, 1e3);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
handleMessage(msg) {
|
|
117
|
+
const type = msg.type;
|
|
118
|
+
if (type === "Results") {
|
|
119
|
+
this.handleResults(msg);
|
|
120
|
+
} else if (type === "UtteranceEnd") {
|
|
121
|
+
this.flushUtterance();
|
|
122
|
+
} else if (type === "Metadata") {
|
|
123
|
+
log.debug("Deepgram session metadata received");
|
|
124
|
+
} else if (type === "SpeechStarted") {
|
|
125
|
+
log.debug("Speech started detected");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
handleResults(msg) {
|
|
129
|
+
const channel = msg.channel;
|
|
130
|
+
const transcript = channel?.alternatives?.[0]?.transcript ?? "";
|
|
131
|
+
const confidence = channel?.alternatives?.[0]?.confidence;
|
|
132
|
+
const isFinal = msg.is_final ?? false;
|
|
133
|
+
const speechFinal = msg.speech_final ?? false;
|
|
134
|
+
if (!transcript) return;
|
|
135
|
+
if (!isFinal) {
|
|
136
|
+
this.lastInterimAt = performance.now();
|
|
137
|
+
const fullInterim = this.utteranceBuffer.length > 0 ? this.utteranceBuffer.join(" ") + " " + transcript : transcript;
|
|
138
|
+
this.emit("transcription", {
|
|
139
|
+
text: fullInterim,
|
|
140
|
+
isFinal: false,
|
|
141
|
+
confidence: confidence ?? void 0
|
|
142
|
+
});
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
this.utteranceBuffer.push(transcript);
|
|
146
|
+
if (speechFinal) {
|
|
147
|
+
this.flushUtterance();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** Emit the buffered utterance as a single final transcription result. */
|
|
151
|
+
flushUtterance() {
|
|
152
|
+
if (this.utteranceBuffer.length === 0) return;
|
|
153
|
+
const now = performance.now();
|
|
154
|
+
const fullText = this.utteranceBuffer.join(" ");
|
|
155
|
+
this.utteranceBuffer = [];
|
|
156
|
+
const sttDuration = this.lastInterimAt > 0 ? now - this.lastInterimAt : void 0;
|
|
157
|
+
if (sttDuration !== void 0) {
|
|
158
|
+
log.info(`stt_final: ${sttDuration.toFixed(0)}ms "${fullText.slice(0, 50)}"`);
|
|
159
|
+
}
|
|
160
|
+
this.lastInterimAt = 0;
|
|
161
|
+
this.emit("transcription", {
|
|
162
|
+
text: fullText,
|
|
163
|
+
isFinal: true,
|
|
164
|
+
sttDuration
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
startKeepAlive() {
|
|
168
|
+
this.stopKeepAlive();
|
|
169
|
+
this.keepAliveTimer = setInterval(() => {
|
|
170
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
171
|
+
this.ws.send(JSON.stringify({ type: "KeepAlive" }));
|
|
172
|
+
}
|
|
173
|
+
}, KEEPALIVE_INTERVAL_MS);
|
|
174
|
+
}
|
|
175
|
+
stopKeepAlive() {
|
|
176
|
+
if (this.keepAliveTimer) {
|
|
177
|
+
clearInterval(this.keepAliveTimer);
|
|
178
|
+
this.keepAliveTimer = null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
function buildWsUrl(options, language) {
|
|
183
|
+
const params = new URLSearchParams();
|
|
184
|
+
params.set("model", options.model ?? "nova-3");
|
|
185
|
+
params.set("language", language);
|
|
186
|
+
params.set("encoding", "linear16");
|
|
187
|
+
params.set("sample_rate", "16000");
|
|
188
|
+
params.set("channels", "1");
|
|
189
|
+
params.set("interim_results", String(options.interimResults ?? true));
|
|
190
|
+
params.set("punctuate", String(options.punctuate ?? true));
|
|
191
|
+
if (options.endpointing === false) {
|
|
192
|
+
params.set("endpointing", "false");
|
|
193
|
+
} else {
|
|
194
|
+
params.set("endpointing", String(options.endpointing ?? 300));
|
|
195
|
+
}
|
|
196
|
+
if (options.smartFormat) {
|
|
197
|
+
params.set("smart_format", "true");
|
|
198
|
+
}
|
|
199
|
+
if (options.utteranceEndMs !== void 0) {
|
|
200
|
+
params.set("utterance_end_ms", String(options.utteranceEndMs));
|
|
201
|
+
} else if (options.interimResults !== false) {
|
|
202
|
+
params.set("utterance_end_ms", "1000");
|
|
203
|
+
}
|
|
204
|
+
if (options.keywords?.length) {
|
|
205
|
+
for (const kw of options.keywords) {
|
|
206
|
+
params.append("keywords", kw);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return `${DEEPGRAM_WS_URL}?${params.toString()}`;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/providers/openrouter-llm.ts
|
|
213
|
+
var log2 = createLogger("OpenRouterLLM");
|
|
214
|
+
var OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions";
|
|
215
|
+
var OpenRouterLLM = class {
|
|
216
|
+
apiKey;
|
|
217
|
+
model;
|
|
218
|
+
maxTokens;
|
|
219
|
+
temperature;
|
|
220
|
+
provider;
|
|
221
|
+
constructor(options) {
|
|
222
|
+
if (!options.apiKey) {
|
|
223
|
+
throw new Error("OpenRouterLLM requires an apiKey");
|
|
224
|
+
}
|
|
225
|
+
this.apiKey = options.apiKey;
|
|
226
|
+
this.model = options.model;
|
|
227
|
+
this.maxTokens = options.maxTokens ?? 512;
|
|
228
|
+
this.temperature = options.temperature ?? 0.7;
|
|
229
|
+
if (options.providerRouting) {
|
|
230
|
+
this.provider = {
|
|
231
|
+
sort: options.providerRouting.sort,
|
|
232
|
+
order: options.providerRouting.order,
|
|
233
|
+
allow_fallbacks: options.providerRouting.allowFallbacks
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Warm up the LLM by sending the system prompt and a short message.
|
|
239
|
+
* Primes the HTTP/TLS connection and model loading on the provider side.
|
|
240
|
+
*/
|
|
241
|
+
async warmup(systemPrompt) {
|
|
242
|
+
log2.info("Warming up LLM connection...");
|
|
243
|
+
const start = performance.now();
|
|
244
|
+
const messages = [
|
|
245
|
+
{ role: "system", content: systemPrompt },
|
|
246
|
+
{ role: "user", content: "Hello" }
|
|
247
|
+
];
|
|
248
|
+
try {
|
|
249
|
+
const gen = this.chat(messages);
|
|
250
|
+
for await (const chunk of gen) {
|
|
251
|
+
if (chunk.type === "done") break;
|
|
252
|
+
}
|
|
253
|
+
log2.info(`LLM warmup complete in ${(performance.now() - start).toFixed(0)}ms`);
|
|
254
|
+
} catch (err) {
|
|
255
|
+
log2.warn("LLM warmup failed (non-fatal):", err);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async *chat(messages, signal) {
|
|
259
|
+
const body = {
|
|
260
|
+
model: this.model,
|
|
261
|
+
messages,
|
|
262
|
+
max_tokens: this.maxTokens,
|
|
263
|
+
temperature: this.temperature,
|
|
264
|
+
stream: true
|
|
265
|
+
};
|
|
266
|
+
if (this.provider) {
|
|
267
|
+
body.provider = this.provider;
|
|
268
|
+
}
|
|
269
|
+
log2.debug(`LLM request: model=${this.model}, messages=${messages.length}`);
|
|
270
|
+
const response = await fetch(OPENROUTER_URL, {
|
|
271
|
+
method: "POST",
|
|
272
|
+
headers: {
|
|
273
|
+
"Content-Type": "application/json",
|
|
274
|
+
"Authorization": `Bearer ${this.apiKey}`
|
|
275
|
+
},
|
|
276
|
+
body: JSON.stringify(body),
|
|
277
|
+
signal
|
|
278
|
+
});
|
|
279
|
+
if (!response.ok) {
|
|
280
|
+
const errorText = await response.text();
|
|
281
|
+
throw new Error(`OpenRouter API error ${response.status}: ${errorText}`);
|
|
282
|
+
}
|
|
283
|
+
if (!response.body) {
|
|
284
|
+
throw new Error("OpenRouter response has no body");
|
|
285
|
+
}
|
|
286
|
+
const reader = response.body.getReader();
|
|
287
|
+
const decoder = new TextDecoder();
|
|
288
|
+
let buffer = "";
|
|
289
|
+
try {
|
|
290
|
+
while (true) {
|
|
291
|
+
if (signal?.aborted) break;
|
|
292
|
+
const { done, value } = await reader.read();
|
|
293
|
+
if (done) break;
|
|
294
|
+
buffer += decoder.decode(value, { stream: true });
|
|
295
|
+
const lines = buffer.split("\n");
|
|
296
|
+
buffer = lines.pop() ?? "";
|
|
297
|
+
for (const line of lines) {
|
|
298
|
+
const trimmed = line.trim();
|
|
299
|
+
if (!trimmed || !trimmed.startsWith("data: ")) continue;
|
|
300
|
+
const data = trimmed.slice(6);
|
|
301
|
+
if (data === "[DONE]") {
|
|
302
|
+
yield { type: "done" };
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
try {
|
|
306
|
+
const parsed = JSON.parse(data);
|
|
307
|
+
const choice = parsed.choices?.[0];
|
|
308
|
+
if (!choice) continue;
|
|
309
|
+
const delta = choice.delta;
|
|
310
|
+
if (delta?.content) {
|
|
311
|
+
yield { type: "token", token: delta.content };
|
|
312
|
+
}
|
|
313
|
+
if (parsed.usage) {
|
|
314
|
+
yield {
|
|
315
|
+
type: "done",
|
|
316
|
+
usage: {
|
|
317
|
+
promptTokens: parsed.usage.prompt_tokens,
|
|
318
|
+
completionTokens: parsed.usage.completion_tokens
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
} catch {
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
} finally {
|
|
328
|
+
reader.releaseLock();
|
|
329
|
+
}
|
|
330
|
+
yield { type: "done" };
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// src/providers/cartesia-tts.ts
|
|
335
|
+
import WebSocket2 from "ws";
|
|
336
|
+
var log3 = createLogger("CartesiaTTS");
|
|
337
|
+
var CARTESIA_WS_BASE = "wss://api.cartesia.ai/tts/websocket";
|
|
338
|
+
var DEFAULT_API_VERSION = "2024-06-10";
|
|
339
|
+
var DEFAULT_MODEL = "sonic-3";
|
|
340
|
+
var DEFAULT_SAMPLE_RATE = 48e3;
|
|
341
|
+
var CartesiaTTS = class {
|
|
342
|
+
apiKey;
|
|
343
|
+
voiceId;
|
|
344
|
+
modelId;
|
|
345
|
+
sampleRate;
|
|
346
|
+
apiVersion;
|
|
347
|
+
language;
|
|
348
|
+
speed;
|
|
349
|
+
emotion;
|
|
350
|
+
ws = null;
|
|
351
|
+
_connected = false;
|
|
352
|
+
connectPromise = null;
|
|
353
|
+
/** Active contexts keyed by context_id */
|
|
354
|
+
contexts = /* @__PURE__ */ new Map();
|
|
355
|
+
contextCounter = 0;
|
|
356
|
+
constructor(options) {
|
|
357
|
+
if (!options.apiKey) {
|
|
358
|
+
throw new Error("CartesiaTTS requires an apiKey");
|
|
359
|
+
}
|
|
360
|
+
if (!options.voiceId) {
|
|
361
|
+
throw new Error("CartesiaTTS requires a voiceId");
|
|
362
|
+
}
|
|
363
|
+
this.apiKey = options.apiKey;
|
|
364
|
+
this.voiceId = options.voiceId;
|
|
365
|
+
this.modelId = options.modelId ?? DEFAULT_MODEL;
|
|
366
|
+
this.sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
367
|
+
this.apiVersion = options.apiVersion ?? DEFAULT_API_VERSION;
|
|
368
|
+
this.language = options.language;
|
|
369
|
+
this.speed = options.speed;
|
|
370
|
+
this.emotion = options.emotion;
|
|
371
|
+
}
|
|
372
|
+
/** Pre-connect the WebSocket so first synthesize() doesn't pay connection cost. */
|
|
373
|
+
async warmup() {
|
|
374
|
+
log3.info("Warming up TTS connection...");
|
|
375
|
+
const start = performance.now();
|
|
376
|
+
try {
|
|
377
|
+
await this.ensureConnection();
|
|
378
|
+
log3.info(`TTS warmup complete in ${(performance.now() - start).toFixed(0)}ms`);
|
|
379
|
+
} catch (err) {
|
|
380
|
+
log3.warn("TTS warmup failed (non-fatal):", err);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
async *synthesize(text, signal) {
|
|
384
|
+
log3.debug(`Synthesizing: "${text.slice(0, 60)}"`);
|
|
385
|
+
await this.ensureConnection();
|
|
386
|
+
if (!this.ws || this.ws.readyState !== WebSocket2.OPEN) {
|
|
387
|
+
throw new Error("Cartesia WebSocket not connected");
|
|
388
|
+
}
|
|
389
|
+
const contextId = `ctx-${++this.contextCounter}-${Date.now()}`;
|
|
390
|
+
const ctx = { chunks: [], done: false, error: null, wake: null };
|
|
391
|
+
this.contexts.set(contextId, ctx);
|
|
392
|
+
const request = {
|
|
393
|
+
model_id: this.modelId,
|
|
394
|
+
transcript: text,
|
|
395
|
+
voice: { mode: "id", id: this.voiceId },
|
|
396
|
+
output_format: {
|
|
397
|
+
container: "raw",
|
|
398
|
+
encoding: "pcm_s16le",
|
|
399
|
+
sample_rate: this.sampleRate
|
|
400
|
+
},
|
|
401
|
+
context_id: contextId,
|
|
402
|
+
continue: false
|
|
403
|
+
};
|
|
404
|
+
if (this.language) {
|
|
405
|
+
request.language = this.language;
|
|
406
|
+
}
|
|
407
|
+
if (this.speed !== void 0 || this.emotion !== void 0) {
|
|
408
|
+
const genConfig = {};
|
|
409
|
+
if (this.speed !== void 0) genConfig.speed = this.speed;
|
|
410
|
+
if (this.emotion !== void 0) genConfig.emotion = this.emotion;
|
|
411
|
+
request.generation_config = genConfig;
|
|
412
|
+
}
|
|
413
|
+
const onAbort = () => {
|
|
414
|
+
ctx.done = true;
|
|
415
|
+
ctx.wake?.();
|
|
416
|
+
if (this.ws?.readyState === WebSocket2.OPEN) {
|
|
417
|
+
try {
|
|
418
|
+
this.ws.send(JSON.stringify({ context_id: contextId, cancel: true }));
|
|
419
|
+
} catch {
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
424
|
+
this.ws.send(JSON.stringify(request));
|
|
425
|
+
try {
|
|
426
|
+
while (true) {
|
|
427
|
+
if (signal?.aborted) break;
|
|
428
|
+
if (ctx.error) throw ctx.error;
|
|
429
|
+
if (ctx.chunks.length > 0) {
|
|
430
|
+
yield ctx.chunks.shift();
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
if (ctx.done) break;
|
|
434
|
+
await new Promise((resolve) => {
|
|
435
|
+
ctx.wake = resolve;
|
|
436
|
+
});
|
|
437
|
+
ctx.wake = null;
|
|
438
|
+
}
|
|
439
|
+
while (ctx.chunks.length > 0) {
|
|
440
|
+
yield ctx.chunks.shift();
|
|
441
|
+
}
|
|
442
|
+
} finally {
|
|
443
|
+
signal?.removeEventListener("abort", onAbort);
|
|
444
|
+
this.contexts.delete(contextId);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
/** Ensure the persistent WebSocket is connected. */
|
|
448
|
+
ensureConnection() {
|
|
449
|
+
if (this._connected && this.ws?.readyState === WebSocket2.OPEN) {
|
|
450
|
+
return Promise.resolve();
|
|
451
|
+
}
|
|
452
|
+
if (this.connectPromise) return this.connectPromise;
|
|
453
|
+
this.connectPromise = new Promise((resolve, reject) => {
|
|
454
|
+
const url = `${CARTESIA_WS_BASE}?api_key=${this.apiKey}&cartesia_version=${this.apiVersion}`;
|
|
455
|
+
log3.debug("Connecting to Cartesia...");
|
|
456
|
+
this.ws = new WebSocket2(url);
|
|
457
|
+
this.ws.on("open", () => {
|
|
458
|
+
this._connected = true;
|
|
459
|
+
this.connectPromise = null;
|
|
460
|
+
log3.info("Cartesia WebSocket connected");
|
|
461
|
+
resolve();
|
|
462
|
+
});
|
|
463
|
+
this.ws.on("message", (data) => {
|
|
464
|
+
try {
|
|
465
|
+
const msg = JSON.parse(data.toString());
|
|
466
|
+
this.handleMessage(msg);
|
|
467
|
+
} catch (err) {
|
|
468
|
+
log3.error("Failed to parse Cartesia message:", err);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
this.ws.on("error", (err) => {
|
|
472
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
473
|
+
log3.error("Cartesia WebSocket error:", error);
|
|
474
|
+
for (const ctx of this.contexts.values()) {
|
|
475
|
+
ctx.error = error;
|
|
476
|
+
ctx.wake?.();
|
|
477
|
+
}
|
|
478
|
+
this._connected = false;
|
|
479
|
+
this.connectPromise = null;
|
|
480
|
+
reject(error);
|
|
481
|
+
});
|
|
482
|
+
this.ws.on("close", (code, reason) => {
|
|
483
|
+
log3.debug(`Cartesia WebSocket closed: ${code} ${reason.toString()}`);
|
|
484
|
+
this._connected = false;
|
|
485
|
+
this.connectPromise = null;
|
|
486
|
+
for (const ctx of this.contexts.values()) {
|
|
487
|
+
ctx.done = true;
|
|
488
|
+
ctx.wake?.();
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
return this.connectPromise;
|
|
493
|
+
}
|
|
494
|
+
handleMessage(msg) {
|
|
495
|
+
const contextId = msg.context_id;
|
|
496
|
+
if (!contextId) return;
|
|
497
|
+
const ctx = this.contexts.get(contextId);
|
|
498
|
+
if (!ctx) return;
|
|
499
|
+
const type = msg.type;
|
|
500
|
+
if (type === "chunk") {
|
|
501
|
+
const b64 = msg.data;
|
|
502
|
+
if (b64) {
|
|
503
|
+
const pcm = Buffer.from(b64, "base64");
|
|
504
|
+
ctx.chunks.push(pcm);
|
|
505
|
+
ctx.wake?.();
|
|
506
|
+
}
|
|
507
|
+
} else if (type === "done") {
|
|
508
|
+
log3.debug(`Cartesia synthesis done for ${contextId} (${ctx.chunks.length} chunks pending)`);
|
|
509
|
+
ctx.done = true;
|
|
510
|
+
ctx.wake?.();
|
|
511
|
+
} else if (type === "error") {
|
|
512
|
+
const errorMsg = msg.error ?? "Unknown Cartesia error";
|
|
513
|
+
log3.error(`Cartesia error for ${contextId}: ${errorMsg}`);
|
|
514
|
+
ctx.error = new Error(`Cartesia TTS error: ${errorMsg}`);
|
|
515
|
+
ctx.wake?.();
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
export {
|
|
520
|
+
CartesiaTTS,
|
|
521
|
+
DeepgramSTT,
|
|
522
|
+
OpenRouterLLM
|
|
523
|
+
};
|
|
524
|
+
//# sourceMappingURL=index.mjs.map
|