Skip to content

Commit 1e8eaae

Browse files
committed
feat: implement dynamic loading of prompt sections based on experimental features
1 parent a714282 commit 1e8eaae

File tree

3 files changed

+110
-7
lines changed

3 files changed

+110
-7
lines changed

src/core/prompts/sections/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ export { getToolUseGuidelinesSection } from "./tool-use-guidelines"
88
export { getCapabilitiesSection } from "./capabilities"
99
export { getModesSection } from "./modes"
1010
export { markdownFormattingSection } from "./markdown-formatting"
11+
12+
// Dynamic loading functionality (for future use)
13+
export { loadPromptSection } from "./prompt-loader"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type { Experiments } from "@roo-code/types"
2+
3+
// Import all sections statically
4+
import * as capabilities from "./capabilities"
5+
import * as capabilitiesOptimized from "./capabilities.optimized"
6+
import * as customInstructions from "./custom-instructions"
7+
import * as customSystemPrompt from "./custom-system-prompt"
8+
import * as markdownFormatting from "./markdown-formatting"
9+
import * as mcpServers from "./mcp-servers"
10+
import * as modes from "./modes"
11+
import * as objective from "./objective"
12+
import * as objectiveOptimized from "./objective.optimized"
13+
import * as rules from "./rules"
14+
import * as rulesOptimized from "./rules.optimized"
15+
import * as systemInfo from "./system-info"
16+
import * as systemInfoOptimized from "./system-info.optimized"
17+
import * as toolUse from "./tool-use"
18+
import * as toolUseOptimized from "./tool-use.optimized"
19+
import * as toolUseGuidelines from "./tool-use-guidelines"
20+
import * as toolUseGuidelinesOptimized from "./tool-use-guidelines.optimized"
21+
22+
// Map of section names to their modules
23+
const sectionMap: Record<string, any> = {
24+
capabilities: capabilities,
25+
"capabilities.optimized": capabilitiesOptimized,
26+
"custom-instructions": customInstructions,
27+
"custom-system-prompt": customSystemPrompt,
28+
"markdown-formatting": markdownFormatting,
29+
"mcp-servers": mcpServers,
30+
modes: modes,
31+
objective: objective,
32+
"objective.optimized": objectiveOptimized,
33+
rules: rules,
34+
"rules.optimized": rulesOptimized,
35+
"system-info": systemInfo,
36+
"system-info.optimized": systemInfoOptimized,
37+
"tool-use": toolUse,
38+
"tool-use.optimized": toolUseOptimized,
39+
"tool-use-guidelines": toolUseGuidelines,
40+
"tool-use-guidelines.optimized": toolUseGuidelinesOptimized,
41+
}
42+
43+
/**
44+
* Dynamically loads prompt sections based on experimental features.
45+
* If optimizedPromptFeatures is enabled, loads .optimized.ts files,
46+
* otherwise loads the standard .ts files.
47+
*
48+
* This function provides a clean way to switch between optimized and standard
49+
* prompt sections without breaking compatibility with the main codebase.
50+
*/
51+
export function loadPromptSection(experiments: Experiments | undefined, sectionName: string): any {
52+
const useOptimized = experiments?.optimizedPromptFeatures === true
53+
54+
try {
55+
if (useOptimized) {
56+
// Try to load optimized version first
57+
const optimizedKey = `${sectionName}.optimized`
58+
if (sectionMap[optimizedKey]) {
59+
return sectionMap[optimizedKey]
60+
}
61+
}
62+
63+
// Load standard version or fallback
64+
if (sectionMap[sectionName]) {
65+
return sectionMap[sectionName]
66+
}
67+
68+
// If we couldn't find the requested section, try the opposite
69+
if (useOptimized && sectionMap[sectionName]) {
70+
console.warn(`Optimized version of ${sectionName} not found, falling back to standard version`)
71+
return sectionMap[sectionName]
72+
} else if (!useOptimized && sectionMap[`${sectionName}.optimized`]) {
73+
console.warn(`Standard version of ${sectionName} not found, falling back to optimized version`)
74+
return sectionMap[`${sectionName}.optimized`]
75+
}
76+
77+
throw new Error(`Unable to load prompt section: ${sectionName}`)
78+
} catch (error) {
79+
console.error(`Failed to load prompt section ${sectionName}:`, error)
80+
throw error
81+
}
82+
}

src/core/prompts/system.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
getModesSection,
2525
addCustomInstructions,
2626
markdownFormattingSection,
27+
loadPromptSection,
2728
} from "./sections"
2829

2930
async function generatePrompt(
@@ -38,7 +39,7 @@ async function generatePrompt(
3839
customModeConfigs?: ModeConfig[],
3940
globalCustomInstructions?: string,
4041
diffEnabled?: boolean,
41-
experiments?: Record<string, boolean>,
42+
experiments?: Record<string, boolean> | import("@roo-code/types").Experiments,
4243
enableMcpServerCreation?: boolean,
4344
language?: string,
4445
rooIgnoreInstructions?: string,
@@ -89,8 +90,12 @@ async function generatePrompt(
8990
promptSections.push(markdownFormattingSection())
9091
}
9192

93+
// Load sections dynamically based on experiments
94+
const experimentsTyped = experiments as import("@roo-code/types").Experiments | undefined
95+
9296
if (promptSettings.toolUseEnabled) {
93-
promptSections.push(getSharedToolUseSection())
97+
const toolUseModule = loadPromptSection(experimentsTyped, "tool-use")
98+
promptSections.push(toolUseModule.getSharedToolUseSection())
9499
promptSections.push(
95100
getToolDescriptionsForMode(
96101
mode,
@@ -109,16 +114,24 @@ async function generatePrompt(
109114
}
110115

111116
if (promptSettings.toolUseGuidelinesEnabled) {
112-
promptSections.push(getToolUseGuidelinesSection(codeIndexManager))
117+
const toolUseGuidelinesModule = loadPromptSection(experimentsTyped, "tool-use-guidelines")
118+
promptSections.push(toolUseGuidelinesModule.getToolUseGuidelinesSection(codeIndexManager))
113119
}
114120

115121
if (promptSettings.mcpServersEnabled) {
116122
promptSections.push(mcpServersSection)
117123
}
118124

119125
if (promptSettings.capabilitiesEnabled) {
126+
const capabilitiesModule = loadPromptSection(experimentsTyped, "capabilities")
120127
promptSections.push(
121-
getCapabilitiesSection(cwd, supportsComputerUse, mcpHub, effectiveDiffStrategy, codeIndexManager),
128+
capabilitiesModule.getCapabilitiesSection(
129+
cwd,
130+
supportsComputerUse,
131+
mcpHub,
132+
effectiveDiffStrategy,
133+
codeIndexManager,
134+
),
122135
)
123136
}
124137

@@ -127,15 +140,20 @@ async function generatePrompt(
127140
}
128141

129142
if (promptSettings.rulesEnabled) {
130-
promptSections.push(getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, codeIndexManager))
143+
const rulesModule = loadPromptSection(experimentsTyped, "rules")
144+
promptSections.push(
145+
rulesModule.getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, codeIndexManager),
146+
)
131147
}
132148

133149
if (promptSettings.systemInfoEnabled) {
134-
promptSections.push(getSystemInfoSection(cwd))
150+
const systemInfoModule = loadPromptSection(experimentsTyped, "system-info")
151+
promptSections.push(systemInfoModule.getSystemInfoSection(cwd))
135152
}
136153

137154
if (promptSettings.objectiveEnabled) {
138-
promptSections.push(getObjectiveSection(codeIndexManager, experiments))
155+
const objectiveModule = loadPromptSection(experimentsTyped, "objective")
156+
promptSections.push(objectiveModule.getObjectiveSection(codeIndexManager, experiments))
139157
}
140158

141159
if (promptSettings.customInstructionsEnabled) {

0 commit comments

Comments
 (0)