@usesketchbook/opencode-plugin 0.0.0-stage → 0.1.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 (88) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -2
  3. package/dist/generated/baseline-manifest.d.ts +2 -0
  4. package/dist/generated/baseline-manifest.js +1590 -0
  5. package/dist/generated/catalog-baseline.d.ts +2 -0
  6. package/dist/generated/catalog-baseline.js +255 -0
  7. package/dist/gui-experimental.cjs +111 -0
  8. package/dist/gui.d.ts +22 -0
  9. package/dist/gui.js +34 -0
  10. package/dist/index.d.ts +3 -0
  11. package/dist/index.js +20 -0
  12. package/dist/rpc.d.ts +1173 -0
  13. package/dist/rpc.js +212 -0
  14. package/dist/server/catalog.d.ts +9 -0
  15. package/dist/server/catalog.js +74 -0
  16. package/dist/server/decision-guard-hooks.d.ts +2 -0
  17. package/dist/server/decision-guard-hooks.js +57 -0
  18. package/dist/server/decision-guards.d.ts +11 -0
  19. package/dist/server/decision-guards.js +36 -0
  20. package/dist/server/engine.d.ts +6 -0
  21. package/dist/server/engine.js +38 -0
  22. package/dist/server/guard-hooks.d.ts +3 -0
  23. package/dist/server/guard-hooks.js +35 -0
  24. package/dist/server/guards.d.ts +13 -0
  25. package/dist/server/guards.js +42 -0
  26. package/dist/server/operations.d.ts +93 -0
  27. package/dist/server/operations.js +300 -0
  28. package/dist/server/permissions.d.ts +29 -0
  29. package/dist/server/permissions.js +97 -0
  30. package/dist/server/rpc.d.ts +126 -0
  31. package/dist/server/rpc.js +308 -0
  32. package/dist/server/runner.d.ts +27 -0
  33. package/dist/server/runner.js +136 -0
  34. package/dist/server/schema.d.ts +4 -0
  35. package/dist/server/schema.js +66 -0
  36. package/dist/server/tools.d.ts +2 -0
  37. package/dist/server/tools.js +176 -0
  38. package/dist/server/tui.d.ts +70 -0
  39. package/dist/server/tui.js +172 -0
  40. package/dist/shared/catalog.d.ts +52 -0
  41. package/dist/shared/catalog.js +84 -0
  42. package/dist/shared/envelope.d.ts +24 -0
  43. package/dist/shared/envelope.js +28 -0
  44. package/dist/shared/manifest.d.ts +53 -0
  45. package/dist/shared/manifest.js +135 -0
  46. package/dist/test/baseline.test.d.ts +1 -0
  47. package/dist/test/baseline.test.js +11 -0
  48. package/dist/test/catalog.test.d.ts +1 -0
  49. package/dist/test/catalog.test.js +232 -0
  50. package/dist/test/decision-guards.test.d.ts +1 -0
  51. package/dist/test/decision-guards.test.js +30 -0
  52. package/dist/test/engine.test.d.ts +1 -0
  53. package/dist/test/engine.test.js +23 -0
  54. package/dist/test/fixtures.d.ts +12 -0
  55. package/dist/test/fixtures.js +55 -0
  56. package/dist/test/guard-hooks.integration.test.d.ts +1 -0
  57. package/dist/test/guard-hooks.integration.test.js +321 -0
  58. package/dist/test/guard-hooks.test.d.ts +1 -0
  59. package/dist/test/guard-hooks.test.js +14 -0
  60. package/dist/test/guards.test.d.ts +1 -0
  61. package/dist/test/guards.test.js +24 -0
  62. package/dist/test/gui.test.d.ts +1 -0
  63. package/dist/test/gui.test.js +61 -0
  64. package/dist/test/manifest.test.d.ts +1 -0
  65. package/dist/test/manifest.test.js +86 -0
  66. package/dist/test/operations.test.d.ts +1 -0
  67. package/dist/test/operations.test.js +263 -0
  68. package/dist/test/parity.test.d.ts +1 -0
  69. package/dist/test/parity.test.js +56 -0
  70. package/dist/test/permissions.integration.test.d.ts +1 -0
  71. package/dist/test/permissions.integration.test.js +535 -0
  72. package/dist/test/permissions.test.d.ts +1 -0
  73. package/dist/test/permissions.test.js +42 -0
  74. package/dist/test/rpc.test.d.ts +1 -0
  75. package/dist/test/rpc.test.js +389 -0
  76. package/dist/test/runner.test.d.ts +1 -0
  77. package/dist/test/runner.test.js +47 -0
  78. package/dist/test/schema.test.d.ts +1 -0
  79. package/dist/test/schema.test.js +51 -0
  80. package/dist/test/tui.test.d.ts +1 -0
  81. package/dist/test/tui.test.js +226 -0
  82. package/dist/tprobe.d.ts +1 -0
  83. package/dist/tprobe.js +2 -0
  84. package/dist/tui.d.ts +10 -0
  85. package/dist/tui.js +21 -0
  86. package/dist/views/operations.d.ts +50 -0
  87. package/dist/views/operations.js +80 -0
  88. package/package.json +71 -4
@@ -0,0 +1,263 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { rm } from "node:fs/promises";
4
+ import { join } from "node:path";
5
+ import { writeFile } from "node:fs/promises";
6
+ import { executeOperation, findCommand, loadCapabilities, splitInput, previewOperation, applyPreview, getPreview, OperationError, StalePreviewError } from "../server/operations.js";
7
+ import { makeEngineProject } from "./fixtures.js";
8
+ const command = (overrides = {}) => ({
9
+ name: "mutate",
10
+ group: "project",
11
+ summary: "A mutating test command.",
12
+ usage: "sketchbook mutate",
13
+ args: [{ name: "target", required: true, description: "Target" }],
14
+ options: { yes: { type: "boolean", description: "Confirm" } },
15
+ examples: [],
16
+ mutates: true,
17
+ ...overrides,
18
+ });
19
+ const manifest = (commands) => ({
20
+ protocolVersion: "1.0.0",
21
+ envelopeVersion: 1,
22
+ engine: { name: "@usesketchbook/cli", version: "0.0.0-test" },
23
+ groups: { project: "Project" },
24
+ commands,
25
+ });
26
+ const echoProject = async () => {
27
+ const dir = await makeEngineProject("sketchbook-plugin-operations-");
28
+ await writeFile(join(dir, "node_modules/@usesketchbook/cli/harness/cli.mjs"), `#!/usr/bin/env node
29
+ const [, , command, ...rest] = process.argv;
30
+ process.stdout.write(
31
+ JSON.stringify({
32
+ ok: true,
33
+ command,
34
+ changed: [],
35
+ warnings: [],
36
+ errors: [],
37
+ next: null,
38
+ data: rest,
39
+ }),
40
+ );
41
+ `);
42
+ return dir;
43
+ };
44
+ const stateProject = async () => {
45
+ const dir = await makeEngineProject("sketchbook-plugin-preview-");
46
+ await writeFile(join(dir, "plan.txt"), "plan-a\n");
47
+ await writeFile(join(dir, "node_modules/@usesketchbook/cli/harness/cli.mjs"), `#!/usr/bin/env node
48
+ import { readFileSync, writeFileSync } from "node:fs";
49
+ import { join } from "node:path";
50
+ const [, , command, ...rest] = process.argv;
51
+ const plan = readFileSync(join(process.cwd(), "plan.txt"), "utf8").trim();
52
+ if (!rest.includes("--dry-run")) writeFileSync(join(process.cwd(), "applied"), plan);
53
+ process.stdout.write(
54
+ JSON.stringify({
55
+ ok: true,
56
+ command,
57
+ changed: [plan],
58
+ warnings: [],
59
+ errors: [],
60
+ next: null,
61
+ data: rest,
62
+ }),
63
+ );
64
+ `);
65
+ return dir;
66
+ };
67
+ test("loadCapabilities caches by location and refreshes on request", async () => {
68
+ const dir = await makeEngineProject("sketchbook-plugin-capabilities-");
69
+ try {
70
+ const first = await loadCapabilities(dir);
71
+ const second = await loadCapabilities(dir);
72
+ assert.equal(first, second);
73
+ const refreshed = await loadCapabilities(dir, { refresh: true });
74
+ assert.notEqual(first, refreshed);
75
+ assert.equal(first.protocolVersion, refreshed.protocolVersion);
76
+ }
77
+ finally {
78
+ await rm(dir, { recursive: true, force: true });
79
+ }
80
+ });
81
+ test("findCommand resolves a command and validates its subcommand", () => {
82
+ const spec = command({ subcommands: [{ name: "apply", usage: "mutate apply", summary: "Apply", mutates: true, examples: [] }] });
83
+ assert.equal(findCommand(manifest([spec]), "mutate"), spec);
84
+ assert.equal(findCommand(manifest([spec]), "mutate", "apply"), spec);
85
+ assert.throws(() => findCommand(manifest([spec]), "mutate", "missing"), OperationError);
86
+ assert.throws(() => findCommand(manifest([spec]), "missing"), OperationError);
87
+ });
88
+ test("splitInput separates manifest args from options", () => {
89
+ const spec = command();
90
+ const { args, options } = splitInput({ target: ["one", "two"], yes: true, unknown: "ignored" }, spec);
91
+ assert.deepEqual(args, ["one", "two"]);
92
+ assert.deepEqual(options, { yes: true });
93
+ });
94
+ test("executeOperation requires engine trust for project-local engines", async () => {
95
+ const dir = await echoProject();
96
+ try {
97
+ const asserted = [];
98
+ const permission = {
99
+ assert: async (input) => {
100
+ asserted.push(input);
101
+ },
102
+ };
103
+ const result = await executeOperation({
104
+ root: dir,
105
+ manifest: manifest([command({ mutates: false, risk: "read" })]),
106
+ command: "mutate",
107
+ args: ["target"],
108
+ permission,
109
+ sessionID: "session",
110
+ });
111
+ assert.equal(result.envelope.ok, true);
112
+ assert.ok(asserted.some((check) => check.action === "sketchbook.engine"));
113
+ assert.ok(asserted.some((check) => check.action === "sketchbook.run"));
114
+ }
115
+ finally {
116
+ await rm(dir, { recursive: true, force: true });
117
+ }
118
+ });
119
+ test("executeOperation previews supported mutations by default", async () => {
120
+ const dir = await echoProject();
121
+ try {
122
+ const result = await executeOperation({
123
+ root: dir,
124
+ manifest: manifest([command({ dryRun: true })]),
125
+ command: "mutate",
126
+ args: ["target"],
127
+ });
128
+ assert.equal(result.dryRun, true);
129
+ assert.ok(result.envelope.data.includes("--dry-run"));
130
+ }
131
+ finally {
132
+ await rm(dir, { recursive: true, force: true });
133
+ }
134
+ });
135
+ test("executeOperation applies when dryRun is false", async () => {
136
+ const dir = await echoProject();
137
+ try {
138
+ const result = await executeOperation({
139
+ root: dir,
140
+ manifest: manifest([command({ dryRun: true })]),
141
+ command: "mutate",
142
+ args: ["target"],
143
+ dryRun: false,
144
+ });
145
+ assert.equal(result.dryRun, false);
146
+ assert.ok(!result.envelope.data.includes("--dry-run"));
147
+ }
148
+ finally {
149
+ await rm(dir, { recursive: true, force: true });
150
+ }
151
+ });
152
+ test("executeOperation rejects previews for commands without dry-run support", async () => {
153
+ const dir = await echoProject();
154
+ try {
155
+ await assert.rejects(executeOperation({
156
+ root: dir,
157
+ manifest: manifest([command({ name: "hard", dryRun: false })]),
158
+ command: "hard",
159
+ args: ["target"],
160
+ dryRun: true,
161
+ }), OperationError);
162
+ }
163
+ finally {
164
+ await rm(dir, { recursive: true, force: true });
165
+ }
166
+ });
167
+ test("previews have deterministic fingerprints and unique operation IDs", async () => {
168
+ const dir = await echoProject();
169
+ try {
170
+ const first = await previewOperation({
171
+ root: dir,
172
+ manifest: manifest([command({ dryRun: true })]),
173
+ command: "mutate",
174
+ args: ["target"],
175
+ options: { yes: true },
176
+ });
177
+ const second = await previewOperation({
178
+ root: dir,
179
+ manifest: manifest([command({ dryRun: true })]),
180
+ command: "mutate",
181
+ args: ["target"],
182
+ options: { yes: true },
183
+ });
184
+ assert.notEqual(first.id, second.id);
185
+ assert.equal(first.fingerprint, second.fingerprint);
186
+ assert.equal(first.status, "pending");
187
+ }
188
+ finally {
189
+ await rm(dir, { recursive: true, force: true });
190
+ }
191
+ });
192
+ test("applyPreview applies the current plan once", async () => {
193
+ const dir = await stateProject();
194
+ try {
195
+ const preview = await previewOperation({
196
+ root: dir,
197
+ manifest: manifest([command({ dryRun: true })]),
198
+ command: "mutate",
199
+ args: ["target"],
200
+ });
201
+ assert.equal(preview.status, "pending");
202
+ const applied = await applyPreview({ operationID: preview.id, root: dir });
203
+ assert.equal(applied.dryRun, false);
204
+ assert.equal(applied.envelope.changed[0], "plan-a");
205
+ assert.equal(preview.status, "applied");
206
+ await assert.rejects(applyPreview({ operationID: preview.id, root: dir }), StalePreviewError);
207
+ }
208
+ finally {
209
+ await rm(dir, { recursive: true, force: true });
210
+ }
211
+ });
212
+ test("applyPreview rejects stale plans without applying", async () => {
213
+ const dir = await stateProject();
214
+ try {
215
+ const preview = await previewOperation({
216
+ root: dir,
217
+ manifest: manifest([command({ dryRun: true })]),
218
+ command: "mutate",
219
+ args: ["target"],
220
+ });
221
+ await writeFile(join(dir, "plan.txt"), "plan-b\n");
222
+ await assert.rejects(applyPreview({ operationID: preview.id, root: dir }), StalePreviewError);
223
+ assert.equal(preview.status, "stale");
224
+ assert.equal(getPreview(preview.id)?.status, "stale");
225
+ const exists = await (await import("node:fs/promises")).access(join(dir, "applied")).then(() => true, () => false);
226
+ assert.equal(exists, false);
227
+ }
228
+ finally {
229
+ await rm(dir, { recursive: true, force: true });
230
+ }
231
+ });
232
+ test("applyPreview rejects a preview from another location", async () => {
233
+ const first = await stateProject();
234
+ const second = await echoProject();
235
+ try {
236
+ const preview = await previewOperation({
237
+ root: first,
238
+ manifest: manifest([command({ dryRun: true })]),
239
+ command: "mutate",
240
+ args: ["target"],
241
+ });
242
+ await assert.rejects(applyPreview({ operationID: preview.id, root: second }), StalePreviewError);
243
+ assert.equal(preview.status, "pending");
244
+ }
245
+ finally {
246
+ await rm(first, { recursive: true, force: true });
247
+ await rm(second, { recursive: true, force: true });
248
+ }
249
+ });
250
+ test("previewOperation requires dry-run support", async () => {
251
+ const dir = await echoProject();
252
+ try {
253
+ await assert.rejects(previewOperation({
254
+ root: dir,
255
+ manifest: manifest([command({ name: "hard", dryRun: false })]),
256
+ command: "hard",
257
+ args: ["target"],
258
+ }), OperationError);
259
+ }
260
+ finally {
261
+ await rm(dir, { recursive: true, force: true });
262
+ }
263
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,56 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { BASELINE_MANIFEST } from "../generated/baseline-manifest.js";
4
+ import { commandInputSchema } from "../server/schema.js";
5
+ const toolable = (command) => command.name !== "capabilities" && !command.interactiveOnly;
6
+ test("the bundled manifest has one stable tool per public command", () => {
7
+ const names = new Set();
8
+ for (const command of BASELINE_MANIFEST.commands) {
9
+ const name = command.name.replace(/\//g, "_");
10
+ assert.equal(names.has(name), false, `${name} is registered twice`);
11
+ names.add(name);
12
+ assert.match(name, /^[A-Za-z0-9_-]+$/, `${name} is not a stable tool name`);
13
+ }
14
+ assert.ok(names.has("status"));
15
+ assert.ok(names.has("add"));
16
+ assert.ok(names.has("tokens"));
17
+ });
18
+ test("every toolable command gets a schema that names its args and options", () => {
19
+ for (const command of BASELINE_MANIFEST.commands.filter(toolable)) {
20
+ const schema = commandInputSchema(command);
21
+ const properties = (schema.properties ?? {});
22
+ for (const arg of command.args) {
23
+ if (arg.name === "subcommand" && command.subcommands?.length)
24
+ continue;
25
+ assert.ok(arg.name in properties, `${command.name} omits arg ${arg.name}`);
26
+ }
27
+ for (const option of Object.keys(command.options)) {
28
+ assert.ok(option in properties, `${command.name} omits option ${option}`);
29
+ }
30
+ if (command.dryRun)
31
+ assert.ok("dryRun" in properties, `${command.name} omits dryRun`);
32
+ else
33
+ assert.equal("dryRun" in properties, false, `${command.name} exposes unsupported dryRun`);
34
+ }
35
+ });
36
+ test("nested commands expose their subcommands as an enum", () => {
37
+ const tokens = BASELINE_MANIFEST.commands.find((command) => command.name === "tokens");
38
+ assert.ok(tokens?.subcommands?.length);
39
+ const schema = commandInputSchema(tokens);
40
+ const subcommand = (schema.properties ?? {}).subcommand;
41
+ assert.deepEqual(subcommand?.enum, tokens.subcommands.map((s) => s.name));
42
+ });
43
+ test("interactive-only workflows stay out of the agent toolset", () => {
44
+ const interactive = BASELINE_MANIFEST.commands.filter((command) => command.interactiveOnly);
45
+ assert.ok(interactive.length > 0);
46
+ assert.ok(interactive.every((command) => !toolable(command)));
47
+ });
48
+ test("mutation commands with dry-run support can be previewed and applied", () => {
49
+ const previewable = BASELINE_MANIFEST.commands.filter((command) => command.mutates && command.dryRun && !command.interactiveOnly);
50
+ assert.ok(previewable.length > 0);
51
+ for (const command of previewable) {
52
+ const schema = commandInputSchema(command);
53
+ assert.ok("dryRun" in (schema.properties ?? {}));
54
+ assert.ok(command.risk && command.risk !== "read", `${command.name} has no mutation risk`);
55
+ }
56
+ });
@@ -0,0 +1 @@
1
+ export {};