system.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {
  2. Mode,
  3. modes,
  4. CustomModePrompts,
  5. PromptComponent,
  6. getRoleDefinition,
  7. defaultModeSlug,
  8. ModeConfig,
  9. getModeBySlug,
  10. getGroupName,
  11. } from "../../shared/modes"
  12. import { PromptVariables } from "./sections/custom-system-prompt"
  13. import { DiffStrategy } from "../../shared/tools"
  14. import { McpHub } from "../../services/mcp/McpHub"
  15. import { getToolDescriptionsForMode } from "./tools"
  16. import * as vscode from "vscode"
  17. import * as os from "os"
  18. import {
  19. getRulesSection,
  20. getSystemInfoSection,
  21. getObjectiveSection,
  22. getSharedToolUseSection,
  23. getMcpServersSection,
  24. getToolUseGuidelinesSection,
  25. getCapabilitiesSection,
  26. getModesSection,
  27. addCustomInstructions,
  28. } from "./sections"
  29. import { loadSystemPromptFile } from "./sections/custom-system-prompt"
  30. import { formatLanguage } from "../../shared/language"
  31. async function generatePrompt(
  32. context: vscode.ExtensionContext,
  33. cwd: string,
  34. supportsComputerUse: boolean,
  35. mode: Mode,
  36. mcpHub?: McpHub,
  37. diffStrategy?: DiffStrategy,
  38. browserViewportSize?: string,
  39. promptComponent?: PromptComponent,
  40. customModeConfigs?: ModeConfig[],
  41. globalCustomInstructions?: string,
  42. diffEnabled?: boolean,
  43. experiments?: Record<string, boolean>,
  44. enableMcpServerCreation?: boolean,
  45. language?: string,
  46. rooIgnoreInstructions?: string,
  47. ): Promise<string> {
  48. if (!context) {
  49. throw new Error("Extension context is required for generating system prompt")
  50. }
  51. // If diff is disabled, don't pass the diffStrategy
  52. const effectiveDiffStrategy = diffEnabled ? diffStrategy : undefined
  53. // Get the full mode config to ensure we have the role definition
  54. const modeConfig = getModeBySlug(mode, customModeConfigs) || modes.find((m) => m.slug === mode) || modes[0]
  55. const roleDefinition = promptComponent?.roleDefinition || modeConfig.roleDefinition
  56. const [modesSection, mcpServersSection] = await Promise.all([
  57. getModesSection(context),
  58. modeConfig.groups.some((groupEntry) => getGroupName(groupEntry) === "mcp")
  59. ? getMcpServersSection(mcpHub, effectiveDiffStrategy, enableMcpServerCreation)
  60. : Promise.resolve(""),
  61. ])
  62. const basePrompt = `${roleDefinition}
  63. ${getSharedToolUseSection()}
  64. ${getToolDescriptionsForMode(
  65. mode,
  66. cwd,
  67. supportsComputerUse,
  68. effectiveDiffStrategy,
  69. browserViewportSize,
  70. mcpHub,
  71. customModeConfigs,
  72. experiments,
  73. )}
  74. ${getToolUseGuidelinesSection()}
  75. ${mcpServersSection}
  76. ${getCapabilitiesSection(cwd, supportsComputerUse, mcpHub, effectiveDiffStrategy)}
  77. ${modesSection}
  78. ${getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, experiments)}
  79. ${getSystemInfoSection(cwd, mode, customModeConfigs)}
  80. ${getObjectiveSection()}
  81. ${await addCustomInstructions(promptComponent?.customInstructions || modeConfig.customInstructions || "", globalCustomInstructions || "", cwd, mode, { language: language ?? formatLanguage(vscode.env.language), rooIgnoreInstructions })}`
  82. return basePrompt
  83. }
  84. export const SYSTEM_PROMPT = async (
  85. context: vscode.ExtensionContext,
  86. cwd: string,
  87. supportsComputerUse: boolean,
  88. mcpHub?: McpHub,
  89. diffStrategy?: DiffStrategy,
  90. browserViewportSize?: string,
  91. mode: Mode = defaultModeSlug,
  92. customModePrompts?: CustomModePrompts,
  93. customModes?: ModeConfig[],
  94. globalCustomInstructions?: string,
  95. diffEnabled?: boolean,
  96. experiments?: Record<string, boolean>,
  97. enableMcpServerCreation?: boolean,
  98. language?: string,
  99. rooIgnoreInstructions?: string,
  100. ): Promise<string> => {
  101. if (!context) {
  102. throw new Error("Extension context is required for generating system prompt")
  103. }
  104. const getPromptComponent = (value: unknown) => {
  105. if (typeof value === "object" && value !== null) {
  106. return value as PromptComponent
  107. }
  108. return undefined
  109. }
  110. // Try to load custom system prompt from file
  111. const variablesForPrompt: PromptVariables = {
  112. workspace: cwd,
  113. mode: mode,
  114. language: language ?? formatLanguage(vscode.env.language),
  115. shell: vscode.env.shell,
  116. operatingSystem: os.type(),
  117. }
  118. const fileCustomSystemPrompt = await loadSystemPromptFile(cwd, mode, variablesForPrompt)
  119. // Check if it's a custom mode
  120. const promptComponent = getPromptComponent(customModePrompts?.[mode])
  121. // Get full mode config from custom modes or fall back to built-in modes
  122. const currentMode = getModeBySlug(mode, customModes) || modes.find((m) => m.slug === mode) || modes[0]
  123. // If a file-based custom system prompt exists, use it
  124. if (fileCustomSystemPrompt) {
  125. const roleDefinition = promptComponent?.roleDefinition || currentMode.roleDefinition
  126. const customInstructions = await addCustomInstructions(
  127. promptComponent?.customInstructions || currentMode.customInstructions || "",
  128. globalCustomInstructions || "",
  129. cwd,
  130. mode,
  131. { language: language ?? formatLanguage(vscode.env.language), rooIgnoreInstructions },
  132. )
  133. // For file-based prompts, don't include the tool sections
  134. return `${roleDefinition}
  135. ${fileCustomSystemPrompt}
  136. ${customInstructions}`
  137. }
  138. // If diff is disabled, don't pass the diffStrategy
  139. const effectiveDiffStrategy = diffEnabled ? diffStrategy : undefined
  140. return generatePrompt(
  141. context,
  142. cwd,
  143. supportsComputerUse,
  144. currentMode.slug,
  145. mcpHub,
  146. effectiveDiffStrategy,
  147. browserViewportSize,
  148. promptComponent,
  149. customModes,
  150. globalCustomInstructions,
  151. diffEnabled,
  152. experiments,
  153. enableMcpServerCreation,
  154. language,
  155. rooIgnoreInstructions,
  156. )
  157. }