barndog 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +8 -7
  2. package/dist/cli.js +11 -7
  3. package/dist/commands/add.js +33 -21
  4. package/dist/commands/arguments.js +12 -0
  5. package/dist/commands/asset-install.js +160 -0
  6. package/dist/commands/asset-list.js +121 -0
  7. package/dist/commands/asset-share.js +94 -0
  8. package/dist/commands/auth.js +4 -0
  9. package/dist/commands/availability.js +54 -26
  10. package/dist/commands/capabilities.js +71 -415
  11. package/dist/commands/collaboration.js +194 -110
  12. package/dist/commands/environment.js +18 -411
  13. package/dist/commands/github.js +200 -0
  14. package/dist/commands/helpers.js +39 -0
  15. package/dist/commands/hooks.js +87 -0
  16. package/dist/commands/init.js +4 -2
  17. package/dist/commands/knowledge.js +73 -0
  18. package/dist/commands/mcp.js +62 -0
  19. package/dist/commands/origin.js +21 -23
  20. package/dist/commands/people.js +51 -0
  21. package/dist/commands/sets.js +141 -0
  22. package/dist/commands/status.js +34 -24
  23. package/dist/commands/sync.js +11 -4
  24. package/dist/commands/workspace.js +33 -19
  25. package/dist/config/environment.js +13 -27
  26. package/dist/config/paths.js +62 -0
  27. package/dist/config/selection.js +30 -0
  28. package/dist/config/settings.js +97 -108
  29. package/dist/config/state.js +57 -57
  30. package/dist/entry/knowledge-startup.js +32 -0
  31. package/dist/entry/mcp-launch.js +71 -0
  32. package/dist/entry/opencode-plugin.js +62 -0
  33. package/dist/{features/git-workspace/startup.js → entry/workspace-startup.js} +7 -2
  34. package/dist/errors.js +198 -55
  35. package/dist/features/asset-kinds/index.js +120 -0
  36. package/dist/features/asset-selection/index.js +51 -19
  37. package/dist/features/auth/credentials.js +80 -47
  38. package/dist/features/auth/session.js +80 -45
  39. package/dist/features/auth/transport.js +66 -15
  40. package/dist/features/catalog/index.js +32 -61
  41. package/dist/features/collaboration/api.js +160 -0
  42. package/dist/features/collaboration/index.js +321 -233
  43. package/dist/features/collaboration/remote.js +35 -115
  44. package/dist/features/collaboration/results.js +2 -0
  45. package/dist/features/collaboration/workspace-git.js +233 -149
  46. package/dist/features/collaboration/workspace-github.js +117 -54
  47. package/dist/features/collaboration/workspace-store.js +140 -100
  48. package/dist/features/collaboration/workspace.js +257 -172
  49. package/dist/features/discovery/index.js +68 -57
  50. package/dist/features/doctor/index.js +32 -24
  51. package/dist/features/environment/auto-sync.js +28 -27
  52. package/dist/features/environment/names.js +67 -47
  53. package/dist/features/environment/operations.js +321 -228
  54. package/dist/features/environment/own-entries.js +89 -0
  55. package/dist/features/environment/settle-names.js +91 -0
  56. package/dist/features/environment/stage.js +178 -0
  57. package/dist/features/environment/sync.js +113 -484
  58. package/dist/features/environment/transaction.js +179 -147
  59. package/dist/features/environment/writes.js +196 -0
  60. package/dist/features/github/account.js +25 -0
  61. package/dist/features/github/connections.js +119 -0
  62. package/dist/features/github/pulls.js +118 -0
  63. package/dist/features/hooks/adapt.js +32 -25
  64. package/dist/features/hooks/catalog.js +28 -0
  65. package/dist/features/hooks/compatibility.js +49 -0
  66. package/dist/features/hooks/divergence.js +9 -6
  67. package/dist/features/hooks/test.js +74 -49
  68. package/dist/features/hooks/types.js +1 -0
  69. package/dist/features/imports/discover.js +134 -61
  70. package/dist/features/imports/folder.js +48 -14
  71. package/dist/features/imports/local.js +113 -134
  72. package/dist/features/imports/remote.js +44 -25
  73. package/dist/features/inspection/index.js +17 -17
  74. package/dist/features/inventory/index.js +99 -110
  75. package/dist/features/knowledge/index.js +69 -49
  76. package/dist/features/mcps/catalog.js +31 -0
  77. package/dist/features/mcps/import.js +316 -0
  78. package/dist/features/mcps/index.js +98 -49
  79. package/dist/features/migration/migrate.js +263 -0
  80. package/dist/features/{git-workspace → migration}/move.js +165 -109
  81. package/dist/features/migration/promote.js +24 -0
  82. package/dist/features/native/adapters.js +53 -94
  83. package/dist/features/native/configuration.js +128 -82
  84. package/dist/features/native/hook-shell.js +55 -0
  85. package/dist/features/native/plugin-module.js +56 -0
  86. package/dist/features/onboarding/environment.js +67 -34
  87. package/dist/features/onboarding/setup.js +43 -40
  88. package/dist/features/repositories/context.js +30 -13
  89. package/dist/features/server/catalog.js +58 -0
  90. package/dist/features/server/client.js +13 -0
  91. package/dist/features/server/connection.js +37 -0
  92. package/dist/features/server/origin.js +74 -0
  93. package/dist/features/server/pages.js +24 -0
  94. package/dist/features/skills/catalog.js +42 -0
  95. package/dist/features/workspace/assets.js +57 -0
  96. package/dist/features/workspace/cloud-source.js +34 -0
  97. package/dist/features/workspace/directory.js +38 -0
  98. package/dist/features/{git-workspace → workspace}/format.js +114 -51
  99. package/dist/features/workspace/git.js +214 -0
  100. package/dist/features/workspace/installed.js +92 -0
  101. package/dist/features/{git-workspace → workspace}/overlay.js +43 -23
  102. package/dist/features/workspace/packages.js +160 -0
  103. package/dist/features/{git-workspace → workspace}/publish.js +120 -64
  104. package/dist/features/workspace/repo-source.js +192 -0
  105. package/dist/features/{git-workspace → workspace}/setup.js +60 -42
  106. package/dist/features/{git-workspace → workspace}/snapshot.js +53 -18
  107. package/dist/features/workspace/source.js +1 -0
  108. package/dist/features/workspace/tree.js +52 -0
  109. package/dist/features/workspace/workspace-source.js +27 -0
  110. package/dist/harnesses/claude.js +23 -1
  111. package/dist/harnesses/codex.js +57 -1
  112. package/dist/harnesses/cursor.js +22 -1
  113. package/dist/harnesses/detect.js +6 -50
  114. package/dist/harnesses/formats.js +45 -0
  115. package/dist/harnesses/opencode.js +111 -0
  116. package/dist/harnesses/registry.js +35 -2
  117. package/dist/platform.js +375 -0
  118. package/dist/program.js +225 -54
  119. package/dist/runtime.js +1 -0
  120. package/dist/shared/cli-error.js +22 -0
  121. package/dist/shared/constants.js +32 -0
  122. package/dist/shared/files.js +72 -0
  123. package/dist/shared/lock.js +49 -0
  124. package/dist/shared/lookup.js +11 -0
  125. package/dist/shared/patterns.js +94 -0
  126. package/dist/shared/shell.js +22 -0
  127. package/dist/shared/values.js +26 -0
  128. package/dist/ui/add-asset.js +41 -400
  129. package/dist/ui/add-hook.js +213 -0
  130. package/dist/ui/add-mcp.js +107 -0
  131. package/dist/ui/add-prompts.js +47 -0
  132. package/dist/ui/add-skills.js +115 -0
  133. package/dist/ui/asset-list.js +31 -13
  134. package/dist/ui/assets.js +33 -44
  135. package/dist/ui/auth.js +43 -33
  136. package/dist/ui/clash.js +11 -6
  137. package/dist/ui/collaboration.js +116 -44
  138. package/dist/ui/connection.js +10 -4
  139. package/dist/ui/decision-error.js +24 -0
  140. package/dist/ui/environment-onboarding.js +35 -13
  141. package/dist/ui/format.js +66 -18
  142. package/dist/ui/hook-compatibility.js +14 -6
  143. package/dist/ui/mcp.js +28 -3
  144. package/dist/ui/move.js +10 -5
  145. package/dist/ui/onboarding.js +113 -73
  146. package/dist/ui/options.js +9 -0
  147. package/dist/ui/output.js +106 -43
  148. package/dist/ui/pick.js +45 -33
  149. package/dist/ui/scope-install.js +15 -7
  150. package/dist/ui/selection.js +15 -11
  151. package/dist/ui/theme.js +37 -14
  152. package/dist/version.js +6 -3
  153. package/package.json +13 -3
  154. package/dist/features/capabilities/index.js +0 -106
  155. package/dist/features/collaboration/domain.js +0 -90
  156. package/dist/features/environment/cloud-source.js +0 -27
  157. package/dist/features/environment/installed.js +0 -124
  158. package/dist/features/environment/packages.js +0 -120
  159. package/dist/features/environment/remote.js +0 -71
  160. package/dist/features/environment/workspace-source.js +0 -19
  161. package/dist/features/git-hosts/index.js +0 -148
  162. package/dist/features/git-workspace/assets.js +0 -71
  163. package/dist/features/git-workspace/git.js +0 -135
  164. package/dist/features/git-workspace/migrate.js +0 -188
  165. package/dist/features/git-workspace/source.js +0 -159
  166. package/dist/features/imports/mcp.js +0 -219
  167. package/dist/features/knowledge/startup.js +0 -25
  168. package/dist/features/native/mcp-launch.js +0 -57
  169. package/dist/features/skills/install.js +0 -25
  170. package/dist/features/skills/remote.js +0 -177
  171. package/dist/features/skills/sync.js +0 -121
  172. package/dist/features/skills/validation.js +0 -34
  173. /package/dist/features/environment/{source.js → events.js} +0 -0
@@ -1,46 +1,55 @@
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";
1
+ import { basename } from "node:path";
5
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 } 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";
31
15
  /** What is selected and what is actually installed on this machine. */
32
- export async function inventory(home = homedir()) {
16
+ /** The state of a selected asset; the first that applies wins. */
17
+ function inventoryState(asset) {
18
+ if (!asset.enabled) {
19
+ return "disabled";
20
+ }
21
+ if (asset.clashed) {
22
+ return "clash";
23
+ }
24
+ if (asset.modified) {
25
+ return "modified";
26
+ }
27
+ if (!asset.present) {
28
+ return "pending";
29
+ }
30
+ return asset.outdated ? "outdated" : "installed";
31
+ }
32
+ /** The state of an installed asset no longer selected. */
33
+ function leftoverState(modified, offline) {
34
+ if (modified) {
35
+ return "modified";
36
+ }
37
+ return offline ? "installed" : "removing";
38
+ }
39
+ /**
40
+ * Compares configured selections, catalog and installed files to report status;
41
+ * unreachable sources fall back to local state.
42
+ */
43
+ export async function inventory(runtime) {
44
+ const { home } = runtime;
33
45
  const settings = await readSettings(home);
34
- if (!settings)
46
+ if (!settings) {
35
47
  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 ?? {}) : {};
48
+ }
49
+ const state = await readState(home);
50
+ const { native, installed } = state;
51
+ const conn = settings.connection;
52
+ const localNames = settings.localNames ?? {};
44
53
  const linksOf = (id) => state.links.filter((l) => l.id === id);
45
54
  const nativeOf = (id) => native.filter((n) => n.id === id);
46
55
  const items = [];
@@ -48,75 +57,57 @@ export async function inventory(home = homedir()) {
48
57
  // The same comparisons that stop sync, so status can't call these up to date.
49
58
  const modified = new Set();
50
59
  for (const tree of installed) {
51
- const actual = await treeFingerprint(tree.path);
52
- if (tree.fingerprint && actual !== null && actual !== tree.fingerprint)
60
+ const actual = await treeFingerprint(tree.path, runtime);
61
+ if (tree.fingerprint && actual !== null && actual !== tree.fingerprint) {
53
62
  modified.add(tree.id);
63
+ }
54
64
  }
55
65
  for (const link of state.links) {
56
66
  const actual = await linkTarget(link.target);
57
- if (actual !== null && actual !== link.source)
67
+ if (actual !== null && actual !== link.source) {
58
68
  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
  }
69
- catch {
70
- /* the folder may be temporarily unavailable */
71
- }
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
70
  }
88
71
  let offline = null;
89
- const repo = openRepoSource(settings, home, { refresh: "auto" });
72
+ const repo = openRepoSource(settings, runtime, { refresh: "auto" });
90
73
  if (conn || repo) {
91
74
  try {
92
75
  const { catalog: primaryCatalog, scopes: loadScopes } = await (async () => {
93
76
  if (repo) {
94
77
  const model = await repo.model();
95
78
  offline = repo.notices()[0] ?? null;
96
- return { catalog: model.catalog, scopes: async () => model.scopes };
79
+ return {
80
+ catalog: model.catalog,
81
+ scopes: () => Promise.resolve(model.scopes),
82
+ };
97
83
  }
98
- const remote = await environmentCatalog(home, conn);
84
+ const remote = await environmentCatalog(runtime, conn);
99
85
  return {
100
86
  catalog: remote.catalog,
101
- scopes: () => request(remote.connection, "/v1/scopes", z.array(scopeSchema), home),
87
+ scopes: () => scopes(remote.connection, runtime),
102
88
  };
103
89
  })();
104
- const overlay = localOverlay(settings, home);
90
+ const overlay = localOverlay(settings, runtime);
105
91
  const catalog = overlay
106
92
  ? await mergeLocalCatalog(primaryCatalog, overlay)
107
93
  : primaryCatalog;
108
94
  const selection = selectionState(settings);
109
95
  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);
96
+ const outcome = await resolveClashes(effective.assets.flatMap(({ asset, enabled }) => {
97
+ const { kind } = asset;
98
+ if (!enabled || kind === "hook") {
99
+ return [];
100
+ }
101
+ return [
102
+ {
103
+ id: asset.id,
104
+ kind,
105
+ name: asset.name,
106
+ origin: asset.origin,
107
+ installed: linksOf(asset.id).length > 0 || nativeOf(asset.id).length > 0,
108
+ },
109
+ ];
110
+ }), localNames);
120
111
  const clashed = new Set(outcome.skipped.map((c) => c.incoming.id));
121
112
  for (const entry of effective.assets) {
122
113
  const { asset } = entry;
@@ -128,7 +119,7 @@ export async function inventory(home = homedir()) {
128
119
  const harnesses = [
129
120
  ...new Set([
130
121
  ...links.map((l) => l.harness),
131
- ...entries.map((n) => n.harness),
122
+ ...entries.map((n) => n.harness).filter(isHarness),
132
123
  ]),
133
124
  ];
134
125
  items.push({
@@ -140,17 +131,13 @@ export async function inventory(home = homedir()) {
140
131
  via: entry.contributions.map((c) => c.type === "individual"
141
132
  ? "individual"
142
133
  : 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",
134
+ state: inventoryState({
135
+ enabled: entry.enabled,
136
+ clashed: clashed.has(asset.id),
137
+ modified: modified.has(asset.id),
138
+ present,
139
+ outdated: Boolean(tree?.versionId && tree.versionId !== asset.publishedVersion),
140
+ }),
154
141
  harnesses,
155
142
  paths: [
156
143
  ...(tree ? [tree.path] : []),
@@ -159,13 +146,14 @@ export async function inventory(home = homedir()) {
159
146
  ],
160
147
  });
161
148
  }
162
- const scopes = await loadScopes();
163
- for (const item of items)
149
+ const visible = await loadScopes();
150
+ for (const item of items) {
164
151
  item.via = item.via.map((label) => {
165
152
  const [type, id] = label.split("/");
166
- const scope = scopes.find((s) => s.type === type && (type === "org" || s.id === id));
153
+ const scope = visible.find((s) => s.type === type && (type === "org" || s.id === id));
167
154
  return scope ? originLabel(scope) : label;
168
155
  });
156
+ }
169
157
  }
170
158
  catch (error) {
171
159
  offline = error instanceof Error ? error.message : String(error);
@@ -178,19 +166,21 @@ export async function inventory(home = homedir()) {
178
166
  .filter((n) => n.kind !== "knowledge" && n.kind !== "sync")
179
167
  .map((n) => n.id),
180
168
  ])) {
181
- if (seen.has(id))
169
+ if (seen.has(id)) {
182
170
  continue;
171
+ }
183
172
  const links = linksOf(id);
184
173
  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);
174
+ const [link] = links;
175
+ const [entry] = entries;
176
+ const kind = link ? "skill" : entry?.kind;
177
+ if (!isAssetKind(kind)) {
178
+ continue;
179
+ }
180
+ const installedPath = installed.find((t) => t.id === id)?.path;
181
+ const installedFolder = installedPath ? basename(installedPath) : undefined;
182
+ const unlinkedName = kind === "mcp" ? entry?.key : installedFolder;
183
+ const name = link ? basename(link.target) : (unlinkedName ?? id);
194
184
  items.push({
195
185
  id,
196
186
  kind,
@@ -198,11 +188,11 @@ export async function inventory(home = homedir()) {
198
188
  installedName: name,
199
189
  origin: "—",
200
190
  via: [],
201
- state: modified.has(id) ? "modified" : offline ? "installed" : "removing",
191
+ state: leftoverState(modified.has(id), Boolean(offline)),
202
192
  harnesses: [
203
193
  ...new Set([
204
194
  ...links.map((l) => l.harness),
205
- ...entries.map((n) => n.harness),
195
+ ...entries.map((n) => n.harness).filter(isHarness),
206
196
  ]),
207
197
  ],
208
198
  paths: [
@@ -216,8 +206,7 @@ export async function inventory(home = homedir()) {
216
206
  return {
217
207
  harnesses: settings.harnesses,
218
208
  server: conn?.server ?? repo?.label ?? null,
219
- knowledge: !!(conn || repo) &&
220
- (settings.version !== 2 || settings.knowledge !== false),
209
+ knowledge: !!(conn || repo) && settings.knowledge !== false,
221
210
  offline,
222
211
  items,
223
212
  };
@@ -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
+ }