Skip to content

Commit abb5934

Browse files
committed
fix(cavekit): drop messages-transform hook, use synthetic part instead
Old two-hook design caused provider error 2013 (messages must not be empty) when messages.transform set output.messages = []. New design uses single command.execute.before hook with synthetic: true part to signal no LLM reply. Refs anomalyco/opencode#9306
1 parent 84c5d66 commit abb5934

5 files changed

Lines changed: 51 additions & 61 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Each module registers TypeScript hooks into OpenCode's plugin lifecycle. The thr
152152
```
153153
CaveOpenPlugin
154154
├── caveman → experimental.chat.system.transform, chat.message, command.execute.before, event
155-
├── cavekit → experimental.chat.messages.transform, command.execute.before, config
155+
├── cavekit → command.execute.before, config
156156
└── cavemem → experimental.chat.system.transform, chat.message, tool.execute.after, event, dispose
157157
```
158158

‎docs/CAVEKIT_HOOKS.md‎

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,43 @@ Cavekit in CaveOpen is scoped: cavekit v4. CaveOpen ports the spec harness — `
2121

2222
Ensures `FORMAT.md` exists at project root. No args. CLI install already handles the global case — this hook only concerns the current project.
2323

24-
**Pattern: mutate `output.parts` with `ignored: true` + `experimental.chat.messages.transform` command check**
24+
**Pattern: two parts — `ignored: true` (user-visible result) + `synthetic: true` (no-LLM signal)**
2525

26-
Two hooks in concert — no shared state needed:
26+
Single hook, no `messages.transform`:
2727

28-
1. `command.execute.before` — copies file, replaces `output.parts` with a single `{ type: "text", ignored: true }` part containing the result message. This surfaces output in the TUI without LLM inference.
29-
2. `experimental.chat.messages.transform` — if the last user message text is `/ck:init`, empties `output.messages` → LLM inference skipped entirely.
28+
`command.execute.before` copies the file, then replaces `output.parts` with two entries:
29+
1. `{ type: "text", ignored: true }` — the result message, shown in the TUI, excluded from LLM context.
30+
2. `{ type: "text", synthetic: true }` — signals to OpenCode that the command produced its own response and no LLM reply is needed.
3031

3132
**`hooks/command.ts`:**
3233

3334
```ts
34-
output.parts.splice(0, output.parts.length, {
35-
id: output.parts[0].id,
36-
messageID: output.parts[0].messageID,
37-
sessionID: input.sessionID,
38-
type: "text",
39-
text, // "FORMAT.md copied to ..." or "FORMAT.md already exists at ..."
40-
ignored: true,
41-
});
35+
output.parts.splice(
36+
0,
37+
output.parts.length,
38+
{
39+
id: output.parts[0].id,
40+
messageID: output.parts[0].messageID,
41+
sessionID: input.sessionID,
42+
type: "text",
43+
text, // "FORMAT.md copied to ..." or "FORMAT.md already exists at ..."
44+
ignored: true,
45+
},
46+
{
47+
id: partId(),
48+
messageID: output.parts[0].messageID,
49+
sessionID: input.sessionID,
50+
type: "text",
51+
text: "FORMAT.md copied, no further action.",
52+
synthetic: true,
53+
},
54+
);
4255
```
4356

4457
Source path: `path.join(__dirname, "../assets/FORMAT.md")` — bundled alongside the plugin.
4558

46-
**`hooks/messages-transform.ts`:**
47-
48-
```ts
49-
"experimental.chat.messages.transform": async (_input, output) => {
50-
const last = output.messages.at(-1);
51-
if (!last || last.info.role !== "user") return;
52-
53-
const isInit = last.parts.some(
54-
(p) => p.type === "text" && (p as TextPart).text.trim() === "/ck:init",
55-
);
56-
if (!isInit) return;
57-
58-
output.messages = []; // drop /ck:init → no LLM inference
59-
},
60-
```
59+
> **Why not `experimental.chat.messages.transform`?**
60+
> An earlier design used that hook to set `output.messages = []`, intending to skip LLM inference entirely. This caused provider error 2013 (`messages must not be empty`) because OpenCode still dispatches the LLM call after the transform. Tracked upstream as [anomalyco/opencode#9306](https://github.com/anomalyco/opencode/issues/9306) — when `noReply` lands, the `synthetic` part can be removed.
6161
6262
**No caching impact.** File copy is a pure side effect — no model context mutated.
6363

@@ -111,7 +111,7 @@ export const CaveOpenPlugin: Plugin = async (ctx) =>
111111
mergeHooks(cavemanHooks(ctx), caveMemHooks(ctx), cavekitHooks(ctx));
112112
```
113113

114-
Cavekit no longer contributes a `system.transform` or `event` handler, so merging is simpler — only `command.execute.before`, `experimental.chat.messages.transform`, and `config` come from this module.
114+
Cavekit no longer contributes a `system.transform` or `event` handler, so merging is simpler — only `command.execute.before` and `config` come from this module.
115115

116116
---
117117

@@ -121,8 +121,7 @@ Cavekit no longer contributes a `system.transform` or `event` handler, so mergin
121121
src/modules/cavekit/
122122
index.ts # cavekitHooks(ctx) factory — composes all hooks
123123
hooks/
124-
command.ts # command.execute.before: /ck:init copy FORMAT.md + noReply
125-
messages-transform.ts # experimental.chat.messages.transform: drop /ck:init turn
124+
command.ts # command.execute.before: /ck:init copy FORMAT.md + synthetic part
126125
set-config.ts # config hook: registers /ck:init as named slash command
127126
```
128127

‎src/modules/cavekit/hooks/command.ts‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "node:fs/promises";
33
import path from "node:path";
44
import { fileURLToPath } from "node:url";
55
import type { Hooks, PluginInput } from "@opencode-ai/plugin";
6-
import { log } from "node:console";
6+
import { partId } from "../../../lib/cuid.js";
77

88
export function commandExecuteBeforeHook(
99
ctx: PluginInput,
@@ -23,13 +23,25 @@ export function commandExecuteBeforeHook(
2323
return `FORMAT.md copied to ${destFormat}\nNext: run /ck:spec to create SPEC.md`;
2424
})();
2525

26-
output.parts.splice(0, output.parts.length, {
27-
id: output.parts[0].id,
28-
messageID: output.parts[0].messageID,
29-
sessionID: input.sessionID,
30-
type: "text",
31-
text,
32-
ignored: true,
33-
});
26+
output.parts.splice(
27+
0,
28+
output.parts.length,
29+
{
30+
id: output.parts[0].id,
31+
messageID: output.parts[0].messageID,
32+
sessionID: input.sessionID,
33+
type: "text",
34+
text,
35+
ignored: true,
36+
},
37+
{
38+
id: partId(),
39+
messageID: output.parts[0].messageID,
40+
sessionID: input.sessionID,
41+
type: "text",
42+
text: "FORMAT.md copied, no futher action.",
43+
synthetic: true,
44+
},
45+
);
3446
};
3547
}

‎src/modules/cavekit/hooks/messages-transform.ts‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎src/modules/cavekit/index.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { Plugin, PluginInput, Hooks } from "@opencode-ai/plugin";
22
import { commandExecuteBeforeHook } from "./hooks/command.js";
33
import { setConfig } from "./hooks/set-config.js";
4-
import { messagesTransformHook } from "./hooks/messages-transform.js";
54

65
export function cavekitHooks(ctx: PluginInput): Hooks {
76
return {
87
"command.execute.before": commandExecuteBeforeHook(ctx),
9-
"experimental.chat.messages.transform": messagesTransformHook(ctx),
108
"config": setConfig(ctx),
119
};
1210
}

0 commit comments

Comments
 (0)