Official Codebase of 3DCodeVerse
Python
models: an SdkModel base owns the SDK adapters' constructor, id and lazy client — anthropic and openai stated them word for word
gaoypeng82e695b5c3bb
diff --git a/harness/codeverse3d/models/anthropic.py b/harness/codeverse3d/models/anthropic.pyindex faf4e700..f3a9b65d 100644--- a/harness/codeverse3d/models/anthropic.py+++ b/harness/codeverse3d/models/anthropic.py@@ -12,9 +12,7 @@ Model-family rules (Anthropic API, 2026-08):from __future__ import annotationsimport json-import threadingimport time-from collections.abc import Callablefrom typing import Anyfrom codeverse3d.contracts.chat import (@@ -27,6 +25,7 @@ from codeverse3d.contracts.chat import (from codeverse3d.contracts.common import Usagefrom codeverse3d.models.base import ModelErrorfrom codeverse3d.models.parts import (+ SdkModel,Stopwatch,attempt_timeout_s,classify_sdk_exception,@@ -185,46 +184,20 @@ def classify_exception(exc: BaseException) -> ModelError:return classify_sdk_exception(exc, anthropic, "Anthropic", extra_retry=frozenset({529}))-class AnthropicModel:+class AnthropicModel(SdkModel):"""ChatModel for ``anthropic:<model>``."""provider = "anthropic"- def __init__(- self,- model: str,- *,- timeout_s: float = 600.0,- max_attempts: int = 6,- sleep: Callable[[float], None] = time.sleep,- client: Any | None = None,- ) -> None:- self.model = model- self.timeout_s = timeout_s- self.max_attempts = max(1, max_attempts)- self._sleep = sleep- self._client = client- self._lock = threading.Lock()-- @property- def id(self) -> str:- return f"anthropic:{self.model}"-- # ---------------------------------------------------------------- client- def client(self) -> Any:- with self._lock:- if self._client is None:- import anthropic-- from codeverse3d.config import get_settings-- key = get_settings().anthropic_api_key- if not key:- raise ModelError("ANTHROPIC_API_KEY is not configured")- self._client = anthropic.Anthropic(- api_key=key, max_retries=0, timeout=self.timeout_s- )- return self._client+ def _make_client(self) -> Any:+ import anthropic++ from codeverse3d.config import get_settings++ key = get_settings().anthropic_api_key+ if not key:+ raise ModelError("ANTHROPIC_API_KEY is not configured")+ return anthropic.Anthropic(api_key=key, max_retries=0, timeout=self.timeout_s)# -------------------------------------------------------------- generatedef generate(self, request: ChatRequest) -> ChatResponse:diff --git a/harness/codeverse3d/models/openai.py b/harness/codeverse3d/models/openai.pyindex 952f8d51..099d5379 100644--- a/harness/codeverse3d/models/openai.py+++ b/harness/codeverse3d/models/openai.py@@ -8,9 +8,7 @@ the Responses API is OpenAI-only.from __future__ import annotationsimport logging-import threadingimport time-from collections.abc import Callablefrom typing import Anyfrom codeverse3d.contracts.chat import (@@ -23,6 +21,7 @@ from codeverse3d.contracts.chat import (from codeverse3d.contracts.common import Usagefrom codeverse3d.models.base import ModelErrorfrom codeverse3d.models.parts import (+ SdkModel,Stopwatch,attempt_timeout_s,classify_sdk_exception,@@ -152,52 +151,28 @@ def _is_schema_rejection(err: ModelError) -> bool:return err.status == 400 and ("schema" in s or "response_format" in s or "strict" in s)-class OpenAIModel:+class OpenAIModel(SdkModel):"""ChatModel for ``openai:<model>`` (and OpenAI-compatible endpoints)."""provider = "openai"-- def __init__(- self,- model: str,- *,- timeout_s: float = 600.0,- max_attempts: int = 6,- sleep: Callable[[float], None] = time.sleep,- client: Any | None = None,- ) -> None:- self.model = model- self.timeout_s = timeout_s- self.max_attempts = max(1, max_attempts)- self._sleep = sleep- self._client = client- self._strict_ok = True # flipped when the endpoint rejects strict schemas- self._lock = threading.Lock()-- @property- def id(self) -> str:- return f"openai:{self.model}"-- # ---------------------------------------------------------------- client- def client(self) -> Any:- with self._lock:- if self._client is None:- import openai-- from codeverse3d.config import get_settings-- s = get_settings()- key = s.openai_api_key- base_url = s.openai_base_url or None- if not key and not base_url:- raise ModelError("OPENAI_API_KEY is not configured")- self._client = openai.OpenAI(- api_key=key or "sk-local",- base_url=base_url,- max_retries=0,- timeout=self.timeout_s,- )- return self._client+ _strict_ok = True # flipped (per instance) when the endpoint rejects strict schemas++ def _make_client(self) -> Any:+ import openai++ from codeverse3d.config import get_settings++ s = get_settings()+ key = s.openai_api_key+ base_url = s.openai_base_url or None+ if not key and not base_url:+ raise ModelError("OPENAI_API_KEY is not configured")+ return openai.OpenAI(+ api_key=key or "sk-local",+ base_url=base_url,+ max_retries=0,+ timeout=self.timeout_s,+ )# -------------------------------------------------------------- generatedef generate(self, request: ChatRequest) -> ChatResponse:diff --git a/harness/codeverse3d/models/parts.py b/harness/codeverse3d/models/parts.pyindex 3de00de4..56060469 100644--- a/harness/codeverse3d/models/parts.py+++ b/harness/codeverse3d/models/parts.py@@ -4,7 +4,9 @@ from __future__ import annotationsimport base64import mimetypes+import threadingimport time+from collections.abc import Callablefrom pathlib import Pathfrom typing import Any@@ -49,6 +51,42 @@ def image_b64(part: ImagePart) -> tuple[str, str]:return base64.b64encode(raw).decode("ascii"), mime+class SdkModel:+ """The shell the SDK adapters (anthropic, openai) share: the constructor, ``id`` and the+ lazily built client. Each adapter owns ``_make_client`` and its own ``generate``."""++ provider = ""++ def __init__(+ self,+ model: str,+ *,+ timeout_s: float = 600.0,+ max_attempts: int = 6,+ sleep: Callable[[float], None] = time.sleep,+ client: Any | None = None,+ ) -> None:+ self.model = model+ self.timeout_s = timeout_s+ self.max_attempts = max(1, max_attempts)+ self._sleep = sleep+ self._client = client+ self._lock = threading.Lock()++ @property+ def id(self) -> str:+ return f"{self.provider}:{self.model}"++ def client(self) -> Any:+ with self._lock:+ if self._client is None:+ self._client = self._make_client()+ return self._client++ def _make_client(self) -> Any:+ raise NotImplementedError++class Stopwatch:"""``with Stopwatch() as sw: ...; sw.ms``"""