modes.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import * as vscode from "vscode"
  2. import { GroupOptions, GroupEntry, ModeConfig, PromptComponent, CustomModePrompts } from "../exports/roo-code"
  3. import { TOOL_GROUPS, ToolGroup, ALWAYS_AVAILABLE_TOOLS } from "./tool-groups"
  4. import { addCustomInstructions } from "../core/prompts/sections/custom-instructions"
  5. export type Mode = string
  6. export type { GroupOptions, GroupEntry, ModeConfig, PromptComponent, CustomModePrompts }
  7. // Helper to extract group name regardless of format
  8. export function getGroupName(group: GroupEntry): ToolGroup {
  9. if (typeof group === "string") {
  10. return group
  11. }
  12. return group[0]
  13. }
  14. // Helper to get group options if they exist
  15. function getGroupOptions(group: GroupEntry): GroupOptions | undefined {
  16. return Array.isArray(group) ? group[1] : undefined
  17. }
  18. // Helper to check if a file path matches a regex pattern
  19. export function doesFileMatchRegex(filePath: string, pattern: string): boolean {
  20. try {
  21. const regex = new RegExp(pattern)
  22. return regex.test(filePath)
  23. } catch (error) {
  24. console.error(`Invalid regex pattern: ${pattern}`, error)
  25. return false
  26. }
  27. }
  28. // Helper to get all tools for a mode
  29. export function getToolsForMode(groups: readonly GroupEntry[]): string[] {
  30. const tools = new Set<string>()
  31. // Add tools from each group
  32. groups.forEach((group) => {
  33. const groupName = getGroupName(group)
  34. const groupConfig = TOOL_GROUPS[groupName]
  35. groupConfig.tools.forEach((tool: string) => tools.add(tool))
  36. })
  37. // Always add required tools
  38. ALWAYS_AVAILABLE_TOOLS.forEach((tool) => tools.add(tool))
  39. return Array.from(tools)
  40. }
  41. // Main modes configuration as an ordered array
  42. export const modes: readonly ModeConfig[] = [
  43. {
  44. slug: "code",
  45. name: "Code",
  46. roleDefinition:
  47. "You are Roo, a highly skilled software engineer with extensive knowledge in many programming languages, frameworks, design patterns, and best practices.",
  48. groups: ["read", "edit", "browser", "command", "mcp"],
  49. },
  50. {
  51. slug: "architect",
  52. name: "Architect",
  53. roleDefinition:
  54. "You are Roo, an experienced technical leader who is inquisitive and an excellent planner. Your goal is to gather information and get context to create a detailed plan for accomplishing the user's task, which the user will review and approve before they switch into another mode to implement the solution.",
  55. groups: ["read", ["edit", { fileRegex: "\\.md$", description: "Markdown files only" }], "browser", "mcp"],
  56. customInstructions:
  57. "1. Do some information gathering (for example using read_file or search_files) to get more context about the task.\n\n2. You should also ask the user clarifying questions to get a better understanding of the task.\n\n3. Once you've gained more context about the user's request, you should create a detailed plan for how to accomplish the task. Include Mermaid diagrams if they help make your plan clearer.\n\n4. Ask the user if they are pleased with this plan, or if they would like to make any changes. Think of this as a brainstorming session where you can discuss the task and plan the best way to accomplish it.\n\n5. Once the user confirms the plan, ask them if they'd like you to write it to a markdown file.\n\n6. Use the switch_mode tool to request that the user switch to another mode to implement the solution.",
  58. },
  59. {
  60. slug: "ask",
  61. name: "Ask",
  62. roleDefinition:
  63. "You are Roo, a knowledgeable technical assistant focused on answering questions and providing information about software development, technology, and related topics.",
  64. groups: ["read", "browser", "mcp"],
  65. customInstructions:
  66. "You can analyze code, explain concepts, and access external resources. Make sure to answer the user's questions and don't rush to switch to implementing code. Include Mermaid diagrams if they help make your response clearer.",
  67. },
  68. {
  69. slug: "debug",
  70. name: "Debug",
  71. roleDefinition:
  72. "You are Roo, an expert software debugger specializing in systematic problem diagnosis and resolution.",
  73. groups: ["read", "edit", "browser", "command", "mcp"],
  74. customInstructions:
  75. "Reflect on 5-7 different possible sources of the problem, distill those down to 1-2 most likely sources, and then add logs to validate your assumptions. Explicitly ask the user to confirm the diagnosis before fixing the problem.",
  76. },
  77. ] as const
  78. // Export the default mode slug
  79. export const defaultModeSlug = modes[0].slug
  80. // Helper functions
  81. export function getModeBySlug(slug: string, customModes?: ModeConfig[]): ModeConfig | undefined {
  82. // Check custom modes first
  83. const customMode = customModes?.find((mode) => mode.slug === slug)
  84. if (customMode) {
  85. return customMode
  86. }
  87. // Then check built-in modes
  88. return modes.find((mode) => mode.slug === slug)
  89. }
  90. export function getModeConfig(slug: string, customModes?: ModeConfig[]): ModeConfig {
  91. const mode = getModeBySlug(slug, customModes)
  92. if (!mode) {
  93. throw new Error(`No mode found for slug: ${slug}`)
  94. }
  95. return mode
  96. }
  97. // Get all available modes, with custom modes overriding built-in modes
  98. export function getAllModes(customModes?: ModeConfig[]): ModeConfig[] {
  99. if (!customModes?.length) {
  100. return [...modes]
  101. }
  102. // Start with built-in modes
  103. const allModes = [...modes]
  104. // Process custom modes
  105. customModes.forEach((customMode) => {
  106. const index = allModes.findIndex((mode) => mode.slug === customMode.slug)
  107. if (index !== -1) {
  108. // Override existing mode
  109. allModes[index] = customMode
  110. } else {
  111. // Add new mode
  112. allModes.push(customMode)
  113. }
  114. })
  115. return allModes
  116. }
  117. // Check if a mode is custom or an override
  118. export function isCustomMode(slug: string, customModes?: ModeConfig[]): boolean {
  119. return !!customModes?.some((mode) => mode.slug === slug)
  120. }
  121. // Custom error class for file restrictions
  122. export class FileRestrictionError extends Error {
  123. constructor(mode: string, pattern: string, description: string | undefined, filePath: string) {
  124. super(
  125. `This mode (${mode}) can only edit files matching pattern: ${pattern}${description ? ` (${description})` : ""}. Got: ${filePath}`,
  126. )
  127. this.name = "FileRestrictionError"
  128. }
  129. }
  130. export function isToolAllowedForMode(
  131. tool: string,
  132. modeSlug: string,
  133. customModes: ModeConfig[],
  134. toolRequirements?: Record<string, boolean>,
  135. toolParams?: Record<string, any>, // All tool parameters
  136. experiments?: Record<string, boolean>,
  137. ): boolean {
  138. // Always allow these tools
  139. if (ALWAYS_AVAILABLE_TOOLS.includes(tool as any)) {
  140. return true
  141. }
  142. if (experiments && tool in experiments) {
  143. if (!experiments[tool]) {
  144. return false
  145. }
  146. }
  147. // Check tool requirements if any exist
  148. if (toolRequirements && typeof toolRequirements === "object") {
  149. if (tool in toolRequirements && !toolRequirements[tool]) {
  150. return false
  151. }
  152. } else if (toolRequirements === false) {
  153. // If toolRequirements is a boolean false, all tools are disabled
  154. return false
  155. }
  156. const mode = getModeBySlug(modeSlug, customModes)
  157. if (!mode) {
  158. return false
  159. }
  160. // Check if tool is in any of the mode's groups and respects any group options
  161. for (const group of mode.groups) {
  162. const groupName = getGroupName(group)
  163. const options = getGroupOptions(group)
  164. const groupConfig = TOOL_GROUPS[groupName]
  165. // If the tool isn't in this group's tools, continue to next group
  166. if (!groupConfig.tools.includes(tool)) {
  167. continue
  168. }
  169. // If there are no options, allow the tool
  170. if (!options) {
  171. return true
  172. }
  173. // For the edit group, check file regex if specified
  174. if (groupName === "edit" && options.fileRegex) {
  175. const filePath = toolParams?.path
  176. if (
  177. filePath &&
  178. (toolParams.diff || toolParams.content || toolParams.operations) &&
  179. !doesFileMatchRegex(filePath, options.fileRegex)
  180. ) {
  181. throw new FileRestrictionError(mode.name, options.fileRegex, options.description, filePath)
  182. }
  183. }
  184. return true
  185. }
  186. return false
  187. }
  188. // Create the mode-specific default prompts
  189. export const defaultPrompts: Readonly<CustomModePrompts> = Object.freeze(
  190. Object.fromEntries(
  191. modes.map((mode) => [
  192. mode.slug,
  193. {
  194. roleDefinition: mode.roleDefinition,
  195. customInstructions: mode.customInstructions,
  196. },
  197. ]),
  198. ),
  199. )
  200. // Helper function to get all modes with their prompt overrides from extension state
  201. export async function getAllModesWithPrompts(context: vscode.ExtensionContext): Promise<ModeConfig[]> {
  202. const customModes = (await context.globalState.get<ModeConfig[]>("customModes")) || []
  203. const customModePrompts = (await context.globalState.get<CustomModePrompts>("customModePrompts")) || {}
  204. const allModes = getAllModes(customModes)
  205. return allModes.map((mode) => ({
  206. ...mode,
  207. roleDefinition: customModePrompts[mode.slug]?.roleDefinition ?? mode.roleDefinition,
  208. customInstructions: customModePrompts[mode.slug]?.customInstructions ?? mode.customInstructions,
  209. }))
  210. }
  211. // Helper function to get complete mode details with all overrides
  212. export async function getFullModeDetails(
  213. modeSlug: string,
  214. customModes?: ModeConfig[],
  215. customModePrompts?: CustomModePrompts,
  216. options?: {
  217. cwd?: string
  218. globalCustomInstructions?: string
  219. language?: string
  220. },
  221. ): Promise<ModeConfig> {
  222. // First get the base mode config from custom modes or built-in modes
  223. const baseMode = getModeBySlug(modeSlug, customModes) || modes.find((m) => m.slug === modeSlug) || modes[0]
  224. // Check for any prompt component overrides
  225. const promptComponent = customModePrompts?.[modeSlug]
  226. // Get the base custom instructions
  227. const baseCustomInstructions = promptComponent?.customInstructions || baseMode.customInstructions || ""
  228. // If we have cwd, load and combine all custom instructions
  229. let fullCustomInstructions = baseCustomInstructions
  230. if (options?.cwd) {
  231. fullCustomInstructions = await addCustomInstructions(
  232. baseCustomInstructions,
  233. options.globalCustomInstructions || "",
  234. options.cwd,
  235. modeSlug,
  236. { language: options.language },
  237. )
  238. }
  239. // Return mode with any overrides applied
  240. return {
  241. ...baseMode,
  242. roleDefinition: promptComponent?.roleDefinition || baseMode.roleDefinition,
  243. customInstructions: fullCustomInstructions,
  244. }
  245. }
  246. // Helper function to safely get role definition
  247. export function getRoleDefinition(modeSlug: string, customModes?: ModeConfig[]): string {
  248. const mode = getModeBySlug(modeSlug, customModes)
  249. if (!mode) {
  250. console.warn(`No mode found for slug: ${modeSlug}`)
  251. return ""
  252. }
  253. return mode.roleDefinition
  254. }
  255. // Helper function to safely get custom instructions
  256. export function getCustomInstructions(modeSlug: string, customModes?: ModeConfig[]): string {
  257. const mode = getModeBySlug(modeSlug, customModes)
  258. if (!mode) {
  259. console.warn(`No mode found for slug: ${modeSlug}`)
  260. return ""
  261. }
  262. return mode.customInstructions ?? ""
  263. }