fix(coding-agent): unify source provenance, closes #1734
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
|
||||
import { homedir } from "os";
|
||||
import { basename, isAbsolute, join, resolve, sep } from "path";
|
||||
import { basename, dirname, isAbsolute, join, resolve, sep } from "path";
|
||||
import { CONFIG_DIR_NAME, getPromptsDir } from "../config.js";
|
||||
import { parseFrontmatter } from "../utils/frontmatter.js";
|
||||
import type { SourceInfo } from "./source-info.js";
|
||||
import { createSyntheticSourceInfo, type SourceInfo } from "./source-info.js";
|
||||
|
||||
/**
|
||||
* Represents a prompt template loaded from a markdown file
|
||||
@@ -12,8 +12,7 @@ export interface PromptTemplate {
|
||||
name: string;
|
||||
description: string;
|
||||
content: string;
|
||||
source: string; // "user", "project", or "path"
|
||||
sourceInfo?: SourceInfo;
|
||||
sourceInfo: SourceInfo;
|
||||
filePath: string; // Absolute path to the template file
|
||||
}
|
||||
|
||||
@@ -101,7 +100,7 @@ export function substituteArgs(content: string, args: string[]): string {
|
||||
return result;
|
||||
}
|
||||
|
||||
function loadTemplateFromFile(filePath: string, source: string): PromptTemplate | null {
|
||||
function loadTemplateFromFile(filePath: string, sourceInfo: SourceInfo): PromptTemplate | null {
|
||||
try {
|
||||
const rawContent = readFileSync(filePath, "utf-8");
|
||||
const { frontmatter, body } = parseFrontmatter<Record<string, string>>(rawContent);
|
||||
@@ -123,7 +122,7 @@ function loadTemplateFromFile(filePath: string, source: string): PromptTemplate
|
||||
name,
|
||||
description,
|
||||
content: body,
|
||||
source,
|
||||
sourceInfo,
|
||||
filePath,
|
||||
};
|
||||
} catch {
|
||||
@@ -134,7 +133,7 @@ function loadTemplateFromFile(filePath: string, source: string): PromptTemplate
|
||||
/**
|
||||
* Scan a directory for .md files (non-recursive) and load them as prompt templates.
|
||||
*/
|
||||
function loadTemplatesFromDir(dir: string, source: string): PromptTemplate[] {
|
||||
function loadTemplatesFromDir(dir: string, getSourceInfo: (filePath: string) => SourceInfo): PromptTemplate[] {
|
||||
const templates: PromptTemplate[] = [];
|
||||
|
||||
if (!existsSync(dir)) {
|
||||
@@ -160,7 +159,7 @@ function loadTemplatesFromDir(dir: string, source: string): PromptTemplate[] {
|
||||
}
|
||||
|
||||
if (isFile && entry.name.endsWith(".md")) {
|
||||
const template = loadTemplateFromFile(fullPath, source);
|
||||
const template = loadTemplateFromFile(fullPath, getSourceInfo(fullPath));
|
||||
if (template) {
|
||||
templates.push(template);
|
||||
}
|
||||
@@ -211,18 +210,7 @@ export function loadPromptTemplates(options: LoadPromptTemplatesOptions = {}): P
|
||||
|
||||
const templates: PromptTemplate[] = [];
|
||||
|
||||
if (includeDefaults) {
|
||||
// 1. Load global templates from agentDir/prompts/
|
||||
// Note: if agentDir is provided, it should be the agent dir, not the prompts dir
|
||||
const globalPromptsDir = options.agentDir ? join(options.agentDir, "prompts") : resolvedAgentDir;
|
||||
templates.push(...loadTemplatesFromDir(globalPromptsDir, "user"));
|
||||
|
||||
// 2. Load project templates from cwd/{CONFIG_DIR_NAME}/prompts/
|
||||
const projectPromptsDir = resolve(resolvedCwd, CONFIG_DIR_NAME, "prompts");
|
||||
templates.push(...loadTemplatesFromDir(projectPromptsDir, "project"));
|
||||
}
|
||||
|
||||
const userPromptsDir = options.agentDir ? join(options.agentDir, "prompts") : resolvedAgentDir;
|
||||
const globalPromptsDir = options.agentDir ? join(options.agentDir, "prompts") : resolvedAgentDir;
|
||||
const projectPromptsDir = resolve(resolvedCwd, CONFIG_DIR_NAME, "prompts");
|
||||
|
||||
const isUnderPath = (target: string, root: string): boolean => {
|
||||
@@ -234,18 +222,32 @@ export function loadPromptTemplates(options: LoadPromptTemplatesOptions = {}): P
|
||||
return target.startsWith(prefix);
|
||||
};
|
||||
|
||||
const getSource = (resolvedPath: string): string => {
|
||||
if (!includeDefaults) {
|
||||
if (isUnderPath(resolvedPath, userPromptsDir)) {
|
||||
return "user";
|
||||
}
|
||||
if (isUnderPath(resolvedPath, projectPromptsDir)) {
|
||||
return "project";
|
||||
}
|
||||
const getSourceInfo = (resolvedPath: string): SourceInfo => {
|
||||
if (isUnderPath(resolvedPath, globalPromptsDir)) {
|
||||
return createSyntheticSourceInfo(resolvedPath, {
|
||||
source: "local",
|
||||
scope: "user",
|
||||
baseDir: globalPromptsDir,
|
||||
});
|
||||
}
|
||||
return "path";
|
||||
if (isUnderPath(resolvedPath, projectPromptsDir)) {
|
||||
return createSyntheticSourceInfo(resolvedPath, {
|
||||
source: "local",
|
||||
scope: "project",
|
||||
baseDir: projectPromptsDir,
|
||||
});
|
||||
}
|
||||
return createSyntheticSourceInfo(resolvedPath, {
|
||||
source: "local",
|
||||
baseDir: statSync(resolvedPath).isDirectory() ? resolvedPath : dirname(resolvedPath),
|
||||
});
|
||||
};
|
||||
|
||||
if (includeDefaults) {
|
||||
templates.push(...loadTemplatesFromDir(globalPromptsDir, getSourceInfo));
|
||||
templates.push(...loadTemplatesFromDir(projectPromptsDir, getSourceInfo));
|
||||
}
|
||||
|
||||
// 3. Load explicit prompt paths
|
||||
for (const rawPath of promptPaths) {
|
||||
const resolvedPath = resolvePromptPath(rawPath, resolvedCwd);
|
||||
@@ -255,11 +257,10 @@ export function loadPromptTemplates(options: LoadPromptTemplatesOptions = {}): P
|
||||
|
||||
try {
|
||||
const stats = statSync(resolvedPath);
|
||||
const source = getSource(resolvedPath);
|
||||
if (stats.isDirectory()) {
|
||||
templates.push(...loadTemplatesFromDir(resolvedPath, source));
|
||||
templates.push(...loadTemplatesFromDir(resolvedPath, getSourceInfo));
|
||||
} else if (stats.isFile() && resolvedPath.endsWith(".md")) {
|
||||
const template = loadTemplateFromFile(resolvedPath, source);
|
||||
const template = loadTemplateFromFile(resolvedPath, getSourceInfo(resolvedPath));
|
||||
if (template) {
|
||||
templates.push(template);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user