fix: show streaming chat messages after startup

Read chat state from the active session directory
Align status rows with the active session
This commit is contained in:
Bohdan Triapitsyn
2026-06-03 22:38:15 +03:00
parent 04b1425c2e
commit 04c9aa57dd
4 changed files with 49 additions and 23 deletions
+24 -8
View File
@@ -199,6 +199,7 @@ export type SessionHistoryMeta = {
export type SessionUIState = {
currentSessionId: string | null
currentSessionDirectory: string | null
newSessionDraft: NewSessionDraftState
abortPromptSessionId: string | null
abortPromptExpiresAt: number | null
@@ -344,6 +345,10 @@ const resolveSessionDirectory = (
if (attachmentDirectory) return attachmentDirectory
const metaPath = getWtMeta(sessionId)?.path
if (typeof metaPath === "string" && metaPath.trim().length > 0) return normalizePath(metaPath)
const runtimeMemory = runtimeSessionMemory.get(runtimeMemoryKey())
if (runtimeMemory?.sessionId === sessionId && runtimeMemory.directory) {
return normalizePath(runtimeMemory.directory)
}
const sessions = getAllSyncSessions()
const target = sessions.find((s) => s.id === sessionId)
if (!target) return null
@@ -391,6 +396,7 @@ const writeRuntimeSessionMemory = (key: string, patch: Partial<RuntimeSessionMem
export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
currentSessionId: null,
currentSessionDirectory: null,
newSessionDraft: { ...DEFAULT_DRAFT },
abortPromptSessionId: null,
abortPromptExpiresAt: null,
@@ -418,10 +424,6 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
activeSessionByRuntime.set(key, id)
const previousSessionId = get().currentSessionId
// Set currentSessionId immediately so the skeleton renders without delay.
set({ currentSessionId: id })
const directoryState = useDirectoryStore.getState()
const sessionDir = resolveSessionDirectory(
@@ -430,6 +432,10 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
)
const fallbackDir = opencodeClient.getDirectory() ?? directoryState.currentDirectory ?? null
const resolvedDir = (directoryHint ? normalizePath(directoryHint) : null) ?? sessionDir ?? fallbackDir
// Set the directory together with the session id so chat hooks read the
// same child store that send/SSE events will update during startup races.
set({ currentSessionId: id, currentSessionDirectory: id ? resolvedDir ?? null : null })
writeRuntimeSessionMemory(key, { sessionId: id, directory: resolvedDir ?? null })
try {
@@ -493,6 +499,7 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
}
set({
currentSessionId: restoredSessionId,
currentSessionDirectory: restoredSessionId ? restoredDirectory : null,
newSessionDraft: restoredSessionId ? { ...DEFAULT_DRAFT } : restoredDraft,
abortPromptSessionId: null,
abortPromptExpiresAt: null,
@@ -501,7 +508,7 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
pendingChangesBarDismissed: new Map(),
})
if (restoredSessionId) {
setActiveSession(opencodeClient.getDirectory() ?? "", restoredSessionId)
setActiveSession(restoredDirectory ?? opencodeClient.getDirectory() ?? "", restoredSessionId)
} else {
setActiveSession("", "")
}
@@ -575,6 +582,7 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
...nextDraft,
},
currentSessionId: null,
currentSessionDirectory: null,
error: null,
})
@@ -1225,6 +1233,11 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
},
getDirectoryForSession: (sessionId) => {
if (sessionId === get().currentSessionId && get().currentSessionDirectory) {
return get().currentSessionDirectory
}
const resolved = resolveSessionDirectory(sessionId, (sid) => get().worktreeMetadata.get(sid))
if (resolved) return resolved
const attachmentDirectory = getAttachedSessionDirectory(getAttachmentForSession(sessionId))
if (attachmentDirectory) return attachmentDirectory
const sessions = getAllSyncSessions()
@@ -1292,9 +1305,12 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({
// Handled by sync system's SSE stream
},
setSessionDirectory: () => {
// Session directory is owned by sync child stores via SSE events.
// This is now a no-op — kept for interface compatibility during migration.
setSessionDirectory: (sessionId, directory) => {
const normalized = normalizePath(directory)
if (sessionId === get().currentSessionId) {
set({ currentSessionDirectory: normalized })
writeRuntimeSessionMemory(runtimeMemoryKey(), { sessionId, directory: normalized })
}
},
// ---------------------------------------------------------------------------