Skip to content

Commit 65be99e

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 ea489cb commit 65be99e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

examples/server/webui/src/main.js

Lines changed: 13 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

@@ -373,6 +373,10 @@ async function* sendSSEPostRequest(url, fetchOptions) {
373373
}
374374
};
375375

376+
const usp = new URLSearchParams (window.location.search);
377+
const initial_query = usp.get ('q');
378+
const initial_msg = initial_query || usp.get ('m') || '';
379+
376380
const mainApp = createApp({
377381
components: {
378382
VueMarkdown,
@@ -385,7 +389,7 @@ const mainApp = createApp({
385389
/** @type {Array<Message>} */
386390
messages: [],
387391
viewingConvId: StorageUtils.getNewConvId(),
388-
inputMsg: '',
392+
inputMsg: initial_msg,
389393
isGenerating: false,
390394
/** @type {Array<Message> | null} */
391395
pendingMsg: null, // the on-going message from assistant
@@ -660,7 +664,13 @@ const mainApp = createApp({
660664
});
661665
mainApp.config.errorHandler = alert;
662666
try {
663-
mainApp.mount('#app');
667+
const appInstance = mainApp.mount('#app');
668+
nextTick().then(() => {
669+
if (initial_query)
670+
appInstance.sendMessage();
671+
else if (initial_msg)
672+
setTimeout(() => document.getElementById('msg-input').focus(), 1);
673+
});
664674
} catch (err) {
665675
console.error(err);
666676
document.getElementById('app').innerHTML = `<div style="margin:2em auto">

0 commit comments

Comments
 (0)