@astratra/ai 1.6.0 → 1.7.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/README.md +349 -6
- package/package.json +1 -1
- package/src/aiConfig.js +72 -0
- package/src/gemini.js +9 -3
- package/src/index.d.ts +290 -4
- package/src/index.js +9 -1
- package/src/localModels.js +99 -0
- package/src/managedRouter.js +258 -0
- package/src/modelAnswers.js +1 -0
- package/src/nativeTools.js +44 -5
- package/src/openaiCompatible.js +5 -1
- package/src/providerLimits.js +45 -0
- package/src/routingState.js +87 -0
- package/src/serviceHttp.js +56 -0
- package/src/serviceLanguage.js +23 -0
- package/src/voice.js +181 -0
package/src/voice.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { createLocalModelsClient, validPcmAudio } = require('./localModels');
|
|
3
|
+
const { createTokenFileReader, serviceRequest, endpoint, AiServiceError } = require('./serviceHttp');
|
|
4
|
+
const { plainText } = require('./answerText');
|
|
5
|
+
|
|
6
|
+
const WHISPER_STOCK_PHRASES = Object.freeze(['sous-titr', 'amara.org', "merci d'avoir regard", 'merci d’avoir regard', 'abonnez-vous', 'thank for watching', 'thanks for watching', 'subtitle by', 'subtitles by', 'please subscribe']);
|
|
7
|
+
|
|
8
|
+
function pcmToWav(pcm, { sampleRate = 16000, channels = 1 } = {}) {
|
|
9
|
+
const bytes = Buffer.from(pcm);
|
|
10
|
+
if (!bytes.length || bytes.length % (channels * 2) || !Number.isInteger(sampleRate) || sampleRate <= 0 || !Number.isInteger(channels) || channels < 1 || channels > 2) throw new AiServiceError('AI_AUDIO_INPUT');
|
|
11
|
+
const header = Buffer.alloc(44);
|
|
12
|
+
header.write('RIFF'); header.writeUInt32LE(36 + bytes.length, 4); header.write('WAVE', 8);
|
|
13
|
+
header.write('fmt ', 12); header.writeUInt32LE(16, 16); header.writeUInt16LE(1, 20);
|
|
14
|
+
header.writeUInt16LE(channels, 22); header.writeUInt32LE(sampleRate, 24); header.writeUInt32LE(sampleRate * channels * 2, 28);
|
|
15
|
+
header.writeUInt16LE(channels * 2, 32); header.writeUInt16LE(16, 34); header.write('data', 36); header.writeUInt32LE(bytes.length, 40);
|
|
16
|
+
return Buffer.concat([header, bytes]);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function readPcmWav(audio) {
|
|
20
|
+
const bytes = Buffer.from(audio);
|
|
21
|
+
if (bytes.length < 44 || bytes.toString('ascii', 0, 4) !== 'RIFF' || bytes.toString('ascii', 8, 12) !== 'WAVE' || bytes.readUInt32LE(4) + 8 > bytes.length) throw new AiServiceError('AI_AUDIO_RESPONSE');
|
|
22
|
+
let format;
|
|
23
|
+
let pcm;
|
|
24
|
+
for (let at = 12; at + 8 <= bytes.length;) {
|
|
25
|
+
const name = bytes.toString('ascii', at, at + 4);
|
|
26
|
+
const size = bytes.readUInt32LE(at + 4);
|
|
27
|
+
const start = at + 8;
|
|
28
|
+
if (start + size > bytes.length) throw new AiServiceError('AI_AUDIO_RESPONSE');
|
|
29
|
+
if (name === 'fmt ' && size >= 16) format = { encoding: bytes.readUInt16LE(start), channels: bytes.readUInt16LE(start + 2), sampleRate: bytes.readUInt32LE(start + 4), bits: bytes.readUInt16LE(start + 14) };
|
|
30
|
+
if (name === 'data') pcm = bytes.subarray(start, start + size);
|
|
31
|
+
at = start + size + size % 2;
|
|
32
|
+
}
|
|
33
|
+
if (!format || format.encoding !== 1 || format.bits !== 16 || ![1, 2].includes(format.channels) || !format.sampleRate || !pcm?.length || pcm.length % (format.channels * 2)) throw new AiServiceError('AI_AUDIO_RESPONSE');
|
|
34
|
+
return { pcm, sampleRate: format.sampleRate, channels: format.channels };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function speakableText(text, maxChars = 6000) {
|
|
38
|
+
return plainText(String(text ?? '')).replace(/[\p{Extended_Pictographic}\p{Emoji_Modifier}\u{FE0F}\u{200D}]/gu, ' ').replace(/\s+/g, ' ').trim().slice(0, maxChars);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function doubtfulTranscription(text, { stockPhrases = WHISPER_STOCK_PHRASES } = {}) {
|
|
42
|
+
const lower = String(text || '').toLowerCase();
|
|
43
|
+
const words = lower.match(/\p{L}+/gu) || [];
|
|
44
|
+
if (stockPhrases.some((phrase) => lower.includes(phrase))) return true;
|
|
45
|
+
if (words.length >= 6 && new Set(words).size <= 2) return true;
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function rotatingPool(config = {}, now) {
|
|
50
|
+
const rests = new Map();
|
|
51
|
+
let next = 0;
|
|
52
|
+
return {
|
|
53
|
+
async candidates(defaultModels) {
|
|
54
|
+
const source = typeof config.keys === 'function' ? await config.keys() : config.keys;
|
|
55
|
+
const keys = [...new Set((Array.isArray(source) ? source : [source]).filter(Boolean))];
|
|
56
|
+
const start = keys.length ? next++ % keys.length : 0;
|
|
57
|
+
const list = [];
|
|
58
|
+
for (const model of config.models || defaultModels) for (let offset = 0; offset < keys.length; offset += 1) {
|
|
59
|
+
const slot = (start + offset) % keys.length;
|
|
60
|
+
const id = `${model}:key-${slot + 1}`;
|
|
61
|
+
if ((rests.get(id) || 0) <= now()) list.push({ model, key: keys[slot], id });
|
|
62
|
+
}
|
|
63
|
+
return list;
|
|
64
|
+
},
|
|
65
|
+
rest(id) { rests.set(id, now() + (config.cooldownMs ?? 120000)); }
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function createTranscriptionService(options = {}) {
|
|
70
|
+
const local = options.local || createLocalModelsClient({ fetch: options.fetch, ...(options.localModels || {}) });
|
|
71
|
+
const now = options.now || Date.now;
|
|
72
|
+
const groq = options.groq || {};
|
|
73
|
+
const cloudflare = options.cloudflare || {};
|
|
74
|
+
const groqPool = rotatingPool(groq, now);
|
|
75
|
+
const cloudPool = rotatingPool(cloudflare, now);
|
|
76
|
+
const fetchImpl = options.fetch || globalThis.fetch;
|
|
77
|
+
async function transcribe({ audio, format = 'pcm16', language = 'fr', vocabulary = '', filename = 'audio.wav', mimeType = 'audio/wav', signal, allowRemote = true } = {}) {
|
|
78
|
+
const bytes = typeof audio === 'string' ? Buffer.from(audio, 'base64') : Buffer.from(audio || []);
|
|
79
|
+
if (!bytes.length || bytes.length > (options.maxAudioBytes ?? 25000000)) throw new AiServiceError('AI_AUDIO_INPUT');
|
|
80
|
+
let pcm;
|
|
81
|
+
let file = bytes;
|
|
82
|
+
if (format === 'pcm16') {
|
|
83
|
+
if (!validPcmAudio(bytes.toString('base64'))) throw new AiServiceError('AI_AUDIO_INPUT');
|
|
84
|
+
pcm = bytes;
|
|
85
|
+
file = pcmToWav(pcm);
|
|
86
|
+
filename = 'audio.wav'; mimeType = 'audio/wav';
|
|
87
|
+
} else if (format === 'wav') {
|
|
88
|
+
const wave = readPcmWav(bytes);
|
|
89
|
+
if (wave.sampleRate === 16000 && wave.channels === 1 && validPcmAudio(wave.pcm.toString('base64'))) pcm = wave.pcm;
|
|
90
|
+
} else if (format !== 'file') throw new AiServiceError('AI_AUDIO_INPUT');
|
|
91
|
+
if (signal?.aborted) throw signal.reason;
|
|
92
|
+
if (pcm) {
|
|
93
|
+
const result = await local.transcrire(pcm.toString('base64'), { langue: language, vocabulaire: vocabulary, timeoutMs: options.localTimeoutMs ?? 15000, signal });
|
|
94
|
+
if (result) {
|
|
95
|
+
// Genuine silence is an answer, not an outage to send to another model.
|
|
96
|
+
const silence = result.sansParole > (options.noSpeechThreshold ?? 0.6);
|
|
97
|
+
const doubtful = result.logprob < (options.logprobThreshold ?? -1) || doubtfulTranscription(result.texte, options);
|
|
98
|
+
if (silence || !doubtful) return { text: silence ? '' : result.texte, language: result.langue, provider: 'local', model: 'whisper', confidence: Math.exp(Math.min(0, result.logprob)), noSpeech: silence, fallback: false };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!allowRemote) throw new AiServiceError('AI_TRANSCRIPTION_UNAVAILABLE');
|
|
102
|
+
for (const candidate of await groqPool.candidates(['whisper-large-v3-turbo', 'whisper-large-v3'])) {
|
|
103
|
+
if (!groq.url) break;
|
|
104
|
+
try {
|
|
105
|
+
const form = new globalThis.FormData();
|
|
106
|
+
form.set('file', new globalThis.Blob([file], { type: mimeType }), filename);
|
|
107
|
+
form.set('model', candidate.model); form.set('response_format', 'verbose_json');
|
|
108
|
+
if (language) form.set('language', language);
|
|
109
|
+
if (vocabulary) form.set('prompt', vocabulary);
|
|
110
|
+
const data = await serviceRequest(groq.url, { fetch: fetchImpl, timeoutMs: groq.timeoutMs ?? 30000, signal, rawBody: form, headers: { Authorization: `Bearer ${candidate.key}` } });
|
|
111
|
+
if (typeof data?.text !== 'string' || doubtfulTranscription(data.text, options)) throw new AiServiceError('AI_SERVICE_RESPONSE');
|
|
112
|
+
return { text: data.text, language: data.language || null, provider: 'groq', model: candidate.model, fallback: true };
|
|
113
|
+
} catch (_error) { if (signal?.aborted) throw signal.reason; groqPool.rest(candidate.id); }
|
|
114
|
+
}
|
|
115
|
+
for (const candidate of await cloudPool.candidates(['@cf/openai/whisper-large-v3-turbo'])) {
|
|
116
|
+
if (!cloudflare.url) break;
|
|
117
|
+
try {
|
|
118
|
+
const data = await serviceRequest(typeof cloudflare.url === 'function' ? cloudflare.url(candidate.model) : cloudflare.url, { fetch: fetchImpl, timeoutMs: cloudflare.timeoutMs ?? 30000, signal, body: { audio: file.toString('base64') }, headers: { Authorization: `Bearer ${candidate.key}` } });
|
|
119
|
+
if (data?.success === false || typeof data?.result?.text !== 'string' || doubtfulTranscription(data.result.text, options)) throw new AiServiceError('AI_SERVICE_RESPONSE');
|
|
120
|
+
return { text: data.result.text, language: data.result.language || null, provider: 'cloudflare', model: candidate.model, fallback: true };
|
|
121
|
+
} catch (_error) { if (signal?.aborted) throw signal.reason; cloudPool.rest(candidate.id); }
|
|
122
|
+
}
|
|
123
|
+
throw new AiServiceError('AI_TRANSCRIPTION_UNAVAILABLE');
|
|
124
|
+
}
|
|
125
|
+
return { transcribe };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function createSpeechService(options = {}) {
|
|
129
|
+
const piper = options.piper || {};
|
|
130
|
+
const gemini = options.gemini || {};
|
|
131
|
+
const now = options.now || Date.now;
|
|
132
|
+
const token = createTokenFileReader({ tokenFile: piper.tokenFile, read: piper.readTokenFile, now });
|
|
133
|
+
const pool = rotatingPool(gemini, now);
|
|
134
|
+
const fetchImpl = options.fetch || globalThis.fetch;
|
|
135
|
+
const piperRests = new Map();
|
|
136
|
+
/* Gemini (en ligne) d'abord, Piper (local) en repli ; `options.order` permet l'inverse. */
|
|
137
|
+
const order = Array.isArray(options.order) && options.order.length ? options.order : ['gemini', 'piper'];
|
|
138
|
+
async function viaPiper(clean, language, signal) {
|
|
139
|
+
if (!piper.baseUrl || (piperRests.get(language) || 0) > now()) return null;
|
|
140
|
+
try {
|
|
141
|
+
const key = await token();
|
|
142
|
+
const audio = await serviceRequest(endpoint(piper.baseUrl, '/tts'), { fetch: fetchImpl, body: { text: clean, lang: language }, signal, timeoutMs: piper.timeoutMs ?? 30000, read: 'bytes', headers: key ? { Authorization: `Bearer ${key}` } : {} });
|
|
143
|
+
const wave = readPcmWav(audio);
|
|
144
|
+
return { audio, format: 'wav', mimeType: 'audio/wav', sampleRate: wave.sampleRate, channels: wave.channels, provider: 'piper', model: null };
|
|
145
|
+
} catch (_error) { if (signal?.aborted) throw signal.reason; piperRests.set(language, now() + (piper.cooldownMs ?? 60000)); return null; }
|
|
146
|
+
}
|
|
147
|
+
async function viaGemini(clean, signal) {
|
|
148
|
+
for (const candidate of await pool.candidates(['gemini-2.5-flash-preview-tts'])) {
|
|
149
|
+
try {
|
|
150
|
+
const data = await serviceRequest(endpoint(gemini.endpoint || 'https://generativelanguage.googleapis.com/v1beta/models', `/${candidate.model}:generateContent`), {
|
|
151
|
+
fetch: fetchImpl, signal, timeoutMs: gemini.timeoutMs ?? 45000, headers: { 'x-goog-api-key': candidate.key },
|
|
152
|
+
body: { contents: [{ role: 'user', parts: [{ text: clean }] }], generationConfig: { responseModalities: ['AUDIO'], speechConfig: { voiceConfig: { prebuiltVoiceConfig: { voiceName: gemini.voice || 'Aoede' } } } } }
|
|
153
|
+
});
|
|
154
|
+
const part = data?.candidates?.[0]?.content?.parts?.find((one) => one.inlineData || one.inline_data);
|
|
155
|
+
const inline = part?.inlineData || part?.inline_data;
|
|
156
|
+
if (typeof inline?.data !== 'string' || !inline.data || inline.data.length % 4 || !/^[A-Za-z0-9+/]*={0,2}$/.test(inline.data)) throw new AiServiceError('AI_AUDIO_RESPONSE');
|
|
157
|
+
const mime = inline.mimeType || inline.mime_type || '';
|
|
158
|
+
const pcm = Buffer.from(inline.data, 'base64');
|
|
159
|
+
if (!/^audio\/(?:L16|pcm)(?:;|$)/i.test(mime) || !pcm.length || pcm.length % 2) throw new AiServiceError('AI_AUDIO_RESPONSE');
|
|
160
|
+
const sampleRate = Number(/rate=(\d+)/.exec(mime)?.[1] || 24000);
|
|
161
|
+
return { audio: pcmToWav(pcm, { sampleRate }), format: 'wav', mimeType: 'audio/wav', sampleRate, channels: 1, provider: 'gemini', model: candidate.model };
|
|
162
|
+
} catch (_error) { if (signal?.aborted) throw signal.reason; pool.rest(candidate.id); }
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
async function synthesize({ text, language = 'fr', signal, allowRemote = true } = {}) {
|
|
167
|
+
const clean = (options.prepareText || speakableText)(text);
|
|
168
|
+
if (!clean) throw new AiServiceError('AI_SPEECH_INPUT');
|
|
169
|
+
if (signal?.aborted) throw signal.reason;
|
|
170
|
+
for (const [rang, nom] of order.entries()) {
|
|
171
|
+
if (signal?.aborted) throw signal.reason;
|
|
172
|
+
const result = nom === 'piper' ? await viaPiper(clean, language, signal)
|
|
173
|
+
: nom === 'gemini' && allowRemote ? await viaGemini(clean, signal) : null;
|
|
174
|
+
if (result) return { ...result, fallback: rang > 0 };
|
|
175
|
+
}
|
|
176
|
+
throw new AiServiceError('AI_SPEECH_UNAVAILABLE');
|
|
177
|
+
}
|
|
178
|
+
return { synthesize };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
module.exports = { createTranscriptionService, createSpeechService, pcmToWav, readPcmWav, speakableText, doubtfulTranscription, WHISPER_STOCK_PHRASES };
|