@pi-agents/orchid 0.1.0-beta.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 +41 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/agents/AGENTS-MANIFEST.md +42 -0
- package/agents/brain.md +42 -0
- package/agents/context-builder.md +46 -0
- package/agents/delegate.md +12 -0
- package/agents/dev-1.md +42 -0
- package/agents/oracle.md +73 -0
- package/agents/planner.md +55 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/tester.md +45 -0
- package/agents/worker.md +55 -0
- package/extensions/ralph.ts +1 -0
- package/extensions/reviewer-extension.ts +125 -0
- package/extensions/task-orchestrator.ts +28 -0
- package/package.json +63 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-context-build.md +53 -0
- package/prompts/parallel-handoff-plan.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +41 -0
- package/skills/orchid/SKILL.md +214 -0
- package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
- package/skills/orchid/orchid-converge/SKILL.md +124 -0
- package/skills/orchid/orchid-decompose/SKILL.md +201 -0
- package/skills/orchid/orchid-doctor/SKILL.md +162 -0
- package/skills/orchid/orchid-investigate/SKILL.md +102 -0
- package/skills/orchid/orchid-launch/SKILL.md +147 -0
- package/skills/ralph/SKILL.md +73 -0
- package/skills/subagents/pi-subagents/SKILL.md +813 -0
- package/src/index.ts +7 -0
- package/src/orchestrator/abort.ts +534 -0
- package/src/orchestrator/agent-bridge-extension.ts +1020 -0
- package/src/orchestrator/agent-host.ts +954 -0
- package/src/orchestrator/cleanup.ts +776 -0
- package/src/orchestrator/config-loader.ts +1412 -0
- package/src/orchestrator/config-schema.ts +690 -0
- package/src/orchestrator/config.ts +81 -0
- package/src/orchestrator/context-window.ts +66 -0
- package/src/orchestrator/diagnostic-reports.ts +475 -0
- package/src/orchestrator/diagnostics.ts +394 -0
- package/src/orchestrator/discovery.ts +1833 -0
- package/src/orchestrator/engine-worker.ts +415 -0
- package/src/orchestrator/engine.ts +5940 -0
- package/src/orchestrator/execution.ts +3104 -0
- package/src/orchestrator/extension.ts +5934 -0
- package/src/orchestrator/formatting.ts +785 -0
- package/src/orchestrator/git.ts +88 -0
- package/src/orchestrator/index.ts +28 -0
- package/src/orchestrator/lane-runner.ts +1787 -0
- package/src/orchestrator/mailbox.ts +780 -0
- package/src/orchestrator/merge.ts +3414 -0
- package/src/orchestrator/messages.ts +1062 -0
- package/src/orchestrator/migrations.ts +278 -0
- package/src/orchestrator/naming.ts +117 -0
- package/src/orchestrator/path-resolver.ts +275 -0
- package/src/orchestrator/persistence.ts +2625 -0
- package/src/orchestrator/process-registry.ts +452 -0
- package/src/orchestrator/quality-gate.ts +1085 -0
- package/src/orchestrator/resume.ts +3488 -0
- package/src/orchestrator/sessions.ts +57 -0
- package/src/orchestrator/settings-loader.ts +136 -0
- package/src/orchestrator/settings-tui.ts +2208 -0
- package/src/orchestrator/sidecar-telemetry.ts +267 -0
- package/src/orchestrator/supervisor.ts +4548 -0
- package/src/orchestrator/task-executor-core.ts +675 -0
- package/src/orchestrator/tmux-compat.ts +37 -0
- package/src/orchestrator/tool-allowlist-constants.ts +37 -0
- package/src/orchestrator/types.ts +4465 -0
- package/src/orchestrator/verification.ts +547 -0
- package/src/orchestrator/waves.ts +1564 -0
- package/src/orchestrator/workspace.ts +707 -0
- package/src/orchestrator/worktree.ts +2725 -0
- package/src/ralph/index.ts +825 -0
- package/src/subagents/agents/agent-management.ts +648 -0
- package/src/subagents/agents/agent-scope.ts +6 -0
- package/src/subagents/agents/agent-selection.ts +23 -0
- package/src/subagents/agents/agent-serializer.ts +86 -0
- package/src/subagents/agents/agents.ts +832 -0
- package/src/subagents/agents/chain-serializer.ts +137 -0
- package/src/subagents/agents/frontmatter.ts +29 -0
- package/src/subagents/agents/identity.ts +30 -0
- package/src/subagents/agents/skills.ts +632 -0
- package/src/subagents/extension/config.ts +16 -0
- package/src/subagents/extension/control-notices.ts +92 -0
- package/src/subagents/extension/doctor.ts +199 -0
- package/src/subagents/extension/fanout-child.ts +170 -0
- package/src/subagents/extension/index.ts +573 -0
- package/src/subagents/extension/schemas.ts +168 -0
- package/src/subagents/intercom/intercom-bridge.ts +379 -0
- package/src/subagents/intercom/result-intercom.ts +377 -0
- package/src/subagents/runs/background/async-execution.ts +712 -0
- package/src/subagents/runs/background/async-job-tracker.ts +310 -0
- package/src/subagents/runs/background/async-resume.ts +345 -0
- package/src/subagents/runs/background/async-status.ts +325 -0
- package/src/subagents/runs/background/completion-dedupe.ts +63 -0
- package/src/subagents/runs/background/notify.ts +108 -0
- package/src/subagents/runs/background/parallel-groups.ts +45 -0
- package/src/subagents/runs/background/result-watcher.ts +307 -0
- package/src/subagents/runs/background/run-id-resolver.ts +83 -0
- package/src/subagents/runs/background/run-status.ts +269 -0
- package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
- package/src/subagents/runs/background/subagent-runner.ts +1808 -0
- package/src/subagents/runs/background/top-level-async.ts +13 -0
- package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
- package/src/subagents/runs/foreground/chain-execution.ts +938 -0
- package/src/subagents/runs/foreground/execution.ts +918 -0
- package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
- package/src/subagents/runs/shared/completion-guard.ts +147 -0
- package/src/subagents/runs/shared/long-running-guard.ts +175 -0
- package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/src/subagents/runs/shared/model-fallback.ts +103 -0
- package/src/subagents/runs/shared/nested-events.ts +819 -0
- package/src/subagents/runs/shared/nested-path.ts +52 -0
- package/src/subagents/runs/shared/nested-render.ts +115 -0
- package/src/subagents/runs/shared/parallel-utils.ts +109 -0
- package/src/subagents/runs/shared/pi-args.ts +220 -0
- package/src/subagents/runs/shared/pi-spawn.ts +115 -0
- package/src/subagents/runs/shared/run-history.ts +60 -0
- package/src/subagents/runs/shared/single-output.ts +164 -0
- package/src/subagents/runs/shared/subagent-control.ts +226 -0
- package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
- package/src/subagents/runs/shared/worktree.ts +577 -0
- package/src/subagents/shared/artifacts.ts +98 -0
- package/src/subagents/shared/atomic-json.ts +16 -0
- package/src/subagents/shared/file-coalescer.ts +40 -0
- package/src/subagents/shared/fork-context.ts +76 -0
- package/src/subagents/shared/formatters.ts +133 -0
- package/src/subagents/shared/jsonl-writer.ts +81 -0
- package/src/subagents/shared/model-info.ts +78 -0
- package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
- package/src/subagents/shared/session-identity.ts +10 -0
- package/src/subagents/shared/session-tokens.ts +44 -0
- package/src/subagents/shared/settings.ts +397 -0
- package/src/subagents/shared/status-format.ts +49 -0
- package/src/subagents/shared/types.ts +822 -0
- package/src/subagents/shared/utils.ts +450 -0
- package/src/subagents/slash/prompt-template-bridge.ts +397 -0
- package/src/subagents/slash/slash-bridge.ts +174 -0
- package/src/subagents/slash/slash-commands.ts +528 -0
- package/src/subagents/slash/slash-live-state.ts +292 -0
- package/src/subagents/tui/render-helpers.ts +80 -0
- package/src/subagents/tui/render.ts +1358 -0
- package/templates/agents/local/supervisor.md +33 -0
- package/templates/agents/local/task-merger.md +27 -0
- package/templates/agents/local/task-reviewer.md +30 -0
- package/templates/agents/local/task-worker.md +34 -0
- package/templates/agents/supervisor-routing.md +92 -0
- package/templates/agents/supervisor.md +229 -0
- package/templates/agents/task-merger.md +214 -0
- package/templates/agents/task-reviewer.md +260 -0
- package/templates/agents/task-worker-segment.md +44 -0
- package/templates/agents/task-worker.md +557 -0
- package/templates/tasks/CONTEXT.md +30 -0
- package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
- package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skill resolution and caching for subagent extension
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { execSync } from "node:child_process";
|
|
6
|
+
import * as fs from "node:fs";
|
|
7
|
+
import * as os from "node:os";
|
|
8
|
+
import * as path from "node:path";
|
|
9
|
+
import { getAgentDir } from "../shared/utils.ts";
|
|
10
|
+
|
|
11
|
+
export type SkillSource =
|
|
12
|
+
| "project"
|
|
13
|
+
| "user"
|
|
14
|
+
| "project-package"
|
|
15
|
+
| "user-package"
|
|
16
|
+
| "project-settings"
|
|
17
|
+
| "user-settings"
|
|
18
|
+
| "extension"
|
|
19
|
+
| "builtin"
|
|
20
|
+
| "unknown";
|
|
21
|
+
|
|
22
|
+
interface ResolvedSkill {
|
|
23
|
+
name: string;
|
|
24
|
+
path: string;
|
|
25
|
+
content: string;
|
|
26
|
+
source: SkillSource;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface SkillCacheEntry {
|
|
30
|
+
mtime: number;
|
|
31
|
+
skill: ResolvedSkill;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface CachedSkillEntry {
|
|
35
|
+
name: string;
|
|
36
|
+
filePath: string;
|
|
37
|
+
source: SkillSource;
|
|
38
|
+
description?: string;
|
|
39
|
+
order: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface SkillSearchPath {
|
|
43
|
+
path: string;
|
|
44
|
+
source: SkillSource;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const skillCache = new Map<string, SkillCacheEntry>();
|
|
48
|
+
const MAX_CACHE_SIZE = 50;
|
|
49
|
+
|
|
50
|
+
let loadSkillsCache: { cwd: string; agentDir: string; skills: CachedSkillEntry[]; timestamp: number } | null = null;
|
|
51
|
+
const LOAD_SKILLS_CACHE_TTL_MS = 5000;
|
|
52
|
+
|
|
53
|
+
const CONFIG_DIR = ".pi";
|
|
54
|
+
const SUBAGENT_ORCHESTRATION_SKILL = "pi-subagents";
|
|
55
|
+
|
|
56
|
+
const SOURCE_PRIORITY: Record<SkillSource, number> = {
|
|
57
|
+
project: 700,
|
|
58
|
+
"project-settings": 650,
|
|
59
|
+
"project-package": 600,
|
|
60
|
+
user: 300,
|
|
61
|
+
"user-settings": 250,
|
|
62
|
+
"user-package": 200,
|
|
63
|
+
extension: 150,
|
|
64
|
+
builtin: 100,
|
|
65
|
+
unknown: 0,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function stripSkillFrontmatter(content: string): string {
|
|
69
|
+
const normalized = content.replace(/\r\n/g, "\n");
|
|
70
|
+
if (!normalized.startsWith("---")) return normalized;
|
|
71
|
+
|
|
72
|
+
const endIndex = normalized.indexOf("\n---", 3);
|
|
73
|
+
if (endIndex === -1) return normalized;
|
|
74
|
+
|
|
75
|
+
return normalized.slice(endIndex + 4).trim();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isWithinPath(filePath: string, dir: string): boolean {
|
|
79
|
+
const relative = path.relative(dir, filePath);
|
|
80
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function readOptionalJsonFile(filePath: string, label: string): unknown {
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const code = typeof error === "object" && error !== null && "code" in error
|
|
88
|
+
? (error as { code?: unknown }).code
|
|
89
|
+
: undefined;
|
|
90
|
+
if (code === "ENOENT") return null;
|
|
91
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
92
|
+
throw new Error(`Failed to read ${label} '${filePath}': ${message}`, {
|
|
93
|
+
cause: error instanceof Error ? error : undefined,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function readJsonFileBestEffort(filePath: string): unknown {
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
101
|
+
} catch {
|
|
102
|
+
// Package scans over installed dependencies are opportunistic.
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function extractSkillPathsFromPackageRoot(packageRoot: string, source: SkillSource, bestEffort = false): SkillSearchPath[] {
|
|
108
|
+
const packageJsonPath = path.join(packageRoot, "package.json");
|
|
109
|
+
const pkg = bestEffort
|
|
110
|
+
? readJsonFileBestEffort(packageJsonPath)
|
|
111
|
+
: readOptionalJsonFile(packageJsonPath, "package manifest");
|
|
112
|
+
if (!pkg || typeof pkg !== "object" || Array.isArray(pkg)) return [];
|
|
113
|
+
const pi = (pkg as { pi?: unknown }).pi;
|
|
114
|
+
if (!pi || typeof pi !== "object" || Array.isArray(pi)) return [];
|
|
115
|
+
const skills = (pi as { skills?: unknown }).skills;
|
|
116
|
+
if (!Array.isArray(skills)) return [];
|
|
117
|
+
return skills
|
|
118
|
+
.filter((entry): entry is string => typeof entry === "string")
|
|
119
|
+
.map((entry) => ({ path: path.resolve(packageRoot, entry), source }));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let cachedGlobalNpmRoot: string | null = null;
|
|
123
|
+
|
|
124
|
+
function getGlobalNpmRoot(): string | null {
|
|
125
|
+
if (cachedGlobalNpmRoot !== null) return cachedGlobalNpmRoot;
|
|
126
|
+
try {
|
|
127
|
+
cachedGlobalNpmRoot = execSync("npm root -g", { encoding: "utf-8", timeout: 5000 }).trim();
|
|
128
|
+
return cachedGlobalNpmRoot;
|
|
129
|
+
} catch {
|
|
130
|
+
// Global npm root is optional in constrained environments.
|
|
131
|
+
cachedGlobalNpmRoot = ""; // Empty string means "tried but failed"
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function collectInstalledPackageSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
137
|
+
const dirs: SkillSearchPath[] = [
|
|
138
|
+
{ path: path.join(cwd, CONFIG_DIR, "npm", "node_modules"), source: "project-package" },
|
|
139
|
+
{ path: path.join(agentDir, "npm", "node_modules"), source: "user-package" },
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
const globalRoot = getGlobalNpmRoot();
|
|
143
|
+
if (globalRoot) {
|
|
144
|
+
dirs.push({ path: globalRoot, source: "user-package" });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const results: SkillSearchPath[] = [];
|
|
148
|
+
|
|
149
|
+
for (const dir of dirs) {
|
|
150
|
+
if (!fs.existsSync(dir.path)) continue;
|
|
151
|
+
let entries: fs.Dirent[];
|
|
152
|
+
try {
|
|
153
|
+
entries = fs.readdirSync(dir.path, { withFileTypes: true });
|
|
154
|
+
} catch {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
for (const entry of entries) {
|
|
159
|
+
if (entry.name.startsWith(".")) continue;
|
|
160
|
+
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
|
161
|
+
|
|
162
|
+
if (entry.name.startsWith("@")) {
|
|
163
|
+
const scopeDir = path.join(dir.path, entry.name);
|
|
164
|
+
let scopeEntries: fs.Dirent[];
|
|
165
|
+
try {
|
|
166
|
+
scopeEntries = fs.readdirSync(scopeDir, { withFileTypes: true });
|
|
167
|
+
} catch {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
for (const scopeEntry of scopeEntries) {
|
|
171
|
+
if (scopeEntry.name.startsWith(".")) continue;
|
|
172
|
+
if (!scopeEntry.isDirectory() && !scopeEntry.isSymbolicLink()) continue;
|
|
173
|
+
const pkgRoot = path.join(scopeDir, scopeEntry.name);
|
|
174
|
+
results.push(...extractSkillPathsFromPackageRoot(pkgRoot, dir.source, true));
|
|
175
|
+
}
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const pkgRoot = path.join(dir.path, entry.name);
|
|
180
|
+
results.push(...extractSkillPathsFromPackageRoot(pkgRoot, dir.source, true));
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return results;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function collectSettingsSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
188
|
+
const results: SkillSearchPath[] = [];
|
|
189
|
+
const settingsFiles = [
|
|
190
|
+
{ file: path.join(cwd, CONFIG_DIR, "settings.json"), base: path.join(cwd, CONFIG_DIR), source: "project-settings" as const },
|
|
191
|
+
{ file: path.join(agentDir, "settings.json"), base: agentDir, source: "user-settings" as const },
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
for (const { file, base, source } of settingsFiles) {
|
|
195
|
+
const settings = readOptionalJsonFile(file, "skills settings file");
|
|
196
|
+
if (!settings || typeof settings !== "object" || Array.isArray(settings)) continue;
|
|
197
|
+
const skills = (settings as { skills?: unknown }).skills;
|
|
198
|
+
if (!Array.isArray(skills)) continue;
|
|
199
|
+
for (const entry of skills) {
|
|
200
|
+
if (typeof entry !== "string") continue;
|
|
201
|
+
let resolved = entry;
|
|
202
|
+
if (resolved.startsWith("~/")) {
|
|
203
|
+
resolved = path.join(os.homedir(), resolved.slice(2));
|
|
204
|
+
} else if (!path.isAbsolute(resolved)) {
|
|
205
|
+
resolved = path.resolve(base, resolved);
|
|
206
|
+
}
|
|
207
|
+
results.push({ path: resolved, source });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return results;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function isSafePackagePath(value: string): boolean {
|
|
215
|
+
return value.length > 0
|
|
216
|
+
&& !path.isAbsolute(value)
|
|
217
|
+
&& value.split(/[\\/]/).every((part) => part.length > 0 && part !== "." && part !== "..");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function parseNpmPackageName(source: string): string | undefined {
|
|
221
|
+
const spec = source.slice(4).trim();
|
|
222
|
+
if (!spec) return undefined;
|
|
223
|
+
const match = spec.match(/^(@?[^@]+(?:\/[^@]+)?)(?:@(.+))?$/);
|
|
224
|
+
const packageName = match?.[1] ?? spec;
|
|
225
|
+
return isSafePackagePath(packageName) ? packageName : undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function stripGitRef(repoPath: string): string {
|
|
229
|
+
const atIndex = repoPath.indexOf("@");
|
|
230
|
+
const hashIndex = repoPath.indexOf("#");
|
|
231
|
+
const refIndex = [atIndex, hashIndex].filter((index) => index >= 0).sort((a, b) => a - b)[0];
|
|
232
|
+
return refIndex === undefined ? repoPath : repoPath.slice(0, refIndex);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function parseGitPackagePath(source: string): { host: string; repoPath: string } | undefined {
|
|
236
|
+
const spec = source.slice(4).trim();
|
|
237
|
+
if (!spec) return undefined;
|
|
238
|
+
|
|
239
|
+
let host = "";
|
|
240
|
+
let repoPath = "";
|
|
241
|
+
const scpLike = spec.match(/^git@([^:]+):(.+)$/);
|
|
242
|
+
if (scpLike) {
|
|
243
|
+
host = scpLike[1] ?? "";
|
|
244
|
+
repoPath = scpLike[2] ?? "";
|
|
245
|
+
} else if (/^[a-z][a-z0-9+.-]*:\/\//i.test(spec)) {
|
|
246
|
+
try {
|
|
247
|
+
const url = new URL(spec);
|
|
248
|
+
host = url.hostname;
|
|
249
|
+
repoPath = url.pathname.replace(/^\/+/, "");
|
|
250
|
+
} catch {
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
const slashIndex = spec.indexOf("/");
|
|
255
|
+
if (slashIndex < 0) return undefined;
|
|
256
|
+
host = spec.slice(0, slashIndex);
|
|
257
|
+
repoPath = spec.slice(slashIndex + 1);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const normalizedPath = stripGitRef(repoPath).replace(/\.git$/, "").replace(/^\/+/, "");
|
|
261
|
+
if (!host || !isSafePackagePath(host) || !isSafePackagePath(normalizedPath) || normalizedPath.split(/[\\/]/).length < 2) {
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
return { host, repoPath: normalizedPath };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function resolveSettingsPackageRoot(source: string, baseDir: string): string | undefined {
|
|
268
|
+
const trimmed = source.trim();
|
|
269
|
+
if (!trimmed) return undefined;
|
|
270
|
+
if (trimmed.startsWith("git:")) {
|
|
271
|
+
const parsed = parseGitPackagePath(trimmed);
|
|
272
|
+
return parsed ? path.join(baseDir, "git", parsed.host, parsed.repoPath) : undefined;
|
|
273
|
+
}
|
|
274
|
+
if (trimmed.startsWith("npm:")) {
|
|
275
|
+
const packageName = parseNpmPackageName(trimmed);
|
|
276
|
+
return packageName ? path.join(baseDir, "npm", "node_modules", packageName) : undefined;
|
|
277
|
+
}
|
|
278
|
+
const normalized = trimmed.startsWith("file:") ? trimmed.slice(5) : trimmed;
|
|
279
|
+
if (normalized === "~") return os.homedir();
|
|
280
|
+
if (normalized.startsWith("~/")) return path.join(os.homedir(), normalized.slice(2));
|
|
281
|
+
if (path.isAbsolute(normalized)) return normalized;
|
|
282
|
+
if (normalized === "." || normalized === ".." || normalized.startsWith("./") || normalized.startsWith("../")) {
|
|
283
|
+
return path.resolve(baseDir, normalized);
|
|
284
|
+
}
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function collectSettingsPackageSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
289
|
+
const settingsFiles = [
|
|
290
|
+
{ file: path.join(cwd, CONFIG_DIR, "settings.json"), base: path.join(cwd, CONFIG_DIR), source: "project-package" as const },
|
|
291
|
+
{ file: path.join(agentDir, "settings.json"), base: agentDir, source: "user-package" as const },
|
|
292
|
+
];
|
|
293
|
+
const results: SkillSearchPath[] = [];
|
|
294
|
+
|
|
295
|
+
for (const { file, base, source } of settingsFiles) {
|
|
296
|
+
const settings = readOptionalJsonFile(file, "skills settings file");
|
|
297
|
+
if (!settings || typeof settings !== "object" || Array.isArray(settings)) continue;
|
|
298
|
+
const packages = (settings as { packages?: unknown }).packages;
|
|
299
|
+
if (!Array.isArray(packages)) continue;
|
|
300
|
+
|
|
301
|
+
for (const entry of packages) {
|
|
302
|
+
const packageSource = typeof entry === "string"
|
|
303
|
+
? entry
|
|
304
|
+
: typeof entry === "object" && entry !== null && typeof (entry as { source?: unknown }).source === "string"
|
|
305
|
+
? (entry as { source: string }).source
|
|
306
|
+
: undefined;
|
|
307
|
+
if (!packageSource) continue;
|
|
308
|
+
|
|
309
|
+
const packageRoot = resolveSettingsPackageRoot(packageSource, base);
|
|
310
|
+
if (!packageRoot) continue;
|
|
311
|
+
results.push(...extractSkillPathsFromPackageRoot(packageRoot, source));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return results;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function buildSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
319
|
+
const skillPaths: SkillSearchPath[] = [
|
|
320
|
+
{ path: path.join(cwd, CONFIG_DIR, "skills"), source: "project" },
|
|
321
|
+
{ path: path.join(cwd, ".agents", "skills"), source: "project" },
|
|
322
|
+
{ path: path.join(agentDir, "skills"), source: "user" },
|
|
323
|
+
{ path: path.join(os.homedir(), ".agents", "skills"), source: "user" },
|
|
324
|
+
...collectInstalledPackageSkillPaths(cwd, agentDir),
|
|
325
|
+
...collectSettingsPackageSkillPaths(cwd, agentDir),
|
|
326
|
+
...extractSkillPathsFromPackageRoot(cwd, "project-package"),
|
|
327
|
+
...collectSettingsSkillPaths(cwd, agentDir),
|
|
328
|
+
];
|
|
329
|
+
|
|
330
|
+
const deduped = new Map<string, SkillSearchPath>();
|
|
331
|
+
for (const entry of skillPaths) {
|
|
332
|
+
const resolvedPath = path.resolve(entry.path);
|
|
333
|
+
if (!deduped.has(resolvedPath)) {
|
|
334
|
+
deduped.set(resolvedPath, { path: resolvedPath, source: entry.source });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return [...deduped.values()];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function inferSkillSource(filePath: string, cwd: string, agentDir: string, sourceHint?: SkillSource): SkillSource {
|
|
341
|
+
if (sourceHint) return sourceHint;
|
|
342
|
+
|
|
343
|
+
const projectConfigRoot = path.resolve(cwd, CONFIG_DIR);
|
|
344
|
+
const projectSkillsRoot = path.resolve(cwd, CONFIG_DIR, "skills");
|
|
345
|
+
const projectPackagesRoot = path.resolve(cwd, CONFIG_DIR, "npm", "node_modules");
|
|
346
|
+
const projectAgentsRoot = path.resolve(cwd, ".agents");
|
|
347
|
+
const userSkillsRoot = path.resolve(agentDir, "skills");
|
|
348
|
+
const userPackagesRoot = path.resolve(agentDir, "npm", "node_modules");
|
|
349
|
+
const userAgentRoot = path.resolve(agentDir);
|
|
350
|
+
const userAgentsRoot = path.resolve(os.homedir(), ".agents");
|
|
351
|
+
|
|
352
|
+
if (isWithinPath(filePath, projectPackagesRoot)) return "project-package";
|
|
353
|
+
if (isWithinPath(filePath, projectSkillsRoot) || isWithinPath(filePath, projectAgentsRoot)) return "project";
|
|
354
|
+
if (isWithinPath(filePath, projectConfigRoot)) return "project-settings";
|
|
355
|
+
|
|
356
|
+
if (isWithinPath(filePath, userPackagesRoot)) return "user-package";
|
|
357
|
+
if (isWithinPath(filePath, userSkillsRoot) || isWithinPath(filePath, userAgentsRoot)) return "user";
|
|
358
|
+
if (isWithinPath(filePath, userAgentRoot)) return "user-settings";
|
|
359
|
+
|
|
360
|
+
const globalRoot = getGlobalNpmRoot();
|
|
361
|
+
if (globalRoot && isWithinPath(filePath, globalRoot)) return "user-package";
|
|
362
|
+
|
|
363
|
+
return "unknown";
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function chooseHigherPrioritySkill(existing: CachedSkillEntry | undefined, candidate: CachedSkillEntry): CachedSkillEntry {
|
|
367
|
+
if (!existing) return candidate;
|
|
368
|
+
const existingPriority = SOURCE_PRIORITY[existing.source] ?? 0;
|
|
369
|
+
const candidatePriority = SOURCE_PRIORITY[candidate.source] ?? 0;
|
|
370
|
+
if (candidatePriority > existingPriority) return candidate;
|
|
371
|
+
if (candidatePriority < existingPriority) return existing;
|
|
372
|
+
return candidate.order < existing.order ? candidate : existing;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function maybeReadSkillDescription(filePath: string): string | undefined {
|
|
376
|
+
try {
|
|
377
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
378
|
+
const normalized = content.replace(/\r\n/g, "\n");
|
|
379
|
+
if (!normalized.startsWith("---")) return undefined;
|
|
380
|
+
|
|
381
|
+
const endIndex = normalized.indexOf("\n---", 3);
|
|
382
|
+
if (endIndex === -1) return undefined;
|
|
383
|
+
|
|
384
|
+
const frontmatter = normalized.slice(3, endIndex).trim();
|
|
385
|
+
const match = frontmatter.match(/^description:\s*(.+)$/m);
|
|
386
|
+
if (!match) return undefined;
|
|
387
|
+
return match[1]?.trim().replace(/^['\"]|['\"]$/g, "");
|
|
388
|
+
} catch {
|
|
389
|
+
// Description parsing is best-effort metadata extraction.
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function collectFilesystemSkills(cwd: string, agentDir: string, skillPaths: SkillSearchPath[]): CachedSkillEntry[] {
|
|
395
|
+
const entries: CachedSkillEntry[] = [];
|
|
396
|
+
const seen = new Set<string>();
|
|
397
|
+
let order = 0;
|
|
398
|
+
|
|
399
|
+
const pushEntry = (name: string, filePath: string, sourceHint?: SkillSource) => {
|
|
400
|
+
const resolvedFile = path.resolve(filePath);
|
|
401
|
+
if (seen.has(resolvedFile)) return;
|
|
402
|
+
if (!fs.existsSync(resolvedFile)) return;
|
|
403
|
+
seen.add(resolvedFile);
|
|
404
|
+
entries.push({
|
|
405
|
+
name,
|
|
406
|
+
filePath: resolvedFile,
|
|
407
|
+
source: inferSkillSource(resolvedFile, cwd, agentDir, sourceHint),
|
|
408
|
+
description: maybeReadSkillDescription(resolvedFile),
|
|
409
|
+
order: order++,
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
for (const skillPath of skillPaths) {
|
|
414
|
+
if (!fs.existsSync(skillPath.path)) continue;
|
|
415
|
+
|
|
416
|
+
let stat: fs.Stats;
|
|
417
|
+
try {
|
|
418
|
+
stat = fs.statSync(skillPath.path);
|
|
419
|
+
} catch {
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (stat.isFile()) {
|
|
424
|
+
const fileName = path.basename(skillPath.path);
|
|
425
|
+
if (!fileName.toLowerCase().endsWith(".md")) continue;
|
|
426
|
+
const skillName = fileName.toLowerCase() === "skill.md"
|
|
427
|
+
? path.basename(path.dirname(skillPath.path))
|
|
428
|
+
: path.basename(fileName, path.extname(fileName));
|
|
429
|
+
pushEntry(skillName, skillPath.path, skillPath.source);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (!stat.isDirectory()) continue;
|
|
434
|
+
|
|
435
|
+
const rootSkillFile = path.join(skillPath.path, "SKILL.md");
|
|
436
|
+
if (fs.existsSync(rootSkillFile)) {
|
|
437
|
+
pushEntry(path.basename(skillPath.path), rootSkillFile, skillPath.source);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
let childEntries: fs.Dirent[];
|
|
441
|
+
try {
|
|
442
|
+
childEntries = fs.readdirSync(skillPath.path, { withFileTypes: true });
|
|
443
|
+
} catch {
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
for (const child of childEntries) {
|
|
448
|
+
if (child.name.startsWith(".")) continue;
|
|
449
|
+
const childPath = path.join(skillPath.path, child.name);
|
|
450
|
+
if (child.isDirectory() || child.isSymbolicLink()) {
|
|
451
|
+
const nestedSkillPath = path.join(childPath, "SKILL.md");
|
|
452
|
+
if (fs.existsSync(nestedSkillPath)) {
|
|
453
|
+
pushEntry(child.name, nestedSkillPath, skillPath.source);
|
|
454
|
+
}
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (child.isFile() && child.name.toLowerCase().endsWith(".md")) {
|
|
458
|
+
pushEntry(path.basename(child.name, path.extname(child.name)), childPath, skillPath.source);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return entries;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function getCachedSkills(cwd: string): CachedSkillEntry[] {
|
|
467
|
+
const now = Date.now();
|
|
468
|
+
const agentDir = getAgentDir();
|
|
469
|
+
if (loadSkillsCache && loadSkillsCache.cwd === cwd && loadSkillsCache.agentDir === agentDir && now - loadSkillsCache.timestamp < LOAD_SKILLS_CACHE_TTL_MS) {
|
|
470
|
+
return loadSkillsCache.skills;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const skillPaths = buildSkillPaths(cwd, agentDir);
|
|
474
|
+
const loaded = collectFilesystemSkills(cwd, agentDir, skillPaths);
|
|
475
|
+
const dedupedByName = new Map<string, CachedSkillEntry>();
|
|
476
|
+
|
|
477
|
+
for (const entry of loaded) {
|
|
478
|
+
const current = dedupedByName.get(entry.name);
|
|
479
|
+
dedupedByName.set(entry.name, chooseHigherPrioritySkill(current, entry));
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const skills = [...dedupedByName.values()].sort((a, b) => a.order - b.order);
|
|
483
|
+
loadSkillsCache = { cwd, agentDir, skills, timestamp: now };
|
|
484
|
+
return skills;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export function resolveSkillPath(
|
|
488
|
+
skillName: string,
|
|
489
|
+
cwd: string,
|
|
490
|
+
): { path: string; source: SkillSource } | undefined {
|
|
491
|
+
const skills = getCachedSkills(cwd);
|
|
492
|
+
const skill = skills.find((s) => s.name === skillName);
|
|
493
|
+
if (!skill) return undefined;
|
|
494
|
+
return { path: skill.filePath, source: skill.source };
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function readSkill(
|
|
498
|
+
skillName: string,
|
|
499
|
+
skillPath: string,
|
|
500
|
+
source: SkillSource,
|
|
501
|
+
): ResolvedSkill | undefined {
|
|
502
|
+
try {
|
|
503
|
+
const stat = fs.statSync(skillPath);
|
|
504
|
+
const cached = skillCache.get(skillPath);
|
|
505
|
+
if (cached && cached.mtime === stat.mtimeMs) {
|
|
506
|
+
return cached.skill;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const raw = fs.readFileSync(skillPath, "utf-8");
|
|
510
|
+
const content = stripSkillFrontmatter(raw);
|
|
511
|
+
const skill: ResolvedSkill = {
|
|
512
|
+
name: skillName,
|
|
513
|
+
path: skillPath,
|
|
514
|
+
content,
|
|
515
|
+
source,
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
skillCache.set(skillPath, { mtime: stat.mtimeMs, skill });
|
|
519
|
+
if (skillCache.size > MAX_CACHE_SIZE) {
|
|
520
|
+
const firstKey = skillCache.keys().next().value;
|
|
521
|
+
if (firstKey) skillCache.delete(firstKey);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
return skill;
|
|
525
|
+
} catch {
|
|
526
|
+
// Treat unreadable skill files as unresolved so callers can surface as missing.
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export function resolveSkills(
|
|
532
|
+
skillNames: string[],
|
|
533
|
+
cwd: string,
|
|
534
|
+
): { resolved: ResolvedSkill[]; missing: string[] } {
|
|
535
|
+
const resolved: ResolvedSkill[] = [];
|
|
536
|
+
const missing: string[] = [];
|
|
537
|
+
|
|
538
|
+
for (const name of skillNames) {
|
|
539
|
+
const trimmed = name.trim();
|
|
540
|
+
if (!trimmed) continue;
|
|
541
|
+
if (trimmed === SUBAGENT_ORCHESTRATION_SKILL) {
|
|
542
|
+
missing.push(trimmed);
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const location = resolveSkillPath(trimmed, cwd);
|
|
547
|
+
if (!location) {
|
|
548
|
+
missing.push(trimmed);
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const skill = readSkill(trimmed, location.path, location.source);
|
|
553
|
+
if (skill) {
|
|
554
|
+
resolved.push(skill);
|
|
555
|
+
} else {
|
|
556
|
+
missing.push(trimmed);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return { resolved, missing };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
export function resolveSkillsWithFallback(
|
|
564
|
+
skillNames: string[],
|
|
565
|
+
primaryCwd: string,
|
|
566
|
+
fallbackCwd?: string,
|
|
567
|
+
): { resolved: ResolvedSkill[]; missing: string[] } {
|
|
568
|
+
const primary = resolveSkills(skillNames, primaryCwd);
|
|
569
|
+
if (!fallbackCwd || primary.missing.length === 0) return primary;
|
|
570
|
+
if (path.resolve(primaryCwd) === path.resolve(fallbackCwd)) return primary;
|
|
571
|
+
|
|
572
|
+
const fallback = resolveSkills(primary.missing, fallbackCwd);
|
|
573
|
+
return {
|
|
574
|
+
resolved: [...primary.resolved, ...fallback.resolved],
|
|
575
|
+
missing: fallback.missing,
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
export function buildSkillInjection(skills: ResolvedSkill[]): string {
|
|
580
|
+
if (skills.length === 0) return "";
|
|
581
|
+
|
|
582
|
+
return skills
|
|
583
|
+
.map((s) => `<skill name="${s.name}">\n${s.content}\n</skill>`)
|
|
584
|
+
.join("\n\n");
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
export function normalizeSkillInput(
|
|
588
|
+
input: string | string[] | boolean | undefined,
|
|
589
|
+
): string[] | false | undefined {
|
|
590
|
+
if (input === false) return false;
|
|
591
|
+
if (input === true || input === undefined) return undefined;
|
|
592
|
+
if (Array.isArray(input)) {
|
|
593
|
+
return [...new Set(input.map((s) => s.trim()).filter((s) => s.length > 0))];
|
|
594
|
+
}
|
|
595
|
+
// Guard against JSON-encoded arrays arriving as strings (e.g. '["a","b"]').
|
|
596
|
+
// Models sometimes serialise the skill parameter as a JSON string instead of
|
|
597
|
+
// a native array, and naively splitting on "," would embed brackets/quotes
|
|
598
|
+
// into the skill names, causing resolution to silently fail.
|
|
599
|
+
const trimmed = input.trim();
|
|
600
|
+
if (trimmed.startsWith("[")) {
|
|
601
|
+
try {
|
|
602
|
+
const parsed = JSON.parse(trimmed);
|
|
603
|
+
if (Array.isArray(parsed)) {
|
|
604
|
+
return normalizeSkillInput(parsed);
|
|
605
|
+
}
|
|
606
|
+
} catch {
|
|
607
|
+
// Not valid JSON – fall through to comma-split
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return [...new Set(input.split(",").map((s) => s.trim()).filter((s) => s.length > 0))];
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
export function discoverAvailableSkills(cwd: string): Array<{
|
|
614
|
+
name: string;
|
|
615
|
+
source: SkillSource;
|
|
616
|
+
description?: string;
|
|
617
|
+
}> {
|
|
618
|
+
const skills = getCachedSkills(cwd);
|
|
619
|
+
return skills
|
|
620
|
+
.filter((s) => s.name !== SUBAGENT_ORCHESTRATION_SKILL)
|
|
621
|
+
.map((s) => ({
|
|
622
|
+
name: s.name,
|
|
623
|
+
source: s.source,
|
|
624
|
+
description: s.description,
|
|
625
|
+
}))
|
|
626
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export function clearSkillCache(): void {
|
|
630
|
+
skillCache.clear();
|
|
631
|
+
loadSkillsCache = null;
|
|
632
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type { ExtensionConfig } from "../shared/types.ts";
|
|
4
|
+
import { getAgentDir } from "../shared/utils.ts";
|
|
5
|
+
|
|
6
|
+
export function loadConfig(): ExtensionConfig {
|
|
7
|
+
const configPath = path.join(getAgentDir(), "extensions", "subagent", "config.json");
|
|
8
|
+
try {
|
|
9
|
+
if (fs.existsSync(configPath)) {
|
|
10
|
+
return JSON.parse(fs.readFileSync(configPath, "utf-8")) as ExtensionConfig;
|
|
11
|
+
}
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.error(`Failed to load subagent config from '${configPath}':`, error);
|
|
14
|
+
}
|
|
15
|
+
return {};
|
|
16
|
+
}
|