|
| 1 | +import React, { useState, useCallback, useEffect } from 'react'; |
| 2 | +import dedent from 'dedent'; |
| 3 | +import { marked } from 'marked'; |
| 4 | +import { markedTerminal } from 'marked-terminal'; |
| 5 | + |
| 6 | +marked.use(markedTerminal()); |
| 7 | + |
| 8 | +import { Box, Text } from 'ink'; |
| 9 | +import type { TaggedLogEntry, LogType } from '../utils/logger.js'; |
| 10 | +import { |
| 11 | + registerLogPane, |
| 12 | + setVerboseMode, |
| 13 | + unregisterLogPane, |
| 14 | +} from '../utils/logger.js'; |
| 15 | + |
| 16 | +// marked.use(new markedTerminal()); |
| 17 | + |
| 18 | +type LogEntry = TaggedLogEntry; |
| 19 | + |
| 20 | +interface LogPaneProps { |
| 21 | + verboseMode: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +const LogPane: React.FC<LogPaneProps> = ({ verboseMode }) => { |
| 25 | + const [logs, setLogs] = useState<TaggedLogEntry[]>([]); |
| 26 | + |
| 27 | + const addLog = useCallback((logEntry: TaggedLogEntry) => { |
| 28 | + setLogs((prevLogs: TaggedLogEntry[]) => { |
| 29 | + // Skip duplicate consecutive logs |
| 30 | + if (prevLogs.length === 0) return [logEntry]; |
| 31 | + |
| 32 | + const lastLog = prevLogs[prevLogs.length - 1]; |
| 33 | + if ( |
| 34 | + lastLog.type === logEntry.type && |
| 35 | + lastLog.content === logEntry.content && |
| 36 | + // Check if it's within 1 second to avoid legitimate duplicates |
| 37 | + Math.abs( |
| 38 | + (lastLog.timestamp?.getTime() || 0) - |
| 39 | + (logEntry.timestamp?.getTime() || 0) |
| 40 | + ) < 1000 |
| 41 | + ) { |
| 42 | + return prevLogs; |
| 43 | + } |
| 44 | + |
| 45 | + return [...prevLogs, logEntry]; |
| 46 | + }); |
| 47 | + }, []); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + registerLogPane(addLog); |
| 51 | + |
| 52 | + return () => { |
| 53 | + unregisterLogPane(addLog); |
| 54 | + }; |
| 55 | + }, []); // Empty dependency array to ensure this only runs once |
| 56 | + const getLogStyles = (type: LogType) => { |
| 57 | + switch (type) { |
| 58 | + case 'success': |
| 59 | + return { color: 'green' as const }; |
| 60 | + case 'error': |
| 61 | + return { color: 'red' as const }; |
| 62 | + case 'warning': |
| 63 | + return { color: 'yellow' as const }; |
| 64 | + case 'debug': |
| 65 | + return { color: 'gray' as const, dimColor: true }; |
| 66 | + case 'substep': |
| 67 | + return { color: 'gray' as const, dimColor: true }; |
| 68 | + case 'step': |
| 69 | + return { color: 'cyan' as const, dimColor: true }; |
| 70 | + case 'multiline': |
| 71 | + return { color: 'gray' as const, dimColor: true }; |
| 72 | + default: |
| 73 | + return {}; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + const processLogContent = (content: string): string[] => { |
| 78 | + return content.split('\n').filter((line) => line.length > 0); |
| 79 | + }; |
| 80 | + |
| 81 | + const renderLogEntry = (log: TaggedLogEntry, index: number) => { |
| 82 | + // Skip debug logs when not in verbose mode AND DEBUG env var is not set |
| 83 | + const shouldShowDebug = |
| 84 | + verboseMode || Boolean(process.env.DEBUG?.includes('explorbot:')); |
| 85 | + if (log.type === 'debug' && !shouldShowDebug) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + const styles = getLogStyles(log.type); |
| 89 | + |
| 90 | + if (log.type === 'multiline') { |
| 91 | + return ( |
| 92 | + <Box |
| 93 | + key={index} |
| 94 | + borderStyle="classic" |
| 95 | + marginY={1} |
| 96 | + borderColor="dim" |
| 97 | + height={15} |
| 98 | + overflow="hidden" |
| 99 | + > |
| 100 | + <Text>{dedent(marked.parse(String(log.content)).toString())}</Text> |
| 101 | + </Box> |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + const lines = processLogContent(String(log.content)); |
| 106 | + |
| 107 | + if (log.type === 'substep') { |
| 108 | + return ( |
| 109 | + <Box key={index} flexDirection="column"> |
| 110 | + {lines.map((line, lineIndex) => ( |
| 111 | + <Text key={`${index}-${lineIndex}`} {...styles}> |
| 112 | + {lineIndex === 0 ? `> ${line}` : ` ${line}`} |
| 113 | + </Text> |
| 114 | + ))} |
| 115 | + </Box> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + if (log.type === 'step') { |
| 120 | + return ( |
| 121 | + <Box key={index} flexDirection="column" paddingLeft={2}> |
| 122 | + {lines.map((line, lineIndex) => ( |
| 123 | + <Text key={`${index}-${lineIndex}`} {...styles}> |
| 124 | + {line} |
| 125 | + </Text> |
| 126 | + ))} |
| 127 | + </Box> |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return ( |
| 132 | + <Box key={index} flexDirection="column"> |
| 133 | + {lines.map((line, lineIndex) => ( |
| 134 | + <Text key={`${index}-${lineIndex}`} {...styles}> |
| 135 | + {line} |
| 136 | + </Text> |
| 137 | + ))} |
| 138 | + </Box> |
| 139 | + ); |
| 140 | + }; |
| 141 | + |
| 142 | + return ( |
| 143 | + <Box flexDirection="column"> |
| 144 | + {logs.map((log, index) => renderLogEntry(log, index)).filter(Boolean)} |
| 145 | + </Box> |
| 146 | + ); |
| 147 | +}; |
| 148 | + |
| 149 | +export default LogPane; |
0 commit comments