Skip to content

Commit e2ec22f

Browse files
author
igardev
committed
setText command from parent window for llama-vscode now sends the message automatically.
1 parent 27aa259 commit e2ec22f

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

tools/server/webui/src/components/ChatScreen.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useMemo, useState } from 'react';
1+
import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
22
import { CallbackGeneratedChunk, useAppContext } from '../utils/app.context';
33
import ChatMessage from './ChatMessage';
44
import { CanvasType, Message, PendingMessage } from '../utils/types';
@@ -103,9 +103,7 @@ export default function ChatScreen() {
103103

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

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

110108
// keep track of leaf node for rendering
111109
const [currNodeId, setCurrNodeId] = useState<number>(-1);
@@ -132,7 +130,7 @@ export default function ChatScreen() {
132130
scrollToBottom(true);
133131
};
134132

135-
const sendNewMessage = async () => {
133+
const sendNewMessage = useCallback(async () => {
136134
const lastInpMsg = textarea.value();
137135
if (lastInpMsg.trim().length === 0 || isGenerating(currConvId ?? ''))
138136
return;
@@ -141,6 +139,8 @@ export default function ChatScreen() {
141139
setCurrNodeId(-1);
142140
// get the last message node
143141
const lastMsgNodeId = messages.at(-1)?.msg.id ?? null;
142+
// TODO: improve this when we have "upload file" feature
143+
const currExtra = extraContextRef.current ? [extraContextRef.current] : undefined;
144144
if (
145145
!(await sendMessage(
146146
currConvId,
@@ -155,7 +155,14 @@ export default function ChatScreen() {
155155
}
156156
// OK
157157
clearExtraContext();
158-
};
158+
}, [textarea, currConvId, isGenerating, messages, sendMessage, onChunk]);
159+
160+
const { extraContext, clearExtraContext } = useVSCodeContext(textarea,sendNewMessage);
161+
const extraContextRef = useRef(extraContext);
162+
163+
useEffect(() => {
164+
extraContextRef.current = extraContext;
165+
}, [extraContext]);
159166

160167
const handleEditMessage = async (msg: Message, content: string) => {
161168
if (!viewingChat) return;

tools/server/webui/src/utils/llama-vscode.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import {useEffect, useRef, useState} from 'react';
22
import { MessageExtraContext } from './types';
33
import { ChatTextareaApi } from '../components/useChatTextarea.ts';
44

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

18-
export const useVSCodeContext = (textarea: ChatTextareaApi) => {
19-
const [extraContext, setExtraContext] = useState<MessageExtraContext | null>(
20-
null
21-
);
18+
export const useVSCodeContext = (textarea: ChatTextareaApi, onSend?: () => void) => {
19+
const [extraContext, _setExtraContext] = useState<MessageExtraContext | null>(null);
2220

21+
// Use ref to store the latest value
22+
const extraContextRef = useRef(extraContext);
23+
24+
const setExtraContext = (value: MessageExtraContext | null) => {
25+
extraContextRef.current = value;
26+
_setExtraContext(value);
27+
};
2328
// Accept setText message from a parent window and set inputMsg and extraContext
2429
useEffect(() => {
2530
const handleMessage = (event: MessageEvent) => {
31+
if (event.source !== window.parent) return;
2632
if (event.data?.command === 'setText') {
2733
const data: SetTextEvData = event.data;
2834
textarea.setValue(data?.text);
@@ -33,12 +39,18 @@ export const useVSCodeContext = (textarea: ChatTextareaApi) => {
3339
});
3440
}
3541
textarea.focus();
42+
if (onSend && data?.text) {
43+
// Use setTimeout to ensure state updates are processed
44+
setTimeout(() => {
45+
onSend()
46+
}, 50);
47+
}
3648
}
3749
};
3850

3951
window.addEventListener('message', handleMessage);
4052
return () => window.removeEventListener('message', handleMessage);
41-
}, [textarea]);
53+
},[textarea, onSend]);
4254

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

0 commit comments

Comments
 (0)