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.
- package/README.md +8 -7
- package/dist/cli.js +11 -7
- package/dist/commands/add.js +33 -21
- package/dist/commands/arguments.js +12 -0
- package/dist/commands/asset-install.js +160 -0
- package/dist/commands/asset-list.js +121 -0
- package/dist/commands/asset-share.js +94 -0
- package/dist/commands/auth.js +4 -0
- package/dist/commands/availability.js +54 -26
- package/dist/commands/capabilities.js +71 -415
- package/dist/commands/collaboration.js +194 -110
- package/dist/commands/environment.js +18 -411
- package/dist/commands/github.js +200 -0
- package/dist/commands/helpers.js +39 -0
- package/dist/commands/hooks.js +87 -0
- package/dist/commands/init.js +4 -2
- package/dist/commands/knowledge.js +73 -0
- package/dist/commands/mcp.js +62 -0
- package/dist/commands/origin.js +21 -23
- package/dist/commands/people.js +51 -0
- package/dist/commands/sets.js +141 -0
- package/dist/commands/status.js +34 -24
- package/dist/commands/sync.js +11 -4
- package/dist/commands/workspace.js +33 -19
- package/dist/config/environment.js +13 -27
- package/dist/config/paths.js +62 -0
- package/dist/config/selection.js +30 -0
- package/dist/config/settings.js +97 -108
- package/dist/config/state.js +57 -57
- package/dist/entry/knowledge-startup.js +32 -0
- package/dist/entry/mcp-launch.js +71 -0
- package/dist/entry/opencode-plugin.js +62 -0
- package/dist/{features/git-workspace/startup.js → entry/workspace-startup.js} +7 -2
- package/dist/errors.js +198 -55
- package/dist/features/asset-kinds/index.js +120 -0
- package/dist/features/asset-selection/index.js +51 -19
- package/dist/features/auth/credentials.js +80 -47
- package/dist/features/auth/session.js +80 -45
- package/dist/features/auth/transport.js +66 -15
- package/dist/features/catalog/index.js +32 -61
- package/dist/features/collaboration/api.js +160 -0
- package/dist/features/collaboration/index.js +321 -233
- package/dist/features/collaboration/remote.js +35 -115
- package/dist/features/collaboration/results.js +2 -0
- package/dist/features/collaboration/workspace-git.js +233 -149
- package/dist/features/collaboration/workspace-github.js +117 -54
- package/dist/features/collaboration/workspace-store.js +140 -100
- package/dist/features/collaboration/workspace.js +257 -172
- package/dist/features/discovery/index.js +68 -57
- package/dist/features/doctor/index.js +32 -24
- package/dist/features/environment/auto-sync.js +28 -27
- package/dist/features/environment/names.js +67 -47
- package/dist/features/environment/operations.js +321 -228
- package/dist/features/environment/own-entries.js +89 -0
- package/dist/features/environment/settle-names.js +91 -0
- package/dist/features/environment/stage.js +178 -0
- package/dist/features/environment/sync.js +113 -484
- package/dist/features/environment/transaction.js +179 -147
- package/dist/features/environment/writes.js +196 -0
- package/dist/features/github/account.js +25 -0
- package/dist/features/github/connections.js +119 -0
- package/dist/features/github/pulls.js +118 -0
- package/dist/features/hooks/adapt.js +32 -25
- package/dist/features/hooks/catalog.js +28 -0
- package/dist/features/hooks/compatibility.js +49 -0
- package/dist/features/hooks/divergence.js +9 -6
- package/dist/features/hooks/test.js +74 -49
- package/dist/features/hooks/types.js +1 -0
- package/dist/features/imports/discover.js +134 -61
- package/dist/features/imports/folder.js +48 -14
- package/dist/features/imports/local.js +113 -134
- package/dist/features/imports/remote.js +44 -25
- package/dist/features/inspection/index.js +17 -17
- package/dist/features/inventory/index.js +99 -110
- package/dist/features/knowledge/index.js +69 -49
- package/dist/features/mcps/catalog.js +31 -0
- package/dist/features/mcps/import.js +316 -0
- package/dist/features/mcps/index.js +98 -49
- package/dist/features/migration/migrate.js +263 -0
- package/dist/features/{git-workspace → migration}/move.js +165 -109
- package/dist/features/migration/promote.js +24 -0
- package/dist/features/native/adapters.js +53 -94
- package/dist/features/native/configuration.js +128 -82
- package/dist/features/native/hook-shell.js +55 -0
- package/dist/features/native/plugin-module.js +56 -0
- package/dist/features/onboarding/environment.js +67 -34
- package/dist/features/onboarding/setup.js +43 -40
- package/dist/features/repositories/context.js +30 -13
- package/dist/features/server/catalog.js +58 -0
- package/dist/features/server/client.js +13 -0
- package/dist/features/server/connection.js +37 -0
- package/dist/features/server/origin.js +74 -0
- package/dist/features/server/pages.js +24 -0
- package/dist/features/skills/catalog.js +42 -0
- package/dist/features/workspace/assets.js +57 -0
- package/dist/features/workspace/cloud-source.js +34 -0
- package/dist/features/workspace/directory.js +38 -0
- package/dist/features/{git-workspace → workspace}/format.js +114 -51
- package/dist/features/workspace/git.js +214 -0
- package/dist/features/workspace/installed.js +92 -0
- package/dist/features/{git-workspace → workspace}/overlay.js +43 -23
- package/dist/features/workspace/packages.js +160 -0
- package/dist/features/{git-workspace → workspace}/publish.js +120 -64
- package/dist/features/workspace/repo-source.js +192 -0
- package/dist/features/{git-workspace → workspace}/setup.js +60 -42
- package/dist/features/{git-workspace → workspace}/snapshot.js +53 -18
- package/dist/features/workspace/source.js +1 -0
- package/dist/features/workspace/tree.js +52 -0
- package/dist/features/workspace/workspace-source.js +27 -0
- package/dist/harnesses/claude.js +23 -1
- package/dist/harnesses/codex.js +57 -1
- package/dist/harnesses/cursor.js +22 -1
- package/dist/harnesses/detect.js +6 -50
- package/dist/harnesses/formats.js +45 -0
- package/dist/harnesses/opencode.js +111 -0
- package/dist/harnesses/registry.js +35 -2
- package/dist/platform.js +375 -0
- package/dist/program.js +225 -54
- package/dist/runtime.js +1 -0
- package/dist/shared/cli-error.js +22 -0
- package/dist/shared/constants.js +32 -0
- package/dist/shared/files.js +72 -0
- package/dist/shared/lock.js +49 -0
- package/dist/shared/lookup.js +11 -0
- package/dist/shared/patterns.js +94 -0
- package/dist/shared/shell.js +22 -0
- package/dist/shared/values.js +26 -0
- package/dist/ui/add-asset.js +41 -400
- package/dist/ui/add-hook.js +213 -0
- package/dist/ui/add-mcp.js +107 -0
- package/dist/ui/add-prompts.js +47 -0
- package/dist/ui/add-skills.js +115 -0
- package/dist/ui/asset-list.js +31 -13
- package/dist/ui/assets.js +33 -44
- package/dist/ui/auth.js +43 -33
- package/dist/ui/clash.js +11 -6
- package/dist/ui/collaboration.js +116 -44
- package/dist/ui/connection.js +10 -4
- package/dist/ui/decision-error.js +24 -0
- package/dist/ui/environment-onboarding.js +35 -13
- package/dist/ui/format.js +66 -18
- package/dist/ui/hook-compatibility.js +14 -6
- package/dist/ui/mcp.js +28 -3
- package/dist/ui/move.js +10 -5
- package/dist/ui/onboarding.js +113 -73
- package/dist/ui/options.js +9 -0
- package/dist/ui/output.js +106 -43
- package/dist/ui/pick.js +45 -33
- package/dist/ui/scope-install.js +15 -7
- package/dist/ui/selection.js +15 -11
- package/dist/ui/theme.js +37 -14
- package/dist/version.js +6 -3
- package/package.json +13 -3
- package/dist/features/capabilities/index.js +0 -106
- package/dist/features/collaboration/domain.js +0 -90
- package/dist/features/environment/cloud-source.js +0 -27
- package/dist/features/environment/installed.js +0 -124
- package/dist/features/environment/packages.js +0 -120
- package/dist/features/environment/remote.js +0 -71
- package/dist/features/environment/workspace-source.js +0 -19
- package/dist/features/git-hosts/index.js +0 -148
- package/dist/features/git-workspace/assets.js +0 -71
- package/dist/features/git-workspace/git.js +0 -135
- package/dist/features/git-workspace/migrate.js +0 -188
- package/dist/features/git-workspace/source.js +0 -159
- package/dist/features/imports/mcp.js +0 -219
- package/dist/features/knowledge/startup.js +0 -25
- package/dist/features/native/mcp-launch.js +0 -57
- package/dist/features/skills/install.js +0 -25
- package/dist/features/skills/remote.js +0 -177
- package/dist/features/skills/sync.js +0 -121
- package/dist/features/skills/validation.js +0 -34
- /package/dist/features/environment/{source.js → events.js} +0 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { gitCopyImportSchema, gitCopyImportResultSchema, gitCreateRepositorySchema, gitRemoteHeadSchema, gitConnectionSchema, gitConnectSchema, gitImportSchema, gitMirrorResolutionSchema, okSchema, } from "@barndog-ai/api-contract";
|
|
4
|
+
import { selectOrigin } from "../server/origin.js";
|
|
5
|
+
import { connection } from "../server/connection.js";
|
|
6
|
+
import { selectAsset } from "../server/catalog.js";
|
|
7
|
+
import { parsed } from "../server/client.js";
|
|
8
|
+
import { CliError } from "../../errors.js";
|
|
9
|
+
import { apiClient } from "../auth/transport.js";
|
|
10
|
+
/** The path parameters of one connection's routes; only a UUID may reach the URL. */
|
|
11
|
+
export const connectionParams = (connectionId) => ({
|
|
12
|
+
path: { id: z.uuidv4().parse(connectionId) },
|
|
13
|
+
});
|
|
14
|
+
function repositoryTarget(options) {
|
|
15
|
+
const [owner, repository, ...rest] = options.repository.split("/");
|
|
16
|
+
if (!owner || !repository || rest.length) {
|
|
17
|
+
throw new CliError("Choose the repository with --repository <owner/name>.");
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
kind: options.kind,
|
|
21
|
+
installationId: options.installation,
|
|
22
|
+
owner,
|
|
23
|
+
repository,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export async function githubList(runtime) {
|
|
27
|
+
const conn = await connection(runtime);
|
|
28
|
+
return parsed(z.array(gitConnectionSchema), apiClient(conn.server, runtime).GET("/v1/git-connections"));
|
|
29
|
+
}
|
|
30
|
+
/** Connects a published Skill or Hook to an existing GitHub repository. */
|
|
31
|
+
export async function githubConnect(options, runtime) {
|
|
32
|
+
const conn = await connection(runtime);
|
|
33
|
+
const target = repositoryTarget(options);
|
|
34
|
+
const selected = await selectAsset(target.kind, options.name, options.origin, runtime);
|
|
35
|
+
const body = gitConnectSchema.parse({
|
|
36
|
+
...target,
|
|
37
|
+
assetId: selected.reference.id,
|
|
38
|
+
expectedVersionId: options.expectedVersion ?? selected.asset.version.id,
|
|
39
|
+
mode: options.mode ?? "connect",
|
|
40
|
+
});
|
|
41
|
+
return parsed(gitConnectionSchema, apiClient(conn.server, runtime).POST("/v1/git-connections", { body }));
|
|
42
|
+
}
|
|
43
|
+
/** Creates a Skill or Hook from a GitHub repository, which stays authoritative. */
|
|
44
|
+
export async function githubImport(options, runtime) {
|
|
45
|
+
const conn = await connection(runtime);
|
|
46
|
+
const target = repositoryTarget(options);
|
|
47
|
+
const scope = await selectOrigin(options.origin, conn, runtime);
|
|
48
|
+
const body = gitImportSchema.parse({
|
|
49
|
+
...target,
|
|
50
|
+
name: options.name,
|
|
51
|
+
origin: { type: scope.type, id: scope.id },
|
|
52
|
+
});
|
|
53
|
+
return parsed(gitConnectionSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/import", {
|
|
54
|
+
body,
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
/** Copies a GitHub repository into a BarnDog-owned asset with no connection left behind. */
|
|
58
|
+
export async function githubImportCopy(options, runtime) {
|
|
59
|
+
const conn = await connection(runtime);
|
|
60
|
+
const target = repositoryTarget(options);
|
|
61
|
+
const scope = await selectOrigin(options.origin, conn, runtime);
|
|
62
|
+
const body = gitCopyImportSchema.parse({
|
|
63
|
+
...target,
|
|
64
|
+
name: options.name,
|
|
65
|
+
origin: { type: scope.type, id: scope.id },
|
|
66
|
+
idempotencyKey: options.key,
|
|
67
|
+
});
|
|
68
|
+
return parsed(gitCopyImportResultSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/import-copy", {
|
|
69
|
+
body,
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
/** Creates a GitHub repository for a published asset and exports its history. */
|
|
73
|
+
export async function githubCreateRepository(options, runtime) {
|
|
74
|
+
const conn = await connection(runtime);
|
|
75
|
+
const target = repositoryTarget(options);
|
|
76
|
+
const selected = await selectAsset(target.kind, options.name, options.origin, runtime);
|
|
77
|
+
const body = gitCreateRepositorySchema.parse({
|
|
78
|
+
...target,
|
|
79
|
+
assetId: selected.reference.id,
|
|
80
|
+
expectedVersionId: options.expectedVersion ?? selected.asset.version.id,
|
|
81
|
+
mode: options.mode ?? "mirror",
|
|
82
|
+
visibility: options.visibility ?? "private",
|
|
83
|
+
idempotencyKey: options.key,
|
|
84
|
+
});
|
|
85
|
+
return parsed(gitConnectionSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/create-repository", {
|
|
86
|
+
body,
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
export async function githubShow(connectionId, runtime) {
|
|
90
|
+
const conn = await connection(runtime);
|
|
91
|
+
return parsed(gitConnectionSchema, apiClient(conn.server, runtime).GET("/v1/git-connections/{id}", {
|
|
92
|
+
params: connectionParams(connectionId),
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
/** Syncs now instead of waiting for the server's next scheduled sync. */
|
|
96
|
+
export async function githubSync(connectionId, runtime) {
|
|
97
|
+
const conn = await connection(runtime);
|
|
98
|
+
return parsed(gitConnectionSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/{id}/sync", {
|
|
99
|
+
params: connectionParams(connectionId),
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
/** The repository's main commit as GitHub reports it now, not as last synced. */
|
|
103
|
+
export async function githubRemoteHead(connectionId, runtime) {
|
|
104
|
+
const conn = await connection(runtime);
|
|
105
|
+
return parsed(gitRemoteHeadSchema, apiClient(conn.server, runtime).GET("/v1/git-connections/{id}/remote-head", {
|
|
106
|
+
params: connectionParams(connectionId),
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
/** Settles a mirror that diverged from BarnDog; refused if either side moved since it was inspected. */
|
|
110
|
+
export async function githubResolveMirror(options, runtime) {
|
|
111
|
+
const conn = await connection(runtime);
|
|
112
|
+
const body = gitMirrorResolutionSchema.parse({
|
|
113
|
+
action: options.action,
|
|
114
|
+
expectedVersionId: options.expectedVersion,
|
|
115
|
+
expectedRemoteCommit: options.expectedRemoteCommit,
|
|
116
|
+
idempotencyKey: options.key ?? randomUUID(),
|
|
117
|
+
});
|
|
118
|
+
return parsed(okSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/{id}/mirror-resolution", { params: connectionParams(options.connectionId), body }));
|
|
119
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pull requests on the GitHub repository behind a connection, for assets
|
|
3
|
+
* GitHub is authoritative for. Every call goes to the BarnDog server, never
|
|
4
|
+
* to GitHub itself.
|
|
5
|
+
*/
|
|
6
|
+
import { localPath } from "../../platform.js";
|
|
7
|
+
import { readDirectory } from "../workspace/directory.js";
|
|
8
|
+
import { readFile } from "node:fs/promises";
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { gitConnectionSchema, gitPullSchema, gitPullDetailsSchema, gitProposalSchema, gitRevisionSchema, gitReviewSchema, gitCommentSchema, gitMergeSchema, } from "@barndog-ai/api-contract";
|
|
12
|
+
import { connection } from "../server/connection.js";
|
|
13
|
+
import { parsed } from "../server/client.js";
|
|
14
|
+
import { connectionParams } from "./connections.js";
|
|
15
|
+
import { apiClient } from "../auth/transport.js";
|
|
16
|
+
const bodyText = async (options, runtime) => options.bodyFile
|
|
17
|
+
? await readFile(localPath(options.bodyFile, runtime), "utf8")
|
|
18
|
+
: (options.body ?? "");
|
|
19
|
+
/** The path parameters of one pull request's routes. */
|
|
20
|
+
const pullParams = (connectionId, number) => ({
|
|
21
|
+
path: {
|
|
22
|
+
...connectionParams(connectionId).path,
|
|
23
|
+
number: z.number().int().positive().parse(number),
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
/** Every file of the package, as the server's proposal routes take them. */
|
|
27
|
+
async function packageFiles(directory, runtime) {
|
|
28
|
+
const entries = await readDirectory(localPath(directory ?? ".", runtime), runtime);
|
|
29
|
+
return entries.map((entry) => ({
|
|
30
|
+
path: entry.path,
|
|
31
|
+
content: entry.data.toString("base64"),
|
|
32
|
+
executable: entry.executable,
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
export async function githubPullsList(connectionId, runtime) {
|
|
36
|
+
const conn = await connection(runtime);
|
|
37
|
+
return parsed(z.array(gitPullSchema), apiClient(conn.server, runtime).GET("/v1/git-connections/{id}/pulls", {
|
|
38
|
+
params: connectionParams(connectionId),
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
/** One pull request with its reviews and comments. */
|
|
42
|
+
export async function githubPullsShow(connectionId, number, runtime) {
|
|
43
|
+
const conn = await connection(runtime);
|
|
44
|
+
return parsed(gitPullDetailsSchema, apiClient(conn.server, runtime).GET("/v1/git-connections/{id}/pulls/{number}", {
|
|
45
|
+
params: pullParams(connectionId, number),
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
/** Opens a pull request whose content is the complete package, not a patch. */
|
|
49
|
+
export async function githubPullsCreate(options, runtime) {
|
|
50
|
+
const conn = await connection(runtime);
|
|
51
|
+
const body = await bodyText(options, runtime);
|
|
52
|
+
const proposal = gitProposalSchema.parse({
|
|
53
|
+
expectedCommit: options.expectedCommit,
|
|
54
|
+
files: await packageFiles(options.directory, runtime),
|
|
55
|
+
title: options.title,
|
|
56
|
+
body,
|
|
57
|
+
idempotencyKey: options.key ?? randomUUID(),
|
|
58
|
+
});
|
|
59
|
+
return parsed(gitPullSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/{id}/pulls", {
|
|
60
|
+
params: connectionParams(options.connectionId),
|
|
61
|
+
body: proposal,
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
/** Replaces an open pull request's content with the complete package. */
|
|
65
|
+
export async function githubPullsRevise(options, runtime) {
|
|
66
|
+
const conn = await connection(runtime);
|
|
67
|
+
const body = await bodyText(options, runtime);
|
|
68
|
+
const revision = gitRevisionSchema.parse({
|
|
69
|
+
expectedCommit: options.expectedCommit,
|
|
70
|
+
files: await packageFiles(options.directory, runtime),
|
|
71
|
+
title: options.title,
|
|
72
|
+
body,
|
|
73
|
+
idempotencyKey: options.key ?? randomUUID(),
|
|
74
|
+
});
|
|
75
|
+
return parsed(gitPullSchema, apiClient(conn.server, runtime).PUT("/v1/git-connections/{id}/pulls/{number}/revisions", {
|
|
76
|
+
params: pullParams(options.connectionId, options.number),
|
|
77
|
+
body: revision,
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Approves, requests changes or comments on a pull request at the inspected
|
|
82
|
+
* head commit. Reads `bodyFile` when given.
|
|
83
|
+
*/
|
|
84
|
+
export async function githubPullsReview(options, runtime) {
|
|
85
|
+
const conn = await connection(runtime);
|
|
86
|
+
const body = await bodyText(options, runtime);
|
|
87
|
+
return parsed(gitPullDetailsSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/{id}/pulls/{number}/reviews", {
|
|
88
|
+
params: pullParams(options.connectionId, options.number),
|
|
89
|
+
body: gitReviewSchema.parse({
|
|
90
|
+
expectedCommit: options.expectedCommit,
|
|
91
|
+
event: options.event,
|
|
92
|
+
body,
|
|
93
|
+
}),
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
/** A general comment, or an inline one when `path`, `line` and `side` are given. */
|
|
97
|
+
export async function githubPullsComment(options, runtime) {
|
|
98
|
+
const conn = await connection(runtime);
|
|
99
|
+
const body = await bodyText(options, runtime);
|
|
100
|
+
return parsed(gitPullDetailsSchema, apiClient(conn.server, runtime).POST("/v1/git-connections/{id}/pulls/{number}/comments", {
|
|
101
|
+
params: pullParams(options.connectionId, options.number),
|
|
102
|
+
body: gitCommentSchema.parse({
|
|
103
|
+
expectedCommit: options.expectedCommit,
|
|
104
|
+
body,
|
|
105
|
+
path: options.path,
|
|
106
|
+
line: options.line,
|
|
107
|
+
side: options.side,
|
|
108
|
+
}),
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
/** Returns the connection, now at the merged commit, rather than the pull request. */
|
|
112
|
+
export async function githubPullsMerge(options, runtime) {
|
|
113
|
+
const conn = await connection(runtime);
|
|
114
|
+
return parsed(gitConnectionSchema, apiClient(conn.server, runtime).PUT("/v1/git-connections/{id}/pulls/{number}/merge", {
|
|
115
|
+
params: pullParams(options.connectionId, options.number),
|
|
116
|
+
body: gitMergeSchema.parse({ expectedCommit: options.expectedCommit }),
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { homedir } from "node:os";
|
|
2
1
|
import { join } from "node:path";
|
|
3
2
|
import { hookAdaptPrompt, hookSchema, } from "@barndog-ai/api-contract";
|
|
4
3
|
import { hookMetadata } from "@barndog-ai/api-contract/package";
|
|
5
4
|
import { readSettings, SetupRequiredError } from "../../config/settings.js";
|
|
6
5
|
import { CliError } from "../../errors.js";
|
|
7
|
-
import { adapters,
|
|
8
|
-
import { selectAsset } from "../
|
|
9
|
-
import { repoForOrigin, selectRepoAsset } from "../
|
|
10
|
-
import { LOCAL_ORIGIN } from "../
|
|
6
|
+
import { adapters, hookHarnesses, isHookHarness, } from "../../harnesses/registry.js";
|
|
7
|
+
import { selectAsset } from "../server/catalog.js";
|
|
8
|
+
import { repoForOrigin, selectRepoAsset } from "../workspace/assets.js";
|
|
9
|
+
import { LOCAL_ORIGIN } from "../workspace/overlay.js";
|
|
11
10
|
import { importedFrom } from "../imports/local.js";
|
|
12
11
|
/** "Codex", "Codex and Cursor", "Claude Code, Codex or Cursor". */
|
|
13
12
|
export function toolNames(tools, joiner = "and") {
|
|
@@ -17,34 +16,40 @@ export function toolNames(tools, joiner = "and") {
|
|
|
17
16
|
: `${names.slice(0, -1).join(", ")} ${joiner} ${names.at(-1)}`;
|
|
18
17
|
}
|
|
19
18
|
/** The shared prompt, with this machine's names for coding tools and the local folder. */
|
|
20
|
-
|
|
19
|
+
function adaptPrompt(input) {
|
|
21
20
|
return hookAdaptPrompt({
|
|
22
21
|
...input,
|
|
23
|
-
|
|
22
|
+
// Object.fromEntries types its keys as string; the map covers every harness.
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
24
|
+
tools: Object.fromEntries(hookHarnesses.map((harness) => [harness, adapters[harness].name])),
|
|
24
25
|
place: input.place.type === "folder"
|
|
25
26
|
? { ...input.place, origin: LOCAL_ORIGIN }
|
|
26
27
|
: input.place,
|
|
27
28
|
});
|
|
28
29
|
}
|
|
29
|
-
async function resolveHook(name, origin,
|
|
30
|
+
async function resolveHook(name, origin, runtime) {
|
|
31
|
+
const { home } = runtime;
|
|
30
32
|
const settings = await readSettings(home);
|
|
31
|
-
if (!settings)
|
|
33
|
+
if (!settings) {
|
|
32
34
|
throw new SetupRequiredError();
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const
|
|
35
|
+
}
|
|
36
|
+
const local = settings.localWorkspace;
|
|
37
|
+
const git = settings.gitWorkspace;
|
|
38
|
+
const shared = Boolean(git || settings.connection);
|
|
39
|
+
const repo = repoForOrigin(settings, runtime, origin);
|
|
37
40
|
if (!repo) {
|
|
38
|
-
const selected = await selectAsset("hook", name, origin,
|
|
41
|
+
const selected = await selectAsset("hook", name, origin, runtime);
|
|
39
42
|
const asset = hookSchema.parse(selected.asset);
|
|
40
43
|
const implementations = {};
|
|
41
|
-
for (const row of asset.compatibility)
|
|
42
|
-
if (row.implementation)
|
|
44
|
+
for (const row of asset.compatibility) {
|
|
45
|
+
if (row.implementation) {
|
|
43
46
|
implementations[row.harness] = row.implementation;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
44
49
|
return {
|
|
45
50
|
name: asset.name,
|
|
46
51
|
implementations,
|
|
47
|
-
machine: settings.harnesses,
|
|
52
|
+
machine: settings.harnesses.filter(isHookHarness),
|
|
48
53
|
place: {
|
|
49
54
|
type: "workspace",
|
|
50
55
|
origin,
|
|
@@ -58,7 +63,7 @@ async function resolveHook(name, origin, home) {
|
|
|
58
63
|
return {
|
|
59
64
|
name: asset.name,
|
|
60
65
|
implementations,
|
|
61
|
-
machine: settings.harnesses,
|
|
66
|
+
machine: settings.harnesses.filter(isHookHarness),
|
|
62
67
|
place: inFolder
|
|
63
68
|
? {
|
|
64
69
|
type: "folder",
|
|
@@ -76,17 +81,19 @@ async function resolveHook(name, origin, home) {
|
|
|
76
81
|
* Works out which coding tools a Hook doesn't cover and writes the prompt for
|
|
77
82
|
* them. Without `targets`, those are the tools set up on this machine.
|
|
78
83
|
*/
|
|
79
|
-
export async function adaptHook(name, origin, targets,
|
|
80
|
-
const hook = await resolveHook(name, origin,
|
|
81
|
-
const written =
|
|
84
|
+
export async function adaptHook(name, origin, targets, runtime) {
|
|
85
|
+
const hook = await resolveHook(name, origin, runtime);
|
|
86
|
+
const written = hookHarnesses.filter((h) => hook.implementations[h]);
|
|
82
87
|
const covered = targets?.filter((h) => written.includes(h)) ?? [];
|
|
83
|
-
if (covered.length)
|
|
88
|
+
if (covered.length) {
|
|
84
89
|
throw new CliError(`${hook.name} already has settings for ${toolNames(covered)}.`);
|
|
85
|
-
|
|
86
|
-
|
|
90
|
+
}
|
|
91
|
+
const missing = hookHarnesses.filter((h) => (targets ?? hook.machine).includes(h) && !written.includes(h));
|
|
92
|
+
if (!missing.length) {
|
|
87
93
|
return { name: hook.name, written, targets: [], prompt: null };
|
|
94
|
+
}
|
|
88
95
|
const record = hook.place.type === "folder"
|
|
89
|
-
? await importedFrom("hook", hook.name,
|
|
96
|
+
? await importedFrom("hook", hook.name, runtime)
|
|
90
97
|
: null;
|
|
91
98
|
return {
|
|
92
99
|
name: hook.name,
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { hookListSchema } from "@barndog-ai/api-contract";
|
|
2
|
+
import { apiClient } from "../auth/transport.js";
|
|
3
|
+
import { allPages } from "../server/pages.js";
|
|
4
|
+
/** Every published Hook you can see, as the server describes it. */
|
|
5
|
+
export async function publishedHooks(conn, runtime) {
|
|
6
|
+
const client = apiClient(conn.server, runtime);
|
|
7
|
+
return allPages(async (cursor) => {
|
|
8
|
+
const page = hookListSchema.parse((await client.GET("/v1/hooks", {
|
|
9
|
+
params: { query: { limit: 100, cursor } },
|
|
10
|
+
})).data);
|
|
11
|
+
if (page.hooks.some((hook) => hook.organizationId !== conn.organizationId)) {
|
|
12
|
+
throw new Error("The server sent a catalog page for another organization");
|
|
13
|
+
}
|
|
14
|
+
return { items: page.hooks, nextCursor: page.nextCursor };
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export async function hookCatalog(conn, runtime) {
|
|
18
|
+
return (await publishedHooks(conn, runtime)).map((hook) => ({
|
|
19
|
+
id: hook.id,
|
|
20
|
+
kind: "hook",
|
|
21
|
+
name: hook.name,
|
|
22
|
+
origin: hook.origin,
|
|
23
|
+
version: {
|
|
24
|
+
number: hook.version.number,
|
|
25
|
+
description: hook.version.description,
|
|
26
|
+
},
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { hookSchema } from "@barndog-ai/api-contract";
|
|
2
|
+
import { readSettings } from "../../config/settings.js";
|
|
3
|
+
import { selectAsset } from "../server/catalog.js";
|
|
4
|
+
import { repoForOrigin, selectRepoAsset } from "../workspace/assets.js";
|
|
5
|
+
import { runtimeRequirements } from "../native/adapters.js";
|
|
6
|
+
import { hookHarnesses } from "../../harnesses/registry.js";
|
|
7
|
+
import { hookMetadata } from "@barndog-ai/api-contract/package";
|
|
8
|
+
/**
|
|
9
|
+
* What a Hook's current version declares for each coding tool, and whether
|
|
10
|
+
* this machine has what each implementation needs to run.
|
|
11
|
+
*/
|
|
12
|
+
export async function hookCompatibility(name, origin, runtime) {
|
|
13
|
+
const { home } = runtime;
|
|
14
|
+
const settings = await readSettings(home);
|
|
15
|
+
const repo = settings && repoForOrigin(settings, runtime, origin);
|
|
16
|
+
if (repo) {
|
|
17
|
+
return repoHookCompatibility(selectRepoAsset(await repo.model(), "hook", name, origin), runtime);
|
|
18
|
+
}
|
|
19
|
+
const selected = await selectAsset("hook", name, origin, runtime);
|
|
20
|
+
const asset = hookSchema.parse(selected.asset);
|
|
21
|
+
return {
|
|
22
|
+
version: asset.version.number,
|
|
23
|
+
compatibility: await Promise.all(asset.compatibility.map(async (row) => ({
|
|
24
|
+
...row,
|
|
25
|
+
runtimeIssue: row.implementation
|
|
26
|
+
? await runtimeRequirements(row.harness, row.implementation.requirements, runtime)
|
|
27
|
+
: "No implementation for this harness",
|
|
28
|
+
}))),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** What a Git or local workspace Hook declares for each coding tool, and what this machine lacks. */
|
|
32
|
+
async function repoHookCompatibility(asset, runtime) {
|
|
33
|
+
const manifest = hookMetadata(asset.entries);
|
|
34
|
+
return {
|
|
35
|
+
version: asset.publishedVersion,
|
|
36
|
+
compatibility: await Promise.all(hookHarnesses.map(async (harness) => {
|
|
37
|
+
const implementation = manifest.implementations[harness] ?? null;
|
|
38
|
+
return {
|
|
39
|
+
harness,
|
|
40
|
+
status: implementation?.compatibility ?? "unsupported",
|
|
41
|
+
declared: true,
|
|
42
|
+
implementation,
|
|
43
|
+
runtimeIssue: implementation
|
|
44
|
+
? await runtimeRequirements(harness, implementation.requirements, runtime)
|
|
45
|
+
: "No implementation for this harness",
|
|
46
|
+
};
|
|
47
|
+
})),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -3,16 +3,17 @@ import { readFile } from "node:fs/promises";
|
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { divergentHookTools, hookManifestSchema, } from "@barndog-ai/api-contract";
|
|
5
5
|
import { parse } from "yaml";
|
|
6
|
-
import { adapters,
|
|
6
|
+
import { adapters, hookHarnesses } from "../../harnesses/registry.js";
|
|
7
7
|
/**
|
|
8
8
|
* A caution for Hooks whose coding tools run different scripts: changing one
|
|
9
9
|
* tool's scripts leaves the others as they were. Null when all share one set.
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
function divergenceWarning(implementations) {
|
|
12
12
|
const divergent = divergentHookTools(implementations);
|
|
13
|
-
if (!divergent.length)
|
|
13
|
+
if (!divergent.length) {
|
|
14
14
|
return null;
|
|
15
|
-
|
|
15
|
+
}
|
|
16
|
+
const names = hookHarnesses
|
|
16
17
|
.filter((harness) => divergent.includes(harness))
|
|
17
18
|
.map((harness) => adapters[harness].name);
|
|
18
19
|
return `${names.slice(0, -1).join(", ")} and ${names.at(-1)} run different scripts in this Hook. When you change it for one, check whether the others need the same change.`;
|
|
@@ -30,9 +31,11 @@ export async function folderDivergence(folder) {
|
|
|
30
31
|
/** The Hook working copy a folder sits in, however deep; null outside one. */
|
|
31
32
|
export function nearestHookFolder(from) {
|
|
32
33
|
for (let folder = from;; folder = dirname(folder)) {
|
|
33
|
-
if (existsSync(join(folder, "hook.yaml")))
|
|
34
|
+
if (existsSync(join(folder, "hook.yaml"))) {
|
|
34
35
|
return folder;
|
|
35
|
-
|
|
36
|
+
}
|
|
37
|
+
if (folder === dirname(folder)) {
|
|
36
38
|
return null;
|
|
39
|
+
}
|
|
37
40
|
}
|
|
38
41
|
}
|
|
@@ -1,73 +1,94 @@
|
|
|
1
|
+
import { isWindows, localPath, stopProcessTree } from "../../platform.js";
|
|
2
|
+
import { MILLISECONDS_PER_SECOND } from "../../shared/constants.js";
|
|
1
3
|
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
4
|
import { join } from "node:path";
|
|
4
5
|
import { spawn } from "node:child_process";
|
|
5
6
|
import { parse } from "yaml";
|
|
6
7
|
import { hookManifestSchema, hookSchema } from "@barndog-ai/api-contract";
|
|
7
|
-
import { selectAsset } from "../
|
|
8
|
-
import { cachedPackage } from "../
|
|
9
|
-
import {
|
|
8
|
+
import { selectAsset } from "../server/catalog.js";
|
|
9
|
+
import { cachedPackage } from "../workspace/packages.js";
|
|
10
|
+
import { runtimeRequirements } from "../native/adapters.js";
|
|
11
|
+
import { hookInvocation } from "../native/hook-shell.js";
|
|
10
12
|
import { readSettings } from "../../config/settings.js";
|
|
11
|
-
import { repoForOrigin, selectRepoAsset } from "../
|
|
12
|
-
|
|
13
|
+
import { repoForOrigin, selectRepoAsset } from "../workspace/assets.js";
|
|
14
|
+
import { CliError } from "../../errors.js";
|
|
15
|
+
/** CLI hook test policy caps each input/output stream at one MiB. */
|
|
16
|
+
const HOOK_IO_LIMIT_BYTES = 1048576;
|
|
17
|
+
/** CLI hook test policy defaults to ten seconds. */
|
|
18
|
+
const DEFAULT_HOOK_TIMEOUT_SECONDS = 10;
|
|
19
|
+
/** CLI hook test policy caps execution at ten minutes. */
|
|
20
|
+
const MAX_HOOK_TIMEOUT_SECONDS = 600;
|
|
21
|
+
async function hookPackage(name, origin, runtime) {
|
|
22
|
+
const { home } = runtime;
|
|
13
23
|
const settings = await readSettings(home);
|
|
14
|
-
const repo = settings && repoForOrigin(settings,
|
|
24
|
+
const repo = settings && repoForOrigin(settings, runtime, origin);
|
|
15
25
|
if (repo) {
|
|
16
26
|
const asset = selectRepoAsset(await repo.model(), "hook", name, origin);
|
|
17
|
-
return repo.
|
|
27
|
+
return repo.packageFolder("hook", asset);
|
|
18
28
|
}
|
|
19
|
-
const selected = await selectAsset("hook", name, origin,
|
|
20
|
-
|
|
29
|
+
const selected = await selectAsset("hook", name, origin, runtime);
|
|
30
|
+
const asset = hookSchema.parse(selected.asset);
|
|
31
|
+
return cachedPackage("hook", asset, selected.connection, runtime);
|
|
21
32
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Executes the selected Hook command in a POSIX shell with supplied native
|
|
35
|
+
* input, enforcing runtime and output limits.
|
|
36
|
+
*/
|
|
37
|
+
export async function testHook(name, origin, options, runtime) {
|
|
38
|
+
const { cwd } = runtime;
|
|
39
|
+
const root = await hookPackage(name, origin, runtime);
|
|
26
40
|
const manifest = hookManifestSchema.parse(parse(await readFile(join(root, "hook.yaml"), "utf8")));
|
|
27
41
|
const implementation = manifest.implementations[options.harness];
|
|
28
|
-
if (!implementation)
|
|
29
|
-
throw new
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
42
|
+
if (!implementation) {
|
|
43
|
+
throw new CliError(`${name} has no ${options.harness} implementation.`, `barndog hooks test ${name} --harness <claude|codex|cursor>`);
|
|
44
|
+
}
|
|
45
|
+
const unmet = await runtimeRequirements(options.harness, implementation.requirements, runtime);
|
|
46
|
+
if (unmet) {
|
|
47
|
+
throw new CliError(unmet);
|
|
48
|
+
}
|
|
33
49
|
const event = options.event ??
|
|
34
50
|
(Object.keys(implementation.events).length === 1
|
|
35
51
|
? Object.keys(implementation.events)[0]
|
|
36
52
|
: undefined);
|
|
37
|
-
if (!event || !implementation.events[event])
|
|
38
|
-
throw new
|
|
53
|
+
if (!event || !implementation.events[event]) {
|
|
54
|
+
throw new CliError(`Choose an event with --event: ${Object.keys(implementation.events).join(", ")}.`);
|
|
55
|
+
}
|
|
39
56
|
const handlers = implementation.events[event].flatMap((h) => "hooks" in h ? h.hooks : [h]);
|
|
40
|
-
if (handlers.length > 1 && options.handler === undefined)
|
|
41
|
-
throw new
|
|
57
|
+
if (handlers.length > 1 && options.handler === undefined) {
|
|
58
|
+
throw new CliError("This event runs several commands. Choose one with --handler <number>.");
|
|
59
|
+
}
|
|
42
60
|
const handler = handlers[(options.handler ?? 1) - 1];
|
|
43
|
-
if (!handler)
|
|
44
|
-
throw new
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
61
|
+
if (!handler) {
|
|
62
|
+
throw new CliError(`This event has no command ${options.handler}. Choose 1 to ${handlers.length}.`);
|
|
63
|
+
}
|
|
64
|
+
const input = await readFile(localPath(options.input, runtime));
|
|
65
|
+
if (input.length > HOOK_IO_LIMIT_BYTES) {
|
|
66
|
+
throw new CliError("The --input file is over 1 MiB.");
|
|
67
|
+
}
|
|
48
68
|
JSON.parse(input.toString("utf8"));
|
|
49
|
-
const timeout = options.timeout ?? handler.timeout ??
|
|
50
|
-
if (!Number.isFinite(timeout) ||
|
|
51
|
-
|
|
52
|
-
|
|
69
|
+
const timeout = options.timeout ?? handler.timeout ?? DEFAULT_HOOK_TIMEOUT_SECONDS;
|
|
70
|
+
if (!Number.isFinite(timeout) ||
|
|
71
|
+
timeout <= 0 ||
|
|
72
|
+
timeout > MAX_HOOK_TIMEOUT_SECONDS) {
|
|
73
|
+
throw new CliError("--timeout must be more than 0 and at most 600 seconds.");
|
|
74
|
+
}
|
|
75
|
+
const shell = await hookInvocation(root, handler.command, options.harness, runtime);
|
|
76
|
+
const child = spawn(shell.command, shell.args, {
|
|
53
77
|
cwd,
|
|
54
78
|
stdio: ["pipe", "pipe", "pipe"],
|
|
55
|
-
|
|
79
|
+
env: { ...runtime.env },
|
|
80
|
+
detached: !isWindows(runtime.platform),
|
|
81
|
+
windowsHide: true,
|
|
56
82
|
});
|
|
57
|
-
let stdout = ""
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
/* exited */
|
|
65
|
-
}
|
|
66
|
-
};
|
|
83
|
+
let stdout = "";
|
|
84
|
+
let stderr = "";
|
|
85
|
+
let length = 0;
|
|
86
|
+
let timedOut = false;
|
|
87
|
+
const stop = () => stopProcessTree(child, runtime);
|
|
67
88
|
const timer = setTimeout(() => {
|
|
68
89
|
timedOut = true;
|
|
69
90
|
stop();
|
|
70
|
-
}, timeout *
|
|
91
|
+
}, timeout * MILLISECONDS_PER_SECOND);
|
|
71
92
|
for (const [stream, append] of [
|
|
72
93
|
[
|
|
73
94
|
child.stdout,
|
|
@@ -81,14 +102,17 @@ export async function testHook(name, origin, options, home = homedir(), cwd = pr
|
|
|
81
102
|
stderr += text;
|
|
82
103
|
},
|
|
83
104
|
],
|
|
84
|
-
])
|
|
105
|
+
]) {
|
|
85
106
|
stream.on("data", (chunk) => {
|
|
86
107
|
length += chunk.length;
|
|
87
|
-
if (length >
|
|
108
|
+
if (length > HOOK_IO_LIMIT_BYTES) {
|
|
88
109
|
stop();
|
|
89
|
-
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
90
112
|
append(chunk.toString());
|
|
113
|
+
}
|
|
91
114
|
});
|
|
115
|
+
}
|
|
92
116
|
child.stdin.on("error", () => undefined);
|
|
93
117
|
child.stdin.end(input);
|
|
94
118
|
try {
|
|
@@ -96,8 +120,9 @@ export async function testHook(name, origin, options, home = homedir(), cwd = pr
|
|
|
96
120
|
child.once("error", reject);
|
|
97
121
|
child.once("close", (code) => resolve(code ?? 1));
|
|
98
122
|
});
|
|
99
|
-
if (length >
|
|
100
|
-
throw new
|
|
123
|
+
if (length > HOOK_IO_LIMIT_BYTES) {
|
|
124
|
+
throw new CliError("The Hook printed more than 1 MiB.");
|
|
125
|
+
}
|
|
101
126
|
return {
|
|
102
127
|
harness: options.harness,
|
|
103
128
|
event,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|