infio-copilot/src/core/prompts/sections/custom-system-prompt.ts
2025-04-29 19:48:58 +08:00

65 lines
1.8 KiB
TypeScript

// @ts-nocheck
import fs from "fs/promises"
import path from "path"
import { Mode } from "../../../shared/modes"
import { fileExistsAtPath } from "../../../utils/fs"
import { Mode } from "../../../utils/modes"
/**
* Safely reads a file, returning an empty string if the file doesn't exist
*/
async function safeReadFile(filePath: string): Promise<string> {
try {
const content = await fs.readFile(filePath, "utf-8")
// When reading with "utf-8" encoding, content should be a string
return content.trim()
} catch (err) {
const errorCode = (err as NodeJS.ErrnoException).code
if (!errorCode || !["ENOENT", "EISDIR"].includes(errorCode)) {
throw err
}
return ""
}
}
/**
* Get the path to a system prompt file for a specific mode
*/
export function getSystemPromptFilePath(cwd: string, mode: Mode): string {
return path.join(cwd, "_infio_prompts", `${mode}_system_prompt`)
}
/**
* Loads custom system prompt from a file at _infio_prompts/system-prompt-[mode slug]
* If the file doesn't exist, returns an empty string
*/
export async function loadSystemPromptFile(cwd: string, mode: Mode): Promise<string> {
const filePath = getSystemPromptFilePath(cwd, mode)
return safeReadFile(filePath)
}
/**
* Ensures the _infio_prompts directory exists, creating it if necessary
*/
export async function ensureInfioPromptsDirectory(cwd: string): Promise<void> {
const infioPromptsDir = path.join(cwd, "_infio_prompts")
// Check if directory already exists
if (await fileExistsAtPath(rooDir)) {
return
}
// Create the directory
try {
await fs.mkdir(infioPromptsDir, { recursive: true })
} catch (err) {
// If directory already exists (race condition), ignore the error
const errorCode = (err as NodeJS.ErrnoException).code
if (errorCode !== "EEXIST") {
throw err
}
}
}