Skip to content

About

Local relay that injects x-opencode-session so OpenAI-compatible clients (WorkBuddy, Cherry Studio, Chatbox) can use OpenCode Go.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

opencode-go2workbuddy

English · 中文

A tiny local relay that makes OpenCode Go usable from any OpenAI-compatible GUI client — WorkBuddy, Cherry Studio, Chatbox, LobeChat, your own scripts.

It exists for one reason: OpenCode Go requires a stable x-opencode-session header on every request. Most GUI clients only let you fill in a base URL and an API key, so their requests are rejected with MissingSessionID. This relay adds the header for them.


English

Why this exists

Calling the Go endpoint directly from a GUI client returns:

{"type":"error","error":{"type":"MissingSessionID","message":"Request is missing x-opencode-session ..."}}

OpenCode Go asks clients to send a stable session id so it can route requests and keep prompt caching coherent. Terminal agents do this themselves; GUI clients generally can't.

This project is a ~200-line, zero-dependency Node.js server that:

  1. listens on 127.0.0.1 only,
  2. injects x-opencode-session (one stable id per process) and a client-identifying User-Agent,
  3. forwards the request to OpenCode Go and streams the response back unchanged.
┌───────────────┐   no session header    ┌────────────────────┐   + x-opencode-session   ┌─────────────────┐
│   WorkBuddy   │ ─────────────────────► │  opencode-go-relay │ ───────────────────────► │  OpenCode Go    │
│ (GUI client)  │ ◄───────────────────── │     127.0.0.1      │ ◄─────────────────────── │  (upstream API) │
└───────────────┘      streamed SSE      └────────────────────┘      streamed SSE        └─────────────────┘

Quick start

Requires Node.js 18+ (uses the built-in fetch).

git clone https://github.com/<you>/opencode-go2workbuddy.git
cd opencode-go2workbuddy
node opencode-go-relay.js

You should see:

relay    http://127.0.0.1:8787/v1
upstream https://opencode.ai/inference/go/openai/v1
session  ses_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Then configure your client:

Field Value
Base URL / endpoint http://127.0.0.1:8787/v1/chat/completions
API key your own OpenCode Go key (oc_... / sk-...)
Model id deepseek-v4.1-flash, kimi-k3.1-pro, glm-5.2, …

Model ids must be exact — hyphens, no spaces. deepseek v4.1 flash is rejected upstream; deepseek-v4.1-flash is correct. Get the list with:

curl https://opencode.ai/inference/go/v1/models

Leave the relay window open while you work; Ctrl+C stops it.

Control panel

Open http://127.0.0.1:8787/ while the relay is running:

  • live status (running / stopped)
  • the endpoint to paste into your client, one click to copy
  • request count, last upstream status + model, uptime
  • the raw upstream error when a request fails (so you don't have to read logs)
  • a Stop gateway button (calls POST /api/stop, graceful shutdown)

Windows tray app (optional)

If you'd rather not keep a console window around, windows/tray.cs is a tiny WinForms tray app with no third-party dependencies. It starts the relay hidden, turns green/red with the relay state, and stops it gracefully.

Requirements: Windows 10/11, Node.js 18+, and .NET Framework 4.x (ships with Windows and provides csc.exe). No admin rights, no NuGet, no extra packages.

From the repository root:

powershell -ExecutionPolicy Bypass -File windows\build.ps1

The script draws a small icon, compiles windows/tray.cs, and writes opencode-go-relay.exe into the repo root — right next to opencode-go-relay.js. Double-click it and the relay starts silently with a green tray icon.

  • double-click the tray icon → open the panel
  • right-click → start / stop / open panel / view log / quit

Notes:

  • -ExecutionPolicy Bypass is only there so the script can run on default Windows settings. If your machine blocks it anyway, compile manually with the csc command at the top of windows/tray.cs.
  • The exe is a local build and is git-ignored (*.exe), so every user compiles their own copy.
  • It locates the relay in this order: same folder as the exe → one folder up → %USERPROFILE%. So moving the exe around is fine as long as the script stays findable.
  • The tray app talks to the relay over HTTP, so Stop is always graceful.
  • Double-clicking twice won't start two relays (single-instance mutex).

CLI flags for scripting:

opencode-go-relay.exe --start | --stop | --status | --open

--status exits with 0 when running and 1 when stopped.

Configuration

Env var Default Meaning
PORT 8787 listen port (bound to 127.0.0.1 only)
UPSTREAM https://opencode.ai/inference/go/openai/v1 upstream base URL
OPENCODE_SESSION random per run pin the session id
OPENCODE_API_KEY – fallback key, used only if the client sends none
PORT=9000 node opencode-go-relay.js

The legacy Go base URL https://opencode.ai/zen/go/v1 also works and can be selected with UPSTREAM.

HTTP surface

Route Purpose
GET / control panel
GET /api/status JSON status (requests, last status/model, uptime)
POST /api/stop graceful shutdown
GET /health plain-text liveness probe
everything else proxied to UPSTREAM

Both /v1/chat/completions and /chat/completions are accepted, and a doubled /chat/completions/chat/completions is normalized, which covers clients that build the URL differently.

Troubleshooting

Symptom Cause / fix
MissingSessionID You're hitting OpenCode Go directly. Point the client at 127.0.0.1:8787, not opencode.ai.
Model is unavailable Model id is wrong (spaces vs hyphens), or your workspace privacy settings block that model. In the OpenCode console check Settings › Privacy › Regions — DeepSeek models need Global outside the EU/US.
Invalid credential Wrong / expired API key on the Go plan.
ECONNREFUSED / connection failed Relay isn't running, or the port differs from the one in the client.
EADDRINUSE Another relay already listens on the port — stop the old one first.
Panel says stopped but requests work Port mismatch: the client is using a different port than the panel.

Notes & disclaimer

  • The relay binds to 127.0.0.1 only; it is not reachable from your network.
  • Your API key is passed through untouched. It is never written to a log; the log records only presence/absence, model id, status code and upstream error text.
  • POST /api/stop is unauthenticated by design. Fine for a local-only service, but do not expose the port.
  • Not affiliated with OpenCode, WorkBuddy or any model provider. Respect the terms of the services you use.
  • Licence: MIT.

中文

一个几十行的本地中转,让 OpenCode Go 能在任何 OpenAI 兼容的图形客户端里使用 —— WorkBuddy、Cherry Studio、Chatbox、LobeChat,或者你自己写的脚本。

它只为一件事存在:OpenCode Go 要求每个请求都带一个稳定的 x-opencode-session 头。绝大多数图形客户端只能填「接口地址 + API Key」,于是请求被拒:MissingSessionID。这个中转帮你补上这个头。

为什么需要它

图形客户端直连 Go 端点会收到:

{"type":"error","error":{"type":"MissingSessionID","message":"Request is missing x-opencode-session ..."}}

Go 需要稳定的会话 id 来做路由和 prompt 缓存。终端类 agent 自己会发,图形客户端一般发不了。

本项目是一个约 200 行、零依赖的 Node.js 服务:

  1. 只监听 127.0.0.1;
  2. 自动补 x-opencode-session(每个进程一个稳定 id)和标识来源的 User-Agent;
  3. 把请求转发给 OpenCode Go,响应(含 SSE 流)原样回传。
┌───────────────┐   没有 session 头       ┌────────────────────┐   + x-opencode-session   ┌─────────────────┐
│   WorkBuddy   │ ─────────────────────► │  opencode-go-relay │ ───────────────────────► │  OpenCode Go    │
│  (图形客户端)│ ◄───────────────────── │     127.0.0.1      │ ◄─────────────────────── │  (上游 API)   │
└───────────────┘      流式返回 SSE       └────────────────────┘      流式返回 SSE        └─────────────────┘

快速开始

需要 Node.js 18+(用到内置 fetch)。

git clone https://github.com/<you>/opencode-go2workbuddy.git
cd opencode-go2workbuddy
node opencode-go-relay.js

看到类似输出就说明起来了:

relay    http://127.0.0.1:8787/v1
upstream https://opencode.ai/inference/go/openai/v1
session  ses_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

然后在客户端里填:

项目 值
接口地址 http://127.0.0.1:8787/v1/chat/completions
API Key 你自己的 OpenCode Go key(oc_... / sk-...)
模型名称 deepseek-v4.1-flash、kimi-k3.1-pro、glm-5.2 等

模型 ID 必须完全一致:用短横线,不能有空格。 deepseek v4.1 flash 会被上游拒绝,deepseek-v4.1-flash 才对。完整列表:

curl https://opencode.ai/inference/go/v1/models

用的时候别关窗口,Ctrl+C 停止。

控制面板

中转运行期间打开 http://127.0.0.1:8787/:

  • 实时状态(运行中 / 已停止)
  • 可直接复制到客户端的接口地址
  • 转发次数、最近一次的状态码与模型名、运行时长
  • 请求失败时直接显示上游原始报错,不用翻日志
  • 关闭网关按钮(调用 POST /api/stop,优雅退出)

Windows 托盘程序(可选)

不想留一个黑色控制台窗口的话,windows/tray.cs 是一个零第三方依赖的 WinForms 托盘小工具:静默启动中转、用绿/灰图标显示状态、优雅关闭。

需要:Windows 10/11、Node.js 18+、.NET Framework 4.x(Windows 自带,提供 csc.exe)。不需要管理员权限,不需要装任何包。

在仓库根目录执行:

powershell -ExecutionPolicy Bypass -File windows\build.ps1

脚本会画一个图标、编译 windows/tray.cs,并把 opencode-go-relay.exe 输出到仓库根目录,正好躺在 opencode-go-relay.js 旁边。双击它,中转静默启动,托盘出现绿点。

  • 双击托盘图标 → 打开控制面板
  • 右键 → 启动 / 停止 / 打开面板 / 查看日志 / 退出

补充:

  • -ExecutionPolicy Bypass 只是为了让脚本在 Windows 默认策略下能跑;如果公司策略强制拦截,可以用 windows/tray.cs 文件头注释里的 csc 命令手动编译
  • exe 是本地编译产物,已被 .gitignore 排除,每个人自己编译一份
  • 查找中转脚本的顺序:exe 同目录 → 上一级目录 → %USERPROFILE%,所以 exe 挪位置也能用
  • 停止走 HTTP /api/stop,永远是优雅退出,不是强杀
  • 重复双击不会起两个网关(单实例互斥)

给脚本用的命令行参数:

opencode-go-relay.exe --start | --stop | --status | --open

--status 退出码 0 表示运行中,1 表示已停止。

配置项

环境变量 默认值 说明
PORT 8787 监听端口(只绑定 127.0.0.1)
UPSTREAM https://opencode.ai/inference/go/openai/v1 上游地址
OPENCODE_SESSION 每次启动随机 固定会话 id
OPENCODE_API_KEY 无 兜底 key,仅在客户端没传时使用
PORT=9000 node opencode-go-relay.js

旧的 Go 线路 https://opencode.ai/zen/go/v1 同样可用,通过 UPSTREAM 切换。

接口一览

路由 作用
GET / 控制面板
GET /api/status JSON 状态(请求数、最近状态/模型、运行时长)
POST /api/stop 优雅关闭
GET /health 纯文本存活探测
其它 全部转发到 UPSTREAM

/v1/chat/completions 和 /chat/completions 都接受;如果客户端把 /chat/completions 拼了两遍也会被归一化。

常见问题

现象 原因 / 处理
MissingSessionID 还在直连 OpenCode Go。把客户端指向 127.0.0.1:8787,不是 opencode.ai。
Model is unavailable 模型 ID 写错(空格/横线),或工作区隐私设置拦了该模型。到 OpenCode 控制台 Settings › Privacy › Regions 检查:DeepSeek 系列在欧美之外需要选 Global。
Invalid credential API Key 错误或已失效(确认是 Go 订阅的 key)。
ECONNREFUSED / 连接失败 中转没启动,或客户端里的端口和中转不一致。
EADDRINUSE 端口已被占用,先停掉旧的实例。
面板显示已停止但请求仍可用 端口不一致:客户端连的是另一个端口。

说明

  • 中转只绑定 127.0.0.1,局域网访问不到。
  • API Key 原样透传,不会写进日志;日志只记录是否带 key、模型名、状态码和上游报错。
  • POST /api/stop 没有鉴权(本地服务的取舍)。不要把端口暴露出去。
  • 与 OpenCode、WorkBuddy 及各模型厂商无隶属关系,使用时请遵守相应服务条款。
  • 许可证:MIT。

About

Local relay that injects x-opencode-session so OpenAI-compatible clients (WorkBuddy, Cherry Studio, Chatbox) can use OpenCode Go.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages