Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | import React, { createContext, useContext, useEffect, useState, useCallback } from "react"; import type { AgentState, TimelineLine } from "../../shared/types"; import type { SessionPatch } from "../types/ipc-client"; import { logger } from "../logger"; export interface TabSession { sessionId: string; startedAt: number; lastActivityAt: number; status: "active" | "idle" | "ended"; title?: string; agents: Map<string, AgentState>; timeline: TimelineLine[]; pinned: boolean; } interface Ctx { sessions: Map<string, TabSession>; openTabIds: string[]; activeTabId: string | null; setActiveTab: (id: string) => void; closeTab: (id: string) => void; togglePin: (id: string) => void; openTab: (id: string) => void; refreshSessions: () => Promise<void>; refreshSession: (sessionId: string) => Promise<void>; } const SessionCtx = createContext<Ctx | null>(null); export const useSessions = (): Ctx => { const c = useContext(SessionCtx); if (!c) throw new Error("useSessions must be used inside SessionProvider"); return c; }; export function SessionProvider({ children }: { children: React.ReactNode }): JSX.Element { const [sessions, setSessions] = useState<Map<string, TabSession>>(new Map()); const [openTabIds, setOpenTabIds] = useState<string[]>([]); const [activeTabId, setActiveTabId] = useState<string | null>(null); const [closedTabIds, setClosedTabIds] = useState<Set<string>>(new Set()); useEffect(() => { let cancelled = false; void (async () => { const list = await window.claudeVillage.listSessions(); if (cancelled) return; const map = new Map<string, TabSession>(); for (const s of list) { map.set(s.sessionId, { sessionId: s.sessionId, startedAt: s.startedAt, lastActivityAt: s.lastActivityAt, status: s.status, ...(s.title !== undefined ? { title: s.title } : {}), agents: new Map(s.agents.map((a) => [a.id, a])), timeline: s.timeline, pinned: false }); } setSessions(map); // Only auto-open tabs for sessions that received activity in the last // 60 seconds. Anything older is reachable via the sidebar - without // this guard the app flips open a tab for every historical JSONL on // disk and pushes the village off-screen. const ACTIVE_WINDOW_MS = 60_000; const active = list .filter((s) => Date.now() - s.lastActivityAt < ACTIVE_WINDOW_MS) .map((s) => s.sessionId); setOpenTabIds(active); setActiveTabId(active[0] ?? null); })(); const applyPatch = (p: SessionPatch): void => { setSessions((prev) => { const next = new Map(prev); let session = next.get(p.sessionId); // Lazily materialise the session if an `agent-upsert` or // `timeline-append` arrives before a `session-upsert` (e.g. a // subagent-start patch reaches the renderer slightly ahead of the // parent's session-start patch, or the main-process ordering is // ever changed). Without this, the change is silently dropped and // the character never spawns. `session-upsert` still fills in the // authoritative fields when its patch arrives later. const ensureSession = (): TabSession => { if (session) return session; const now = Date.now(); session = { sessionId: p.sessionId, startedAt: now, lastActivityAt: now, status: "active", agents: new Map(), timeline: [], pinned: false }; return session; }; try { for (const change of p.changes) { if (change.kind === "session-upsert") { ensureSession(); session = { ...session!, ...change.session }; } else if (change.kind === "agent-upsert") { ensureSession(); const agents = new Map(session!.agents); agents.set(change.agent.id, change.agent); session = { ...session!, agents }; } else if (change.kind === "agent-remove" && session) { const agents = new Map(session.agents); agents.delete(change.agentId); session = { ...session, agents }; } else if (change.kind === "timeline-append") { ensureSession(); const timeline = [...session!.timeline, change.line].slice(-500); session = { ...session!, timeline }; } } } catch (err) { const e = err instanceof Error ? err : new Error(String(err)); logger.warn("SessionContext failed to apply IPC patch", { sessionId: p.sessionId, message: e.message }); } if (session) next.set(p.sessionId, session); return next; }); setOpenTabIds((prev) => { if (prev.includes(p.sessionId)) return prev; if (closedTabIds.has(p.sessionId)) return prev; return [...prev, p.sessionId]; }); setActiveTabId((prev) => prev ?? p.sessionId); }; const unsubscribe = window.claudeVillage.onPatch(applyPatch); return () => { cancelled = true; unsubscribe(); }; }, [closedTabIds]); const setActiveTab = useCallback((id: string) => setActiveTabId(id), []); const openTab = useCallback((id: string) => { logger.info("session tab opened", { sessionId: id }); setClosedTabIds((prev) => { if (!prev.has(id)) return prev; const n = new Set(prev); n.delete(id); return n; }); setOpenTabIds((prev) => (prev.includes(id) ? prev : [...prev, id])); setActiveTabId(id); }, []); const closeTab = useCallback((id: string) => { logger.info("session tab closed", { sessionId: id }); setOpenTabIds((prev) => prev.filter((x) => x !== id)); setClosedTabIds((prev) => { if (prev.has(id)) return prev; const n = new Set(prev); n.add(id); return n; }); setActiveTabId((prev) => (prev === id ? null : prev)); }, []); const refreshSessions = useCallback(async (): Promise<void> => { // Chokidar can miss JSONL files created after startup on some platforms. // This callback re-queries the main process for the authoritative list // and merges any previously unseen sessions into state. try { const list = await window.claudeVillage.listSessions(); setSessions((prev) => { const next = new Map(prev); for (const s of list) { const existing = next.get(s.sessionId); if (existing) { // Preserve live in-memory data (timeline/agents) that may be // richer than what the main process serializes back, but pick // up any new title/status/timestamp fields. next.set(s.sessionId, { ...existing, startedAt: s.startedAt, lastActivityAt: s.lastActivityAt, status: s.status, ...(s.title !== undefined ? { title: s.title } : {}) }); } else { next.set(s.sessionId, { sessionId: s.sessionId, startedAt: s.startedAt, lastActivityAt: s.lastActivityAt, status: s.status, ...(s.title !== undefined ? { title: s.title } : {}), agents: new Map(s.agents.map((a) => [a.id, a])), timeline: s.timeline, pinned: false }); } } return next; }); } catch (err) { const e = err instanceof Error ? err : new Error(String(err)); logger.warn("SessionContext refreshSessions failed", { message: e.message }); } }, []); const refreshSession = useCallback(async (sessionId: string): Promise<void> => { // Force-refresh a single session by re-fetching the authoritative snapshot // from the main process and replacing the entry in state. We preserve the // local `pinned` flag because it lives purely on the renderer side. try { const snapshot = await window.claudeVillage.getSession(sessionId); if (!snapshot) return; setSessions((prev) => { const next = new Map(prev); const existing = next.get(sessionId); const pinned = existing?.pinned ?? false; next.set(sessionId, { sessionId: snapshot.sessionId, startedAt: snapshot.startedAt, lastActivityAt: snapshot.lastActivityAt, status: snapshot.status, ...(snapshot.title !== undefined ? { title: snapshot.title } : {}), agents: new Map(snapshot.agents.map((a) => [a.id, a])), timeline: snapshot.timeline, pinned }); return next; }); } catch (err) { const e = err instanceof Error ? err : new Error(String(err)); logger.warn("SessionContext refreshSession failed", { sessionId, message: e.message }); } }, []); const togglePin = useCallback((id: string) => { setSessions((prev) => { const s = prev.get(id); if (!s) return prev; const pinned = !s.pinned; logger.info("session pin toggled", { sessionId: id, pinned }); const next = new Map(prev); next.set(id, { ...s, pinned }); void (pinned ? window.claudeVillage.pinSession(id) : window.claudeVillage.unpinSession(id)); return next; }); }, []); return ( <SessionCtx.Provider value={{ sessions, openTabIds, activeTabId, setActiveTab, closeTab, togglePin, openTab, refreshSessions, refreshSession }} > {children} </SessionCtx.Provider> ); } |