From 0e0be3327e71e239911f6ad8c59cf230c589237c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Padez?= Date: Fri, 6 Mar 2026 20:46:09 +0000 Subject: [PATCH] dashboard chats: auto-load latest saved session for context on mount Co-Authored-By: Claude Opus 4.6 --- .../src/apps/Chat/ChatPanelWrapper.tsx | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/workspaces/officerdev/src/apps/Chat/ChatPanelWrapper.tsx b/src/workspaces/officerdev/src/apps/Chat/ChatPanelWrapper.tsx index 8d35b074..070f476b 100644 --- a/src/workspaces/officerdev/src/apps/Chat/ChatPanelWrapper.tsx +++ b/src/workspaces/officerdev/src/apps/Chat/ChatPanelWrapper.tsx @@ -1,7 +1,7 @@ import { useCallback, useEffect, useRef, useState } from 'react'; import { useWorkspace } from '../../components/Workspace'; import { usePanelChannel } from 'hooks/usePanelChannel'; -import { useSavedSessions } from 'state/useSavedSessions'; +import { useSavedSessions, messagesToTranscript, type RawMessage } from 'state/useSavedSessions'; import { usePiChat } from '../../hooks/usePiChat'; import { EmbeddableChat } from './EmbeddableChat'; @@ -96,7 +96,7 @@ export const ChatPanelWrapper = () => { ? { context: 'dashboard' as const, contextId: dashboardId } : {}; - const [selection] = usePanelChannel('chat:panel-session', null); + const [selection, setSelection] = usePanelChannel('chat:panel-session', null); const [resumeKey, setResumeKey] = useState(0); const prevSelectionRef = useRef(selection); if (selection !== prevSelectionRef.current) { @@ -104,6 +104,43 @@ export const ChatPanelWrapper = () => { if (selection?.resumeSummary) setResumeKey((k) => k + 1); } const [, setActiveSession] = usePanelChannel('chat:active-session', null); + + // Auto-load the latest saved session for this dashboard/project context + const { sessions, resumeSession } = useSavedSessions(); + const autoLoadedRef = useRef(null); + const contextId = 'contextId' in chatContext ? chatContext.contextId : undefined; + useEffect(() => { + if (selection || !contextId || autoLoadedRef.current === contextId) return; + const latest = sessions.find((s) => s.contextId === contextId); + if (!latest) return; + autoLoadedRef.current = contextId; + resumeSession(latest.id).then((result) => { + const rawMessages = result.rawMessages ?? []; + const chatMessages: ChatMessage[] = rawMessages.map((m: RawMessage) => { + if (m.role === 'user') return { role: 'user' as const, text: m.text || '' }; + if (m.role === 'assistant') return { role: 'assistant' as const, id: m.id, text: m.text || '' }; + if (m.role === 'tool') { + return { + role: 'tool' as const, + toolName: m.toolName || '', + toolInput: m.toolInput || {}, + toolCallId: m.toolCallId || '', + output: m.output, + isError: m.isError, + }; + } + return { role: 'assistant' as const, text: '' }; + }); + const transcript = messagesToTranscript(rawMessages); + setSelection({ + sessionId: null, + model: result.model, + dashboardId: dashboardId ?? undefined, + resumeSummary: transcript, + initialMessages: chatMessages, + }); + }); + }, [selection, contextId, sessions]); const [, setPreviewRefresh] = usePanelChannel('preview:refresh', 0); const [, setFilesRefresh] = usePanelChannel('files:refresh-signal', 0);