|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Tools for the Cube-backed skill agent.""" |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +from trpc_agent_sdk.code_executors import WorkspaceInputSpec |
| 12 | +from trpc_agent_sdk.code_executors.cube import CubeClientConfig |
| 13 | +from trpc_agent_sdk.code_executors.cube import CubeWorkspaceRuntime |
| 14 | +from trpc_agent_sdk.code_executors.cube import CubeWorkspaceRuntimeConfig |
| 15 | +from trpc_agent_sdk.code_executors.cube import create_cube_sandbox_client |
| 16 | +from trpc_agent_sdk.code_executors.cube import create_cube_workspace_runtime |
| 17 | +from trpc_agent_sdk.skills import ENV_SKILLS_ROOT |
| 18 | +from trpc_agent_sdk.skills import SkillToolSet |
| 19 | +from trpc_agent_sdk.skills import create_default_skill_repository |
| 20 | + |
| 21 | + |
| 22 | +def _get_skill_paths() -> str: |
| 23 | + """Get the skill paths.""" |
| 24 | + skills_root = os.getenv(ENV_SKILLS_ROOT) |
| 25 | + if skills_root: |
| 26 | + return skills_root |
| 27 | + current_path = Path(__file__).parent |
| 28 | + path = str(current_path.parent / "skills") |
| 29 | + # convert to file URL |
| 30 | + # path = "file://" + path |
| 31 | + # "http://{host}:{port}/{path}/{filename}.{extension}" |
| 32 | + # path = "http://localhost:8000/skills/skills.tar.gz" |
| 33 | + return path |
| 34 | + |
| 35 | + |
| 36 | +def _cube_client_config() -> CubeClientConfig: |
| 37 | + """Build Cube executor config from environment variables.""" |
| 38 | + return CubeClientConfig( |
| 39 | + execute_timeout=float(os.getenv("CUBE_EXECUTE_TIMEOUT", "30")), |
| 40 | + idle_timeout=int(os.getenv("CUBE_IDLE_TIMEOUT", "600")), |
| 41 | + auto_recover=True, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +async def create_skill_tool_set() -> tuple[SkillToolSet, Any, CubeWorkspaceRuntime]: |
| 46 | + """Create a Cube-backed skill tool set and its Cube runtime.""" |
| 47 | + tool_kwargs = { |
| 48 | + "save_as_artifacts": True, |
| 49 | + "omit_inline_content": False, |
| 50 | + } |
| 51 | + |
| 52 | + cfg = _cube_client_config() |
| 53 | + sandbox_client = await create_cube_sandbox_client(cfg) |
| 54 | + workspace_runtime = create_cube_workspace_runtime( |
| 55 | + sandbox_client=sandbox_client, |
| 56 | + execute_timeout=cfg.execute_timeout, |
| 57 | + workspace_cfg=CubeWorkspaceRuntimeConfig(), |
| 58 | + ) |
| 59 | + print(f"[skills_with_cube] using Cube sandbox: {workspace_runtime.sandbox_id}", flush=True) |
| 60 | + skill_paths = _get_skill_paths() |
| 61 | + repository = create_default_skill_repository(skill_paths, workspace_runtime=workspace_runtime) |
| 62 | + toolset = SkillToolSet(repository=repository, run_tool_kwargs=tool_kwargs) |
| 63 | + return toolset, repository, workspace_runtime |
| 64 | + |
| 65 | + |
| 66 | +def build_cube_stage_inputs_specs(inputs_host: str = "/tmp/skillrun-inputs") -> list[WorkspaceInputSpec]: |
| 67 | + """Build example input specs for Cube runtime. |
| 68 | +
|
| 69 | + The returned specs demonstrate the supported input schemes used by |
| 70 | + ``CubeWorkspaceFS.stage_inputs``: |
| 71 | +
|
| 72 | + - ``host://`` : upload from a host path into the remote Cube sandbox |
| 73 | + - ``workspace://``: reuse a file already present in current workspace |
| 74 | + - ``skill://`` : reference a file under workspace ``skills/`` |
| 75 | + """ |
| 76 | + return [ |
| 77 | + WorkspaceInputSpec( |
| 78 | + src=f"host://{inputs_host}/sales.csv", |
| 79 | + dst="work/inputs/sales.csv", |
| 80 | + mode="link", |
| 81 | + ), |
| 82 | + WorkspaceInputSpec( |
| 83 | + # This file exists after skill staging, so the workspace:// demo is stable. |
| 84 | + src="workspace://skills/python-math/SKILL.md", |
| 85 | + dst="work/staged_inputs/python-math_skill.md", |
| 86 | + mode="copy", |
| 87 | + ), |
| 88 | + WorkspaceInputSpec( |
| 89 | + src="skill://python-math/scripts/fib.py", |
| 90 | + dst="work/staged_inputs/fib.py", |
| 91 | + mode="copy", |
| 92 | + ), |
| 93 | + ] |
| 94 | + |
| 95 | + |
| 96 | +def build_cube_skill_run_payload(skill_name: str = "python-math", |
| 97 | + inputs_host: str = "/tmp/skillrun-inputs") -> dict[str, Any]: |
| 98 | + """Build a full ``skill_run`` payload for Cube mode demonstration. |
| 99 | +
|
| 100 | + This payload can be used directly when invoking the ``skill_run`` tool: |
| 101 | + it stages input schemes into the remote Cube workspace and writes outputs |
| 102 | + under ``out/``. |
| 103 | + """ |
| 104 | + return { |
| 105 | + "skill": |
| 106 | + skill_name, |
| 107 | + "cwd": |
| 108 | + f"$SKILLS_DIR/{skill_name}", |
| 109 | + "command": ("python scripts/fib.py 10 > out/fib.txt && " |
| 110 | + "(ls -R work/inputs; echo '---'; ls -R work/staged_inputs) > out/staged_inputs_tree.txt"), |
| 111 | + "inputs": [spec.model_dump() for spec in build_cube_stage_inputs_specs(inputs_host=inputs_host)], |
| 112 | + "output_files": [ |
| 113 | + "out/fib.txt", |
| 114 | + "out/staged_inputs_tree.txt", |
| 115 | + ], |
| 116 | + } |
0 commit comments