|
| 1 | +"""Workspace CLI tool injected when skills are active. |
| 2 | +
|
| 3 | +``uipath_cli`` runs ``uipath`` / ``uip`` commands a skill prescribes, jailed to |
| 4 | +the agent's workspace. It is runtime-injected (bound to a workspace path) rather |
| 5 | +than declared in ``agent.json``, so it is not registered in |
| 6 | +``internal_tool_factory``; see the note there for the user-selectable path. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import os |
| 12 | +import shlex |
| 13 | +import subprocess |
| 14 | +from collections.abc import Iterable |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from langchain_core.tools import BaseTool |
| 18 | +from pydantic import BaseModel, Field |
| 19 | +from uipath.runtime import Workspace |
| 20 | + |
| 21 | +from uipath_langchain.agent.tools.base_uipath_structured_tool import ( |
| 22 | + BaseUiPathStructuredTool, |
| 23 | +) |
| 24 | + |
| 25 | +ALLOWED_BINARIES = {"uipath", "uip"} |
| 26 | +MAX_LOG_BYTES = 4000 |
| 27 | + |
| 28 | +# Classify as "internal" so traces label it (untyped default is "Integration"). |
| 29 | +UIPATH_CLI_TOOL_NAME = "uipath_cli" |
| 30 | +UIPATH_CLI_TOOL_TYPE = "internal" |
| 31 | + |
| 32 | +# Destructive verbs blocked by default. |
| 33 | +DEFAULT_DENIED_SUBCOMMANDS = frozenset( |
| 34 | + { |
| 35 | + "delete", |
| 36 | + "remove", |
| 37 | + "cancel", |
| 38 | + "uninstall", |
| 39 | + "unassign", |
| 40 | + "unlink", |
| 41 | + "unset", |
| 42 | + "clear", |
| 43 | + "rm", |
| 44 | + "del", |
| 45 | + "destroy", |
| 46 | + "purge", |
| 47 | + "drop", |
| 48 | + "revoke", |
| 49 | + "deactivate", |
| 50 | + "undeploy", |
| 51 | + "unpublish", |
| 52 | + "unregister", |
| 53 | + "unimport", |
| 54 | + "prune", |
| 55 | + "wipe", |
| 56 | + "reset", |
| 57 | + } |
| 58 | +) |
| 59 | + |
| 60 | +__all__ = [ |
| 61 | + "create_uipath_cli_tool", |
| 62 | + "DEFAULT_DENIED_SUBCOMMANDS", |
| 63 | + "UIPATH_CLI_TOOL_NAME", |
| 64 | + "UIPATH_CLI_TOOL_TYPE", |
| 65 | +] |
| 66 | + |
| 67 | +_TOOL_DESCRIPTION = ( |
| 68 | + "Run a `uipath` / `uip` command in a workspace subdirectory. Returns combined " |
| 69 | + "stdout/stderr. Only `uipath` and `uip` are allowed. Inherits the agent process " |
| 70 | + "environment, including `UIPATH_ACCESS_TOKEN` / `UIPATH_BASE_URL` for tenant auth." |
| 71 | +) |
| 72 | + |
| 73 | + |
| 74 | +class UiPathCliArgs(BaseModel): |
| 75 | + """Arguments for ``uipath_cli``.""" |
| 76 | + |
| 77 | + command: str = Field( |
| 78 | + description=( |
| 79 | + "Full CLI command starting with `uipath` or `uip`, e.g. 'uipath pack' " |
| 80 | + "or 'uip solution publish dev'." |
| 81 | + ), |
| 82 | + ) |
| 83 | + subdir: str = Field( |
| 84 | + default="", |
| 85 | + description=( |
| 86 | + "Workspace-relative subdirectory to run in. Give each scaffolded " |
| 87 | + "project its own subdir. Defaults to the workspace root." |
| 88 | + ), |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def create_uipath_cli_tool( |
| 93 | + workspace: Workspace, |
| 94 | + timeout_sec: int = 120, |
| 95 | + denied_subcommands: Iterable[str] | None = None, |
| 96 | +) -> BaseTool: |
| 97 | + """Build a ``uipath_cli`` tool jailed to ``workspace``. |
| 98 | +
|
| 99 | + Pass it via the ``tools`` argument of the advanced agent builders — inject it |
| 100 | + only for UiPath skills, not customer skills. ``denied_subcommands`` defaults to |
| 101 | + :data:`DEFAULT_DENIED_SUBCOMMANDS`; pass an empty iterable to allow everything. |
| 102 | +
|
| 103 | + The tool only reads ``workspace.path``; the workspace lifecycle (creation, |
| 104 | + cleanup) stays owned by the runtime. |
| 105 | + """ |
| 106 | + workspace_path = Path(workspace.path).expanduser().resolve() |
| 107 | + workspace_path.mkdir(parents=True, exist_ok=True) |
| 108 | + denied = frozenset( |
| 109 | + s.lower() |
| 110 | + for s in ( |
| 111 | + DEFAULT_DENIED_SUBCOMMANDS |
| 112 | + if denied_subcommands is None |
| 113 | + else denied_subcommands |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + def run_uipath_cli(command: str, subdir: str = "") -> str: |
| 118 | + run_dir = workspace_path |
| 119 | + if subdir: |
| 120 | + candidate = (workspace_path / subdir).resolve() |
| 121 | + if not candidate.is_relative_to(workspace_path): |
| 122 | + return f"ERROR: subdir '{subdir}' escapes the workspace directory." |
| 123 | + candidate.mkdir(parents=True, exist_ok=True) |
| 124 | + run_dir = candidate |
| 125 | + |
| 126 | + try: |
| 127 | + argv = shlex.split(command) |
| 128 | + except ValueError as e: |
| 129 | + return f"ERROR: could not parse command: {e}" |
| 130 | + |
| 131 | + if not argv: |
| 132 | + return "ERROR: empty command" |
| 133 | + |
| 134 | + binary = argv[0] |
| 135 | + # Reject any path-qualified binary (e.g. ./uip, /tmp/uip) so only the |
| 136 | + # UiPath CLI resolved from PATH runs, not a lookalike in the workspace. |
| 137 | + if os.path.basename(binary) != binary or binary not in ALLOWED_BINARIES: |
| 138 | + return ( |
| 139 | + f"ERROR: '{binary}' is not allowed. Only these binaries are " |
| 140 | + f"permitted (by name, without a path): {sorted(ALLOWED_BINARIES)}" |
| 141 | + ) |
| 142 | + |
| 143 | + def is_denied(tok: str) -> bool: |
| 144 | + low = tok.lower() |
| 145 | + return any(low == d or low.startswith(f"{d}-") for d in denied) |
| 146 | + |
| 147 | + blocked = next( |
| 148 | + (tok for tok in argv[1:] if not tok.startswith("-") and is_denied(tok)), |
| 149 | + None, |
| 150 | + ) |
| 151 | + if blocked is not None: |
| 152 | + return ( |
| 153 | + f"ERROR: subcommand '{blocked}' is not allowed. These destructive " |
| 154 | + f"subcommands are blocked: {sorted(denied)}" |
| 155 | + ) |
| 156 | + |
| 157 | + try: |
| 158 | + proc = subprocess.run( |
| 159 | + argv, |
| 160 | + cwd=str(run_dir), |
| 161 | + capture_output=True, |
| 162 | + text=True, |
| 163 | + timeout=timeout_sec, |
| 164 | + check=False, |
| 165 | + ) |
| 166 | + except subprocess.TimeoutExpired: |
| 167 | + return f"ERROR: command timed out after {timeout_sec}s" |
| 168 | + except FileNotFoundError: |
| 169 | + return f"ERROR: '{binary}' not found on PATH. Install the UiPath CLI." |
| 170 | + |
| 171 | + return ( |
| 172 | + f"exit_code: {proc.returncode}\n" |
| 173 | + f"--- stdout ---\n{proc.stdout[:MAX_LOG_BYTES]}\n" |
| 174 | + f"--- stderr ---\n{proc.stderr[:MAX_LOG_BYTES]}" |
| 175 | + ) |
| 176 | + |
| 177 | + return BaseUiPathStructuredTool( |
| 178 | + name=UIPATH_CLI_TOOL_NAME, |
| 179 | + description=_TOOL_DESCRIPTION, |
| 180 | + args_schema=UiPathCliArgs, |
| 181 | + func=run_uipath_cli, |
| 182 | + # Declarative metadata mirrors the sibling internal tools; classify as |
| 183 | + # "internal" so traces label it (untyped default is "Integration"). |
| 184 | + metadata={ |
| 185 | + "tool_type": UIPATH_CLI_TOOL_TYPE, |
| 186 | + "display_name": UIPATH_CLI_TOOL_NAME, |
| 187 | + "args_schema": UiPathCliArgs, |
| 188 | + }, |
| 189 | + ) |
0 commit comments