Skip to content

Commit

Permalink
Merge pull request #1039 from kga245/unified-logs-handler-2
Browse files Browse the repository at this point in the history
Bug fixes to unified logs
  • Loading branch information
assafelovic authored Dec 25, 2024
2 parents 6964e4e + 94636ea commit dd61cdc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
24 changes: 0 additions & 24 deletions backend/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@
execute_multi_agents, handle_websocket_communication
)

from gpt_researcher.utils.logging_config import setup_research_logging

import logging

# Get logger instance
logger = logging.getLogger(__name__)

# Don't override parent logger settings
logger.propagate = True

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("server_log.txt"), # Log to file
logging.StreamHandler() # Also print to console
]
)


# Models

Expand Down Expand Up @@ -94,11 +75,6 @@ def startup_event():
app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")
os.makedirs(DOC_PATH, exist_ok=True)

# Setup research logging
log_file, json_file, research_logger, json_handler = setup_research_logging() # Unpack all 4 values
research_logger.json_handler = json_handler # Store the JSON handler on the logger
research_logger.info(f"Research log file: {log_file}")
research_logger.info(f"Research JSON file: {json_file}")

# Routes

Expand Down
43 changes: 27 additions & 16 deletions frontend/nextjs/components/ResearchBlocks/AccessReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,38 @@ interface AccessReportProps {
docx?: string;
json?: string;
};
chatBoxSettings: any;
logs?: any[];
chatBoxSettings: {
report_type?: string;
};
report: string;
}

const AccessReport: React.FC<AccessReportProps> = ({ accessData, chatBoxSettings, report }) => {
const host = getHost();

const getReportLink = (dataType: 'pdf' | 'docx' | 'json'): string => {
if (!accessData[dataType]) {
// Early return if path is not available
if (!accessData?.[dataType]) {
console.warn(`No ${dataType} path provided`);
return '#';
}

// Ensure path starts with outputs/
let path = accessData[dataType];
if (!path.startsWith('outputs/')) {
path = `outputs/${path}`;
}
const path = accessData[dataType] as string;

// Clean the path - remove leading/trailing slashes and handle outputs/ prefix
const cleanPath = path
.trim()
.replace(/^\/+|\/+$/g, ''); // Remove leading/trailing slashes

// Only prepend outputs/ if it's not already there
const finalPath = cleanPath.startsWith('outputs/')
? cleanPath
: `outputs/${cleanPath}`;

return `${host}/${path}`;
return `${host}/${finalPath}`;
};

// Safety check for accessData
if (!accessData || typeof accessData !== 'object') {
return null;
}
Expand All @@ -50,13 +59,15 @@ const AccessReport: React.FC<AccessReportProps> = ({ accessData, chatBoxSettings
rel="noopener noreferrer">
Download DocX
</a>
<a
href={getReportLink('json')}
className="bg-purple-500 text-white active:bg-purple-600 font-bold uppercase text-sm px-6 py-3 rounded shadow hover:shadow-lg outline-none focus:outline-none mr-1 mb-1 ease-linear transition-all duration-150"
target="_blank"
rel="noopener noreferrer">
Download Logs
</a>
{chatBoxSettings?.report_type === 'research_report' && (
<a
href={getReportLink('json')}
className="bg-purple-500 text-white active:bg-purple-600 font-bold uppercase text-sm px-6 py-3 rounded shadow hover:shadow-lg outline-none focus:outline-none mr-1 mb-1 ease-linear transition-all duration-150"
target="_blank"
rel="noopener noreferrer">
Download Logs
</a>
)}
</div>
);
};
Expand Down
25 changes: 19 additions & 6 deletions frontend/nextjs/components/Settings/ChatBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ interface ChatBoxProps {
chatBoxSettings: ChatBoxSettings;
setChatBoxSettings: React.Dispatch<React.SetStateAction<ChatBoxSettings>>;
}

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<any[]>([]);
Expand All @@ -31,17 +43,18 @@ export default function ChatBox({ chatBoxSettings, setChatBoxSettings }: ChatBox
setSocket(newSocket);

newSocket.onmessage = (event) => {
const data = JSON.parse(event.data);
const data = JSON.parse(event.data) as WebSocketMessage;

if (data.type === 'logs') {
setAgentLogs((prevLogs) => [...prevLogs, data]);
setAgentLogs((prevLogs: any[]) => [...prevLogs, data]);
} else if (data.type === 'report') {
setReport((prevReport) => prevReport + data.output);
setReport((prevReport: string) => prevReport + (data.output as string));
} else if (data.type === 'path') {
const output = data.output as OutputData;
setAccessData({
pdf: `outputs/${data.output.pdf}`,
docx: `outputs/${data.output.docx}`,
json: `outputs/${data.output.json}`
...(output.pdf && { pdf: `outputs/${output.pdf}` }),
...(output.docx && { docx: `outputs/${output.docx}` }),
...(output.json && { json: `outputs/${output.json}` })
});
}
};
Expand Down

0 comments on commit dd61cdc

Please sign in to comment.