-
Notifications
You must be signed in to change notification settings - Fork 502
Expand file tree
/
Copy pathchatroom-viewer.tsx
More file actions
284 lines (248 loc) · 9.88 KB
/
chatroom-viewer.tsx
File metadata and controls
284 lines (248 loc) · 9.88 KB
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
278
279
280
281
282
283
284
#!/usr/bin/env node
/**
* Chatroom Viewer — Real-time TUI for watching and posting to team chat rooms.
*
* Polls the API server for new chat messages and displays them in a
* scrolling log. Supports type-to-send for posting messages.
*
* Usage: node dist/visualizer/chatroom-viewer.js --team <id> [--port <num>]
*/
import React, { useState, useEffect, useCallback } from 'react';
import { render, Box, Text, useApp, useInput } from 'ink';
import http from 'http';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { fileURLToPath } from 'url';
// ─── Paths ──────────────────────────────────────────────────────────────────
const __filename_ = fileURLToPath(import.meta.url);
const __dirname_ = path.dirname(__filename_);
const _localTinyclaw = path.join(__dirname_, '..', '..', '.tinyclaw');
const TINYCLAW_HOME = fs.existsSync(path.join(_localTinyclaw, 'settings.json'))
? _localTinyclaw
: path.join(os.homedir(), '.tinyclaw');
const SETTINGS_FILE = path.join(TINYCLAW_HOME, 'settings.json');
// ─── Types ──────────────────────────────────────────────────────────────────
interface TeamConfig {
name: string;
agents: string[];
leader_agent: string;
}
interface ChatMessage {
id: number;
team_id: string;
from_agent: string;
message: string;
created_at: number;
}
// ─── Settings loader ────────────────────────────────────────────────────────
function loadSettings(): { teams: Record<string, TeamConfig> } {
try {
const raw = fs.readFileSync(SETTINGS_FILE, 'utf8');
const settings = JSON.parse(raw);
return { teams: settings.teams || {} };
} catch {
return { teams: {} };
}
}
// ─── Helpers ────────────────────────────────────────────────────────────────
function formatTime(ts: number): string {
return new Date(ts).toLocaleTimeString('en-US', {
hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit',
});
}
// ─── HTTP helpers ───────────────────────────────────────────────────────────
function fetchJson(url: string): Promise<unknown> {
return new Promise((resolve, reject) => {
http.get(url, (res) => {
if (res.statusCode !== 200) {
res.resume();
reject(new Error(`HTTP ${res.statusCode}`));
return;
}
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk: string) => { data += chunk; });
res.on('end', () => {
try { resolve(JSON.parse(data)); }
catch (e) { reject(e); }
});
}).on('error', reject);
});
}
function postJson(url: string, body: unknown): Promise<unknown> {
return new Promise((resolve, reject) => {
const payload = JSON.stringify(body);
const urlObj = new URL(url);
const req = http.request({
hostname: urlObj.hostname,
port: urlObj.port,
path: urlObj.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload),
},
}, (res) => {
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk: string) => { data += chunk; });
res.on('end', () => {
try { resolve(JSON.parse(data)); }
catch (e) { reject(e); }
});
});
req.on('error', reject);
req.write(payload);
req.end();
});
}
// ─── Components ─────────────────────────────────────────────────────────────
const MAX_VISIBLE_MESSAGES = 50;
function MessageLine({ msg }: { msg: ChatMessage }) {
const isUser = msg.from_agent === 'user';
const nameColor = isUser ? 'green' : 'cyan';
const label = isUser ? 'you' : `@${msg.from_agent}`;
return (
<Box flexDirection="column" marginBottom={0}>
<Box>
<Text color="gray">[{formatTime(msg.created_at)}] </Text>
<Text color={nameColor} bold>{label}</Text>
</Box>
<Box marginLeft={2}>
<Text wrap="wrap">{msg.message}</Text>
</Box>
</Box>
);
}
function App({ teamId, apiPort }: { teamId: string; apiPort: number }) {
const { exit } = useApp();
const [messages, setMessages] = useState<ChatMessage[]>([]);
const [input, setInput] = useState('');
const [teamName, setTeamName] = useState('');
const [error, setError] = useState('');
const [connected, setConnected] = useState(false);
const lastIdRef = React.useRef(0);
// Load team name from settings
useEffect(() => {
const { teams } = loadSettings();
const team = teams[teamId];
if (team) {
setTeamName(team.name);
} else {
setError(`Team '${teamId}' not found in settings`);
}
}, [teamId]);
// Poll for new messages
const poll = useCallback(async () => {
try {
const url = `http://localhost:${apiPort}/api/chatroom/${teamId}?since=${lastIdRef.current}&limit=100`;
const data = await fetchJson(url) as ChatMessage[];
if (Array.isArray(data) && data.length > 0) {
lastIdRef.current = data[data.length - 1].id;
setMessages(prev => {
const combined = [...prev, ...data];
return combined.slice(-MAX_VISIBLE_MESSAGES);
});
}
setConnected(true);
setError('');
} catch {
setConnected(false);
}
}, [teamId, apiPort]);
useEffect(() => {
// Initial fetch — get recent history
poll();
const timer = setInterval(poll, 1000);
return () => clearInterval(timer);
}, [poll]);
// Send message
const sendMessage = useCallback(async (text: string) => {
try {
await postJson(`http://localhost:${apiPort}/api/chatroom/${teamId}`, { message: text });
} catch {
setError('Failed to send message');
}
}, [teamId, apiPort]);
// Keyboard input
useInput((ch, key) => {
if (key.return && input.trim()) {
const text = input.trim();
setInput('');
sendMessage(text);
return;
}
if (key.backspace || key.delete) {
setInput(prev => prev.slice(0, -1));
return;
}
// Quit on Ctrl+C or q when input is empty
if (key.escape || (ch === 'q' && input === '')) {
exit();
return;
}
// Regular character input
if (ch && !key.ctrl && !key.meta) {
setInput(prev => prev + ch);
}
});
const title = teamName ? `${teamName} (#${teamId})` : `#${teamId}`;
return (
<Box flexDirection="column" width="100%">
{/* Header */}
<Box borderStyle="single" borderBottom={false} borderLeft={false} borderRight={false} paddingX={1}>
<Text bold color="cyan">Chat Room: {title}</Text>
<Text> </Text>
{connected
? <Text color="green">{'\u25CF'} connected</Text>
: <Text color="red">{'\u25CB'} disconnected</Text>
}
</Box>
{/* Messages */}
<Box flexDirection="column" paddingX={1} flexGrow={1}>
{messages.length === 0 && !error && (
<Text color="gray" italic>No messages yet. Type below to start the conversation.</Text>
)}
{messages.map((msg) => (
<MessageLine key={msg.id} msg={msg} />
))}
</Box>
{/* Error */}
{error && (
<Box paddingX={1}>
<Text color="red">{error}</Text>
</Box>
)}
{/* Input */}
<Box borderStyle="single" borderTop={true} borderBottom={false} borderLeft={false} borderRight={false} paddingX={1}>
<Text color="green" bold>{'> '}</Text>
<Text>{input}</Text>
<Text color="gray">{'\u2588'}</Text>
</Box>
{/* Help */}
<Box paddingX={1}>
<Text color="gray" dimColor>Enter to send | q to quit | Esc to exit</Text>
</Box>
</Box>
);
}
// ─── Entry point ────────────────────────────────────────────────────────────
const args = process.argv.slice(2);
let teamId: string | null = null;
let apiPort = parseInt(process.env.TINYCLAW_API_PORT || '3777', 10);
for (let i = 0; i < args.length; i++) {
if ((args[i] === '--team' || args[i] === '-t') && args[i + 1]) {
teamId = args[i + 1];
i++;
}
if ((args[i] === '--port' || args[i] === '-p') && args[i + 1]) {
apiPort = parseInt(args[i + 1], 10);
i++;
}
}
if (!teamId) {
console.error('Usage: chatroom-viewer --team <team_id> [--port <num>]');
process.exit(1);
}
render(<App teamId={teamId!} apiPort={apiPort} />);