refactor: separate mode and support prompts

- Rename customPrompts to customModePrompts for mode-specific prompts
- Add new customSupportPrompts type for support action prompts
- Update types to be more specific (CustomModePrompts and CustomSupportPrompts)
- Fix all related tests and component implementations
This commit is contained in:
sam hoang
2025-01-24 01:46:33 +07:00
parent 085d42873c
commit f86e96d157
13 changed files with 119 additions and 99 deletions

View File

@@ -4,7 +4,8 @@ import { ApiConfiguration, ApiProvider, ModelInfo } from "./api"
import { HistoryItem } from "./HistoryItem"
import { McpServer } from "./mcp"
import { GitCommit } from "../utils/git"
import { Mode, CustomPrompts, ModeConfig } from "./modes"
import { Mode, CustomModePrompts, ModeConfig } from "./modes"
import { CustomSupportPrompts } from "./support-prompt"
export interface LanguageModelChatSelector {
vendor?: string
@@ -82,7 +83,8 @@ export interface ExtensionState {
currentApiConfigName?: string
listApiConfigMeta?: ApiConfigMeta[]
customInstructions?: string
customPrompts?: CustomPrompts
customModePrompts?: CustomModePrompts
customSupportPrompts?: CustomSupportPrompts
alwaysAllowReadOnly?: boolean
alwaysAllowWrite?: boolean
alwaysAllowExecute?: boolean

View File

@@ -97,18 +97,18 @@ describe("Code Action Prompts", () => {
it("should return custom template when provided", () => {
const customTemplate = "Custom template for explaining code"
const customPrompts = {
const customSupportPrompts = {
EXPLAIN: customTemplate,
}
const template = supportPrompt.get(customPrompts, "EXPLAIN")
const template = supportPrompt.get(customSupportPrompts, "EXPLAIN")
expect(template).toBe(customTemplate)
})
it("should return default template when custom prompts does not include type", () => {
const customPrompts = {
const customSupportPrompts = {
SOMETHING_ELSE: "Other template",
}
const template = supportPrompt.get(customPrompts, "EXPLAIN")
const template = supportPrompt.get(customSupportPrompts, "EXPLAIN")
expect(template).toBe(supportPrompt.default.EXPLAIN)
})
})
@@ -116,7 +116,7 @@ describe("Code Action Prompts", () => {
describe("create with custom prompts", () => {
it("should use custom template when provided", () => {
const customTemplate = "Custom template for ${filePath}"
const customPrompts = {
const customSupportPrompts = {
EXPLAIN: customTemplate,
}
@@ -126,7 +126,7 @@ describe("Code Action Prompts", () => {
filePath: testFilePath,
selectedText: testCode,
},
customPrompts,
customSupportPrompts,
)
expect(prompt).toContain(`Custom template for ${testFilePath}`)
@@ -134,7 +134,7 @@ describe("Code Action Prompts", () => {
})
it("should use default template when custom prompts does not include type", () => {
const customPrompts = {
const customSupportPrompts = {
EXPLAIN: "Other template",
}
@@ -144,7 +144,7 @@ describe("Code Action Prompts", () => {
filePath: testFilePath,
selectedText: testCode,
},
customPrompts,
customSupportPrompts,
)
expect(prompt).toContain("Other template")

View File

@@ -18,8 +18,8 @@ export type PromptComponent = {
customInstructions?: string
}
export type CustomPrompts = {
[key: string]: PromptComponent | undefined | string
export type CustomModePrompts = {
[key: string]: PromptComponent | undefined
}
// Helper to get all tools for a mode
@@ -141,7 +141,7 @@ export function isToolAllowedForMode(
}
// Create the mode-specific default prompts
export const defaultPrompts: Readonly<CustomPrompts> = Object.freeze(
export const defaultPrompts: Readonly<CustomModePrompts> = Object.freeze(
Object.fromEntries(modes.map((mode) => [mode.slug, { roleDefinition: mode.roleDefinition }])),
)

View File

@@ -84,11 +84,11 @@ type SupportPromptType = keyof typeof defaultTemplates
export const supportPrompt = {
default: defaultTemplates,
get: (customPrompts: Record<string, any> | undefined, type: SupportPromptType): string => {
return customPrompts?.[type] ?? defaultTemplates[type]
get: (customSupportPrompts: Record<string, any> | undefined, type: SupportPromptType): string => {
return customSupportPrompts?.[type] ?? defaultTemplates[type]
},
create: (type: SupportPromptType, params: PromptParams, customPrompts?: Record<string, any>): string => {
const template = supportPrompt.get(customPrompts, type)
create: (type: SupportPromptType, params: PromptParams, customSupportPrompts?: Record<string, any>): string => {
const template = supportPrompt.get(customSupportPrompts, type)
return createPrompt(template, params)
},
} as const
@@ -102,3 +102,7 @@ export const supportPromptLabels: Record<SupportPromptType, string> = {
IMPROVE: "Improve Code",
ENHANCE: "Enhance Prompt",
} as const
export type CustomSupportPrompts = {
[key: string]: string | undefined
}