@xynogen/pix-subagent 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.
@@ -0,0 +1,152 @@
1
+ /**
2
+ * agent-types.ts — Unified agent type registry.
3
+ *
4
+ * Merges embedded default agents with user-defined agents from .pi/agents/*.md.
5
+ * User agents override defaults with the same name. Disabled agents are kept
6
+ * but excluded from spawning.
7
+ *
8
+ * Ported from tintinweb/pi-subagents (MIT). Trimmed: dropped memory tool helpers.
9
+ */
10
+
11
+ import {
12
+ createCodingTools,
13
+ createReadOnlyTools,
14
+ } from "@earendil-works/pi-coding-agent";
15
+ import { DEFAULT_AGENTS } from "./default-agents.ts";
16
+ import type { AgentConfig } from "./types.ts";
17
+
18
+ /**
19
+ * All known built-in tool names, derived from pi's own tool factories so the
20
+ * set tracks pi-mono if it adds/renames a built-in. The `cwd` only binds tool
21
+ * operations we never invoke — we read each tool's `.name` and discard it.
22
+ */
23
+ export const BUILTIN_TOOL_NAMES: string[] = [
24
+ ...new Set(
25
+ [...createCodingTools("."), ...createReadOnlyTools(".")].map((t) => t.name),
26
+ ),
27
+ ];
28
+
29
+ /** Unified runtime registry of all agents (defaults + user-defined). */
30
+ const agents = new Map<string, AgentConfig>();
31
+
32
+ /** When true, DEFAULT_AGENTS are skipped during registration. */
33
+ let disableDefaults = false;
34
+
35
+ export function isDefaultsDisabled(): boolean {
36
+ return disableDefaults;
37
+ }
38
+
39
+ export function setDefaultsDisabled(b: boolean): void {
40
+ disableDefaults = b;
41
+ }
42
+
43
+ /**
44
+ * Register agents into the unified registry.
45
+ * Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
46
+ * Disabled agents (enabled === false) are kept but excluded from spawning.
47
+ */
48
+ export function registerAgents(userAgents: Map<string, AgentConfig>): void {
49
+ agents.clear();
50
+
51
+ if (!disableDefaults) {
52
+ for (const [name, config] of DEFAULT_AGENTS) {
53
+ agents.set(name, config);
54
+ }
55
+ }
56
+
57
+ for (const [name, config] of userAgents) {
58
+ agents.set(name, config);
59
+ }
60
+ }
61
+
62
+ /** Case-insensitive key resolution. */
63
+ function resolveKey(name: string): string | undefined {
64
+ if (agents.has(name)) return name;
65
+ const lower = name.toLowerCase();
66
+ for (const key of agents.keys()) {
67
+ if (key.toLowerCase() === lower) return key;
68
+ }
69
+ return undefined;
70
+ }
71
+
72
+ export function resolveType(name: string): string | undefined {
73
+ return resolveKey(name);
74
+ }
75
+
76
+ export function getAgentConfig(name: string): AgentConfig | undefined {
77
+ const key = resolveKey(name);
78
+ return key ? agents.get(key) : undefined;
79
+ }
80
+
81
+ /** Get all enabled type names (for spawning and tool descriptions). */
82
+ export function getAvailableTypes(): string[] {
83
+ return [...agents.entries()]
84
+ .filter(([, config]) => config.enabled !== false)
85
+ .map(([name]) => name);
86
+ }
87
+
88
+ export function getAllTypes(): string[] {
89
+ return [...agents.keys()];
90
+ }
91
+
92
+ export function isValidType(type: string): boolean {
93
+ const key = resolveKey(type);
94
+ if (!key) return false;
95
+ return agents.get(key)?.enabled !== false;
96
+ }
97
+
98
+ /** Get built-in tool names for a type (case-insensitive). */
99
+ export function getToolNamesForType(type: string): string[] {
100
+ const key = resolveKey(type);
101
+ const raw = key ? agents.get(key) : undefined;
102
+ const config = raw?.enabled !== false ? raw : undefined;
103
+ // undefined → all built-ins; explicit [] → zero built-ins.
104
+ return config?.builtinToolNames ?? [...BUILTIN_TOOL_NAMES];
105
+ }
106
+
107
+ /** Get config for a type, falling back to general-purpose. */
108
+ export function getConfig(type: string): {
109
+ displayName: string;
110
+ description: string;
111
+ builtinToolNames: string[];
112
+ extensions: true | string[] | false;
113
+ excludeExtensions?: string[];
114
+ skills: true | string[] | false;
115
+ promptMode: "replace" | "append";
116
+ } {
117
+ const key = resolveKey(type);
118
+ const config = key ? agents.get(key) : undefined;
119
+ if (config && config.enabled !== false) {
120
+ return {
121
+ displayName: config.displayName ?? config.name,
122
+ description: config.description,
123
+ builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
124
+ extensions: config.extensions,
125
+ excludeExtensions: config.excludeExtensions,
126
+ skills: config.skills,
127
+ promptMode: config.promptMode,
128
+ };
129
+ }
130
+
131
+ const gp = agents.get("general-purpose");
132
+ if (gp && gp.enabled !== false) {
133
+ return {
134
+ displayName: gp.displayName ?? gp.name,
135
+ description: gp.description,
136
+ builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
137
+ extensions: gp.extensions,
138
+ excludeExtensions: gp.excludeExtensions,
139
+ skills: gp.skills,
140
+ promptMode: gp.promptMode,
141
+ };
142
+ }
143
+
144
+ return {
145
+ displayName: "Agent",
146
+ description: "General-purpose agent for complex, multi-step tasks",
147
+ builtinToolNames: BUILTIN_TOOL_NAMES,
148
+ extensions: true,
149
+ skills: true,
150
+ promptMode: "append",
151
+ };
152
+ }
package/src/context.ts ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * context.ts — Extract parent conversation context for subagent inheritance.
3
+ */
4
+
5
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
6
+
7
+ /** Extract text from a message content block array. */
8
+ export function extractText(content: unknown[]): string {
9
+ return content
10
+ .filter((c: any) => c.type === "text")
11
+ .map((c: any) => c.text ?? "")
12
+ .join("\n");
13
+ }
14
+
15
+ /**
16
+ * Build a text representation of the parent conversation context.
17
+ * Used when inherit_context is true to give the subagent visibility
18
+ * into what has been discussed/done so far.
19
+ */
20
+ export function buildParentContext(ctx: ExtensionContext): string {
21
+ const entries = ctx.sessionManager.getBranch();
22
+ if (!entries || entries.length === 0) return "";
23
+
24
+ const parts: string[] = [];
25
+
26
+ for (const entry of entries) {
27
+ if (entry.type === "message") {
28
+ const msg = entry.message;
29
+ if (msg.role === "user") {
30
+ const text =
31
+ typeof msg.content === "string"
32
+ ? msg.content
33
+ : extractText(msg.content);
34
+ if (text.trim()) parts.push(`[User]: ${text.trim()}`);
35
+ } else if (msg.role === "assistant") {
36
+ const text = extractText(msg.content);
37
+ if (text.trim()) parts.push(`[Assistant]: ${text.trim()}`);
38
+ }
39
+ // Skip toolResult messages — too verbose for context
40
+ } else if (entry.type === "compaction") {
41
+ // Include compaction summaries — they're already condensed
42
+ if (entry.summary) {
43
+ parts.push(`[Summary]: ${entry.summary}`);
44
+ }
45
+ }
46
+ }
47
+
48
+ if (parts.length === 0) return "";
49
+
50
+ return `# Parent Conversation Context
51
+ The following is the conversation history from the parent session that spawned you.
52
+ Use this context to understand what has been discussed and decided so far.
53
+
54
+ ${parts.join("\n\n")}
55
+
56
+ ---
57
+ # Your Task (below)
58
+ `;
59
+ }
@@ -0,0 +1,165 @@
1
+ /**
2
+ * custom-agents.ts — Load user-defined agents from project (.pi/agents/) and global ($PI_CODING_AGENT_DIR/agents/, default ~/.pi/agent/agents/) locations.
3
+ */
4
+
5
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
6
+ import { basename, join } from "node:path";
7
+ import { getAgentDir, parseFrontmatter } from "@earendil-works/pi-coding-agent";
8
+ import { BUILTIN_TOOL_NAMES } from "./agent-types.ts";
9
+ import type { AgentConfig, ThinkingLevel } from "./types.ts";
10
+
11
+ /**
12
+ * Scan for custom agent .md files from multiple locations.
13
+ * Discovery hierarchy (higher priority wins):
14
+ * 1. Project: <cwd>/.pi/agents/*.md
15
+ * 2. Global: $PI_CODING_AGENT_DIR/agents/*.md (default: ~/.pi/agent/agents/*.md)
16
+ *
17
+ * Project-level agents override global ones with the same name.
18
+ * Any name is allowed — names matching defaults (e.g. "Explore") override them.
19
+ */
20
+ export function loadCustomAgents(cwd: string): Map<string, AgentConfig> {
21
+ const globalDir = join(getAgentDir(), "agents");
22
+ const projectDir = join(cwd, ".pi", "agents");
23
+
24
+ const agents = new Map<string, AgentConfig>();
25
+ loadFromDir(globalDir, agents, "global"); // lower priority
26
+ loadFromDir(projectDir, agents, "project"); // higher priority (overwrites)
27
+ return agents;
28
+ }
29
+
30
+ /** Load agent configs from a directory into the map. */
31
+ function loadFromDir(
32
+ dir: string,
33
+ agents: Map<string, AgentConfig>,
34
+ source: "project" | "global",
35
+ ): void {
36
+ if (!existsSync(dir)) return;
37
+
38
+ let files: string[];
39
+ try {
40
+ files = readdirSync(dir).filter((f) => f.endsWith(".md"));
41
+ } catch {
42
+ return;
43
+ }
44
+
45
+ for (const file of files) {
46
+ const name = basename(file, ".md");
47
+
48
+ let content: string;
49
+ try {
50
+ content = readFileSync(join(dir, file), "utf-8");
51
+ } catch {
52
+ continue;
53
+ }
54
+
55
+ const { frontmatter: fm, body } =
56
+ parseFrontmatter<Record<string, unknown>>(content);
57
+
58
+ const { builtinToolNames, extSelectors } = parseToolsField(fm.tools);
59
+
60
+ agents.set(name, {
61
+ name,
62
+ displayName: str(fm.display_name),
63
+ description: str(fm.description) ?? name,
64
+ builtinToolNames,
65
+ extSelectors,
66
+ disallowedTools: csvListOptional(fm.disallowed_tools),
67
+ extensions: inheritField(fm.extensions ?? fm.inherit_extensions),
68
+ excludeExtensions: csvListOptional(fm.exclude_extensions),
69
+ skills: inheritField(fm.skills ?? fm.inherit_skills),
70
+ model: str(fm.model),
71
+ thinking: str(fm.thinking) as ThinkingLevel | undefined,
72
+ maxTurns: nonNegativeInt(fm.max_turns),
73
+ systemPrompt: body.trim(),
74
+ promptMode: fm.prompt_mode === "append" ? "append" : "replace",
75
+ inheritContext:
76
+ fm.inherit_context != null ? fm.inherit_context === true : undefined,
77
+ runInBackground:
78
+ fm.run_in_background != null
79
+ ? fm.run_in_background === true
80
+ : undefined,
81
+ isolated: fm.isolated != null ? fm.isolated === true : undefined,
82
+ enabled: fm.enabled !== false, // default true; explicitly false disables
83
+ source,
84
+ });
85
+ }
86
+ }
87
+
88
+ // ---- Field parsers ----
89
+ // All follow the same convention: omitted → default, "none"/empty → nothing, value → exact.
90
+
91
+ /** Extract a string or undefined. */
92
+ function str(val: unknown): string | undefined {
93
+ return typeof val === "string" ? val : undefined;
94
+ }
95
+
96
+ /** Extract a non-negative integer or undefined. 0 means unlimited for max_turns. */
97
+ function nonNegativeInt(val: unknown): number | undefined {
98
+ return typeof val === "number" && val >= 0 ? val : undefined;
99
+ }
100
+
101
+ /**
102
+ * Parse a raw CSV field value into items, or undefined if absent/empty/"none".
103
+ */
104
+ function parseCsvField(val: unknown): string[] | undefined {
105
+ if (val === undefined || val === null) return undefined;
106
+ const s = String(val).trim();
107
+ if (!s || s === "none") return undefined;
108
+ const items = s
109
+ .split(",")
110
+ .map((t) => t.trim())
111
+ .filter(Boolean);
112
+ return items.length > 0 ? items : undefined;
113
+ }
114
+
115
+ /**
116
+ * Parse a comma-separated list field with defaults.
117
+ * omitted → defaults; "none"/empty → []; csv → listed items.
118
+ */
119
+ function csvList(val: unknown, defaults: string[]): string[] {
120
+ if (val === undefined || val === null) return defaults;
121
+ return parseCsvField(val) ?? [];
122
+ }
123
+
124
+ /**
125
+ * Partition the `tools:` CSV into the built-in tool allowlist and raw `ext:` selectors.
126
+ * `*` (and the case-insensitive alias `all`, for `tools: all`) expands to all
127
+ * built-ins; plain entries are built-in names; `ext:` entries are extension-tool
128
+ * selectors parsed later by the runner. omitted → all built-ins, no selectors.
129
+ * `tools:` present with only `ext:` entries → zero built-ins (use `*`).
130
+ */
131
+ function parseToolsField(val: unknown): {
132
+ builtinToolNames: string[];
133
+ extSelectors: string[] | undefined;
134
+ } {
135
+ const entries = csvList(val, BUILTIN_TOOL_NAMES);
136
+ const isWildcard = (e: string) => e === "*" || e.toLowerCase() === "all";
137
+ const hasWildcard = entries.some(isWildcard);
138
+ const plain = entries.filter((e) => !isWildcard(e) && !e.startsWith("ext:"));
139
+ const extEntries = entries.filter((e) => e.startsWith("ext:"));
140
+ return {
141
+ builtinToolNames: hasWildcard
142
+ ? [...new Set([...BUILTIN_TOOL_NAMES, ...plain])]
143
+ : plain,
144
+ extSelectors: extEntries.length > 0 ? extEntries : undefined,
145
+ };
146
+ }
147
+
148
+ /**
149
+ * Parse an optional comma-separated list field.
150
+ * omitted → undefined; "none"/empty → undefined; csv → listed items.
151
+ */
152
+ function csvListOptional(val: unknown): string[] | undefined {
153
+ return parseCsvField(val);
154
+ }
155
+
156
+ /**
157
+ * Parse an inherit field (extensions, skills).
158
+ * omitted/true → true (inherit all); false/"none"/empty → false; csv → listed names.
159
+ */
160
+ function inheritField(val: unknown): true | string[] | false {
161
+ if (val === undefined || val === null || val === true) return true;
162
+ if (val === false || val === "none") return false;
163
+ const items = csvList(val, []);
164
+ return items.length > 0 ? items : false;
165
+ }
@@ -0,0 +1,126 @@
1
+ /**
2
+ * default-agents.ts — Embedded default agent configurations.
3
+ *
4
+ * These are always available but can be overridden by user .md files with the same name.
5
+ */
6
+
7
+ import type { AgentConfig } from "./types.ts";
8
+
9
+ const READ_ONLY_TOOLS = ["read", "bash", "grep", "find", "ls"];
10
+
11
+ export const DEFAULT_AGENTS: Map<string, AgentConfig> = new Map([
12
+ [
13
+ "general-purpose",
14
+ {
15
+ name: "general-purpose",
16
+ displayName: "Agent",
17
+ description:
18
+ "General-purpose agent for researching complex questions, searching for code, and executing multi-step tasks. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries use this agent to perform the search for you.",
19
+ // builtinToolNames omitted — means "all available tools" (resolved at lookup time)
20
+ // inheritContext / runInBackground / isolated omitted — strategy fields, callers decide per-call.
21
+ // Setting them to false would lock callsite intent (see resolveAgentInvocationConfig in invocation-config.ts).
22
+ extensions: true,
23
+ skills: true,
24
+ systemPrompt: "",
25
+ promptMode: "append",
26
+ isDefault: true,
27
+ },
28
+ ],
29
+ [
30
+ "Explore",
31
+ {
32
+ name: "Explore",
33
+ displayName: "Explore",
34
+ description:
35
+ 'Fast read-only search agent for locating code. Use it to find files by pattern (eg. "src/components/**/*.tsx"), grep for symbols or keywords (eg. "API endpoints"), or answer "where is X defined / which files reference Y." Do NOT use it for code review, design-doc auditing, cross-file consistency checks, or open-ended analysis — it reads excerpts rather than whole files and will miss content past its read window. When calling, specify search breadth: "quick" for a single targeted lookup, "medium" for moderate exploration, or "very thorough" to search across multiple locations and naming conventions.',
36
+ builtinToolNames: READ_ONLY_TOOLS,
37
+ extensions: true,
38
+ skills: true,
39
+ model: "anthropic/claude-haiku-4-5-20251001",
40
+ systemPrompt: `# CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS
41
+ You are a file search specialist. You excel at thoroughly navigating and exploring codebases.
42
+ Your role is EXCLUSIVELY to search and analyze existing code. You do NOT have access to file editing tools.
43
+
44
+ You are STRICTLY PROHIBITED from:
45
+ - Creating new files
46
+ - Modifying existing files
47
+ - Deleting files
48
+ - Moving or copying files
49
+ - Creating temporary files anywhere, including /tmp
50
+ - Using redirect operators (>, >>, |) or heredocs to write to files
51
+ - Running ANY commands that change system state
52
+
53
+ Use Bash ONLY for read-only operations: ls, git status, git log, git diff, find, cat, head, tail.
54
+
55
+ # Tool Usage
56
+ - Use the find tool for file pattern matching (NOT the bash find command)
57
+ - Use the grep tool for content search (NOT bash grep/rg command)
58
+ - Use the read tool for reading files (NOT bash cat/head/tail)
59
+ - Use Bash ONLY for read-only operations
60
+ - Make independent tool calls in parallel for efficiency
61
+ - Adapt search approach based on thoroughness level specified
62
+
63
+ # Output
64
+ - Use absolute file paths in all references
65
+ - Report findings as regular messages
66
+ - Do not use emojis
67
+ - Be thorough and precise`,
68
+ promptMode: "replace",
69
+ isDefault: true,
70
+ },
71
+ ],
72
+ [
73
+ "Plan",
74
+ {
75
+ name: "Plan",
76
+ displayName: "Plan",
77
+ description:
78
+ "Software architect agent for designing implementation plans. Use this when you need to plan the implementation strategy for a task. Returns step-by-step plans, identifies critical files, and considers architectural trade-offs.",
79
+ builtinToolNames: READ_ONLY_TOOLS,
80
+ extensions: true,
81
+ skills: true,
82
+ systemPrompt: `# CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS
83
+ You are a software architect and planning specialist.
84
+ Your role is EXCLUSIVELY to explore the codebase and design implementation plans.
85
+ You do NOT have access to file editing tools — attempting to edit files will fail.
86
+
87
+ You are STRICTLY PROHIBITED from:
88
+ - Creating new files
89
+ - Modifying existing files
90
+ - Deleting files
91
+ - Moving or copying files
92
+ - Creating temporary files anywhere, including /tmp
93
+ - Using redirect operators (>, >>, |) or heredocs to write to files
94
+ - Running ANY commands that change system state
95
+
96
+ # Planning Process
97
+ 1. Understand requirements
98
+ 2. Explore thoroughly (read files, find patterns, understand architecture)
99
+ 3. Design solution based on your assigned perspective
100
+ 4. Detail the plan with step-by-step implementation strategy
101
+
102
+ # Requirements
103
+ - Consider trade-offs and architectural decisions
104
+ - Identify dependencies and sequencing
105
+ - Anticipate potential challenges
106
+ - Follow existing patterns where appropriate
107
+
108
+ # Tool Usage
109
+ - Use the find tool for file pattern matching (NOT the bash find command)
110
+ - Use the grep tool for content search (NOT bash grep/rg command)
111
+ - Use the read tool for reading files (NOT bash cat/head/tail)
112
+ - Use Bash ONLY for read-only operations
113
+
114
+ # Output Format
115
+ - Use absolute file paths
116
+ - Do not use emojis
117
+ - End your response with:
118
+
119
+ ### Critical Files for Implementation
120
+ List 3-5 files most critical for implementing this plan:
121
+ - /absolute/path/to/file.ts - [Brief reason]`,
122
+ promptMode: "replace",
123
+ isDefault: true,
124
+ },
125
+ ],
126
+ ]);
package/src/env.ts ADDED
@@ -0,0 +1,43 @@
1
+ /**
2
+ * env.ts — Detect environment info (git, platform) for subagent system prompts.
3
+ */
4
+
5
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
6
+ import type { EnvInfo } from "./types.js";
7
+
8
+ export async function detectEnv(
9
+ pi: ExtensionAPI,
10
+ cwd: string,
11
+ ): Promise<EnvInfo> {
12
+ let isGitRepo = false;
13
+ let branch = "";
14
+
15
+ try {
16
+ const result = await pi.exec(
17
+ "git",
18
+ ["rev-parse", "--is-inside-work-tree"],
19
+ { cwd, timeout: 5000 },
20
+ );
21
+ isGitRepo = result.code === 0 && result.stdout.trim() === "true";
22
+ } catch {
23
+ // Not a git repo or git not installed
24
+ }
25
+
26
+ if (isGitRepo) {
27
+ try {
28
+ const result = await pi.exec("git", ["branch", "--show-current"], {
29
+ cwd,
30
+ timeout: 5000,
31
+ });
32
+ branch = result.code === 0 ? result.stdout.trim() : "unknown";
33
+ } catch {
34
+ branch = "unknown";
35
+ }
36
+ }
37
+
38
+ return {
39
+ isGitRepo,
40
+ branch,
41
+ platform: process.platform,
42
+ };
43
+ }
@@ -0,0 +1,9 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import registerPixSubagent from "./index.ts";
3
+ import { once } from "./once.ts";
4
+
5
+ export default function pixSubagentExtension(pi: ExtensionAPI): void {
6
+ once(pi, "pix-subagent", () => {
7
+ registerPixSubagent(pi);
8
+ });
9
+ }