@micdrop/mistral 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -44,17 +44,67 @@ var DEFAULT_MODEL = "mistral-large-latest";
44
44
  var DEFAULT_MAX_RETRY = 3;
45
45
  var DEFAULT_MAX_STEPS = 5;
46
46
  var DEFAULT_RETRY_DELAY = 500;
47
+ var StepInterruptedError = class extends Error {
48
+ constructor(outcome) {
49
+ super("Answer interrupted");
50
+ this.outcome = outcome;
51
+ }
52
+ };
47
53
  var MistralAgent = class extends import_server.Agent {
48
54
  constructor(options) {
49
55
  super(options);
50
56
  this.mistral = new import_mistralai.Mistral({ apiKey: options.apiKey });
51
57
  }
52
- async generateAnswer(stream, stepCount = 0, tryCount = 0) {
53
- if (stepCount >= (this.options.maxSteps || DEFAULT_MAX_STEPS)) {
54
- console.error("[MistralAgent] Max steps reached");
55
- return;
58
+ async generateAnswer(stream, signal) {
59
+ const maxSteps = this.options.maxSteps || DEFAULT_MAX_STEPS;
60
+ const maxRetry = this.options.maxRetry || DEFAULT_MAX_RETRY;
61
+ for (let step = 0; step < maxSteps; step++) {
62
+ let outcome;
63
+ for (let tryCount = 0; !outcome; tryCount++) {
64
+ try {
65
+ outcome = await this.generateStep(stream, signal);
66
+ } catch (error) {
67
+ if (signal.aborted) return;
68
+ if (error instanceof StepInterruptedError) {
69
+ outcome = error.outcome;
70
+ break;
71
+ }
72
+ console.error("[MistralAgent] Error:", error);
73
+ if (tryCount >= maxRetry) {
74
+ this.log("Max retries reached, giving up");
75
+ this.emit("Failed");
76
+ return;
77
+ }
78
+ await new Promise(
79
+ (resolve) => setTimeout(resolve, this.options.retryDelay || DEFAULT_RETRY_DELAY)
80
+ );
81
+ if (signal.aborted) return;
82
+ }
83
+ }
84
+ if (signal.aborted) return;
85
+ if (outcome.fullAnswer) {
86
+ const { message, metadata } = this.extract(outcome.fullAnswer);
87
+ this.addAssistantMessage(message, metadata);
88
+ return;
89
+ }
90
+ if (outcome.skipAnswer) {
91
+ this.conversation.push({
92
+ role: "assistant",
93
+ content: " "
94
+ });
95
+ return;
96
+ }
56
97
  }
57
- const answerCount = this.answerCount;
98
+ console.error("[MistralAgent] Max steps reached");
99
+ }
100
+ /**
101
+ * Runs one request, writing its text to the stream and running its tools.
102
+ *
103
+ * It throws a plain error when the request failed before producing
104
+ * anything, which can then be retried, and a {@link StepInterruptedError}
105
+ * once something was written or done.
106
+ */
107
+ async generateStep(stream, signal) {
58
108
  if (this.conversation.length === 1) {
59
109
  this.conversation.push({
60
110
  role: "user",
@@ -63,30 +113,36 @@ var MistralAgent = class extends import_server.Agent {
63
113
  }
64
114
  let extracting = false;
65
115
  const extractOptions = this.getExtractOptions();
116
+ const outcome = { fullAnswer: "", skipAnswer: false };
117
+ let produced = false;
66
118
  try {
67
- const result = await this.mistral.chat.stream({
68
- model: this.options.model || DEFAULT_MODEL,
69
- messages: this.buildMessages(),
70
- tools: this.buildTools(),
71
- ...this.options.settings
72
- });
73
- let fullAnswer = "";
74
- let skipAnswer = false;
119
+ const result = await this.mistral.chat.stream(
120
+ {
121
+ model: this.options.model || DEFAULT_MODEL,
122
+ messages: this.buildMessages(),
123
+ tools: this.buildTools(),
124
+ ...this.options.settings
125
+ },
126
+ { signal }
127
+ );
75
128
  for await (const event of result) {
76
- if (answerCount !== this.answerCount) return;
129
+ if (signal.aborted) return outcome;
77
130
  const delta = event.data.choices[0]?.delta;
78
131
  const chunk = delta?.content;
79
132
  const toolCalls = delta?.toolCalls;
80
133
  if (typeof chunk === "string") {
81
134
  if (chunk === "") continue;
82
135
  this.log(`Answer chunk: "${chunk}"`);
83
- fullAnswer += chunk;
136
+ outcome.fullAnswer += chunk;
137
+ produced = true;
84
138
  if (extractOptions) {
85
139
  if (!extracting) {
86
- const startTagIndex = fullAnswer.indexOf(extractOptions.startTag);
140
+ const startTagIndex = outcome.fullAnswer.indexOf(
141
+ extractOptions.startTag
142
+ );
87
143
  if (startTagIndex !== -1) {
88
144
  extracting = true;
89
- const messagePart = fullAnswer.slice(0, startTagIndex).trimEnd();
145
+ const messagePart = outcome.fullAnswer.slice(0, startTagIndex).trimEnd();
90
146
  stream.write(messagePart);
91
147
  continue;
92
148
  }
@@ -97,42 +153,27 @@ var MistralAgent = class extends import_server.Agent {
97
153
  stream.write(chunk);
98
154
  } else if (toolCalls?.length) {
99
155
  for (const toolCall of toolCalls) {
156
+ if (signal.aborted) return outcome;
157
+ produced = true;
100
158
  const result2 = await this.executeTool({
101
159
  role: "tool_call",
102
160
  toolCallId: toolCall.id || "",
103
161
  toolName: toolCall.function.name,
104
- parameters: toolCall.function.arguments
162
+ // The SDK hands the arguments over parsed or as JSON
163
+ parameters: typeof toolCall.function.arguments === "string" ? toolCall.function.arguments : JSON.stringify(toolCall.function.arguments)
105
164
  });
106
165
  if (result2.skipAnswer) {
107
- skipAnswer = true;
166
+ outcome.skipAnswer = true;
108
167
  }
109
168
  }
110
169
  }
111
170
  }
112
- if (answerCount !== this.answerCount) return;
113
- if (fullAnswer) {
114
- const { message, metadata } = this.extract(fullAnswer);
115
- this.addAssistantMessage(message, metadata);
116
- } else if (skipAnswer) {
117
- this.conversation.push({
118
- role: "assistant",
119
- content: " "
120
- });
121
- } else {
122
- await this.generateAnswer(stream, stepCount + 1);
123
- }
124
171
  } catch (error) {
125
- console.error("[MistralAgent] Error:", error);
126
- if (tryCount < (this.options.maxRetry || DEFAULT_MAX_RETRY)) {
127
- await new Promise(
128
- (resolve) => setTimeout(resolve, this.options.retryDelay || DEFAULT_RETRY_DELAY)
129
- );
130
- await this.generateAnswer(stream, stepCount, tryCount + 1);
131
- } else {
132
- this.log("Max retries reached, giving up");
133
- this.emit("Failed");
134
- }
172
+ if (!produced || signal.aborted) throw error;
173
+ console.error("[MistralAgent] Error after a partial answer:", error);
174
+ throw new StepInterruptedError(outcome);
135
175
  }
176
+ return outcome;
136
177
  }
137
178
  buildMessages() {
138
179
  return this.conversation.map(
@@ -154,7 +195,8 @@ var MistralAgent = class extends import_server.Agent {
154
195
  id: message.toolCallId,
155
196
  function: {
156
197
  name: message.toolName,
157
- arguments: JSON.stringify(message.parameters)
198
+ // Already the JSON the model wrote
199
+ arguments: message.parameters
158
200
  }
159
201
  }
160
202
  ]
@@ -187,17 +229,10 @@ var MistralAgent = class extends import_server.Agent {
187
229
  }
188
230
  }));
189
231
  }
190
- cancel() {
191
- if (!this.answering) return;
192
- this.log("Cancel");
193
- this.answering = false;
194
- this.answerCount++;
195
- }
196
232
  };
197
233
 
198
234
  // src/MistralSTT.ts
199
235
  var import_server2 = require("@micdrop/server");
200
- var import_ws = __toESM(require("ws"));
201
236
  var DEFAULT_MODEL2 = "voxtral-mini-transcribe-realtime-3-0";
202
237
  var DEFAULT_ENCODING = "pcm_s16le";
203
238
  var SAMPLE_RATE = 16e3;
@@ -209,62 +244,117 @@ var MistralSTT = class extends import_server2.STT {
209
244
  constructor(options) {
210
245
  super();
211
246
  this.options = options;
212
- this.audioChunksPending = [];
213
- // Store audio chunks to send them again if reconnecting
214
- this.transcriptDelta = "";
215
- // Accumulated transcription.text.delta chunks
216
- this.retryCount = 0;
217
- this.destroyed = false;
218
- this.initPromise = this.connect();
247
+ // Utterances not done yet, oldest first. The server answers them in the
248
+ // order their audio went out, so the transcript in progress is the oldest's.
249
+ this.utterances = [];
250
+ // Flushes of utterances that timed out on this session, whose deltas and
251
+ // transcription.done may still arrive and must not go to the next one. Each
252
+ // is forgotten after another timeout, so an answer that never comes does not
253
+ // swallow every transcript after it.
254
+ this.staleFlushes = [];
255
+ // A new session knows nothing of the previous one: every pending utterance
256
+ // is sent again in full and replays its whole transcript, so the deltas it
257
+ // gathered start over to avoid duplicating them
258
+ this.onOpen = ({ reconnected }) => {
259
+ this.staleFlushes = [];
260
+ const pending = this.utterances.filter(
261
+ (utterance) => utterance.chunks.length > 0
262
+ );
263
+ if (pending.length === 0) return;
264
+ this.log(
265
+ reconnected ? "Sending audio chunks again" : "Sending queued audio chunks"
266
+ );
267
+ for (const utterance of pending) {
268
+ utterance.text = "";
269
+ utterance.chunks.forEach((chunk) => this.sendAudioChunk(chunk));
270
+ if (utterance.status === "flushing") this.flushAudio(utterance);
271
+ }
272
+ };
273
+ this.onMessage = (data) => {
274
+ let message;
275
+ try {
276
+ message = JSON.parse(data.toString());
277
+ } catch {
278
+ this.log("Ignoring a message that is not JSON");
279
+ return;
280
+ }
281
+ switch (message.type) {
282
+ case "transcription.text.delta": {
283
+ if (typeof message.text !== "string") break;
284
+ this.log(`Received transcript delta: "${message.text}"`);
285
+ if (this.hasStaleFlush()) break;
286
+ const utterance = this.utterances[0];
287
+ if (utterance) utterance.text += message.text;
288
+ break;
289
+ }
290
+ case "transcription.done": {
291
+ if (this.hasStaleFlush()) {
292
+ this.staleFlushes.shift();
293
+ this.log("Ignoring the transcript of a timed out utterance");
294
+ break;
295
+ }
296
+ const utterance = this.utterances[0];
297
+ if (utterance?.status !== "flushing") {
298
+ this.log("Ignoring an unexpected transcription.done");
299
+ break;
300
+ }
301
+ const transcript = typeof message.text === "string" ? message.text : utterance.text;
302
+ this.log(`Received completed transcript: "${transcript}"`);
303
+ this.complete(utterance, transcript);
304
+ break;
305
+ }
306
+ case "error":
307
+ this.log("Error:", message.error ?? message);
308
+ break;
309
+ default:
310
+ break;
311
+ }
312
+ };
313
+ this.onFailed = () => {
314
+ const chunks = this.utterances.flatMap((utterance) => utterance.chunks);
315
+ this.utterances.forEach((utterance) => this.dropUtterance(utterance));
316
+ this.utterances = [];
317
+ this.emit("Failed", chunks);
318
+ };
319
+ this.socket = new import_server2.ReconnectingSocket({
320
+ url: this.getURL(),
321
+ clientOptions: {
322
+ headers: {
323
+ Authorization: `Bearer ${this.options.apiKey}`
324
+ }
325
+ },
326
+ connectionTimeout: this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT,
327
+ retryDelay: this.options.retryDelay ?? DEFAULT_RETRY_DELAY2,
328
+ maxRetry: this.options.maxRetry ?? DEFAULT_MAX_RETRY2,
329
+ // The audio format must be set before any audio is appended
330
+ handshake: {
331
+ start: (socket) => socket.send(this.getSessionUpdate())
332
+ },
333
+ log: (...message) => this.log(...message)
334
+ });
335
+ this.socket.on("Open", this.onOpen);
336
+ this.socket.on("Message", this.onMessage);
337
+ this.socket.on("Failed", this.onFailed);
338
+ this.socket.connect();
219
339
  }
220
340
  transcribe(audioStream) {
221
- let started = false;
222
- audioStream.on("data", async (chunk) => {
223
- if (!started) {
224
- started = true;
225
- this.transcriptDelta = "";
341
+ this.socket.connect();
342
+ const utterance = { status: "streaming", chunks: [], text: "" };
343
+ this.utterances.push(utterance);
344
+ audioStream.on("data", (chunk) => {
345
+ if (utterance.status !== "streaming") return;
346
+ utterance.chunks.push(chunk);
347
+ if (this.sendAudioChunk(chunk)) {
348
+ this.log(`Sent audio chunk (${chunk.byteLength} bytes)`);
226
349
  }
227
- this.audioChunksPending.push(chunk);
228
- await this.initPromise;
229
- this.sendAudioChunk(chunk);
230
- this.log(`Sent audio chunk (${chunk.byteLength} bytes)`);
231
- });
232
- audioStream.on("end", async () => {
233
- await this.initPromise;
234
- if (this.audioChunksPending.length === 0) return;
235
- this.flushAudio();
236
- this.transcriptionTimeout = setTimeout(() => {
237
- this.transcriptionTimeout = void 0;
238
- this.log("Transcription timeout");
239
- this.emit("Transcript", this.transcriptDelta.trim());
240
- this.transcriptDelta = "";
241
- this.audioChunksPending.length = 0;
242
- }, this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT);
243
350
  });
351
+ audioStream.on("end", () => this.endUtterance(utterance));
244
352
  }
245
353
  destroy() {
246
354
  super.destroy();
247
- this.destroyed = true;
248
- if (this.reconnectTimeout) {
249
- clearTimeout(this.reconnectTimeout);
250
- this.reconnectTimeout = void 0;
251
- }
252
- if (this.transcriptionTimeout) {
253
- clearTimeout(this.transcriptionTimeout);
254
- this.transcriptionTimeout = void 0;
255
- }
256
- if (this.connectionTimeout) {
257
- clearTimeout(this.connectionTimeout);
258
- this.connectionTimeout = void 0;
259
- }
260
- if (this.socket) closeSocket(this.socket);
261
- this.socket = void 0;
262
- }
263
- connect() {
264
- return this.initWS().catch((error) => {
265
- console.error("[MistralSTT] Connection error:", error);
266
- this.reconnect();
267
- });
355
+ this.socket.destroy();
356
+ this.utterances.forEach((utterance) => this.dropUtterance(utterance));
357
+ this.utterances = [];
268
358
  }
269
359
  getURL() {
270
360
  const model = this.options.model || DEFAULT_MODEL2;
@@ -272,142 +362,76 @@ var MistralSTT = class extends import_server2.STT {
272
362
  model
273
363
  )}`;
274
364
  }
275
- async initWS() {
276
- return new Promise((resolve, reject) => {
277
- const socket = new import_ws.default(this.getURL(), {
278
- headers: {
279
- Authorization: `Bearer ${this.options.apiKey}`
280
- }
281
- });
282
- this.socket = socket;
283
- const timeout = setTimeout(() => {
284
- this.log("Connection timeout");
285
- closeSocket(socket);
286
- this.socket = void 0;
287
- reject(new Error("WebSocket connection timeout"));
288
- }, this.options.connectionTimeout ?? DEFAULT_CONNECTION_TIMEOUT);
289
- this.connectionTimeout = timeout;
290
- socket.addEventListener("open", () => {
291
- clearTimeout(timeout);
292
- this.sendSessionUpdate();
293
- this.log("Connection opened");
294
- resolve();
295
- });
296
- socket.addEventListener("error", (error) => {
297
- clearTimeout(timeout);
298
- this.log("WebSocket error:", error);
299
- reject(new Error("WebSocket connection error"));
300
- });
301
- socket.addEventListener("close", ({ code, reason }) => {
302
- clearTimeout(timeout);
303
- this.socket?.removeAllListeners();
304
- this.socket = void 0;
305
- if (code !== 1e3) {
306
- this.reconnect();
307
- } else {
308
- this.log("Connection closed", { code, reason });
365
+ getSessionUpdate() {
366
+ return JSON.stringify({
367
+ type: "session.update",
368
+ session: {
369
+ audio_format: {
370
+ encoding: this.options.encoding || DEFAULT_ENCODING,
371
+ sample_rate: SAMPLE_RATE
372
+ },
373
+ ...this.options.targetStreamingDelayMs !== void 0 && {
374
+ target_streaming_delay_ms: this.options.targetStreamingDelayMs
309
375
  }
310
- });
311
- socket.addEventListener("message", (event) => {
312
- this.handleMessage(JSON.parse(event.data.toString()));
313
- });
376
+ }
314
377
  });
315
378
  }
316
- sendSessionUpdate() {
317
- if (!this.socket) return;
318
- this.socket.send(
319
- JSON.stringify({
320
- type: "session.update",
321
- session: {
322
- audio_format: {
323
- encoding: this.options.encoding || DEFAULT_ENCODING,
324
- sample_rate: SAMPLE_RATE
325
- },
326
- ...this.options.targetStreamingDelayMs !== void 0 && {
327
- target_streaming_delay_ms: this.options.targetStreamingDelayMs
328
- }
329
- }
330
- })
331
- );
379
+ endUtterance(utterance) {
380
+ if (utterance.status !== "streaming") return;
381
+ if (utterance.chunks.length === 0) {
382
+ this.removeUtterance(utterance);
383
+ return;
384
+ }
385
+ utterance.status = "flushing";
386
+ if (this.socket.isOpen) this.flushAudio(utterance);
387
+ }
388
+ flushAudio(utterance) {
389
+ if (!this.socket.send(JSON.stringify({ type: "input_audio.flush" }))) {
390
+ return;
391
+ }
392
+ this.log("Flushed audio input");
393
+ clearTimeout(utterance.timeout);
394
+ utterance.timeout = setTimeout(() => {
395
+ this.log("Transcription timeout");
396
+ this.staleFlushes.push(Date.now() + this.transcriptionTimeout);
397
+ this.complete(utterance, utterance.text);
398
+ }, this.transcriptionTimeout);
399
+ }
400
+ get transcriptionTimeout() {
401
+ return this.options.transcriptionTimeout || DEFAULT_TRANSCRIPTION_TIMEOUT;
402
+ }
403
+ /** Whether an answer to a timed out flush may still be on its way */
404
+ hasStaleFlush() {
405
+ const now = Date.now();
406
+ while (this.staleFlushes.length > 0 && this.staleFlushes[0] <= now) {
407
+ this.log("Forgetting a flush that was never answered");
408
+ this.staleFlushes.shift();
409
+ }
410
+ return this.staleFlushes.length > 0;
411
+ }
412
+ complete(utterance, transcript) {
413
+ if (utterance.status === "done") return;
414
+ this.removeUtterance(utterance);
415
+ this.emit("Transcript", transcript.trim());
416
+ }
417
+ removeUtterance(utterance) {
418
+ this.dropUtterance(utterance);
419
+ this.utterances = this.utterances.filter((u) => u !== utterance);
420
+ }
421
+ dropUtterance(utterance) {
422
+ utterance.status = "done";
423
+ clearTimeout(utterance.timeout);
424
+ utterance.timeout = void 0;
332
425
  }
333
426
  sendAudioChunk(chunk) {
334
- if (!this.socket) return;
335
- this.socket.send(
427
+ return this.socket.send(
336
428
  JSON.stringify({
337
429
  type: "input_audio.append",
338
430
  audio: chunk.toString("base64")
339
431
  })
340
432
  );
341
433
  }
342
- flushAudio() {
343
- if (!this.socket) return;
344
- this.socket.send(JSON.stringify({ type: "input_audio.flush" }));
345
- this.log("Flushed audio input");
346
- }
347
- handleMessage(message) {
348
- switch (message.type) {
349
- case "transcription.text.delta":
350
- if (typeof message.text === "string") {
351
- this.transcriptDelta += message.text;
352
- this.log(`Received transcript delta: "${message.text}"`);
353
- }
354
- break;
355
- case "transcription.done": {
356
- const transcript = typeof message.text === "string" ? message.text : this.transcriptDelta;
357
- this.log(`Received completed transcript: "${transcript}"`);
358
- this.emit("Transcript", transcript.trim());
359
- this.transcriptDelta = "";
360
- this.audioChunksPending.length = 0;
361
- if (this.transcriptionTimeout) {
362
- clearTimeout(this.transcriptionTimeout);
363
- this.transcriptionTimeout = void 0;
364
- }
365
- break;
366
- }
367
- case "error":
368
- this.log("Error:", message.error ?? message);
369
- break;
370
- default:
371
- break;
372
- }
373
- }
374
- reconnect() {
375
- if (this.destroyed) return;
376
- this.retryCount++;
377
- if (this.retryCount > (this.options.maxRetry ?? DEFAULT_MAX_RETRY2)) {
378
- this.log("Max retries reached, giving up");
379
- this.emit("Failed", this.audioChunksPending);
380
- return;
381
- }
382
- this.initPromise = new Promise((resolve) => {
383
- this.log("Reconnecting...");
384
- this.reconnectTimeout = setTimeout(() => {
385
- this.reconnectTimeout = void 0;
386
- this.initWS().then(() => {
387
- this.retryCount = 0;
388
- if (this.audioChunksPending.length > 0) {
389
- this.log("Sending audio chunks again");
390
- this.transcriptDelta = "";
391
- this.audioChunksPending.forEach(
392
- (chunk) => this.sendAudioChunk(chunk)
393
- );
394
- }
395
- }).then(resolve).catch((error) => {
396
- this.log("Reconnection error:", error);
397
- this.reconnect();
398
- });
399
- }, this.options.retryDelay ?? DEFAULT_RETRY_DELAY2);
400
- });
401
- }
402
434
  };
403
- function closeSocket(socket) {
404
- socket.removeAllListeners();
405
- socket.on("error", () => {
406
- });
407
- if (socket.readyState === import_ws.default.OPEN || socket.readyState === import_ws.default.CONNECTING) {
408
- socket.close(1e3);
409
- }
410
- }
411
435
 
412
436
  // src/MistralTTS.ts
413
437
  var import_server3 = require("@micdrop/server");