mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat: add session status store with optimistic active state and SSE polling (#272)
* feat: add session status store with optimistic active state and SSE polling - Adds sessionStatusStore with setOptimisticActive (auto-timeout), statusCache deduplication, and replaceStatuses/clearStatus - Integrates store in useSendPrompt (set optimistic on send, clear on error) - Adds session.status SSE event handling in useSSE with 5s polling fallback - Refactors test to mock the store instead of using real store instance - Fixes repoId null check in useRepoActivity * feat: replace event-source-polyfill with native SSE for PromptInput * fix: keep stop button visible and hide clear prompt button during active session * feat: restore failed queued prompts and preserve optimistic active status during poll * test: add MessageThread tests for optimistic editing state and resend flow * fix: handle queued prompt edge cases during pending submission and SSE errors
This commit is contained in:
1 parent
931f1215e6
commit
4e32fd5b00
16 files changed
+833
-61
No files matched your search
@@ -10,12 +10,22 @@ interface SessionStatusStore {
|
||||
statuses: Map<string, SessionStatusType>
|
||||
statusCache: Map<string, string>
|
||||
setStatus: (sessionID: string, status: SessionStatusType) => void
|
||||
setOptimisticActive: (sessionID: string, timeoutMs?: number) => void
|
||||
replaceStatuses: (statuses: Record<string, SessionStatusType>) => void
|
||||
getStatus: (sessionID: string) => SessionStatusType
|
||||
clearStatus: (sessionID: string) => void
|
||||
}
|
||||
|
||||
const DEFAULT_STATUS: SessionStatusType = { type: 'idle' }
|
||||
const OPTIMISTIC_ACTIVE_TIMEOUT_MS = 120_000
|
||||
const optimisticActiveTimers = new Map<string, ReturnType<typeof setTimeout>>()
|
||||
|
||||
const clearOptimisticActiveTimer = (sessionID: string): void => {
|
||||
const timer = optimisticActiveTimers.get(sessionID)
|
||||
if (!timer) return
|
||||
clearTimeout(timer)
|
||||
optimisticActiveTimers.delete(sessionID)
|
||||
}
|
||||
|
||||
const getStatusHash = (status: SessionStatusType): string => {
|
||||
if (status.type === 'retry') {
|
||||
@@ -29,6 +39,7 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => ({
|
||||
statusCache: new Map(),
|
||||
|
||||
setStatus: (sessionID: string, status: SessionStatusType) => {
|
||||
clearOptimisticActiveTimer(sessionID)
|
||||
const hash = getStatusHash(status)
|
||||
const previousHash = get().statusCache.get(sessionID)
|
||||
|
||||
@@ -56,9 +67,40 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => ({
|
||||
})
|
||||
},
|
||||
|
||||
setOptimisticActive: (sessionID: string, timeoutMs = OPTIMISTIC_ACTIVE_TIMEOUT_MS) => {
|
||||
clearOptimisticActiveTimer(sessionID)
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
optimisticActiveTimers.delete(sessionID)
|
||||
const currentStatus = get().getStatus(sessionID)
|
||||
if (currentStatus.type === 'busy') {
|
||||
get().clearStatus(sessionID)
|
||||
}
|
||||
}, timeoutMs)
|
||||
|
||||
optimisticActiveTimers.set(sessionID, timer)
|
||||
|
||||
const hash = getStatusHash({ type: 'busy' })
|
||||
const previousHash = get().statusCache.get(sessionID)
|
||||
if (previousHash === hash) return
|
||||
|
||||
set((state) => {
|
||||
const newMap = new Map(state.statuses)
|
||||
const newCache = new Map(state.statusCache)
|
||||
newMap.set(sessionID, { type: 'busy' })
|
||||
newCache.set(sessionID, hash)
|
||||
return { statuses: newMap, statusCache: newCache }
|
||||
})
|
||||
},
|
||||
|
||||
replaceStatuses: (statuses: Record<string, SessionStatusType>) => {
|
||||
for (const sessionID of Object.keys(statuses)) {
|
||||
clearOptimisticActiveTimer(sessionID)
|
||||
}
|
||||
|
||||
const newMap = new Map<string, SessionStatusType>()
|
||||
const newCache = new Map<string, string>()
|
||||
const currentStatuses = get().statuses
|
||||
|
||||
for (const [sessionID, status] of Object.entries(statuses)) {
|
||||
if (status.type === 'idle') continue
|
||||
@@ -66,6 +108,12 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => ({
|
||||
newCache.set(sessionID, getStatusHash(status))
|
||||
}
|
||||
|
||||
for (const [sessionID, status] of currentStatuses.entries()) {
|
||||
if (!optimisticActiveTimers.has(sessionID) || sessionID in statuses) continue
|
||||
newMap.set(sessionID, status)
|
||||
newCache.set(sessionID, getStatusHash(status))
|
||||
}
|
||||
|
||||
const currentCache = get().statusCache
|
||||
if (currentCache.size === newCache.size) {
|
||||
let unchanged = true
|
||||
@@ -86,6 +134,7 @@ export const useSessionStatus = create<SessionStatusStore>((set, get) => ({
|
||||
},
|
||||
|
||||
clearStatus: (sessionID: string) => {
|
||||
clearOptimisticActiveTimer(sessionID)
|
||||
const previousHash = get().statusCache.get(sessionID)
|
||||
if (!previousHash) return
|
||||
|
||||
|
||||
Reference in new issue
Block a user