from plato.sims.ubuntu_vm.models import (
Action,
BashRequest,
Command,
ComputerRequest,
EditRequest,
)
def dispatch_tool(env, tool_name: str, tool_input: dict) -> dict:
"""Map a model's tool call to a Plato VM call.
Returns a provider-neutral dict: {"type": "image"|"text", ...}.
Re-wrap it for whatever tool_result shape your model SDK expects.
"""
if tool_name == "computer":
req = ComputerRequest(
action=Action(tool_input.get("action", "screenshot")),
coordinate=tool_input.get("coordinate"),
text=tool_input.get("text"),
scroll_direction=tool_input.get("scroll_direction"),
scroll_amount=tool_input.get("scroll_amount"),
duration=tool_input.get("duration"),
)
result = env.sdk.computer(req)
if result.base64_image:
return {
"type": "image",
"media_type": "image/png",
"data": result.base64_image,
}
return {"type": "text", "text": result.output or result.error or "OK"}
if tool_name == "bash":
result = env.sdk.bash(BashRequest(
command=tool_input["command"],
restart=tool_input.get("restart", False),
timeout=tool_input.get("timeout", 120),
))
return {"type": "text", "text": (result.output or "") + (result.error or "")}
if tool_name == "edit":
result = env.sdk.edit(EditRequest(
command=Command(tool_input["command"]),
path=tool_input["path"],
file_text=tool_input.get("file_text"),
old_str=tool_input.get("old_str"),
new_str=tool_input.get("new_str"),
insert_line=tool_input.get("insert_line"),
view_range=tool_input.get("view_range"),
))
return {"type": "text", "text": result.output or result.error or "OK"}
raise ValueError(f"unknown tool: {tool_name}")