@@ -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,24 @@ 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 "?q=...", prefill and SEND the message.
24+ */
25+ const prefilledMsg = {
26+ content ( ) {
27+ const url = new URL ( window . location . href ) ;
28+ return url . searchParams . get ( 'm' ) ?? url . searchParams . get ( 'q' ) ?? '' ;
29+ } ,
30+ shouldSend ( ) {
31+ const url = new URL ( window . location . href ) ;
32+ return url . searchParams . has ( 'q' ) ;
33+ } ,
34+ clear ( ) {
35+ cleanCurrentUrl ( [ 'm' , 'q' ] ) ;
36+ } ,
37+ } ;
38+
2139function getListMessageDisplay (
2240 msgs : Readonly < Message [ ] > ,
2341 leafNodeId : Message [ 'id' ]
@@ -81,7 +99,7 @@ export default function ChatScreen() {
8199 canvasData,
82100 replaceMessageAndGenerate,
83101 } = useAppContext ( ) ;
84- const [ inputMsg , setInputMsg ] = useState ( '' ) ;
102+ const [ inputMsg , setInputMsg ] = useState ( prefilledMsg . content ( ) ) ;
85103 const inputRef = useRef < HTMLTextAreaElement > ( null ) ;
86104
87105 const { extraContext, clearExtraContext } = useVSCodeContext (
@@ -172,6 +190,22 @@ export default function ChatScreen() {
172190
173191 const hasCanvas = ! ! canvasData ;
174192
193+ useEffect ( ( ) => {
194+ if ( prefilledMsg . shouldSend ( ) ) {
195+ // send the prefilled message if needed
196+ sendNewMessage ( ) ;
197+ } else {
198+ // otherwise, focus on the input and move the cursor to the end
199+ if ( inputRef . current ) {
200+ inputRef . current . focus ( ) ;
201+ inputRef . current . selectionStart = inputRef . current . value . length ;
202+ }
203+ }
204+ prefilledMsg . clear ( ) ;
205+ // no need to keep track of sendNewMessage
206+ // eslint-disable-next-line react-hooks/exhaustive-deps
207+ } , [ inputRef ] ) ;
208+
175209 // 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)
176210 const pendingMsgDisplay : MessageDisplay [ ] =
177211 pendingMsg && messages . at ( - 1 ) ?. msg . id !== pendingMsg . id
0 commit comments