mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
- Speech-to-Text: full pipeline with sttCore handler, /v1/audio/transcriptions endpoint, sttConfig for OpenAI, Gemini, Groq, Deepgram, AssemblyAI, HuggingFace, NVIDIA Parakeet; new 9router-stt skill - Gemini TTS: add gemini provider with 30 prebuilt voices and TTS_PROVIDER_CONFIG - Usage: implement GLM (intl/cn) and MiniMax (intl/cn) quota fetchers; refactor Gemini CLI usage to use retrieveUserQuota with per-model buckets - Disabled models: lowdb-backed disabledModelsDb + /api/models/disabled route - Header search: reusable Zustand store (headerSearchStore) wired into Header - CLI tools: add Claude Cowork tool card and cowork-settings API - Providers: introduce mediaPriority sorting in getProvidersByKind, add Kimi K2.6, reorder hermes, drop qwen STT kind - UI: expand media-providers/[kind]/[id] page (+314), enhance OAuthModal, ModelSelectModal, ProviderTopology, ProxyPools, ProviderLimits - Assets: refresh provider PNGs (alicode, byteplus, cloudflare-ai, nvidia, ollama, vertex, volcengine-ark) and add aws-polly, fal-ai, jina-ai, recraft, runwayml, stability-ai, topaz, black-forest-labs
89 lines
3.6 KiB
JavaScript
89 lines
3.6 KiB
JavaScript
import {
|
|
extractApiKey, isValidApiKey,
|
|
getProviderCredentials, markAccountUnavailable,
|
|
} from "../services/auth.js";
|
|
import { getSettings } from "@/lib/localDb";
|
|
import { getModelInfo } from "../services/model.js";
|
|
import { handleSttCore } from "open-sse/handlers/sttCore.js";
|
|
import { errorResponse, unavailableResponse } from "open-sse/utils/error.js";
|
|
import { HTTP_STATUS } from "open-sse/config/runtimeConfig.js";
|
|
import { AI_PROVIDERS } from "@/shared/constants/providers";
|
|
import * as log from "../utils/logger.js";
|
|
|
|
// Providers requiring credentials for STT
|
|
const CREDENTIALED_PROVIDERS = new Set(
|
|
Object.entries(AI_PROVIDERS)
|
|
.filter(([, p]) => p.serviceKinds?.includes("stt") && !p.noAuth && p.sttConfig?.authType !== "none")
|
|
.map(([id]) => id)
|
|
);
|
|
|
|
export async function handleStt(request) {
|
|
let formData;
|
|
try {
|
|
formData = await request.formData();
|
|
} catch {
|
|
return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid multipart form data");
|
|
}
|
|
|
|
const modelStr = formData.get("model");
|
|
log.request("POST", `/v1/audio/transcriptions | ${modelStr}`);
|
|
|
|
const settings = await getSettings();
|
|
if (settings.requireApiKey) {
|
|
const apiKey = extractApiKey(request);
|
|
if (!apiKey) return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Missing API key");
|
|
const valid = await isValidApiKey(apiKey);
|
|
if (!valid) return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Invalid API key");
|
|
}
|
|
|
|
if (!modelStr) return errorResponse(HTTP_STATUS.BAD_REQUEST, "Missing model");
|
|
if (!formData.get("file")) return errorResponse(HTTP_STATUS.BAD_REQUEST, "Missing required field: file");
|
|
|
|
const modelInfo = await getModelInfo(modelStr);
|
|
if (!modelInfo.provider) return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid model format");
|
|
|
|
const { provider, model } = modelInfo;
|
|
log.info("ROUTING", `Provider: ${provider}, Model: ${model}`);
|
|
|
|
// noAuth providers
|
|
if (!CREDENTIALED_PROVIDERS.has(provider)) {
|
|
const result = await handleSttCore({ provider, model, formData });
|
|
if (result.success) return result.response;
|
|
return errorResponse(result.status || HTTP_STATUS.BAD_GATEWAY, result.error || "STT failed");
|
|
}
|
|
|
|
// Credentialed — fallback loop
|
|
const excludeConnectionIds = new Set();
|
|
let lastError = null;
|
|
let lastStatus = null;
|
|
|
|
while (true) {
|
|
const credentials = await getProviderCredentials(provider, excludeConnectionIds, model);
|
|
|
|
if (!credentials || credentials.allRateLimited) {
|
|
if (credentials?.allRateLimited) {
|
|
const msg = lastError || credentials.lastError || "Unavailable";
|
|
const status = lastStatus || Number(credentials.lastErrorCode) || HTTP_STATUS.SERVICE_UNAVAILABLE;
|
|
return unavailableResponse(status, `[${provider}/${model}] ${msg}`, credentials.retryAfter, credentials.retryAfterHuman);
|
|
}
|
|
if (excludeConnectionIds.size === 0) return errorResponse(HTTP_STATUS.BAD_REQUEST, `No credentials for provider: ${provider}`);
|
|
return errorResponse(lastStatus || HTTP_STATUS.SERVICE_UNAVAILABLE, lastError || "All accounts unavailable");
|
|
}
|
|
|
|
log.info("AUTH", `\x1b[32mUsing ${provider} account: ${credentials.connectionName}\x1b[0m`);
|
|
|
|
const result = await handleSttCore({ provider, model, formData, credentials });
|
|
|
|
if (result.success) return result.response;
|
|
|
|
const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider, model);
|
|
if (shouldFallback) {
|
|
excludeConnectionIds.add(credentials.connectionId);
|
|
lastError = result.error;
|
|
lastStatus = result.status;
|
|
continue;
|
|
}
|
|
return result.response || errorResponse(result.status, result.error);
|
|
}
|
|
}
|