barndog 0.1.0 → 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 +11 -10
  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 +24 -11
  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
@@ -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
+ }