UNPKG

terminal-chat-ui

Version:

Shared UI components for terminal-based chat interfaces using Theater actors

78 lines (77 loc) 3.21 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /** * Simplified MessageComponent - no more pending state handling */ import { Box, Text } from 'ink'; /** * Simplified message component - all messages are complete when rendered */ export function MessageComponent({ message, toolDisplayMode, variant = 'default', prefixOverrides = {}, contentColor, showTimestamp = false }) { const { role, content, toolName, toolArgs } = message; // Don't show hidden tool messages if (role === 'tool' && toolDisplayMode === 'hidden') { return null; } // Skip empty system messages if (role === 'system' && !content.trim()) { return null; } // Default prefixes based on variant const getDefaultPrefixes = (variant) => { const base = { user: 'You: ', assistant: 'Assistant: ', system: '[system] ', tool: '[tool] ', error: '[error] ' }; // Customize based on variant switch (variant) { case 'git': return { ...base, system: '[git] ', error: '[git] ' }; case 'chat': return { ...base, assistant: 'Chat: ' }; default: return base; } }; const defaultPrefixes = getDefaultPrefixes(variant); const prefixes = { ...defaultPrefixes, ...prefixOverrides }; // Default colors based on role const getContentColor = (role) => { if (contentColor) return contentColor; return { user: 'gray', assistant: 'white', system: 'gray', tool: 'magenta', error: 'red' }[role] || 'white'; }; const roleColor = getContentColor(role); // Handle tool messages specially if (role === 'tool') { return _jsx(ToolMessage, { toolName: toolName || 'unknown', toolArgs: toolArgs || [], toolDisplayMode: toolDisplayMode, prefix: prefixes.tool }); } // Skip empty assistant messages (when only tools were used) if (!content.trim()) { return null; } // Handle regular messages - all are complete, no pending state const trimmed_content = content.trim(); const lines = trimmed_content.split('\n'); const hasMultipleLines = lines.length > 1; const prefix = prefixes[role] || ''; return (_jsx(Box, { flexDirection: "column", marginBottom: 1, children: lines.map((line, index) => (_jsxs(Text, { color: roleColor, children: [index === 0 ? prefix : '', line || ' '] }, index))) })); } function ToolMessage({ toolName, toolArgs, toolDisplayMode, prefix }) { const args = toolArgs.join(' '); if (toolDisplayMode === 'minimal') { return (_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { color: "magenta", dimColor: true, children: [prefix, toolName, args ? `: ${args}` : ''] }) })); } else if (toolDisplayMode === 'full') { return (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Text, { color: "magenta", children: [prefix, toolName] }), args && (_jsxs(Text, { color: "magenta", dimColor: true, children: ["Args: ", args] }))] })); } return null; // hidden }