|
| 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. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import os |
| 10 | +import shlex |
| 11 | +import subprocess |
| 12 | +from collections.abc import Iterable |
| 13 | +from pathlib import Path |
| 14 | +from typing import Annotated |
| 15 | + |
| 16 | +from langchain_core.tools import BaseTool, tool |
| 17 | + |
| 18 | +ALLOWED_BINARIES = {"uipath", "uip"} |
| 19 | +MAX_LOG_BYTES = 4000 |
| 20 | + |
| 21 | +# Destructive verbs blocked by default. |
| 22 | +DEFAULT_DENIED_SUBCOMMANDS = frozenset( |
| 23 | + { |
| 24 | + "delete", |
| 25 | + "remove", |
| 26 | + "cancel", |
| 27 | + "uninstall", |
| 28 | + "unassign", |
| 29 | + "unlink", |
| 30 | + "unset", |
| 31 | + "clear", |
| 32 | + "rm", |
| 33 | + "del", |
| 34 | + "destroy", |
| 35 | + "purge", |
| 36 | + "drop", |
| 37 | + "revoke", |
| 38 | + "deactivate", |
| 39 | + "undeploy", |
| 40 | + "unpublish", |
| 41 | + "unregister", |
| 42 | + "unimport", |
| 43 | + "prune", |
| 44 | + "wipe", |
| 45 | + "reset", |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | +__all__ = [ |
| 50 | + "create_uipath_cli_tool", |
| 51 | + "DEFAULT_DENIED_SUBCOMMANDS", |
| 52 | +] |
| 53 | + |
| 54 | + |
| 55 | +def create_uipath_cli_tool( |
| 56 | + workspace: Path | str, |
| 57 | + timeout_sec: int = 120, |
| 58 | + denied_subcommands: Iterable[str] | None = None, |
| 59 | +) -> BaseTool: |
| 60 | + """Build a CLI tool bound to ``workspace``. |
| 61 | +
|
| 62 | + Pass the resulting tool via the ``tools`` argument of the advanced agent |
| 63 | + builders — inject it only for UiPath skills, not customer skills. |
| 64 | +
|
| 65 | + Args: |
| 66 | + workspace: Root for CLI invocations, created if absent. Commands cannot |
| 67 | + escape it; subdirectories are created on demand. |
| 68 | + timeout_sec: Per-command subprocess timeout. Default 120s. |
| 69 | + denied_subcommands: Blocked verbs; a command is rejected when a non-flag |
| 70 | + token equals one (case-insensitive) or starts with ``<verb>-``. |
| 71 | + Defaults to :data:`DEFAULT_DENIED_SUBCOMMANDS`; pass an empty |
| 72 | + iterable to allow everything. |
| 73 | + """ |
| 74 | + workspace_path = Path(workspace).expanduser().resolve() |
| 75 | + workspace_path.mkdir(parents=True, exist_ok=True) |
| 76 | + denied = frozenset( |
| 77 | + s.lower() |
| 78 | + for s in ( |
| 79 | + DEFAULT_DENIED_SUBCOMMANDS |
| 80 | + if denied_subcommands is None |
| 81 | + else denied_subcommands |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + @tool |
| 86 | + def uipath_cli( |
| 87 | + command: Annotated[ |
| 88 | + str, |
| 89 | + "Full CLI command starting with `uipath` or `uip`, e.g. 'uipath pack' or 'uip solution publish dev'.", |
| 90 | + ], |
| 91 | + subdir: Annotated[ |
| 92 | + str, |
| 93 | + "Workspace-relative subdirectory to run in. Give each scaffolded project its own subdir. Defaults to the workspace root.", |
| 94 | + ] = "", |
| 95 | + ) -> str: |
| 96 | + """Run a ``uipath`` / ``uip`` command in a workspace subdirectory. |
| 97 | +
|
| 98 | + Returns combined stdout/stderr. Only ``uipath`` and ``uip`` are allowed. |
| 99 | + Inherits the agent process environment, including ``UIPATH_ACCESS_TOKEN`` |
| 100 | + / ``UIPATH_BASE_URL`` for tenant auth. |
| 101 | + """ |
| 102 | + run_dir = workspace_path |
| 103 | + if subdir: |
| 104 | + candidate = (workspace_path / subdir).resolve() |
| 105 | + if not candidate.is_relative_to(workspace_path): |
| 106 | + return f"ERROR: subdir '{subdir}' escapes the workspace directory." |
| 107 | + candidate.mkdir(parents=True, exist_ok=True) |
| 108 | + run_dir = candidate |
| 109 | + |
| 110 | + try: |
| 111 | + argv = shlex.split(command) |
| 112 | + except ValueError as e: |
| 113 | + return f"ERROR: could not parse command: {e}" |
| 114 | + |
| 115 | + if not argv: |
| 116 | + return "ERROR: empty command" |
| 117 | + |
| 118 | + binary = os.path.basename(argv[0]) |
| 119 | + if binary not in ALLOWED_BINARIES: |
| 120 | + return ( |
| 121 | + f"ERROR: '{binary}' is not allowed. Only these binaries are " |
| 122 | + f"permitted: {sorted(ALLOWED_BINARIES)}" |
| 123 | + ) |
| 124 | + |
| 125 | + def is_denied(tok: str) -> bool: |
| 126 | + low = tok.lower() |
| 127 | + return any(low == d or low.startswith(f"{d}-") for d in denied) |
| 128 | + |
| 129 | + blocked = next( |
| 130 | + (tok for tok in argv[1:] if not tok.startswith("-") and is_denied(tok)), |
| 131 | + None, |
| 132 | + ) |
| 133 | + if blocked is not None: |
| 134 | + return ( |
| 135 | + f"ERROR: subcommand '{blocked}' is not allowed. These destructive " |
| 136 | + f"subcommands are blocked: {sorted(denied)}" |
| 137 | + ) |
| 138 | + |
| 139 | + try: |
| 140 | + proc = subprocess.run( |
| 141 | + argv, |
| 142 | + cwd=str(run_dir), |
| 143 | + capture_output=True, |
| 144 | + text=True, |
| 145 | + timeout=timeout_sec, |
| 146 | + check=False, |
| 147 | + ) |
| 148 | + except subprocess.TimeoutExpired: |
| 149 | + return f"ERROR: command timed out after {timeout_sec}s" |
| 150 | + except FileNotFoundError: |
| 151 | + return f"ERROR: '{binary}' not found on PATH. Install the UiPath CLI." |
| 152 | + |
| 153 | + return ( |
| 154 | + f"exit_code: {proc.returncode}\n" |
| 155 | + f"--- stdout ---\n{proc.stdout[:MAX_LOG_BYTES]}\n" |
| 156 | + f"--- stderr ---\n{proc.stderr[:MAX_LOG_BYTES]}" |
| 157 | + ) |
| 158 | + |
| 159 | + # Classify as "internal" so traces label it (untyped default is "Integration"). |
| 160 | + uipath_cli.metadata = {"tool_type": "internal", "display_name": "uipath_cli"} |
| 161 | + return uipath_cli |
0 commit comments