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.
Files changed (194) hide show
  1. package/README.md +31 -28
  2. package/dist/cli.js +11 -7
  3. package/dist/commands/add.js +34 -21
  4. package/dist/commands/adopt.js +91 -0
  5. package/dist/commands/arguments.js +12 -0
  6. package/dist/commands/asset-install.js +161 -0
  7. package/dist/commands/asset-list.js +126 -0
  8. package/dist/commands/asset-share.js +94 -0
  9. package/dist/commands/auth.js +4 -0
  10. package/dist/commands/availability.js +72 -27
  11. package/dist/commands/capabilities.js +79 -417
  12. package/dist/commands/collaboration.js +212 -108
  13. package/dist/commands/environment.js +18 -411
  14. package/dist/commands/github.js +200 -0
  15. package/dist/commands/harnesses.js +74 -0
  16. package/dist/commands/helpers.js +39 -0
  17. package/dist/commands/hooks.js +87 -0
  18. package/dist/commands/init.js +4 -2
  19. package/dist/commands/knowledge.js +73 -0
  20. package/dist/commands/mcp-env.js +78 -0
  21. package/dist/commands/mcp.js +64 -0
  22. package/dist/commands/origin.js +21 -23
  23. package/dist/commands/people.js +51 -0
  24. package/dist/commands/sets.js +141 -0
  25. package/dist/commands/status.js +36 -25
  26. package/dist/commands/sync.js +13 -5
  27. package/dist/commands/workspace.js +75 -23
  28. package/dist/config/environment.js +13 -27
  29. package/dist/config/paths.js +81 -0
  30. package/dist/config/selection.js +30 -0
  31. package/dist/config/settings.js +99 -108
  32. package/dist/config/state.js +67 -55
  33. package/dist/entry/knowledge-startup.js +32 -0
  34. package/dist/entry/mcp-launch.js +71 -0
  35. package/dist/entry/opencode-plugin.js +62 -0
  36. package/dist/{features/git-workspace/startup.js → entry/workspace-startup.js} +7 -2
  37. package/dist/errors.js +198 -55
  38. package/dist/features/adoption/adopt.js +234 -0
  39. package/dist/features/adoption/copies.js +29 -0
  40. package/dist/features/adoption/originals.js +116 -0
  41. package/dist/features/adoption/record.js +120 -0
  42. package/dist/features/adoption/restore.js +28 -0
  43. package/dist/features/adoption/scan.js +482 -0
  44. package/dist/features/adoption/undo.js +42 -0
  45. package/dist/features/asset-kinds/index.js +129 -0
  46. package/dist/features/asset-selection/index.js +51 -19
  47. package/dist/features/auth/credentials.js +80 -54
  48. package/dist/features/auth/session.js +83 -49
  49. package/dist/features/auth/transport.js +69 -17
  50. package/dist/features/catalog/index.js +32 -61
  51. package/dist/features/collaboration/api.js +160 -0
  52. package/dist/features/collaboration/index.js +321 -233
  53. package/dist/features/collaboration/remote.js +35 -115
  54. package/dist/features/collaboration/results.js +2 -0
  55. package/dist/features/collaboration/workspace-git.js +242 -152
  56. package/dist/features/collaboration/workspace-github.js +118 -54
  57. package/dist/features/collaboration/workspace-store.js +157 -97
  58. package/dist/features/collaboration/workspace.js +278 -173
  59. package/dist/features/discovery/index.js +68 -57
  60. package/dist/features/doctor/index.js +56 -26
  61. package/dist/features/environment/auto-sync.js +28 -27
  62. package/dist/features/environment/harnesses.js +36 -0
  63. package/dist/features/environment/moves.js +171 -0
  64. package/dist/features/environment/names.js +73 -49
  65. package/dist/features/environment/operations.js +322 -229
  66. package/dist/features/environment/own-entries.js +89 -0
  67. package/dist/features/environment/settle-names.js +91 -0
  68. package/dist/features/environment/stage.js +202 -0
  69. package/dist/features/environment/sync.js +127 -487
  70. package/dist/features/environment/transaction.js +179 -147
  71. package/dist/features/environment/writes.js +243 -0
  72. package/dist/features/github/account.js +25 -0
  73. package/dist/features/github/connections.js +119 -0
  74. package/dist/features/github/pulls.js +118 -0
  75. package/dist/features/hooks/adapt.js +32 -25
  76. package/dist/features/hooks/catalog.js +28 -0
  77. package/dist/features/hooks/compatibility.js +55 -0
  78. package/dist/features/hooks/divergence.js +36 -12
  79. package/dist/features/hooks/test.js +91 -66
  80. package/dist/features/hooks/types.js +1 -0
  81. package/dist/features/imports/discover.js +133 -60
  82. package/dist/features/imports/folder.js +48 -14
  83. package/dist/features/imports/local.js +127 -135
  84. package/dist/features/imports/remote.js +46 -26
  85. package/dist/features/inspection/index.js +17 -17
  86. package/dist/features/inventory/index.js +111 -111
  87. package/dist/features/inventory/show.js +74 -0
  88. package/dist/features/knowledge/index.js +69 -49
  89. package/dist/features/mcps/catalog.js +31 -0
  90. package/dist/features/mcps/import.js +369 -0
  91. package/dist/features/mcps/index.js +104 -50
  92. package/dist/features/mcps/values.js +33 -0
  93. package/dist/features/migration/migrate.js +263 -0
  94. package/dist/features/migration/move.js +448 -0
  95. package/dist/features/migration/promote.js +26 -0
  96. package/dist/features/native/adapters.js +69 -96
  97. package/dist/features/native/configuration.js +134 -84
  98. package/dist/features/native/hook-shell.js +55 -0
  99. package/dist/features/native/plugin-module.js +56 -0
  100. package/dist/features/native/secret-values.js +74 -0
  101. package/dist/features/onboarding/environment.js +67 -34
  102. package/dist/features/onboarding/setup.js +43 -40
  103. package/dist/features/repositories/context.js +30 -13
  104. package/dist/features/secrets/index.js +77 -0
  105. package/dist/features/server/catalog.js +58 -0
  106. package/dist/features/server/client.js +13 -0
  107. package/dist/features/server/connection.js +37 -0
  108. package/dist/features/server/origin.js +74 -0
  109. package/dist/features/server/pages.js +24 -0
  110. package/dist/features/skills/catalog.js +42 -0
  111. package/dist/features/workspace/assets.js +57 -0
  112. package/dist/features/workspace/cloud-source.js +34 -0
  113. package/dist/features/workspace/directory.js +38 -0
  114. package/dist/features/{git-workspace → workspace}/format.js +121 -54
  115. package/dist/features/workspace/git.js +221 -0
  116. package/dist/features/workspace/installed.js +92 -0
  117. package/dist/features/{git-workspace → workspace}/overlay.js +53 -46
  118. package/dist/features/workspace/packages.js +173 -0
  119. package/dist/features/{git-workspace → workspace}/publish.js +120 -64
  120. package/dist/features/workspace/repo-source.js +193 -0
  121. package/dist/features/{git-workspace → workspace}/setup.js +72 -45
  122. package/dist/features/{git-workspace → workspace}/snapshot.js +60 -19
  123. package/dist/features/workspace/source.js +1 -0
  124. package/dist/features/workspace/tree.js +52 -0
  125. package/dist/features/workspace/workspace-source.js +27 -0
  126. package/dist/harnesses/claude.js +23 -1
  127. package/dist/harnesses/codex.js +67 -1
  128. package/dist/harnesses/cursor.js +22 -1
  129. package/dist/harnesses/detect.js +6 -50
  130. package/dist/harnesses/formats.js +52 -0
  131. package/dist/harnesses/opencode.js +108 -0
  132. package/dist/harnesses/registry.js +35 -2
  133. package/dist/platform.js +403 -0
  134. package/dist/program.js +275 -56
  135. package/dist/runtime.js +1 -0
  136. package/dist/shared/cli-error.js +22 -0
  137. package/dist/shared/constants.js +32 -0
  138. package/dist/shared/files.js +107 -0
  139. package/dist/shared/lock.js +113 -0
  140. package/dist/shared/lookup.js +11 -0
  141. package/dist/shared/patterns.js +102 -0
  142. package/dist/shared/shell.js +20 -0
  143. package/dist/shared/values.js +26 -0
  144. package/dist/ui/add-asset.js +41 -400
  145. package/dist/ui/add-hook.js +219 -0
  146. package/dist/ui/add-mcp.js +170 -0
  147. package/dist/ui/add-prompts.js +47 -0
  148. package/dist/ui/add-skills.js +117 -0
  149. package/dist/ui/adopt.js +197 -0
  150. package/dist/ui/asset-list.js +31 -13
  151. package/dist/ui/assets.js +38 -47
  152. package/dist/ui/auth.js +45 -34
  153. package/dist/ui/clash.js +10 -6
  154. package/dist/ui/collaboration.js +116 -44
  155. package/dist/ui/connection.js +17 -6
  156. package/dist/ui/decision-error.js +24 -0
  157. package/dist/ui/environment-onboarding.js +35 -13
  158. package/dist/ui/format.js +66 -18
  159. package/dist/ui/harness-picker.js +18 -0
  160. package/dist/ui/hook-compatibility.js +14 -6
  161. package/dist/ui/mcp.js +28 -3
  162. package/dist/ui/move.js +57 -6
  163. package/dist/ui/onboarding.js +148 -95
  164. package/dist/ui/options.js +9 -0
  165. package/dist/ui/output.js +108 -43
  166. package/dist/ui/package-details.js +34 -0
  167. package/dist/ui/pick.js +45 -33
  168. package/dist/ui/scope-install.js +18 -7
  169. package/dist/ui/secret-input.js +19 -0
  170. package/dist/ui/selection.js +17 -11
  171. package/dist/ui/theme.js +37 -14
  172. package/dist/version.js +6 -3
  173. package/package.json +13 -3
  174. package/dist/features/capabilities/index.js +0 -106
  175. package/dist/features/collaboration/domain.js +0 -90
  176. package/dist/features/environment/cloud-source.js +0 -27
  177. package/dist/features/environment/installed.js +0 -124
  178. package/dist/features/environment/packages.js +0 -120
  179. package/dist/features/environment/remote.js +0 -71
  180. package/dist/features/environment/workspace-source.js +0 -19
  181. package/dist/features/git-hosts/index.js +0 -148
  182. package/dist/features/git-workspace/assets.js +0 -71
  183. package/dist/features/git-workspace/git.js +0 -135
  184. package/dist/features/git-workspace/migrate.js +0 -188
  185. package/dist/features/git-workspace/move.js +0 -337
  186. package/dist/features/git-workspace/source.js +0 -159
  187. package/dist/features/imports/mcp.js +0 -219
  188. package/dist/features/knowledge/startup.js +0 -25
  189. package/dist/features/native/mcp-launch.js +0 -57
  190. package/dist/features/skills/install.js +0 -25
  191. package/dist/features/skills/remote.js +0 -177
  192. package/dist/features/skills/sync.js +0 -121
  193. package/dist/features/skills/validation.js +0 -34
  194. /package/dist/features/environment/{source.js → events.js} +0 -0
@@ -1,31 +1,53 @@
1
- import { homedir } from "node:os";
2
- import { readFile } from "node:fs/promises";
1
+ import { localPath } from "../../platform.js";
3
2
  import { z } from "zod";
4
- import { scopeSchema } from "@barndog-ai/api-contract";
5
3
  import { readSettings, SetupRequiredError, } from "../../config/settings.js";
6
4
  import { selectionState, withSelection } from "../../config/environment.js";
7
- import { applySelectionAction, resolveAssets, } from "../asset-selection/index.js";
8
- import { environmentCatalog, request } from "./remote.js";
9
- import { matchesOrigin } from "../collaboration/remote.js";
5
+ import { applySelectionAction, resolveAssets, ScopeNotSelectedError, } from "../asset-selection/index.js";
6
+ import { environmentCatalog } from "../server/catalog.js";
7
+ import { matchesOrigin, scopes } from "../server/origin.js";
10
8
  import { locked } from "./transaction.js";
11
9
  import { installedNames, synchronize } from "./sync.js";
12
- import { openRepoSource } from "./workspace-source.js";
13
- import { localOverlay, mergeLocalCatalog, withOverlay, } from "../git-workspace/overlay.js";
14
- import { CliError } from "../../errors.js";
15
- import { originLabel, validInstalledName } from "./names.js";
16
- async function overrideResolution(options) {
17
- if (options.decisions)
10
+ import { openRepoSource } from "../workspace/workspace-source.js";
11
+ import { localOverlay, mergeLocalCatalog, withOverlay, } from "../workspace/overlay.js";
12
+ import { CliError, friendlyMessage } from "../../errors.js";
13
+ import { inputFile } from "../../shared/files.js";
14
+ import { originLabel } from "../server/origin.js";
15
+ import { assetKinds } from "../asset-kinds/index.js";
16
+ import { only } from "../../shared/lookup.js";
17
+ import { splitOrigin } from "../../shared/patterns.js";
18
+ const decisionsSchema = z.array(z
19
+ .object({
20
+ assetId: z.uuidv4(),
21
+ choice: z.enum(["preserve", "clear"]),
22
+ })
23
+ .strict());
24
+ /** A `--decisions` file: `[{"assetId":"<uuid>","choice":"preserve"}]`. */
25
+ async function readDecisions(file, runtime) {
26
+ const text = (await inputFile(localPath(file, runtime))).toString("utf8");
27
+ let value;
28
+ try {
29
+ value = JSON.parse(text);
30
+ }
31
+ catch (error) {
32
+ throw new CliError(`${file} isn't valid JSON.`, DECISIONS_FORMAT, {
33
+ cause: error,
34
+ });
35
+ }
36
+ const result = decisionsSchema.safeParse(value);
37
+ if (!result.success) {
38
+ throw new CliError(`${file} isn't a decisions file: ${friendlyMessage(result.error)}.`, DECISIONS_FORMAT, { cause: result.error });
39
+ }
40
+ return result.data;
41
+ }
42
+ /** What a `--decisions` file holds, as the fix for one that doesn't. */
43
+ const DECISIONS_FORMAT = 'Write a list such as [{"assetId": "<uuid>", "choice": "preserve"}], with "preserve" or "clear" for each asset';
44
+ async function overrideResolution(options, runtime) {
45
+ if (options.decisions) {
18
46
  return {
19
47
  type: "per-asset",
20
- decisions: z
21
- .array(z
22
- .object({
23
- assetId: z.uuidv4(),
24
- choice: z.enum(["preserve", "clear"]),
25
- })
26
- .strict())
27
- .parse(JSON.parse(await readFile(options.decisions, "utf8"))),
48
+ decisions: await readDecisions(options.decisions, runtime),
28
49
  };
50
+ }
29
51
  return (options.resolution ??
30
52
  (options.overrides
31
53
  ? {
@@ -35,243 +57,314 @@ async function overrideResolution(options) {
35
57
  }
36
58
  : undefined));
37
59
  }
38
- function assetChange(action, assetId) {
39
- if (action === "install")
40
- return { type: "install-asset", assetId };
41
- if (action === "remove")
42
- return { type: "remove-asset", assetId };
43
- if (action === "enable" || action === "disable")
44
- return {
45
- type: "set-asset-enabled",
46
- assetId,
47
- enabled: action === "enable",
48
- };
49
- throw new Error(`Unknown environment action: ${action}`);
60
+ function assetChange(change, assetId) {
61
+ switch (change) {
62
+ case "install":
63
+ return { type: "install-asset", assetId };
64
+ case "remove":
65
+ return { type: "remove-asset", assetId };
66
+ case "enable":
67
+ case "disable":
68
+ return {
69
+ type: "set-asset-enabled",
70
+ assetId,
71
+ enabled: change === "enable",
72
+ };
73
+ }
50
74
  }
51
75
  /** Cloud answers these through its API; repo workspaces answer them from files. */
52
- async function workspaceAccess(settings, home) {
53
- const overlay = localOverlay(settings, home);
76
+ async function workspaceAccess(settings, runtime) {
77
+ const overlay = localOverlay(settings, runtime);
54
78
  const merge = (catalog) => overlay ? mergeLocalCatalog(catalog, overlay) : catalog;
55
- const source = openRepoSource(settings, home);
79
+ const source = openRepoSource(settings, runtime);
56
80
  if (source) {
57
81
  const model = await source.model();
58
82
  return {
59
83
  identity: model,
60
84
  catalog: await merge(model.catalog),
61
85
  source: overlay ? withOverlay(source, overlay) : source,
62
- scopes: async () => model.scopes,
86
+ scopes: () => Promise.resolve(model.scopes),
63
87
  };
64
88
  }
65
- const { connection: conn, catalog } = await environmentCatalog(home);
89
+ const { connection: conn, catalog } = await environmentCatalog(runtime);
66
90
  return {
67
91
  identity: conn,
68
92
  catalog: await merge(catalog),
69
- scopes: () => request(conn, "/v1/scopes", z.array(scopeSchema), home),
93
+ scopes: () => scopes(conn, runtime),
70
94
  };
71
95
  }
72
- export async function environmentOperation(action, options = {}, home = homedir()) {
73
- return locked(home, async () => {
74
- const settings = await readSettings(home);
75
- if (!settings)
76
- throw new SetupRequiredError();
77
- if (action === "knowledge-enable" || action === "knowledge-disable") {
78
- if (settings.version !== 2 ||
79
- (!settings.connection &&
80
- !settings.gitWorkspace &&
81
- !settings.localWorkspace))
82
- throw new Error("Set up a workspace with barndog init first");
83
- return {
84
- status: "applied",
85
- events: await synchronize(undefined, home, {
86
- ...settings,
87
- knowledge: action === "knowledge-enable",
88
- }),
89
- };
96
+ export async function readRequiredSettings(runtime) {
97
+ const { home } = runtime;
98
+ const settings = await readSettings(home);
99
+ if (!settings) {
100
+ throw new SetupRequiredError();
101
+ }
102
+ return settings;
103
+ }
104
+ function withLockedWorkspace(runtime, operation) {
105
+ return locked(runtime, async () => {
106
+ const settings = await readRequiredSettings(runtime);
107
+ const access = await workspaceAccess(settings, runtime);
108
+ return operation({
109
+ ...access,
110
+ runtime,
111
+ settings,
112
+ current: selectionState(settings),
113
+ });
114
+ });
115
+ }
116
+ async function syncSelection(workspace, state, next = withSelection(workspace.settings, state)) {
117
+ return {
118
+ status: "applied",
119
+ events: await synchronize(undefined, workspace.runtime, next, {
120
+ catalog: workspace.catalog,
121
+ source: workspace.source,
122
+ }),
123
+ state,
124
+ };
125
+ }
126
+ async function applyChange(workspace, change, overrides, origin) {
127
+ const resolution = await overrideResolution(overrides, workspace.runtime);
128
+ const result = (() => {
129
+ try {
130
+ return applySelectionAction(workspace.catalog, workspace.current, change, resolution);
90
131
  }
91
- const access = await workspaceAccess(settings, home);
92
- const { identity: conn, catalog } = access;
93
- const known = { catalog, source: access.source };
94
- const current = selectionState(settings);
95
- if (action === "list")
96
- return catalog.assets.filter((a) => !options.kind || a.kind === options.kind);
97
- if (action === "status")
98
- return resolveAssets(catalog, current);
99
- // Picker selections arrive as IDs and apply in one synchronization.
100
- if (options.ids?.length &&
101
- ["install", "remove", "enable", "disable"].includes(action)) {
102
- const resolution = await overrideResolution(options);
103
- let state = current;
104
- for (const assetId of options.ids) {
105
- const result = applySelectionAction(catalog, state, assetChange(action, assetId), resolution);
106
- if (result.status === "decision-required")
107
- return result;
108
- state = result.state;
132
+ catch (error) {
133
+ if (error instanceof ScopeNotSelectedError) {
134
+ throw new CliError(`${origin} isn't in your sets yet.`, `barndog sets install ${origin}`, { cause: error });
109
135
  }
110
- return {
111
- status: "applied",
112
- events: await synchronize(undefined, home, withSelection(settings, state), known),
113
- state,
114
- };
136
+ throw error;
115
137
  }
116
- const scopes = await access.scopes();
117
- if (action === "sets-list")
118
- return {
119
- available: scopes.filter((s) => s.type !== "dev"),
120
- subscriptions: current.subscriptions,
121
- diagnostics: resolveAssets(catalog, current).diagnostics,
122
- };
123
- let change;
124
- if (action.startsWith("sets-") ||
125
- action === "switch-team" ||
126
- action === "switch") {
127
- const selector = action === "switch-team" ? `team/${options.team}` : options.origin;
128
- const matches = scopes.filter((s) => s.type !== "dev" && matchesOrigin(s, selector, conn));
129
- let scope;
130
- if (matches.length === 1 && matches[0].type !== "dev")
131
- scope = { type: matches[0].type, id: matches[0].id };
132
- else {
133
- const [type, id] = selector.split(/[/:]/);
134
- if (!action.endsWith("remove") ||
135
- !["org", "team", "repo"].includes(type ?? "") ||
136
- !z.uuidv4().safeParse(id).success)
137
- throw new CliError(`No scope named ${selector} is available to you. To remove an unavailable subscription, pass its UUID, such as team/<uuid>.`, "barndog sets list");
138
- scope = { type: type, id: id };
139
- }
140
- if (action === "sets-preview" || action === "sets-install-custom") {
141
- const distribution = catalog.distributions.find((d) => d.scope.type === scope.type && d.scope.id === scope.id);
142
- if (!distribution)
143
- throw new Error("Scope distribution unavailable");
144
- const available = catalog.assets.filter((asset) => distribution.assets.some((entry) => entry.assetId === asset.id));
145
- if (action === "sets-preview")
146
- return {
147
- assets: available,
148
- selectedAssetIds: resolveAssets(catalog, current).assets.map((entry) => entry.asset.id),
149
- };
150
- const ids = z.array(z.uuidv4()).parse(options.assetIds);
151
- if (new Set(ids).size !== ids.length ||
152
- ids.some((id) => !available.some((asset) => asset.id === id)))
153
- throw new Error("Custom selection contains duplicate or unavailable assets");
154
- if (!ids.length)
155
- return { status: "applied", events: [] };
156
- let state = {
157
- ...current,
158
- subscriptions: current.subscriptions.filter((s) => s.scope.type !== scope.type || s.scope.id !== scope.id),
159
- };
160
- for (const assetId of ids) {
161
- const result = applySelectionAction(catalog, state, {
162
- type: "install-asset",
163
- assetId,
164
- });
165
- if (result.status !== "applied")
166
- throw new Error("Individual installation needs a decision");
167
- state = result.state;
168
- }
169
- return {
170
- status: "applied",
171
- events: await synchronize(undefined, home, withSelection(settings, state), known),
172
- state,
173
- };
138
+ })();
139
+ if (result.status === "decision-required") {
140
+ return result;
141
+ }
142
+ return syncSelection(workspace, result.state);
143
+ }
144
+ /**
145
+ * The asset `target` names for `change`. Anything but install also accepts the
146
+ * UUID of an asset that's no longer in the catalog, so it can still be cleaned up.
147
+ */
148
+ function assetId(workspace, target, change) {
149
+ const { settings, catalog, identity } = workspace;
150
+ const command = `barndog ${assetKinds[target.kind].command} ${change}`;
151
+ const localNames = settings.localNames ?? {};
152
+ const named = catalog.assets.filter((a) => a.kind === target.kind &&
153
+ (a.id === target.name ||
154
+ a.name === target.name ||
155
+ localNames[a.id] === target.name));
156
+ const { origin } = target;
157
+ const matches = origin
158
+ ? named.filter((a) => matchesOrigin(a.origin, origin, identity))
159
+ : named;
160
+ const staleId = change !== "install" && z.uuidv4().safeParse(target.name).success
161
+ ? target.name
162
+ : undefined;
163
+ const id = only(matches)?.id ?? staleId;
164
+ const [first] = matches;
165
+ if (!id && first && matches.length > 1) {
166
+ throw new CliError(`${target.name} exists in several scopes: ${matches.map((a) => originLabel(a.origin)).join(", ")}. Choose one with --origin.`, `${command} ${target.name} --origin ${originLabel(first.origin)}${change === "rename" ? " --as <new-name>" : ""}`);
167
+ }
168
+ if (!id) {
169
+ throw new CliError(`No ${assetKinds[target.kind].one} named ${target.name}${target.origin ? ` in ${target.origin}` : ""} is available to you.`, `barndog ${assetKinds[target.kind].command} search ${target.name}`);
170
+ }
171
+ return id;
172
+ }
173
+ /**
174
+ * Installs, removes, enables or disables one asset, then syncs. Removing an
175
+ * asset a subscribed set still supplies also excludes it. Takes the sync lock.
176
+ */
177
+ export function changeAsset(change, target, runtime) {
178
+ return withLockedWorkspace(runtime, async (workspace) => {
179
+ const id = assetId(workspace, target, change);
180
+ return applyChange(workspace, assetChange(change, id), target, target.origin);
181
+ });
182
+ }
183
+ export function changeAssets(change, target, runtime) {
184
+ return withLockedWorkspace(runtime, async (workspace) => {
185
+ if (!target.ids.length) {
186
+ return { status: "applied", events: [] };
187
+ }
188
+ const resolution = await overrideResolution(target, workspace.runtime);
189
+ let state = workspace.current;
190
+ for (const id of target.ids) {
191
+ const result = applySelectionAction(workspace.catalog, state, assetChange(change, id), resolution);
192
+ if (result.status === "decision-required") {
193
+ return result;
174
194
  }
175
- if (action === "switch") {
176
- if (scope.type !== "team" && scope.type !== "repo")
177
- throw new CliError("Switch between teams or repositories, such as team/engineering or repo/web-app.");
178
- change = {
179
- type: "switch-scope",
180
- scope: { type: scope.type, id: scope.id },
181
- mode: options.mode ?? "recommended",
182
- };
195
+ state = result.state;
196
+ }
197
+ return syncSelection(workspace, state);
198
+ });
199
+ }
200
+ /** `reset`, or `as` set to the published name, drops the local name. */
201
+ export async function renameAsset(target, runtime) {
202
+ if (!target.reset && target.as === undefined) {
203
+ const { command } = assetKinds[target.kind];
204
+ throw new CliError(`Say what to rename ${target.name} to.`, `barndog ${command} rename ${target.name} --as <new-name>, or --reset for its published name`);
205
+ }
206
+ return withLockedWorkspace(runtime, async (workspace) => {
207
+ const { settings, catalog, current } = workspace;
208
+ const id = assetId(workspace, target, "rename");
209
+ const asset = catalog.assets.find((a) => a.id === id);
210
+ const names = {
211
+ ...(settings.localNames ?? {}),
212
+ };
213
+ if (target.reset || target.as === asset?.name) {
214
+ delete names[id];
215
+ }
216
+ else {
217
+ const name = target.as ?? "";
218
+ if (!assetKinds[target.kind].validName(name)) {
219
+ throw new CliError(`${assetKinds[target.kind].nameRule}.`);
183
220
  }
184
- else if (action === "switch-team") {
185
- if (scope.type !== "team")
186
- throw new Error("A team scope is required");
187
- change = {
188
- type: "switch-team",
189
- scope: { type: "team", id: scope.id },
190
- mode: options.mode ?? "recommended",
191
- };
221
+ const held = await installedNames(runtime);
222
+ const taken = catalog.assets.find((a) => a.id !== id &&
223
+ a.kind === target.kind &&
224
+ ((names[a.id] ?? a.name) === name || held(a.id) === name) &&
225
+ resolveAssets(catalog, current).assets.some((entry) => entry.asset.id === a.id && entry.enabled));
226
+ if (taken) {
227
+ throw new CliError(`${name} is already used by ${originLabel(taken.origin)}.`);
192
228
  }
193
- else if (action === "sets-install")
194
- change = {
195
- type: "install-scope",
196
- scope,
197
- mode: options.mode ?? "recommended",
198
- };
199
- else if (action === "sets-remove")
200
- change = { type: "remove-scope", scope };
201
- else
202
- change = {
203
- type: "set-scope-enabled",
204
- scope,
205
- enabled: action === "sets-enable",
206
- };
229
+ names[id] = name;
230
+ }
231
+ const next = withSelection(settings, current);
232
+ if (Object.keys(names).length) {
233
+ next.localNames = names;
207
234
  }
208
235
  else {
209
- const localNames = settings.version === 2 ? (settings.localNames ?? {}) : {};
210
- const named = catalog.assets.filter((a) => a.kind === options.kind &&
211
- (a.id === options.name ||
212
- a.name === options.name ||
213
- localNames[a.id] === options.name));
214
- const matches = options.origin
215
- ? named.filter((a) => matchesOrigin(a.origin, options.origin, conn))
216
- : named;
217
- const staleId = action !== "install" && z.uuidv4().safeParse(options.name).success
218
- ? options.name
219
- : undefined;
220
- const id = matches.length === 1 ? matches[0].id : staleId;
221
- const command = `barndog ${options.kind === "skill" ? "skills" : options.kind === "mcp" ? "mcp" : "hooks"} ${action}`;
222
- if (!id && matches.length > 1)
223
- throw new CliError(`${options.name} exists in several scopes: ${matches.map((a) => originLabel(a.origin)).join(", ")}. Choose one with --origin.`, `${command} ${options.name} --origin ${originLabel(matches[0].origin)}`);
224
- if (!id)
225
- throw new CliError(`No ${options.kind === "mcp" ? "MCP server" : options.kind} named ${options.name}${options.origin ? ` in ${options.origin}` : ""} is available to you.`, `barndog ${options.kind === "skill" ? "skills" : options.kind === "mcp" ? "mcp" : "hooks"} search ${options.name}`);
226
- if (action === "rename") {
227
- const asset = catalog.assets.find((a) => a.id === id);
228
- const names = { ...localNames };
229
- if (options.reset || options.as === asset?.name)
230
- delete names[id];
231
- else {
232
- const name = options.as ?? "";
233
- if (!validInstalledName(name))
234
- throw new CliError("Use lowercase letters, numbers and single hyphens (at most 64 characters).");
235
- const held = await installedNames(home);
236
- const taken = catalog.assets.find((a) => a.id !== id &&
237
- a.kind === options.kind &&
238
- ((names[a.id] ?? a.name) === name || held(a.id) === name) &&
239
- resolveAssets(catalog, current).assets.some((entry) => entry.asset.id === a.id && entry.enabled));
240
- if (taken)
241
- throw new CliError(`${name} is already used by ${originLabel(taken.origin)}.`);
242
- names[id] = name;
243
- }
244
- const next = withSelection(settings, current);
245
- if (Object.keys(names).length)
246
- next.localNames = names;
247
- else
248
- delete next.localNames;
249
- return {
250
- status: "applied",
251
- events: await synchronize(undefined, home, next, known),
252
- state: current,
253
- };
254
- }
255
- change = assetChange(action, id);
236
+ delete next.localNames;
256
237
  }
257
- const resolution = await overrideResolution(options);
258
- const result = (() => {
259
- try {
260
- return applySelectionAction(catalog, current, change, resolution);
261
- }
262
- catch (error) {
263
- if (error.message.startsWith("Scope is not selected"))
264
- throw new CliError(`${options.origin} isn't in your sets yet.`, `barndog sets install ${options.origin}`, { cause: error });
265
- throw error;
238
+ return syncSelection(workspace, current, next);
239
+ });
240
+ }
241
+ export function listSets(runtime) {
242
+ return withLockedWorkspace(runtime, async ({ scopes, current, catalog }) => ({
243
+ available: (await scopes()).filter((s) => s.type !== "dev"),
244
+ subscriptions: current.subscriptions,
245
+ diagnostics: resolveAssets(catalog, current).diagnostics,
246
+ }));
247
+ }
248
+ /** Removal also accepts the UUID of a scope you can no longer see, such as `team/<uuid>`. */
249
+ async function findScope(workspace, selector, { forRemoval = false } = {}) {
250
+ const matches = (await workspace.scopes()).filter((s) => s.type !== "dev" && matchesOrigin(s, selector, workspace.identity));
251
+ const [match] = matches;
252
+ if (matches.length === 1 && match && match.type !== "dev") {
253
+ return { type: match.type, id: match.id };
254
+ }
255
+ const { type, value: id } = splitOrigin(selector);
256
+ if (!forRemoval ||
257
+ (type !== "org" && type !== "team" && type !== "repo") ||
258
+ id === undefined ||
259
+ !z.uuidv4().safeParse(id).success) {
260
+ throw new CliError(`No scope named ${selector} is available to you. To remove an unavailable subscription, pass its UUID, such as team/<uuid>.`, "barndog sets list");
261
+ }
262
+ return { type, id };
263
+ }
264
+ function scopeAssets(workspace, scope) {
265
+ const distribution = workspace.catalog.distributions.find((d) => d.scope.type === scope.type && d.scope.id === scope.id);
266
+ if (!distribution) {
267
+ throw new Error("Scope distribution unavailable");
268
+ }
269
+ return workspace.catalog.assets.filter((asset) => distribution.assets.some((entry) => entry.assetId === asset.id));
270
+ }
271
+ export function previewSet(origin, runtime) {
272
+ return withLockedWorkspace(runtime, async (workspace) => ({
273
+ assets: scopeAssets(workspace, await findScope(workspace, origin)),
274
+ selectedAssetIds: resolveAssets(workspace.catalog, workspace.current).assets.map((entry) => entry.asset.id),
275
+ }));
276
+ }
277
+ /**
278
+ * Subscribes to a set in recommended, optional or all mode, then syncs. Returns
279
+ * a decision-required outcome when existing overrides need an explicit choice.
280
+ */
281
+ export function installSet(options, runtime) {
282
+ return withLockedWorkspace(runtime, async (workspace) => applyChange(workspace, {
283
+ type: "install-scope",
284
+ scope: await findScope(workspace, options.origin),
285
+ mode: options.mode ?? "recommended",
286
+ }, options, options.origin));
287
+ }
288
+ /**
289
+ * Selects some of a scope's assets individually instead of subscribing, so the
290
+ * selection doesn't follow later additions to the scope.
291
+ */
292
+ export function installSetAssets(options, runtime) {
293
+ return withLockedWorkspace(runtime, async (workspace) => {
294
+ const scope = await findScope(workspace, options.origin);
295
+ const available = scopeAssets(workspace, scope);
296
+ const ids = z.array(z.uuidv4()).parse(options.assetIds);
297
+ const repeated = ids.filter((id, index) => ids.indexOf(id) !== index);
298
+ if (repeated.length) {
299
+ throw new CliError(`Each asset can be chosen once, but ${[...new Set(repeated)].join(", ")} is listed more than once.`);
300
+ }
301
+ const outside = ids.filter((id) => !available.some((asset) => asset.id === id));
302
+ if (outside.length) {
303
+ throw new CliError(`${outside.join(", ")} isn't one of ${options.origin}'s assets.`, `barndog sets show ${options.origin}`);
304
+ }
305
+ if (!ids.length) {
306
+ return { status: "applied", events: [] };
307
+ }
308
+ let state = {
309
+ ...workspace.current,
310
+ subscriptions: workspace.current.subscriptions.filter((s) => s.scope.type !== scope.type || s.scope.id !== scope.id),
311
+ };
312
+ for (const assetId of ids) {
313
+ const result = applySelectionAction(workspace.catalog, state, {
314
+ type: "install-asset",
315
+ assetId,
316
+ });
317
+ if (result.status !== "applied") {
318
+ throw new Error("Installing a set's assets one by one asked for a decision");
266
319
  }
267
- })();
268
- if (result.status === "decision-required")
269
- return result;
270
- const next = withSelection(settings, result.state);
320
+ state = result.state;
321
+ }
322
+ return syncSelection(workspace, state);
323
+ });
324
+ }
325
+ /**
326
+ * Removes, enables or disables a subscription, then syncs. A removed set's
327
+ * assets go unless something else selects them.
328
+ */
329
+ export function changeSet(change, options, runtime) {
330
+ return withLockedWorkspace(runtime, async (workspace) => {
331
+ const scope = await findScope(workspace, options.origin, {
332
+ forRemoval: change === "remove",
333
+ });
334
+ return applyChange(workspace, change === "remove"
335
+ ? { type: "remove-scope", scope }
336
+ : { type: "set-scope-enabled", scope, enabled: change === "enable" }, options, options.origin);
337
+ });
338
+ }
339
+ /** Replaces the subscriptions of the same type (team or repository). */
340
+ export function switchSet(options, runtime) {
341
+ return withLockedWorkspace(runtime, async (workspace) => {
342
+ const scope = await findScope(workspace, options.origin);
343
+ if (scope.type !== "team" && scope.type !== "repo") {
344
+ throw new CliError("Switch between teams or repositories, such as team/engineering or repo/web-app.");
345
+ }
346
+ return applyChange(workspace, {
347
+ type: "switch-scope",
348
+ scope: { type: scope.type, id: scope.id },
349
+ mode: options.mode ?? "recommended",
350
+ }, options, options.origin);
351
+ });
352
+ }
353
+ /** Whether coding tools are told at session start which knowledge documents exist. */
354
+ export function setKnowledge(enabled, runtime) {
355
+ return locked(runtime, async () => {
356
+ const settings = await readRequiredSettings(runtime);
357
+ if (!settings.connection &&
358
+ !settings.gitWorkspace &&
359
+ !settings.localWorkspace) {
360
+ throw new CliError("Set up a workspace first.", "barndog init");
361
+ }
271
362
  return {
272
363
  status: "applied",
273
- events: await synchronize(undefined, home, next, known),
274
- state: result.state,
364
+ events: await synchronize(undefined, runtime, {
365
+ ...settings,
366
+ knowledge: enabled,
367
+ }),
275
368
  };
276
369
  });
277
370
  }