File size: 9,900 Bytes
4e909c7 |
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 285 286 287 288 289 290 291 292 293 294 295 296 297 |
import { useEffect, useRef, useState, useCallback } from 'react';
import type { LogMessage, SystemEvent } from '../types';
import { getWebSocketBaseUrl } from '../config';
const WS_BASE_URL = getWebSocketBaseUrl();
interface UseWebSocketOptions {
onMessage?: (message: any) => void;
onError?: (error: Event) => void;
reconnectInterval?: number;
maxReconnectAttempts?: number;
}
export const useWebSocket = (url: string, options: UseWebSocketOptions = {}) => {
const {
onMessage,
onError,
reconnectInterval = 5000,
maxReconnectAttempts = 10,
} = options;
const [isConnected, setIsConnected] = useState(false);
const [error, setError] = useState<string | null>(null);
const wsRef = useRef<WebSocket | null>(null);
const reconnectAttemptsRef = useRef(0);
const reconnectTimeoutRef = useRef<number | undefined>();
const isConnectingRef = useRef(false);
const backoffRef = useRef(reconnectInterval);
// Keep latest handlers in refs to avoid reconnects on render
const onMessageRef = useRef<typeof onMessage>();
const onErrorRef = useRef<typeof onError>();
useEffect(() => {
onMessageRef.current = onMessage;
}, [onMessage]);
useEffect(() => {
onErrorRef.current = onError;
}, [onError]);
const connect = useCallback(() => {
if (wsRef.current || isConnectingRef.current) return;
try {
isConnectingRef.current = true;
const fullUrl = url.startsWith('ws') ? url : `${WS_BASE_URL}${url}`;
console.log(`[WS] connecting ${fullUrl}`);
const ws = new WebSocket(fullUrl);
ws.onopen = () => {
console.log('[WS] open');
setIsConnected(true);
setError(null);
reconnectAttemptsRef.current = 0;
backoffRef.current = reconnectInterval;
isConnectingRef.current = false;
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
onMessageRef.current?.(data);
} catch (err) {
console.error('Failed to parse WebSocket message:', err);
}
};
ws.onerror = (event) => {
console.warn('[WS] error');
setError('WebSocket error occurred');
onErrorRef.current?.(event);
};
ws.onclose = () => {
console.log('[WS] close');
setIsConnected(false);
wsRef.current = null;
isConnectingRef.current = false;
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current);
}
if (reconnectAttemptsRef.current < maxReconnectAttempts) {
reconnectAttemptsRef.current++;
const delay = Math.min(backoffRef.current, 30000);
console.log(`[WS] reconnect in ${delay}ms`);
reconnectTimeoutRef.current = window.setTimeout(() => {
backoffRef.current = Math.min(backoffRef.current * 2, 30000);
connect();
}, delay);
} else {
setError('Max reconnection attempts reached');
}
};
wsRef.current = ws;
} catch (err) {
setError('Failed to create WebSocket connection');
console.error('WebSocket connection error:', err);
isConnectingRef.current = false;
}
}, [url, reconnectInterval, maxReconnectAttempts]);
useEffect(() => {
connect();
return () => {
if (reconnectTimeoutRef.current) clearTimeout(reconnectTimeoutRef.current);
if (wsRef.current) {
try {
console.log('[WS] cleanup close');
wsRef.current.close();
} catch {}
}
wsRef.current = null;
isConnectingRef.current = false;
};
}, [connect]);
const send = useCallback((data: any) => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
wsRef.current.send(JSON.stringify(data));
}
}, []);
return { isConnected, error, send };
};
export const useAgentLogs = (agentName: string) => {
const [logs, setLogs] = useState<LogMessage[]>([]);
const { isConnected, error } = useWebSocket(`/ws/logs/${agentName}`, {
onMessage: (message: any) => {
const t = message?.timestamp;
const kind = message?.type;
if (kind === 'connected' || kind === 'status') return;
let tsNum: number = typeof t === 'number' ? t : Date.now();
if (tsNum < 1e12) tsNum = Math.floor(tsNum * 1000);
const level: LogMessage['level'] = String(message?.level || 'INFO').toUpperCase() as any;
const msgText: string = typeof message?.message === 'string' ? message.message : JSON.stringify(message);
const log: LogMessage = {
timestamp: new Date(tsNum).toISOString(),
level,
agent_name: agentName,
message: msgText,
};
setLogs((prev) => [...prev, log].slice(-100));
},
});
const clearLogs = useCallback(() => {
setLogs([]);
}, []);
return { logs, isConnected, error, clearLogs };
};
export const useSystemEvents = () => {
const [events, setEvents] = useState<SystemEvent[]>([]);
const { isConnected, error } = useWebSocket('/ws/events', {
onMessage: (event: any) => {
const t = event?.timestamp;
const typ = event?.event_type || event?.type;
if (typ === 'system_status' || typ === 'connected') return;
let tsNum: number = typeof t === 'number' ? t : Date.now();
if (tsNum < 1e12) tsNum = Math.floor(tsNum * 1000);
const normalized: SystemEvent = {
event_type: (typ || 'event') as any,
timestamp: new Date(tsNum).toISOString(),
data: (event?.data ?? event) as any,
};
setEvents((prev) => [normalized, ...prev].slice(0, 50));
},
});
const clearEvents = useCallback(() => {
setEvents([]);
}, []);
return { events, isConnected, error, clearEvents };
};
export const usePromptTraces = (promptId: string | null) => {
const [traces, setTraces] = useState<any[]>([]);
const [isComplete, setIsComplete] = useState(false);
const [status, setStatus] = useState<string | null>(null);
// Clear traces when promptId changes to prevent mixing traces from different prompts
useEffect(() => {
setTraces([]);
setIsComplete(false);
setStatus(null);
}, [promptId]);
const { isConnected, error } = useWebSocket(
promptId ? `/ws/prompts/${promptId}` : '',
{
onMessage: (message: any) => {
if (message.type === 'trace' && message.data) {
setTraces((prev) => [...prev, message.data]);
} else if (message.type === 'complete' && message.data) {
setIsComplete(true);
setStatus(message.data.status);
} else if (message.type === 'error') {
setIsComplete(true);
setStatus('error');
}
},
}
);
const clearTraces = useCallback(() => {
setTraces([]);
setIsComplete(false);
setStatus(null);
}, []);
return { traces, isConnected, isComplete, status, error, clearTraces };
};
export const usePlaygroundTraces = (playgroundId: string | null) => {
const [traces, setTraces] = useState<any[]>([]);
const [isComplete, setIsComplete] = useState(false);
const [status, setStatus] = useState<string | null>(null);
// Clear traces when playgroundId changes to prevent mixing traces from different runs
useEffect(() => {
setTraces([]);
setIsComplete(false);
setStatus(null);
}, [playgroundId]);
const { isConnected, error } = useWebSocket(
playgroundId ? `/ws/prompts/${playgroundId}` : '',
{
onMessage: (message: any) => {
if (message.type === 'traces' && message.data) {
// New hierarchical structure: data contains {spans: [...], count: n}
if (message.data.spans && Array.isArray(message.data.spans)) {
// Backend sends incremental updates with new spans
// We need to merge them with existing spans
setTraces((prev) => {
const existingSpans = prev[0]?.spans || [];
const newSpans = message.data.spans;
// Create a map of existing spans by ID for quick lookup
const spanMap = new Map();
// Helper to add spans to map recursively
const addToMap = (spans: any[]) => {
spans.forEach(span => {
spanMap.set(span.id, span);
if (span.children && span.children.length > 0) {
addToMap(span.children);
}
});
};
// Add existing spans to map
addToMap(existingSpans);
// Add/update with new spans (this handles updates to existing spans too)
addToMap(newSpans);
// For simplicity, just append new unique spans
const existingIds = new Set(existingSpans.map((s: any) => s.id));
const spansToAdd = newSpans.filter((s: any) => !existingIds.has(s.id));
return [{ spans: [...existingSpans, ...spansToAdd] }];
});
}
} else if (message.type === 'trace' && message.data) {
// Legacy flat trace format (fallback)
setTraces((prev) => [...prev, message.data]);
} else if (message.type === 'complete' && message.data) {
setIsComplete(true);
setStatus(message.data.status);
// Update with final complete spans if provided (this is the full tree)
if (message.data.spans && Array.isArray(message.data.spans)) {
setTraces([{ spans: message.data.spans }]);
}
} else if (message.type === 'error') {
setIsComplete(true);
setStatus('error');
}
},
}
);
const clearTraces = useCallback(() => {
setTraces([]);
setIsComplete(false);
setStatus(null);
}, []);
return { traces, isConnected, isComplete, status, error, clearTraces };
};
|