@duckmind/dm-darwin-x64 0.43.4 → 0.43.8
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/dm +0 -0
- package/extensions/.dm-extensions.json +241 -49
- package/extensions/dm-9router-ext/package.json +2 -19
- package/extensions/dm-9router-ext/src/index.js +5 -0
- package/extensions/dm-ask-user/index.js +10 -0
- package/extensions/dm-ask-user/package.json +1 -21
- package/extensions/dm-cli-anything/package.json +0 -6
- package/extensions/dm-context/package.json +2 -12
- package/extensions/dm-context/src/context.js +1 -0
- package/extensions/dm-context/src/index.js +7 -0
- package/extensions/dm-context/src/utils.js +1 -0
- package/extensions/dm-cua/package.json +0 -7
- package/extensions/dm-fff/package.json +1 -28
- package/extensions/dm-fff/src/index.js +15 -0
- package/extensions/dm-fff/src/query.js +1 -0
- package/extensions/dm-goal/package.json +2 -31
- package/extensions/dm-goal/src/goal.js +43 -0
- package/extensions/dm-grill-me/index.js +174 -0
- package/extensions/dm-grill-me/package.json +1 -26
- package/extensions/dm-image2/package.json +0 -7
- package/extensions/dm-subagents/package.json +1 -32
- package/extensions/dm-subagents/src/agents/agent-management.js +37 -0
- package/extensions/dm-subagents/src/agents/agent-memory.js +6 -0
- package/extensions/dm-subagents/src/agents/agent-scope.js +1 -0
- package/extensions/dm-subagents/src/agents/agent-selection.js +1 -0
- package/extensions/dm-subagents/src/agents/agent-serializer.js +7 -0
- package/extensions/dm-subagents/src/agents/agents.js +11 -0
- package/extensions/dm-subagents/src/agents/chain-serializer.js +12 -0
- package/extensions/dm-subagents/src/agents/frontmatter.js +6 -0
- package/extensions/dm-subagents/src/agents/identity.js +1 -0
- package/extensions/dm-subagents/src/agents/proactive-skills.js +1 -0
- package/extensions/dm-subagents/src/agents/skills.js +6 -0
- package/extensions/dm-subagents/src/extension/config.js +2 -0
- package/extensions/dm-subagents/src/extension/control-notices.js +4 -0
- package/extensions/dm-subagents/src/extension/doctor.js +16 -0
- package/extensions/dm-subagents/src/extension/fanout-child.js +231 -0
- package/extensions/dm-subagents/src/extension/index.js +367 -0
- package/extensions/dm-subagents/src/extension/rpc.js +8 -0
- package/extensions/dm-subagents/src/extension/schemas.js +1 -0
- package/extensions/dm-subagents/src/extension/{tool-description.ts → tool-description.js} +6 -126
- package/extensions/dm-subagents/src/intercom/intercom-bridge.js +13 -0
- package/extensions/dm-subagents/src/intercom/native-supervisor-channel.js +5 -0
- package/extensions/dm-subagents/src/intercom/result-intercom.js +3 -0
- package/extensions/dm-subagents/src/profiles/profiles.js +3 -0
- package/extensions/dm-subagents/src/runs/background/async-execution.js +46 -0
- package/extensions/dm-subagents/src/runs/background/async-job-tracker.js +14 -0
- package/extensions/dm-subagents/src/runs/background/async-resume.js +8 -0
- package/extensions/dm-subagents/src/runs/background/async-status.js +12 -0
- package/extensions/dm-subagents/src/runs/background/chain-append.js +2 -0
- package/extensions/dm-subagents/src/runs/background/chain-root-attachment.js +1 -0
- package/extensions/dm-subagents/src/runs/background/completion-batcher.js +1 -0
- package/extensions/dm-subagents/src/runs/background/completion-dedupe.js +1 -0
- package/extensions/dm-subagents/src/runs/background/control-channel.js +1 -0
- package/extensions/dm-subagents/src/runs/background/fleet-view.js +17 -0
- package/extensions/dm-subagents/src/runs/background/notify.js +3 -0
- package/extensions/dm-subagents/src/runs/background/parallel-groups.js +1 -0
- package/extensions/dm-subagents/src/runs/background/result-watcher.js +8 -0
- package/extensions/dm-subagents/src/runs/background/run-id-resolver.js +4 -0
- package/extensions/dm-subagents/src/runs/background/run-status.js +23 -0
- package/extensions/dm-subagents/src/runs/background/scheduled-runs.js +4 -0
- package/extensions/dm-subagents/src/runs/background/stale-run-reconciler.js +11 -0
- package/extensions/dm-subagents/src/runs/background/subagent-runner.js +78 -0
- package/extensions/dm-subagents/src/runs/background/top-level-async.js +1 -0
- package/extensions/dm-subagents/src/runs/background/wait.js +11 -0
- package/extensions/dm-subagents/src/runs/foreground/chain-clarify.js +12 -0
- package/extensions/dm-subagents/src/runs/foreground/chain-execution.js +102 -0
- package/extensions/dm-subagents/src/runs/foreground/execution.js +51 -0
- package/extensions/dm-subagents/src/runs/foreground/subagent-executor.js +228 -0
- package/extensions/dm-subagents/src/runs/shared/acceptance.js +3 -0
- package/extensions/dm-subagents/src/runs/shared/chain-outputs.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/completion-guard.js +3 -0
- package/extensions/dm-subagents/src/runs/shared/dm-args.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/dm-spawn.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/dynamic-fanout.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/long-running-guard.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/mcp-direct-tool-allowlist.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/model-fallback.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/model-scope.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/nested-events.js +8 -0
- package/extensions/dm-subagents/src/runs/shared/nested-path.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/nested-render.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/parallel-utils.js +5 -0
- package/extensions/dm-subagents/src/runs/shared/run-history.js +5 -0
- package/extensions/dm-subagents/src/runs/shared/single-output.js +14 -0
- package/extensions/dm-subagents/src/runs/shared/structured-output.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/subagent-control.js +5 -0
- package/extensions/dm-subagents/src/runs/shared/subagent-prompt-runtime.js +22 -0
- package/extensions/dm-subagents/src/runs/shared/tool-budget.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/turn-budget.js +7 -0
- package/extensions/dm-subagents/src/runs/shared/workflow-graph.js +1 -0
- package/extensions/dm-subagents/src/runs/shared/worktree.js +3 -0
- package/extensions/dm-subagents/src/shared/artifacts.js +2 -0
- package/extensions/dm-subagents/src/shared/atomic-json.js +1 -0
- package/extensions/dm-subagents/src/shared/child-transcript.js +5 -0
- package/extensions/dm-subagents/src/shared/file-coalescer.js +1 -0
- package/extensions/dm-subagents/src/shared/fork-context.js +6 -0
- package/extensions/dm-subagents/src/shared/formatters.js +9 -0
- package/extensions/dm-subagents/src/shared/jsonl-writer.js +2 -0
- package/extensions/dm-subagents/src/shared/model-info.js +1 -0
- package/extensions/dm-subagents/src/shared/post-exit-stdio-guard.js +1 -0
- package/extensions/dm-subagents/src/shared/session-identity.js +1 -0
- package/extensions/dm-subagents/src/shared/session-tokens.js +2 -0
- package/extensions/dm-subagents/src/shared/settings.js +23 -0
- package/extensions/dm-subagents/src/shared/status-format.js +1 -0
- package/extensions/dm-subagents/src/shared/types.js +8 -0
- package/extensions/dm-subagents/src/shared/utils.js +2 -0
- package/extensions/dm-subagents/src/slash/prompt-template-bridge.js +1 -0
- package/extensions/dm-subagents/src/slash/prompt-workflows.js +7 -0
- package/extensions/dm-subagents/src/slash/slash-bridge.js +1 -0
- package/extensions/dm-subagents/src/slash/slash-commands.js +38 -0
- package/extensions/dm-subagents/src/slash/slash-live-state.js +6 -0
- package/extensions/dm-subagents/src/tui/render-helpers.js +1 -0
- package/extensions/dm-subagents/src/tui/render.js +4 -0
- package/extensions/dm-tasks/package.json +1 -30
- package/extensions/dm-tasks/src/auto-clear.js +1 -0
- package/extensions/dm-tasks/src/index.js +212 -0
- package/extensions/dm-tasks/src/process-tracker.js +1 -0
- package/extensions/dm-tasks/src/task-store.js +1 -0
- package/extensions/dm-tasks/src/tasks-config.js +1 -0
- package/extensions/dm-tasks/src/types.js +0 -0
- package/extensions/dm-tasks/src/ui/settings-menu.js +1 -0
- package/extensions/dm-tasks/src/ui/task-widget.js +1 -0
- package/extensions/dm-usage/index.js +9 -0
- package/extensions/dm-usage/package.json +1 -18
- package/extensions/greedysearch-dm/index.js +20 -0
- package/extensions/greedysearch-dm/package.json +1 -22
- package/extensions/greedysearch-dm/src/formatters/results.js +8 -0
- package/extensions/greedysearch-dm/src/formatters/sources.js +1 -0
- package/extensions/greedysearch-dm/src/formatters/synthesis.js +1 -0
- package/extensions/greedysearch-dm/src/tools/greedy-search-handler.js +20 -0
- package/extensions/greedysearch-dm/src/tools/shared.js +9 -0
- package/extensions/greedysearch-dm/src/types.js +0 -0
- package/extensions/greedysearch-dm/src/utils/helpers.js +1 -0
- package/package.json +1 -1
- package/extensions/dm-9router-ext/src/index.ts +0 -541
- package/extensions/dm-ask-user/index.ts +0 -857
- package/extensions/dm-context/src/context.ts +0 -158
- package/extensions/dm-context/src/index.ts +0 -439
- package/extensions/dm-context/src/utils.ts +0 -6
- package/extensions/dm-fff/src/index.ts +0 -1023
- package/extensions/dm-fff/src/query.ts +0 -87
- package/extensions/dm-goal/src/goal.ts +0 -1073
- package/extensions/dm-grill-me/index.ts +0 -1085
- package/extensions/dm-subagents/src/agents/agent-management.ts +0 -1052
- package/extensions/dm-subagents/src/agents/agent-memory.ts +0 -254
- package/extensions/dm-subagents/src/agents/agent-scope.ts +0 -6
- package/extensions/dm-subagents/src/agents/agent-selection.ts +0 -25
- package/extensions/dm-subagents/src/agents/agent-serializer.ts +0 -121
- package/extensions/dm-subagents/src/agents/agents.ts +0 -1553
- package/extensions/dm-subagents/src/agents/chain-serializer.ts +0 -277
- package/extensions/dm-subagents/src/agents/frontmatter.ts +0 -93
- package/extensions/dm-subagents/src/agents/identity.ts +0 -30
- package/extensions/dm-subagents/src/agents/proactive-skills.ts +0 -191
- package/extensions/dm-subagents/src/agents/skills.ts +0 -729
- package/extensions/dm-subagents/src/extension/config.ts +0 -39
- package/extensions/dm-subagents/src/extension/control-notices.ts +0 -92
- package/extensions/dm-subagents/src/extension/doctor.ts +0 -214
- package/extensions/dm-subagents/src/extension/fanout-child.ts +0 -172
- package/extensions/dm-subagents/src/extension/index.ts +0 -656
- package/extensions/dm-subagents/src/extension/rpc.ts +0 -369
- package/extensions/dm-subagents/src/extension/schemas.ts +0 -309
- package/extensions/dm-subagents/src/intercom/intercom-bridge.ts +0 -180
- package/extensions/dm-subagents/src/intercom/native-supervisor-channel.ts +0 -519
- package/extensions/dm-subagents/src/intercom/result-intercom.ts +0 -377
- package/extensions/dm-subagents/src/profiles/profiles.ts +0 -637
- package/extensions/dm-subagents/src/runs/background/async-execution.ts +0 -1065
- package/extensions/dm-subagents/src/runs/background/async-job-tracker.ts +0 -441
- package/extensions/dm-subagents/src/runs/background/async-resume.ts +0 -391
- package/extensions/dm-subagents/src/runs/background/async-status.ts +0 -395
- package/extensions/dm-subagents/src/runs/background/chain-append.ts +0 -282
- package/extensions/dm-subagents/src/runs/background/chain-root-attachment.ts +0 -191
- package/extensions/dm-subagents/src/runs/background/completion-batcher.ts +0 -166
- package/extensions/dm-subagents/src/runs/background/completion-dedupe.ts +0 -63
- package/extensions/dm-subagents/src/runs/background/control-channel.ts +0 -332
- package/extensions/dm-subagents/src/runs/background/fleet-view.ts +0 -515
- package/extensions/dm-subagents/src/runs/background/notify.ts +0 -225
- package/extensions/dm-subagents/src/runs/background/parallel-groups.ts +0 -45
- package/extensions/dm-subagents/src/runs/background/result-watcher.ts +0 -315
- package/extensions/dm-subagents/src/runs/background/run-id-resolver.ts +0 -84
- package/extensions/dm-subagents/src/runs/background/run-status.ts +0 -434
- package/extensions/dm-subagents/src/runs/background/scheduled-runs.ts +0 -514
- package/extensions/dm-subagents/src/runs/background/stale-run-reconciler.ts +0 -368
- package/extensions/dm-subagents/src/runs/background/subagent-runner.ts +0 -3171
- package/extensions/dm-subagents/src/runs/background/top-level-async.ts +0 -13
- package/extensions/dm-subagents/src/runs/background/wait.ts +0 -353
- package/extensions/dm-subagents/src/runs/foreground/chain-clarify.ts +0 -1333
- package/extensions/dm-subagents/src/runs/foreground/chain-execution.ts +0 -1313
- package/extensions/dm-subagents/src/runs/foreground/execution.ts +0 -1239
- package/extensions/dm-subagents/src/runs/foreground/subagent-executor.ts +0 -3613
- package/extensions/dm-subagents/src/runs/shared/acceptance.ts +0 -879
- package/extensions/dm-subagents/src/runs/shared/chain-outputs.ts +0 -116
- package/extensions/dm-subagents/src/runs/shared/completion-guard.ts +0 -147
- package/extensions/dm-subagents/src/runs/shared/dm-args.ts +0 -271
- package/extensions/dm-subagents/src/runs/shared/dm-spawn.ts +0 -147
- package/extensions/dm-subagents/src/runs/shared/dynamic-fanout.ts +0 -295
- package/extensions/dm-subagents/src/runs/shared/long-running-guard.ts +0 -175
- package/extensions/dm-subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +0 -365
- package/extensions/dm-subagents/src/runs/shared/model-fallback.ts +0 -292
- package/extensions/dm-subagents/src/runs/shared/model-scope.ts +0 -128
- package/extensions/dm-subagents/src/runs/shared/nested-events.ts +0 -908
- package/extensions/dm-subagents/src/runs/shared/nested-path.ts +0 -52
- package/extensions/dm-subagents/src/runs/shared/nested-render.ts +0 -115
- package/extensions/dm-subagents/src/runs/shared/parallel-utils.ts +0 -198
- package/extensions/dm-subagents/src/runs/shared/run-history.ts +0 -60
- package/extensions/dm-subagents/src/runs/shared/single-output.ts +0 -180
- package/extensions/dm-subagents/src/runs/shared/structured-output.ts +0 -77
- package/extensions/dm-subagents/src/runs/shared/subagent-control.ts +0 -223
- package/extensions/dm-subagents/src/runs/shared/subagent-prompt-runtime.ts +0 -342
- package/extensions/dm-subagents/src/runs/shared/tool-budget.ts +0 -74
- package/extensions/dm-subagents/src/runs/shared/turn-budget.ts +0 -52
- package/extensions/dm-subagents/src/runs/shared/workflow-graph.ts +0 -206
- package/extensions/dm-subagents/src/runs/shared/worktree.ts +0 -600
- package/extensions/dm-subagents/src/shared/artifacts.ts +0 -113
- package/extensions/dm-subagents/src/shared/atomic-json.ts +0 -86
- package/extensions/dm-subagents/src/shared/child-transcript.ts +0 -212
- package/extensions/dm-subagents/src/shared/file-coalescer.ts +0 -40
- package/extensions/dm-subagents/src/shared/fork-context.ts +0 -194
- package/extensions/dm-subagents/src/shared/formatters.ts +0 -133
- package/extensions/dm-subagents/src/shared/jsonl-writer.ts +0 -81
- package/extensions/dm-subagents/src/shared/model-info.ts +0 -78
- package/extensions/dm-subagents/src/shared/post-exit-stdio-guard.ts +0 -85
- package/extensions/dm-subagents/src/shared/session-identity.ts +0 -10
- package/extensions/dm-subagents/src/shared/session-tokens.ts +0 -44
- package/extensions/dm-subagents/src/shared/settings.ts +0 -450
- package/extensions/dm-subagents/src/shared/status-format.ts +0 -49
- package/extensions/dm-subagents/src/shared/types.ts +0 -1257
- package/extensions/dm-subagents/src/shared/utils.ts +0 -554
- package/extensions/dm-subagents/src/slash/prompt-template-bridge.ts +0 -420
- package/extensions/dm-subagents/src/slash/prompt-workflows.ts +0 -330
- package/extensions/dm-subagents/src/slash/slash-bridge.ts +0 -176
- package/extensions/dm-subagents/src/slash/slash-commands.ts +0 -1296
- package/extensions/dm-subagents/src/slash/slash-live-state.ts +0 -292
- package/extensions/dm-subagents/src/tui/render-helpers.ts +0 -80
- package/extensions/dm-subagents/src/tui/render.ts +0 -1748
- package/extensions/dm-tasks/src/auto-clear.ts +0 -91
- package/extensions/dm-tasks/src/index.ts +0 -1145
- package/extensions/dm-tasks/src/process-tracker.ts +0 -140
- package/extensions/dm-tasks/src/task-store.ts +0 -305
- package/extensions/dm-tasks/src/tasks-config.ts +0 -23
- package/extensions/dm-tasks/src/types.ts +0 -40
- package/extensions/dm-tasks/src/ui/settings-menu.ts +0 -100
- package/extensions/dm-tasks/src/ui/task-widget.ts +0 -265
- package/extensions/dm-usage/index.ts +0 -1718
- package/extensions/greedysearch-dm/index.ts +0 -177
- package/extensions/greedysearch-dm/src/formatters/results.ts +0 -163
- package/extensions/greedysearch-dm/src/formatters/sources.ts +0 -116
- package/extensions/greedysearch-dm/src/formatters/synthesis.ts +0 -87
- package/extensions/greedysearch-dm/src/tools/greedy-search-handler.ts +0 -370
- package/extensions/greedysearch-dm/src/tools/shared.ts +0 -187
- package/extensions/greedysearch-dm/src/types.ts +0 -110
- package/extensions/greedysearch-dm/src/utils/helpers.ts +0 -40
|
@@ -1,729 +0,0 @@
|
|
|
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, getProjectConfigDir } 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
|
-
description?: string;
|
|
27
|
-
source: SkillSource;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface SkillCacheEntry {
|
|
31
|
-
mtime: number;
|
|
32
|
-
skill: ResolvedSkill;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface CachedSkillEntry {
|
|
36
|
-
name: string;
|
|
37
|
-
filePath: string;
|
|
38
|
-
source: SkillSource;
|
|
39
|
-
description?: string;
|
|
40
|
-
order: number;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
interface SkillSearchPath {
|
|
44
|
-
path: string;
|
|
45
|
-
source: SkillSource;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const skillCache = new Map<string, SkillCacheEntry>();
|
|
49
|
-
const MAX_CACHE_SIZE = 50;
|
|
50
|
-
|
|
51
|
-
let loadSkillsCache: { cwd: string; agentDir: string; skills: CachedSkillEntry[]; timestamp: number } | null = null;
|
|
52
|
-
const LOAD_SKILLS_CACHE_TTL_MS = 5000;
|
|
53
|
-
|
|
54
|
-
const SUBAGENT_ORCHESTRATION_SKILL = "dm-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 projectConfigDir = getProjectConfigDir(cwd);
|
|
138
|
-
const dirs: SkillSearchPath[] = [
|
|
139
|
-
{ path: path.join(projectConfigDir, "npm", "node_modules"), source: "project-package" },
|
|
140
|
-
{ path: path.join(agentDir, "npm", "node_modules"), source: "user-package" },
|
|
141
|
-
];
|
|
142
|
-
|
|
143
|
-
const globalRoot = getGlobalNpmRoot();
|
|
144
|
-
if (globalRoot) {
|
|
145
|
-
dirs.push({ path: globalRoot, source: "user-package" });
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const results: SkillSearchPath[] = [];
|
|
149
|
-
|
|
150
|
-
for (const dir of dirs) {
|
|
151
|
-
if (!fs.existsSync(dir.path)) continue;
|
|
152
|
-
let entries: fs.Dirent[];
|
|
153
|
-
try {
|
|
154
|
-
entries = fs.readdirSync(dir.path, { withFileTypes: true });
|
|
155
|
-
} catch {
|
|
156
|
-
continue;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
for (const entry of entries) {
|
|
160
|
-
if (entry.name.startsWith(".")) continue;
|
|
161
|
-
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
|
162
|
-
|
|
163
|
-
if (entry.name.startsWith("@")) {
|
|
164
|
-
const scopeDir = path.join(dir.path, entry.name);
|
|
165
|
-
let scopeEntries: fs.Dirent[];
|
|
166
|
-
try {
|
|
167
|
-
scopeEntries = fs.readdirSync(scopeDir, { withFileTypes: true });
|
|
168
|
-
} catch {
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
for (const scopeEntry of scopeEntries) {
|
|
172
|
-
if (scopeEntry.name.startsWith(".")) continue;
|
|
173
|
-
if (!scopeEntry.isDirectory() && !scopeEntry.isSymbolicLink()) continue;
|
|
174
|
-
const pkgRoot = path.join(scopeDir, scopeEntry.name);
|
|
175
|
-
results.push(...extractSkillPathsFromPackageRoot(pkgRoot, dir.source, true));
|
|
176
|
-
}
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const pkgRoot = path.join(dir.path, entry.name);
|
|
181
|
-
results.push(...extractSkillPathsFromPackageRoot(pkgRoot, dir.source, true));
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return results;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function collectSettingsSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
189
|
-
const results: SkillSearchPath[] = [];
|
|
190
|
-
const projectConfigDir = getProjectConfigDir(cwd);
|
|
191
|
-
const settingsFiles = [
|
|
192
|
-
{ file: path.join(projectConfigDir, "settings.json"), base: projectConfigDir, source: "project-settings" as const },
|
|
193
|
-
{ file: path.join(agentDir, "settings.json"), base: agentDir, source: "user-settings" as const },
|
|
194
|
-
];
|
|
195
|
-
|
|
196
|
-
for (const { file, base, source } of settingsFiles) {
|
|
197
|
-
const settings = readOptionalJsonFile(file, "skills settings file");
|
|
198
|
-
if (!settings || typeof settings !== "object" || Array.isArray(settings)) continue;
|
|
199
|
-
const skills = (settings as { skills?: unknown }).skills;
|
|
200
|
-
if (!Array.isArray(skills)) continue;
|
|
201
|
-
for (const entry of skills) {
|
|
202
|
-
if (typeof entry !== "string") continue;
|
|
203
|
-
let resolved = entry;
|
|
204
|
-
if (resolved.startsWith("~/")) {
|
|
205
|
-
resolved = path.join(os.homedir(), resolved.slice(2));
|
|
206
|
-
} else if (!path.isAbsolute(resolved)) {
|
|
207
|
-
resolved = path.resolve(base, resolved);
|
|
208
|
-
}
|
|
209
|
-
results.push({ path: resolved, source });
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return results;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function isSafePackagePath(value: string): boolean {
|
|
217
|
-
return value.length > 0
|
|
218
|
-
&& !path.isAbsolute(value)
|
|
219
|
-
&& value.split(/[\\/]/).every((part) => part.length > 0 && part !== "." && part !== "..");
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function parseNpmPackageName(source: string): string | undefined {
|
|
223
|
-
const spec = source.slice(4).trim();
|
|
224
|
-
if (!spec) return undefined;
|
|
225
|
-
const match = spec.match(/^(@?[^@]+(?:\/[^@]+)?)(?:@(.+))?$/);
|
|
226
|
-
const packageName = match?.[1] ?? spec;
|
|
227
|
-
return isSafePackagePath(packageName) ? packageName : undefined;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function stripGitRef(repoPath: string): string {
|
|
231
|
-
const atIndex = repoPath.indexOf("@");
|
|
232
|
-
const hashIndex = repoPath.indexOf("#");
|
|
233
|
-
const refIndex = [atIndex, hashIndex].filter((index) => index >= 0).sort((a, b) => a - b)[0];
|
|
234
|
-
return refIndex === undefined ? repoPath : repoPath.slice(0, refIndex);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function parseGitPackagePath(source: string): { host: string; repoPath: string } | undefined {
|
|
238
|
-
const spec = source.slice(4).trim();
|
|
239
|
-
if (!spec) return undefined;
|
|
240
|
-
|
|
241
|
-
let host = "";
|
|
242
|
-
let repoPath = "";
|
|
243
|
-
const scpLike = spec.match(/^git@([^:]+):(.+)$/);
|
|
244
|
-
if (scpLike) {
|
|
245
|
-
host = scpLike[1] ?? "";
|
|
246
|
-
repoPath = scpLike[2] ?? "";
|
|
247
|
-
} else if (/^[a-z][a-z0-9+.-]*:\/\//i.test(spec)) {
|
|
248
|
-
try {
|
|
249
|
-
const url = new URL(spec);
|
|
250
|
-
host = url.hostname;
|
|
251
|
-
repoPath = url.pathname.replace(/^\/+/, "");
|
|
252
|
-
} catch {
|
|
253
|
-
return undefined;
|
|
254
|
-
}
|
|
255
|
-
} else {
|
|
256
|
-
const slashIndex = spec.indexOf("/");
|
|
257
|
-
if (slashIndex < 0) return undefined;
|
|
258
|
-
host = spec.slice(0, slashIndex);
|
|
259
|
-
repoPath = spec.slice(slashIndex + 1);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const normalizedPath = stripGitRef(repoPath).replace(/\.git$/, "").replace(/^\/+/, "");
|
|
263
|
-
if (!host || !isSafePackagePath(host) || !isSafePackagePath(normalizedPath) || normalizedPath.split(/[\\/]/).length < 2) {
|
|
264
|
-
return undefined;
|
|
265
|
-
}
|
|
266
|
-
return { host, repoPath: normalizedPath };
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function resolveSettingsPackageRoot(source: string, baseDir: string): string | undefined {
|
|
270
|
-
const trimmed = source.trim();
|
|
271
|
-
if (!trimmed) return undefined;
|
|
272
|
-
if (trimmed.startsWith("git:")) {
|
|
273
|
-
const parsed = parseGitPackagePath(trimmed);
|
|
274
|
-
return parsed ? path.join(baseDir, "git", parsed.host, parsed.repoPath) : undefined;
|
|
275
|
-
}
|
|
276
|
-
if (trimmed.startsWith("npm:")) {
|
|
277
|
-
const packageName = parseNpmPackageName(trimmed);
|
|
278
|
-
return packageName ? path.join(baseDir, "npm", "node_modules", packageName) : undefined;
|
|
279
|
-
}
|
|
280
|
-
const normalized = trimmed.startsWith("file:") ? trimmed.slice(5) : trimmed;
|
|
281
|
-
if (normalized === "~") return os.homedir();
|
|
282
|
-
if (normalized.startsWith("~/")) return path.join(os.homedir(), normalized.slice(2));
|
|
283
|
-
if (path.isAbsolute(normalized)) return normalized;
|
|
284
|
-
if (normalized === "." || normalized === ".." || normalized.startsWith("./") || normalized.startsWith("../")) {
|
|
285
|
-
return path.resolve(baseDir, normalized);
|
|
286
|
-
}
|
|
287
|
-
return undefined;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
function collectSettingsPackageSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
291
|
-
const projectConfigDir = getProjectConfigDir(cwd);
|
|
292
|
-
const settingsFiles = [
|
|
293
|
-
{ file: path.join(projectConfigDir, "settings.json"), base: projectConfigDir, source: "project-package" as const },
|
|
294
|
-
{ file: path.join(agentDir, "settings.json"), base: agentDir, source: "user-package" as const },
|
|
295
|
-
];
|
|
296
|
-
const results: SkillSearchPath[] = [];
|
|
297
|
-
|
|
298
|
-
for (const { file, base, source } of settingsFiles) {
|
|
299
|
-
const settings = readOptionalJsonFile(file, "skills settings file");
|
|
300
|
-
if (!settings || typeof settings !== "object" || Array.isArray(settings)) continue;
|
|
301
|
-
const packages = (settings as { packages?: unknown }).packages;
|
|
302
|
-
if (!Array.isArray(packages)) continue;
|
|
303
|
-
|
|
304
|
-
for (const entry of packages) {
|
|
305
|
-
const packageSource = typeof entry === "string"
|
|
306
|
-
? entry
|
|
307
|
-
: typeof entry === "object" && entry !== null && typeof (entry as { source?: unknown }).source === "string"
|
|
308
|
-
? (entry as { source: string }).source
|
|
309
|
-
: undefined;
|
|
310
|
-
if (!packageSource) continue;
|
|
311
|
-
|
|
312
|
-
const packageRoot = resolveSettingsPackageRoot(packageSource, base);
|
|
313
|
-
if (!packageRoot) continue;
|
|
314
|
-
results.push(...extractSkillPathsFromPackageRoot(packageRoot, source));
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
return results;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function buildSkillPaths(cwd: string, agentDir: string): SkillSearchPath[] {
|
|
322
|
-
const projectConfigDir = getProjectConfigDir(cwd);
|
|
323
|
-
const skillPaths: SkillSearchPath[] = [
|
|
324
|
-
{ path: path.join(projectConfigDir, "skills"), source: "project" },
|
|
325
|
-
{ path: path.join(cwd, ".agents", "skills"), source: "project" },
|
|
326
|
-
{ path: path.join(agentDir, "skills"), source: "user" },
|
|
327
|
-
{ path: path.join(os.homedir(), ".agents", "skills"), source: "user" },
|
|
328
|
-
...collectInstalledPackageSkillPaths(cwd, agentDir),
|
|
329
|
-
...collectSettingsPackageSkillPaths(cwd, agentDir),
|
|
330
|
-
...extractSkillPathsFromPackageRoot(cwd, "project-package"),
|
|
331
|
-
...collectSettingsSkillPaths(cwd, agentDir),
|
|
332
|
-
];
|
|
333
|
-
|
|
334
|
-
const deduped = new Map<string, SkillSearchPath>();
|
|
335
|
-
for (const entry of skillPaths) {
|
|
336
|
-
const resolvedPath = path.resolve(entry.path);
|
|
337
|
-
const existing = deduped.get(resolvedPath);
|
|
338
|
-
if (!existing || (SOURCE_PRIORITY[entry.source] ?? 0) > (SOURCE_PRIORITY[existing.source] ?? 0)) {
|
|
339
|
-
deduped.set(resolvedPath, { path: resolvedPath, source: entry.source });
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
return [...deduped.values()];
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function inferSkillSource(filePath: string, cwd: string, agentDir: string, sourceHint?: SkillSource): SkillSource {
|
|
346
|
-
if (sourceHint) return sourceHint;
|
|
347
|
-
|
|
348
|
-
const projectConfigRoot = path.resolve(getProjectConfigDir(cwd));
|
|
349
|
-
const projectSkillsRoot = path.resolve(projectConfigRoot, "skills");
|
|
350
|
-
const projectPackagesRoot = path.resolve(projectConfigRoot, "npm", "node_modules");
|
|
351
|
-
const projectAgentsRoot = path.resolve(cwd, ".agents");
|
|
352
|
-
const userSkillsRoot = path.resolve(agentDir, "skills");
|
|
353
|
-
const userPackagesRoot = path.resolve(agentDir, "npm", "node_modules");
|
|
354
|
-
const userAgentRoot = path.resolve(agentDir);
|
|
355
|
-
const userAgentsRoot = path.resolve(os.homedir(), ".agents");
|
|
356
|
-
|
|
357
|
-
if (isWithinPath(filePath, projectPackagesRoot)) return "project-package";
|
|
358
|
-
if (isWithinPath(filePath, projectSkillsRoot) || isWithinPath(filePath, projectAgentsRoot)) return "project";
|
|
359
|
-
if (isWithinPath(filePath, projectConfigRoot)) return "project-settings";
|
|
360
|
-
|
|
361
|
-
if (isWithinPath(filePath, userPackagesRoot)) return "user-package";
|
|
362
|
-
if (isWithinPath(filePath, userSkillsRoot) || isWithinPath(filePath, userAgentsRoot)) return "user";
|
|
363
|
-
if (isWithinPath(filePath, userAgentRoot)) return "user-settings";
|
|
364
|
-
|
|
365
|
-
const globalRoot = getGlobalNpmRoot();
|
|
366
|
-
if (globalRoot && isWithinPath(filePath, globalRoot)) return "user-package";
|
|
367
|
-
|
|
368
|
-
return "unknown";
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
function chooseHigherPrioritySkill(existing: CachedSkillEntry | undefined, candidate: CachedSkillEntry): CachedSkillEntry {
|
|
372
|
-
if (!existing) return candidate;
|
|
373
|
-
const existingPriority = SOURCE_PRIORITY[existing.source] ?? 0;
|
|
374
|
-
const candidatePriority = SOURCE_PRIORITY[candidate.source] ?? 0;
|
|
375
|
-
if (candidatePriority > existingPriority) return candidate;
|
|
376
|
-
if (candidatePriority < existingPriority) return existing;
|
|
377
|
-
return candidate.order < existing.order ? candidate : existing;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
function maybeReadSkillDescription(filePath: string): string | undefined {
|
|
381
|
-
try {
|
|
382
|
-
const content = fs.readFileSync(filePath, "utf-8");
|
|
383
|
-
const normalized = content.replace(/\r\n/g, "\n");
|
|
384
|
-
if (!normalized.startsWith("---")) return undefined;
|
|
385
|
-
|
|
386
|
-
const endIndex = normalized.indexOf("\n---", 3);
|
|
387
|
-
if (endIndex === -1) return undefined;
|
|
388
|
-
|
|
389
|
-
const frontmatter = normalized.slice(3, endIndex).trim();
|
|
390
|
-
const match = frontmatter.match(/^description:\s*(.+)$/m);
|
|
391
|
-
if (!match) return undefined;
|
|
392
|
-
return match[1]?.trim().replace(/^['\"]|['\"]$/g, "");
|
|
393
|
-
} catch {
|
|
394
|
-
// Description parsing is best-effort metadata extraction.
|
|
395
|
-
return undefined;
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
function collectFilesystemSkills(cwd: string, agentDir: string, skillPaths: SkillSearchPath[]): CachedSkillEntry[] {
|
|
400
|
-
const entries: CachedSkillEntry[] = [];
|
|
401
|
-
const seen = new Map<string, number>();
|
|
402
|
-
const visitedDirectories = new Map<string, number>();
|
|
403
|
-
let order = 0;
|
|
404
|
-
|
|
405
|
-
const pushEntry = (name: string, filePath: string, sourceHint?: SkillSource) => {
|
|
406
|
-
const resolvedFile = path.resolve(filePath);
|
|
407
|
-
if (!fs.existsSync(resolvedFile)) return;
|
|
408
|
-
const source = inferSkillSource(resolvedFile, cwd, agentDir, sourceHint);
|
|
409
|
-
const existingIndex = seen.get(resolvedFile);
|
|
410
|
-
if (existingIndex !== undefined) {
|
|
411
|
-
const existing = entries[existingIndex];
|
|
412
|
-
if (existing && (SOURCE_PRIORITY[source] ?? 0) > (SOURCE_PRIORITY[existing.source] ?? 0)) {
|
|
413
|
-
entries[existingIndex] = {
|
|
414
|
-
...existing,
|
|
415
|
-
name,
|
|
416
|
-
source,
|
|
417
|
-
description: maybeReadSkillDescription(resolvedFile),
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
return;
|
|
421
|
-
}
|
|
422
|
-
seen.set(resolvedFile, entries.length);
|
|
423
|
-
entries.push({
|
|
424
|
-
name,
|
|
425
|
-
filePath: resolvedFile,
|
|
426
|
-
source,
|
|
427
|
-
description: maybeReadSkillDescription(resolvedFile),
|
|
428
|
-
order: order++,
|
|
429
|
-
});
|
|
430
|
-
};
|
|
431
|
-
|
|
432
|
-
const shouldSkipDirectory = (name: string) => name.startsWith(".") || name === "node_modules";
|
|
433
|
-
|
|
434
|
-
const markDirectoryVisited = (dirPath: string, sourceHint?: SkillSource): boolean => {
|
|
435
|
-
let resolvedDir: string;
|
|
436
|
-
try {
|
|
437
|
-
resolvedDir = fs.realpathSync(dirPath);
|
|
438
|
-
} catch {
|
|
439
|
-
resolvedDir = path.resolve(dirPath);
|
|
440
|
-
}
|
|
441
|
-
const priority = sourceHint ? SOURCE_PRIORITY[sourceHint] ?? 0 : SOURCE_PRIORITY.unknown;
|
|
442
|
-
const previousPriority = visitedDirectories.get(resolvedDir);
|
|
443
|
-
if (previousPriority !== undefined && previousPriority >= priority) return false;
|
|
444
|
-
visitedDirectories.set(resolvedDir, priority);
|
|
445
|
-
return true;
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
const walkSkillDirectories = (dirPath: string, sourceHint?: SkillSource) => {
|
|
449
|
-
if (!markDirectoryVisited(dirPath, sourceHint)) return;
|
|
450
|
-
|
|
451
|
-
const skillFile = path.join(dirPath, "SKILL.md");
|
|
452
|
-
if (fs.existsSync(skillFile)) {
|
|
453
|
-
pushEntry(path.basename(dirPath), skillFile, sourceHint);
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
let entriesInDir: fs.Dirent[];
|
|
458
|
-
try {
|
|
459
|
-
entriesInDir = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
460
|
-
} catch {
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
for (const entry of entriesInDir) {
|
|
465
|
-
if (shouldSkipDirectory(entry.name)) continue;
|
|
466
|
-
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
|
467
|
-
|
|
468
|
-
const entryPath = path.join(dirPath, entry.name);
|
|
469
|
-
let stat: fs.Stats;
|
|
470
|
-
try {
|
|
471
|
-
stat = fs.statSync(entryPath);
|
|
472
|
-
} catch {
|
|
473
|
-
continue;
|
|
474
|
-
}
|
|
475
|
-
if (stat.isDirectory()) {
|
|
476
|
-
walkSkillDirectories(entryPath, sourceHint);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
|
|
481
|
-
for (const skillPath of skillPaths) {
|
|
482
|
-
if (!fs.existsSync(skillPath.path)) continue;
|
|
483
|
-
|
|
484
|
-
let stat: fs.Stats;
|
|
485
|
-
try {
|
|
486
|
-
stat = fs.statSync(skillPath.path);
|
|
487
|
-
} catch {
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
if (stat.isFile()) {
|
|
492
|
-
const fileName = path.basename(skillPath.path);
|
|
493
|
-
if (!fileName.toLowerCase().endsWith(".md")) continue;
|
|
494
|
-
const skillName = fileName.toLowerCase() === "skill.md"
|
|
495
|
-
? path.basename(path.dirname(skillPath.path))
|
|
496
|
-
: path.basename(fileName, path.extname(fileName));
|
|
497
|
-
pushEntry(skillName, skillPath.path, skillPath.source);
|
|
498
|
-
continue;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
if (!stat.isDirectory()) continue;
|
|
502
|
-
|
|
503
|
-
const rootSkillFile = path.join(skillPath.path, "SKILL.md");
|
|
504
|
-
if (fs.existsSync(rootSkillFile)) {
|
|
505
|
-
pushEntry(path.basename(skillPath.path), rootSkillFile, skillPath.source);
|
|
506
|
-
continue;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
markDirectoryVisited(skillPath.path, skillPath.source);
|
|
510
|
-
|
|
511
|
-
let childEntries: fs.Dirent[];
|
|
512
|
-
try {
|
|
513
|
-
childEntries = fs.readdirSync(skillPath.path, { withFileTypes: true });
|
|
514
|
-
} catch {
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
for (const child of childEntries) {
|
|
519
|
-
if (child.name.startsWith(".")) continue;
|
|
520
|
-
const childPath = path.join(skillPath.path, child.name);
|
|
521
|
-
if (child.isDirectory() || child.isSymbolicLink()) {
|
|
522
|
-
if (shouldSkipDirectory(child.name)) continue;
|
|
523
|
-
let childStat: fs.Stats;
|
|
524
|
-
try {
|
|
525
|
-
childStat = fs.statSync(childPath);
|
|
526
|
-
} catch {
|
|
527
|
-
continue;
|
|
528
|
-
}
|
|
529
|
-
if (childStat.isDirectory()) walkSkillDirectories(childPath, skillPath.source);
|
|
530
|
-
continue;
|
|
531
|
-
}
|
|
532
|
-
if (child.isFile() && child.name.toLowerCase().endsWith(".md")) {
|
|
533
|
-
pushEntry(path.basename(child.name, path.extname(child.name)), childPath, skillPath.source);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
return entries;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
function getCachedSkills(cwd: string): CachedSkillEntry[] {
|
|
542
|
-
const now = Date.now();
|
|
543
|
-
const agentDir = getAgentDir();
|
|
544
|
-
if (loadSkillsCache && loadSkillsCache.cwd === cwd && loadSkillsCache.agentDir === agentDir && now - loadSkillsCache.timestamp < LOAD_SKILLS_CACHE_TTL_MS) {
|
|
545
|
-
return loadSkillsCache.skills;
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
const skillPaths = buildSkillPaths(cwd, agentDir);
|
|
549
|
-
const loaded = collectFilesystemSkills(cwd, agentDir, skillPaths);
|
|
550
|
-
const dedupedByName = new Map<string, CachedSkillEntry>();
|
|
551
|
-
|
|
552
|
-
for (const entry of loaded) {
|
|
553
|
-
const current = dedupedByName.get(entry.name);
|
|
554
|
-
dedupedByName.set(entry.name, chooseHigherPrioritySkill(current, entry));
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
const skills = [...dedupedByName.values()].sort((a, b) => a.order - b.order);
|
|
558
|
-
loadSkillsCache = { cwd, agentDir, skills, timestamp: now };
|
|
559
|
-
return skills;
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
export function resolveSkillPath(
|
|
563
|
-
skillName: string,
|
|
564
|
-
cwd: string,
|
|
565
|
-
): { path: string; source: SkillSource } | undefined {
|
|
566
|
-
const skills = getCachedSkills(cwd);
|
|
567
|
-
const skill = skills.find((s) => s.name === skillName);
|
|
568
|
-
if (!skill) return undefined;
|
|
569
|
-
return { path: skill.filePath, source: skill.source };
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
function readSkill(
|
|
573
|
-
skillName: string,
|
|
574
|
-
skillPath: string,
|
|
575
|
-
source: SkillSource,
|
|
576
|
-
): ResolvedSkill | undefined {
|
|
577
|
-
try {
|
|
578
|
-
const stat = fs.statSync(skillPath);
|
|
579
|
-
const cached = skillCache.get(skillPath);
|
|
580
|
-
if (cached && cached.mtime === stat.mtimeMs) {
|
|
581
|
-
return cached.skill;
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
const raw = fs.readFileSync(skillPath, "utf-8");
|
|
585
|
-
const content = stripSkillFrontmatter(raw);
|
|
586
|
-
const description = maybeReadSkillDescription(skillPath);
|
|
587
|
-
const skill: ResolvedSkill = {
|
|
588
|
-
name: skillName,
|
|
589
|
-
path: skillPath,
|
|
590
|
-
content,
|
|
591
|
-
description,
|
|
592
|
-
source,
|
|
593
|
-
};
|
|
594
|
-
|
|
595
|
-
skillCache.set(skillPath, { mtime: stat.mtimeMs, skill });
|
|
596
|
-
if (skillCache.size > MAX_CACHE_SIZE) {
|
|
597
|
-
const firstKey = skillCache.keys().next().value;
|
|
598
|
-
if (firstKey) skillCache.delete(firstKey);
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
return skill;
|
|
602
|
-
} catch {
|
|
603
|
-
// Treat unreadable skill files as unresolved so callers can surface as missing.
|
|
604
|
-
return undefined;
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
export function resolveSkills(
|
|
609
|
-
skillNames: string[],
|
|
610
|
-
cwd: string,
|
|
611
|
-
): { resolved: ResolvedSkill[]; missing: string[] } {
|
|
612
|
-
const resolved: ResolvedSkill[] = [];
|
|
613
|
-
const missing: string[] = [];
|
|
614
|
-
|
|
615
|
-
for (const name of skillNames) {
|
|
616
|
-
const trimmed = name.trim();
|
|
617
|
-
if (!trimmed) continue;
|
|
618
|
-
if (trimmed === SUBAGENT_ORCHESTRATION_SKILL) {
|
|
619
|
-
missing.push(trimmed);
|
|
620
|
-
continue;
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
const location = resolveSkillPath(trimmed, cwd);
|
|
624
|
-
if (!location) {
|
|
625
|
-
missing.push(trimmed);
|
|
626
|
-
continue;
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
const skill = readSkill(trimmed, location.path, location.source);
|
|
630
|
-
if (skill) {
|
|
631
|
-
resolved.push(skill);
|
|
632
|
-
} else {
|
|
633
|
-
missing.push(trimmed);
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
return { resolved, missing };
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
export function resolveSkillsWithFallback(
|
|
641
|
-
skillNames: string[],
|
|
642
|
-
primaryCwd: string,
|
|
643
|
-
fallbackCwd?: string,
|
|
644
|
-
): { resolved: ResolvedSkill[]; missing: string[] } {
|
|
645
|
-
const primary = resolveSkills(skillNames, primaryCwd);
|
|
646
|
-
if (!fallbackCwd || primary.missing.length === 0) return primary;
|
|
647
|
-
if (path.resolve(primaryCwd) === path.resolve(fallbackCwd)) return primary;
|
|
648
|
-
|
|
649
|
-
const fallback = resolveSkills(primary.missing, fallbackCwd);
|
|
650
|
-
return {
|
|
651
|
-
resolved: [...primary.resolved, ...fallback.resolved],
|
|
652
|
-
missing: fallback.missing,
|
|
653
|
-
};
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
export function buildSkillInjection(skills: ResolvedSkill[]): string {
|
|
657
|
-
if (skills.length === 0) return "";
|
|
658
|
-
|
|
659
|
-
const lines = [
|
|
660
|
-
"The following configured skills are available to this subagent.",
|
|
661
|
-
"Use the read tool to load a skill's file when the task matches its description.",
|
|
662
|
-
"When a skill file references a relative path, resolve it against the skill directory (parent of SKILL.md / dirname of the path) and use that absolute path in tool commands.",
|
|
663
|
-
"",
|
|
664
|
-
"<available_skills>",
|
|
665
|
-
];
|
|
666
|
-
for (const skill of skills) {
|
|
667
|
-
lines.push(" <skill>");
|
|
668
|
-
lines.push(` <name>${escapeXmlText(skill.name)}</name>`);
|
|
669
|
-
lines.push(` <description>${escapeXmlText(skill.description ?? "")}</description>`);
|
|
670
|
-
lines.push(` <location>${escapeXmlText(skill.path)}</location>`);
|
|
671
|
-
lines.push(" </skill>");
|
|
672
|
-
}
|
|
673
|
-
lines.push("</available_skills>");
|
|
674
|
-
return lines.join("\n");
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
function escapeXmlText(value: string): string {
|
|
678
|
-
return value
|
|
679
|
-
.replace(/&/g, "&")
|
|
680
|
-
.replace(/</g, "<")
|
|
681
|
-
.replace(/>/g, ">");
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
export function normalizeSkillInput(
|
|
685
|
-
input: string | string[] | boolean | undefined,
|
|
686
|
-
): string[] | false | undefined {
|
|
687
|
-
if (input === false) return false;
|
|
688
|
-
if (input === true || input === undefined) return undefined;
|
|
689
|
-
if (Array.isArray(input)) {
|
|
690
|
-
return [...new Set(input.map((s) => s.trim()).filter((s) => s.length > 0))];
|
|
691
|
-
}
|
|
692
|
-
// Guard against JSON-encoded arrays arriving as strings (e.g. '["a","b"]').
|
|
693
|
-
// Models sometimes serialise the skill parameter as a JSON string instead of
|
|
694
|
-
// a native array, and naively splitting on "," would embed brackets/quotes
|
|
695
|
-
// into the skill names, causing resolution to silently fail.
|
|
696
|
-
const trimmed = input.trim();
|
|
697
|
-
if (trimmed.startsWith("[")) {
|
|
698
|
-
try {
|
|
699
|
-
const parsed = JSON.parse(trimmed);
|
|
700
|
-
if (Array.isArray(parsed)) {
|
|
701
|
-
return normalizeSkillInput(parsed);
|
|
702
|
-
}
|
|
703
|
-
} catch {
|
|
704
|
-
// Not valid JSON – fall through to comma-split
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
return [...new Set(input.split(",").map((s) => s.trim()).filter((s) => s.length > 0))];
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
export function discoverAvailableSkills(cwd: string): Array<{
|
|
711
|
-
name: string;
|
|
712
|
-
source: SkillSource;
|
|
713
|
-
description?: string;
|
|
714
|
-
}> {
|
|
715
|
-
const skills = getCachedSkills(cwd);
|
|
716
|
-
return skills
|
|
717
|
-
.filter((s) => s.name !== SUBAGENT_ORCHESTRATION_SKILL)
|
|
718
|
-
.map((s) => ({
|
|
719
|
-
name: s.name,
|
|
720
|
-
source: s.source,
|
|
721
|
-
description: s.description,
|
|
722
|
-
}))
|
|
723
|
-
.sort((a, b) => a.name.localeCompare(b.name));
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
export function clearSkillCache(): void {
|
|
727
|
-
skillCache.clear();
|
|
728
|
-
loadSkillsCache = null;
|
|
729
|
-
}
|