@micdrop/mistral 1.3.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -11
- package/dist/index.d.mts +47 -1
- package/dist/index.d.ts +47 -1
- package/dist/index.js +191 -52
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +191 -46
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -4
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/MistralAgent.ts","../src/MistralSTT.ts","../src/MistralTTS.ts"],"sourcesContent":["export * from './MistralAgent'\nexport * from './MistralSTT'\nexport * from './MistralTTS'\n","import { Agent, AgentOptions } from '@micdrop/server'\nimport { Mistral } from '@mistralai/mistralai'\nimport {\n ChatCompletionStreamRequest,\n ChatCompletionStreamRequestMessages,\n} from '@mistralai/mistralai/models/components'\nimport { Writable } from 'stream'\nimport z, { toJSONSchema } from 'zod'\n\nexport interface MistralAgentOptions extends AgentOptions {\n apiKey: string\n model?: string\n settings?: Omit<ChatCompletionStreamRequest, 'messages' | 'model'>\n maxRetry?: number\n retryDelay?: number\n maxSteps?: number\n}\n\nconst DEFAULT_MODEL = 'mistral-large-latest'\nconst DEFAULT_MAX_RETRY = 3\nconst DEFAULT_MAX_STEPS = 5\nconst DEFAULT_RETRY_DELAY = 500\n\n/** What one request produced */\ninterface StepOutcome {\n fullAnswer: string\n skipAnswer: boolean\n}\n\n/** A request that failed after writing text or running tools */\nclass StepInterruptedError extends Error {\n constructor(readonly outcome: StepOutcome) {\n super('Answer interrupted')\n }\n}\n\nexport class MistralAgent extends Agent<MistralAgentOptions> {\n private mistral: Mistral\n\n constructor(options: MistralAgentOptions) {\n super(options)\n this.mistral = new Mistral({ apiKey: options.apiKey })\n }\n\n protected async generateAnswer(stream: Writable, signal: AbortSignal) {\n const maxSteps = this.options.maxSteps || DEFAULT_MAX_STEPS\n const maxRetry = this.options.maxRetry || DEFAULT_MAX_RETRY\n\n // Each step is one request. A step that only calls tools asks for another\n // one, so the LLM can answer with their results.\n for (let step = 0; step < maxSteps; step++) {\n let outcome: StepOutcome | undefined\n for (let tryCount = 0; !outcome; tryCount++) {\n try {\n outcome = await this.generateStep(stream, signal)\n } catch (error: any) {\n // An aborted request throws too, but that is the cancellation the\n // base asked for, not a failure to report\n if (signal.aborted) return\n if (error instanceof StepInterruptedError) {\n // Text already reached the stream or tools already ran: a retry\n // would say or do it again, so the step keeps what it got\n outcome = error.outcome\n break\n }\n console.error('[MistralAgent] Error:', error)\n if (tryCount >= maxRetry) {\n this.log('Max retries reached, giving up')\n this.emit('Failed')\n return\n }\n await new Promise((resolve) =>\n setTimeout(resolve, this.options.retryDelay || DEFAULT_RETRY_DELAY)\n )\n // Cancelled during the delay: the retry would answer nobody\n if (signal.aborted) return\n }\n }\n if (signal.aborted) return\n\n if (outcome.fullAnswer) {\n // Add full answer to conversation\n const { message, metadata } = this.extract(outcome.fullAnswer)\n this.addAssistantMessage(message, metadata)\n return\n }\n if (outcome.skipAnswer) {\n // Hack: Mistral needs an assistant message after tool calls\n this.conversation.push({\n role: 'assistant',\n content: ' ',\n })\n return\n }\n // Query again in case of tool call\n }\n console.error('[MistralAgent] Max steps reached')\n }\n\n /**\n * Runs one request, writing its text to the stream and running its tools.\n *\n * It throws a plain error when the request failed before producing\n * anything, which can then be retried, and a {@link StepInterruptedError}\n * once something was written or done.\n */\n private async generateStep(\n stream: Writable,\n signal: AbortSignal\n ): Promise<StepOutcome> {\n // Hack: Mistral needs a user message if there is only a system message\n if (this.conversation.length === 1) {\n this.conversation.push({\n role: 'user',\n content: '',\n })\n }\n\n // Prepare extracting\n let extracting = false\n const extractOptions = this.getExtractOptions()\n const outcome: StepOutcome = { fullAnswer: '', skipAnswer: false }\n let produced = false\n\n try {\n const result = await this.mistral.chat.stream(\n {\n model: this.options.model || DEFAULT_MODEL,\n messages: this.buildMessages(),\n tools: this.buildTools(),\n ...this.options.settings,\n },\n { signal }\n )\n\n // Handle response events\n for await (const event of result) {\n if (signal.aborted) return outcome\n const delta = event.data.choices[0]?.delta\n const chunk = delta?.content\n const toolCalls = delta?.toolCalls\n\n // Text chunk\n if (typeof chunk === 'string') {\n if (chunk === '') continue\n this.log(`Answer chunk: \"${chunk}\"`)\n outcome.fullAnswer += chunk\n produced = true\n\n // Extracting value?\n if (extractOptions) {\n if (!extracting) {\n const startTagIndex = outcome.fullAnswer.indexOf(\n extractOptions.startTag\n )\n if (startTagIndex !== -1) {\n extracting = true\n const messagePart = outcome.fullAnswer\n .slice(0, startTagIndex)\n .trimEnd()\n stream.write(messagePart)\n continue\n }\n } else {\n // Extracting, don't write to stream\n continue\n }\n }\n stream.write(chunk)\n } else if (toolCalls?.length) {\n for (const toolCall of toolCalls) {\n if (signal.aborted) return outcome\n produced = true\n const result = await this.executeTool({\n role: 'tool_call',\n toolCallId: toolCall.id || '',\n toolName: toolCall.function.name,\n // The SDK hands the arguments over parsed or as JSON\n parameters:\n typeof toolCall.function.arguments === 'string'\n ? toolCall.function.arguments\n : JSON.stringify(toolCall.function.arguments),\n })\n if (result.skipAnswer) {\n outcome.skipAnswer = true\n }\n }\n }\n }\n } catch (error) {\n if (!produced || signal.aborted) throw error\n console.error('[MistralAgent] Error after a partial answer:', error)\n throw new StepInterruptedError(outcome)\n }\n return outcome\n }\n\n private buildMessages(): ChatCompletionStreamRequestMessages[] {\n return this.conversation.map(\n (message): ChatCompletionStreamRequestMessages => {\n switch (message.role) {\n case 'user':\n case 'assistant':\n case 'system':\n return {\n role: message.role,\n content: message.content,\n }\n case 'tool_call':\n return {\n role: 'assistant',\n toolCalls: [\n {\n type: 'function',\n id: message.toolCallId,\n function: {\n name: message.toolName,\n // Already the JSON the model wrote\n arguments: message.parameters,\n },\n },\n ],\n }\n case 'tool_result':\n return {\n role: 'tool',\n toolCallId: message.toolCallId,\n name: message.toolName,\n content: [\n {\n type: 'text',\n text: message.output,\n },\n ],\n }\n }\n }\n )\n }\n\n private buildTools(): ChatCompletionStreamRequest['tools'] {\n // Disable tools if first message\n const enableTools = this.conversation.length > 1\n if (!enableTools) return undefined\n\n return this.tools.map((tool) => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: toJSONSchema(tool.inputSchema || z.object()),\n },\n }))\n }\n}\n","import { ReconnectingSocket, STT } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport type { RawData } from 'ws'\n\n/**\n * Mistral Real-time STT (Voxtral)\n *\n * @see https://docs.mistral.ai/studio/audio/speech-to-text/overview\n */\n\nexport type MistralAudioEncoding =\n | 'pcm_s16le'\n | 'pcm_s32le'\n | 'pcm_f16le'\n | 'pcm_f32le'\n | 'pcm_mulaw'\n | 'pcm_alaw'\n\nexport interface MistralSTTOptions {\n apiKey: string\n model?: string\n encoding?: MistralAudioEncoding\n targetStreamingDelayMs?: number\n /** How long a connection attempt may take */\n connectionTimeout?: number\n /** How long an utterance waits for its transcript once it ended */\n transcriptionTimeout?: number\n /** Delay between two connection attempts */\n retryDelay?: number\n /** How many attempts in a row may fail before `Failed` is emitted */\n maxRetry?: number\n}\n\n/**\n * Where an utterance stands.\n *\n * - `streaming`: its audio is still coming\n * - `flushing`: its audio ended and was flushed, it waits for\n * `transcription.done`\n * - `done`: its transcript was emitted, or it was dropped\n */\ntype UtteranceStatus = 'streaming' | 'flushing' | 'done'\n\n/** One audio stream given to `transcribe()`, which gets exactly one transcript */\ninterface Utterance {\n status: UtteranceStatus\n // Audio of the utterance, kept to send it again on a new session\n chunks: Buffer[]\n // Accumulated transcription.text.delta chunks of the current session\n text: string\n // Runs once the flush went out\n timeout?: ReturnType<typeof setTimeout>\n}\n\nconst DEFAULT_MODEL = 'voxtral-mini-transcribe-realtime-3-0'\nconst DEFAULT_ENCODING: MistralAudioEncoding = 'pcm_s16le'\nconst SAMPLE_RATE = 16000 // Rate of the incoming audio (Micdrop client)\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\n\nexport class MistralSTT extends STT {\n private socket: ReconnectingSocket\n // Utterances not done yet, oldest first. The server answers them in the\n // order their audio went out, so the transcript in progress is the oldest's.\n private utterances: Utterance[] = []\n // Flushes of utterances that timed out on this session, whose deltas and\n // transcription.done may still arrive and must not go to the next one. Each\n // is forgotten after another timeout, so an answer that never comes does not\n // swallow every transcript after it.\n private staleFlushes: number[] = []\n\n constructor(private options: MistralSTTOptions) {\n super()\n\n this.socket = new ReconnectingSocket({\n url: this.getURL(),\n clientOptions: {\n headers: {\n Authorization: `Bearer ${this.options.apiKey}`,\n },\n },\n connectionTimeout:\n this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT,\n retryDelay: this.options.retryDelay ?? DEFAULT_RETRY_DELAY,\n maxRetry: this.options.maxRetry ?? DEFAULT_MAX_RETRY,\n // The audio format must be set before any audio is appended\n handshake: {\n start: (socket) => socket.send(this.getSessionUpdate()),\n },\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n transcribe(audioStream: Readable) {\n // After giving up, a new utterance gives the connection another chance\n this.socket.connect()\n\n const utterance: Utterance = { status: 'streaming', chunks: [], text: '' }\n this.utterances.push(utterance)\n\n // Read audio stream and send to Mistral\n audioStream.on('data', (chunk: Buffer) => {\n if (utterance.status !== 'streaming') return\n utterance.chunks.push(chunk)\n // A chunk that does not go out now goes with the others once connected\n if (this.sendAudioChunk(chunk)) {\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n }\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // transcription (transcription.done). Unlike input_audio.end, a flush keeps\n // the session alive, so the same connection is reused for the next utterance.\n audioStream.on('end', () => this.endUtterance(utterance))\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.utterances.forEach((utterance) => this.dropUtterance(utterance))\n this.utterances = []\n }\n\n private getURL() {\n const model = this.options.model || DEFAULT_MODEL\n return `wss://api.mistral.ai/v1/audio/transcriptions/realtime?model=${encodeURIComponent(\n model\n )}`\n }\n\n private getSessionUpdate() {\n // Configure the audio format. Must be sent before any audio is appended;\n // Mistral rejects format updates once audio has started.\n return JSON.stringify({\n type: 'session.update',\n session: {\n audio_format: {\n encoding: this.options.encoding || DEFAULT_ENCODING,\n sample_rate: SAMPLE_RATE,\n },\n ...(this.options.targetStreamingDelayMs !== undefined && {\n target_streaming_delay_ms: this.options.targetStreamingDelayMs,\n }),\n },\n })\n }\n\n // A new session knows nothing of the previous one: every pending utterance\n // is sent again in full and replays its whole transcript, so the deltas it\n // gathered start over to avoid duplicating them\n private onOpen = ({ reconnected }: { reconnected: boolean }) => {\n this.staleFlushes = []\n const pending = this.utterances.filter(\n (utterance) => utterance.chunks.length > 0\n )\n if (pending.length === 0) return\n this.log(\n reconnected ? 'Sending audio chunks again' : 'Sending queued audio chunks'\n )\n for (const utterance of pending) {\n utterance.text = ''\n utterance.chunks.forEach((chunk) => this.sendAudioChunk(chunk))\n if (utterance.status === 'flushing') this.flushAudio(utterance)\n }\n }\n\n private onMessage = (data: RawData) => {\n let message: any\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Ignoring a message that is not JSON')\n return\n }\n\n switch (message.type) {\n case 'transcription.text.delta': {\n if (typeof message.text !== 'string') break\n this.log(`Received transcript delta: \"${message.text}\"`)\n // Deltas of an utterance that timed out are dropped with it\n if (this.hasStaleFlush()) break\n const utterance = this.utterances[0]\n if (utterance) utterance.text += message.text\n break\n }\n\n case 'transcription.done': {\n // The answer to a flush whose utterance already timed out\n if (this.hasStaleFlush()) {\n this.staleFlushes.shift()\n this.log('Ignoring the transcript of a timed out utterance')\n break\n }\n const utterance = this.utterances[0]\n // Only a flush finalizes a transcript: one that was not asked for\n // leaves the utterance in progress alone\n if (utterance?.status !== 'flushing') {\n this.log('Ignoring an unexpected transcription.done')\n break\n }\n const transcript =\n typeof message.text === 'string' ? message.text : utterance.text\n this.log(`Received completed transcript: \"${transcript}\"`)\n this.complete(utterance, transcript)\n break\n }\n\n case 'error':\n this.log('Error:', message.error ?? message)\n break\n\n default:\n break\n }\n }\n\n private onFailed = () => {\n // Every pending utterance goes back to the caller, for another STT\n const chunks = this.utterances.flatMap((utterance) => utterance.chunks)\n this.utterances.forEach((utterance) => this.dropUtterance(utterance))\n this.utterances = []\n this.emit('Failed', chunks)\n }\n\n private endUtterance(utterance: Utterance) {\n if (utterance.status !== 'streaming') return\n // A stream that carried no audio expects no transcript\n if (utterance.chunks.length === 0) {\n this.removeUtterance(utterance)\n return\n }\n utterance.status = 'flushing'\n // Not connected yet: the flush goes out with the audio, on `Open`\n if (this.socket.isOpen) this.flushAudio(utterance)\n }\n\n private flushAudio(utterance: Utterance) {\n // Flush buffered audio so the server processes the tail and emits a final\n // transcription.done for this utterance. We deliberately do NOT send\n // input_audio.end: that terminates the session (the server drops the\n // connection), whereas a flush finalizes the current utterance and keeps the\n // session ready for the next one.\n if (!this.socket.send(JSON.stringify({ type: 'input_audio.flush' }))) {\n return\n }\n this.log('Flushed audio input')\n\n // Timeout transcription if no transcript is received, with the deltas\n // received so far\n clearTimeout(utterance.timeout)\n utterance.timeout = setTimeout(() => {\n this.log('Transcription timeout')\n // Its flush is still answered later on this session, and that answer\n // belongs to no other utterance\n this.staleFlushes.push(Date.now() + this.transcriptionTimeout)\n this.complete(utterance, utterance.text)\n }, this.transcriptionTimeout)\n }\n\n private get transcriptionTimeout(): number {\n return this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT\n }\n\n /** Whether an answer to a timed out flush may still be on its way */\n private hasStaleFlush(): boolean {\n const now = Date.now()\n while (this.staleFlushes.length > 0 && this.staleFlushes[0] <= now) {\n this.log('Forgetting a flush that was never answered')\n this.staleFlushes.shift()\n }\n return this.staleFlushes.length > 0\n }\n\n private complete(utterance: Utterance, transcript: string) {\n if (utterance.status === 'done') return\n this.removeUtterance(utterance)\n this.emit('Transcript', transcript.trim())\n }\n\n private removeUtterance(utterance: Utterance) {\n this.dropUtterance(utterance)\n this.utterances = this.utterances.filter((u) => u !== utterance)\n }\n\n private dropUtterance(utterance: Utterance) {\n utterance.status = 'done'\n clearTimeout(utterance.timeout)\n utterance.timeout = undefined\n }\n\n private sendAudioChunk(chunk: Buffer): boolean {\n return this.socket.send(\n JSON.stringify({\n type: 'input_audio.append',\n audio: chunk.toString('base64'),\n })\n )\n }\n}\n","import { float32ToPcm16, Pcm16Resampler, SentenceTTS } from '@micdrop/server'\n\n/**\n * Mistral text to speech (Voxtral Speech 3)\n *\n * @see https://docs.mistral.ai/studio/audio/text-to-speech/overview\n * @see https://docs.mistral.ai/api/endpoint/audio/speech\n *\n * The speech endpoint reads a whole text at once, so the answer is cut into\n * sentences, and the audio of each one is emitted as the server streams it.\n * A sentence starts playing in about half a second and is generated several\n * times faster than it is spoken, so the next one is ready before the current\n * one ends.\n */\n\nexport interface MistralTTSOptions {\n apiKey: string\n model?: string\n // Preset voice (`voice_592` for Marie in French, `gb_jane_neutral`...) or\n // the id of a voice saved in your Mistral account\n voiceId?: string\n // A few seconds of speech to clone, base64 encoded, used instead of voiceId\n refAudio?: string\n // Extra fields of the request body, such as phoneme_hints\n settings?: Record<string, unknown>\n // API address, to reach the European or US endpoint\n baseUrl?: string\n}\n\nconst DEFAULT_MODEL = 'voxtral-mini-tts-3-0'\nconst DEFAULT_VOICE_ID = 'voice_3150' // James, American English\nconst DEFAULT_BASE_URL = 'https://api.mistral.ai'\nconst MISTRAL_SAMPLE_RATE = 24000 // Rate of the pcm output from Mistral\nconst OUTPUT_SAMPLE_RATE = 16000 // Rate expected by the Micdrop client\nconst FLOAT32_BYTES = 4 // The pcm output is mono float32 little-endian\n\nexport class MistralTTS extends SentenceTTS {\n constructor(private readonly options: MistralTTSOptions) {\n super()\n }\n\n protected async synthesize(\n text: string,\n signal: AbortSignal\n ): Promise<undefined> {\n const response = await this.request(text, signal)\n if (!response.body) throw new Error('Mistral answered without a body')\n\n // One resampler per sentence: two sentences are independent streams, so\n // the fractional position must not carry over from one to the next\n const resampler = new Pcm16Resampler(\n MISTRAL_SAMPLE_RATE,\n OUTPUT_SAMPLE_RATE\n )\n // Half of a float32 sample split between two events\n let leftover = Buffer.alloc(0)\n\n for await (const event of readEvents(response.body, signal)) {\n if (event.type === 'error') {\n throw new Error(\n `Speech generation failed: ${JSON.stringify(event.error ?? event)}`\n )\n }\n if (event.type !== 'speech.audio.delta' || !event.audio_data) continue\n\n const data = Buffer.concat([\n leftover,\n Buffer.from(event.audio_data, 'base64'),\n ])\n const length = data.length - (data.length % FLOAT32_BYTES)\n leftover = data.subarray(length)\n const samples = new Float32Array(length / FLOAT32_BYTES)\n for (let i = 0; i < samples.length; i++) {\n samples[i] = data.readFloatLE(i * FLOAT32_BYTES)\n }\n\n // Cancelled or replaced, the rest is not wanted\n if (!this.emitAudio(resampler.process(float32ToPcm16(samples)))) break\n }\n\n return undefined\n }\n\n private async request(text: string, signal: AbortSignal) {\n const { options } = this\n const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/+$/, '')\n\n const response = await fetch(`${baseUrl}/v1/audio/speech`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${options.apiKey}`,\n 'Content-Type': 'application/json',\n Accept: 'text/event-stream',\n },\n signal,\n body: JSON.stringify({\n ...options.settings,\n model: options.model || DEFAULT_MODEL,\n input: text,\n ...(options.refAudio\n ? { ref_audio: options.refAudio }\n : { voice_id: options.voiceId || DEFAULT_VOICE_ID }),\n // Raw samples rather than wav, so chunks can be concatenated without a\n // header in the middle of the audio\n response_format: 'pcm',\n stream: true,\n }),\n })\n\n if (!response.ok) {\n const details = await response.text().catch(() => '')\n throw new Error(\n `Mistral answered ${response.status} ${response.statusText}: ${details}`\n )\n }\n\n return response\n }\n}\n\n/** Parses the server-sent events of the response, one JSON payload each. */\nasync function* readEvents(\n body: ReadableStream<Uint8Array>,\n signal: AbortSignal\n): AsyncGenerator<any> {\n const reader = body.getReader()\n const decoder = new TextDecoder()\n let buffer = ''\n\n try {\n while (!signal.aborted) {\n const { done, value } = await reader.read()\n if (done) break\n buffer += decoder.decode(value, { stream: true }).replace(/\\r\\n/g, '\\n')\n\n let end: number\n while ((end = buffer.indexOf('\\n\\n')) >= 0) {\n const block = buffer.slice(0, end)\n buffer = buffer.slice(end + 2)\n const data = block\n .split('\\n')\n .filter((line) => line.startsWith('data:'))\n .map((line) => line.slice(5).trimStart())\n .join('\\n')\n if (data) yield JSON.parse(data)\n }\n }\n } finally {\n // Closing the body closes the connection, and the server stops generating\n reader.cancel().catch(() => {})\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAoC;AACpC,uBAAwB;AAMxB,iBAAgC;AAWhC,IAAM,gBAAgB;AACtB,IAAM,oBAAoB;AAC1B,IAAM,oBAAoB;AAC1B,IAAM,sBAAsB;AAS5B,IAAM,uBAAN,cAAmC,MAAM;AAAA,EACvC,YAAqB,SAAsB;AACzC,UAAM,oBAAoB;AADP;AAAA,EAErB;AACF;AAEO,IAAM,eAAN,cAA2B,oBAA2B;AAAA,EAG3D,YAAY,SAA8B;AACxC,UAAM,OAAO;AACb,SAAK,UAAU,IAAI,yBAAQ,EAAE,QAAQ,QAAQ,OAAO,CAAC;AAAA,EACvD;AAAA,EAEA,MAAgB,eAAe,QAAkB,QAAqB;AACpE,UAAM,WAAW,KAAK,QAAQ,YAAY;AAC1C,UAAM,WAAW,KAAK,QAAQ,YAAY;AAI1C,aAAS,OAAO,GAAG,OAAO,UAAU,QAAQ;AAC1C,UAAI;AACJ,eAAS,WAAW,GAAG,CAAC,SAAS,YAAY;AAC3C,YAAI;AACF,oBAAU,MAAM,KAAK,aAAa,QAAQ,MAAM;AAAA,QAClD,SAAS,OAAY;AAGnB,cAAI,O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AK,MAAM,IAAI;AAAA,MACjC;AAAA,IACF;AAAA,EACF,UAAE;AAEA,WAAO,OAAO,EAAE,MAAM,MAAM;AAAA,IAAC,CAAC;AAAA,EAChC;AACF;","names":["result","z","import_server","DEFAULT_MODEL","DEFAULT_RETRY_DELAY","DEFAULT_MAX_RETRY","import_server","DEFAULT_MODEL"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/MistralAgent.ts","../src/deltaText.ts","../src/models.ts","../src/MistralSTT.ts","../src/MistralTTS.ts"],"sourcesContent":["export * from './MistralAgent'\nexport * from './MistralSTT'\nexport * from './MistralTTS'\n","import { Agent, AgentOptions } from '@micdrop/server'\nimport { MicdropModel } from '@micdrop/server'\nimport { Mistral } from '@mistralai/mistralai'\nimport type {\n ChatCompletionStreamRequest,\n ChatCompletionStreamRequestMessage,\n} from '@mistralai/mistralai/models/components'\nimport { Writable } from 'stream'\nimport z, { toJSONSchema } from 'zod'\nimport { textOf } from './deltaText'\nimport { listMistralModels } from './models'\n\nexport interface MistralAgentOptions extends AgentOptions {\n apiKey: string\n model?: string\n // Other parameters of the request. `reasoningEffort` sets how long the\n // model reasons before answering: the lowest level the model accepts brings\n // the first word forward, \"none\" for Mistral Medium 3.5 and Small 4, \"low\"\n // for Mistral Large 4.\n settings?: Omit<ChatCompletionStreamRequest, 'messages' | 'model'>\n maxRetry?: number\n retryDelay?: number\n maxSteps?: number\n}\n\n// Answers without reasoning, where Mistral Large 4 always reasons first\nconst DEFAULT_MODEL = 'mistral-medium-latest'\nconst DEFAULT_MAX_RETRY = 3\nconst DEFAULT_MAX_STEPS = 5\nconst DEFAULT_RETRY_DELAY = 500\n\n/** What one request produced */\ninterface StepOutcome {\n fullAnswer: string\n skipAnswer: boolean\n ranTools: boolean\n}\n\n/** A request that failed after writing text or running tools */\nclass StepInterruptedError extends Error {\n constructor(readonly outcome: StepOutcome) {\n super('Answer interrupted')\n }\n}\n\nexport class MistralAgent extends Agent<MistralAgentOptions> {\n /**\n * Lists the models that can answer a call: chat models that call tools. `reasoning` marks the ones that can reason, slower to answer when they do, each one once with its aliases.\n *\n * The list is fetched once and shared by every call of the process for a\n * day. `refresh` fetches it again.\n */\n static listModels(options: {\n apiKey: string\n refresh?: boolean\n }): Promise<MicdropModel[]> {\n return listMistralModels(\n 'agent',\n ({ capabilities }) =>\n capabilities.completion_chat && capabilities.function_calling,\n options\n )\n }\n\n private mistral: Mistral\n\n constructor(options: MistralAgentOptions) {\n super(options)\n this.mistral = new Mistral({ apiKey: options.apiKey })\n }\n\n protected async generateAnswer(stream: Writable, signal: AbortSignal) {\n const maxSteps = this.options.maxSteps || DEFAULT_MAX_STEPS\n const maxRetry = this.options.maxRetry || DEFAULT_MAX_RETRY\n\n // Each step is one request. A step that only calls tools asks for another\n // one, so the LLM can answer with their results.\n for (let step = 0; step < maxSteps; step++) {\n let outcome: StepOutcome | undefined\n for (let tryCount = 0; !outcome; tryCount++) {\n try {\n outcome = await this.generateStep(stream, signal)\n } catch (error: any) {\n // An aborted request throws too, but that is the cancellation the\n // base asked for, not a failure to report\n if (signal.aborted) return\n if (error instanceof StepInterruptedError) {\n // Text already reached the stream or tools already ran: a retry\n // would say or do it again, so the step keeps what it got\n outcome = error.outcome\n break\n }\n console.error('[MistralAgent] Error:', error)\n if (tryCount >= maxRetry) {\n this.log('Max retries reached, giving up')\n this.emit('Failed')\n return\n }\n await new Promise((resolve) =>\n setTimeout(resolve, this.options.retryDelay || DEFAULT_RETRY_DELAY)\n )\n // Cancelled during the delay: the retry would answer nobody\n if (signal.aborted) return\n }\n }\n if (signal.aborted) return\n\n if (outcome.fullAnswer) {\n // Add full answer to conversation\n const { message, metadata } = this.extract(outcome.fullAnswer)\n this.addAssistantMessage(message, metadata)\n return\n }\n if (outcome.skipAnswer) {\n // Hack: Mistral needs an assistant message after tool calls\n this.conversation.push({\n role: 'assistant',\n content: ' ',\n })\n return\n }\n // An empty answer would come back the same on every step\n if (!outcome.ranTools) {\n this.log('Empty answer')\n return\n }\n // Query again in case of tool call\n }\n console.error('[MistralAgent] Max steps reached')\n }\n\n /**\n * Runs one request, writing its text to the stream and running its tools.\n *\n * It throws a plain error when the request failed before producing\n * anything, which can then be retried, and a {@link StepInterruptedError}\n * once something was written or done.\n */\n private async generateStep(\n stream: Writable,\n signal: AbortSignal\n ): Promise<StepOutcome> {\n // Hack: Mistral needs a user message if there is only a system message\n if (this.conversation.length === 1) {\n this.conversation.push({\n role: 'user',\n content: '',\n })\n }\n\n const write = this.answerWriter(stream)\n const outcome: StepOutcome = {\n fullAnswer: '',\n skipAnswer: false,\n ranTools: false,\n }\n let produced = false\n\n try {\n const result = await this.mistral.chat.stream(\n {\n model: this.options.model || DEFAULT_MODEL,\n messages: this.buildMessages(),\n tools: this.buildTools(),\n ...this.options.settings,\n },\n { signal }\n )\n\n // A model may stream a tool call in fragments, the arguments split over\n // several deltas sharing its index, so tools run once the stream ends\n const toolCalls: { id: string; name: string; arguments: string }[] = []\n const toolCallsByIndex = new Map<number, (typeof toolCalls)[number]>()\n\n // Handle response events\n for await (const event of result) {\n if (signal.aborted) return outcome\n const delta = event.data.choices[0]?.delta\n\n // Text chunk. A delta can carry an empty content next to its tool\n // calls, so both are read.\n const chunk = textOf(delta?.content)\n if (chunk) {\n this.log(`Answer chunk: \"${chunk}\"`)\n outcome.fullAnswer += chunk\n produced = true\n write(chunk)\n }\n\n for (const [position, fragment] of (delta?.toolCalls ?? []).entries()) {\n const index = fragment.index ?? position\n let toolCall = toolCallsByIndex.get(index)\n // Another id at the same index opens another call\n if (\n !toolCall ||\n (fragment.id && toolCall.id && fragment.id !== toolCall.id)\n ) {\n toolCall = { id: '', name: '', arguments: '' }\n toolCalls.push(toolCall)\n toolCallsByIndex.set(index, toolCall)\n }\n if (fragment.id) toolCall.id = fragment.id\n if (fragment.function.name) toolCall.name = fragment.function.name\n // The SDK hands the arguments over parsed or as JSON\n const args = fragment.function.arguments\n toolCall.arguments +=\n typeof args === 'string' ? args : JSON.stringify(args)\n }\n }\n\n for (const toolCall of toolCalls) {\n if (signal.aborted) return outcome\n produced = true\n outcome.ranTools = true\n const result = await this.executeTool({\n role: 'tool_call',\n toolCallId: toolCall.id,\n toolName: toolCall.name,\n parameters: toolCall.arguments || '{}',\n })\n if (result.skipAnswer) {\n outcome.skipAnswer = true\n }\n }\n } catch (error) {\n if (!produced || signal.aborted) throw error\n console.error('[MistralAgent] Error after a partial answer:', error)\n throw new StepInterruptedError(outcome)\n }\n return outcome\n }\n\n private buildMessages(): ChatCompletionStreamRequestMessage[] {\n return this.conversation.map(\n (message): ChatCompletionStreamRequestMessage => {\n switch (message.role) {\n case 'user':\n case 'assistant':\n case 'system':\n return {\n role: message.role,\n content: message.content,\n }\n case 'tool_call':\n return {\n role: 'assistant',\n toolCalls: [\n {\n type: 'function',\n id: message.toolCallId,\n function: {\n name: message.toolName,\n // Already the JSON the model wrote\n arguments: message.parameters,\n },\n },\n ],\n }\n case 'tool_result':\n return {\n role: 'tool',\n toolCallId: message.toolCallId,\n name: message.toolName,\n content: [\n {\n type: 'text',\n text: message.output,\n },\n ],\n }\n }\n }\n )\n }\n\n private buildTools(): ChatCompletionStreamRequest['tools'] {\n // Disable tools if first message\n const enableTools = this.conversation.length > 1\n if (!enableTools) return undefined\n\n return this.tools.map((tool) => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: toJSONSchema(tool.inputSchema || z.object()),\n },\n }))\n }\n}\n","import type { DeltaMessage } from '@mistralai/mistralai/models/components'\n\n/**\n * The text of a streamed delta.\n *\n * A model that reasons streams its content as an array: `thinking` chunks\n * first, then `text` chunks, the first word of the answer among them. Only\n * the text belongs to the answer, the reasoning is left out.\n *\n * @returns Undefined for a delta without content, a tool call for instance\n */\nexport function textOf(content: DeltaMessage['content']): string | undefined {\n if (typeof content === 'string') return content\n if (!Array.isArray(content)) return undefined\n return content\n .map((chunk) => (chunk.type === 'text' ? chunk.text : ''))\n .join('')\n}\n","import { cachedList, cachedModels, MicdropModel } from '@micdrop/server'\n\nexport const DEFAULT_BASE_URL = 'https://api.mistral.ai'\n\n/** A model as the Mistral API lists it, the fields read here */\ninterface MistralApiModel {\n id: string\n name?: string | null\n description?: string | null\n aliases?: string[]\n capabilities: Record<string, boolean>\n deprecation?: string | null\n audio_transcription_capabilities?: { languages?: string[] } | null\n}\n\n/** Every model of the account, fetched once a day for the whole package */\nfunction fetchAll(apiKey: string, baseUrl: string): Promise<MistralApiModel[]> {\n return cachedList(['mistral-raw-models', baseUrl], apiKey, async () => {\n const response = await fetch(`${baseUrl}/v1/models`, {\n headers: { Authorization: `Bearer ${apiKey}` },\n })\n if (!response.ok) throw new Error(`Mistral answered ${response.status}`)\n const { data } = (await response.json()) as { data: MistralApiModel[] }\n return data\n })\n}\n\n/**\n * The models with a capability, each one once.\n *\n * The API lists every alias as a model of its own, so the aliases of a model\n * are gathered under one id: its `-latest` alias when it has one, which\n * follows the newest version, otherwise its own id.\n */\nexport function listMistralModels(\n component: string,\n keep: (model: MistralApiModel) => boolean,\n options: { apiKey: string; baseUrl?: string; refresh?: boolean }\n): Promise<MicdropModel[]> {\n const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/+$/, '')\n return cachedModels(\n ['mistral', component, baseUrl],\n options.apiKey,\n async () => {\n const models: MicdropModel[] = []\n const seen = new Set<string>()\n for (const model of await fetchAll(options.apiKey, baseUrl)) {\n if (!keep(model) || seen.has(model.id)) continue\n const names = [model.id, ...(model.aliases ?? [])]\n names.forEach((name) => seen.add(name))\n const id = names.find((name) => name.endsWith('-latest')) ?? model.id\n models.push({\n id,\n name: model.name || undefined,\n description: model.description || undefined,\n aliases: names.filter((name) => name !== id),\n languages:\n model.audio_transcription_capabilities?.languages ?? undefined,\n reasoning: !!model.capabilities.reasoning,\n deprecated: !!model.deprecation,\n shutdownDate: model.deprecation?.slice(0, 10) || undefined,\n })\n }\n return models\n },\n options.refresh\n )\n}\n","import { MicdropModel, ReconnectingSocket, STT } from '@micdrop/server'\nimport { Readable } from 'stream'\nimport type { RawData } from 'ws'\nimport { listMistralModels } from './models'\n\n/**\n * Mistral Real-time STT (Voxtral)\n *\n * @see https://docs.mistral.ai/studio/audio/speech-to-text/overview\n */\n\nexport type MistralAudioEncoding =\n | 'pcm_s16le'\n | 'pcm_s32le'\n | 'pcm_f16le'\n | 'pcm_f32le'\n | 'pcm_mulaw'\n | 'pcm_alaw'\n\nexport interface MistralSTTOptions {\n apiKey: string\n model?: string\n encoding?: MistralAudioEncoding\n targetStreamingDelayMs?: number\n /** How long a connection attempt may take */\n connectionTimeout?: number\n /** How long an utterance waits for its transcript once it ended */\n transcriptionTimeout?: number\n /** Delay between two connection attempts */\n retryDelay?: number\n /** How many attempts in a row may fail before `Failed` is emitted */\n maxRetry?: number\n}\n\n/**\n * Where an utterance stands.\n *\n * - `streaming`: its audio is still coming\n * - `flushing`: its audio ended and was flushed, it waits for\n * `transcription.done`\n * - `done`: its transcript was emitted, or it was dropped\n */\ntype UtteranceStatus = 'streaming' | 'flushing' | 'done'\n\n/** One audio stream given to `transcribe()`, which gets exactly one transcript */\ninterface Utterance {\n status: UtteranceStatus\n // Audio of the utterance, kept to send it again on a new session\n chunks: Buffer[]\n // Accumulated transcription.text.delta chunks of the current session\n text: string\n // Runs once the flush went out\n timeout?: ReturnType<typeof setTimeout>\n}\n\nconst DEFAULT_MODEL = 'voxtral-mini-transcribe-realtime-3-0'\nconst DEFAULT_ENCODING: MistralAudioEncoding = 'pcm_s16le'\nconst SAMPLE_RATE = 16000 // Rate of the incoming audio (Micdrop client)\nconst DEFAULT_CONNECTION_TIMEOUT = 5000\nconst DEFAULT_TRANSCRIPTION_TIMEOUT = 4000\nconst DEFAULT_RETRY_DELAY = 1000\nconst DEFAULT_MAX_RETRY = 3\n\nexport class MistralSTT extends STT {\n /**\n * Lists the models that transcribe as the user speaks, the realtime ones `MistralSTT` streams to, each one once with its aliases.\n *\n * The list is fetched once and shared by every call of the process for a\n * day. `refresh` fetches it again.\n */\n static listModels(options: {\n apiKey: string\n refresh?: boolean\n }): Promise<MicdropModel[]> {\n return listMistralModels(\n 'stt',\n ({ capabilities }) => capabilities.audio_transcription_realtime,\n options\n )\n }\n\n private socket: ReconnectingSocket\n // Utterances not done yet, oldest first. The server answers them in the\n // order their audio went out, so the transcript in progress is the oldest's.\n private utterances: Utterance[] = []\n // Flushes of utterances that timed out on this session, whose deltas and\n // transcription.done may still arrive and must not go to the next one. Each\n // is forgotten after another timeout, so an answer that never comes does not\n // swallow every transcript after it.\n private staleFlushes: number[] = []\n\n constructor(private options: MistralSTTOptions) {\n super()\n\n this.socket = new ReconnectingSocket({\n url: this.getURL(),\n clientOptions: {\n headers: {\n Authorization: `Bearer ${this.options.apiKey}`,\n },\n },\n connectionTimeout:\n this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT,\n retryDelay: this.options.retryDelay ?? DEFAULT_RETRY_DELAY,\n maxRetry: this.options.maxRetry ?? DEFAULT_MAX_RETRY,\n // The audio format must be set before any audio is appended\n handshake: {\n start: (socket) => socket.send(this.getSessionUpdate()),\n },\n log: (...message) => this.log(...message),\n })\n this.socket.on('Open', this.onOpen)\n this.socket.on('Message', this.onMessage)\n this.socket.on('Failed', this.onFailed)\n this.socket.connect()\n }\n\n transcribe(audioStream: Readable) {\n // After giving up, a new utterance gives the connection another chance\n this.socket.connect()\n\n const utterance: Utterance = { status: 'streaming', chunks: [], text: '' }\n this.utterances.push(utterance)\n\n // Read audio stream and send to Mistral\n audioStream.on('data', (chunk: Buffer) => {\n if (utterance.status !== 'streaming') return\n utterance.chunks.push(chunk)\n // A chunk that does not go out now goes with the others once connected\n if (this.sendAudioChunk(chunk)) {\n this.log(`Sent audio chunk (${chunk.byteLength} bytes)`)\n }\n })\n\n // Handle stream end: flush buffered audio so the server finalizes the\n // transcription (transcription.done). Unlike input_audio.end, a flush keeps\n // the session alive, so the same connection is reused for the next utterance.\n audioStream.on('end', () => this.endUtterance(utterance))\n }\n\n destroy() {\n super.destroy()\n this.socket.destroy()\n this.utterances.forEach((utterance) => this.dropUtterance(utterance))\n this.utterances = []\n }\n\n private getURL() {\n const model = this.options.model || DEFAULT_MODEL\n return `wss://api.mistral.ai/v1/audio/transcriptions/realtime?model=${encodeURIComponent(\n model\n )}`\n }\n\n private getSessionUpdate() {\n // Configure the audio format. Must be sent before any audio is appended;\n // Mistral rejects format updates once audio has started.\n return JSON.stringify({\n type: 'session.update',\n session: {\n audio_format: {\n encoding: this.options.encoding || DEFAULT_ENCODING,\n sample_rate: SAMPLE_RATE,\n },\n ...(this.options.targetStreamingDelayMs !== undefined && {\n target_streaming_delay_ms: this.options.targetStreamingDelayMs,\n }),\n },\n })\n }\n\n // A new session knows nothing of the previous one: every pending utterance\n // is sent again in full and replays its whole transcript, so the deltas it\n // gathered start over to avoid duplicating them\n private onOpen = ({ reconnected }: { reconnected: boolean }) => {\n this.staleFlushes = []\n const pending = this.utterances.filter(\n (utterance) => utterance.chunks.length > 0\n )\n if (pending.length === 0) return\n this.log(\n reconnected ? 'Sending audio chunks again' : 'Sending queued audio chunks'\n )\n for (const utterance of pending) {\n utterance.text = ''\n utterance.chunks.forEach((chunk) => this.sendAudioChunk(chunk))\n if (utterance.status === 'flushing') this.flushAudio(utterance)\n }\n }\n\n private onMessage = (data: RawData) => {\n let message: any\n try {\n message = JSON.parse(data.toString())\n } catch {\n this.log('Ignoring a message that is not JSON')\n return\n }\n\n switch (message.type) {\n case 'transcription.text.delta': {\n if (typeof message.text !== 'string') break\n this.log(`Received transcript delta: \"${message.text}\"`)\n // Deltas of an utterance that timed out are dropped with it\n if (this.hasStaleFlush()) break\n const utterance = this.utterances[0]\n if (utterance) utterance.text += message.text\n break\n }\n\n case 'transcription.done': {\n // The answer to a flush whose utterance already timed out\n if (this.hasStaleFlush()) {\n this.staleFlushes.shift()\n this.log('Ignoring the transcript of a timed out utterance')\n break\n }\n const utterance = this.utterances[0]\n // Only a flush finalizes a transcript: one that was not asked for\n // leaves the utterance in progress alone\n if (utterance?.status !== 'flushing') {\n this.log('Ignoring an unexpected transcription.done')\n break\n }\n const transcript =\n typeof message.text === 'string' ? message.text : utterance.text\n this.log(`Received completed transcript: \"${transcript}\"`)\n this.complete(utterance, transcript)\n break\n }\n\n case 'error':\n this.log('Error:', message.error ?? message)\n break\n\n default:\n break\n }\n }\n\n private onFailed = () => {\n // Every pending utterance goes back to the caller, for another STT\n const chunks = this.utterances.flatMap((utterance) => utterance.chunks)\n this.utterances.forEach((utterance) => this.dropUtterance(utterance))\n this.utterances = []\n this.emit('Failed', chunks)\n }\n\n private endUtterance(utterance: Utterance) {\n if (utterance.status !== 'streaming') return\n // A stream that carried no audio expects no transcript\n if (utterance.chunks.length === 0) {\n this.removeUtterance(utterance)\n return\n }\n utterance.status = 'flushing'\n // Not connected yet: the flush goes out with the audio, on `Open`\n if (this.socket.isOpen) this.flushAudio(utterance)\n }\n\n private flushAudio(utterance: Utterance) {\n // Flush buffered audio so the server processes the tail and emits a final\n // transcription.done for this utterance. We deliberately do NOT send\n // input_audio.end: that terminates the session (the server drops the\n // connection), whereas a flush finalizes the current utterance and keeps the\n // session ready for the next one.\n if (!this.socket.send(JSON.stringify({ type: 'input_audio.flush' }))) {\n return\n }\n this.log('Flushed audio input')\n\n // Timeout transcription if no transcript is received, with the deltas\n // received so far\n clearTimeout(utterance.timeout)\n utterance.timeout = setTimeout(() => {\n this.log('Transcription timeout')\n // Its flush is still answered later on this session, and that answer\n // belongs to no other utterance\n this.staleFlushes.push(Date.now() + this.transcriptionTimeout)\n this.complete(utterance, utterance.text)\n }, this.transcriptionTimeout)\n }\n\n private get transcriptionTimeout(): number {\n return this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT\n }\n\n /** Whether an answer to a timed out flush may still be on its way */\n private hasStaleFlush(): boolean {\n const now = Date.now()\n while (this.staleFlushes.length > 0 && this.staleFlushes[0] <= now) {\n this.log('Forgetting a flush that was never answered')\n this.staleFlushes.shift()\n }\n return this.staleFlushes.length > 0\n }\n\n private complete(utterance: Utterance, transcript: string) {\n if (utterance.status === 'done') return\n this.removeUtterance(utterance)\n this.emit('Transcript', transcript.trim())\n }\n\n private removeUtterance(utterance: Utterance) {\n this.dropUtterance(utterance)\n this.utterances = this.utterances.filter((u) => u !== utterance)\n }\n\n private dropUtterance(utterance: Utterance) {\n utterance.status = 'done'\n clearTimeout(utterance.timeout)\n utterance.timeout = undefined\n }\n\n private sendAudioChunk(chunk: Buffer): boolean {\n return this.socket.send(\n JSON.stringify({\n type: 'input_audio.append',\n audio: chunk.toString('base64'),\n })\n )\n }\n}\n","import {\n cachedVoices,\n float32ToPcm16,\n MicdropModel,\n MicdropVoice,\n SentenceTTS,\n voiceGender,\n voicesForLanguage,\n} from '@micdrop/server'\nimport { DEFAULT_BASE_URL, listMistralModels } from './models'\n\n/**\n * Mistral text to speech (Voxtral Speech 3)\n *\n * @see https://docs.mistral.ai/studio/audio/text-to-speech/overview\n * @see https://docs.mistral.ai/api/endpoint/audio/speech\n *\n * The speech endpoint reads a whole text at once, so the answer is cut into\n * sentences, and the audio of each one is emitted as the server streams it.\n * A sentence starts playing in about half a second and is generated several\n * times faster than it is spoken, so the next one is ready before the current\n * one ends.\n */\n\nexport interface MistralTTSOptions {\n apiKey: string\n model?: string\n // Preset voice (`voice_592` for Marie in French, `gb_jane_neutral`...) or\n // the id of a voice saved in your Mistral account\n voiceId?: string\n // A few seconds of speech to clone, base64 encoded, used instead of voiceId\n refAudio?: string\n // Extra fields of the request body, such as phoneme_hints\n settings?: Record<string, unknown>\n // API address, to reach the European or US endpoint\n baseUrl?: string\n}\n\nconst DEFAULT_MODEL = 'voxtral-mini-tts-3-0'\nconst DEFAULT_VOICE_ID = 'voice_3150' // James, American English\nconst MISTRAL_SAMPLE_RATE = 24000 // Rate of the pcm output from Mistral, sent as is\nconst FLOAT32_BYTES = 4 // The pcm output is mono float32 little-endian\nconst PAGE_SIZE = 100 // Voices per page of the listing\n\n/** A voice as the Mistral API lists it, the fields read here */\ninterface MistralVoice {\n id: string\n slug?: string | null\n name: string\n languages?: string[] | null\n gender?: string | null\n description?: string | null\n type?: string\n}\n\nexport class MistralTTS extends SentenceTTS {\n /**\n * Lists the models that speak, each one once with its aliases.\n *\n * The list is fetched once and shared by every call of the process for a\n * day. `refresh` fetches it again.\n */\n static listModels(options: {\n apiKey: string\n baseUrl?: string\n refresh?: boolean\n }): Promise<MicdropModel[]> {\n return listMistralModels(\n 'tts',\n ({ capabilities }) => capabilities.audio_speech,\n options\n )\n }\n\n /**\n * Lists the voices the account can speak with, the presets and its own.\n *\n * `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.\n *\n * The list is fetched once and shared by every call of the process for a\n * day. `refresh` fetches it again, for a voice added a moment ago.\n */\n static listVoices(options: {\n apiKey: string\n baseUrl?: string\n language?: string\n refresh?: boolean\n }): Promise<MicdropVoice[]> {\n const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/+$/, '')\n return cachedVoices(\n ['mistral', baseUrl],\n options.apiKey,\n () => fetchVoices(options.apiKey, baseUrl),\n options.refresh\n ).then((voices) => voicesForLanguage(voices, options.language))\n }\n\n constructor(private readonly options: MistralTTSOptions) {\n super()\n }\n\n get sampleRate() {\n return MISTRAL_SAMPLE_RATE\n }\n\n protected async synthesize(\n text: string,\n signal: AbortSignal\n ): Promise<undefined> {\n const response = await this.request(text, signal)\n if (!response.body) throw new Error('Mistral answered without a body')\n\n // Half of a float32 sample split between two events\n let leftover = Buffer.alloc(0)\n\n for await (const event of readEvents(response.body, signal)) {\n if (event.type === 'error') {\n throw new Error(\n `Speech generation failed: ${JSON.stringify(event.error ?? event)}`\n )\n }\n if (event.type !== 'speech.audio.delta' || !event.audio_data) continue\n\n const data = Buffer.concat([\n leftover,\n Buffer.from(event.audio_data, 'base64'),\n ])\n const length = data.length - (data.length % FLOAT32_BYTES)\n leftover = data.subarray(length)\n const samples = new Float32Array(length / FLOAT32_BYTES)\n for (let i = 0; i < samples.length; i++) {\n samples[i] = data.readFloatLE(i * FLOAT32_BYTES)\n }\n\n // Cancelled or replaced, the rest is not wanted\n if (!this.emitAudio(float32ToPcm16(samples))) break\n }\n\n return undefined\n }\n\n private async request(text: string, signal: AbortSignal) {\n const { options } = this\n const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/+$/, '')\n\n const response = await fetch(`${baseUrl}/v1/audio/speech`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${options.apiKey}`,\n 'Content-Type': 'application/json',\n Accept: 'text/event-stream',\n },\n signal,\n body: JSON.stringify({\n ...options.settings,\n model: options.model || DEFAULT_MODEL,\n input: text,\n ...(options.refAudio\n ? { ref_audio: options.refAudio }\n : { voice_id: options.voiceId || DEFAULT_VOICE_ID }),\n // Raw samples rather than wav, so chunks can be concatenated without a\n // header in the middle of the audio\n response_format: 'pcm',\n stream: true,\n }),\n })\n\n if (!response.ok) {\n const details = await response.text().catch(() => '')\n throw new Error(\n `Mistral answered ${response.status} ${response.statusText}: ${details}`\n )\n }\n\n return response\n }\n}\n\n/** Parses the server-sent events of the response, one JSON payload each. */\nasync function* readEvents(\n body: ReadableStream<Uint8Array>,\n signal: AbortSignal\n): AsyncGenerator<any> {\n const reader = body.getReader()\n const decoder = new TextDecoder()\n let buffer = ''\n\n try {\n while (!signal.aborted) {\n const { done, value } = await reader.read()\n if (done) break\n buffer += decoder.decode(value, { stream: true }).replace(/\\r\\n/g, '\\n')\n\n let end: number\n while ((end = buffer.indexOf('\\n\\n')) >= 0) {\n const block = buffer.slice(0, end)\n buffer = buffer.slice(end + 2)\n const data = block\n .split('\\n')\n .filter((line) => line.startsWith('data:'))\n .map((line) => line.slice(5).trimStart())\n .join('\\n')\n if (data) yield JSON.parse(data)\n }\n }\n } finally {\n // Closing the body closes the connection, and the server stops generating\n reader.cancel().catch(() => {})\n }\n}\n\n/** Reads every page of the voices of the account */\nasync function fetchVoices(\n apiKey: string,\n baseUrl: string\n): Promise<MicdropVoice[]> {\n const voices: MicdropVoice[] = []\n // The listing pages by offset, and ignores a page number\n for (let offset = 0, total = 1; offset < total; offset += PAGE_SIZE) {\n const response = await fetch(\n `${baseUrl}/v1/audio/voices?offset=${offset}&limit=${PAGE_SIZE}`,\n { headers: { Authorization: `Bearer ${apiKey}` } }\n )\n if (!response.ok) throw new Error(`Mistral answered ${response.status}`)\n const data = (await response.json()) as {\n items: MistralVoice[]\n total?: number\n }\n total = data.total ?? 0\n if (data.items.length === 0) break\n for (const voice of data.items) {\n voices.push({\n // A preset goes by its slug, a voice of the account by its id\n id: voice.slug || voice.id,\n name: voice.name,\n // Mistral writes en_us where the other providers write en-US\n languages: voice.languages?.map((code) => {\n const [language, region] = code.split('_')\n return region ? `${language}-${region.toUpperCase()}` : language\n }),\n gender: voiceGender(voice.gender),\n description: voice.description || undefined,\n custom: voice.type !== 'preset',\n })\n }\n }\n return voices\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAAoC;AAEpC,uBAAwB;AAMxB,iBAAgC;;;ACGzB,SAAS,OAAO,SAAsD;AAC3E,MAAI,OAAO,YAAY,SAAU,QAAO;AACxC,MAAI,CAAC,MAAM,QAAQ,OAAO,EAAG,QAAO;AACpC,SAAO,QACJ,IAAI,CAAC,UAAW,MAAM,SAAS,SAAS,MAAM,OAAO,EAAG,EACxD,KAAK,EAAE;AACZ;;;ACjBA,oBAAuD;AAEhD,IAAM,mBAAmB;AAchC,SAAS,SAAS,QAAgB,SAA6C;AAC7E,aAAO,0BAAW,CAAC,sBAAsB,OAAO,GAAG,QAAQ,YAAY;AACrE,UAAM,WAAW,MAAM,MAAM,GAAG,OAAO,cAAc;AAAA,MACnD,SAAS,EAAE,eAAe,UAAU,MAAM,GAAG;AAAA,IAC/C,CAAC;AACD,QAAI,CAAC,SAAS,GAAI,OAAM,IAAI,MAAM,oBAAoB,SAAS,MAAM,EAAE;AACvE,UAAM,EAAE,KAAK,IAAK,MAAM,S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A,IACH;AAAA,EACF;AACA,SAAO;AACT;","names":["import_server","result","z","import_server","DEFAULT_MODEL","DEFAULT_RETRY_DELAY","DEFAULT_MAX_RETRY","import_server","DEFAULT_MODEL"]}
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,59 @@
|
|
|
2
2
|
import { Agent } from "@micdrop/server";
|
|
3
3
|
import { Mistral } from "@mistralai/mistralai";
|
|
4
4
|
import z, { toJSONSchema } from "zod";
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
// src/deltaText.ts
|
|
7
|
+
function textOf(content) {
|
|
8
|
+
if (typeof content === "string") return content;
|
|
9
|
+
if (!Array.isArray(content)) return void 0;
|
|
10
|
+
return content.map((chunk) => chunk.type === "text" ? chunk.text : "").join("");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/models.ts
|
|
14
|
+
import { cachedList, cachedModels } from "@micdrop/server";
|
|
15
|
+
var DEFAULT_BASE_URL = "https://api.mistral.ai";
|
|
16
|
+
function fetchAll(apiKey, baseUrl) {
|
|
17
|
+
return cachedList(["mistral-raw-models", baseUrl], apiKey, async () => {
|
|
18
|
+
const response = await fetch(`${baseUrl}/v1/models`, {
|
|
19
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
20
|
+
});
|
|
21
|
+
if (!response.ok) throw new Error(`Mistral answered ${response.status}`);
|
|
22
|
+
const { data } = await response.json();
|
|
23
|
+
return data;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function listMistralModels(component, keep, options) {
|
|
27
|
+
const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
28
|
+
return cachedModels(
|
|
29
|
+
["mistral", component, baseUrl],
|
|
30
|
+
options.apiKey,
|
|
31
|
+
async () => {
|
|
32
|
+
const models = [];
|
|
33
|
+
const seen = /* @__PURE__ */ new Set();
|
|
34
|
+
for (const model of await fetchAll(options.apiKey, baseUrl)) {
|
|
35
|
+
if (!keep(model) || seen.has(model.id)) continue;
|
|
36
|
+
const names = [model.id, ...model.aliases ?? []];
|
|
37
|
+
names.forEach((name) => seen.add(name));
|
|
38
|
+
const id = names.find((name) => name.endsWith("-latest")) ?? model.id;
|
|
39
|
+
models.push({
|
|
40
|
+
id,
|
|
41
|
+
name: model.name || void 0,
|
|
42
|
+
description: model.description || void 0,
|
|
43
|
+
aliases: names.filter((name) => name !== id),
|
|
44
|
+
languages: model.audio_transcription_capabilities?.languages ?? void 0,
|
|
45
|
+
reasoning: !!model.capabilities.reasoning,
|
|
46
|
+
deprecated: !!model.deprecation,
|
|
47
|
+
shutdownDate: model.deprecation?.slice(0, 10) || void 0
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return models;
|
|
51
|
+
},
|
|
52
|
+
options.refresh
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/MistralAgent.ts
|
|
57
|
+
var DEFAULT_MODEL = "mistral-medium-latest";
|
|
6
58
|
var DEFAULT_MAX_RETRY = 3;
|
|
7
59
|
var DEFAULT_MAX_STEPS = 5;
|
|
8
60
|
var DEFAULT_RETRY_DELAY = 500;
|
|
@@ -13,6 +65,19 @@ var StepInterruptedError = class extends Error {
|
|
|
13
65
|
}
|
|
14
66
|
};
|
|
15
67
|
var MistralAgent = class extends Agent {
|
|
68
|
+
/**
|
|
69
|
+
* Lists the models that can answer a call: chat models that call tools. `reasoning` marks the ones that can reason, slower to answer when they do, each one once with its aliases.
|
|
70
|
+
*
|
|
71
|
+
* The list is fetched once and shared by every call of the process for a
|
|
72
|
+
* day. `refresh` fetches it again.
|
|
73
|
+
*/
|
|
74
|
+
static listModels(options) {
|
|
75
|
+
return listMistralModels(
|
|
76
|
+
"agent",
|
|
77
|
+
({ capabilities }) => capabilities.completion_chat && capabilities.function_calling,
|
|
78
|
+
options
|
|
79
|
+
);
|
|
80
|
+
}
|
|
16
81
|
constructor(options) {
|
|
17
82
|
super(options);
|
|
18
83
|
this.mistral = new Mistral({ apiKey: options.apiKey });
|
|
@@ -56,6 +121,10 @@ var MistralAgent = class extends Agent {
|
|
|
56
121
|
});
|
|
57
122
|
return;
|
|
58
123
|
}
|
|
124
|
+
if (!outcome.ranTools) {
|
|
125
|
+
this.log("Empty answer");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
59
128
|
}
|
|
60
129
|
console.error("[MistralAgent] Max steps reached");
|
|
61
130
|
}
|
|
@@ -73,9 +142,12 @@ var MistralAgent = class extends Agent {
|
|
|
73
142
|
content: ""
|
|
74
143
|
});
|
|
75
144
|
}
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
|
|
145
|
+
const write = this.answerWriter(stream);
|
|
146
|
+
const outcome = {
|
|
147
|
+
fullAnswer: "",
|
|
148
|
+
skipAnswer: false,
|
|
149
|
+
ranTools: false
|
|
150
|
+
};
|
|
79
151
|
let produced = false;
|
|
80
152
|
try {
|
|
81
153
|
const result = await this.mistral.chat.stream(
|
|
@@ -87,47 +159,44 @@ var MistralAgent = class extends Agent {
|
|
|
87
159
|
},
|
|
88
160
|
{ signal }
|
|
89
161
|
);
|
|
162
|
+
const toolCalls = [];
|
|
163
|
+
const toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
90
164
|
for await (const event of result) {
|
|
91
165
|
if (signal.aborted) return outcome;
|
|
92
166
|
const delta = event.data.choices[0]?.delta;
|
|
93
|
-
const chunk = delta?.content;
|
|
94
|
-
|
|
95
|
-
if (typeof chunk === "string") {
|
|
96
|
-
if (chunk === "") continue;
|
|
167
|
+
const chunk = textOf(delta?.content);
|
|
168
|
+
if (chunk) {
|
|
97
169
|
this.log(`Answer chunk: "${chunk}"`);
|
|
98
170
|
outcome.fullAnswer += chunk;
|
|
99
171
|
produced = true;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
} else {
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
stream.write(chunk);
|
|
116
|
-
} else if (toolCalls?.length) {
|
|
117
|
-
for (const toolCall of toolCalls) {
|
|
118
|
-
if (signal.aborted) return outcome;
|
|
119
|
-
produced = true;
|
|
120
|
-
const result2 = await this.executeTool({
|
|
121
|
-
role: "tool_call",
|
|
122
|
-
toolCallId: toolCall.id || "",
|
|
123
|
-
toolName: toolCall.function.name,
|
|
124
|
-
// The SDK hands the arguments over parsed or as JSON
|
|
125
|
-
parameters: typeof toolCall.function.arguments === "string" ? toolCall.function.arguments : JSON.stringify(toolCall.function.arguments)
|
|
126
|
-
});
|
|
127
|
-
if (result2.skipAnswer) {
|
|
128
|
-
outcome.skipAnswer = true;
|
|
129
|
-
}
|
|
172
|
+
write(chunk);
|
|
173
|
+
}
|
|
174
|
+
for (const [position, fragment] of (delta?.toolCalls ?? []).entries()) {
|
|
175
|
+
const index = fragment.index ?? position;
|
|
176
|
+
let toolCall = toolCallsByIndex.get(index);
|
|
177
|
+
if (!toolCall || fragment.id && toolCall.id && fragment.id !== toolCall.id) {
|
|
178
|
+
toolCall = { id: "", name: "", arguments: "" };
|
|
179
|
+
toolCalls.push(toolCall);
|
|
180
|
+
toolCallsByIndex.set(index, toolCall);
|
|
130
181
|
}
|
|
182
|
+
if (fragment.id) toolCall.id = fragment.id;
|
|
183
|
+
if (fragment.function.name) toolCall.name = fragment.function.name;
|
|
184
|
+
const args = fragment.function.arguments;
|
|
185
|
+
toolCall.arguments += typeof args === "string" ? args : JSON.stringify(args);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
for (const toolCall of toolCalls) {
|
|
189
|
+
if (signal.aborted) return outcome;
|
|
190
|
+
produced = true;
|
|
191
|
+
outcome.ranTools = true;
|
|
192
|
+
const result2 = await this.executeTool({
|
|
193
|
+
role: "tool_call",
|
|
194
|
+
toolCallId: toolCall.id,
|
|
195
|
+
toolName: toolCall.name,
|
|
196
|
+
parameters: toolCall.arguments || "{}"
|
|
197
|
+
});
|
|
198
|
+
if (result2.skipAnswer) {
|
|
199
|
+
outcome.skipAnswer = true;
|
|
131
200
|
}
|
|
132
201
|
}
|
|
133
202
|
} catch (error) {
|
|
@@ -299,6 +368,19 @@ var MistralSTT = class extends STT {
|
|
|
299
368
|
this.socket.on("Failed", this.onFailed);
|
|
300
369
|
this.socket.connect();
|
|
301
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* Lists the models that transcribe as the user speaks, the realtime ones `MistralSTT` streams to, each one once with its aliases.
|
|
373
|
+
*
|
|
374
|
+
* The list is fetched once and shared by every call of the process for a
|
|
375
|
+
* day. `refresh` fetches it again.
|
|
376
|
+
*/
|
|
377
|
+
static listModels(options) {
|
|
378
|
+
return listMistralModels(
|
|
379
|
+
"stt",
|
|
380
|
+
({ capabilities }) => capabilities.audio_transcription_realtime,
|
|
381
|
+
options
|
|
382
|
+
);
|
|
383
|
+
}
|
|
302
384
|
transcribe(audioStream) {
|
|
303
385
|
this.socket.connect();
|
|
304
386
|
const utterance = { status: "streaming", chunks: [], text: "" };
|
|
@@ -396,25 +478,59 @@ var MistralSTT = class extends STT {
|
|
|
396
478
|
};
|
|
397
479
|
|
|
398
480
|
// src/MistralTTS.ts
|
|
399
|
-
import {
|
|
481
|
+
import {
|
|
482
|
+
cachedVoices,
|
|
483
|
+
float32ToPcm16,
|
|
484
|
+
SentenceTTS,
|
|
485
|
+
voiceGender,
|
|
486
|
+
voicesForLanguage
|
|
487
|
+
} from "@micdrop/server";
|
|
400
488
|
var DEFAULT_MODEL3 = "voxtral-mini-tts-3-0";
|
|
401
489
|
var DEFAULT_VOICE_ID = "voice_3150";
|
|
402
|
-
var DEFAULT_BASE_URL = "https://api.mistral.ai";
|
|
403
490
|
var MISTRAL_SAMPLE_RATE = 24e3;
|
|
404
|
-
var OUTPUT_SAMPLE_RATE = 16e3;
|
|
405
491
|
var FLOAT32_BYTES = 4;
|
|
492
|
+
var PAGE_SIZE = 100;
|
|
406
493
|
var MistralTTS = class extends SentenceTTS {
|
|
407
494
|
constructor(options) {
|
|
408
495
|
super();
|
|
409
496
|
this.options = options;
|
|
410
497
|
}
|
|
498
|
+
/**
|
|
499
|
+
* Lists the models that speak, each one once with its aliases.
|
|
500
|
+
*
|
|
501
|
+
* The list is fetched once and shared by every call of the process for a
|
|
502
|
+
* day. `refresh` fetches it again.
|
|
503
|
+
*/
|
|
504
|
+
static listModels(options) {
|
|
505
|
+
return listMistralModels(
|
|
506
|
+
"tts",
|
|
507
|
+
({ capabilities }) => capabilities.audio_speech,
|
|
508
|
+
options
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Lists the voices the account can speak with, the presets and its own.
|
|
513
|
+
*
|
|
514
|
+
* `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
|
|
515
|
+
*
|
|
516
|
+
* The list is fetched once and shared by every call of the process for a
|
|
517
|
+
* day. `refresh` fetches it again, for a voice added a moment ago.
|
|
518
|
+
*/
|
|
519
|
+
static listVoices(options) {
|
|
520
|
+
const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
521
|
+
return cachedVoices(
|
|
522
|
+
["mistral", baseUrl],
|
|
523
|
+
options.apiKey,
|
|
524
|
+
() => fetchVoices(options.apiKey, baseUrl),
|
|
525
|
+
options.refresh
|
|
526
|
+
).then((voices) => voicesForLanguage(voices, options.language));
|
|
527
|
+
}
|
|
528
|
+
get sampleRate() {
|
|
529
|
+
return MISTRAL_SAMPLE_RATE;
|
|
530
|
+
}
|
|
411
531
|
async synthesize(text, signal) {
|
|
412
532
|
const response = await this.request(text, signal);
|
|
413
533
|
if (!response.body) throw new Error("Mistral answered without a body");
|
|
414
|
-
const resampler = new Pcm16Resampler(
|
|
415
|
-
MISTRAL_SAMPLE_RATE,
|
|
416
|
-
OUTPUT_SAMPLE_RATE
|
|
417
|
-
);
|
|
418
534
|
let leftover = Buffer.alloc(0);
|
|
419
535
|
for await (const event of readEvents(response.body, signal)) {
|
|
420
536
|
if (event.type === "error") {
|
|
@@ -433,7 +549,7 @@ var MistralTTS = class extends SentenceTTS {
|
|
|
433
549
|
for (let i = 0; i < samples.length; i++) {
|
|
434
550
|
samples[i] = data.readFloatLE(i * FLOAT32_BYTES);
|
|
435
551
|
}
|
|
436
|
-
if (!this.emitAudio(
|
|
552
|
+
if (!this.emitAudio(float32ToPcm16(samples))) break;
|
|
437
553
|
}
|
|
438
554
|
return void 0;
|
|
439
555
|
}
|
|
@@ -490,6 +606,35 @@ async function* readEvents(body, signal) {
|
|
|
490
606
|
});
|
|
491
607
|
}
|
|
492
608
|
}
|
|
609
|
+
async function fetchVoices(apiKey, baseUrl) {
|
|
610
|
+
const voices = [];
|
|
611
|
+
for (let offset = 0, total = 1; offset < total; offset += PAGE_SIZE) {
|
|
612
|
+
const response = await fetch(
|
|
613
|
+
`${baseUrl}/v1/audio/voices?offset=${offset}&limit=${PAGE_SIZE}`,
|
|
614
|
+
{ headers: { Authorization: `Bearer ${apiKey}` } }
|
|
615
|
+
);
|
|
616
|
+
if (!response.ok) throw new Error(`Mistral answered ${response.status}`);
|
|
617
|
+
const data = await response.json();
|
|
618
|
+
total = data.total ?? 0;
|
|
619
|
+
if (data.items.length === 0) break;
|
|
620
|
+
for (const voice of data.items) {
|
|
621
|
+
voices.push({
|
|
622
|
+
// A preset goes by its slug, a voice of the account by its id
|
|
623
|
+
id: voice.slug || voice.id,
|
|
624
|
+
name: voice.name,
|
|
625
|
+
// Mistral writes en_us where the other providers write en-US
|
|
626
|
+
languages: voice.languages?.map((code) => {
|
|
627
|
+
const [language, region] = code.split("_");
|
|
628
|
+
return region ? `${language}-${region.toUpperCase()}` : language;
|
|
629
|
+
}),
|
|
630
|
+
gender: voiceGender(voice.gender),
|
|
631
|
+
description: voice.description || void 0,
|
|
632
|
+
custom: voice.type !== "preset"
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return voices;
|
|
637
|
+
}
|
|
493
638
|
export {
|
|
494
639
|
MistralAgent,
|
|
495
640
|
MistralSTT,
|