mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
feat: add README files for Context API and RPC communication; implement StarManager for plugin discovery
This commit is contained in:
8
src/astrbot_sdk/runtime/api/README.md
Normal file
8
src/astrbot_sdk/runtime/api/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# AstrBot SDK Runtime Context API
|
||||
|
||||
这个包下存储了暴露给 AstrBot 插件的 Context API 的 RPC 实现。
|
||||
|
||||
## 组件
|
||||
|
||||
- `Context`:这是在实例化插件时,注入到插件中的上下文对象。它封装了插件可以调用的各种功能组件。
|
||||
- `ConversationManager`:这是一个管理对话相关的功能组件。它提供了与对话历史、用户信息等相关的操作接口。
|
||||
7
src/astrbot_sdk/runtime/rpc/README.md
Normal file
7
src/astrbot_sdk/runtime/rpc/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# AstrBor SDK 与 Core 通信的数据交换实现
|
||||
|
||||
这个包下存储了 AstrBot 插件运行时与 AstrBot Core 之间通信的数据交换实现。
|
||||
|
||||
AstrBot SDK 设计了两种传输协议,即 stdio 和 WebSockets,用于实现 AstrBot 插件与 AstrBot Core 之间的双向通信。
|
||||
|
||||
在这两种传输协议之上,我们使用 JSON-RPC 2.0 作为通信的消息格式和调用规范。
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user