|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +import os |
| 17 | +from typing import Optional, List |
| 18 | + |
| 19 | +from google.adk.tools import ToolContext |
| 20 | + |
| 21 | +from veadk.config import getenv |
| 22 | +from veadk.utils.logger import get_logger |
| 23 | +from veadk.utils.volcengine_sign import ve_request |
| 24 | +from veadk.auth.veauth.utils import get_credential_from_vefaas_iam |
| 25 | + |
| 26 | +logger = get_logger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def execute_skills( |
| 30 | + workflow_prompt: str, |
| 31 | + skills: Optional[List[str]] = None, |
| 32 | + tool_context: ToolContext = None, |
| 33 | + timeout: int = 300, |
| 34 | +) -> str: |
| 35 | + """execute skills in a code sandbox and return the output. |
| 36 | + For C++ code, don't execute it directly, compile and execute via Python; write sources and object files to /tmp. |
| 37 | +
|
| 38 | + Args: |
| 39 | + workflow_prompt (str): instruction of workflow |
| 40 | + skills (Optional[List[str]]): The skills will be invoked |
| 41 | + timeout (int, optional): The timeout in seconds for the code execution, less than or equal to 300. Defaults to 300. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + str: The output of the code execution. |
| 45 | + """ |
| 46 | + |
| 47 | + tool_id = getenv("AGENTKIT_TOOL_ID") |
| 48 | + |
| 49 | + service = getenv( |
| 50 | + "AGENTKIT_TOOL_SERVICE_CODE", "agentkit" |
| 51 | + ) # temporary service for code run tool |
| 52 | + region = getenv("AGENTKIT_TOOL_REGION", "cn-beijing") |
| 53 | + host = getenv( |
| 54 | + "AGENTKIT_TOOL_HOST", service + "." + region + ".volces.com" |
| 55 | + ) # temporary host for code run tool |
| 56 | + logger.debug(f"tools endpoint: {host}") |
| 57 | + |
| 58 | + session_id = tool_context._invocation_context.session.id |
| 59 | + agent_name = tool_context._invocation_context.agent.name |
| 60 | + user_id = tool_context._invocation_context.user_id |
| 61 | + tool_user_session_id = agent_name + "_" + user_id + "_" + session_id |
| 62 | + logger.debug(f"tool_user_session_id: {tool_user_session_id}") |
| 63 | + |
| 64 | + logger.debug( |
| 65 | + f"Execute skills in session_id={session_id}, tool_id={tool_id}, host={host}, service={service}, region={region}, timeout={timeout}" |
| 66 | + ) |
| 67 | + |
| 68 | + ak = getenv("VOLCENGINE_ACCESS_KEY") |
| 69 | + sk = getenv("VOLCENGINE_SECRET_KEY") |
| 70 | + header = {} |
| 71 | + |
| 72 | + if not (ak and sk): |
| 73 | + logger.debug("Get AK/SK from tool context failed.") |
| 74 | + ak = os.getenv("VOLCENGINE_ACCESS_KEY") |
| 75 | + sk = os.getenv("VOLCENGINE_SECRET_KEY") |
| 76 | + if not (ak and sk): |
| 77 | + logger.debug( |
| 78 | + "Get AK/SK from environment variables failed. Try to use credential from Iam." |
| 79 | + ) |
| 80 | + credential = get_credential_from_vefaas_iam() |
| 81 | + ak = credential.access_key_id |
| 82 | + sk = credential.secret_access_key |
| 83 | + header = {"X-Security-Token": credential.session_token} |
| 84 | + else: |
| 85 | + logger.debug("Successfully get AK/SK from environment variables.") |
| 86 | + else: |
| 87 | + logger.debug("Successfully get AK/SK from tool context.") |
| 88 | + |
| 89 | + cmd = ["python", "agent.py", workflow_prompt] |
| 90 | + if skills: |
| 91 | + cmd.extend(["--skills"] + skills) |
| 92 | + |
| 93 | + # TODO: remove after agentkit supports custom environment variables setting |
| 94 | + env_vars = { |
| 95 | + "MODEL_AGENT_API_KEY": os.getenv("MODEL_AGENT_API_KEY"), |
| 96 | + "TOS_SKILLS_DIR": os.getenv("TOS_SKILLS_DIR"), |
| 97 | + } |
| 98 | + |
| 99 | + code = f""" |
| 100 | +import subprocess |
| 101 | +import os |
| 102 | +
|
| 103 | +env = os.environ.copy() |
| 104 | +for key, value in {env_vars!r}.items(): |
| 105 | + if key not in env: |
| 106 | + env[key] = value |
| 107 | +
|
| 108 | +result = subprocess.run( |
| 109 | + {cmd!r}, |
| 110 | + cwd='/home/gem/veadk_skills', |
| 111 | + capture_output=True, |
| 112 | + text=True, |
| 113 | + env=env, |
| 114 | + timeout={timeout - 10}, |
| 115 | +) |
| 116 | +print(result.stdout) |
| 117 | +if result.stderr: |
| 118 | + print(result.stderr) |
| 119 | + """ |
| 120 | + |
| 121 | + res = ve_request( |
| 122 | + request_body={ |
| 123 | + "ToolId": tool_id, |
| 124 | + "UserSessionId": tool_user_session_id, |
| 125 | + "OperationType": "RunCode", |
| 126 | + "OperationPayload": json.dumps( |
| 127 | + { |
| 128 | + "code": code, |
| 129 | + "timeout": timeout, |
| 130 | + "kernel_name": "python3", |
| 131 | + } |
| 132 | + ), |
| 133 | + }, |
| 134 | + action="InvokeTool", |
| 135 | + ak=ak, |
| 136 | + sk=sk, |
| 137 | + service=service, |
| 138 | + version="2025-10-30", |
| 139 | + region=region, |
| 140 | + host=host, |
| 141 | + header=header, |
| 142 | + ) |
| 143 | + logger.debug(f"Invoke run code response: {res}") |
| 144 | + |
| 145 | + try: |
| 146 | + return res["Result"]["Result"] |
| 147 | + except KeyError as e: |
| 148 | + logger.error(f"Error occurred while running code: {e}, response is {res}") |
| 149 | + return res |
0 commit comments