@@ -229,60 +229,37 @@ const InternalPromptInput = React.forwardRef(
229229 return ;
230230 }
231231
232- if ( isTokenMode ) {
233- if ( ! editableElementRef . current || ! tokens || ! caretControllerRef . current ) {
234- return ;
235- }
236-
237- let adjustedCaretStart : number ;
238- let adjustedCaretEnd : number | undefined ;
239-
240- if ( caretStart === undefined ) {
241- const currentPos = caretControllerRef . current . getPosition ( ) ;
242- const pinnedCount = tokens . filter ( isPinnedReferenceToken ) . length ;
232+ if ( ! isTokenMode || ! editableElementRef . current || ! tokens || ! caretControllerRef . current ) {
233+ return ;
234+ }
243235
244- // If the caret is before or between pinned tokens, move it after them.
245- // Text inserted here would get pushed after pinned tokens by enforcePinnedTokenOrdering,
246- // but the caret wouldn't follow — so we preemptively position it correctly.
247- adjustedCaretStart = pinnedCount > 0 && currentPos < pinnedCount ? pinnedCount : currentPos ;
248- adjustedCaretEnd = undefined ;
249- } else {
250- const pinnedTokens = tokens . filter ( isPinnedReferenceToken ) ;
251- const pinnedOffset = pinnedTokens . length ;
236+ let adjustedCaretStart : number ;
237+ let adjustedCaretEnd : number | undefined ;
252238
253- adjustedCaretStart = caretStart + pinnedOffset ;
254- adjustedCaretEnd = caretEnd !== undefined ? caretEnd + pinnedOffset : undefined ;
255- }
239+ if ( caretStart === undefined ) {
240+ const currentPos = caretControllerRef . current . getPosition ( ) ;
241+ const pinnedCount = tokens . filter ( isPinnedReferenceToken ) . length ;
256242
257- insertTextIntoContentEditable (
258- editableElementRef . current ,
259- text ,
260- adjustedCaretStart ,
261- adjustedCaretEnd ,
262- caretControllerRef . current
263- ) ;
243+ // If the caret is before or between pinned tokens, move it after them.
244+ // Text inserted here would get pushed after pinned tokens by enforcePinnedTokenOrdering,
245+ // but the caret wouldn't follow — so we preemptively position it correctly.
246+ adjustedCaretStart = pinnedCount > 0 && currentPos < pinnedCount ? pinnedCount : currentPos ;
247+ adjustedCaretEnd = undefined ;
264248 } else {
265- if ( ! textareaRef . current ) {
266- return ;
267- }
268-
269- const textarea = textareaRef . current ;
270- textarea . focus ( ) ;
271-
272- const currentValue = textarea . value ;
273- const insertPosition = caretStart ?? textarea . selectionStart ?? 0 ;
274- const newValue = currentValue . substring ( 0 , insertPosition ) + text + currentValue . substring ( insertPosition ) ;
275-
276- textarea . value = newValue ;
249+ const pinnedTokens = tokens . filter ( isPinnedReferenceToken ) ;
250+ const pinnedOffset = pinnedTokens . length ;
277251
278- const finalCursorPosition = caretEnd ?? insertPosition + text . length ;
279- textarea . setSelectionRange ( finalCursorPosition , finalCursorPosition ) ;
280-
281- textarea . dispatchEvent ( new Event ( 'input' , { bubbles : true } ) ) ;
282- fireNonCancelableEvent ( onChange , {
283- value : newValue ,
284- } ) ;
252+ adjustedCaretStart = caretStart + pinnedOffset ;
253+ adjustedCaretEnd = caretEnd !== undefined ? caretEnd + pinnedOffset : undefined ;
285254 }
255+
256+ insertTextIntoContentEditable (
257+ editableElementRef . current ,
258+ text ,
259+ adjustedCaretStart ,
260+ adjustedCaretEnd ,
261+ caretControllerRef . current
262+ ) ;
286263 } ) ;
287264
288265 useImperativeHandle (
0 commit comments