opencode-dev-framework 0.1.5 → 0.1.6
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 +33 -0
- package/dist/gate.js +9 -0
- package/dist/index.js +96 -64
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,6 +43,39 @@ Then add it to your project's `opencode.json`:
|
|
|
43
43
|
}
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
## Local development and testing
|
|
47
|
+
|
|
48
|
+
To test the plugin from source without publishing to npm, point OpenCode at the
|
|
49
|
+
local repository path in your project's `opencode.json`:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"plugin": ["/home/jerome/opencode-dev-framework"]
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The path must be the **repository root** (where `package.json` lives). OpenCode
|
|
58
|
+
loads `dist/index.js`, so rebuild after every source change:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm run build
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Important caveats
|
|
65
|
+
|
|
66
|
+
- The `opencode plugin <module>` command creates a `.opencode/opencode.json`
|
|
67
|
+
file in the project directory that takes precedence over the project-level
|
|
68
|
+
`opencode.json`. If you used that command while testing, either delete
|
|
69
|
+
`.opencode/opencode.json` or make sure it also points to the local path.
|
|
70
|
+
- OpenCode caches downloaded plugins in
|
|
71
|
+
`~/.cache/opencode/packages/opencode-dev-framework*/`. If you previously
|
|
72
|
+
loaded a published version and then switch to a local source, clear that
|
|
73
|
+
cache so OpenCode does not reuse the old build:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
rm -rf ~/.cache/opencode/packages/opencode-dev-framework*
|
|
77
|
+
```
|
|
78
|
+
|
|
46
79
|
## Configuration
|
|
47
80
|
|
|
48
81
|
Create `.opencode-dev-framework.yml` in your project root:
|
package/dist/gate.js
CHANGED
|
@@ -49,6 +49,15 @@ export function substituteFiles(template, files) {
|
|
|
49
49
|
* aggregated — a failing step does not abort the remaining steps.
|
|
50
50
|
*/
|
|
51
51
|
export async function runGate(run, config, changedFiles, options) {
|
|
52
|
+
if (!config.gate) {
|
|
53
|
+
return {
|
|
54
|
+
ran: false,
|
|
55
|
+
reason: "incomplete plugin config: gate section missing",
|
|
56
|
+
ok: true,
|
|
57
|
+
steps: [],
|
|
58
|
+
failedSteps: [],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
52
61
|
if (config.gate.skip_unchanged && changedFiles.length === 0) {
|
|
53
62
|
return { ran: false, reason: "no changed files", steps: [], ok: true, failedSteps: [] };
|
|
54
63
|
}
|
package/dist/index.js
CHANGED
|
@@ -14,78 +14,46 @@ import { isLintFailure, lintFile, summarizeLint } from "./lint.js";
|
|
|
14
14
|
import { createLogger } from "./logger.js";
|
|
15
15
|
import { checkToolCall } from "./protect.js";
|
|
16
16
|
import { injectConstitution, loadConstitution } from "./rules.js";
|
|
17
|
+
/**
|
|
18
|
+
* Per-directory hook state registry. OpenCode's effect runtime can strip
|
|
19
|
+
* closure variables when invoking hooks asynchronously, so we store the state
|
|
20
|
+
* in module-level maps and look it up at call time.
|
|
21
|
+
*/
|
|
22
|
+
const hookRegistry = new Map();
|
|
23
|
+
let activeDirectory = null;
|
|
24
|
+
function getHookState(directoryHint) {
|
|
25
|
+
const directory = directoryHint ?? activeDirectory;
|
|
26
|
+
if (!directory) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
return hookRegistry.get(directory) ?? null;
|
|
30
|
+
}
|
|
17
31
|
/**
|
|
18
32
|
* Build the plugin's hooks. Exported (rather than inlined in the plugin) so
|
|
19
33
|
* tests can inject a stubbed command runner and tracker.
|
|
20
34
|
*/
|
|
21
35
|
export function buildHooks(ctx, config, log, run = runCommand, tracker = createChangedFileTracker(), constitution = null) {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const outcome = await lintFile(run, config, filePath, {
|
|
28
|
-
cwd: ctx.directory,
|
|
29
|
-
timeout: config.gate.timeout,
|
|
30
|
-
});
|
|
31
|
-
if (outcome.skipped) {
|
|
32
|
-
await log("debug", summarizeLint(outcome), { filePath, reason: outcome.reason });
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
if (!isLintFailure(outcome)) {
|
|
36
|
-
await log("info", summarizeLint(outcome), { filePath });
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
const summary = summarizeLint(outcome);
|
|
40
|
-
await log("error", summary, {
|
|
41
|
-
filePath,
|
|
42
|
-
command: outcome.command?.join(" "),
|
|
43
|
-
stdout: outcome.result?.stdout,
|
|
44
|
-
stderr: outcome.result?.stderr,
|
|
45
|
-
});
|
|
46
|
-
// In strict mode lint failures throw. Note: file.edited is an event
|
|
47
|
-
// notification (the edit already happened), so this cannot undo the
|
|
48
|
-
// edit — it surfaces the failure loudly to the session.
|
|
49
|
-
if (config.profile === "strict") {
|
|
50
|
-
throw new Error(`[opencode-dev-framework] ${summary}`);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const onSessionIdle = async () => {
|
|
54
|
-
const changedFiles = tracker.getChangedFiles();
|
|
55
|
-
const report = await runGate(run, config, changedFiles, { cwd: ctx.directory });
|
|
56
|
-
tracker.clearChangedFiles();
|
|
57
|
-
const summary = summarizeGate(report);
|
|
58
|
-
if (!report.ran) {
|
|
59
|
-
await log("debug", summary);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (report.ok) {
|
|
63
|
-
await log("info", summary);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
// The gate cannot physically block (session.idle fires after the turn
|
|
67
|
-
// ends), so failure visibility is the enforcement mechanism.
|
|
68
|
-
const level = config.gate.block_on_failure ? "error" : "warn";
|
|
69
|
-
await log(level, summary, {
|
|
70
|
-
failedSteps: report.failedSteps.map((step) => ({
|
|
71
|
-
name: step.name,
|
|
72
|
-
command: step.command?.join(" "),
|
|
73
|
-
exitCode: step.result?.exitCode,
|
|
74
|
-
timedOut: step.result?.timedOut,
|
|
75
|
-
stderr: step.result?.stderr,
|
|
76
|
-
})),
|
|
77
|
-
});
|
|
78
|
-
};
|
|
36
|
+
const directory = ctx.directory;
|
|
37
|
+
hookRegistry.set(directory, { config, log, run, tracker, constitution });
|
|
38
|
+
activeDirectory = directory;
|
|
79
39
|
return {
|
|
80
40
|
"experimental.chat.system.transform": async (_input, output) => {
|
|
81
|
-
const
|
|
41
|
+
const state = getHookState();
|
|
42
|
+
if (!state) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const next = injectConstitution(output.system, state.constitution);
|
|
82
46
|
if (next !== output.system) {
|
|
83
|
-
await log("info", "constitution injected into system prompt");
|
|
47
|
+
await state.log("info", "constitution injected into system prompt");
|
|
84
48
|
output.system = next;
|
|
85
49
|
}
|
|
86
50
|
},
|
|
87
51
|
"tool.execute.before": async (input, output) => {
|
|
88
|
-
const
|
|
52
|
+
const state = getHookState();
|
|
53
|
+
if (!state) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const result = checkToolCall(state.config, input.tool, output.args, activeDirectory ?? undefined);
|
|
89
57
|
if (result.decision === "allow") {
|
|
90
58
|
return;
|
|
91
59
|
}
|
|
@@ -96,19 +64,83 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
|
|
|
96
64
|
pattern: result.matchedPattern,
|
|
97
65
|
};
|
|
98
66
|
if (result.decision === "warn") {
|
|
99
|
-
await log("warn", message, extra);
|
|
67
|
+
await state.log("warn", message, extra);
|
|
100
68
|
return;
|
|
101
69
|
}
|
|
102
|
-
await log("error", message, extra);
|
|
70
|
+
await state.log("error", message, extra);
|
|
103
71
|
throw new Error(`[opencode-dev-framework] ${message}`);
|
|
104
72
|
},
|
|
105
73
|
event: async ({ event }) => {
|
|
74
|
+
const state = getHookState();
|
|
75
|
+
if (!state) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
106
78
|
if (event.type === "file.edited") {
|
|
107
|
-
|
|
79
|
+
const filePath = event.properties.file;
|
|
80
|
+
state.tracker.add(filePath, activeDirectory ?? undefined);
|
|
81
|
+
if (!state.config.on_edit.lint) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const outcome = await lintFile(state.run, state.config, filePath, {
|
|
85
|
+
cwd: activeDirectory ?? undefined,
|
|
86
|
+
timeout: state.config.gate?.timeout,
|
|
87
|
+
});
|
|
88
|
+
if (outcome.skipped) {
|
|
89
|
+
await state.log("debug", summarizeLint(outcome), { filePath, reason: outcome.reason });
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!isLintFailure(outcome)) {
|
|
93
|
+
await state.log("info", summarizeLint(outcome), { filePath });
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const summary = summarizeLint(outcome);
|
|
97
|
+
await state.log("error", summary, {
|
|
98
|
+
filePath,
|
|
99
|
+
command: outcome.command?.join(" "),
|
|
100
|
+
stdout: outcome.result?.stdout,
|
|
101
|
+
stderr: outcome.result?.stderr,
|
|
102
|
+
});
|
|
103
|
+
// In strict mode lint failures throw. Note: file.edited is an event
|
|
104
|
+
// notification (the edit already happened), so this cannot undo the
|
|
105
|
+
// edit — it surfaces the failure loudly to the session.
|
|
106
|
+
if (state.config.profile === "strict") {
|
|
107
|
+
throw new Error(`[opencode-dev-framework] ${summary}`);
|
|
108
|
+
}
|
|
108
109
|
return;
|
|
109
110
|
}
|
|
110
111
|
if (event.type === "session.idle") {
|
|
111
|
-
|
|
112
|
+
if (!state.config.gate) {
|
|
113
|
+
await state.log("warn", "plugin config is incomplete (missing gate section), skipping completion gate", {
|
|
114
|
+
directory: activeDirectory ?? undefined,
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const changedFiles = state.tracker.getChangedFiles();
|
|
119
|
+
const report = await runGate(state.run, state.config, changedFiles, {
|
|
120
|
+
cwd: activeDirectory ?? undefined,
|
|
121
|
+
});
|
|
122
|
+
state.tracker.clearChangedFiles();
|
|
123
|
+
const summary = summarizeGate(report);
|
|
124
|
+
if (!report.ran) {
|
|
125
|
+
await state.log("debug", summary);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (report.ok) {
|
|
129
|
+
await state.log("info", summary);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
// The gate cannot physically block (session.idle fires after the turn
|
|
133
|
+
// ends), so failure visibility is the enforcement mechanism.
|
|
134
|
+
const level = state.config.gate.block_on_failure ? "error" : "warn";
|
|
135
|
+
await state.log(level, summary, {
|
|
136
|
+
failedSteps: report.failedSteps.map((step) => ({
|
|
137
|
+
name: step.name,
|
|
138
|
+
command: step.command?.join(" "),
|
|
139
|
+
exitCode: step.result?.exitCode,
|
|
140
|
+
timedOut: step.result?.timedOut,
|
|
141
|
+
stderr: step.result?.stderr,
|
|
142
|
+
})),
|
|
143
|
+
});
|
|
112
144
|
}
|
|
113
145
|
},
|
|
114
146
|
};
|