Skip to content

Commit 7af0ac8

Browse files
author
poulphunter
committed
https://github.com/ggml-org/llama.cpp/pull/12148
1 parent f55fd28 commit 7af0ac8

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/components/ChatScreen.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { 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';
5-
import { classNames, throttle } from '../utils/misc';
5+
import { classNames, cleanCurrentUrl, throttle } from '../utils/misc';
66
import CanvasPyInterpreter from './CanvasPyInterpreter';
77
import StorageUtils from '../utils/storage';
88
import { useVSCodeContext } from '../utils/llama-vscode';
@@ -19,6 +19,24 @@ export interface MessageDisplay {
1919
isPending?: boolean;
2020
}
2121

22+
/**
23+
* If the current URL contains "?m=...", prefill the message input with the value.
24+
* If the current URL contains "?q=...", prefill and SEND the message.
25+
*/
26+
const prefilledMsg = {
27+
content() {
28+
const url = new URL(window.location.href);
29+
return url.searchParams.get('m') ?? url.searchParams.get('q') ?? '';
30+
},
31+
shouldSend() {
32+
const url = new URL(window.location.href);
33+
return url.searchParams.has('q');
34+
},
35+
clear() {
36+
cleanCurrentUrl(['m', 'q']);
37+
},
38+
};
39+
2240
function getListMessageDisplay(
2341
msgs: Readonly<Message[]>,
2442
leafNodeId: Message['id']
@@ -83,7 +101,7 @@ export default function ChatScreen() {
83101
settingsSeed,
84102
} = useAppContext();
85103
const { t } = useTranslation();
86-
const [inputMsg, setInputMsg] = useState('');
104+
const [inputMsg, setInputMsg] = useState(prefilledMsg.content());
87105
const [automaticSend, setAutomaticSend] = useState(false);
88106
const inputRef = useRef<HTMLTextAreaElement>(null);
89107
const { config } = useAppContext();
@@ -209,6 +227,20 @@ export default function ChatScreen() {
209227

210228
const hasCanvas = !!canvasData;
211229

230+
useEffect(() => {
231+
if (prefilledMsg.shouldSend()) {
232+
// send the prefilled message if needed
233+
sendNewMessage().then(() => {});
234+
} else if (inputRef.current) {
235+
// otherwise, focus on the input and move the cursor to the end
236+
inputRef.current.focus();
237+
inputRef.current.selectionStart = inputRef.current.value.length;
238+
}
239+
prefilledMsg.clear();
240+
// no need to keep track of sendNewMessage
241+
// eslint-disable-next-line react-hooks/exhaustive-deps
242+
}, [inputRef]);
243+
212244
// due to some timing issues of StorageUtils.appendMsg(), we need to make sure the pendingMsg is not duplicated upon rendering (i.e. appears once in the saved conversation and once in the pendingMsg)
213245
const pendingMsgDisplay: MessageDisplay[] =
214246
pendingMsg && messages.at(-1)?.msg.id !== pendingMsg.id

src/utils/misc.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const isBoolean = (x: any) => x === true || x === false;
1212
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1313
export const isNumeric = (n: any) =>
1414
!isString(n) && !isNaN(n) && !isBoolean(n) && !Array.isArray(n);
15+
1516
// wrapper for SSE
1617
export async function* getSSEStreamAsync(fetchResponse: Response) {
1718
if (!fetchResponse.body) throw new Error('Response body is empty');
@@ -93,6 +94,7 @@ export function classNames(classes: Record<string, boolean>): string {
9394
.map(([key]) => key)
9495
.join(' ');
9596
}
97+
9698
export const throttle = <T extends unknown[]>(
9799
callback: (...args: T) => void,
98100
delay: number
@@ -112,3 +114,10 @@ export const throttle = <T extends unknown[]>(
112114
}, delay);
113115
};
114116
};
117+
export const cleanCurrentUrl = (removeQueryParams: string[]) => {
118+
const url = new URL(window.location.href);
119+
removeQueryParams.forEach((param) => {
120+
url.searchParams.delete(param);
121+
});
122+
window.history.replaceState({}, '', url.toString());
123+
};

0 commit comments

Comments
 (0)