11import type {
22 EnvironmentId ,
3+ ExplicitSkillInvocation ,
34 MessageId ,
45 ModelSelection ,
56 OrchestrationThreadShell ,
67 ProviderInteractionMode ,
78 RuntimeMode ,
89 ServerConfig as T3ServerConfig ,
910} from "@t3tools/contracts" ;
11+ import { updateExplicitSkillInvocationsForTextEdit } from "@t3tools/shared/explicitSkillInvocations" ;
1012import { StackActions , useFocusEffect , useNavigation } from "@react-navigation/native" ;
1113import type { ReactNode } from "react" ;
1214import {
@@ -88,6 +90,7 @@ export const COMPOSER_EXPANDED_CHROME = 156;
8890
8991export interface ThreadComposerProps {
9092 readonly draftMessage : string ;
93+ readonly draftSkillInvocations : ReadonlyArray < ExplicitSkillInvocation > ;
9194 readonly draftAttachments : ReadonlyArray < DraftComposerAttachment > ;
9295 readonly placeholder : string ;
9396 readonly contentMaxWidth ?: number ;
@@ -107,7 +110,10 @@ export interface ThreadComposerProps {
107110 readonly environmentId : EnvironmentId ;
108111 readonly projectCwd : string | null ;
109112 readonly editorRef ?: RefObject < ComposerEditorHandle | null > ;
110- readonly onChangeDraftMessage : ( value : string ) => void ;
113+ readonly onChangeDraftMessage : (
114+ value : string ,
115+ skillInvocations : ReadonlyArray < ExplicitSkillInvocation > ,
116+ ) => void ;
111117 readonly onPickDraftMedia : ( ) => Promise < void > ;
112118 readonly onPickDraftFiles : ( ) => Promise < void > ;
113119 readonly onNativePasteImages : ( uris : ReadonlyArray < string > ) => Promise < void > ;
@@ -303,8 +309,20 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
303309 const settingsRoutePresentedRef = useRef ( false ) ;
304310 const wasExpandedBeforePreviewRef = useRef ( false ) ;
305311 const inFlightThreadIdsRef = useRef ( new Set < string > ( ) ) ;
312+ const skillInvocationsRef = useRef < ExplicitSkillInvocation [ ] > ( [ ...props . draftSkillInvocations ] ) ;
313+ const previousDraftMessageRef = useRef ( props . draftMessage ) ;
306314 const { onExpandedChange } = props ;
307315
316+ useEffect ( ( ) => {
317+ previousDraftMessageRef . current = props . draftMessage ;
318+ skillInvocationsRef . current = [ ...props . draftSkillInvocations ] ;
319+ } , [
320+ props . draftMessage ,
321+ props . draftSkillInvocations ,
322+ props . environmentId ,
323+ props . selectedThread . id ,
324+ ] ) ;
325+
308326 const [ previewImageUri , setPreviewImageUri ] = useState < string | null > ( null ) ;
309327 const hasContent = props . draftMessage . trim ( ) . length > 0 || props . draftAttachments . length > 0 ;
310328 const showStopAction =
@@ -332,21 +350,42 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
332350 } , [ props . serverConfig , props . selectedThread . modelSelection . instanceId ] ) ;
333351 const composerOwnerKey = scopedThreadKey ( props . environmentId , props . selectedThread . id ) ;
334352
353+ const { onChangeDraftMessage } = props ;
354+ // Keeps the draft's `$skill` ranges aligned with the text they point at, and
355+ // records the range when the command menu inserts a skill.
356+ const handleDraftMessageChange = useCallback (
357+ ( nextMessage : string , addedInvocation ?: ExplicitSkillInvocation ) => {
358+ const previousMessage = previousDraftMessageRef . current ;
359+ skillInvocationsRef . current = updateExplicitSkillInvocationsForTextEdit ( {
360+ previousText : previousMessage ,
361+ nextText : nextMessage ,
362+ invocations : skillInvocationsRef . current ,
363+ } ) ;
364+ if ( addedInvocation ) {
365+ skillInvocationsRef . current . push ( addedInvocation ) ;
366+ skillInvocationsRef . current . sort ( ( left , right ) => left . start - right . start ) ;
367+ }
368+ previousDraftMessageRef . current = nextMessage ;
369+ onChangeDraftMessage ( nextMessage , skillInvocationsRef . current ) ;
370+ } ,
371+ [ onChangeDraftMessage ] ,
372+ ) ;
373+
335374 const composerMenu = useComposerCommandMenu ( {
336375 draftMessage : props . draftMessage ,
337376 ownerKey : composerOwnerKey ,
338377 environmentId : props . environmentId ,
339378 projectCwd : props . projectCwd ,
340379 selectedProviderStatus,
341380 hasThread : true ,
342- onChangeDraftMessage : props . onChangeDraftMessage ,
381+ onChangeDraftMessage : handleDraftMessageChange ,
343382 onUpdateInteractionMode : props . onUpdateInteractionMode ,
344383 } ) ;
345384 const voiceInput = useVoiceInputController ( {
346385 ownerKey : composerOwnerKey ,
347386 draftMessage : props . draftMessage ,
348387 selection : composerMenu . selection ,
349- onChangeDraftMessage : props . onChangeDraftMessage ,
388+ onChangeDraftMessage : handleDraftMessageChange ,
350389 onChangeSelection : composerMenu . onSelectionChange ,
351390 } ) ;
352391 const voicePresentation = resolveVoiceComposerPresentation (
@@ -406,6 +445,8 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
406445 if ( messageId === null ) {
407446 return ;
408447 }
448+ skillInvocationsRef . current = [ ] ;
449+ previousDraftMessageRef . current = "" ;
409450 // Sending a prompt starts agent work: arm the lock-screen card while the
410451 // app is foregrounded and the activity token can be registered. Armed
411452 // after the send so its preference read and native Activity start don't
@@ -614,7 +655,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
614655 readOnly = { voiceInput . freezesEditor }
615656 skills = { selectedProviderStatus ?. skills ?? [ ] }
616657 selection = { composerMenu . selection }
617- onChangeText = { props . onChangeDraftMessage }
658+ onChangeText = { handleDraftMessageChange }
618659 onSelectionChange = { composerMenu . onSelectionChange }
619660 onPasteImages = { ( uris ) => void props . onNativePasteImages ( uris ) }
620661 placeholder = { props . placeholder }
0 commit comments