npm package: @suncreation/opencode-claude-patch
Description: Anthropic OAuth provider bridge and Claude request patch for OpenCode
Latest version: 0.3.8

# opencode-anthropic-oauth-patch

Installable OpenCode plugin that restores Anthropic provider visibility, reads the working OpenCode OAuth token file, refreshes Anthropic access tokens, and patches Claude request shape for OpenCode/oh-my-opencode.

This is meant for OpenCode/oh-my-opencode-style integrations where Claude OAuth requests break because extra Anthropic-incompatible fields are injected into the request path.

## What it patches automatically as an OpenCode plugin

- Registers an Anthropic auth/provider bridge for OpenCode using `~/.local/share/opencode/auth.json`
- Refreshes Anthropic OAuth access tokens through `https://console.anthropic.com/v1/oauth/token`
- Removes `thinking`, `output_config`, and related Anthropic-problematic option fields from OpenCode provider options
- Rewrites OpenCode system prompt strings into a single Claude-Code-compatible system string
- Sets Claude-Code-compatible Anthropic headers by default
- Installs a targeted global `fetch` patch that sanitizes outbound Anthropic OAuth message bodies, including final `system[*].cache_control` removal

## How the automatic patch works

The root plugin uses two layers:

1. OpenCode hooks (`chat.params`, `chat.headers`, `experimental.chat.system.transform`) for public-ABI-safe fixes
2. a narrowly targeted `globalThis.fetch` wrapper that only patches `https://api.anthropic.com/v1/messages...` requests carrying `Authorization: Bearer ...`

That second layer is what makes the package automatic for the `cache_control` problem too.

## Install

Add the package to your `opencode.json` plugin list:

```json
{
  "$schema": "https://opencode.ai/config.json",
  "plugin": [
    "@suncreation/opencode-claude-patch"
  ]
}
```

Install it from this repository:

```bash
npm install /Users/admin/Projects/npm/opencode-anthropic-oauth-patch
```

If you later publish it, keep the same `plugin` entry and install with:

```bash
npm install @suncreation/opencode-claude-patch
```

## One-line npx installer

To both download the package and register it in your OpenCode plugin config automatically:

```bash
npx -y @suncreation/opencode-claude-patch install
```

This command now does both:
- installs the package into OpenCode's runtime cache
- adds the plugin entry to `~/.config/opencode/opencode.json`

Useful variants:

```bash
npx -y @suncreation/opencode-claude-patch status
npx -y @suncreation/opencode-claude-patch remove
npx -y @suncreation/opencode-claude-patch install --config ~/.config/opencode/opencode.json
npx -y @suncreation/opencode-claude-patch install --runtime ~/.cache/opencode --version latest
```

## Anthropic login commands

The package can also manage Anthropic OAuth credentials directly in `~/.local/share/opencode/auth.json`.

```bash
npx -y @suncreation/opencode-claude-patch login
npx -y @suncreation/opencode-claude-patch status
npx -y @suncreation/opencode-claude-patch refresh
npx -y @suncreation/opencode-claude-patch logout
```

Useful login options:

```bash
npx -y @suncreation/opencode-claude-patch login --mode max
npx -y @suncreation/opencode-claude-patch login --mode console
npx -y @suncreation/opencode-claude-patch login --no-open
npx -y @suncreation/opencode-claude-patch login --code '<code#state>'
npx -y @suncreation/opencode-claude-patch status --auth-path ~/.local/share/opencode/auth.json
```

OpenCode will auto-install npm plugins referenced in `opencode.json` at startup.

## What the root package export does

The root package export is an OpenCode plugin function. Once installed and listed in `opencode.json`, it auto-applies:

- Anthropic auth/provider loading backed by the OpenCode OAuth token file
- `chat.params` patching for Anthropic/Claude models
- `chat.headers` patching for Claude-compatible headers
- `experimental.chat.system.transform` patching for a single Claude-Code-style system prompt
- a targeted `globalThis.fetch` patch for final Anthropic OAuth request-body cleanup

No extra wiring is needed.

## Optional environment toggles

```bash
OPENCODE_ANTHROPIC_OAUTH_SET_HEADERS=0
OPENCODE_ANTHROPIC_OAUTH_SYSTEM_PREFIX="You are Claude Code, Anthropic's official CLI for Claude."
```

## Helper subpath for lower-level integrations

```js
import {
  createAnthropicOAuthPatchedFetch,
  mergePatchHeaders,
  normalizeAnthropicSystemStrings,
  sanitizeAnthropicOptions
} from "@suncreation/opencode-claude-patch/helpers";

// if published:
// import { ... } from "@suncreation/opencode-anthropic-oauth-patch/helpers";

const options = sanitizeAnthropicOptions({
  thinking: { type: "adaptive" },
  output_config: { effort: "max" }
});

const headers = mergePatchHeaders({});
const system = normalizeAnthropicSystemStrings([
  "Be concise.",
  "Answer in Korean."
]);

const patchedFetch = createAnthropicOAuthPatchedFetch(fetch, {
  ensureClaudeCodeSystem: true,
  normalizeSystemToUserMessage: true
});

console.log(options, headers, system, typeof patchedFetch);
```


## Notes

- The root export is safe for `opencode.json -> plugin` installation.
- The `./helpers` subpath is for deeper interceptors or forks.
- It does not mint OAuth tokens or bypass account-level/provider policy restrictions.
- This package is targeted at Claude OAuth / oh-my-opencode-style setups. If you use Anthropic API keys and want native reasoning features, do not enable it globally.