import { useState } from "react"; import useSWR from "swr"; import { Check, ChevronDown, SlidersHorizontal } from "lucide-react"; import { fetchSpeechVoiceCatalog } from "../speech-voice-catalog"; import { Button } from "@/shared/ui/button"; import { Popover, PopoverContent, PopoverTrigger } from "@/shared/ui/popover"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/shared/ui/command"; import { getSpeechVoiceDescription, getSpeechVoiceName, setSelectedSpeechVoice, useSelectedSpeechVoice } from "../speech-voices"; import { AGENT_MESSAGE_ACTION_CLASS_NAME, AGENT_MESSAGE_ACTION_LABEL_CLASS_NAME } from "./agent-chat-message-chrome"; export function AgentSpeechVoicePicker() { const [open, setOpen] = useState(false); const selected = useSelectedSpeechVoice(); const { data, error, isLoading, mutate } = useSWR("/api/tts/edge/voices", fetchSpeechVoiceCatalog, { revalidateOnFocus: false, revalidateOnReconnect: false, dedupingInterval: 60 * 60 * 1_000, shouldRetryOnError: false }); const selectedVoice = data?.voices.find((voice) => voice.id === (selected || data.defaultVoice)); const defaultVoice = data?.voices.find((voice) => voice.id === data.defaultVoice); const selectedName = selectedVoice ? getSpeechVoiceName(selectedVoice) : selected ? "已选音色" : "默认音色"; const choose = (id: string) => { setSelectedSpeechVoice(id); setOpen(false); }; return (

普通话音色

{data ? `${data.voices.length} 种音色,选择后下次播放生效` : "选择后下次播放生效"}

{isLoading ?

正在加载音色…

: null} {error ?

音色列表加载失败

{error instanceof Error ?

{error.message}

: null}
: null} 没有找到匹配的音色 choose("")}> 默认音色{defaultVoice ? `(${getSpeechVoiceName(defaultVoice)})` : ""} {!selected ? {selected && data && !selectedVoice ?

原选音色已不可用,请重新选择。

: null} {data?.voices.map((voice) => { const name = getSpeechVoiceName(voice); const description = getSpeechVoiceDescription(voice); return choose(voice.id)}> {name} {description} {selected === voice.id ? ; })}

音色偏好保存在当前浏览器。

); }