@micdrop/server 2.6.0 → 2.8.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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/agent/Agent.ts","../src/agent/tools.ts","../src/Logger.ts","../src/agent/FallbackAgent.ts","../src/agent/MockAgent.ts","../src/audio/Pcm16Resampler.ts","../src/audio/pcm16.ts","../src/errors.ts","../src/MicdropServer.ts","../src/types.ts","../src/recorder/MicdropRecorder.ts","../src/stt/STT.ts","../src/stt/MockSTT.ts","../src/stt/FallbackSTT.ts","../src/tts/FallbackTTS.ts","../src/tts/TTS.ts","../src/tts/MockTTS.ts","../src/tts/SentenceSplitter.ts","../src/tts/SentenceTTS.ts","../src/waitForParams.ts"],"sourcesContent":["import { EventEmitter } from 'eventemitter3'\nimport { PassThrough, Readable, Writable } from 'stream'\nimport type { z } from 'zod'\nimport { Logger } from '../Logger'\nimport {\n MicdropAnswerMetadata,\n MicdropConversation,\n MicdropConversationItem,\n MicdropConversationMessage,\n MicdropConversationToolCall,\n MicdropConversationToolResult,\n MicdropToolCall,\n} from '../types'\nimport {\n AUTO_END_CALL_PROMPT,\n AUTO_END_CALL_TOOL_NAME,\n AUTO_IGNORE_USER_NOISE_PROMPT,\n AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n AUTO_SEMANTIC_TURN_PROMPT,\n AUTO_SEMANTIC_TURN_TOOL_NAME,\n Tool,\n} from './tools'\n\nexport interface AgentOptions {\n systemPrompt: string\n\n // Enable auto ending of the call when user asks to end the call\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoEndCall?: boolean | string\n\n // Enable detection of an incomplete sentence, and skip the answer (assistant waits)\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoSemanticTurn?: boolean | string\n\n // Ignore of the last user message when it's meaningless\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoIgnoreUserNoise?: boolean | string\n\n // Extract a value from the answer\n // Value must be at the end of the answer, in JSON or between tags\n extract?: ExtractJsonOptions | ExtractTagOptions\n\n // Function called before any answer is generated\n // Return true to skip generation\n onBeforeAnswer?: (\n this: Agent,\n stream: Writable\n ) => void | boolean | Promise<boolean>\n}\n\nexport interface AgentEvents {\n Message: [MicdropConversationItem]\n CancelLastUserMessage: []\n SkipAnswer: []\n EndCall: []\n ToolCall: [MicdropToolCall]\n // Emitted when the agent gives up generating an answer (e.g. after exhausting\n // its retries). Used by FallbackAgent to switch to the next agent.\n Failed: []\n}\n\nexport interface ExtractOptions {\n callback?: (value: string) => void\n saveInMetadata?: boolean\n}\n\nexport interface ExtractJsonOptions extends ExtractOptions {\n json: true\n callback?: (value: any) => void\n}\n\nexport interface ExtractTagOptions extends ExtractOptions {\n startTag: string\n endTag: string\n}\n\nexport abstract class Agent<\n Options extends AgentOptions = AgentOptions,\n> extends EventEmitter<AgentEvents> {\n public logger?: Logger\n public conversation: MicdropConversation\n public tools: Tool[]\n\n protected answerCount = 0\n protected answering = false\n\n constructor(protected options: Options) {\n super()\n this.conversation = [{ role: 'system', content: options.systemPrompt }]\n this.tools = this.getDefaultTools()\n }\n\n protected abstract generateAnswer(stream: PassThrough): Promise<void>\n abstract cancel(): void\n\n answer(): Readable {\n this.log('Start answering')\n const answerCount = ++this.answerCount\n const stream = new PassThrough()\n this.answering = true\n\n Promise.resolve()\n // Call hook onBeforeAnswer\n .then(() => this.options.onBeforeAnswer?.bind(this)(stream))\n // Generate answer (if not skipped)\n .then((skip) => {\n if (skip) return\n return this.generateAnswer(stream)\n })\n // End stream\n .finally(() => {\n if (stream.writable) {\n stream.end()\n }\n if (answerCount === this.answerCount) {\n this.answering = false\n }\n })\n\n return stream\n }\n\n addUserMessage(text: string, metadata?: MicdropAnswerMetadata) {\n this.addMessage('user', text, metadata)\n }\n\n addAssistantMessage(text: string, metadata?: MicdropAnswerMetadata) {\n this.addMessage('assistant', text, metadata)\n }\n\n addTool<Schema extends z.ZodObject>(tool: Tool<Schema>) {\n this.tools.push(tool)\n }\n\n removeTool(name: string) {\n const index = this.tools.findIndex((tool) => tool.name === name)\n if (index !== -1) {\n this.tools.splice(index, 1)\n }\n }\n\n getTool(name: string): Tool | undefined {\n return this.tools.find((tool) => tool.name === name)\n }\n\n addMessage(\n role: 'user' | 'assistant' | 'system',\n text: string,\n metadata?: MicdropAnswerMetadata\n ) {\n // A turn can carry no text at all, typically when the LLM answered with a\n // tool call only. Keeping it would send an empty message back to the LLM on\n // the next turn, and emit a Message event that consumers store as an empty\n // exchange in their transcripts.\n if (text.trim() === '') {\n this.log(`Skipping empty ${role} message`)\n return\n }\n\n this.log(`Adding ${role} message to conversation: ${text}`)\n const message: MicdropConversationMessage = {\n role,\n content: text,\n metadata,\n }\n this.conversation.push(message)\n this.emit('Message', message)\n }\n\n addToolMessage(\n message: MicdropConversationToolCall | MicdropConversationToolResult\n ) {\n this.log('Adding tool message:', message)\n this.conversation.push(message)\n this.emit('Message', message)\n }\n\n protected endCall() {\n this.log('Ending call')\n this.emit('EndCall')\n }\n\n protected cancelLastUserMessage() {\n this.log('Cancelling last user message')\n const lastMessageIndex = this.conversation.findLastIndex(\n (message) => message.role === 'user'\n )\n if (lastMessageIndex !== -1) {\n this.conversation.splice(lastMessageIndex, 1)\n }\n this.emit('CancelLastUserMessage')\n }\n\n protected skipAnswer() {\n this.log('Skipping answer')\n this.emit('SkipAnswer')\n }\n\n protected getDefaultTools() {\n const tools: Tool[] = []\n if (this.options.autoEndCall) {\n tools.push({\n name: AUTO_END_CALL_TOOL_NAME,\n description:\n typeof this.options.autoEndCall === 'string'\n ? this.options.autoEndCall\n : AUTO_END_CALL_PROMPT,\n execute: (_input, agent) => agent.endCall(),\n })\n }\n if (this.options.autoSemanticTurn) {\n tools.push({\n name: AUTO_SEMANTIC_TURN_TOOL_NAME,\n description:\n typeof this.options.autoSemanticTurn === 'string'\n ? this.options.autoSemanticTurn\n : AUTO_SEMANTIC_TURN_PROMPT,\n skipAnswer: true,\n execute: (_input, agent) => agent.skipAnswer(),\n })\n }\n if (this.options.autoIgnoreUserNoise) {\n tools.push({\n name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n description:\n typeof this.options.autoIgnoreUserNoise === 'string'\n ? this.options.autoIgnoreUserNoise\n : AUTO_IGNORE_USER_NOISE_PROMPT,\n skipAnswer: true,\n execute: (_input, agent) => agent.cancelLastUserMessage(),\n })\n }\n return tools\n }\n\n protected async executeTool(toolCall: MicdropConversationToolCall) {\n try {\n const tool = this.getTool(toolCall.toolName)\n if (!tool) {\n throw new Error(`Tool not found \"${toolCall.toolName}\"`)\n }\n\n this.log('Executing tool:', toolCall.toolName, toolCall.parameters)\n\n // Save tool call in conversation\n this.addToolMessage(toolCall)\n\n const parameters = JSON.parse(toolCall.parameters)\n const output = tool.execute ? await tool.execute(parameters, this) : {}\n\n // Save tool result in conversation\n this.addToolMessage({\n role: 'tool_result',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: JSON.stringify(output ?? null),\n })\n\n // Emit output\n if (tool.emitOutput) {\n this.emit('ToolCall', {\n name: toolCall.toolName,\n parameters,\n output,\n })\n }\n\n return {\n output,\n skipAnswer: tool.skipAnswer,\n }\n } catch (error: any) {\n console.error('[OpenaiAgent] Error executing tool:', error)\n return {\n output: {\n error: error.message,\n },\n }\n }\n }\n\n protected getExtractOptions(): ExtractTagOptions | undefined {\n const extract = this.options.extract\n if (!extract) return undefined\n if ('json' in extract && extract.json) {\n return { ...extract, startTag: '{', endTag: '}' }\n }\n if ('startTag' in extract && 'endTag' in extract) {\n return extract\n }\n return undefined\n }\n\n public extract(message: string) {\n const extractOptions = this.getExtractOptions()\n let metadata: MicdropAnswerMetadata | undefined = undefined\n\n // Extract value?\n if (extractOptions) {\n const startTagIndex = message.indexOf(extractOptions.startTag)\n if (startTagIndex !== -1) {\n // Find end tag\n let endTagIndex = message.lastIndexOf(extractOptions.endTag)\n if (endTagIndex === -1) endTagIndex = message.length + 1\n else endTagIndex += extractOptions.endTag.length\n const extractedText = message.slice(startTagIndex, endTagIndex).trim()\n\n // Parse extracted value\n try {\n const extractedValue =\n 'json' in extractOptions && extractOptions.json\n ? JSON.parse(extractedText)\n : extractedText\n\n // Call callback\n if (extractOptions.callback) {\n extractOptions.callback(extractedValue)\n }\n\n // Save in metadata\n if (extractOptions.saveInMetadata) {\n metadata = { extracted: extractedValue }\n }\n } catch (error) {\n console.error(\n `[OpenaiAgent] Error parsing extracted value (${extractedText}):`,\n error\n )\n }\n\n // Remove extracted value from message\n message = message.slice(0, startTagIndex).trimEnd()\n }\n }\n return { message, metadata }\n }\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n destroy() {\n this.log('Destroyed')\n this.removeAllListeners()\n this.cancel()\n }\n}\n","import type { z } from 'zod'\nimport type { Agent } from './Agent'\n\nexport interface Tool<Schema extends z.ZodObject = z.ZodObject> {\n name: string\n description: string\n inputSchema?: Schema\n // The executing agent is passed as context so tools stay portable (no binding\n // to a specific agent instance), which lets them be shared between agents.\n execute?: (input: z.infer<Schema>, agent: Agent) => any | Promise<any>\n skipAnswer?: boolean\n emitOutput?: boolean\n}\n\nexport const AUTO_END_CALL_TOOL_NAME = 'end_call'\nexport const AUTO_END_CALL_PROMPT =\n 'Call this tool only if user asks to end the call'\n\nexport const AUTO_SEMANTIC_TURN_TOOL_NAME = 'semantic_turn'\nexport const AUTO_SEMANTIC_TURN_PROMPT =\n 'Call this tool only if last user message is obviously an incomplete sentence that you need to wait for the end before answering'\n\nexport const AUTO_IGNORE_USER_NOISE_TOOL_NAME = 'ignore_user_noise'\nexport const AUTO_IGNORE_USER_NOISE_PROMPT =\n 'Call this tool only if last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: \"Uh\", \"Ahem\", \"Hmm\", \"Ah\") but doesn\\'t carry any clear meaning like agreeing, refusing, or commanding'\n","export class Logger {\n constructor(public name: string) {}\n\n log(...message: any[]) {\n const time = process.uptime().toFixed(3)\n console.log(`[${this.name} ${time}]`, ...message)\n }\n}\n","import { PassThrough } from 'stream'\nimport { Logger } from '../Logger'\nimport { MicdropConversationItem, MicdropToolCall } from '../types'\nimport { Agent } from './Agent'\n\nexport interface FallbackAgentOptions {\n factories: Array<() => Agent>\n}\n\nexport class FallbackAgent extends Agent {\n private agent: Agent | null = null\n private agentIndex = -1 // Start at -1 because we need to increment it before using it\n\n constructor(private readonly fallbackOptions: FallbackAgentOptions) {\n super({ systemPrompt: '' })\n if (this.fallbackOptions.factories.length === 0) {\n throw new Error('FallbackAgent: No factories provided')\n }\n this.startNextAgent()\n }\n\n // Delegate extraction to the active agent (extract config lives on children)\n extract(message: string) {\n return this.agent ? this.agent.extract(message) : super.extract(message)\n }\n\n protected async generateAnswer(stream: PassThrough): Promise<void> {\n // Try each agent once (one full rotation) until one answers successfully.\n // The conversation is shared between agents, so the next agent picks up\n // exactly where the failed one stopped.\n for (\n let attempt = 0;\n attempt < this.fallbackOptions.factories.length;\n attempt++\n ) {\n const agent = this.agent\n if (!agent) return\n\n let failed = false\n const onFailed = () => {\n failed = true\n }\n agent.once('Failed', onFailed)\n\n try {\n await this.pipeAnswer(agent, stream)\n } finally {\n agent.off('Failed', onFailed)\n }\n\n if (!failed) return\n\n this.log('Agent failed, trying next agent')\n this.startNextAgent()\n }\n\n // Every agent failed within this rotation: report it so an outer consumer\n // (e.g. a wrapping FallbackAgent) can react.\n this.log('All agents failed')\n this.emit('Failed')\n }\n\n cancel() {\n this.agent?.cancel()\n }\n\n destroy() {\n super.destroy()\n this.agent?.destroy()\n this.agent = null\n this.agentIndex = -1\n }\n\n // Run the child agent and forward its answer chunks to our own stream\n private pipeAnswer(agent: Agent, stream: PassThrough): Promise<void> {\n return new Promise((resolve) => {\n const answerStream = agent.answer()\n answerStream.on('data', (chunk) => {\n if (stream.writable) {\n stream.write(chunk)\n }\n })\n answerStream.on('end', resolve)\n answerStream.on('error', resolve)\n })\n }\n\n private startNextAgent() {\n this.agentIndex++\n if (this.agentIndex >= this.fallbackOptions.factories.length) {\n this.agentIndex = 0\n }\n\n const previousAgent = this.agent\n const isFirstAgent = previousAgent === null\n const agent = this.fallbackOptions.factories[this.agentIndex]()\n this.agent = agent\n\n // Share the conversation and tools between the fallback and the child agent.\n // Both are now portable (tools no longer bind to a specific instance), so a\n // single reference is shared, exactly like the conversation.\n if (isFirstAgent) {\n // Adopt the first agent's conversation and tools (keeps its system prompt)\n this.conversation = agent.conversation\n this.tools = agent.tools\n } else {\n // Keep the accumulated history, but use the new agent's system prompt\n if (agent.conversation[0]?.role === 'system') {\n this.conversation[0] = agent.conversation[0]\n }\n agent.conversation = this.conversation\n agent.tools = this.tools\n }\n\n // Forward events from the child agent\n agent.on('Message', this.onMessage)\n agent.on('CancelLastUserMessage', this.onCancelLastUserMessage)\n agent.on('SkipAnswer', this.onSkipAnswer)\n agent.on('EndCall', this.onEndCall)\n agent.on('ToolCall', this.onToolCall)\n\n // Destroy the previous agent (after moving the conversation over)\n previousAgent?.destroy()\n\n // Set logger after event loop\n setTimeout(() => {\n if (this.agent && this.logger) {\n this.agent.logger = new Logger(this.agent.constructor.name)\n }\n }, 0)\n }\n\n private onMessage = (message: MicdropConversationItem) => {\n this.emit('Message', message)\n }\n\n private onCancelLastUserMessage = () => {\n this.emit('CancelLastUserMessage')\n }\n\n private onSkipAnswer = () => {\n this.emit('SkipAnswer')\n }\n\n private onEndCall = () => {\n this.emit('EndCall')\n }\n\n private onToolCall = (toolCall: MicdropToolCall) => {\n this.emit('ToolCall', toolCall)\n }\n}\n","import { PassThrough } from 'stream'\nimport { Agent } from './Agent'\n\nexport class MockAgent extends Agent {\n private i = 0\n\n constructor() {\n super({ systemPrompt: '' })\n }\n\n protected async generateAnswer(stream: PassThrough): Promise<void> {\n const message = `Assistant Message ${this.i++}`\n this.addAssistantMessage(message)\n stream.write(message)\n }\n\n cancel() {}\n}\n","/**\n * Streaming linear-interpolation resampler for PCM16 mono audio.\n *\n * Works in both directions (up or downsampling). It is stateful: it handles\n * arbitrary byte boundaries (a network chunk can split a 16-bit sample) and\n * keeps the fractional sample position continuous across chunks, so feeding a\n * stream chunk by chunk yields the same result as resampling it in one go.\n *\n * Providers use it to bridge their own rate with the 16kHz PCM16 the Micdrop\n * client records and plays: OpenaiSTT (16kHz -> 24kHz, the GA Realtime API\n * requires >= 24kHz), OpenaiTTS and KokoroTTS (24kHz output -> 16kHz).\n */\nexport class Pcm16Resampler {\n private readonly step: number\n private leftover: Buffer<ArrayBufferLike> = Buffer.alloc(0)\n private pos = 0 // Fractional position into the first sample of the buffer\n\n constructor(inRate: number, outRate: number) {\n this.step = inRate / outRate\n }\n\n // Reset to the initial state, to resample a new independent stream\n // (e.g. resending buffered audio after a reconnection).\n reset() {\n this.leftover = Buffer.alloc(0)\n this.pos = 0\n }\n\n process(chunk: Buffer): Buffer {\n const buf = this.leftover.length\n ? Buffer.concat([this.leftover, chunk])\n : chunk\n const samples = Math.floor(buf.length / 2)\n\n // Need at least 2 samples to interpolate\n if (samples < 2) {\n this.leftover = buf\n return Buffer.alloc(0)\n }\n\n const out: number[] = []\n let p = this.pos\n while (Math.floor(p) + 1 < samples) {\n const i = Math.floor(p)\n const frac = p - i\n const s0 = buf.readInt16LE(i * 2)\n const s1 = buf.readInt16LE((i + 1) * 2)\n out.push(Math.round(s0 + (s1 - s0) * frac))\n p += this.step\n }\n\n // Keep the last still-needed sample (and any trailing odd byte) for the\n // next chunk, and carry the fractional position relative to it.\n const consumed = Math.floor(p)\n this.pos = p - consumed\n this.leftover = buf.subarray(consumed * 2)\n\n const result = Buffer.alloc(out.length * 2)\n for (let k = 0; k < out.length; k++) {\n result.writeInt16LE(out[k], k * 2)\n }\n return result\n }\n}\n","/**\n * Conversions between the PCM16 buffers exchanged with the Micdrop client and\n * the float samples that local speech models read and write.\n *\n * Both formats are mono. PCM16 is signed 16-bit little-endian, floats are in\n * the [-1, 1] range. Only the scale changes, the sample rate is left alone.\n */\n\nconst PCM16_MAX = 32767\nconst PCM16_MIN = -32768\n\n/** Turns float samples into a PCM16 buffer, clamping anything out of range. */\nexport function float32ToPcm16(samples: Float32Array): Buffer {\n const buffer = Buffer.alloc(samples.length * 2)\n for (let i = 0; i < samples.length; i++) {\n const scaled = Math.round(samples[i] * PCM16_MAX)\n const clamped = Math.max(PCM16_MIN, Math.min(PCM16_MAX, scaled))\n buffer.writeInt16LE(clamped, i * 2)\n }\n return buffer\n}\n\n/**\n * Turns a PCM16 buffer into float samples.\n *\n * A trailing odd byte is dropped: it is half of a sample whose other half has\n * not arrived, and a caller feeding whole utterances never produces one.\n */\nexport function pcm16ToFloat32(buffer: Buffer): Float32Array {\n const length = Math.floor(buffer.length / 2)\n const samples = new Float32Array(length)\n for (let i = 0; i < length; i++) {\n samples[i] = buffer.readInt16LE(i * 2) / PCM16_MAX\n }\n return samples\n}\n","import WebSocket from 'ws'\n\nexport enum MicdropErrorCode {\n BadRequest = 4400,\n Unauthorized = 4401,\n NotFound = 4404,\n}\n\nexport class MicdropError extends Error {\n code: number\n\n constructor(code: number, message: string) {\n super(message)\n this.code = code\n }\n}\n\nexport function handleError(socket: WebSocket, error: unknown) {\n if (error instanceof MicdropError) {\n socket.close(error.code, error.message)\n } else {\n console.error(error)\n socket.close(1011)\n }\n socket.terminate()\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { Duplex, PassThrough, Readable, Transform } from 'stream'\nimport { WebSocket } from 'ws'\nimport type { Agent } from './agent'\nimport { pcm16ToFloat32 } from './audio'\nimport { Logger } from './Logger'\nimport type { STT } from './stt'\nimport type { TTS } from './tts'\nimport {\n MicdropAnswerMetadata,\n MicdropCallSummary,\n MicdropClientCommands,\n MicdropConversation,\n MicdropConversationItem,\n MicdropConversationMessage,\n MicdropServerCommands,\n TurnDetector,\n} from './types'\n\n/** Rate the client records at, and sends its chunks in */\nconst USER_SAMPLE_RATE = 16000\n\n/**\n * How long an answer waits once the detector asked for the rest of a sentence.\n *\n * The way out of a wrong verdict: a speaker who never comes back still gets an\n * answer instead of a call that goes quiet.\n */\nconst DEFAULT_TURN_MAX_WAIT = 4000 // ms\n\nexport interface MicdropServerEvents {\n End: [MicdropCallSummary]\n /** A message was added to the conversation, by the agent or by the server */\n Message: [MicdropConversationItem]\n UserAudio: [Buffer]\n AssistantAudio: [Buffer]\n}\n\nexport interface MicdropConfig {\n /**\n * Opening line, spoken before the user has said anything.\n *\n * It goes into the conversation like any other assistant message. Leaving it\n * out, along with `generateFirstMessage`, hands the first turn to the user.\n */\n firstMessage?: string\n\n /**\n * Asks the agent for its opening line instead of fixing one, so the greeting\n * can lean on the system prompt and on whatever the call was given.\n *\n * Ignored when `firstMessage` is set, and out of reach without an agent.\n */\n generateFirstMessage?: boolean\n\n /**\n * Generates the answers.\n *\n * Leaving it out turns the call into a one-way one: what the user says is\n * transcribed and sent to the client, and the assistant stays quiet unless\n * the application calls `speak()` itself.\n */\n agent?: Agent\n\n /**\n * Turns what the user says into text, the one component a call cannot do\n * without.\n *\n * It is fed the audio of each turn as it arrives, and the transcripts it\n * returns drive everything that follows.\n */\n stt: STT\n\n /**\n * Gives the answers a voice.\n *\n * Leaving it out keeps the call textual: the answer reaches the client as a\n * message and nothing is synthesized.\n */\n tts?: TTS\n\n /**\n * Sends the answer to the client while it is still being written, on top of\n * the complete message that follows.\n *\n * Works with any agent, since the text is read from the stream it writes to.\n * Off by default, since a client that only displays finished messages has\n * nothing to do with it.\n */\n partialMessages?: boolean\n\n /**\n * Waits for the rest of the sentence when the speaker paused in the middle\n * of one, instead of answering an unfinished question.\n *\n * Prefer running the detector in the client, which reaches the same decision\n * without the round trip and can then close its turns sooner. This is the\n * option for the browsers where the model has nowhere to run.\n */\n turnDetector?: TurnDetector\n\n /** How long to wait for the rest of a sentence, 4000 ms by default */\n turnMaxWait?: number\n}\n\nexport class MicdropServer extends EventEmitter<MicdropServerEvents> {\n public socket: WebSocket | null = null\n public config: MicdropConfig | null = null\n public logger?: Logger\n\n private startTime = Date.now()\n private lastMessageSpeeched?: MicdropConversationItem\n\n // Holds the conversation when no agent does, and keeps it readable once the\n // call is over and the config is gone\n private ownConversation: MicdropConversation = []\n\n // Queue system for operations\n private operationQueue: Array<() => Promise<void>> = []\n private isProcessingQueue = false\n\n // When user is speaking, we're streaming chunks for STT\n private currentUserStream?: Duplex\n private userSpeechChunks = 0\n // Asked as soon as the speaker pauses, so it weighs that stretch of audio\n // and not the next one\n private turnComplete?: Promise<boolean>\n private heldTurnTimer?: ReturnType<typeof setTimeout>\n\n constructor(socket: WebSocket, config: MicdropConfig) {\n super()\n this.socket = socket\n this.config = config\n this.log(`Call started`)\n\n // Setup STT\n config.stt.on('Transcript', this.onTranscriptSTT)\n\n // Setup TTS\n config.tts?.on('Audio', this.onAudioTTS)\n\n // Setup agent\n const agent = config.agent\n if (agent) {\n agent.on('Message', this.onMessageAgent)\n agent.on('CancelLastUserMessage', () =>\n this.socket?.send(MicdropServerCommands.CancelLastUserMessage)\n )\n agent.on('SkipAnswer', () =>\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n )\n agent.on('EndCall', () =>\n this.socket?.send(MicdropServerCommands.EndCall)\n )\n agent.on('ToolCall', (toolCall) =>\n this.socket?.send(\n `${MicdropServerCommands.ToolCall} ${JSON.stringify(toolCall)}`\n )\n )\n }\n\n // Assistant speaks first\n // Deferred so consumers (e.g. MicdropRecorder) can subscribe to the server\n // events before the first message is added to the conversation.\n queueMicrotask(() => this.sendFirstMessage())\n\n // Listen to events\n socket.on('close', this.onClose)\n socket.on('message', this.onMessage)\n }\n\n /** Everything said so far, whether an agent or the server keeps it */\n get conversation(): MicdropConversation {\n return this.config?.agent?.conversation ?? this.ownConversation\n }\n\n private log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n private async processQueue() {\n if (this.isProcessingQueue || this.operationQueue.length === 0) return\n\n this.isProcessingQueue = true\n\n while (this.operationQueue.length > 0) {\n const operation = this.operationQueue.shift()\n if (operation) {\n try {\n await operation()\n } catch (error) {\n this.log('Error processing queued operation:', error)\n }\n }\n }\n\n this.isProcessingQueue = false\n }\n\n private queueOperation(operation: () => Promise<void>) {\n this.operationQueue.push(operation)\n this.processQueue()\n }\n\n public cancel() {\n this.config?.tts?.cancel()\n this.config?.agent?.cancel()\n // Clear the queue\n this.operationQueue = []\n }\n\n private onClose = () => {\n this.releaseHeldTurn()\n if (!this.config) return\n this.log('Connection closed')\n const duration = Math.round((Date.now() - this.startTime) / 1000)\n const conversation = this.conversation\n\n // Destroy instances\n this.config.agent?.destroy()\n this.config.stt.destroy()\n this.config.tts?.destroy()\n\n // Emit End event\n this.emit('End', {\n conversation,\n duration,\n })\n\n // Unset params, keeping the conversation readable\n this.ownConversation = conversation\n this.socket = null\n this.config = null\n }\n\n private onMessage = async (message: Buffer) => {\n if (message.byteLength === 0) return\n if (!Buffer.isBuffer(message)) {\n this.log('Message is not a buffer')\n return\n }\n\n // Commands\n if (message.byteLength < 15) {\n const cmd = message.toString()\n this.log(`Command: ${cmd}`)\n\n if (cmd === MicdropClientCommands.StartSpeaking) {\n // User started speaking\n this.onStartSpeaking()\n } else if (cmd === MicdropClientCommands.Mute) {\n // User muted the call\n this.onMute()\n } else if (cmd === MicdropClientCommands.StopSpeaking) {\n // User stopped speaking\n this.onStopSpeaking()\n }\n }\n\n // Audio chunk\n else if (this.currentUserStream) {\n this.onUserAudio(message)\n }\n }\n\n private onUserAudio(chunk: Buffer) {\n this.log(`Received chunk (${chunk.byteLength} bytes)`)\n this.currentUserStream?.write(chunk)\n this.userSpeechChunks++\n this.config?.turnDetector?.push(pcm16ToFloat32(chunk), USER_SAMPLE_RATE)\n this.emit('UserAudio', chunk)\n }\n\n private onMute() {\n this.userSpeechChunks = 0\n this.currentUserStream?.end()\n this.currentUserStream = undefined\n this.cancel()\n }\n\n private onStartSpeaking() {\n if (!this.config) return\n this.userSpeechChunks = 0\n this.currentUserStream?.end()\n this.currentUserStream = new PassThrough()\n this.config.turnDetector?.reset()\n this.turnComplete = undefined\n // The rest of the sentence is arriving, so the deadline can go\n this.releaseHeldTurn()\n this.config.stt.transcribe(this.currentUserStream)\n this.cancel()\n }\n\n private onStopSpeaking() {\n const hasNoUserSpeech =\n !this.currentUserStream || this.userSpeechChunks === 0\n this.currentUserStream?.end()\n this.currentUserStream = undefined\n this.userSpeechChunks = 0\n\n // If user is not speaking or no chunks were received, skip\n if (hasNoUserSpeech) {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n return\n }\n\n // Weigh the stretch of audio that just ended, before the next one starts\n this.turnComplete = this.predictTurnComplete()\n\n const conversation = this.conversation\n const lastMessage = conversation[conversation.length - 1]\n if (\n lastMessage?.role === 'user' &&\n this.lastMessageSpeeched !== lastMessage\n ) {\n this.log(\n 'User stopped speaking and a transcript already exists, answering'\n )\n this.answerUserTurn()\n }\n }\n\n private async predictTurnComplete(): Promise<boolean> {\n const detector = this.config?.turnDetector\n if (!detector) return true\n try {\n const { complete } = await detector.predict()\n this.log(`Turn sounds ${complete ? 'finished' : 'unfinished'}`)\n return complete\n } catch (error) {\n this.log(`Turn detection failed: ${error}`)\n return true\n }\n }\n\n /** Answers, unless the sentence sounds like it has more coming */\n private async answerUserTurn() {\n const complete = await (this.turnComplete ?? Promise.resolve(true))\n if (!complete) {\n this.log('Waiting for the rest of the sentence')\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n this.holdTurn()\n return\n }\n this.releaseHeldTurn()\n this.cancel()\n this.answer()\n }\n\n /**\n * Answers anyway if the rest of the sentence never comes.\n *\n * Without it, a detector that hears an unfinished sentence where there is\n * none leaves the call silent for good.\n */\n private holdTurn() {\n this.releaseHeldTurn()\n this.heldTurnTimer = setTimeout(() => {\n this.heldTurnTimer = undefined\n this.log('Nothing more came, answering')\n this.cancel()\n this.answer()\n }, this.config?.turnMaxWait ?? DEFAULT_TURN_MAX_WAIT)\n }\n\n private releaseHeldTurn() {\n if (!this.heldTurnTimer) return\n clearTimeout(this.heldTurnTimer)\n this.heldTurnTimer = undefined\n }\n\n private onTranscriptSTT = async (transcript: string) => {\n if (!this.config) return\n\n // Skip answer if transcript is empty\n if (transcript === '') {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n return\n }\n\n this.log(`User transcript: \"${transcript}\"`)\n this.addUserMessage(transcript)\n\n // Answer if user stopped speaking\n if (!this.currentUserStream) {\n this.log('User stopped speaking, answering')\n this.answerUserTurn()\n }\n }\n\n private onAudioTTS = (audio: Buffer) => {\n if (!this.socket) return\n this.log(`Send audio chunk (${audio.byteLength} bytes)`)\n this.socket.send(audio)\n this.emit('AssistantAudio', audio)\n }\n\n private onMessageAgent = (message: MicdropConversationItem) => {\n this.sendMessage(message)\n }\n\n /** Adds a message to the conversation and sends it to the client */\n public addUserMessage(text: string, metadata?: MicdropAnswerMetadata) {\n if (this.config?.agent) {\n this.config.agent.addUserMessage(text, metadata)\n } else {\n this.addOwnMessage('user', text, metadata)\n }\n }\n\n /** Adds a message to the conversation and sends it to the client */\n public addAssistantMessage(text: string, metadata?: MicdropAnswerMetadata) {\n if (this.config?.agent) {\n this.config.agent.addAssistantMessage(text, metadata)\n } else {\n this.addOwnMessage('assistant', text, metadata)\n }\n }\n\n private addOwnMessage(\n role: 'user' | 'assistant',\n text: string,\n metadata?: MicdropAnswerMetadata\n ) {\n if (text.trim() === '') {\n this.log(`Skipping empty ${role} message`)\n return\n }\n this.log(`Adding ${role} message to conversation: ${text}`)\n const message: MicdropConversationMessage = {\n role,\n content: text,\n metadata,\n }\n this.ownConversation.push(message)\n this.sendMessage(message)\n }\n\n private sendMessage(message: MicdropConversationItem) {\n this.socket?.send(\n `${MicdropServerCommands.Message} ${JSON.stringify(message)}`\n )\n this.emit('Message', message)\n }\n\n private sendPartialAnswer(content: string) {\n if (!this.socket || content === '') return\n this.log(`Send partial answer: \"${content}\"`)\n this.socket.send(\n `${MicdropServerCommands.PartialAssistantMessage} ${JSON.stringify(content)}`\n )\n }\n\n private sendFirstMessage() {\n if (!this.config) return\n if (this.config.firstMessage) {\n // Send first message\n // Without an agent, speak() is the one adding it to the conversation\n this.config.agent?.addAssistantMessage(this.config.firstMessage)\n this.speak(this.config.firstMessage)\n } else if (this.config.generateFirstMessage && this.config.agent) {\n // Generate first message\n this.answer()\n } else {\n // Skip answer if no first message is provided\n // to avoid keeping the client in a processing state\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n }\n }\n\n public answer() {\n this.queueOperation(async () => {\n await this._answer()\n })\n }\n\n private async _answer() {\n if (!this.config) return\n\n // Nothing generates an answer, hand the turn back to the user\n if (!this.config.agent) {\n this.log('No agent, skipping answer')\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n return\n }\n\n // Prevent answering twice\n const conversation = this.config.agent.conversation\n const lastMessage = conversation[conversation.length - 1]\n if (this.lastMessageSpeeched === lastMessage) {\n this.log('Already answered, skipping')\n return\n }\n this.lastMessageSpeeched = lastMessage\n\n try {\n // LLM: Generate answer\n const stream = this.config.agent.answer()\n\n // TTS: Generate answer audio, unless there is nothing to say.\n //\n // An answer can be skipped after the fact: a tool with skipAnswer, or an\n // onBeforeAnswer hook returning true, ends the stream without a word in\n // it. Handing that empty stream to the TTS opens a synthesis request for\n // nothing, and a provider that stamps each request (Gradium multiplexes\n // this way) then drops the audio of the sentence still playing, so a\n // skipped answer cuts the assistant off mid-word.\n if (await hasContent(stream)) {\n await this._speak(\n this.config.partialMessages\n ? this.forwardPartialAnswer(stream)\n : stream\n )\n } else {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n }\n } catch (error) {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n throw error\n }\n }\n\n /** Sends the answer to the client as it is written, ahead of the voice */\n private forwardPartialAnswer(stream: Readable): Readable {\n let content = ''\n const send = (text: string) => this.sendPartialAnswer(text)\n const forward = new Transform({\n transform(chunk, _encoding, callback) {\n content += chunk.toString()\n send(content)\n callback(null, chunk)\n },\n })\n return stream.pipe(forward)\n }\n\n // Run text-to-speech and send to client\n public speak(message: string | Readable) {\n this.queueOperation(async () => {\n await this._speak(message)\n })\n }\n\n private async _speak(message: string | Readable) {\n if (!this.socket || !this.config) return\n\n // Convert message to stream if needed\n let textStream: Readable\n if (typeof message === 'string') {\n // Without an agent, nothing else records what the assistant says\n if (!this.config.agent) {\n this.addOwnMessage('assistant', message)\n }\n const stream = new PassThrough()\n stream.write(message)\n stream.end()\n textStream = stream\n } else {\n textStream = message\n }\n\n // Run TTS\n if (this.config.tts) {\n this.config.tts.speak(textStream)\n return\n }\n\n // No voice to synthesize: read the answer to the end, then hand the turn\n // back, since it is the first audio chunk that normally tells the client\n // it can stop waiting.\n await drain(textStream)\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n }\n}\n\n/**\n * Resolves true as soon as the stream holds something to read, false if it ends\n * without ever carrying anything.\n *\n * The chunk read to find out is put back, so the consumer that follows sees the\n * whole stream from its first byte.\n */\nfunction hasContent(stream: Readable): Promise<boolean> {\n return new Promise((resolve) => {\n const onReadable = () => {\n const chunk = stream.read()\n if (chunk === null) return\n stream.unshift(chunk)\n done(true)\n }\n const onEnd = () => done(false)\n const done = (result: boolean) => {\n stream.off('readable', onReadable)\n stream.off('end', onEnd)\n resolve(result)\n }\n stream.on('readable', onReadable)\n stream.on('end', onEnd)\n })\n}\n\n/** Reads a stream to its end, for when nothing else consumes it */\nfunction drain(stream: Readable): Promise<void> {\n return new Promise((resolve) => {\n stream.on('data', () => {})\n stream.once('end', () => resolve())\n stream.once('error', () => resolve())\n stream.once('close', () => resolve())\n })\n}\n","export enum MicdropClientCommands {\n StartSpeaking = 'StartSpeaking',\n StopSpeaking = 'StopSpeaking',\n Mute = 'Mute',\n}\n\nexport enum MicdropServerCommands {\n Message = 'Message',\n PartialAssistantMessage = 'PartialAssistantMessage',\n CancelLastUserMessage = 'CancelLastUserMessage',\n SkipAnswer = 'SkipAnswer',\n EndCall = 'EndCall',\n ToolCall = 'ToolCall',\n}\n\n/**\n * Hears whether a sentence has landed, where voice activity detection only\n * hears whether someone is speaking.\n *\n * `SmartTurn` from `@micdrop/smart-turn` implements it, and so can anything\n * else, a call to a service included. Both sides of a call can hold one, the\n * client to decide when its turn ends and the server to decide when to answer.\n */\nexport interface TurnDetector {\n /**\n * Feeds the audio received since the last call\n * @param samples - Mono samples, in the -1..1 range\n * @param sampleRate - Sample rate of `samples`, in Hz\n */\n push(samples: Float32Array, sampleRate?: number): void\n\n /** Answers whether the turn pushed so far sounds finished */\n predict(): Promise<{ complete: boolean }>\n\n /** Starts a new turn, forgetting the previous one */\n reset(): void\n}\n\nexport interface MicdropCallSummary {\n conversation: MicdropConversation\n duration: number\n}\n\nexport type MicdropConversationItem =\n | MicdropConversationMessage\n | MicdropConversationToolCall\n | MicdropConversationToolResult\n\nexport type MicdropConversation = Array<MicdropConversationItem>\n\nexport type MicdropAnswerMetadata = {\n [key: string]: any\n}\n\nexport interface MicdropConversationMessage<\n Data extends MicdropAnswerMetadata = MicdropAnswerMetadata,\n> {\n role: 'system' | 'user' | 'assistant'\n content: string\n metadata?: Data\n}\n\nexport interface MicdropConversationToolCall {\n role: 'tool_call'\n toolCallId: string\n toolName: string\n parameters: string\n}\n\nexport interface MicdropConversationToolResult {\n role: 'tool_result'\n toolCallId: string\n toolName: string\n output: string\n}\n\nexport interface MicdropToolCall {\n name: string\n parameters: any\n output: any\n}\n\nexport type DeepPartial<T> = T extends object\n ? {\n [P in keyof T]?: DeepPartial<T[P]>\n }\n : T\n","import { EventEmitter } from 'eventemitter3'\nimport type { MicdropServer } from '../MicdropServer'\nimport type { MicdropConversationItem } from '../types'\nimport { Logger } from '../Logger'\n\nexport interface AudioMessage {\n buffer: Buffer\n messageIndex: number\n message: string\n role: 'user' | 'assistant'\n}\n\nexport interface MicdropRecorderEvents {\n AudioMessage: [AudioMessage]\n Complete: [AudioMessage[]]\n}\n\nexport class MicdropRecorder extends EventEmitter<MicdropRecorderEvents> {\n public logger?: Logger\n\n private audioMessages: AudioMessage[] = []\n private currentUserChunks: Buffer[] = []\n private currentAssistantChunks: Buffer[] = []\n private lastUserMessageIndex: number = -1\n private lastAssistantMessageIndex: number = -1\n\n constructor(private server: MicdropServer) {\n super()\n this.setupListeners()\n }\n\n private setupListeners() {\n // Listen to audio events from server\n this.server.on('UserAudio', this.onUserAudio)\n this.server.on('AssistantAudio', this.onAssistantAudio)\n this.server.on('End', this.onEnd)\n this.server.on('Message', this.onMessage)\n }\n\n private onUserAudio = (chunk: Buffer) => {\n // Finalize or discard assistant audio when user starts speaking\n if (this.currentAssistantChunks.length > 0) {\n if (this.lastAssistantMessageIndex >= 0) {\n this.finalizeAssistantAudio()\n } else {\n // Discard orphaned chunks (no associated message)\n this.log('Discarding orphaned assistant audio chunks')\n this.currentAssistantChunks = []\n }\n }\n\n this.log('Recording user audio chunk')\n this.currentUserChunks.push(chunk)\n }\n\n private onAssistantAudio = (chunk: Buffer) => {\n // Finalize or discard user audio when assistant starts speaking\n if (this.currentUserChunks.length > 0) {\n if (this.lastUserMessageIndex >= 0) {\n this.finalizeUserAudio()\n } else {\n // Discard orphaned chunks (no associated message)\n this.log('Discarding orphaned user audio chunks')\n this.currentUserChunks = []\n }\n }\n\n this.log('Recording assistant audio chunk')\n this.currentAssistantChunks.push(chunk)\n }\n\n private onMessage = (message: MicdropConversationItem) => {\n const messageIndex = this.server.conversation.length - 1\n\n if (message.role === 'user') {\n this.lastUserMessageIndex = messageIndex\n // User audio might already be complete, finalize if we have chunks\n // Audio chunks arrive BEFORE message, so we finalize when we know the message\n if (this.currentUserChunks.length > 0) {\n this.finalizeUserAudio()\n }\n } else if (message.role === 'assistant') {\n this.lastAssistantMessageIndex = messageIndex\n // Don't finalize assistant audio here - chunks can still arrive after message\n }\n }\n\n private finalizeUserAudio() {\n if (this.currentUserChunks.length === 0) return\n if (this.lastUserMessageIndex < 0) return\n\n const message = this.server.conversation[this.lastUserMessageIndex]\n if (!message) return\n const buffer = Buffer.concat(this.currentUserChunks)\n\n const audioMessage: AudioMessage = {\n buffer,\n messageIndex: this.lastUserMessageIndex,\n message: 'content' in message ? message.content : '',\n role: 'user',\n }\n\n this.log(\n `Finalized user audio: ${buffer.length} bytes, message index ${this.lastUserMessageIndex}`\n )\n this.audioMessages.push(audioMessage)\n this.emit('AudioMessage', audioMessage)\n\n // Reset\n this.currentUserChunks = []\n this.lastUserMessageIndex = -1\n }\n\n private finalizeAssistantAudio() {\n if (this.currentAssistantChunks.length === 0) return\n if (this.lastAssistantMessageIndex < 0) return\n\n const message = this.server.conversation[this.lastAssistantMessageIndex]\n if (!message) return\n const buffer = Buffer.concat(this.currentAssistantChunks)\n\n const audioMessage: AudioMessage = {\n buffer,\n messageIndex: this.lastAssistantMessageIndex,\n message: 'content' in message ? message.content : '',\n role: 'assistant',\n }\n\n this.log(\n `Finalized assistant audio: ${buffer.length} bytes, message index ${this.lastAssistantMessageIndex}`\n )\n this.audioMessages.push(audioMessage)\n this.emit('AudioMessage', audioMessage)\n\n // Reset\n this.currentAssistantChunks = []\n this.lastAssistantMessageIndex = -1\n }\n\n private onEnd = () => {\n // Finalize any remaining audio\n if (this.currentUserChunks.length > 0) {\n this.finalizeUserAudio()\n }\n if (this.currentAssistantChunks.length > 0) {\n this.finalizeAssistantAudio()\n }\n\n this.log(`Recording complete: ${this.audioMessages.length} audio messages`)\n this.emit('Complete', this.audioMessages)\n }\n\n public getAudioMessages(): AudioMessage[] {\n return [...this.audioMessages]\n }\n\n public destroy() {\n this.log('Destroyed')\n this.server.off('UserAudio', this.onUserAudio)\n this.server.off('AssistantAudio', this.onAssistantAudio)\n this.server.off('End', this.onEnd)\n this.server.off('Message', this.onMessage)\n\n this.removeAllListeners()\n }\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { Readable } from 'stream'\nimport { Logger } from '../Logger'\n\nexport interface STTEvents {\n Transcript: [string]\n Failed: [Buffer[]]\n}\n\nexport abstract class STT extends EventEmitter<STTEvents> {\n public logger?: Logger\n\n // Set stream of audio to transcribe\n abstract transcribe(audioStream: Readable): void\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n destroy() {\n this.log('Destroyed')\n this.removeAllListeners()\n }\n}\n","import { STT } from './STT'\n\nexport class MockSTT extends STT {\n private i = 0\n\n async transcribe() {\n setTimeout(() => {\n this.emit('Transcript', `User Message ${this.i++}`)\n }, 300)\n }\n}\n","import { PassThrough, Readable } from 'stream'\nimport { STT } from './STT'\nimport { Logger } from '..'\n\nexport interface FallbackSTTOptions {\n factories: Array<() => STT>\n}\n\nexport class FallbackSTT extends STT {\n private stt: STT | null = null\n private sttIndex = -1 // Start at -1 because we need to increment it before using it\n\n constructor(private readonly options: FallbackSTTOptions) {\n super()\n if (this.options.factories.length === 0) {\n throw new Error('FallbackSTT: No factories provided')\n }\n this.startNextSTT()\n }\n\n transcribe(audioStream: Readable) {\n this.stt?.transcribe(audioStream)\n }\n\n destroy() {\n super.destroy()\n this.stt?.destroy()\n this.stt = null\n this.sttIndex = -1\n }\n\n private startNextSTT() {\n this.sttIndex++\n if (this.sttIndex >= this.options.factories.length) {\n this.sttIndex = 0\n }\n this.stt?.destroy()\n this.stt = this.options.factories[this.sttIndex]()\n this.stt.on('Transcript', this.onTranscript)\n this.stt.on('Failed', this.onFailed)\n\n // Set logger after event loop\n setTimeout(() => {\n if (this.stt && this.logger) {\n this.stt.logger = new Logger(this.stt.constructor.name)\n }\n }, 0)\n }\n\n private onTranscript = (transcript: string) => {\n this.emit('Transcript', transcript)\n }\n\n private onFailed = (chunks: Buffer[]) => {\n this.log('STT failed, trying next STT')\n this.startNextSTT()\n\n if (chunks.length > 0) {\n this.log('Sending audio chunks again')\n const stream = new PassThrough()\n this.stt?.transcribe(stream)\n chunks.forEach((chunk) => stream.write(chunk))\n stream.end()\n }\n }\n}\n","import { PassThrough, Readable } from 'stream'\nimport { TTS } from './TTS'\nimport { Logger } from '..'\n\nexport interface FallbackTTSOptions {\n factories: Array<() => TTS>\n}\n\nexport class FallbackTTS extends TTS {\n private tts: TTS | null = null\n private ttsIndex = -1 // Start at -1 because we need to increment it before using it\n\n constructor(private readonly options: FallbackTTSOptions) {\n super()\n if (this.options.factories.length === 0) {\n throw new Error('FallbackTTS: No factories provided')\n }\n this.startNextTTS()\n }\n\n speak(textStream: Readable) {\n this.tts?.speak(textStream)\n }\n\n cancel() {\n this.tts?.cancel()\n }\n\n destroy() {\n super.destroy()\n this.tts?.destroy()\n this.tts = null\n this.ttsIndex = -1\n }\n\n private startNextTTS() {\n this.ttsIndex++\n if (this.ttsIndex >= this.options.factories.length) {\n this.ttsIndex = 0\n }\n this.tts?.destroy()\n this.tts = this.options.factories[this.ttsIndex]()\n this.tts.on('Audio', this.onAudio)\n this.tts.on('Failed', this.onFailed)\n\n // Set logger after event loop\n setTimeout(() => {\n if (this.tts && this.logger) {\n this.tts.logger = new Logger(this.tts.constructor.name)\n }\n }, 0)\n }\n\n private onAudio = (audio: Buffer) => {\n this.emit('Audio', audio)\n }\n\n private onFailed = (chunks: string[]) => {\n this.log('TTS failed, trying next TTS')\n this.startNextTTS()\n\n if (chunks.length > 0) {\n this.log('Sending text chunks again')\n const stream = new PassThrough()\n this.tts?.speak(stream)\n chunks.forEach((chunk) => stream.write(chunk))\n stream.end()\n }\n }\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { Readable } from 'stream'\nimport { Logger } from '../Logger'\n\nexport interface TTSEvents {\n Audio: [Buffer]\n Failed: [string[]]\n}\n\nexport abstract class TTS extends EventEmitter<TTSEvents> {\n public logger?: Logger\n\n abstract speak(textStream: Readable): void\n abstract cancel(): void\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n destroy() {\n this.log('Destroyed')\n this.cancel()\n }\n}\n","import * as fs from 'fs'\nimport { PassThrough, Readable } from 'stream'\nimport { TTS } from './TTS'\n\nexport class MockTTS extends TTS {\n constructor(private audioFilePaths: string[]) {\n super()\n }\n\n speak(textStream: Readable) {\n const audioStream = new PassThrough()\n textStream.once('data', async () => {\n for (const filePath of this.audioFilePaths) {\n await new Promise((resolve) => setTimeout(resolve, 200))\n const audioBuffer = fs.readFileSync(filePath)\n this.log(`Loaded chunk (${audioBuffer.length} bytes)`)\n audioStream.write(audioBuffer)\n }\n audioStream.end()\n })\n return audioStream\n }\n\n cancel() {}\n}\n","/**\n * Cuts a stream of text into sentences as it arrives.\n *\n * Providers that synthesize a whole input at once need complete sentences, and\n * an agent writes its answer token by token. Feeding every fragment as it comes\n * would either cut words in half or wait for the end of the answer, so the text\n * is buffered until a sentence closes and released the moment it does.\n *\n * The splitter is stateful: `push` returns the sentences that are complete,\n * `flush` returns whatever is left when the stream ends.\n */\nexport class SentenceSplitter {\n private buffer = ''\n\n /** Adds text and returns the sentences it completes. */\n push(text: string): string[] {\n this.buffer += text\n return this.extract(false)\n }\n\n /** Returns the sentences left in the buffer and empties it. */\n flush(): string[] {\n const sentences = this.extract(true)\n const rest = this.buffer.trim()\n this.buffer = ''\n if (rest) sentences.push(rest)\n return sentences\n }\n\n /** Drops the buffered text, used when an utterance is cancelled. */\n reset() {\n this.buffer = ''\n }\n\n private extract(end: boolean): string[] {\n const sentences: string[] = []\n const regex = /[\\s\\S]*?[.!?…\\n]+(?=\\s|$)/g\n let match: RegExpExecArray | null\n let lastIndex = 0\n\n while ((match = regex.exec(this.buffer)) !== null) {\n // A sentence ending at the very end of an unfinished stream may still\n // grow, so keep it buffered until more text arrives or the stream ends.\n if (!end && regex.lastIndex === this.buffer.length) break\n const sentence = match[0].trim()\n if (sentence) sentences.push(sentence)\n lastIndex = regex.lastIndex\n }\n\n this.buffer = this.buffer.slice(lastIndex)\n return sentences\n }\n}\n","import { Readable } from 'stream'\nimport { SentenceSplitter } from './SentenceSplitter'\nimport { TTS } from './TTS'\n\n/**\n * Base class for text to speech engines that read a whole input at once.\n *\n * A local model, and a remote endpoint without a streaming interface, cannot\n * be fed the agent's answer token by token. This class buffers the answer into\n * sentences, hands them over one at a time, and emits the audio in the order\n * they were written. Subclasses only have to turn one sentence into PCM16 at\n * the rate the Micdrop client expects.\n *\n * Sentences are synthesized one after the other rather than at once: a local\n * model is single threaded, so racing two sentences through it slows both down\n * without bringing the first word any closer.\n */\nexport abstract class SentenceTTS extends TTS {\n private splitter = new SentenceSplitter()\n private queue: string[] = []\n private draining = false\n private controller?: AbortController\n // Bumped by every speak() and every cancel(), so a call claimed late can tell\n // whether it is still the one that should be heard.\n private generation = 0\n private counter = 0 // Identifies the current speak() call\n private synthesizing = 0 // Stamp of the sentence being synthesized\n\n /**\n * Turns one sentence into PCM16 audio at the client's sample rate.\n *\n * The signal is aborted when the utterance is cancelled, which is the moment\n * to stop a subprocess or an inference that is no longer needed. Returning\n * nothing emits nothing, which is how a cancelled synthesis reports back.\n */\n protected abstract synthesize(\n text: string,\n signal: AbortSignal\n ): Promise<Buffer | undefined>\n\n /**\n * Emits a piece of the sentence being synthesized.\n *\n * A model that generates progressively can hand its chunks over as they\n * come rather than waiting for the sentence to be finished, which brings\n * the first word forward by the duration of that sentence. The false it\n * returns says the utterance was cancelled or replaced, so the generation\n * it comes from can be stopped there.\n */\n protected emitAudio(audio: Buffer): boolean {\n if (this.synthesizing !== this.counter) return false\n if (audio.length) this.emit('Audio', audio)\n return true\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let counter = 0\n\n // Claiming the call is deferred until there is something to say.\n //\n // Taking the next number right away would drop the utterance still being\n // spoken, since the queue skips anything stamped with an older one. A\n // stream that never carries a word, which is what an answer skipped by a\n // tool or by onBeforeAnswer hands over, would then cut the assistant off\n // and throw away the sentences still queued.\n const claimCall = () => {\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 this.splitter.reset()\n return true\n }\n\n textStream.on('data', (chunk: Buffer) => {\n if (!claimCall()) return\n if (counter !== this.counter) return\n this.enqueue(counter, this.splitter.push(chunk.toString('utf-8')))\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream', error)\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is nothing left to flush\n if (!counter || counter !== this.counter) return\n this.enqueue(counter, this.splitter.flush())\n })\n }\n\n cancel() {\n this.log('Cancel')\n this.generation++\n // Increment counter to ignore queued work and the sentence in flight\n this.counter++\n this.splitter.reset()\n this.queue = []\n this.controller?.abort()\n this.controller = undefined\n }\n\n private enqueue(counter: number, sentences: string[]) {\n if (sentences.length === 0) return\n if (counter !== this.counter) return\n this.queue.push(...sentences)\n this.drain()\n }\n\n private async drain() {\n if (this.draining) return\n this.draining = true\n\n while (this.queue.length > 0) {\n const counter = this.counter\n this.synthesizing = counter\n const text = this.queue.shift()!\n const controller = new AbortController()\n this.controller = controller\n\n try {\n this.log(`Synthesizing: \"${text}\"`)\n const audio = await this.synthesize(text, controller.signal)\n // The utterance may have been cancelled while it was being synthesized\n if (counter !== this.counter) continue\n if (audio?.length) this.emit('Audio', audio)\n } catch (error) {\n // A cancelled utterance is not a failure, it left its queue on purpose\n if (counter !== this.counter) continue\n this.log('Error synthesizing speech', error)\n this.emit('Failed', [text, ...this.queue])\n this.queue = []\n } finally {\n if (this.controller === controller) this.controller = undefined\n }\n }\n\n this.draining = false\n // Sentences may have arrived right as we exited the loop\n if (this.queue.length > 0) this.drain()\n }\n}\n","import { WebSocket } from 'ws'\nimport { MicdropError, MicdropErrorCode } from './errors'\n\nexport async function waitForParams<CallParams>(\n socket: WebSocket,\n validate: (params: any) => CallParams\n): Promise<CallParams> {\n return new Promise<CallParams>((resolve, reject) => {\n // Handle timeout\n const timeout = setTimeout(() => {\n reject(new MicdropError(MicdropErrorCode.BadRequest, 'Missing params'))\n }, 3000)\n\n const onParams = (payload: string) => {\n // Clear timeout and listener\n clearTimeout(timeout)\n socket.off('message', onParams)\n\n try {\n // Parse JSON payload\n const params = validate(JSON.parse(payload))\n resolve(params)\n } catch (error) {\n reject(new MicdropError(MicdropErrorCode.BadRequest, 'Invalid params'))\n }\n }\n\n // Listen for params\n socket.on('message', onParams)\n })\n}\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,mBAAuC;;;ACazC,IAAM,0BAA0B;AAChC,IAAM,uBACX;AAEK,IAAM,+BAA+B;AACrC,IAAM,4BACX;AAEK,IAAM,mCAAm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D,UAAM,UAAU,WAAW,MAAM;AAC/B,aAAO,IAAI,oCAA0C,gBAAgB,CAAC;AAAA,IACxE,GAAG,GAAI;AAEP,UAAM,WAAW,CAAC,YAAoB;AAEpC,mBAAa,OAAO;AACpB,aAAO,IAAI,WAAW,QAAQ;AAE9B,UAAI;AAEF,cAAM,SAAS,SAAS,KAAK,MAAM,OAAO,CAAC;AAC3C,gBAAQ,MAAM;AAAA,MAChB,SAAS,OAAO;AACd,eAAO,IAAI,oCAA0C,gBAAgB,CAAC;AAAA,MACxE;AAAA,IACF;AAGA,WAAO,GAAG,WAAW,QAAQ;AAAA,EAC/B,CAAC;AACH;","names":["MicdropErrorCode","EventEmitter","PassThrough","MicdropClientCommands","MicdropServerCommands","EventEmitter","PassThrough","EventEmitter","EventEmitter","PassThrough","PassThrough","PassThrough","EventEmitter","PassThrough","PassThrough","PassThrough"]}
1
+ {"version":3,"sources":["../src/agent/Agent.ts","../src/agent/tools.ts","../src/Logger.ts","../src/agent/FallbackAgent.ts","../src/agent/MockAgent.ts","../src/audio/Pcm16Resampler.ts","../src/audio/pcm16.ts","../src/errors.ts","../src/MicdropServer.ts","../src/types.ts","../src/realtime/Realtime.ts","../src/recorder/MicdropRecorder.ts","../src/SharedInstances.ts","../src/stt/STT.ts","../src/stt/MockSTT.ts","../src/stt/FallbackSTT.ts","../src/tts/FallbackTTS.ts","../src/tts/TTS.ts","../src/tts/MockTTS.ts","../src/tts/SentenceSplitter.ts","../src/tts/SentenceTTS.ts","../src/waitForParams.ts"],"sourcesContent":["import { EventEmitter } from 'eventemitter3'\nimport { PassThrough, Readable, Writable } from 'stream'\nimport type { z } from 'zod'\nimport { Logger } from '../Logger'\nimport {\n MicdropAnswerMetadata,\n MicdropConversation,\n MicdropConversationItem,\n MicdropConversationMessage,\n MicdropConversationToolCall,\n MicdropConversationToolResult,\n MicdropToolCall,\n} from '../types'\nimport {\n AUTO_END_CALL_PROMPT,\n AUTO_END_CALL_TOOL_NAME,\n AUTO_IGNORE_USER_NOISE_PROMPT,\n AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n AUTO_SEMANTIC_TURN_PROMPT,\n AUTO_SEMANTIC_TURN_TOOL_NAME,\n Tool,\n} from './tools'\n\nexport interface AgentOptions {\n systemPrompt: string\n\n // Enable auto ending of the call when user asks to end the call\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoEndCall?: boolean | string\n\n // Enable detection of an incomplete sentence, and skip the answer (assistant waits)\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoSemanticTurn?: boolean | string\n\n // Ignore of the last user message when it's meaningless\n // You can provide a custom prompt to use instead of the default one by passing a string\n autoIgnoreUserNoise?: boolean | string\n\n // Extract a value from the answer\n // Value must be at the end of the answer, in JSON or between tags\n extract?: ExtractJsonOptions | ExtractTagOptions\n\n // Function called before any answer is generated\n // Return true to skip generation\n onBeforeAnswer?: (\n this: Agent,\n stream: Writable\n ) => void | boolean | Promise<boolean>\n}\n\nexport interface AgentEvents {\n Message: [MicdropConversationItem]\n CancelLastUserMessage: []\n SkipAnswer: []\n EndCall: []\n ToolCall: [MicdropToolCall]\n // Emitted when the agent gives up generating an answer (e.g. after exhausting\n // its retries). Used by FallbackAgent to switch to the next agent.\n Failed: []\n}\n\nexport interface ExtractOptions {\n callback?: (value: string) => void\n saveInMetadata?: boolean\n}\n\nexport interface ExtractJsonOptions extends ExtractOptions {\n json: true\n callback?: (value: any) => void\n}\n\nexport interface ExtractTagOptions extends ExtractOptions {\n startTag: string\n endTag: string\n}\n\nexport abstract class Agent<\n Options extends AgentOptions = AgentOptions,\n Events extends AgentEvents = AgentEvents,\n> extends EventEmitter<Events> {\n public logger?: Logger\n public conversation: MicdropConversation\n public tools: Tool[]\n\n protected answerCount = 0\n protected answering = false\n\n constructor(protected options: Options) {\n super()\n this.conversation = [{ role: 'system', content: options.systemPrompt }]\n this.tools = this.getDefaultTools()\n }\n\n protected abstract generateAnswer(stream: PassThrough): Promise<void>\n abstract cancel(): void\n\n answer(): Readable {\n this.log('Start answering')\n const answerCount = ++this.answerCount\n const stream = new PassThrough()\n this.answering = true\n\n Promise.resolve()\n // Call hook onBeforeAnswer\n .then(() =>\n this.options.onBeforeAnswer?.bind(this as unknown as Agent)(stream)\n )\n // Generate answer (if not skipped)\n .then((skip) => {\n if (skip) return\n return this.generateAnswer(stream)\n })\n // End stream\n .finally(() => {\n if (stream.writable) {\n stream.end()\n }\n if (answerCount === this.answerCount) {\n this.answering = false\n }\n })\n\n return stream\n }\n\n addUserMessage(text: string, metadata?: MicdropAnswerMetadata) {\n this.addMessage('user', text, metadata)\n }\n\n addAssistantMessage(text: string, metadata?: MicdropAnswerMetadata) {\n this.addMessage('assistant', text, metadata)\n }\n\n addTool<Schema extends z.ZodObject>(tool: Tool<Schema>) {\n this.tools.push(tool)\n }\n\n removeTool(name: string) {\n const index = this.tools.findIndex((tool) => tool.name === name)\n if (index !== -1) {\n this.tools.splice(index, 1)\n }\n }\n\n getTool(name: string): Tool | undefined {\n return this.tools.find((tool) => tool.name === name)\n }\n\n addMessage(\n role: 'user' | 'assistant' | 'system',\n text: string,\n metadata?: MicdropAnswerMetadata\n ) {\n // A turn can carry no text at all, typically when the LLM answered with a\n // tool call only. Keeping it would send an empty message back to the LLM on\n // the next turn, and emit a Message event that consumers store as an empty\n // exchange in their transcripts.\n if (text.trim() === '') {\n this.log(`Skipping empty ${role} message`)\n return\n }\n\n this.log(`Adding ${role} message to conversation: ${text}`)\n const message: MicdropConversationMessage = {\n role,\n content: text,\n metadata,\n }\n this.conversation.push(message)\n this.emitAgentEvent('Message', message)\n }\n\n addToolMessage(\n message: MicdropConversationToolCall | MicdropConversationToolResult\n ) {\n this.log('Adding tool message:', message)\n this.conversation.push(message)\n this.emitAgentEvent('Message', message)\n }\n\n protected endCall() {\n this.log('Ending call')\n this.emitAgentEvent('EndCall')\n }\n\n protected cancelLastUserMessage() {\n this.log('Cancelling last user message')\n const lastMessageIndex = this.conversation.findLastIndex(\n (message) => message.role === 'user'\n )\n if (lastMessageIndex !== -1) {\n this.conversation.splice(lastMessageIndex, 1)\n }\n this.emitAgentEvent('CancelLastUserMessage')\n }\n\n protected skipAnswer() {\n this.log('Skipping answer')\n this.emitAgentEvent('SkipAnswer')\n }\n\n protected getDefaultTools() {\n const tools: Tool[] = []\n if (this.options.autoEndCall) {\n tools.push({\n name: AUTO_END_CALL_TOOL_NAME,\n description:\n typeof this.options.autoEndCall === 'string'\n ? this.options.autoEndCall\n : AUTO_END_CALL_PROMPT,\n execute: (_input, agent) => agent.endCall(),\n })\n }\n if (this.options.autoSemanticTurn) {\n tools.push({\n name: AUTO_SEMANTIC_TURN_TOOL_NAME,\n description:\n typeof this.options.autoSemanticTurn === 'string'\n ? this.options.autoSemanticTurn\n : AUTO_SEMANTIC_TURN_PROMPT,\n skipAnswer: true,\n execute: (_input, agent) => agent.skipAnswer(),\n })\n }\n if (this.options.autoIgnoreUserNoise) {\n tools.push({\n name: AUTO_IGNORE_USER_NOISE_TOOL_NAME,\n description:\n typeof this.options.autoIgnoreUserNoise === 'string'\n ? this.options.autoIgnoreUserNoise\n : AUTO_IGNORE_USER_NOISE_PROMPT,\n skipAnswer: true,\n execute: (_input, agent) => agent.cancelLastUserMessage(),\n })\n }\n return tools\n }\n\n protected async executeTool(toolCall: MicdropConversationToolCall) {\n try {\n const tool = this.getTool(toolCall.toolName)\n if (!tool) {\n throw new Error(`Tool not found \"${toolCall.toolName}\"`)\n }\n\n this.log('Executing tool:', toolCall.toolName, toolCall.parameters)\n\n // Save tool call in conversation\n this.addToolMessage(toolCall)\n\n const parameters = JSON.parse(toolCall.parameters)\n const output = tool.execute ? await tool.execute(parameters, this) : {}\n\n // Save tool result in conversation\n this.addToolMessage({\n role: 'tool_result',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: JSON.stringify(output ?? null),\n })\n\n // Emit output\n if (tool.emitOutput) {\n this.emitAgentEvent('ToolCall', {\n name: toolCall.toolName,\n parameters,\n output,\n })\n }\n\n return {\n output,\n skipAnswer: tool.skipAnswer,\n }\n } catch (error: any) {\n console.error('[OpenaiAgent] Error executing tool:', error)\n return {\n output: {\n error: error.message,\n },\n }\n }\n }\n\n protected getExtractOptions(): ExtractTagOptions | undefined {\n const extract = this.options.extract\n if (!extract) return undefined\n if ('json' in extract && extract.json) {\n return { ...extract, startTag: '{', endTag: '}' }\n }\n if ('startTag' in extract && 'endTag' in extract) {\n return extract\n }\n return undefined\n }\n\n public extract(message: string) {\n const extractOptions = this.getExtractOptions()\n let metadata: MicdropAnswerMetadata | undefined = undefined\n\n // Extract value?\n if (extractOptions) {\n const startTagIndex = message.indexOf(extractOptions.startTag)\n if (startTagIndex !== -1) {\n // Find end tag\n let endTagIndex = message.lastIndexOf(extractOptions.endTag)\n if (endTagIndex === -1) endTagIndex = message.length + 1\n else endTagIndex += extractOptions.endTag.length\n const extractedText = message.slice(startTagIndex, endTagIndex).trim()\n\n // Parse extracted value\n try {\n const extractedValue =\n 'json' in extractOptions && extractOptions.json\n ? JSON.parse(extractedText)\n : extractedText\n\n // Call callback\n if (extractOptions.callback) {\n extractOptions.callback(extractedValue)\n }\n\n // Save in metadata\n if (extractOptions.saveInMetadata) {\n metadata = { extracted: extractedValue }\n }\n } catch (error) {\n console.error(\n `[OpenaiAgent] Error parsing extracted value (${extractedText}):`,\n error\n )\n }\n\n // Remove extracted value from message\n message = message.slice(0, startTagIndex).trimEnd()\n }\n }\n return { message, metadata }\n }\n\n /**\n * Emits one of the events every agent has.\n *\n * A subclass can add events of its own, and TypeScript then no longer knows\n * the shared ones are among them.\n */\n protected emitAgentEvent<Name extends keyof AgentEvents>(\n event: Name,\n ...args: AgentEvents[Name]\n ) {\n const emitter = this as unknown as EventEmitter<AgentEvents>\n emitter.emit(event, ...(args as any))\n }\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n destroy() {\n this.log('Destroyed')\n this.removeAllListeners()\n this.cancel()\n }\n}\n","import type { z } from 'zod'\nimport type { Agent } from './Agent'\n\nexport interface Tool<Schema extends z.ZodObject = z.ZodObject> {\n name: string\n description: string\n inputSchema?: Schema\n // The executing agent is passed as context so tools stay portable (no binding\n // to a specific agent instance), which lets them be shared between agents.\n execute?: (input: z.infer<Schema>, agent: Agent) => any | Promise<any>\n skipAnswer?: boolean\n emitOutput?: boolean\n}\n\nexport const AUTO_END_CALL_TOOL_NAME = 'end_call'\nexport const AUTO_END_CALL_PROMPT =\n 'Call this tool only if user asks to end the call'\n\nexport const AUTO_SEMANTIC_TURN_TOOL_NAME = 'semantic_turn'\nexport const AUTO_SEMANTIC_TURN_PROMPT =\n 'Call this tool only if last user message is obviously an incomplete sentence that you need to wait for the end before answering'\n\nexport const AUTO_IGNORE_USER_NOISE_TOOL_NAME = 'ignore_user_noise'\nexport const AUTO_IGNORE_USER_NOISE_PROMPT =\n 'Call this tool only if last user message is just an interjection or a sound that expresses emotion, hesitation, or reaction (ex: \"Uh\", \"Ahem\", \"Hmm\", \"Ah\") but doesn\\'t carry any clear meaning like agreeing, refusing, or commanding'\n","export class Logger {\n constructor(public name: string) {}\n\n log(...message: any[]) {\n const time = process.uptime().toFixed(3)\n console.log(`[${this.name} ${time}]`, ...message)\n }\n}\n","import { PassThrough } from 'stream'\nimport { Logger } from '../Logger'\nimport { MicdropConversationItem, MicdropToolCall } from '../types'\nimport { Agent } from './Agent'\n\nexport interface FallbackAgentOptions {\n factories: Array<() => Agent>\n}\n\nexport class FallbackAgent extends Agent {\n private agent: Agent | null = null\n private agentIndex = -1 // Start at -1 because we need to increment it before using it\n\n constructor(private readonly fallbackOptions: FallbackAgentOptions) {\n super({ systemPrompt: '' })\n if (this.fallbackOptions.factories.length === 0) {\n throw new Error('FallbackAgent: No factories provided')\n }\n this.startNextAgent()\n }\n\n // Delegate extraction to the active agent (extract config lives on children)\n extract(message: string) {\n return this.agent ? this.agent.extract(message) : super.extract(message)\n }\n\n protected async generateAnswer(stream: PassThrough): Promise<void> {\n // Try each agent once (one full rotation) until one answers successfully.\n // The conversation is shared between agents, so the next agent picks up\n // exactly where the failed one stopped.\n for (\n let attempt = 0;\n attempt < this.fallbackOptions.factories.length;\n attempt++\n ) {\n const agent = this.agent\n if (!agent) return\n\n let failed = false\n const onFailed = () => {\n failed = true\n }\n agent.once('Failed', onFailed)\n\n try {\n await this.pipeAnswer(agent, stream)\n } finally {\n agent.off('Failed', onFailed)\n }\n\n if (!failed) return\n\n this.log('Agent failed, trying next agent')\n this.startNextAgent()\n }\n\n // Every agent failed within this rotation: report it so an outer consumer\n // (e.g. a wrapping FallbackAgent) can react.\n this.log('All agents failed')\n this.emit('Failed')\n }\n\n cancel() {\n this.agent?.cancel()\n }\n\n destroy() {\n super.destroy()\n this.agent?.destroy()\n this.agent = null\n this.agentIndex = -1\n }\n\n // Run the child agent and forward its answer chunks to our own stream\n private pipeAnswer(agent: Agent, stream: PassThrough): Promise<void> {\n return new Promise((resolve) => {\n const answerStream = agent.answer()\n answerStream.on('data', (chunk) => {\n if (stream.writable) {\n stream.write(chunk)\n }\n })\n answerStream.on('end', resolve)\n answerStream.on('error', resolve)\n })\n }\n\n private startNextAgent() {\n this.agentIndex++\n if (this.agentIndex >= this.fallbackOptions.factories.length) {\n this.agentIndex = 0\n }\n\n const previousAgent = this.agent\n const isFirstAgent = previousAgent === null\n const agent = this.fallbackOptions.factories[this.agentIndex]()\n this.agent = agent\n\n // Share the conversation and tools between the fallback and the child agent.\n // Both are now portable (tools no longer bind to a specific instance), so a\n // single reference is shared, exactly like the conversation.\n if (isFirstAgent) {\n // Adopt the first agent's conversation and tools (keeps its system prompt)\n this.conversation = agent.conversation\n this.tools = agent.tools\n } else {\n // Keep the accumulated history, but use the new agent's system prompt\n if (agent.conversation[0]?.role === 'system') {\n this.conversation[0] = agent.conversation[0]\n }\n agent.conversation = this.conversation\n agent.tools = this.tools\n }\n\n // Forward events from the child agent\n agent.on('Message', this.onMessage)\n agent.on('CancelLastUserMessage', this.onCancelLastUserMessage)\n agent.on('SkipAnswer', this.onSkipAnswer)\n agent.on('EndCall', this.onEndCall)\n agent.on('ToolCall', this.onToolCall)\n\n // Destroy the previous agent (after moving the conversation over)\n previousAgent?.destroy()\n\n // Set logger after event loop\n setTimeout(() => {\n if (this.agent && this.logger) {\n this.agent.logger = new Logger(this.agent.constructor.name)\n }\n }, 0)\n }\n\n private onMessage = (message: MicdropConversationItem) => {\n this.emit('Message', message)\n }\n\n private onCancelLastUserMessage = () => {\n this.emit('CancelLastUserMessage')\n }\n\n private onSkipAnswer = () => {\n this.emit('SkipAnswer')\n }\n\n private onEndCall = () => {\n this.emit('EndCall')\n }\n\n private onToolCall = (toolCall: MicdropToolCall) => {\n this.emit('ToolCall', toolCall)\n }\n}\n","import { PassThrough } from 'stream'\nimport { Agent } from './Agent'\n\nexport class MockAgent extends Agent {\n private i = 0\n\n constructor() {\n super({ systemPrompt: '' })\n }\n\n protected async generateAnswer(stream: PassThrough): Promise<void> {\n const message = `Assistant Message ${this.i++}`\n this.addAssistantMessage(message)\n stream.write(message)\n }\n\n cancel() {}\n}\n","/**\n * Streaming linear-interpolation resampler for PCM16 mono audio.\n *\n * Works in both directions (up or downsampling). It is stateful: it handles\n * arbitrary byte boundaries (a network chunk can split a 16-bit sample) and\n * keeps the fractional sample position continuous across chunks, so feeding a\n * stream chunk by chunk yields the same result as resampling it in one go.\n *\n * Providers use it to bridge their own rate with the 16kHz PCM16 the Micdrop\n * client records and plays: OpenaiSTT (16kHz -> 24kHz, the GA Realtime API\n * requires >= 24kHz), OpenaiTTS and KokoroTTS (24kHz output -> 16kHz).\n */\nexport class Pcm16Resampler {\n private readonly step: number\n private leftover: Buffer<ArrayBufferLike> = Buffer.alloc(0)\n private pos = 0 // Fractional position into the first sample of the buffer\n\n constructor(inRate: number, outRate: number) {\n this.step = inRate / outRate\n }\n\n // Reset to the initial state, to resample a new independent stream\n // (e.g. resending buffered audio after a reconnection).\n reset() {\n this.leftover = Buffer.alloc(0)\n this.pos = 0\n }\n\n process(chunk: Buffer): Buffer {\n const buf = this.leftover.length\n ? Buffer.concat([this.leftover, chunk])\n : chunk\n const samples = Math.floor(buf.length / 2)\n\n // Need at least 2 samples to interpolate\n if (samples < 2) {\n this.leftover = buf\n return Buffer.alloc(0)\n }\n\n const out: number[] = []\n let p = this.pos\n while (Math.floor(p) + 1 < samples) {\n const i = Math.floor(p)\n const frac = p - i\n const s0 = buf.readInt16LE(i * 2)\n const s1 = buf.readInt16LE((i + 1) * 2)\n out.push(Math.round(s0 + (s1 - s0) * frac))\n p += this.step\n }\n\n // Keep the last still-needed sample (and any trailing odd byte) for the\n // next chunk, and carry the fractional position relative to it.\n const consumed = Math.floor(p)\n this.pos = p - consumed\n this.leftover = buf.subarray(consumed * 2)\n\n const result = Buffer.alloc(out.length * 2)\n for (let k = 0; k < out.length; k++) {\n result.writeInt16LE(out[k], k * 2)\n }\n return result\n }\n}\n","/**\n * Conversions between the PCM16 buffers exchanged with the Micdrop client and\n * the float samples that local speech models read and write.\n *\n * Both formats are mono. PCM16 is signed 16-bit little-endian, floats are in\n * the [-1, 1] range. Only the scale changes, the sample rate is left alone.\n */\n\nconst PCM16_MAX = 32767\nconst PCM16_MIN = -32768\n\n/** Turns float samples into a PCM16 buffer, clamping anything out of range. */\nexport function float32ToPcm16(samples: Float32Array): Buffer {\n const buffer = Buffer.alloc(samples.length * 2)\n for (let i = 0; i < samples.length; i++) {\n const scaled = Math.round(samples[i] * PCM16_MAX)\n const clamped = Math.max(PCM16_MIN, Math.min(PCM16_MAX, scaled))\n buffer.writeInt16LE(clamped, i * 2)\n }\n return buffer\n}\n\n/**\n * Turns a PCM16 buffer into float samples.\n *\n * A trailing odd byte is dropped: it is half of a sample whose other half has\n * not arrived, and a caller feeding whole utterances never produces one.\n */\nexport function pcm16ToFloat32(buffer: Buffer): Float32Array {\n const length = Math.floor(buffer.length / 2)\n const samples = new Float32Array(length)\n for (let i = 0; i < length; i++) {\n samples[i] = buffer.readInt16LE(i * 2) / PCM16_MAX\n }\n return samples\n}\n","import WebSocket from 'ws'\n\nexport enum MicdropErrorCode {\n BadRequest = 4400,\n Unauthorized = 4401,\n NotFound = 4404,\n}\n\nexport class MicdropError extends Error {\n code: number\n\n constructor(code: number, message: string) {\n super(message)\n this.code = code\n }\n}\n\nexport function handleError(socket: WebSocket, error: unknown) {\n if (error instanceof MicdropError) {\n socket.close(error.code, error.message)\n } else {\n console.error(error)\n socket.close(1011)\n }\n socket.terminate()\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { Duplex, PassThrough, Readable, Transform } from 'stream'\nimport { WebSocket } from 'ws'\nimport type { Agent } from './agent'\nimport { pcm16ToFloat32 } from './audio'\nimport { Logger } from './Logger'\nimport type { Realtime } from './realtime'\nimport type { STT } from './stt'\nimport type { TTS } from './tts'\nimport {\n MicdropAnswerMetadata,\n MicdropCallSummary,\n MicdropClientCommands,\n MicdropConversation,\n MicdropConversationItem,\n MicdropConversationMessage,\n MicdropServerCommands,\n TurnDetector,\n} from './types'\n\n/** Rate the client records at, and sends its chunks in */\nconst USER_SAMPLE_RATE = 16000\n\n/**\n * How long an answer waits once the detector asked for the rest of a sentence.\n *\n * The way out of a wrong verdict: a speaker who never comes back still gets an\n * answer instead of a call that goes quiet.\n */\nconst DEFAULT_TURN_MAX_WAIT = 4000 // ms\n\nexport interface MicdropServerEvents {\n End: [MicdropCallSummary]\n /** A message was added to the conversation, by the agent or by the server */\n Message: [MicdropConversationItem]\n UserAudio: [Buffer]\n AssistantAudio: [Buffer]\n}\n\nexport interface MicdropConfig {\n /**\n * Opening line, spoken before the user has said anything.\n *\n * It goes into the conversation like any other assistant message. Leaving it\n * out, along with `generateFirstMessage`, hands the first turn to the user.\n */\n firstMessage?: string\n\n /**\n * Asks the agent for its opening line instead of fixing one, so the greeting\n * can lean on the system prompt and on whatever the call was given.\n *\n * Ignored when `firstMessage` is set, and out of reach without an agent.\n */\n generateFirstMessage?: boolean\n\n /**\n * Generates the answers.\n *\n * Leaving it out turns the call into a one-way one: what the user says is\n * transcribed and sent to the client, and the assistant stays quiet unless\n * the application calls `speak()` itself.\n */\n agent?: Agent\n\n /**\n * Turns what the user says into text, the one component a call cannot do\n * without, unless a realtime model hears the user itself.\n *\n * It is fed the audio of each turn as it arrives, and the transcripts it\n * returns drive everything that follows.\n */\n stt?: STT\n\n /**\n * Gives the answers a voice.\n *\n * Leaving it out keeps the call textual: the answer reaches the client as a\n * message and nothing is synthesized.\n */\n tts?: TTS\n\n /**\n * Sends the answer to the client while it is still being written, on top of\n * the complete message that follows.\n *\n * Works with any agent, since the text is read from the stream it writes to.\n * Off by default, since a client that only displays finished messages has\n * nothing to do with it.\n */\n partialMessages?: boolean\n\n /**\n * Waits for the rest of the sentence when the speaker paused in the middle\n * of one, instead of answering an unfinished question.\n *\n * Prefer running the detector in the client, which reaches the same decision\n * without the round trip and can then close its turns sooner. This is the\n * option for the browsers where the model has nowhere to run.\n */\n turnDetector?: TurnDetector\n\n /** How long to wait for the rest of a sentence, 4000 ms by default */\n turnMaxWait?: number\n\n /**\n * Hears the user and answers with its own voice, in place of `stt`, `agent`\n * and `tts`, which are then ignored.\n *\n * The client does not change: it still detects when the user speaks, and the\n * turn it sends goes to the model as audio.\n */\n realtime?: Realtime\n}\n\nexport class MicdropServer extends EventEmitter<MicdropServerEvents> {\n public socket: WebSocket | null = null\n public config: MicdropConfig | null = null\n public logger?: Logger\n\n private startTime = Date.now()\n private lastMessageSpeeched?: MicdropConversationItem\n\n // Holds the conversation when no agent does, and keeps it readable once the\n // call is over and the config is gone\n private ownConversation: MicdropConversation = []\n\n // Queue system for operations\n private operationQueue: Array<() => Promise<void>> = []\n private isProcessingQueue = false\n\n // When user is speaking, we're streaming chunks for STT\n private currentUserStream?: Duplex\n private userSpeechChunks = 0\n // Asked as soon as the speaker pauses, so it weighs that stretch of audio\n // and not the next one\n private turnComplete?: Promise<boolean>\n private heldTurnTimer?: ReturnType<typeof setTimeout>\n\n constructor(socket: WebSocket, config: MicdropConfig) {\n super()\n this.socket = socket\n this.config = config\n this.log(`Call started`)\n\n const { realtime } = config\n if (realtime) {\n // Setup realtime model\n realtime.on('Audio', this.onAssistantAudio)\n if (config.partialMessages) {\n realtime.on('PartialMessage', (content) =>\n this.sendPartialAnswer(content)\n )\n }\n } else {\n if (!config.stt) {\n throw new Error('MicdropServer needs an stt, or a realtime model')\n }\n\n // Setup STT\n config.stt.on('Transcript', this.onTranscriptSTT)\n\n // Setup TTS\n config.tts?.on('Audio', this.onAssistantAudio)\n }\n\n // Setup agent, which a realtime model is too\n const agent = this.agent\n if (agent) {\n agent.on('Message', this.onMessageAgent)\n agent.on('CancelLastUserMessage', () =>\n this.socket?.send(MicdropServerCommands.CancelLastUserMessage)\n )\n agent.on('SkipAnswer', () =>\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n )\n agent.on('EndCall', () =>\n this.socket?.send(MicdropServerCommands.EndCall)\n )\n agent.on('ToolCall', (toolCall) =>\n this.socket?.send(\n `${MicdropServerCommands.ToolCall} ${JSON.stringify(toolCall)}`\n )\n )\n }\n\n // Assistant speaks first\n // Deferred so consumers (e.g. MicdropRecorder) can subscribe to the server\n // events before the first message is added to the conversation.\n queueMicrotask(() => this.sendFirstMessage())\n\n // Listen to events\n socket.on('close', this.onClose)\n socket.on('message', this.onMessage)\n }\n\n /** Everything said so far, whether an agent or the server keeps it */\n get conversation(): MicdropConversation {\n return this.agent?.conversation ?? this.ownConversation\n }\n\n /** What holds the conversation and answers, a realtime model included */\n private get agent(): Agent | undefined {\n if (!this.config) return undefined\n return this.config.realtime ?? this.config.agent\n }\n\n private log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n private async processQueue() {\n if (this.isProcessingQueue || this.operationQueue.length === 0) return\n\n this.isProcessingQueue = true\n\n while (this.operationQueue.length > 0) {\n const operation = this.operationQueue.shift()\n if (operation) {\n try {\n await operation()\n } catch (error) {\n this.log('Error processing queued operation:', error)\n }\n }\n }\n\n this.isProcessingQueue = false\n }\n\n private queueOperation(operation: () => Promise<void>) {\n this.operationQueue.push(operation)\n this.processQueue()\n }\n\n public cancel() {\n this.config?.tts?.cancel()\n this.agent?.cancel()\n // Clear the queue\n this.operationQueue = []\n }\n\n private onClose = () => {\n this.releaseHeldTurn()\n if (!this.config) return\n this.log('Connection closed')\n const duration = Math.round((Date.now() - this.startTime) / 1000)\n const conversation = this.conversation\n\n // Destroy instances\n this.agent?.destroy()\n this.config.stt?.destroy()\n this.config.tts?.destroy()\n\n // Emit End event\n this.emit('End', {\n conversation,\n duration,\n })\n\n // Unset params, keeping the conversation readable\n this.ownConversation = conversation\n this.socket = null\n this.config = null\n }\n\n private onMessage = async (message: Buffer) => {\n if (message.byteLength === 0) return\n if (!Buffer.isBuffer(message)) {\n this.log('Message is not a buffer')\n return\n }\n\n // Commands\n if (message.byteLength < 15) {\n const cmd = message.toString()\n this.log(`Command: ${cmd}`)\n\n if (cmd === MicdropClientCommands.StartSpeaking) {\n // User started speaking\n this.onStartSpeaking()\n } else if (cmd === MicdropClientCommands.Mute) {\n // User muted the call\n this.onMute()\n } else if (cmd === MicdropClientCommands.StopSpeaking) {\n // User stopped speaking\n this.onStopSpeaking()\n }\n }\n\n // Audio chunk\n else if (this.currentUserStream) {\n this.onUserAudio(message)\n }\n }\n\n private onUserAudio(chunk: Buffer) {\n this.log(`Received chunk (${chunk.byteLength} bytes)`)\n this.currentUserStream?.write(chunk)\n this.userSpeechChunks++\n this.config?.turnDetector?.push(pcm16ToFloat32(chunk), USER_SAMPLE_RATE)\n this.emit('UserAudio', chunk)\n }\n\n private onMute() {\n this.userSpeechChunks = 0\n this.currentUserStream?.end()\n this.currentUserStream = undefined\n this.cancel()\n this.config?.realtime?.discardTurn()\n }\n\n private onStartSpeaking() {\n if (!this.config) return\n this.userSpeechChunks = 0\n this.currentUserStream?.end()\n this.currentUserStream = new PassThrough()\n this.config.turnDetector?.reset()\n this.turnComplete = undefined\n // The rest of the sentence is arriving, so the deadline can go\n this.releaseHeldTurn()\n if (this.config.realtime) {\n this.cancel()\n this.config.realtime.startTurn(this.currentUserStream)\n } else {\n this.config.stt?.transcribe(this.currentUserStream)\n this.cancel()\n }\n }\n\n private onStopSpeaking() {\n const hasNoUserSpeech =\n !this.currentUserStream || this.userSpeechChunks === 0\n this.currentUserStream?.end()\n this.currentUserStream = undefined\n this.userSpeechChunks = 0\n\n // If user is not speaking or no chunks were received, skip\n if (hasNoUserSpeech) {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n return\n }\n\n // Weigh the stretch of audio that just ended, before the next one starts\n this.turnComplete = this.predictTurnComplete()\n\n // A realtime model answers the audio, with no transcript to wait for\n if (this.config?.realtime) {\n this.answerUserTurn()\n return\n }\n\n const conversation = this.conversation\n const lastMessage = conversation[conversation.length - 1]\n if (\n lastMessage?.role === 'user' &&\n this.lastMessageSpeeched !== lastMessage\n ) {\n this.log(\n 'User stopped speaking and a transcript already exists, answering'\n )\n this.answerUserTurn()\n }\n }\n\n private async predictTurnComplete(): Promise<boolean> {\n const detector = this.config?.turnDetector\n if (!detector) return true\n try {\n const { complete } = await detector.predict()\n this.log(`Turn sounds ${complete ? 'finished' : 'unfinished'}`)\n return complete\n } catch (error) {\n this.log(`Turn detection failed: ${error}`)\n return true\n }\n }\n\n /** Answers, unless the sentence sounds like it has more coming */\n private async answerUserTurn() {\n const complete = await (this.turnComplete ?? Promise.resolve(true))\n if (!complete) {\n this.log('Waiting for the rest of the sentence')\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n this.holdTurn()\n return\n }\n this.releaseHeldTurn()\n this.cancel()\n this.answerTurn()\n }\n\n /**\n * Answers anyway if the rest of the sentence never comes.\n *\n * Without it, a detector that hears an unfinished sentence where there is\n * none leaves the call silent for good.\n */\n private holdTurn() {\n this.releaseHeldTurn()\n this.heldTurnTimer = setTimeout(() => {\n this.heldTurnTimer = undefined\n this.log('Nothing more came, answering')\n this.cancel()\n this.answerTurn()\n }, this.config?.turnMaxWait ?? DEFAULT_TURN_MAX_WAIT)\n }\n\n /** Answers what the user just said */\n private answerTurn() {\n const realtime = this.config?.realtime\n if (realtime) {\n realtime.endTurn()\n } else {\n this.answer()\n }\n }\n\n private releaseHeldTurn() {\n if (!this.heldTurnTimer) return\n clearTimeout(this.heldTurnTimer)\n this.heldTurnTimer = undefined\n }\n\n private onTranscriptSTT = async (transcript: string) => {\n if (!this.config) return\n\n // Skip answer if transcript is empty\n if (transcript === '') {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n return\n }\n\n this.log(`User transcript: \"${transcript}\"`)\n this.addUserMessage(transcript)\n\n // Answer if user stopped speaking\n if (!this.currentUserStream) {\n this.log('User stopped speaking, answering')\n this.answerUserTurn()\n }\n }\n\n private onAssistantAudio = (audio: Buffer) => {\n if (!this.socket) return\n this.log(`Send audio chunk (${audio.byteLength} bytes)`)\n this.socket.send(audio)\n this.emit('AssistantAudio', audio)\n }\n\n private onMessageAgent = (message: MicdropConversationItem) => {\n this.sendMessage(message)\n }\n\n /** Adds a message to the conversation and sends it to the client */\n public addUserMessage(text: string, metadata?: MicdropAnswerMetadata) {\n if (this.agent) {\n this.agent.addUserMessage(text, metadata)\n } else {\n this.addOwnMessage('user', text, metadata)\n }\n }\n\n /** Adds a message to the conversation and sends it to the client */\n public addAssistantMessage(text: string, metadata?: MicdropAnswerMetadata) {\n if (this.agent) {\n this.agent.addAssistantMessage(text, metadata)\n } else {\n this.addOwnMessage('assistant', text, metadata)\n }\n }\n\n private addOwnMessage(\n role: 'user' | 'assistant',\n text: string,\n metadata?: MicdropAnswerMetadata\n ) {\n if (text.trim() === '') {\n this.log(`Skipping empty ${role} message`)\n return\n }\n this.log(`Adding ${role} message to conversation: ${text}`)\n const message: MicdropConversationMessage = {\n role,\n content: text,\n metadata,\n }\n this.ownConversation.push(message)\n this.sendMessage(message)\n }\n\n private sendMessage(message: MicdropConversationItem) {\n this.socket?.send(\n `${MicdropServerCommands.Message} ${JSON.stringify(message)}`\n )\n this.emit('Message', message)\n }\n\n private sendPartialAnswer(content: string) {\n if (!this.socket || content === '') return\n this.log(`Send partial answer: \"${content}\"`)\n this.socket.send(\n `${MicdropServerCommands.PartialAssistantMessage} ${JSON.stringify(content)}`\n )\n }\n\n private sendFirstMessage() {\n if (!this.config) return\n if (this.config.firstMessage) {\n // Send first message\n // Without an agent, speak() is the one adding it to the conversation\n this.agent?.addAssistantMessage(this.config.firstMessage)\n this.speak(this.config.firstMessage)\n } else if (this.config.generateFirstMessage && this.agent) {\n // Generate first message\n this.answer()\n } else {\n // Skip answer if no first message is provided\n // to avoid keeping the client in a processing state\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n }\n }\n\n public answer() {\n this.queueOperation(async () => {\n await this._answer()\n })\n }\n\n private async _answer() {\n if (!this.config) return\n\n // A realtime model speaks its answer itself\n if (this.config.realtime) {\n this.config.realtime.respond()\n return\n }\n\n // Nothing generates an answer, hand the turn back to the user\n if (!this.config.agent) {\n this.log('No agent, skipping answer')\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n return\n }\n\n // Prevent answering twice\n const conversation = this.config.agent.conversation\n const lastMessage = conversation[conversation.length - 1]\n if (this.lastMessageSpeeched === lastMessage) {\n this.log('Already answered, skipping')\n return\n }\n this.lastMessageSpeeched = lastMessage\n\n try {\n // LLM: Generate answer\n const stream = this.config.agent.answer()\n\n // TTS: Generate answer audio, unless there is nothing to say.\n //\n // An answer can be skipped after the fact: a tool with skipAnswer, or an\n // onBeforeAnswer hook returning true, ends the stream without a word in\n // it. Handing that empty stream to the TTS opens a synthesis request for\n // nothing, and a provider that stamps each request (Gradium multiplexes\n // this way) then drops the audio of the sentence still playing, so a\n // skipped answer cuts the assistant off mid-word.\n if (await hasContent(stream)) {\n await this._speak(\n this.config.partialMessages\n ? this.forwardPartialAnswer(stream)\n : stream\n )\n } else {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n }\n } catch (error) {\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n throw error\n }\n }\n\n /** Sends the answer to the client as it is written, ahead of the voice */\n private forwardPartialAnswer(stream: Readable): Readable {\n let content = ''\n const send = (text: string) => this.sendPartialAnswer(text)\n const forward = new Transform({\n transform(chunk, _encoding, callback) {\n content += chunk.toString()\n send(content)\n callback(null, chunk)\n },\n })\n return stream.pipe(forward)\n }\n\n // Run text-to-speech and send to client\n public speak(message: string | Readable) {\n this.queueOperation(async () => {\n await this._speak(message)\n })\n }\n\n private async _speak(message: string | Readable) {\n if (!this.socket || !this.config) return\n\n // A realtime model reads the text in its own voice\n if (this.config.realtime) {\n const text =\n typeof message === 'string' ? message : await readText(message)\n if (text.trim() !== '') this.config.realtime.say(text)\n return\n }\n\n // Convert message to stream if needed\n let textStream: Readable\n if (typeof message === 'string') {\n // Without an agent, nothing else records what the assistant says\n if (!this.config.agent) {\n this.addOwnMessage('assistant', message)\n }\n const stream = new PassThrough()\n stream.write(message)\n stream.end()\n textStream = stream\n } else {\n textStream = message\n }\n\n // Run TTS\n if (this.config.tts) {\n this.config.tts.speak(textStream)\n return\n }\n\n // No voice to synthesize: read the answer to the end, then hand the turn\n // back, since it is the first audio chunk that normally tells the client\n // it can stop waiting.\n await drain(textStream)\n this.socket?.send(MicdropServerCommands.SkipAnswer)\n }\n}\n\n/**\n * Resolves true as soon as the stream holds something to read, false if it ends\n * without ever carrying anything.\n *\n * The chunk read to find out is put back, so the consumer that follows sees the\n * whole stream from its first byte.\n */\nfunction hasContent(stream: Readable): Promise<boolean> {\n return new Promise((resolve) => {\n const onReadable = () => {\n const chunk = stream.read()\n if (chunk === null) return\n stream.unshift(chunk)\n done(true)\n }\n const onEnd = () => done(false)\n const done = (result: boolean) => {\n stream.off('readable', onReadable)\n stream.off('end', onEnd)\n resolve(result)\n }\n stream.on('readable', onReadable)\n stream.on('end', onEnd)\n })\n}\n\n/** Reads a text stream to its end */\nfunction readText(stream: Readable): Promise<string> {\n return new Promise((resolve) => {\n let text = ''\n stream.on('data', (chunk) => (text += chunk.toString()))\n stream.once('end', () => resolve(text))\n stream.once('error', () => resolve(text))\n })\n}\n\n/** Reads a stream to its end, for when nothing else consumes it */\nfunction drain(stream: Readable): Promise<void> {\n return new Promise((resolve) => {\n stream.on('data', () => {})\n stream.once('end', () => resolve())\n stream.once('error', () => resolve())\n stream.once('close', () => resolve())\n })\n}\n","export enum MicdropClientCommands {\n StartSpeaking = 'StartSpeaking',\n StopSpeaking = 'StopSpeaking',\n Mute = 'Mute',\n}\n\nexport enum MicdropServerCommands {\n Message = 'Message',\n PartialAssistantMessage = 'PartialAssistantMessage',\n CancelLastUserMessage = 'CancelLastUserMessage',\n SkipAnswer = 'SkipAnswer',\n EndCall = 'EndCall',\n ToolCall = 'ToolCall',\n}\n\n/**\n * Hears whether a sentence has landed, where voice activity detection only\n * hears whether someone is speaking.\n *\n * `SmartTurn` from `@micdrop/smart-turn` implements it, and so can anything\n * else, a call to a service included. Both sides of a call can hold one, the\n * client to decide when its turn ends and the server to decide when to answer.\n */\nexport interface TurnDetector {\n /**\n * Feeds the audio received since the last call\n * @param samples - Mono samples, in the -1..1 range\n * @param sampleRate - Sample rate of `samples`, in Hz\n */\n push(samples: Float32Array, sampleRate?: number): void\n\n /** Answers whether the turn pushed so far sounds finished */\n predict(): Promise<{ complete: boolean }>\n\n /** Starts a new turn, forgetting the previous one */\n reset(): void\n}\n\nexport interface MicdropCallSummary {\n conversation: MicdropConversation\n duration: number\n}\n\nexport type MicdropConversationItem =\n | MicdropConversationMessage\n | MicdropConversationToolCall\n | MicdropConversationToolResult\n\nexport type MicdropConversation = Array<MicdropConversationItem>\n\nexport type MicdropAnswerMetadata = {\n [key: string]: any\n}\n\nexport interface MicdropConversationMessage<\n Data extends MicdropAnswerMetadata = MicdropAnswerMetadata,\n> {\n role: 'system' | 'user' | 'assistant'\n content: string\n metadata?: Data\n}\n\nexport interface MicdropConversationToolCall {\n role: 'tool_call'\n toolCallId: string\n toolName: string\n parameters: string\n}\n\nexport interface MicdropConversationToolResult {\n role: 'tool_result'\n toolCallId: string\n toolName: string\n output: string\n}\n\nexport interface MicdropToolCall {\n name: string\n parameters: any\n output: any\n}\n\nexport type DeepPartial<T> = T extends object\n ? {\n [P in keyof T]?: DeepPartial<T[P]>\n }\n : T\n","import { PassThrough, Readable } from 'stream'\nimport type { z } from 'zod'\nimport { Agent, AgentEvents, AgentOptions, Tool } from '../agent'\nimport { MicdropAnswerMetadata, MicdropConversationToolCall } from '../types'\n\nexport type RealtimeOptions = Pick<\n AgentOptions,\n 'systemPrompt' | 'autoEndCall' | 'autoSemanticTurn' | 'autoIgnoreUserNoise'\n>\n\nexport interface RealtimeEvents extends AgentEvents {\n /** Voice of the assistant, in PCM16 at 16 kHz */\n Audio: [Buffer]\n /** What the assistant has said so far in the answer it is giving */\n PartialMessage: [string]\n}\n\n/**\n * How long the answer waits for the transcript of the turn it answers.\n *\n * Transcription runs apart from the answer and can land after it, while the\n * conversation has to read the question before the answer. Past this delay the\n * answer is recorded anyway, and a transcript arriving later follows it.\n */\nconst USER_TRANSCRIPT_TIMEOUT = 3000 // ms\n\ninterface RealtimeAnswer {\n transcript: string\n hasAudio: boolean\n /** Off for text the application records itself, see `say()` */\n record: boolean\n /** The model spoke before a tool call, and its next words start anew */\n resumed: boolean\n}\n\nfunction newAnswer(record: boolean): RealtimeAnswer {\n return { transcript: '', hasAudio: false, record, resumed: false }\n}\n\n/**\n * A model that hears the user and answers with its own voice, standing in for\n * the speech to text, the agent and the text to speech of a call.\n *\n * It is an agent: it holds the conversation and the tools, and emits the same\n * events. What differs is what goes in and out, audio in turns rather than\n * transcripts, and audio rather than text.\n *\n * This class keeps what every provider shares: when a turn opens, which\n * answer the output belongs to, dropping what arrives after an interruption,\n * and writing the conversation in the order it was spoken. A provider\n * implements the protected methods that talk to its API.\n */\nexport abstract class Realtime<\n Options extends RealtimeOptions = RealtimeOptions,\n> extends Agent<Options, RealtimeEvents> {\n // A turn opens with its first audio, so a blip without any sends nothing\n private turn: 'none' | 'pending' | 'open' = 'none'\n private currentAnswer?: RealtimeAnswer\n // Output of an interrupted answer can still be on its way\n private discarding = false\n // Answers asked for while another one was being given\n private queue: Array<() => void> = []\n private answerWaiters: Array<() => void> = []\n\n private userTranscript = ''\n private awaitingUserTranscript = false\n private userTranscriptTimer?: ReturnType<typeof setTimeout>\n private afterUserTranscript: Array<() => void> = []\n\n /**\n * The user started speaking.\n *\n * The assistant stops, and the audio read from the stream goes to the model.\n * A turn left open, because the sentence sounded unfinished, carries on.\n */\n startTurn(audio: Readable) {\n this.cancel()\n if (this.turn === 'none') this.turn = 'pending'\n audio.on('data', (chunk: Buffer) => this.sendAudio(chunk))\n }\n\n /** Closes the turn of the user, and has the model answer it */\n endTurn() {\n if (this.turn !== 'open') return\n this.turn = 'none'\n this.awaitUserTranscript()\n this.beginAnswer(true)\n this.closeTurn()\n }\n\n /** Drops the turn of the user, without an answer */\n discardTurn() {\n this.cancel()\n if (this.turn === 'open') {\n this.clearTurn()\n // A model that cannot drop audio answers it, and nobody wants to hear it\n this.discarding = true\n }\n this.turn = 'none'\n }\n\n /** Has the model speak without waiting for the user, to open a call */\n respond() {\n this.whenIdle(() => {\n this.beginAnswer(true)\n this.generate()\n })\n }\n\n /**\n * Has the model say this text, word for word.\n *\n * The caller records the text in the conversation, as with a text to speech,\n * so the transcript of what the model says is not recorded again.\n */\n say(text: string) {\n this.whenIdle(() => {\n this.beginAnswer(false)\n this.speakText(text)\n })\n }\n\n cancel() {\n this.queue = []\n const answer = this.currentAnswer\n if (!answer) return\n this.log('Cancel')\n this.currentAnswer = undefined\n this.discarding = true\n this.cancelAnswer()\n this.recordAnswer(answer)\n this.resolveAnswerWaiters()\n }\n\n /**\n * Adds a message the model did not hear, and tells the model about it.\n *\n * The transcripts of the call are recorded apart, so they are not sent back.\n */\n addMessage(\n role: 'user' | 'assistant' | 'system',\n text: string,\n metadata?: MicdropAnswerMetadata\n ) {\n super.addMessage(role, text, metadata)\n if (text.trim() !== '') this.injectMessage(role, text)\n }\n\n addTool<Schema extends z.ZodObject>(tool: Tool<Schema>) {\n super.addTool(tool)\n this.updateTools()\n }\n\n removeTool(name: string) {\n super.removeTool(name)\n this.updateTools()\n }\n\n /**\n * Lists the events that have listeners.\n *\n * Typed as the events of any agent, so a realtime model goes wherever an\n * agent is expected. The two extra events do not change what that code reads.\n */\n eventNames() {\n return super.eventNames() as Array<keyof AgentEvents>\n }\n\n destroy() {\n super.destroy()\n this.clearUserTranscriptTimer()\n this.afterUserTranscript = []\n this.resolveAnswerWaiters()\n }\n\n protected generateAnswer(stream: PassThrough): Promise<void> {\n let written = 0\n const onPartial = (transcript: string) => {\n stream.write(transcript.slice(written))\n written = transcript.length\n }\n this.on('PartialMessage', onPartial)\n this.respond()\n return new Promise((resolve) => {\n this.answerWaiters.push(() => {\n this.off('PartialMessage', onPartial)\n resolve()\n })\n })\n }\n\n /** Sends audio of the user, PCM16 at 16 kHz */\n protected abstract appendAudio(chunk: Buffer): void\n\n /** Opens a turn of the user, right before its first audio */\n protected abstract openTurn(): void\n\n /** Closes the turn of the user, which has the model answer it */\n protected abstract closeTurn(): void\n\n /** Drops the audio of the open turn, without an answer */\n protected abstract clearTurn(): void\n\n /** Has the model speak with no turn of the user to answer */\n protected abstract generate(): void\n\n /** Has the model say this text, word for word */\n protected abstract speakText(text: string): void\n\n /** Stops the answer the model is giving */\n protected abstract cancelAnswer(): void\n\n /** Sends the tools to the model again, after one was added or removed */\n protected abstract updateTools(): void\n\n /** Tells the model about a message it did not hear */\n protected abstract injectMessage(\n role: 'user' | 'assistant' | 'system',\n text: string\n ): void\n\n /** Nobody is speaking, nor about to, so the connection can be renewed */\n protected get isIdle(): boolean {\n return this.turn === 'none' && !this.currentAnswer\n }\n\n /** Voice of the answer, PCM16 at 16 kHz */\n protected emitAudio(chunk: Buffer) {\n const answer = this.activeAnswer()\n if (!answer) return\n answer.hasAudio = true\n this.emit('Audio', chunk)\n }\n\n /** Transcript of the answer, as it is spoken */\n protected addAnswerTranscript(text: string) {\n const answer = this.activeAnswer()\n if (!answer) return\n if (answer.resumed) {\n answer.resumed = false\n if (answer.transcript !== '' && !/\\s$/.test(answer.transcript)) {\n text = ` ${text.trimStart()}`\n }\n }\n answer.transcript += text\n if (answer.record) this.emit('PartialMessage', answer.transcript)\n }\n\n /**\n * The model finished its answer, or finished dropping an interrupted one.\n *\n * An answer without audio tells the client to stop waiting for one.\n */\n protected endAnswer() {\n if (this.discarding) {\n this.discarding = false\n return\n }\n const answer = this.currentAnswer\n if (!answer) return\n this.currentAnswer = undefined\n if (!answer.hasAudio) this.emitAgentEvent('SkipAnswer')\n this.recordAnswer(answer)\n this.resolveAnswerWaiters()\n this.queue.shift()?.()\n }\n\n /** Runs a tool the model called, and records it in the conversation */\n protected async runTool(toolCall: MicdropConversationToolCall) {\n const result = await this.executeTool(toolCall)\n if (this.currentAnswer) this.currentAnswer.resumed = true\n return result\n }\n\n /** Transcript of the turn of the user, possibly in several pieces */\n protected addUserTranscript(text: string) {\n this.userTranscript += text\n }\n\n /** The transcript of the turn is complete, it goes in the conversation */\n protected commitUserTranscript() {\n this.clearUserTranscriptTimer()\n this.awaitingUserTranscript = false\n const text = this.userTranscript.trim()\n this.userTranscript = ''\n if (text !== '') super.addMessage('user', text)\n const pending = this.afterUserTranscript\n this.afterUserTranscript = []\n pending.forEach((record) => record())\n }\n\n private sendAudio(chunk: Buffer) {\n if (this.turn === 'none') return\n if (this.turn === 'pending') {\n this.turn = 'open'\n this.openTurn()\n }\n this.appendAudio(chunk)\n }\n\n private beginAnswer(record: boolean) {\n this.discarding = false\n this.currentAnswer = newAnswer(record)\n }\n\n /**\n * The answer output belongs to, if it is still wanted.\n *\n * The model can speak without being asked, to follow up on a tool call for\n * instance, which opens an answer of its own.\n */\n private activeAnswer(): RealtimeAnswer | undefined {\n if (this.discarding) return undefined\n if (!this.currentAnswer) this.currentAnswer = newAnswer(true)\n return this.currentAnswer\n }\n\n private whenIdle(start: () => void) {\n if (this.currentAnswer) {\n this.queue.push(start)\n } else {\n start()\n }\n }\n\n private recordAnswer(answer: RealtimeAnswer) {\n if (!answer.record) return\n const record = () => super.addMessage('assistant', answer.transcript.trim())\n if (this.awaitingUserTranscript) {\n this.afterUserTranscript.push(record)\n } else {\n record()\n }\n }\n\n private awaitUserTranscript() {\n this.awaitingUserTranscript = true\n this.clearUserTranscriptTimer()\n this.userTranscriptTimer = setTimeout(() => {\n this.log('No transcript of the turn, recording the answer anyway')\n this.commitUserTranscript()\n }, USER_TRANSCRIPT_TIMEOUT)\n }\n\n private clearUserTranscriptTimer() {\n if (!this.userTranscriptTimer) return\n clearTimeout(this.userTranscriptTimer)\n this.userTranscriptTimer = undefined\n }\n\n private resolveAnswerWaiters() {\n const waiters = this.answerWaiters\n this.answerWaiters = []\n waiters.forEach((resolve) => resolve())\n }\n}\n","import { EventEmitter } from 'eventemitter3'\nimport type { MicdropServer } from '../MicdropServer'\nimport type { MicdropConversationItem } from '../types'\nimport { Logger } from '../Logger'\n\nexport interface AudioMessage {\n buffer: Buffer\n messageIndex: number\n message: string\n role: 'user' | 'assistant'\n}\n\nexport interface MicdropRecorderEvents {\n AudioMessage: [AudioMessage]\n Complete: [AudioMessage[]]\n}\n\nexport class MicdropRecorder extends EventEmitter<MicdropRecorderEvents> {\n public logger?: Logger\n\n private audioMessages: AudioMessage[] = []\n private currentUserChunks: Buffer[] = []\n private currentAssistantChunks: Buffer[] = []\n private lastUserMessageIndex: number = -1\n private lastAssistantMessageIndex: number = -1\n\n constructor(private server: MicdropServer) {\n super()\n this.setupListeners()\n }\n\n private setupListeners() {\n // Listen to audio events from server\n this.server.on('UserAudio', this.onUserAudio)\n this.server.on('AssistantAudio', this.onAssistantAudio)\n this.server.on('End', this.onEnd)\n this.server.on('Message', this.onMessage)\n }\n\n private onUserAudio = (chunk: Buffer) => {\n // Finalize or discard assistant audio when user starts speaking\n if (this.currentAssistantChunks.length > 0) {\n if (this.lastAssistantMessageIndex >= 0) {\n this.finalizeAssistantAudio()\n } else {\n // Discard orphaned chunks (no associated message)\n this.log('Discarding orphaned assistant audio chunks')\n this.currentAssistantChunks = []\n }\n }\n\n this.log('Recording user audio chunk')\n this.currentUserChunks.push(chunk)\n }\n\n private onAssistantAudio = (chunk: Buffer) => {\n // Finalize or discard user audio when assistant starts speaking\n if (this.currentUserChunks.length > 0) {\n if (this.lastUserMessageIndex >= 0) {\n this.finalizeUserAudio()\n } else {\n // Discard orphaned chunks (no associated message)\n this.log('Discarding orphaned user audio chunks')\n this.currentUserChunks = []\n }\n }\n\n this.log('Recording assistant audio chunk')\n this.currentAssistantChunks.push(chunk)\n }\n\n private onMessage = (message: MicdropConversationItem) => {\n const messageIndex = this.server.conversation.length - 1\n\n if (message.role === 'user') {\n this.lastUserMessageIndex = messageIndex\n // User audio might already be complete, finalize if we have chunks\n // Audio chunks arrive BEFORE message, so we finalize when we know the message\n if (this.currentUserChunks.length > 0) {\n this.finalizeUserAudio()\n }\n } else if (message.role === 'assistant') {\n this.lastAssistantMessageIndex = messageIndex\n // Don't finalize assistant audio here - chunks can still arrive after message\n }\n }\n\n private finalizeUserAudio() {\n if (this.currentUserChunks.length === 0) return\n if (this.lastUserMessageIndex < 0) return\n\n const message = this.server.conversation[this.lastUserMessageIndex]\n if (!message) return\n const buffer = Buffer.concat(this.currentUserChunks)\n\n const audioMessage: AudioMessage = {\n buffer,\n messageIndex: this.lastUserMessageIndex,\n message: 'content' in message ? message.content : '',\n role: 'user',\n }\n\n this.log(\n `Finalized user audio: ${buffer.length} bytes, message index ${this.lastUserMessageIndex}`\n )\n this.audioMessages.push(audioMessage)\n this.emit('AudioMessage', audioMessage)\n\n // Reset\n this.currentUserChunks = []\n this.lastUserMessageIndex = -1\n }\n\n private finalizeAssistantAudio() {\n if (this.currentAssistantChunks.length === 0) return\n if (this.lastAssistantMessageIndex < 0) return\n\n const message = this.server.conversation[this.lastAssistantMessageIndex]\n if (!message) return\n const buffer = Buffer.concat(this.currentAssistantChunks)\n\n const audioMessage: AudioMessage = {\n buffer,\n messageIndex: this.lastAssistantMessageIndex,\n message: 'content' in message ? message.content : '',\n role: 'assistant',\n }\n\n this.log(\n `Finalized assistant audio: ${buffer.length} bytes, message index ${this.lastAssistantMessageIndex}`\n )\n this.audioMessages.push(audioMessage)\n this.emit('AudioMessage', audioMessage)\n\n // Reset\n this.currentAssistantChunks = []\n this.lastAssistantMessageIndex = -1\n }\n\n private onEnd = () => {\n // Finalize any remaining audio\n if (this.currentUserChunks.length > 0) {\n this.finalizeUserAudio()\n }\n if (this.currentAssistantChunks.length > 0) {\n this.finalizeAssistantAudio()\n }\n\n this.log(`Recording complete: ${this.audioMessages.length} audio messages`)\n this.emit('Complete', this.audioMessages)\n }\n\n public getAudioMessages(): AudioMessage[] {\n return [...this.audioMessages]\n }\n\n public destroy() {\n this.log('Destroyed')\n this.server.off('UserAudio', this.onUserAudio)\n this.server.off('AssistantAudio', this.onAssistantAudio)\n this.server.off('End', this.onEnd)\n this.server.off('Message', this.onMessage)\n\n this.removeAllListeners()\n }\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n}\n","/**\n * Keeps one loaded instance per configuration, shared by every call.\n *\n * A local model costs from a few hundred milliseconds to a minute to load, and\n * holding a second copy of the same weights wastes the memory the rest of the\n * pipeline needs. Inference is bound to a few threads anyway, so two calls\n * generating at once compete for the same cores whether they share the\n * instance or not.\n *\n * Instances are keyed by the options they were built from, so changing a model\n * or a device loads that one and leaves the others alone. They stay loaded for\n * the lifetime of the process.\n *\n * ```typescript\n * const synthesizers = new SharedInstances<Synthesizer>()\n *\n * function loadSynthesizer(options: Options) {\n * return synthesizers.load([options.model, options.device], () =>\n * Synthesizer.from_pretrained(options.model, { device: options.device })\n * )\n * }\n * ```\n */\nexport class SharedInstances<T> {\n private entries = new Map<string, Promise<T>>()\n\n /**\n * Returns the instance for this key, calling `create` the first time.\n *\n * The key can be any JSON-serializable value, usually an array of the\n * options that define the instance. Concurrent callers get the same promise\n * instead of loading the model twice.\n */\n load(key: unknown, create: () => Promise<T>): Promise<T> {\n const id = typeof key === 'string' ? key : JSON.stringify(key)\n const existing = this.entries.get(id)\n if (existing) return existing\n\n let loading: Promise<T>\n try {\n loading = create()\n } catch (error) {\n // Report a synchronous throw as a rejection, without keeping an entry\n return Promise.reject(error)\n }\n\n // A failed download must not poison the cache, the next call retries it\n loading.catch(() => {\n if (this.entries.get(id) === loading) this.entries.delete(id)\n })\n\n this.entries.set(id, loading)\n return loading\n }\n\n /** Whether an instance is loaded, or being loaded, for this key. */\n has(key: unknown): boolean {\n return this.entries.has(typeof key === 'string' ? key : JSON.stringify(key))\n }\n\n /** Drops every instance, so the next call loads again. */\n clear(): void {\n this.entries.clear()\n }\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { Readable } from 'stream'\nimport { Logger } from '../Logger'\n\nexport interface STTEvents {\n Transcript: [string]\n Failed: [Buffer[]]\n}\n\nexport abstract class STT extends EventEmitter<STTEvents> {\n public logger?: Logger\n\n // Set stream of audio to transcribe\n abstract transcribe(audioStream: Readable): void\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n destroy() {\n this.log('Destroyed')\n this.removeAllListeners()\n }\n}\n","import { STT } from './STT'\n\nexport class MockSTT extends STT {\n private i = 0\n\n async transcribe() {\n setTimeout(() => {\n this.emit('Transcript', `User Message ${this.i++}`)\n }, 300)\n }\n}\n","import { PassThrough, Readable } from 'stream'\nimport { STT } from './STT'\nimport { Logger } from '..'\n\nexport interface FallbackSTTOptions {\n factories: Array<() => STT>\n}\n\nexport class FallbackSTT extends STT {\n private stt: STT | null = null\n private sttIndex = -1 // Start at -1 because we need to increment it before using it\n\n constructor(private readonly options: FallbackSTTOptions) {\n super()\n if (this.options.factories.length === 0) {\n throw new Error('FallbackSTT: No factories provided')\n }\n this.startNextSTT()\n }\n\n transcribe(audioStream: Readable) {\n this.stt?.transcribe(audioStream)\n }\n\n destroy() {\n super.destroy()\n this.stt?.destroy()\n this.stt = null\n this.sttIndex = -1\n }\n\n private startNextSTT() {\n this.sttIndex++\n if (this.sttIndex >= this.options.factories.length) {\n this.sttIndex = 0\n }\n this.stt?.destroy()\n this.stt = this.options.factories[this.sttIndex]()\n this.stt.on('Transcript', this.onTranscript)\n this.stt.on('Failed', this.onFailed)\n\n // Set logger after event loop\n setTimeout(() => {\n if (this.stt && this.logger) {\n this.stt.logger = new Logger(this.stt.constructor.name)\n }\n }, 0)\n }\n\n private onTranscript = (transcript: string) => {\n this.emit('Transcript', transcript)\n }\n\n private onFailed = (chunks: Buffer[]) => {\n this.log('STT failed, trying next STT')\n this.startNextSTT()\n\n if (chunks.length > 0) {\n this.log('Sending audio chunks again')\n const stream = new PassThrough()\n this.stt?.transcribe(stream)\n chunks.forEach((chunk) => stream.write(chunk))\n stream.end()\n }\n }\n}\n","import { PassThrough, Readable } from 'stream'\nimport { TTS } from './TTS'\nimport { Logger } from '..'\n\nexport interface FallbackTTSOptions {\n factories: Array<() => TTS>\n}\n\nexport class FallbackTTS extends TTS {\n private tts: TTS | null = null\n private ttsIndex = -1 // Start at -1 because we need to increment it before using it\n\n constructor(private readonly options: FallbackTTSOptions) {\n super()\n if (this.options.factories.length === 0) {\n throw new Error('FallbackTTS: No factories provided')\n }\n this.startNextTTS()\n }\n\n speak(textStream: Readable) {\n this.tts?.speak(textStream)\n }\n\n cancel() {\n this.tts?.cancel()\n }\n\n destroy() {\n super.destroy()\n this.tts?.destroy()\n this.tts = null\n this.ttsIndex = -1\n }\n\n private startNextTTS() {\n this.ttsIndex++\n if (this.ttsIndex >= this.options.factories.length) {\n this.ttsIndex = 0\n }\n this.tts?.destroy()\n this.tts = this.options.factories[this.ttsIndex]()\n this.tts.on('Audio', this.onAudio)\n this.tts.on('Failed', this.onFailed)\n\n // Set logger after event loop\n setTimeout(() => {\n if (this.tts && this.logger) {\n this.tts.logger = new Logger(this.tts.constructor.name)\n }\n }, 0)\n }\n\n private onAudio = (audio: Buffer) => {\n this.emit('Audio', audio)\n }\n\n private onFailed = (chunks: string[]) => {\n this.log('TTS failed, trying next TTS')\n this.startNextTTS()\n\n if (chunks.length > 0) {\n this.log('Sending text chunks again')\n const stream = new PassThrough()\n this.tts?.speak(stream)\n chunks.forEach((chunk) => stream.write(chunk))\n stream.end()\n }\n }\n}\n","import { EventEmitter } from 'eventemitter3'\nimport { Readable } from 'stream'\nimport { Logger } from '../Logger'\n\nexport interface TTSEvents {\n Audio: [Buffer]\n Failed: [string[]]\n}\n\nexport abstract class TTS extends EventEmitter<TTSEvents> {\n public logger?: Logger\n\n abstract speak(textStream: Readable): void\n abstract cancel(): void\n\n protected log(...message: any[]) {\n this.logger?.log(...message)\n }\n\n destroy() {\n this.log('Destroyed')\n this.cancel()\n }\n}\n","import * as fs from 'fs'\nimport { PassThrough, Readable } from 'stream'\nimport { TTS } from './TTS'\n\nexport class MockTTS extends TTS {\n constructor(private audioFilePaths: string[]) {\n super()\n }\n\n speak(textStream: Readable) {\n const audioStream = new PassThrough()\n textStream.once('data', async () => {\n for (const filePath of this.audioFilePaths) {\n await new Promise((resolve) => setTimeout(resolve, 200))\n const audioBuffer = fs.readFileSync(filePath)\n this.log(`Loaded chunk (${audioBuffer.length} bytes)`)\n audioStream.write(audioBuffer)\n }\n audioStream.end()\n })\n return audioStream\n }\n\n cancel() {}\n}\n","/**\n * Cuts a stream of text into sentences as it arrives.\n *\n * Providers that synthesize a whole input at once need complete sentences, and\n * an agent writes its answer token by token. Feeding every fragment as it comes\n * would either cut words in half or wait for the end of the answer, so the text\n * is buffered until a sentence closes and released the moment it does.\n *\n * The splitter is stateful: `push` returns the sentences that are complete,\n * `flush` returns whatever is left when the stream ends.\n */\nexport class SentenceSplitter {\n private buffer = ''\n\n /** Adds text and returns the sentences it completes. */\n push(text: string): string[] {\n this.buffer += text\n return this.extract(false)\n }\n\n /** Returns the sentences left in the buffer and empties it. */\n flush(): string[] {\n const sentences = this.extract(true)\n const rest = this.buffer.trim()\n this.buffer = ''\n if (rest) sentences.push(rest)\n return sentences\n }\n\n /** Drops the buffered text, used when an utterance is cancelled. */\n reset() {\n this.buffer = ''\n }\n\n private extract(end: boolean): string[] {\n const sentences: string[] = []\n const regex = /[\\s\\S]*?[.!?…\\n]+(?=\\s|$)/g\n let match: RegExpExecArray | null\n let lastIndex = 0\n\n while ((match = regex.exec(this.buffer)) !== null) {\n // A sentence ending at the very end of an unfinished stream may still\n // grow, so keep it buffered until more text arrives or the stream ends.\n if (!end && regex.lastIndex === this.buffer.length) break\n const sentence = match[0].trim()\n if (sentence) sentences.push(sentence)\n lastIndex = regex.lastIndex\n }\n\n this.buffer = this.buffer.slice(lastIndex)\n return sentences\n }\n}\n","import { Readable } from 'stream'\nimport { SentenceSplitter } from './SentenceSplitter'\nimport { TTS } from './TTS'\n\n/**\n * Base class for text to speech engines that read a whole input at once.\n *\n * A local model, and a remote endpoint without a streaming interface, cannot\n * be fed the agent's answer token by token. This class buffers the answer into\n * sentences, hands them over one at a time, and emits the audio in the order\n * they were written. Subclasses only have to turn one sentence into PCM16 at\n * the rate the Micdrop client expects.\n *\n * Sentences are synthesized one after the other rather than at once: a local\n * model is single threaded, so racing two sentences through it slows both down\n * without bringing the first word any closer.\n */\nexport abstract class SentenceTTS extends TTS {\n private splitter = new SentenceSplitter()\n private queue: string[] = []\n private draining = false\n private controller?: AbortController\n // Bumped by every speak() and every cancel(), so a call claimed late can tell\n // whether it is still the one that should be heard.\n private generation = 0\n private counter = 0 // Identifies the current speak() call\n private synthesizing = 0 // Stamp of the sentence being synthesized\n\n /**\n * Turns one sentence into PCM16 audio at the client's sample rate.\n *\n * The signal is aborted when the utterance is cancelled, which is the moment\n * to stop a subprocess or an inference that is no longer needed. Returning\n * nothing emits nothing, which is how a cancelled synthesis reports back.\n */\n protected abstract synthesize(\n text: string,\n signal: AbortSignal\n ): Promise<Buffer | undefined>\n\n /**\n * Emits a piece of the sentence being synthesized.\n *\n * A model that generates progressively can hand its chunks over as they\n * come rather than waiting for the sentence to be finished, which brings\n * the first word forward by the duration of that sentence. The false it\n * returns says the utterance was cancelled or replaced, so the generation\n * it comes from can be stopped there.\n */\n protected emitAudio(audio: Buffer): boolean {\n if (this.synthesizing !== this.counter) return false\n if (audio.length) this.emit('Audio', audio)\n return true\n }\n\n speak(textStream: Readable) {\n const generation = ++this.generation\n let counter = 0\n\n // Claiming the call is deferred until there is something to say.\n //\n // Taking the next number right away would drop the utterance still being\n // spoken, since the queue skips anything stamped with an older one. A\n // stream that never carries a word, which is what an answer skipped by a\n // tool or by onBeforeAnswer hands over, would then cut the assistant off\n // and throw away the sentences still queued.\n const claimCall = () => {\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 this.splitter.reset()\n return true\n }\n\n textStream.on('data', (chunk: Buffer) => {\n if (!claimCall()) return\n if (counter !== this.counter) return\n this.enqueue(counter, this.splitter.push(chunk.toString('utf-8')))\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream', error)\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is nothing left to flush\n if (!counter || counter !== this.counter) return\n this.enqueue(counter, this.splitter.flush())\n })\n }\n\n cancel() {\n this.log('Cancel')\n this.generation++\n // Increment counter to ignore queued work and the sentence in flight\n this.counter++\n this.splitter.reset()\n this.queue = []\n this.controller?.abort()\n this.controller = undefined\n }\n\n private enqueue(counter: number, sentences: string[]) {\n if (sentences.length === 0) return\n if (counter !== this.counter) return\n this.queue.push(...sentences)\n this.drain()\n }\n\n private async drain() {\n if (this.draining) return\n this.draining = true\n\n while (this.queue.length > 0) {\n const counter = this.counter\n this.synthesizing = counter\n const text = this.queue.shift()!\n const controller = new AbortController()\n this.controller = controller\n\n try {\n this.log(`Synthesizing: \"${text}\"`)\n const audio = await this.synthesize(text, controller.signal)\n // The utterance may have been cancelled while it was being synthesized\n if (counter !== this.counter) continue\n if (audio?.length) this.emit('Audio', audio)\n } catch (error) {\n // A cancelled utterance is not a failure, it left its queue on purpose\n if (counter !== this.counter) continue\n this.log('Error synthesizing speech', error)\n this.emit('Failed', [text, ...this.queue])\n this.queue = []\n } finally {\n if (this.controller === controller) this.controller = undefined\n }\n }\n\n this.draining = false\n // Sentences may have arrived right as we exited the loop\n if (this.queue.length > 0) this.drain()\n }\n}\n","import { WebSocket } from 'ws'\nimport { MicdropError, MicdropErrorCode } from './errors'\n\nexport async function waitForParams<CallParams>(\n socket: WebSocket,\n validate: (params: any) => CallParams\n): Promise<CallParams> {\n return new Promise<CallParams>((resolve, reject) => {\n // Handle timeout\n const timeout = setTimeout(() => {\n reject(new MicdropError(MicdropErrorCode.BadRequest, 'Missing params'))\n }, 3000)\n\n const onParams = (payload: string) => {\n // Clear timeout and listener\n clearTimeout(timeout)\n socket.off('message', onParams)\n\n try {\n // Parse JSON payload\n const params = validate(JSON.parse(payload))\n resolve(params)\n } catch (error) {\n reject(new MicdropError(MicdropErrorCode.BadRequest, 'Invalid params'))\n }\n }\n\n // Listen for params\n socket.on('message', onParams)\n })\n}\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,mBAAuC;;;ACazC,IAAM,0BAA0B;AAChC,IAAM,uBACX;AAEK,IAAM,+BAA+B;AACrC,IAAM,4BACX;AAEK,IAAM,mCAAmC;AACzC,IAAM,gCACX;;;ADoDK,IAAe,QAAf,cAGG,aAAqB;AAAA,EAQ7B,YAAsB,SAAkB;AACtC,UAAM;AADc;AAHtB,SAAU,cAAc;AACxB,SAAU,YAAY;AAIpB,SAAK,eAAe,CAAC,EAAE,MAAM,UAAU,SAAS,QAAQ,aAAa,CAAC;AACtE,SAAK,QAAQ,KAAK,gBAAgB;AAAA,EACpC;AAAA,EAKA,SAAmB;AACjB,SAAK,IAAI,iBAAiB;AAC1B,UAAM,cAAc,EAAE,KAAK;AAC3B,UAAM,SAAS,IAAI,YAAY;AAC/B,SAAK,YAAY;AAEjB,YAAQ,QAAQ,EAEb;AAAA,MAAK,MACJ,KAAK,QAAQ,gBAAgB,KAAK,IAAwB,EAAE,MAAM;AAAA,IACpE,EAEC,KAAK,CAAC,SAAS;AACd,UAAI,KAAM;AACV,aAAO,KAAK,eAAe,MAAM;AAAA,IACnC,CAAC,EAEA,QAAQ,MAAM;AACb,UAAI,OAAO,UAAU;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C,MAAM,GAAG,KAAK,KAAK,CAAC;AACzC,aAAK,QAAQ,CAAC;AAAA,MAChB,UAAE;AACA,YAAI,KAAK,eAAe,WAAY,MAAK,aAAa;AAAA,MACxD;AAAA,IACF;AAEA,SAAK,WAAW;AAEhB,QAAI,KAAK,MAAM,SAAS,EAAG,MAAK,MAAM;AAAA,EACxC;AACF;;;AC5IA,eAAsB,cACpB,QACA,UACqB;AACrB,SAAO,IAAI,QAAoB,CAAC,SAAS,WAAW;AAElD,UAAM,UAAU,WAAW,MAAM;AAC/B,aAAO,IAAI,oCAA0C,gBAAgB,CAAC;AAAA,IACxE,GAAG,GAAI;AAEP,UAAM,WAAW,CAAC,YAAoB;AAEpC,mBAAa,OAAO;AACpB,aAAO,IAAI,WAAW,QAAQ;AAE9B,UAAI;AAEF,cAAM,SAAS,SAAS,KAAK,MAAM,OAAO,CAAC;AAC3C,gBAAQ,MAAM;AAAA,MAChB,SAAS,OAAO;AACd,eAAO,IAAI,oCAA0C,gBAAgB,CAAC;AAAA,MACxE;AAAA,IACF;AAGA,WAAO,GAAG,WAAW,QAAQ;AAAA,EAC/B,CAAC;AACH;","names":["MicdropErrorCode","EventEmitter","PassThrough","MicdropClientCommands","MicdropServerCommands","EventEmitter","PassThrough","EventEmitter","EventEmitter","PassThrough","PassThrough","PassThrough","EventEmitter","PassThrough","PassThrough","PassThrough"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micdrop/server",
3
- "version": "2.6.0",
3
+ "version": "2.8.0",
4
4
  "description": "\ud83d\udd90\ufe0f\ud83c\udfa4 Micdrop: Real-Time Voice Conversations with AI",
5
5
  "author": "Godefroy de Compreignac",
6
6
  "license": "MIT",