File size: 3,871 Bytes
8f1f637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Agent I/O schemas (Pydantic) + helpers to emit OpenAI strict json_schema.

Strict mode requires every object to set additionalProperties:false and list all
properties as required. `strictify` enforces that recursively over a Pydantic schema.
"""
from __future__ import annotations

from typing import Any, Literal

from pydantic import BaseModel, Field


# ---------------------------------------------------------------------------
# Vision agent
# ---------------------------------------------------------------------------
class Dimensions(BaseModel):
    height_mm: float
    width_mm: float
    depth_mm: float


class VisionSpec(BaseModel):
    object: str = Field(description="Concise name of the primary object")
    geometry: str = Field(description="Geometric description (shape, parts, proportions)")
    dimensions: Dimensions = Field(description="Best-estimate real-world size in mm")
    materials: list[str]
    features: list[str] = Field(description="Salient features: handles, holes, ribs, text")
    defects: list[str] = Field(description="Visible damage/defects, empty if none")
    confidence: float = Field(description="0..1 confidence in the analysis")


# ---------------------------------------------------------------------------
# Planner agent
# ---------------------------------------------------------------------------
class Primitive(BaseModel):
    type: Literal["box", "cylinder", "sphere", "torus"]
    op: Literal["add", "subtract"] = Field(description="Boolean op vs the accumulated body")
    dims_mm: list[float] = Field(description="box:[x,y,z] cyl:[r,h] sphere:[r] torus:[R,r]")
    pose_mm: list[float] = Field(description="[x,y,z] translation of this primitive's center")


class FabPlan(BaseModel):
    fab_method: str = Field(description="e.g. '3D printing (FDM)', 'CNC', 'repair'")
    steps: list[str] = Field(description="Human-readable fabrication steps")
    primitives: list[Primitive] = Field(description="Constructive primitives, applied in order")
    notes: str


# ---------------------------------------------------------------------------
# Generator agent
# ---------------------------------------------------------------------------
class GeneratedArtifact(BaseModel):
    lang: Literal["trimesh"] = "trimesh"
    code: str = Field(description="Python using `trimesh` and `np`; must assign `result`")
    est_bbox_mm: list[float] = Field(description="Estimated bounding box [x,y,z]")


# ---------------------------------------------------------------------------
# Critic agent
# ---------------------------------------------------------------------------
class CritiqueVerdict(BaseModel):
    approved: bool
    score: float = Field(description="0..1 fidelity vs the analyzed object")
    issues: list[str]
    fix_instructions: str = Field(description="Concrete changes for the generator, empty if approved")


class BestSelection(BaseModel):
    best_index: int = Field(description="0-based index of the candidate render that best matches the photo")
    reason: str


def strictify(schema: dict[str, Any]) -> dict[str, Any]:
    """Recursively make a JSON schema strict-mode compatible."""
    if isinstance(schema, dict):
        if schema.get("type") == "object" and "properties" in schema:
            schema["additionalProperties"] = False
            schema["required"] = list(schema["properties"].keys())
        for v in schema.values():
            strictify(v)
    elif isinstance(schema, list):
        for v in schema:
            strictify(v)
    return schema


def response_format(model: type[BaseModel]) -> dict:
    """Build a Cerebras/OpenAI strict json_schema response_format from a Pydantic model."""
    schema = strictify(model.model_json_schema())
    return {
        "type": "json_schema",
        "json_schema": {"name": model.__name__, "strict": True, "schema": schema},
    }