Skip to content

Commit bc1869c

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, click(msg-send) on ?q * main.js: once the App is mounted, focus(msg-input) on ?m * examples/server/webui/index.html: assign id="msg-send" to the "Send" button Signed-off-by: Tim Janik <[email protected]>
1 parent 8d59d91 commit bc1869c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

examples/server/webui/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ <h2 class="font-bold ml-4">Conversations</h2>
153153
:disabled="isGenerating"
154154
id="msg-input"
155155
></textarea>
156-
<button v-if="!isGenerating" class="btn btn-primary ml-2" @click="sendMessage" :disabled="inputMsg.length === 0">Send</button>
156+
<button v-if="!isGenerating" class="btn btn-primary ml-2" @click="sendMessage" :disabled="inputMsg.length === 0" id="msg-send">Send</button>
157157
<button v-else class="btn btn-neutral ml-2" @click="stopGeneration">Stop</button>
158158
</div>
159159
</div>

examples/server/webui/src/main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import './styles.scss';
22
import { createApp, defineComponent, shallowRef, computed, h } from 'vue/dist/vue.esm-bundler.js';
3+
import { nextTick } from 'vue/dist/vue.esm-bundler.js';
34
import MarkdownIt from 'markdown-it';
45
import TextLineStream from 'textlinestream';
56

@@ -313,6 +314,9 @@ async function* sendSSEPostRequest(url, fetchOptions) {
313314
}
314315
};
315316

317+
const usp = new URLSearchParams (window.location.search);
318+
const initial_query = usp.get ('q'), initial_msg = initial_query || usp.get ('m') || '';
319+
316320
const mainApp = createApp({
317321
components: {
318322
VueMarkdown,
@@ -324,7 +328,7 @@ const mainApp = createApp({
324328
conversations: StorageUtils.getAllConversations(),
325329
messages: [], // { id: number, role: 'user' | 'assistant', content: string }
326330
viewingConvId: StorageUtils.getNewConvId(),
327-
inputMsg: '',
331+
inputMsg: initial_msg, // '',
328332
isGenerating: false,
329333
pendingMsg: null, // the on-going message from assistant
330334
stopGeneration: () => {},
@@ -590,6 +594,12 @@ const mainApp = createApp({
590594
mainApp.config.errorHandler = alert;
591595
try {
592596
mainApp.mount('#app');
597+
nextTick().then (() => {
598+
if (initial_query)
599+
document.getElementById('msg-send').click();
600+
else if (initial_msg)
601+
setTimeout(() => document.getElementById('msg-input').focus(), 1);
602+
});
593603
} catch (err) {
594604
console.error(err);
595605
document.getElementById('app').innerHTML = `<div style="margin:2em auto">

0 commit comments

Comments
 (0)