Skip to content
Closed
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
2 changes: 1 addition & 1 deletion dashboard/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3731,7 +3731,7 @@
"fetchModelsMerged": "Added {{count}} new models to list (existing ones unchanged)",
"fetchModelsNoNew": "No new models to add",
"fetchModelsFailed": "Failed to fetch models: {{error}}",
"fetchModelsUnsupportedKind": "Auto-fetch is only supported for OpenAI-compatible providers",
"fetchModelsUnsupportedKind": "Auto-fetch is only supported for OpenAI and Anthropic providers",
"customModelsLabel": "Models",
"customModelsHint": "Fetched models are off by default — enable, test, edit, or remove; or add manually",
"onnxLocalService": "Local ONNX embedding",
Expand Down
2 changes: 1 addition & 1 deletion dashboard/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -3730,7 +3730,7 @@
"fetchModelsMerged": "已新增 {{count}} 个模型到列表(已有模型保持不变)",
"fetchModelsNoNew": "没有可新增的模型",
"fetchModelsFailed": "获取模型失败:{{error}}",
"fetchModelsUnsupportedKind": "仅 OpenAI 兼容供应商支持自动获取模型列表",
"fetchModelsUnsupportedKind": "仅 OpenAI 和 Anthropic 供应商支持自动获取模型列表",
"customModelsLabel": "模型",
"customModelsHint": "获取后默认未启用,可开启开关、测试、编辑或删除;也可手动添加",
"onnxLocalService": "本地 ONNX Embedding",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export function CustomProviderModal({
const handleFetchModels = async () => {
try {
const values = await form.validateFields(["kind", "api_key", "base_url"]);
if ((values.kind as string) !== "openai") {
const currentKind = String(values.kind || "").toLowerCase();
if (currentKind !== "openai" && currentKind !== "anthropic") {
message.warning(t("models.fetchModelsUnsupportedKind"));
return;
}
Expand All @@ -135,7 +136,7 @@ export function CustomProviderModal({
}
setFetchingModels(true);
const result = await fetchProviderModels({
kind: "openai",
kind: currentKind,
api_key: apiKey,
base_url: (values.base_url as string | undefined)?.trim() || null,
});
Expand Down Expand Up @@ -333,7 +334,7 @@ export function CustomProviderModal({
<Input.Password placeholder="sk-..." visibilityToggle />
</Form.Item>

{kind === "openai" && (
{(kind === "openai" || kind === "anthropic") && (
<Form.Item style={{ marginBottom: 12 }}>
<Button
icon={<Download size={14} />}
Expand Down Expand Up @@ -404,7 +405,7 @@ export function CustomProviderModal({
>
{t("models.testConnection")}
</Button>
{kind === "openai" && (
{(kind === "openai" || kind === "anthropic") && (
<Button
size="small"
icon={<Download size={12} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ export function PresetProviderModal({
return;
}
setFetchingModels(true);
const kind =
preset.protocol === "anthropic" || preset.id.includes("anthropic")
? "anthropic"
: "openai";
const result = await fetchProviderModels({
kind: "openai",
kind,
api_key: key,
base_url: values.base_url?.trim() || preset.base_url,
name: preset.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,8 +934,10 @@ export function ProviderConfigModal({
}

const values = form.getFieldsValue();
const kind = (values.kind as string | undefined) ?? provider.kind;
if (kind !== "openai") {
const currentKind = String(
(values.kind as string | undefined) ?? provider.kind ?? "",
).toLowerCase();
if (currentKind !== "openai" && currentKind !== "anthropic") {
message.warning(t("models.fetchModelsUnsupportedKind"));
return;
}
Expand All @@ -949,7 +951,7 @@ export function ProviderConfigModal({

setFetchingModels(true);
const result = await fetchProviderModels({
kind: "openai",
kind: currentKind,
api_key: apiKey,
base_url: draftBaseUrl || provider.base_url,
name: provider.name,
Expand Down
12 changes: 12 additions & 0 deletions src/octop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@
# httpx 0.28 treats NO_PROXY CIDR as an exact IP (see issue #1347). Patch once
# at import so CLI, ``octop run``, and in-process harness clients all honor it.
install_httpx_cidr_no_proxy()

# langchain-anthropic 1.7.x crashes on message_delta events carrying dict-shaped
# context_management from recent Anthropic API updates. Patch once at import.
try:
from octop.infra.agents.providers.anthropic_compat import (
patch_langchain_anthropic_context_management,
)

patch_langchain_anthropic_context_management()
except Exception:
pass

13 changes: 8 additions & 5 deletions src/octop/api/routers/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from octop.infra.agents.providers.model_flags import is_local_runtime_provider
from octop.infra.agents.providers.presets import load_provider_presets
from octop.infra.agents.providers.probe import (
fetch_anthropic_models,
fetch_openai_compatible_models,
make_probe_provider_row,
parse_provider_extra,
Expand Down Expand Up @@ -490,23 +491,25 @@ async def admin_test_provider_draft(
)


@admin_router.post("/fetch-models", summary="List models from an OpenAI-compatible draft")
@admin_router.post("/fetch-models", summary="List models from a provider draft")
async def admin_fetch_provider_models(
body: ProviderFetchModelsBody,
request: Request,
_: Any = Depends(require_permission("providers")),
) -> dict[str, Any]:
"""Fetch remote model ids via OpenAI-compatible ``GET /models`` (openai kind only)."""
if body.kind != "openai":
"""Fetch remote model ids via GET /models (OpenAI-compatible or Anthropic)."""
kind = (body.kind or "").strip().lower()
if kind not in ("openai", "anthropic"):
return {
"ok": False,
"error": "fetch models is only supported for openai-compatible providers",
"error": "fetch models is only supported for openai and anthropic providers",
}
api_key = (body.api_key or "").strip()
if not api_key:
return {"ok": False, "error": "api_key is required"}
draft = SimpleNamespace(extra_json=body.extra_json)
return await fetch_openai_compatible_models(
fetcher = fetch_anthropic_models if kind == "anthropic" else fetch_openai_compatible_models
return await fetcher(
base_url=(body.base_url or "").strip() or None,
api_key=api_key,
extra_headers=provider_headers(draft) or None,
Expand Down
5 changes: 5 additions & 0 deletions src/octop/infra/agents/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""DB-backed LLM provider catalog and harness factory sync."""

from octop.infra.agents.providers.anthropic_compat import (
patch_langchain_anthropic_context_management,
)
from octop.infra.agents.providers.harness_factory import sync_providers_to_harness
from octop.infra.agents.providers.host_factory import HostModelFactory
from octop.infra.agents.providers.store import (
Expand All @@ -13,5 +16,7 @@
"AutoRouteCandidate",
"HostModelFactory",
"ProviderStore",
"patch_langchain_anthropic_context_management",
"sync_providers_to_harness",
]

114 changes: 114 additions & 0 deletions src/octop/infra/agents/providers/anthropic_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Upstream compatibility patches for langchain-anthropic / anthropic SDK.

Fixes an AttributeError crash in ``langchain_anthropic.chat_models`` during streaming:
when Anthropic returns a ``message_delta`` event carrying ``context_management``
(an unannounced or newly introduced field on the raw API response), the anthropic
SDK stores it in ``__pydantic_extra__`` as a plain ``dict`` rather than a pydantic
model because the non-beta ``RawMessageDeltaEvent`` type does not declare the field.

``ChatAnthropic._make_message_chunk_from_anthropic_event`` then unconditionally calls
``context_management.model_dump()``, raising:
AttributeError: 'dict' object has no attribute 'model_dump'

This patch wraps ``_make_message_chunk_from_anthropic_event`` (or normalizes the
field on the event) so dicts pass through safely while pydantic models continue to
call ``model_dump()``.
"""

from __future__ import annotations

import logging
from typing import Any

logger = logging.getLogger(__name__)

_PATCHED_ATTR = "_octop_anthropic_compat_applied"


def _dump_or_dict(value: Any) -> Any:
"""Return dict representation whether *value* is a pydantic model or already a dict."""
if value is None:
return None
dump = getattr(value, "model_dump", None)
if callable(dump):
try:
return dump()
except TypeError:
return dump(mode="json")
if isinstance(value, dict):
return dict(value)
return value


def patch_langchain_anthropic_context_management() -> bool:
"""Monkey-patch ChatAnthropic._make_message_chunk_from_anthropic_event if present."""
try:
import langchain_anthropic.chat_models as mod
except ImportError:
return False

chat_cls = getattr(mod, "ChatAnthropic", None)
if chat_cls is None:
return False

if getattr(chat_cls, _PATCHED_ATTR, False):
return True

original_make = getattr(chat_cls, "_make_message_chunk_from_anthropic_event", None)
if not callable(original_make):
return False

def _safe_make_message_chunk_from_anthropic_event(
self: Any,
event: Any,
*args: Any,
**kwargs: Any,
) -> Any:
# If the event carries a dict-shaped context_management, adapt it so
# .model_dump() won't crash if called, OR let original run if it's already a model.
cm = getattr(event, "context_management", None)
if isinstance(cm, dict) and not hasattr(cm, "model_dump"):
# Provide a dummy model_dump method on the dict, or swap to an object
# that implements model_dump. Subclassing dict is cleanest.
class _DictWithModelDump(dict):
def model_dump(self, *a: Any, **kw: Any) -> dict[str, Any]:
return dict(self)

try:
object.__setattr__(event, "context_management", _DictWithModelDump(cm))
except (AttributeError, TypeError):
# Event might be frozen; fall through to wrapped call
pass

try:
return original_make(self, event, *args, **kwargs)
except AttributeError as exc:
if "model_dump" not in str(exc):
raise
# Fallback path if the attribute could not be replaced on event:
# temporarily suppress the failure by creating a proxy or catching it.
# Usually the above object.__setattr__ succeeds because BaseModel allows
# mutating __dict__/__pydantic_extra__.
logger.warning(
"ChatAnthropic._make_message_chunk_from_anthropic_event hit model_dump error, adapting: %s",
exc,
)
# Re-read context_management and replace it in event.__dict__ or __pydantic_extra__
extra = getattr(event, "__pydantic_extra__", None)
if isinstance(extra, dict) and "context_management" in extra:
val = extra["context_management"]
if isinstance(val, dict):
class _D(dict):
def model_dump(self, *a: Any, **kw: Any) -> dict[str, Any]:
return dict(self)
extra["context_management"] = _D(val)
return original_make(self, event, *args, **kwargs)
raise

setattr(chat_cls, "_make_message_chunk_from_anthropic_event", _safe_make_message_chunk_from_anthropic_event)
setattr(chat_cls, _PATCHED_ATTR, True)
logger.debug("Applied ChatAnthropic context_management stream patch")
return True


__all__ = ["patch_langchain_anthropic_context_management"]
75 changes: 75 additions & 0 deletions src/octop/infra/agents/providers/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
logger = logging.getLogger(__name__)

_DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1"
_DEFAULT_ANTHROPIC_BASE_URL = "https://api.anthropic.com/v1"
_FETCH_MODELS_TIMEOUT_S = 30.0
_EMBEDDING_PROBE_TEXT = "ping"

Expand Down Expand Up @@ -376,3 +377,77 @@ async def fetch_openai_compatible_models(
models.append({"id": mid, "name": mid})
models.sort(key=lambda m: m["id"])
return {"ok": True, "models": models}


async def fetch_anthropic_models(
*,
base_url: str | None,
api_key: str,
extra_headers: dict[str, str] | None = None,
locale: str = "en",
provider_name: str | None = None,
) -> dict[str, Any]:
"""List models via Anthropic's ``GET {base}/models`` endpoint."""
root = (base_url or "").strip().rstrip("/") or _DEFAULT_ANTHROPIC_BASE_URL
url = f"{root}/models"
headers: dict[str, str] = {
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
}
extra_headers = ensure_opencode_session_header(provider_name, extra_headers, base_url=base_url)
if extra_headers:
headers.update(extra_headers)
try:
async with httpx.AsyncClient(timeout=_FETCH_MODELS_TIMEOUT_S) as client:
response = await client.get(url, headers=headers)
except Exception as exc:
logger.info(
"provider fetch-models (anthropic) failed for %s: %r (%s)",
url,
exc,
type(exc).__name__,
)
return {"ok": False, "error": _friendly_probe_error(exc, locale=locale)}

if response.status_code >= 400:
detail = response.text.strip()
if len(detail) > 300:
detail = detail[:300] + "…"
error = f"HTTP {response.status_code}"
if detail:
error = f"{error}: {detail}"
return {"ok": False, "error": _friendly_probe_error(error, locale=locale)}

try:
payload = response.json()
except Exception:
return {
"ok": False,
"error": "response is not valid JSON (expected Anthropic /models)",
}

data = payload.get("data") if isinstance(payload, dict) else None
if not isinstance(data, list):
return {
"ok": False,
"error": "response is not valid Anthropic models payload (expected {data: [{id, display_name, …}]})",
}

models: list[dict[str, str]] = []
seen: set[str] = set()
for item in data:
if not isinstance(item, dict):
continue
mid = item.get("id")
if not isinstance(mid, str) or not mid.strip():
continue
mid = mid.strip()
if mid in seen:
continue
seen.add(mid)
display_name = item.get("display_name")
name = display_name.strip() if isinstance(display_name, str) and display_name.strip() else mid
models.append({"id": mid, "name": name})
models.sort(key=lambda m: m["id"])
return {"ok": True, "models": models}

Loading
Loading