Skip to content

Commit 7b69003

Browse files
authored
webui : add ?m=... and ?q=... params (#12148)
* webui : add ?m=... and ?q=... params * also clear prefilledMessage variable * better approach * fix comment * test: bump timeout on GITHUB_ACTION
1 parent ece9745 commit 7b69003

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

examples/server/public/index.html.gz

169 Bytes
Binary file not shown.

examples/server/tests/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import wget
2727

2828

29-
DEFAULT_HTTP_TIMEOUT = 12 if "LLAMA_SANITIZE" not in os.environ else 30
29+
DEFAULT_HTTP_TIMEOUT = 12
30+
31+
if "LLAMA_SANITIZE" in os.environ or "GITHUB_ACTION" in os.environ:
32+
DEFAULT_HTTP_TIMEOUT = 30
3033

3134

3235
class ServerResponse:

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

Lines changed: 36 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,24 @@ 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 "?q=...", prefill and SEND the message.
24+
*/
25+
const prefilledMsg = {
26+
content() {
27+
const url = new URL(window.location.href);
28+
return url.searchParams.get('m') ?? url.searchParams.get('q') ?? '';
29+
},
30+
shouldSend() {
31+
const url = new URL(window.location.href);
32+
return url.searchParams.has('q');
33+
},
34+
clear() {
35+
cleanCurrentUrl(['m', 'q']);
36+
},
37+
};
38+
2139
function getListMessageDisplay(
2240
msgs: Readonly<Message[]>,
2341
leafNodeId: Message['id']
@@ -81,7 +99,7 @@ export default function ChatScreen() {
8199
canvasData,
82100
replaceMessageAndGenerate,
83101
} = useAppContext();
84-
const [inputMsg, setInputMsg] = useState('');
102+
const [inputMsg, setInputMsg] = useState(prefilledMsg.content());
85103
const inputRef = useRef<HTMLTextAreaElement>(null);
86104

87105
const { extraContext, clearExtraContext } = useVSCodeContext(
@@ -172,6 +190,22 @@ export default function ChatScreen() {
172190

173191
const hasCanvas = !!canvasData;
174192

193+
useEffect(() => {
194+
if (prefilledMsg.shouldSend()) {
195+
// send the prefilled message if needed
196+
sendNewMessage();
197+
} else {
198+
// otherwise, focus on the input and move the cursor to the end
199+
if (inputRef.current) {
200+
inputRef.current.focus();
201+
inputRef.current.selectionStart = inputRef.current.value.length;
202+
}
203+
}
204+
prefilledMsg.clear();
205+
// no need to keep track of sendNewMessage
206+
// eslint-disable-next-line react-hooks/exhaustive-deps
207+
}, [inputRef]);
208+
175209
// 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)
176210
const pendingMsgDisplay: MessageDisplay[] =
177211
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)