repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

feat(goal): expand prompt templates

66f7f96cc6868092209348402403732766cc3170

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (5)

CHANGELOG.md

Modified
13 13 errors to verdict history
14 14 - Keep visible goal judge/event reasons untruncated while preserving internal
15 15 bounded context snippets
16 +- Expand prompt templates when starting goals with `/goal /template args`
16 17 - Rename `autoresearch` extension package/slug to `tune` and slash command to
17 18 `/tune`
18 19 - Move tune session state to `.pi/tune/`

extensions/goal/CHANGELOG.md

Modified
2 2 # Changelog
4 4 ## 0.0.3
6 +- Expand prompt templates when starting goals with `/goal /template args`.
6 7 - Add planner dossier for autonomous goal runs.
7 8 - Harden continuation and judge loops; retry empty verdicts and errors.
8 9 - Goal widget: live updates, judge history, deduped output, full criteria list.

extensions/goal/README.md

Modified
23 23 ```text
24 24 /goal
25 25 /goal <objective>
26 +/goal /prompt-template [args]
26 27 /goal status
27 28 /goal pause
28 29 /goal resume
112 113 - `/goal <objective>` creates an active goal with empty criteria and current
113 114 session ownership.
114 115 It sends a kickoff prompt immediately.
116 +- `/goal /prompt-template [args]` expands a prompt template and uses the expanded
117 + content as the objective.
115 118 - A raw prompt containing `standing goal` plus an `<objective>...</objective>`
116 119 block is adopted as an active goal when no goal exists.
117 120 - New goals require an idle agent.

extensions/goal/__tests__/harness.test.ts

Modified
1 +import { mkdirSync, writeFileSync } from "node:fs";
2 +import { join } from "node:path";
1 3 import { fileURLToPath } from "node:url";
2 4 import { createAssistantMessageEventStream } from "@earendil-works/pi-ai";
3 5 import {
424 + it("expands prompt templates when a new goal starts with slash", async () => {
425 + const restore = installJudgeProvider([
426 + '{"done":true,"blocked":false,"reason":"template goal complete"}',
427 + ]);
428 + try {
429 + t = await createTestSession({ extensions: [goalPath()] });
430 + mkdirSync(join(t.cwd, ".pi", "prompts"), { recursive: true });
431 + writeFileSync(
432 + join(t.cwd, ".pi", "prompts", "ship.md"),
433 + "Ship template goal for $ARGUMENTS.",
434 + );
435 + installSaysStream(t, ["template work done"]);
437 + await t.session.prompt("/goal /ship prompt args");
438 + await waitFor(() => latestGoal()?.status === "complete");
440 + expect(latestGoal()).toMatchObject({
441 + objective: "Ship template goal for prompt args.",
442 + status: "complete",
443 + lastReason: "template goal complete",
444 + });
445 + } finally {
446 + restore();
448 + });
422 450 it("judge complete verdict completes goal", async () => {
423 451 const restore = installJudgeProvider([
424 452 '{"done":true,"blocked":false,"reason":"all criteria verified"}',

extensions/goal/index.ts

Modified
1959 1959 throw lastError;
1962 +type GoalPromptTemplate = { name: string; content: string };
1962 1964 function judgeResourceLoaderOptions(ctx: ExtensionContext) {
1963 1965 return {
1964 1966 cwd: ctx.cwd,
1972 +function parseGoalPromptArgs(argsString: string): string[] {
1973 + const args: string[] = [];
1974 + let current = "";
1975 + let quote: string | undefined;
1976 + for (const char of argsString) {
1977 + if (quote) {
1978 + if (char === quote) quote = undefined;
1979 + else current += char;
1980 + continue;
1982 + if (char === '"' || char === "'") {
1983 + quote = char;
1984 + continue;
1986 + if (/\s/.test(char)) {
1987 + if (current) args.push(current);
1988 + current = "";
1989 + continue;
1991 + current += char;
1993 + if (current) args.push(current);
1994 + return args;
1997 +function substituteGoalPromptArgs(content: string, args: string[]): string {
1998 + let result = content.replace(/\$(\d+)/g, (_, num: string) => {
1999 + const index = Number(num) - 1;
2000 + return args[index] ?? "";
2001 + });
2002 + result = result.replace(
2003 + /\$\{@:(\d+)(?::(\d+))?\}/g,
2004 + (_, startText: string, lengthText: string | undefined) => {
2005 + const start = Math.max(0, Number(startText) - 1);
2006 + if (lengthText)
2007 + return args.slice(start, start + Number(lengthText)).join(" ");
2008 + return args.slice(start).join(" ");
2009 + },
2010 + );
2011 + const all = args.join(" ");
2012 + return result.replace(/\$ARGUMENTS/g, all).replace(/\$@/g, all);
2015 +function expandGoalPromptTemplate(
2016 + input: string,
2017 + templates: GoalPromptTemplate[],
2018 +): string | undefined {
2019 + const match = input.match(/^\/([^\s]+)(?:\s+([\s\S]*))?$/);
2020 + if (!match) return undefined;
2021 + const template = templates.find((item) => item.name === match[1]);
2022 + if (!template) return undefined;
2023 + return substituteGoalPromptArgs(
2024 + template.content,
2025 + parseGoalPromptArgs(match[2] ?? ""),
2026 + );
2029 +async function resolveGoalObjectiveInput(
2030 + input: string,
2031 + ctx: ExtensionContext,
2032 +): Promise<string | undefined> {
2033 + if (!input.startsWith("/")) return input;
2034 + const loader = new DefaultResourceLoader({
2035 + cwd: ctx.cwd,
2036 + agentDir: getAgentDir(),
2037 + noExtensions: true,
2038 + noSkills: true,
2039 + noThemes: true,
2040 + noContextFiles: true,
2041 + });
2042 + await loader.reload();
2043 + return expandGoalPromptTemplate(input, loader.getPrompts().prompts);
1970 2046 async function judgeGoalWithAgent(
1971 2047 ctx: ExtensionContext,
1972 2048 state: GoalState,
2842 2918 "Agent is running — use /goal status, pause, resume, complete, clear, add, remove, criteria, clear-criteria, or widget.",
2843 2919 "warning",
2921 + const objective = await resolveGoalObjectiveInput(input, ctx);
2922 + if (!objective)
2923 + return ctx.ui.notify(
2924 + `Prompt template not found: ${input.split(/\s+/, 1)[0]}`,
2925 + "warning",
2926 + );
2845 2927 const settings = loadSettings(pi);
2846 - state = createGoalState(input, settings, currentSessionId(ctx));
2928 + state = createGoalState(objective, settings, currentSessionId(ctx));
2847 2929 appendGoal(pi, state);
2848 2930 const dossier = appendDossier(pi, createInitialDossier(state));
2849 2931 refreshPlannerInBackground(pi, ctx, state, dossier, "new-goal");