@@ -2,7 +2,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
22import { CallbackGeneratedChunk , useAppContext } from '../utils/app.context' ;
33import ChatMessage from './ChatMessage' ;
44import { CanvasType , Message , PendingMessage } from '../utils/types' ;
5- import { classNames , throttle } from '../utils/misc' ;
5+ import { classNames , cleanCurrentUrl , throttle } from '../utils/misc' ;
66import CanvasPyInterpreter from './CanvasPyInterpreter' ;
77import StorageUtils from '../utils/storage' ;
88import { useVSCodeContext } from '../utils/llama-vscode' ;
@@ -19,6 +19,24 @@ export interface MessageDisplay {
1919 isPending ?: boolean ;
2020}
2121
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+
2240function getListMessageDisplay (
2341 msgs : Readonly < Message [ ] > ,
2442 leafNodeId : Message [ 'id' ]
@@ -83,7 +101,7 @@ export default function ChatScreen() {
83101 settingsSeed,
84102 } = useAppContext ( ) ;
85103 const { t } = useTranslation ( ) ;
86- const [ inputMsg , setInputMsg ] = useState ( '' ) ;
104+ const [ inputMsg , setInputMsg ] = useState ( prefilledMsg . content ( ) ) ;
87105 const [ automaticSend , setAutomaticSend ] = useState ( false ) ;
88106 const inputRef = useRef < HTMLTextAreaElement > ( null ) ;
89107 const { config } = useAppContext ( ) ;
@@ -209,6 +227,20 @@ export default function ChatScreen() {
209227
210228 const hasCanvas = ! ! canvasData ;
211229
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+
212244 // 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)
213245 const pendingMsgDisplay : MessageDisplay [ ] =
214246 pendingMsg && messages . at ( - 1 ) ?. msg . id !== pendingMsg . id
0 commit comments