import { memo, useMemo, useState, useCallback, useEffect } from 'react' import { Pencil } from 'lucide-react' import { MessagePart } from './MessagePart' import { UserMessageActionButtons } from './UserMessageActionButtons' import { EditableUserMessage, ClickableUserMessage } from './EditableUserMessage' import { MessageError } from './MessageError' import type { Message, Part, MessageWithParts } from '@/api/types' import { useSessionStatusForSession } from '@/stores/sessionStatusStore' import { useSessionTodos } from '@/stores/sessionTodosStore' import { useSettings } from '@/hooks/useSettings' import type { components } from '@/api/opencode-types' import type { Todo } from '@/components/message/SessionTodoDisplay' import type { OpenCodeError } from '@/lib/opencode-errors' function getMessageTextContent(parts: Part[]): string { return parts .filter(p => p.type === 'text') .map(p => p.text || '') .join('\n\n') .trim() } interface MessageThreadProps { opcodeUrl: string sessionID: string directory?: string messages?: MessageWithParts[] onFileClick?: (filePath: string, lineNumber?: number) => void onChildSessionClick?: (sessionId: string) => void onUndoMessage?: (restoredPrompt: string) => void model?: string } const isMessageStreaming = (msg: Message): boolean => { if (msg.role !== 'assistant') return false return !('completed' in msg.time && msg.time.completed) } function isSessionInRetry(sessionStatus: { type?: string }): boolean { return sessionStatus?.type === 'retry' } const compareMessageIds = (id1: string, id2: string): number => { const num1 = parseInt(id1, 10) const num2 = parseInt(id2, 10) if (!isNaN(num1) && !isNaN(num2)) return num1 - num2 return id1.localeCompare(id2) } const hasRenderableContent = (role: Message['role'], parts: Part[], simpleChatMode: boolean, showReasoning: boolean): boolean => { if (!parts || parts.length === 0) return false return parts.some(part => { switch (part.type) { case 'text': return !!(part.text && part.text.trim()) case 'reasoning': return !simpleChatMode && showReasoning && !!(part.text && part.text.trim()) case 'file': return role === 'user' case 'patch': case 'snapshot': case 'agent': return !simpleChatMode case 'tool': return !simpleChatMode || part.tool === 'task' case 'retry': return true case 'step-finish': case 'step-start': case 'compaction': return false case 'subtask': return true default: return false } }) } function isTaskToolPart(part: Part): part is components['schemas']['ToolPart'] { return part.type === 'tool' && part.tool === 'task' } function isSubAgentActivityPart(part: Part): boolean { return part.type === 'subtask' || isTaskToolPart(part) } function hasTextContent(parts: Part[]): boolean { return parts.some(p => p.type === 'text' && !!(p.text && p.text.trim())) } function isIgnorableSubAgentMessagePart(part: Part): boolean { if (part.type === 'step-start' || part.type === 'step-finish' || part.type === 'compaction') { return true } if (part.type === 'text') { return !part.text?.trim() } if (part.type === 'reasoning') { return true } return false } function isStandaloneSubAgentMessage(role: Message['role'], parts: Part[]): boolean { if (role !== 'assistant') return false if (parts.length === 0) return false if (hasTextContent(parts)) return false const hasSubAgentActivity = parts.some(isSubAgentActivityPart) const allPartsAreSubAgentOrStructural = parts.every(part => { if (isIgnorableSubAgentMessagePart(part)) { return true } return isSubAgentActivityPart(part) }) return hasSubAgentActivity && allPartsAreSubAgentOrStructural } const findLastMessageByRole = ( messages: MessageWithParts[], role: 'user' | 'assistant', predicate?: (msg: Message) => boolean ): string | undefined => { for (let i = messages.length - 1; i >= 0; i--) { const msg = messages[i].info if (msg.role === role && (!predicate || predicate(msg))) { return msg.id } } return undefined } interface MessageRowProps { msgWithParts: MessageWithParts nextAssistantMessage: MessageWithParts | undefined pendingAssistantId: string | undefined lastUserMessageId: string | undefined isSessionBusy: boolean onUndoMessage?: (restoredPrompt: string) => void editingUserMessageId: string | null editingForAssistantId: string | null opcodeUrl: string sessionID: string directory?: string onFileClick?: (filePath: string, lineNumber?: number) => void onChildSessionClick?: (sessionId: string) => void handleStartEditUserMessage: (userMessageId: string, assistantMessageId: string) => void handleCancelEdit: () => void model?: string simpleChatMode: boolean showReasoning: boolean } const MessageRow = memo(function MessageRow({ msgWithParts, nextAssistantMessage, pendingAssistantId, lastUserMessageId, isSessionBusy, onUndoMessage, editingUserMessageId, editingForAssistantId, opcodeUrl, sessionID, directory, onFileClick, onChildSessionClick, handleStartEditUserMessage, handleCancelEdit, model, simpleChatMode, showReasoning, }: MessageRowProps) { const msg = msgWithParts.info const parts = msgWithParts.parts const streaming = isMessageStreaming(msg) const isQueued = msg.role === 'user' && pendingAssistantId && compareMessageIds(msg.id, pendingAssistantId) > 0 const isLastUserMessage = msg.role === 'user' && msg.id === lastUserMessageId const messageTextContent = getMessageTextContent(parts) const nextAssistantMsg = nextAssistantMessage?.info const isUserBeforeAssistant = msg.role === 'user' && nextAssistantMessage const canEditUserMessage = isLastUserMessage && isUserBeforeAssistant && !isSessionBusy const canUndoUserMessage = isLastUserMessage && nextAssistantMessage && !isSessionBusy && onUndoMessage const isEditingThisMessage = editingUserMessageId === msg.id const hasContent = hasRenderableContent(msg.role, parts, simpleChatMode, showReasoning) const hasError = msg.role === 'assistant' && 'error' in msg && msg.error const standaloneSubAgentMessage = isStandaloneSubAgentMessage(msg.role, parts) if (!hasContent && !hasError) { return null } if (standaloneSubAgentMessage) { return (