From ff904bae8fb194bc048314d64bbe17b53e107bec Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Wed, 12 Nov 2025 20:49:57 +0800 Subject: [PATCH] feat: add README files for Context API and RPC communication; implement StarManager for plugin discovery --- src/astrbot_sdk/runtime/api/README.md | 8 ++ src/astrbot_sdk/runtime/rpc/README.md | 7 ++ src/astrbot_sdk/runtime/serve.py | 2 +- .../runtime/{ => stars}/star_manager.py | 6 +- src/astrbot_sdk/util.py | 81 ------------------- 5 files changed, 19 insertions(+), 85 deletions(-) create mode 100644 src/astrbot_sdk/runtime/api/README.md create mode 100644 src/astrbot_sdk/runtime/rpc/README.md rename src/astrbot_sdk/runtime/{ => stars}/star_manager.py (96%) delete mode 100644 src/astrbot_sdk/util.py diff --git a/src/astrbot_sdk/runtime/api/README.md b/src/astrbot_sdk/runtime/api/README.md new file mode 100644 index 000000000..b3afae19f --- /dev/null +++ b/src/astrbot_sdk/runtime/api/README.md @@ -0,0 +1,8 @@ +# AstrBot SDK Runtime Context API + +这个包下存储了暴露给 AstrBot 插件的 Context API 的 RPC 实现。 + +## 组件 + +- `Context`:这是在实例化插件时,注入到插件中的上下文对象。它封装了插件可以调用的各种功能组件。 +- `ConversationManager`:这是一个管理对话相关的功能组件。它提供了与对话历史、用户信息等相关的操作接口。 diff --git a/src/astrbot_sdk/runtime/rpc/README.md b/src/astrbot_sdk/runtime/rpc/README.md new file mode 100644 index 000000000..aac32be25 --- /dev/null +++ b/src/astrbot_sdk/runtime/rpc/README.md @@ -0,0 +1,7 @@ +# AstrBor SDK 与 Core 通信的数据交换实现 + +这个包下存储了 AstrBot 插件运行时与 AstrBot Core 之间通信的数据交换实现。 + +AstrBot SDK 设计了两种传输协议,即 stdio 和 WebSockets,用于实现 AstrBot 插件与 AstrBot Core 之间的双向通信。 + +在这两种传输协议之上,我们使用 JSON-RPC 2.0 作为通信的消息格式和调用规范。 diff --git a/src/astrbot_sdk/runtime/serve.py b/src/astrbot_sdk/runtime/serve.py index a31993f6f..7460733d5 100644 --- a/src/astrbot_sdk/runtime/serve.py +++ b/src/astrbot_sdk/runtime/serve.py @@ -2,7 +2,7 @@ import asyncio import signal from .rpc.server import WebSocketServer, StdioServer from .star_runner import StarRunner -from .star_manager import StarManager +from .stars.star_manager import StarManager from .api.context import Context from loguru import logger from typing import IO, Any diff --git a/src/astrbot_sdk/runtime/star_manager.py b/src/astrbot_sdk/runtime/stars/star_manager.py similarity index 96% rename from src/astrbot_sdk/runtime/star_manager.py rename to src/astrbot_sdk/runtime/stars/star_manager.py index 99cb0fe59..c3f574883 100644 --- a/src/astrbot_sdk/runtime/star_manager.py +++ b/src/astrbot_sdk/runtime/stars/star_manager.py @@ -4,9 +4,9 @@ import functools import sys from pathlib import Path from loguru import logger -from .stars.registry import star_handlers_registry, star_map, star_registry -from ..runtime.api.context import Context -from ..api.star.star import StarMetadata +from .registry import star_handlers_registry, star_map, star_registry +from ..api.context import Context +from ...api.star.star import StarMetadata class StarManager: diff --git a/src/astrbot_sdk/util.py b/src/astrbot_sdk/util.py deleted file mode 100644 index 1d3ebdcd7..000000000 --- a/src/astrbot_sdk/util.py +++ /dev/null @@ -1,81 +0,0 @@ -import aiohttp -import certifi -import ssl -import time -from loguru import logger - - -async def download_file(url: str, path: str, show_progress: bool = False): - """从指定 url 下载文件到指定路径 path""" - try: - ssl_context = ssl.create_default_context( - cafile=certifi.where(), - ) # 使用 certifi 提供的 CA 证书 - connector = aiohttp.TCPConnector(ssl=ssl_context) - async with aiohttp.ClientSession( - trust_env=True, - connector=connector, - ) as session: - async with session.get(url, timeout=1800) as resp: - if resp.status != 200: - raise Exception(f"下载文件失败: {resp.status}") - total_size = int(resp.headers.get("content-length", 0)) - downloaded_size = 0 - start_time = time.time() - if show_progress: - print(f"文件大小: {total_size / 1024:.2f} KB | 文件地址: {url}") - with open(path, "wb") as f: - while True: - chunk = await resp.content.read(8192) - if not chunk: - break - f.write(chunk) - downloaded_size += len(chunk) - if show_progress: - elapsed_time = ( - time.time() - start_time - if time.time() - start_time > 0 - else 1 - ) - speed = downloaded_size / 1024 / elapsed_time # KB/s - print( - f"\r下载进度: {downloaded_size / total_size:.2%} 速度: {speed:.2f} KB/s", - end="", - ) - except (aiohttp.ClientConnectorSSLError, aiohttp.ClientConnectorCertificateError): - # 关闭SSL验证(仅在证书验证失败时作为fallback) - logger.warning( - "SSL 证书验证失败,已关闭 SSL 验证(不安全,仅用于临时下载)。请检查目标服务器的证书配置。" - ) - logger.warning( - f"SSL certificate verification failed for {url}. " - "Falling back to unverified connection (CERT_NONE). " - "This is insecure and exposes the application to man-in-the-middle attacks. " - "Please investigate certificate issues with the remote server." - ) - ssl_context = ssl.create_default_context() - ssl_context.check_hostname = False - ssl_context.verify_mode = ssl.CERT_NONE - async with aiohttp.ClientSession() as session: - async with session.get(url, ssl=ssl_context, timeout=120) as resp: - total_size = int(resp.headers.get("content-length", 0)) - downloaded_size = 0 - start_time = time.time() - if show_progress: - print(f"文件大小: {total_size / 1024:.2f} KB | 文件地址: {url}") - with open(path, "wb") as f: - while True: - chunk = await resp.content.read(8192) - if not chunk: - break - f.write(chunk) - downloaded_size += len(chunk) - if show_progress: - elapsed_time = time.time() - start_time - speed = downloaded_size / 1024 / elapsed_time # KB/s - print( - f"\r下载进度: {downloaded_size / total_size:.2%} 速度: {speed:.2f} KB/s", - end="", - ) - if show_progress: - print()