@@ -13,14 +13,19 @@ import {
1313 dedupeProviderSkillsByName ,
1414 getProviderSkillsForSlashMenu ,
1515 isProviderSkillUserInvocable ,
16+ resolveProviderSkillsForCwd ,
1617} from "@t3tools/client-runtime/providerSkills" ;
1718import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
1819
1920import type { ComposerEditorSelection } from "../../components/ComposerEditor" ;
21+ import { serverEnvironment } from "../../state/server" ;
22+ import { useAtomCommand } from "../../state/use-atom-command" ;
2023import { useComposerPathSearch } from "../../state/queries" ;
2124import type { ComposerCommandItem } from "./ComposerCommandPopover" ;
2225import { matchesSlashSkillQuery } from "./composerSlashSkillSearch" ;
2326
27+ const WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS = 10_000 ;
28+
2429export function composerSelectionAtEnd ( draftMessage : string ) : ComposerEditorSelection {
2530 return { start : draftMessage . length , end : draftMessage . length } ;
2631}
@@ -69,6 +74,72 @@ export function useComposerCommandMenu({
6974 setSelection ( composerSelectionAtEnd ( draftMessage ) ) ;
7075 } , [ draftMessage , ownerKey ] ) ;
7176
77+ const skills = useMemo (
78+ ( ) =>
79+ selectedProviderStatus ? resolveProviderSkillsForCwd ( selectedProviderStatus , projectCwd ) : [ ] ,
80+ [ projectCwd , selectedProviderStatus ] ,
81+ ) ;
82+ const slashCommands = selectedProviderStatus ?. slashCommands ?? [ ] ;
83+ const refreshProviders = useAtomCommand ( serverEnvironment . refreshProviders , {
84+ reportFailure : false ,
85+ } ) ;
86+ const selectedProviderInstanceId = selectedProviderStatus ?. instanceId ;
87+ const hasWorkspaceSnapshot = Boolean (
88+ projectCwd &&
89+ selectedProviderStatus ?. workspaceSnapshots ?. some ( ( snapshot ) => snapshot . cwd === projectCwd ) ,
90+ ) ;
91+ const workspaceRefreshKeyRef = useRef < string | null > ( null ) ;
92+ const workspaceRefreshRetryRef = useRef < { key : string ; notBefore : number } | null > ( null ) ;
93+ const hadWorkspaceSnapshotRef = useRef ( false ) ;
94+ useEffect ( ( ) => {
95+ if ( hadWorkspaceSnapshotRef . current && ! hasWorkspaceSnapshot ) {
96+ workspaceRefreshKeyRef . current = null ;
97+ workspaceRefreshRetryRef . current = null ;
98+ }
99+ hadWorkspaceSnapshotRef . current = hasWorkspaceSnapshot ;
100+ } , [ hasWorkspaceSnapshot ] ) ;
101+ useEffect ( ( ) => {
102+ if ( ! environmentId || ! projectCwd || ! selectedProviderInstanceId ) return ;
103+ const key = `${ environmentId } :${ selectedProviderInstanceId } :${ projectCwd } ` ;
104+ if ( workspaceRefreshKeyRef . current === key ) return ;
105+ if ( hasWorkspaceSnapshot ) {
106+ workspaceRefreshKeyRef . current = key ;
107+ workspaceRefreshRetryRef . current = null ;
108+ return ;
109+ }
110+ const retry = workspaceRefreshRetryRef . current ;
111+ if ( retry ?. key === key && Date . now ( ) < retry . notBefore ) return ;
112+ workspaceRefreshKeyRef . current = key ;
113+ const retryLater = ( ) => {
114+ if ( workspaceRefreshKeyRef . current !== key ) return ;
115+ workspaceRefreshKeyRef . current = null ;
116+ workspaceRefreshRetryRef . current = {
117+ key,
118+ notBefore : Date . now ( ) + WORKSPACE_SNAPSHOT_RETRY_COOLDOWN_MS ,
119+ } ;
120+ } ;
121+ void refreshProviders ( {
122+ environmentId,
123+ input : { instanceId : selectedProviderInstanceId , cwd : projectCwd } ,
124+ } ) . then ( ( result ) => {
125+ const refreshed =
126+ result . _tag === "Success" &&
127+ result . value . providers
128+ . find ( ( provider ) => provider . instanceId === selectedProviderInstanceId )
129+ ?. workspaceSnapshots ?. some ( ( snapshot ) => snapshot . cwd === projectCwd ) ;
130+ if ( ! refreshed && workspaceRefreshKeyRef . current === key ) {
131+ retryLater ( ) ;
132+ }
133+ } , retryLater ) ;
134+ } , [
135+ draftMessage ,
136+ environmentId ,
137+ hasWorkspaceSnapshot ,
138+ projectCwd ,
139+ refreshProviders ,
140+ selectedProviderInstanceId ,
141+ ] ) ;
142+
72143 const trigger = useMemo ( ( ) => {
73144 if ( ! enabled || selection . start !== selection . end ) {
74145 return null ;
@@ -120,8 +191,7 @@ export function useComposerCommandMenu({
120191 // locally and skills insert a `$` mention the server dispatches from
121192 // any position, so only provider commands are position-gated.
122193 const providerCommands : ComposerCommandItem [ ] = [ ] ;
123- const expandableCommands =
124- trigger . rangeStart === 0 ? ( selectedProviderStatus ?. slashCommands ?? [ ] ) : [ ] ;
194+ const expandableCommands = trigger . rangeStart === 0 ? slashCommands : [ ] ;
125195 for ( const command of expandableCommands ) {
126196 if ( ! command . name . toLowerCase ( ) . includes ( q ) ) continue ;
127197 // Codex feedback uploads an existing thread's session and logs.
@@ -141,7 +211,7 @@ export function useComposerCommandMenu({
141211 } ) ;
142212 }
143213
144- const skillItems = getProviderSkillsForSlashMenu ( selectedProviderStatus ?. skills ?? [ ] , true )
214+ const skillItems = getProviderSkillsForSlashMenu ( skills , true )
145215 . filter ( ( skill ) => matchesSlashSkillQuery ( skill , q ) )
146216 . map ( ( skill ) => ( {
147217 id : `skill:${ skill . name } ` ,
@@ -155,9 +225,7 @@ export function useComposerCommandMenu({
155225 }
156226
157227 if ( trigger . kind === "skill" ) {
158- const enabledSkills = dedupeProviderSkillsByName (
159- ( selectedProviderStatus ?. skills ?? [ ] ) . filter ( isProviderSkillUserInvocable ) ,
160- ) ;
228+ const enabledSkills = dedupeProviderSkillsByName ( skills . filter ( isProviderSkillUserInvocable ) ) ;
161229 const normalizedQuery = normalizeSearchQuery ( trigger . query , {
162230 trimLeadingPattern : / ^ \$ + / ,
163231 } ) ;
@@ -254,7 +322,15 @@ export function useComposerCommandMenu({
254322 }
255323
256324 return [ ] ;
257- } , [ hasThread , onUpdateInteractionMode , pathSearch . entries , selectedProviderStatus , trigger ] ) ;
325+ } , [
326+ hasThread ,
327+ onUpdateInteractionMode ,
328+ pathSearch . entries ,
329+ selectedProviderStatus ,
330+ skills ,
331+ slashCommands ,
332+ trigger ,
333+ ] ) ;
258334
259335 const onSelect = useCallback (
260336 ( item : ComposerCommandItem ) => {
@@ -299,6 +375,7 @@ export function useComposerCommandMenu({
299375 onSelectionChange,
300376 trigger,
301377 items,
378+ skills,
302379 isLoading : pathSearch . isPending ,
303380 onSelect,
304381 } ;
0 commit comments