feat: custom prompt

This commit is contained in:
duanfuxiang
2025-04-27 13:21:51 +08:00
parent b5a322df31
commit 5558c96aa1
7 changed files with 307 additions and 202 deletions

View File

@@ -1,173 +0,0 @@
import {
CustomModePrompts,
Mode,
ModeConfig,
PromptComponent,
defaultModeSlug,
getGroupName,
getModeBySlug,
modes
} from "../../utils/modes"
import { DiffStrategy } from "../diff/DiffStrategy"
import { McpHub } from "../mcp/McpHub"
import {
addCustomInstructions,
getCapabilitiesSection,
getMcpServersSection,
getModesSection,
getObjectiveSection,
getRulesSection,
getSharedToolUseSection,
getSystemInfoSection,
getToolUseGuidelinesSection,
} from "./sections"
import { getToolDescriptionsForMode } from "./tools"
async function generatePrompt(
cwd: string,
supportsComputerUse: boolean,
mode: Mode,
filesSearchMethod: string,
mcpHub?: McpHub,
diffStrategy?: DiffStrategy,
browserViewportSize?: string,
promptComponent?: PromptComponent,
customModeConfigs?: ModeConfig[],
globalCustomInstructions?: string,
preferredLanguage?: string,
diffEnabled?: boolean,
experiments?: Record<string, boolean>,
enableMcpServerCreation?: boolean,
): 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(),
modeConfig.groups.some((groupEntry) => getGroupName(groupEntry) === "mcp")
? getMcpServersSection(mcpHub, diffStrategy, enableMcpServerCreation)
: Promise.resolve(""),
])
const basePrompt = `${roleDefinition}
${getSharedToolUseSection()}
${getToolDescriptionsForMode(
mode,
cwd,
filesSearchMethod,
supportsComputerUse,
diffStrategy,
browserViewportSize,
mcpHub,
customModeConfigs,
experiments,
)}
${getToolUseGuidelinesSection()}
${mcpServersSection}
${getCapabilitiesSection(
mode,
cwd,
filesSearchMethod,
)}
${modesSection}
${getRulesSection(
mode,
cwd,
filesSearchMethod,
supportsComputerUse,
diffStrategy,
experiments,
)}
${getSystemInfoSection(cwd)}
${getObjectiveSection(mode)}
${await addCustomInstructions(promptComponent?.customInstructions || modeConfig.customInstructions || "", globalCustomInstructions || "", cwd, mode, { preferredLanguage })}`
return basePrompt
}
export const SYSTEM_PROMPT = async (
cwd: string,
supportsComputerUse: boolean,
mode: Mode = defaultModeSlug,
filesSearchMethod: string = 'regex',
preferredLanguage?: string,
diffStrategy?: DiffStrategy,
mcpHub?: McpHub,
browserViewportSize?: string,
customModePrompts?: CustomModePrompts,
customModes?: ModeConfig[],
globalCustomInstructions?: string,
diffEnabled?: boolean,
experiments?: Record<string, boolean>,
enableMcpServerCreation?: boolean,
): 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 fileCustomSystemPrompt = await loadSystemPromptFile(cwd, mode)
// 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
// return `${roleDefinition}
// ${fileCustomSystemPrompt}
// ${await addCustomInstructions(promptComponent?.customInstructions || currentMode.customInstructions || "", globalCustomInstructions || "", cwd, mode, { preferredLanguage })}`
// }
// // If diff is disabled, don't pass the diffStrategy
// const effectiveDiffStrategy = diffEnabled ? diffStrategy : undefined
return generatePrompt(
// context,
cwd,
supportsComputerUse,
currentMode.slug,
filesSearchMethod,
mcpHub,
diffStrategy,
browserViewportSize,
promptComponent,
customModes,
globalCustomInstructions,
preferredLanguage,
diffEnabled,
experiments,
enableMcpServerCreation,
)
}