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,46 +1,65 @@
1
- import { homedir } from "node:os";
2
- import { basename, join } from "node:path";
3
- import { realpath } from "node:fs/promises";
4
- import { z } from "zod";
5
- import { readSettings, SetupRequiredError } from "../../config/settings.js";
1
+ import { basename } from "node:path";
2
+ import { readSettings, SetupRequiredError, } from "../../config/settings.js";
6
3
  import { selectionState } from "../../config/environment.js";
7
4
  import { readState } from "../../config/state.js";
8
- import { treeFingerprint } from "../environment/installed.js";
9
- import { fileText, linkTarget } from "../environment/transaction.js";
10
- import { environmentCatalog, request } from "../environment/remote.js";
11
- import { openRepoSource } from "../environment/workspace-source.js";
12
- import { localOverlay, mergeLocalCatalog } from "../git-workspace/overlay.js";
13
- import { scopeSchema } from "@barndog-ai/api-contract";
5
+ import { treeFingerprint } from "../workspace/tree.js";
6
+ import { linkTarget } from "../environment/transaction.js";
7
+ import { environmentCatalog } from "../server/catalog.js";
8
+ import { openRepoSource } from "../workspace/workspace-source.js";
9
+ import { localOverlay, mergeLocalCatalog, moveDestination, readMoved, } from "../workspace/overlay.js";
14
10
  import { resolveAssets } from "../asset-selection/index.js";
15
- import { installedName, originLabel, resolveClashes, } from "../environment/names.js";
16
- import { validateSkillSource } from "../skills/validation.js";
17
- const nativeSchema = z.array(z.object({
18
- id: z.string(),
19
- kind: z.string(),
20
- harness: z.string(),
21
- file: z.string(),
22
- key: z.string(),
23
- }));
24
- const installedSchema = z.array(z.object({
25
- id: z.string(),
26
- kind: z.string(),
27
- path: z.string(),
28
- fingerprint: z.string().optional(),
29
- versionId: z.string().optional(),
30
- }));
11
+ import { installedName, resolveClashes } from "../environment/names.js";
12
+ import { originLabel, scopes } from "../server/origin.js";
13
+ import { isAssetKind } from "../asset-kinds/index.js";
14
+ import { isHarness } from "../../harnesses/registry.js";
15
+ /** Local assets still moving to the shared workspace; none without a local folder. */
16
+ async function pendingMoves(settings) {
17
+ const folder = settings.localWorkspace?.path;
18
+ const records = folder ? await readMoved(folder) : [];
19
+ return records.map((entry) => ({
20
+ kind: entry.kind,
21
+ name: entry.name,
22
+ to: moveDestination(entry),
23
+ }));
24
+ }
31
25
  /** What is selected and what is actually installed on this machine. */
32
- export async function inventory(home = homedir()) {
26
+ /** The state of a selected asset; the first that applies wins. */
27
+ function inventoryState(asset) {
28
+ if (!asset.enabled) {
29
+ return "disabled";
30
+ }
31
+ if (asset.clashed) {
32
+ return "clash";
33
+ }
34
+ if (asset.modified) {
35
+ return "modified";
36
+ }
37
+ if (!asset.present) {
38
+ return "pending";
39
+ }
40
+ return asset.outdated ? "outdated" : "installed";
41
+ }
42
+ /** The state of an installed asset no longer selected. */
43
+ function leftoverState(modified, offline) {
44
+ if (modified) {
45
+ return "modified";
46
+ }
47
+ return offline ? "installed" : "removing";
48
+ }
49
+ /**
50
+ * Compares configured selections, catalog and installed files to report status;
51
+ * unreachable sources fall back to local state.
52
+ */
53
+ export async function inventory(runtime) {
54
+ const { home } = runtime;
33
55
  const settings = await readSettings(home);
34
- if (!settings)
56
+ if (!settings) {
35
57
  throw new SetupRequiredError();
36
- const statePath = join(home, ".barndog/state.json");
37
- const raw = await fileText(statePath);
38
- const state = await readState(statePath, home);
39
- const parsed = raw ? JSON.parse(raw) : {};
40
- const native = nativeSchema.parse(parsed.native ?? []);
41
- const installed = installedSchema.parse(parsed.installed ?? []);
42
- const conn = settings.version === 2 ? settings.connection : undefined;
43
- const localNames = settings.version === 2 ? (settings.localNames ?? {}) : {};
58
+ }
59
+ const state = await readState(home);
60
+ const { native, installed } = state;
61
+ const conn = settings.connection;
62
+ const localNames = settings.localNames ?? {};
44
63
  const linksOf = (id) => state.links.filter((l) => l.id === id);
45
64
  const nativeOf = (id) => native.filter((n) => n.id === id);
46
65
  const items = [];
@@ -48,75 +67,57 @@ export async function inventory(home = homedir()) {
48
67
  // The same comparisons that stop sync, so status can't call these up to date.
49
68
  const modified = new Set();
50
69
  for (const tree of installed) {
51
- const actual = await treeFingerprint(tree.path);
52
- if (tree.fingerprint && actual !== null && actual !== tree.fingerprint)
70
+ const actual = await treeFingerprint(tree.path, runtime);
71
+ if (tree.fingerprint && actual !== null && actual !== tree.fingerprint) {
53
72
  modified.add(tree.id);
73
+ }
54
74
  }
55
75
  for (const link of state.links) {
56
76
  const actual = await linkTarget(link.target);
57
- if (actual !== null && actual !== link.source)
77
+ if (actual !== null && actual !== link.source) {
58
78
  modified.add(link.id);
59
- }
60
- for (const local of settings.skills) {
61
- if (!("source" in local))
62
- continue;
63
- seen.add(local.id);
64
- const links = linksOf(local.id);
65
- let name = basename(local.source);
66
- try {
67
- name = (await validateSkillSource(await realpath(local.source))).name;
68
- }
69
- catch {
70
- /* the folder may be temporarily unavailable */
71
79
  }
72
- items.push({
73
- id: local.id,
74
- kind: "skill",
75
- name,
76
- installedName: name,
77
- origin: "local",
78
- via: ["config"],
79
- state: !local.enabled
80
- ? "disabled"
81
- : links.length
82
- ? "installed"
83
- : "pending",
84
- harnesses: links.map((l) => l.harness),
85
- paths: [local.source, ...links.map((l) => l.target)],
86
- });
87
80
  }
88
81
  let offline = null;
89
- const repo = openRepoSource(settings, home, { refresh: "auto" });
82
+ const repo = openRepoSource(settings, runtime, { refresh: "auto" });
90
83
  if (conn || repo) {
91
84
  try {
92
85
  const { catalog: primaryCatalog, scopes: loadScopes } = await (async () => {
93
86
  if (repo) {
94
87
  const model = await repo.model();
95
88
  offline = repo.notices()[0] ?? null;
96
- return { catalog: model.catalog, scopes: async () => model.scopes };
89
+ return {
90
+ catalog: model.catalog,
91
+ scopes: () => Promise.resolve(model.scopes),
92
+ };
97
93
  }
98
- const remote = await environmentCatalog(home, conn);
94
+ const remote = await environmentCatalog(runtime, conn);
99
95
  return {
100
96
  catalog: remote.catalog,
101
- scopes: () => request(remote.connection, "/v1/scopes", z.array(scopeSchema), home),
97
+ scopes: () => scopes(remote.connection, runtime),
102
98
  };
103
99
  })();
104
- const overlay = localOverlay(settings, home);
100
+ const overlay = localOverlay(settings, runtime);
105
101
  const catalog = overlay
106
102
  ? await mergeLocalCatalog(primaryCatalog, overlay)
107
103
  : primaryCatalog;
108
104
  const selection = selectionState(settings);
109
105
  const effective = resolveAssets(catalog, selection);
110
- const outcome = await resolveClashes(effective.assets
111
- .filter((entry) => entry.enabled && entry.asset.kind !== "hook")
112
- .map((entry) => ({
113
- id: entry.asset.id,
114
- kind: entry.asset.kind,
115
- name: entry.asset.name,
116
- origin: entry.asset.origin,
117
- installed: linksOf(entry.asset.id).length > 0 ||
118
- nativeOf(entry.asset.id).length > 0,
119
- })), localNames);
106
+ const outcome = await resolveClashes(effective.assets.flatMap(({ asset, enabled }) => {
107
+ const { kind } = asset;
108
+ if (!enabled || kind === "hook") {
109
+ return [];
110
+ }
111
+ return [
112
+ {
113
+ id: asset.id,
114
+ kind,
115
+ name: asset.name,
116
+ origin: asset.origin,
117
+ installed: linksOf(asset.id).length > 0 || nativeOf(asset.id).length > 0,
118
+ },
119
+ ];
120
+ }), localNames);
120
121
  const clashed = new Set(outcome.skipped.map((c) => c.incoming.id));
121
122
  for (const entry of effective.assets) {
122
123
  const { asset } = entry;
@@ -128,7 +129,7 @@ export async function inventory(home = homedir()) {
128
129
  const harnesses = [
129
130
  ...new Set([
130
131
  ...links.map((l) => l.harness),
131
- ...entries.map((n) => n.harness),
132
+ ...entries.map((n) => n.harness).filter(isHarness),
132
133
  ]),
133
134
  ];
134
135
  items.push({
@@ -140,17 +141,13 @@ export async function inventory(home = homedir()) {
140
141
  via: entry.contributions.map((c) => c.type === "individual"
141
142
  ? "individual"
142
143
  : originLabel(asset.origin.localOnly ? asset.origin : c.scope)),
143
- state: !entry.enabled
144
- ? "disabled"
145
- : clashed.has(asset.id)
146
- ? "clash"
147
- : modified.has(asset.id)
148
- ? "modified"
149
- : !present
150
- ? "pending"
151
- : tree?.versionId && tree.versionId !== asset.publishedVersion
152
- ? "outdated"
153
- : "installed",
144
+ state: inventoryState({
145
+ enabled: entry.enabled,
146
+ clashed: clashed.has(asset.id),
147
+ modified: modified.has(asset.id),
148
+ present,
149
+ outdated: Boolean(tree?.versionId && tree.versionId !== asset.publishedVersion),
150
+ }),
154
151
  harnesses,
155
152
  paths: [
156
153
  ...(tree ? [tree.path] : []),
@@ -159,13 +156,14 @@ export async function inventory(home = homedir()) {
159
156
  ],
160
157
  });
161
158
  }
162
- const scopes = await loadScopes();
163
- for (const item of items)
159
+ const visible = await loadScopes();
160
+ for (const item of items) {
164
161
  item.via = item.via.map((label) => {
165
162
  const [type, id] = label.split("/");
166
- const scope = scopes.find((s) => s.type === type && (type === "org" || s.id === id));
163
+ const scope = visible.find((s) => s.type === type && (type === "org" || s.id === id));
167
164
  return scope ? originLabel(scope) : label;
168
165
  });
166
+ }
169
167
  }
170
168
  catch (error) {
171
169
  offline = error instanceof Error ? error.message : String(error);
@@ -178,19 +176,21 @@ export async function inventory(home = homedir()) {
178
176
  .filter((n) => n.kind !== "knowledge" && n.kind !== "sync")
179
177
  .map((n) => n.id),
180
178
  ])) {
181
- if (seen.has(id))
179
+ if (seen.has(id)) {
182
180
  continue;
181
+ }
183
182
  const links = linksOf(id);
184
183
  const entries = nativeOf(id);
185
- const kind = (links.length ? "skill" : entries[0].kind);
186
- const name = links.length
187
- ? basename(links[0].target)
188
- : kind === "mcp"
189
- ? entries[0].key
190
- : (installed
191
- .find((t) => t.id === id)
192
- ?.path.split("/")
193
- .pop() ?? id);
184
+ const [link] = links;
185
+ const [entry] = entries;
186
+ const kind = link ? "skill" : entry?.kind;
187
+ if (!isAssetKind(kind)) {
188
+ continue;
189
+ }
190
+ const installedPath = installed.find((t) => t.id === id)?.path;
191
+ const installedFolder = installedPath ? basename(installedPath) : undefined;
192
+ const unlinkedName = kind === "mcp" ? entry?.key : installedFolder;
193
+ const name = link ? basename(link.target) : (unlinkedName ?? id);
194
194
  items.push({
195
195
  id,
196
196
  kind,
@@ -198,11 +198,11 @@ export async function inventory(home = homedir()) {
198
198
  installedName: name,
199
199
  origin: "—",
200
200
  via: [],
201
- state: modified.has(id) ? "modified" : offline ? "installed" : "removing",
201
+ state: leftoverState(modified.has(id), Boolean(offline)),
202
202
  harnesses: [
203
203
  ...new Set([
204
204
  ...links.map((l) => l.harness),
205
- ...entries.map((n) => n.harness),
205
+ ...entries.map((n) => n.harness).filter(isHarness),
206
206
  ]),
207
207
  ],
208
208
  paths: [
@@ -216,9 +216,9 @@ export async function inventory(home = homedir()) {
216
216
  return {
217
217
  harnesses: settings.harnesses,
218
218
  server: conn?.server ?? repo?.label ?? null,
219
- knowledge: !!(conn || repo) &&
220
- (settings.version !== 2 || settings.knowledge !== false),
219
+ knowledge: !!(conn || repo) && settings.knowledge !== false,
221
220
  offline,
222
221
  items,
222
+ moving: await pendingMoves(settings),
223
223
  };
224
224
  }
@@ -0,0 +1,74 @@
1
+ import { hookSchema, skillSchema } from "@barndog-ai/api-contract";
2
+ import { readSettings } from "../../config/settings.js";
3
+ import { hookHarnesses } from "../../harnesses/registry.js";
4
+ import { selectAsset } from "../server/catalog.js";
5
+ import { originLabel } from "../server/origin.js";
6
+ import { repoForOrigin, selectRepoAsset, showRepoAsset, } from "../workspace/assets.js";
7
+ import { inventory } from "./index.js";
8
+ /** What a Skill or Hook is, where it comes from, and where this machine has it. */
9
+ export async function showPackage(kind, name, origin, runtime) {
10
+ const { home } = runtime;
11
+ const settings = await readSettings(home);
12
+ const repo = settings && repoForOrigin(settings, runtime, origin);
13
+ const described = repo
14
+ ? repoDetails(selectRepoAsset(await repo.model(), kind, name, origin), kind)
15
+ : await cloudDetails(kind, name, origin, runtime);
16
+ const item = (await inventory(runtime)).items.find((entry) => entry.id === described.id);
17
+ return {
18
+ ...described,
19
+ installed: item
20
+ ? { state: item.state, harnesses: item.harnesses, paths: item.paths }
21
+ : null,
22
+ };
23
+ }
24
+ function repoDetails(asset, kind) {
25
+ const view = showRepoAsset(asset);
26
+ const events = view.manifest
27
+ ? eventsByTool((harness) => view.manifest?.implementations[harness]?.events)
28
+ : undefined;
29
+ return {
30
+ id: view.id,
31
+ kind,
32
+ name: view.name,
33
+ origin: view.origin,
34
+ version: view.version,
35
+ description: view.description,
36
+ ...(events ? { events } : {}),
37
+ };
38
+ }
39
+ async function cloudDetails(kind, name, origin, runtime) {
40
+ const { asset } = await selectAsset(kind, name, origin, runtime);
41
+ if (kind === "skill") {
42
+ const skill = skillSchema.parse(asset);
43
+ return {
44
+ id: skill.id,
45
+ kind,
46
+ name: skill.name,
47
+ origin: originLabel(skill.origin),
48
+ version: skill.version.number,
49
+ description: skill.version.description,
50
+ };
51
+ }
52
+ const hook = hookSchema.parse(asset);
53
+ return {
54
+ id: hook.id,
55
+ kind,
56
+ name: hook.name,
57
+ origin: originLabel(hook.origin),
58
+ version: hook.version.number,
59
+ description: hook.version.description,
60
+ events: eventsByTool((harness) => hook.compatibility.find((row) => row.harness === harness)
61
+ ?.implementation?.events),
62
+ };
63
+ }
64
+ /** Event names per tool, leaving out tools the Hook has no implementation for. */
65
+ function eventsByTool(eventsOf) {
66
+ const result = {};
67
+ for (const harness of hookHarnesses) {
68
+ const events = eventsOf(harness);
69
+ if (events) {
70
+ result[harness] = Object.keys(events);
71
+ }
72
+ }
73
+ return result;
74
+ }
@@ -1,20 +1,22 @@
1
- import { homedir } from "node:os";
2
1
  import { knowledgeDocumentSchema, knowledgeIndexSchema, } from "@barndog-ai/api-contract";
3
- import { connection } from "../collaboration/remote.js";
4
- import { request } from "../environment/remote.js";
5
- import { openRepoSource } from "../environment/workspace-source.js";
6
- import { activeLocal, localOverlay } from "../git-workspace/overlay.js";
2
+ import { connection } from "../server/connection.js";
3
+ import { parsed } from "../server/client.js";
4
+ import { openRepoSource } from "../workspace/workspace-source.js";
5
+ import { activeLocal, localOverlay } from "../workspace/overlay.js";
7
6
  import { readSettings } from "../../config/settings.js";
8
7
  import { CliError } from "../../errors.js";
8
+ import { allPages, PAGE_SIZE } from "../server/pages.js";
9
+ import { apiClient } from "../auth/transport.js";
9
10
  /** Session start must stay fast, so reading knowledge never contacts the Git host. */
10
- async function repoKnowledge(home) {
11
+ async function repoKnowledge(runtime) {
12
+ const { home } = runtime;
11
13
  const settings = await readSettings(home);
12
- const repo = settings && openRepoSource(settings, home, { refresh: "none" });
14
+ const repo = settings && openRepoSource(settings, runtime, { refresh: "none" });
13
15
  return repo ? (await repo.model()).knowledge : undefined;
14
16
  }
15
- async function sharedKnowledge(home, always) {
16
- const local = await repoKnowledge(home);
17
- if (local)
17
+ async function sharedKnowledge(runtime, always) {
18
+ const local = await repoKnowledge(runtime);
19
+ if (local) {
18
20
  return local
19
21
  .filter((doc) => !always || doc.content.accessMode === "always")
20
22
  .map((doc) => ({
@@ -26,62 +28,70 @@ async function sharedKnowledge(home, always) {
26
28
  versionId: doc.version,
27
29
  version: 0,
28
30
  }));
29
- const conn = await connection(home), documents = [];
30
- const seen = new Set();
31
- let cursor = null;
32
- do {
33
- const params = new URLSearchParams({
34
- limit: "100",
35
- ...(always ? { accessMode: "always" } : {}),
36
- ...(cursor ? { cursor } : {}),
37
- });
38
- const page = await request(conn, `/v1/knowledge?${params}`, knowledgeIndexSchema, home);
39
- documents.push(...page.documents);
40
- if (page.nextCursor && seen.has(page.nextCursor))
41
- throw new Error("Invalid knowledge pagination");
42
- if (page.nextCursor)
43
- seen.add(page.nextCursor);
44
- cursor = page.nextCursor;
45
- } while (cursor);
46
- return documents;
31
+ }
32
+ const conn = await connection(runtime);
33
+ return allPages(async (cursor) => {
34
+ const page = await parsed(knowledgeIndexSchema, apiClient(conn.server, runtime).GET("/v1/knowledge", {
35
+ params: {
36
+ query: {
37
+ limit: PAGE_SIZE,
38
+ cursor,
39
+ ...(always ? { accessMode: "always" } : {}),
40
+ },
41
+ },
42
+ }));
43
+ return { items: page.documents, nextCursor: page.nextCursor };
44
+ });
47
45
  }
48
- async function sharedDocument(name, home) {
49
- const local = await repoKnowledge(home);
46
+ async function sharedDocument(name, runtime) {
47
+ const local = await repoKnowledge(runtime);
50
48
  if (local) {
51
49
  const doc = local.find((d) => d.content.name === name);
52
- if (!doc)
50
+ if (!doc) {
53
51
  throw new CliError(`No knowledge document named ${name}.`, "barndog knowledge list");
52
+ }
54
53
  return { id: doc.id, version: { id: doc.version, content: doc.content } };
55
54
  }
56
- const conn = await connection(home);
57
- return request(conn, `/v1/knowledge/${encodeURIComponent(name)}`, knowledgeDocumentSchema, home);
55
+ const conn = await connection(runtime);
56
+ return parsed(knowledgeDocumentSchema, apiClient(conn.server, runtime).GET("/v1/knowledge/{selector}", {
57
+ params: { path: { selector: name } },
58
+ }));
58
59
  }
59
- async function keptLocal(home, shared) {
60
+ async function keptLocal(runtime, shared) {
61
+ const { home } = runtime;
60
62
  const settings = await readSettings(home);
61
- const overlay = settings && localOverlay(settings, home);
62
- if (!overlay)
63
+ const overlay = settings && localOverlay(settings, runtime);
64
+ if (!overlay) {
63
65
  return [];
66
+ }
64
67
  return (await activeLocal(overlay, shared.map((doc) => ({ kind: "knowledge", name: doc.name })))).knowledge;
65
68
  }
66
69
  /** Knowledge files that can't be read, so they don't vanish without a word. */
67
- export async function knowledgeProblems(home = homedir()) {
70
+ export async function knowledgeProblems(runtime) {
71
+ const { home } = runtime;
68
72
  const settings = await readSettings(home);
69
- if (!settings)
73
+ if (!settings) {
70
74
  return [];
75
+ }
71
76
  const sources = [
72
- openRepoSource(settings, home, { refresh: "none" }),
73
- localOverlay(settings, home),
77
+ openRepoSource(settings, runtime, { refresh: "none" }),
78
+ localOverlay(settings, runtime),
74
79
  ].flatMap((source) => (source ? [source] : []));
75
80
  const models = await Promise.all(sources.map((source) => source.model()));
76
81
  return [
77
82
  ...new Map(models.flatMap((m) => m.invalidKnowledge).map((p) => [p.path, p])).values(),
78
83
  ];
79
84
  }
80
- export async function knowledgeList(home = homedir(), always = false) {
85
+ /**
86
+ * Lists shared and retained local knowledge, optionally restricting to always-
87
+ * access documents, and marks session-start visibility.
88
+ */
89
+ export async function knowledgeList(always = false, runtime) {
90
+ const { home } = runtime;
81
91
  const settings = await readSettings(home);
82
- const listing = settings?.version !== 2 || settings.knowledge !== false;
83
- const shared = await sharedKnowledge(home, always);
84
- const local = (await keptLocal(home, shared))
92
+ const listing = settings?.knowledge !== false;
93
+ const shared = await sharedKnowledge(runtime, always);
94
+ const local = (await keptLocal(runtime, shared))
85
95
  .filter((doc) => !always || doc.content.accessMode === "always")
86
96
  .map((doc) => ({
87
97
  id: doc.id,
@@ -97,20 +107,30 @@ export async function knowledgeList(home = homedir(), always = false) {
97
107
  listedAtStart: listing && doc.accessMode === "always",
98
108
  }));
99
109
  }
100
- export async function knowledgeGet(name, home = homedir()) {
110
+ /**
111
+ * Retrieves shared knowledge by name, falling back to a retained local document
112
+ * if the shared lookup fails.
113
+ */
114
+ export async function knowledgeGet(name, runtime) {
101
115
  try {
102
- return await sharedDocument(name, home);
116
+ return await sharedDocument(name, runtime);
103
117
  }
104
118
  catch (error) {
105
- const doc = (await keptLocal(home, [])).find((d) => d.content.name === name);
106
- if (!doc)
119
+ const doc = (await keptLocal(runtime, [])).find((d) => d.content.name === name);
120
+ if (!doc) {
107
121
  throw error;
122
+ }
108
123
  return { id: doc.id, version: { id: doc.version, content: doc.content } };
109
124
  }
110
125
  }
126
+ /**
127
+ * Formats a newline-separated session-start index with single-line
128
+ * descriptions; no documents produce an empty string.
129
+ */
111
130
  export function knowledgeIndexText(documents) {
112
- if (!documents.length)
131
+ if (!documents.length) {
113
132
  return "";
133
+ }
114
134
  return [
115
135
  "Available organization knowledge (retrieve full content with barndog knowledge get <name>):",
116
136
  ...documents.map((d) => `- ${d.name}: ${d.description.replace(/[\r\n]+/g, " ")}`),
@@ -0,0 +1,31 @@
1
+ import { mcpListSchema } from "@barndog-ai/api-contract";
2
+ import { parsed } from "../server/client.js";
3
+ import { allPages, PAGE_SIZE } from "../server/pages.js";
4
+ import { apiClient } from "../auth/transport.js";
5
+ /**
6
+ * Fetches every MCP catalog page, rejecting organization mismatches and
7
+ * repeated pagination cursors.
8
+ */
9
+ export async function mcpCatalog(conn, runtime) {
10
+ return allPages(async (cursor) => {
11
+ const page = await parsed(mcpListSchema, apiClient(conn.server, runtime).GET("/v1/mcps", {
12
+ params: { query: { limit: PAGE_SIZE, cursor } },
13
+ }));
14
+ const items = page.mcps.map((mcp) => {
15
+ if (mcp.organizationId !== conn.organizationId) {
16
+ throw new Error("The server sent a catalog page for another organization");
17
+ }
18
+ return {
19
+ id: mcp.id,
20
+ kind: "mcp",
21
+ name: mcp.name,
22
+ origin: mcp.origin,
23
+ version: {
24
+ number: mcp.version.number,
25
+ description: mcp.version.definition.description,
26
+ },
27
+ };
28
+ });
29
+ return { items, nextCursor: page.nextCursor };
30
+ });
31
+ }