@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/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @micdrop/mistral
|
|
2
2
|
|
|
3
|
-
[Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/
|
|
3
|
+
[Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/providers)
|
|
4
4
|
|
|
5
5
|
Mistral AI implementation for [@micdrop/server](https://micdrop.dev/docs/server).
|
|
6
6
|
|
|
@@ -20,8 +20,9 @@ import { MicdropServer } from '@micdrop/server'
|
|
|
20
20
|
|
|
21
21
|
const agent = new MistralAgent({
|
|
22
22
|
apiKey: process.env.MISTRAL_API_KEY || '',
|
|
23
|
-
model: '
|
|
23
|
+
model: 'mistral-medium-latest', // Default model
|
|
24
24
|
systemPrompt: 'You are a helpful assistant',
|
|
25
|
+
settings: { reasoningEffort: 'none' }, // Optional, the lowest answers soonest
|
|
25
26
|
})
|
|
26
27
|
|
|
27
28
|
// Use with MicdropServer
|
|
@@ -60,7 +61,7 @@ agent.answer().on('data', (chunk) => process.stdout.write(chunk))
|
|
|
60
61
|
| `EndCall` | none | The agent decided that the call is over. |
|
|
61
62
|
| `Failed` | none | The agent gave up generating an answer after its retries. |
|
|
62
63
|
|
|
63
|
-
See the [Agent interface](https://micdrop.dev/docs/
|
|
64
|
+
See the [Agent interface](https://micdrop.dev/docs/agents/custom) for the full contract.
|
|
64
65
|
|
|
65
66
|
## Mistral STT (Speech-to-Text)
|
|
66
67
|
|
|
@@ -108,7 +109,7 @@ stt.transcribe(createReadStream('speech.pcm'))
|
|
|
108
109
|
| `Transcript` | `string` | Transcription of one utterance. The text is empty when nothing was recognized. |
|
|
109
110
|
| `Failed` | `Buffer[]` | Transcription gave up after its retries, with the audio chunks left pending. |
|
|
110
111
|
|
|
111
|
-
See the [STT interface](https://micdrop.dev/docs/
|
|
112
|
+
See the [STT interface](https://micdrop.dev/docs/speech-to-text/custom) for the full contract.
|
|
112
113
|
|
|
113
114
|
## Mistral TTS (Text-to-Speech)
|
|
114
115
|
|
|
@@ -141,7 +142,7 @@ import { Readable } from 'stream'
|
|
|
141
142
|
|
|
142
143
|
const tts = new MistralTTS({ apiKey: process.env.MISTRAL_API_KEY || '' })
|
|
143
144
|
|
|
144
|
-
// Audio is raw PCM, 16 bits,
|
|
145
|
+
// Audio is raw PCM, 16 bits, mono, at tts.sampleRate
|
|
145
146
|
tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))
|
|
146
147
|
tts.on('Failed', (texts) => console.error('Failed:', texts))
|
|
147
148
|
|
|
@@ -150,16 +151,16 @@ tts.speak(Readable.from(['Hello! ', 'What can I do for you?']))
|
|
|
150
151
|
|
|
151
152
|
### Events
|
|
152
153
|
|
|
153
|
-
| Event | Payload | Description
|
|
154
|
-
| -------- | ---------- |
|
|
155
|
-
| `Audio` | `Buffer` | A chunk of audio, PCM 16 bits,
|
|
156
|
-
| `Failed` | `string[]` | Synthesis gave up after an error, with the text that stayed unspoken.
|
|
154
|
+
| Event | Payload | Description |
|
|
155
|
+
| -------- | ---------- | -------------------------------------------------------------------------------------- |
|
|
156
|
+
| `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, mono, at `tts.sampleRate` (24 kHz), ready to be played. |
|
|
157
|
+
| `Failed` | `string[]` | Synthesis gave up after an error, with the text that stayed unspoken. |
|
|
157
158
|
|
|
158
|
-
See the [TTS interface](https://micdrop.dev/docs/
|
|
159
|
+
See the [TTS interface](https://micdrop.dev/docs/text-to-speech/custom) for the full contract.
|
|
159
160
|
|
|
160
161
|
## Documentation
|
|
161
162
|
|
|
162
|
-
Read full [documentation of the Mistral integration for Micdrop](https://micdrop.dev/docs/
|
|
163
|
+
Read full [documentation of the Mistral integration for Micdrop](https://micdrop.dev/docs/providers) on the [website](https://micdrop.dev).
|
|
163
164
|
|
|
164
165
|
## License
|
|
165
166
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentOptions, STT, SentenceTTS } from '@micdrop/server';
|
|
1
|
+
import { Agent, AgentOptions, MicdropModel, STT, SentenceTTS, MicdropVoice } from '@micdrop/server';
|
|
2
2
|
import { ChatCompletionStreamRequest } from '@mistralai/mistralai/models/components';
|
|
3
3
|
import { Writable, Readable } from 'stream';
|
|
4
4
|
|
|
@@ -11,6 +11,16 @@ interface MistralAgentOptions extends AgentOptions {
|
|
|
11
11
|
maxSteps?: number;
|
|
12
12
|
}
|
|
13
13
|
declare class MistralAgent extends Agent<MistralAgentOptions> {
|
|
14
|
+
/**
|
|
15
|
+
* 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.
|
|
16
|
+
*
|
|
17
|
+
* The list is fetched once and shared by every call of the process for a
|
|
18
|
+
* day. `refresh` fetches it again.
|
|
19
|
+
*/
|
|
20
|
+
static listModels(options: {
|
|
21
|
+
apiKey: string;
|
|
22
|
+
refresh?: boolean;
|
|
23
|
+
}): Promise<MicdropModel[]>;
|
|
14
24
|
private mistral;
|
|
15
25
|
constructor(options: MistralAgentOptions);
|
|
16
26
|
protected generateAnswer(stream: Writable, signal: AbortSignal): Promise<void>;
|
|
@@ -48,6 +58,16 @@ interface MistralSTTOptions {
|
|
|
48
58
|
}
|
|
49
59
|
declare class MistralSTT extends STT {
|
|
50
60
|
private options;
|
|
61
|
+
/**
|
|
62
|
+
* Lists the models that transcribe as the user speaks, the realtime ones `MistralSTT` streams to, each one once with its aliases.
|
|
63
|
+
*
|
|
64
|
+
* The list is fetched once and shared by every call of the process for a
|
|
65
|
+
* day. `refresh` fetches it again.
|
|
66
|
+
*/
|
|
67
|
+
static listModels(options: {
|
|
68
|
+
apiKey: string;
|
|
69
|
+
refresh?: boolean;
|
|
70
|
+
}): Promise<MicdropModel[]>;
|
|
51
71
|
private socket;
|
|
52
72
|
private utterances;
|
|
53
73
|
private staleFlushes;
|
|
@@ -92,7 +112,33 @@ interface MistralTTSOptions {
|
|
|
92
112
|
}
|
|
93
113
|
declare class MistralTTS extends SentenceTTS {
|
|
94
114
|
private readonly options;
|
|
115
|
+
/**
|
|
116
|
+
* Lists the models that speak, each one once with its aliases.
|
|
117
|
+
*
|
|
118
|
+
* The list is fetched once and shared by every call of the process for a
|
|
119
|
+
* day. `refresh` fetches it again.
|
|
120
|
+
*/
|
|
121
|
+
static listModels(options: {
|
|
122
|
+
apiKey: string;
|
|
123
|
+
baseUrl?: string;
|
|
124
|
+
refresh?: boolean;
|
|
125
|
+
}): Promise<MicdropModel[]>;
|
|
126
|
+
/**
|
|
127
|
+
* Lists the voices the account can speak with, the presets and its own.
|
|
128
|
+
*
|
|
129
|
+
* `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
|
|
130
|
+
*
|
|
131
|
+
* The list is fetched once and shared by every call of the process for a
|
|
132
|
+
* day. `refresh` fetches it again, for a voice added a moment ago.
|
|
133
|
+
*/
|
|
134
|
+
static listVoices(options: {
|
|
135
|
+
apiKey: string;
|
|
136
|
+
baseUrl?: string;
|
|
137
|
+
language?: string;
|
|
138
|
+
refresh?: boolean;
|
|
139
|
+
}): Promise<MicdropVoice[]>;
|
|
95
140
|
constructor(options: MistralTTSOptions);
|
|
141
|
+
get sampleRate(): number;
|
|
96
142
|
protected synthesize(text: string, signal: AbortSignal): Promise<undefined>;
|
|
97
143
|
private request;
|
|
98
144
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Agent, AgentOptions, STT, SentenceTTS } from '@micdrop/server';
|
|
1
|
+
import { Agent, AgentOptions, MicdropModel, STT, SentenceTTS, MicdropVoice } from '@micdrop/server';
|
|
2
2
|
import { ChatCompletionStreamRequest } from '@mistralai/mistralai/models/components';
|
|
3
3
|
import { Writable, Readable } from 'stream';
|
|
4
4
|
|
|
@@ -11,6 +11,16 @@ interface MistralAgentOptions extends AgentOptions {
|
|
|
11
11
|
maxSteps?: number;
|
|
12
12
|
}
|
|
13
13
|
declare class MistralAgent extends Agent<MistralAgentOptions> {
|
|
14
|
+
/**
|
|
15
|
+
* 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.
|
|
16
|
+
*
|
|
17
|
+
* The list is fetched once and shared by every call of the process for a
|
|
18
|
+
* day. `refresh` fetches it again.
|
|
19
|
+
*/
|
|
20
|
+
static listModels(options: {
|
|
21
|
+
apiKey: string;
|
|
22
|
+
refresh?: boolean;
|
|
23
|
+
}): Promise<MicdropModel[]>;
|
|
14
24
|
private mistral;
|
|
15
25
|
constructor(options: MistralAgentOptions);
|
|
16
26
|
protected generateAnswer(stream: Writable, signal: AbortSignal): Promise<void>;
|
|
@@ -48,6 +58,16 @@ interface MistralSTTOptions {
|
|
|
48
58
|
}
|
|
49
59
|
declare class MistralSTT extends STT {
|
|
50
60
|
private options;
|
|
61
|
+
/**
|
|
62
|
+
* Lists the models that transcribe as the user speaks, the realtime ones `MistralSTT` streams to, each one once with its aliases.
|
|
63
|
+
*
|
|
64
|
+
* The list is fetched once and shared by every call of the process for a
|
|
65
|
+
* day. `refresh` fetches it again.
|
|
66
|
+
*/
|
|
67
|
+
static listModels(options: {
|
|
68
|
+
apiKey: string;
|
|
69
|
+
refresh?: boolean;
|
|
70
|
+
}): Promise<MicdropModel[]>;
|
|
51
71
|
private socket;
|
|
52
72
|
private utterances;
|
|
53
73
|
private staleFlushes;
|
|
@@ -92,7 +112,33 @@ interface MistralTTSOptions {
|
|
|
92
112
|
}
|
|
93
113
|
declare class MistralTTS extends SentenceTTS {
|
|
94
114
|
private readonly options;
|
|
115
|
+
/**
|
|
116
|
+
* Lists the models that speak, each one once with its aliases.
|
|
117
|
+
*
|
|
118
|
+
* The list is fetched once and shared by every call of the process for a
|
|
119
|
+
* day. `refresh` fetches it again.
|
|
120
|
+
*/
|
|
121
|
+
static listModels(options: {
|
|
122
|
+
apiKey: string;
|
|
123
|
+
baseUrl?: string;
|
|
124
|
+
refresh?: boolean;
|
|
125
|
+
}): Promise<MicdropModel[]>;
|
|
126
|
+
/**
|
|
127
|
+
* Lists the voices the account can speak with, the presets and its own.
|
|
128
|
+
*
|
|
129
|
+
* `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
|
|
130
|
+
*
|
|
131
|
+
* The list is fetched once and shared by every call of the process for a
|
|
132
|
+
* day. `refresh` fetches it again, for a voice added a moment ago.
|
|
133
|
+
*/
|
|
134
|
+
static listVoices(options: {
|
|
135
|
+
apiKey: string;
|
|
136
|
+
baseUrl?: string;
|
|
137
|
+
language?: string;
|
|
138
|
+
refresh?: boolean;
|
|
139
|
+
}): Promise<MicdropVoice[]>;
|
|
95
140
|
constructor(options: MistralTTSOptions);
|
|
141
|
+
get sampleRate(): number;
|
|
96
142
|
protected synthesize(text: string, signal: AbortSignal): Promise<undefined>;
|
|
97
143
|
private request;
|
|
98
144
|
}
|
package/dist/index.js
CHANGED
|
@@ -37,10 +37,62 @@ __export(index_exports, {
|
|
|
37
37
|
module.exports = __toCommonJS(index_exports);
|
|
38
38
|
|
|
39
39
|
// src/MistralAgent.ts
|
|
40
|
-
var
|
|
40
|
+
var import_server2 = require("@micdrop/server");
|
|
41
41
|
var import_mistralai = require("@mistralai/mistralai");
|
|
42
42
|
var import_zod = __toESM(require("zod"));
|
|
43
|
-
|
|
43
|
+
|
|
44
|
+
// src/deltaText.ts
|
|
45
|
+
function textOf(content) {
|
|
46
|
+
if (typeof content === "string") return content;
|
|
47
|
+
if (!Array.isArray(content)) return void 0;
|
|
48
|
+
return content.map((chunk) => chunk.type === "text" ? chunk.text : "").join("");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/models.ts
|
|
52
|
+
var import_server = require("@micdrop/server");
|
|
53
|
+
var DEFAULT_BASE_URL = "https://api.mistral.ai";
|
|
54
|
+
function fetchAll(apiKey, baseUrl) {
|
|
55
|
+
return (0, import_server.cachedList)(["mistral-raw-models", baseUrl], apiKey, async () => {
|
|
56
|
+
const response = await fetch(`${baseUrl}/v1/models`, {
|
|
57
|
+
headers: { Authorization: `Bearer ${apiKey}` }
|
|
58
|
+
});
|
|
59
|
+
if (!response.ok) throw new Error(`Mistral answered ${response.status}`);
|
|
60
|
+
const { data } = await response.json();
|
|
61
|
+
return data;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function listMistralModels(component, keep, options) {
|
|
65
|
+
const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
66
|
+
return (0, import_server.cachedModels)(
|
|
67
|
+
["mistral", component, baseUrl],
|
|
68
|
+
options.apiKey,
|
|
69
|
+
async () => {
|
|
70
|
+
const models = [];
|
|
71
|
+
const seen = /* @__PURE__ */ new Set();
|
|
72
|
+
for (const model of await fetchAll(options.apiKey, baseUrl)) {
|
|
73
|
+
if (!keep(model) || seen.has(model.id)) continue;
|
|
74
|
+
const names = [model.id, ...model.aliases ?? []];
|
|
75
|
+
names.forEach((name) => seen.add(name));
|
|
76
|
+
const id = names.find((name) => name.endsWith("-latest")) ?? model.id;
|
|
77
|
+
models.push({
|
|
78
|
+
id,
|
|
79
|
+
name: model.name || void 0,
|
|
80
|
+
description: model.description || void 0,
|
|
81
|
+
aliases: names.filter((name) => name !== id),
|
|
82
|
+
languages: model.audio_transcription_capabilities?.languages ?? void 0,
|
|
83
|
+
reasoning: !!model.capabilities.reasoning,
|
|
84
|
+
deprecated: !!model.deprecation,
|
|
85
|
+
shutdownDate: model.deprecation?.slice(0, 10) || void 0
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return models;
|
|
89
|
+
},
|
|
90
|
+
options.refresh
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/MistralAgent.ts
|
|
95
|
+
var DEFAULT_MODEL = "mistral-medium-latest";
|
|
44
96
|
var DEFAULT_MAX_RETRY = 3;
|
|
45
97
|
var DEFAULT_MAX_STEPS = 5;
|
|
46
98
|
var DEFAULT_RETRY_DELAY = 500;
|
|
@@ -50,7 +102,20 @@ var StepInterruptedError = class extends Error {
|
|
|
50
102
|
this.outcome = outcome;
|
|
51
103
|
}
|
|
52
104
|
};
|
|
53
|
-
var MistralAgent = class extends
|
|
105
|
+
var MistralAgent = class extends import_server2.Agent {
|
|
106
|
+
/**
|
|
107
|
+
* 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.
|
|
108
|
+
*
|
|
109
|
+
* The list is fetched once and shared by every call of the process for a
|
|
110
|
+
* day. `refresh` fetches it again.
|
|
111
|
+
*/
|
|
112
|
+
static listModels(options) {
|
|
113
|
+
return listMistralModels(
|
|
114
|
+
"agent",
|
|
115
|
+
({ capabilities }) => capabilities.completion_chat && capabilities.function_calling,
|
|
116
|
+
options
|
|
117
|
+
);
|
|
118
|
+
}
|
|
54
119
|
constructor(options) {
|
|
55
120
|
super(options);
|
|
56
121
|
this.mistral = new import_mistralai.Mistral({ apiKey: options.apiKey });
|
|
@@ -94,6 +159,10 @@ var MistralAgent = class extends import_server.Agent {
|
|
|
94
159
|
});
|
|
95
160
|
return;
|
|
96
161
|
}
|
|
162
|
+
if (!outcome.ranTools) {
|
|
163
|
+
this.log("Empty answer");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
97
166
|
}
|
|
98
167
|
console.error("[MistralAgent] Max steps reached");
|
|
99
168
|
}
|
|
@@ -111,9 +180,12 @@ var MistralAgent = class extends import_server.Agent {
|
|
|
111
180
|
content: ""
|
|
112
181
|
});
|
|
113
182
|
}
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
183
|
+
const write = this.answerWriter(stream);
|
|
184
|
+
const outcome = {
|
|
185
|
+
fullAnswer: "",
|
|
186
|
+
skipAnswer: false,
|
|
187
|
+
ranTools: false
|
|
188
|
+
};
|
|
117
189
|
let produced = false;
|
|
118
190
|
try {
|
|
119
191
|
const result = await this.mistral.chat.stream(
|
|
@@ -125,47 +197,44 @@ var MistralAgent = class extends import_server.Agent {
|
|
|
125
197
|
},
|
|
126
198
|
{ signal }
|
|
127
199
|
);
|
|
200
|
+
const toolCalls = [];
|
|
201
|
+
const toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
128
202
|
for await (const event of result) {
|
|
129
203
|
if (signal.aborted) return outcome;
|
|
130
204
|
const delta = event.data.choices[0]?.delta;
|
|
131
|
-
const chunk = delta?.content;
|
|
132
|
-
|
|
133
|
-
if (typeof chunk === "string") {
|
|
134
|
-
if (chunk === "") continue;
|
|
205
|
+
const chunk = textOf(delta?.content);
|
|
206
|
+
if (chunk) {
|
|
135
207
|
this.log(`Answer chunk: "${chunk}"`);
|
|
136
208
|
outcome.fullAnswer += chunk;
|
|
137
209
|
produced = true;
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
stream.write(chunk);
|
|
154
|
-
} else if (toolCalls?.length) {
|
|
155
|
-
for (const toolCall of toolCalls) {
|
|
156
|
-
if (signal.aborted) return outcome;
|
|
157
|
-
produced = true;
|
|
158
|
-
const result2 = await this.executeTool({
|
|
159
|
-
role: "tool_call",
|
|
160
|
-
toolCallId: toolCall.id || "",
|
|
161
|
-
toolName: toolCall.function.name,
|
|
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)
|
|
164
|
-
});
|
|
165
|
-
if (result2.skipAnswer) {
|
|
166
|
-
outcome.skipAnswer = true;
|
|
167
|
-
}
|
|
210
|
+
write(chunk);
|
|
211
|
+
}
|
|
212
|
+
for (const [position, fragment] of (delta?.toolCalls ?? []).entries()) {
|
|
213
|
+
const index = fragment.index ?? position;
|
|
214
|
+
let toolCall = toolCallsByIndex.get(index);
|
|
215
|
+
if (!toolCall || fragment.id && toolCall.id && fragment.id !== toolCall.id) {
|
|
216
|
+
toolCall = { id: "", name: "", arguments: "" };
|
|
217
|
+
toolCalls.push(toolCall);
|
|
218
|
+
toolCallsByIndex.set(index, toolCall);
|
|
168
219
|
}
|
|
220
|
+
if (fragment.id) toolCall.id = fragment.id;
|
|
221
|
+
if (fragment.function.name) toolCall.name = fragment.function.name;
|
|
222
|
+
const args = fragment.function.arguments;
|
|
223
|
+
toolCall.arguments += typeof args === "string" ? args : JSON.stringify(args);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (const toolCall of toolCalls) {
|
|
227
|
+
if (signal.aborted) return outcome;
|
|
228
|
+
produced = true;
|
|
229
|
+
outcome.ranTools = true;
|
|
230
|
+
const result2 = await this.executeTool({
|
|
231
|
+
role: "tool_call",
|
|
232
|
+
toolCallId: toolCall.id,
|
|
233
|
+
toolName: toolCall.name,
|
|
234
|
+
parameters: toolCall.arguments || "{}"
|
|
235
|
+
});
|
|
236
|
+
if (result2.skipAnswer) {
|
|
237
|
+
outcome.skipAnswer = true;
|
|
169
238
|
}
|
|
170
239
|
}
|
|
171
240
|
} catch (error) {
|
|
@@ -232,7 +301,7 @@ var MistralAgent = class extends import_server.Agent {
|
|
|
232
301
|
};
|
|
233
302
|
|
|
234
303
|
// src/MistralSTT.ts
|
|
235
|
-
var
|
|
304
|
+
var import_server3 = require("@micdrop/server");
|
|
236
305
|
var DEFAULT_MODEL2 = "voxtral-mini-transcribe-realtime-3-0";
|
|
237
306
|
var DEFAULT_ENCODING = "pcm_s16le";
|
|
238
307
|
var SAMPLE_RATE = 16e3;
|
|
@@ -240,7 +309,7 @@ var DEFAULT_CONNECTION_TIMEOUT = 5e3;
|
|
|
240
309
|
var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
|
|
241
310
|
var DEFAULT_RETRY_DELAY2 = 1e3;
|
|
242
311
|
var DEFAULT_MAX_RETRY2 = 3;
|
|
243
|
-
var MistralSTT = class extends
|
|
312
|
+
var MistralSTT = class extends import_server3.STT {
|
|
244
313
|
constructor(options) {
|
|
245
314
|
super();
|
|
246
315
|
this.options = options;
|
|
@@ -316,7 +385,7 @@ var MistralSTT = class extends import_server2.STT {
|
|
|
316
385
|
this.utterances = [];
|
|
317
386
|
this.emit("Failed", chunks);
|
|
318
387
|
};
|
|
319
|
-
this.socket = new
|
|
388
|
+
this.socket = new import_server3.ReconnectingSocket({
|
|
320
389
|
url: this.getURL(),
|
|
321
390
|
clientOptions: {
|
|
322
391
|
headers: {
|
|
@@ -337,6 +406,19 @@ var MistralSTT = class extends import_server2.STT {
|
|
|
337
406
|
this.socket.on("Failed", this.onFailed);
|
|
338
407
|
this.socket.connect();
|
|
339
408
|
}
|
|
409
|
+
/**
|
|
410
|
+
* Lists the models that transcribe as the user speaks, the realtime ones `MistralSTT` streams to, each one once with its aliases.
|
|
411
|
+
*
|
|
412
|
+
* The list is fetched once and shared by every call of the process for a
|
|
413
|
+
* day. `refresh` fetches it again.
|
|
414
|
+
*/
|
|
415
|
+
static listModels(options) {
|
|
416
|
+
return listMistralModels(
|
|
417
|
+
"stt",
|
|
418
|
+
({ capabilities }) => capabilities.audio_transcription_realtime,
|
|
419
|
+
options
|
|
420
|
+
);
|
|
421
|
+
}
|
|
340
422
|
transcribe(audioStream) {
|
|
341
423
|
this.socket.connect();
|
|
342
424
|
const utterance = { status: "streaming", chunks: [], text: "" };
|
|
@@ -434,25 +516,53 @@ var MistralSTT = class extends import_server2.STT {
|
|
|
434
516
|
};
|
|
435
517
|
|
|
436
518
|
// src/MistralTTS.ts
|
|
437
|
-
var
|
|
519
|
+
var import_server4 = require("@micdrop/server");
|
|
438
520
|
var DEFAULT_MODEL3 = "voxtral-mini-tts-3-0";
|
|
439
521
|
var DEFAULT_VOICE_ID = "voice_3150";
|
|
440
|
-
var DEFAULT_BASE_URL = "https://api.mistral.ai";
|
|
441
522
|
var MISTRAL_SAMPLE_RATE = 24e3;
|
|
442
|
-
var OUTPUT_SAMPLE_RATE = 16e3;
|
|
443
523
|
var FLOAT32_BYTES = 4;
|
|
444
|
-
var
|
|
524
|
+
var PAGE_SIZE = 100;
|
|
525
|
+
var MistralTTS = class extends import_server4.SentenceTTS {
|
|
445
526
|
constructor(options) {
|
|
446
527
|
super();
|
|
447
528
|
this.options = options;
|
|
448
529
|
}
|
|
530
|
+
/**
|
|
531
|
+
* Lists the models that speak, each one once with its aliases.
|
|
532
|
+
*
|
|
533
|
+
* The list is fetched once and shared by every call of the process for a
|
|
534
|
+
* day. `refresh` fetches it again.
|
|
535
|
+
*/
|
|
536
|
+
static listModels(options) {
|
|
537
|
+
return listMistralModels(
|
|
538
|
+
"tts",
|
|
539
|
+
({ capabilities }) => capabilities.audio_speech,
|
|
540
|
+
options
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Lists the voices the account can speak with, the presets and its own.
|
|
545
|
+
*
|
|
546
|
+
* `language`, such as `fr` or `fr-FR`, keeps the voices that speak it.
|
|
547
|
+
*
|
|
548
|
+
* The list is fetched once and shared by every call of the process for a
|
|
549
|
+
* day. `refresh` fetches it again, for a voice added a moment ago.
|
|
550
|
+
*/
|
|
551
|
+
static listVoices(options) {
|
|
552
|
+
const baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
553
|
+
return (0, import_server4.cachedVoices)(
|
|
554
|
+
["mistral", baseUrl],
|
|
555
|
+
options.apiKey,
|
|
556
|
+
() => fetchVoices(options.apiKey, baseUrl),
|
|
557
|
+
options.refresh
|
|
558
|
+
).then((voices) => (0, import_server4.voicesForLanguage)(voices, options.language));
|
|
559
|
+
}
|
|
560
|
+
get sampleRate() {
|
|
561
|
+
return MISTRAL_SAMPLE_RATE;
|
|
562
|
+
}
|
|
449
563
|
async synthesize(text, signal) {
|
|
450
564
|
const response = await this.request(text, signal);
|
|
451
565
|
if (!response.body) throw new Error("Mistral answered without a body");
|
|
452
|
-
const resampler = new import_server3.Pcm16Resampler(
|
|
453
|
-
MISTRAL_SAMPLE_RATE,
|
|
454
|
-
OUTPUT_SAMPLE_RATE
|
|
455
|
-
);
|
|
456
566
|
let leftover = Buffer.alloc(0);
|
|
457
567
|
for await (const event of readEvents(response.body, signal)) {
|
|
458
568
|
if (event.type === "error") {
|
|
@@ -471,7 +581,7 @@ var MistralTTS = class extends import_server3.SentenceTTS {
|
|
|
471
581
|
for (let i = 0; i < samples.length; i++) {
|
|
472
582
|
samples[i] = data.readFloatLE(i * FLOAT32_BYTES);
|
|
473
583
|
}
|
|
474
|
-
if (!this.emitAudio(
|
|
584
|
+
if (!this.emitAudio((0, import_server4.float32ToPcm16)(samples))) break;
|
|
475
585
|
}
|
|
476
586
|
return void 0;
|
|
477
587
|
}
|
|
@@ -528,6 +638,35 @@ async function* readEvents(body, signal) {
|
|
|
528
638
|
});
|
|
529
639
|
}
|
|
530
640
|
}
|
|
641
|
+
async function fetchVoices(apiKey, baseUrl) {
|
|
642
|
+
const voices = [];
|
|
643
|
+
for (let offset = 0, total = 1; offset < total; offset += PAGE_SIZE) {
|
|
644
|
+
const response = await fetch(
|
|
645
|
+
`${baseUrl}/v1/audio/voices?offset=${offset}&limit=${PAGE_SIZE}`,
|
|
646
|
+
{ headers: { Authorization: `Bearer ${apiKey}` } }
|
|
647
|
+
);
|
|
648
|
+
if (!response.ok) throw new Error(`Mistral answered ${response.status}`);
|
|
649
|
+
const data = await response.json();
|
|
650
|
+
total = data.total ?? 0;
|
|
651
|
+
if (data.items.length === 0) break;
|
|
652
|
+
for (const voice of data.items) {
|
|
653
|
+
voices.push({
|
|
654
|
+
// A preset goes by its slug, a voice of the account by its id
|
|
655
|
+
id: voice.slug || voice.id,
|
|
656
|
+
name: voice.name,
|
|
657
|
+
// Mistral writes en_us where the other providers write en-US
|
|
658
|
+
languages: voice.languages?.map((code) => {
|
|
659
|
+
const [language, region] = code.split("_");
|
|
660
|
+
return region ? `${language}-${region.toUpperCase()}` : language;
|
|
661
|
+
}),
|
|
662
|
+
gender: (0, import_server4.voiceGender)(voice.gender),
|
|
663
|
+
description: voice.description || void 0,
|
|
664
|
+
custom: voice.type !== "preset"
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
return voices;
|
|
669
|
+
}
|
|
531
670
|
// Annotate the CommonJS export names for ESM import in node:
|
|
532
671
|
0 && (module.exports = {
|
|
533
672
|
MistralAgent,
|