File size: 1,650 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 | # SPDX-License-Identifier: MIT
"""
Base64 Plugin for TrashClaw
Encode or decode text and files using Base64.
Plugin contract:
TOOL_DEF = dict with name, description, parameters (OpenAI function schema)
run(**kwargs) -> str (called with the parsed arguments)
"""
import base64 as _b64
import os
TOOL_DEF = {
"name": "base64",
"description": "Encode or decode text using Base64 encoding.",
"parameters": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "Text to encode or decode"
},
"action": {
"type": "string",
"description": "Action to perform: encode or decode (default: encode)"
}
},
"required": ["text"]
}
}
def run(text: str = "", action: str = "encode", **kwargs) -> str:
action = action.lower().strip()
if not text:
return "Error: provide 'text' to encode or decode."
if action == "encode":
try:
encoded = _b64.b64encode(text.encode("utf-8")).decode("utf-8")
return f"Base64 encoded:\n{encoded}"
except Exception as e:
return f"Error encoding: {e}"
elif action == "decode":
try:
# Add padding if needed
padded = text + "=" * (4 - len(text) % 4) if len(text) % 4 else text
decoded = _b64.b64decode(padded).decode("utf-8")
return f"Base64 decoded:\n{decoded}"
except Exception as e:
return f"Error decoding: {e}"
else:
return f"Unknown action '{action}'. Use: encode, decode"
|