bylua-lspec-subagents 1.0.2

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.
Files changed (86) hide show
  1. package/CHANGELOG.md +482 -0
  2. package/LICENSE +21 -0
  3. package/README.md +123 -0
  4. package/dist/agent-manager.d.ts +108 -0
  5. package/dist/agent-manager.js +391 -0
  6. package/dist/agent-runner.d.ts +95 -0
  7. package/dist/agent-runner.js +377 -0
  8. package/dist/agent-types.d.ts +58 -0
  9. package/dist/agent-types.js +157 -0
  10. package/dist/context.d.ts +12 -0
  11. package/dist/context.js +56 -0
  12. package/dist/cross-extension-rpc.d.ts +46 -0
  13. package/dist/cross-extension-rpc.js +76 -0
  14. package/dist/custom-agents.d.ts +14 -0
  15. package/dist/custom-agents.js +127 -0
  16. package/dist/default-agents.d.ts +12 -0
  17. package/dist/default-agents.js +489 -0
  18. package/dist/env.d.ts +6 -0
  19. package/dist/env.js +28 -0
  20. package/dist/group-join.d.ts +32 -0
  21. package/dist/group-join.js +116 -0
  22. package/dist/index.d.ts +13 -0
  23. package/dist/index.js +1863 -0
  24. package/dist/invocation-config.d.ts +22 -0
  25. package/dist/invocation-config.js +15 -0
  26. package/dist/memory.d.ts +49 -0
  27. package/dist/memory.js +151 -0
  28. package/dist/model-config-loader.d.ts +58 -0
  29. package/dist/model-config-loader.js +157 -0
  30. package/dist/model-resolver.d.ts +19 -0
  31. package/dist/model-resolver.js +62 -0
  32. package/dist/output-file.d.ts +24 -0
  33. package/dist/output-file.js +86 -0
  34. package/dist/prompts.d.ts +29 -0
  35. package/dist/prompts.js +65 -0
  36. package/dist/schedule-store.d.ts +38 -0
  37. package/dist/schedule-store.js +155 -0
  38. package/dist/schedule.d.ts +109 -0
  39. package/dist/schedule.js +338 -0
  40. package/dist/settings.d.ts +66 -0
  41. package/dist/settings.js +130 -0
  42. package/dist/skill-loader.d.ts +24 -0
  43. package/dist/skill-loader.js +93 -0
  44. package/dist/types.d.ts +164 -0
  45. package/dist/types.js +8 -0
  46. package/dist/ui/agent-widget.d.ts +134 -0
  47. package/dist/ui/agent-widget.js +451 -0
  48. package/dist/ui/conversation-viewer.d.ts +35 -0
  49. package/dist/ui/conversation-viewer.js +252 -0
  50. package/dist/ui/schedule-menu.d.ts +16 -0
  51. package/dist/ui/schedule-menu.js +95 -0
  52. package/dist/usage.d.ts +50 -0
  53. package/dist/usage.js +49 -0
  54. package/dist/worktree.d.ts +36 -0
  55. package/dist/worktree.js +139 -0
  56. package/install.sh +77 -0
  57. package/lspec-model-config.example.json +17 -0
  58. package/package.json +50 -0
  59. package/src/agent-manager.ts +483 -0
  60. package/src/agent-runner.ts +486 -0
  61. package/src/agent-types.ts +188 -0
  62. package/src/context.ts +58 -0
  63. package/src/cross-extension-rpc.ts +122 -0
  64. package/src/custom-agents.ts +136 -0
  65. package/src/default-agents.ts +501 -0
  66. package/src/env.ts +33 -0
  67. package/src/group-join.ts +141 -0
  68. package/src/index.ts +2032 -0
  69. package/src/invocation-config.ts +40 -0
  70. package/src/memory.ts +165 -0
  71. package/src/model-config-loader.ts +193 -0
  72. package/src/model-resolver.ts +81 -0
  73. package/src/output-file.ts +96 -0
  74. package/src/prompts.ts +91 -0
  75. package/src/schedule-store.ts +153 -0
  76. package/src/schedule.ts +365 -0
  77. package/src/settings.ts +186 -0
  78. package/src/skill-loader.ts +102 -0
  79. package/src/types.ts +179 -0
  80. package/src/ui/agent-widget.ts +533 -0
  81. package/src/ui/conversation-viewer.ts +261 -0
  82. package/src/ui/schedule-menu.ts +104 -0
  83. package/src/usage.ts +60 -0
  84. package/src/worktree.ts +162 -0
  85. package/uninstall.sh +55 -0
  86. package/update.sh +64 -0
@@ -0,0 +1,40 @@
1
+ import type { AgentConfig, IsolationMode, JoinMode, ThinkingLevel } from "./types.js";
2
+
3
+ interface AgentInvocationParams {
4
+ model?: string;
5
+ thinking?: string;
6
+ max_turns?: number;
7
+ run_in_background?: boolean;
8
+ inherit_context?: boolean;
9
+ isolated?: boolean;
10
+ isolation?: IsolationMode;
11
+ }
12
+
13
+ export function resolveAgentInvocationConfig(
14
+ agentConfig: AgentConfig | undefined,
15
+ params: AgentInvocationParams,
16
+ ): {
17
+ modelInput?: string;
18
+ modelFromParams: boolean;
19
+ thinking?: ThinkingLevel;
20
+ maxTurns?: number;
21
+ inheritContext: boolean;
22
+ runInBackground: boolean;
23
+ isolated: boolean;
24
+ isolation?: IsolationMode;
25
+ } {
26
+ return {
27
+ modelInput: agentConfig?.model ?? params.model,
28
+ modelFromParams: agentConfig?.model == null && params.model != null,
29
+ thinking: (agentConfig?.thinking ?? params.thinking) as ThinkingLevel | undefined,
30
+ maxTurns: agentConfig?.maxTurns ?? params.max_turns,
31
+ inheritContext: agentConfig?.inheritContext ?? params.inherit_context ?? false,
32
+ runInBackground: agentConfig?.runInBackground ?? params.run_in_background ?? false,
33
+ isolated: agentConfig?.isolated ?? params.isolated ?? false,
34
+ isolation: agentConfig?.isolation ?? params.isolation,
35
+ };
36
+ }
37
+
38
+ export function resolveJoinMode(defaultJoinMode: JoinMode, runInBackground: boolean): JoinMode | undefined {
39
+ return runInBackground ? defaultJoinMode : undefined;
40
+ }
package/src/memory.ts ADDED
@@ -0,0 +1,165 @@
1
+ /**
2
+ * memory.ts — Persistent agent memory: per-agent memory directories that persist across sessions.
3
+ *
4
+ * Memory scopes:
5
+ * - "user" → ~/.pi/agent-memory/{agent-name}/
6
+ * - "project" → .pi/agent-memory/{agent-name}/
7
+ * - "local" → .pi/agent-memory-local/{agent-name}/
8
+ */
9
+
10
+ import { existsSync, lstatSync, mkdirSync, readFileSync } from "node:fs";
11
+ import { homedir } from "node:os";
12
+ import { join, } from "node:path";
13
+ import type { MemoryScope } from "./types.js";
14
+
15
+ /** Maximum lines to read from MEMORY.md */
16
+ const MAX_MEMORY_LINES = 200;
17
+
18
+ /**
19
+ * Returns true if a name contains characters not allowed in agent/skill names.
20
+ * Uses a whitelist: only alphanumeric, hyphens, underscores, and dots (no leading dot).
21
+ */
22
+ export function isUnsafeName(name: string): boolean {
23
+ if (!name || name.length > 128) return true;
24
+ return !/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(name);
25
+ }
26
+
27
+ /**
28
+ * Returns true if the given path is a symlink (defense against symlink attacks).
29
+ */
30
+ export function isSymlink(filePath: string): boolean {
31
+ try {
32
+ return lstatSync(filePath).isSymbolicLink();
33
+ } catch {
34
+ return false;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Safely read a file, rejecting symlinks.
40
+ * Returns undefined if the file doesn't exist, is a symlink, or can't be read.
41
+ */
42
+ export function safeReadFile(filePath: string): string | undefined {
43
+ if (!existsSync(filePath)) return undefined;
44
+ if (isSymlink(filePath)) return undefined;
45
+ try {
46
+ return readFileSync(filePath, "utf-8");
47
+ } catch {
48
+ return undefined;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Resolve the memory directory path for a given agent + scope + cwd.
54
+ * Throws if agentName contains path traversal characters.
55
+ */
56
+ export function resolveMemoryDir(agentName: string, scope: MemoryScope, cwd: string): string {
57
+ if (isUnsafeName(agentName)) {
58
+ throw new Error(`Unsafe agent name for memory directory: "${agentName}"`);
59
+ }
60
+ switch (scope) {
61
+ case "user":
62
+ return join(homedir(), ".pi", "agent-memory", agentName);
63
+ case "project":
64
+ return join(cwd, ".pi", "agent-memory", agentName);
65
+ case "local":
66
+ return join(cwd, ".pi", "agent-memory-local", agentName);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Ensure the memory directory exists, creating it if needed.
72
+ * Refuses to create directories if any component in the path is a symlink
73
+ * to prevent symlink-based directory traversal attacks.
74
+ */
75
+ export function ensureMemoryDir(memoryDir: string): void {
76
+ // If the directory already exists, verify it's not a symlink
77
+ if (existsSync(memoryDir)) {
78
+ if (isSymlink(memoryDir)) {
79
+ throw new Error(`Refusing to use symlinked memory directory: ${memoryDir}`);
80
+ }
81
+ return;
82
+ }
83
+ mkdirSync(memoryDir, { recursive: true });
84
+ }
85
+
86
+ /**
87
+ * Read the first N lines of MEMORY.md from the memory directory, if it exists.
88
+ * Returns undefined if no MEMORY.md exists or if the path is a symlink.
89
+ */
90
+ export function readMemoryIndex(memoryDir: string): string | undefined {
91
+ // Reject symlinked memory directories
92
+ if (isSymlink(memoryDir)) return undefined;
93
+
94
+ const memoryFile = join(memoryDir, "MEMORY.md");
95
+ const content = safeReadFile(memoryFile);
96
+ if (content === undefined) return undefined;
97
+
98
+ const lines = content.split("\n");
99
+ if (lines.length > MAX_MEMORY_LINES) {
100
+ return lines.slice(0, MAX_MEMORY_LINES).join("\n") + "\n... (truncated at 200 lines)";
101
+ }
102
+ return content;
103
+ }
104
+
105
+ /**
106
+ * Build the memory block to inject into the agent's system prompt.
107
+ * Also ensures the memory directory exists (creates it if needed).
108
+ */
109
+ export function buildMemoryBlock(agentName: string, scope: MemoryScope, cwd: string): string {
110
+ const memoryDir = resolveMemoryDir(agentName, scope, cwd);
111
+ // Create the memory directory so the agent can immediately write to it
112
+ ensureMemoryDir(memoryDir);
113
+
114
+ const existingMemory = readMemoryIndex(memoryDir);
115
+
116
+ const header = `# Agent Memory
117
+
118
+ You have a persistent memory directory at: ${memoryDir}/
119
+ Memory scope: ${scope}
120
+
121
+ This memory persists across sessions. Use it to build up knowledge over time.`;
122
+
123
+ const memoryContent = existingMemory
124
+ ? `\n\n## Current MEMORY.md\n${existingMemory}`
125
+ : `\n\nNo MEMORY.md exists yet. Create one at ${join(memoryDir, "MEMORY.md")} to start building persistent memory.`;
126
+
127
+ const instructions = `
128
+
129
+ ## Memory Instructions
130
+ - MEMORY.md is an index file — keep it concise (under 200 lines). Lines after 200 are truncated.
131
+ - Store detailed memories in separate files within ${memoryDir}/ and link to them from MEMORY.md.
132
+ - Each memory file should use this frontmatter format:
133
+ \`\`\`markdown
134
+ ---
135
+ name: <memory name>
136
+ description: <one-line description>
137
+ type: <user|feedback|project|reference>
138
+ ---
139
+ <memory content>
140
+ \`\`\`
141
+ - Update or remove memories that become outdated. Check for existing memories before creating duplicates.
142
+ - You have Read, Write, and Edit tools available for managing memory files.`;
143
+
144
+ return header + memoryContent + instructions;
145
+ }
146
+
147
+ /**
148
+ * Build a read-only memory block for agents that lack write/edit tools.
149
+ * Does NOT create the memory directory — agents can only consume existing memory.
150
+ */
151
+ export function buildReadOnlyMemoryBlock(agentName: string, scope: MemoryScope, cwd: string): string {
152
+ const memoryDir = resolveMemoryDir(agentName, scope, cwd);
153
+ const existingMemory = readMemoryIndex(memoryDir);
154
+
155
+ const header = `# Agent Memory (read-only)
156
+
157
+ Memory scope: ${scope}
158
+ You have read-only access to memory. You can reference existing memories but cannot create or modify them.`;
159
+
160
+ const memoryContent = existingMemory
161
+ ? `\n\n## Current MEMORY.md\n${existingMemory}`
162
+ : `\n\nNo memory is available yet. Other agents or sessions with write access can create memories for you to consume.`;
163
+
164
+ return header + memoryContent;
165
+ }
@@ -0,0 +1,193 @@
1
+ /**
2
+ * model-config-loader.ts — Load centralized model configuration for L-Spec subagents.
3
+ *
4
+ * Inspired by oh-my-opencode-slim's model config approach.
5
+ *
6
+ * Resolution order (highest priority wins):
7
+ * 1. Project: <cwd>/.pi/lspec-model-config.json
8
+ * 2. Global: ~/.pi/agent/lspec-model-config.json
9
+ * 3. Embedded: hardcoded defaults in this file
10
+ */
11
+
12
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
13
+ import { join } from "node:path";
14
+ import { getAgentDir } from "@mariozechner/pi-coding-agent";
15
+
16
+ export interface ModelConfig {
17
+ /** Map of agent name → model string (e.g. "claude-sonnet-4" or "provider/modelId") */
18
+ agents: Record<string, string>;
19
+ }
20
+
21
+ /** Embedded defaults — used when no config file exists. Using generic models as reference. */
22
+ const EMBEDDED_DEFAULTS: ModelConfig = {
23
+ agents: {
24
+ orchestrator: "claude-sonnet-4",
25
+ explorer: "gpt-4o-mini",
26
+ librarian: "claude-sonnet-4",
27
+ oracle: "claude-opus-4",
28
+ designer: "claude-sonnet-4",
29
+ fixer: "claude-sonnet-4",
30
+ observer: "gpt-4o-mini",
31
+ council: "claude-opus-4",
32
+ councillor: "gpt-4o-mini",
33
+ },
34
+ };
35
+
36
+ /** The placeholder marker prefix/suffix used in default-agents.ts */
37
+ export const MODEL_PLACEHOLDER_PREFIX = "{{model:";
38
+ export const MODEL_PLACEHOLDER_SUFFIX = "}}";
39
+
40
+ /**
41
+ * Check if a model string is a placeholder like "{{model:explorer}}"
42
+ */
43
+ export function isModelPlaceholder(model: string): boolean {
44
+ return model.startsWith(MODEL_PLACEHOLDER_PREFIX) && model.endsWith(MODEL_PLACEHOLDER_SUFFIX);
45
+ }
46
+
47
+ /**
48
+ * Extract agent name from a placeholder like "{{model:explorer}}" → "explorer"
49
+ */
50
+ export function parseModelPlaceholder(model: string): string | null {
51
+ if (!isModelPlaceholder(model)) return null;
52
+ return model.slice(MODEL_PLACEHOLDER_PREFIX.length, -MODEL_PLACEHOLDER_SUFFIX.length);
53
+ }
54
+
55
+ /**
56
+ * Resolve a model placeholder to the actual model string from config.
57
+ * Returns the resolved model, or undefined if the placeholder can't be resolved.
58
+ */
59
+ export function resolveModelPlaceholder(
60
+ model: string | undefined,
61
+ config: ModelConfig
62
+ ): string | undefined {
63
+ if (!model) return undefined;
64
+ if (!isModelPlaceholder(model)) return model;
65
+
66
+ const agentName = parseModelPlaceholder(model);
67
+ if (!agentName) return undefined;
68
+
69
+ return config.agents[agentName];
70
+ }
71
+
72
+ /**
73
+ * Resolve all model placeholders in a map of agent configs.
74
+ * Modifies the map in place.
75
+ */
76
+ export function resolveAllPlaceholders(
77
+ agents: Map<string, { model?: string }>,
78
+ config: ModelConfig
79
+ ): void {
80
+ for (const [, agent] of agents) {
81
+ if (agent.model && isModelPlaceholder(agent.model)) {
82
+ const resolved = resolveModelPlaceholder(agent.model, config);
83
+ if (resolved) {
84
+ agent.model = resolved;
85
+ }
86
+ }
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Load model config from disk, merging global + project overrides.
92
+ * Returns the merged config.
93
+ */
94
+ export function loadModelConfig(cwd: string): ModelConfig {
95
+ const config = structuredClone(EMBEDDED_DEFAULTS) as ModelConfig;
96
+ const globalDir = join(getAgentDir());
97
+ const projectDir = join(cwd, ".pi");
98
+
99
+ // Load global config (lower priority)
100
+ const globalPath = join(globalDir, "lspec-model-config.json");
101
+ loadAndMerge(globalPath, config);
102
+
103
+ // Load project config (higher priority — overwrites)
104
+ const projectPath = join(projectDir, "lspec-model-config.json");
105
+ loadAndMerge(projectPath, config);
106
+
107
+ return config;
108
+ }
109
+
110
+ /**
111
+ * Load a JSON config file and merge its agents into the config object.
112
+ */
113
+ function loadAndMerge(path: string, config: ModelConfig): void {
114
+ if (!existsSync(path)) return;
115
+
116
+ try {
117
+ const raw = readFileSync(path, "utf-8");
118
+ const parsed = JSON.parse(raw) as Partial<ModelConfig>;
119
+
120
+ // Skip _note, _docs keys — they're metadata
121
+ if (parsed.agents && typeof parsed.agents === "object") {
122
+ for (const [key, value] of Object.entries(parsed.agents)) {
123
+ if (typeof value === "string") {
124
+ config.agents[key] = value;
125
+ }
126
+ }
127
+ }
128
+ } catch {
129
+ // Silently ignore malformed config files
130
+ }
131
+ }
132
+
133
+ /** Re-export for convenience — returns a fully resolved model for an agent. */
134
+ export function getModelForAgent(
135
+ agentName: string,
136
+ cwd: string
137
+ ): string | undefined {
138
+ const config = loadModelConfig(cwd);
139
+ return config.agents[agentName];
140
+ }
141
+
142
+ /**
143
+ * Get the global model config file path.
144
+ */
145
+ export function getGlobalModelConfigPath(): string {
146
+ return join(getAgentDir(), "lspec-model-config.json");
147
+ }
148
+
149
+ /**
150
+ * Get the project model config file path.
151
+ */
152
+ export function getProjectModelConfigPath(cwd: string): string {
153
+ return join(cwd, ".pi", "lspec-model-config.json");
154
+ }
155
+
156
+ /**
157
+ * Save a model assignment for an agent to the config file.
158
+ * Updates global config by default, or project config if specified.
159
+ * Preserves existing entries and metadata keys (_note, _docs, etc).
160
+ */
161
+ export function saveModelForAgent(
162
+ agentName: string,
163
+ model: string,
164
+ target: "global" | "project" = "global",
165
+ cwd?: string,
166
+ ): void {
167
+ const configPath = target === "project"
168
+ ? getProjectModelConfigPath(cwd ?? process.cwd())
169
+ : getGlobalModelConfigPath();
170
+
171
+ // Read existing config or create new
172
+ let existing: Record<string, unknown> = {};
173
+ if (existsSync(configPath)) {
174
+ try {
175
+ const raw = readFileSync(configPath, "utf-8");
176
+ existing = JSON.parse(raw);
177
+ } catch {
178
+ existing = {};
179
+ }
180
+ }
181
+
182
+ // Update the agents section
183
+ if (!existing.agents || typeof existing.agents !== "object") {
184
+ existing.agents = {};
185
+ }
186
+ (existing.agents as Record<string, string>)[agentName] = model;
187
+
188
+ // Ensure directory exists
189
+ mkdirSync(join(configPath, ".."), { recursive: true });
190
+
191
+ // Write back
192
+ writeFileSync(configPath, JSON.stringify(existing, null, 2) + "\n", "utf-8");
193
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Model resolution: exact match ("provider/modelId") with fuzzy fallback.
3
+ */
4
+
5
+ export interface ModelEntry {
6
+ id: string;
7
+ name: string;
8
+ provider: string;
9
+ }
10
+
11
+ export interface ModelRegistry {
12
+ find(provider: string, modelId: string): any;
13
+ getAll(): any[];
14
+ getAvailable?(): any[];
15
+ }
16
+
17
+ /**
18
+ * Resolve a model string to a Model instance.
19
+ * Tries exact match first ("provider/modelId"), then fuzzy match against all available models.
20
+ * Returns the Model on success, or an error message string on failure.
21
+ */
22
+ export function resolveModel(
23
+ input: string,
24
+ registry: ModelRegistry,
25
+ ): any | string {
26
+ // Available models (those with auth configured)
27
+ const all = (registry.getAvailable?.() ?? registry.getAll()) as ModelEntry[];
28
+ const availableSet = new Set(all.map(m => `${m.provider}/${m.id}`.toLowerCase()));
29
+
30
+ // 1. Exact match: "provider/modelId" — only if available (has auth)
31
+ const slashIdx = input.indexOf("/");
32
+ if (slashIdx !== -1) {
33
+ const provider = input.slice(0, slashIdx);
34
+ const modelId = input.slice(slashIdx + 1);
35
+ if (availableSet.has(input.toLowerCase())) {
36
+ const found = registry.find(provider, modelId);
37
+ if (found) return found;
38
+ }
39
+ }
40
+
41
+ // 2. Fuzzy match against available models
42
+ const query = input.toLowerCase();
43
+
44
+ // Score each model: prefer exact id match > id contains > name contains > provider+id contains
45
+ let bestMatch: ModelEntry | undefined;
46
+ let bestScore = 0;
47
+
48
+ for (const m of all) {
49
+ const id = m.id.toLowerCase();
50
+ const name = m.name.toLowerCase();
51
+ const full = `${m.provider}/${m.id}`.toLowerCase();
52
+
53
+ let score = 0;
54
+ if (id === query || full === query) {
55
+ score = 100; // exact
56
+ } else if (id.includes(query) || full.includes(query)) {
57
+ score = 60 + (query.length / id.length) * 30; // substring, prefer tighter matches
58
+ } else if (name.includes(query)) {
59
+ score = 40 + (query.length / name.length) * 20;
60
+ } else if (query.split(/[\s\-/]+/).every(part => id.includes(part) || name.includes(part) || m.provider.toLowerCase().includes(part))) {
61
+ score = 20; // all parts present somewhere
62
+ }
63
+
64
+ if (score > bestScore) {
65
+ bestScore = score;
66
+ bestMatch = m;
67
+ }
68
+ }
69
+
70
+ if (bestMatch && bestScore >= 20) {
71
+ const found = registry.find(bestMatch.provider, bestMatch.id);
72
+ if (found) return found;
73
+ }
74
+
75
+ // 3. No match — list available models
76
+ const modelList = all
77
+ .map(m => ` ${m.provider}/${m.id}`)
78
+ .sort()
79
+ .join("\n");
80
+ return `Model not found: "${input}".\n\nAvailable models:\n${modelList}`;
81
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * output-file.ts — Streaming JSONL output file for agent transcripts.
3
+ *
4
+ * Creates a per-agent output file that streams conversation turns as JSONL,
5
+ * matching Claude Code's task output file format.
6
+ */
7
+
8
+ import { appendFileSync, chmodSync, mkdirSync, writeFileSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import type { AgentSession, AgentSessionEvent } from "@mariozechner/pi-coding-agent";
12
+
13
+ /**
14
+ * Encode a cwd path as a filesystem-safe directory name. Handles:
15
+ * - POSIX: "/home/user/project" → "home-user-project"
16
+ * - Windows: "C:\Users\foo\project" → "Users-foo-project"
17
+ * - UNC: "\\\\server\\share\\project" → "server-share-project"
18
+ */
19
+ export function encodeCwd(cwd: string): string {
20
+ return cwd
21
+ .replace(/[/\\]/g, "-") // both separators → dash
22
+ .replace(/^[A-Za-z]:-/, "") // strip Windows drive prefix ("C:-")
23
+ .replace(/^-+/, ""); // strip leading dashes (POSIX root, UNC)
24
+ }
25
+
26
+ /** Create the output file path, ensuring the directory exists.
27
+ * Mirrors Claude Code's layout: /tmp/{prefix}-{uid}/{encoded-cwd}/{sessionId}/tasks/{agentId}.output */
28
+ export function createOutputFilePath(cwd: string, agentId: string, sessionId: string): string {
29
+ const encoded = encodeCwd(cwd);
30
+ const root = join(tmpdir(), `pi-subagents-${process.getuid?.() ?? 0}`);
31
+ mkdirSync(root, { recursive: true, mode: 0o700 });
32
+ // chmod is a no-op on Windows and throws on some Windows filesystems.
33
+ // On Unix we still want to enforce 0o700 past umask, so only swallow on Windows.
34
+ try {
35
+ chmodSync(root, 0o700);
36
+ } catch (err) {
37
+ if (process.platform !== "win32") throw err;
38
+ }
39
+ const dir = join(root, encoded, sessionId, "tasks");
40
+ mkdirSync(dir, { recursive: true });
41
+ return join(dir, `${agentId}.output`);
42
+ }
43
+
44
+ /** Write the initial user prompt entry. */
45
+ export function writeInitialEntry(path: string, agentId: string, prompt: string, cwd: string): void {
46
+ const entry = {
47
+ isSidechain: true,
48
+ agentId,
49
+ type: "user",
50
+ message: { role: "user", content: prompt },
51
+ timestamp: new Date().toISOString(),
52
+ cwd,
53
+ };
54
+ writeFileSync(path, JSON.stringify(entry) + "\n", "utf-8");
55
+ }
56
+
57
+ /**
58
+ * Subscribe to session events and flush new messages to the output file on each turn_end.
59
+ * Returns a cleanup function that does a final flush and unsubscribes.
60
+ */
61
+ export function streamToOutputFile(
62
+ session: AgentSession,
63
+ path: string,
64
+ agentId: string,
65
+ cwd: string,
66
+ ): () => void {
67
+ let writtenCount = 1; // initial user prompt already written
68
+
69
+ const flush = () => {
70
+ const messages = session.messages;
71
+ while (writtenCount < messages.length) {
72
+ const msg = messages[writtenCount];
73
+ const entry = {
74
+ isSidechain: true,
75
+ agentId,
76
+ type: msg.role === "assistant" ? "assistant" : msg.role === "user" ? "user" : "toolResult",
77
+ message: msg,
78
+ timestamp: new Date().toISOString(),
79
+ cwd,
80
+ };
81
+ try {
82
+ appendFileSync(path, JSON.stringify(entry) + "\n", "utf-8");
83
+ } catch { /* ignore write errors */ }
84
+ writtenCount++;
85
+ }
86
+ };
87
+
88
+ const unsubscribe = session.subscribe((event: AgentSessionEvent) => {
89
+ if (event.type === "turn_end") flush();
90
+ });
91
+
92
+ return () => {
93
+ flush();
94
+ unsubscribe();
95
+ };
96
+ }
package/src/prompts.ts ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * prompts.ts — System prompt builder for agents.
3
+ */
4
+
5
+ import type { AgentConfig, EnvInfo } from "./types.js";
6
+
7
+ /** Extra sections to inject into the system prompt (memory, skills, etc.). */
8
+ export interface PromptExtras {
9
+ /** Persistent memory content to inject (first 200 lines of MEMORY.md + instructions). */
10
+ memoryBlock?: string;
11
+ /** Preloaded skill contents to inject. */
12
+ skillBlocks?: { name: string; content: string }[];
13
+ }
14
+
15
+ /**
16
+ * Build the system prompt for an agent from its config.
17
+ *
18
+ * - "replace" mode: env header + config.systemPrompt (full control, no parent identity)
19
+ * - "append" mode: env header + parent system prompt + sub-agent context + config.systemPrompt
20
+ * - "append" with empty systemPrompt: pure parent clone
21
+ *
22
+ * Both modes prepend an `<active_agent name="${config.name}"/>` tag so downstream
23
+ * extensions (e.g. permission/policy systems) can resolve per-agent policy
24
+ * inside the child session by parsing the system prompt.
25
+ *
26
+ * @param parentSystemPrompt The parent agent's effective system prompt (for append mode).
27
+ * @param extras Optional extra sections to inject (memory, preloaded skills).
28
+ */
29
+ export function buildAgentPrompt(
30
+ config: AgentConfig,
31
+ cwd: string,
32
+ env: EnvInfo,
33
+ parentSystemPrompt?: string,
34
+ extras?: PromptExtras,
35
+ ): string {
36
+ const activeAgentTag = `<active_agent name="${config.name}"/>\n\n`;
37
+
38
+ const envBlock = `# Environment
39
+ Working directory: ${cwd}
40
+ ${env.isGitRepo ? `Git repository: yes\nBranch: ${env.branch}` : "Not a git repository"}
41
+ Platform: ${env.platform}`;
42
+
43
+ // Build optional extras suffix
44
+ const extraSections: string[] = [];
45
+ if (extras?.memoryBlock) {
46
+ extraSections.push(extras.memoryBlock);
47
+ }
48
+ if (extras?.skillBlocks?.length) {
49
+ for (const skill of extras.skillBlocks) {
50
+ extraSections.push(`\n# Preloaded Skill: ${skill.name}\n${skill.content}`);
51
+ }
52
+ }
53
+ const extrasSuffix = extraSections.length > 0 ? "\n\n" + extraSections.join("\n") : "";
54
+
55
+ if (config.promptMode === "append") {
56
+ const identity = parentSystemPrompt || genericBase;
57
+
58
+ const bridge = `<sub_agent_context>
59
+ You are operating as a sub-agent invoked to handle a specific task.
60
+ - Use the read tool instead of cat/head/tail
61
+ - Use the edit tool instead of sed/awk
62
+ - Use the write tool instead of echo/heredoc
63
+ - Use the find tool instead of bash find/ls for file search
64
+ - Use the grep tool instead of bash grep/rg for content search
65
+ - Make independent tool calls in parallel
66
+ - Use absolute file paths
67
+ - Do not use emojis
68
+ - Be concise but complete
69
+ </sub_agent_context>`;
70
+
71
+ const customSection = config.systemPrompt?.trim()
72
+ ? `\n\n<agent_instructions>\n${config.systemPrompt}\n</agent_instructions>`
73
+ : "";
74
+
75
+ return activeAgentTag + envBlock + "\n\n<inherited_system_prompt>\n" + identity + "\n</inherited_system_prompt>\n\n" + bridge + customSection + extrasSuffix;
76
+ }
77
+
78
+ // "replace" mode — env header + the config's full system prompt
79
+ const replaceHeader = `You are a pi coding agent sub-agent.
80
+ You have been invoked to handle a specific task autonomously.
81
+
82
+ ${envBlock}`;
83
+
84
+ return activeAgentTag + replaceHeader + "\n\n" + config.systemPrompt + extrasSuffix;
85
+ }
86
+
87
+ /** Fallback base prompt when parent system prompt is unavailable in append mode. */
88
+ const genericBase = `# Role
89
+ You are a coding agent for complex, multi-step tasks.
90
+ You have full access to read, write, edit files, and execute commands.
91
+ Do what has been asked; nothing more, nothing less.`;