| 164 | }, [panelId]); |
| 165 | |
| 166 | const initializeCodexPanel = async () => { |
| 167 | if (!sessionId || !activeSession) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | try { |
| 172 | initializingRef.current = true; |
| 173 | |
| 174 | // Initialize the Codex panel |
| 175 | const initResult = await window.electron?.invoke('codexPanel:initialize', panelId, sessionId, activeSession.worktreePath); |
| 176 | |
| 177 | if (initResult?.hasExistingSession) { |
| 178 | // If there's an existing session ID, treat the panel as initialized so we can continue it |
| 179 | setIsInitialized(true); |
| 180 | |
| 181 | // Load existing conversation history for this panel |
| 182 | try { |
| 183 | const outputs = await window.electron?.invoke('codexPanel:getOutputs', panelId); |
| 184 | if (outputs && outputs.length > 0) { |
| 185 | // Rebuild conversation history from outputs |
| 186 | const history: ConversationMessage[] = []; |
| 187 | for (const output of outputs) { |
| 188 | if (output.type === 'json' && output.data) { |
| 189 | history.push({ |
| 190 | type: output.data.type || 'unknown', |
| 191 | content: output.data, |
| 192 | timestamp: output.timestamp || new Date().toISOString() |
| 193 | }); |
| 194 | } |
| 195 | } |
| 196 | conversationHistoryRef.current = history; |
| 197 | } |
| 198 | } catch (error) { |
| 199 | console.error('Failed to load conversation history:', error); |
| 200 | } |
| 201 | } else { |
| 202 | // Check if it's running |
| 203 | const { isRunning } = await window.electron?.invoke('codexPanel:isRunning', panelId) || { isRunning: false }; |
| 204 | setIsInitialized(isRunning); |
| 205 | } |
| 206 | |
| 207 | } catch (error) { |
| 208 | console.error('Failed to initialize panel:', error); |
| 209 | } finally { |
| 210 | initializingRef.current = false; |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | const handleSendMessage = useCallback(async (message: string, options?: MessageOptions) => { |
| 215 | if (!activeSession || !panelId) { |