Skip to content

feat(provider): add OpenCode Go protocol providers - #10005

Open
piexian wants to merge 6 commits into
AstrBotDevs:masterfrom
piexian:feat/opencode-go-provider
Open

piexian wants to merge 6 commits into
AstrBotDevs:masterfrom
piexian:feat/opencode-go-provider

Conversation

@piexian

@piexian piexian commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

新增 OpenCode Go 订阅服务接入。OpenCode Go 要求客户端携带自身标识和稳定的对话会话请求头;仅通过通用服务商配置 API 地址和密钥,无法完整满足这些要求,可能导致 Go 订阅无法正常使用。此次添加专用入口,自动补齐所需请求头,并提供 Chat Completions、Responses 和 Messages 三种协议供用户选择
fixes #10054 #10045 #8158

Modifications / 改动点

  • 新增三个 OpenCode Go 服务商模板,复用现有协议适配器。

  • 使用 AstrBot/{版本} 标识客户端,通过具体对话 UUID 的稳定哈希生成 x-opencode-session 请求头。

  • 主请求、工具重查询、上下文压缩、图片描述和标题生成传递所属对话 ID,确保同一对话使用一致标识。

  • 添加中英文接入文档及测试。

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Integrate OpenCode Go subscriptions with dedicated protocol providers and stable conversation-aware request identification.

New Features:

  • Add dedicated OpenCode Go provider templates for Chat Completions, Responses, and Messages protocols.
  • Automatically identify AstrBot clients and maintain stable per-conversation OpenCode session headers across requests and agent workflows.

Bug Fixes:

  • Ensure tool retries, context compression, image captioning, and title generation retain the originating conversation identity.

Enhancements:

  • Extend provider adapters to support request-specific headers while preserving existing protocol integrations.

Documentation:

  • Add English and Chinese documentation for configuring OpenCode Go providers.

Tests:

  • Add coverage for OpenCode Go protocol requests, headers, model selection, streaming, concurrent sessions, and conversation identity propagation.

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/sources/opencode_go_source.py" line_range="89-97" />
<code_context>
+        """
+        model = (model or self.get_model()).removeprefix("opencode-go/")
+        delegate = self.delegate
+        extra_headers = {
+            key: value
+            for key, value in (kwargs.pop("extra_headers", None) or {}).items()
+            if key.lower() not in {"user-agent", "x-opencode-session"}
+        }
+        # Calls without a conversation (such as connection tests) are independent.
+        extra_headers["x-opencode-session"] = hashlib.sha256(
+            (kwargs.pop("conversation_id", None) or uuid4().hex).encode()
+        ).hexdigest()
+        return await delegate.text_chat(
+            prompt=prompt,
</code_context>
<issue_to_address>
**issue (bug_risk):** The required `x-opencode-session` value is not sent as an HTTP header for the Chat Completions and Responses adapters. `extra_headers` is inserted into the payload, then those adapters classify the unknown `extra_headers` key as `extra_body`, so OpenCode Go receives it in the JSON request body instead of the request headers and rejects or misidentifies the request.

**Triggers:** When an OpenCode Go Chat Completions or Responses provider sends a request.

**Suggested fix:** Pass `extra_headers` to the SDK request as a top-level request option, rather than placing it in the payload consumed by `_query`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and this adds a third-party provider path that sends prompts, images, tool data, and API-authenticated requests to OpenCode Go while creating provider-side sessions keyed by conversation IDs. If the endpoint, protocol, or session handling is wrong, content could be sent to the wrong external service or mixed into a retained session; reverting stops future calls but cannot retract data already transmitted.

Blocking findings: astrbot/core/provider/sources/opencode_go_source.py:97


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread astrbot/core/provider/sources/opencode_go_source.py
@RC-CHN

RC-CHN commented Sep 10, 2026

Copy link
Copy Markdown
Member

注意到事实上群聊上下文标注图片任务使用一个非稳定的session,考虑到此为一个独立任务语义上也尚可接受,请确定这是否为预期设计

Comment thread astrbot/core/config/default.py
…ovider

# Conflicts:
#	astrbot/core/astr_main_agent.py
#	tests/test_tool_loop_agent_runner.py
@piexian

piexian commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

Screenshot_2026-09-14-04-23-15-577_com.microsoft.emmx.png
顺带一提虽然他只是建议但是没有x-opencode-session已经百分百报错了所以才会有其他改动 @Soulter

Context.tool_loop_agent forwards extra kwargs to the runner, but reset()
never read conversation_id, so plugin-specified identities were silently
replaced with random UUIDs and each call produced a new OpenCode session.

Accept conversation_id in reset() with three-tier precedence:
request.conversation.cid, then the explicit value, then a random UUID.
@piexian
piexian requested a review from Soulter September 14, 2026 11:52
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.

[Feature] 在 API 请求中发送 x-opencode-session 请求头(OpenCode Go 自 9 月 5 日起要求此项)

3 participants