Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified tools/server/public/index.html.gz
Binary file not shown.
24 changes: 17 additions & 7 deletions tools/server/webui/src/components/ChatScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { CallbackGeneratedChunk, useAppContext } from '../utils/app.context';
import ChatMessage from './ChatMessage';
import { CanvasType, Message, PendingMessage } from '../utils/types';
Expand Down Expand Up @@ -103,10 +103,6 @@ export default function ChatScreen() {

const textarea: ChatTextareaApi = useChatTextarea(prefilledMsg.content());

const { extraContext, clearExtraContext } = useVSCodeContext(textarea);
// TODO: improve this when we have "upload file" feature
const currExtra: Message['extra'] = extraContext ? [extraContext] : undefined;

// keep track of leaf node for rendering
const [currNodeId, setCurrNodeId] = useState<number>(-1);
const messages: MessageDisplay[] = useMemo(() => {
Expand All @@ -132,7 +128,7 @@ export default function ChatScreen() {
scrollToBottom(true);
};

const sendNewMessage = async () => {
const sendNewMessage = useCallback(async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need useCallback here. We can simply update textarea.onSubmit = sendNewMessage each time

const lastInpMsg = textarea.value();
if (lastInpMsg.trim().length === 0 || isGenerating(currConvId ?? ''))
return;
Expand All @@ -141,6 +137,10 @@ export default function ChatScreen() {
setCurrNodeId(-1);
// get the last message node
const lastMsgNodeId = messages.at(-1)?.msg.id ?? null;
// TODO: improve this when we have "upload file" feature
const currExtra = extraContextRef.current
? [extraContextRef.current]
: undefined;
if (
!(await sendMessage(
currConvId,
Expand All @@ -155,7 +155,17 @@ export default function ChatScreen() {
}
// OK
clearExtraContext();
};
}, [textarea, currConvId, isGenerating, messages, sendMessage, onChunk]);

const { extraContext, clearExtraContext } = useVSCodeContext(
textarea,
sendNewMessage
);
const extraContextRef = useRef(extraContext);

useEffect(() => {
extraContextRef.current = extraContext;
}, [extraContext]);

const handleEditMessage = async (msg: Message, content: string) => {
if (!viewingChat) return;
Expand Down
25 changes: 21 additions & 4 deletions tools/server/webui/src/utils/llama-vscode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { MessageExtraContext } from './types';
import { ChatTextareaApi } from '../components/useChatTextarea.ts';

Expand All @@ -15,14 +15,25 @@ interface SetTextEvData {
* window.postMessage({ command: 'setText', text: 'Spot the syntax error', context: 'def test()\n return 123' }, '*');
*/

export const useVSCodeContext = (textarea: ChatTextareaApi) => {
const [extraContext, setExtraContext] = useState<MessageExtraContext | null>(
export const useVSCodeContext = (
textarea: ChatTextareaApi,
onSend?: () => void
) => {
const [extraContext, _setExtraContext] = useState<MessageExtraContext | null>(
null
);

// Use ref to store the latest value
const extraContextRef = useRef(extraContext);

const setExtraContext = (value: MessageExtraContext | null) => {
extraContextRef.current = value;
_setExtraContext(value);
};
// Accept setText message from a parent window and set inputMsg and extraContext
useEffect(() => {
const handleMessage = (event: MessageEvent) => {
if (event.source !== window.parent) return;
if (event.data?.command === 'setText') {
const data: SetTextEvData = event.data;
textarea.setValue(data?.text);
Expand All @@ -33,12 +44,18 @@ export const useVSCodeContext = (textarea: ChatTextareaApi) => {
});
}
textarea.focus();
if (onSend && data?.text) {
// Use setTimeout to ensure state updates are processed
setTimeout(() => {
onSend();
}, 50);
}
Copy link
Collaborator

@ngxson ngxson May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I don't quite like this approach because it makes the logic looks kinda circular dependency.

A cleaner approach is to simply extend useChatTextarea to temporary hold a callback let's say textarea.onSubmit, then call it here:

Suggested change
textarea.focus();
if (onSend && data?.text) {
// Use setTimeout to ensure state updates are processed
setTimeout(() => {
onSend();
}, 50);
}
textarea.focus();
textarea.onSubmit();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It is cleaner.

}
};

window.addEventListener('message', handleMessage);
return () => window.removeEventListener('message', handleMessage);
}, [textarea]);
}, [textarea, onSend]);

// Add a keydown listener that sends the "escapePressed" message to the parent window
useEffect(() => {
Expand Down