Skip to content

Commit 96d40e1

Browse files
committed
examples/server/webui/src/main.js: populate textarea from query string
Allow pre-filling the msg-input textarea with a message supplied via a query parameter ?m=... and auto-focus the textarea. Example: http://localhost:8080/?m=What%20is%20your%20name? Alternatively, allow pre-filling the msg-input textarea via a query parameter ?q=... and automatically start generating a response, e.g.: http://localhost:8080/?q=What%20is%20your%20name? * main.js: fill inputMsg from ?q or ?m * main.js: once the App is mounted, call sendMessage() on ?q * main.js: once the App is mounted, focus(msg-input) on ?m Signed-off-by: Tim Janik <[email protected]>
1 parent 4981d4b commit 96d40e1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

examples/server/webui/src/main.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './styles.scss';
2-
import { createApp, defineComponent, shallowRef, computed, h } from 'vue/dist/vue.esm-bundler.js';
2+
import { createApp, defineComponent, shallowRef, computed, h, nextTick } from 'vue/dist/vue.esm-bundler.js';
33
import MarkdownIt from 'markdown-it';
44
import TextLineStream from 'textlinestream';
55

@@ -313,6 +313,9 @@ async function* sendSSEPostRequest(url, fetchOptions) {
313313
}
314314
};
315315

316+
const usp = new URLSearchParams (window.location.search);
317+
const initial_query = usp.get ('q'), initial_msg = initial_query || usp.get ('m') || '';
318+
316319
const mainApp = createApp({
317320
components: {
318321
VueMarkdown,
@@ -324,7 +327,7 @@ const mainApp = createApp({
324327
conversations: StorageUtils.getAllConversations(),
325328
messages: [], // { id: number, role: 'user' | 'assistant', content: string }
326329
viewingConvId: StorageUtils.getNewConvId(),
327-
inputMsg: '',
330+
inputMsg: initial_msg,
328331
isGenerating: false,
329332
pendingMsg: null, // the on-going message from assistant
330333
stopGeneration: () => {},
@@ -589,7 +592,13 @@ const mainApp = createApp({
589592
});
590593
mainApp.config.errorHandler = alert;
591594
try {
592-
mainApp.mount('#app');
595+
const appInstance = mainApp.mount('#app');
596+
nextTick().then(() => {
597+
if (initial_query)
598+
appInstance.sendMessage();
599+
else if (initial_msg)
600+
setTimeout(() => document.getElementById('msg-input').focus(), 1);
601+
});
593602
} catch (err) {
594603
console.error(err);
595604
document.getElementById('app').innerHTML = `<div style="margin:2em auto">

0 commit comments

Comments
 (0)