Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions astrbot/core/tools/computer_tools/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"description": "Whether to suppress the output of the code execution.",
"default": False,
},
"timeout": {
"type": "integer",
"description": "Optional timeout in seconds for code execution.",
"default": 30,
},
},
"required": ["code"],
}
Expand Down Expand Up @@ -77,16 +82,29 @@ class PythonTool(FunctionTool):
parameters: dict = field(default_factory=lambda: param_schema)

async def call(
self, context: ContextWrapper[AstrAgentContext], code: str, silent: bool = False
self,
context: ContextWrapper[AstrAgentContext],
code: str,
silent: bool = False,
timeout: int = 30,
) -> ToolExecResult:
if permission_error := check_admin_permission(context, "Python execution"):
return permission_error
sb = await get_booter(
context.context.context,
context.context.event.unified_msg_origin,
)
effective_timeout = (
min(timeout, context.tool_call_timeout)
if timeout > 0
else context.tool_call_timeout
)
try:
result = await sb.python.exec(code, silent=silent)
result = await sb.python.exec(
code,
timeout=effective_timeout,
silent=silent,
)
return await handle_result(result, context.context.event)
except Exception as e:
return f"Error executing code: {str(e)}"
Expand All @@ -104,13 +122,26 @@ class LocalPythonTool(FunctionTool):
parameters: dict = field(default_factory=lambda: param_schema)

async def call(
self, context: ContextWrapper[AstrAgentContext], code: str, silent: bool = False
self,
context: ContextWrapper[AstrAgentContext],
code: str,
silent: bool = False,
timeout: int = 30,
) -> ToolExecResult:
if permission_error := check_admin_permission(context, "Python execution"):
return permission_error
sb = get_local_booter()
effective_timeout = (
min(timeout, context.tool_call_timeout)
if timeout > 0
else context.tool_call_timeout
)
Comment thread
Fronut marked this conversation as resolved.
try:
result = await sb.python.exec(code, silent=silent)
result = await sb.python.exec(
code,
timeout=effective_timeout,
silent=silent,
)
return await handle_result(result, context.context.event)
except Exception as e:
return f"Error executing code: {str(e)}"
Loading