Spaces:
Running
Running
Update pygmyclaw_multitool.py
Browse files- pygmyclaw_multitool.py +106 -34
pygmyclaw_multitool.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
PygmyClaw Multitool – Contains the actual tool implementations.
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
|
|
|
| 5 |
import json
|
| 6 |
import sys
|
| 7 |
import os
|
|
@@ -10,11 +14,15 @@ import time
|
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
SCRIPT_DIR = Path(__file__).parent.resolve()
|
|
|
|
| 13 |
ERROR_LOG = SCRIPT_DIR / "error_log.json"
|
|
|
|
| 14 |
MAX_LOG_ENTRIES = 1000
|
| 15 |
|
|
|
|
| 16 |
# ----------------------------------------------------------------------
|
| 17 |
# Tool definitions
|
|
|
|
| 18 |
TOOLS = {
|
| 19 |
"list_tools_detailed": {
|
| 20 |
"name": "list_tools_detailed",
|
|
@@ -22,12 +30,14 @@ TOOLS = {
|
|
| 22 |
"parameters": {},
|
| 23 |
"func": "do_list_tools"
|
| 24 |
},
|
|
|
|
| 25 |
"sys_info": {
|
| 26 |
"name": "sys_info",
|
| 27 |
"description": "Get system information (OS, Python version, etc.).",
|
| 28 |
"parameters": {},
|
| 29 |
"func": "do_sys_info"
|
| 30 |
},
|
|
|
|
| 31 |
"log_error": {
|
| 32 |
"name": "log_error",
|
| 33 |
"description": "Log an error message to the error log.",
|
|
@@ -37,6 +47,7 @@ TOOLS = {
|
|
| 37 |
},
|
| 38 |
"func": "do_log_error"
|
| 39 |
},
|
|
|
|
| 40 |
"echo": {
|
| 41 |
"name": "echo",
|
| 42 |
"description": "Echo the input text (for testing).",
|
|
@@ -45,19 +56,30 @@ TOOLS = {
|
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def do_list_tools():
|
| 49 |
-
"""Return the list of tools with
|
|
|
|
| 50 |
tools_list = []
|
|
|
|
| 51 |
for name, info in TOOLS.items():
|
|
|
|
| 52 |
tools_list.append({
|
| 53 |
"name": name,
|
| 54 |
"description": info["description"],
|
| 55 |
"parameters": info["parameters"]
|
| 56 |
})
|
|
|
|
| 57 |
return {"tools": tools_list}
|
| 58 |
|
|
|
|
| 59 |
def do_sys_info():
|
| 60 |
"""Return system information."""
|
|
|
|
| 61 |
return {
|
| 62 |
"os": platform.system(),
|
| 63 |
"os_release": platform.release(),
|
|
@@ -65,74 +87,124 @@ def do_sys_info():
|
|
| 65 |
"hostname": platform.node()
|
| 66 |
}
|
| 67 |
|
|
|
|
| 68 |
def do_log_error(msg, trace=""):
|
| 69 |
-
"""Append an error to the error log
|
|
|
|
| 70 |
entry = {
|
| 71 |
"timestamp": time.time(),
|
| 72 |
"msg": msg,
|
| 73 |
"trace": trace
|
| 74 |
}
|
|
|
|
| 75 |
try:
|
|
|
|
| 76 |
if ERROR_LOG.exists():
|
|
|
|
| 77 |
with open(ERROR_LOG) as f:
|
| 78 |
log = json.load(f)
|
|
|
|
| 79 |
else:
|
|
|
|
| 80 |
log = []
|
|
|
|
| 81 |
log.append(entry)
|
| 82 |
-
|
| 83 |
if len(log) > MAX_LOG_ENTRIES:
|
| 84 |
log = log[-MAX_LOG_ENTRIES:]
|
| 85 |
-
|
|
|
|
| 86 |
json.dump(log, f, indent=2)
|
|
|
|
| 87 |
return {"status": "logged"}
|
|
|
|
| 88 |
except Exception as e:
|
|
|
|
| 89 |
return {"error": f"Failed to write log: {e}"}
|
| 90 |
|
|
|
|
| 91 |
def do_echo(text):
|
| 92 |
-
"""Echo
|
|
|
|
| 93 |
return {"echo": text}
|
| 94 |
|
|
|
|
| 95 |
# ----------------------------------------------------------------------
|
| 96 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
def main():
|
|
|
|
| 98 |
try:
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
action = data.get("action")
|
|
|
|
| 101 |
if not action:
|
|
|
|
| 102 |
print(json.dumps({"error": "No action specified"}))
|
| 103 |
return
|
| 104 |
|
| 105 |
-
|
| 106 |
-
tool_info = TOOLS.get(action)
|
| 107 |
-
if not tool_info:
|
| 108 |
-
print(json.dumps({"error": f"Unknown action '{action}'"}))
|
| 109 |
-
return
|
| 110 |
|
| 111 |
-
|
| 112 |
-
func_name = tool_info["func"]
|
| 113 |
-
if func_name == "do_list_tools":
|
| 114 |
-
result = do_list_tools()
|
| 115 |
-
elif func_name == "do_sys_info":
|
| 116 |
-
result = do_sys_info()
|
| 117 |
-
elif func_name == "do_log_error":
|
| 118 |
-
msg = data.get("msg")
|
| 119 |
-
trace = data.get("trace", "")
|
| 120 |
-
if msg is None:
|
| 121 |
-
result = {"error": "Missing 'msg' parameter"}
|
| 122 |
-
else:
|
| 123 |
-
result = do_log_error(msg, trace)
|
| 124 |
-
elif func_name == "do_echo":
|
| 125 |
-
text = data.get("text")
|
| 126 |
-
if text is None:
|
| 127 |
-
result = {"error": "Missing 'text' parameter"}
|
| 128 |
-
else:
|
| 129 |
-
result = do_echo(text)
|
| 130 |
-
else:
|
| 131 |
-
result = {"error": f"Internal error: unknown function {func_name}"}
|
| 132 |
|
| 133 |
print(json.dumps(result))
|
|
|
|
| 134 |
except Exception as e:
|
|
|
|
| 135 |
print(json.dumps({"error": f"Multitool exception: {e}"}))
|
| 136 |
|
|
|
|
| 137 |
if __name__ == "__main__":
|
| 138 |
-
main()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
PygmyClaw Multitool – Contains the actual tool implementations.
|
| 4 |
+
Works both as:
|
| 5 |
+
1) CLI tool (stdin → stdout JSON)
|
| 6 |
+
2) Python module (import + run_tool())
|
| 7 |
"""
|
| 8 |
+
|
| 9 |
import json
|
| 10 |
import sys
|
| 11 |
import os
|
|
|
|
| 14 |
from pathlib import Path
|
| 15 |
|
| 16 |
SCRIPT_DIR = Path(__file__).parent.resolve()
|
| 17 |
+
|
| 18 |
ERROR_LOG = SCRIPT_DIR / "error_log.json"
|
| 19 |
+
|
| 20 |
MAX_LOG_ENTRIES = 1000
|
| 21 |
|
| 22 |
+
|
| 23 |
# ----------------------------------------------------------------------
|
| 24 |
# Tool definitions
|
| 25 |
+
|
| 26 |
TOOLS = {
|
| 27 |
"list_tools_detailed": {
|
| 28 |
"name": "list_tools_detailed",
|
|
|
|
| 30 |
"parameters": {},
|
| 31 |
"func": "do_list_tools"
|
| 32 |
},
|
| 33 |
+
|
| 34 |
"sys_info": {
|
| 35 |
"name": "sys_info",
|
| 36 |
"description": "Get system information (OS, Python version, etc.).",
|
| 37 |
"parameters": {},
|
| 38 |
"func": "do_sys_info"
|
| 39 |
},
|
| 40 |
+
|
| 41 |
"log_error": {
|
| 42 |
"name": "log_error",
|
| 43 |
"description": "Log an error message to the error log.",
|
|
|
|
| 47 |
},
|
| 48 |
"func": "do_log_error"
|
| 49 |
},
|
| 50 |
+
|
| 51 |
"echo": {
|
| 52 |
"name": "echo",
|
| 53 |
"description": "Echo the input text (for testing).",
|
|
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
+
|
| 60 |
+
# ----------------------------------------------------------------------
|
| 61 |
+
# Tool Implementations
|
| 62 |
+
|
| 63 |
+
|
| 64 |
def do_list_tools():
|
| 65 |
+
"""Return the list of tools with metadata."""
|
| 66 |
+
|
| 67 |
tools_list = []
|
| 68 |
+
|
| 69 |
for name, info in TOOLS.items():
|
| 70 |
+
|
| 71 |
tools_list.append({
|
| 72 |
"name": name,
|
| 73 |
"description": info["description"],
|
| 74 |
"parameters": info["parameters"]
|
| 75 |
})
|
| 76 |
+
|
| 77 |
return {"tools": tools_list}
|
| 78 |
|
| 79 |
+
|
| 80 |
def do_sys_info():
|
| 81 |
"""Return system information."""
|
| 82 |
+
|
| 83 |
return {
|
| 84 |
"os": platform.system(),
|
| 85 |
"os_release": platform.release(),
|
|
|
|
| 87 |
"hostname": platform.node()
|
| 88 |
}
|
| 89 |
|
| 90 |
+
|
| 91 |
def do_log_error(msg, trace=""):
|
| 92 |
+
"""Append an error to the error log."""
|
| 93 |
+
|
| 94 |
entry = {
|
| 95 |
"timestamp": time.time(),
|
| 96 |
"msg": msg,
|
| 97 |
"trace": trace
|
| 98 |
}
|
| 99 |
+
|
| 100 |
try:
|
| 101 |
+
|
| 102 |
if ERROR_LOG.exists():
|
| 103 |
+
|
| 104 |
with open(ERROR_LOG) as f:
|
| 105 |
log = json.load(f)
|
| 106 |
+
|
| 107 |
else:
|
| 108 |
+
|
| 109 |
log = []
|
| 110 |
+
|
| 111 |
log.append(entry)
|
| 112 |
+
|
| 113 |
if len(log) > MAX_LOG_ENTRIES:
|
| 114 |
log = log[-MAX_LOG_ENTRIES:]
|
| 115 |
+
|
| 116 |
+
with open(ERROR_LOG, "w") as f:
|
| 117 |
json.dump(log, f, indent=2)
|
| 118 |
+
|
| 119 |
return {"status": "logged"}
|
| 120 |
+
|
| 121 |
except Exception as e:
|
| 122 |
+
|
| 123 |
return {"error": f"Failed to write log: {e}"}
|
| 124 |
|
| 125 |
+
|
| 126 |
def do_echo(text):
|
| 127 |
+
"""Echo input."""
|
| 128 |
+
|
| 129 |
return {"echo": text}
|
| 130 |
|
| 131 |
+
|
| 132 |
# ----------------------------------------------------------------------
|
| 133 |
+
# INTERNAL TOOL EXECUTOR (used by PygmyClaw agent)
|
| 134 |
+
|
| 135 |
+
def run_tool(action, **params):
|
| 136 |
+
"""
|
| 137 |
+
Run tool programmatically.
|
| 138 |
+
|
| 139 |
+
Example:
|
| 140 |
+
run_tool("sys_info")
|
| 141 |
+
run_tool("echo", text="hello")
|
| 142 |
+
"""
|
| 143 |
+
|
| 144 |
+
tool = TOOLS.get(action)
|
| 145 |
+
|
| 146 |
+
if not tool:
|
| 147 |
+
return {"error": f"Unknown tool '{action}'"}
|
| 148 |
+
|
| 149 |
+
func_name = tool["func"]
|
| 150 |
+
|
| 151 |
+
try:
|
| 152 |
+
|
| 153 |
+
if func_name == "do_list_tools":
|
| 154 |
+
return do_list_tools()
|
| 155 |
+
|
| 156 |
+
elif func_name == "do_sys_info":
|
| 157 |
+
return do_sys_info()
|
| 158 |
+
|
| 159 |
+
elif func_name == "do_log_error":
|
| 160 |
+
return do_log_error(
|
| 161 |
+
params.get("msg"),
|
| 162 |
+
params.get("trace", "")
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
elif func_name == "do_echo":
|
| 166 |
+
return do_echo(params.get("text"))
|
| 167 |
+
|
| 168 |
+
else:
|
| 169 |
+
return {"error": f"Unknown function '{func_name}'"}
|
| 170 |
+
|
| 171 |
+
except Exception as e:
|
| 172 |
+
|
| 173 |
+
return {"error": f"Tool execution failed: {e}"}
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
# ----------------------------------------------------------------------
|
| 177 |
+
# CLI Dispatcher (existing behavior preserved)
|
| 178 |
+
|
| 179 |
def main():
|
| 180 |
+
|
| 181 |
try:
|
| 182 |
+
|
| 183 |
+
raw = sys.stdin.read()
|
| 184 |
+
|
| 185 |
+
if not raw:
|
| 186 |
+
print(json.dumps({"error": "No input"}))
|
| 187 |
+
return
|
| 188 |
+
|
| 189 |
+
data = json.loads(raw)
|
| 190 |
+
|
| 191 |
action = data.get("action")
|
| 192 |
+
|
| 193 |
if not action:
|
| 194 |
+
|
| 195 |
print(json.dumps({"error": "No action specified"}))
|
| 196 |
return
|
| 197 |
|
| 198 |
+
params = {k: v for k, v in data.items() if k != "action"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
result = run_tool(action, **params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
print(json.dumps(result))
|
| 203 |
+
|
| 204 |
except Exception as e:
|
| 205 |
+
|
| 206 |
print(json.dumps({"error": f"Multitool exception: {e}"}))
|
| 207 |
|
| 208 |
+
|
| 209 |
if __name__ == "__main__":
|
| 210 |
+
main()
|