Files
Jordan Humberto de Souza 720492d632 fix(opencode): support OpenCode 2.x via native tools and commands (#1213)
* fix(opencode): support OpenCode 2.x via native tools and commands

The single-file plugin (open-code-review.ts) cannot load on OpenCode 2.x:
the 2.x loader requires a default-exported { id, effect | setup } and has
no custom-tool registration API. It also fails on 1.x for most users
because nothing installs the @opencode-ai/plugin dependency the file
imports.

Ship the same ocr_review / ocr_health features as 2.x-native custom
tools plus /ocr-review /ocr-health commands, and document the
@opencode-ai/plugin dependency step for both versions.

* fix(opencode): dual V1+V2 plugin entrypoint instead of separate files

Replace the tools/+commands/ split with the documented dual plugin form:
default-export { ...Plugin.define({ id, setup }), server }. V2 registers
ocr_review/ocr_health via ctx.tool.transform and /ocr-review//ocr-health
via ctx.command.transform, reusing the same OCR logic as V1. Add
@opencode/plugin beta devDependency and a test for the default export.

* fix(opencode): review feedback: typeless V2 import, strict schemas, tests

- Import @opencode/plugin as types only and export a plain dual object,
  so OpenCode 1.x never needs the V2 beta package at runtime (verified
  in the built output: only node:*, @opencode-ai/plugin imports remain).
- V2 numeric inputs now require positive integers
  ({ type: integer, minimum: 1 }), matching the V1 zod schema.
- V2 commands keep the V1 sentence break, skip user-defined names like
  the V1 ??= guards, and resolveSessionCwd falls back to the plugin
  location when the session lookup fails.
- README: single download block, corrected 30-minute tool timeout,
  deduped project section.
- Track package-lock.json (drop the local ignore) so npm ci works.
- Move the V2 stub harness into the test suite (+6 tests, 29 passing).
- Verified live on OpenCode 1.18.30 sandbox: plugin loads with no
  errors, single init across sessions, both commands registered once.
2026-09-14 15:31:42 +08:00

109 lines
3.2 KiB
Markdown

# OpenCode integration
This integration exposes OpenCodeReview as native tools and slash commands in
[OpenCode](https://opencode.ai/).
It registers:
- `ocr_review` — review workspace changes, one commit, or a ref range and
return structured JSON findings.
- `ocr_health` — show the installed OCR version and test its configured LLM
connection.
- `/ocr-review` and `/ocr-health` — convenient prompts that invoke the tools.
Existing user commands with either name are preserved.
## Prerequisites
Install and configure OpenCodeReview first:
```bash
npm install -g @alibaba-group/open-code-review
ocr config provider
ocr config model
ocr llm test
```
Check your OpenCode version (`opencode --version` vs `opencode2 --version`)
and install the matching dependencies below. The single plugin file
(`open-code-review.ts`) serves both versions: V1 reads its `server`
entrypoint, V2 reads its `id` + `setup` entrypoint
([dual plugin form](https://opencode.ai/v2/docs/build/plugins#support-v1)).
The V2 API is imported as types only, so OpenCode 1.x needs just
`@opencode-ai/plugin` at runtime, while OpenCode 2.x needs both packages.
## Install globally
```bash
mkdir -p ~/.config/opencode/plugins
curl -fsSL \
https://raw.githubusercontent.com/alibaba/open-code-review/main/plugins/open-code-review/opencode/open-code-review.ts \
-o ~/.config/opencode/plugins/open-code-review.ts
```
Then install the runtime dependencies for your version (otherwise the
server log shows `Cannot find package ...` and the plugin fails to load):
```bash
cd ~/.config/opencode
npm init -y # skip if package.json already exists
npm install @opencode-ai/plugin # OpenCode 1.x
npm install @opencode-ai/plugin @opencode/plugin@beta # OpenCode 2.x
```
Restart OpenCode after installation.
## Install for one project
Run this from the project root:
```bash
mkdir -p .opencode/plugins
curl -fsSL \
https://raw.githubusercontent.com/alibaba/open-code-review/main/plugins/open-code-review/opencode/open-code-review.ts \
-o .opencode/plugins/open-code-review.ts
```
Then add the runtime dependencies for your version to the project's
`.opencode/package.json` as above. Commit the plugin file if the
integration should be shared with the project.
## Usage
Use the registered commands:
```text
/ocr-review current workspace; focus on authentication regressions
/ocr-review compare main to feature/auth-refresh
/ocr-health
```
Or ask OpenCode naturally:
```text
Use ocr_review to review my current changes. The goal is to add rate limiting
without changing the public API.
```
Set `preview` to `true` to inspect which files would be reviewed without making
an LLM request.
## Behavior and safety
- Reviews use `--audience agent` and JSON output.
- The process is launched with an argument array and `shell: false`.
- Tool-driven reviews default to a 30-minute overall timeout and a 10 MiB output limit.
- Cancelling the OpenCode tool terminates the OCR process (1.x; on 2.x
the tool API has no abort signal, so cancellation relies on the overall
timeout).
- OCR credentials remain in the existing OCR configuration or environment.
- Workspace mode includes staged, unstaged, and untracked files.
## Development
```bash
cd plugins/open-code-review/opencode
npm install
npm run check
```