Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ MODEL=anthropic:claude-sonnet-4-6
# Anthropic-compatible endpoint (pay-as-you-go billing applies here).
# ZAI_API_KEY=...
# ZAI_API_URL=https://api.z.ai/api/paas/v4/

# OpenCode (OpenCode Console gateway, formerly Zen): set MODEL=opencode:<model>
# with a model id from opencode.ai/docs/zen (e.g. opencode:claude-sonnet-4-6,
# opencode:gpt-5.4, opencode:glm-5.3) and provide a Console API key. claude-*
# uses the Anthropic endpoint, gpt-/grok-/muse-* the Responses API, everything
# else Chat Completions. The *-free models only work inside OpenCode itself.
# OPENCODE_API_KEY=...
# OPENCODE_API_URL=https://opencode.ai/inference
# Optional: pin which upstream providers OpenRouter routes each model to. Copy
# openrouter.toml.example to openrouter.toml, or point this at a file.
# OPENROUTER_CONFIG=./openrouter.toml
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ All configuration is done via environment variables (`.env` file or shell export
| Variable | Default | Description |
|---|---|---|
| `ANTHROPIC_API_KEY` | — | API key for Anthropic *(not needed for Ollama)* |
| `MODEL` | `anthropic:claude-sonnet-4-6` | Any `provider:model` string supported by LangChain (also `openrouter:<model-id>` — see [Using OpenRouter](#using-openrouter)) |
| `MODEL` | `anthropic:claude-sonnet-4-6` | Any `provider:model` string supported by LangChain (also `openrouter:<model-id>` — see [Using OpenRouter](#using-openrouter) — and `opencode:<model-id>` for the OpenCode Console gateway, keyed by `OPENCODE_API_KEY`) |
| `AGENT_CONFIG` | `./subagents.toml` | Per-agent models and tool allowlists (coordinator + sub-agents); point this at a different TOML to override |
| `OPENROUTER_API_KEY` | — | API key for OpenRouter *(required for `openrouter:` models)* |
| `DEEPSEEK_API_KEY` | — | API key for DeepSeek *(required for `deepseek:` models)* |
Expand Down
70 changes: 68 additions & 2 deletions src/ghidra_deep_agent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage
from langchain_deepseek import ChatDeepSeek
from pydantic import SecretStr

from ghidra_deep_agent.defaults import config_path

Expand Down Expand Up @@ -191,6 +192,67 @@ def _build_zai_model(model_name: str, max_tokens: int | None) -> BaseChatModel:
)


# OpenCode's model gateway (OpenCode Console, formerly Zen). No LangChain
# integration exists, but it speaks each model family's native protocol, so
# ``opencode:<model>`` builds the stock class for the family against it:
# claude-* Anthropic Messages -> ChatAnthropic
# gpt-*, grok-*, muse-* OpenAI Responses -> ChatOpenAI(use_responses_api)
# everything else Chat Completions -> _ChatDeepSeekFixed
# The last row covers DeepSeek, GLM, Kimi, Qwen, MiniMax, and Gemini (which the
# gateway translates). _ChatDeepSeekFixed is ChatOpenAI plus reasoning_content
# capture and round-trip; thinking models on that protocol 400 without it.
# Endpoints per opencode.ai/docs/zen; override the base via OPENCODE_API_URL.
_OPENCODE_DEFAULT_API_URL = "https://opencode.ai/inference"
_OPENCODE_RESPONSES_PREFIXES = ("gpt-", "grok-", "muse-")


def _build_opencode_model(model_name: str, max_tokens: int | None) -> BaseChatModel:
raw_key = os.environ.get("OPENCODE_API_KEY")
if not raw_key:
raise ValueError(
f"opencode:{model_name} requires the OPENCODE_API_KEY environment "
"variable (an OpenCode Console API key)."
)
api_key = SecretStr(raw_key)
base = os.environ.get("OPENCODE_API_URL", _OPENCODE_DEFAULT_API_URL).rstrip("/")
if model_name.startswith("claude-"):
from langchain_anthropic import ChatAnthropic

if max_tokens is None and not _anthropic_has_profile(model_name):
max_tokens = _UNPROFILED_ANTHROPIC_MAX_TOKENS
anthropic_kwargs: dict[str, Any] = {}
if max_tokens is not None:
anthropic_kwargs["max_tokens"] = max_tokens
# The SDK appends /v1/messages itself.
return ChatAnthropic( # type: ignore[call-arg]
model=model_name,
base_url=f"{base}/anthropic",
api_key=api_key,
**anthropic_kwargs,
)
kwargs: dict[str, Any] = {}
if max_tokens is not None:
kwargs["max_tokens"] = max_tokens
if model_name.startswith(_OPENCODE_RESPONSES_PREFIXES):
from langchain_openai import ChatOpenAI

return ChatOpenAI(
model=model_name,
base_url=f"{base}/openai/v1",
api_key=api_key,
use_responses_api=True,
**kwargs,
)
# `api_base` is ChatDeepSeek's documented runtime name; mypy's pydantic
# plugin only sees the field alias.
return _ChatDeepSeekFixed( # type: ignore[call-arg]
model=model_name,
api_base=f"{base}/openai/v1",
api_key=api_key,
**kwargs,
)


def build_model(
model_string: str, max_tokens: int | None = None
) -> BaseChatModel | str:
Expand All @@ -202,7 +264,9 @@ def build_model(
``ChatOpenRouter`` directly with that routing; otherwise the string is
returned as-is for init_chat_model to resolve. ``zai:<model>`` targets
z.ai's OpenAI-compatible endpoint via ChatOpenAI (their documented
LangChain path; no dedicated integration exists).
LangChain path; no dedicated integration exists). ``opencode:<model>``
targets OpenCode's gateway with the stock class for the model's family
(see ``_build_opencode_model``).

``max_tokens`` (from ``max_tokens`` in subagents.toml) caps output tokens.
Anthropic-protocol models that langchain-anthropic has no profile for get
Expand All @@ -211,6 +275,8 @@ def build_model(
"""
if model_string.startswith("zai:"):
return _build_zai_model(model_string.split(":", 1)[1], max_tokens)
if model_string.startswith("opencode:"):
return _build_opencode_model(model_string.split(":", 1)[1], max_tokens)
if model_string.startswith("deepseek:"):
model_name = model_string.split(":", 1)[1]
if max_tokens is not None:
Expand Down Expand Up @@ -247,7 +313,7 @@ def build_model(
# num_predict, etc.), so don't guess — say it's ignored.
print(
f"Warning: max_tokens is not supported for {model_string!r}; "
"ignoring (supported: anthropic, deepseek, openrouter, zai).",
"ignoring (supported: anthropic, deepseek, opencode, openrouter, zai).",
file=sys.stderr,
)
return model_string
Expand Down
51 changes: 51 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from ghidra_deep_agent.models import (
_UNPROFILED_ANTHROPIC_MAX_TOKENS,
_ChatDeepSeekFixed,
build_model,
)

Expand All @@ -31,6 +32,8 @@ def _api_keys(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OPENROUTER_API_KEY", "test-key")
monkeypatch.setenv("ZAI_API_KEY", "test-key")
monkeypatch.delenv("ZAI_API_URL", raising=False)
monkeypatch.setenv("OPENCODE_API_KEY", "oc-key")
monkeypatch.delenv("OPENCODE_API_URL", raising=False)


def test_unprofiled_anthropic_model_gets_the_floor_cap() -> None:
Expand Down Expand Up @@ -103,3 +106,51 @@ def test_zai_without_key_fails_with_a_clear_error(
monkeypatch.delenv("ZAI_API_KEY", raising=False)
with pytest.raises(ValueError, match="ZAI_API_KEY"):
build_model("zai:glm-5.2")


# --- opencode: provider (OpenCode Console gateway) -------------------------------


def test_opencode_claude_uses_the_anthropic_endpoint() -> None:
model = build_model("opencode:claude-sonnet-4-6")
assert isinstance(model, ChatAnthropic)
assert model.model == "claude-sonnet-4-6"
assert model.anthropic_api_url == "https://opencode.ai/inference/anthropic"
# The OpenCode key, not ANTHROPIC_API_KEY.
assert model.anthropic_api_key.get_secret_value() == "oc-key"


def test_opencode_gpt_uses_the_responses_api() -> None:
model = build_model("opencode:gpt-5.4")
assert isinstance(model, ChatOpenAI)
assert model.use_responses_api is True
assert model.openai_api_base == "https://opencode.ai/inference/openai/v1"


def test_opencode_other_models_use_chat_completions_with_reasoning_round_trip() -> None:
model = build_model("opencode:deepseek-v4-pro")
assert isinstance(model, _ChatDeepSeekFixed)
assert model.api_base == "https://opencode.ai/inference/openai/v1"
assert model.api_key is not None
assert model.api_key.get_secret_value() == "oc-key"


def test_opencode_threads_max_tokens_through() -> None:
for name in ("claude-sonnet-4-6", "gpt-5.4", "glm-5.3"):
model = build_model(f"opencode:{name}", max_tokens=12_345)
assert getattr(model, "max_tokens") == 12_345 # noqa: B009


def test_opencode_base_url_is_overridable(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OPENCODE_API_URL", "https://example.test/inference/")
model = build_model("opencode:kimi-k3")
assert isinstance(model, _ChatDeepSeekFixed)
assert model.api_base == "https://example.test/inference/openai/v1"


def test_opencode_without_key_fails_with_a_clear_error(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.delenv("OPENCODE_API_KEY", raising=False)
with pytest.raises(ValueError, match="OPENCODE_API_KEY"):
build_model("opencode:glm-5.3")
Loading