diff --git a/frontend/src/components/message/MessagePart.test.tsx b/frontend/src/components/message/MessagePart.test.tsx index e49f9ad7..ad3a0bed 100644 --- a/frontend/src/components/message/MessagePart.test.tsx +++ b/frontend/src/components/message/MessagePart.test.tsx @@ -341,7 +341,16 @@ describe('MessagePart', () => { sessionID: 'test-session', }) - it('renders null for tool part when simpleChatMode is true', () => { + const createSubtaskPart = (): MessagePartType => ({ + type: 'subtask', + prompt: 'Please review this change', + description: 'Review changes', + agent: 'auditor', + command: 'review', + sessionID: 'test-session', + }) + + it('renders null for non-task tool part when simpleChatMode is true', () => { setupSettings({ simpleChatMode: true, showReasoning: false, @@ -358,6 +367,78 @@ describe('MessagePart', () => { expect(container.firstChild).toBeNull() }) + it('renders a compact clickable task row when simpleChatMode is true', () => { + setupSettings({ + simpleChatMode: true, + showReasoning: false, + expandToolCalls: false, + expandDiffs: true, + autoScroll: true, + theme: 'dark', + mode: 'build', + }) + + const part = { + ...createToolPart(), + tool: 'task', + callID: 'call-1', + metadata: { sessionId: 'child-session' }, + state: { + status: 'completed', + input: { description: 'Review changes' }, + output: 'done', + time: { start: Date.now(), end: Date.now() + 100 }, + }, + } as MessagePartType + const onChildSessionClick = vi.fn() + + render() + + expect(screen.getByText('Review changes')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('View details')).not.toBeInTheDocument() + fireEvent.click(screen.getByRole('button')) + expect(onChildSessionClick).toHaveBeenCalledWith('child-session') + }) + + it('renders a compact subtask row when simpleChatMode is true', () => { + setupSettings({ + simpleChatMode: true, + showReasoning: false, + expandToolCalls: false, + expandDiffs: true, + autoScroll: true, + theme: 'dark', + mode: 'build', + }) + + render() + + expect(screen.getByText('Review changes')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('auditor')).not.toBeInTheDocument() + expect(screen.queryByText('Please review this change')).not.toBeInTheDocument() + }) + + it('renders the same compact subtask row when simpleChatMode is false', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + expandToolCalls: false, + expandDiffs: true, + autoScroll: true, + theme: 'dark', + mode: 'build', + }) + + render() + + expect(screen.getByText('Review changes')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('Sub-agent: auditor')).not.toBeInTheDocument() + expect(screen.queryByText('Please review this change')).not.toBeInTheDocument() + }) + it('renders null for patch part when simpleChatMode is true', () => { setupSettings({ simpleChatMode: true, diff --git a/frontend/src/components/message/MessagePart.tsx b/frontend/src/components/message/MessagePart.tsx index db3eb491..6e1011f4 100644 --- a/frontend/src/components/message/MessagePart.tsx +++ b/frontend/src/components/message/MessagePart.tsx @@ -43,6 +43,8 @@ function getCopyableContent(part: Part, allParts?: Part[]): string { return part.snapshot || '' case 'agent': return `Agent: ${part.name}` + case 'subtask': + return `${part.agent}: ${part.description}\n\n${part.prompt}`.trim() case 'step-finish': if (allParts) { return allParts @@ -120,7 +122,7 @@ export const MessagePart = memo(function MessagePart({ part, role, allParts, par if (simpleChatMode) return null return case 'tool': - if (simpleChatMode) return null + if (simpleChatMode && part.tool !== 'task') return null return case 'reasoning': if (simpleChatMode || !showReasoning) return null @@ -171,7 +173,21 @@ export const MessagePart = memo(function MessagePart({ part, role, allParts, par ) case 'retry': return + case 'subtask': { + const label = part.description || part.prompt || 'Sub-agent task' + return ( +
+
+ {label} + sub-agent +
+
+ ) + } + case 'step-start': + case 'compaction': + return null default: - return + return null } }) diff --git a/frontend/src/components/message/MessageThread.test.tsx b/frontend/src/components/message/MessageThread.test.tsx new file mode 100644 index 00000000..3fc8c52e --- /dev/null +++ b/frontend/src/components/message/MessageThread.test.tsx @@ -0,0 +1,400 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { render, screen, fireEvent } from '@testing-library/react' +import { MessageThread } from './MessageThread' + +const mocks = vi.hoisted(() => ({ + useSessionStatus: vi.fn(), + useSessionTodos: vi.fn(), + useSettings: vi.fn(), + usePermissions: vi.fn(), + useQuestions: vi.fn(), +})) + +vi.mock('@/stores/sessionStatusStore', () => ({ + useSessionStatusForSession: () => mocks.useSessionStatus(), +})) + +vi.mock('@/stores/sessionTodosStore', () => ({ + useSessionTodos: mocks.useSessionTodos, +})) + +vi.mock('@/hooks/useSettings', () => ({ + useSettings: mocks.useSettings, +})) + +vi.mock('@/contexts/EventContext', () => ({ + usePermissions: () => mocks.usePermissions(), + useQuestions: () => mocks.useQuestions(), +})) + +interface MockSettingsReturn { + preferences: { + simpleChatMode: boolean + showReasoning: boolean + } | undefined +} + +const setupSettings = (preferences: MockSettingsReturn['preferences']) => { + mocks.useSettings.mockReturnValue({ + preferences, + isLoading: false, + updateSettings: vi.fn(), + isUpdating: false, + }) +} + +const createTextPart = (text: string, messageId: string) => ({ + type: 'text' as const, + text, + sessionID: 'test-session', + messageID: messageId, + id: 'part-1', +}) + +const createTaskToolPart = (description: string, sessionId: string | undefined, messageId: string) => ({ + type: 'tool' as const, + tool: 'task', + sessionID: 'test-session', + messageID: messageId, + id: 'part-2', + callID: 'call-1', + metadata: sessionId ? { sessionId } : undefined, + state: { + status: 'completed' as const, + input: { description }, + output: 'done', + title: 'Task', + metadata: {}, + time: { start: Date.now(), end: Date.now() + 100 }, + }, +}) + +const createSubtaskPart = (description: string, messageId: string) => ({ + type: 'subtask' as const, + prompt: 'Please review this', + description, + agent: 'reviewer', + sessionID: 'test-session', + messageID: messageId, + id: 'part-3', +}) + +const createStepFinishPart = (messageId: string) => ({ + type: 'step-finish' as const, + sessionID: 'test-session', + messageID: messageId, + id: 'part-4', + reason: 'stop', + cost: 0, + tokens: { + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, + }, +}) + +const createReasoningPart = (text: string, messageId: string) => ({ + type: 'reasoning' as const, + text, + sessionID: 'test-session', + messageID: messageId, + id: 'part-5', +}) + +const createAssistantMessage = ( + id: string, + parts: unknown[], + modelID?: string +) => ({ + info: { + id, + role: 'assistant' as const, + sessionID: 'test-session', + parentID: 'parent-1', + providerID: 'test-provider', + mode: 'build', + time: { + created: Date.now(), + completed: Date.now() + 100, + }, + modelID: modelID || 'test-model', + }, + parts, +}) + +const createUserMessage = (id: string, text: string) => ({ + info: { + id, + role: 'user' as const, + sessionID: 'test-session', + agent: 'test-agent', + model: 'test-model', + time: { + created: Date.now(), + }, + }, + parts: [createTextPart(text, id)], +}) + +describe('MessageThread', () => { + beforeEach(() => { + mocks.useSessionStatus.mockReturnValue({ type: 'idle' }) + mocks.useSessionTodos.mockReturnValue({ setTodos: vi.fn() }) + mocks.usePermissions.mockReturnValue({ + getForCallID: vi.fn(() => null), + }) + mocks.useQuestions.mockReturnValue({ + getForCallID: vi.fn(() => null), + }) + }) + + it('renders assistant message with only subtask part as standalone row without header', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [createSubtaskPart('Review changes', '2')]), + ] + + const onChildSessionClick = vi.fn() + + render( + + ) + + expect(screen.getByText('Review changes')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('test-model')).not.toBeInTheDocument() + }) + + it('renders assistant message with only task tool part as standalone row without header', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [createTaskToolPart('Do something', 'child-session', '2')]), + ] + + const onChildSessionClick = vi.fn() + + const { container } = render( + + ) + + expect(screen.getByText('Do something')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('test-model')).not.toBeInTheDocument() + + const buttons = container.querySelectorAll('button') + expect(buttons.length).toBeGreaterThan(0) + fireEvent.click(buttons[buttons.length - 1]) + expect(onChildSessionClick).toHaveBeenCalledWith('child-session') + }) + + it('renders assistant task message with empty text and step finish as standalone row', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [ + createTextPart(' ', '2'), + createTaskToolPart('Explore codebase structure', 'child-session', '2'), + createStepFinishPart('2'), + ]), + ] + + render( + + ) + + expect(screen.getByText('Explore codebase structure')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('test-model')).not.toBeInTheDocument() + }) + + it('renders assistant task message with hidden reasoning as standalone row', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [ + createReasoningPart('I should use the explore agent', '2'), + createTextPart('\n\n', '2'), + createTaskToolPart('Explore codebase structure', 'child-session', '2'), + createStepFinishPart('2'), + ]), + ] + + render( + + ) + + expect(screen.getByText('Explore codebase structure')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('test-model')).not.toBeInTheDocument() + expect(screen.queryByText('I should use the explore agent')).not.toBeInTheDocument() + }) + + it('renders assistant task message with simple-chat reasoning setting as standalone row', () => { + setupSettings({ + simpleChatMode: true, + showReasoning: true, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [ + createReasoningPart('I should use the explore agent', '2'), + createTextPart('\n\n', '2'), + createTaskToolPart('Explore codebase structure', 'child-session', '2'), + createStepFinishPart('2'), + ]), + ] + + render( + + ) + + expect(screen.getByText('Explore codebase structure')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('test-model')).not.toBeInTheDocument() + expect(screen.queryByText('I should use the explore agent')).not.toBeInTheDocument() + }) + + it('renders assistant task message with visible reasoning as standalone row when no text exists', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: true, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [ + createReasoningPart('I should use the explore agent', '2'), + createTextPart('\n\n', '2'), + createTaskToolPart('Explore codebase structure', 'child-session', '2'), + createStepFinishPart('2'), + ]), + ] + + render( + + ) + + expect(screen.getByText('Explore codebase structure')).toBeInTheDocument() + expect(screen.getByText('sub-agent')).toBeInTheDocument() + expect(screen.queryByText('I should use the explore agent')).not.toBeInTheDocument() + expect(screen.queryByText('test-model')).not.toBeInTheDocument() + }) + + it('renders assistant message with text normally with header', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [createTextPart('This is a response', '2')]), + ] + + render( + + ) + + expect(screen.getByText('This is a response')).toBeInTheDocument() + expect(screen.getByText('test-model')).toBeInTheDocument() + }) + + it('renders assistant message with text and subtask normally with header', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + createAssistantMessage('2', [ + createTextPart('Here is the analysis', '2'), + createSubtaskPart('Review changes', '2'), + ]), + ] + + render( + + ) + + expect(screen.getByText('Here is the analysis')).toBeInTheDocument() + expect(screen.getByText('Review changes')).toBeInTheDocument() + expect(screen.getByText('test-model')).toBeInTheDocument() + }) + + it('renders user messages normally', () => { + setupSettings({ + simpleChatMode: false, + showReasoning: false, + }) + + const messages = [ + createUserMessage('1', 'Hello'), + ] + + render( + + ) + + expect(screen.getByText('Hello')).toBeInTheDocument() + expect(screen.getByText('You')).toBeInTheDocument() + }) +}) diff --git a/frontend/src/components/message/MessageThread.tsx b/frontend/src/components/message/MessageThread.tsx index e5ffe90d..4d102d03 100644 --- a/frontend/src/components/message/MessageThread.tsx +++ b/frontend/src/components/message/MessageThread.tsx @@ -47,6 +47,78 @@ const compareMessageIds = (id1: string, id2: string): number => { 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', @@ -80,6 +152,7 @@ interface MessageRowProps { handleCancelEdit: () => void model?: string simpleChatMode: boolean + showReasoning: boolean } const MessageRow = memo(function MessageRow({ @@ -101,6 +174,7 @@ const MessageRow = memo(function MessageRow({ handleCancelEdit, model, simpleChatMode, + showReasoning, }: MessageRowProps) { const msg = msgWithParts.info const parts = msgWithParts.parts @@ -117,6 +191,39 @@ const MessageRow = memo(function MessageRow({ 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 ( +
+
+ {parts.filter(isSubAgentActivityPart).map((part, partIndex) => ( +
+ +
+ ))} +
+
+ ) + } + return (
)) ) : null} - {msg.role === 'assistant' && 'error' in msg && msg.error && ( + {hasError && ( )}
@@ -233,6 +340,7 @@ export const MessageThread = memo(function MessageThread({ const sessionStatus = useSessionStatusForSession(sessionID) const { preferences } = useSettings() const simpleChatMode = preferences?.simpleChatMode ?? false + const showReasoning = preferences?.showReasoning ?? false const pendingAssistantId = useMemo(() => { if (!messages) return undefined @@ -328,6 +436,7 @@ export const MessageThread = memo(function MessageThread({ handleCancelEdit={handleCancelEdit} model={model} simpleChatMode={simpleChatMode} + showReasoning={showReasoning} /> ))} diff --git a/frontend/src/components/message/ToolCallPart.tsx b/frontend/src/components/message/ToolCallPart.tsx index 876c3d1c..3497e85f 100644 --- a/frontend/src/components/message/ToolCallPart.tsx +++ b/frontend/src/components/message/ToolCallPart.tsx @@ -14,6 +14,15 @@ interface ToolCallPartProps { part: ToolPart onFileClick?: (filePath: string, lineNumber?: number) => void onChildSessionClick?: (sessionId: string) => void + simpleChatMode?: boolean +} + +function getTaskSessionId(part: ToolPart): string | undefined { + let sessionId = part.metadata?.sessionId as string | undefined + if (!sessionId && part.state.status !== 'pending' && 'metadata' in part.state) { + sessionId = part.state.metadata?.sessionId as string | undefined + } + return sessionId } function ClickableJson({ json, onFileClick }: { json: unknown; onFileClick?: (filePath: string) => void }) { @@ -142,6 +151,40 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal const previewText = getPreviewText() const isFileTool = ['read', 'write', 'edit'].includes(part.tool) + if (part.tool === 'task') { + const sessionId = getTaskSessionId(part) + const description = previewText || 'Sub-agent task' + const isRunning = part.state.status === 'running' || part.state.status === 'pending' + const content = ( +
+ {isRunning ? ( + + ) : null} + {description} + sub-agent + {sessionId && } +
+ ) + + if (sessionId) { + return ( + + ) + } + + return ( +
+ {content} +
+ ) + } + if (isTodoTool) { if (part.state.status === 'pending') { return ( @@ -254,10 +297,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal ) : null} {part.tool === 'task' && (() => { - let sessionId: string | undefined = part.metadata?.sessionId as string | undefined - if (!sessionId && part.state.status !== 'pending' && 'metadata' in part.state) { - sessionId = part.state.metadata?.sessionId as string | undefined - } + const sessionId = getTaskSessionId(part) return sessionId ? ( { @@ -372,4 +412,4 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal )} ) -} \ No newline at end of file +}