mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Fix TTS sanitization and message handling bugs
- Add duplicate config validation to backend - Fix TTS button comparison using original text - Track original vs sanitized text in TTS context - Update toast import in message hooks - Add type safety for error handling
This commit is contained in:
1 parent
698ad7b020
commit
44240f0aab
9 files changed
+23
-10
No files matched your search
@@ -138,6 +138,9 @@ export function createSettingsRoutes(db: Database) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return c.json({ error: 'Invalid config data', details: error.issues }, 400)
|
||||
}
|
||||
if (error instanceof Error && error.message.includes('already exists')) {
|
||||
return c.json({ error: error.message }, 409)
|
||||
}
|
||||
return c.json({ error: 'Failed to create OpenCode config' }, 500)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -156,6 +156,12 @@ export class SettingsService {
|
||||
request: CreateOpenCodeConfigRequest,
|
||||
userId: string = 'default'
|
||||
): OpenCodeConfigWithRaw {
|
||||
// Check for existing config with the same name
|
||||
const existing = this.getOpenCodeConfigByName(request.name, userId)
|
||||
if (existing) {
|
||||
throw new Error(`Config with name '${request.name}' already exists`)
|
||||
}
|
||||
|
||||
const rawContent = typeof request.content === 'string'
|
||||
? request.content
|
||||
: JSON.stringify(request.content, null, 2)
|
||||
|
||||
@@ -65,13 +65,13 @@ interface TTSButtonProps {
|
||||
}
|
||||
|
||||
export function TTSButton({ content, className = "" }: TTSButtonProps) {
|
||||
const { speak, stop, isEnabled, isPlaying, isLoading, currentText } = useTTS()
|
||||
const { speak, stop, isEnabled, isPlaying, isLoading, originalText } = useTTS()
|
||||
|
||||
if (!isEnabled || !content.trim()) {
|
||||
return null
|
||||
}
|
||||
|
||||
const isThisPlaying = (isPlaying || isLoading) && currentText === content
|
||||
const isThisPlaying = (isPlaying || isLoading) && originalText === content
|
||||
|
||||
const handleClick = () => {
|
||||
if (isThisPlaying) {
|
||||
@@ -86,7 +86,7 @@ export function TTSButton({ content, className = "" }: TTSButtonProps) {
|
||||
onClick={handleClick}
|
||||
className={`p-1.5 rounded ${isThisPlaying ? 'bg-red-500/20 text-red-500 hover:bg-red-500/30' : 'bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground'} ${className}`}
|
||||
title={isThisPlaying ? "Stop playback" : "Read aloud"}
|
||||
disabled={isLoading && currentText !== content}
|
||||
disabled={isLoading && originalText !== content}
|
||||
>
|
||||
{isLoading && isThisPlaying ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
|
||||
@@ -243,7 +243,7 @@ export function TextPart({ part }: TextPartProps) {
|
||||
remarkPlugins={[remarkGfm]}
|
||||
rehypePlugins={[rehypeHighlight, rehypeRaw]}
|
||||
components={{
|
||||
code({ className, children, node, ...props }) {
|
||||
code({ className, children, ...props }) {
|
||||
const isInline = !className || !className.includes('language-')
|
||||
|
||||
if (className?.includes('language-mermaid') && mermaidComplete) {
|
||||
|
||||
@@ -36,7 +36,7 @@ export function CreateConfigDialog({ isOpen, onOpenChange, onCreate, isUpdating
|
||||
setIsDefault(false)
|
||||
setError('')
|
||||
setErrorLine(null)
|
||||
} catch (error) {
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof SyntaxError) {
|
||||
const match = error.message.match(/line (\d+)/i)
|
||||
const line = match ? parseInt(match[1]) : null
|
||||
|
||||
@@ -31,6 +31,7 @@ export function TTSProvider({ children }: TTSProviderProps) {
|
||||
const [state, setState] = useState<TTSState>('idle')
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [currentText, setCurrentText] = useState<string | null>(null)
|
||||
const [originalText, setOriginalText] = useState<string | null>(null)
|
||||
|
||||
const abortControllerRef = useRef<AbortController | null>(null)
|
||||
const audioRef = useRef<HTMLAudioElement | null>(null)
|
||||
@@ -67,6 +68,7 @@ export function TTSProvider({ children }: TTSProviderProps) {
|
||||
cleanup()
|
||||
setState('idle')
|
||||
setCurrentText(null)
|
||||
setOriginalText(null)
|
||||
setError(null)
|
||||
}, [cleanup])
|
||||
|
||||
@@ -240,7 +242,7 @@ export function TTSProvider({ children }: TTSProviderProps) {
|
||||
stoppedRef.current = false
|
||||
setError(null)
|
||||
|
||||
// Store the sanitized text for playback
|
||||
setOriginalText(text)
|
||||
setCurrentText(sanitizedText)
|
||||
|
||||
abortControllerRef.current = new AbortController()
|
||||
@@ -257,6 +259,7 @@ export function TTSProvider({ children }: TTSProviderProps) {
|
||||
state,
|
||||
error,
|
||||
currentText,
|
||||
originalText,
|
||||
isEnabled,
|
||||
isPlaying: state === 'playing',
|
||||
isLoading: state === 'loading',
|
||||
|
||||
@@ -8,6 +8,7 @@ export interface TTSContextValue {
|
||||
state: TTSState
|
||||
error: string | null
|
||||
currentText: string | null
|
||||
originalText: string | null
|
||||
isEnabled: boolean
|
||||
isPlaying: boolean
|
||||
isLoading: boolean
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { createOpenCodeClient } from '@/api/opencode'
|
||||
import { toast } from '@/lib/toast'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import type { MessageListResponse } from '@/api/types'
|
||||
|
||||
interface UseRemoveMessageOptions {
|
||||
@@ -46,7 +46,7 @@ export function useRemoveMessage({ opcodeUrl, sessionId, directory }: UseRemoveM
|
||||
)
|
||||
}
|
||||
|
||||
toast.error('Failed to remove message')
|
||||
showToast.error('Failed to remove message')
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({
|
||||
@@ -117,7 +117,7 @@ export function useRefreshMessage({ opcodeUrl, sessionId, directory }: UseRefres
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('Failed to refresh message:', error)
|
||||
toast.error('Failed to refresh message')
|
||||
showToast.error('Failed to refresh message')
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -71,7 +71,7 @@ describe('sanitizeForTTS', () => {
|
||||
it('should return empty string for empty input', () => {
|
||||
expect(sanitizeForTTS('')).toBe('')
|
||||
expect(sanitizeForTTS(' ')).toBe('')
|
||||
expect(sanitizeForTTS(null as any)).toBe('')
|
||||
expect(sanitizeForTTS(null as unknown as string)).toBe('')
|
||||
})
|
||||
|
||||
it('should handle headers and lists combined', () => {
|
||||
|
||||
Reference in new issue
Block a user