dashboard chats: auto-load latest saved session for context on mount
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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<ChatSessionSelection | null>('chat:panel-session', null);
|
||||
const [selection, setSelection] = usePanelChannel<ChatSessionSelection | null>('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<string | null>('chat:active-session', null);
|
||||
|
||||
// Auto-load the latest saved session for this dashboard/project context
|
||||
const { sessions, resumeSession } = useSavedSessions();
|
||||
const autoLoadedRef = useRef<string | null>(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<number>('preview:refresh', 0);
|
||||
const [, setFilesRefresh] = usePanelChannel<number>('files:refresh-signal', 0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user