Canonicalize session message/part materialization across load, prefetch, reconnect, and recovery paths so OpenChamber restores session snapshots through one consistent merge flow. Preserve live assistant streaming text when stale or delayed snapshots arrive, while still replacing optimistic user parts with confirmed server snapshots to avoid duplicated user messages. Narrow recovery triggers to explicit incomplete snapshot signals instead of broad session-event fallbacks, reducing unnecessary session refetches during active streaming. Keep turn windowing aligned with parented assistant replies and add regression coverage for materialization gaps, stale snapshot protection, optimistic user replacement, reconnect recovery, and turn grouping.
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import type { SessionStatus, Message, Part } from "@opencode-ai/sdk/v2/client"
|
|
import type { Session } from "@opencode-ai/sdk/v2"
|
|
import { getSessionMaterializationStatus } from "./materialization"
|
|
|
|
type ReconnectMaterializationState = {
|
|
session: Session[]
|
|
session_status?: Record<string, SessionStatus>
|
|
message?: Record<string, Message[]>
|
|
part?: Record<string, Part[]>
|
|
}
|
|
|
|
export type ViewedSessionMaterializationTarget = {
|
|
directory: string
|
|
sessionId: string
|
|
}
|
|
|
|
type ReconnectCandidateOptions = {
|
|
directory?: string
|
|
viewedSession?: ViewedSessionMaterializationTarget | null
|
|
}
|
|
|
|
export function getReconnectCandidateSessionIds(state: ReconnectMaterializationState, options?: ReconnectCandidateOptions) {
|
|
const ids = new Set<string>()
|
|
|
|
for (const [sessionId, status] of Object.entries(state.session_status ?? {})) {
|
|
if (status && status.type !== "idle") ids.add(sessionId)
|
|
}
|
|
|
|
for (const [sessionId, messages] of Object.entries(state.message ?? {})) {
|
|
const lastMessage = messages[messages.length - 1]
|
|
if (
|
|
lastMessage
|
|
&& lastMessage.role === "assistant"
|
|
&& typeof (lastMessage as { time?: { completed?: number } }).time?.completed !== "number"
|
|
) {
|
|
ids.add(sessionId)
|
|
} else if (!getSessionMaterializationStatus({ message: state.message ?? {}, part: state.part ?? {} }, sessionId).renderable) {
|
|
ids.add(sessionId)
|
|
}
|
|
}
|
|
|
|
const parentIds = new Set<string>()
|
|
for (const session of state.session) {
|
|
const parentId = (session as Session & { parentID?: string | null }).parentID
|
|
if (parentId) {
|
|
parentIds.add(parentId)
|
|
}
|
|
}
|
|
for (const pid of parentIds) {
|
|
ids.add(pid)
|
|
}
|
|
|
|
const viewedSession = options?.viewedSession
|
|
if (viewedSession?.sessionId && viewedSession.directory === options?.directory) {
|
|
const sessionId = viewedSession.sessionId
|
|
const sessionExists = state.session.some((session) => session.id === sessionId)
|
|
|| Object.hasOwn(state.session_status ?? {}, sessionId)
|
|
|| Object.hasOwn(state.message ?? {}, sessionId)
|
|
|
|
if (sessionExists) {
|
|
ids.add(sessionId)
|
|
}
|
|
}
|
|
|
|
return Array.from(ids)
|
|
}
|