@esso0428/pi-subagents 0.15.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 +638 -0
- package/CONTRIBUTING.md +68 -0
- package/LICENSE +21 -0
- package/README.md +745 -0
- package/SECURITY.md +95 -0
- package/dist/agent-manager.d.ts +144 -0
- package/dist/agent-manager.js +542 -0
- package/dist/agent-runner.d.ts +212 -0
- package/dist/agent-runner.js +850 -0
- package/dist/agent-types.d.ts +67 -0
- package/dist/agent-types.js +168 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +46 -0
- package/dist/cross-extension-rpc.js +76 -0
- package/dist/custom-agents.d.ts +17 -0
- package/dist/custom-agents.js +156 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +122 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +2209 -0
- package/dist/invocation-config.d.ts +22 -0
- package/dist/invocation-config.js +15 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/model-resolver.d.ts +19 -0
- package/dist/model-resolver.js +80 -0
- package/dist/nico-overrides.d.ts +53 -0
- package/dist/nico-overrides.js +169 -0
- package/dist/output-file.d.ts +24 -0
- package/dist/output-file.js +101 -0
- package/dist/prompts.d.ts +32 -0
- package/dist/prompts.js +73 -0
- package/dist/schedule-store.d.ts +38 -0
- package/dist/schedule-store.js +155 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +338 -0
- package/dist/settings.d.ts +141 -0
- package/dist/settings.js +162 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/status-note.d.ts +13 -0
- package/dist/status-note.js +24 -0
- package/dist/types.d.ts +197 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-widget.d.ts +160 -0
- package/dist/ui/agent-widget.js +484 -0
- package/dist/ui/conversation-viewer.d.ts +57 -0
- package/dist/ui/conversation-viewer.js +354 -0
- package/dist/ui/fleet-list.d.ts +106 -0
- package/dist/ui/fleet-list.js +345 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +95 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/usage.d.ts +50 -0
- package/dist/usage.js +49 -0
- package/dist/worktree.d.ts +45 -0
- package/dist/worktree.js +160 -0
- package/examples/agent-tool-description.md +42 -0
- package/package.json +56 -0
- package/src/agent-manager.ts +631 -0
- package/src/agent-runner.ts +1014 -0
- package/src/agent-types.ts +202 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +122 -0
- package/src/custom-agents.ts +167 -0
- package/src/default-agents.ts +126 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +2400 -0
- package/src/invocation-config.ts +40 -0
- package/src/memory.ts +179 -0
- package/src/model-resolver.ts +100 -0
- package/src/nico-overrides.ts +235 -0
- package/src/output-file.ts +110 -0
- package/src/prompts.ts +99 -0
- package/src/schedule-store.ts +153 -0
- package/src/schedule.ts +365 -0
- package/src/settings.ts +288 -0
- package/src/skill-loader.ts +102 -0
- package/src/status-note.ts +25 -0
- package/src/types.ts +208 -0
- package/src/ui/agent-widget.ts +566 -0
- package/src/ui/conversation-viewer.ts +362 -0
- package/src/ui/fleet-list.ts +380 -0
- package/src/ui/schedule-menu.ts +104 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/usage.ts +60 -0
- package/src/worktree.ts +191 -0
- package/vitest.config.ts +18 -0
package/dist/worktree.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* worktree.ts — Git worktree isolation for agents.
|
|
3
|
+
*
|
|
4
|
+
* Creates a temporary git worktree so the agent works on an isolated copy of the repo.
|
|
5
|
+
* On completion, if no changes were made, the worktree is cleaned up.
|
|
6
|
+
* If changes exist, a branch is created and returned in the result.
|
|
7
|
+
*/
|
|
8
|
+
import { execFileSync } from "node:child_process";
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join, relative } from "node:path";
|
|
13
|
+
/**
|
|
14
|
+
* Create a temporary git worktree for an agent.
|
|
15
|
+
* Returns the worktree path, or undefined if not in a git repo.
|
|
16
|
+
*/
|
|
17
|
+
export function createWorktree(cwd, agentId) {
|
|
18
|
+
// Verify we're in a git repo with at least one commit (HEAD must exist)
|
|
19
|
+
let baseSha;
|
|
20
|
+
let subdir;
|
|
21
|
+
try {
|
|
22
|
+
execFileSync("git", ["rev-parse", "--is-inside-work-tree"], { cwd, stdio: "pipe", timeout: 5000 });
|
|
23
|
+
baseSha = execFileSync("git", ["rev-parse", "HEAD"], { cwd, stdio: "pipe", timeout: 5000 })
|
|
24
|
+
.toString()
|
|
25
|
+
.trim();
|
|
26
|
+
// Where cwd sits inside the repo ("" at the root): the agent must work at
|
|
27
|
+
// the same subdirectory inside the copy, or a monorepo-package cwd would
|
|
28
|
+
// silently widen to the whole repo. realpath both sides — git emits
|
|
29
|
+
// resolved paths while cwd may arrive through a symlink (macOS /tmp).
|
|
30
|
+
const topLevel = execFileSync("git", ["rev-parse", "--show-toplevel"], { cwd, stdio: "pipe", timeout: 5000 })
|
|
31
|
+
.toString()
|
|
32
|
+
.trim();
|
|
33
|
+
subdir = relative(realpathSync(topLevel), realpathSync(cwd));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const branch = `pi-agent-${agentId}`;
|
|
39
|
+
const suffix = randomUUID().slice(0, 8);
|
|
40
|
+
const worktreePath = join(tmpdir(), `pi-agent-${agentId}-${suffix}`);
|
|
41
|
+
try {
|
|
42
|
+
// Create detached worktree at HEAD
|
|
43
|
+
execFileSync("git", ["worktree", "add", "--detach", worktreePath, "HEAD"], {
|
|
44
|
+
cwd,
|
|
45
|
+
stdio: "pipe",
|
|
46
|
+
timeout: 30000,
|
|
47
|
+
});
|
|
48
|
+
return { path: worktreePath, branch, baseSha, workPath: subdir ? join(worktreePath, subdir) : worktreePath };
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// If worktree creation fails, return undefined (agent runs in normal cwd)
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Clean up a worktree after agent completion.
|
|
57
|
+
* - If no changes: remove worktree entirely.
|
|
58
|
+
* - If changes exist: create a branch, commit changes, return branch info.
|
|
59
|
+
*/
|
|
60
|
+
export function cleanupWorktree(cwd, worktree, agentDescription) {
|
|
61
|
+
if (!existsSync(worktree.path)) {
|
|
62
|
+
return { hasChanges: false };
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
// Check for uncommitted changes in the worktree
|
|
66
|
+
const status = execFileSync("git", ["status", "--porcelain"], {
|
|
67
|
+
cwd: worktree.path,
|
|
68
|
+
stdio: "pipe",
|
|
69
|
+
timeout: 10000,
|
|
70
|
+
}).toString().trim();
|
|
71
|
+
if (status) {
|
|
72
|
+
// Changes exist — stage, commit, and create a branch
|
|
73
|
+
execFileSync("git", ["add", "-A"], { cwd: worktree.path, stdio: "pipe", timeout: 10000 });
|
|
74
|
+
// Truncate description for commit message (no shell sanitization needed — execFileSync uses argv)
|
|
75
|
+
const safeDesc = agentDescription.slice(0, 200);
|
|
76
|
+
const commitMsg = `pi-agent: ${safeDesc}`;
|
|
77
|
+
execFileSync("git", ["commit", "--no-verify", "-m", commitMsg], {
|
|
78
|
+
cwd: worktree.path,
|
|
79
|
+
stdio: "pipe",
|
|
80
|
+
timeout: 10000,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const currentSha = execFileSync("git", ["rev-parse", "HEAD"], {
|
|
85
|
+
cwd: worktree.path,
|
|
86
|
+
stdio: "pipe",
|
|
87
|
+
timeout: 5000,
|
|
88
|
+
}).toString().trim();
|
|
89
|
+
if (currentSha === worktree.baseSha) {
|
|
90
|
+
// No changes — remove worktree
|
|
91
|
+
removeWorktree(cwd, worktree.path);
|
|
92
|
+
return { hasChanges: false };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Create a branch pointing to the worktree's HEAD.
|
|
96
|
+
// If the branch already exists, append a suffix to avoid overwriting previous work.
|
|
97
|
+
let branchName = worktree.branch;
|
|
98
|
+
try {
|
|
99
|
+
execFileSync("git", ["branch", branchName], {
|
|
100
|
+
cwd: worktree.path,
|
|
101
|
+
stdio: "pipe",
|
|
102
|
+
timeout: 5000,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// Branch already exists — use a unique suffix
|
|
107
|
+
branchName = `${worktree.branch}-${Date.now()}`;
|
|
108
|
+
execFileSync("git", ["branch", branchName], {
|
|
109
|
+
cwd: worktree.path,
|
|
110
|
+
stdio: "pipe",
|
|
111
|
+
timeout: 5000,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// Update branch name in worktree info for the caller
|
|
115
|
+
worktree.branch = branchName;
|
|
116
|
+
// Remove the worktree (branch persists in main repo)
|
|
117
|
+
removeWorktree(cwd, worktree.path);
|
|
118
|
+
return {
|
|
119
|
+
hasChanges: true,
|
|
120
|
+
branch: worktree.branch,
|
|
121
|
+
path: worktree.path,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// Best effort cleanup on error
|
|
126
|
+
try {
|
|
127
|
+
removeWorktree(cwd, worktree.path);
|
|
128
|
+
}
|
|
129
|
+
catch { /* ignore */ }
|
|
130
|
+
return { hasChanges: false };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Force-remove a worktree.
|
|
135
|
+
*/
|
|
136
|
+
function removeWorktree(cwd, worktreePath) {
|
|
137
|
+
try {
|
|
138
|
+
execFileSync("git", ["worktree", "remove", "--force", worktreePath], {
|
|
139
|
+
cwd,
|
|
140
|
+
stdio: "pipe",
|
|
141
|
+
timeout: 10000,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// If git worktree remove fails, try pruning
|
|
146
|
+
try {
|
|
147
|
+
execFileSync("git", ["worktree", "prune"], { cwd, stdio: "pipe", timeout: 5000 });
|
|
148
|
+
}
|
|
149
|
+
catch { /* ignore */ }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Prune any orphaned worktrees (crash recovery).
|
|
154
|
+
*/
|
|
155
|
+
export function pruneWorktrees(cwd) {
|
|
156
|
+
try {
|
|
157
|
+
execFileSync("git", ["worktree", "prune"], { cwd, stdio: "pipe", timeout: 5000 });
|
|
158
|
+
}
|
|
159
|
+
catch { /* ignore */ }
|
|
160
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Launch a new agent to handle complex, multi-step tasks autonomously. Each agent type has specific capabilities and tools available to it.
|
|
2
|
+
|
|
3
|
+
Available agent types and the tools they have access to:
|
|
4
|
+
{{typeList}}
|
|
5
|
+
|
|
6
|
+
Custom agents can be defined in .pi/agents/<name>.md (project) or {{agentDir}}/agents/<name>.md (global) — they are picked up automatically. Project-level agents override global ones. Creating a .md file with the same name as a default agent overrides it.
|
|
7
|
+
|
|
8
|
+
When using the Agent tool, specify a subagent_type parameter to select which agent type to use.
|
|
9
|
+
|
|
10
|
+
## When not to use
|
|
11
|
+
|
|
12
|
+
If the target is already known, use a direct tool — `read` for a known path, `grep`/`find` for a specific symbol or string. Reserve this tool for open-ended questions that span the codebase, or tasks that match an available agent type.
|
|
13
|
+
|
|
14
|
+
## Usage notes
|
|
15
|
+
|
|
16
|
+
- Always include a short (3-5 word) description summarizing what the agent will do (shown in UI).
|
|
17
|
+
- When you launch multiple agents for independent work, send them in a single message with multiple tool uses, with run_in_background: true on each, so they run concurrently. If the user specifies that they want agents run "in parallel", you MUST send a single message with multiple tool calls. Foreground calls run sequentially — only one executes at a time.
|
|
18
|
+
- When the agent is done, it returns a single message back to you. The result is not visible to the user — to show the user, send a text message with a concise summary.
|
|
19
|
+
- Trust but verify: an agent's summary describes what it intended to do, not necessarily what it did. When an agent writes or edits code, check the actual changes before reporting work as done.
|
|
20
|
+
- Use run_in_background for work you don't need immediately. You will be notified when it completes — do NOT poll or sleep waiting for it. Continue with other work or respond to the user instead.
|
|
21
|
+
- Foreground vs background: use foreground (default) when you need the agent's results before you can proceed. Use background when you have genuinely independent work to do in parallel.
|
|
22
|
+
- Use resume with an agent ID to continue a previous agent's work. A new (non-resume) Agent call starts a fresh agent with no memory of prior runs, so the prompt must be self-contained.
|
|
23
|
+
- Use steer_subagent to send mid-run messages to a running background agent.
|
|
24
|
+
- Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, etc.), since it is not aware of the user's intent.
|
|
25
|
+
- If an agent's description says it should be used proactively, try to use it without the user having to ask for it first.
|
|
26
|
+
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
27
|
+
- Use thinking to control extended thinking level.
|
|
28
|
+
- Use inherit_context if the agent needs the parent conversation history.
|
|
29
|
+
- Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications). The worktree is automatically cleaned up if the agent makes no changes; otherwise the path and branch are returned in the result.{{scheduleGuideline}}
|
|
30
|
+
|
|
31
|
+
## Writing the prompt
|
|
32
|
+
|
|
33
|
+
Provide clear, detailed prompts so the agent can work autonomously. Brief it like a smart colleague who just walked into the room — it hasn't seen this conversation, doesn't know what you've tried, doesn't understand why this task matters.
|
|
34
|
+
- Explain what you're trying to accomplish and why.
|
|
35
|
+
- Describe what you've already learned or ruled out.
|
|
36
|
+
- Give enough context about the surrounding problem that the agent can make judgment calls rather than just following a narrow instruction.
|
|
37
|
+
- If you need a short response, say so ("report in under 200 words").
|
|
38
|
+
- Lookups: hand over the exact command. Investigations: hand over the question — prescribed steps become dead weight when the premise is wrong.
|
|
39
|
+
|
|
40
|
+
Terse command-style prompts produce shallow, generic work.
|
|
41
|
+
|
|
42
|
+
**Never delegate understanding.** Don't write "based on your findings, fix the bug" or "based on the research, implement it." Those phrases push synthesis onto the agent instead of doing it yourself. Write prompts that prove you understood: include file paths, line numbers, what specifically to change.
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@esso0428/pi-subagents",
|
|
3
|
+
"version": "0.15.0",
|
|
4
|
+
"description": "A pi extension that brings smart Claude Code-style autonomous sub-agents to pi, with npm:pi-subagents-style JSON agent overrides.",
|
|
5
|
+
"author": "ESSO0428",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/ESSO0428/pi-subagents.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/ESSO0428/pi-subagents#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/ESSO0428/pi-subagents/issues"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"pi-package",
|
|
17
|
+
"pi",
|
|
18
|
+
"pi-extension",
|
|
19
|
+
"subagent",
|
|
20
|
+
"agent",
|
|
21
|
+
"autonomous"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@earendil-works/pi-ai": ">=0.80.0",
|
|
25
|
+
"@earendil-works/pi-coding-agent": ">=0.80.0",
|
|
26
|
+
"@earendil-works/pi-tui": ">=0.80.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@sinclair/typebox": "^0.34.49",
|
|
30
|
+
"croner": "^10.0.1",
|
|
31
|
+
"nanoid": "^5.0.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc",
|
|
35
|
+
"prepublishOnly": "npm run lint && npm run typecheck && npm run test && npm run build",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest",
|
|
38
|
+
"test:e2e": "vitest run e2e --reporter=verbose",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"lint": "biome check src/ test/",
|
|
41
|
+
"lint:fix": "biome check --fix src/ test/"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@biomejs/biome": "^2.4.14",
|
|
45
|
+
"@types/node": "^25.5.0",
|
|
46
|
+
"typescript": "^6.0.0",
|
|
47
|
+
"vitest": "^4.0.18"
|
|
48
|
+
},
|
|
49
|
+
"pi": {
|
|
50
|
+
"extensions": [
|
|
51
|
+
"./src/index.ts"
|
|
52
|
+
],
|
|
53
|
+
"video": "https://github.com/tintinweb/pi-subagents/raw/master/media/demo.mp4",
|
|
54
|
+
"image": "https://github.com/tintinweb/pi-subagents/raw/master/media/screenshot.png"
|
|
55
|
+
}
|
|
56
|
+
}
|