@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.
- package/LICENSE +21 -0
- package/README.md +123 -2
- package/dist/generated/baseline-manifest.d.ts +2 -0
- package/dist/generated/baseline-manifest.js +1590 -0
- package/dist/generated/catalog-baseline.d.ts +2 -0
- package/dist/generated/catalog-baseline.js +255 -0
- package/dist/gui-experimental.cjs +111 -0
- package/dist/gui.d.ts +22 -0
- package/dist/gui.js +34 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/rpc.d.ts +1173 -0
- package/dist/rpc.js +212 -0
- package/dist/server/catalog.d.ts +9 -0
- package/dist/server/catalog.js +74 -0
- package/dist/server/decision-guard-hooks.d.ts +2 -0
- package/dist/server/decision-guard-hooks.js +57 -0
- package/dist/server/decision-guards.d.ts +11 -0
- package/dist/server/decision-guards.js +36 -0
- package/dist/server/engine.d.ts +6 -0
- package/dist/server/engine.js +38 -0
- package/dist/server/guard-hooks.d.ts +3 -0
- package/dist/server/guard-hooks.js +35 -0
- package/dist/server/guards.d.ts +13 -0
- package/dist/server/guards.js +42 -0
- package/dist/server/operations.d.ts +93 -0
- package/dist/server/operations.js +300 -0
- package/dist/server/permissions.d.ts +29 -0
- package/dist/server/permissions.js +97 -0
- package/dist/server/rpc.d.ts +126 -0
- package/dist/server/rpc.js +308 -0
- package/dist/server/runner.d.ts +27 -0
- package/dist/server/runner.js +136 -0
- package/dist/server/schema.d.ts +4 -0
- package/dist/server/schema.js +66 -0
- package/dist/server/tools.d.ts +2 -0
- package/dist/server/tools.js +176 -0
- package/dist/server/tui.d.ts +70 -0
- package/dist/server/tui.js +172 -0
- package/dist/shared/catalog.d.ts +52 -0
- package/dist/shared/catalog.js +84 -0
- package/dist/shared/envelope.d.ts +24 -0
- package/dist/shared/envelope.js +28 -0
- package/dist/shared/manifest.d.ts +53 -0
- package/dist/shared/manifest.js +135 -0
- package/dist/test/baseline.test.d.ts +1 -0
- package/dist/test/baseline.test.js +11 -0
- package/dist/test/catalog.test.d.ts +1 -0
- package/dist/test/catalog.test.js +232 -0
- package/dist/test/decision-guards.test.d.ts +1 -0
- package/dist/test/decision-guards.test.js +30 -0
- package/dist/test/engine.test.d.ts +1 -0
- package/dist/test/engine.test.js +23 -0
- package/dist/test/fixtures.d.ts +12 -0
- package/dist/test/fixtures.js +55 -0
- package/dist/test/guard-hooks.integration.test.d.ts +1 -0
- package/dist/test/guard-hooks.integration.test.js +321 -0
- package/dist/test/guard-hooks.test.d.ts +1 -0
- package/dist/test/guard-hooks.test.js +14 -0
- package/dist/test/guards.test.d.ts +1 -0
- package/dist/test/guards.test.js +24 -0
- package/dist/test/gui.test.d.ts +1 -0
- package/dist/test/gui.test.js +61 -0
- package/dist/test/manifest.test.d.ts +1 -0
- package/dist/test/manifest.test.js +86 -0
- package/dist/test/operations.test.d.ts +1 -0
- package/dist/test/operations.test.js +263 -0
- package/dist/test/parity.test.d.ts +1 -0
- package/dist/test/parity.test.js +56 -0
- package/dist/test/permissions.integration.test.d.ts +1 -0
- package/dist/test/permissions.integration.test.js +535 -0
- package/dist/test/permissions.test.d.ts +1 -0
- package/dist/test/permissions.test.js +42 -0
- package/dist/test/rpc.test.d.ts +1 -0
- package/dist/test/rpc.test.js +389 -0
- package/dist/test/runner.test.d.ts +1 -0
- package/dist/test/runner.test.js +47 -0
- package/dist/test/schema.test.d.ts +1 -0
- package/dist/test/schema.test.js +51 -0
- package/dist/test/tui.test.d.ts +1 -0
- package/dist/test/tui.test.js +226 -0
- package/dist/tprobe.d.ts +1 -0
- package/dist/tprobe.js +2 -0
- package/dist/tui.d.ts +10 -0
- package/dist/tui.js +21 -0
- package/dist/views/operations.d.ts +50 -0
- package/dist/views/operations.js +80 -0
- package/package.json +71 -4
package/dist/rpc.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { Rpc } from "@opencode/plugin/rpc";
|
|
2
|
+
// Error data shared by every typed Sketchbook failure. `code` uses the
|
|
3
|
+
// SKETCHBOOK_* convention so RPC clients classify failures without matching
|
|
4
|
+
// message text.
|
|
5
|
+
const errorData = {
|
|
6
|
+
type: "object",
|
|
7
|
+
properties: {
|
|
8
|
+
code: { type: "string" },
|
|
9
|
+
command: { type: "string" },
|
|
10
|
+
operationID: { type: "string" },
|
|
11
|
+
message: { type: "string" },
|
|
12
|
+
},
|
|
13
|
+
required: ["code", "message"],
|
|
14
|
+
};
|
|
15
|
+
const errors = {
|
|
16
|
+
sketchbook_operation: errorData,
|
|
17
|
+
sketchbook_stale_preview: errorData,
|
|
18
|
+
sketchbook_not_found: errorData,
|
|
19
|
+
sketchbook_cancelled: errorData,
|
|
20
|
+
sketchbook_timeout: errorData,
|
|
21
|
+
sketchbook_output_limit: errorData,
|
|
22
|
+
sketchbook_permission: errorData,
|
|
23
|
+
};
|
|
24
|
+
const operationInput = {
|
|
25
|
+
type: "object",
|
|
26
|
+
properties: {
|
|
27
|
+
command: { type: "string", description: "Sketchbook command name from the capabilities manifest." },
|
|
28
|
+
subcommand: { type: "string", description: "Optional subcommand name." },
|
|
29
|
+
args: { type: "array", items: { type: "string" }, description: "Positional arguments." },
|
|
30
|
+
options: {
|
|
31
|
+
type: "object",
|
|
32
|
+
additionalProperties: { type: ["string", "boolean"] },
|
|
33
|
+
description: "Command options keyed by their manifest name.",
|
|
34
|
+
},
|
|
35
|
+
dryRun: { type: "boolean", description: "Force a preview (true) or an apply (false). Mutations preview by default." },
|
|
36
|
+
operationID: {
|
|
37
|
+
type: "string",
|
|
38
|
+
description: "Client-chosen operation ID for cancel/status tracking. Generated when omitted.",
|
|
39
|
+
},
|
|
40
|
+
timeoutMs: { type: "number", description: "Operation resource budget: wall-clock limit in milliseconds." },
|
|
41
|
+
maxOutputBytes: { type: "number", description: "Operation resource budget: maximum engine output bytes." },
|
|
42
|
+
},
|
|
43
|
+
required: ["command"],
|
|
44
|
+
};
|
|
45
|
+
const engineSchema = {
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
source: { type: "string", enum: ["project", "bundled"] },
|
|
49
|
+
path: { type: "string" },
|
|
50
|
+
version: { type: "string" },
|
|
51
|
+
},
|
|
52
|
+
required: ["source", "path", "version"],
|
|
53
|
+
};
|
|
54
|
+
const previewSummary = {
|
|
55
|
+
type: "object",
|
|
56
|
+
properties: {
|
|
57
|
+
id: { type: "string" },
|
|
58
|
+
fingerprint: { type: "string" },
|
|
59
|
+
status: { type: "string", enum: ["pending", "applied", "stale"] },
|
|
60
|
+
},
|
|
61
|
+
required: ["id", "fingerprint", "status"],
|
|
62
|
+
};
|
|
63
|
+
const runOutput = {
|
|
64
|
+
type: "object",
|
|
65
|
+
properties: {
|
|
66
|
+
ok: { type: "boolean" },
|
|
67
|
+
operationID: { type: "string" },
|
|
68
|
+
command: { type: "string" },
|
|
69
|
+
dryRun: { type: "boolean" },
|
|
70
|
+
envelope: { type: "object" },
|
|
71
|
+
engine: engineSchema,
|
|
72
|
+
preview: previewSummary,
|
|
73
|
+
},
|
|
74
|
+
required: ["ok", "operationID", "command", "dryRun", "envelope", "engine"],
|
|
75
|
+
};
|
|
76
|
+
const applyOutput = {
|
|
77
|
+
type: "object",
|
|
78
|
+
properties: {
|
|
79
|
+
ok: { type: "boolean" },
|
|
80
|
+
operationID: { type: "string" },
|
|
81
|
+
command: { type: "string" },
|
|
82
|
+
envelope: { type: "object" },
|
|
83
|
+
engine: engineSchema,
|
|
84
|
+
},
|
|
85
|
+
required: ["ok", "operationID", "command", "envelope", "engine"],
|
|
86
|
+
};
|
|
87
|
+
const operationStatus = {
|
|
88
|
+
type: "object",
|
|
89
|
+
properties: {
|
|
90
|
+
operationID: { type: "string" },
|
|
91
|
+
command: { type: "string" },
|
|
92
|
+
status: { type: "string", enum: ["running", "completed", "cancelled", "failed"] },
|
|
93
|
+
startedAt: { type: "string" },
|
|
94
|
+
finishedAt: { type: "string" },
|
|
95
|
+
progressCount: { type: "number" },
|
|
96
|
+
},
|
|
97
|
+
required: ["operationID", "command", "status", "startedAt"],
|
|
98
|
+
};
|
|
99
|
+
export const SketchbookRpc = Rpc.define({
|
|
100
|
+
id: "sketchbook",
|
|
101
|
+
methods: {
|
|
102
|
+
capabilities: {
|
|
103
|
+
input: {
|
|
104
|
+
type: "object",
|
|
105
|
+
properties: {},
|
|
106
|
+
additionalProperties: false,
|
|
107
|
+
},
|
|
108
|
+
output: {
|
|
109
|
+
type: "object",
|
|
110
|
+
properties: {
|
|
111
|
+
protocolVersion: { type: "string" },
|
|
112
|
+
envelopeVersion: { type: "number" },
|
|
113
|
+
engine: {
|
|
114
|
+
type: "object",
|
|
115
|
+
properties: {
|
|
116
|
+
name: { type: "string" },
|
|
117
|
+
version: { type: "string" },
|
|
118
|
+
},
|
|
119
|
+
required: ["name", "version"],
|
|
120
|
+
},
|
|
121
|
+
commands: { type: "array" },
|
|
122
|
+
},
|
|
123
|
+
required: ["protocolVersion", "envelopeVersion", "engine", "commands"],
|
|
124
|
+
},
|
|
125
|
+
errors,
|
|
126
|
+
},
|
|
127
|
+
run: {
|
|
128
|
+
input: operationInput,
|
|
129
|
+
output: runOutput,
|
|
130
|
+
errors,
|
|
131
|
+
},
|
|
132
|
+
preview: {
|
|
133
|
+
input: operationInput,
|
|
134
|
+
output: runOutput,
|
|
135
|
+
errors,
|
|
136
|
+
},
|
|
137
|
+
apply: {
|
|
138
|
+
input: {
|
|
139
|
+
type: "object",
|
|
140
|
+
properties: {
|
|
141
|
+
operationID: { type: "string", description: "Preview operation ID to apply." },
|
|
142
|
+
},
|
|
143
|
+
required: ["operationID"],
|
|
144
|
+
},
|
|
145
|
+
output: applyOutput,
|
|
146
|
+
errors,
|
|
147
|
+
},
|
|
148
|
+
cancel: {
|
|
149
|
+
input: {
|
|
150
|
+
type: "object",
|
|
151
|
+
properties: {
|
|
152
|
+
operationID: { type: "string" },
|
|
153
|
+
},
|
|
154
|
+
required: ["operationID"],
|
|
155
|
+
},
|
|
156
|
+
output: {
|
|
157
|
+
type: "object",
|
|
158
|
+
properties: {
|
|
159
|
+
ok: { type: "boolean" },
|
|
160
|
+
operationID: { type: "string" },
|
|
161
|
+
cancelled: { type: "boolean" },
|
|
162
|
+
status: { type: "string" },
|
|
163
|
+
},
|
|
164
|
+
required: ["ok", "operationID", "cancelled", "status"],
|
|
165
|
+
},
|
|
166
|
+
errors,
|
|
167
|
+
},
|
|
168
|
+
status: {
|
|
169
|
+
input: {
|
|
170
|
+
type: "object",
|
|
171
|
+
properties: {
|
|
172
|
+
operationID: { type: "string", description: "Omit to list every tracked operation." },
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
output: {
|
|
176
|
+
type: "object",
|
|
177
|
+
properties: {
|
|
178
|
+
ok: { type: "boolean" },
|
|
179
|
+
operations: { type: "array", items: operationStatus },
|
|
180
|
+
},
|
|
181
|
+
required: ["ok", "operations"],
|
|
182
|
+
},
|
|
183
|
+
errors,
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
events: {
|
|
187
|
+
progress: {
|
|
188
|
+
schema: {
|
|
189
|
+
type: "object",
|
|
190
|
+
properties: {
|
|
191
|
+
operationID: { type: "string" },
|
|
192
|
+
command: { type: "string" },
|
|
193
|
+
sequence: { type: "number", description: "Monotonic per-operation progress counter." },
|
|
194
|
+
source: { type: "string", enum: ["stdout", "stderr"] },
|
|
195
|
+
line: { type: "string" },
|
|
196
|
+
},
|
|
197
|
+
required: ["operationID", "command", "sequence", "source", "line"],
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
operation: {
|
|
201
|
+
schema: {
|
|
202
|
+
type: "object",
|
|
203
|
+
properties: {
|
|
204
|
+
operationID: { type: "string" },
|
|
205
|
+
command: { type: "string" },
|
|
206
|
+
status: { type: "string", enum: ["running", "completed", "cancelled", "failed"] },
|
|
207
|
+
},
|
|
208
|
+
required: ["operationID", "command", "status"],
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { CatalogBaseline } from "../shared/catalog.js";
|
|
3
|
+
/**
|
|
4
|
+
* Registers the shared Sketchbook catalog as OpenCode surfaces: embedded
|
|
5
|
+
* skills, slash commands, and native subagents (one per skill agent brief).
|
|
6
|
+
* Everything comes from the bundled catalog baseline; no copies are defined
|
|
7
|
+
* here.
|
|
8
|
+
*/
|
|
9
|
+
export declare function setupCatalogSurfaces(ctx: Plugin.Context, baseline?: CatalogBaseline): Promise<void>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { loadCatalogBaseline, renderCommandBody } from "../shared/catalog.js";
|
|
2
|
+
/**
|
|
3
|
+
* Registers the shared Sketchbook catalog as OpenCode surfaces: embedded
|
|
4
|
+
* skills, slash commands, and native subagents (one per skill agent brief).
|
|
5
|
+
* Everything comes from the bundled catalog baseline; no copies are defined
|
|
6
|
+
* here.
|
|
7
|
+
*/
|
|
8
|
+
export async function setupCatalogSurfaces(ctx, baseline = loadCatalogBaseline()) {
|
|
9
|
+
await ctx.skill.transform((editor) => {
|
|
10
|
+
const existing = new Set(editor.list().map((skill) => skill.id));
|
|
11
|
+
for (const skill of baseline.skills) {
|
|
12
|
+
if (existing.has(skill.name))
|
|
13
|
+
continue; // the location already provides it
|
|
14
|
+
editor.add(embedSkill(skill));
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
await ctx.command.transform((editor) => {
|
|
18
|
+
for (const command of baseline.commands) {
|
|
19
|
+
editor.add(commandDefinition(ctx, command));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
await ctx.agent.transform((editor) => {
|
|
23
|
+
const existing = new Set(editor.list().map((agent) => agent.id));
|
|
24
|
+
for (const brief of baseline.skills.flatMap((skill) => skill.briefs)) {
|
|
25
|
+
const id = `sketchbook/${brief.name}`;
|
|
26
|
+
if (existing.has(id))
|
|
27
|
+
continue;
|
|
28
|
+
pushAgent(editor, nativeSubagent(id, brief));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function embedSkill(skill) {
|
|
33
|
+
return {
|
|
34
|
+
id: skill.name,
|
|
35
|
+
name: skill.name,
|
|
36
|
+
description: skill.description,
|
|
37
|
+
autoinvoke: skill.autoinvoke,
|
|
38
|
+
path: skill.source,
|
|
39
|
+
content: skill.content,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function commandDefinition(ctx, command) {
|
|
43
|
+
const description = `Sketchbook: ${command.description}`;
|
|
44
|
+
return {
|
|
45
|
+
name: command.name,
|
|
46
|
+
description: command.argumentHint ? `${description} (usage: ${command.argumentHint})` : description,
|
|
47
|
+
execute: async (invocation) => {
|
|
48
|
+
await ctx.session.prompt({
|
|
49
|
+
sessionID: invocation.sessionID,
|
|
50
|
+
text: renderCommandBody(command.content, invocation.prompt.text),
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function nativeSubagent(id, brief) {
|
|
56
|
+
return {
|
|
57
|
+
id: id,
|
|
58
|
+
name: brief.name,
|
|
59
|
+
description: brief.description,
|
|
60
|
+
mode: "subagent",
|
|
61
|
+
hidden: false,
|
|
62
|
+
request: { settings: {}, headers: {}, body: {} },
|
|
63
|
+
system: brief.content,
|
|
64
|
+
permissions: [],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// OpenCode 2.4's agent editor exposes list/get/default/update/remove but no
|
|
68
|
+
// `add`, and `list()` returns a copy, so mutating it is silently dropped on a
|
|
69
|
+
// real host. `update()` on a new id creates the agent.
|
|
70
|
+
function pushAgent(editor, agent) {
|
|
71
|
+
editor.update(agent.id, (created) => {
|
|
72
|
+
Object.assign(created, agent);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { Tool } from "@opencode/plugin/effect";
|
|
4
|
+
import { filePathFromToolInput } from "./guard-hooks.js";
|
|
5
|
+
import { inspectDecisionEdits, inspectDecisionWrite, } from "./decision-guards.js";
|
|
6
|
+
const FILE_TOOLS = new Set(["edit", "write", "patch"]);
|
|
7
|
+
function isDecisionPath(root, target) {
|
|
8
|
+
return resolve(root, target) === join(resolve(root), "spec/decisions/DECISIONS.md");
|
|
9
|
+
}
|
|
10
|
+
function editsFromToolInput(input) {
|
|
11
|
+
if (!input || typeof input !== "object")
|
|
12
|
+
return [];
|
|
13
|
+
const record = input;
|
|
14
|
+
if (Array.isArray(record.edits)) {
|
|
15
|
+
return record.edits.filter((edit) => Boolean(edit && typeof edit === "object"));
|
|
16
|
+
}
|
|
17
|
+
if (typeof record.old_string === "string" || typeof record.new_string === "string") {
|
|
18
|
+
return [
|
|
19
|
+
{
|
|
20
|
+
oldString: typeof record.old_string === "string" ? record.old_string : undefined,
|
|
21
|
+
newString: typeof record.new_string === "string" ? record.new_string : undefined,
|
|
22
|
+
},
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
export async function setupDecisionGuardHooks(ctx) {
|
|
28
|
+
const root = ctx.location.directory;
|
|
29
|
+
const decisionPath = join(root, "spec/decisions/DECISIONS.md");
|
|
30
|
+
const registration = await ctx.tool.hook("execute.before", async (event) => {
|
|
31
|
+
if (!FILE_TOOLS.has(event.tool))
|
|
32
|
+
return;
|
|
33
|
+
const target = filePathFromToolInput(event.input);
|
|
34
|
+
if (!target || !isDecisionPath(root, target))
|
|
35
|
+
return;
|
|
36
|
+
let current;
|
|
37
|
+
try {
|
|
38
|
+
current = await readFile(decisionPath, "utf8");
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const input = event.input;
|
|
44
|
+
const check = event.tool === "write" && typeof input.content === "string"
|
|
45
|
+
? inspectDecisionWrite(current, input.content)
|
|
46
|
+
: inspectDecisionEdits(current, editsFromToolInput(input));
|
|
47
|
+
if (check.allowed || !check.reason)
|
|
48
|
+
return;
|
|
49
|
+
throw new Tool.Error({
|
|
50
|
+
message: `Settled decisions stay settled: ${check.reason} Add a new SB entry that supersedes it instead.`,
|
|
51
|
+
metadata: { path: target },
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
return async () => {
|
|
55
|
+
await registration.dispose();
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface DecisionCheck {
|
|
2
|
+
allowed: boolean;
|
|
3
|
+
reason?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare function settledDecisions(content: string): string[];
|
|
6
|
+
export declare function inspectDecisionWrite(current: string, next: string): DecisionCheck;
|
|
7
|
+
export interface DecisionEdit {
|
|
8
|
+
oldString?: string;
|
|
9
|
+
newString?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function inspectDecisionEdits(current: string, edits: DecisionEdit[]): DecisionCheck;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const SETTLED = /### SB-\d{4} /;
|
|
2
|
+
const NEW_ENTRY = /^### SB-\d{4} /;
|
|
3
|
+
export function settledDecisions(content) {
|
|
4
|
+
return content
|
|
5
|
+
.split(/\n(?=### SB-\d{4} )/)
|
|
6
|
+
.slice(1)
|
|
7
|
+
.map((entry) => entry.trim())
|
|
8
|
+
.filter((entry) => SETTLED.test(entry) && !/\*\*Status:\*\* proposed/.test(entry));
|
|
9
|
+
}
|
|
10
|
+
export function inspectDecisionWrite(current, next) {
|
|
11
|
+
const missing = settledDecisions(current).find((entry) => !next.includes(entry));
|
|
12
|
+
if (!missing)
|
|
13
|
+
return { allowed: true };
|
|
14
|
+
return {
|
|
15
|
+
allowed: false,
|
|
16
|
+
reason: `this write would change or remove ${missing.slice(4, 11)}.`,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function inspectDecisionEdits(current, edits) {
|
|
20
|
+
const entries = settledDecisions(current);
|
|
21
|
+
for (const { oldString: oldText = "", newString: newText = "" } of edits) {
|
|
22
|
+
if (!oldText)
|
|
23
|
+
continue;
|
|
24
|
+
const touched = entries.find((entry) => entry.includes(oldText) || oldText.includes(entry.slice(0, 40)));
|
|
25
|
+
if (!touched)
|
|
26
|
+
continue;
|
|
27
|
+
if (!newText.includes(oldText)) {
|
|
28
|
+
return { allowed: false, reason: `this edit changes ${touched.slice(4, 11)}.` };
|
|
29
|
+
}
|
|
30
|
+
const added = newText.replace(oldText, "").replace(/^[\s-]+/, "");
|
|
31
|
+
if (added && !NEW_ENTRY.test(added)) {
|
|
32
|
+
return { allowed: false, reason: `this edit adds text to ${touched.slice(4, 11)}.` };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return { allowed: true };
|
|
36
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
export function resolveEngine(root) {
|
|
5
|
+
const project = resolveProjectEngine(root);
|
|
6
|
+
if (project)
|
|
7
|
+
return project;
|
|
8
|
+
return resolveBundledEngine();
|
|
9
|
+
}
|
|
10
|
+
function resolveProjectEngine(root) {
|
|
11
|
+
const packageJson = join(root, "package.json");
|
|
12
|
+
if (!existsSync(packageJson))
|
|
13
|
+
return undefined;
|
|
14
|
+
try {
|
|
15
|
+
const req = createRequire(packageJson);
|
|
16
|
+
const cliPath = req.resolve("@usesketchbook/cli/harness/cli.mjs");
|
|
17
|
+
return { source: "project", path: cliPath, version: manifestVersion(cliPath) };
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function resolveBundledEngine() {
|
|
24
|
+
const req = createRequire(import.meta.url);
|
|
25
|
+
const cliPath = req.resolve("@usesketchbook/cli/harness/cli.mjs");
|
|
26
|
+
return { source: "bundled", path: cliPath, version: manifestVersion(cliPath) };
|
|
27
|
+
}
|
|
28
|
+
// The CLI entry point lives at <package>/harness/cli.mjs, so the manifest is
|
|
29
|
+
// always one directory above it. Reading it directly avoids requiring the
|
|
30
|
+
// package to export "./package.json", which older published engines do not.
|
|
31
|
+
function manifestVersion(cliPath) {
|
|
32
|
+
const manifestPath = join(dirname(cliPath), "..", "package.json");
|
|
33
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
34
|
+
if (!manifest.version) {
|
|
35
|
+
throw new Error(`Sketchbook CLI manifest at ${manifestPath} has no version`);
|
|
36
|
+
}
|
|
37
|
+
return manifest.version;
|
|
38
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Tool } from "@opencode/plugin/effect";
|
|
2
|
+
import { inspectProtectedPath } from "./guards.js";
|
|
3
|
+
const FILE_TOOLS = new Set(["edit", "write", "patch"]);
|
|
4
|
+
const FILE_PATH_KEYS = ["file_path", "filePath", "path"];
|
|
5
|
+
export function filePathFromToolInput(input) {
|
|
6
|
+
if (!input || typeof input !== "object")
|
|
7
|
+
return undefined;
|
|
8
|
+
const record = input;
|
|
9
|
+
for (const key of FILE_PATH_KEYS) {
|
|
10
|
+
const value = record[key];
|
|
11
|
+
if (typeof value === "string" && value.trim())
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
export async function setupGuardHooks(ctx) {
|
|
17
|
+
const root = ctx.location.directory;
|
|
18
|
+
const registration = await ctx.tool.hook("execute.before", (event) => {
|
|
19
|
+
if (!FILE_TOOLS.has(event.tool))
|
|
20
|
+
return;
|
|
21
|
+
const target = filePathFromToolInput(event.input);
|
|
22
|
+
if (!target)
|
|
23
|
+
return;
|
|
24
|
+
const inspected = inspectProtectedPath(root, target);
|
|
25
|
+
if (!inspected.protected)
|
|
26
|
+
return;
|
|
27
|
+
throw new Tool.Error({
|
|
28
|
+
message: `${target} is generated output (${inspected.pattern}). Change the Sketchbook source and rebuild instead.`,
|
|
29
|
+
metadata: { path: target, pattern: inspected.pattern },
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
return async () => {
|
|
33
|
+
await registration.dispose();
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const PROTECTED_PATH_PATTERNS: readonly ["generated/**", "src/components/*/*.styles.ts", "src/index.ts", "spec/tokens/foundation/**", "spec/tokens/interface.json"];
|
|
2
|
+
export interface ProtectedPath {
|
|
3
|
+
protected: boolean;
|
|
4
|
+
pattern?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Whether a project-relative path is protected generated output.
|
|
8
|
+
*
|
|
9
|
+
* Paths outside the project are not protected by this guard; they are handled by
|
|
10
|
+
* OpenCode's own external-directory boundary.
|
|
11
|
+
*/
|
|
12
|
+
export declare function inspectProtectedPath(root: string, target: string): ProtectedPath;
|
|
13
|
+
export declare function isProtectedPath(root: string, target: string): boolean;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve } from "node:path";
|
|
2
|
+
// Provider-neutral generated-file policy shared by client adapters.
|
|
3
|
+
// The paths mirror the project guidance: these outputs are rebuilt by Sketchbook.
|
|
4
|
+
export const PROTECTED_PATH_PATTERNS = [
|
|
5
|
+
"generated/**",
|
|
6
|
+
"src/components/*/*.styles.ts",
|
|
7
|
+
"src/index.ts",
|
|
8
|
+
"spec/tokens/foundation/**",
|
|
9
|
+
"spec/tokens/interface.json",
|
|
10
|
+
];
|
|
11
|
+
const escapeRegex = (value) => value.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
12
|
+
const matchPattern = (path, pattern) => {
|
|
13
|
+
const source = escapeRegex(pattern)
|
|
14
|
+
.replaceAll("**", "\u0000")
|
|
15
|
+
.replaceAll("*", "[^/]*")
|
|
16
|
+
.replaceAll("\u0000", ".*");
|
|
17
|
+
return new RegExp(`^${source}$`).test(path);
|
|
18
|
+
};
|
|
19
|
+
const normalizePath = (value) => value.replaceAll("\\", "/");
|
|
20
|
+
function isWithin(root, target) {
|
|
21
|
+
const rel = relative(resolve(root), resolve(target));
|
|
22
|
+
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether a project-relative path is protected generated output.
|
|
26
|
+
*
|
|
27
|
+
* Paths outside the project are not protected by this guard; they are handled by
|
|
28
|
+
* OpenCode's own external-directory boundary.
|
|
29
|
+
*/
|
|
30
|
+
export function inspectProtectedPath(root, target) {
|
|
31
|
+
if (!isWithin(root, target))
|
|
32
|
+
return { protected: false };
|
|
33
|
+
const relativePath = normalizePath(relative(resolve(root), resolve(target)));
|
|
34
|
+
for (const pattern of PROTECTED_PATH_PATTERNS) {
|
|
35
|
+
if (matchPattern(relativePath, pattern))
|
|
36
|
+
return { protected: true, pattern };
|
|
37
|
+
}
|
|
38
|
+
return { protected: false };
|
|
39
|
+
}
|
|
40
|
+
export function isProtectedPath(root, target) {
|
|
41
|
+
return inspectProtectedPath(root, target).protected;
|
|
42
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { resolveEngine } from "./engine.js";
|
|
2
|
+
import { parseEnvelope } from "../shared/envelope.js";
|
|
3
|
+
import { type CapabilitiesManifest, type ManifestCommand } from "../shared/manifest.js";
|
|
4
|
+
import type { Envelope } from "../shared/envelope.js";
|
|
5
|
+
import type { Plugin } from "@opencode/plugin";
|
|
6
|
+
export declare class OperationError extends Error {
|
|
7
|
+
readonly command: string;
|
|
8
|
+
readonly code = "SKETCHBOOK_OPERATION_ERROR";
|
|
9
|
+
constructor(command: string, message: string);
|
|
10
|
+
}
|
|
11
|
+
export declare class StalePreviewError extends Error {
|
|
12
|
+
readonly code = "SKETCHBOOK_STALE_PREVIEW";
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
export interface LoadCapabilitiesOptions {
|
|
16
|
+
refresh?: boolean;
|
|
17
|
+
permission?: Plugin.Context["permission"];
|
|
18
|
+
sessionID?: string;
|
|
19
|
+
}
|
|
20
|
+
export declare function loadCapabilities(root: string, { refresh, permission, sessionID }?: LoadCapabilitiesOptions): Promise<CapabilitiesManifest>;
|
|
21
|
+
export declare function findCommand(manifest: CapabilitiesManifest, command: string, subcommand?: string): ManifestCommand;
|
|
22
|
+
export declare function splitInput(input: Record<string, unknown>, command: ManifestCommand): {
|
|
23
|
+
args: string[];
|
|
24
|
+
options: Record<string, string | boolean | undefined>;
|
|
25
|
+
};
|
|
26
|
+
export interface OperationInput {
|
|
27
|
+
root: string;
|
|
28
|
+
manifest?: CapabilitiesManifest;
|
|
29
|
+
command: string;
|
|
30
|
+
subcommand?: string;
|
|
31
|
+
args?: string[];
|
|
32
|
+
options?: Record<string, string | boolean | undefined>;
|
|
33
|
+
dryRun?: boolean;
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
onLine?: (line: string, source: "stdout" | "stderr") => void;
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
maxOutputBytes?: number;
|
|
38
|
+
permission?: Plugin.Context["permission"];
|
|
39
|
+
sessionID?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface OperationResult {
|
|
42
|
+
engine: ReturnType<typeof resolveEngine>;
|
|
43
|
+
envelope: ReturnType<typeof parseEnvelope>;
|
|
44
|
+
command: ManifestCommand;
|
|
45
|
+
dryRun: boolean;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Runs one validated Sketchbook operation.
|
|
49
|
+
*
|
|
50
|
+
* The manifest is supplied by the caller so this function can later be reused by
|
|
51
|
+
* tools, RPC, TUI, and Desktop without re-executing the engine for registration.
|
|
52
|
+
*/
|
|
53
|
+
export declare function executeOperation(input: OperationInput): Promise<OperationResult>;
|
|
54
|
+
export interface OperationIdentity {
|
|
55
|
+
root: string;
|
|
56
|
+
engine: {
|
|
57
|
+
path: string;
|
|
58
|
+
version: string;
|
|
59
|
+
};
|
|
60
|
+
command: string;
|
|
61
|
+
subcommand?: string;
|
|
62
|
+
args: string[];
|
|
63
|
+
options: Record<string, string | boolean>;
|
|
64
|
+
dryRun: boolean;
|
|
65
|
+
}
|
|
66
|
+
export type PreviewStatus = "pending" | "applied" | "stale";
|
|
67
|
+
export interface PreviewRecord {
|
|
68
|
+
readonly id: string;
|
|
69
|
+
readonly createdAt: string;
|
|
70
|
+
status: PreviewStatus;
|
|
71
|
+
readonly identity: OperationIdentity;
|
|
72
|
+
readonly fingerprint: string;
|
|
73
|
+
readonly plan: Envelope;
|
|
74
|
+
readonly manifest?: CapabilitiesManifest;
|
|
75
|
+
lock?: Promise<unknown>;
|
|
76
|
+
}
|
|
77
|
+
export declare function stableStringify(value: unknown): string;
|
|
78
|
+
export declare function operationIdentity(input: Pick<OperationInput, "root" | "command" | "subcommand" | "args" | "options">, engine: ReturnType<typeof resolveEngine>, dryRun: boolean): OperationIdentity;
|
|
79
|
+
export declare function fingerprintOperation(identity: OperationIdentity, plan: Envelope): string;
|
|
80
|
+
export type PreviewInput = Omit<OperationInput, "dryRun">;
|
|
81
|
+
export declare function previewOperation(input: PreviewInput): Promise<PreviewRecord>;
|
|
82
|
+
export interface ApplyPreviewInput {
|
|
83
|
+
operationID: string;
|
|
84
|
+
root: string;
|
|
85
|
+
permission?: Plugin.Context["permission"];
|
|
86
|
+
sessionID?: string;
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
onLine?: (line: string, source: "stdout" | "stderr") => void;
|
|
89
|
+
timeoutMs?: number;
|
|
90
|
+
maxOutputBytes?: number;
|
|
91
|
+
}
|
|
92
|
+
export declare function applyPreview(input: ApplyPreviewInput): Promise<OperationResult>;
|
|
93
|
+
export declare function getPreview(operationID: string): PreviewRecord | undefined;
|