diff --git a/.changeset/warm-yaks-lay.md b/.changeset/warm-yaks-lay.md new file mode 100644 index 00000000..53db3d09 --- /dev/null +++ b/.changeset/warm-yaks-lay.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +add global workflows diff --git a/proto/file.proto b/proto/file.proto index a6866942..4b09984d 100644 --- a/proto/file.proto +++ b/proto/file.proto @@ -57,7 +57,8 @@ message RefreshedRules { ClineRulesToggles local_cline_rules_toggles = 2; ClineRulesToggles local_cursor_rules_toggles = 3; ClineRulesToggles local_windsurf_rules_toggles = 4; - ClineRulesToggles workflow_toggles = 5; + ClineRulesToggles local_workflow_toggles = 5; + ClineRulesToggles global_workflow_toggles = 6; } // Request to toggle a Windsurf rule diff --git a/src/core/context/instructions/user-instructions/rule-helpers.ts b/src/core/context/instructions/user-instructions/rule-helpers.ts index b25d5156..7005bd20 100644 --- a/src/core/context/instructions/user-instructions/rule-helpers.ts +++ b/src/core/context/instructions/user-instructions/rule-helpers.ts @@ -1,5 +1,5 @@ import { fileExistsAtPath, isDirectory, readDirectory } from "@utils/fs" -import { ensureRulesDirectoryExists, GlobalFileNames } from "@core/storage/disk" +import { ensureRulesDirectoryExists, ensureWorkflowsDirectoryExists, GlobalFileNames } from "@core/storage/disk" import { getGlobalState, getWorkspaceState, updateGlobalState, updateWorkspaceState } from "@core/storage/state" import * as path from "path" import fs from "fs/promises" @@ -172,9 +172,13 @@ export const createRuleFile = async (isGlobal: boolean, filename: string, cwd: s try { let filePath: string if (isGlobal) { - // global means its implicitly clinerules - const globalClineRulesFilePath = await ensureRulesDirectoryExists() - filePath = path.join(globalClineRulesFilePath, filename) + if (type === "workflow") { + const globalClineWorkflowFilePath = await ensureWorkflowsDirectoryExists() + filePath = path.join(globalClineWorkflowFilePath, filename) + } else { + const globalClineRulesFilePath = await ensureRulesDirectoryExists() + filePath = path.join(globalClineRulesFilePath, filename) + } } else { const localClineRulesFilePath = path.resolve(cwd, GlobalFileNames.clineRules) @@ -243,9 +247,15 @@ export async function deleteRuleFile( // Update the appropriate toggles if (isGlobal) { - const toggles = ((await getGlobalState(context, "globalClineRulesToggles")) as ClineRulesToggles) || {} - delete toggles[rulePath] - await updateGlobalState(context, "globalClineRulesToggles", toggles) + if (type === "workflow") { + const toggles = ((await getGlobalState(context, "globalWorkflowToggles")) as ClineRulesToggles) || {} + delete toggles[rulePath] + await updateGlobalState(context, "globalWorkflowToggles", toggles) + } else { + const toggles = ((await getGlobalState(context, "globalClineRulesToggles")) as ClineRulesToggles) || {} + delete toggles[rulePath] + await updateGlobalState(context, "globalClineRulesToggles", toggles) + } } else { if (type === "workflow") { const toggles = ((await getWorkspaceState(context, "workflowToggles")) as ClineRulesToggles) || {} diff --git a/src/core/context/instructions/user-instructions/workflows.ts b/src/core/context/instructions/user-instructions/workflows.ts index 94e4d545..268a5dad 100644 --- a/src/core/context/instructions/user-instructions/workflows.ts +++ b/src/core/context/instructions/user-instructions/workflows.ts @@ -1,7 +1,7 @@ import path from "path" -import { GlobalFileNames } from "@core/storage/disk" +import { GlobalFileNames, ensureWorkflowsDirectoryExists } from "@core/storage/disk" import { ClineRulesToggles } from "@shared/cline-rules" -import { getWorkspaceState, updateWorkspaceState } from "@core/storage/state" +import { getWorkspaceState, updateWorkspaceState, getGlobalState, updateGlobalState } from "@core/storage/state" import * as vscode from "vscode" import { synchronizeRuleToggles } from "@core/context/instructions/user-instructions/rule-helpers" @@ -11,10 +11,23 @@ import { synchronizeRuleToggles } from "@core/context/instructions/user-instruct export async function refreshWorkflowToggles( context: vscode.ExtensionContext, workingDirectory: string, -): Promise { +): Promise<{ + globalWorkflowToggles: ClineRulesToggles + localWorkflowToggles: ClineRulesToggles +}> { + // Global workflows + const globalWorkflowToggles = ((await getGlobalState(context, "globalWorkflowToggles")) as ClineRulesToggles) || {} + const globalClineWorkflowsFilePath = await ensureWorkflowsDirectoryExists() + const updatedGlobalWorkflowToggles = await synchronizeRuleToggles(globalClineWorkflowsFilePath, globalWorkflowToggles) + await updateGlobalState(context, "globalWorkflowToggles", updatedGlobalWorkflowToggles) + const workflowRulesToggles = ((await getWorkspaceState(context, "workflowToggles")) as ClineRulesToggles) || {} const workflowsDirPath = path.resolve(workingDirectory, GlobalFileNames.workflows) const updatedWorkflowToggles = await synchronizeRuleToggles(workflowsDirPath, workflowRulesToggles) await updateWorkspaceState(context, "workflowToggles", updatedWorkflowToggles) - return updatedWorkflowToggles + + return { + globalWorkflowToggles: updatedGlobalWorkflowToggles, + localWorkflowToggles: updatedWorkflowToggles, + } } diff --git a/src/core/controller/file/refreshRules.ts b/src/core/controller/file/refreshRules.ts index 1b897b01..c56c6d38 100644 --- a/src/core/controller/file/refreshRules.ts +++ b/src/core/controller/file/refreshRules.ts @@ -16,14 +16,15 @@ export async function refreshRules(controller: Controller, _request: EmptyReques try { const { globalToggles, localToggles } = await refreshClineRulesToggles(controller.context, cwd) const { cursorLocalToggles, windsurfLocalToggles } = await refreshExternalRulesToggles(controller.context, cwd) - const workflowToggles = await refreshWorkflowToggles(controller.context, cwd) + const { localWorkflowToggles, globalWorkflowToggles } = await refreshWorkflowToggles(controller.context, cwd) return { globalClineRulesToggles: { toggles: globalToggles }, localClineRulesToggles: { toggles: localToggles }, localCursorRulesToggles: { toggles: cursorLocalToggles }, localWindsurfRulesToggles: { toggles: windsurfLocalToggles }, - workflowToggles: { toggles: workflowToggles }, + localWorkflowToggles: { toggles: localWorkflowToggles }, + globalWorkflowToggles: { toggles: globalWorkflowToggles }, } } catch (error) { console.error("Failed to refresh rules:", error) diff --git a/src/core/controller/index.ts b/src/core/controller/index.ts index 66cbf12b..7ee106d0 100644 --- a/src/core/controller/index.ts +++ b/src/core/controller/index.ts @@ -32,7 +32,12 @@ import { fileExistsAtPath } from "@utils/fs" import { getWorkingState } from "@utils/git" import { extractCommitMessage } from "@integrations/git/commit-message-generator" import { getTotalTasksSize } from "@utils/storage" -import { ensureMcpServersDirectoryExists, ensureSettingsDirectoryExists, GlobalFileNames } from "../storage/disk" +import { + ensureMcpServersDirectoryExists, + ensureSettingsDirectoryExists, + GlobalFileNames, + ensureWorkflowsDirectoryExists, +} from "../storage/disk" import { getAllExtensionState, getGlobalState, @@ -358,12 +363,20 @@ export class Controller { // break // } case "toggleWorkflow": { - const { workflowPath, enabled } = message - if (workflowPath && typeof enabled === "boolean") { - const toggles = ((await getWorkspaceState(this.context, "workflowToggles")) as ClineRulesToggles) || {} - toggles[workflowPath] = enabled - await updateWorkspaceState(this.context, "workflowToggles", toggles) - await this.postStateToWebview() + const { workflowPath, enabled, isGlobal } = message + if (workflowPath && typeof enabled === "boolean" && typeof isGlobal === "boolean") { + if (isGlobal) { + const globalWorkflowToggles = + ((await getGlobalState(this.context, "globalWorkflowToggles")) as ClineRulesToggles) || {} + globalWorkflowToggles[workflowPath] = enabled + await updateGlobalState(this.context, "globalWorkflowToggles", globalWorkflowToggles) + await this.postStateToWebview() + } else { + const toggles = ((await getWorkspaceState(this.context, "workflowToggles")) as ClineRulesToggles) || {} + toggles[workflowPath] = enabled + await updateWorkspaceState(this.context, "workflowToggles", toggles) + await this.postStateToWebview() + } } break } @@ -1236,6 +1249,7 @@ export class Controller { planActSeparateModelsSetting, enableCheckpointsSetting, globalClineRulesToggles, + globalWorkflowToggles, shellIntegrationTimeout, isNewUser, } = await getAllExtensionState(this.context) @@ -1249,7 +1263,7 @@ export class Controller { const localCursorRulesToggles = ((await getWorkspaceState(this.context, "localCursorRulesToggles")) as ClineRulesToggles) || {} - const workflowToggles = ((await getWorkspaceState(this.context, "workflowToggles")) as ClineRulesToggles) || {} + const localWorkflowToggles = ((await getWorkspaceState(this.context, "workflowToggles")) as ClineRulesToggles) || {} return { version: this.context.extension?.packageJSON?.version ?? "", @@ -1278,7 +1292,8 @@ export class Controller { localClineRulesToggles: localClineRulesToggles || {}, localWindsurfRulesToggles: localWindsurfRulesToggles || {}, localCursorRulesToggles: localCursorRulesToggles || {}, - workflowToggles: workflowToggles || {}, + localWorkflowToggles: localWorkflowToggles || {}, + globalWorkflowToggles: globalWorkflowToggles || {}, shellIntegrationTimeout, isNewUser, } diff --git a/src/core/slash-commands/index.ts b/src/core/slash-commands/index.ts index dddc2de5..759a59bd 100644 --- a/src/core/slash-commands/index.ts +++ b/src/core/slash-commands/index.ts @@ -8,7 +8,8 @@ import fs from "fs/promises" */ export async function parseSlashCommands( text: string, - workflowToggles: ClineRulesToggles, + localWorkflowToggles: ClineRulesToggles, + globalWorkflowToggles: ClineRulesToggles, ): Promise<{ processedText: string; needsClinerulesFileCheck: boolean }> { const SUPPORTED_DEFAULT_COMMANDS = ["newtask", "smol", "compact", "newrule", "reportbug"] @@ -58,18 +59,29 @@ export async function parseSlashCommands( return { processedText: processedText, needsClinerulesFileCheck: commandName === "newrule" ? true : false } } - // in practice we want to minimize this work, so we only do it if theres a possible match - const enabledWorkflows = Object.entries(workflowToggles) + const globalWorkflows = Object.entries(globalWorkflowToggles) .filter(([_, enabled]) => enabled) .map(([filePath, _]) => { const fileName = filePath.replace(/^.*[/\\]/, "") - return { fullPath: filePath, fileName: fileName, } }) + const localWorkflows = Object.entries(localWorkflowToggles) + .filter(([_, enabled]) => enabled) + .map(([filePath, _]) => { + const fileName = filePath.replace(/^.*[/\\]/, "") + return { + fullPath: filePath, + fileName: fileName, + } + }) + + // local workflows have precedence over global workflows + const enabledWorkflows = [...localWorkflows, ...globalWorkflows] + // Then check if the command matches any enabled workflow filename const matchingWorkflow = enabledWorkflows.find((workflow) => workflow.fileName === commandName) diff --git a/src/core/storage/disk.ts b/src/core/storage/disk.ts index 78f1b0ec..fdaa7f06 100644 --- a/src/core/storage/disk.ts +++ b/src/core/storage/disk.ts @@ -76,6 +76,17 @@ export async function ensureRulesDirectoryExists(): Promise { return clineRulesDir } +export async function ensureWorkflowsDirectoryExists(): Promise { + const userDocumentsPath = await getDocumentsPath() + const clineWorkflowsDir = path.join(userDocumentsPath, "Cline", "Workflows") + try { + await fs.mkdir(clineWorkflowsDir, { recursive: true }) + } catch (error) { + return path.join(os.homedir(), "Documents", "Cline", "Workflows") // in case creating a directory in documents fails for whatever reason (e.g. permissions) - this is fine because we will fail gracefully with a path that does not exist + } + return clineWorkflowsDir +} + export async function ensureMcpServersDirectoryExists(): Promise { const userDocumentsPath = await getDocumentsPath() const mcpServersDir = path.join(userDocumentsPath, "Cline", "MCP") diff --git a/src/core/storage/state-keys.ts b/src/core/storage/state-keys.ts index 3c73219f..2a45bca9 100644 --- a/src/core/storage/state-keys.ts +++ b/src/core/storage/state-keys.ts @@ -55,6 +55,7 @@ export type GlobalStateKey = | "openRouterProviderSorting" | "autoApprovalSettings" | "globalClineRulesToggles" + | "globalWorkflowToggles" | "browserSettings" | "chatSettings" | "vsCodeLmModelSelector" diff --git a/src/core/storage/state.ts b/src/core/storage/state.ts index 795f1799..cb386266 100644 --- a/src/core/storage/state.ts +++ b/src/core/storage/state.ts @@ -163,6 +163,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { shellIntegrationTimeout, enableCheckpointsSettingRaw, mcpMarketplaceEnabledRaw, + globalWorkflowToggles, ] = await Promise.all([ getGlobalState(context, "isNewUser") as Promise, getGlobalState(context, "apiProvider") as Promise, @@ -251,6 +252,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { getGlobalState(context, "shellIntegrationTimeout") as Promise, getGlobalState(context, "enableCheckpointsSetting") as Promise, getGlobalState(context, "mcpMarketplaceEnabled") as Promise, + getGlobalState(context, "globalWorkflowToggles") as Promise, fetch, ]) @@ -385,6 +387,7 @@ export async function getAllExtensionState(context: vscode.ExtensionContext) { planActSeparateModelsSetting, enableCheckpointsSetting: enableCheckpointsSetting, shellIntegrationTimeout: shellIntegrationTimeout || 4000, + globalWorkflowToggles: globalWorkflowToggles || {}, } } diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 2df6b91b..eb49f9bf 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -4117,7 +4117,7 @@ export class Task { // Track if we need to check clinerulesFile let needsClinerulesFileCheck = false - const workflowToggles = await refreshWorkflowToggles(this.getContext(), cwd) + const { localWorkflowToggles, globalWorkflowToggles } = await refreshWorkflowToggles(this.getContext(), cwd) const processUserContent = async () => { // This is a temporary solution to dynamically load context mentions from tool results. It checks for the presence of tags that indicate that the tool was rejected and feedback was provided (see formatToolDeniedFeedback, attemptCompletion, executeCommand, and consecutiveMistakeCount >= 3) or "" (see askFollowupQuestion), we place all user generated content in these tags so they can effectively be used as markers for when we should parse mentions). However if we allow multiple tools responses in the future, we will need to parse mentions specifically within the user content tags. @@ -4143,7 +4143,8 @@ export class Task { // when parsing slash commands, we still want to allow the user to provide their desired context const { processedText, needsClinerulesFileCheck: needsCheck } = await parseSlashCommands( parsedText, - workflowToggles, + localWorkflowToggles, + globalWorkflowToggles, ) if (needsCheck) { diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index 3bf5cac9..48a4b631 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -140,7 +140,8 @@ export interface ExtensionState { vscMachineId: string globalClineRulesToggles: ClineRulesToggles localClineRulesToggles: ClineRulesToggles - workflowToggles: ClineRulesToggles + localWorkflowToggles: ClineRulesToggles + globalWorkflowToggles: ClineRulesToggles localCursorRulesToggles: ClineRulesToggles localWindsurfRulesToggles: ClineRulesToggles } diff --git a/webview-ui/src/components/chat/ChatTextArea.tsx b/webview-ui/src/components/chat/ChatTextArea.tsx index 10abcc95..8f0fc0c0 100644 --- a/webview-ui/src/components/chat/ChatTextArea.tsx +++ b/webview-ui/src/components/chat/ChatTextArea.tsx @@ -260,7 +260,15 @@ const ChatTextArea = forwardRef( }, ref, ) => { - const { filePaths, chatSettings, apiConfiguration, openRouterModels, platform, workflowToggles } = useExtensionState() + const { + filePaths, + chatSettings, + apiConfiguration, + openRouterModels, + platform, + localWorkflowToggles, + globalWorkflowToggles, + } = useExtensionState() const [isTextAreaFocused, setIsTextAreaFocused] = useState(false) const [isDraggingOver, setIsDraggingOver] = useState(false) const [gitCommits, setGitCommits] = useState([]) @@ -484,7 +492,11 @@ const ChatTextArea = forwardRef( setSelectedSlashCommandsIndex((prevIndex) => { const direction = event.key === "ArrowUp" ? -1 : 1 // Get commands with workflow toggles - const allCommands = getMatchingSlashCommands(slashCommandsQuery, workflowToggles) + const allCommands = getMatchingSlashCommands( + slashCommandsQuery, + localWorkflowToggles, + globalWorkflowToggles, + ) if (allCommands.length === 0) { return prevIndex @@ -502,7 +514,7 @@ const ChatTextArea = forwardRef( if ((event.key === "Enter" || event.key === "Tab") && selectedSlashCommandsIndex !== -1) { event.preventDefault() - const commands = getMatchingSlashCommands(slashCommandsQuery, workflowToggles) + const commands = getMatchingSlashCommands(slashCommandsQuery, localWorkflowToggles, globalWorkflowToggles) if (commands.length > 0) { handleSlashCommandsSelect(commands[selectedSlashCommandsIndex]) } @@ -905,7 +917,7 @@ const ChatTextArea = forwardRef( // extract and validate the exact command text const commandText = processedText.substring(slashIndex + 1, endIndex) - const isValidCommand = validateSlashCommand(commandText, workflowToggles) + const isValidCommand = validateSlashCommand(commandText, localWorkflowToggles, globalWorkflowToggles) if (isValidCommand) { const fullCommand = processedText.substring(slashIndex, endIndex) // includes slash @@ -918,7 +930,7 @@ const ChatTextArea = forwardRef( highlightLayerRef.current.innerHTML = processedText highlightLayerRef.current.scrollTop = textAreaRef.current.scrollTop highlightLayerRef.current.scrollLeft = textAreaRef.current.scrollLeft - }, [workflowToggles]) + }, [localWorkflowToggles, globalWorkflowToggles]) useLayoutEffect(() => { updateHighlights() @@ -1400,7 +1412,8 @@ const ChatTextArea = forwardRef( setSelectedIndex={setSelectedSlashCommandsIndex} onMouseDown={handleMenuMouseDown} query={slashCommandsQuery} - workflowToggles={workflowToggles} + localWorkflowToggles={localWorkflowToggles} + globalWorkflowToggles={globalWorkflowToggles} /> )} diff --git a/webview-ui/src/components/chat/SlashCommandMenu.tsx b/webview-ui/src/components/chat/SlashCommandMenu.tsx index e5c18ecd..5f366b8f 100644 --- a/webview-ui/src/components/chat/SlashCommandMenu.tsx +++ b/webview-ui/src/components/chat/SlashCommandMenu.tsx @@ -7,7 +7,8 @@ interface SlashCommandMenuProps { setSelectedIndex: (index: number) => void onMouseDown: () => void query: string - workflowToggles?: Record + localWorkflowToggles?: Record + globalWorkflowToggles?: Record } const SlashCommandMenu: React.FC = ({ @@ -16,7 +17,8 @@ const SlashCommandMenu: React.FC = ({ setSelectedIndex, onMouseDown, query, - workflowToggles = {}, + localWorkflowToggles = {}, + globalWorkflowToggles = {}, }) => { const menuRef = useRef(null) @@ -44,7 +46,7 @@ const SlashCommandMenu: React.FC = ({ }, [selectedIndex]) // Filter commands based on query - const filteredCommands = getMatchingSlashCommands(query, workflowToggles) + const filteredCommands = getMatchingSlashCommands(query, localWorkflowToggles, globalWorkflowToggles) const defaultCommands = filteredCommands.filter((cmd) => cmd.section === "default" || !cmd.section) const workflowCommands = filteredCommands.filter((cmd) => cmd.section === "custom") diff --git a/webview-ui/src/components/cline-rules/ClineRulesToggleModal.tsx b/webview-ui/src/components/cline-rules/ClineRulesToggleModal.tsx index e0dbd4be..35ee1517 100644 --- a/webview-ui/src/components/cline-rules/ClineRulesToggleModal.tsx +++ b/webview-ui/src/components/cline-rules/ClineRulesToggleModal.tsx @@ -17,12 +17,14 @@ const ClineRulesToggleModal: React.FC = () => { localClineRulesToggles = {}, localCursorRulesToggles = {}, localWindsurfRulesToggles = {}, - workflowToggles = {}, + localWorkflowToggles = {}, + globalWorkflowToggles = {}, setGlobalClineRulesToggles, setLocalClineRulesToggles, setLocalCursorRulesToggles, setLocalWindsurfRulesToggles, - setWorkflowToggles, + setLocalWorkflowToggles, + setGlobalWorkflowToggles, } = useExtensionState() const [isVisible, setIsVisible] = useState(false) const buttonRef = useRef(null) @@ -49,8 +51,11 @@ const ClineRulesToggleModal: React.FC = () => { if (response.localWindsurfRulesToggles?.toggles) { setLocalWindsurfRulesToggles(response.localWindsurfRulesToggles.toggles) } - if (response.workflowToggles?.toggles) { - setWorkflowToggles(response.workflowToggles.toggles) + if (response.localWorkflowToggles?.toggles) { + setLocalWorkflowToggles(response.localWorkflowToggles.toggles) + } + if (response.globalWorkflowToggles?.toggles) { + setGlobalWorkflowToggles(response.globalWorkflowToggles.toggles) } }) .catch((error) => { @@ -77,7 +82,11 @@ const ClineRulesToggleModal: React.FC = () => { .map(([path, enabled]): [string, boolean] => [path, enabled as boolean]) .sort(([a], [b]) => a.localeCompare(b)) - const workflows = Object.entries(workflowToggles || {}) + const localWorkflows = Object.entries(localWorkflowToggles || {}) + .map(([path, enabled]): [string, boolean] => [path, enabled as boolean]) + .sort(([a], [b]) => a.localeCompare(b)) + + const globalWorkflows = Object.entries(globalWorkflowToggles || {}) .map(([path, enabled]): [string, boolean] => [path, enabled as boolean]) .sort(([a], [b]) => a.localeCompare(b)) @@ -133,11 +142,12 @@ const ClineRulesToggleModal: React.FC = () => { }) } - const toggleWorkflow = (workflowPath: string, enabled: boolean) => { + const toggleWorkflow = (isGlobal: boolean, workflowPath: string, enabled: boolean) => { vscode.postMessage({ type: "toggleWorkflow", workflowPath, enabled, + isGlobal, }) } @@ -299,19 +309,35 @@ const ClineRulesToggleModal: React.FC = () => { ) : ( - /* Workflows section */ -
-
Workspace Workflows
- -
+ <> + {/* Global Workflows Section */} +
+
Global Workflows
+ toggleWorkflow(true, rulePath, enabled)} + listGap="small" + isGlobal={true} + ruleType={"workflow"} + showNewRule={true} + showNoRules={false} + /> +
+ + {/* Local Workflows Section */} +
+
Workspace Workflows
+ toggleWorkflow(false, rulePath, enabled)} + listGap="small" + isGlobal={false} + ruleType={"workflow"} + showNewRule={true} + showNoRules={false} + /> +
+ )} )} diff --git a/webview-ui/src/context/ExtensionStateContext.tsx b/webview-ui/src/context/ExtensionStateContext.tsx index d8455df2..6433c99b 100644 --- a/webview-ui/src/context/ExtensionStateContext.tsx +++ b/webview-ui/src/context/ExtensionStateContext.tsx @@ -56,7 +56,8 @@ interface ExtensionStateContextType extends ExtensionState { setLocalClineRulesToggles: (toggles: Record) => void setLocalCursorRulesToggles: (toggles: Record) => void setLocalWindsurfRulesToggles: (toggles: Record) => void - setWorkflowToggles: (toggles: Record) => void + setLocalWorkflowToggles: (toggles: Record) => void + setGlobalWorkflowToggles: (toggles: Record) => void setMcpMarketplaceCatalog: (value: McpMarketplaceCatalog) => void // Navigation state setters @@ -162,7 +163,8 @@ export const ExtensionStateContextProvider: React.FC<{ localClineRulesToggles: {}, localCursorRulesToggles: {}, localWindsurfRulesToggles: {}, - workflowToggles: {}, + localWorkflowToggles: {}, + globalWorkflowToggles: {}, shellIntegrationTimeout: 4000, // default timeout for shell integration isNewUser: false, }) @@ -448,7 +450,8 @@ export const ExtensionStateContextProvider: React.FC<{ localClineRulesToggles: state.localClineRulesToggles || {}, localCursorRulesToggles: state.localCursorRulesToggles || {}, localWindsurfRulesToggles: state.localWindsurfRulesToggles || {}, - workflowToggles: state.workflowToggles || {}, + localWorkflowToggles: state.localWorkflowToggles || {}, + globalWorkflowToggles: state.globalWorkflowToggles || {}, enableCheckpointsSetting: state.enableCheckpointsSetting, // Navigation functions @@ -544,10 +547,15 @@ export const ExtensionStateContextProvider: React.FC<{ ...prevState, localWindsurfRulesToggles: toggles, })), - setWorkflowToggles: (toggles) => + setLocalWorkflowToggles: (toggles) => setState((prevState) => ({ ...prevState, - workflowToggles: toggles, + localWorkflowToggles: toggles, + })), + setGlobalWorkflowToggles: (toggles) => + setState((prevState) => ({ + ...prevState, + globalWorkflowToggles: toggles, })), setMcpTab, } diff --git a/webview-ui/src/utils/slash-commands.ts b/webview-ui/src/utils/slash-commands.ts index 7cd94a72..a7f155a0 100644 --- a/webview-ui/src/utils/slash-commands.ts +++ b/webview-ui/src/utils/slash-commands.ts @@ -27,19 +27,50 @@ export const DEFAULT_SLASH_COMMANDS: SlashCommand[] = [ }, ] -export function getWorkflowCommands(workflowToggles: Record): SlashCommand[] { - return Object.entries(workflowToggles) +export function getWorkflowCommands( + localWorkflowToggles: Record, + globalWorkflowToggles: Record, +): SlashCommand[] { + const { workflows: localWorkflows, nameSet: localWorkflowNames } = Object.entries(localWorkflowToggles) .filter(([_, enabled]) => enabled) - .map(([filePath, _]) => { - // potentially remove the file extension if there is one, but this would then require - // that we prevent users from having the same fname with different extensions + .reduce( + (acc, [filePath, _]) => { + const fileName = filePath.replace(/^.*[/\\]/, "") + + // Add to array of workflows + acc.workflows.push({ + name: fileName, + section: "custom", + } as SlashCommand) + + // Add to set of names + acc.nameSet.add(fileName) + + return acc + }, + { workflows: [] as SlashCommand[], nameSet: new Set() }, + ) + + const globalWorkflows = Object.entries(globalWorkflowToggles) + .filter(([_, enabled]) => enabled) + .flatMap(([filePath, _]) => { const fileName = filePath.replace(/^.*[/\\]/, "") - return { - name: fileName, - section: "custom", + // skip if a local workflow with the same name exists + if (localWorkflowNames.has(fileName)) { + return [] } + + return [ + { + name: fileName, + section: "custom", + }, + ] as SlashCommand[] }) + + const workflows = [...localWorkflows, ...globalWorkflows] + return workflows } // Regex for detecting slash commands in text @@ -102,8 +133,12 @@ export function shouldShowSlashCommandsMenu(text: string, cursorPosition: number /** * Gets filtered slash commands that match the current input */ -export function getMatchingSlashCommands(query: string, workflowToggles: Record = {}): SlashCommand[] { - const workflowCommands = getWorkflowCommands(workflowToggles) +export function getMatchingSlashCommands( + query: string, + localWorkflowToggles: Record = {}, + globalWorkflowToggles: Record = {}, +): SlashCommand[] { + const workflowCommands = getWorkflowCommands(localWorkflowToggles, globalWorkflowToggles) const allCommands = [...DEFAULT_SLASH_COMMANDS, ...workflowCommands] if (!query) { @@ -134,12 +169,16 @@ export function insertSlashCommand(text: string, commandName: string): { newValu * Determines the validation state of a slash command * Returns partial if we have a partial match against valid commands, or full for full match */ -export function validateSlashCommand(command: string, workflowToggles: Record = {}): "full" | "partial" | null { +export function validateSlashCommand( + command: string, + localWorkflowToggles: Record = {}, + globalWorkflowToggles: Record = {}, +): "full" | "partial" | null { if (!command) { return null } - const workflowCommands = getWorkflowCommands(workflowToggles) + const workflowCommands = getWorkflowCommands(localWorkflowToggles, globalWorkflowToggles) const allCommands = [...DEFAULT_SLASH_COMMANDS, ...workflowCommands] // case sensitive matching