318 lines
17 KiB
TypeScript
318 lines
17 KiB
TypeScript
import { reduceCodexTimelineFrame } from '@dsh-remote/client-core'
|
|
import { strings as zhCN } from '../locales/i18n'
|
|
import {
|
|
codexItemsToChat,
|
|
codexPermissionPresetFromResponse,
|
|
mergeCodexLive,
|
|
updateCodexSession,
|
|
withCodexPermission,
|
|
} from '../services/codex'
|
|
import { applyCursorFrame, cursorNativeId } from '../services/cursor'
|
|
import type { ChatItem, RemoteSession } from '../types'
|
|
import { applyMuxFrameToMessages, sessionRunningForMuxFrame } from './event-reducer'
|
|
import { inboxRpcIds, reconcileUserQuestionsProjection } from './message-helpers'
|
|
import type { SessionAccess } from './session-runtime'
|
|
import { isHarnessPromptStarted, isRecord } from './session-state'
|
|
import type { AppState } from './store-state'
|
|
|
|
/** SessionEvent actions use the store's single connection/session runtime. */
|
|
export function createSessionEventActions({ get, set, runtime }: SessionAccess): Pick<AppState,
|
|
'handleMuxFrame' | 'handleCodexFrame' | 'handleCodexFrames' | 'handleCursorFrame'> {
|
|
return {
|
|
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 => {
|
|
const sessionId = frame.payload.sessionId
|
|
if (frame.payload.type === 'session/projection' && sessionId !== undefined && typeof frame.payload.key === 'string') {
|
|
const { key, value, seq } = frame.payload
|
|
const previous = state.sessionProjectionSeqs[sessionId]?.[key]
|
|
if (seq !== undefined && previous !== undefined && seq < previous) return {}
|
|
const update = (session: RemoteSession): RemoteSession => session.sessionId !== sessionId ? session : {
|
|
...session,
|
|
...(key === 'title' && typeof value === 'string' && value !== '' ? { title: value } : {}),
|
|
projections: { ...session.projections, values: { ...session.projections?.values, [key]: value } },
|
|
}
|
|
const currentSession = state.sessions.find(item => item.sessionId === sessionId) ?? state.selectedSession
|
|
const nextValues = {
|
|
...currentSession?.projections?.values,
|
|
[key]: value,
|
|
}
|
|
// Only local pending rows follow the inbox. A durable user/message
|
|
// echo is stronger evidence than a delayed control-stream projection.
|
|
// Leaving the inbox may mean execution, steering, or removal; keep the
|
|
// row visible as unconfirmed until an echo/history or explicit action
|
|
// settles it. No second projection or timer is needed for visibility.
|
|
const settle = key === 'inbox' ? (items: ChatItem[]): ChatItem[] => {
|
|
const queued = inboxRpcIds(value)
|
|
const next = items.map(item => {
|
|
if (item.kind !== 'message' || item.role !== 'user' || !item.id.startsWith('local:')
|
|
|| item.nativeSeq !== undefined || item.requestRpcId === undefined || item.sendState === 'canceled') return item
|
|
if (queued.has(item.requestRpcId)) {
|
|
return { ...item, sendState: 'queued' as const, queuedAdopted: true, sendError: undefined }
|
|
}
|
|
if (item.queuedAdopted === true && (item.sendState === 'queued' || item.sendState === undefined)) {
|
|
return { ...item, sendState: 'unconfirmed' as const, sendError: undefined }
|
|
}
|
|
return item
|
|
})
|
|
return nextValues.userQuestions === undefined
|
|
? next
|
|
: reconcileUserQuestionsProjection(next, sessionId, nextValues.userQuestions, value)
|
|
} : key === 'userQuestions' ? (items: ChatItem[]): ChatItem[] => (
|
|
reconcileUserQuestionsProjection(items, sessionId, value, nextValues.inbox)
|
|
) : undefined
|
|
return {
|
|
sessions: state.sessions.map(update),
|
|
selectedSession: state.selectedSession === undefined ? undefined : update(state.selectedSession),
|
|
...(settle === undefined ? {} : {
|
|
messages: {
|
|
...state.messages,
|
|
[sessionId]: settle(state.messages[sessionId] ?? []),
|
|
},
|
|
}),
|
|
...(seq === undefined ? {} : {
|
|
sessionProjectionSeqs: {
|
|
...state.sessionProjectionSeqs,
|
|
[sessionId]: { ...state.sessionProjectionSeqs[sessionId], [key]: seq },
|
|
}
|
|
}),
|
|
}
|
|
}
|
|
const running = sessionRunningForMuxFrame(frame)
|
|
const seq = frame.payload.event?.seq
|
|
const previousLifecycle = sessionId === undefined ? undefined : state.sessionLifecycles[sessionId]
|
|
if (running !== undefined && seq !== undefined && previousLifecycle !== undefined && seq < previousLifecycle.seq) return {}
|
|
const releasePrompt = state.busyAction === 'send-message'
|
|
&& sessionId !== undefined
|
|
&& state.selectedSession?.sessionId === sessionId
|
|
&& (isHarnessPromptStarted(frame) || running !== undefined)
|
|
// Row status marks mirror the official session dots: a finished turn
|
|
// leaves a "done" reminder (green) until the session is opened; a failed
|
|
// turn leaves the red mark instead.
|
|
const turnReason = running === false && frame.payload.event?.type === 'turn/end'
|
|
? frame.payload.event.data.reason
|
|
: undefined
|
|
const finishedMark = running === undefined ? undefined
|
|
: running ? undefined
|
|
: isRecord(turnReason) && (turnReason.kind === 'failed' || turnReason.kind === 'error') ? 'failed' as const
|
|
: turnReason === undefined ? undefined : 'completed' as const
|
|
const updateRunning = (session: RemoteSession): RemoteSession => (
|
|
sessionId !== undefined && running !== undefined && session.sessionId === sessionId
|
|
? { ...session, running }
|
|
: session
|
|
)
|
|
return {
|
|
messages: applyMuxFrameToMessages(state.messages, frame),
|
|
...(sessionId === undefined || running === undefined ? {} : {
|
|
sessions: state.sessions.map(updateRunning),
|
|
selectedSession: state.selectedSession === undefined ? undefined : updateRunning(state.selectedSession),
|
|
...(seq === undefined ? {} : {
|
|
sessionLifecycles: {
|
|
...state.sessionLifecycles,
|
|
[sessionId]: {
|
|
seq,
|
|
running,
|
|
...(frame.payload.event?.data.turn === undefined ? {} : { turn: String(frame.payload.event.data.turn) }),
|
|
...(finishedMark === undefined ? {} : { finished: finishedMark }),
|
|
},
|
|
}
|
|
}),
|
|
}),
|
|
...(releasePrompt ? { busyAction: undefined } : {}),
|
|
}
|
|
})
|
|
},
|
|
|
|
handleCodexFrame(frame) {
|
|
get().handleCodexFrames([frame])
|
|
},
|
|
|
|
handleCodexFrames(frames) {
|
|
const timeline = runtime.activeCodexTimeline
|
|
if (timeline === undefined) return
|
|
let next = timeline
|
|
let framePermission: ReturnType<typeof codexPermissionPresetFromResponse>
|
|
for (const frame of frames) {
|
|
framePermission = codexPermissionPresetFromResponse(frame.params) ?? framePermission
|
|
next = reduceCodexTimelineFrame(next, frame)
|
|
}
|
|
// Unknown notifications (usage, heartbeats, etc.) do not invalidate the
|
|
// list. Publish an ordered burst once rather than synchronously per delta.
|
|
if (next === timeline && framePermission === undefined) return
|
|
runtime.activeCodexTimeline = next
|
|
const previousItems = new Map(timeline.items.map(item => [item.id, item]))
|
|
const live = codexItemsToChat(next.items.filter(item => previousItems.get(item.id) !== item))
|
|
set(state => {
|
|
const current = state.sessions.find(item => item.sessionId === next.session.id)
|
|
?? state.selectedSession
|
|
if (current === undefined || current.backend !== 'codex') return {}
|
|
const session = framePermission === undefined
|
|
? updateCodexSession(current, next.session)
|
|
: withCodexPermission(updateCodexSession(current, next.session), framePermission)
|
|
return {
|
|
sessions: state.sessions.map(item => item.sessionId === session.sessionId ? session : item),
|
|
selectedSession: state.selectedSession?.sessionId === session.sessionId ? session : state.selectedSession,
|
|
...(live.length === 0 ? {} : { messages: {
|
|
...state.messages,
|
|
[session.sessionId]: mergeCodexLive(state.messages[session.sessionId] ?? [], live),
|
|
} }),
|
|
}
|
|
})
|
|
},
|
|
|
|
handleCursorFrame(frame) {
|
|
const session = get().selectedSession
|
|
if (session === undefined || (session.backend !== 'cursor' && session.backend !== 'antigravity' && session.backend !== 'pi' && session.backend !== 'opencode' && session.backend !== 'claude' && session.backend !== 'hermes')) return
|
|
const params = isRecord(frame.frame.params) ? frame.frame.params : undefined
|
|
if (params?.sessionId !== undefined && params.sessionId !== cursorNativeId(session)) return
|
|
const update = params === undefined
|
|
? undefined
|
|
: (isRecord(params.update) ? params.update : params)
|
|
let kind: string | undefined
|
|
if (update !== undefined && typeof update.sessionUpdate === 'string') {
|
|
kind = update.sessionUpdate
|
|
const catchUpCount = Array.isArray(update.catchUp) ? update.catchUp.length : 0
|
|
// Diagnostic only: kind + catch-up size, never prompt or tool payloads.
|
|
console.info('[dsh-remote] acp frame:', session.backend, kind, catchUpCount > 0 ? `catchUp=${catchUpCount}` : '')
|
|
}
|
|
set(state => ({
|
|
messages: {
|
|
...state.messages,
|
|
[session.sessionId]: applyCursorFrame(state.messages[session.sessionId] ?? [], session.sessionId, frame),
|
|
},
|
|
...(kind === 'prompt_completed' || kind === 'prompt_failed'
|
|
? {
|
|
sessions: state.sessions.map(item => item.sessionId === session.sessionId ? { ...item, running: false } : item),
|
|
selectedSession: state.selectedSession?.sessionId === session.sessionId
|
|
? { ...state.selectedSession, running: false }
|
|
: state.selectedSession,
|
|
...(kind === 'prompt_failed'
|
|
? { error: session.backend === 'claude' ? zhCN.runtime.claudeUnavailable : session.backend === 'hermes' ? zhCN.runtime.hermesUnavailable : session.backend === 'opencode' ? zhCN.runtime.opencodeUnavailable : session.backend === 'pi' ? zhCN.runtime.piUnavailable : session.backend === 'antigravity' ? zhCN.runtime.antigravityUnavailable : zhCN.runtime.cursorUnavailable }
|
|
: {}),
|
|
}
|
|
: {}),
|
|
}))
|
|
if (session.backend === 'antigravity' && kind === 'prompt_completed') void runtime.refreshAntigravityCatalog()
|
|
if (session.backend === 'pi' && kind === 'prompt_completed') void runtime.refreshPiCatalog()
|
|
if (session.backend === 'opencode' && kind === 'prompt_completed') void runtime.refreshOpencodeCatalog()
|
|
if (session.backend === 'claude' && kind === 'prompt_completed') void runtime.refreshClaudeCatalog()
|
|
if (session.backend === 'hermes' && kind === 'prompt_completed') void runtime.refreshHermesCatalog()
|
|
},
|
|
}
|
|
}
|