mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2025-11-15 11:17:31 +00:00
setText command from parent window for llama-vscode now sends the message automatically.
This commit is contained in:
@@ -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 { CallbackGeneratedChunk, useAppContext } from '../utils/app.context';
|
||||||
import ChatMessage from './ChatMessage';
|
import ChatMessage from './ChatMessage';
|
||||||
import { CanvasType, Message, PendingMessage } from '../utils/types';
|
import { CanvasType, Message, PendingMessage } from '../utils/types';
|
||||||
@@ -103,9 +103,7 @@ export default function ChatScreen() {
|
|||||||
|
|
||||||
const textarea: ChatTextareaApi = useChatTextarea(prefilledMsg.content());
|
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
|
// keep track of leaf node for rendering
|
||||||
const [currNodeId, setCurrNodeId] = useState<number>(-1);
|
const [currNodeId, setCurrNodeId] = useState<number>(-1);
|
||||||
@@ -132,7 +130,7 @@ export default function ChatScreen() {
|
|||||||
scrollToBottom(true);
|
scrollToBottom(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendNewMessage = async () => {
|
const sendNewMessage = useCallback(async () => {
|
||||||
const lastInpMsg = textarea.value();
|
const lastInpMsg = textarea.value();
|
||||||
if (lastInpMsg.trim().length === 0 || isGenerating(currConvId ?? ''))
|
if (lastInpMsg.trim().length === 0 || isGenerating(currConvId ?? ''))
|
||||||
return;
|
return;
|
||||||
@@ -141,6 +139,8 @@ export default function ChatScreen() {
|
|||||||
setCurrNodeId(-1);
|
setCurrNodeId(-1);
|
||||||
// get the last message node
|
// get the last message node
|
||||||
const lastMsgNodeId = messages.at(-1)?.msg.id ?? null;
|
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 (
|
if (
|
||||||
!(await sendMessage(
|
!(await sendMessage(
|
||||||
currConvId,
|
currConvId,
|
||||||
@@ -155,7 +155,14 @@ export default function ChatScreen() {
|
|||||||
}
|
}
|
||||||
// OK
|
// OK
|
||||||
clearExtraContext();
|
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) => {
|
const handleEditMessage = async (msg: Message, content: string) => {
|
||||||
if (!viewingChat) return;
|
if (!viewingChat) return;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import {useEffect, useRef, useState} from 'react';
|
||||||
import { MessageExtraContext } from './types';
|
import { MessageExtraContext } from './types';
|
||||||
import { ChatTextareaApi } from '../components/useChatTextarea.ts';
|
import { ChatTextareaApi } from '../components/useChatTextarea.ts';
|
||||||
|
|
||||||
@@ -15,14 +15,20 @@ interface SetTextEvData {
|
|||||||
* window.postMessage({ command: 'setText', text: 'Spot the syntax error', context: 'def test()\n return 123' }, '*');
|
* window.postMessage({ command: 'setText', text: 'Spot the syntax error', context: 'def test()\n return 123' }, '*');
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const useVSCodeContext = (textarea: ChatTextareaApi) => {
|
export const useVSCodeContext = (textarea: ChatTextareaApi, onSend?: () => void) => {
|
||||||
const [extraContext, setExtraContext] = useState<MessageExtraContext | null>(
|
const [extraContext, _setExtraContext] = useState<MessageExtraContext | null>(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
|
// Accept setText message from a parent window and set inputMsg and extraContext
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleMessage = (event: MessageEvent) => {
|
const handleMessage = (event: MessageEvent) => {
|
||||||
|
if (event.source !== window.parent) return;
|
||||||
if (event.data?.command === 'setText') {
|
if (event.data?.command === 'setText') {
|
||||||
const data: SetTextEvData = event.data;
|
const data: SetTextEvData = event.data;
|
||||||
textarea.setValue(data?.text);
|
textarea.setValue(data?.text);
|
||||||
@@ -33,12 +39,18 @@ export const useVSCodeContext = (textarea: ChatTextareaApi) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
textarea.focus();
|
textarea.focus();
|
||||||
|
if (onSend && data?.text) {
|
||||||
|
// Use setTimeout to ensure state updates are processed
|
||||||
|
setTimeout(() => {
|
||||||
|
onSend()
|
||||||
|
}, 50);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener('message', handleMessage);
|
window.addEventListener('message', handleMessage);
|
||||||
return () => window.removeEventListener('message', handleMessage);
|
return () => window.removeEventListener('message', handleMessage);
|
||||||
}, [textarea]);
|
},[textarea, onSend]);
|
||||||
|
|
||||||
// Add a keydown listener that sends the "escapePressed" message to the parent window
|
// Add a keydown listener that sends the "escapePressed" message to the parent window
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user