|
| 1 | +"""Action contracts for bundled tools, shared by policy and discovery.""" |
| 2 | + |
| 3 | +from dataclasses import dataclass, field |
| 4 | +from typing import Any |
| 5 | + |
| 6 | + |
| 7 | +MEMORY_TOOL_ID = "plugin:_memory:memory" |
| 8 | +MEMORY_ACTIONS = ("load", "save", "delete", "forget") |
| 9 | + |
| 10 | + |
| 11 | +@dataclass(frozen=True) |
| 12 | +class ToolActions: |
| 13 | + actions: tuple[str, ...] |
| 14 | + argument: str = "action" |
| 15 | + default: str = "" |
| 16 | + aliases: dict[str, str] = field(default_factory=dict) |
| 17 | + |
| 18 | + def normalize(self, value: Any) -> str: |
| 19 | + action = str(value or "").strip().lower().replace("-", "_") |
| 20 | + return self.aliases.get(action, action) |
| 21 | + |
| 22 | + |
| 23 | +_EDITOR = ToolActions(("read", "write", "patch")) |
| 24 | +_CODE = ToolActions(("terminal", "python", "nodejs", "output", "reset"), argument="runtime") |
| 25 | +BUNDLED_ACTIONS = { |
| 26 | + MEMORY_TOOL_ID: ToolActions(MEMORY_ACTIONS), |
| 27 | + "plugin:_scheduler:scheduler": ToolActions(( |
| 28 | + "list_tasks", "find_task_by_name", "show_task", "run_task", "delete_task", |
| 29 | + "update_task", "create_scheduled_task", "create_adhoc_task", |
| 30 | + "create_planned_task", "wait_for_task", |
| 31 | + )), |
| 32 | + "plugin:_text_editor:text_editor": _EDITOR, |
| 33 | + "plugin:_a0_connector:text_editor_remote": _EDITOR, |
| 34 | + "plugin:_code_execution:code_execution_tool": _CODE, |
| 35 | + "plugin:_a0_connector:code_execution_remote": _CODE, |
| 36 | + "plugin:_browser:browser": ToolActions(( |
| 37 | + "open", "list", "state", "set_active", "navigate", "back", "forward", "reload", |
| 38 | + "content", "detail", "screenshot", "click", "type", "submit", "type_submit", |
| 39 | + "scroll", "evaluate", "key_chord", "hover", "double_click", "right_click", |
| 40 | + "drag", "wheel", "keyboard", "clipboard", "set_viewport", "select_option", |
| 41 | + "set_checked", "upload_file", "mouse", "multi", "close", "close_all", |
| 42 | + ), default="state", aliases={ |
| 43 | + "setactive": "set_active", "activate": "set_active", "focus": "set_active", |
| 44 | + "typesubmit": "type_submit", "keychord": "key_chord", |
| 45 | + "copy": "clipboard", "cut": "clipboard", "paste": "clipboard", |
| 46 | + }), |
| 47 | + "plugin:_a0_connector:computer_use_remote": ToolActions(( |
| 48 | + "start_session", "status", "capture", "list_windows", "get_window_state", |
| 49 | + "element_action", "ax_snapshot", "ax_action", "uia_snapshot", "uia_action", |
| 50 | + "move", "click", "scroll", "key", "type", "stop_session", |
| 51 | + )), |
| 52 | + "plugin:_office:office_artifact": ToolActions(( |
| 53 | + "create", "open", "read", "edit", "inspect", "version_history", |
| 54 | + "restore_version", "export", "status", |
| 55 | + ), default="status", aliases={"extract": "read", "update": "edit", "patch": "edit"}), |
| 56 | + "plugin:_goal:goal": ToolActions( |
| 57 | + ("get", "create", "update"), default="get", |
| 58 | + aliases={"show": "get", "status": "get", "set": "create", "complete": "update", "blocked": "update"}, |
| 59 | + ), |
| 60 | + "local:skills_tool": ToolActions(("list", "search", "load", "read_file"), default="list"), |
| 61 | + "local:parallel": ToolActions( |
| 62 | + ("start", "await", "collect", "cancel"), |
| 63 | + aliases={"background": "start", "wait": "await"}, |
| 64 | + ), |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +def bundled_path(tool_id: str) -> str: |
| 69 | + parts = tool_id.split(":") |
| 70 | + return f"plugins/{parts[1]}/tools/{parts[2]}.py" if parts[0] == "plugin" else f"tools/{parts[1]}.py" |
| 71 | + |
| 72 | + |
| 73 | +def action_policy_id(tool_id: str, action: str) -> str: |
| 74 | + # The four former memory tools already have durable permission IDs. |
| 75 | + separator = "_" if tool_id == MEMORY_TOOL_ID else ":" |
| 76 | + return f"{tool_id}{separator}{action}" |
| 77 | + |
| 78 | + |
| 79 | +def requested_actions(tool_id: str, args: dict[str, Any]) -> list[str]: |
| 80 | + """Include compound operations, so wrappers cannot hide a denied action.""" |
| 81 | + spec = BUNDLED_ACTIONS[tool_id] |
| 82 | + value = args.get(spec.argument) |
| 83 | + if tool_id == "local:skills_tool": |
| 84 | + value = value or args.get("method") |
| 85 | + action = spec.normalize(value or spec.default) |
| 86 | + result = [action] |
| 87 | + |
| 88 | + if tool_id == "plugin:_browser:browser" and action == "multi": |
| 89 | + calls = args.get("calls") |
| 90 | + if isinstance(calls, list): |
| 91 | + for call in calls: |
| 92 | + if isinstance(call, dict): |
| 93 | + result.extend(requested_actions(tool_id, call)) |
| 94 | + elif spec.argument == "runtime": |
| 95 | + from helpers.tool import coerce_bool |
| 96 | + |
| 97 | + if coerce_bool(args.get("reset"), False): |
| 98 | + result.append("reset") |
| 99 | + elif tool_id == "local:parallel" and action != "cancel": |
| 100 | + from helpers.parallel_tools import extract_tool_calls |
| 101 | + from helpers.tool import coerce_bool |
| 102 | + |
| 103 | + result = [] |
| 104 | + if extract_tool_calls(args) is not None: |
| 105 | + result.append("start") |
| 106 | + wait = coerce_bool(args.get("wait"), action not in {"start", "collect"}) |
| 107 | + if action == "await" or wait: |
| 108 | + result.append("await") |
| 109 | + elif args.get("job_ids"): |
| 110 | + result.append("collect") |
| 111 | + if not result: |
| 112 | + result.append(action) |
| 113 | + return list(dict.fromkeys(result)) |
0 commit comments