import React, { useState, useEffect } from 'react'; import ResearchForm from '../Task/ResearchForm'; import Report from '../Task/Report'; import AgentLogs from '../Task/AgentLogs'; import AccessReport from '../ResearchBlocks/AccessReport'; interface ChatBoxSettings { report_source: string; report_type: string; tone: string; } interface ChatBoxProps { chatBoxSettings: ChatBoxSettings; setChatBoxSettings: React.Dispatch>; } interface OutputData { pdf?: string; docx?: string; json?: string; } interface WebSocketMessage { type: 'logs' | 'report' | 'path'; output: string | OutputData; } export default function ChatBox({ chatBoxSettings, setChatBoxSettings }: ChatBoxProps) { const [agentLogs, setAgentLogs] = useState([]); const [report, setReport] = useState(""); const [accessData, setAccessData] = useState({}); const [socket, setSocket] = useState(null); useEffect(() => { if (typeof window !== 'undefined') { const { protocol, pathname } = window.location; let { host } = window.location; host = host.includes('localhost') ? 'localhost:8000' : host; const ws_uri = `${protocol === 'https:' ? 'wss:' : 'ws:'}//${host}${pathname}ws`; const newSocket = new WebSocket(ws_uri); setSocket(newSocket); newSocket.onmessage = (event) => { const data = JSON.parse(event.data) as WebSocketMessage; if (data.type === 'logs') { setAgentLogs((prevLogs: any[]) => [...prevLogs, data]); } else if (data.type === 'report') { setReport((prevReport: string) => prevReport + (data.output as string)); } else if (data.type === 'path') { const output = data.output as OutputData; setAccessData({ ...(output.pdf && { pdf: `outputs/${output.pdf}` }), ...(output.docx && { docx: `outputs/${output.docx}` }), ...(output.json && { json: `outputs/${output.json}` }) }); } }; return () => { newSocket.close(); }; } }, []); return (
{agentLogs?.length > 0 ? : ''}
{report ? : ''} {Object.keys(accessData).length > 0 && }
); }