@@ -48,45 +48,92 @@ const connectIPC = () => {
4848 ipcClient . setEncoding ( 'utf8' ) ;
4949
5050 ipcClient . connect ( CROWDLLAMA_SOCKET_PATH , ( ) => {
51- console . log ( 'Connected to Go backend socket (persistent)' ) ;
51+ console . log ( '🟢 [MAIN] Connected to Go backend socket (persistent)' ) ;
5252 } ) ;
5353
54- ipcClient . on ( 'data' , ( data : string ) => {
54+ ipcClient . on ( 'data' , ( data : string ) => {
55+ console . log ( '🔵 [MAIN] Raw data received from backend:' , data ) ;
56+ console . log ( '🔵 [MAIN] Data length:' , data . length ) ;
57+ console . log ( '🔵 [MAIN] Data type:' , typeof data ) ;
58+ console . log ( '🔵 [MAIN] Buffer before adding:' , ipcClientBuffer ) ;
5559 ipcClientBuffer += data ;
60+ console . log ( '🔵 [MAIN] Buffer after adding:' , ipcClientBuffer ) ;
61+ console . log ( '🔵 [MAIN] Buffer length:' , ipcClientBuffer . length ) ;
62+
63+ // Try to parse the entire buffer as a single JSON message first
64+ if ( ipcClientBuffer . trim ( ) ) {
65+ try {
66+ const parsed = JSON . parse ( ipcClientBuffer . trim ( ) ) ;
67+ console . log ( '🔵 [MAIN] Parsed single JSON message from backend:' , parsed ) ;
68+
69+ // Route incoming messages to renderer process
70+ if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
71+ console . log ( '🔵 [MAIN] Forwarding to renderer:' , parsed ) ;
72+ mainWindow . webContents . send ( 'backend-message' , parsed ) ;
73+ } else {
74+ console . log ( '🔴 [MAIN] Main window not available for forwarding' ) ;
75+ }
76+
77+ // Clear the buffer after successful parsing
78+ ipcClientBuffer = '' ;
79+ console . log ( '🔵 [MAIN] Cleared buffer after parsing' ) ;
80+ return ;
81+ } catch ( err ) {
82+ console . log ( '🔵 [MAIN] Not a single JSON message, trying newline-delimited parsing' ) ;
83+ }
84+ }
85+
86+ // Fallback to newline-delimited parsing
5687 let index ;
5788 while ( ( index = ipcClientBuffer . indexOf ( '\n' ) ) !== - 1 ) {
89+ console . log ( '🔵 [MAIN] Found newline at index:' , index ) ;
5890 const message = ipcClientBuffer . slice ( 0 , index ) ;
5991 ipcClientBuffer = ipcClientBuffer . slice ( index + 1 ) ;
92+ console . log ( '🔵 [MAIN] Processing message chunk:' , message ) ;
93+ console . log ( '🔵 [MAIN] Message chunk length:' , message . length ) ;
94+ console . log ( '🔵 [MAIN] Message chunk trimmed:' , message . trim ( ) ) ;
95+ console . log ( '🔵 [MAIN] Remaining buffer:' , ipcClientBuffer ) ;
96+
6097 if ( message . trim ( ) ) {
6198 try {
6299 const parsed = JSON . parse ( message ) ;
63- console . log ( 'Received from backend:' , parsed ) ;
64- // TODO: handle parsed message (route to renderer, etc)
100+ console . log ( '🔵 [MAIN] Parsed message from backend:' , parsed ) ;
101+
102+ // Route incoming messages to renderer process
103+ if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
104+ console . log ( '🔵 [MAIN] Forwarding to renderer:' , parsed ) ;
105+ mainWindow . webContents . send ( 'backend-message' , parsed ) ;
106+ } else {
107+ console . log ( '🔴 [MAIN] Main window not available for forwarding' ) ;
108+ }
65109 } catch ( err ) {
66- console . log ( 'Failed to parse backend message:' , message ) ;
110+ console . log ( '🔴 [MAIN] Failed to parse backend message:' , message , err ) ;
67111 }
112+ } else {
113+ console . log ( '🔵 [MAIN] Empty message chunk, skipping' ) ;
68114 }
69115 }
70116 } ) ;
71117
72118 ipcClient . on ( 'error' , ( err : any ) => {
73- console . log ( 'IPC socket error:' , err . message ) ;
119+ console . log ( '🔴 [MAIN] IPC socket error:' , err . message ) ;
74120 // Optionally, try to reconnect or clean up
75121 ipcClient = null ;
76122 } ) ;
77123
78124 ipcClient . on ( 'close' , ( ) => {
79- console . log ( 'IPC socket closed' ) ;
125+ console . log ( '🔴 [MAIN] IPC socket closed' ) ;
80126 ipcClient = null ;
81127 } ) ;
82128} ;
83129
84130// Send a message over the persistent IPC connection
85131const sendIPCMessage = ( msg : object ) => {
86132 if ( ipcClient && ! ipcClient . destroyed ) {
133+ console . log ( '🟡 [MAIN] Sending to backend:' , msg ) ;
87134 ipcClient . write ( JSON . stringify ( msg ) + '\n' ) ;
88135 } else {
89- console . log ( 'IPC client not connected, cannot send message' ) ;
136+ console . log ( '🔴 [MAIN] IPC client not connected, cannot send message' ) ;
90137 }
91138} ;
92139
@@ -258,6 +305,37 @@ ipcMain.handle('ping-backend', async () => {
258305 }
259306} ) ;
260307
308+ // New IPC handlers for initialize and prompt messages
309+ ipcMain . handle ( 'initialize-backend' , async ( event , mode : 'worker' | 'consumer' ) => {
310+ console . log ( '🟡 [MAIN] Received initialize-backend request:' , { mode } ) ;
311+ try {
312+ sendIPCMessage ( { type : 'initialize' , mode } ) ;
313+ console . log ( '🟡 [MAIN] Initialize message sent successfully' ) ;
314+ return { success : true , message : 'Initialize message sent successfully' } ;
315+ } catch ( error ) {
316+ console . error ( '🔴 [MAIN] Error sending initialize message:' , error ) ;
317+ return {
318+ success : false ,
319+ message : error instanceof Error ? error . message : 'Unknown error' ,
320+ } ;
321+ }
322+ } ) ;
323+
324+ ipcMain . handle ( 'send-prompt' , async ( event , prompt : string , model : string ) => {
325+ console . log ( '🟡 [MAIN] Received send-prompt request:' , { prompt, model } ) ;
326+ try {
327+ sendIPCMessage ( { type : 'prompt' , prompt, model } ) ;
328+ console . log ( '🟡 [MAIN] Prompt message sent successfully' ) ;
329+ return { success : true , message : 'Prompt sent successfully' } ;
330+ } catch ( error ) {
331+ console . error ( '🔴 [MAIN] Error sending prompt:' , error ) ;
332+ return {
333+ success : false ,
334+ message : error instanceof Error ? error . message : 'Unknown error' ,
335+ } ;
336+ }
337+ } ) ;
338+
261339if ( process . env . NODE_ENV === 'production' ) {
262340 const sourceMapSupport = require ( 'source-map-support' ) ;
263341 sourceMapSupport . install ( ) ;
0 commit comments