From aacc7f99a9d7b419dd8a0c30c214586e7866bcbc Mon Sep 17 00:00:00 2001 From: Will Li Date: Fri, 15 Aug 2025 00:06:04 -0700 Subject: [PATCH] working --- packages/types/src/task.ts | 2 +- src/core/task/Task.ts | 49 ++++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/types/src/task.ts b/packages/types/src/task.ts index bf6fc23c96..792298350f 100644 --- a/packages/types/src/task.ts +++ b/packages/types/src/task.ts @@ -95,7 +95,7 @@ export interface TaskLike { off(event: K, listener: (...args: TaskEvents[K]) => void | Promise): this setMessageResponse(text: string, images?: string[]): void - submitUserMessage(text: string, images?: string[]): void + submitUserMessage(text: string, images?: string[], mode_slug?: string): void } export type TaskEvents = { diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index 67be67075c..f2e39f3f86 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -47,7 +47,7 @@ import { t } from "../../i18n" import { ClineApiReqCancelReason, ClineApiReqInfo } from "../../shared/ExtensionMessage" import { getApiMetrics } from "../../shared/getApiMetrics" import { ClineAskResponse } from "../../shared/WebviewMessage" -import { defaultModeSlug } from "../../shared/modes" +import { defaultModeSlug, getModeBySlug } from "../../shared/modes" import { DiffStrategy } from "../../shared/tools" import { EXPERIMENT_IDS, experiments } from "../../shared/experiments" import { getModelMaxOutputTokens } from "../../shared/api" @@ -768,27 +768,52 @@ export class Task extends EventEmitter implements TaskLike { this.askResponseImages = images } - public submitUserMessage(text: string, images?: string[]): void { + public submitUserMessage(text: string, images?: string[], mode_slug?: string): void { try { const trimmed = (text ?? "").trim() const imgs = images ?? [] - if (!trimmed && imgs.length === 0) { - return - } - const provider = this.providerRef.deref() if (!provider) { console.error("[Task#submitUserMessage] Provider reference lost") return } - void provider.postMessageToWebview({ - type: "invoke", - invoke: "sendMessage", - text: trimmed, - images: imgs, - }) + // Run asynchronously to allow awaiting mode switch before sending the message + void (async () => { + // If a mode slug is provided, handle the mode switch first (same behavior as initClineWithTask) + try { + const modeSlugValue = (mode_slug ?? "").trim() + if (modeSlugValue.length > 0) { + const customModes = await provider.customModesManager.getCustomModes() + const targetMode = getModeBySlug(modeSlugValue, customModes) + if (targetMode) { + await provider.handleModeSwitch(targetMode.slug) + provider.log(`[Task#submitUserMessage] Applied mode from mode_slug: '${targetMode.slug}'`) + } else { + provider.log(`[Task#submitUserMessage] Ignoring invalid mode_slug: '${modeSlugValue}'.`) + } + } + } catch (err) { + provider.log( + `[Task#submitUserMessage] Failed to apply mode_slug: ${ + err instanceof Error ? err.message : String(err) + }`, + ) + } + + // If there's no content to send, exit after potential mode switch + if (!trimmed && imgs.length === 0) { + return + } + + await provider.postMessageToWebview({ + type: "invoke", + invoke: "sendMessage", + text: trimmed, + images: imgs, + }) + })() } catch (error) { console.error("[Task#submitUserMessage] Failed to submit user message:", error) }