@@ -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' ;
@@ -18,6 +18,24 @@ export interface MessageDisplay {
18
18
isPending ?: boolean ;
19
19
}
20
20
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
+
21
39
function getListMessageDisplay (
22
40
msgs : Readonly < Message [ ] > ,
23
41
leafNodeId : Message [ 'id' ]
@@ -81,7 +99,7 @@ export default function ChatScreen() {
81
99
canvasData,
82
100
replaceMessageAndGenerate,
83
101
} = useAppContext ( ) ;
84
- const [ inputMsg , setInputMsg ] = useState ( '' ) ;
102
+ const [ inputMsg , setInputMsg ] = useState ( prefilledMsg . content ( ) ) ;
85
103
const inputRef = useRef < HTMLTextAreaElement > ( null ) ;
86
104
87
105
const { extraContext, clearExtraContext } = useVSCodeContext (
@@ -172,6 +190,22 @@ export default function ChatScreen() {
172
190
173
191
const hasCanvas = ! ! canvasData ;
174
192
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
+
175
209
// 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)
176
210
const pendingMsgDisplay : MessageDisplay [ ] =
177
211
pendingMsg && messages . at ( - 1 ) ?. msg . id !== pendingMsg . id
0 commit comments