hide the ChatBox instead of unmount it to prevent lose chat history.

This commit is contained in:
Xiaohan-Tian
2025-08-12 19:29:32 -07:00
parent f9fc309c91
commit 3534dc7fca
3 changed files with 40 additions and 22 deletions
+3 -4
View File
@@ -61,15 +61,14 @@ function App() {
{/* Main Display Area containing MainContent, ChatBox, and Settings */} {/* Main Display Area containing MainContent, ChatBox, and Settings */}
<div className="main-display-area"> <div className="main-display-area">
{showSettings ? ( {showSettings && <SettingsPanel onClose={() => setShowSettings(false)} />}
<SettingsPanel onClose={() => setShowSettings(false)} /> {!showSettings && (
) : (
<> <>
{showInstrumentSelection && <InstrumentSelection />} {showInstrumentSelection && <InstrumentSelection />}
<MainContent /> <MainContent />
{showChatBox && <ChatBox />}
</> </>
)} )}
<ChatBox isVisible={showChatBox && !showSettings} />
</div> </div>
{/* Track Control */} {/* Track Control */}
+35 -16
View File
@@ -1,4 +1,4 @@
import React, { useState, useRef, useEffect, memo } from 'react'; import React, { useState, useRef, useEffect, memo, useCallback } from 'react';
import { FaPlus, FaBan } from 'react-icons/fa'; import { FaPlus, FaBan } from 'react-icons/fa';
import { UserMessage, AssistantMessage } from './chat'; import { UserMessage, AssistantMessage } from './chat';
import { AgentCore } from '../agent/core/AgentCore'; import { AgentCore } from '../agent/core/AgentCore';
@@ -44,7 +44,11 @@ const createLLMProvider = (): LLMProvider => {
} }
}; };
const ChatBox: React.FC = () => { interface ChatBoxProps {
isVisible: boolean;
}
const ChatBox: React.FC<ChatBoxProps> = ({ isVisible }) => {
const [inputValue, setInputValue] = useState(''); const [inputValue, setInputValue] = useState('');
const textareaRef = useRef<HTMLTextAreaElement>(null); const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -62,6 +66,33 @@ const ChatBox: React.FC = () => {
// Track if this is the first message (for system prompt logging) // Track if this is the first message (for system prompt logging)
const [isFirstMessage, setIsFirstMessage] = useState(true); const [isFirstMessage, setIsFirstMessage] = useState(true);
const generateMessageId = (): string => {
return `msg_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
};
const clearChatUI = useCallback(async () => {
// Clear UI state
setMessages([]);
// Reset first message flag so system prompt will be logged again
setIsFirstMessage(true);
// Auto-show welcome message after clearing (like on app startup)
try {
const result = await processUserMessage('/welcome');
if (result.pseudoAssistantResponse) {
const pseudoId = generateMessageId();
setMessages(prev => [...prev, {
id: pseudoId,
role: 'assistant',
content: result.pseudoAssistantResponse!,
}]);
}
} catch {
// ignore errors, just don't show welcome if it fails
}
}, []);
// Initialize AgentCore with configured provider and register clear UI callback // Initialize AgentCore with configured provider and register clear UI callback
useEffect(() => { useEffect(() => {
const initializeProvider = async () => { const initializeProvider = async () => {
@@ -103,11 +134,7 @@ const ChatBox: React.FC = () => {
// ignore // ignore
} }
})(); })();
}, []); }, [clearChatUI]);
const generateMessageId = (): string => {
return `msg_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
};
const handleAbort = () => { const handleAbort = () => {
if (abortController) { if (abortController) {
@@ -128,14 +155,6 @@ const ChatBox: React.FC = () => {
} }
}; };
const clearChatUI = () => {
// Clear UI state
setMessages([]);
// Reset first message flag so system prompt will be logged again
setIsFirstMessage(true);
};
const handleClearCommand = () => { const handleClearCommand = () => {
const { setStatus } = useProjectStore.getState(); const { setStatus } = useProjectStore.getState();
clearChatHistoryAndUI(setStatus); clearChatHistoryAndUI(setStatus);
@@ -519,7 +538,7 @@ const ChatBox: React.FC = () => {
}, [isProcessing, isExecutingTools]); }, [isProcessing, isExecutingTools]);
return ( return (
<div className="chatbox"> <div className="chatbox" style={{ display: isVisible ? 'flex' : 'none' }}>
<div className="chatbox-header"> <div className="chatbox-header">
<h3>K.G.Studio Musician Assistant</h3> <h3>K.G.Studio Musician Assistant</h3>
<div className="chatbox-actions"> <div className="chatbox-actions">
+2 -2
View File
@@ -25,13 +25,13 @@ export const clearChatHistoryWithStatus = (setStatus?: (message: string) => void
}; };
// Global callback for clearing UI state // Global callback for clearing UI state
let globalClearChatUI: (() => void) | null = null; let globalClearChatUI: (() => void | Promise<void>) | null = null;
/** /**
* Register a callback to clear chat UI state * Register a callback to clear chat UI state
* This allows external components to clear the ChatBox UI * This allows external components to clear the ChatBox UI
*/ */
export const registerClearChatUICallback = (callback: () => void) => { export const registerClearChatUICallback = (callback: () => void | Promise<void>) => {
globalClearChatUI = callback; globalClearChatUI = callback;
}; };