Skip to content

fix(llmcore): support opencode.ai Zen/Go endpoints (x-opencode-session header) - #831

Open
tuxdio wants to merge 1 commit into
lsdefine:mainfrom
tuxdio:feat/opencode-go-session
Open

tuxdio wants to merge 1 commit into
lsdefine:mainfrom
tuxdio:feat/opencode-go-session

Conversation

@tuxdio

@tuxdio tuxdio commented Oct 11, 2026

Copy link
Copy Markdown

Summary

Adds first-class support for opencode.ai Zen/Go endpoints to llmcore.py.

opencode's gateway requires every request to carry a stable x-opencode-session header and a self-reported User-Agent; without them the OpenAI-compatible paths return 400 MissingSessionID. Its Anthropic /v1/messages path also only accepts x-api-key authentication — a non-sk-ant- key currently falls through to Authorization: Bearer and gets 401 Missing API key.

Changes

  1. _oc_session_id() + _OC_SIDS — stable per-model session id (reused across requests in-process; config can override via session_id/extra_headers).
  2. BaseSession.__init__ — when api_base contains opencode.ai, auto-inject extra_headers['x-opencode-session'] and replace the default Claude CLI UA with GenericAgent/1.0.
  3. _openai_stream() — merge extra_headers so OpenAI / chat_completions / responses paths send the session header.
  4. ClaudeSession.raw_ask() — merge extra_headers for the Anthropic /v1/messages path.
  5. NativeClaudeSession.raw_ask() — also send x-api-key when api_base contains opencode.ai (covers non-sk-ant- keys).

Verification (2026-10-12, local)

  • OpenAI path (kimi-k3, grok-4.7 via /v1/responses) — ✅ reachable with this patch; upstream llmcore.py alone fails with 400 MissingSessionID.
  • Anthropic native path (minimax-m3 via /v1/messages, api_key_header non-standard) — ✅ 1.6s first token; upstream alone fails with 401 Missing API key.
  • Patch is git diff HEAD -- llmcore.py (14 insertions / 1 deletion, 5 hunks), applied on current main (6e0c54f).

Notes

mykey.py configs need no extra_headers/session_id fields — the patch injects them automatically when api_base contains opencode.ai.

…n header)

opencode.ai /v1 (OpenAI and Anthropic paths) requires a stable
x-opencode-session header per request and a self-reported User-Agent;
without it the endpoint returns 400 MissingSessionID. Native paths also
require x-api-key even when the key is not sk-ant- prefixed.

- add _oc_session_id() stable per-model session id
- BaseSession: inject extra_headers when api_base contains opencode.ai,
  override default UA with GenericAgent/1.0
- _openai_stream()/ClaudeSession.raw_ask(): merge extra_headers
- NativeClaudeSession: send x-api-key for opencode.ai api_base
@lsdefine

Copy link
Copy Markdown
Owner

感谢这个适配!目前 ClaudeSession 基本已弃用,评审重点应放在 NativeClaudeSession 主路径。

这里还有一个关键漏项:BaseSession 将 x-opencode-session 放入了 self.extra_headers,但 NativeClaudeSession.raw_ask() 构造请求头时没有合并它。离线截获请求构造结果确认:原生路径虽然改用了 x-api-key,但没有发出 x-opencode-session,用户配置的其他 extra_headers 也未传出。旧 ClaudeSession 反而已经合并了这些头。

请补齐原生路径的请求头合并,注意与现有鉴权头的覆盖顺序,并增加一条原生路径的请求构造测试,确认会话头、自定义头和鉴权都实际进入请求。不需要为这个 PR 继续扩展旧 ClaudeSession 的适配。这个核心漏项修好后我们再看合入。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants