opencode-jinguobyte-adapter
v1.0.0
Published
OpenCode plugin adapter for JinguoByte New API proxy, fixing tool-call compatibility issues
Maintainers
Readme
OpenCode JinguoByte Adapter
Fix compatibility issues when using OpenCode with JinguoByte (New API) proxy for Claude models.
Problem
When using JinguoByte's New API proxy with OpenCode, two common errors occur:
- Third-party apps now draw from your extra usage - Provider rejects requests with long system prompts and extensive tool descriptions
- messages: text content blocks must be non-empty - Provider rejects assistant messages with empty
contentfield whentool_callsare present
This plugin provides a local adapter that fixes both issues.
Installation
Method 1: From npm
npm install -g opencode-jinguobyte-adapterThen install as an OpenCode plugin:
opencode plugin opencode-jinguobyte-adapterMethod 2: Manual Setup (Recommended)
Clone or download this repository:
git clone https://github.com/tao/opencode-jinguobyte-adapter.git cd opencode-jinguobyte-adapterCopy the plugin file to OpenCode's plugins directory:
mkdir -p ~/.config/opencode/plugins cp index.js ~/.config/opencode/plugins/opencode-jinguobyte-adapter.jsCopy the proxy script:
cp proxy.js ~/.config/opencode/jinguobyte-proxy.mjs
Note: The plugin file must export
JinguobyteAdapterPlugin. The proxy script is referenced by the shell wrapper (see Step 2 in Configuration) as~/.config/opencode/jinguobyte-proxy.mjs.
Configuration
Step 1: Add to OpenCode config
Edit your ~/.config/opencode/opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"model": "jinguobyte/claude-opus-4-7",
"small_model": "jinguobyte/claude-haiku-4-5-20251001",
"permission": {
"external_directory": "allow",
"doom_loop": "allow"
},
"tools": {
"todowrite": false
},
"provider": {
"jinguobyte": {
"npm": "@ai-sdk/openai-compatible",
"name": "JinguoByte New API",
"options": {
"baseURL": "http://127.0.0.1:4141/v1",
"apiKey": "{env:OPENCODE_JINGUOBYTE_API_KEY}"
},
"models": {
"claude-haiku-4-5-20251001": {
"name": "Claude Haiku 4.5"
},
"claude-sonnet-4-6": {
"name": "Claude Sonnet 4.6"
},
"claude-opus-4-6": {
"name": "Claude Opus 4.6"
},
"claude-opus-4-7": {
"name": "Claude Opus 4.7"
}
}
}
}
}Step 2: Add to your shell config
Add to ~/.zshrc or ~/.bashrc:
# Ensure proxy is running before opencode starts
ensure_opencode_jinguobyte_proxy() {
local proxy_port="${OPENCODE_JINGUOBYTE_PROXY_PORT:-4141}"
local proxy_script="$HOME/.config/opencode/jinguobyte-proxy.mjs"
local proxy_log="$HOME/.local/share/opencode/jinguobyte-proxy.log"
if command -v lsof >/dev/null 2>&1 && lsof -iTCP:"$proxy_port" -sTCP:LISTEN >/dev/null 2>&1; then
return 0
fi
mkdir -p "$HOME/.local/share/opencode"
nohup node "$proxy_script" >>"$proxy_log" 2>&1 &
sleep 0.4
}
# Wrap opencode to auto-start proxy
opencode() {
ensure_opencode_jinguobyte_proxy
command opencode "$@"
}
# Aliases
alias ocj="opencode"
alias oc='OPENCODE_CONFIG="$HOME/.config/opencode/opencode.full-tools.json" opencode'Then reload your shell:
source ~/.zshrcStep 3: Set environment variables
Add your API key to ~/.zshrc or ~/.bashrc:
export OPENCODE_JINGUOBYTE_API_KEY="your-api-key-here"Important: You must also map this environment variable in your OpenCode provider config via
"apiKey": "{env:OPENCODE_JINGUOBYTE_API_KEY}"(already included in the config above). Simply exporting the variable is not enough -- OpenCode needs theoptions.apiKeyfield to know where to read it from.
Or configure interactively in OpenCode via the /connect command.
How It Works
1. System Prompt Transformation
The plugin intercepts outgoing requests and replaces OpenCode's default long system prompt with a minimal one for JinguoByte provider:
"You are a careful coding assistant for software engineering tasks. Read relevant files before editing, prefer minimal correct changes, verify results, and keep responses concise."This avoids the "Third-party apps" error by not sending tool-heavy descriptions.
2. Empty Content Fix
When assistant messages contain tool_calls but have empty content, the proxy automatically injects a placeholder:
// Before (rejected by JinguoByte)
{
"role": "assistant",
"content": "",
"tool_calls": [...]
}
// After (accepted)
{
"role": "assistant",
"content": ".",
"tool_calls": [...]
}3. Local Proxy
The adapter runs a local HTTP proxy at 127.0.0.1:4141 that:
- Receives requests from OpenCode
- Rewrites problematic message formats
- Forwards to JinguoByte API
- Returns responses back to OpenCode
Usage
Quick Start
# Start OpenCode with JinguoByte adapter
ocj
# Or use full name
opencodeUsing Different Models
# Use Sonnet model
opencode --model jinguobyte/claude-sonnet-4-6
# Use Opus model (recommended for complex tasks)
opencode --model jinguobyte/claude-opus-4-7
# Use Haiku for quick tasks
opencode --model jinguobyte/claude-haiku-4-5-20251001CLI Mode
# Run a single command
opencode run "explain this code" --agent build --model jinguobyte/claude-opus-4-7
# Continue last session
opencode --continue
# List available models
opencode models jinguobyteFull Tools Configuration (Optional)
For other providers that support full tool set, create ~/.config/opencode/opencode.full-tools.json:
{
"$schema": "https://opencode.ai/config.json",
"model": "openai/gpt-4",
"permission": {
"*": "allow",
"external_directory": "allow",
"doom_loop": "allow"
},
"tools": {
"todowrite": true
}
}Then use:
ocEnvironment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| OPENCODE_JINGUOBYTE_PROXY_PORT | 4141 | Local proxy port |
| OPENCODE_JINGUOBYTE_PROXY_TARGET | https://token.jinguobyte.com | Upstream API URL |
| OPENCODE_JINGUOBYTE_API_KEY | - | Your JinguoByte API key |
Troubleshooting
Connection Refused
Make sure the proxy is running:
lsof -iTCP:4141 -sTCP:LISTENIf not running, restart:
source ~/.zshrc # Re-load the opencode wrapperAPI Key Issues
Configure via OpenCode:
/connect
# Select "Other"
# Provider ID: jinguobyte
# Paste your API keyEmpty Content Error Still Occurs
Check proxy logs:
tail -f ~/.local/share/opencode/jinguobyte-proxy.logLicense
MIT
Contributing
Issues and PRs welcome at https://github.com/tao/opencode-jinguobyte-adapter
