Skip to content

Commit

Permalink
scroll down after tool output bubbles
Browse files Browse the repository at this point in the history
  • Loading branch information
lily-de committed Dec 19, 2024
1 parent ba19e4e commit d942845
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions ui/desktop/src/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ function ChatContent({
const [lastInteractionTime, setLastInteractionTime] = useState<number>(Date.now());
const [showGame, setShowGame] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
const [working, setWorkingLocal] = useState<Working>(Working.Idle);

useEffect(() => {
setWorking(working);
}, [working, setWorking]);

const updateWorking = (newWorking: Working) => {
setWorkingLocal(newWorking);
};

const {
messages,
Expand All @@ -70,28 +79,30 @@ function ChatContent({
api: getApiUrl('/reply'),
initialMessages: chat?.messages || [],
onToolCall: ({ toolCall }) => {
setWorking(Working.Working);
updateWorking(Working.Working);
setProgressMessage(`Executing tool: ${toolCall.toolName}`);
requestAnimationFrame(() => scrollToBottom('instant'));

Check failure on line 84 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined

Check failure on line 84 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined
},
onResponse: (response) => {
if (!response.ok) {
setProgressMessage('An error occurred while receiving the response.');
setWorking(Working.Idle);
updateWorking(Working.Idle);
} else {
setProgressMessage('thinking...');
setWorking(Working.Working);
updateWorking(Working.Working);
}
},
onFinish: async (message, options) => {

Check warning on line 95 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'options' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 95 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'options' is defined but never used. Allowed unused args must match /^_/u
setTimeout(() => {
setProgressMessage('Task finished. Click here to expand.');
setWorking(Working.Idle);
updateWorking(Working.Idle);
}, 500);

const fetchResponses = await askAi(message.content);
setMessageMetadata((prev) => ({ ...prev, [message.id]: fetchResponses }));

// Only show notification if it's been more than a minute since last interaction
requestAnimationFrame(() => scrollToBottom('smooth'));

Check failure on line 104 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined

Check failure on line 104 in ui/desktop/src/ChatWindow.tsx

View workflow job for this annotation

GitHub Actions / build

'requestAnimationFrame' is not defined

const timeSinceLastInteraction = Date.now() - lastInteractionTime;
window.electron.logInfo("last interaction:" + lastInteractionTime);
if (timeSinceLastInteraction > 60000) { // 60000ms = 1 minute
Expand Down Expand Up @@ -135,16 +146,16 @@ function ChatContent({

// Single effect to handle all scrolling
useEffect(() => {
if (isLoading || messages.length > 0) {
if (isLoading || messages.length > 0 || working === Working.Working) {
// Initial scroll
scrollToBottom(isLoading ? 'instant' : 'smooth');
scrollToBottom(isLoading || working === Working.Working ? 'instant' : 'smooth');

// Additional scrolls to catch dynamic content
[100, 300, 500].forEach(delay => {
setTimeout(() => scrollToBottom('smooth'), delay);
});
}
}, [messages, isLoading]);
}, [messages, isLoading, working]);

// Handle submit
const handleSubmit = (e: React.FormEvent) => {
Expand Down

0 comments on commit d942845

Please sign in to comment.