barndog 0.1.1 → 0.2.1
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/README.md +31 -28
- package/dist/cli.js +11 -7
- package/dist/commands/add.js +34 -21
- package/dist/commands/adopt.js +91 -0
- package/dist/commands/arguments.js +12 -0
- package/dist/commands/asset-install.js +161 -0
- package/dist/commands/asset-list.js +126 -0
- package/dist/commands/asset-share.js +94 -0
- package/dist/commands/auth.js +4 -0
- package/dist/commands/availability.js +72 -27
- package/dist/commands/capabilities.js +79 -417
- package/dist/commands/collaboration.js +212 -108
- package/dist/commands/environment.js +18 -411
- package/dist/commands/github.js +200 -0
- package/dist/commands/harnesses.js +74 -0
- package/dist/commands/helpers.js +39 -0
- package/dist/commands/hooks.js +87 -0
- package/dist/commands/init.js +4 -2
- package/dist/commands/knowledge.js +73 -0
- package/dist/commands/mcp-env.js +78 -0
- package/dist/commands/mcp.js +64 -0
- package/dist/commands/origin.js +21 -23
- package/dist/commands/people.js +51 -0
- package/dist/commands/sets.js +141 -0
- package/dist/commands/status.js +36 -25
- package/dist/commands/sync.js +13 -5
- package/dist/commands/workspace.js +75 -23
- package/dist/config/environment.js +13 -27
- package/dist/config/paths.js +81 -0
- package/dist/config/selection.js +30 -0
- package/dist/config/settings.js +99 -108
- package/dist/config/state.js +67 -55
- package/dist/entry/knowledge-startup.js +32 -0
- package/dist/entry/mcp-launch.js +71 -0
- package/dist/entry/opencode-plugin.js +62 -0
- package/dist/{features/git-workspace/startup.js → entry/workspace-startup.js} +7 -2
- package/dist/errors.js +198 -55
- package/dist/features/adoption/adopt.js +234 -0
- package/dist/features/adoption/copies.js +29 -0
- package/dist/features/adoption/originals.js +116 -0
- package/dist/features/adoption/record.js +120 -0
- package/dist/features/adoption/restore.js +28 -0
- package/dist/features/adoption/scan.js +482 -0
- package/dist/features/adoption/undo.js +42 -0
- package/dist/features/asset-kinds/index.js +129 -0
- package/dist/features/asset-selection/index.js +51 -19
- package/dist/features/auth/credentials.js +80 -54
- package/dist/features/auth/session.js +83 -49
- package/dist/features/auth/transport.js +69 -17
- package/dist/features/catalog/index.js +32 -61
- package/dist/features/collaboration/api.js +160 -0
- package/dist/features/collaboration/index.js +321 -233
- package/dist/features/collaboration/remote.js +35 -115
- package/dist/features/collaboration/results.js +2 -0
- package/dist/features/collaboration/workspace-git.js +242 -152
- package/dist/features/collaboration/workspace-github.js +118 -54
- package/dist/features/collaboration/workspace-store.js +157 -97
- package/dist/features/collaboration/workspace.js +278 -173
- package/dist/features/discovery/index.js +68 -57
- package/dist/features/doctor/index.js +56 -26
- package/dist/features/environment/auto-sync.js +28 -27
- package/dist/features/environment/harnesses.js +36 -0
- package/dist/features/environment/moves.js +171 -0
- package/dist/features/environment/names.js +73 -49
- package/dist/features/environment/operations.js +322 -229
- package/dist/features/environment/own-entries.js +89 -0
- package/dist/features/environment/settle-names.js +91 -0
- package/dist/features/environment/stage.js +202 -0
- package/dist/features/environment/sync.js +127 -487
- package/dist/features/environment/transaction.js +179 -147
- package/dist/features/environment/writes.js +243 -0
- package/dist/features/github/account.js +25 -0
- package/dist/features/github/connections.js +119 -0
- package/dist/features/github/pulls.js +118 -0
- package/dist/features/hooks/adapt.js +32 -25
- package/dist/features/hooks/catalog.js +28 -0
- package/dist/features/hooks/compatibility.js +55 -0
- package/dist/features/hooks/divergence.js +36 -12
- package/dist/features/hooks/test.js +91 -66
- package/dist/features/hooks/types.js +1 -0
- package/dist/features/imports/discover.js +133 -60
- package/dist/features/imports/folder.js +48 -14
- package/dist/features/imports/local.js +127 -135
- package/dist/features/imports/remote.js +46 -26
- package/dist/features/inspection/index.js +17 -17
- package/dist/features/inventory/index.js +111 -111
- package/dist/features/inventory/show.js +74 -0
- package/dist/features/knowledge/index.js +69 -49
- package/dist/features/mcps/catalog.js +31 -0
- package/dist/features/mcps/import.js +369 -0
- package/dist/features/mcps/index.js +104 -50
- package/dist/features/mcps/values.js +33 -0
- package/dist/features/migration/migrate.js +263 -0
- package/dist/features/migration/move.js +448 -0
- package/dist/features/migration/promote.js +26 -0
- package/dist/features/native/adapters.js +69 -96
- package/dist/features/native/configuration.js +134 -84
- package/dist/features/native/hook-shell.js +55 -0
- package/dist/features/native/plugin-module.js +56 -0
- package/dist/features/native/secret-values.js +74 -0
- package/dist/features/onboarding/environment.js +67 -34
- package/dist/features/onboarding/setup.js +43 -40
- package/dist/features/repositories/context.js +30 -13
- package/dist/features/secrets/index.js +77 -0
- package/dist/features/server/catalog.js +58 -0
- package/dist/features/server/client.js +13 -0
- package/dist/features/server/connection.js +37 -0
- package/dist/features/server/origin.js +74 -0
- package/dist/features/server/pages.js +24 -0
- package/dist/features/skills/catalog.js +42 -0
- package/dist/features/workspace/assets.js +57 -0
- package/dist/features/workspace/cloud-source.js +34 -0
- package/dist/features/workspace/directory.js +38 -0
- package/dist/features/{git-workspace → workspace}/format.js +121 -54
- package/dist/features/workspace/git.js +221 -0
- package/dist/features/workspace/installed.js +92 -0
- package/dist/features/{git-workspace → workspace}/overlay.js +53 -46
- package/dist/features/workspace/packages.js +173 -0
- package/dist/features/{git-workspace → workspace}/publish.js +120 -64
- package/dist/features/workspace/repo-source.js +193 -0
- package/dist/features/{git-workspace → workspace}/setup.js +72 -45
- package/dist/features/{git-workspace → workspace}/snapshot.js +60 -19
- package/dist/features/workspace/source.js +1 -0
- package/dist/features/workspace/tree.js +52 -0
- package/dist/features/workspace/workspace-source.js +27 -0
- package/dist/harnesses/claude.js +23 -1
- package/dist/harnesses/codex.js +67 -1
- package/dist/harnesses/cursor.js +22 -1
- package/dist/harnesses/detect.js +6 -50
- package/dist/harnesses/formats.js +52 -0
- package/dist/harnesses/opencode.js +108 -0
- package/dist/harnesses/registry.js +35 -2
- package/dist/platform.js +403 -0
- package/dist/program.js +275 -56
- package/dist/runtime.js +1 -0
- package/dist/shared/cli-error.js +22 -0
- package/dist/shared/constants.js +32 -0
- package/dist/shared/files.js +107 -0
- package/dist/shared/lock.js +113 -0
- package/dist/shared/lookup.js +11 -0
- package/dist/shared/patterns.js +102 -0
- package/dist/shared/shell.js +20 -0
- package/dist/shared/values.js +26 -0
- package/dist/ui/add-asset.js +41 -400
- package/dist/ui/add-hook.js +219 -0
- package/dist/ui/add-mcp.js +170 -0
- package/dist/ui/add-prompts.js +47 -0
- package/dist/ui/add-skills.js +117 -0
- package/dist/ui/adopt.js +197 -0
- package/dist/ui/asset-list.js +31 -13
- package/dist/ui/assets.js +38 -47
- package/dist/ui/auth.js +45 -34
- package/dist/ui/clash.js +10 -6
- package/dist/ui/collaboration.js +116 -44
- package/dist/ui/connection.js +17 -6
- package/dist/ui/decision-error.js +24 -0
- package/dist/ui/environment-onboarding.js +35 -13
- package/dist/ui/format.js +66 -18
- package/dist/ui/harness-picker.js +18 -0
- package/dist/ui/hook-compatibility.js +14 -6
- package/dist/ui/mcp.js +28 -3
- package/dist/ui/move.js +57 -6
- package/dist/ui/onboarding.js +148 -95
- package/dist/ui/options.js +9 -0
- package/dist/ui/output.js +108 -43
- package/dist/ui/package-details.js +34 -0
- package/dist/ui/pick.js +45 -33
- package/dist/ui/scope-install.js +18 -7
- package/dist/ui/secret-input.js +19 -0
- package/dist/ui/selection.js +17 -11
- package/dist/ui/theme.js +37 -14
- package/dist/version.js +6 -3
- package/package.json +13 -3
- package/dist/features/capabilities/index.js +0 -106
- package/dist/features/collaboration/domain.js +0 -90
- package/dist/features/environment/cloud-source.js +0 -27
- package/dist/features/environment/installed.js +0 -124
- package/dist/features/environment/packages.js +0 -120
- package/dist/features/environment/remote.js +0 -71
- package/dist/features/environment/workspace-source.js +0 -19
- package/dist/features/git-hosts/index.js +0 -148
- package/dist/features/git-workspace/assets.js +0 -71
- package/dist/features/git-workspace/git.js +0 -135
- package/dist/features/git-workspace/migrate.js +0 -188
- package/dist/features/git-workspace/move.js +0 -337
- package/dist/features/git-workspace/source.js +0 -159
- package/dist/features/imports/mcp.js +0 -219
- package/dist/features/knowledge/startup.js +0 -25
- package/dist/features/native/mcp-launch.js +0 -57
- package/dist/features/skills/install.js +0 -25
- package/dist/features/skills/remote.js +0 -177
- package/dist/features/skills/sync.js +0 -121
- package/dist/features/skills/validation.js +0 -34
- /package/dist/features/environment/{source.js → events.js} +0 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the Skills, MCP servers and Hooks the user set up in each selected
|
|
3
|
+
* coding tool themselves, leaving out everything `state.json` says BarnDog
|
|
4
|
+
* installed. Nothing here changes a file.
|
|
5
|
+
*/
|
|
6
|
+
import { lstat, readFile, readdir, readlink, realpath } from "node:fs/promises";
|
|
7
|
+
import { basename, extname, isAbsolute, join, resolve } from "node:path";
|
|
8
|
+
import { isDeepStrictEqual } from "node:util";
|
|
9
|
+
import { hookImplementationSchema, } from "@barndog-ai/api-contract";
|
|
10
|
+
import { hookMetadata, packageFingerprint, validatePackageEntries, } from "@barndog-ai/api-contract/package";
|
|
11
|
+
import { parse as parseJsonc } from "jsonc-parser";
|
|
12
|
+
import { stringify } from "yaml";
|
|
13
|
+
import { readSettings, SetupRequiredError } from "../../config/settings.js";
|
|
14
|
+
import { readState } from "../../config/state.js";
|
|
15
|
+
import { CliError, friendlyMessage } from "../../errors.js";
|
|
16
|
+
import { adapters, hookHarnesses, nativeFile, skillDirectory, } from "../../harnesses/registry.js";
|
|
17
|
+
import { isWindows } from "../../platform.js";
|
|
18
|
+
import { ifExists, readIfExists } from "../../shared/files.js";
|
|
19
|
+
import { slugify, validAssetName } from "../../shared/patterns.js";
|
|
20
|
+
import { asRecord, isRecord } from "../../shared/values.js";
|
|
21
|
+
import { discoverSkills, ROOT_TOKEN } from "../imports/discover.js";
|
|
22
|
+
import { readFolder } from "../imports/folder.js";
|
|
23
|
+
import { renamedSkill } from "../imports/local.js";
|
|
24
|
+
import { convertServer, harnessServers } from "../mcps/import.js";
|
|
25
|
+
import { assetKinds } from "../asset-kinds/index.js";
|
|
26
|
+
import { assetPath, NAME_RULE } from "../workspace/format.js";
|
|
27
|
+
import { localMcpNamed } from "../mcps/import.js";
|
|
28
|
+
/** What can be adopted, without the file contents. */
|
|
29
|
+
export async function scanNative(runtime) {
|
|
30
|
+
const { found, skipped } = await findNativeAssets(runtime);
|
|
31
|
+
return { assets: found.map(publicAsset), skipped };
|
|
32
|
+
}
|
|
33
|
+
const publicAsset = (asset) => ({
|
|
34
|
+
key: asset.key,
|
|
35
|
+
harness: asset.harness,
|
|
36
|
+
kind: asset.kind,
|
|
37
|
+
name: asset.name,
|
|
38
|
+
description: asset.description,
|
|
39
|
+
location: asset.location,
|
|
40
|
+
sameName: asset.sameName,
|
|
41
|
+
identical: asset.identical,
|
|
42
|
+
notes: asset.notes,
|
|
43
|
+
values: asset.values,
|
|
44
|
+
});
|
|
45
|
+
/** Every adoptable asset in the selected tools, with its contents. */
|
|
46
|
+
export async function findNativeAssets(runtime) {
|
|
47
|
+
const { home } = runtime;
|
|
48
|
+
const settings = await readSettings(home);
|
|
49
|
+
if (!settings) {
|
|
50
|
+
throw new SetupRequiredError();
|
|
51
|
+
}
|
|
52
|
+
const state = await readState(home);
|
|
53
|
+
const selected = settings.harnesses;
|
|
54
|
+
const local = settings.localWorkspace?.path;
|
|
55
|
+
/** The local asset a candidate's name collides with; for an MCP server, ignoring case. */
|
|
56
|
+
const taken = async (kind, name) => {
|
|
57
|
+
if (kind === "mcp") {
|
|
58
|
+
return localMcpNamed(name, runtime);
|
|
59
|
+
}
|
|
60
|
+
const path = local && join(local, assetPath("org", kind, name));
|
|
61
|
+
return path && (await ifExists(lstat(path))) !== null ? name : null;
|
|
62
|
+
};
|
|
63
|
+
const skipped = [];
|
|
64
|
+
const candidates = [];
|
|
65
|
+
for (const harness of selected) {
|
|
66
|
+
const skills = await toolSkills(harness, state, runtime);
|
|
67
|
+
candidates.push(...skills.found);
|
|
68
|
+
skipped.push(...skills.skipped);
|
|
69
|
+
}
|
|
70
|
+
const servers = await toolServers(selected, runtime);
|
|
71
|
+
candidates.push(...servers.found);
|
|
72
|
+
skipped.push(...servers.skipped);
|
|
73
|
+
for (const harness of hookHarnesses) {
|
|
74
|
+
if (selected.includes(harness)) {
|
|
75
|
+
const hooks = await toolHooks(harness, state, runtime);
|
|
76
|
+
candidates.push(...hooks.found);
|
|
77
|
+
skipped.push(...hooks.skipped);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Skill and MCP names come from the tool and can't change; a Hook's name is
|
|
81
|
+
// made up here, so it moves aside instead.
|
|
82
|
+
const kept = [];
|
|
83
|
+
for (const candidate of candidates) {
|
|
84
|
+
const existing = candidate.kind === "hook"
|
|
85
|
+
? null
|
|
86
|
+
: await taken(candidate.kind, candidate.name);
|
|
87
|
+
if (existing !== null) {
|
|
88
|
+
skipped.push({
|
|
89
|
+
...describe(candidate),
|
|
90
|
+
reason: existing === candidate.name
|
|
91
|
+
? `Your local folder already has ${assetKinds[candidate.kind].a} named ${candidate.name}.`
|
|
92
|
+
: `Your local folder has ${existing}, which differs only in case and would share its file on this disk.`,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
kept.push(candidate);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const hookNames = new Set();
|
|
100
|
+
for (const candidate of kept.filter((c) => c.kind === "hook")) {
|
|
101
|
+
let name = candidate.name;
|
|
102
|
+
for (let suffix = 2; hookNames.has(name) || (await taken("hook", name)); suffix++) {
|
|
103
|
+
name = `${candidate.name}-${suffix}`;
|
|
104
|
+
}
|
|
105
|
+
hookNames.add(name);
|
|
106
|
+
if (name !== candidate.name && candidate.content.kind === "hook") {
|
|
107
|
+
candidate.content = {
|
|
108
|
+
kind: "hook",
|
|
109
|
+
entries: renameHook(candidate.content.entries, name),
|
|
110
|
+
};
|
|
111
|
+
candidate.name = name;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { found: compare(kept), skipped };
|
|
115
|
+
}
|
|
116
|
+
const describe = (asset) => ({
|
|
117
|
+
harness: asset.harness,
|
|
118
|
+
kind: asset.kind,
|
|
119
|
+
name: asset.name,
|
|
120
|
+
location: asset.location,
|
|
121
|
+
});
|
|
122
|
+
/** Fills in which other tools have an asset of the same kind and name, and whether it matches. */
|
|
123
|
+
function compare(candidates) {
|
|
124
|
+
return candidates.map((candidate) => {
|
|
125
|
+
const others = candidate.kind === "hook"
|
|
126
|
+
? []
|
|
127
|
+
: candidates.filter((other) => other !== candidate &&
|
|
128
|
+
other.kind === candidate.kind &&
|
|
129
|
+
other.name === candidate.name);
|
|
130
|
+
return {
|
|
131
|
+
...candidate,
|
|
132
|
+
key: assetKey(candidate),
|
|
133
|
+
copy: assetPath("org", candidate.kind, candidate.name),
|
|
134
|
+
sameName: others.map((other) => ({
|
|
135
|
+
key: assetKey(other),
|
|
136
|
+
harness: other.harness,
|
|
137
|
+
})),
|
|
138
|
+
identical: others.every((other) => sameContent(other.content, candidate.content)),
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const assetKey = (asset) => `${asset.harness}/${asset.kind}/${asset.name}`;
|
|
143
|
+
function sameContent(left, right) {
|
|
144
|
+
if (left.kind === "mcp" || right.kind === "mcp") {
|
|
145
|
+
return (left.kind === "mcp" &&
|
|
146
|
+
right.kind === "mcp" &&
|
|
147
|
+
isDeepStrictEqual(left.definition, right.definition));
|
|
148
|
+
}
|
|
149
|
+
return packageFingerprint(left.entries) === packageFingerprint(right.entries);
|
|
150
|
+
}
|
|
151
|
+
/** Skill folders in one tool's Skill directory, followed through links the user made. */
|
|
152
|
+
async function toolSkills(harness, state, runtime) {
|
|
153
|
+
const directory = skillDirectory(harness, runtime.home);
|
|
154
|
+
const owned = new Set(state.links
|
|
155
|
+
.filter((link) => link.harness === harness)
|
|
156
|
+
.map((link) => resolve(link.target)));
|
|
157
|
+
const found = [];
|
|
158
|
+
const skipped = [];
|
|
159
|
+
const names = (await ifExists(readdir(directory))) ?? [];
|
|
160
|
+
for (const name of names.sort()) {
|
|
161
|
+
const path = join(directory, name);
|
|
162
|
+
// Hidden folders hold the tool's own Skills, such as Codex's `.system`.
|
|
163
|
+
if (name.startsWith(".") || owned.has(resolve(path))) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const skill = await readSkill({ harness, name, path }, runtime);
|
|
167
|
+
if (skill && "reason" in skill) {
|
|
168
|
+
skipped.push(skill);
|
|
169
|
+
}
|
|
170
|
+
else if (skill) {
|
|
171
|
+
found.push(skill);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return { found, skipped };
|
|
175
|
+
}
|
|
176
|
+
/** One entry of a Skill directory: a Skill, why it can't be adopted, or null when it isn't a Skill. */
|
|
177
|
+
async function readSkill(entry, runtime) {
|
|
178
|
+
const { name, path } = entry;
|
|
179
|
+
const where = {
|
|
180
|
+
harness: entry.harness,
|
|
181
|
+
kind: "skill",
|
|
182
|
+
name,
|
|
183
|
+
location: path,
|
|
184
|
+
};
|
|
185
|
+
const link = (await lstat(path)).isSymbolicLink()
|
|
186
|
+
? await readlink(path)
|
|
187
|
+
: null;
|
|
188
|
+
const folder = link === null ? path : await ifExists(realpath(path));
|
|
189
|
+
if (folder === null) {
|
|
190
|
+
return {
|
|
191
|
+
...where,
|
|
192
|
+
reason: `It links to ${link ?? path}, which doesn't exist.`,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (!(await lstat(folder)).isDirectory() ||
|
|
196
|
+
(await ifExists(lstat(join(folder, "SKILL.md")))) === null) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
if (!validAssetName(name)) {
|
|
200
|
+
return {
|
|
201
|
+
...where,
|
|
202
|
+
reason: `${name} can't be a BarnDog name. ${NAME_RULE}.`,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const { skills, skipped } = discoverSkills(await readFolder(folder, runtime));
|
|
206
|
+
const skill = skills.find((candidate) => candidate.root === "");
|
|
207
|
+
if (!skill) {
|
|
208
|
+
return {
|
|
209
|
+
...where,
|
|
210
|
+
reason: skipped.find((problem) => problem.path === "SKILL.md")?.reason ??
|
|
211
|
+
"It isn't a valid Skill.",
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
...where,
|
|
216
|
+
description: skill.description,
|
|
217
|
+
notes: [],
|
|
218
|
+
literals: [],
|
|
219
|
+
values: [],
|
|
220
|
+
content: { kind: "skill", entries: renamedSkill(skill, name) },
|
|
221
|
+
original: link === null
|
|
222
|
+
? { type: "folder", path }
|
|
223
|
+
: { type: "link", path, target: link },
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/** MCP servers configured for the whole user, not for one project. */
|
|
227
|
+
async function toolServers(selected, runtime) {
|
|
228
|
+
const found = [];
|
|
229
|
+
const skipped = [];
|
|
230
|
+
for (const server of await harnessServers(runtime)) {
|
|
231
|
+
if (!selected.includes(server.harness) ||
|
|
232
|
+
server.label.includes("project")) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const asset = {
|
|
236
|
+
harness: server.harness,
|
|
237
|
+
kind: "mcp",
|
|
238
|
+
name: server.name,
|
|
239
|
+
location: server.file,
|
|
240
|
+
};
|
|
241
|
+
if (!assetKinds.mcp.validName(server.name)) {
|
|
242
|
+
skipped.push({
|
|
243
|
+
...asset,
|
|
244
|
+
reason: `${server.name} can't be a BarnDog name. ${assetKinds.mcp.nameRule}.`,
|
|
245
|
+
});
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
// OpenCode reads several files; one name in two of them is two servers.
|
|
249
|
+
const twice = found.find((other) => other.harness === server.harness && other.name === server.name);
|
|
250
|
+
if (twice) {
|
|
251
|
+
skipped.push({
|
|
252
|
+
...asset,
|
|
253
|
+
reason: `${twice.location} also configures ${server.name}.`,
|
|
254
|
+
});
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const converted = convertServer(server);
|
|
259
|
+
found.push({
|
|
260
|
+
...asset,
|
|
261
|
+
description: converted.definition.description,
|
|
262
|
+
notes: converted.notes,
|
|
263
|
+
literals: converted.literals,
|
|
264
|
+
values: converted.literals.map(({ key, variable }) => ({
|
|
265
|
+
key,
|
|
266
|
+
variable,
|
|
267
|
+
})),
|
|
268
|
+
content: { kind: "mcp", definition: converted.definition },
|
|
269
|
+
original: {
|
|
270
|
+
type: "entry",
|
|
271
|
+
kind: "mcp",
|
|
272
|
+
file: server.file,
|
|
273
|
+
key: server.name,
|
|
274
|
+
value: server.native,
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
skipped.push({ ...asset, reason: friendlyMessage(error) });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return { found, skipped };
|
|
283
|
+
}
|
|
284
|
+
/** The hook entries in one tool's user settings file, one Hook per event entry. */
|
|
285
|
+
async function toolHooks(harness, state, runtime) {
|
|
286
|
+
const file = nativeFile(harness, "hook", runtime.home);
|
|
287
|
+
const found = [];
|
|
288
|
+
const skipped = [];
|
|
289
|
+
const text = await readIfExists(file);
|
|
290
|
+
if (text === null) {
|
|
291
|
+
return { found, skipped };
|
|
292
|
+
}
|
|
293
|
+
const owned = state.native.filter((entry) => entry.harness === harness && entry.file === file);
|
|
294
|
+
for (const [event, groups] of Object.entries(asRecord(asRecord(parseJsonc(text)).hooks))) {
|
|
295
|
+
if (!Array.isArray(groups)) {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
for (const group of groups.filter(isRecord)) {
|
|
299
|
+
if (owned.some((entry) => entry.key === event && isDeepStrictEqual(entry.value, group))) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const where = { harness, kind: "hook", location: file };
|
|
303
|
+
const label = `${event} hook`;
|
|
304
|
+
// Undo and adoption find an entry by its value, so it must be unique.
|
|
305
|
+
if (groups.filter((other) => isDeepStrictEqual(other, group)).length > 1) {
|
|
306
|
+
skipped.push({
|
|
307
|
+
...where,
|
|
308
|
+
name: label,
|
|
309
|
+
reason: `${file} lists the same ${event} hook twice. Remove one copy first.`,
|
|
310
|
+
});
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
try {
|
|
314
|
+
const hook = await hookPackage({ harness, event, group }, runtime);
|
|
315
|
+
found.push({
|
|
316
|
+
...where,
|
|
317
|
+
name: hook.name,
|
|
318
|
+
description: hook.description,
|
|
319
|
+
notes: hook.notes,
|
|
320
|
+
literals: [],
|
|
321
|
+
values: [],
|
|
322
|
+
content: { kind: "hook", entries: hook.entries },
|
|
323
|
+
original: {
|
|
324
|
+
type: "entry",
|
|
325
|
+
kind: "hook",
|
|
326
|
+
file,
|
|
327
|
+
key: event,
|
|
328
|
+
value: group,
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
skipped.push({ ...where, name: label, reason: friendlyMessage(error) });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return { found, skipped };
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The absolute paths a hook command line mentions, with the word each came
|
|
341
|
+
* from: `"$HOME/.claude/hooks/x.sh" --fix` → `$HOME/.claude/hooks/x.sh` as
|
|
342
|
+
* `<home>/.claude/hooks/x.sh`. `~/`, `$HOME/` and `${HOME}/` count as the home
|
|
343
|
+
* folder; relative paths name nothing, since the tool's working folder varies.
|
|
344
|
+
*
|
|
345
|
+
* @internal Exported for its table test in `src/tests/patterns.test.ts`.
|
|
346
|
+
*/
|
|
347
|
+
export function commandPaths(command, home) {
|
|
348
|
+
const words = [];
|
|
349
|
+
for (const raw of command.split(" ")) {
|
|
350
|
+
const quote = raw.at(0);
|
|
351
|
+
const word = raw.length > 1 && (quote === '"' || quote === "'") && raw.endsWith(quote)
|
|
352
|
+
? raw.slice(1, -1)
|
|
353
|
+
: raw;
|
|
354
|
+
const prefix = ["~/", "$HOME/", "${HOME}/"].find((p) => word.startsWith(p));
|
|
355
|
+
const path = prefix ? join(home, word.slice(prefix.length)) : word;
|
|
356
|
+
if (path && isAbsolute(path)) {
|
|
357
|
+
words.push({ word, path });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return words;
|
|
361
|
+
}
|
|
362
|
+
/** The first script a command runs, copied into the package, and the command pointed at it. */
|
|
363
|
+
async function packagedCommand(command, scriptName, runtime) {
|
|
364
|
+
for (const { word, path } of commandPaths(command, runtime.home)) {
|
|
365
|
+
const info = await ifExists(lstat(path));
|
|
366
|
+
if (info?.isFile()) {
|
|
367
|
+
const file = basename(path);
|
|
368
|
+
return {
|
|
369
|
+
command: command.replace(word, () => `${ROOT_TOKEN}/${file}`),
|
|
370
|
+
script: { path: file, data: await readFile(path) },
|
|
371
|
+
from: path,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
// A command with no script file becomes one; that needs a POSIX shell.
|
|
376
|
+
if (isWindows(runtime.platform)) {
|
|
377
|
+
throw new CliError(`"${command}" doesn't run a script file, and on Windows BarnDog only adopts hooks that do.`);
|
|
378
|
+
}
|
|
379
|
+
return {
|
|
380
|
+
command: `${ROOT_TOKEN}/${scriptName}`,
|
|
381
|
+
script: {
|
|
382
|
+
path: scriptName,
|
|
383
|
+
data: Buffer.from(`#!/bin/sh\n${command}\n`),
|
|
384
|
+
},
|
|
385
|
+
from: undefined,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Hook names are made from a script's name; this leaves room for a `-2`
|
|
390
|
+
* suffix within the 64-character asset name limit.
|
|
391
|
+
*/
|
|
392
|
+
const HOOK_NAME_LENGTH = 60;
|
|
393
|
+
/** Each command handler pointed at a script inside the package. */
|
|
394
|
+
async function packageHandlers(handlers, event, runtime) {
|
|
395
|
+
const scripts = new Map();
|
|
396
|
+
const copied = [];
|
|
397
|
+
const rewritten = [];
|
|
398
|
+
for (const [index, handler] of handlers.entries()) {
|
|
399
|
+
if (!isRecord(handler) ||
|
|
400
|
+
typeof handler.command !== "string" ||
|
|
401
|
+
(handler.type ?? "command") !== "command") {
|
|
402
|
+
const type = asRecord(handler).type;
|
|
403
|
+
throw new CliError(`It runs a ${typeof type === "string" ? type : "non-command"} hook, and BarnDog adopts only command hooks.`);
|
|
404
|
+
}
|
|
405
|
+
const packaged = await packagedCommand(handler.command, `${slugify(event) || "hook"}-${index + 1}.sh`, runtime);
|
|
406
|
+
const already = scripts.get(packaged.script.path);
|
|
407
|
+
if (already && !already.equals(packaged.script.data)) {
|
|
408
|
+
throw new CliError(`It runs two different scripts named ${packaged.script.path}.`);
|
|
409
|
+
}
|
|
410
|
+
scripts.set(packaged.script.path, packaged.script.data);
|
|
411
|
+
if (packaged.from) {
|
|
412
|
+
copied.push(packaged.from);
|
|
413
|
+
}
|
|
414
|
+
rewritten.push({ ...handler, command: packaged.command });
|
|
415
|
+
}
|
|
416
|
+
return { scripts, copied, rewritten };
|
|
417
|
+
}
|
|
418
|
+
/** A Hook package that runs what one native hook entry ran. */
|
|
419
|
+
async function hookPackage(input, runtime) {
|
|
420
|
+
const { harness, event, group } = input;
|
|
421
|
+
const nested = Array.isArray(group.hooks);
|
|
422
|
+
const { scripts, copied, rewritten } = await packageHandlers(Array.isArray(group.hooks) ? group.hooks : [group], event, runtime);
|
|
423
|
+
// Named after the script it runs, else after its event.
|
|
424
|
+
const [script] = copied;
|
|
425
|
+
const name = slugify(script ? basename(script, extname(script)) : event).slice(0, HOOK_NAME_LENGTH) || "hook";
|
|
426
|
+
// The home-relative path, since a description may be shared beyond this machine.
|
|
427
|
+
const description = `${adapters[harness].name} ${event} hook from ~/${adapters[harness].hooks.file}`;
|
|
428
|
+
const events = hookImplementationSchema.shape.events.safeParse({
|
|
429
|
+
[event]: [nested ? { ...group, hooks: rewritten } : rewritten[0]],
|
|
430
|
+
});
|
|
431
|
+
if (!events.success) {
|
|
432
|
+
throw new CliError(`It uses settings a BarnDog Hook can't hold: ${events.error.issues.map((issue) => issue.path.join(".")).join(", ")}.`);
|
|
433
|
+
}
|
|
434
|
+
const manifest = {
|
|
435
|
+
name,
|
|
436
|
+
description,
|
|
437
|
+
implementations: {
|
|
438
|
+
[harness]: {
|
|
439
|
+
compatibility: "supported",
|
|
440
|
+
scripts: [...scripts.keys()],
|
|
441
|
+
requirements: { commands: [] },
|
|
442
|
+
events: events.data,
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
const entries = hookEntries(manifest, scripts);
|
|
447
|
+
return {
|
|
448
|
+
name,
|
|
449
|
+
description,
|
|
450
|
+
entries,
|
|
451
|
+
notes: copied.map((path) => `BarnDog runs its own copy of ${path}. The original stays, but edits to it no longer reach this hook.`),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
function hookEntries(manifest, scripts) {
|
|
455
|
+
const entries = [
|
|
456
|
+
...[...scripts].map(([path, data]) => ({ path, data, executable: true })),
|
|
457
|
+
{
|
|
458
|
+
path: "hook.yaml",
|
|
459
|
+
data: Buffer.from(stringify(manifest)),
|
|
460
|
+
executable: false,
|
|
461
|
+
},
|
|
462
|
+
];
|
|
463
|
+
hookMetadata(validatePackageEntries(entries, undefined, "hook.yaml"));
|
|
464
|
+
return entries;
|
|
465
|
+
}
|
|
466
|
+
/** The same Hook package under another name, for when the first choice is taken. */
|
|
467
|
+
function renameHook(entries, name) {
|
|
468
|
+
const scripts = new Map();
|
|
469
|
+
let manifest;
|
|
470
|
+
for (const entry of entries) {
|
|
471
|
+
if (entry.path === "hook.yaml") {
|
|
472
|
+
manifest = hookMetadata(entries);
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
scripts.set(entry.path, entry.data);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (!manifest) {
|
|
479
|
+
throw new Error("An adopted Hook package has no hook.yaml");
|
|
480
|
+
}
|
|
481
|
+
return hookEntries({ ...manifest, name }, scripts);
|
|
482
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** `barndog adopt undo`: put adopted assets back in their coding tools. */
|
|
2
|
+
import { CliError } from "../../errors.js";
|
|
3
|
+
import { locked } from "../environment/transaction.js";
|
|
4
|
+
import { copyFingerprint } from "./copies.js";
|
|
5
|
+
import { findAdoption, summary } from "./record.js";
|
|
6
|
+
import { restore } from "./restore.js";
|
|
7
|
+
export async function undoAdoption(target, runtime) {
|
|
8
|
+
const { home } = runtime;
|
|
9
|
+
return locked(runtime, async () => {
|
|
10
|
+
const record = await findAdoption(target.id, home);
|
|
11
|
+
const open = record.items.filter((item) => !item.undone);
|
|
12
|
+
const unknown = (target.only ?? []).filter((key) => !open.some((item) => item.key === key));
|
|
13
|
+
if (unknown.length) {
|
|
14
|
+
throw new CliError(`Adoption ${record.id} has nothing to undo for ${unknown.join(", ")}.`, "barndog adopt list");
|
|
15
|
+
}
|
|
16
|
+
if (target.only && record.status === "pending") {
|
|
17
|
+
throw new CliError(`Adoption ${record.id} didn't finish, so it can only be undone as a whole.`, `barndog adopt undo ${record.id}`);
|
|
18
|
+
}
|
|
19
|
+
const chosen = target.only
|
|
20
|
+
? open.filter((item) => open.some((picked) => target.only?.includes(picked.key) && picked.copy === item.copy))
|
|
21
|
+
: open;
|
|
22
|
+
if (!chosen.length) {
|
|
23
|
+
throw new CliError(`Adoption ${record.id} was already undone.`, "barndog adopt list");
|
|
24
|
+
}
|
|
25
|
+
// A pending adoption may have stopped before its copies were fingerprinted.
|
|
26
|
+
if (!target.force && record.status !== "pending") {
|
|
27
|
+
for (const item of chosen) {
|
|
28
|
+
const now = await copyFingerprint(item, runtime);
|
|
29
|
+
if (now !== null && now !== item.fingerprint) {
|
|
30
|
+
throw new CliError(`${item.copy} changed since ${item.name} was adopted, and undoing would lose those changes.`, "Save your changes somewhere else, then undo with --force", { command: `barndog adopt undo ${record.id} --force` });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
await restore(record, chosen, runtime);
|
|
35
|
+
const { items } = summary(record);
|
|
36
|
+
return {
|
|
37
|
+
id: record.id,
|
|
38
|
+
restored: items.filter((item) => chosen.some((c) => c.key === item.key)),
|
|
39
|
+
remaining: items.filter((item) => !item.undone),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { hookManifestSchema, hookSchema, mcpSchema, skillSchema, } from "@barndog-ai/api-contract";
|
|
2
|
+
import { packageMetadata, } from "@barndog-ai/api-contract/package";
|
|
3
|
+
import { parse } from "yaml";
|
|
4
|
+
import { assetFolders, packageKindList, } from "../../config/selection.js";
|
|
5
|
+
import { CliError, PackageError } from "../../errors.js";
|
|
6
|
+
import { only } from "../../shared/lookup.js";
|
|
7
|
+
import { validAssetName, validMcpName } from "../../shared/patterns.js";
|
|
8
|
+
/** In the order commands list them. */
|
|
9
|
+
export const assetKindList = [
|
|
10
|
+
"skill",
|
|
11
|
+
"mcp",
|
|
12
|
+
"hook",
|
|
13
|
+
];
|
|
14
|
+
export const isAssetKind = (value) => assetKindList.some((kind) => kind === value);
|
|
15
|
+
const file = (path, text, executable = false) => ({
|
|
16
|
+
path,
|
|
17
|
+
data: Buffer.from(text),
|
|
18
|
+
executable,
|
|
19
|
+
});
|
|
20
|
+
const hookManifest = (name) => `name: ${name}
|
|
21
|
+
description: Describe this Hook
|
|
22
|
+
implementations:
|
|
23
|
+
claude:
|
|
24
|
+
compatibility: supported
|
|
25
|
+
scripts: [scripts/hook.sh]
|
|
26
|
+
requirements:
|
|
27
|
+
commands: [sh]
|
|
28
|
+
events:
|
|
29
|
+
SessionStart:
|
|
30
|
+
- hooks:
|
|
31
|
+
- type: command
|
|
32
|
+
command: '\${BARNDOG_HOOK_ROOT}/scripts/hook.sh'
|
|
33
|
+
`;
|
|
34
|
+
/** Skill and Hook names, which become folder names: the `NAME` shape. */
|
|
35
|
+
const ASSET_NAME_RULE = "Names use lowercase letters, numbers and single hyphens, at most 64 characters";
|
|
36
|
+
/**
|
|
37
|
+
* What differs between asset kinds. Code that needs a kind's word, route, folder
|
|
38
|
+
* or schema reads it here instead of comparing `kind`.
|
|
39
|
+
*/
|
|
40
|
+
export const assetKinds = {
|
|
41
|
+
skill: {
|
|
42
|
+
one: "Skill",
|
|
43
|
+
a: "a Skill",
|
|
44
|
+
many: "Skills",
|
|
45
|
+
/** `barndog skills …` */
|
|
46
|
+
command: "skills",
|
|
47
|
+
/** `/v1/skills/…` */
|
|
48
|
+
route: "skills",
|
|
49
|
+
/** Inside a workspace scope and `~/.barndog/installed/`. */
|
|
50
|
+
folder: assetFolders.skill,
|
|
51
|
+
schema: skillSchema,
|
|
52
|
+
validName: validAssetName,
|
|
53
|
+
nameRule: ASSET_NAME_RULE,
|
|
54
|
+
manifest: "SKILL.md",
|
|
55
|
+
scaffold: (name) => [
|
|
56
|
+
file("SKILL.md", `---\nname: ${name}\ndescription: Describe this Skill\n---\n\nAdd instructions here.\n`),
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
mcp: {
|
|
60
|
+
one: "MCP server",
|
|
61
|
+
a: "an MCP server",
|
|
62
|
+
many: "MCP servers",
|
|
63
|
+
command: "mcp",
|
|
64
|
+
route: "mcps",
|
|
65
|
+
folder: assetFolders.mcp,
|
|
66
|
+
schema: mcpSchema,
|
|
67
|
+
validName: validMcpName,
|
|
68
|
+
nameRule: "MCP server names use letters, numbers, underscores and hyphens, at most 64 characters",
|
|
69
|
+
},
|
|
70
|
+
hook: {
|
|
71
|
+
one: "Hook",
|
|
72
|
+
a: "a Hook",
|
|
73
|
+
many: "Hooks",
|
|
74
|
+
command: "hooks",
|
|
75
|
+
route: "hooks",
|
|
76
|
+
folder: assetFolders.hook,
|
|
77
|
+
schema: hookSchema,
|
|
78
|
+
validName: validAssetName,
|
|
79
|
+
nameRule: ASSET_NAME_RULE,
|
|
80
|
+
manifest: "hook.yaml",
|
|
81
|
+
scaffold: (name) => [
|
|
82
|
+
file("hook.yaml", hookManifest(name)),
|
|
83
|
+
file("scripts/hook.sh", "#!/bin/sh\n# Read native input; implement the desired behavior here.\ncat >/dev/null\n", true),
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
export const assetNotFound = (kind, name, origin) => new CliError(`No ${assetKinds[kind].one} named ${name} in ${origin}.`, `barndog ${assetKinds[kind].command} search ${name}`);
|
|
88
|
+
/**
|
|
89
|
+
* Which kind of package a folder holds, from the one manifest file it has at
|
|
90
|
+
* its root (`SKILL.md` or `hook.yaml`). Throws when it has neither or both, or
|
|
91
|
+
* when the command that asked (`expected`) is for the other kind.
|
|
92
|
+
*/
|
|
93
|
+
export function packageKindOf(entries, expected) {
|
|
94
|
+
const kinds = packageKindList.filter((kind) => entries.some((entry) => entry.path === assetKinds[kind].manifest));
|
|
95
|
+
const kind = only(kinds);
|
|
96
|
+
if (!kind) {
|
|
97
|
+
throw new CliError(kinds.length
|
|
98
|
+
? `This folder has both ${kinds.map((k) => assetKinds[k].manifest).join(" and ")}, so it isn't clear what to push.`
|
|
99
|
+
: `This folder has no ${packageKindList.map((k) => assetKinds[k].manifest).join(" or ")}, so there is nothing to push.`, `Keep exactly one of ${packageKindList.map((k) => `${assetKinds[k].manifest} for ${assetKinds[k].a}`).join(" or ")}.`);
|
|
100
|
+
}
|
|
101
|
+
if (expected && expected !== kind) {
|
|
102
|
+
throw new CliError(`This folder holds ${assetKinds[kind].a}, not ${assetKinds[expected].a}.`, `barndog ${assetKinds[kind].command} push --origin <origin>`);
|
|
103
|
+
}
|
|
104
|
+
return kind;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* A package's metadata: the `SKILL.md` front matter, or the parsed `hook.yaml`
|
|
108
|
+
* manifest. Throws when the manifest is missing or invalid, or a Hook script it
|
|
109
|
+
* names is missing.
|
|
110
|
+
*/
|
|
111
|
+
export function metadata(entries, kind) {
|
|
112
|
+
if (kind === "skill") {
|
|
113
|
+
return packageMetadata(entries);
|
|
114
|
+
}
|
|
115
|
+
if (!entries.some((e) => e.path === "hook.yaml")) {
|
|
116
|
+
throw new PackageError("MISSING_PACKAGE_MANIFEST", "hook.yaml");
|
|
117
|
+
}
|
|
118
|
+
const manifest = hookManifestSchema.parse(parse(entries.find((e) => e.path === "hook.yaml")?.data.toString("utf8") ?? ""));
|
|
119
|
+
for (const implementation of Object.values(manifest.implementations)) {
|
|
120
|
+
if (implementation) {
|
|
121
|
+
for (const script of implementation.scripts) {
|
|
122
|
+
if (!entries.some((e) => e.path === script)) {
|
|
123
|
+
throw new PackageError("MISSING_HOOK_SCRIPT", script);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return manifest;
|
|
129
|
+
}
|