← Back to Plugins
Channels

Channel Context Inject

manos By manos 👁 41 views ▲ 0 votes

OpenClaw plugin: prepend recent channel messages (incl. cron output) to every turn via before_prompt_build. Fail-open.

GitHub

Install

npm install &&

README

# channel-context-inject

An [OpenClaw](https://github.com/openclaw/openclaw) plugin that prepends the
last N recent channel/session exchanges to **every** agent turn via the
`before_prompt_build` hook — so recent messages (including cron output that
posted to the channel) are always in the model's context before it replies.

## Why

OpenClaw agents are instructed to "read recent messages before replying," but
an instruction can be skipped at compose time. This plugin makes the recent
context *structurally present* in the prompt instead of relying on the model to
go fetch it — closing the class of failures where the agent references a stale
or wrong prior message, or misses a cron result that landed in the channel.

## What it does

On `before_prompt_build`:

1. Reads the last `maxTurns` entries from `event.messages`.
2. Strips system/tool noise, normalizes roles, truncates each message to
   `perMessageChars`.
3. Returns `{ prependContext }` — a compact block like:

   ```
   Recent channel messages (auto-injected — read these before replying; cron output lands here too):
   - Charlie: sounds like the cron is looking in the wrong place
   - Emily: Let me check the actual cron config...
   ```

## Safety

Fully **fail-open**. The handler is wrapped in a hard timeout + try/catch and
returns `undefined` on any error or timeout, so the turn always proceeds. The
hook can never block, throw into, or wedge the gateway. Worst case is "no
injection this turn."

## Config

| Key               | Default | Description                                          |
| ----------------- | ------- | ---------------------------------------------------- |
| `enabled`         | `true`  | Master switch.                                       |
| `maxTurns`        | `12`    | How many recent messages to prepend.                 |
| `maxChars`        | `6000`  | Hard cap on total injected characters.               |
| `perMessageChars` | `800`   | Per-message truncation length.                       |
| `timeoutMs`       | `250`   | Hook deadline; on timeout, no injection (fail-open). |
| `excludeAgents`   | `[]`    | Agent ids that never receive injection.              |
| `contextLabel`    | (see manifest) | Header line for the injected block.           |

## Install

```bash
npm install && npm run build
openclaw plugins install .
# Loading a NEW hook requires a FULL gateway restart (not SIGUSR1 hot-reload):
sudo systemctl restart openclaw-<instance>.service
```

## Note on `openclaw plugins inspect`

`inspect` reports `hookCount: 0` / `hookNames: []` for this plugin even when the
hook is live, because it counts manifest-*declared* hook contracts and this
plugin registers at runtime via `api.on("before_prompt_build", ...)`. Ground
truth is the injected block appearing in context, or the log line
`[channel-context-inject] injected N chars`.

## License

MIT
channels

Comments

Sign in to leave a comment

Loading comments...