Import apps/android/src/state/session-event-actions.ts
This commit is contained in:
parent
ddde85a1e8
commit
2cca6ef5e6
|
|
@ -20,6 +20,122 @@ export function createSessionEventActions({ get, set, runtime }: SessionAccess):
|
||||||
'handleMuxFrame' | 'handleCodexFrame' | 'handleCodexFrames' | 'handleCursorFrame'> {
|
'handleMuxFrame' | 'handleCodexFrame' | 'handleCodexFrames' | 'handleCursorFrame'> {
|
||||||
return {
|
return {
|
||||||
handleMuxFrame(frame) {
|
handleMuxFrame(frame) {
|
||||||
|
if (frame.payload.type === 'session/catalog-changed') {
|
||||||
|
runtime.scheduleHarnessCatalogSync()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (frame.payload.type === 'session/status') {
|
||||||
|
const { sessionId, running } = frame.payload
|
||||||
|
if (typeof sessionId !== 'string' || typeof running !== 'boolean') return
|
||||||
|
let missingFromCatalog = false
|
||||||
|
set(state => {
|
||||||
|
const existing = state.sessions.find(item => item.sessionId === sessionId)
|
||||||
|
if (existing === undefined) missingFromCatalog = true
|
||||||
|
const entry = state.sessionLifecycles[sessionId]
|
||||||
|
const nextLifecycle = {
|
||||||
|
seq: entry?.seq ?? -1,
|
||||||
|
turn: entry?.turn,
|
||||||
|
running,
|
||||||
|
...(running
|
||||||
|
? { finished: undefined }
|
||||||
|
: { finished: entry?.finished === 'failed' ? 'failed' as const : 'completed' as const }),
|
||||||
|
}
|
||||||
|
const rowChanged = existing !== undefined && existing.running !== running
|
||||||
|
const selectedChanged = state.selectedSession?.sessionId === sessionId && state.selectedSession.running !== running
|
||||||
|
return {
|
||||||
|
sessionLifecycles: { ...state.sessionLifecycles, [sessionId]: nextLifecycle },
|
||||||
|
...(rowChanged ? { sessions: state.sessions.map(item => item.sessionId === sessionId ? { ...item, running } : item) } : {}),
|
||||||
|
...(selectedChanged && state.selectedSession !== undefined ? { selectedSession: { ...state.selectedSession, running } } : {}),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (missingFromCatalog) runtime.scheduleHarnessCatalogSync()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (frame.payload.type === 'session/activity') {
|
||||||
|
const { sessionId, updatedAt } = frame.payload
|
||||||
|
if (typeof sessionId !== 'string' || typeof updatedAt !== 'number' || !Number.isFinite(updatedAt)) return
|
||||||
|
let missingFromCatalog = false
|
||||||
|
set(state => {
|
||||||
|
const existing = state.sessions.find(item => item.sessionId === sessionId)
|
||||||
|
if (existing === undefined) missingFromCatalog = true
|
||||||
|
const rowChanged = existing !== undefined && updatedAt > existing.updatedAt
|
||||||
|
const selectedChanged = state.selectedSession?.sessionId === sessionId && updatedAt > state.selectedSession.updatedAt
|
||||||
|
if (!rowChanged && !selectedChanged) return {}
|
||||||
|
return {
|
||||||
|
...(rowChanged ? { sessions: state.sessions.map(item => item.sessionId === sessionId ? { ...item, updatedAt } : item) } : {}),
|
||||||
|
...(selectedChanged && state.selectedSession !== undefined ? { selectedSession: { ...state.selectedSession, updatedAt } } : {}),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (missingFromCatalog) runtime.scheduleHarnessCatalogSync()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (frame.payload.type === 'session/error') {
|
||||||
|
const { sessionId } = frame.payload
|
||||||
|
if (typeof sessionId !== 'string') return
|
||||||
|
set(state => {
|
||||||
|
const entry = state.sessionLifecycles[sessionId]
|
||||||
|
const existing = state.sessions.find(item => item.sessionId === sessionId)
|
||||||
|
return {
|
||||||
|
sessionLifecycles: {
|
||||||
|
...state.sessionLifecycles,
|
||||||
|
[sessionId]: {
|
||||||
|
seq: entry?.seq ?? -1,
|
||||||
|
turn: entry?.turn,
|
||||||
|
running: entry?.running ?? existing?.running ?? false,
|
||||||
|
finished: 'failed' as const,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (frame.payload.type === 'session/added') {
|
||||||
|
const incoming = frame.payload.session
|
||||||
|
if (incoming === undefined) return
|
||||||
|
let needsWorkspaceSync = false
|
||||||
|
set(state => {
|
||||||
|
const existing = state.sessions.find(item => item.sessionId === incoming.sessionId)
|
||||||
|
const isRoot = incoming.parentSessionId === undefined && incoming.origin !== 'subagent'
|
||||||
|
const assigned = state.workspaces.some(w => (w.backend ?? 'harness') === 'harness' && w.sessionIds.includes(incoming.sessionId))
|
||||||
|
if (existing === undefined && isRoot && !assigned) needsWorkspaceSync = true
|
||||||
|
const merged = existing === undefined
|
||||||
|
? incoming
|
||||||
|
: {
|
||||||
|
...existing,
|
||||||
|
...incoming,
|
||||||
|
...(incoming.title === undefined && existing.title !== undefined ? { title: existing.title } : {}),
|
||||||
|
...(incoming.projections === undefined && existing.projections !== undefined ? { projections: existing.projections } : {}),
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
sessions: existing === undefined
|
||||||
|
? [merged, ...state.sessions]
|
||||||
|
: state.sessions.map(item => item.sessionId === incoming.sessionId ? merged : item),
|
||||||
|
...(state.selectedSession?.sessionId === incoming.sessionId ? { selectedSession: merged } : {}),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (needsWorkspaceSync) runtime.scheduleHarnessCatalogSync()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (frame.payload.type === 'session/removed') {
|
||||||
|
const { sessionId } = frame.payload
|
||||||
|
if (typeof sessionId !== 'string') return
|
||||||
|
set(state => {
|
||||||
|
const inSessions = state.sessions.some(item => item.sessionId === sessionId)
|
||||||
|
const inWorkspaces = state.workspaces.some(w => (w.backend ?? 'harness') === 'harness' && w.sessionIds.includes(sessionId))
|
||||||
|
if (!inSessions && !inWorkspaces) return {}
|
||||||
|
return {
|
||||||
|
...(inSessions ? { sessions: state.sessions.filter(item => item.sessionId !== sessionId) } : {}),
|
||||||
|
...(inWorkspaces
|
||||||
|
? {
|
||||||
|
workspaces: state.workspaces.map(w => (w.backend ?? 'harness') === 'harness' && w.sessionIds.includes(sessionId)
|
||||||
|
? { ...w, sessionIds: w.sessionIds.filter(id => id !== sessionId) }
|
||||||
|
: w),
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
set(state => {
|
set(state => {
|
||||||
const sessionId = frame.payload.sessionId
|
const sessionId = frame.payload.sessionId
|
||||||
if (frame.payload.type === 'session/projection' && sessionId !== undefined && typeof frame.payload.key === 'string') {
|
if (frame.payload.type === 'session/projection' && sessionId !== undefined && typeof frame.payload.key === 'string') {
|
||||||
|
|
@ -28,6 +144,7 @@ export function createSessionEventActions({ get, set, runtime }: SessionAccess):
|
||||||
if (seq !== undefined && previous !== undefined && seq < previous) return {}
|
if (seq !== undefined && previous !== undefined && seq < previous) return {}
|
||||||
const update = (session: RemoteSession): RemoteSession => session.sessionId !== sessionId ? session : {
|
const update = (session: RemoteSession): RemoteSession => session.sessionId !== sessionId ? session : {
|
||||||
...session,
|
...session,
|
||||||
|
...(key === 'title' && typeof value === 'string' && value !== '' ? { title: value } : {}),
|
||||||
projections: { ...session.projections, values: { ...session.projections?.values, [key]: value } },
|
projections: { ...session.projections, values: { ...session.projections?.values, [key]: value } },
|
||||||
}
|
}
|
||||||
const currentSession = state.sessions.find(item => item.sessionId === sessionId) ?? state.selectedSession
|
const currentSession = state.sessions.find(item => item.sessionId === sessionId) ?? state.selectedSession
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue