|
14 | 14 | Provides object-oriented wrapper and complete lifecycle management for tool resources. |
15 | 15 | """ |
16 | 16 |
|
| 17 | +import io |
| 18 | +import os |
| 19 | +import shutil |
17 | 20 | from typing import Any, Dict, List, Optional |
| 21 | +import zipfile |
18 | 22 |
|
| 23 | +import httpx |
19 | 24 | import pydash |
20 | 25 |
|
21 | 26 | from agentrun.utils.config import Config |
@@ -438,3 +443,141 @@ def call_tool( |
438 | 443 | return result |
439 | 444 |
|
440 | 445 | raise ValueError(f"Unsupported tool type: {self.tool_type}") |
| 446 | + |
| 447 | + def _get_skill_download_url( |
| 448 | + self, config: Optional[Config] = None |
| 449 | + ) -> Optional[str]: |
| 450 | + """获取 Skill 工具的下载 URL / Get download URL for Skill tools |
| 451 | +
|
| 452 | + 根据 data_endpoint 和 tool_name 构造下载地址。 |
| 453 | + Constructs download URL from data_endpoint and tool_name. |
| 454 | +
|
| 455 | + Returns: |
| 456 | + Optional[str]: 下载 URL / Download URL |
| 457 | + """ |
| 458 | + effective_name = self.tool_name or self.name |
| 459 | + data_endpoint = self.data_endpoint |
| 460 | + if not data_endpoint: |
| 461 | + cfg = Config.with_configs(config) |
| 462 | + data_endpoint = cfg._data_endpoint |
| 463 | + if not data_endpoint or not effective_name: |
| 464 | + return None |
| 465 | + return f"{data_endpoint}/tools/{effective_name}/download" |
| 466 | + |
| 467 | + async def download_skill_async( |
| 468 | + self, |
| 469 | + target_dir: str = ".skills", |
| 470 | + config: Optional[Config] = None, |
| 471 | + ) -> str: |
| 472 | + """异步下载 Skill 包并解压到本地目录 / Download skill package and extract to local directory asynchronously |
| 473 | +
|
| 474 | + 从数据链路下载 skill 的 zip 包,并解压到 {target_dir}/{tool_name}/ 目录下。 |
| 475 | + Downloads skill zip package from data endpoint and extracts to {target_dir}/{tool_name}/ directory. |
| 476 | +
|
| 477 | + Args: |
| 478 | + target_dir: 目标根目录,默认为 ".skills" / Target root directory, defaults to ".skills" |
| 479 | + config: 配置对象,可选 / Configuration object, optional |
| 480 | +
|
| 481 | + Returns: |
| 482 | + str: 解压后的 skill 目录路径 / Extracted skill directory path |
| 483 | +
|
| 484 | + Raises: |
| 485 | + ValueError: 工具类型不是 SKILL 或缺少必要信息 / Tool type is not SKILL or missing required info |
| 486 | + httpx.HTTPStatusError: 下载失败 / Download failed |
| 487 | + """ |
| 488 | + tool_type = self._get_tool_type() |
| 489 | + if tool_type != ToolType.SKILL: |
| 490 | + raise ValueError( |
| 491 | + "download_skill is only available for SKILL type tools," |
| 492 | + f" got {self.tool_type}" |
| 493 | + ) |
| 494 | + |
| 495 | + download_url = self._get_skill_download_url(config) |
| 496 | + if not download_url: |
| 497 | + raise ValueError( |
| 498 | + "Cannot construct download URL: data_endpoint or tool_name" |
| 499 | + " is missing" |
| 500 | + ) |
| 501 | + |
| 502 | + effective_name = self.tool_name or self.name |
| 503 | + skill_dir = os.path.join(target_dir, effective_name or "unknown_skill") |
| 504 | + |
| 505 | + logger.debug("downloading skill from %s to %s", download_url, skill_dir) |
| 506 | + |
| 507 | + cfg = Config.with_configs(config) |
| 508 | + headers = cfg.get_headers() |
| 509 | + |
| 510 | + async with httpx.AsyncClient( |
| 511 | + timeout=300, follow_redirects=True |
| 512 | + ) as http_client: |
| 513 | + response = await http_client.get(download_url, headers=headers) |
| 514 | + response.raise_for_status() |
| 515 | + |
| 516 | + if os.path.exists(skill_dir): |
| 517 | + shutil.rmtree(skill_dir) |
| 518 | + os.makedirs(skill_dir, exist_ok=True) |
| 519 | + |
| 520 | + zip_buffer = io.BytesIO(response.content) |
| 521 | + with zipfile.ZipFile(zip_buffer, "r") as zip_file: |
| 522 | + zip_file.extractall(skill_dir) |
| 523 | + |
| 524 | + logger.info("skill downloaded and extracted to %s", skill_dir) |
| 525 | + return skill_dir |
| 526 | + |
| 527 | + def download_skill( |
| 528 | + self, |
| 529 | + target_dir: str = ".skills", |
| 530 | + config: Optional[Config] = None, |
| 531 | + ) -> str: |
| 532 | + """同步下载 Skill 包并解压到本地目录 / Download skill package and extract to local directory synchronously |
| 533 | +
|
| 534 | + 从数据链路下载 skill 的 zip 包,并解压到 {target_dir}/{tool_name}/ 目录下。 |
| 535 | + Downloads skill zip package from data endpoint and extracts to {target_dir}/{tool_name}/ directory. |
| 536 | +
|
| 537 | + Args: |
| 538 | + target_dir: 目标根目录,默认为 ".skills" / Target root directory, defaults to ".skills" |
| 539 | + config: 配置对象,可选 / Configuration object, optional |
| 540 | +
|
| 541 | + Returns: |
| 542 | + str: 解压后的 skill 目录路径 / Extracted skill directory path |
| 543 | +
|
| 544 | + Raises: |
| 545 | + ValueError: 工具类型不是 SKILL 或缺少必要信息 / Tool type is not SKILL or missing required info |
| 546 | + httpx.HTTPStatusError: 下载失败 / Download failed |
| 547 | + """ |
| 548 | + tool_type = self._get_tool_type() |
| 549 | + if tool_type != ToolType.SKILL: |
| 550 | + raise ValueError( |
| 551 | + "download_skill is only available for SKILL type tools," |
| 552 | + f" got {self.tool_type}" |
| 553 | + ) |
| 554 | + |
| 555 | + download_url = self._get_skill_download_url(config) |
| 556 | + if not download_url: |
| 557 | + raise ValueError( |
| 558 | + "Cannot construct download URL: data_endpoint or tool_name" |
| 559 | + " is missing" |
| 560 | + ) |
| 561 | + |
| 562 | + effective_name = self.tool_name or self.name |
| 563 | + skill_dir = os.path.join(target_dir, effective_name or "unknown_skill") |
| 564 | + |
| 565 | + logger.debug("downloading skill from %s to %s", download_url, skill_dir) |
| 566 | + |
| 567 | + cfg = Config.with_configs(config) |
| 568 | + headers = cfg.get_headers() |
| 569 | + |
| 570 | + with httpx.Client(timeout=300, follow_redirects=True) as http_client: |
| 571 | + response = http_client.get(download_url, headers=headers) |
| 572 | + response.raise_for_status() |
| 573 | + |
| 574 | + if os.path.exists(skill_dir): |
| 575 | + shutil.rmtree(skill_dir) |
| 576 | + os.makedirs(skill_dir, exist_ok=True) |
| 577 | + |
| 578 | + zip_buffer = io.BytesIO(response.content) |
| 579 | + with zipfile.ZipFile(zip_buffer, "r") as zip_file: |
| 580 | + zip_file.extractall(skill_dir) |
| 581 | + |
| 582 | + logger.info("skill downloaded and extracted to %s", skill_dir) |
| 583 | + return skill_dir |
0 commit comments