@@ -7,12 +7,6 @@ import type {
77 RuntimeMode ,
88 ServerConfig as T3ServerConfig ,
99} from "@t3tools/contracts" ;
10- import {
11- detectComposerTrigger ,
12- replaceTextRange ,
13- serializeComposerFileLink ,
14- type ComposerTrigger ,
15- } from "@t3tools/shared/composerTrigger" ;
1610import { StackActions , useFocusEffect , useNavigation } from "@react-navigation/native" ;
1711import type { ReactNode } from "react" ;
1812import { memo , useCallback , useEffect , useMemo , useRef , useState , type RefObject } from "react" ;
@@ -32,11 +26,7 @@ import { scopedThreadKey } from "../../lib/scopedEntities";
3226import { AppText as Text } from "../../components/AppText" ;
3327import { ComposerAttachmentStrip } from "../../components/ComposerAttachmentStrip" ;
3428import { GlassSurface } from "../../components/GlassSurface" ;
35- import {
36- ComposerEditor ,
37- type ComposerEditorHandle ,
38- type ComposerEditorSelection ,
39- } from "../../components/ComposerEditor" ;
29+ import { ComposerEditor , type ComposerEditorHandle } from "../../components/ComposerEditor" ;
4030import {
4131 ComposerInlineControl ,
4232 ComposerToolbarButton ,
@@ -49,15 +39,9 @@ import type { DraftComposerImageAttachment } from "../../lib/composerImages";
4939import { buildModelOptions , groupByProvider } from "../../lib/modelOptions" ;
5040import { useScaledTextRole } from "../settings/appearance/useScaledTextRole" ;
5141import type { RemoteClientConnectionState } from "../../lib/connection" ;
52- import {
53- insertRankedSearchResult ,
54- normalizeSearchQuery ,
55- scoreQueryMatch ,
56- } from "@t3tools/shared/searchRanking" ;
5742import { resolveProviderOptionDescriptors } from "../../lib/providerOptions" ;
58- import { useComposerPathSearch } from "../../state/use-composer-path-search" ;
59- import { ComposerCommandPopover , type ComposerCommandItem } from "./ComposerCommandPopover" ;
60- import { matchesSlashSkillQuery } from "./composerSlashSkillSearch" ;
43+ import { ComposerCommandPopover } from "./ComposerCommandPopover" ;
44+ import { useComposerCommandMenu } from "./use-composer-command-menu" ;
6145import {
6246 type ExistingThreadSettingsRouteSession ,
6347 useExistingThreadSettingsRoutePresentation ,
@@ -329,196 +313,16 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
329313 ) ;
330314 } , [ props . serverConfig , props . selectedThread . modelSelection . instanceId ] ) ;
331315
332- // ── Trigger detection ────────────────────────────────────
333- const [ composerSelection , setComposerSelection ] = useState ( ( ) => ( {
334- start : props . draftMessage . length ,
335- end : props . draftMessage . length ,
336- } ) ) ;
337-
338- const handleSelectionChange = useCallback ( ( selection : ComposerEditorSelection ) => {
339- setComposerSelection ( selection ) ;
340- } , [ ] ) ;
341- useEffect ( ( ) => {
342- const end = props . draftMessage . length ;
343- setComposerSelection ( ( selection ) => {
344- const start = Math . min ( selection . start , end ) ;
345- const selectionEnd = Math . min ( selection . end , end ) ;
346- if ( start === selection . start && selectionEnd === selection . end ) {
347- return selection ;
348- }
349- return { start, end : selectionEnd } ;
350- } ) ;
351- } , [ props . draftMessage . length ] ) ;
352-
353- const composerTrigger = useMemo < ComposerTrigger | null > ( ( ) => {
354- if ( composerSelection . start !== composerSelection . end ) {
355- return null ;
356- }
357- return detectComposerTrigger ( props . draftMessage , composerSelection . end ) ;
358- } , [ composerSelection , props . draftMessage ] ) ;
359- const pathSearch = useComposerPathSearch ( {
316+ const composerMenu = useComposerCommandMenu ( {
317+ draftMessage : props . draftMessage ,
360318 environmentId : props . environmentId ,
361- cwd : composerTrigger ?. kind === "path" ? props . projectCwd : null ,
362- query : composerTrigger ?. kind === "path" ? composerTrigger . query : null ,
319+ projectCwd : props . projectCwd ,
320+ selectedProviderStatus,
321+ hasThread : true ,
322+ onChangeDraftMessage : props . onChangeDraftMessage ,
323+ onUpdateInteractionMode : props . onUpdateInteractionMode ,
363324 } ) ;
364-
365- const composerMenuItems : ComposerCommandItem [ ] = useMemo ( ( ) => {
366- if ( ! composerTrigger ) return [ ] ;
367-
368- if ( composerTrigger . kind === "slash-command" ) {
369- const q = composerTrigger . query . toLowerCase ( ) ;
370- const allBuiltIn = [
371- {
372- id : "cmd:model" ,
373- type : "slash-command" as const ,
374- command : "model" ,
375- label : "/model" ,
376- description : "Switch model" ,
377- } ,
378- {
379- id : "cmd:plan" ,
380- type : "slash-command" as const ,
381- command : "plan" ,
382- label : "/plan" ,
383- description : "Switch to plan mode" ,
384- } ,
385- {
386- id : "cmd:default" ,
387- type : "slash-command" as const ,
388- command : "default" ,
389- label : "/default" ,
390- description : "Switch to default mode" ,
391- } ,
392- ] ;
393- const builtIn = allBuiltIn . filter ( ( item ) => item . command . includes ( q ) ) ;
394-
395- const providerCommands : ComposerCommandItem [ ] = [ ] ;
396- for ( const cmd of selectedProviderStatus ?. slashCommands ?? [ ] ) {
397- if ( ! cmd . name . toLowerCase ( ) . includes ( q ) ) continue ;
398- providerCommands . push ( {
399- id : `pcmd:${ cmd . name } ` ,
400- type : "provider-slash-command" as const ,
401- command : cmd ,
402- label : `/${ cmd . name } ` ,
403- description : cmd . description ?? "" ,
404- } ) ;
405- }
406-
407- const skillItems = ( selectedProviderStatus ?. skills ?? [ ] )
408- . filter ( ( skill ) => matchesSlashSkillQuery ( skill , q ) )
409- . map ( ( skill ) => ( {
410- id : `skill:${ skill . name } ` ,
411- type : "skill" as const ,
412- skill,
413- label : `skill:${ skill . name } ` ,
414- description : skill . shortDescription ?? skill . description ?? "" ,
415- } ) ) ;
416-
417- return [ ...builtIn , ...providerCommands , ...skillItems ] ;
418- }
419-
420- if ( composerTrigger . kind === "skill" ) {
421- const enabledSkills = ( selectedProviderStatus ?. skills ?? [ ] ) . filter ( ( s ) => s . enabled ) ;
422- const normalizedQuery = normalizeSearchQuery ( composerTrigger . query , {
423- trimLeadingPattern : / ^ \$ + / ,
424- } ) ;
425-
426- if ( ! normalizedQuery ) {
427- return enabledSkills . slice ( 0 , 20 ) . map ( ( skill ) => ( {
428- id : `skill:${ skill . name } ` ,
429- type : "skill" as const ,
430- skill,
431- label : skill . displayName ?? skill . name ,
432- description : skill . shortDescription ?? skill . description ?? "" ,
433- } ) ) ;
434- }
435-
436- const ranked : Array < {
437- item : ( typeof enabledSkills ) [ number ] ;
438- score : number ;
439- tieBreaker : string ;
440- } > = [ ] ;
441- for ( const skill of enabledSkills ) {
442- const displayLabel = ( skill . displayName ?? skill . name ) . toLowerCase ( ) ;
443- const scores = [
444- scoreQueryMatch ( {
445- value : skill . name . toLowerCase ( ) ,
446- query : normalizedQuery ,
447- exactBase : 0 ,
448- prefixBase : 2 ,
449- boundaryBase : 4 ,
450- includesBase : 6 ,
451- fuzzyBase : 100 ,
452- boundaryMarkers : [ "-" , "_" , "/" ] ,
453- } ) ,
454- scoreQueryMatch ( {
455- value : displayLabel ,
456- query : normalizedQuery ,
457- exactBase : 1 ,
458- prefixBase : 3 ,
459- boundaryBase : 5 ,
460- includesBase : 7 ,
461- fuzzyBase : 110 ,
462- } ) ,
463- scoreQueryMatch ( {
464- value : skill . shortDescription ?. toLowerCase ( ) ?? "" ,
465- query : normalizedQuery ,
466- exactBase : 20 ,
467- prefixBase : 22 ,
468- boundaryBase : 24 ,
469- includesBase : 26 ,
470- } ) ,
471- scoreQueryMatch ( {
472- value : skill . description ?. toLowerCase ( ) ?? "" ,
473- query : normalizedQuery ,
474- exactBase : 30 ,
475- prefixBase : 32 ,
476- boundaryBase : 34 ,
477- includesBase : 36 ,
478- } ) ,
479- ] . filter ( ( s ) : s is number => s !== null ) ;
480-
481- if ( scores . length > 0 ) {
482- insertRankedSearchResult (
483- ranked ,
484- {
485- item : skill ,
486- score : Math . min ( ...scores ) ,
487- tieBreaker : `${ displayLabel } \u0000${ skill . name } ` ,
488- } ,
489- 20 ,
490- ) ;
491- }
492- }
493-
494- return ranked . map ( ( { item : skill } ) => ( {
495- id : `skill:${ skill . name } ` ,
496- type : "skill" as const ,
497- skill,
498- label : skill . displayName ?? skill . name ,
499- description : skill . shortDescription ?? skill . description ?? "" ,
500- } ) ) ;
501- }
502-
503- if ( composerTrigger . kind === "path" ) {
504- return pathSearch . entries . map ( ( entry ) => {
505- const parts = entry . path . split ( "/" ) ;
506- return {
507- id : `path:${ entry . path } ` ,
508- type : "path" as const ,
509- path : entry . path ,
510- kind : entry . kind ,
511- label : parts [ parts . length - 1 ] ?? entry . path ,
512- description : parts . length > 1 ? parts . slice ( 0 , - 1 ) . join ( "/" ) : "" ,
513- } ;
514- } ) ;
515- }
516-
517- return [ ] ;
518- } , [ composerTrigger , pathSearch . entries , selectedProviderStatus ] ) ;
519-
520- // ── Handle command selection ──────────────────────────────
521- const { onChangeDraftMessage, onUpdateInteractionMode, draftMessage, onSendMessage } = props ;
325+ const { onSendMessage } = props ;
522326
523327 const handleSend = useCallback ( async ( ) => {
524328 const threadKey = scopedThreadKey ( props . environmentId , props . selectedThread . id ) ;
@@ -548,48 +352,6 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
548352 props . selectedThread . id ,
549353 props . selectedThread . title ,
550354 ] ) ;
551- const handleCommandSelect = useCallback (
552- ( item : ComposerCommandItem ) => {
553- if ( ! composerTrigger ) return ;
554-
555- if (
556- item . type === "slash-command" &&
557- ( item . command === "plan" || item . command === "default" )
558- ) {
559- const result = replaceTextRange (
560- draftMessage ,
561- composerTrigger . rangeStart ,
562- composerTrigger . rangeEnd ,
563- "" ,
564- ) ;
565- setComposerSelection ( { start : result . cursor , end : result . cursor } ) ;
566- onChangeDraftMessage ( result . text ) ;
567- onUpdateInteractionMode ( item . command ) ;
568- return ;
569- }
570-
571- let replacement = "" ;
572- if ( item . type === "path" ) {
573- replacement = `${ serializeComposerFileLink ( item . path ) } ` ;
574- } else if ( item . type === "skill" ) {
575- replacement = `$${ item . skill . name } ` ;
576- } else if ( item . type === "slash-command" ) {
577- replacement = `/${ item . command } ` ;
578- } else if ( item . type === "provider-slash-command" ) {
579- replacement = `/${ item . command . name } ` ;
580- }
581-
582- const result = replaceTextRange (
583- draftMessage ,
584- composerTrigger . rangeStart ,
585- composerTrigger . rangeEnd ,
586- replacement ,
587- ) ;
588- setComposerSelection ( { start : result . cursor , end : result . cursor } ) ;
589- onChangeDraftMessage ( result . text ) ;
590- } ,
591- [ composerTrigger , draftMessage , onChangeDraftMessage , onUpdateInteractionMode ] ,
592- ) ;
593355
594356 // ── Model menu ───────────────────────────────────────────
595357 const modelOptions = useMemo (
@@ -705,13 +467,13 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
705467 layout = { COMPOSER_LAYOUT_TRANSITION }
706468 style = { { maxWidth : props . contentMaxWidth } }
707469 >
708- { composerTrigger && composerMenuItems . length > 0 ? (
470+ { composerMenu . trigger && composerMenu . items . length > 0 ? (
709471 < View className = "absolute inset-x-0 bottom-full z-10 mb-2" >
710472 < ComposerCommandPopover
711- items = { composerMenuItems }
712- triggerKind = { composerTrigger . kind }
713- isLoading = { pathSearch . isPending }
714- onSelect = { handleCommandSelect }
473+ items = { composerMenu . items }
474+ triggerKind = { composerMenu . trigger . kind }
475+ isLoading = { composerMenu . isLoading }
476+ onSelect = { composerMenu . onSelect }
715477 />
716478 </ View >
717479 ) : null }
@@ -766,9 +528,9 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
766528 multiline
767529 value = { props . draftMessage }
768530 skills = { selectedProviderStatus ?. skills ?? [ ] }
769- selection = { composerSelection }
531+ selection = { composerMenu . selection }
770532 onChangeText = { props . onChangeDraftMessage }
771- onSelectionChange = { handleSelectionChange }
533+ onSelectionChange = { composerMenu . onSelectionChange }
772534 onPasteImages = { ( uris ) => void props . onNativePasteImages ( uris ) }
773535 placeholder = { props . placeholder }
774536 onFocus = { handleFocus }
0 commit comments