Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/adaptive-thinking-v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"opencode-adaptive-thinking": minor
---

Port the plugin to the OpenCode V2 plugin API. The implementation now default-exports a
`Plugin.define` definition, registers the reasoning-effort tool and hooks on the V2
domains, and is distributed as a V2 server plugin.

**OpenCode 1 is no longer supported**: this package does not load in OpenCode 1.
5 changes: 5 additions & 0 deletions .changeset/llm-classifier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"opencode-adaptive-thinking": minor
---

Add optional `afterIdle` / `afterUserMessage` triggers for the guidance and an optional LLM classifier. The classifier uses the same guidance, the last compaction summary or the current segment as context, and the current message last. Session state is bounded at 5,000 entries, expires after seven days, and is removed when a session is deleted.
5 changes: 5 additions & 0 deletions .changeset/quiet-resets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"opencode-adaptive-thinking": patch
---

Reset a temporary reasoning effort to the level in effect before the first override, so repeated changes no longer reset to an intermediate level or skip the reset. Read session history once per tool call with a bounded query, cache provider variants, merge guidance into an existing first system prompt, and remove the runtime Zod dependency.
7 changes: 0 additions & 7 deletions .opencode/opencode.jsonc

This file was deleted.

68 changes: 54 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The plugin injects adaptive-thinking guidance into the system prompt and exposes

## Requirements

- OpenCode with plugin support compatible with `@opencode-ai/plugin` `^1.14.24`.
- OpenCode `2` with a plugin runtime compatible with `@opencode/plugin` `2.0.30`.
- A selected model that exposes reasoning-effort variants such as `none`, `low`, `medium`, `high`, or `xhigh`.

## Installation
Expand All @@ -26,38 +26,74 @@ Add the package to your OpenCode config plugin list:
```json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-adaptive-thinking"]
"plugins": ["opencode-adaptive-thinking"]
}
```

To customize behavior, use the tuple form shown in the configuration example below.
To customize behavior, use the object form shown in the configuration example below.

## Configuration

The plugin accepts these optional settings:

- `enabled`: enable or disable the plugin without removing it from config. Defaults to `true`.
- `quiet`: suppress toast notifications for configuration errors. Defaults to `false`.
- `quiet`: suppress the generic configuration error message. Details go to the server process output (stderr) and the last one is recorded under the `lastDiagnostic` storage key. Defaults to `false`.
- `toolName`: name of the reasoning-effort tool exposed to agents. Defaults to `set_reasoning_effort`.
- `toolDescription`: description for the reasoning-effort tool. Defaults to `Set your reasoning effort`.
- `systemPrompt`: custom adaptive-thinking guidance injected into the system prompt.
- `triggers` (optional): when to inject the guidance and run the classifier. Fields: `afterIdle` and `afterUserMessage` (booleans). Omit `triggers` for the original behavior (guidance on every request); both `false` disables the guidance.
- `classifier` (optional): an LLM classifier that chooses the effort on real user messages. Disabled by default. Fields: `enabled` (boolean) and `model` (`{ providerID, modelID }`, default `opencode/big-pickle`).

### Example
### Triggers and classifier

`triggers` scopes when the guidance is injected:

- `afterIdle`: first request after an idle or session start, when no user message is pending.
- `afterUserMessage`: first request after a real user message.

Both are reset when a session execution ends (success, failure, or interruption). Omit `triggers` for the original behavior (guidance on every request). Both `false` disables the guidance.

When `classifier.enabled` is true, the plugin asks the configured model for one valid effort level on real user messages. It uses the same guidance as its system prompt, then the last compaction summary or the messages since session start/last idle, then the current message last. The call is a stateless generation request with no tools and a 5 s timeout; it fails open, so the request keeps its current level and no error reaches the request or the TUI; classifier failures print to stderr only the first time, and every failure is recorded as the last diagnostic. A malformed successful answer is reported as an invalid classifier response. The chosen level is applied to the request that carries the user message and, unless a tool call or explicit variant choice changes it, re-derived for later primary requests in the same turn: the plugin copies the selected catalog variant's request settings into each such request's options and applies the variant's headers or body overrides through request-scoped hooks, on every transport attempt of that call. A variant declares a body override as `extraBody` in its catalog settings (the host exposes it as the variant's `body`); `headers` is applied as-is. The request model ref is stamped with the level so the assistant message records the variant that was actually used, but the level is never written to the session model. The plugin does not override later user or agent variant choices, and it does not claim a level when the catalog has no matching variant or the variant carries no applicable request settings. When the request model exposes variants, the main session still receives the guidance at the triggers.

The `http.request` hook covers the documented HTTP path. A variant whose only mapping is a `body` override is not applied to `experimental.ws.*` requests.

Classifier context comes from `session.context`, which returns the full session history.

```json
"triggers": {
"afterIdle": true,
"afterUserMessage": true
},
"classifier": {
"enabled": true,
"model": { "providerID": "opencode", "modelID": "big-pickle" }
}
```

Synthetic messages never reach the prompt hook, and synthetic transcript entries are excluded from the classifier context. The classifier sends the user's request text and session context to the configured model; keep it disabled if that is not acceptable.

Session state is kept in a 5,000-entry LRU and expires after seven days of inactivity; deleted sessions are removed.

### Configuration example

```json
{
"$schema": "https://opencode.ai/config.json",
"plugin": [
[
"opencode-adaptive-thinking",
{
"plugins": [
{
"package": "opencode-adaptive-thinking",
"options": {
"enabled": true,
"quiet": false,
"toolName": "set_reasoning_effort",
"toolDescription": "Set your reasoning effort",
"systemPrompt": "Actively choose the lowest reasoning effort that can safely complete the task. Raise effort for ambiguity, debugging, risky changes, or multi-step synthesis."
"systemPrompt": "Actively choose the lowest reasoning effort that can safely complete the task. Raise effort for ambiguity, debugging, risky changes, or multi-step synthesis.",
"triggers": {
"afterIdle": true,
"afterUserMessage": true
}
}
]
}
]
}
```
Expand All @@ -67,7 +103,7 @@ The plugin accepts these optional settings:
Agents receive system guidance to actively choose the lowest reasoning effort that can safely complete the task. The plugin also exposes the configured tool, `set_reasoning_effort` by default, with these arguments:

- `level`: one of the valid reasoning-effort variants for the current model.
- `persist`: when `true`, keep the selected effort for the session. When `false` or omitted, apply it temporarily and reset to the previous effort after `session.idle`.
- `persist`: when `true`, keep the selected effort for the session. When `false` or omitted, apply it temporarily and reset at session idle to the session's own variant from before the tool call (a classified level is request-scoped and is not the reset target).

Example temporary change:

Expand All @@ -87,15 +123,19 @@ Example persisted change:
}
```

The system prompt lists the valid levels for the current session. Cached session state is only used when it is still valid for the current model, so switching models will not advertise or reuse an invalid effort level.
The system prompt lists the valid levels for the current session. A remembered level is only reused when the current model still supports it, so switching models will not reuse an invalid effort level. Message lookups now read full session history; the former 20-message limit is gone, so long sessions cost more.

Adaptive-thinking guidance is appended to the existing system instructions as an additional text part instead of a separate system message, so strict chat templates that allow only one system message keep working.

## Troubleshooting

Plugin output is written to the server process console (stderr). It is not part of `opencode.log` or the structured `--print-logs` log stream; when the server's stderr is captured, it appears there as raw lines. Repeated warnings (classifier, model-switch, and request-body) print once; errors print every time; every failure updates `lastDiagnostic`. The plugin also records the most recent warning or error under the `lastDiagnostic` key in plugin storage as `{ at, level, message }`, so a failure can be inspected after the fact. Classifier failures fail open: the request proceeds without a classified level.

If the tool returns `no valid reasoning effort levels are available for this session`, check that the active model exposes variants in OpenCode. Some providers or models may not support reasoning-effort levels.

If the tool returns `Invalid reasoning effort level`, use one of the levels listed in the system prompt for the current session.

If configuration errors appear, check the plugin options against the documented keys and value types. Set `quiet` to `true` to suppress configuration error toasts while still writing details to the OpenCode logs.
If configuration errors appear, check the plugin options against the documented keys and value types. Set `quiet` to `true` to suppress the generic configuration error message; details are written to the server process output (stderr), not to `opencode.log`. OpenCode 2 does not expose a TUI toast API to server plugins, so configuration errors are logged instead.

## Development

Expand Down
Loading