@@ -1847,6 +1847,99 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18471847 } ;
18481848 } ;
18491849
1850+ const blurMobileComposerAfterSend = ( ) => {
1851+ if ( ! isSoftwareKeyboardOpen ) return ;
1852+ if ( composerBlurFrameRef . current !== null ) {
1853+ window . cancelAnimationFrame ( composerBlurFrameRef . current ) ;
1854+ composerBlurFrameRef . current = null ;
1855+ }
1856+ const activeElement = document . activeElement ;
1857+ if ( activeElement instanceof HTMLElement ) {
1858+ activeElement . blur ( ) ;
1859+ }
1860+ setIsComposerFocused ( false ) ;
1861+ } ;
1862+
1863+ const shouldBlurMobileComposerOnSubmit = ( ) => {
1864+ if ( ! isSoftwareKeyboardOpen ) return false ;
1865+ if (
1866+ isSendBusy ||
1867+ isSendDisabled ||
1868+ noProviderAvailable ||
1869+ environmentUnavailable !== null ||
1870+ phase === "running"
1871+ ) {
1872+ return false ;
1873+ }
1874+ if ( activePendingProgress ) {
1875+ return activePendingProgress . isLastQuestion && Boolean ( activePendingResolvedAnswers ) ;
1876+ }
1877+ return showPlanFollowUpPrompt || composerSendState . hasSendableContent ;
1878+ } ;
1879+
1880+ const submitComposer = (
1881+ event ?: { preventDefault : ( ) => void } ,
1882+ dispatchMode ?: ComposerDispatchMode ,
1883+ submissionIntent ?: ComposerSubmissionIntent ,
1884+ ) => {
1885+ if (
1886+ noProviderAvailable ||
1887+ isSendDisabled ||
1888+ ! readEnvironmentScope ( environmentId , AuthOrchestrationOperateScope )
1889+ ) {
1890+ event ?. preventDefault ( ) ;
1891+ return ;
1892+ }
1893+ // A send while a pasted image is still compressing would strand that image:
1894+ // the turn snapshot wouldn't include it, and it would surface in the *next*
1895+ // draft instead.
1896+ if ( activeThreadId && countPreparingComposerImages ( attachmentTargetKey ) > 0 ) {
1897+ event ?. preventDefault ( ) ;
1898+ toastManager . add ( {
1899+ type : "info" ,
1900+ title : "Still compressing a pasted image." ,
1901+ description : "Send again once its thumbnail appears." ,
1902+ } ) ;
1903+ return ;
1904+ }
1905+ if ( pendingDraftWork . has ( attachmentTargetKey ) ) {
1906+ event ?. preventDefault ( ) ;
1907+ toastManager . add ( {
1908+ type : "info" ,
1909+ title : "Still bringing attached context into this message." ,
1910+ description : "Send again once its chip resolves." ,
1911+ } ) ;
1912+ return ;
1913+ }
1914+ const submission = submitComposerDraft ( {
1915+ prompt : promptRef . current ,
1916+ submissionTarget : activePendingProgress ? "pending-user-input" : "provider-turn" ,
1917+ event,
1918+ onSend : ( sendEvent ) => {
1919+ providerInputRejectedRef . current = false ;
1920+ onSend (
1921+ sendEvent ,
1922+ dispatchMode ??
1923+ resolveComposerDispatchMode ( {
1924+ running : phase === "running" ,
1925+ alternateModifier : false ,
1926+ activeTurnDefault : followUpBehavior ,
1927+ canSteer : ! isCompacting ,
1928+ } ) ,
1929+ submissionIntent ,
1930+ ) ;
1931+ return ! providerInputRejectedRef . current ;
1932+ } ,
1933+ } ) ;
1934+ setComposerSubmissionError ( submission . validationMessage ) ;
1935+ if ( ! submission . didDispatch ) return ;
1936+ onTypingGuardSend ( ) ;
1937+ if ( shouldBlurMobileComposerOnSubmit ( ) ) {
1938+ blurMobileComposerAfterSend ( ) ;
1939+ }
1940+ } ;
1941+ const submitComposerStable = useStableCallback ( submitComposer ) ;
1942+
18501943 const { onUsageLimitsCommand } = props ;
18511944 const onSelectComposerItem = ( item : ComposerCommandItem ) => {
18521945 if ( composerSelectLockRef . current ) return ;
@@ -1886,13 +1979,28 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18861979 }
18871980 if ( item . command === "side" || item . command === "btw" ) {
18881981 if ( props . sideChatDisabledReason ) return ;
1982+ const openImmediately =
1983+ snapshot . value . slice ( 0 , trigger . rangeStart ) . trim ( ) === "" &&
1984+ snapshot . value . slice ( trigger . rangeEnd ) . trim ( ) === "" ;
1985+ const replacement = `/${ item . command } ` ;
1986+ const replacementRangeEnd = extendReplacementRangeForTrailingSpace (
1987+ snapshot . value ,
1988+ trigger . rangeEnd ,
1989+ replacement ,
1990+ ) ;
18891991 const applied = applyPromptReplacement (
18901992 trigger . rangeStart ,
1891- trigger . rangeEnd ,
1892- `/${ item . command } ` ,
1893- { expectedText : snapshot . value . slice ( trigger . rangeStart , trigger . rangeEnd ) } ,
1993+ replacementRangeEnd ,
1994+ replacement ,
1995+ {
1996+ expectedText : snapshot . value . slice ( trigger . rangeStart , replacementRangeEnd ) ,
1997+ focusEditorAfterReplace : ! openImmediately ,
1998+ } ,
18941999 ) ;
1895- if ( applied ) setComposerHighlightedItemId ( null ) ;
2000+ if ( applied ) {
2001+ setComposerHighlightedItemId ( null ) ;
2002+ if ( openImmediately ) submitComposer ( ) ;
2003+ }
18962004 return ;
18972005 }
18982006 if ( ! planModeUiEnabled ) return ;
@@ -2037,98 +2145,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
20372145 setComposerHighlightedItemId ( nextItem ?. id ?? null ) ;
20382146 } ;
20392147
2040- const blurMobileComposerAfterSend = ( ) => {
2041- if ( ! isSoftwareKeyboardOpen ) return ;
2042- if ( composerBlurFrameRef . current !== null ) {
2043- window . cancelAnimationFrame ( composerBlurFrameRef . current ) ;
2044- composerBlurFrameRef . current = null ;
2045- }
2046- const activeElement = document . activeElement ;
2047- if ( activeElement instanceof HTMLElement ) {
2048- activeElement . blur ( ) ;
2049- }
2050- setIsComposerFocused ( false ) ;
2051- } ;
2052-
2053- const shouldBlurMobileComposerOnSubmit = ( ) => {
2054- if ( ! isSoftwareKeyboardOpen ) return false ;
2055- if (
2056- isSendBusy ||
2057- isSendDisabled ||
2058- noProviderAvailable ||
2059- environmentUnavailable !== null ||
2060- phase === "running"
2061- ) {
2062- return false ;
2063- }
2064- if ( activePendingProgress ) {
2065- return activePendingProgress . isLastQuestion && Boolean ( activePendingResolvedAnswers ) ;
2066- }
2067- return showPlanFollowUpPrompt || composerSendState . hasSendableContent ;
2068- } ;
2069-
2070- const submitComposer = (
2071- event ?: { preventDefault : ( ) => void } ,
2072- dispatchMode ?: ComposerDispatchMode ,
2073- submissionIntent ?: ComposerSubmissionIntent ,
2074- ) => {
2075- if (
2076- noProviderAvailable ||
2077- isSendDisabled ||
2078- ! readEnvironmentScope ( environmentId , AuthOrchestrationOperateScope )
2079- ) {
2080- event ?. preventDefault ( ) ;
2081- return ;
2082- }
2083- // A send while a pasted image is still compressing would strand that image:
2084- // the turn snapshot wouldn't include it, and it would surface in the *next*
2085- // draft instead.
2086- if ( activeThreadId && countPreparingComposerImages ( attachmentTargetKey ) > 0 ) {
2087- event ?. preventDefault ( ) ;
2088- toastManager . add ( {
2089- type : "info" ,
2090- title : "Still compressing a pasted image." ,
2091- description : "Send again once its thumbnail appears." ,
2092- } ) ;
2093- return ;
2094- }
2095- if ( pendingDraftWork . has ( attachmentTargetKey ) ) {
2096- event ?. preventDefault ( ) ;
2097- toastManager . add ( {
2098- type : "info" ,
2099- title : "Still bringing attached context into this message." ,
2100- description : "Send again once its chip resolves." ,
2101- } ) ;
2102- return ;
2103- }
2104- const submission = submitComposerDraft ( {
2105- prompt : promptRef . current ,
2106- submissionTarget : activePendingProgress ? "pending-user-input" : "provider-turn" ,
2107- event,
2108- onSend : ( sendEvent ) => {
2109- providerInputRejectedRef . current = false ;
2110- onSend (
2111- sendEvent ,
2112- dispatchMode ??
2113- resolveComposerDispatchMode ( {
2114- running : phase === "running" ,
2115- alternateModifier : false ,
2116- activeTurnDefault : followUpBehavior ,
2117- canSteer : ! isCompacting ,
2118- } ) ,
2119- submissionIntent ,
2120- ) ;
2121- return ! providerInputRejectedRef . current ;
2122- } ,
2123- } ) ;
2124- setComposerSubmissionError ( submission . validationMessage ) ;
2125- if ( ! submission . didDispatch ) return ;
2126- onTypingGuardSend ( ) ;
2127- if ( shouldBlurMobileComposerOnSubmit ( ) ) {
2128- blurMobileComposerAfterSend ( ) ;
2129- }
2130- } ;
2131- const submitComposerStable = useStableCallback ( submitComposer ) ;
21322148 const handleSubmitMessage = ( event : React . MouseEvent < HTMLButtonElement > ) => {
21332149 event . preventDefault ( ) ;
21342150 submitComposer (
0 commit comments