@piotr-oles/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/README.md +105 -0
- package/package.json +49 -0
- package/src/commands/components/subagent-template-details-component.ts +153 -0
- package/src/commands/components/subagent-templates-component.ts +93 -0
- package/src/commands/components/subagent-templates-list-component.ts +118 -0
- package/src/commands/subagent-templates-menu.ts +27 -0
- package/src/domain/instance/done-subagent.ts +101 -0
- package/src/domain/instance/queued-subagent.ts +87 -0
- package/src/domain/instance/running-subagent.ts +224 -0
- package/src/domain/subagent-config.ts +13 -0
- package/src/domain/subagent-instances-manager.ts +248 -0
- package/src/domain/subagent-resource-loader.ts +53 -0
- package/src/domain/subagent-store.ts +208 -0
- package/src/domain/subagent-template.ts +30 -0
- package/src/domain/subagent-templates-manager.ts +183 -0
- package/src/domain/types.ts +26 -0
- package/src/flags.ts +14 -0
- package/src/format.ts +48 -0
- package/src/index.ts +76 -0
- package/src/test-helpers/scripted-session-builder.ts +62 -0
- package/src/test-helpers/scripted-session.ts +95 -0
- package/src/test-helpers.ts +184 -0
- package/src/throttle.ts +14 -0
- package/src/tools/components/inline-container.ts +55 -0
- package/src/tools/components/subagent-tool-call-component.ts +37 -0
- package/src/tools/components/subagent-tool-result-component.ts +362 -0
- package/src/tools/subagent-tool.ts +152 -0
- package/src/tools/types.ts +19 -0
- package/src/xml.ts +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# pi-subagents
|
|
2
|
+
|
|
3
|
+
A [pi coding agent](https://github.com/earendil-works/pi) extension that lets the agent spawn specialized subagents — each running in its own isolated session with its own model, tools, and instructions.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pi install npm:@piotr-oles/pi-subagents
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Agent templates
|
|
12
|
+
|
|
13
|
+
Define agents as markdown files with YAML frontmatter. Two locations are supported:
|
|
14
|
+
|
|
15
|
+
| Location | Scope |
|
|
16
|
+
|---|---|
|
|
17
|
+
| `~/.pi/agent/subagents/<name>.md` | Global — available in all projects |
|
|
18
|
+
| `.pi/subagents/<name>.md` | Project — available in current project only |
|
|
19
|
+
|
|
20
|
+
A project template overrides a global template with the same name.
|
|
21
|
+
|
|
22
|
+
**Example** `.pi/subagents/coder.md`:
|
|
23
|
+
|
|
24
|
+
```markdown
|
|
25
|
+
---
|
|
26
|
+
description: Specialist for writing and editing code
|
|
27
|
+
model: anthropic/claude-opus-4-5
|
|
28
|
+
thinking: low
|
|
29
|
+
max_turns: 30
|
|
30
|
+
included_tools: bash, read, edit, write
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
You are an expert software engineer. Focus only on the assigned task.
|
|
34
|
+
Write clean, well-tested code. Do not ask for clarification — make reasonable assumptions.
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Frontmatter fields:**
|
|
38
|
+
|
|
39
|
+
| Field | Type | Description |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `description` | string | Short description shown in the UI. Defaults to the filename. |
|
|
42
|
+
| `model` | string | Model to use (e.g. `anthropic/claude-haiku-4-5`). Inherits from parent session if omitted. |
|
|
43
|
+
| `thinking` | string | Thinking level: `off`, `minimal`, `low`, `medium`, `high`, `xhigh`. |
|
|
44
|
+
| `max_turns` | number | Hard turn limit. When reached the agent is given grace turns to wrap up. |
|
|
45
|
+
| `grace_turns` | number | Extra turns after `max_turns` before the agent is force-stopped. |
|
|
46
|
+
| `enabled` | boolean | Set to `false` to disable this agent type without deleting the file. |
|
|
47
|
+
| `included_tools` | string | Comma-separated list of tool names available to this agent. Omit to inherit all tools from the parent session. |
|
|
48
|
+
| `included_skills` | string | Comma-separated list of skill names available to this agent. Omit to inherit all skills. |
|
|
49
|
+
| `included_subagents` | string | Comma-separated list of subagent template names this agent may spawn. |
|
|
50
|
+
|
|
51
|
+
If no matching template is found the agent falls back to `general-purpose`, or a plain default if that is also absent.
|
|
52
|
+
|
|
53
|
+
## Tools
|
|
54
|
+
|
|
55
|
+
### `subagent`
|
|
56
|
+
|
|
57
|
+
Spawns a subagent to handle a task, or follows up on a previously completed one.
|
|
58
|
+
|
|
59
|
+
| Parameter | Type | Description |
|
|
60
|
+
|---|---|---|
|
|
61
|
+
| `name` | string | Name of the agent template to use. Required for both spawn and follow-up. |
|
|
62
|
+
| `id` | string | ID of a done subagent to follow-up. Omit when spawning fresh. |
|
|
63
|
+
| `description` | string | Short description of the task (shown in UI). |
|
|
64
|
+
| `prompt` | string | Task prompt sent to the subagent. |
|
|
65
|
+
|
|
66
|
+
Blocks until the subagent completes and returns its result inline. To follow up on a finished subagent, call `subagent` again with the `id` returned in the previous result plus a new `prompt`.
|
|
67
|
+
|
|
68
|
+
## How it works
|
|
69
|
+
|
|
70
|
+
1. On `before_agent_start`, templates are reloaded from disk.
|
|
71
|
+
2. When `subagent` is called, the extension resolves the template, builds an `AgentConfig`, and starts an isolated pi session.
|
|
72
|
+
3. The subagent session inherits the parent's model registry and working directory but gets its own system prompt built from the template's instructions.
|
|
73
|
+
4. The `subagent` tool is excluded from subagent sessions unless `included_subagents` is set — only then can a subagent spawn further subagents (limited to the listed templates).
|
|
74
|
+
5. A concurrency queue limits how many agents run simultaneously; excess agents are queued and started as slots free up.
|
|
75
|
+
|
|
76
|
+
## Flags
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pi --pi-subagents-max-concurrent 4 # max agents running at once (default: 4)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Or via environment variable:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
PI_SUBAGENTS_MAX_CONCURRENT=2 pi
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## `subagent:templates` command
|
|
89
|
+
|
|
90
|
+
Opens an interactive menu listing all loaded agent templates with their source (global `◦` / project `•`) and model. Run via the pi command palette.
|
|
91
|
+
|
|
92
|
+
## Development
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
pnpm install
|
|
96
|
+
pnpm test
|
|
97
|
+
pnpm typecheck
|
|
98
|
+
pnpm check
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
To test changes manually, pass the source entry point directly to pi:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
pi -ne -e packages/pi-subagents/src/index.ts
|
|
105
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@piotr-oles/pi-subagents",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pi Agent extension: spawn specialized subagents with isolated sessions",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/piotr-oles/pi-extensions.git",
|
|
8
|
+
"directory": "packages/pi-subagents"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"pi-package"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"!src/*.test.ts",
|
|
17
|
+
"!src/**/*.test.ts"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"pi": {
|
|
23
|
+
"extensions": [
|
|
24
|
+
"./src/index.ts"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@earendil-works/pi-agent-core": "*",
|
|
29
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@earendil-works/pi-agent-core": "^0.75.5",
|
|
33
|
+
"@earendil-works/pi-ai": "^0.75.5",
|
|
34
|
+
"@earendil-works/pi-coding-agent": "^0.75.5",
|
|
35
|
+
"@earendil-works/pi-tui": "^0.75.5",
|
|
36
|
+
"@marcfargas/pi-test-harness": "^0.6.1",
|
|
37
|
+
"strip-ansi": "^7.2.0",
|
|
38
|
+
"typebox": "^1.1.38",
|
|
39
|
+
"typescript": "^5.8.3",
|
|
40
|
+
"vitest": "^3.2.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"test:watch": "vitest",
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"check": "biome ci src/",
|
|
47
|
+
"fix": "biome check --write src/"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Box, type Component, Container, matchesKey, Spacer, Text } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { SubagentTemplate } from "../../domain/subagent-template.js";
|
|
4
|
+
import { formatFilePath } from "../../format.js";
|
|
5
|
+
|
|
6
|
+
const MAX_INSTRUCTION_LINES = 8;
|
|
7
|
+
const KEY_COLUMN_WIDTH = 14;
|
|
8
|
+
|
|
9
|
+
const THINKING_LEVEL_COLORS = {
|
|
10
|
+
off: "thinkingOff",
|
|
11
|
+
minimal: "thinkingMinimal",
|
|
12
|
+
low: "thinkingLow",
|
|
13
|
+
medium: "thinkingMedium",
|
|
14
|
+
high: "thinkingHigh",
|
|
15
|
+
xhigh: "thinkingXhigh",
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export class SubagentTemplateDetailsComponent implements Component {
|
|
19
|
+
private readonly container: Container;
|
|
20
|
+
|
|
21
|
+
onBack?: () => void;
|
|
22
|
+
onConfirm?: (template: SubagentTemplate) => void;
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
private readonly template: SubagentTemplate,
|
|
26
|
+
private readonly theme: Theme,
|
|
27
|
+
) {
|
|
28
|
+
this.container = new Container();
|
|
29
|
+
|
|
30
|
+
const model = this.template.model ?? "inherit";
|
|
31
|
+
const heading = `${this.theme.bold(this.theme.fg("accent", this.template.name))} · ${this.theme.fg("dim", model)}`;
|
|
32
|
+
this.container.addChild(new Text(heading, 1, 0));
|
|
33
|
+
|
|
34
|
+
if (this.template.filePath) {
|
|
35
|
+
this.container.addChild(
|
|
36
|
+
new Text(this.theme.fg("dim", formatFilePath(this.template.filePath)), 1, 0),
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.container.addChild(new Spacer(1));
|
|
41
|
+
|
|
42
|
+
this.container.addChild(new Text(this.theme.bold("Description"), 1, 0));
|
|
43
|
+
const desc = this.template.description || "(no description)";
|
|
44
|
+
const descBox = new Box(2, 0);
|
|
45
|
+
descBox.addChild(new Text(this.theme.fg("text", desc), 0, 0));
|
|
46
|
+
this.container.addChild(descBox);
|
|
47
|
+
|
|
48
|
+
this.container.addChild(new Spacer(1));
|
|
49
|
+
|
|
50
|
+
this.container.addChild(new Text(this.theme.bold("Configuration"), 1, 0));
|
|
51
|
+
this.container.addChild(new DetailsRow("Model", model, this.theme));
|
|
52
|
+
|
|
53
|
+
if (this.template.thinkingLevel !== undefined) {
|
|
54
|
+
const levelColor = THINKING_LEVEL_COLORS[this.template.thinkingLevel] ?? "thinkingMedium";
|
|
55
|
+
const levelStyled = this.theme.fg(levelColor, this.template.thinkingLevel);
|
|
56
|
+
this.container.addChild(new DetailsRow("Thinking", levelStyled, this.theme));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (this.template.maxTurns !== undefined) {
|
|
60
|
+
this.container.addChild(
|
|
61
|
+
new DetailsRow("Max turns", String(this.template.maxTurns), this.theme),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (this.template.graceTurns !== undefined) {
|
|
66
|
+
this.container.addChild(
|
|
67
|
+
new DetailsRow("Grace turns", String(this.template.graceTurns), this.theme),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (this.template.includedTools !== undefined) {
|
|
72
|
+
this.container.addChild(
|
|
73
|
+
new DetailsRow("Included tools", this.template.includedTools.join(", "), this.theme),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (this.template.includedSkills !== undefined) {
|
|
78
|
+
this.container.addChild(
|
|
79
|
+
new DetailsRow("Included skills", this.template.includedSkills.join(", "), this.theme),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (this.template.instructions.trim().length > 0) {
|
|
84
|
+
this.container.addChild(new Spacer(1));
|
|
85
|
+
this.container.addChild(new Text(this.theme.bold("Instructions"), 1, 0));
|
|
86
|
+
this.container.addChild(new InstructionsPreview(this.template.instructions, this.theme));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.container.addChild(new Spacer(1));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
handleInput(data: string): void {
|
|
93
|
+
if (matchesKey(data, "escape")) {
|
|
94
|
+
this.onBack?.();
|
|
95
|
+
} else if (matchesKey(data, "enter")) {
|
|
96
|
+
this.onConfirm?.(this.template);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
render(width: number): string[] {
|
|
101
|
+
return this.container.render(width);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
invalidate(): void {
|
|
105
|
+
this.container.invalidate();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
class DetailsRow implements Component {
|
|
110
|
+
private readonly box: Box;
|
|
111
|
+
|
|
112
|
+
constructor(key: string, value: string, theme: Theme) {
|
|
113
|
+
this.box = new Box(2, 0);
|
|
114
|
+
const paddedKey = theme.fg("dim", key.padEnd(KEY_COLUMN_WIDTH));
|
|
115
|
+
this.box.addChild(new Text(`${paddedKey}${value}`, 0, 0));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
render(width: number): string[] {
|
|
119
|
+
return this.box.render(width);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
invalidate(): void {
|
|
123
|
+
this.box.invalidate();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class InstructionsPreview implements Component {
|
|
128
|
+
private readonly box: Box;
|
|
129
|
+
|
|
130
|
+
constructor(instructions: string, theme: Theme) {
|
|
131
|
+
this.box = new Box(2, 0);
|
|
132
|
+
|
|
133
|
+
const lines = instructions.split("\n");
|
|
134
|
+
const truncated = lines.length > MAX_INSTRUCTION_LINES;
|
|
135
|
+
const visible = truncated ? lines.slice(0, MAX_INSTRUCTION_LINES) : lines;
|
|
136
|
+
|
|
137
|
+
for (const line of visible) {
|
|
138
|
+
this.box.addChild(new Text(theme.fg("dim", line), 0, 0));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (truncated) {
|
|
142
|
+
this.box.addChild(new Text(theme.fg("dim", "… (truncated)"), 0, 0));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
render(width: number): string[] {
|
|
147
|
+
return this.box.render(width);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
invalidate(): void {
|
|
151
|
+
this.box.invalidate();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { DynamicBorder } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { type Component, Container, Spacer, Text, type TUI } from "@earendil-works/pi-tui";
|
|
4
|
+
import type { SubagentTemplate } from "../../domain/subagent-template.js";
|
|
5
|
+
import { SubagentTemplateDetailsComponent } from "./subagent-template-details-component.js";
|
|
6
|
+
import { SubagentTemplatesListComponent } from "./subagent-templates-list-component.js";
|
|
7
|
+
|
|
8
|
+
type MenuMode = "list" | "detail";
|
|
9
|
+
|
|
10
|
+
export class SubagentTemplatesComponent implements Component {
|
|
11
|
+
private readonly header: Text;
|
|
12
|
+
private readonly listContainer: Container;
|
|
13
|
+
private readonly listSelect: SubagentTemplatesListComponent;
|
|
14
|
+
private readonly detailContainer: Container;
|
|
15
|
+
private readonly listFooter: Text;
|
|
16
|
+
private readonly detailFooter: Text;
|
|
17
|
+
private readonly border: DynamicBorder;
|
|
18
|
+
private detail: SubagentTemplateDetailsComponent | undefined;
|
|
19
|
+
private mode: MenuMode = "list";
|
|
20
|
+
|
|
21
|
+
onClose?: () => void;
|
|
22
|
+
onSelect?: (template: SubagentTemplate) => void;
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
private readonly tui: TUI,
|
|
26
|
+
private readonly theme: Theme,
|
|
27
|
+
templates: SubagentTemplate[],
|
|
28
|
+
) {
|
|
29
|
+
this.listSelect = new SubagentTemplatesListComponent(templates, theme);
|
|
30
|
+
this.listSelect.onCancel = () => this.onClose?.();
|
|
31
|
+
this.listSelect.onOpen = (template) => this.openDetail(template);
|
|
32
|
+
|
|
33
|
+
this.header = new Text(theme.fg("mdHeading", "Subagent Templates"), 1, 0);
|
|
34
|
+
this.listFooter = new Text(theme.fg("dim", "↑↓ navigate • enter select • esc close"), 1, 0);
|
|
35
|
+
this.detailFooter = new Text(theme.fg("dim", "esc back"), 1, 0);
|
|
36
|
+
this.border = new DynamicBorder((s: string) => theme.fg("accent", s));
|
|
37
|
+
|
|
38
|
+
this.listContainer = new Container();
|
|
39
|
+
this.listContainer.addChild(this.border);
|
|
40
|
+
this.listContainer.addChild(this.header);
|
|
41
|
+
this.listContainer.addChild(new Spacer(1));
|
|
42
|
+
this.listContainer.addChild(this.listSelect);
|
|
43
|
+
this.listContainer.addChild(new Spacer(1));
|
|
44
|
+
this.listContainer.addChild(this.listFooter);
|
|
45
|
+
this.listContainer.addChild(this.border);
|
|
46
|
+
|
|
47
|
+
this.detailContainer = new Container();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private openDetail(template: SubagentTemplate): void {
|
|
51
|
+
this.detail = new SubagentTemplateDetailsComponent(template, this.theme);
|
|
52
|
+
this.detail.onBack = () => this.closeDetail();
|
|
53
|
+
this.detail.onConfirm = (t) => this.onSelect?.(t);
|
|
54
|
+
|
|
55
|
+
this.detailContainer.clear();
|
|
56
|
+
this.detailContainer.addChild(this.border);
|
|
57
|
+
this.detailContainer.addChild(this.header);
|
|
58
|
+
this.detailContainer.addChild(new Spacer(1));
|
|
59
|
+
this.detailContainer.addChild(this.detail);
|
|
60
|
+
this.detailContainer.addChild(new Spacer(1));
|
|
61
|
+
this.detailContainer.addChild(this.detailFooter);
|
|
62
|
+
this.detailContainer.addChild(this.border);
|
|
63
|
+
|
|
64
|
+
this.mode = "detail";
|
|
65
|
+
this.tui.requestRender();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private closeDetail(): void {
|
|
69
|
+
this.mode = "list";
|
|
70
|
+
this.detail = undefined;
|
|
71
|
+
this.tui.requestRender();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
handleInput(data: string): void {
|
|
75
|
+
if (this.mode === "detail") {
|
|
76
|
+
this.detail?.handleInput(data);
|
|
77
|
+
} else {
|
|
78
|
+
this.listSelect.handleInput(data);
|
|
79
|
+
}
|
|
80
|
+
this.tui.requestRender();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
render(width: number): string[] {
|
|
84
|
+
return this.mode === "detail"
|
|
85
|
+
? this.detailContainer.render(width)
|
|
86
|
+
: this.listContainer.render(width);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
invalidate(): void {
|
|
90
|
+
this.listContainer.invalidate();
|
|
91
|
+
this.detailContainer.invalidate();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Box, type Component, getKeybindings, Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { SubagentTemplate } from "../../domain/subagent-template.js";
|
|
4
|
+
|
|
5
|
+
const MAX_VISIBLE = 8;
|
|
6
|
+
|
|
7
|
+
export class SubagentTemplatesListComponent implements Component {
|
|
8
|
+
private readonly templates: SubagentTemplate[];
|
|
9
|
+
private readonly rows: SubagentTemplateOptionComponent[];
|
|
10
|
+
private readonly theme: Theme;
|
|
11
|
+
private selectedIndex = 0;
|
|
12
|
+
private maxVisible: number;
|
|
13
|
+
|
|
14
|
+
onOpen?: (template: SubagentTemplate) => void;
|
|
15
|
+
onCancel?: () => void;
|
|
16
|
+
|
|
17
|
+
constructor(templates: SubagentTemplate[], theme: Theme, maxVisible = MAX_VISIBLE) {
|
|
18
|
+
this.templates = templates;
|
|
19
|
+
this.theme = theme;
|
|
20
|
+
this.rows = templates.map(
|
|
21
|
+
(template, i) => new SubagentTemplateOptionComponent(template, theme, i === 0),
|
|
22
|
+
);
|
|
23
|
+
this.maxVisible = maxVisible;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
handleInput(data: string): void {
|
|
27
|
+
const kb = getKeybindings();
|
|
28
|
+
const { rows } = this;
|
|
29
|
+
|
|
30
|
+
if (kb.matches(data, "tui.select.up")) {
|
|
31
|
+
rows[this.selectedIndex]?.setSelected(false);
|
|
32
|
+
this.selectedIndex = this.selectedIndex === 0 ? rows.length - 1 : this.selectedIndex - 1;
|
|
33
|
+
rows[this.selectedIndex]?.setSelected(true);
|
|
34
|
+
} else if (kb.matches(data, "tui.select.down")) {
|
|
35
|
+
rows[this.selectedIndex]?.setSelected(false);
|
|
36
|
+
this.selectedIndex = this.selectedIndex === rows.length - 1 ? 0 : this.selectedIndex + 1;
|
|
37
|
+
rows[this.selectedIndex]?.setSelected(true);
|
|
38
|
+
} else if (kb.matches(data, "tui.select.confirm")) {
|
|
39
|
+
const template = this.templates[this.selectedIndex];
|
|
40
|
+
if (template) {
|
|
41
|
+
this.onOpen?.(template);
|
|
42
|
+
}
|
|
43
|
+
} else if (kb.matches(data, "tui.select.cancel")) {
|
|
44
|
+
this.onCancel?.();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
render(width: number): string[] {
|
|
49
|
+
const lines: string[] = [];
|
|
50
|
+
const { rows, maxVisible } = this;
|
|
51
|
+
|
|
52
|
+
const startIndex = Math.max(
|
|
53
|
+
0,
|
|
54
|
+
Math.min(this.selectedIndex - Math.floor(maxVisible / 2), rows.length - maxVisible),
|
|
55
|
+
);
|
|
56
|
+
const endIndex = Math.min(startIndex + maxVisible, rows.length);
|
|
57
|
+
|
|
58
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
59
|
+
const row = rows[i];
|
|
60
|
+
if (row) {
|
|
61
|
+
lines.push(...row.render(width));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (startIndex > 0 || endIndex < rows.length) {
|
|
66
|
+
const scrollText = ` (${this.selectedIndex + 1}/${rows.length})`;
|
|
67
|
+
lines.push(this.theme.fg("dim", truncateToWidth(scrollText, width - 2, "")));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return lines;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
invalidate(): void {
|
|
74
|
+
for (const row of this.rows) {
|
|
75
|
+
row.invalidate();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class SubagentTemplateOptionComponent implements Component {
|
|
81
|
+
private readonly box: Box;
|
|
82
|
+
private readonly heading: Text;
|
|
83
|
+
|
|
84
|
+
constructor(
|
|
85
|
+
private readonly template: SubagentTemplate,
|
|
86
|
+
private readonly theme: Theme,
|
|
87
|
+
private selected: boolean,
|
|
88
|
+
) {
|
|
89
|
+
this.selected = selected;
|
|
90
|
+
this.box = new Box(0, 1, (s) => (this.selected ? theme.bg("selectedBg", s) : s));
|
|
91
|
+
this.heading = new Text(this.headingText(selected), 0, 0);
|
|
92
|
+
this.box.addChild(this.heading);
|
|
93
|
+
|
|
94
|
+
const desc = template.enabled ? template.description || "(no description)" : "(disabled)";
|
|
95
|
+
this.box.addChild(new Text(theme.fg("dim", `${desc}`), 2, 0));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
setSelected(selected: boolean): void {
|
|
99
|
+
this.selected = selected;
|
|
100
|
+
this.heading.setText(this.headingText(selected));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private headingText(selected: boolean): string {
|
|
104
|
+
const prefix = selected ? this.theme.fg("accent", "›") : " ";
|
|
105
|
+
const nameColor = this.template.enabled ? "accent" : "dim";
|
|
106
|
+
const name = this.theme.bold(this.theme.fg(nameColor, this.template.name));
|
|
107
|
+
const model = this.theme.fg("dim", this.template.model ?? "inherit");
|
|
108
|
+
return `${prefix} ${name} · ${model}`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
render(width: number): string[] {
|
|
112
|
+
return this.box.render(width);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
invalidate(): void {
|
|
116
|
+
this.box.invalidate();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { SubagentTemplate } from "../domain/subagent-template.js";
|
|
3
|
+
import type { SubagentTemplatesManager } from "../domain/subagent-templates-manager.js";
|
|
4
|
+
import { SubagentTemplatesComponent } from "./components/subagent-templates-component.js";
|
|
5
|
+
|
|
6
|
+
export async function showSubagentTemplatesMenu(
|
|
7
|
+
ctx: ExtensionCommandContext,
|
|
8
|
+
templatesManager: SubagentTemplatesManager,
|
|
9
|
+
): Promise<SubagentTemplate | null> {
|
|
10
|
+
await templatesManager.reload();
|
|
11
|
+
const templates = templatesManager.listTemplates();
|
|
12
|
+
|
|
13
|
+
if (!templates.length) {
|
|
14
|
+
ctx.ui.notify(
|
|
15
|
+
"No subagents found in ~/.pi/agent/subagents/ and .pi/subagents directories.",
|
|
16
|
+
"warning",
|
|
17
|
+
);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return ctx.ui.custom<SubagentTemplate | null>((tui, theme, _kb, done) => {
|
|
22
|
+
const menu = new SubagentTemplatesComponent(tui, theme, templates);
|
|
23
|
+
menu.onClose = () => done(null);
|
|
24
|
+
menu.onSelect = (template) => done(template);
|
|
25
|
+
return menu;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { AgentSession, ContextUsage } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { SubagentConfig } from "../subagent-config.js";
|
|
3
|
+
import { QueuedSubagent } from "./queued-subagent.js";
|
|
4
|
+
import type { RunningSubagent } from "./running-subagent.js";
|
|
5
|
+
|
|
6
|
+
export type ExceededLimit = { type: "turns"; maxTurns: number; graceTurns: number };
|
|
7
|
+
|
|
8
|
+
export type DoneResult =
|
|
9
|
+
| { status: "completed"; message: string; steered: boolean }
|
|
10
|
+
| { status: "aborted" }
|
|
11
|
+
| { status: "exceeded_limit"; limit: ExceededLimit }
|
|
12
|
+
| { status: "error"; error: string };
|
|
13
|
+
|
|
14
|
+
export interface DoneAgentParams {
|
|
15
|
+
instance: QueuedSubagent | RunningSubagent;
|
|
16
|
+
result: DoneResult;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface FollowUpSubagentParams {
|
|
20
|
+
prompt: string;
|
|
21
|
+
description: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface DoneSubagentSessionEntry {
|
|
25
|
+
readonly status: "done";
|
|
26
|
+
readonly result: DoneResult;
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly prompt: string;
|
|
29
|
+
readonly description: string;
|
|
30
|
+
readonly config: SubagentConfig;
|
|
31
|
+
readonly sessionFile: string | undefined;
|
|
32
|
+
readonly startedAt: number | undefined;
|
|
33
|
+
readonly doneAt: number;
|
|
34
|
+
readonly duration: number;
|
|
35
|
+
readonly turns: number;
|
|
36
|
+
readonly usage: ContextUsage | undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class DoneSubagent {
|
|
40
|
+
readonly status = "done" as const;
|
|
41
|
+
readonly result: DoneResult;
|
|
42
|
+
readonly id: string;
|
|
43
|
+
readonly prompt: string;
|
|
44
|
+
readonly description: string;
|
|
45
|
+
readonly config: SubagentConfig;
|
|
46
|
+
readonly startedAt: number | undefined;
|
|
47
|
+
readonly doneAt: number;
|
|
48
|
+
readonly session: AgentSession;
|
|
49
|
+
readonly turns: number;
|
|
50
|
+
|
|
51
|
+
get name() {
|
|
52
|
+
return this.config.name;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get template() {
|
|
56
|
+
return this.config.template;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get duration(): number {
|
|
60
|
+
return this.startedAt !== undefined ? this.doneAt - this.startedAt : 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
constructor({ instance, result }: DoneAgentParams) {
|
|
64
|
+
this.result = result;
|
|
65
|
+
this.id = instance.id;
|
|
66
|
+
this.prompt = instance.prompt;
|
|
67
|
+
this.description = instance.description;
|
|
68
|
+
this.config = instance.config;
|
|
69
|
+
this.startedAt = instance.status === "running" ? instance.startedAt : undefined;
|
|
70
|
+
this.doneAt = Date.now();
|
|
71
|
+
this.session = instance.session;
|
|
72
|
+
this.turns = instance.status === "running" ? instance.turn : 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
followUp({ prompt, description }: FollowUpSubagentParams): QueuedSubagent {
|
|
76
|
+
return new QueuedSubagent({
|
|
77
|
+
id: this.id,
|
|
78
|
+
prompt,
|
|
79
|
+
description,
|
|
80
|
+
config: this.config,
|
|
81
|
+
session: this.session,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
toEntry(): DoneSubagentSessionEntry {
|
|
86
|
+
return {
|
|
87
|
+
status: "done",
|
|
88
|
+
result: this.result,
|
|
89
|
+
id: this.id,
|
|
90
|
+
prompt: this.prompt,
|
|
91
|
+
description: this.description,
|
|
92
|
+
config: this.config,
|
|
93
|
+
sessionFile: this.session.sessionFile,
|
|
94
|
+
startedAt: this.startedAt,
|
|
95
|
+
doneAt: this.doneAt,
|
|
96
|
+
duration: this.duration,
|
|
97
|
+
turns: this.turns,
|
|
98
|
+
usage: this.session.getContextUsage(),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|