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

47
src/utils/fs.ts Normal file
View File

@@ -0,0 +1,47 @@
import fs from "fs/promises"
import * as path from "path"
/**
* Asynchronously creates all non-existing subdirectories for a given file path
* and collects them in an array for later deletion.
*
* @param filePath - The full path to a file.
* @returns A promise that resolves to an array of newly created directories.
*/
export async function createDirectoriesForFile(filePath: string): Promise<string[]> {
const newDirectories: string[] = []
const normalizedFilePath = path.normalize(filePath) // Normalize path for cross-platform compatibility
const directoryPath = path.dirname(normalizedFilePath)
let currentPath = directoryPath
const dirsToCreate: string[] = []
// Traverse up the directory tree and collect missing directories
while (!(await fileExistsAtPath(currentPath))) {
dirsToCreate.push(currentPath)
currentPath = path.dirname(currentPath)
}
// Create directories from the topmost missing one down to the target directory
for (let i = dirsToCreate.length - 1; i >= 0; i--) {
await fs.mkdir(dirsToCreate[i])
newDirectories.push(dirsToCreate[i])
}
return newDirectories
}
/**
* Helper function to check if a path exists.
*
* @param path - The path to check.
* @returns A promise that resolves to true if the path exists, false otherwise.
*/
export async function fileExistsAtPath(filePath: string): Promise<boolean> {
try {
await fs.access(filePath)
return true
} catch {
return false
}
}

View File

@@ -8,6 +8,7 @@ import { RAGEngine } from '../core/rag/rag-engine'
import { SelectVector } from '../database/schema'
import { ChatMessage, ChatUserMessage } from '../types/chat'
import { ContentPart, RequestMessage } from '../types/llm/request'
import { SystemPromptsManager } from '../core/prompts/system-prompts-manager'
import {
MentionableBlock,
MentionableFile,
@@ -115,6 +116,7 @@ export class PromptGenerator {
private app: App
private settings: InfioSettings
private diffStrategy: DiffStrategy
private systemPromptsManager: SystemPromptsManager
private static readonly EMPTY_ASSISTANT_MESSAGE: RequestMessage = {
role: 'assistant',
content: '',
@@ -130,6 +132,7 @@ export class PromptGenerator {
this.app = app
this.settings = settings
this.diffStrategy = diffStrategy
this.systemPromptsManager = new SystemPromptsManager(this.app)
}
public async generateRequestMessages({
@@ -465,7 +468,7 @@ export class PromptGenerator {
}
private async getSystemMessageNew(mode: Mode, filesSearchMethod: string, preferredLanguage: string): Promise<RequestMessage> {
const systemPrompt = await SYSTEM_PROMPT(
const systemPrompt = await this.systemPromptsManager.getSystemPrompt(
this.app.vault.getRoot().path,
false,
mode,