#!/usr/bin/env python3 """StructFix — Schema Compiler. Converts: - JSON Schema → DSL - OpenAI function/tool definitions → DSL - Anthropic tool definitions → DSL - MCP tool definitions → DSL DSL V1 format: FIELD TYPE [VALUES ] REQUIRED FIELD [] TYPE REQUIRED # array of primitives FIELD []. TYPE REQUIRED # array of objects TOOL ARG TYPE [VALUES ] REQUIRED """ import json from typing import List, Optional def _type_str(schema: dict) -> str: t = schema.get("type", "string") fmt = schema.get("format") if fmt: return f"{t}_{fmt}" return t def _required_set(schema: dict) -> set: return set(schema.get("required", [])) def _compile_object(properties: dict, required: set, prefix: str = "") -> List[str]: lines = [] for name, prop in properties.items(): path = f"{prefix}{name}" if not prefix else f"{prefix}.{name}" if prefix == "" and "." not in name: path = name elif prefix: path = f"{prefix}.{name}" t = prop.get("type", "string") if t == "object": sub_required = set(prop.get("required", [])) sub_props = prop.get("properties", {}) lines.extend(_compile_object(sub_props, sub_required, path)) continue if t == "array": items = prop.get("items", {"type": "string"}) items_type = items.get("type", "string") if items_type == "object": sub_required = set(items.get("required", [])) sub_props = items.get("properties", {}) arr_prefix = f"{path}[]." for sp in sub_props: lines.append( f"FIELD {arr_prefix}{sp} TYPE {_type_str(sub_props[sp])}" + (f" VALUES {'|'.join(sub_props[sp]['enum'])}" if "enum" in sub_props[sp] else "") + f" REQUIRED {'yes' if sp in sub_required else 'no'}" ) if sub_props[sp].get("type") == "object": deep_req = set(sub_props[sp].get("required", [])) deep_props = sub_props[sp].get("properties", {}) lines.extend(_compile_object(deep_props, deep_req, f"{arr_prefix}{sp}")) else: item_enum = "" if "enum" in items: item_enum = f" VALUES {'|'.join(items['enum'])}" lines.append( f"FIELD {path}[] TYPE {_type_str(items)}{item_enum}" + f" REQUIRED {'yes' if name in required else 'no'}" ) continue enum_str = "" if "enum" in prop: enum_str = f" VALUES {'|'.join(prop['enum'])}" req = "yes" if name in required else "no" lines.append(f"FIELD {path} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}") return lines def json_schema_to_dsl(schema: dict) -> str: if schema.get("type") != "object": return "FIELD root TYPE string REQUIRED yes" required = _required_set(schema) properties = schema.get("properties", {}) lines = _compile_object(properties, required) return "\n".join(lines) def openai_tool_to_dsl(tool: dict) -> str: func = tool.get("function", tool) name = func.get("name", "unknown") params = func.get("parameters", {}) required = set(params.get("required", [])) properties = params.get("properties", {}) lines = [f"TOOL {name}"] for pname, prop in properties.items(): t = prop.get("type", "string") enum_str = "" if "enum" in prop: enum_str = f" VALUES {'|'.join(prop['enum'])}" if t == "object": sub_lines = _compile_object( prop.get("properties", {}), set(prop.get("required", [])), pname ) for sl in sub_lines: lines.append(f"ARG {sl}") continue if t == "array": items = prop.get("items", {"type": "string"}) items_type = items.get("type", "string") item_enum = "" if "enum" in items: item_enum = f" VALUES {'|'.join(items['enum'])}" if items_type == "object": sub_lines = _compile_object( items.get("properties", {}), set(items.get("required", [])), f"{pname}[]." ) for sl in sub_lines: lines.append(f"ARG {sl}") else: lines.append(f"ARG {pname}[] TYPE {_type_str(items)}{item_enum} REQUIRED {'yes' if pname in required else 'no'}") continue req = "yes" if pname in required else "no" lines.append(f"ARG {pname} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}") return "\n".join(lines) def anthropic_tool_to_dsl(tool: dict) -> str: name = tool.get("name", "unknown") schema = tool.get("input_schema", {}) required = set(schema.get("required", [])) properties = schema.get("properties", {}) lines = [f"TOOL {name}"] for pname, prop in properties.items(): t = prop.get("type", "string") enum_str = "" if "enum" in prop: enum_str = f" VALUES {'|'.join(prop['enum'])}" if t == "object": sub_lines = _compile_object( prop.get("properties", {}), set(prop.get("required", [])), pname ) for sl in sub_lines: lines.append(f"ARG {sl}") continue req = "yes" if pname in required else "no" lines.append(f"ARG {pname} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}") return "\n".join(lines) def mcp_tool_to_dsl(tool: dict) -> str: name = tool.get("name", "unknown") schema = tool.get("inputSchema", tool.get("input_schema", {})) required = set(schema.get("required", [])) properties = schema.get("properties", {}) lines = [f"TOOL {name}"] for pname, prop in properties.items(): t = prop.get("type", "string") enum_str = "" if "enum" in prop: enum_str = f" VALUES {'|'.join(prop['enum'])}" req = "yes" if pname in required else "no" lines.append(f"ARG {pname} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}") return "\n".join(lines) def auto_compile(schema: dict, source: str = "json_schema") -> str: compilers = { "json_schema": json_schema_to_dsl, "openai": openai_tool_to_dsl, "anthropic": anthropic_tool_to_dsl, "mcp": mcp_tool_to_dsl, } return compilers.get(source, json_schema_to_dsl)(schema) if __name__ == "__main__": print("=== JSON Schema → DSL ===") schema1 = { "type": "object", "properties": { "priority": {"type": "string", "enum": ["low", "medium", "high"]}, "description": {"type": "string"}, "customer_id": {"type": "integer"}, }, "required": ["priority", "description"], } print(json_schema_to_dsl(schema1)) print() print("=== Nested ===") schema2 = { "type": "object", "properties": { "customer": { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "integer"}, }, "required": ["name"], }, "ticket": { "type": "object", "properties": { "priority": {"type": "string", "enum": ["low", "medium", "high"]}, }, "required": ["priority"], }, }, "required": ["customer", "ticket"], } print(json_schema_to_dsl(schema2)) print() print("=== Arrays ===") schema3 = { "type": "object", "properties": { "items": { "type": "array", "items": { "type": "object", "properties": { "sku": {"type": "string"}, "quantity": {"type": "integer"}, }, "required": ["sku"], }, }, "tags": { "type": "array", "items": {"type": "string"}, }, }, "required": ["items"], } print(json_schema_to_dsl(schema3)) print() print("=== OpenAI Tool → DSL ===") tool1 = { "type": "function", "function": { "name": "create_ticket", "parameters": { "type": "object", "properties": { "priority": {"type": "string", "enum": ["low", "medium", "high"]}, "description": {"type": "string"}, "customer_id": {"type": "integer"}, }, "required": ["priority", "description"], }, }, } print(openai_tool_to_dsl(tool1)) print() print("=== Anthropic Tool → DSL ===") tool2 = { "name": "query_port_status", "input_schema": { "type": "object", "properties": { "port_code": {"type": "string"}, "include_vessels": {"type": "boolean"}, }, "required": ["port_code"], }, } print(anthropic_tool_to_dsl(tool2)) print() print("=== MCP Tool → DSL ===") tool3 = { "name": "read_sensor", "inputSchema": { "type": "object", "properties": { "sensor_id": {"type": "string"}, "metric": {"type": "string", "enum": ["temperature", "humidity", "pressure"]}, }, "required": ["sensor_id", "metric"], }, } print(mcp_tool_to_dsl(tool3))