File size: 7,394 Bytes
b4f7b22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, { useEffect, useRef, useState } from 'react';

/* Left-border accent colors per thought type — no emojis */
const BORDER_COLORS: Record<string, string> = {
    thought:     '#5b8fb9',
    tool_call:   '#d47c3a',
    tool_result: '#4a7c59',
    observation: '#5b8fb9',
    decision:    '#8b6cc1',
    user_action: '#8b6cc1',
    warning:     '#c0392b',
};

interface ActivityEvent {
    type: string;
    message: string;
    state: string;
    timestamp: number | string;
    agent_name?: string;
    thought_type?: string;
    content?: string;
    stage_name?: string;
    summary?: string;
    artifacts?: Array<{ name: string; path: string; description: string }>;
    decisions?: string[];
    warnings?: string[];
    next_stage_name?: string;
    next_stage_preview?: string;
}

interface Props {
    events: ActivityEvent[];
    thinkingData?: { agent_name: string; message: string } | null;
}

/* Noise patterns to suppress from the live log */
const NOISE_PATTERNS = [
    /^Transitioning:/i,
    /^Beginning stage:/i,
    /^Generating approval summary/i,
    /^Waiting for user approval/i,
    /^User approved stage/i,
    /^User rejected stage/i,
    /^Human-in-the-loop mode enabled/i,
    /^LLM backend:/i,
    /^Approved:/i,
    /^AgentIC Compute Engine selected/i,
    /^Compute engine ready/i,
    /^Starting Build Process for/i,
    /^Build started for/i,
    /^Stage \w+ complete.*awaiting approval/i,
    /^\[Orchestrator\] (Beginning|Generating|Waiting|Human-in)/i,
];

/* Filter noise: skip internal transitions, raw file paths, empty messages, known noise */
function isNoise(evt: ActivityEvent): boolean {
    const msg = (evt.content || evt.message || '').trim();
    if (!msg) return true;
    if (/^\/[a-zA-Z0-9/_.\-]+$/.test(msg)) return true;
    if (evt.type === 'transition') return true;
    for (const pattern of NOISE_PATTERNS) {
        if (pattern.test(msg)) return true;
    }
    return false;
}

/* Truncate full file paths to just the filename */
function shortenPaths(text: string): string {
    return text.replace(/\/(?:home|tmp|var|opt)\/[^\s]+\/([^\s/]+)/g, '$1');
}

/* Truncate long messages at ~120 chars */
function truncate(text: string, max = 120): { display: string; isTruncated: boolean } {
    if (text.length <= max) return { display: text, isTruncated: false };
    return { display: text.slice(0, max) + '…', isTruncated: true };
}

export const ActivityFeed: React.FC<Props> = ({ events, thinkingData }) => {
    const ref = useRef<HTMLDivElement>(null);
    const [autoScroll, setAutoScroll] = useState(true);
    const [expanded, setExpanded] = useState<Set<number>>(new Set());
    const lastTop = useRef(0);

    useEffect(() => {
        if (autoScroll && ref.current) {
            ref.current.scrollTop = ref.current.scrollHeight;
        }
    }, [events, autoScroll]);

    const onScroll = () => {
        if (!ref.current) return;
        const el = ref.current;
        const atBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 60;
        if (el.scrollTop < lastTop.current && !atBottom) setAutoScroll(false);
        else if (atBottom) setAutoScroll(true);
        lastTop.current = el.scrollTop;
    };

    const toggle = (i: number) =>
        setExpanded(prev => {
            const s = new Set(prev);
            s.has(i) ? s.delete(i) : s.add(i);
            return s;
        });

    /* Filter + deduplicate consecutive identical messages */
    const filtered = events.filter(e => {
        if (e.type === 'ping' || e.type === 'stream_end' || e.type === 'stage_complete') return false;
        if (isNoise(e)) return false;
        return true;
    });
    const rows: typeof filtered = [];
    for (const evt of filtered) {
        const prev = rows[rows.length - 1];
        if (prev && (prev.content || prev.message) === (evt.content || evt.message)) continue;
        rows.push(evt);
    }

    return (
        <div className="hitl-log">
            <div className="hitl-log-header">
                <span className="hitl-log-title">Live Log</span>
                {!autoScroll && (
                    <button
                        className="hitl-log-scroll"
                        onClick={() => {
                            setAutoScroll(true);
                            ref.current?.scrollTo({ top: ref.current.scrollHeight, behavior: 'smooth' });
                        }}
                    >
                        ↓ Latest
                    </button>
                )}
            </div>
            <div className="hitl-log-body" ref={ref} onScroll={onScroll}>
                {rows.length === 0 ? (
                    <div className="hitl-log-empty">Waiting for agent activity…</div>
                ) : (
                    rows.map((evt, i) => {
                        const type = evt.thought_type || 'thought';
                        const border = BORDER_COLORS[type] || BORDER_COLORS.thought;
                        const raw = shortenPaths(evt.content || evt.message || '');
                        const isExp = expanded.has(i);
                        const { display, isTruncated } = isExp
                            ? { display: raw, isTruncated: false }
                            : truncate(raw);

                        const ts =
                            typeof evt.timestamp === 'string'
                                ? evt.timestamp.split('T')[1]?.substring(0, 8) || evt.timestamp
                                : new Date((evt.timestamp as number) * 1000).toLocaleTimeString('en-US', {
                                      hour12: false,
                                  });
                        const stage = evt.state?.replace(/_/g, ' ') || '';

                        return (
                            <div
                                key={i}
                                className={`hitl-log-row${isTruncated || isExp ? ' hitl-log-row--expandable' : ''}`}
                                style={{ borderLeftColor: border }}
                                onClick={() => (isTruncated || isExp) && toggle(i)}
                            >
                                <span className="hitl-log-ts">{ts}</span>
                                {stage && <span className="hitl-log-badge">{stage}</span>}
                                <span className="hitl-log-msg">
                                    {display}
                                    {isTruncated && <span className="hitl-log-expand"></span>}
                                </span>
                            </div>
                        );
                    })
                )}
                {/* Thinking indicator */}
                {thinkingData && (
                    <div className="hitl-log-row hitl-log-row--thinking" style={{ borderLeftColor: '#d47c3a' }}>
                        <span className="hitl-thinking-pulse" />
                        <span className="hitl-log-badge" style={{ fontStyle: 'italic' }}>
                            {thinkingData.agent_name || 'Agent'}
                        </span>
                        <span className="hitl-log-msg" style={{ fontStyle: 'italic', color: 'var(--text-secondary, #6b6560)' }}>
                            {thinkingData.message}
                        </span>
                    </div>
                )}
            </div>
        </div>
    );
};