|
| 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 | +} |
0 commit comments