mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(sessions): show background activity for shells and child sessions
This commit is contained in:
1 parent
51e92d8cf2
commit
2e2eff05af
7 files changed
+434
-15
No files matched your search
@@ -13,12 +13,19 @@ interface StatusSnapshotToken {
|
||||
order: number
|
||||
}
|
||||
|
||||
interface RunningShell {
|
||||
sessionID: string
|
||||
directory: string
|
||||
}
|
||||
|
||||
interface SessionStatusStore {
|
||||
statuses: Map<string, SessionStatusType>
|
||||
statusCache: Map<string, string>
|
||||
statusRevisions: Map<string, number>
|
||||
knownSessions: Set<string>
|
||||
outcomes: Map<string, ChildOutcome>
|
||||
parents: Map<string, string | null>
|
||||
runningShells: Map<string, RunningShell>
|
||||
revision: number
|
||||
setStatus: (sessionID: string, status: SessionStatusType) => void
|
||||
setOutcome: (sessionID: string, outcome: ChildOutcome) => void
|
||||
@@ -30,6 +37,10 @@ interface SessionStatusStore {
|
||||
endStatusSnapshot: (token: StatusSnapshotToken) => void
|
||||
getStatus: (sessionID: string) => SessionStatusType
|
||||
clearStatus: (sessionID: string) => void
|
||||
setSessionParent: (sessionID: string, parentID: string | null) => void
|
||||
setShellRunning: (shellID: string, sessionID: string, directory: string) => void
|
||||
clearShell: (shellID: string) => void
|
||||
replaceRunningShells: (directory: string, shells: Array<{ id: string; sessionID: string }>) => void
|
||||
forgetSession: (sessionID: string) => void
|
||||
}
|
||||
|
||||
@@ -70,6 +81,15 @@ const sameSets = (left: Set<string>, right: Set<string>): boolean => {
|
||||
return true
|
||||
}
|
||||
|
||||
const sameRunningShells = (left: Map<string, RunningShell>, right: Map<string, RunningShell>): boolean => {
|
||||
if (left.size !== right.size) return false
|
||||
for (const [shellID, shell] of right.entries()) {
|
||||
const existing = left.get(shellID)
|
||||
if (!existing || existing.sessionID !== shell.sessionID || existing.directory !== shell.directory) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const markSessionKnown = (knownSessions: Set<string>, sessionID: string): Set<string> | null => {
|
||||
if (knownSessions.has(sessionID)) return null
|
||||
const next = new Set(knownSessions)
|
||||
@@ -109,6 +129,28 @@ export function busyStatusesFromActiveSessions(active: Record<string, unknown>):
|
||||
)
|
||||
}
|
||||
|
||||
export function hasBackgroundActivity(
|
||||
state: Pick<SessionStatusStore, 'statuses' | 'parents' | 'runningShells'>,
|
||||
sessionID: string,
|
||||
): boolean {
|
||||
const visited = new Set<string>()
|
||||
const visit = (id: string): boolean => {
|
||||
if (visited.has(id)) return false
|
||||
visited.add(id)
|
||||
for (const shell of state.runningShells.values()) {
|
||||
if (shell.sessionID === id) return true
|
||||
}
|
||||
for (const [childID, parentID] of state.parents.entries()) {
|
||||
if (parentID !== id) continue
|
||||
const childStatus = state.statuses.get(childID)
|
||||
if (childStatus && childStatus.type !== 'idle') return true
|
||||
if (visit(childID)) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return visit(sessionID)
|
||||
}
|
||||
|
||||
export const useSessionStatus = create<SessionStatusStore>((set, get) => {
|
||||
const recordRevisionPatch = (
|
||||
state: SessionStatusStore,
|
||||
@@ -151,6 +193,8 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
|
||||
statusRevisions: new Map(),
|
||||
knownSessions: new Set(),
|
||||
outcomes: new Map(),
|
||||
parents: new Map(),
|
||||
runningShells: new Map(),
|
||||
revision: 0,
|
||||
|
||||
setStatus: (sessionID: string, status: SessionStatusType) => {
|
||||
@@ -341,14 +385,59 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
|
||||
})
|
||||
},
|
||||
|
||||
setSessionParent: (sessionID: string, parentID: string | null) => {
|
||||
const current = get().parents
|
||||
if (current.has(sessionID) && current.get(sessionID) === parentID) return
|
||||
set((state) => {
|
||||
const parents = new Map(state.parents)
|
||||
parents.set(sessionID, parentID)
|
||||
return { parents }
|
||||
})
|
||||
},
|
||||
|
||||
setShellRunning: (shellID: string, sessionID: string, directory: string) => {
|
||||
const current = get().runningShells.get(shellID)
|
||||
if (current && current.sessionID === sessionID && current.directory === directory) return
|
||||
set((state) => {
|
||||
const runningShells = new Map(state.runningShells)
|
||||
runningShells.set(shellID, { sessionID, directory })
|
||||
return { runningShells }
|
||||
})
|
||||
},
|
||||
|
||||
clearShell: (shellID: string) => {
|
||||
if (!get().runningShells.has(shellID)) return
|
||||
set((state) => {
|
||||
const runningShells = new Map(state.runningShells)
|
||||
runningShells.delete(shellID)
|
||||
return { runningShells }
|
||||
})
|
||||
},
|
||||
|
||||
replaceRunningShells: (directory: string, shells: Array<{ id: string; sessionID: string }>) => {
|
||||
const current = get().runningShells
|
||||
const runningShells = new Map(current)
|
||||
for (const [shellID, shell] of runningShells.entries()) {
|
||||
if (shell.directory === directory) runningShells.delete(shellID)
|
||||
}
|
||||
for (const shell of shells) {
|
||||
runningShells.set(shell.id, { sessionID: shell.sessionID, directory })
|
||||
}
|
||||
if (sameRunningShells(current, runningShells)) return
|
||||
set({ runningShells })
|
||||
},
|
||||
|
||||
forgetSession: (sessionID: string) => {
|
||||
clearOptimisticActiveTimer(sessionID)
|
||||
const current = get()
|
||||
const ownsShell = Array.from(current.runningShells.values()).some((shell) => shell.sessionID === sessionID)
|
||||
const hasSession =
|
||||
current.statuses.has(sessionID) ||
|
||||
current.statusCache.has(sessionID) ||
|
||||
current.knownSessions.has(sessionID) ||
|
||||
current.outcomes.has(sessionID)
|
||||
current.outcomes.has(sessionID) ||
|
||||
current.parents.has(sessionID) ||
|
||||
ownsShell
|
||||
if (!hasSession && inFlightSnapshots.size === 0) return
|
||||
|
||||
set((state) => {
|
||||
@@ -356,15 +445,23 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => {
|
||||
const statusCache = new Map(state.statusCache)
|
||||
const knownSessions = new Set(state.knownSessions)
|
||||
const outcomes = new Map(state.outcomes)
|
||||
const parents = new Map(state.parents)
|
||||
const runningShells = new Map(state.runningShells)
|
||||
statuses.delete(sessionID)
|
||||
statusCache.delete(sessionID)
|
||||
knownSessions.delete(sessionID)
|
||||
outcomes.delete(sessionID)
|
||||
parents.delete(sessionID)
|
||||
for (const [shellID, shell] of runningShells.entries()) {
|
||||
if (shell.sessionID === sessionID) runningShells.delete(shellID)
|
||||
}
|
||||
return {
|
||||
statuses,
|
||||
statusCache,
|
||||
knownSessions,
|
||||
outcomes,
|
||||
parents,
|
||||
runningShells,
|
||||
...(recordRevisionPatch(state, sessionID) ?? {}),
|
||||
}
|
||||
})
|
||||
@@ -385,3 +482,7 @@ export const useChildLifecycleForSession = (sessionID: string | undefined): Back
|
||||
return childLifecycle(status, state.knownSessions.has(sessionID), state.outcomes.get(sessionID))
|
||||
})
|
||||
}
|
||||
|
||||
export const useHasBackgroundActivity = (sessionID: string | undefined): boolean => {
|
||||
return useSessionStatus((state) => (sessionID ? hasBackgroundActivity(state, sessionID) : false))
|
||||
}
|
||||
Reference in new issue
Block a user