mirror of
https://github.com/bitbaum/ai-kit.git
synced 2026-12-05 13:18:58 +01:00
heidi's Swiss German pages passed their locale, gsw, as the language hint. Groq answered 400 'unsupported language: gsw' on every link, and the microphone said dictation did not work in this browser. Romansh (rm) failed the same way. whisperLanguage() maps a tag or locale to a hint Whisper accepts: gsw -> de, de-CH -> de, unknown -> none (auto-detect). Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
368 lines
11 KiB
JavaScript
368 lines
11 KiB
JavaScript
/**
|
|
* `transcribe`'s reason to exist: the fleet's three hand-rolled transcription
|
|
* clients each had one vendor and one key, so a blip took dictation down with
|
|
* no fallback and a different error message per app.
|
|
*
|
|
* These assert that it borrows the SAME routing judgements `complete` makes —
|
|
* because the point of putting it here was that there is one answer to "is
|
|
* this vendor dead", not two.
|
|
*/
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
transcribe,
|
|
whisperLanguage,
|
|
ChainExhaustedError,
|
|
createHealthTracker,
|
|
} from "@bitbaum/ai-kit";
|
|
|
|
const ENV = { GROQ_API_KEY: "g", OPENROUTER_API_KEY: "o" };
|
|
|
|
function provider(id, keyEnv, models) {
|
|
return { id, baseUrl: `https://${id}.invalid/v1`, keyEnv, models, dailyTokens: 1000 };
|
|
}
|
|
|
|
function chain() {
|
|
const groq = provider("groq", "GROQ_API_KEY", ["whisper-big", "whisper-small"]);
|
|
const or = provider("openrouter", "OPENROUTER_API_KEY", ["whisper-free"]);
|
|
return [
|
|
{ provider: groq, model: "whisper-big" },
|
|
{ provider: groq, model: "whisper-small" },
|
|
{ provider: or, model: "whisper-free" },
|
|
];
|
|
}
|
|
|
|
const audio = () => new Blob([new Uint8Array([1, 2, 3])], { type: "audio/webm" });
|
|
|
|
function ok(text) {
|
|
return new Response(JSON.stringify({ text }), { status: 200 });
|
|
}
|
|
|
|
test("the first link that works answers, and says which one did", async () => {
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async () => ok("chunnsch au no verbi"),
|
|
});
|
|
|
|
assert.equal(result.text, "chunnsch au no verbi");
|
|
assert.equal(result.id, "groq/whisper-big");
|
|
});
|
|
|
|
test("a dead vendor steps aside for the next one", async () => {
|
|
// The whole reason this is not a single-vendor fetch.
|
|
const seen = [];
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async (url) => {
|
|
seen.push(new URL(url).host);
|
|
if (seen.length === 1) return new Response("upstream on fire", { status: 503 });
|
|
return ok("second time lucky");
|
|
},
|
|
});
|
|
|
|
assert.equal(result.text, "second time lucky");
|
|
assert.equal(result.id, "groq/whisper-small");
|
|
});
|
|
|
|
test("a DAILY 429 condemns the whole vendor, a busy minute does not", async () => {
|
|
// The judgement `complete` makes, made here by the same function rather than
|
|
// by a second copy that could disagree with it.
|
|
const tried = [];
|
|
await assert.rejects(
|
|
transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async (url, init) => {
|
|
tried.push(init.body.get("model"));
|
|
return new Response("Rate limit reached for model on tokens per day (TPD)", {
|
|
status: 429,
|
|
});
|
|
},
|
|
}),
|
|
ChainExhaustedError,
|
|
);
|
|
|
|
// groq's second model is skipped: it draws on the same exhausted daily budget.
|
|
assert.deepEqual(tried, ["whisper-big", "whisper-free"]);
|
|
});
|
|
|
|
test("the audio goes as multipart, with the model and the language", async () => {
|
|
let body;
|
|
await transcribe({
|
|
audio: audio(),
|
|
filename: "dictation.webm",
|
|
language: "de",
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async (_url, init) => {
|
|
body = init.body;
|
|
return ok("guet");
|
|
},
|
|
});
|
|
|
|
assert.ok(body instanceof FormData);
|
|
assert.equal(body.get("model"), "whisper-big");
|
|
assert.equal(body.get("language"), "de");
|
|
assert.equal(body.get("file").name, "dictation.webm");
|
|
});
|
|
|
|
test("a language Whisper has no code for never fails the take", async () => {
|
|
// heidi's Swiss German pages sent `gsw`; Groq answered 400 on every link and
|
|
// the microphone said it did not work. Swiss German is written as German,
|
|
// and anything else Whisper does not know is left to auto-detect.
|
|
const sent = async (language) => {
|
|
let body;
|
|
await transcribe({
|
|
audio: audio(),
|
|
language,
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async (_url, init) => {
|
|
body = init.body;
|
|
return ok("guet");
|
|
},
|
|
});
|
|
return body.get("language");
|
|
};
|
|
assert.equal(await sent("gsw"), "de");
|
|
assert.equal(await sent("de-CH"), "de");
|
|
assert.equal(await sent("rm"), null);
|
|
assert.equal(await sent("fr"), "fr");
|
|
});
|
|
|
|
test("whisperLanguage: tags and locales to a hint Whisper takes, or none", () => {
|
|
assert.equal(whisperLanguage("gsw-CH"), "de");
|
|
assert.equal(whisperLanguage("EN_us"), "en");
|
|
assert.equal(whisperLanguage("ru"), "ru");
|
|
assert.equal(whisperLanguage("haw"), "haw");
|
|
assert.equal(whisperLanguage("rm"), undefined);
|
|
assert.equal(whisperLanguage("xx"), undefined);
|
|
assert.equal(whisperLanguage(""), undefined);
|
|
assert.equal(whisperLanguage(undefined), undefined);
|
|
});
|
|
|
|
test("no content-type is set by hand", async () => {
|
|
// `fetch` writes it from the FormData, including the multipart boundary.
|
|
// Setting it manually omits the boundary and every vendor answers 400.
|
|
let headers;
|
|
await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async (_url, init) => {
|
|
headers = init.headers;
|
|
return ok("x");
|
|
},
|
|
});
|
|
|
|
const keys = Object.keys(headers).map((k) => k.toLowerCase());
|
|
assert.ok(!keys.includes("content-type"));
|
|
assert.equal(headers.authorization, "Bearer g");
|
|
});
|
|
|
|
test("it posts to the transcription endpoint, not the chat one", async () => {
|
|
let url;
|
|
await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async (u) => {
|
|
url = u;
|
|
return ok("x");
|
|
},
|
|
});
|
|
assert.equal(url, "https://groq.invalid/v1/audio/transcriptions");
|
|
});
|
|
|
|
test("an EMPTY transcript is an answer, not a failure", async () => {
|
|
// Silence really does transcribe to nothing. Demoting it would re-upload the
|
|
// same audio to every vendor in the chain to be told the same true thing.
|
|
let calls = 0;
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async () => {
|
|
calls += 1;
|
|
return ok("");
|
|
},
|
|
});
|
|
|
|
assert.equal(result.text, "");
|
|
assert.equal(calls, 1, "an empty transcript must not walk the chain");
|
|
});
|
|
|
|
test("a 200 carrying no text field IS a failure", async () => {
|
|
// A success code over a body that is not a transcript is an outage wearing
|
|
// the wrong clothes — the same call `complete` makes about empty content.
|
|
let calls = 0;
|
|
await assert.rejects(
|
|
transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async () => {
|
|
calls += 1;
|
|
return new Response(JSON.stringify({ error: "nope" }), { status: 200 });
|
|
},
|
|
}),
|
|
ChainExhaustedError,
|
|
);
|
|
assert.equal(calls, 3, "every link should have been tried");
|
|
});
|
|
|
|
test("a missing key skips the vendor rather than failing the call", async () => {
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: { OPENROUTER_API_KEY: "o" },
|
|
fetchImpl: async () => ok("from openrouter"),
|
|
});
|
|
assert.equal(result.id, "openrouter/whisper-free");
|
|
});
|
|
|
|
test("health records the outcome, so a dashboard can see it", async () => {
|
|
const health = createHealthTracker();
|
|
await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
health,
|
|
fetchImpl: async () => ok("hi"),
|
|
});
|
|
|
|
await assert.rejects(
|
|
transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
health,
|
|
fetchImpl: async () => new Response("down", { status: 500 }),
|
|
}),
|
|
ChainExhaustedError,
|
|
);
|
|
|
|
const after = health.getHealth();
|
|
assert.equal(after.consecutiveFailures, 1, "the exhausted chain counts once, not once per link");
|
|
assert.ok(after.lastSuccessAt !== null, "the earlier success was recorded");
|
|
assert.ok(after.lastError?.includes("500"), "and the reason survives for whoever reads it");
|
|
});
|
|
|
|
test("every failure is reported, not only the last", async () => {
|
|
// The failure that explains an outage is usually not the final one.
|
|
const error = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async () => new Response("nope", { status: 500 }),
|
|
}).catch((e) => e);
|
|
|
|
assert.ok(error instanceof ChainExhaustedError);
|
|
assert.equal(error.failures.length, 3);
|
|
});
|
|
|
|
// ── Word timings ─────────────────────────────────────────────────────────────
|
|
//
|
|
// Opt-in, because dictation needs only the text; a caller measuring HOW
|
|
// something was said needs to know when each word was.
|
|
|
|
test("word timings are requested only when asked for", async () => {
|
|
const forms = [];
|
|
const capture = async (_url, init) => {
|
|
forms.push(init.body);
|
|
return ok("x");
|
|
};
|
|
await transcribe({ audio: audio(), chain: chain(), env: ENV, fetchImpl: capture });
|
|
await transcribe({ audio: audio(), chain: chain(), env: ENV, words: true, fetchImpl: capture });
|
|
|
|
assert.equal(forms[0].get("response_format"), "json");
|
|
assert.deepEqual(forms[0].getAll("timestamp_granularities[]"), []);
|
|
assert.equal(forms[1].get("response_format"), "verbose_json");
|
|
assert.deepEqual(forms[1].getAll("timestamp_granularities[]"), ["word"]);
|
|
});
|
|
|
|
test("timings come back normalised, and impossible ones are dropped", async () => {
|
|
const body = {
|
|
text: "Guten Tag ich",
|
|
words: [
|
|
{ word: " Guten", start: 0.1, end: 0.4 },
|
|
{ word: "Tag", start: 0.45, end: 0.7 },
|
|
{ word: "broken", start: 1.2, end: 0.9 },
|
|
{ word: "", start: 1, end: 1.1 },
|
|
{ word: "nan", start: Number.NaN, end: 1 },
|
|
{ word: "ich", start: 1.3, end: 1.5 },
|
|
],
|
|
};
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
words: true,
|
|
fetchImpl: async () => new Response(JSON.stringify(body), { status: 200 }),
|
|
});
|
|
assert.deepEqual(result.words, [
|
|
{ word: "Guten", start: 0.1, end: 0.4 },
|
|
{ word: "Tag", start: 0.45, end: 0.7 },
|
|
{ word: "ich", start: 1.3, end: 1.5 },
|
|
]);
|
|
});
|
|
|
|
test("ABSENT IS NOT EMPTY: a vendor that ignores the granularity gives no words, not []", async () => {
|
|
// Reading absence as zero words would report a fluent speaker as silent.
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
words: true,
|
|
fetchImpl: async () => ok("the text is still fine"),
|
|
});
|
|
assert.equal(result.text, "the text is still fine");
|
|
assert.equal("words" in result, false);
|
|
});
|
|
|
|
test("missing timings do not fail the link or walk the chain", async () => {
|
|
// Re-uploading the audio to every vendor for a nice-to-have is the wrong trade.
|
|
let calls = 0;
|
|
await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
words: true,
|
|
fetchImpl: async () => {
|
|
calls++;
|
|
return ok("no timings here");
|
|
},
|
|
});
|
|
assert.equal(calls, 1);
|
|
});
|
|
|
|
test("a silent recording with timings requested is [] — measured, and nothing said", async () => {
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
words: true,
|
|
fetchImpl: async () => new Response(JSON.stringify({ text: "", words: [] }), { status: 200 }),
|
|
});
|
|
assert.deepEqual(result.words, []);
|
|
});
|
|
|
|
test("without the flag, timings are never returned even if the vendor sends them", async () => {
|
|
const result = await transcribe({
|
|
audio: audio(),
|
|
chain: chain(),
|
|
env: ENV,
|
|
fetchImpl: async () =>
|
|
new Response(JSON.stringify({ text: "a", words: [{ word: "a", start: 0, end: 1 }] }), {
|
|
status: 200,
|
|
}),
|
|
});
|
|
assert.equal("words" in result, false);
|
|
});
|