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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
import React, { useEffect, useRef, useState, useCallback } from 'react';
interface StepLogFeedProps {
taskId: string;
stepName: string;
stepStatus: string;
onCollapse: () => void;
}
interface LogEntry {
timestamp: string;
content: string;
type: 'stdout' | 'stderr' | 'system' | 'user';
}
/**
* Live log feed for an expanded step row.
* Connects via WebSocket to stream task output and allows
* sending messages (comments) and interrupting the task.
*/
export function StepLogFeed({ taskId, stepName, stepStatus, onCollapse }: StepLogFeedProps) {
const [logs, setLogs] = useState<LogEntry[]>([]);
const [message, setMessage] = useState('');
const [sending, setSending] = useState(false);
const [connected, setConnected] = useState(false);
const [error, setError] = useState<string | null>(null);
const logsEndRef = useRef<HTMLDivElement>(null);
const wsRef = useRef<WebSocket | null>(null);
const logContainerRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const isActive = ['running', 'starting'].includes(stepStatus.toLowerCase());
// Auto-scroll to bottom when new logs arrive
useEffect(() => {
logsEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [logs]);
// Connect to WebSocket for live streaming
useEffect(() => {
if (!taskId) return;
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${window.location.host}/api/v1/mesh/tasks/subscribe`;
let ws: WebSocket;
let reconnectTimer: ReturnType<typeof setTimeout>;
let shouldReconnect = true;
function connect() {
try {
ws = new WebSocket(wsUrl);
wsRef.current = ws;
ws.addEventListener('open', () => {
setConnected(true);
setError(null);
// Subscribe to this specific task
ws.send(JSON.stringify({ type: 'subscribe', taskId }));
});
ws.addEventListener('message', (evt) => {
try {
const data = JSON.parse(evt.data);
// Handle different message formats from the backend
if (data.taskId === taskId || data.task_id === taskId) {
const entry: LogEntry = {
timestamp: data.timestamp || new Date().toISOString(),
content: data.content || data.output || data.message || JSON.stringify(data),
type: data.type || data.stream || 'stdout',
};
setLogs(prev => [...prev, entry]);
}
} catch {
// Non-JSON message, treat as raw log
setLogs(prev => [...prev, {
timestamp: new Date().toISOString(),
content: evt.data,
type: 'stdout',
}]);
}
});
ws.addEventListener('close', () => {
setConnected(false);
wsRef.current = null;
if (shouldReconnect && isActive) {
reconnectTimer = setTimeout(connect, 3000);
}
});
ws.addEventListener('error', () => {
setConnected(false);
setError('WebSocket connection failed');
});
} catch (err) {
setError('Failed to connect to log stream');
}
}
connect();
return () => {
shouldReconnect = false;
clearTimeout(reconnectTimer);
if (wsRef.current) {
wsRef.current.close();
wsRef.current = null;
}
};
}, [taskId, isActive]);
// Keyboard shortcut: Escape to collapse
useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
onCollapse();
}
};
document.addEventListener('keydown', handler);
return () => document.removeEventListener('keydown', handler);
}, [onCollapse]);
// Send a message/comment to the task
const handleSendMessage = useCallback(async () => {
if (!message.trim() || !taskId || sending) return;
setSending(true);
try {
const response = await fetch(`/api/v1/mesh/tasks/${taskId}/message`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: message.trim() }),
});
if (!response.ok) {
const body = await response.json().catch(() => ({ message: response.statusText }));
throw new Error(body.message || body.error || `HTTP ${response.status}`);
}
// Add as a user message in the log
setLogs(prev => [...prev, {
timestamp: new Date().toISOString(),
content: message.trim(),
type: 'user',
}]);
setMessage('');
inputRef.current?.focus();
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to send message');
} finally {
setSending(false);
}
}, [message, taskId, sending]);
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSendMessage();
}
// Prevent Escape from bubbling when input is focused
if (e.key === 'Escape') {
e.stopPropagation();
inputRef.current?.blur();
}
}, [handleSendMessage]);
// Interrupt the running task
const handleInterrupt = useCallback(async () => {
if (!taskId) return;
try {
// Send a special interrupt message
const response = await fetch(`/api/v1/mesh/tasks/${taskId}/message`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: '/interrupt' }),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
setLogs(prev => [...prev, {
timestamp: new Date().toISOString(),
content: 'Interrupt signal sent',
type: 'system',
}]);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to interrupt');
}
}, [taskId]);
const formatTimestamp = (ts: string) => {
try {
return new Date(ts).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' });
} catch {
return '';
}
};
return (
<div className="step-log-feed">
{/* Header */}
<div className="step-log-feed-header">
<div className="step-log-feed-header-left">
<span className="step-log-feed-title">{stepName} - Logs</span>
<span className={`step-log-feed-status ${connected ? 'connected' : 'disconnected'}`}>
{connected ? 'Live' : 'Disconnected'}
</span>
</div>
<div className="step-log-feed-header-right">
{isActive && (
<button
className="step-log-feed-interrupt-btn"
onClick={handleInterrupt}
title="Interrupt this contract"
>
⏹ Interrupt
</button>
)}
<button
className="step-log-feed-collapse-btn"
onClick={onCollapse}
title="Collapse (Esc)"
>
✕
</button>
</div>
</div>
{/* Log content */}
<div className="step-log-feed-content" ref={logContainerRef}>
{logs.length === 0 && !error && (
<div className="step-log-feed-empty">
{isActive
? 'Waiting for log output...'
: 'No logs available for this step.'}
</div>
)}
{error && (
<div className="step-log-feed-error">{error}</div>
)}
{logs.map((entry, idx) => (
<div key={idx} className={`step-log-entry step-log-entry--${entry.type}`}>
<span className="step-log-entry-time">{formatTimestamp(entry.timestamp)}</span>
<span className="step-log-entry-content">{entry.content}</span>
</div>
))}
<div ref={logsEndRef} />
</div>
{/* Message input (comment/interrupt controls) */}
{isActive && (
<div className="step-log-feed-input">
<input
ref={inputRef}
type="text"
className="step-log-feed-input-field"
placeholder="Send a message to this contract..."
value={message}
onChange={(e) => setMessage(e.target.value)}
onKeyDown={handleKeyDown}
disabled={sending}
/>
<button
className="step-log-feed-send-btn"
onClick={handleSendMessage}
disabled={!message.trim() || sending}
title="Send message (Enter)"
>
{sending ? '...' : '➤'}
</button>
</div>
)}
</div>
);
}
|