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,254 +1,342 @@
1
+ import { localPath } from "../../platform.js";
1
2
  import { synchronize } from "../environment/sync.js";
2
3
  import { readFile } from "node:fs/promises";
3
- import { homedir } from "node:os";
4
4
  import { z } from "zod";
5
- import { changeSchema } from "@barndog-ai/api-contract";
6
- import { collaborationClient } from "./domain.js";
7
- import { connection, download, getChange, selectSkill } from "./remote.js";
5
+ import { packageApi } from "./api.js";
6
+ import { downloadChange, selectPackage } from "./remote.js";
7
+ import { connection } from "../server/connection.js";
8
8
  import { readSettings } from "../../config/settings.js";
9
9
  import { CliError } from "../../errors.js";
10
10
  import { developers } from "../discovery/index.js";
11
- import { LOCAL_ORIGIN } from "../git-workspace/overlay.js";
12
- import { createGitAsset, gitHistory, gitWorkingStatus, pullGitAsset, pushGitWorkingCopy, resolveGitConflict, updateGitWorkingCopy, } from "./workspace-git.js";
13
- import { NoWorkingCopyError, pullSkill, createWorkspace, workspaceStatus, updateWorkspace, resolveConflict, pushWorkspace, } from "./workspace.js";
11
+ import { LOCAL_ORIGIN } from "../workspace/overlay.js";
12
+ import { createGitAsset, gitVersionDiff, gitVersionFiles, gitVersions, gitWorkingStatus, pullGitAsset, pushGitWorkingCopy, resolveGitConflict, updateGitWorkingCopy, } from "./workspace-git.js";
13
+ import { NoWorkingCopyError, pullCloudPackage, createWorkspace, workspaceStatus, updateWorkspace, resolveConflict, pushWorkspace, } from "./workspace.js";
14
+ import { allPages } from "../server/pages.js";
15
+ import { changeAssetId } from "./results.js";
14
16
  /** Who an ID is, when the server will say; IDs still print if it won't. */
15
- const developerNames = (home) => developers(undefined, home).then((people) => Object.fromEntries(people.map((p) => [p.id, p.name])), () => ({}));
17
+ const developerNames = (runtime) => {
18
+ return developers(undefined, runtime).then((people) => Object.fromEntries(people.map((p) => [p.id, p.name])), () => ({}));
19
+ };
16
20
  /**
17
21
  * A text file's closing newline isn't part of the selected lines. With a known line
18
22
  * count it's kept when it really is content: a range that ends on an empty line.
19
23
  */
20
- export function selectedText(text, lines) {
21
- if (!text.endsWith("\n"))
24
+ function selectedText(text, lines) {
25
+ if (!text.endsWith("\n")) {
22
26
  return text;
27
+ }
23
28
  return lines === undefined || text.split("\n").length > lines
24
29
  ? text.slice(0, -1)
25
30
  : text;
26
31
  }
27
- const preview = async (action, home) => action === "status"
28
- ? { mode: "environment" }
29
- : {
30
- mode: "environment-preview",
31
- events: await synchronize(undefined, home, undefined, {
32
- dryRun: true,
33
- }),
34
- };
35
- async function collaborateLocal(path, action, home) {
36
- if (action === "status" || action === "diff")
37
- return preview(action, home);
38
- throw new CliError(`Local assets have no review step. Edit them directly in ${path}, then sync.`, "barndog sync");
39
- }
40
- async function collaborateGit(workspace, action, options, cwd, home) {
41
- const asset = {
42
- name: options.name,
43
- origin: options.origin,
44
- target: options.directory,
45
- kind: options.kind,
46
- };
47
- switch (action) {
48
- case "create":
49
- return createGitAsset(workspace, asset, cwd, home);
50
- case "pull-skill":
51
- return pullGitAsset(workspace, asset, cwd, home);
52
- case "pull":
53
- return updateGitWorkingCopy(cwd, home);
54
- case "status":
55
- case "diff":
56
- try {
57
- const working = await gitWorkingStatus(cwd, home);
58
- return action === "status" ? working : working.changes;
59
- }
60
- catch (error) {
61
- if (!(error instanceof NoWorkingCopyError))
62
- throw error;
63
- return preview(action, home);
64
- }
65
- case "push":
66
- return pushGitWorkingCopy(workspace, options, cwd, home);
67
- case "resolve":
68
- return resolveGitConflict(options.path, cwd, home);
69
- case "versions":
70
- case "version-show":
71
- case "version-diff":
72
- return gitHistory(workspace, action, { ...options, ...asset }, home);
73
- default:
74
- throw new CliError("Reviews happen as pull requests on your Git host in a Git repo workspace.", "barndog status");
75
- }
76
- }
77
- export async function collaborate(action, options = {}, cwd = process.cwd(), home = homedir()) {
32
+ async function backend(runtime, origin) {
33
+ const { home } = runtime;
78
34
  const settings = await readSettings(home);
79
- if (settings?.version === 2 && settings.gitWorkspace)
80
- return collaborateGit(settings.gitWorkspace, action, options, cwd, home);
81
- // A Cloud machine can keep a local folder too; only its own assets stay out of review.
82
- if (settings?.version === 2 &&
35
+ if (settings?.gitWorkspace) {
36
+ return { type: "git", workspace: settings.gitWorkspace };
37
+ }
38
+ if (settings &&
83
39
  settings.localWorkspace &&
84
- (!settings.connection || options.origin === LOCAL_ORIGIN))
85
- return collaborateLocal(settings.localWorkspace.path, action, home);
86
- switch (action) {
87
- case "create":
88
- return createWorkspace(options.name, options.origin, options.directory, cwd, home, options.kind);
89
- case "pull-skill":
90
- return pullSkill(options.name, options.origin, options.directory, cwd, home, options.kind);
91
- case "pull":
92
- return updateWorkspace(cwd, home);
93
- case "status":
94
- case "diff": {
95
- try {
96
- const working = await workspaceStatus(cwd, home);
97
- return action === "status" ? working : working.changes;
98
- }
99
- catch (error) {
100
- if (!(error instanceof NoWorkingCopyError))
101
- throw error;
102
- return action === "status"
103
- ? { mode: "environment" }
104
- : {
105
- mode: "environment-preview",
106
- events: await synchronize(undefined, home, undefined, {
107
- dryRun: true,
108
- }),
109
- };
110
- }
111
- }
112
- case "push":
113
- return pushWorkspace(options, cwd, home);
114
- case "resolve":
115
- return resolveConflict(options.path, cwd, home);
116
- }
117
- const conn = await connection(home), client = collaborationClient(conn.server, home, options.kind);
118
- if (action === "list") {
119
- const result = [];
120
- let cursor;
121
- const seen = new Set();
122
- do {
123
- const page = (await client.GET("/v1/skill-changes", { params: { query: { cursor } } })).data;
124
- result.push(...page.changes.map((c) => changeSchema.parse(c)));
125
- if (page.nextCursor && seen.has(page.nextCursor))
126
- throw new Error("Invalid pagination");
127
- if (page.nextCursor)
128
- seen.add(page.nextCursor);
129
- cursor = page.nextCursor ?? undefined;
130
- } while (cursor);
131
- return result;
132
- }
133
- if (["versions", "version-show", "version-diff"].includes(action)) {
134
- const skill = await selectSkill(options.name, options.origin, conn, home, options.kind);
135
- if (action === "versions") {
136
- const [rows, names] = await Promise.all([
137
- client.GET("/v1/skills/{id}/versions", {
138
- params: { path: { id: skill.id } },
139
- }),
140
- developerNames(home),
141
- ]);
142
- return rows.data.map((row) => ({
143
- ...row,
144
- publisherName: names[row.publishedBy],
145
- }));
40
+ (!settings.connection || origin === LOCAL_ORIGIN)) {
41
+ return { type: "local", path: settings.localWorkspace.path };
42
+ }
43
+ return { type: "cloud" };
44
+ }
45
+ const noLocalReview = (path) => new CliError(`Local assets have no review step. Edit them directly in ${path}, then sync.`, "barndog sync");
46
+ async function cloudConnection(runtime, origin) {
47
+ const where = await backend(runtime, origin);
48
+ if (where.type === "git") {
49
+ throw new CliError("Reviews happen as pull requests on your Git host in a Git repo workspace.", "barndog status");
50
+ }
51
+ if (where.type === "local") {
52
+ throw noLocalReview(where.path);
53
+ }
54
+ return connection(runtime);
55
+ }
56
+ /**
57
+ * `create` for a new Skill or Hook: a working copy from the kind's scaffold, in
58
+ * Git or Cloud as the workspace is set up. Local workspaces have no review
59
+ * step, so they are refused with a fix.
60
+ */
61
+ export async function createPackage(target, runtime) {
62
+ const where = await backend(runtime, target.origin);
63
+ if (where.type === "local") {
64
+ throw noLocalReview(where.path);
65
+ }
66
+ if (where.type === "git") {
67
+ return createGitAsset(where.workspace, { ...target, target: target.directory }, runtime);
68
+ }
69
+ return createWorkspace({ name: target.name, origin: target.origin, kind: target.kind }, { target: target.directory, runtime });
70
+ }
71
+ /**
72
+ * `pull <name>`: a new working folder holding the published asset, in Git or
73
+ * Cloud as the workspace is set up. Refused in a local workspace.
74
+ */
75
+ export async function pullPackage(target, runtime) {
76
+ const where = await backend(runtime, target.origin);
77
+ if (where.type === "local") {
78
+ throw noLocalReview(where.path);
79
+ }
80
+ if (where.type === "git") {
81
+ return pullGitAsset(where.workspace, { ...target, target: target.directory }, runtime);
82
+ }
83
+ return pullCloudPackage({ name: target.name, origin: target.origin, kind: target.kind }, { target: target.directory, runtime });
84
+ }
85
+ /**
86
+ * `pull` inside a working copy: merges what changed remotely into the folder.
87
+ * Refused in a local workspace.
88
+ */
89
+ export async function updateWorkingCopy(runtime) {
90
+ const where = await backend(runtime);
91
+ if (where.type === "local") {
92
+ throw noLocalReview(where.path);
93
+ }
94
+ return where.type === "git"
95
+ ? updateGitWorkingCopy(runtime)
96
+ : updateWorkspace(runtime);
97
+ }
98
+ /**
99
+ * `status` inside a working copy; anywhere else, or in a local workspace, says
100
+ * to describe the machine-wide environment instead.
101
+ */
102
+ export async function workingStatus(runtime) {
103
+ const where = await backend(runtime);
104
+ if (where.type === "local") {
105
+ return { mode: "environment" };
106
+ }
107
+ try {
108
+ return where.type === "git"
109
+ ? await gitWorkingStatus(runtime)
110
+ : await workspaceStatus(runtime);
111
+ }
112
+ catch (error) {
113
+ if (!(error instanceof NoWorkingCopyError)) {
114
+ throw error;
146
115
  }
147
- if (action === "version-show")
148
- return (await client.GET("/v1/skills/{id}/content", {
149
- params: {
150
- path: { id: skill.id },
151
- query: { version: options.version ?? skill.version.number },
152
- },
153
- })).data;
154
- return (await client.GET("/v1/skills/{id}/diff", {
155
- params: {
156
- path: { id: skill.id },
157
- query: { from: options.from, to: options.to },
158
- },
159
- })).data;
160
- }
161
- const id = z.uuid().parse(options.id), params = { path: { id } };
162
- if (action === "show") {
163
- const change = await getChange(id, conn, home, options.kind);
164
- const [files, names, versions] = await Promise.all([
165
- download(`/v1/skill-changes/${id}/package?revision=${change.revision}`, conn, home, change.commit, options.kind),
166
- developerNames(home),
167
- change.baseVersionId
168
- ? client
169
- .GET("/v1/skills/{id}/versions", {
170
- params: { path: { id: change.skillId } },
171
- })
172
- .then((result) => result.data ?? [], () => [])
173
- : [],
174
- ]);
175
- return {
176
- ...change,
177
- names,
178
- baseVersionNumber: versions.find((v) => v.id === change.baseVersionId)
179
- ?.number,
180
- files: files.map((e) => ({
181
- path: e.path,
182
- content: e.data.toString("base64"),
183
- executable: e.executable,
184
- })),
185
- };
186
- }
187
- if (action === "change-diff")
188
- return (await client.GET("/v1/skill-changes/{id}/diff", {
189
- params: {
190
- ...params,
191
- query: {
192
- revision: options.revision,
193
- fromRevision: options.fromRevision,
194
- },
195
- },
196
- })).data;
197
- const expectedRevision = z.number().int().positive().parse(options.revision), body = { expectedRevision };
198
- if (action === "approve" || action === "request-changes")
199
- return (await client.POST("/v1/skill-changes/{id}/reviews", {
200
- params,
201
- body: {
202
- ...body,
203
- decision: action === "approve" ? "approve" : "request_changes",
204
- },
205
- })).data;
206
- if (action === "reject")
207
- return (await client.POST("/v1/skill-changes/{id}/reject", { params, body })).data;
208
- if (action === "merge")
209
- return (await client.POST("/v1/skill-changes/{id}/merge", { params, body }))
210
- .data;
211
- if (action === "comment") {
212
- const text = options.bodyFile
213
- ? await readFile(options.bodyFile, "utf8")
214
- : options.body;
215
- if (!text?.trim())
216
- throw new Error("Supply --body <text> or --body-file <path>");
217
- if (options.thread)
218
- return (await client.POST("/v1/skill-changes/{id}/discussions/{threadId}/comments", {
219
- params: { path: { id, threadId: z.uuid().parse(options.thread) } },
220
- body: { ...body, body: text },
221
- })).data;
222
- return (await client.POST("/v1/skill-changes/{id}/discussions", {
223
- params,
224
- body: {
225
- ...body,
226
- body: text,
227
- path: options.path,
228
- side: options.side,
229
- startLine: options.startLine,
230
- endLine: options.endLine,
231
- replacement: options.replacementFile
232
- ? selectedText(await readFile(options.replacementFile, "utf8"))
233
- : undefined,
234
- expectedContent: options.expectedFile
235
- ? selectedText(await readFile(options.expectedFile, "utf8"), options.startLine && options.endLine
236
- ? options.endLine - options.startLine + 1
237
- : undefined)
238
- : undefined,
239
- },
240
- })).data;
241
- }
242
- const threadParams = {
243
- path: { id, threadId: z.uuid().parse(options.thread) },
116
+ return { mode: "environment" };
117
+ }
118
+ }
119
+ /**
120
+ * Returns working-copy edits, or outside a working copy, planned sync events.
121
+ * The dry-run path can still fetch and populate caches.
122
+ */
123
+ export async function workingDiff(runtime) {
124
+ const status = await workingStatus(runtime);
125
+ if ("changes" in status) {
126
+ return status.changes;
127
+ }
128
+ return {
129
+ mode: "environment-preview",
130
+ events: await synchronize(undefined, runtime, undefined, { dryRun: true }),
244
131
  };
245
- if (action === "apply")
246
- return (await client.POST("/v1/skill-changes/{id}/discussions/{threadId}/apply", {
247
- params: threadParams,
248
- body,
249
- })).data;
250
- return (await client.POST("/v1/skill-changes/{id}/discussions/{threadId}/resolve", {
251
- params: threadParams,
252
- body: { ...body, resolved: action === "resolve-discussion" },
253
- })).data;
132
+ }
133
+ /**
134
+ * `push` inside a working copy, to a review branch or proposal as the workspace
135
+ * is set up. Refused in a local workspace.
136
+ */
137
+ export async function pushWorkingCopy(options, runtime) {
138
+ const where = await backend(runtime, options.origin);
139
+ if (where.type === "local") {
140
+ throw noLocalReview(where.path);
141
+ }
142
+ return where.type === "git"
143
+ ? pushGitWorkingCopy(where.workspace, options, runtime)
144
+ : pushWorkspace(options, runtime);
145
+ }
146
+ /**
147
+ * `resolve <path>`: marks one conflicted file resolved once its markers are
148
+ * gone. `path` is relative to `cwd`.
149
+ */
150
+ export async function resolveFile(path, runtime) {
151
+ const where = await backend(runtime);
152
+ if (where.type === "local") {
153
+ throw noLocalReview(where.path);
154
+ }
155
+ return where.type === "git"
156
+ ? resolveGitConflict(path, runtime)
157
+ : resolveConflict(path, runtime);
158
+ }
159
+ async function publishedPackage(target, runtime) {
160
+ const where = await backend(runtime, target.origin);
161
+ if (where.type === "local") {
162
+ throw noLocalReview(where.path);
163
+ }
164
+ if (where.type === "git") {
165
+ return where;
166
+ }
167
+ const conn = await connection(runtime);
168
+ return {
169
+ type: "cloud",
170
+ api: packageApi(target.kind, conn, runtime),
171
+ published: await selectPackage({ name: target.name, selector: target.origin, kind: target.kind }, conn, runtime),
172
+ };
173
+ }
174
+ /** Newest last. */
175
+ export async function packageVersions(target, runtime) {
176
+ const where = await publishedPackage(target, runtime);
177
+ if (where.type === "git") {
178
+ return gitVersions(where.workspace, target, runtime);
179
+ }
180
+ const [rows, names] = await Promise.all([
181
+ where.api.versions(where.published.id),
182
+ developerNames(runtime),
183
+ ]);
184
+ return rows.map((row) => ({
185
+ ...row,
186
+ publisherName: names[row.publishedBy],
187
+ }));
188
+ }
189
+ /**
190
+ * A published version's files, base64-encoded; the newest when `version` is
191
+ * left out.
192
+ */
193
+ export async function versionFiles(target, runtime) {
194
+ const where = await publishedPackage(target, runtime);
195
+ if (where.type === "git") {
196
+ return gitVersionFiles(where.workspace, target, runtime);
197
+ }
198
+ return where.api.content(where.published.id, target.version ?? where.published.version.number);
199
+ }
200
+ export async function diffVersions(target, runtime) {
201
+ const where = await publishedPackage(target, runtime);
202
+ if (where.type === "git") {
203
+ return gitVersionDiff(where.workspace, target, runtime);
204
+ }
205
+ return where.api.diff(where.published.id, {
206
+ from: target.from,
207
+ to: target.to,
208
+ });
209
+ }
210
+ /**
211
+ * Every proposal the developer can see, following the server's pages. Cloud
212
+ * only: a Git workspace reviews in pull requests on its host.
213
+ */
214
+ export async function listChanges({ kind }, runtime) {
215
+ const api = packageApi(kind, await cloudConnection(runtime), runtime);
216
+ return allPages((cursor) => api.changes(cursor));
217
+ }
218
+ /**
219
+ * A proposal with its files (base64-encoded), its base version number and
220
+ * developer names by ID. Cloud only.
221
+ */
222
+ export async function showChange(target, runtime) {
223
+ const conn = await cloudConnection(runtime);
224
+ const id = z.uuid().parse(target.id);
225
+ const { kind } = target;
226
+ const api = packageApi(kind, conn, runtime);
227
+ const change = await api.change(id);
228
+ const [files, names, versions] = await Promise.all([
229
+ downloadChange({ id, revision: change.revision, commit: change.commit }, conn, { runtime, kind }),
230
+ developerNames(runtime),
231
+ // The base number is a nicety; show the change without it if this fails.
232
+ change.baseVersionId
233
+ ? api.versions(changeAssetId(change)).catch(() => [])
234
+ : [],
235
+ ]);
236
+ return {
237
+ ...change,
238
+ names,
239
+ baseVersionNumber: versions.find((v) => v.id === change.baseVersionId)
240
+ ?.number,
241
+ files: files.map((e) => ({
242
+ path: e.path,
243
+ content: e.data.toString("base64"),
244
+ executable: e.executable,
245
+ })),
246
+ };
247
+ }
248
+ /**
249
+ * A proposal's diff at a revision (the newest by default), against
250
+ * `fromRevision` or the version it started from. Cloud only.
251
+ */
252
+ export async function changeDiff(target, runtime) {
253
+ const api = packageApi(target.kind, await cloudConnection(runtime), runtime);
254
+ return api.changeDiff(z.uuid().parse(target.id), {
255
+ revision: target.revision,
256
+ fromRevision: target.fromRevision,
257
+ });
258
+ }
259
+ async function changeAtRevision(target, runtime) {
260
+ return {
261
+ api: packageApi(target.kind, await cloudConnection(runtime), runtime),
262
+ id: z.uuid().parse(target.id),
263
+ body: {
264
+ expectedRevision: z.number().int().positive().parse(target.revision),
265
+ },
266
+ };
267
+ }
268
+ /**
269
+ * Approves or requests changes on the revision the reviewer inspected. Cloud
270
+ * only.
271
+ */
272
+ export async function reviewChange(decision, target, runtime) {
273
+ const { api, id, body } = await changeAtRevision(target, runtime);
274
+ return api.review(id, {
275
+ ...body,
276
+ decision: decision === "approve" ? "approve" : "request_changes",
277
+ });
278
+ }
279
+ /**
280
+ * Closes a proposal without publishing it; the author needs `push --new` to
281
+ * propose again. Cloud only.
282
+ */
283
+ export async function rejectChange(target, runtime) {
284
+ const { api, id, body } = await changeAtRevision(target, runtime);
285
+ return api.reject(id, body);
286
+ }
287
+ /**
288
+ * Publishes a proposal at the inspected revision, as its scope's review
289
+ * settings allow. Cloud only.
290
+ */
291
+ export async function mergeChange(target, runtime) {
292
+ const { api, id, body } = await changeAtRevision(target, runtime);
293
+ return api.merge(id, body);
294
+ }
295
+ /**
296
+ * Adds a comment to a proposal and returns the updated proposal. Reads
297
+ * `bodyFile`, `replacementFile` and `expectedFile` from disk; a file's closing
298
+ * newline isn't part of the selected lines. Cloud only.
299
+ */
300
+ export async function commentOnChange(options, runtime) {
301
+ const { api, id, body } = await changeAtRevision(options, runtime);
302
+ const text = options.bodyFile
303
+ ? await readFile(localPath(options.bodyFile, runtime), "utf8")
304
+ : options.body;
305
+ if (!text?.trim()) {
306
+ throw new CliError("Add the comment with --body <text> or --body-file <path>.");
307
+ }
308
+ if (options.thread) {
309
+ return api.comment(id, z.uuid().parse(options.thread), {
310
+ ...body,
311
+ body: text,
312
+ });
313
+ }
314
+ return api.discuss(id, {
315
+ ...body,
316
+ body: text,
317
+ path: options.path,
318
+ side: options.side,
319
+ startLine: options.startLine,
320
+ endLine: options.endLine,
321
+ replacement: options.replacementFile
322
+ ? selectedText(await readFile(localPath(options.replacementFile, runtime), "utf8"))
323
+ : undefined,
324
+ expectedContent: options.expectedFile
325
+ ? selectedText(await readFile(localPath(options.expectedFile, runtime), "utf8"), options.startLine && options.endLine
326
+ ? options.endLine - options.startLine + 1
327
+ : undefined)
328
+ : undefined,
329
+ });
330
+ }
331
+ /** Creates a new revision; the author runs `barndog pull` to receive it. */
332
+ export async function applySuggestion(target, runtime) {
333
+ const { api, id, body } = await changeAtRevision(target, runtime);
334
+ return api.applySuggestion(id, z.uuid().parse(target.thread), body);
335
+ }
336
+ export async function setDiscussionResolved(target, runtime) {
337
+ const { api, id, body } = await changeAtRevision(target, runtime);
338
+ return api.resolve(id, z.uuid().parse(target.thread), {
339
+ ...body,
340
+ resolved: target.resolved,
341
+ });
254
342
  }