|
8 | 8 | from __future__ import annotations |
9 | 9 |
|
10 | 10 | from collections.abc import Mapping, Sequence |
11 | | -from typing import TYPE_CHECKING, Any, Literal |
| 11 | +from typing import TYPE_CHECKING, Any, Literal, cast |
12 | 12 |
|
13 | 13 | from rsb.models.base_model import BaseModel |
14 | 14 | from rsb.models.field import Field |
@@ -39,3 +39,88 @@ class ObjectSchema(BaseModel): |
39 | 39 | example: Mapping[str, Any] | None = Field( |
40 | 40 | default=None, description="Example value for the object" |
41 | 41 | ) |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def from_json_schema( |
| 45 | + cls, schema: Mapping[str, Any] |
| 46 | + ) -> ObjectSchema | ArraySchema | PrimitiveSchema: |
| 47 | + """ |
| 48 | + Recursively convert a JSON Schema definition to Agentle schema types. |
| 49 | + |
| 50 | + This method handles deeply nested objects, arrays, and primitives, |
| 51 | + making it easy to convert complex JSON Schema definitions. |
| 52 | + |
| 53 | + Args: |
| 54 | + schema: JSON Schema definition (dict with 'type', 'properties', etc.) |
| 55 | + |
| 56 | + Returns: |
| 57 | + Appropriate schema type (ObjectSchema, ArraySchema, or PrimitiveSchema) |
| 58 | + |
| 59 | + Example: |
| 60 | + ```python |
| 61 | + from agentle.agents.apis.object_schema import ObjectSchema |
| 62 | + |
| 63 | + json_schema = { |
| 64 | + "type": "object", |
| 65 | + "properties": { |
| 66 | + "user": { |
| 67 | + "type": "object", |
| 68 | + "properties": { |
| 69 | + "name": {"type": "string"}, |
| 70 | + "age": {"type": "integer"}, |
| 71 | + "settings": { |
| 72 | + "type": "object", |
| 73 | + "properties": { |
| 74 | + "theme": {"type": "string"}, |
| 75 | + "notifications": {"type": "boolean"} |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + schema = ObjectSchema.from_json_schema(json_schema) |
| 84 | + ``` |
| 85 | + """ |
| 86 | + from agentle.agents.apis.array_schema import ArraySchema |
| 87 | + from agentle.agents.apis.primitive_schema import PrimitiveSchema |
| 88 | + |
| 89 | + schema_type = schema.get("type", "string") |
| 90 | + |
| 91 | + if schema_type == "object": |
| 92 | + properties: dict[str, ObjectSchema | ArraySchema | PrimitiveSchema] = {} |
| 93 | + for prop_name, prop_schema in schema.get("properties", {}).items(): |
| 94 | + properties[prop_name] = cls.from_json_schema(prop_schema) |
| 95 | + |
| 96 | + return cls( |
| 97 | + properties=properties, |
| 98 | + required=list(schema.get("required", [])), |
| 99 | + additional_properties=schema.get("additionalProperties", True), |
| 100 | + example=schema.get("example"), |
| 101 | + ) |
| 102 | + |
| 103 | + elif schema_type == "array": |
| 104 | + items_schema = schema.get("items", {"type": "string"}) |
| 105 | + return ArraySchema( |
| 106 | + items=cls.from_json_schema(items_schema), |
| 107 | + min_items=schema.get("minItems"), |
| 108 | + max_items=schema.get("maxItems"), |
| 109 | + example=schema.get("example"), |
| 110 | + ) |
| 111 | + |
| 112 | + else: |
| 113 | + # Primitive type |
| 114 | + return PrimitiveSchema( |
| 115 | + type=cast( |
| 116 | + Literal["string", "integer", "boolean", "number"], schema_type |
| 117 | + ) |
| 118 | + if schema_type in ["string", "integer", "number", "boolean"] |
| 119 | + else "string", |
| 120 | + format=schema.get("format"), |
| 121 | + enum=schema.get("enum"), |
| 122 | + minimum=schema.get("minimum"), |
| 123 | + maximum=schema.get("maximum"), |
| 124 | + pattern=schema.get("pattern"), |
| 125 | + example=schema.get("example"), |
| 126 | + ) |
0 commit comments