import { memo } from 'react'
import type { components } from '@/api/opencode-types'
import { Volume2, VolumeX, Loader2 } from 'lucide-react'
import { TextPart } from './TextPart'
import { PatchPart } from './PatchPart'
import { ToolCallPart } from './ToolCallPart'
import { RetryPart } from './RetryPart'
import { useTTS } from '@/hooks/useTTS'
import { useMobile } from '@/hooks/useMobile'
import { CopyButton } from '@/components/ui/copy-button'
type RetryPartType = components['schemas']['RetryPart']
type Part = components['schemas']['Part']
interface MessagePartProps {
part: Part
role?: string
allParts?: Part[]
partIndex?: number
onFileClick?: (filePath: string, lineNumber?: number) => void
onChildSessionClick?: (sessionId: string) => void
messageTextContent?: string
}
function getCopyableContent(part: Part, allParts?: Part[]): string {
switch (part.type) {
case 'text':
return part.text || ''
case 'patch':
return `Patch: ${part.hash}\nFiles: ${part.files.join(', ')}`
case 'tool':
if (part.state.status === 'completed' && part.state.input) {
return JSON.stringify(part.state.input, null, 2)
} else if (part.state.status === 'running' && part.state.input) {
return JSON.stringify(part.state.input, null, 2)
}
return `Tool: ${part.tool} (${part.state.status})`
case 'reasoning':
return part.text || ''
case 'snapshot':
return part.snapshot || ''
case 'agent':
return `Agent: ${part.name}`
case 'step-finish':
if (allParts) {
return allParts
.filter(p => p.type === 'text')
.map(p => p.text || '')
.join('\n\n')
.trim()
}
return ''
case 'file':
return part.filename || part.url || 'File'
default:
return ''
}
}
interface TTSButtonProps {
messageId: string
content: string
className?: string
}
function TTSButton({ messageId, content, className = "" }: TTSButtonProps) {
const { speakMessage, stop, isEnabled, isPlaying, isLoading, activeMessageId } = useTTS()
if (!isEnabled || !content.trim()) {
return null
}
const isThisPlaying = (isPlaying || isLoading) && activeMessageId === messageId
const handleClick = () => {
if (isThisPlaying) {
stop()
} else {
speakMessage(messageId, content)
}
}
return (
)
}
export const MessagePart = memo(function MessagePart({ part, role, allParts, partIndex, onFileClick, onChildSessionClick, messageTextContent }: MessagePartProps) {
const copyableContent = getCopyableContent(part, allParts)
const isMobile = useMobile()
switch (part.type) {
case 'text':
if (role === 'user' && allParts && partIndex !== undefined) {
const nextPart = allParts[partIndex + 1]
if (nextPart && nextPart.type === 'file') {
return null
}
}
return
Reasoning