refactor(agent): rename resource formatting helpers

This commit is contained in:
Mario Zechner
2026-05-09 19:43:57 +02:00
parent 3d5cbe98c3
commit 401017a3e8
6 changed files with 21 additions and 21 deletions

View File

@@ -4,8 +4,8 @@ import { Agent } from "../agent.js";
import type { AgentEvent, AgentMessage, AgentTool, ThinkingLevel } from "../types.js"; import type { AgentEvent, AgentMessage, AgentTool, ThinkingLevel } from "../types.js";
import { collectEntriesForBranchSummary, generateBranchSummary } from "./compaction/branch-summarization.js"; import { collectEntriesForBranchSummary, generateBranchSummary } from "./compaction/branch-summarization.js";
import { compact, DEFAULT_COMPACTION_SETTINGS, prepareCompaction } from "./compaction/compaction.js"; import { compact, DEFAULT_COMPACTION_SETTINGS, prepareCompaction } from "./compaction/compaction.js";
import { expandPromptTemplate } from "./prompt-templates.js"; import { formatPromptTemplateInvocation } from "./prompt-templates.js";
import { expandSkillCommand } from "./skills.js"; import { formatSkillInvocation } from "./skills.js";
import type { import type {
AbortResult, AbortResult,
AgentHarnessContext, AgentHarnessContext,
@@ -335,14 +335,14 @@ export class AgentHarness {
const resources = await this.resolveResources(); const resources = await this.resolveResources();
const skill = (resources.skills ?? []).find((candidate) => candidate.name === name); const skill = (resources.skills ?? []).find((candidate) => candidate.name === name);
if (!skill) throw new Error(`Unknown skill: ${name}`); if (!skill) throw new Error(`Unknown skill: ${name}`);
return await this.prompt(expandSkillCommand(skill, additionalInstructions)); return await this.prompt(formatSkillInvocation(skill, additionalInstructions));
} }
async promptFromTemplate(name: string, args: string[] = []): Promise<AssistantMessage> { async promptFromTemplate(name: string, args: string[] = []): Promise<AssistantMessage> {
const resources = await this.resolveResources(); const resources = await this.resolveResources();
const template = (resources.promptTemplates ?? []).find((candidate) => candidate.name === name); const template = (resources.promptTemplates ?? []).find((candidate) => candidate.name === name);
if (!template) throw new Error(`Unknown prompt template: ${name}`); if (!template) throw new Error(`Unknown prompt template: ${name}`);
return await this.prompt(expandPromptTemplate(template, args)); return await this.prompt(formatPromptTemplateInvocation(template, args));
} }
steer(message: AgentMessage): void { steer(message: AgentMessage): void {

View File

@@ -211,7 +211,7 @@ export function substituteArgs(content: string, args: string[]): string {
return result; return result;
} }
/** Expand a prompt template with positional command arguments. */ /** Format a prompt template invocation with positional arguments. */
export function expandPromptTemplate(template: PromptTemplate, args: string[] = []): string { export function formatPromptTemplateInvocation(template: PromptTemplate, args: string[] = []): string {
return substituteArgs(template.content, args); return substituteArgs(template.content, args);
} }

View File

@@ -25,8 +25,8 @@ interface SkillFrontmatter {
[key: string]: unknown; [key: string]: unknown;
} }
/** Expand a skill into a prompt, optionally appending additional user instructions. */ /** Format a skill invocation prompt, optionally appending additional user instructions. */
export function expandSkillCommand(skill: Skill, additionalInstructions?: string): string { export function formatSkillInvocation(skill: Skill, additionalInstructions?: string): string {
const skillBlock = `<skill name="${skill.name}" location="${skill.filePath}">\nReferences are relative to ${dirnameEnvPath(skill.filePath)}.\n\n${skill.content}\n</skill>`; const skillBlock = `<skill name="${skill.name}" location="${skill.filePath}">\nReferences are relative to ${dirnameEnvPath(skill.filePath)}.\n\n${skill.content}\n</skill>`;
return additionalInstructions ? `${skillBlock}\n\n${additionalInstructions}` : skillBlock; return additionalInstructions ? `${skillBlock}\n\n${additionalInstructions}` : skillBlock;
} }

View File

@@ -22,7 +22,7 @@ export interface PromptTemplate {
name: string; name: string;
/** Optional description for command lists or autocomplete. */ /** Optional description for command lists or autocomplete. */
description?: string; description?: string;
/** Template content. Argument placeholders are expanded by `expandPromptTemplate`. */ /** Template content. Argument placeholders are expanded by `formatPromptTemplateInvocation`. */
content: string; content: string;
} }

View File

@@ -3,7 +3,7 @@ import { join } from "node:path";
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { NodeExecutionEnv } from "../../src/harness/execution-env.js"; import { NodeExecutionEnv } from "../../src/harness/execution-env.js";
import { import {
expandPromptTemplate, formatPromptTemplateInvocation,
loadPromptTemplates, loadPromptTemplates,
loadSourcedPromptTemplates, loadSourcedPromptTemplates,
} from "../../src/harness/prompt-templates.js"; } from "../../src/harness/prompt-templates.js";
@@ -80,10 +80,10 @@ describe("loadPromptTemplates", () => {
}); });
}); });
describe("expandPromptTemplate", () => { describe("formatPromptTemplateInvocation", () => {
it("substitutes command arguments", () => { it("substitutes command arguments", () => {
const content = "$1 $" + "{@:2} $ARGUMENTS"; const content = "$1 $" + "{@:2} $ARGUMENTS";
expect(expandPromptTemplate({ name: "one", content }, ["hello world", "test"])).toBe( expect(formatPromptTemplateInvocation({ name: "one", content }, ["hello world", "test"])).toBe(
"hello world test hello world test", "hello world test hello world test",
); );
}); });

View File

@@ -1,9 +1,9 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { expandPromptTemplate } from "../../src/harness/prompt-templates.js"; import { formatPromptTemplateInvocation } from "../../src/harness/prompt-templates.js";
import { expandSkillCommand } from "../../src/harness/skills.js"; import { formatSkillInvocation } from "../../src/harness/skills.js";
describe("resource expansion helpers", () => { describe("resource formatting helpers", () => {
it("expands skills with additional instructions", () => { it("formats skill invocations with additional instructions", () => {
const skill = { const skill = {
name: "inspect", name: "inspect",
description: "Inspect things", description: "Inspect things",
@@ -11,14 +11,14 @@ describe("resource expansion helpers", () => {
filePath: "/project/.pi/skills/inspect/SKILL.md", filePath: "/project/.pi/skills/inspect/SKILL.md",
}; };
expect(expandSkillCommand(skill, "Check errors.")).toBe( expect(formatSkillInvocation(skill, "Check errors.")).toBe(
'<skill name="inspect" location="/project/.pi/skills/inspect/SKILL.md">\nReferences are relative to /project/.pi/skills/inspect.\n\nUse inspection tools.\n</skill>\n\nCheck errors.', '<skill name="inspect" location="/project/.pi/skills/inspect/SKILL.md">\nReferences are relative to /project/.pi/skills/inspect.\n\nUse inspection tools.\n</skill>\n\nCheck errors.',
); );
}); });
it("expands prompt templates with positional arguments", () => { it("formats prompt template invocations with positional arguments", () => {
expect(expandPromptTemplate({ name: "review", content: "Review $1 with $ARGUMENTS" }, ["a.ts", "care"])).toBe( expect(
"Review a.ts with a.ts care", formatPromptTemplateInvocation({ name: "review", content: "Review $1 with $ARGUMENTS" }, ["a.ts", "care"]),
); ).toBe("Review a.ts with a.ts care");
}); });
}); });