fix(coding-agent): attach source info to resources and commands, fixes #1734

This commit is contained in:
Mario Zechner
2026-03-23 00:52:55 +01:00
parent 0cf18f3cf2
commit d501b9ca26
21 changed files with 425 additions and 321 deletions

View File

@@ -3,6 +3,7 @@ import { homedir } from "os";
import { basename, 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";
/**
* Represents a prompt template loaded from a markdown file
@@ -12,6 +13,7 @@ export interface PromptTemplate {
description: string;
content: string;
source: string; // "user", "project", or "path"
sourceInfo?: SourceInfo;
filePath: string; // Absolute path to the template file
}
@@ -99,7 +101,7 @@ export function substituteArgs(content: string, args: string[]): string {
return result;
}
function loadTemplateFromFile(filePath: string, source: string, sourceLabel: string): PromptTemplate | null {
function loadTemplateFromFile(filePath: string, source: string): PromptTemplate | null {
try {
const rawContent = readFileSync(filePath, "utf-8");
const { frontmatter, body } = parseFrontmatter<Record<string, string>>(rawContent);
@@ -117,9 +119,6 @@ function loadTemplateFromFile(filePath: string, source: string, sourceLabel: str
}
}
// Append source to description
description = description ? `${description} ${sourceLabel}` : sourceLabel;
return {
name,
description,
@@ -135,7 +134,7 @@ function loadTemplateFromFile(filePath: string, source: string, sourceLabel: str
/**
* Scan a directory for .md files (non-recursive) and load them as prompt templates.
*/
function loadTemplatesFromDir(dir: string, source: string, sourceLabel: string): PromptTemplate[] {
function loadTemplatesFromDir(dir: string, source: string): PromptTemplate[] {
const templates: PromptTemplate[] = [];
if (!existsSync(dir)) {
@@ -161,7 +160,7 @@ function loadTemplatesFromDir(dir: string, source: string, sourceLabel: string):
}
if (isFile && entry.name.endsWith(".md")) {
const template = loadTemplateFromFile(fullPath, source, sourceLabel);
const template = loadTemplateFromFile(fullPath, source);
if (template) {
templates.push(template);
}
@@ -198,11 +197,6 @@ function resolvePromptPath(p: string, cwd: string): string {
return isAbsolute(normalized) ? normalized : resolve(cwd, normalized);
}
function buildPathSourceLabel(p: string): string {
const base = basename(p).replace(/\.md$/, "") || "path";
return `(path:${base})`;
}
/**
* Load all prompt templates from:
* 1. Global: agentDir/prompts/
@@ -221,11 +215,11 @@ export function loadPromptTemplates(options: LoadPromptTemplatesOptions = {}): P
// 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", "(user)"));
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", "(project)"));
templates.push(...loadTemplatesFromDir(projectPromptsDir, "project"));
}
const userPromptsDir = options.agentDir ? join(options.agentDir, "prompts") : resolvedAgentDir;
@@ -240,16 +234,16 @@ export function loadPromptTemplates(options: LoadPromptTemplatesOptions = {}): P
return target.startsWith(prefix);
};
const getSourceInfo = (resolvedPath: string): { source: string; label: string } => {
const getSource = (resolvedPath: string): string => {
if (!includeDefaults) {
if (isUnderPath(resolvedPath, userPromptsDir)) {
return { source: "user", label: "(user)" };
return "user";
}
if (isUnderPath(resolvedPath, projectPromptsDir)) {
return { source: "project", label: "(project)" };
return "project";
}
}
return { source: "path", label: buildPathSourceLabel(resolvedPath) };
return "path";
};
// 3. Load explicit prompt paths
@@ -261,11 +255,11 @@ export function loadPromptTemplates(options: LoadPromptTemplatesOptions = {}): P
try {
const stats = statSync(resolvedPath);
const { source, label } = getSourceInfo(resolvedPath);
const source = getSource(resolvedPath);
if (stats.isDirectory()) {
templates.push(...loadTemplatesFromDir(resolvedPath, source, label));
templates.push(...loadTemplatesFromDir(resolvedPath, source));
} else if (stats.isFile() && resolvedPath.endsWith(".md")) {
const template = loadTemplateFromFile(resolvedPath, source, label);
const template = loadTemplateFromFile(resolvedPath, source);
if (template) {
templates.push(template);
}