Import apps/android/src/state/session-actions.ts
This commit is contained in:
parent
e26cde2617
commit
e7d0e1db42
|
|
@ -42,7 +42,7 @@ import type { AppState } from './store-state'
|
|||
|
||||
/** Session actions use the store's single connection/session runtime. */
|
||||
export function createSessionActions({ get, set, runtime }: SessionAccess): Pick<AppState,
|
||||
'refreshSessionStatus' | 'openSession' | 'forkChatMessage' | 'quoteChatMessage' | 'rateMessage' | 'createSession' | 'forkSession' | 'archiveSession' | 'unarchiveSession' | 'pinSession' | 'unpinSession' | 'renameSession' | 'selectModel' | 'selectPermission' | 'loadAgentPresets' | 'selectAgentPreset' | 'loadOlderHistory'> {
|
||||
'refreshSessionStatus' | 'syncHarnessCatalog' | 'openSession' | 'forkChatMessage' | 'quoteChatMessage' | 'rateMessage' | 'createSession' | 'forkSession' | 'archiveSession' | 'unarchiveSession' | 'pinSession' | 'unpinSession' | 'renameSession' | 'selectModel' | 'selectPermission' | 'loadAgentPresets' | 'selectAgentPreset' | 'loadOlderHistory'> {
|
||||
const isHarnessBackend = (backend?: RemoteSession['backend']) =>
|
||||
(backend ?? 'harness') === 'harness'
|
||||
|
||||
|
|
@ -53,6 +53,7 @@ export function createSessionActions({ get, set, runtime }: SessionAccess): Pick
|
|||
}
|
||||
|
||||
return {
|
||||
/** Pull full `session/list` after a real catalog-change hint (or manual refresh). */
|
||||
async refreshSessionStatus() {
|
||||
if (get().connection.phase !== 'connected') return
|
||||
try {
|
||||
|
|
@ -89,8 +90,8 @@ export function createSessionActions({ get, set, runtime }: SessionAccess): Pick
|
|||
const selectedFresh = state.selectedSession === undefined
|
||||
? undefined
|
||||
: rows.find(item => item.sessionId === state.selectedSession?.sessionId)
|
||||
// Preserve array identity when nothing moved: the 8s refresh otherwise
|
||||
// hands consumers a fresh `sessions` reference and re-runs their memos.
|
||||
// Preserve array identity when nothing moved so consumers do not
|
||||
// re-run memos on a no-op explicit refresh.
|
||||
return {
|
||||
sessionLifecycles: marks,
|
||||
...(changed || added.length > 0
|
||||
|
|
@ -105,6 +106,63 @@ export function createSessionActions({ get, set, runtime }: SessionAccess): Pick
|
|||
} catch { /* status refresh is best-effort */ }
|
||||
},
|
||||
|
||||
/** Pull Harness workspaces + sessions after an event-driven membership change. */
|
||||
async syncHarnessCatalog() {
|
||||
if (get().connection.phase !== 'connected') return
|
||||
const generation = runtime.connectionGeneration
|
||||
const deviceId = get().selectedDevice?.deviceId
|
||||
const { config, identity } = get()
|
||||
let proxy: ReturnType<typeof runtime.connection.requireProxy>
|
||||
try {
|
||||
proxy = runtime.connection.requireProxy()
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
const current = () => generation === runtime.connectionGeneration
|
||||
&& get().config === config
|
||||
&& get().identity === identity
|
||||
&& get().connection.phase === 'connected'
|
||||
&& get().selectedDevice?.deviceId === deviceId
|
||||
&& (() => { try { return runtime.connection.requireProxy() === proxy } catch { return false } })()
|
||||
try {
|
||||
const [workspaces, rows] = await Promise.all([proxy.workspaceList(), proxy.sessionList()])
|
||||
if (!current()) return
|
||||
set(state => {
|
||||
const byId = new Map(rows.map(item => [item.sessionId, item]))
|
||||
const marks: typeof state.sessionLifecycles = { ...state.sessionLifecycles }
|
||||
for (const row of state.sessions) {
|
||||
if ((row.backend ?? 'harness') !== 'harness') continue
|
||||
const fresh = byId.get(row.sessionId)
|
||||
if (fresh === undefined) continue
|
||||
const entry = marks[row.sessionId]
|
||||
const wasRunning = entry?.running ?? row.running
|
||||
if (wasRunning === fresh.running) continue
|
||||
marks[row.sessionId] = {
|
||||
seq: entry?.seq ?? -1,
|
||||
turn: entry?.turn,
|
||||
running: fresh.running,
|
||||
...(fresh.running
|
||||
? { finished: undefined }
|
||||
: { finished: entry?.finished === 'failed' ? 'failed' as const : 'completed' as const }),
|
||||
}
|
||||
}
|
||||
const nonHarnessWorkspaces = state.workspaces.filter(item => (item.backend ?? 'harness') !== 'harness')
|
||||
const nonHarnessSessions = state.sessions.filter(item => (item.backend ?? 'harness') !== 'harness')
|
||||
const selectedFresh = state.selectedSession === undefined
|
||||
? undefined
|
||||
: byId.get(state.selectedSession.sessionId)
|
||||
return {
|
||||
sessionLifecycles: marks,
|
||||
workspaces: [...nonHarnessWorkspaces, ...workspaces.items],
|
||||
sessions: [...nonHarnessSessions, ...rows],
|
||||
archivedSessionIds: workspaces.archivedSessionIds,
|
||||
...(workspaces.pinnedSessionIds === undefined ? {} : { pinnedSessionIds: workspaces.pinnedSessionIds }),
|
||||
...(selectedFresh === undefined ? {} : { selectedSession: selectedFresh }),
|
||||
}
|
||||
})
|
||||
} catch { /* background catalog sync is best-effort */ }
|
||||
},
|
||||
|
||||
async openSession(session) {
|
||||
runtime.flushPendingMuxFrames()
|
||||
runtime.flushPendingCodexFrames()
|
||||
|
|
@ -517,6 +575,21 @@ export function createSessionActions({ get, set, runtime }: SessionAccess): Pick
|
|||
try {
|
||||
const workspace = workspaceId === undefined ? undefined : get().workspaces.find(item => item.workspaceId === workspaceId)
|
||||
const backend = targetBackend ?? workspace?.backend ?? 'harness'
|
||||
// Only an explicitly blank, idle conversation in this workspace is
|
||||
// reusable. Titles are not evidence that a conversation is empty.
|
||||
const state = get()
|
||||
const reusable = workspace === undefined ? undefined : state.sessions
|
||||
.filter(item => workspace.sessionIds.includes(item.sessionId)
|
||||
&& (item.backend ?? 'harness') === backend
|
||||
&& item.blank === true && !item.running
|
||||
&& !state.sessionLifecycles[item.sessionId]?.running
|
||||
&& !isSubagentSession(item)
|
||||
&& !state.archivedSessionIds.includes(item.sessionId)
|
||||
&& !(state.messages[item.sessionId]?.length)
|
||||
&& !(state.selectedSession?.sessionId === item.sessionId
|
||||
&& (state.selectedSession.blank === false || state.selectedSession.running)))
|
||||
.sort((a, b) => b.updatedAt - a.updatedAt)[0]
|
||||
if (reusable !== undefined) return await get().openSession(reusable)
|
||||
const existingPath = workspace?.path || get().workspaces.find(w => !!w.path)?.path
|
||||
const isWin = get().selectedDevice?.platform === 'windows'
|
||||
const cwd = existingPath || (isWin ? 'C:\\' : '/')
|
||||
|
|
|
|||
Loading…
Reference in New Issue