@@ -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' ;
@@ -18,6 +18,20 @@ export interface MessageDisplay {
1818 isPending ?: boolean ;
1919}
2020
21+ /**
22+ * If the current URL contains "?m=...", prefill the message input with the value.
23+ * If the current URL contains "?p=...", prefill and SEND the message.
24+ */
25+ let prefilledMessage = '' ;
26+ let sendPrefilledMessage = false ;
27+ {
28+ const url = new URL ( window . location . href ) ;
29+ prefilledMessage =
30+ url . searchParams . get ( 'm' ) ?? url . searchParams . get ( 'p' ) ?? '' ;
31+ sendPrefilledMessage = url . searchParams . has ( 'p' ) ;
32+ cleanCurrentUrl ( [ 'm' , 'p' ] ) ;
33+ }
34+
2135function getListMessageDisplay (
2236 msgs : Readonly < Message [ ] > ,
2337 leafNodeId : Message [ 'id' ]
@@ -81,7 +95,7 @@ export default function ChatScreen() {
8195 canvasData,
8296 replaceMessageAndGenerate,
8397 } = useAppContext ( ) ;
84- const [ inputMsg , setInputMsg ] = useState ( '' ) ;
98+ const [ inputMsg , setInputMsg ] = useState ( prefilledMessage ) ;
8599 const inputRef = useRef < HTMLTextAreaElement > ( null ) ;
86100
87101 const { extraContext, clearExtraContext } = useVSCodeContext (
@@ -172,6 +186,22 @@ export default function ChatScreen() {
172186
173187 const hasCanvas = ! ! canvasData ;
174188
189+ useEffect ( ( ) => {
190+ if ( sendPrefilledMessage ) {
191+ // send the prefilled message if needed
192+ sendPrefilledMessage = false ;
193+ sendNewMessage ( ) ;
194+ } else {
195+ // otherwise, focus on the input and move the cursor to the end
196+ if ( inputRef . current ) {
197+ inputRef . current . focus ( ) ;
198+ inputRef . current . selectionStart = inputRef . current . value . length ;
199+ }
200+ }
201+ // no need to keep track of sendNewMessage
202+ // eslint-disable-next-line react-hooks/exhaustive-deps
203+ } , [ inputRef ] ) ;
204+
175205 // 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)
176206 const pendingMsgDisplay : MessageDisplay [ ] =
177207 pendingMsg && messages . at ( - 1 ) ?. msg . id !== pendingMsg . id
0 commit comments