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
@@ -0,0 +1,263 @@
1
+ import { basename } from "node:path";
2
+ import { mcpSchema, knowledgeDocumentSchema, knowledgeIndexSchema, } from "@barndog-ai/api-contract";
3
+ import { selectionState, withSelection } from "../../config/environment.js";
4
+ import { readSettings, saveSettings, SetupRequiredError, } from "../../config/settings.js";
5
+ import { CliError } from "../../errors.js";
6
+ import { normalizeServer, apiClient } from "../auth/transport.js";
7
+ import { packageApi } from "../collaboration/api.js";
8
+ import { archive } from "../collaboration/workspace.js";
9
+ import { encodeFiles, } from "../collaboration/workspace-store.js";
10
+ import { originLabel, scopes } from "../server/origin.js";
11
+ import { environmentCatalog } from "../server/catalog.js";
12
+ import { parsed } from "../server/client.js";
13
+ import { synchronize } from "../environment/sync.js";
14
+ import { locked } from "../environment/transaction.js";
15
+ import { openRepoSource } from "../workspace/workspace-source.js";
16
+ import { connectionContext } from "../auth/session.js";
17
+ import { localOverlay } from "../workspace/overlay.js";
18
+ import { allPages, PAGE_SIZE } from "../server/pages.js";
19
+ import { defined } from "../../shared/lookup.js";
20
+ const sameScope = (cloud, local) => cloud.type === local.type &&
21
+ (local.type === "org" || cloud.slug === local.slug);
22
+ /**
23
+ * Publishes a Skill or Hook to a Cloud scope by proposing it and merging the
24
+ * proposal at once. The retry key comes from the asset and version, so re-
25
+ * running after a failure repeats the upload instead of duplicating it.
26
+ */
27
+ export async function publishPackage(asset, scope, conn, runtime) {
28
+ const api = packageApi(asset.kind, conn, runtime);
29
+ const files = encodeFiles(asset.entries);
30
+ const metadata = {
31
+ name: asset.name,
32
+ origin: { type: scope.type, id: scope.id },
33
+ baseVersionId: null,
34
+ title: `Migrate ${asset.name} from Git`,
35
+ // Stable per content, so re-running after a failure repeats rather than duplicates.
36
+ idempotencyKey: `migrate-${asset.id}-${asset.publishedVersion}-${scope.id}-${asset.name}`,
37
+ };
38
+ const change = await api.submit(metadata, await archive(files, runtime));
39
+ await api.merge(change.id, { expectedRevision: change.submittedRevision });
40
+ }
41
+ /**
42
+ * Publishes an MCP configuration to a Cloud scope, with a retry key from the
43
+ * asset and version.
44
+ */
45
+ export const publishMcp = (asset, scope, conn, runtime) => {
46
+ return parsed(mcpSchema, apiClient(conn.server, runtime).POST("/v1/mcps", {
47
+ body: {
48
+ name: asset.name,
49
+ origin: { type: scope.type, id: scope.id },
50
+ definition: defined(asset.definition, `The MCP configuration ${asset.name} has no definition`),
51
+ idempotencyKey: `migrate-${asset.id}-${asset.publishedVersion}-${scope.id}-${asset.name}`,
52
+ },
53
+ }));
54
+ };
55
+ export const publishKnowledge = (doc, conn, runtime) => {
56
+ return parsed(knowledgeDocumentSchema, apiClient(conn.server, runtime).POST("/v1/knowledge/publish", {
57
+ body: {
58
+ baseVersionId: null,
59
+ content: doc.content,
60
+ idempotencyKey: `migrate-${doc.id}-${doc.version}-${doc.content.name}`,
61
+ },
62
+ }));
63
+ };
64
+ function cloudIds(scopes, assets, target) {
65
+ return (id) => {
66
+ const asset = assets.find((a) => a.id === id);
67
+ const pair = asset && scopes.find((s) => s.local.id === asset.origin.id);
68
+ return target.find((t) => t.kind === asset?.kind &&
69
+ t.name === asset.name &&
70
+ t.origin.type === pair?.cloud.type &&
71
+ t.origin.id === pair.cloud.id)?.id;
72
+ };
73
+ }
74
+ /**
75
+ * Moves local renames into `next`: each follows its asset to its Cloud ID, and
76
+ * a kept local folder's assets keep theirs. A rename whose asset is in neither
77
+ * is dropped, since nothing would carry that name.
78
+ */
79
+ function carryNames(next, names = {}, cloudId, kept) {
80
+ const carried = Object.fromEntries(Object.entries(names).flatMap(([id, name]) => {
81
+ if (kept?.assets.some((asset) => asset.id === id)) {
82
+ return [[id, name]];
83
+ }
84
+ const translated = cloudId(id);
85
+ return translated ? [[translated, name]] : [];
86
+ }));
87
+ delete next.localNames;
88
+ if (Object.keys(carried).length) {
89
+ next.localNames = carried;
90
+ }
91
+ }
92
+ /**
93
+ * Knowledge is organization-wide. A broken document blocks the move like a
94
+ * broken asset, so nothing is left behind.
95
+ */
96
+ function planKnowledge(model, existing) {
97
+ const rows = model.invalidKnowledge.map((broken) => ({
98
+ kind: "knowledge",
99
+ name: basename(broken.path, ".md"),
100
+ origin: "org",
101
+ action: "invalid",
102
+ detail: `${broken.path}: ${broken.reason}`,
103
+ }));
104
+ const publish = [];
105
+ for (const doc of model.knowledge) {
106
+ const base = { kind: "knowledge", name: doc.content.name, origin: "org" };
107
+ if (existing.some((d) => d.name === doc.content.name)) {
108
+ rows.push({ ...base, action: "exists" });
109
+ }
110
+ else {
111
+ rows.push({ ...base, action: "publish" });
112
+ publish.push(doc);
113
+ }
114
+ }
115
+ return { rows, publish };
116
+ }
117
+ /** Carries each choice over by scope and name, because cloud assigns new IDs. */
118
+ function translateSelection(state, scopes, assetId) {
119
+ const ids = (values) => values.flatMap((id) => assetId(id) ?? []);
120
+ return {
121
+ subscriptions: state.subscriptions.flatMap((subscription) => {
122
+ const pair = scopes.find((s) => s.local.id === subscription.scope.id);
123
+ return pair && pair.cloud.type !== "dev"
124
+ ? [
125
+ {
126
+ ...subscription,
127
+ scope: { type: pair.cloud.type, id: pair.cloud.id },
128
+ },
129
+ ]
130
+ : [];
131
+ }),
132
+ individualAssetIds: ids(state.individualAssetIds),
133
+ excludedAssetIds: ids(state.excludedAssetIds),
134
+ disabledAssetIds: ids(state.disabledAssetIds),
135
+ };
136
+ }
137
+ /** A kept local folder isn't part of the move, so its choices stay as they were. */
138
+ function keepLocalChoices(next, before, kept) {
139
+ if (!kept) {
140
+ return next;
141
+ }
142
+ const local = (ids) => ids.filter((id) => kept.assets.some((asset) => asset.id === id));
143
+ return {
144
+ subscriptions: [
145
+ ...next.subscriptions,
146
+ ...before.subscriptions.filter((s) => s.scope.id === kept.organizationId),
147
+ ],
148
+ individualAssetIds: [
149
+ ...next.individualAssetIds,
150
+ ...local(before.individualAssetIds),
151
+ ],
152
+ excludedAssetIds: [
153
+ ...next.excludedAssetIds,
154
+ ...local(before.excludedAssetIds),
155
+ ],
156
+ disabledAssetIds: [
157
+ ...next.disabledAssetIds,
158
+ ...local(before.disabledAssetIds),
159
+ ],
160
+ };
161
+ }
162
+ /**
163
+ * `migrate --to <server>`: publishes every asset and knowledge document of the
164
+ * Git workspace to Cloud, then switches this machine's settings to the server,
165
+ * translating the selection to Cloud IDs. Publishes nothing while any asset is
166
+ * invalid or lacks its scope in Cloud. Takes the sync lock.
167
+ */
168
+ export async function migrate(options, runtime) {
169
+ const { home } = runtime;
170
+ return locked(runtime, async () => {
171
+ const settings = await readSettings(home);
172
+ if (!settings) {
173
+ throw new SetupRequiredError();
174
+ }
175
+ const source = openRepoSource(settings, runtime);
176
+ if (!settings.gitWorkspace &&
177
+ settings.localWorkspace &&
178
+ !settings.connection) {
179
+ throw new CliError("`barndog migrate` moves a team's Git repo. To bring local assets to cloud, switch first: they stay active, and you can then move them with barndog local move.", "barndog init --server <url>");
180
+ }
181
+ if (!source) {
182
+ throw new CliError("This machine isn't using a Git repo, so there's nothing to migrate.");
183
+ }
184
+ const overlay = localOverlay(settings, runtime);
185
+ const kept = overlay ? await overlay.model() : undefined;
186
+ const model = await source.model();
187
+ const server = normalizeServer(options.server);
188
+ const conn = await connectionContext(server, runtime);
189
+ const cloudScopes = await scopes(conn, runtime);
190
+ const { catalog: existing } = await environmentCatalog(runtime, conn);
191
+ const existingDocs = await allPages(async (cursor) => {
192
+ const page = await parsed(knowledgeIndexSchema, apiClient(conn.server, runtime).GET("/v1/knowledge", {
193
+ params: { query: { limit: PAGE_SIZE, cursor } },
194
+ }));
195
+ return { items: page.documents, nextCursor: page.nextCursor };
196
+ });
197
+ const pairs = model.scopes.flatMap((local) => {
198
+ const cloud = cloudScopes.find((scope) => sameScope(scope, local));
199
+ return cloud ? [{ local, cloud }] : [];
200
+ });
201
+ const rows = [];
202
+ const pending = [];
203
+ for (const asset of model.assets) {
204
+ const base = {
205
+ kind: asset.kind,
206
+ name: asset.name,
207
+ origin: originLabel(asset.origin),
208
+ };
209
+ const pair = pairs.find((p) => p.local.id === asset.origin.id);
210
+ if (asset.invalid) {
211
+ rows.push({ ...base, action: "invalid", detail: asset.invalid });
212
+ }
213
+ else if (!pair) {
214
+ rows.push({
215
+ ...base,
216
+ action: "missing-scope",
217
+ detail: `Create ${originLabel(asset.origin)} in the workspace first`,
218
+ });
219
+ }
220
+ else if (existing.assets.some((a) => a.kind === asset.kind &&
221
+ a.name === asset.name &&
222
+ a.origin.type === pair.cloud.type &&
223
+ a.origin.id === pair.cloud.id)) {
224
+ rows.push({ ...base, action: "exists" });
225
+ }
226
+ else {
227
+ rows.push({ ...base, action: "publish" });
228
+ pending.push(() => asset.kind === "mcp"
229
+ ? publishMcp(asset, pair.cloud, conn, runtime)
230
+ : publishPackage({ ...asset, kind: asset.kind }, pair.cloud, conn, runtime));
231
+ }
232
+ }
233
+ const knowledge = planKnowledge(model, existingDocs);
234
+ rows.push(...knowledge.rows);
235
+ for (const doc of knowledge.publish) {
236
+ pending.push(() => publishKnowledge(doc, conn, runtime));
237
+ }
238
+ if (options.dryRun) {
239
+ return { server, dryRun: true, rows, migrated: false };
240
+ }
241
+ const blocked = rows.filter((row) => row.action === "invalid" || row.action === "missing-scope");
242
+ if (blocked.length) {
243
+ throw new CliError(`Nothing was published. Fix these first:\n${blocked.map((row) => ` ${row.origin}/${row.name}: ${row.detail}`).join("\n")}`, `barndog migrate --to ${server} --dry-run`);
244
+ }
245
+ for (const publish of pending) {
246
+ await publish();
247
+ }
248
+ const { catalog } = await environmentCatalog(runtime, conn);
249
+ const cloudId = cloudIds(pairs, model.assets, catalog.assets);
250
+ const next = withSelection({
251
+ ...settings,
252
+ connection: conn,
253
+ gitWorkspace: undefined,
254
+ }, keepLocalChoices(translateSelection(selectionState(settings), pairs, cloudId), selectionState(settings), kept));
255
+ delete next.gitWorkspace;
256
+ carryNames(next, settings.localNames, cloudId, kept);
257
+ await saveSettings(next, home);
258
+ // Not the Cloud catalog fetched above: a kept local folder's assets are
259
+ // selected too, and only the full source (Cloud plus that folder) has them.
260
+ await synchronize(undefined, runtime, next);
261
+ return { server, dryRun: false, rows, migrated: true };
262
+ });
263
+ }