|
| 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +import json |
| 18 | +import os |
| 19 | +import zipfile |
| 20 | +from pathlib import Path |
| 21 | +from datetime import datetime |
| 22 | + |
| 23 | +from google.adk.tools import ToolContext |
| 24 | + |
| 25 | +from veadk.tools.skills_tools.session_path import get_session_path |
| 26 | +from veadk.integrations.ve_tos.ve_tos import VeTOS |
| 27 | +from veadk.utils.volcengine_sign import ve_request |
| 28 | +from veadk.utils.logger import get_logger |
| 29 | + |
| 30 | +logger = get_logger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +def register_skills_tool( |
| 34 | + skill_name: str, |
| 35 | + skill_description: str, |
| 36 | + skill_local_path: str, |
| 37 | + tool_context: ToolContext, |
| 38 | +) -> str: |
| 39 | + """Register a skill to the remote skill space by uploading its zip package to TOS and calling the CreateSkill API. |
| 40 | +
|
| 41 | + Args: |
| 42 | + skill_name (str): The name of the skill. |
| 43 | + skill_description (str): The description of the skill. |
| 44 | + skill_local_path (str): The local path of the skill directory. |
| 45 | + - The format of the skill directory is as follows: |
| 46 | + skill_local_path/ |
| 47 | + SKILL.md |
| 48 | + other files... |
| 49 | + tool_context (ToolContext): The context of the tool execution. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + str: Result message indicating success or failure. |
| 53 | + """ |
| 54 | + working_dir = get_session_path(session_id=tool_context.session.id) |
| 55 | + |
| 56 | + # skill_path = Path(skill_local_path) |
| 57 | + raw = Path(skill_local_path).expanduser() |
| 58 | + if not raw.is_absolute(): |
| 59 | + skill_path = (working_dir / raw).resolve() |
| 60 | + else: |
| 61 | + skill_path = raw.resolve() |
| 62 | + if not skill_path.exists() or not skill_path.is_dir(): |
| 63 | + logger.error(f"Skill path '{skill_path}' does not exist or is not a directory.") |
| 64 | + return f"Skill path '{skill_path}' does not exist or is not a directory." |
| 65 | + |
| 66 | + skill_readme = skill_path / "SKILL.md" |
| 67 | + if not skill_readme.exists(): |
| 68 | + logger.error(f"Skill path '{skill_path}' has no SKILL.md file.") |
| 69 | + return f"Skill path '{skill_path}' has no SKILL.md file." |
| 70 | + |
| 71 | + zip_file_path = working_dir / "outputs" / f"{skill_name}.zip" |
| 72 | + |
| 73 | + with zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) as zipf: |
| 74 | + for root, dirs, files in os.walk(skill_path): |
| 75 | + for file in files: |
| 76 | + file_path = Path(root) / file |
| 77 | + arcname = Path(skill_name) / file_path.relative_to(skill_path) |
| 78 | + zipf.write(file_path, arcname) |
| 79 | + |
| 80 | + try: |
| 81 | + from veadk.auth.veauth.utils import get_credential_from_vefaas_iam |
| 82 | + |
| 83 | + service = os.getenv("AGENTKIT_TOOL_SERVICE_CODE", "agentkit") |
| 84 | + region = os.getenv("AGENTKIT_TOOL_REGION", "cn-beijing") |
| 85 | + host = os.getenv("AGENTKIT_SKILL_HOST", "open.volcengineapi.com") |
| 86 | + |
| 87 | + access_key = os.getenv("VOLCENGINE_ACCESS_KEY") |
| 88 | + secret_key = os.getenv("VOLCENGINE_SECRET_KEY") |
| 89 | + session_token = "" |
| 90 | + region = os.getenv("AGENTKIT_TOOL_REGION", "cn-beijing") |
| 91 | + |
| 92 | + if not (access_key and secret_key): |
| 93 | + cred = get_credential_from_vefaas_iam() |
| 94 | + access_key = cred.access_key_id |
| 95 | + secret_key = cred.secret_access_key |
| 96 | + session_token = cred.session_token |
| 97 | + |
| 98 | + res = ve_request( |
| 99 | + request_body={}, |
| 100 | + action="GetCallerIdentity", |
| 101 | + ak=access_key, |
| 102 | + sk=secret_key, |
| 103 | + service="sts", |
| 104 | + version="2018-01-01", |
| 105 | + region=region, |
| 106 | + host="sts.volcengineapi.com", |
| 107 | + header={"X-Security-Token": session_token}, |
| 108 | + ) |
| 109 | + try: |
| 110 | + account_id = res["Result"]["AccountId"] |
| 111 | + except KeyError as e: |
| 112 | + logger.error( |
| 113 | + f"Error occurred while getting account id: {e}, response is {res}" |
| 114 | + ) |
| 115 | + return f"Error: Failed to get account id when registering skill '{skill_name}'." |
| 116 | + |
| 117 | + tos_bucket = f"agentkit-platform-{region}-{account_id}-skill" |
| 118 | + |
| 119 | + tos_client = VeTOS( |
| 120 | + ak=access_key, |
| 121 | + sk=secret_key, |
| 122 | + session_token=session_token, |
| 123 | + bucket_name=tos_bucket, |
| 124 | + ) |
| 125 | + |
| 126 | + object_key = ( |
| 127 | + f"uploads/{datetime.now().strftime('%Y%m%d_%H%M%S')}/{skill_name}.zip" |
| 128 | + ) |
| 129 | + tos_client.upload_file( |
| 130 | + file_path=zip_file_path, bucket_name=tos_bucket, object_key=object_key |
| 131 | + ) |
| 132 | + tos_url = tos_client.build_tos_url( |
| 133 | + bucket_name=tos_bucket, object_key=object_key |
| 134 | + ) |
| 135 | + |
| 136 | + skill_space_ids = os.getenv("SKILL_SPACE_ID", "") |
| 137 | + skill_space_ids_list = [ |
| 138 | + x.strip() for x in skill_space_ids.split(",") if x.strip() |
| 139 | + ] |
| 140 | + |
| 141 | + response = ve_request( |
| 142 | + request_body={ |
| 143 | + "Name": skill_name, |
| 144 | + "Description": skill_description, |
| 145 | + "TosUrl": tos_url, |
| 146 | + "SkillSpaces": skill_space_ids_list, |
| 147 | + }, |
| 148 | + action="CreateSkill", |
| 149 | + ak=access_key, |
| 150 | + sk=secret_key, |
| 151 | + service=service, |
| 152 | + version="2025-10-30", |
| 153 | + region=region, |
| 154 | + host=host, |
| 155 | + header={"X-Security-Token": session_token}, |
| 156 | + ) |
| 157 | + |
| 158 | + if isinstance(response, str): |
| 159 | + response = json.loads(response) |
| 160 | + |
| 161 | + logger.debug(f"CreateSkill response: {response}") |
| 162 | + |
| 163 | + if "Error" in response: |
| 164 | + logger.error( |
| 165 | + f"Failed to register skill '{skill_name}': {response['Error']}" |
| 166 | + ) |
| 167 | + return f"Failed to register skill '{skill_name}': {response['Error']}" |
| 168 | + |
| 169 | + logger.info( |
| 170 | + f"Successfully registered skill '{skill_name}' to skill space {skill_space_ids_list}." |
| 171 | + ) |
| 172 | + return f"Successfully registered skill '{skill_name}' to skill space {skill_space_ids_list}." |
| 173 | + |
| 174 | + except Exception as e: |
| 175 | + logger.error(f"Failed to register skill '{skill_name}': {e}") |
| 176 | + return f"Failed to register skill '{skill_name}'" |
0 commit comments