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
|
import React, { useState, useRef, useEffect, useCallback } from 'react';
import {
sendContractMessage,
interruptContract,
getContractOutput,
} from '../../../services/directiveApi';
import './ContractLogFeed.css';
interface ContractLogFeedProps {
taskId: string;
contractName: string;
status: string;
onClose?: () => void;
}
interface LogEntry {
id: string;
text: string;
type: 'output' | 'user' | 'system';
timestamp: Date;
}
const INTERACTIVE_STATUSES = ['running', 'starting'];
export function ContractLogFeed({ taskId, contractName, status, onClose }: ContractLogFeedProps) {
const [logEntries, setLogEntries] = useState<LogEntry[]>([]);
const [message, setMessage] = useState('');
const [sending, setSending] = useState(false);
const [sentIndicator, setSentIndicator] = useState(false);
const [interruptConfirm, setInterruptConfirm] = useState(false);
const [interrupting, setInterrupting] = useState(false);
const [error, setError] = useState<string | null>(null);
const logEndRef = useRef<HTMLDivElement>(null);
const textareaRef = useRef<HTMLTextAreaElement>(null);
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
const lastOutputRef = useRef<string>('');
const entryIdRef = useRef(0);
const isInteractive = INTERACTIVE_STATUSES.includes(status.toLowerCase());
const addLogEntry = useCallback((text: string, type: LogEntry['type']) => {
entryIdRef.current += 1;
setLogEntries(prev => [
...prev,
{ id: `entry-${entryIdRef.current}`, text, type, timestamp: new Date() },
]);
}, []);
// Poll for contract output
const fetchOutput = useCallback(async () => {
if (!taskId) return;
try {
const data = await getContractOutput(taskId);
const output = data.output || '';
if (output && output !== lastOutputRef.current) {
// Find new content
const newContent = output.startsWith(lastOutputRef.current)
? output.slice(lastOutputRef.current.length).trim()
: output.trim();
lastOutputRef.current = output;
if (newContent) {
addLogEntry(newContent, 'output');
}
}
} catch {
// Silently ignore fetch errors for output polling
}
}, [taskId, addLogEntry]);
useEffect(() => {
fetchOutput();
pollRef.current = setInterval(fetchOutput, 3000);
return () => {
if (pollRef.current) clearInterval(pollRef.current);
};
}, [fetchOutput]);
// Auto-scroll to bottom on new entries
useEffect(() => {
logEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [logEntries]);
// Reset interrupt confirm after timeout
useEffect(() => {
if (!interruptConfirm) return;
const timer = setTimeout(() => setInterruptConfirm(false), 3000);
return () => clearTimeout(timer);
}, [interruptConfirm]);
const handleSendMessage = async () => {
const trimmed = message.trim();
if (!trimmed || sending || !isInteractive) return;
setSending(true);
setError(null);
try {
await sendContractMessage(taskId, trimmed);
addLogEntry(trimmed, 'user');
setMessage('');
setSentIndicator(true);
setTimeout(() => setSentIndicator(false), 1500);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to send message');
} finally {
setSending(false);
}
};
const handleInterrupt = async () => {
if (!interruptConfirm) {
setInterruptConfirm(true);
return;
}
setInterrupting(true);
setError(null);
setInterruptConfirm(false);
try {
await interruptContract(taskId);
addLogEntry('Contract interrupted by user', 'system');
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to interrupt contract');
} finally {
setInterrupting(false);
}
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSendMessage();
}
};
const statusLower = status.toLowerCase();
return (
<div className="contract-log-feed">
<div className="contract-log-feed-header">
<div className="contract-log-feed-title">
<span className="contract-log-feed-name">{contractName}</span>
<span className={`contract-log-feed-status contract-log-feed-status--${statusLower}`}>
{status}
</span>
</div>
{onClose && (
<button className="contract-log-feed-close" onClick={onClose} title="Close">
×
</button>
)}
</div>
<div className="contract-log-feed-content">
{logEntries.length === 0 && (
<div className="contract-log-feed-empty">
{isInteractive ? 'Waiting for output...' : 'No output available.'}
</div>
)}
{logEntries.map(entry => (
<div key={entry.id} className={`contract-log-entry contract-log-entry--${entry.type}`}>
<span className="contract-log-entry-time">
{entry.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' })}
</span>
<span className="contract-log-entry-text">{entry.text}</span>
</div>
))}
<div ref={logEndRef} />
</div>
{error && (
<div className="contract-log-feed-error">{error}</div>
)}
{isInteractive && (
<div className="contract-interaction-bar">
<div className="contract-interaction-message-row">
<textarea
ref={textareaRef}
className="contract-message-input"
value={message}
onChange={e => setMessage(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Send a message to the contract... (Enter to send, Shift+Enter for newline)"
rows={1}
disabled={sending}
/>
<button
className="contract-send-btn"
onClick={handleSendMessage}
disabled={sending || !message.trim()}
title="Send message"
>
{sending ? 'Sending...' : 'Send'}
</button>
{sentIndicator && (
<span className="contract-sent-indicator">Sent</span>
)}
</div>
<div className="contract-interaction-actions-row">
<button
className={`contract-interrupt-btn ${interruptConfirm ? 'contract-interrupt-btn--confirm' : ''}`}
onClick={handleInterrupt}
disabled={interrupting}
title={interruptConfirm ? 'Click again to confirm interrupt' : 'Interrupt contract'}
>
{interrupting
? 'Interrupting...'
: interruptConfirm
? 'Click again to confirm interrupt'
: 'Interrupt'}
</button>
</div>
</div>
)}
{!isInteractive && statusLower !== 'pending' && statusLower !== 'ready' && (
<div className="contract-interaction-bar contract-interaction-bar--disabled">
<span className="contract-interaction-disabled-text">
Contract is {status.toLowerCase()} - interaction unavailable
</span>
</div>
)}
</div>
);
}
|