Add src/config/api-endpoints.ts with ENV variable support for all external API base URLs. This enables routing traffic through a proxy for multi-tenant deployments, usage metering, and security filtering. Supported ENV variables: - TELEGRAM_API_BASE (default: https://api.telegram.org) - DISCORD_API_BASE (default: https://discord.com/api/v10) - OPENAI_API_BASE (default: https://api.openai.com/v1) - ANTHROPIC_API_BASE (default: https://api.anthropic.com) - GOOGLE_GENERATIVE_API_BASE (default: https://generativelanguage.googleapis.com/v1beta) - GROQ_API_BASE (default: https://api.groq.com/openai/v1) - DEEPGRAM_API_BASE (default: https://api.deepgram.com/v1) - ELEVENLABS_API_BASE (default: https://api.elevenlabs.io) - OPENAI_TTS_BASE_URL (default: same as OPENAI_API_BASE) Updated files to use centralized configuration: - telegram/audit.ts, probe.ts, download.ts, bot/delivery.ts - channels/plugins/onboarding/telegram.ts - discord/api.ts, probe.ts - tts/tts.ts - memory/embeddings-openai.ts, embeddings-gemini.ts - media-understanding/providers/openai/audio.ts - media-understanding/providers/google/audio.ts, video.ts - media-understanding/providers/deepgram/audio.ts - media-understanding/providers/groq/index.ts Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { GOOGLE_GENERATIVE_API_BASE } from "../../../config/api-endpoints.js";
|
|
import { normalizeGoogleModelId } from "../../../agents/models-config.providers.js";
|
|
import type { AudioTranscriptionRequest, AudioTranscriptionResult } from "../../types.js";
|
|
import { fetchWithTimeout, normalizeBaseUrl, readErrorResponse } from "../shared.js";
|
|
|
|
export const DEFAULT_GOOGLE_AUDIO_BASE_URL = GOOGLE_GENERATIVE_API_BASE;
|
|
const DEFAULT_GOOGLE_AUDIO_MODEL = "gemini-3-flash-preview";
|
|
const DEFAULT_GOOGLE_AUDIO_PROMPT = "Transcribe the audio.";
|
|
|
|
function resolveModel(model?: string): string {
|
|
const trimmed = model?.trim();
|
|
if (!trimmed) return DEFAULT_GOOGLE_AUDIO_MODEL;
|
|
return normalizeGoogleModelId(trimmed);
|
|
}
|
|
|
|
function resolvePrompt(prompt?: string): string {
|
|
const trimmed = prompt?.trim();
|
|
return trimmed || DEFAULT_GOOGLE_AUDIO_PROMPT;
|
|
}
|
|
|
|
export async function transcribeGeminiAudio(
|
|
params: AudioTranscriptionRequest,
|
|
): Promise<AudioTranscriptionResult> {
|
|
const fetchFn = params.fetchFn ?? fetch;
|
|
const baseUrl = normalizeBaseUrl(params.baseUrl, DEFAULT_GOOGLE_AUDIO_BASE_URL);
|
|
const model = resolveModel(params.model);
|
|
const url = `${baseUrl}/models/${model}:generateContent`;
|
|
|
|
const headers = new Headers(params.headers);
|
|
if (!headers.has("content-type")) {
|
|
headers.set("content-type", "application/json");
|
|
}
|
|
if (!headers.has("x-goog-api-key")) {
|
|
headers.set("x-goog-api-key", params.apiKey);
|
|
}
|
|
|
|
const body = {
|
|
contents: [
|
|
{
|
|
role: "user",
|
|
parts: [
|
|
{ text: resolvePrompt(params.prompt) },
|
|
{
|
|
inline_data: {
|
|
mime_type: params.mime ?? "audio/wav",
|
|
data: params.buffer.toString("base64"),
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
const res = await fetchWithTimeout(
|
|
url,
|
|
{
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify(body),
|
|
},
|
|
params.timeoutMs,
|
|
fetchFn,
|
|
);
|
|
|
|
if (!res.ok) {
|
|
const detail = await readErrorResponse(res);
|
|
const suffix = detail ? `: ${detail}` : "";
|
|
throw new Error(`Audio transcription failed (HTTP ${res.status})${suffix}`);
|
|
}
|
|
|
|
const payload = (await res.json()) as {
|
|
candidates?: Array<{
|
|
content?: { parts?: Array<{ text?: string }> };
|
|
}>;
|
|
};
|
|
const parts = payload.candidates?.[0]?.content?.parts ?? [];
|
|
const text = parts
|
|
.map((part) => part?.text?.trim())
|
|
.filter(Boolean)
|
|
.join("\n");
|
|
if (!text) {
|
|
throw new Error("Audio transcription response missing text");
|
|
}
|
|
return { text, model };
|
|
}
|