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
| 5 |
+
- JSON Schema → DSL
|
| 6 |
+
- OpenAI function/tool definitions → DSL
|
| 7 |
+
- Anthropic tool definitions → DSL
|
| 8 |
+
- MCP tool definitions → DSL
|
| 9 |
+
|
| 10 |
+
DSL V1 format:
|
| 11 |
+
FIELD <path> TYPE <type> [VALUES <v1|v2|...>] REQUIRED <yes|no>
|
| 12 |
+
FIELD <path>[] TYPE <type> REQUIRED <yes|no> # array of primitives
|
| 13 |
+
FIELD <path>[].<sub> TYPE <type> REQUIRED <yes|no> # array of objects
|
| 14 |
+
TOOL <name>
|
| 15 |
+
ARG <name> TYPE <type> [VALUES <v1|v2|...>] REQUIRED <yes|no>
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
from typing import List, Optional
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _type_str(schema: dict) -> str:
|
| 23 |
+
t = schema.get("type", "string")
|
| 24 |
+
fmt = schema.get("format")
|
| 25 |
+
if fmt:
|
| 26 |
+
return f"{t}_{fmt}"
|
| 27 |
+
return t
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _required_set(schema: dict) -> set:
|
| 31 |
+
return set(schema.get("required", []))
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _compile_object(properties: dict, required: set, prefix: str = "") -> List[str]:
|
| 35 |
+
lines = []
|
| 36 |
+
for name, prop in properties.items():
|
| 37 |
+
path = f"{prefix}{name}" if not prefix else f"{prefix}.{name}"
|
| 38 |
+
if prefix == "" and "." not in name:
|
| 39 |
+
path = name
|
| 40 |
+
elif prefix:
|
| 41 |
+
path = f"{prefix}.{name}"
|
| 42 |
+
|
| 43 |
+
t = prop.get("type", "string")
|
| 44 |
+
|
| 45 |
+
if t == "object":
|
| 46 |
+
sub_required = set(prop.get("required", []))
|
| 47 |
+
sub_props = prop.get("properties", {})
|
| 48 |
+
lines.extend(_compile_object(sub_props, sub_required, path))
|
| 49 |
+
continue
|
| 50 |
+
|
| 51 |
+
if t == "array":
|
| 52 |
+
items = prop.get("items", {"type": "string"})
|
| 53 |
+
items_type = items.get("type", "string")
|
| 54 |
+
|
| 55 |
+
if items_type == "object":
|
| 56 |
+
sub_required = set(items.get("required", []))
|
| 57 |
+
sub_props = items.get("properties", {})
|
| 58 |
+
arr_prefix = f"{path}[]."
|
| 59 |
+
for sp in sub_props:
|
| 60 |
+
lines.append(
|
| 61 |
+
f"FIELD {arr_prefix}{sp} TYPE {_type_str(sub_props[sp])}"
|
| 62 |
+
+ (f" VALUES {'|'.join(sub_props[sp]['enum'])}" if "enum" in sub_props[sp] else "")
|
| 63 |
+
+ f" REQUIRED {'yes' if sp in sub_required else 'no'}"
|
| 64 |
+
)
|
| 65 |
+
if sub_props[sp].get("type") == "object":
|
| 66 |
+
deep_req = set(sub_props[sp].get("required", []))
|
| 67 |
+
deep_props = sub_props[sp].get("properties", {})
|
| 68 |
+
lines.extend(_compile_object(deep_props, deep_req, f"{arr_prefix}{sp}"))
|
| 69 |
+
else:
|
| 70 |
+
item_enum = ""
|
| 71 |
+
if "enum" in items:
|
| 72 |
+
item_enum = f" VALUES {'|'.join(items['enum'])}"
|
| 73 |
+
lines.append(
|
| 74 |
+
f"FIELD {path}[] TYPE {_type_str(items)}{item_enum}"
|
| 75 |
+
+ f" REQUIRED {'yes' if name in required else 'no'}"
|
| 76 |
+
)
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
+
enum_str = ""
|
| 80 |
+
if "enum" in prop:
|
| 81 |
+
enum_str = f" VALUES {'|'.join(prop['enum'])}"
|
| 82 |
+
|
| 83 |
+
req = "yes" if name in required else "no"
|
| 84 |
+
lines.append(f"FIELD {path} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}")
|
| 85 |
+
|
| 86 |
+
return lines
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def json_schema_to_dsl(schema: dict) -> str:
|
| 90 |
+
if schema.get("type") != "object":
|
| 91 |
+
return "FIELD root TYPE string REQUIRED yes"
|
| 92 |
+
|
| 93 |
+
required = _required_set(schema)
|
| 94 |
+
properties = schema.get("properties", {})
|
| 95 |
+
lines = _compile_object(properties, required)
|
| 96 |
+
return "\n".join(lines)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def openai_tool_to_dsl(tool: dict) -> str:
|
| 100 |
+
func = tool.get("function", tool)
|
| 101 |
+
name = func.get("name", "unknown")
|
| 102 |
+
params = func.get("parameters", {})
|
| 103 |
+
required = set(params.get("required", []))
|
| 104 |
+
properties = params.get("properties", {})
|
| 105 |
+
|
| 106 |
+
lines = [f"TOOL {name}"]
|
| 107 |
+
for pname, prop in properties.items():
|
| 108 |
+
t = prop.get("type", "string")
|
| 109 |
+
enum_str = ""
|
| 110 |
+
if "enum" in prop:
|
| 111 |
+
enum_str = f" VALUES {'|'.join(prop['enum'])}"
|
| 112 |
+
|
| 113 |
+
if t == "object":
|
| 114 |
+
sub_lines = _compile_object(
|
| 115 |
+
prop.get("properties", {}),
|
| 116 |
+
set(prop.get("required", [])),
|
| 117 |
+
pname
|
| 118 |
+
)
|
| 119 |
+
for sl in sub_lines:
|
| 120 |
+
lines.append(f"ARG {sl}")
|
| 121 |
+
continue
|
| 122 |
+
|
| 123 |
+
if t == "array":
|
| 124 |
+
items = prop.get("items", {"type": "string"})
|
| 125 |
+
items_type = items.get("type", "string")
|
| 126 |
+
item_enum = ""
|
| 127 |
+
if "enum" in items:
|
| 128 |
+
item_enum = f" VALUES {'|'.join(items['enum'])}"
|
| 129 |
+
|
| 130 |
+
if items_type == "object":
|
| 131 |
+
sub_lines = _compile_object(
|
| 132 |
+
items.get("properties", {}),
|
| 133 |
+
set(items.get("required", [])),
|
| 134 |
+
f"{pname}[]."
|
| 135 |
+
)
|
| 136 |
+
for sl in sub_lines:
|
| 137 |
+
lines.append(f"ARG {sl}")
|
| 138 |
+
else:
|
| 139 |
+
lines.append(f"ARG {pname}[] TYPE {_type_str(items)}{item_enum} REQUIRED {'yes' if pname in required else 'no'}")
|
| 140 |
+
continue
|
| 141 |
+
|
| 142 |
+
req = "yes" if pname in required else "no"
|
| 143 |
+
lines.append(f"ARG {pname} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}")
|
| 144 |
+
|
| 145 |
+
return "\n".join(lines)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def anthropic_tool_to_dsl(tool: dict) -> str:
|
| 149 |
+
name = tool.get("name", "unknown")
|
| 150 |
+
schema = tool.get("input_schema", {})
|
| 151 |
+
required = set(schema.get("required", []))
|
| 152 |
+
properties = schema.get("properties", {})
|
| 153 |
+
|
| 154 |
+
lines = [f"TOOL {name}"]
|
| 155 |
+
for pname, prop in properties.items():
|
| 156 |
+
t = prop.get("type", "string")
|
| 157 |
+
enum_str = ""
|
| 158 |
+
if "enum" in prop:
|
| 159 |
+
enum_str = f" VALUES {'|'.join(prop['enum'])}"
|
| 160 |
+
|
| 161 |
+
if t == "object":
|
| 162 |
+
sub_lines = _compile_object(
|
| 163 |
+
prop.get("properties", {}),
|
| 164 |
+
set(prop.get("required", [])),
|
| 165 |
+
pname
|
| 166 |
+
)
|
| 167 |
+
for sl in sub_lines:
|
| 168 |
+
lines.append(f"ARG {sl}")
|
| 169 |
+
continue
|
| 170 |
+
|
| 171 |
+
req = "yes" if pname in required else "no"
|
| 172 |
+
lines.append(f"ARG {pname} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}")
|
| 173 |
+
|
| 174 |
+
return "\n".join(lines)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def mcp_tool_to_dsl(tool: dict) -> str:
|
| 178 |
+
name = tool.get("name", "unknown")
|
| 179 |
+
schema = tool.get("inputSchema", tool.get("input_schema", {}))
|
| 180 |
+
required = set(schema.get("required", []))
|
| 181 |
+
properties = schema.get("properties", {})
|
| 182 |
+
|
| 183 |
+
lines = [f"TOOL {name}"]
|
| 184 |
+
for pname, prop in properties.items():
|
| 185 |
+
t = prop.get("type", "string")
|
| 186 |
+
enum_str = ""
|
| 187 |
+
if "enum" in prop:
|
| 188 |
+
enum_str = f" VALUES {'|'.join(prop['enum'])}"
|
| 189 |
+
|
| 190 |
+
req = "yes" if pname in required else "no"
|
| 191 |
+
lines.append(f"ARG {pname} TYPE {_type_str(prop)}{enum_str} REQUIRED {req}")
|
| 192 |
+
|
| 193 |
+
return "\n".join(lines)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def auto_compile(schema: dict, source: str = "json_schema") -> str:
|
| 197 |
+
compilers = {
|
| 198 |
+
"json_schema": json_schema_to_dsl,
|
| 199 |
+
"openai": openai_tool_to_dsl,
|
| 200 |
+
"anthropic": anthropic_tool_to_dsl,
|
| 201 |
+
"mcp": mcp_tool_to_dsl,
|
| 202 |
+
}
|
| 203 |
+
return compilers.get(source, json_schema_to_dsl)(schema)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
if __name__ == "__main__":
|
| 207 |
+
print("=== JSON Schema → DSL ===")
|
| 208 |
+
schema1 = {
|
| 209 |
+
"type": "object",
|
| 210 |
+
"properties": {
|
| 211 |
+
"priority": {"type": "string", "enum": ["low", "medium", "high"]},
|
| 212 |
+
"description": {"type": "string"},
|
| 213 |
+
"customer_id": {"type": "integer"},
|
| 214 |
+
},
|
| 215 |
+
"required": ["priority", "description"],
|
| 216 |
+
}
|
| 217 |
+
print(json_schema_to_dsl(schema1))
|
| 218 |
+
print()
|
| 219 |
+
|
| 220 |
+
print("=== Nested ===")
|
| 221 |
+
schema2 = {
|
| 222 |
+
"type": "object",
|
| 223 |
+
"properties": {
|
| 224 |
+
"customer": {
|
| 225 |
+
"type": "object",
|
| 226 |
+
"properties": {
|
| 227 |
+
"name": {"type": "string"},
|
| 228 |
+
"age": {"type": "integer"},
|
| 229 |
+
},
|
| 230 |
+
"required": ["name"],
|
| 231 |
+
},
|
| 232 |
+
"ticket": {
|
| 233 |
+
"type": "object",
|
| 234 |
+
"properties": {
|
| 235 |
+
"priority": {"type": "string", "enum": ["low", "medium", "high"]},
|
| 236 |
+
},
|
| 237 |
+
"required": ["priority"],
|
| 238 |
+
},
|
| 239 |
+
},
|
| 240 |
+
"required": ["customer", "ticket"],
|
| 241 |
+
}
|
| 242 |
+
print(json_schema_to_dsl(schema2))
|
| 243 |
+
print()
|
| 244 |
+
|
| 245 |
+
print("=== Arrays ===")
|
| 246 |
+
schema3 = {
|
| 247 |
+
"type": "object",
|
| 248 |
+
"properties": {
|
| 249 |
+
"items": {
|
| 250 |
+
"type": "array",
|
| 251 |
+
"items": {
|
| 252 |
+
"type": "object",
|
| 253 |
+
"properties": {
|
| 254 |
+
"sku": {"type": "string"},
|
| 255 |
+
"quantity": {"type": "integer"},
|
| 256 |
+
},
|
| 257 |
+
"required": ["sku"],
|
| 258 |
+
},
|
| 259 |
+
},
|
| 260 |
+
"tags": {
|
| 261 |
+
"type": "array",
|
| 262 |
+
"items": {"type": "string"},
|
| 263 |
+
},
|
| 264 |
+
},
|
| 265 |
+
"required": ["items"],
|
| 266 |
+
}
|
| 267 |
+
print(json_schema_to_dsl(schema3))
|
| 268 |
+
print()
|
| 269 |
+
|
| 270 |
+
print("=== OpenAI Tool → DSL ===")
|
| 271 |
+
tool1 = {
|
| 272 |
+
"type": "function",
|
| 273 |
+
"function": {
|
| 274 |
+
"name": "create_ticket",
|
| 275 |
+
"parameters": {
|
| 276 |
+
"type": "object",
|
| 277 |
+
"properties": {
|
| 278 |
+
"priority": {"type": "string", "enum": ["low", "medium", "high"]},
|
| 279 |
+
"description": {"type": "string"},
|
| 280 |
+
"customer_id": {"type": "integer"},
|
| 281 |
+
},
|
| 282 |
+
"required": ["priority", "description"],
|
| 283 |
+
},
|
| 284 |
+
},
|
| 285 |
+
}
|
| 286 |
+
print(openai_tool_to_dsl(tool1))
|
| 287 |
+
print()
|
| 288 |
+
|
| 289 |
+
print("=== Anthropic Tool → DSL ===")
|
| 290 |
+
tool2 = {
|
| 291 |
+
"name": "query_port_status",
|
| 292 |
+
"input_schema": {
|
| 293 |
+
"type": "object",
|
| 294 |
+
"properties": {
|
| 295 |
+
"port_code": {"type": "string"},
|
| 296 |
+
"include_vessels": {"type": "boolean"},
|
| 297 |
+
},
|
| 298 |
+
"required": ["port_code"],
|
| 299 |
+
},
|
| 300 |
+
}
|
| 301 |
+
print(anthropic_tool_to_dsl(tool2))
|
| 302 |
+
print()
|
| 303 |
+
|
| 304 |
+
print("=== MCP Tool → DSL ===")
|
| 305 |
+
tool3 = {
|
| 306 |
+
"name": "read_sensor",
|
| 307 |
+
"inputSchema": {
|
| 308 |
+
"type": "object",
|
| 309 |
+
"properties": {
|
| 310 |
+
"sensor_id": {"type": "string"},
|
| 311 |
+
"metric": {"type": "string", "enum": ["temperature", "humidity", "pressure"]},
|
| 312 |
+
},
|
| 313 |
+
"required": ["sensor_id", "metric"],
|
| 314 |
+
},
|
| 315 |
+
}
|
| 316 |
+
print(mcp_tool_to_dsl(tool3))
|