import { Ansi, Box, NoSelect, Text } from '@hermes/ink' import { memo } from 'react' import { LONG_MSG } from '../config/limits.js' import { userDisplay } from '../domain/messages.js' import { ROLE } from '../domain/roles.js' import { compactPreview, hasAnsi, isPasteBackedText, stripAnsi } from '../lib/text.js' import type { Theme } from '../theme.js' import type { DetailsMode, Msg } from '../types.js' import { Md } from './markdown.js' import { ToolTrail } from './thinking.js' export const MessageLine = memo(function MessageLine({ cols, compact, detailsMode = 'collapsed', isStreaming = false, msg, t }: MessageLineProps) { if (msg.kind === 'trail' && msg.tools?.length) { return detailsMode === 'hidden' ? null : ( ) } if (msg.role === 'tool') { return ( {compactPreview(hasAnsi(msg.text) ? stripAnsi(msg.text) : msg.text, Math.max(24, cols - 14)) || '(empty tool result)'} ) } const { body, glyph, prefix } = ROLE[msg.role](t) const thinking = msg.thinking?.trim() ?? '' const showDetails = detailsMode !== 'hidden' && (Boolean(msg.tools?.length) || Boolean(thinking)) const content = (() => { if (msg.kind === 'slash') { return {msg.text} } if (msg.role !== 'user' && hasAnsi(msg.text)) { return {msg.text} } if (msg.role === 'assistant') { return isStreaming ? {msg.text} : } if (msg.role === 'user' && msg.text.length > LONG_MSG && isPasteBackedText(msg.text)) { const [head, ...rest] = userDisplay(msg.text).split('[long message]') return ( {head} [long message] {rest.join('')} ) } return {msg.text} })() return ( {showDetails && ( )} {glyph}{' '} {content} ) }) interface MessageLineProps { cols: number compact?: boolean detailsMode?: DetailsMode isStreaming?: boolean msg: Msg t: Theme }