openmaic-custom/lib/server/api-response.ts
wyuc 3e1e9f083b
feat(tts): unify provider-enablement model, default browser-native off (#665) (#666)
A single TTS provider-enablement model honored everywhere (picker,
auto-assignment, fallback, lecture/playback, server).

Model
- lib/audio/provider-enablement.ts: isTTSProviderConfigured /
  isTTSProviderEnabled / listEnabledTTSProviderIds. Availability drops the
  registry defaultBaseUrl fallback (fixes Lemonade's always-shown voices),
  honors the per-provider `enabled` flag, server-disable overrides (server
  precedence).
- voice-resolver: getEnabledProvidersWithVoices (enabled server/custom
  providers) and getSelectableProvidersWithVoices (the ONE client source of
  truth = enabled providers + opt-in browser-native when the browser exposes
  voices). resolveAgentVoice picks deterministically among the given providers,
  honors a voiceConfig only while its provider is present.
- Picker + discussion build their list from getSelectableProvidersWithVoices, so
  the AgentBar and the live discussion agree on selectable voices (fixes
  student agents going silent in auto mode). Students resolve via
  resolveAgentVoice; the teacher uses the global lecture voice verbatim (same
  voice the pre-generated lecture sends), so lecture and discussion never
  diverge.
- Voice assignment vs fallback: an agent's voice is bound at generation (the
  agent-profile LLM picks from availableVoices; preset uses the user's pick).
  The fallback for a missing/invalid/unavailable voice is applied only at the
  live TTS calls, not baked into the saved agent — so it reflects current
  provider availability.

Defaults / opt-in
- Browser-native: first-class, default OFF, opt-in. Built-in providers default
  enabled:true (only surface once configured).
- TTS master toggle defaults OFF; first server-sync auto-enables it when a
  provider is configured (mirrors image/video).
- Persist v3->v4 migration normalizes the never-user-editable flags.

Server/admin disable (server precedence): server-providers.yml `enabled:false`
and env `TTS_<P>_ENABLED=false` (incl. browser-native). Surfaced as a per-provider
`disabled` flag on /api/server-providers; merged as serverDisabled; enforced at
the /api/generate/tts trust boundary, classroom media generation, and /api/health.

UI
- Settings TTS panel: per-provider enable toggle, disabled (with a hint) when the
  provider is unavailable, locked off (with a note) when server-disabled.
- AgentBar voice pills render disabled with a "No voice" placeholder when no
  provider is enabled. The toolbar media-popover TTS tab keeps its existing
  on/off toggle (no picker/CTA).

Tests: predicates, resolver determinism + unified selectable list, server
force-disable, store merge + migration + TTS auto-enable, i18n parity. Verified
in a real browser (Playwright): settings toggle states + a full Gemini classroom
generation whose TTS requests used the picked voice.

Found on main @ 2586cb5.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: 杨慎 <117187635+cosarah@users.noreply.github.com>
2026-06-07 17:25:57 +08:00

50 lines
1.4 KiB
TypeScript

import { NextResponse } from 'next/server';
export const API_ERROR_CODES = {
MISSING_REQUIRED_FIELD: 'MISSING_REQUIRED_FIELD',
MISSING_API_KEY: 'MISSING_API_KEY',
INVALID_REQUEST: 'INVALID_REQUEST',
PROVIDER_DISABLED: 'PROVIDER_DISABLED',
VOXCPM_AUTO_VOICE_REQUIRES_CONTEXT: 'VOXCPM_AUTO_VOICE_REQUIRES_CONTEXT',
INVALID_URL: 'INVALID_URL',
REDIRECT_NOT_ALLOWED: 'REDIRECT_NOT_ALLOWED',
TOO_MANY_REDIRECTS: 'TOO_MANY_REDIRECTS',
CONTENT_SENSITIVE: 'CONTENT_SENSITIVE',
UPSTREAM_ERROR: 'UPSTREAM_ERROR',
RATE_LIMITED: 'RATE_LIMITED',
GENERATION_FAILED: 'GENERATION_FAILED',
TRANSCRIPTION_FAILED: 'TRANSCRIPTION_FAILED',
PARSE_FAILED: 'PARSE_FAILED',
INTERNAL_ERROR: 'INTERNAL_ERROR',
} as const;
export type ApiErrorCode = (typeof API_ERROR_CODES)[keyof typeof API_ERROR_CODES];
export interface ApiErrorBody {
success: false;
errorCode: ApiErrorCode;
error: string;
details?: string;
}
export function apiError(
code: ApiErrorCode,
status: number,
error: string,
details?: string,
): NextResponse<ApiErrorBody> {
return NextResponse.json(
{
success: false as const,
errorCode: code,
error,
...(details ? { details } : {}),
},
{ status },
);
}
export function apiSuccess<T extends Record<string, unknown>>(data: T, status = 200): NextResponse {
return NextResponse.json({ success: true, ...data }, { status });
}