File size: 2,120 Bytes
53b2ac9 | 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 | # SPDX-License-Identifier: MIT
"""
JSON Format Plugin for TrashClaw
Pretty-print, validate, minify, or extract keys from JSON text.
Plugin contract:
TOOL_DEF = dict with name, description, parameters (OpenAI function schema)
run(**kwargs) -> str (called with the parsed arguments)
"""
import json
TOOL_DEF = {
"name": "json_format",
"description": "Pretty-print, validate, minify, or extract keys from JSON text.",
"parameters": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "JSON text to process"
},
"action": {
"type": "string",
"description": "Action: pretty (default), minify, validate, keys"
},
"indent": {
"type": "integer",
"description": "Indentation spaces for pretty mode (default: 2)"
}
},
"required": ["text"]
}
}
def run(text: str = "", action: str = "pretty", indent: int = 2, **kwargs) -> str:
if not text:
return "Error: provide 'text' to format."
action = action.lower().strip()
try:
data = json.loads(text)
except json.JSONDecodeError as e:
return f"Invalid JSON: {e}"
if action == "pretty":
return json.dumps(data, indent=indent, ensure_ascii=False)
elif action == "minify":
return json.dumps(data, separators=(",", ":"), ensure_ascii=False)
elif action == "validate":
kind = type(data).__name__
if isinstance(data, dict):
return f"Valid JSON object with {len(data)} keys: {', '.join(list(data.keys())[:10])}"
elif isinstance(data, list):
return f"Valid JSON array with {len(data)} items"
else:
return f"Valid JSON {kind}: {data}"
elif action == "keys":
if not isinstance(data, dict):
return "Error: 'keys' action requires a JSON object (dict)"
return "\n".join(data.keys())
else:
return f"Unknown action '{action}'. Use: pretty, minify, validate, keys"
|