@@ -2,7 +2,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
2
2
import { CallbackGeneratedChunk , useAppContext } from '../utils/app.context' ;
3
3
import ChatMessage from './ChatMessage' ;
4
4
import { CanvasType , Message , PendingMessage } from '../utils/types' ;
5
- import { classNames , throttle } from '../utils/misc' ;
5
+ import { classNames , cleanCurrentUrl , throttle } from '../utils/misc' ;
6
6
import CanvasPyInterpreter from './CanvasPyInterpreter' ;
7
7
import StorageUtils from '../utils/storage' ;
8
8
import { useVSCodeContext } from '../utils/llama-vscode' ;
@@ -19,6 +19,24 @@ export interface MessageDisplay {
19
19
isPending ?: boolean ;
20
20
}
21
21
22
+ /**
23
+ * If the current URL contains "?m=...", prefill the message input with the value.
24
+ * If the current URL contains "?q=...", prefill and SEND the message.
25
+ */
26
+ const prefilledMsg = {
27
+ content ( ) {
28
+ const url = new URL ( window . location . href ) ;
29
+ return url . searchParams . get ( 'm' ) ?? url . searchParams . get ( 'q' ) ?? '' ;
30
+ } ,
31
+ shouldSend ( ) {
32
+ const url = new URL ( window . location . href ) ;
33
+ return url . searchParams . has ( 'q' ) ;
34
+ } ,
35
+ clear ( ) {
36
+ cleanCurrentUrl ( [ 'm' , 'q' ] ) ;
37
+ } ,
38
+ } ;
39
+
22
40
function getListMessageDisplay (
23
41
msgs : Readonly < Message [ ] > ,
24
42
leafNodeId : Message [ 'id' ]
@@ -83,7 +101,7 @@ export default function ChatScreen() {
83
101
settingsSeed,
84
102
} = useAppContext ( ) ;
85
103
const { t } = useTranslation ( ) ;
86
- const [ inputMsg , setInputMsg ] = useState ( '' ) ;
104
+ const [ inputMsg , setInputMsg ] = useState ( prefilledMsg . content ( ) ) ;
87
105
const [ automaticSend , setAutomaticSend ] = useState ( false ) ;
88
106
const inputRef = useRef < HTMLTextAreaElement > ( null ) ;
89
107
const { config } = useAppContext ( ) ;
@@ -209,6 +227,20 @@ export default function ChatScreen() {
209
227
210
228
const hasCanvas = ! ! canvasData ;
211
229
230
+ useEffect ( ( ) => {
231
+ if ( prefilledMsg . shouldSend ( ) ) {
232
+ // send the prefilled message if needed
233
+ sendNewMessage ( ) . then ( ( ) => { } ) ;
234
+ } else if ( inputRef . current ) {
235
+ // otherwise, focus on the input and move the cursor to the end
236
+ inputRef . current . focus ( ) ;
237
+ inputRef . current . selectionStart = inputRef . current . value . length ;
238
+ }
239
+ prefilledMsg . clear ( ) ;
240
+ // no need to keep track of sendNewMessage
241
+ // eslint-disable-next-line react-hooks/exhaustive-deps
242
+ } , [ inputRef ] ) ;
243
+
212
244
// 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)
213
245
const pendingMsgDisplay : MessageDisplay [ ] =
214
246
pendingMsg && messages . at ( - 1 ) ?. msg . id !== pendingMsg . id
0 commit comments