| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- import * as vscode from "vscode"
- import * as os from "os"
- import { type ModeConfig, type PromptComponent, type CustomModePrompts, type TodoItem } from "@roo-code/types"
- import { Mode, modes, defaultModeSlug, getModeBySlug, getGroupName, getModeSelection } from "../../shared/modes"
- import { DiffStrategy } from "../../shared/tools"
- import { formatLanguage } from "../../shared/language"
- import { isEmpty } from "../../utils/object"
- import { McpHub } from "../../services/mcp/McpHub"
- import { CodeIndexManager } from "../../services/code-index/manager"
- import { SkillsManager } from "../../services/skills/SkillsManager"
- import { PromptVariables, loadSystemPromptFile } from "./sections/custom-system-prompt"
- import type { SystemPromptSettings } from "./types"
- import {
- getRulesSection,
- getSystemInfoSection,
- getObjectiveSection,
- getSharedToolUseSection,
- getToolUseGuidelinesSection,
- getCapabilitiesSection,
- getModesSection,
- addCustomInstructions,
- markdownFormattingSection,
- getSkillsSection,
- } from "./sections"
- // Helper function to get prompt component, filtering out empty objects
- export function getPromptComponent(
- customModePrompts: CustomModePrompts | undefined,
- mode: string,
- ): PromptComponent | undefined {
- const component = customModePrompts?.[mode]
- // Return undefined if component is empty
- if (isEmpty(component)) {
- return undefined
- }
- return component
- }
- async function generatePrompt(
- context: vscode.ExtensionContext,
- cwd: string,
- supportsComputerUse: boolean,
- mode: Mode,
- mcpHub?: McpHub,
- diffStrategy?: DiffStrategy,
- browserViewportSize?: string,
- promptComponent?: PromptComponent,
- customModeConfigs?: ModeConfig[],
- globalCustomInstructions?: string,
- experiments?: Record<string, boolean>,
- language?: string,
- rooIgnoreInstructions?: string,
- settings?: SystemPromptSettings,
- todoList?: TodoItem[],
- modelId?: string,
- skillsManager?: SkillsManager,
- ): Promise<string> {
- if (!context) {
- throw new Error("Extension context is required for generating system prompt")
- }
- // Get the full mode config to ensure we have the role definition (used for groups, etc.)
- const modeConfig = getModeBySlug(mode, customModeConfigs) || modes.find((m) => m.slug === mode) || modes[0]
- const { roleDefinition, baseInstructions } = getModeSelection(mode, promptComponent, customModeConfigs)
- // Check if MCP functionality should be included
- const hasMcpGroup = modeConfig.groups.some((groupEntry) => getGroupName(groupEntry) === "mcp")
- const hasMcpServers = mcpHub && mcpHub.getServers().length > 0
- const shouldIncludeMcp = hasMcpGroup && hasMcpServers
- const codeIndexManager = CodeIndexManager.getInstance(context, cwd)
- // Tool calling is native-only.
- const effectiveProtocol = "native"
- const [modesSection, skillsSection] = await Promise.all([
- getModesSection(context),
- getSkillsSection(skillsManager, mode as string),
- ])
- // Tools catalog is not included in the system prompt.
- const toolsCatalog = ""
- const basePrompt = `${roleDefinition}
- ${markdownFormattingSection()}
- ${getSharedToolUseSection()}${toolsCatalog}
- ${getToolUseGuidelinesSection()}
- ${getCapabilitiesSection(cwd, shouldIncludeMcp ? mcpHub : undefined)}
- ${modesSection}
- ${skillsSection ? `\n${skillsSection}` : ""}
- ${getRulesSection(cwd, settings)}
- ${getSystemInfoSection(cwd)}
- ${getObjectiveSection()}
- ${await addCustomInstructions(baseInstructions, globalCustomInstructions || "", cwd, mode, {
- language: language ?? formatLanguage(vscode.env.language),
- rooIgnoreInstructions,
- settings,
- })}`
- 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,
- experiments?: Record<string, boolean>,
- language?: string,
- rooIgnoreInstructions?: string,
- settings?: SystemPromptSettings,
- todoList?: TodoItem[],
- modelId?: string,
- skillsManager?: SkillsManager,
- ): Promise<string> => {
- if (!context) {
- throw new Error("Extension context is required for generating system prompt")
- }
- // 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, baseInstructions: baseInstructionsForFile } = getModeSelection(
- mode,
- promptComponent,
- customModes,
- )
- const customInstructions = await addCustomInstructions(
- baseInstructionsForFile,
- globalCustomInstructions || "",
- cwd,
- mode,
- {
- language: language ?? formatLanguage(vscode.env.language),
- rooIgnoreInstructions,
- settings,
- },
- )
- // For file-based prompts, don't include the tool sections
- return `${roleDefinition}
- ${fileCustomSystemPrompt}
- ${customInstructions}`
- }
- return generatePrompt(
- context,
- cwd,
- supportsComputerUse,
- currentMode.slug,
- mcpHub,
- diffStrategy,
- browserViewportSize,
- promptComponent,
- customModes,
- globalCustomInstructions,
- experiments,
- language,
- rooIgnoreInstructions,
- settings,
- todoList,
- modelId,
- skillsManager,
- )
- }
|