@xzzpig/pi-subagents 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1621 -0
- package/LICENSE +21 -0
- package/README.md +118 -0
- package/agents/delegate.md +14 -0
- package/agents/oracle.md +74 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/worker.md +58 -0
- package/index.ts +1 -0
- package/inspector-runner.mjs +11 -0
- package/install.mjs +93 -0
- package/package.json +105 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +43 -0
- package/skills/pi-subagents/SKILL.md +41 -0
- package/skills/pi-subagents/references/constraints-and-recipes.md +251 -0
- package/skills/pi-subagents/references/execution-controls.md +428 -0
- package/skills/pi-subagents/references/management-authoring-rpc.md +161 -0
- package/skills/pi-subagents/references/prompting-and-roles.md +242 -0
- package/src/agents/agent-management.ts +1256 -0
- package/src/agents/agent-memory.ts +254 -0
- package/src/agents/agent-refinements.ts +624 -0
- package/src/agents/agent-scope.ts +6 -0
- package/src/agents/agent-selection.ts +25 -0
- package/src/agents/agent-serializer.ts +163 -0
- package/src/agents/agents.ts +1889 -0
- package/src/agents/chain-serializer.ts +280 -0
- package/src/agents/frontmatter.ts +149 -0
- package/src/agents/identity.ts +30 -0
- package/src/agents/proactive-skills.ts +194 -0
- package/src/agents/skills.ts +761 -0
- package/src/api/background-work.ts +197 -0
- package/src/api/capability-ceiling.ts +17 -0
- package/src/api/control-channel.ts +4 -0
- package/src/api/delegation.ts +119 -0
- package/src/api/external-runs.ts +129 -0
- package/src/api/intercom-bridge.ts +3 -0
- package/src/api/pi-args.ts +5 -0
- package/src/api/preflight.ts +403 -0
- package/src/api/shared-types.ts +19 -0
- package/src/extension/config.ts +76 -0
- package/src/extension/control-notices.ts +58 -0
- package/src/extension/doctor.ts +230 -0
- package/src/extension/fanout-child.ts +201 -0
- package/src/extension/index.ts +805 -0
- package/src/extension/public-execution.ts +71 -0
- package/src/extension/rpc.ts +653 -0
- package/src/extension/schemas.ts +375 -0
- package/src/extension/steering-notices.ts +35 -0
- package/src/extension/tool-description.ts +164 -0
- package/src/inspectors/herdr/actions.ts +229 -0
- package/src/inspectors/herdr/client.ts +130 -0
- package/src/inspectors/herdr/inspector-runner.ts +141 -0
- package/src/inspectors/herdr/project-panes.ts +154 -0
- package/src/integrations/herdr-status.ts +330 -0
- package/src/intercom/intercom-bridge.ts +187 -0
- package/src/intercom/native-supervisor-channel.ts +715 -0
- package/src/intercom/result-intercom.ts +421 -0
- package/src/missions/actions.ts +410 -0
- package/src/missions/goal-driver.ts +162 -0
- package/src/missions/lifecycle.ts +346 -0
- package/src/missions/store.ts +507 -0
- package/src/missions/types.ts +157 -0
- package/src/missions/workflow-state.ts +250 -0
- package/src/policy/authority.ts +46 -0
- package/src/profiles/profiles.ts +661 -0
- package/src/runs/background/async-execution.ts +1574 -0
- package/src/runs/background/async-job-tracker.ts +511 -0
- package/src/runs/background/async-resume.ts +539 -0
- package/src/runs/background/async-status.ts +542 -0
- package/src/runs/background/auto-drain.ts +67 -0
- package/src/runs/background/chain-append.ts +328 -0
- package/src/runs/background/chain-root-attachment.ts +199 -0
- package/src/runs/background/completion-batcher.ts +168 -0
- package/src/runs/background/completion-dedupe.ts +54 -0
- package/src/runs/background/control-channel.ts +692 -0
- package/src/runs/background/fleet-view.ts +540 -0
- package/src/runs/background/notify.ts +332 -0
- package/src/runs/background/parallel-groups.ts +45 -0
- package/src/runs/background/process-terminal.ts +280 -0
- package/src/runs/background/result-watcher.ts +414 -0
- package/src/runs/background/retained-children.ts +68 -0
- package/src/runs/background/run-id-resolver.ts +167 -0
- package/src/runs/background/run-status.ts +504 -0
- package/src/runs/background/scheduled-runs.ts +753 -0
- package/src/runs/background/stale-run-reconciler.ts +396 -0
- package/src/runs/background/steering.ts +239 -0
- package/src/runs/background/subagent-runner.ts +4718 -0
- package/src/runs/background/subagent-wait.ts +661 -0
- package/src/runs/background/top-level-async.ts +14 -0
- package/src/runs/background/wait-completions.ts +112 -0
- package/src/runs/background/wait-config.ts +36 -0
- package/src/runs/background/wait-subscriptions.ts +253 -0
- package/src/runs/background/wait-tool.ts +34 -0
- package/src/runs/foreground/async-steering-action.ts +256 -0
- package/src/runs/foreground/async-stop-action.ts +65 -0
- package/src/runs/foreground/chain-clarify.ts +1354 -0
- package/src/runs/foreground/chain-execution.ts +1527 -0
- package/src/runs/foreground/execution.ts +1868 -0
- package/src/runs/foreground/foreground-control.ts +132 -0
- package/src/runs/foreground/subagent-executor.ts +5532 -0
- package/src/runs/shared/acceptance.ts +1365 -0
- package/src/runs/shared/agent-contract.ts +38 -0
- package/src/runs/shared/capability-ceiling.ts +209 -0
- package/src/runs/shared/chain-outputs.ts +118 -0
- package/src/runs/shared/child-protocol.ts +401 -0
- package/src/runs/shared/completion-guard.ts +95 -0
- package/src/runs/shared/context-mode.ts +44 -0
- package/src/runs/shared/dynamic-fanout.ts +297 -0
- package/src/runs/shared/external-cli-runner.ts +130 -0
- package/src/runs/shared/long-running-guard.ts +220 -0
- package/src/runs/shared/mcp-direct-tool-allowlist.ts +406 -0
- package/src/runs/shared/model-fallback.ts +336 -0
- package/src/runs/shared/model-scope.ts +128 -0
- package/src/runs/shared/nested-events.ts +1076 -0
- package/src/runs/shared/nested-path.ts +52 -0
- package/src/runs/shared/nested-render.ts +123 -0
- package/src/runs/shared/parallel-handoff.ts +238 -0
- package/src/runs/shared/parallel-utils.ts +256 -0
- package/src/runs/shared/permissions.ts +95 -0
- package/src/runs/shared/pi-args.ts +808 -0
- package/src/runs/shared/pi-spawn.ts +163 -0
- package/src/runs/shared/process-signal.ts +19 -0
- package/src/runs/shared/run-history.ts +181 -0
- package/src/runs/shared/runtime-acknowledged-extensions.ts +71 -0
- package/src/runs/shared/session-lease.ts +299 -0
- package/src/runs/shared/single-output.ts +235 -0
- package/src/runs/shared/spawn-budget.ts +128 -0
- package/src/runs/shared/structured-output.ts +182 -0
- package/src/runs/shared/subagent-control.ts +231 -0
- package/src/runs/shared/subagent-prompt-runtime.ts +588 -0
- package/src/runs/shared/subagent-startup-retry.ts +104 -0
- package/src/runs/shared/task-intent.ts +181 -0
- package/src/runs/shared/tool-availability.ts +83 -0
- package/src/runs/shared/tool-budget.ts +80 -0
- package/src/runs/shared/turn-budget.ts +98 -0
- package/src/runs/shared/usage-budget.ts +65 -0
- package/src/runs/shared/workflow-graph.ts +231 -0
- package/src/runs/shared/worktree.ts +759 -0
- package/src/shared/accessible-dir.ts +47 -0
- package/src/shared/artifacts.ts +285 -0
- package/src/shared/atomic-json.ts +61 -0
- package/src/shared/child-transcript.ts +264 -0
- package/src/shared/file-coalescer.ts +40 -0
- package/src/shared/file-system-retry.ts +47 -0
- package/src/shared/fork-context.ts +213 -0
- package/src/shared/formatters.ts +133 -0
- package/src/shared/jsonl-writer.ts +81 -0
- package/src/shared/launch-contract.ts +124 -0
- package/src/shared/model-info.ts +81 -0
- package/src/shared/post-exit-stdio-guard.ts +85 -0
- package/src/shared/session-identity.ts +10 -0
- package/src/shared/session-tokens.ts +44 -0
- package/src/shared/settings.ts +501 -0
- package/src/shared/status-format.ts +55 -0
- package/src/shared/types.ts +2069 -0
- package/src/shared/utils.ts +613 -0
- package/src/slash/delegation-adapters.ts +434 -0
- package/src/slash/delegation-json.ts +108 -0
- package/src/slash/delegation-request.ts +166 -0
- package/src/slash/prompt-template-bridge.ts +383 -0
- package/src/slash/prompt-workflows.ts +300 -0
- package/src/slash/selector.ts +147 -0
- package/src/slash/slash-bridge.ts +192 -0
- package/src/slash/slash-commands.ts +994 -0
- package/src/slash/slash-live-state.ts +296 -0
- package/src/slash/subagents-admin.ts +432 -0
- package/src/tui/fleet-status.ts +564 -0
- package/src/tui/fleet-transcript.ts +577 -0
- package/src/tui/fleet.ts +879 -0
- package/src/tui/render-helpers.ts +80 -0
- package/src/tui/render.ts +2029 -0
- package/src/types/pi-runtime-compat.d.ts +8 -0
- package/src/watchdog/change-signature.ts +220 -0
- package/src/watchdog/child-status.ts +205 -0
- package/src/watchdog/emission-guard.ts +123 -0
- package/src/watchdog/lsp-diagnostics.ts +537 -0
- package/src/watchdog/model-selection.ts +167 -0
- package/src/watchdog/permission-arbiter.ts +145 -0
- package/src/watchdog/register-child.ts +117 -0
- package/src/watchdog/register-main.ts +440 -0
- package/src/watchdog/render.ts +54 -0
- package/src/watchdog/review.ts +302 -0
- package/src/watchdog/runtime.ts +868 -0
- package/src/watchdog/scope.ts +62 -0
- package/src/watchdog/settings.ts +568 -0
- package/src/watchdog/tool-actions.ts +155 -0
- package/src/watchdog/turn-delta.ts +161 -0
- package/src/watchdog/types.ts +198 -0
- package/src/watchdog/warning-format.ts +73 -0
- package/src/workflows/chat-progress.ts +140 -0
- package/src/workflows/scripted-workflow.ts +506 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { parseFrontmatter } from "../agents/frontmatter.ts";
|
|
6
|
+
import type { SubagentParamsLike } from "../runs/foreground/subagent-executor.ts";
|
|
7
|
+
import { getAgentDir, getProjectConfigDir } from "../shared/utils.ts";
|
|
8
|
+
|
|
9
|
+
interface PromptWorkflow {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
body: string;
|
|
13
|
+
filePath: string;
|
|
14
|
+
agent: string;
|
|
15
|
+
context?: "fresh" | "fork";
|
|
16
|
+
model?: string;
|
|
17
|
+
skill?: string | string[] | false;
|
|
18
|
+
cwd?: string;
|
|
19
|
+
chain?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type PromptWorkflowRunner = (params: SubagentParamsLike, ctx: ExtensionContext) => Promise<void>;
|
|
23
|
+
|
|
24
|
+
const RESERVED_COMMAND_NAMES = new Set([
|
|
25
|
+
"chain-prompts",
|
|
26
|
+
"prompt-workflow",
|
|
27
|
+
"run",
|
|
28
|
+
"chain",
|
|
29
|
+
"parallel",
|
|
30
|
+
"run-chain",
|
|
31
|
+
"subagents-doctor",
|
|
32
|
+
"subagents-models",
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
function packagePromptsDir(): string {
|
|
36
|
+
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "prompts");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function promptDirs(cwd: string): string[] {
|
|
40
|
+
return [
|
|
41
|
+
packagePromptsDir(),
|
|
42
|
+
path.join(getAgentDir(), "prompts"),
|
|
43
|
+
path.join(getProjectConfigDir(cwd), "prompts"),
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readPromptFiles(cwd: string): string[] {
|
|
48
|
+
const files: string[] = [];
|
|
49
|
+
for (const dir of promptDirs(cwd)) {
|
|
50
|
+
let entries: fs.Dirent[];
|
|
51
|
+
try {
|
|
52
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
53
|
+
} catch {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
if (entry.isFile() && entry.name.endsWith(".md")) files.push(path.join(dir, entry.name));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return files;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function firstNonEmptyLine(value: string): string {
|
|
64
|
+
return value.split(/\r?\n/).map((line) => line.trim()).find(Boolean) ?? "Prompt workflow";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function stringField(frontmatter: Record<string, string>, key: string): string | undefined {
|
|
68
|
+
const value = frontmatter[key]?.trim();
|
|
69
|
+
return value ? value : undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function booleanField(frontmatter: Record<string, string>, key: string): boolean | undefined {
|
|
73
|
+
const value = frontmatter[key]?.trim().toLowerCase();
|
|
74
|
+
if (value === "true" || value === "yes" || value === "1") return true;
|
|
75
|
+
if (value === "false" || value === "no" || value === "0") return false;
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseSkill(value: string | undefined): string | string[] | false | undefined {
|
|
80
|
+
if (!value) return undefined;
|
|
81
|
+
if (value === "false") return false;
|
|
82
|
+
const parts = value.split(",").map((part) => part.trim()).filter(Boolean);
|
|
83
|
+
return parts.length > 1 ? parts : parts[0];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function parseAgent(frontmatter: Record<string, string>): string {
|
|
87
|
+
const subagent = stringField(frontmatter, "subagent");
|
|
88
|
+
if (!subagent || subagent === "true") return "delegate";
|
|
89
|
+
return subagent;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function loadPromptWorkflow(filePath: string): PromptWorkflow | undefined {
|
|
93
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
94
|
+
const { frontmatter, body } = parseFrontmatter(content);
|
|
95
|
+
const name = path.basename(filePath, ".md");
|
|
96
|
+
if (!name || RESERVED_COMMAND_NAMES.has(name)) return undefined;
|
|
97
|
+
const model = stringField(frontmatter, "model");
|
|
98
|
+
const skill = parseSkill(stringField(frontmatter, "skill"));
|
|
99
|
+
const cwd = stringField(frontmatter, "cwd");
|
|
100
|
+
const chain = stringField(frontmatter, "chain");
|
|
101
|
+
return {
|
|
102
|
+
name,
|
|
103
|
+
description: stringField(frontmatter, "description") ?? firstNonEmptyLine(body),
|
|
104
|
+
body,
|
|
105
|
+
filePath,
|
|
106
|
+
agent: parseAgent(frontmatter),
|
|
107
|
+
...(booleanField(frontmatter, "inheritContext") === true || booleanField(frontmatter, "fork") === true ? { context: "fork" as const } : {}),
|
|
108
|
+
...(booleanField(frontmatter, "fresh") === true ? { context: "fresh" as const } : {}),
|
|
109
|
+
...(model ? { model } : {}),
|
|
110
|
+
...(skill !== undefined ? { skill } : {}),
|
|
111
|
+
...(cwd ? { cwd } : {}),
|
|
112
|
+
...(chain ? { chain } : {}),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function discoverPromptWorkflows(cwd: string): PromptWorkflow[] {
|
|
117
|
+
const workflows = new Map<string, PromptWorkflow>();
|
|
118
|
+
for (const file of readPromptFiles(cwd)) {
|
|
119
|
+
const workflow = loadPromptWorkflow(file);
|
|
120
|
+
if (workflow) workflows.set(workflow.name, workflow);
|
|
121
|
+
}
|
|
122
|
+
return [...workflows.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function shellWords(input: string): string[] {
|
|
126
|
+
const words: string[] = [];
|
|
127
|
+
let current = "";
|
|
128
|
+
let quote: "'" | '"' | undefined;
|
|
129
|
+
let escaped = false;
|
|
130
|
+
for (const ch of input) {
|
|
131
|
+
if (escaped) {
|
|
132
|
+
current += ch;
|
|
133
|
+
escaped = false;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (ch === "\\") {
|
|
137
|
+
escaped = true;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (quote) {
|
|
141
|
+
if (ch === quote) quote = undefined;
|
|
142
|
+
else current += ch;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (ch === "'" || ch === '"') {
|
|
146
|
+
quote = ch;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (/\s/.test(ch)) {
|
|
150
|
+
if (current) {
|
|
151
|
+
words.push(current);
|
|
152
|
+
current = "";
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
current += ch;
|
|
157
|
+
}
|
|
158
|
+
if (current) words.push(current);
|
|
159
|
+
return words;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function substituteArgs(template: string, args: string[]): string {
|
|
163
|
+
const all = args.join(" ");
|
|
164
|
+
return template
|
|
165
|
+
.replace(/\$ARGUMENTS/g, all)
|
|
166
|
+
.replace(/\$@/g, all)
|
|
167
|
+
.replace(/\$\{(\d+):-([^}]*)\}/g, (_match, index: string, fallback: string) => args[Number(index) - 1] || fallback)
|
|
168
|
+
.replace(/\$(\d+)/g, (_match, index: string) => args[Number(index) - 1] ?? "");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function parseRuntimeOptions(words: string[]): { args: string[]; agentOverride?: string; fork?: boolean; fresh?: boolean; bg?: boolean } {
|
|
172
|
+
const args: string[] = [];
|
|
173
|
+
let agentOverride: string | undefined;
|
|
174
|
+
let fork = false;
|
|
175
|
+
let fresh = false;
|
|
176
|
+
let bg = false;
|
|
177
|
+
for (let i = 0; i < words.length; i++) {
|
|
178
|
+
const word = words[i]!;
|
|
179
|
+
if (word === "--fork") {
|
|
180
|
+
fork = true;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (word === "--fresh") {
|
|
184
|
+
fresh = true;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (word === "--bg" || word === "--async") {
|
|
188
|
+
bg = true;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (word === "--subagent") {
|
|
192
|
+
agentOverride = words[++i];
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const eq = word.match(/^--subagent(?:=|:)(.+)$/);
|
|
196
|
+
if (eq) {
|
|
197
|
+
agentOverride = eq[1];
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
args.push(word);
|
|
201
|
+
}
|
|
202
|
+
return { args, agentOverride, fork, fresh, bg };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
function splitPromptChain(input: string): string[] {
|
|
207
|
+
return input.split(" -> ").map((part) => part.trim()).filter(Boolean);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function workflowParams(workflow: PromptWorkflow, args: string[], runtime: ReturnType<typeof parseRuntimeOptions>): SubagentParamsLike {
|
|
211
|
+
const task = substituteArgs(workflow.body, args).trim();
|
|
212
|
+
const context = runtime.fork ? "fork" : runtime.fresh ? "fresh" : workflow.context;
|
|
213
|
+
return {
|
|
214
|
+
agent: runtime.agentOverride ?? workflow.agent,
|
|
215
|
+
task,
|
|
216
|
+
agentScope: "both",
|
|
217
|
+
...(context ? { context } : {}),
|
|
218
|
+
...(workflow.model ? { model: workflow.model } : {}),
|
|
219
|
+
...(workflow.skill !== undefined ? { skill: workflow.skill } : {}),
|
|
220
|
+
...(workflow.cwd ? { cwd: workflow.cwd } : {}),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function promptWorkflowExecutionParams(workflows: PromptWorkflow[], args: string[], runtime: ReturnType<typeof parseRuntimeOptions>): SubagentParamsLike {
|
|
225
|
+
return {
|
|
226
|
+
workflowScript: promptWorkflowScript(workflows, args, runtime),
|
|
227
|
+
agentScope: "both",
|
|
228
|
+
async: runtime.bg ? true : false,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function promptWorkflowScript(workflows: PromptWorkflow[], args: string[], runtime: ReturnType<typeof parseRuntimeOptions>): string {
|
|
233
|
+
const launches = workflows.map((workflow, index) => {
|
|
234
|
+
const params = workflowParams(workflow, args, runtime);
|
|
235
|
+
const task = params.task ?? "";
|
|
236
|
+
const child = {
|
|
237
|
+
agent: params.agent ?? "delegate",
|
|
238
|
+
task,
|
|
239
|
+
...(params.model ? { model: params.model } : {}),
|
|
240
|
+
...(params.skill !== undefined ? { skill: params.skill } : {}),
|
|
241
|
+
...(params.cwd ? { cwd: params.cwd } : {}),
|
|
242
|
+
...(params.context ? { context: params.context } : {}),
|
|
243
|
+
};
|
|
244
|
+
return `const step${index} = await runs.run(${JSON.stringify(`prompt-${index + 1}-${workflow.name}`)}, { ...${JSON.stringify(child)}, task: ${JSON.stringify(task)}.replaceAll("{previous}", previous) });\nprevious = step${index}.output;`;
|
|
245
|
+
});
|
|
246
|
+
return `let previous = "";\n${launches.join("\n")}\nreturn previous;`;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function findWorkflow(workflows: PromptWorkflow[], name: string): PromptWorkflow | undefined {
|
|
250
|
+
return workflows.find((workflow) => workflow.name === name);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function formatWorkflowList(workflows: PromptWorkflow[]): string {
|
|
254
|
+
if (workflows.length === 0) return "No prompt workflows found in package, user, or project prompts.";
|
|
255
|
+
return [
|
|
256
|
+
"Prompt workflows:",
|
|
257
|
+
...workflows.map((workflow) => `- ${workflow.name}: ${workflow.description} (${workflow.filePath})`),
|
|
258
|
+
].join("\n");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function registerPromptWorkflowCommands(input: {
|
|
262
|
+
pi: ExtensionAPI;
|
|
263
|
+
run: PromptWorkflowRunner;
|
|
264
|
+
}): void {
|
|
265
|
+
const { pi, run } = input;
|
|
266
|
+
|
|
267
|
+
pi.registerCommand("prompt-workflow", {
|
|
268
|
+
description: "Run a prompt template through native pi-subagents: /prompt-workflow <name> [args]",
|
|
269
|
+
handler: async (rawArgs, ctx) => {
|
|
270
|
+
const words = shellWords(rawArgs);
|
|
271
|
+
const name = words.shift();
|
|
272
|
+
const workflows = discoverPromptWorkflows(ctx.cwd);
|
|
273
|
+
if (!name || name === "list") {
|
|
274
|
+
pi.sendMessage({ content: formatWorkflowList(workflows), display: true } as Parameters<typeof pi.sendMessage>[0]);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const workflow = findWorkflow(workflows, name);
|
|
278
|
+
if (!workflow) {
|
|
279
|
+
ctx.ui.notify(`Unknown prompt workflow: ${name}`, "error");
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const runtime = parseRuntimeOptions(words);
|
|
283
|
+
try {
|
|
284
|
+
if (workflow.chain) {
|
|
285
|
+
const chain = splitPromptChain(workflow.chain).map((stepName) => {
|
|
286
|
+
const step = findWorkflow(workflows, stepName);
|
|
287
|
+
if (!step) throw new Error(`Unknown prompt workflow in chain '${workflow.name}': ${stepName}`);
|
|
288
|
+
return step;
|
|
289
|
+
});
|
|
290
|
+
await run(promptWorkflowExecutionParams(chain, runtime.args, runtime), ctx);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
await run(promptWorkflowExecutionParams([workflow], runtime.args, runtime), ctx);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { DynamicBorder, keyHint, rawKeyHint, type Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Container, fuzzyFilter, Input, type KeybindingsManager, Spacer, Text, type TUI } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
/** A single selectable row. `value` is returned on confirm; `label` is the primary text. */
|
|
5
|
+
export interface SelectorItem {
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
/** Optional dimmed badge shown after the label (e.g. a provider name). */
|
|
9
|
+
badge?: string;
|
|
10
|
+
/** Marks the current selection (rendered with a ✓ checkmark). */
|
|
11
|
+
current?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SelectorResult {
|
|
15
|
+
confirmed: boolean;
|
|
16
|
+
value?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SelectorOptions {
|
|
20
|
+
title: string;
|
|
21
|
+
/** Optional dimmed hint line under the title (e.g. the current value). */
|
|
22
|
+
subtitle?: string;
|
|
23
|
+
items: SelectorItem[];
|
|
24
|
+
done: (result: SelectorResult) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const MAX_VISIBLE = 10;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A single-select list with a search field and a bounded scroll window that keeps the
|
|
31
|
+
* highlighted row on screen with an `(n/total)` indicator, so the selection never scrolls
|
|
32
|
+
* out of view when the option list is long. Composed from pi's own TUI primitives so it
|
|
33
|
+
* matches the built-in `/model` picker.
|
|
34
|
+
*
|
|
35
|
+
* Colors come from the `theme` passed to the factory: the module-level `theme` singleton
|
|
36
|
+
* is undefined under the extension's jiti module cache.
|
|
37
|
+
*/
|
|
38
|
+
export class SelectorComponent extends Container {
|
|
39
|
+
private readonly tui: TUI;
|
|
40
|
+
private readonly theme: Theme;
|
|
41
|
+
private readonly keybindings: KeybindingsManager;
|
|
42
|
+
private readonly items: SelectorItem[];
|
|
43
|
+
private readonly done: (result: SelectorResult) => void;
|
|
44
|
+
|
|
45
|
+
private readonly searchInput: Input;
|
|
46
|
+
private readonly listContainer: Container;
|
|
47
|
+
private filtered: SelectorItem[];
|
|
48
|
+
private selectedIndex = 0;
|
|
49
|
+
|
|
50
|
+
constructor(tui: TUI, theme: Theme, keybindings: KeybindingsManager, options: SelectorOptions) {
|
|
51
|
+
super();
|
|
52
|
+
this.tui = tui;
|
|
53
|
+
this.theme = theme;
|
|
54
|
+
this.keybindings = keybindings;
|
|
55
|
+
this.items = options.items;
|
|
56
|
+
this.done = options.done;
|
|
57
|
+
this.filtered = [...this.items];
|
|
58
|
+
|
|
59
|
+
const border = () => new DynamicBorder((str) => theme.fg("border", str));
|
|
60
|
+
this.addChild(border());
|
|
61
|
+
this.addChild(new Spacer(1));
|
|
62
|
+
this.addChild(new Text(theme.fg("accent", theme.bold(options.title)), 0, 0));
|
|
63
|
+
if (options.subtitle) this.addChild(new Text(theme.fg("muted", options.subtitle), 0, 0));
|
|
64
|
+
this.addChild(new Spacer(1));
|
|
65
|
+
|
|
66
|
+
this.searchInput = new Input();
|
|
67
|
+
this.searchInput.focused = true;
|
|
68
|
+
this.searchInput.onSubmit = () => this.confirmSelection();
|
|
69
|
+
this.addChild(this.searchInput);
|
|
70
|
+
this.addChild(new Spacer(1));
|
|
71
|
+
|
|
72
|
+
this.listContainer = new Container();
|
|
73
|
+
this.addChild(this.listContainer);
|
|
74
|
+
this.addChild(new Spacer(1));
|
|
75
|
+
|
|
76
|
+
this.addChild(new Text(rawKeyHint("↑↓", "navigate") + " " + keyHint("tui.select.confirm", "select") + " " + keyHint("tui.select.cancel", "cancel"), 0, 0));
|
|
77
|
+
this.addChild(new Spacer(1));
|
|
78
|
+
this.addChild(border());
|
|
79
|
+
|
|
80
|
+
const currentIndex = this.filtered.findIndex((item) => item.current);
|
|
81
|
+
if (currentIndex >= 0) this.selectedIndex = currentIndex;
|
|
82
|
+
this.updateList();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private applyFilter(query: string): void {
|
|
86
|
+
const previous = this.filtered[this.selectedIndex]?.value;
|
|
87
|
+
this.filtered = query
|
|
88
|
+
? fuzzyFilter(this.items, query, (item) => `${item.label} ${item.value} ${item.badge ?? ""}`)
|
|
89
|
+
: [...this.items];
|
|
90
|
+
const keepIndex = this.filtered.findIndex((item) => item.value === previous);
|
|
91
|
+
this.selectedIndex = keepIndex >= 0 ? keepIndex : Math.min(this.selectedIndex, Math.max(0, this.filtered.length - 1));
|
|
92
|
+
this.updateList();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private updateList(): void {
|
|
96
|
+
const th = this.theme;
|
|
97
|
+
this.listContainer.clear();
|
|
98
|
+
const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(MAX_VISIBLE / 2), this.filtered.length - MAX_VISIBLE));
|
|
99
|
+
const endIndex = Math.min(startIndex + MAX_VISIBLE, this.filtered.length);
|
|
100
|
+
|
|
101
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
102
|
+
const item = this.filtered[i]!;
|
|
103
|
+
const isSelected = i === this.selectedIndex;
|
|
104
|
+
const badge = item.badge ? ` ${th.fg("muted", `[${item.badge}]`)}` : "";
|
|
105
|
+
const checkmark = item.current ? th.fg("success", " ✓") : "";
|
|
106
|
+
const line = isSelected
|
|
107
|
+
? `${th.fg("accent", "→ ")}${th.fg("accent", item.label)}${badge}${checkmark}`
|
|
108
|
+
: ` ${item.label}${badge}${checkmark}`;
|
|
109
|
+
this.listContainer.addChild(new Text(line, 0, 0));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (startIndex > 0 || endIndex < this.filtered.length) {
|
|
113
|
+
this.listContainer.addChild(new Text(th.fg("muted", ` (${this.selectedIndex + 1}/${this.filtered.length})`), 0, 0));
|
|
114
|
+
}
|
|
115
|
+
if (this.filtered.length === 0) {
|
|
116
|
+
this.listContainer.addChild(new Text(th.fg("muted", " No matching entries"), 0, 0));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private confirmSelection(): void {
|
|
121
|
+
const selected = this.filtered[this.selectedIndex];
|
|
122
|
+
this.done(selected ? { confirmed: true, value: selected.value } : { confirmed: false });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
handleInput(keyData: string): void {
|
|
126
|
+
const kb = this.keybindings;
|
|
127
|
+
if (kb.matches(keyData, "tui.select.up")) {
|
|
128
|
+
if (this.filtered.length === 0) return;
|
|
129
|
+
this.selectedIndex = this.selectedIndex === 0 ? this.filtered.length - 1 : this.selectedIndex - 1;
|
|
130
|
+
this.updateList();
|
|
131
|
+
this.tui.requestRender();
|
|
132
|
+
} else if (kb.matches(keyData, "tui.select.down")) {
|
|
133
|
+
if (this.filtered.length === 0) return;
|
|
134
|
+
this.selectedIndex = this.selectedIndex === this.filtered.length - 1 ? 0 : this.selectedIndex + 1;
|
|
135
|
+
this.updateList();
|
|
136
|
+
this.tui.requestRender();
|
|
137
|
+
} else if (kb.matches(keyData, "tui.select.confirm")) {
|
|
138
|
+
this.confirmSelection();
|
|
139
|
+
} else if (kb.matches(keyData, "tui.select.cancel")) {
|
|
140
|
+
this.done({ confirmed: false });
|
|
141
|
+
} else {
|
|
142
|
+
this.searchInput.handleInput(keyData);
|
|
143
|
+
this.applyFilter(this.searchInput.getValue());
|
|
144
|
+
this.tui.requestRender();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { AgentToolResult } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { normalizePublicSubagentExecution } from "../extension/public-execution.ts";
|
|
4
|
+
import type { SubagentParamsLike } from "../runs/foreground/subagent-executor.ts";
|
|
5
|
+
import {
|
|
6
|
+
SLASH_SUBAGENT_CANCEL_EVENT,
|
|
7
|
+
SLASH_SUBAGENT_REQUEST_EVENT,
|
|
8
|
+
SLASH_SUBAGENT_RESPONSE_EVENT,
|
|
9
|
+
SLASH_SUBAGENT_STARTED_EVENT,
|
|
10
|
+
SLASH_SUBAGENT_UPDATE_EVENT,
|
|
11
|
+
type Details,
|
|
12
|
+
} from "../shared/types.ts";
|
|
13
|
+
|
|
14
|
+
interface SlashSubagentRequest {
|
|
15
|
+
requestId: string;
|
|
16
|
+
params: SubagentParamsLike;
|
|
17
|
+
/** Optional requester context for in-process extension bridge calls. */
|
|
18
|
+
ctx?: ExtensionContext;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface SlashSubagentResponse {
|
|
22
|
+
requestId: string;
|
|
23
|
+
result: AgentToolResult<Details>;
|
|
24
|
+
isError: boolean;
|
|
25
|
+
errorText?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SlashSubagentUpdate {
|
|
29
|
+
requestId: string;
|
|
30
|
+
progress?: Details["progress"];
|
|
31
|
+
currentTool?: string;
|
|
32
|
+
toolCount?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface EventBus {
|
|
36
|
+
on(event: string, handler: (data: unknown) => void): (() => void) | void;
|
|
37
|
+
emit(event: string, data: unknown): void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface SlashBridgeOptions {
|
|
41
|
+
events: EventBus;
|
|
42
|
+
getContext: () => ExtensionContext | null;
|
|
43
|
+
execute: (
|
|
44
|
+
id: string,
|
|
45
|
+
params: SubagentParamsLike,
|
|
46
|
+
signal: AbortSignal,
|
|
47
|
+
onUpdate: ((r: AgentToolResult<Details>) => void) | undefined,
|
|
48
|
+
ctx: ExtensionContext,
|
|
49
|
+
) => Promise<AgentToolResult<Details>>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function registerSlashSubagentBridge(options: SlashBridgeOptions): {
|
|
53
|
+
cancelAll: () => void;
|
|
54
|
+
dispose: () => void;
|
|
55
|
+
} {
|
|
56
|
+
const controllers = new Map<string, AbortController>();
|
|
57
|
+
const pendingCancels = new Set<string>();
|
|
58
|
+
const subscriptions: Array<() => void> = [];
|
|
59
|
+
|
|
60
|
+
const subscribe = (event: string, handler: (data: unknown) => void): void => {
|
|
61
|
+
const unsubscribe = options.events.on(event, handler);
|
|
62
|
+
if (typeof unsubscribe === "function") subscriptions.push(unsubscribe);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
subscribe(SLASH_SUBAGENT_CANCEL_EVENT, (data) => {
|
|
66
|
+
if (!data || typeof data !== "object") return;
|
|
67
|
+
const requestId = (data as { requestId?: unknown }).requestId;
|
|
68
|
+
if (typeof requestId !== "string") return;
|
|
69
|
+
const controller = controllers.get(requestId);
|
|
70
|
+
if (controller) {
|
|
71
|
+
controller.abort();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
pendingCancels.add(requestId);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
subscribe(SLASH_SUBAGENT_REQUEST_EVENT, async (data) => {
|
|
78
|
+
if (!data || typeof data !== "object") return;
|
|
79
|
+
const request = data as Partial<SlashSubagentRequest>;
|
|
80
|
+
if (typeof request.requestId !== "string" || !request.params) return;
|
|
81
|
+
const { requestId } = request as SlashSubagentRequest;
|
|
82
|
+
const normalized = normalizePublicSubagentExecution((request as SlashSubagentRequest).params);
|
|
83
|
+
if (!normalized.ok) {
|
|
84
|
+
options.events.emit(SLASH_SUBAGENT_RESPONSE_EVENT, {
|
|
85
|
+
requestId,
|
|
86
|
+
result: {
|
|
87
|
+
content: [{ type: "text", text: normalized.error }],
|
|
88
|
+
isError: true,
|
|
89
|
+
details: { mode: normalized.mode, results: [] },
|
|
90
|
+
},
|
|
91
|
+
isError: true,
|
|
92
|
+
errorText: normalized.error,
|
|
93
|
+
} satisfies SlashSubagentResponse);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const params = normalized.params;
|
|
97
|
+
|
|
98
|
+
const ctx = request.ctx ?? options.getContext();
|
|
99
|
+
if (!ctx) {
|
|
100
|
+
const response: SlashSubagentResponse = {
|
|
101
|
+
requestId,
|
|
102
|
+
result: {
|
|
103
|
+
content: [{ type: "text", text: "No active extension context for slash subagent execution." }],
|
|
104
|
+
details: { mode: "single" as const, results: [] },
|
|
105
|
+
},
|
|
106
|
+
isError: true,
|
|
107
|
+
errorText: "No active extension context.",
|
|
108
|
+
};
|
|
109
|
+
options.events.emit(SLASH_SUBAGENT_RESPONSE_EVENT, response);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const controller = new AbortController();
|
|
114
|
+
controllers.set(requestId, controller);
|
|
115
|
+
|
|
116
|
+
if (pendingCancels.delete(requestId)) {
|
|
117
|
+
controller.abort();
|
|
118
|
+
const response: SlashSubagentResponse = {
|
|
119
|
+
requestId,
|
|
120
|
+
result: {
|
|
121
|
+
content: [{ type: "text", text: "Cancelled." }],
|
|
122
|
+
details: { mode: "single" as const, results: [] },
|
|
123
|
+
},
|
|
124
|
+
isError: true,
|
|
125
|
+
errorText: "Cancelled before start.",
|
|
126
|
+
};
|
|
127
|
+
options.events.emit(SLASH_SUBAGENT_RESPONSE_EVENT, response);
|
|
128
|
+
controllers.delete(requestId);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
options.events.emit(SLASH_SUBAGENT_STARTED_EVENT, { requestId });
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
const result = await options.execute(
|
|
136
|
+
requestId,
|
|
137
|
+
params,
|
|
138
|
+
controller.signal,
|
|
139
|
+
(update) => {
|
|
140
|
+
const progress = update.details?.progress;
|
|
141
|
+
const first = progress?.[0];
|
|
142
|
+
const payload: SlashSubagentUpdate = {
|
|
143
|
+
requestId,
|
|
144
|
+
progress,
|
|
145
|
+
currentTool: first?.currentTool,
|
|
146
|
+
toolCount: first?.toolCount,
|
|
147
|
+
};
|
|
148
|
+
options.events.emit(SLASH_SUBAGENT_UPDATE_EVENT, payload);
|
|
149
|
+
},
|
|
150
|
+
ctx,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const response: SlashSubagentResponse = {
|
|
154
|
+
requestId,
|
|
155
|
+
result,
|
|
156
|
+
isError: (result as { isError?: boolean }).isError === true,
|
|
157
|
+
errorText: (result as { isError?: boolean }).isError
|
|
158
|
+
? result.content.find((c) => c.type === "text")?.text
|
|
159
|
+
: undefined,
|
|
160
|
+
};
|
|
161
|
+
options.events.emit(SLASH_SUBAGENT_RESPONSE_EVENT, response);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
const response: SlashSubagentResponse = {
|
|
164
|
+
requestId,
|
|
165
|
+
result: {
|
|
166
|
+
content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }],
|
|
167
|
+
details: { mode: "single" as const, results: [] },
|
|
168
|
+
},
|
|
169
|
+
isError: true,
|
|
170
|
+
errorText: error instanceof Error ? error.message : String(error),
|
|
171
|
+
};
|
|
172
|
+
options.events.emit(SLASH_SUBAGENT_RESPONSE_EVENT, response);
|
|
173
|
+
} finally {
|
|
174
|
+
controllers.delete(requestId);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
cancelAll: () => {
|
|
180
|
+
for (const controller of controllers.values()) {
|
|
181
|
+
controller.abort();
|
|
182
|
+
}
|
|
183
|
+
controllers.clear();
|
|
184
|
+
pendingCancels.clear();
|
|
185
|
+
},
|
|
186
|
+
dispose: () => {
|
|
187
|
+
for (const unsubscribe of subscriptions) unsubscribe();
|
|
188
|
+
subscriptions.length = 0;
|
|
189
|
+
pendingCancels.clear();
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|