(skipCache = false)
| 91 | |
| 92 | |
| 93 | const loadOutput = async (skipCache = false) => { |
| 94 | if (!session.id) return; |
| 95 | |
| 96 | try { |
| 97 | // Check cache first if not skipping cache |
| 98 | if (!skipCache) { |
| 99 | const cached = getCachedOutput(session.id); |
| 100 | if (cached) { |
| 101 | const cachedJsonlLines = cached.output.split('\n').filter(line => line.trim()); |
| 102 | setRawJsonlOutput(cachedJsonlLines); |
| 103 | setMessages(cached.messages); |
| 104 | // If cache is recent (less than 5 seconds old) and session isn't running, use cache only |
| 105 | if (Date.now() - cached.lastUpdated < 5000 && session.status !== 'running') { |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | setLoading(true); |
| 112 | |
| 113 | // If we have a session_id, try to load from JSONL file first |
| 114 | if (session.session_id && session.session_id !== '') { |
| 115 | try { |
| 116 | const history = await api.loadAgentSessionHistory(session.session_id); |
| 117 | |
| 118 | // Convert history to messages format using AgentExecution style |
| 119 | const loadedMessages: ClaudeStreamMessage[] = history.map(entry => ({ |
| 120 | ...entry, |
| 121 | type: entry.type || "assistant" |
| 122 | })); |
| 123 | |
| 124 | setMessages(loadedMessages); |
| 125 | setRawJsonlOutput(history.map(h => JSON.stringify(h))); |
| 126 | |
| 127 | // Update cache |
| 128 | setCachedOutput(session.id, { |
| 129 | output: history.map(h => JSON.stringify(h)).join('\n'), |
| 130 | messages: loadedMessages, |
| 131 | lastUpdated: Date.now(), |
| 132 | status: session.status |
| 133 | }); |
| 134 | |
| 135 | // Set up live event listeners for running sessions |
| 136 | if (session.status === 'running') { |
| 137 | setupLiveEventListeners(); |
| 138 | |
| 139 | try { |
| 140 | await api.streamSessionOutput(session.id); |
| 141 | } catch (streamError) { |
| 142 | console.warn('Failed to start streaming, will poll instead:', streamError); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return; |
| 147 | } catch (err) { |
| 148 | console.warn('Failed to load from JSONL, falling back to regular output:', err); |
| 149 | } |
| 150 | } |
no test coverage detected