@@ -26,6 +26,10 @@ const OPENAI_BASE_URL = (process.env.OPENAI_BASE_URL || 'https://api.openai.com/
2626
2727const wsClients = new Set ( ) ;
2828
29+ const LOG_DIR = path . join ( __dirname , 'logs' ) ;
30+ const LOG_FILE = path . join ( LOG_DIR , 'events.jsonl' ) ;
31+ if ( ! fs . existsSync ( LOG_DIR ) ) fs . mkdirSync ( LOG_DIR , { recursive : true } ) ;
32+
2933const mimeTypes = {
3034 '.html' : 'text/html' , '.js' : 'text/javascript' , '.css' : 'text/css' ,
3135 '.json' : 'application/json' , '.png' : 'image/png' , '.jpg' : 'image/jpeg' ,
@@ -137,7 +141,33 @@ function generateFallbackResponse(persona) {
137141 return responses [ Math . floor ( Math . random ( ) * responses . length ) ] ;
138142}
139143
144+ async function handleLogRequest ( req , res ) {
145+ try {
146+ const data = await readBody ( req ) ;
147+ const entries = data . entries ;
148+ if ( ! Array . isArray ( entries ) || entries . length === 0 ) {
149+ res . writeHead ( 400 , { 'Content-Type' : 'application/json' } ) ;
150+ res . end ( JSON . stringify ( { error : 'entries array is required' } ) ) ;
151+ return ;
152+ }
153+ const lines = entries . map ( e => JSON . stringify ( e ) ) . join ( '\n' ) + '\n' ;
154+ fs . appendFile ( LOG_FILE , lines , ( err ) => {
155+ if ( err ) console . error ( 'Failed to write log:' , err ) ;
156+ } ) ;
157+ res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
158+ res . end ( JSON . stringify ( { ok : true , count : entries . length } ) ) ;
159+ } catch ( error ) {
160+ res . writeHead ( 400 , { 'Content-Type' : 'application/json' } ) ;
161+ res . end ( JSON . stringify ( { error : 'Invalid JSON' } ) ) ;
162+ }
163+ }
164+
140165function handlePostRequest ( req , res , parsedUrl ) {
166+ if ( parsedUrl . pathname === '/api/log' ) {
167+ handleLogRequest ( req , res ) ;
168+ return ;
169+ }
170+
141171 if ( parsedUrl . pathname === '/api/chat' ) {
142172 handleChatRequest ( req , res ) ;
143173 return ;
0 commit comments