Files
AstrBot/packages/astrbot_agent/tools/shell.py
Soulter 185501d1b5 stage
2025-10-02 14:06:23 +08:00

49 lines
1.6 KiB
Python

import json
from astrbot.api import FunctionTool
from astrbot.api.event import AstrMessageEvent
from dataclasses import dataclass, field
from ..sandbox_client import SandboxClient
@dataclass
class ExecuteShellTool(FunctionTool):
name: str = "astrbot_execute_shell"
description: str = "Execute a command in the shell."
parameters: dict = field(
default_factory=lambda: {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "The shell command to execute.",
},
"background": {
"type": "boolean",
"description": "Whether to run the command in the background.",
"default": False,
},
"env": {
"type": "object",
"description": "Optional environment variables to set for the file creation process.",
"additionalProperties": {"type": "string"},
"default": {},
},
},
"required": ["command"],
}
)
async def run(
self,
event: AstrMessageEvent,
command: str,
background: bool = False,
env: dict = {},
):
sb = await SandboxClient().get_ship(event.unified_msg_origin)
try:
result = await sb.shell.exec(command, background=background, env=env)
return json.dumps(result)
except Exception as e:
return f"Error executing command: {str(e)}"