mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add TTS support with OpenAI-compatible endpoint configuration
- Add TTS settings schema and configuration UI with validation - Create backend TTS proxy with 24hr file-based caching - Add frontend useTTS hook with Cache API caching - Add TTS playback button on assistant messages (step-finish) - Include test button to verify TTS configuration
This commit is contained in:
1 parent
283348b64f
commit
fb91b4d3a3
12 files changed
+859
-5
No files matched your search
@@ -1,12 +1,22 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { TTSSettings } from './TTSSettings'
|
||||
|
||||
export function GeneralSettings() {
|
||||
const { preferences, isLoading, updateSettings, isUpdating } = useSettings()
|
||||
|
||||
const [gitToken, setGitToken] = useState('')
|
||||
|
||||
useEffect(() => {
|
||||
if (preferences) {
|
||||
setGitToken(preferences.gitToken || '')
|
||||
}
|
||||
}, [preferences])
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
@@ -108,8 +118,9 @@ export function GeneralSettings() {
|
||||
id="gitToken"
|
||||
type="password"
|
||||
placeholder="ghp_..."
|
||||
value={preferences?.gitToken || ''}
|
||||
onChange={(e) => updateSettings({ gitToken: e.target.value })}
|
||||
value={gitToken}
|
||||
onChange={(e) => setGitToken(e.target.value)}
|
||||
onBlur={() => updateSettings({ gitToken })}
|
||||
className="bg-background border-border text-foreground placeholder:text-muted-foreground"
|
||||
/>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
@@ -124,6 +135,10 @@ export function GeneralSettings() {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mt-6">
|
||||
<TTSSettings />
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,317 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { z } from 'zod'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
import { useTTS } from '@/hooks/useTTS'
|
||||
import { Loader2, Volume2, Square, CheckCircle, XCircle } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Slider } from '@/components/ui/slider'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
|
||||
import { DEFAULT_TTS_CONFIG } from '@/api/types/settings'
|
||||
|
||||
const TEST_PHRASE = 'Text to speech is working correctly.'
|
||||
|
||||
const ttsFormSchema = z.object({
|
||||
enabled: z.boolean(),
|
||||
endpoint: z.string().url('Must be a valid URL').min(1, 'Endpoint is required'),
|
||||
apiKey: z.string().min(1, 'API key is required when TTS is enabled').or(z.literal('')),
|
||||
voice: z.string().min(1, 'Voice is required'),
|
||||
model: z.string().min(1, 'Model is required'),
|
||||
speed: z.number().min(0.25).max(4.0),
|
||||
}).refine((data) => {
|
||||
if (data.enabled && !data.apiKey) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}, {
|
||||
message: 'API key is required when TTS is enabled',
|
||||
path: ['apiKey'],
|
||||
})
|
||||
|
||||
type TTSFormValues = z.infer<typeof ttsFormSchema>
|
||||
|
||||
export function TTSSettings() {
|
||||
const { preferences, isLoading, updateSettings, isUpdating } = useSettings()
|
||||
const { speak, stop, isPlaying, isLoading: isTTSLoading, error: ttsError } = useTTS()
|
||||
const [testStatus, setTestStatus] = useState<'idle' | 'success' | 'error'>('idle')
|
||||
|
||||
const form = useForm<TTSFormValues>({
|
||||
resolver: zodResolver(ttsFormSchema),
|
||||
defaultValues: DEFAULT_TTS_CONFIG,
|
||||
})
|
||||
|
||||
const { reset, formState: { isDirty, isValid } } = form
|
||||
const watchEnabled = form.watch('enabled')
|
||||
const watchApiKey = form.watch('apiKey')
|
||||
|
||||
const canTest = watchEnabled && watchApiKey && !isDirty
|
||||
|
||||
useEffect(() => {
|
||||
if (preferences?.tts) {
|
||||
reset({
|
||||
enabled: preferences.tts.enabled ?? DEFAULT_TTS_CONFIG.enabled,
|
||||
endpoint: preferences.tts.endpoint ?? DEFAULT_TTS_CONFIG.endpoint,
|
||||
apiKey: preferences.tts.apiKey ?? DEFAULT_TTS_CONFIG.apiKey,
|
||||
voice: preferences.tts.voice ?? DEFAULT_TTS_CONFIG.voice,
|
||||
model: preferences.tts.model ?? DEFAULT_TTS_CONFIG.model,
|
||||
speed: preferences.tts.speed ?? DEFAULT_TTS_CONFIG.speed,
|
||||
})
|
||||
}
|
||||
}, [preferences?.tts, reset])
|
||||
|
||||
const onSubmit = (data: TTSFormValues) => {
|
||||
updateSettings({ tts: data })
|
||||
}
|
||||
|
||||
const handleTest = async () => {
|
||||
setTestStatus('idle')
|
||||
try {
|
||||
await speak(TEST_PHRASE)
|
||||
setTestStatus('success')
|
||||
setTimeout(() => setTestStatus('idle'), 3000)
|
||||
} catch {
|
||||
setTestStatus('error')
|
||||
}
|
||||
}
|
||||
|
||||
const handleStopTest = () => {
|
||||
stop()
|
||||
setTestStatus('idle')
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-card border border-border rounded-lg p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<h2 className="text-lg font-semibold text-foreground">Text-to-Speech</h2>
|
||||
<Button
|
||||
onClick={form.handleSubmit(onSubmit)}
|
||||
disabled={!isDirty || !isValid || isUpdating}
|
||||
size="sm"
|
||||
>
|
||||
{isUpdating ? (
|
||||
<>
|
||||
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
||||
Saving...
|
||||
</>
|
||||
) : (
|
||||
'Save'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="enabled"
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex flex-row items-center justify-between rounded-lg border border-border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<FormLabel className="text-base">Enable TTS</FormLabel>
|
||||
<FormDescription>
|
||||
Allow text-to-speech playback for messages
|
||||
</FormDescription>
|
||||
</div>
|
||||
<FormControl>
|
||||
<Switch
|
||||
checked={field.value}
|
||||
onCheckedChange={field.onChange}
|
||||
/>
|
||||
</FormControl>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
{watchEnabled && (
|
||||
<>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="endpoint"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>TTS Endpoint</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder="https://api.openai.com/v1/audio/speech"
|
||||
className="bg-background"
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
OpenAI-compatible TTS API endpoint
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="apiKey"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>API Key</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
type="password"
|
||||
placeholder="sk-..."
|
||||
className="bg-background"
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
API key for the TTS service
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="voice"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Voice</FormLabel>
|
||||
<Select onValueChange={field.onChange} value={field.value}>
|
||||
<FormControl>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select a voice" />
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="alloy">Alloy</SelectItem>
|
||||
<SelectItem value="echo">Echo</SelectItem>
|
||||
<SelectItem value="fable">Fable</SelectItem>
|
||||
<SelectItem value="onyx">Onyx</SelectItem>
|
||||
<SelectItem value="nova">Nova</SelectItem>
|
||||
<SelectItem value="shimmer">Shimmer</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormDescription>
|
||||
Voice for text-to-speech output
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="model"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Model</FormLabel>
|
||||
<Select onValueChange={field.onChange} value={field.value}>
|
||||
<FormControl>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select a model" />
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="tts-1">TTS-1 (faster)</SelectItem>
|
||||
<SelectItem value="tts-1-hd">TTS-1-HD (higher quality)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormDescription>
|
||||
TTS model quality
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="speed"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<div className="flex justify-between">
|
||||
<FormLabel>Speed</FormLabel>
|
||||
<span className="text-sm text-muted-foreground">
|
||||
{field.value.toFixed(2)}x
|
||||
</span>
|
||||
</div>
|
||||
<FormControl>
|
||||
<Slider
|
||||
min={0.25}
|
||||
max={4.0}
|
||||
step={0.25}
|
||||
value={[field.value]}
|
||||
onValueChange={([value]) => field.onChange(value)}
|
||||
className="w-full"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
Playback speed (0.25x to 4.0x)
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="flex flex-row items-center justify-between rounded-lg border border-border p-4">
|
||||
<div className="space-y-0.5">
|
||||
<div className="text-base font-medium">Test TTS</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{isDirty
|
||||
? 'Save changes before testing'
|
||||
: 'Verify your TTS configuration works'}
|
||||
</p>
|
||||
{ttsError && (
|
||||
<p className="text-sm text-destructive flex items-center gap-1">
|
||||
<XCircle className="h-4 w-4" />
|
||||
{ttsError}
|
||||
</p>
|
||||
)}
|
||||
{testStatus === 'success' && (
|
||||
<p className="text-sm text-green-500 flex items-center gap-1">
|
||||
<CheckCircle className="h-4 w-4" />
|
||||
TTS is working
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={isPlaying || isTTSLoading ? handleStopTest : handleTest}
|
||||
disabled={!canTest}
|
||||
>
|
||||
{isTTSLoading ? (
|
||||
<>
|
||||
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
||||
Testing...
|
||||
</>
|
||||
) : isPlaying ? (
|
||||
<>
|
||||
<Square className="h-4 w-4 mr-2" />
|
||||
Stop
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Volume2 className="h-4 w-4 mr-2" />
|
||||
Test
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user