| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import {
- Mode,
- modes,
- CustomModePrompts,
- PromptComponent,
- getRoleDefinition,
- defaultModeSlug,
- ModeConfig,
- getModeBySlug,
- getGroupName,
- } from "../../shared/modes"
- import { PromptVariables } from "./sections/custom-system-prompt"
- import { DiffStrategy } from "../../shared/tools"
- import { McpHub } from "../../services/mcp/McpHub"
- import { getToolDescriptionsForMode } from "./tools"
- import * as vscode from "vscode"
- import * as os from "os"
- import {
- getRulesSection,
- getSystemInfoSection,
- getObjectiveSection,
- getSharedToolUseSection,
- getMcpServersSection,
- getToolUseGuidelinesSection,
- getCapabilitiesSection,
- getModesSection,
- addCustomInstructions,
- } from "./sections"
- import { loadSystemPromptFile } from "./sections/custom-system-prompt"
- import { formatLanguage } from "../../shared/language"
- async function generatePrompt(
- context: vscode.ExtensionContext,
- cwd: string,
- supportsComputerUse: boolean,
- mode: Mode,
- mcpHub?: McpHub,
- diffStrategy?: DiffStrategy,
- browserViewportSize?: string,
- promptComponent?: PromptComponent,
- customModeConfigs?: ModeConfig[],
- globalCustomInstructions?: string,
- diffEnabled?: boolean,
- experiments?: Record<string, boolean>,
- enableMcpServerCreation?: boolean,
- language?: string,
- rooIgnoreInstructions?: string,
- ): Promise<string> {
- if (!context) {
- throw new Error("Extension context is required for generating system prompt")
- }
- // If diff is disabled, don't pass the diffStrategy
- const effectiveDiffStrategy = diffEnabled ? diffStrategy : undefined
- // Get the full mode config to ensure we have the role definition
- const modeConfig = getModeBySlug(mode, customModeConfigs) || modes.find((m) => m.slug === mode) || modes[0]
- const roleDefinition = promptComponent?.roleDefinition || modeConfig.roleDefinition
- const [modesSection, mcpServersSection] = await Promise.all([
- getModesSection(context),
- modeConfig.groups.some((groupEntry) => getGroupName(groupEntry) === "mcp")
- ? getMcpServersSection(mcpHub, effectiveDiffStrategy, enableMcpServerCreation)
- : Promise.resolve(""),
- ])
- const basePrompt = `${roleDefinition}
- ${getSharedToolUseSection()}
- ${getToolDescriptionsForMode(
- mode,
- cwd,
- supportsComputerUse,
- effectiveDiffStrategy,
- browserViewportSize,
- mcpHub,
- customModeConfigs,
- experiments,
- )}
- ${getToolUseGuidelinesSection()}
- ${mcpServersSection}
- ${getCapabilitiesSection(cwd, supportsComputerUse, mcpHub, effectiveDiffStrategy)}
- ${modesSection}
- ${getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, experiments)}
- ${getSystemInfoSection(cwd, mode, customModeConfigs)}
- ${getObjectiveSection()}
- ${await addCustomInstructions(promptComponent?.customInstructions || modeConfig.customInstructions || "", globalCustomInstructions || "", cwd, mode, { language: language ?? formatLanguage(vscode.env.language), rooIgnoreInstructions })}`
- return basePrompt
- }
- export const SYSTEM_PROMPT = async (
- context: vscode.ExtensionContext,
- cwd: string,
- supportsComputerUse: boolean,
- mcpHub?: McpHub,
- diffStrategy?: DiffStrategy,
- browserViewportSize?: string,
- mode: Mode = defaultModeSlug,
- customModePrompts?: CustomModePrompts,
- customModes?: ModeConfig[],
- globalCustomInstructions?: string,
- diffEnabled?: boolean,
- experiments?: Record<string, boolean>,
- enableMcpServerCreation?: boolean,
- language?: string,
- rooIgnoreInstructions?: string,
- ): Promise<string> => {
- if (!context) {
- throw new Error("Extension context is required for generating system prompt")
- }
- const getPromptComponent = (value: unknown) => {
- if (typeof value === "object" && value !== null) {
- return value as PromptComponent
- }
- return undefined
- }
- // Try to load custom system prompt from file
- const variablesForPrompt: PromptVariables = {
- workspace: cwd,
- mode: mode,
- language: language ?? formatLanguage(vscode.env.language),
- shell: vscode.env.shell,
- operatingSystem: os.type(),
- }
- const fileCustomSystemPrompt = await loadSystemPromptFile(cwd, mode, variablesForPrompt)
- // Check if it's a custom mode
- const promptComponent = getPromptComponent(customModePrompts?.[mode])
- // Get full mode config from custom modes or fall back to built-in modes
- const currentMode = getModeBySlug(mode, customModes) || modes.find((m) => m.slug === mode) || modes[0]
- // If a file-based custom system prompt exists, use it
- if (fileCustomSystemPrompt) {
- const roleDefinition = promptComponent?.roleDefinition || currentMode.roleDefinition
- const customInstructions = await addCustomInstructions(
- promptComponent?.customInstructions || currentMode.customInstructions || "",
- globalCustomInstructions || "",
- cwd,
- mode,
- { language: language ?? formatLanguage(vscode.env.language), rooIgnoreInstructions },
- )
- // For file-based prompts, don't include the tool sections
- return `${roleDefinition}
- ${fileCustomSystemPrompt}
- ${customInstructions}`
- }
- // If diff is disabled, don't pass the diffStrategy
- const effectiveDiffStrategy = diffEnabled ? diffStrategy : undefined
- return generatePrompt(
- context,
- cwd,
- supportsComputerUse,
- currentMode.slug,
- mcpHub,
- effectiveDiffStrategy,
- browserViewportSize,
- promptComponent,
- customModes,
- globalCustomInstructions,
- diffEnabled,
- experiments,
- enableMcpServerCreation,
- language,
- rooIgnoreInstructions,
- )
- }
|