Skip to content

Commit ce91456

Browse files
committed
webui : add ?m=... and ?q=... params
1 parent 14dec0c commit ce91456

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed
153 Bytes
Binary file not shown.

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

Lines changed: 32 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';
@@ -18,6 +18,20 @@ export interface MessageDisplay {
1818
isPending?: boolean;
1919
}
2020

21+
/**
22+
* If the current URL contains "?m=...", prefill the message input with the value.
23+
* If the current URL contains "?p=...", prefill and SEND the message.
24+
*/
25+
let prefilledMessage = '';
26+
let sendPrefilledMessage = false;
27+
{
28+
const url = new URL(window.location.href);
29+
prefilledMessage =
30+
url.searchParams.get('m') ?? url.searchParams.get('p') ?? '';
31+
sendPrefilledMessage = url.searchParams.has('p');
32+
cleanCurrentUrl(['m', 'p']);
33+
}
34+
2135
function getListMessageDisplay(
2236
msgs: Readonly<Message[]>,
2337
leafNodeId: Message['id']
@@ -81,7 +95,7 @@ export default function ChatScreen() {
8195
canvasData,
8296
replaceMessageAndGenerate,
8397
} = useAppContext();
84-
const [inputMsg, setInputMsg] = useState('');
98+
const [inputMsg, setInputMsg] = useState(prefilledMessage);
8599
const inputRef = useRef<HTMLTextAreaElement>(null);
86100

87101
const { extraContext, clearExtraContext } = useVSCodeContext(
@@ -172,6 +186,22 @@ export default function ChatScreen() {
172186

173187
const hasCanvas = !!canvasData;
174188

189+
useEffect(() => {
190+
if (sendPrefilledMessage) {
191+
// send the prefilled message if needed
192+
sendPrefilledMessage = false;
193+
sendNewMessage();
194+
} else {
195+
// otherwise, focus on the input and move the cursor to the end
196+
if (inputRef.current) {
197+
inputRef.current.focus();
198+
inputRef.current.selectionStart = inputRef.current.value.length;
199+
}
200+
}
201+
// no need to keep track of sendNewMessage
202+
// eslint-disable-next-line react-hooks/exhaustive-deps
203+
}, [inputRef]);
204+
175205
// 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)
176206
const pendingMsgDisplay: MessageDisplay[] =
177207
pendingMsg && messages.at(-1)?.msg.id !== pendingMsg.id

examples/server/webui/src/utils/misc.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,11 @@ export const throttle = <T extends unknown[]>(
118118
}, delay);
119119
};
120120
};
121+
122+
export const cleanCurrentUrl = (removeQueryParams: string[]) => {
123+
const url = new URL(window.location.href);
124+
removeQueryParams.forEach((param) => {
125+
url.searchParams.delete(param);
126+
});
127+
window.history.replaceState({}, '', url.toString());
128+
};

0 commit comments

Comments
 (0)