@astratra/ai 1.6.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +412 -6
- package/guards/package.json +4 -0
- package/guards/testing/package.json +4 -0
- package/package.json +29 -6
- package/src/aiConfig.js +72 -0
- package/src/gemini.js +9 -3
- package/src/guards/createGuards.js +456 -0
- package/src/guards/frame.js +36 -0
- package/src/guards/index.d.ts +150 -0
- package/src/guards/index.js +16 -0
- package/src/guards/leaks.js +63 -0
- package/src/guards/recommended.js +31 -0
- package/src/guards/testing/index.d.ts +30 -0
- package/src/guards/testing/index.js +116 -0
- package/src/index.d.ts +294 -4
- package/src/index.js +9 -1
- package/src/localModels.js +99 -0
- package/src/managedRouter.js +258 -0
- package/src/masking.js +34 -1
- 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
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// update() is an atomic read/modify/write boundary. Database adapters must
|
|
4
|
+
// serialize it across processes (Redis CAS, PostgreSQL row/advisory locks).
|
|
5
|
+
function createMemoryRoutingStore() {
|
|
6
|
+
const states = new Map();
|
|
7
|
+
const pending = new Map();
|
|
8
|
+
const clone = (value) => value == null ? null : globalThis.structuredClone(value);
|
|
9
|
+
async function update(key, change) {
|
|
10
|
+
const previous = pending.get(key) || Promise.resolve();
|
|
11
|
+
const current = previous.catch(() => {}).then(() => {
|
|
12
|
+
const { state, value } = change(clone(states.get(key)));
|
|
13
|
+
if (state == null) states.delete(key);
|
|
14
|
+
else states.set(key, clone(state));
|
|
15
|
+
return value;
|
|
16
|
+
});
|
|
17
|
+
pending.set(key, current);
|
|
18
|
+
try { return await current; }
|
|
19
|
+
finally { if (pending.get(key) === current) pending.delete(key); }
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
update,
|
|
23
|
+
async read(key) { await pending.get(key); return clone(states.get(key)); }
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const CAS = `
|
|
28
|
+
local old = redis.call('GET', KEYS[1])
|
|
29
|
+
if (old or '') ~= ARGV[1] then return 0 end
|
|
30
|
+
if ARGV[2] == '' then redis.call('DEL', KEYS[1])
|
|
31
|
+
else redis.call('SET', KEYS[1], ARGV[2], 'PX', ARGV[3]) end
|
|
32
|
+
return 1`;
|
|
33
|
+
|
|
34
|
+
// The client is owned by the consumer. No connection or silent RAM fallback.
|
|
35
|
+
function createRedisRoutingStore({ client, prefix = 'astratra:ai:', ttlMs = 172800000, maxRetries = 100 } = {}) {
|
|
36
|
+
if (!Number.isFinite(ttlMs) || ttlMs < 86400000 || !Number.isInteger(maxRetries) || maxRetries < 1) throw new TypeError('INVALID_REDIS_STATE_OPTIONS');
|
|
37
|
+
if (!client || typeof client.eval !== 'function' || typeof client.get !== 'function') throw new TypeError('REDIS_CLIENT_REQUIRED');
|
|
38
|
+
return {
|
|
39
|
+
async read(key) {
|
|
40
|
+
const raw = await client.get(prefix + key);
|
|
41
|
+
return raw == null ? null : JSON.parse(raw);
|
|
42
|
+
},
|
|
43
|
+
async update(key, change) {
|
|
44
|
+
for (let attempt = 0; attempt < maxRetries; attempt += 1) {
|
|
45
|
+
const raw = await client.get(prefix + key);
|
|
46
|
+
const { state, value } = change(raw == null ? null : JSON.parse(raw));
|
|
47
|
+
const stored = await client.eval(CAS, {
|
|
48
|
+
keys: [prefix + key], arguments: [raw || '', state == null ? '' : JSON.stringify(state), String(ttlMs)]
|
|
49
|
+
});
|
|
50
|
+
if (Number(stored) === 1) return value;
|
|
51
|
+
}
|
|
52
|
+
throw new Error('AI_STATE_CONTENTION');
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function createPostgresRoutingStore({ pool, table = 'ai_routing_state' } = {}) {
|
|
58
|
+
if (!/^[a-z_][a-z0-9_]*$/.test(table)) throw new TypeError('INVALID_STATE_TABLE');
|
|
59
|
+
if (!pool || typeof pool.connect !== 'function') throw new TypeError('POSTGRES_POOL_REQUIRED');
|
|
60
|
+
return {
|
|
61
|
+
// Explicit: construction does not mutate a consumer's database.
|
|
62
|
+
async initialize() { await pool.query(`CREATE TABLE IF NOT EXISTS ${table} (key text PRIMARY KEY, state jsonb NOT NULL)`); },
|
|
63
|
+
async read(key) {
|
|
64
|
+
const { rows } = await pool.query(`SELECT state FROM ${table} WHERE key = $1`, [key]);
|
|
65
|
+
return rows[0]?.state ?? null;
|
|
66
|
+
},
|
|
67
|
+
async update(key, change) {
|
|
68
|
+
const connection = await pool.connect();
|
|
69
|
+
try {
|
|
70
|
+
await connection.query('BEGIN');
|
|
71
|
+
// Lock also protects first insertion; SELECT FOR UPDATE alone does not.
|
|
72
|
+
await connection.query('SELECT pg_advisory_xact_lock(hashtextextended($1, 0))', [`${table}:${key}`]);
|
|
73
|
+
const { rows } = await connection.query(`SELECT state FROM ${table} WHERE key = $1`, [key]);
|
|
74
|
+
const { state, value } = change(rows[0]?.state ?? null);
|
|
75
|
+
if (state == null) await connection.query(`DELETE FROM ${table} WHERE key = $1`, [key]);
|
|
76
|
+
else await connection.query(`INSERT INTO ${table} (key, state) VALUES ($1, $2::jsonb) ON CONFLICT (key) DO UPDATE SET state = EXCLUDED.state`, [key, JSON.stringify(state)]);
|
|
77
|
+
await connection.query('COMMIT');
|
|
78
|
+
return value;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
await connection.query('ROLLBACK');
|
|
81
|
+
throw error;
|
|
82
|
+
} finally { connection.release(); }
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = { createMemoryRoutingStore, createRedisRoutingStore, createPostgresRoutingStore };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { readFile } = require('node:fs/promises');
|
|
3
|
+
|
|
4
|
+
class AiServiceError extends Error {
|
|
5
|
+
constructor(code, status = null) {
|
|
6
|
+
super(code);
|
|
7
|
+
this.name = 'AiServiceError';
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.statusCode = status;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function createTokenFileReader({ tokenFile, read = readFile, now = Date.now, refreshMs = 60000 } = {}) {
|
|
15
|
+
let cached;
|
|
16
|
+
let expires = 0;
|
|
17
|
+
let loading;
|
|
18
|
+
return async () => {
|
|
19
|
+
if (!tokenFile) return null;
|
|
20
|
+
if (now() < expires) return cached;
|
|
21
|
+
if (!loading) loading = Promise.resolve().then(() => read(tokenFile, 'utf8')).then((text) => {
|
|
22
|
+
cached = String(text).trim() || null;
|
|
23
|
+
expires = now() + refreshMs;
|
|
24
|
+
return cached;
|
|
25
|
+
}, () => { throw new AiServiceError('AI_TOKEN_UNAVAILABLE'); }).finally(() => { loading = null; });
|
|
26
|
+
return loading;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The deadline covers response body parsing, not just the HTTP headers.
|
|
31
|
+
async function serviceRequest(url, { fetch: fetchImpl = globalThis.fetch, timeoutMs = 15000, signal, headers = {}, body, rawBody, read = 'json' } = {}) {
|
|
32
|
+
if (!url) throw new AiServiceError('AI_SERVICE_UNCONFIGURED');
|
|
33
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) throw new AiServiceError('AI_INVALID_TIMEOUT');
|
|
34
|
+
const controller = new globalThis.AbortController();
|
|
35
|
+
const timer = setTimeout(() => controller.abort(new AiServiceError('AI_SERVICE_TIMEOUT')), timeoutMs);
|
|
36
|
+
const combined = signal ? globalThis.AbortSignal.any([signal, controller.signal]) : controller.signal;
|
|
37
|
+
try {
|
|
38
|
+
combined.throwIfAborted();
|
|
39
|
+
const response = await fetchImpl(url, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: { ...(body === undefined ? {} : { 'content-type': 'application/json' }), ...headers },
|
|
42
|
+
body: body === undefined ? rawBody : JSON.stringify(body), signal: combined
|
|
43
|
+
});
|
|
44
|
+
if (!response.ok) throw new AiServiceError('AI_SERVICE_HTTP', response.status);
|
|
45
|
+
try { return read === 'bytes' ? Buffer.from(await response.arrayBuffer()) : await response.json(); }
|
|
46
|
+
catch (error) { if (combined.aborted) throw error; throw new AiServiceError('AI_SERVICE_RESPONSE'); }
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (signal?.aborted) throw signal.reason;
|
|
49
|
+
if (controller.signal.aborted) throw new AiServiceError('AI_SERVICE_TIMEOUT');
|
|
50
|
+
if (error instanceof AiServiceError) throw error;
|
|
51
|
+
throw new AiServiceError('AI_SERVICE_UNREACHABLE');
|
|
52
|
+
} finally { clearTimeout(timer); }
|
|
53
|
+
}
|
|
54
|
+
const endpoint = (base, path) => base ? `${base.replace(/\/+$/, '')}${path}` : null;
|
|
55
|
+
|
|
56
|
+
module.exports = { AiServiceError, createTokenFileReader, serviceRequest, endpoint };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { createLanguageDetector } = require('./language');
|
|
3
|
+
const { serviceRequest, endpoint } = require('./serviceHttp');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_LANGUAGE_WORDS = Object.freeze({
|
|
6
|
+
fr: ['le', 'la', 'les', 'un', 'une', 'des', 'du', 'de', 'et', 'est', 'je', 'tu', 'il', 'elle', 'nous', 'vous', 'ils', 'que', 'qui', 'quoi', 'pour', 'dans', 'sur', 'avec', 'pas', 'ne', 'mon', 'ma', 'mes', 'ton', 'ta', 'tes', 'son', 'sa', 'ses', 'ce', 'cette', 'comment', 'pourquoi', 'quand', 'où', 'merci', 'bonjour', 'salut', 'oui', 'non', 'aide', 'aide-moi', 'explique', 'explique-moi', 'peux', 'veux', 'était', 'sont'],
|
|
7
|
+
en: ['the', 'a', 'an', 'and', 'is', 'are', 'was', 'were', 'i', 'you', 'he', 'she', 'we', 'they', 'what', 'who', 'why', 'how', 'when', 'where', 'for', 'in', 'on', 'with', 'not', 'my', 'your', 'his', 'her', 'our', 'this', 'that', 'thanks', 'thank', 'hello', 'hi', 'yes', 'no', 'help', 'me', 'please', 'explain', 'can', 'could', 'would', 'do', 'does'],
|
|
8
|
+
es: ['el', 'los', 'las', 'una', 'unos', 'unas', 'es', 'son', 'yo', 'tú', 'usted', 'nosotros', 'vosotros', 'ellos', 'qué', 'cómo', 'por', 'para', 'con', 'mi', 'mis', 'hola', 'gracias', 'ayuda', 'explica', 'puedes', 'quiero', 'dónde']
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
function createServiceLanguageDetector(options = {}) {
|
|
12
|
+
const detector = createLanguageDetector({
|
|
13
|
+
...options, words: options.words || DEFAULT_LANGUAGE_WORDS, fallback: options.fallback || 'fr',
|
|
14
|
+
identify: async (text) => {
|
|
15
|
+
if (!options.baseUrl) return null;
|
|
16
|
+
const data = await serviceRequest(endpoint(options.baseUrl, '/detect'), { fetch: options.fetch, body: { text }, timeoutMs: options.timeoutMs ?? 800 });
|
|
17
|
+
if (typeof data?.lang !== 'string' || !Number.isFinite(data.confidence) || data.confidence < 0 || data.confidence > 1) return null;
|
|
18
|
+
return { language: data.lang, confidence: data.confidence };
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return detector;
|
|
22
|
+
}
|
|
23
|
+
module.exports = { createServiceLanguageDetector, DEFAULT_LANGUAGE_WORDS };
|
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 };
|