refactor: share prompt template arguments

This commit is contained in:
Vincent Koc
2026-05-30 02:55:15 +02:00
parent 086df266cc
commit deb48a96fb
5 changed files with 102 additions and 177 deletions

View File

@@ -0,0 +1,72 @@
/** Parse an argument string using simple shell-style single and double quotes. */
export function parseCommandArgs(argsString: string): string[] {
const args: string[] = [];
let current = "";
let inQuote: string | null = null;
for (let i = 0; i < argsString.length; i++) {
const char = argsString[i];
if (inQuote) {
if (char === inQuote) {
inQuote = null;
} else {
current += char;
}
} else if (char === '"' || char === "'") {
inQuote = char;
} else if (/\s/.test(char)) {
if (current) {
args.push(current);
current = "";
}
} else {
current += char;
}
}
if (current) {
args.push(current);
}
return args;
}
function parseSafeNonNegativeInteger(raw: string): number | undefined {
const parsed = Number(raw);
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;
}
/** Substitute prompt template placeholders (`$1`, `$@`, `$ARGUMENTS`, `${@:N}`, `${@:N:L}`) with command arguments. */
export function substituteArgs(content: string, args: string[]): string {
let result = content;
result = result.replace(/\$(\d+)/g, (_, num: string) => {
const parsed = parseSafeNonNegativeInteger(num);
if (parsed === undefined || parsed <= 0) {
return "";
}
return args[parsed - 1] ?? "";
});
result = result.replace(
/\$\{@:(\d+)(?::(\d+))?\}/g,
(_, startStr: string, lengthStr?: string) => {
const parsedStart = parseSafeNonNegativeInteger(startStr);
if (parsedStart === undefined) {
return "";
}
let start = parsedStart - 1;
if (start < 0) {
start = 0;
}
if (lengthStr) {
const length = parseSafeNonNegativeInteger(lengthStr);
if (length === undefined) {
return "";
}
return args.slice(start, start + length).join(" ");
}
return args.slice(start).join(" ");
},
);
const allArgs = args.join(" ");
result = result.replace(/\$ARGUMENTS/g, allArgs);
result = result.replace(/\$@/g, allArgs);
return result;
}

View File

@@ -1,7 +1,16 @@
import { describe, expect, it } from "vitest";
import { substituteArgs } from "./prompt-templates.js";
import { parseCommandArgs, substituteArgs } from "./prompt-templates.js";
describe("prompt template argument substitution", () => {
it("parses quoted and multiline arguments", () => {
expect(parseCommandArgs(`alpha "beta gamma"\ndelta 'echo one two'`)).toEqual([
"alpha",
"beta gamma",
"delta",
"echo one two",
]);
});
it("rejects unsafe positional placeholders", () => {
expect(substituteArgs("$9007199254740992", ["first", "second"])).toBe("");
});

View File

@@ -3,6 +3,8 @@ import {
parseFrontmatter,
resolveFileInfoKind as resolveKind,
} from "./file-loader-utils.js";
export { parseCommandArgs, substituteArgs } from "./prompt-template-arguments.js";
import { substituteArgs } from "./prompt-template-arguments.js";
import { type ExecutionEnv, type PromptTemplate, type Result } from "./types.js";
export type PromptTemplateDiagnosticCode =
@@ -188,79 +190,6 @@ async function loadTemplateFromFile(
};
}
/** Parse an argument string using simple shell-style single and double quotes. */
export function parseCommandArgs(argsString: string): string[] {
const args: string[] = [];
let current = "";
let inQuote: string | null = null;
for (let i = 0; i < argsString.length; i++) {
const char = argsString[i];
if (inQuote) {
if (char === inQuote) {
inQuote = null;
} else {
current += char;
}
} else if (char === '"' || char === "'") {
inQuote = char;
} else if (char === " " || char === "\t") {
if (current) {
args.push(current);
current = "";
}
} else {
current += char;
}
}
if (current) {
args.push(current);
}
return args;
}
function parseSafeNonNegativeInteger(raw: string): number | undefined {
const parsed = Number(raw);
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;
}
/** Substitute prompt template placeholders (`$1`, `$@`, `$ARGUMENTS`, `${@:N}`, `${@:N:L}`) with command arguments. */
export function substituteArgs(content: string, args: string[]): string {
let result = content;
result = result.replace(/\$(\d+)/g, (_, num: string) => {
const parsed = parseSafeNonNegativeInteger(num);
if (parsed === undefined || parsed <= 0) {
return "";
}
return args[parsed - 1] ?? "";
});
result = result.replace(
/\$\{@:(\d+)(?::(\d+))?\}/g,
(_, startStr: string, lengthStr?: string) => {
const parsedStart = parseSafeNonNegativeInteger(startStr);
if (parsedStart === undefined) {
return "";
}
let start = parsedStart - 1;
if (start < 0) {
start = 0;
}
if (lengthStr) {
const length = parseSafeNonNegativeInteger(lengthStr);
if (length === undefined) {
return "";
}
return args.slice(start, start + length).join(" ");
}
return args.slice(start).join(" ");
},
);
const allArgs = args.join(" ");
result = result.replace(/\$ARGUMENTS/g, allArgs);
result = result.replace(/\$@/g, allArgs);
return result;
}
/** Format a prompt template invocation with positional arguments. */
export function formatPromptTemplateInvocation(
template: PromptTemplate,

View File

@@ -1,7 +1,16 @@
import { describe, expect, it } from "vitest";
import { substituteArgs } from "./prompt-templates.js";
import { parseCommandArgs, substituteArgs } from "./prompt-templates.js";
describe("prompt template argument substitution", () => {
it("parses quoted and multiline arguments", () => {
expect(parseCommandArgs(`alpha "beta gamma"\ndelta 'echo one two'`)).toEqual([
"alpha",
"beta gamma",
"delta",
"echo one two",
]);
});
it("rejects unsafe positional placeholders", () => {
expect(substituteArgs("$9007199254740992", ["first", "second"])).toBe("");
});

View File

@@ -1,6 +1,14 @@
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import { homedir } from "node:os";
import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
export {
parseCommandArgs,
substituteArgs,
} from "../../../packages/agent-core/src/harness/prompt-template-arguments.js";
import {
parseCommandArgs,
substituteArgs,
} from "../../../packages/agent-core/src/harness/prompt-template-arguments.js";
import { CONFIG_DIR_NAME } from "../config.js";
import { parseFrontmatter } from "../utils/frontmatter.js";
import { createSyntheticSourceInfo, type SourceInfo } from "./source-info.js";
@@ -17,108 +25,6 @@ export interface PromptTemplate {
filePath: string; // Absolute path to the template file
}
/**
* Parse command arguments respecting quoted strings (bash-style)
* Returns array of arguments
*/
export function parseCommandArgs(argsString: string): string[] {
const args: string[] = [];
let current = "";
let inQuote: string | null = null;
for (let i = 0; i < argsString.length; i++) {
const char = argsString[i];
if (inQuote) {
if (char === inQuote) {
inQuote = null;
} else {
current += char;
}
} else if (char === '"' || char === "'") {
inQuote = char;
} else if (/\s/.test(char)) {
if (current) {
args.push(current);
current = "";
}
} else {
current += char;
}
}
if (current) {
args.push(current);
}
return args;
}
function parseSafeNonNegativeInteger(raw: string): number | undefined {
const parsed = Number(raw);
return Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;
}
/**
* Substitute argument placeholders in template content
* Supports:
* - $1, $2, ... for positional args
* - $@ and $ARGUMENTS for all args
* - ${@:N} for args from Nth onwards (bash-style slicing)
* - ${@:N:L} for L args starting from Nth
*
* Note: Replacement happens on the template string only. Argument values
* containing patterns like $1, $@, or $ARGUMENTS are NOT recursively substituted.
*/
export function substituteArgs(content: string, args: string[]): string {
let result = content;
// Replace $1, $2, etc. with positional args FIRST (before wildcards)
// This prevents wildcard replacement values containing $<digit> patterns from being re-substituted
result = result.replace(/\$(\d+)/g, (_, num) => {
const parsed = parseSafeNonNegativeInteger(num);
if (parsed === undefined || parsed <= 0) {
return "";
}
const index = parsed - 1;
return args[index] ?? "";
});
// Replace ${@:start} or ${@:start:length} with sliced args (bash-style)
// Process BEFORE simple $@ to avoid conflicts
result = result.replace(/\$\{@:(\d+)(?::(\d+))?\}/g, (_, startStr, lengthStr) => {
const parsedStart = parseSafeNonNegativeInteger(startStr);
if (parsedStart === undefined) {
return "";
}
let start = parsedStart - 1; // Convert to 0-indexed (user provides 1-indexed)
// Treat 0 as 1 (bash convention: args start at 1)
if (start < 0) {
start = 0;
}
if (lengthStr) {
const length = parseSafeNonNegativeInteger(lengthStr);
if (length === undefined) {
return "";
}
return args.slice(start, start + length).join(" ");
}
return args.slice(start).join(" ");
});
// Pre-compute all args joined (optimization)
const allArgs = args.join(" ");
// Replace $ARGUMENTS with all args joined (new syntax, aligns with Claude, Codex, OpenCode)
result = result.replace(/\$ARGUMENTS/g, allArgs);
// Replace $@ with all args joined (existing syntax)
result = result.replace(/\$@/g, allArgs);
return result;
}
function loadTemplateFromFile(filePath: string, sourceInfo: SourceInfo): PromptTemplate | null {
try {
const rawContent = readFileSync(filePath, "utf-8");