Skip to content

Official Codebase of 3DCodeVerse

Python

models: parts.message_blocks is the one part loop of the three provider converters — the '[image: <label>]' text a model sees is written once

gaoypeng25ea14b5d9be
diff --git a/harness/codeverse3d/models/anthropic.py b/harness/codeverse3d/models/anthropic.py
index f3a9b65d..20b94ce9 100644
--- a/harness/codeverse3d/models/anthropic.py
+++ b/harness/codeverse3d/models/anthropic.py
@@ -20,7 +20,6 @@ from codeverse3d.contracts.chat import (
ChatRequest,
ChatResponse,
ImagePart,
- TextPart,
)
from codeverse3d.contracts.common import Usage
from codeverse3d.models.base import ModelError
@@ -30,6 +29,7 @@ from codeverse3d.models.parts import (
attempt_timeout_s,
classify_sdk_exception,
image_b64,
+ message_blocks,
retry_budget_s,
retry_one_key,
)
@@ -85,15 +85,7 @@ def to_messages(messages: list[ChatMessage]) -> list[dict[str, Any]]:
out: list[dict[str, Any]] = []
for msg in messages:
role = "assistant" if msg.role == "assistant" else "user"
- blocks: list[dict[str, Any]] = []
- for p in msg.parts:
- if isinstance(p, TextPart):
- if p.text:
- blocks.append({"type": "text", "text": p.text})
- elif isinstance(p, ImagePart):
- if p.label:
- blocks.append({"type": "text", "text": f"[image: {p.label}]"})
- blocks.append(_image_block(p))
+ blocks = message_blocks(msg, text=lambda t: {"type": "text", "text": t}, image=_image_block)
if not blocks:
continue
if out and out[-1]["role"] == role:
diff --git a/harness/codeverse3d/models/gemini.py b/harness/codeverse3d/models/gemini.py
index fbba4c12..5085b118 100644
--- a/harness/codeverse3d/models/gemini.py
+++ b/harness/codeverse3d/models/gemini.py
@@ -32,14 +32,19 @@ from codeverse3d.contracts.chat import (
ChatRequest,
ChatResponse,
ImagePart,
- TextPart,
)
from codeverse3d.contracts.common import Usage
from codeverse3d.cost.context import AttemptRecord, attempt_sink
from codeverse3d.cost.ledger import record_call
from codeverse3d.cost.types import Role, Stage
from codeverse3d.models.base import ModelError
-from codeverse3d.models.parts import Stopwatch, attempt_timeout_s, image_bytes, retry_budget_s
+from codeverse3d.models.parts import (
+ Stopwatch,
+ attempt_timeout_s,
+ image_bytes,
+ message_blocks,
+ retry_budget_s,
+)
from codeverse3d.models.pricing import estimate_cost, per_image_usd
from codeverse3d.models.retry import KeyPool, KeyPoolExhausted, OnAttempt, _Try, rotate_with_retries
from codeverse3d.models.schema_utils import JsonParseError, parse_json_lenient, to_gemini_schema
@@ -54,21 +59,17 @@ FATAL_FINISH = {"SAFETY", "PROHIBITED_CONTENT", "BLOCKLIST", "SPII", "IMAGE_SAFE
# ------------------------------------------------------------------ contents
+def _image_part(p: ImagePart) -> types.Part:
+ raw, mime = image_bytes(p)
+ return types.Part.from_bytes(data=raw, mime_type=mime)
+
+
def to_contents(messages: list[ChatMessage]) -> list[types.Content]:
"""ChatMessages → Gemini ``Content`` list."""
out: list[types.Content] = []
for msg in messages:
role = "model" if msg.role == "assistant" else "user"
- parts: list[types.Part] = []
- for p in msg.parts:
- if isinstance(p, TextPart):
- if p.text:
- parts.append(types.Part.from_text(text=p.text))
- elif isinstance(p, ImagePart):
- raw, mime = image_bytes(p)
- if p.label:
- parts.append(types.Part.from_text(text=f"[image: {p.label}]"))
- parts.append(types.Part.from_bytes(data=raw, mime_type=mime))
+ parts: list[types.Part] = message_blocks(msg, text=lambda t: types.Part.from_text(text=t), image=_image_part)
if not parts:
continue
# merge consecutive same-role turns (Gemini wants strict alternation)
diff --git a/harness/codeverse3d/models/openai.py b/harness/codeverse3d/models/openai.py
index 90851025..839f0384 100644
--- a/harness/codeverse3d/models/openai.py
+++ b/harness/codeverse3d/models/openai.py
@@ -26,6 +26,7 @@ from codeverse3d.models.parts import (
attempt_timeout_s,
classify_sdk_exception,
image_b64,
+ message_blocks,
retry_budget_s,
retry_one_key,
)
@@ -79,15 +80,7 @@ def to_messages(messages: list[ChatMessage], system: str) -> list[dict[str, Any]
if msg.role == "assistant":
out.append(_assistant_message(msg))
continue
- content: list[dict[str, Any]] = []
- for p in msg.parts:
- if isinstance(p, TextPart):
- if p.text:
- content.append({"type": "text", "text": p.text})
- elif isinstance(p, ImagePart):
- if p.label:
- content.append({"type": "text", "text": f"[image: {p.label}]"})
- content.append(_image_block(p))
+ content = message_blocks(msg, text=lambda t: {"type": "text", "text": t}, image=_image_block)
if content:
out.append({"role": "user", "content": content})
if not out or all(m["role"] == "system" for m in out):
diff --git a/harness/codeverse3d/models/parts.py b/harness/codeverse3d/models/parts.py
index 20f69cc4..0f18defd 100644
--- a/harness/codeverse3d/models/parts.py
+++ b/harness/codeverse3d/models/parts.py
@@ -10,7 +10,7 @@ from collections.abc import Callable
from pathlib import Path
from typing import Any
-from codeverse3d.contracts.chat import ImagePart
+from codeverse3d.contracts.chat import ChatMessage, ImagePart, TextPart
from codeverse3d.models.base import ModelError
@@ -39,6 +39,21 @@ def image_b64(part: ImagePart) -> tuple[str, str]:
return base64.b64encode(raw).decode("ascii"), mime
+def message_blocks(msg: ChatMessage, *, text: Callable[[str], Any], image: Callable[[ImagePart], Any]) -> list[Any]:
+ """One message's parts as a provider's blocks: empty text is dropped, and a labelled image
+ is preceded by an ``[image: <label>]`` text block (the one place that text is written)."""
+ out: list[Any] = []
+ for p in msg.parts:
+ if isinstance(p, TextPart):
+ if p.text:
+ out.append(text(p.text))
+ elif isinstance(p, ImagePart):
+ if p.label:
+ out.append(text(f"[image: {p.label}]"))
+ out.append(image(p))
+ return out
+
+
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``."""