Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion electron/voice-catalog.d.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type VoiceProvider = "elevenlabs" | "openai" | "groq" | "deepgram" | "assemblyai" | "whisper-cpp" | "openai-compatible";
export type VoiceProvider = "elevenlabs" | "openai" | "groq" | "deepgram" | "assemblyai" | "sarvam" | "whisper-cpp" | "openai-compatible";

export interface VoiceProviderInfo {
name: string;
Expand Down
10 changes: 9 additions & 1 deletion electron/voice-catalog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// (Settings > Voice, the engine choice), by electron/voice-providers.mjs (the requests) and by the phone.

/** Services in the order Settings lists them. */
export const VOICE_PROVIDERS = ["elevenlabs", "openai", "groq", "deepgram", "assemblyai", "whisper-cpp", "openai-compatible"];
export const VOICE_PROVIDERS = ["elevenlabs", "openai", "groq", "deepgram", "assemblyai", "sarvam", "whisper-cpp", "openai-compatible"];

export const VOICE_PROVIDER_INFO = {
elevenlabs: {
Expand Down Expand Up @@ -46,6 +46,14 @@ export const VOICE_PROVIDER_INFO = {
keyUrl: "https://www.assemblyai.com/app/api-keys",
placeholder: "",
},
sarvam: {
name: "Sarvam AI",
models: ["saaras:v4", "saaras:v3"],
realtime: false,
key: "required",
keyUrl: "https://dashboard.sarvam.ai/key-management",
placeholder: "",
},
"whisper-cpp": {
name: "whisper.cpp",
models: [],
Expand Down
4 changes: 3 additions & 1 deletion electron/voice-input-main.node-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ test("keys: bad services and bad keys are refused before anything is written", a

test("voiceKeyStatus ignores junk in the stored document", () => {
assert.deepEqual(voiceKeyStatus({ [VOICE_KEYS_CREDENTIAL_KEY]: { openai: 5, groq: "g", "whisper-cpp": "x", evil: "y" } }),
{ elevenlabs: false, openai: false, groq: true, deepgram: false, assemblyai: false, "openai-compatible": false });
{ elevenlabs: false, openai: false, groq: true, deepgram: false, assemblyai: false, sarvam: false, "openai-compatible": false });
assert.equal(voiceKeyStatus({ [VOICE_KEYS_CREDENTIAL_KEY]: ["x"] }).openai, false);
});

test("start options are checked field by field", () => {
assert.deepEqual(sessionOptions({ provider: "groq", language: "en-US", model: "whisper-large-v3", extra: "dropped" }),
{ provider: "groq", language: "en-US", model: "whisper-large-v3", realtime: false, baseUrl: undefined, whisperCpp: undefined });
assert.deepEqual(sessionOptions({ provider: "sarvam", language: "hi-IN", model: "saaras:v4" }),
{ provider: "sarvam", language: "hi-IN", model: "saaras:v4", realtime: false, baseUrl: undefined, whisperCpp: undefined });
for (const bad of [null, "groq", { provider: "nope" }, { provider: "groq", language: "en US; rm" }, { provider: "groq", model: "a b" },
{ provider: "groq", model: "m".repeat(101) }, { provider: "groq", language: 5 }, { provider: "openai-compatible", baseUrl: {} }]) {
assert.throws(() => sessionOptions(bad));
Expand Down
27 changes: 26 additions & 1 deletion electron/voice-providers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ const languageOf = (tag) => {
return /^[a-z]{2,3}$/.test(primary) ? primary : "";
};

const SARVAM_LANGUAGES = new Set([
"en", "hi", "bn", "kn", "ml", "mr", "od", "pa", "ta", "te", "gu",
"as", "ur", "ne", "kok", "ks", "sd", "sa", "sat", "mni", "brx", "mai", "doi",
]);

/** Sarvam Saaras takes `*-IN` BCP 47 codes (`od-IN` for Odia, `en-IN` for English) or `unknown` to auto-detect. */
function sarvamLanguage(tag) {
const primary = languageOf(tag);
if (primary === "or") return "od-IN";
if (SARVAM_LANGUAGES.has(primary)) return `${primary}-IN`;
return "unknown";
}

/** One fetch with the shared failure mapping; returns the Response only when it is ok. */
async function send(doFetch, url, init, service) {
let response;
Expand Down Expand Up @@ -176,13 +189,25 @@ export async function transcribeClip(request) {
else query.set("detect_language", "true");
url = `https://api.deepgram.com/v1/listen?${query}`;
init = { method: "POST", headers: { authorization: `Token ${key}`, "content-type": "audio/wav" }, body: wav };
} else if (provider === "sarvam") {
const form = new FormData();
form.append("file", wavBlob(wav), "audio.wav");
form.append("model", model);
form.append("mode", "transcribe");
form.append("language_code", sarvamLanguage(request.language));
url = "https://api.sarvam.ai/speech-to-text";
init = { method: "POST", headers: { "api-subscription-key": key }, body: form };
} else {
throw new VoiceProviderError("not-set-up", "This speech service is not available here.");
}

const response = await send(doFetch, url, { ...init, signal }, service);
const body = await json(response, service);
const text = provider === "deepgram" ? body?.results?.channels?.[0]?.alternatives?.[0]?.transcript : body?.text;
const text = provider === "deepgram"
? body?.results?.channels?.[0]?.alternatives?.[0]?.transcript
: provider === "sarvam"
? body?.transcript
: body?.text;
return typeof text === "string" ? text.trim() : "";
}

Expand Down
28 changes: 28 additions & 0 deletions electron/voice-providers.node-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ test("AssemblyAI: an error status is a service failure", async () => {
await assert.rejects(transcribeClip({ provider: "assemblyai", wav, key: "aa", fetch }), (error) => error.reason === "service" && /audio too short/.test(error.message));
});

test("Sarvam AI: multipart to speech-to-text with api-subscription-key, saaras:v4, transcribe mode and language mapping", async () => {
const { fetch, calls } = fakeFetch(() => ok({ transcript: " namaste world " }));
assert.equal(await transcribeClip({ provider: "sarvam", wav, key: "sv-key", language: "en-US", fetch }), "namaste world");
const [{ url, init }] = calls;
assert.equal(url, "https://api.sarvam.ai/speech-to-text");
assert.equal(init.method, "POST");
assert.equal(init.headers["api-subscription-key"], "sv-key");
assert.equal(init.headers.authorization, undefined);
assert.equal(init.body.get("model"), "saaras:v4");
assert.equal(init.body.get("mode"), "transcribe");
assert.equal(init.body.get("language_code"), "en-IN");
assert.equal(init.body.get("file").type, "audio/wav");
assert.equal(init.body.get("file").size, wav.length);

for (const [language, expected] of [
["hi-IN", "hi-IN"],
["or-IN", "od-IN"],
["mr", "mr-IN"],
["fr-FR", "unknown"],
[undefined, "unknown"],
]) {
await transcribeClip({ provider: "sarvam", wav, key: "sv-key", language, model: "saaras:v3", fetch });
const latest = calls.at(-1).init.body;
assert.equal(latest.get("model"), "saaras:v3");
assert.equal(latest.get("language_code"), expected);
}
});

test("failures map to bad-key, rate-limited, network and service; the key never shows in a message", async () => {
const cases = [
[new Response("{}", { status: 401 }), "bad-key"],
Expand Down
4 changes: 4 additions & 0 deletions src/lib/voice-input/voice-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe("which engine listens", () => {
it("uses a chosen service once its key is saved on this device", () => {
expect(resolveEngine(prefs({ engine: "elevenlabs" }), mac, { elevenlabs: true })).toEqual({ kind: "provider", provider: "elevenlabs" });
expect(resolveEngine(prefs({ engine: "groq" }), iphone, { groq: true })).toEqual({ kind: "provider", provider: "groq" });
expect(resolveEngine(prefs({ engine: "sarvam" }), windows, { sarvam: true })).toEqual({ kind: "provider", provider: "sarvam" });
});

it("falls back to the built-in one, saying why, while a chosen service is not set up or cannot run here", () => {
Expand Down Expand Up @@ -57,7 +58,10 @@ describe("which engine listens", () => {
// a gateway on this computer may take no key at all
expect(providerReady("openai-compatible", prefs({ compatBaseUrl: "http://127.0.0.1:8317/v1" }), {})).toBe(true);
expect(providerReady("deepgram", prefs(), { deepgram: false })).toBe(false);
expect(providerReady("sarvam", prefs(), { sarvam: false })).toBe(false);
expect(providerReady("sarvam", prefs(), { sarvam: true })).toBe(true);
expect(providerRunsOn("whisper-cpp", windows)).toBe(true);
expect(providerRunsOn("sarvam", iphone)).toBe(true);
expect(providerRunsOn("assemblyai", browser)).toBe(false);
});

Expand Down
Loading