-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathsystem.ts
More file actions
165 lines (137 loc) · 4.83 KB
/
system.ts
File metadata and controls
165 lines (137 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as vscode from "vscode"
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 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,
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")
let hasMcpServers = mcpHub && mcpHub.getServers().length > 0
// If this mode has an allowedMcpServers allowlist, check that at least one allowed server exists
if (hasMcpServers && modeConfig.allowedMcpServers && modeConfig.allowedMcpServers.length > 0) {
const allowedSet = new Set(modeConfig.allowedMcpServers)
hasMcpServers = mcpHub!.getServers().some((server) => allowedSet.has(server.name))
}
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,
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")
}
// 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]
return generatePrompt(
context,
cwd,
supportsComputerUse,
currentMode.slug,
mcpHub,
diffStrategy,
promptComponent,
customModes,
globalCustomInstructions,
experiments,
language,
rooIgnoreInstructions,
settings,
todoList,
modelId,
skillsManager,
)
}