@@ -47,7 +47,7 @@ import { t } from "../../i18n"
4747import { ClineApiReqCancelReason , ClineApiReqInfo } from "../../shared/ExtensionMessage"
4848import { getApiMetrics } from "../../shared/getApiMetrics"
4949import { ClineAskResponse } from "../../shared/WebviewMessage"
50- import { defaultModeSlug } from "../../shared/modes"
50+ import { defaultModeSlug , getModeBySlug } from "../../shared/modes"
5151import { DiffStrategy } from "../../shared/tools"
5252import { EXPERIMENT_IDS , experiments } from "../../shared/experiments"
5353import { getModelMaxOutputTokens } from "../../shared/api"
@@ -768,27 +768,52 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
768768 this . askResponseImages = images
769769 }
770770
771- public submitUserMessage ( text : string , images ?: string [ ] ) : void {
771+ public submitUserMessage ( text : string , images ?: string [ ] , mode_slug ?: string ) : void {
772772 try {
773773 const trimmed = ( text ?? "" ) . trim ( )
774774 const imgs = images ?? [ ]
775775
776- if ( ! trimmed && imgs . length === 0 ) {
777- return
778- }
779-
780776 const provider = this . providerRef . deref ( )
781777 if ( ! provider ) {
782778 console . error ( "[Task#submitUserMessage] Provider reference lost" )
783779 return
784780 }
785781
786- void provider . postMessageToWebview ( {
787- type : "invoke" ,
788- invoke : "sendMessage" ,
789- text : trimmed ,
790- images : imgs ,
791- } )
782+ // Run asynchronously to allow awaiting mode switch before sending the message
783+ void ( async ( ) => {
784+ // If a mode slug is provided, handle the mode switch first (same behavior as initClineWithTask)
785+ try {
786+ const modeSlugValue = ( mode_slug ?? "" ) . trim ( )
787+ if ( modeSlugValue . length > 0 ) {
788+ const customModes = await provider . customModesManager . getCustomModes ( )
789+ const targetMode = getModeBySlug ( modeSlugValue , customModes )
790+ if ( targetMode ) {
791+ await provider . handleModeSwitch ( targetMode . slug )
792+ provider . log ( `[Task#submitUserMessage] Applied mode from mode_slug: '${ targetMode . slug } '` )
793+ } else {
794+ provider . log ( `[Task#submitUserMessage] Ignoring invalid mode_slug: '${ modeSlugValue } '.` )
795+ }
796+ }
797+ } catch ( err ) {
798+ provider . log (
799+ `[Task#submitUserMessage] Failed to apply mode_slug: ${
800+ err instanceof Error ? err . message : String ( err )
801+ } `,
802+ )
803+ }
804+
805+ // If there's no content to send, exit after potential mode switch
806+ if ( ! trimmed && imgs . length === 0 ) {
807+ return
808+ }
809+
810+ await provider . postMessageToWebview ( {
811+ type : "invoke" ,
812+ invoke : "sendMessage" ,
813+ text : trimmed ,
814+ images : imgs ,
815+ } )
816+ } ) ( )
792817 } catch ( error ) {
793818 console . error ( "[Task#submitUserMessage] Failed to submit user message:" , error )
794819 }
0 commit comments