(skipCache = false)
| 139 | }, [messages, hasUserScrolled, isFullscreen]); |
| 140 | |
| 141 | const loadOutput = async (skipCache = false) => { |
| 142 | if (!run?.id) return; |
| 143 | |
| 144 | console.log('[AgentRunOutputViewer] Loading output for run:', { |
| 145 | runId: run.id, |
| 146 | status: run.status, |
| 147 | sessionId: run.session_id, |
| 148 | skipCache |
| 149 | }); |
| 150 | |
| 151 | try { |
| 152 | // Check cache first if not skipping cache |
| 153 | if (!skipCache) { |
| 154 | const cached = getCachedOutput(run.id); |
| 155 | if (cached) { |
| 156 | console.log('[AgentRunOutputViewer] Found cached output'); |
| 157 | const cachedJsonlLines = cached.output.split('\n').filter(line => line.trim()); |
| 158 | setRawJsonlOutput(cachedJsonlLines); |
| 159 | setMessages(cached.messages); |
| 160 | // If cache is recent (less than 5 seconds old) and session isn't running, use cache only |
| 161 | if (Date.now() - cached.lastUpdated < 5000 && run.status !== 'running') { |
| 162 | console.log('[AgentRunOutputViewer] Using recent cache, skipping refresh'); |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | setLoading(true); |
| 169 | |
| 170 | // If we have a session_id, try to load from JSONL file first |
| 171 | if (run.session_id && run.session_id !== '') { |
| 172 | console.log('[AgentRunOutputViewer] Attempting to load from JSONL with session_id:', run.session_id); |
| 173 | try { |
| 174 | const history = await api.loadAgentSessionHistory(run.session_id); |
| 175 | console.log('[AgentRunOutputViewer] Successfully loaded JSONL history:', history.length, 'messages'); |
| 176 | |
| 177 | // Convert history to messages format |
| 178 | const loadedMessages: ClaudeStreamMessage[] = history.map(entry => ({ |
| 179 | ...entry, |
| 180 | type: entry.type || "assistant" |
| 181 | })); |
| 182 | |
| 183 | setMessages(loadedMessages); |
| 184 | setRawJsonlOutput(history.map(h => JSON.stringify(h))); |
| 185 | |
| 186 | // Update cache |
| 187 | setCachedOutput(run.id, { |
| 188 | output: history.map(h => JSON.stringify(h)).join('\n'), |
| 189 | messages: loadedMessages, |
| 190 | lastUpdated: Date.now(), |
| 191 | status: run.status |
| 192 | }); |
| 193 | |
| 194 | // Set up live event listeners for running sessions |
| 195 | if (run.status === 'running') { |
| 196 | console.log('[AgentRunOutputViewer] Setting up live listeners for running session'); |
| 197 | setupLiveEventListeners(); |
| 198 |
no test coverage detected