feat(agents): project-local agent overrides

loadAgents merges <cwd>/agents/*.md on top of the extension's baseline:
a repo agent wins on name collision and may add brand-new agents. The
unknown-agent error now names both searched directories.
This commit is contained in:
2026-08-09 16:45:30 -04:00
parent c605a709fb
commit cb8a88d0bd
4 changed files with 149 additions and 23 deletions

View File

@@ -18,6 +18,7 @@
import { mkdir, writeFile } from "node:fs/promises";
import { dirname, isAbsolute, join } from "node:path";
import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
import { loadAgents, extensionRoot, type AgentDef } from "./agents.js";
import type { ToolCallEntry } from "./phases.js";
export interface AgentTaskOptions {
@@ -74,13 +75,19 @@ export function resetAgentRunner(): void {
export async function defaultAgentRunner(
opts: AgentTaskOptions,
): Promise<AgentRunResult> {
const agent =
opts.agent ?? (await loadAgents({ cwd: opts.cwd })).get(opts.agentName);
const agents = await loadAgents({ cwd: opts.cwd });
const agent = opts.agent ?? agents.get(opts.agentName);
if (!agent) {
const names = [...agents.keys()];
return {
ok: false,
text: "",
error: `Unknown agent definition: "${opts.agentName}". Add agents/${opts.agentName}.md.`,
toolCalls: [],
error:
`Unknown agent definition: "${opts.agentName}". ` +
`Available agents: ${names.length > 0 ? names.join(", ") : "(none loaded — check agents/ directories exist)"}. ` +
`Searched ${extensionRoot()}/agents/ (extension) and ${opts.cwd}/agents/ (project-local). ` +
`Add agents/${opts.agentName}.md to either location.`,
};
}