From fb56ac9f470d5f4d82b1ec37b5cefb65ff75bb79 Mon Sep 17 00:00:00 2001 From: LIghtJUNction Date: Mon, 3 Nov 2025 23:28:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=9D=E8=B5=96=E6=B3=A8=E5=85=A5=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/__main__.py | 6 +- astrbot/base/README.md | 3 - astrbot/base/__init__.py | 9 --- astrbot/base/abc.py | 71 ------------------- astrbot/cli/__main__.py | 2 +- astrbot/cli/commands/cmd_plug.py | 5 +- astrbot/cli/utils/basic.py | 4 +- astrbot/core/config/astrbot_config.py | 6 +- astrbot/core/config/default.py | 6 +- astrbot/core/message/components.py | 5 +- .../sources/webchat/webchat_adapter.py | 5 +- .../wechatpadpro/wechatpadpro_adapter.py | 5 +- .../core/provider/sources/dashscope_tts.py | 6 +- .../core/provider/sources/edge_tts_source.py | 5 +- .../core/provider/sources/volcengine_tts.py | 5 +- .../provider/sources/whisper_api_source.py | 5 +- astrbot/core/star/config.py | 6 +- astrbot/core/utils/astrbot_path.py | 5 +- astrbot/core/utils/io.py | 4 +- astrbot/dashboard/routes/chat.py | 5 +- astrbot/dashboard/server.py | 5 +- astrbot_api/src/astrbot_api/__init__.py | 9 ++- astrbot_api/src/astrbot_api/abc.py | 23 ++++-- .../src/astrbot_api}/const.py | 0 astrbot_sdk/pyproject.toml | 2 + astrbot_sdk/src/astrbot_sdk/__init__.py | 25 ++++++- astrbot_sdk/src/astrbot_sdk/base/README.md | 3 + astrbot_sdk/src/astrbot_sdk/base/__init__.py | 3 + .../src/astrbot_sdk}/base/paths.py | 24 +++---- astrbot_sdk/src/astrbot_sdk/base/provider.py | 19 +++++ astrbot_sdk/src/astrbot_sdk/star.py | 0 packages/python_interpreter/main.py | 5 +- packages/reminder/main.py | 5 +- pyproject.toml | 6 +- tests/test_paths.py | 5 +- 35 files changed, 168 insertions(+), 134 deletions(-) delete mode 100644 astrbot/base/README.md delete mode 100644 astrbot/base/__init__.py delete mode 100644 astrbot/base/abc.py rename {astrbot/base => astrbot_api/src/astrbot_api}/const.py (100%) create mode 100644 astrbot_sdk/src/astrbot_sdk/base/README.md create mode 100644 astrbot_sdk/src/astrbot_sdk/base/__init__.py rename {astrbot => astrbot_sdk/src/astrbot_sdk}/base/paths.py (90%) create mode 100644 astrbot_sdk/src/astrbot_sdk/base/provider.py delete mode 100644 astrbot_sdk/src/astrbot_sdk/star.py diff --git a/astrbot/__main__.py b/astrbot/__main__.py index c3620ce8..acdf69c3 100644 --- a/astrbot/__main__.py +++ b/astrbot/__main__.py @@ -4,14 +4,16 @@ import mimetypes import os import sys -from astrbot.base import LOGO, AstrbotPaths from astrbot.core import LogBroker, LogManager, db_helper, logger from astrbot.core.config.default import VERSION from astrbot.core.initial_loader import InitialLoader from astrbot.core.utils.io import download_dashboard, get_dashboard_version +from astrbot_api import LOGO, IAstrbotPaths +from astrbot_sdk import sync_base_container +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) -def check_env(): +def check_env() -> None: if not (sys.version_info.major == 3 and sys.version_info.minor >= 10): logger.error("请使用 Python3.10+ 运行本项目。") exit() diff --git a/astrbot/base/README.md b/astrbot/base/README.md deleted file mode 100644 index b62e9f48..00000000 --- a/astrbot/base/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Base 包 - -- 此包内容仅可单向导出 diff --git a/astrbot/base/__init__.py b/astrbot/base/__init__.py deleted file mode 100644 index 069d4804..00000000 --- a/astrbot/base/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -from .abc import IAstrbotPaths -from .const import LOGO -from .paths import AstrbotPaths - -__all__ = [ - "IAstrbotPaths", - "AstrbotPaths", - "LOGO", -] diff --git a/astrbot/base/abc.py b/astrbot/base/abc.py deleted file mode 100644 index 78642fac..00000000 --- a/astrbot/base/abc.py +++ /dev/null @@ -1,71 +0,0 @@ -from __future__ import annotations - -from abc import ABC, abstractmethod -from contextlib import AbstractAsyncContextManager, AbstractContextManager -from pathlib import Path - - -# TODO: 抽象基类 -class IAstrbotPaths(ABC): - """路径管理的抽象基类.""" - - @abstractmethod - def __init__(self, name: str) -> None: - """初始化路径管理器.""" - - @classmethod - @abstractmethod - def getPaths(cls, name: str) -> IAstrbotPaths: - """返回Paths实例,用于访问模块的各类目录.""" - - @property - @abstractmethod - def root(self) -> Path: - """获取根目录.""" - - @property - @abstractmethod - def home(self) -> Path: - """获取模块/插件主目录.""" - - @property - @abstractmethod - def config(self) -> Path: - """获取模块配置目录.""" - - @property - @abstractmethod - def data(self) -> Path: - """获取模块数据目录.""" - - @property - @abstractmethod - def log(self) -> Path: - """获取模块日志目录.""" - - @property - @abstractmethod - def temp(self) -> Path: - """获取模块临时目录.""" - - @property - @abstractmethod - def plugins(self) -> Path: - """获取插件目录.""" - - @abstractmethod - def reload(self) -> None: - """重新加载环境变量.""" - - @classmethod - @abstractmethod - def is_root(cls, path: Path) -> bool: - """判断路径是否为根目录.""" - - @abstractmethod - def chdir(self, cwd: str = "home") -> AbstractContextManager[Path]: - """临时切换到指定目录, 子进程将继承此 CWD。""" - - @abstractmethod - async def achdir(self, cwd: str = "home") -> AbstractAsyncContextManager[Path]: - """异步临时切换到指定目录, 子进程将继承此 CWD。""" diff --git a/astrbot/cli/__main__.py b/astrbot/cli/__main__.py index 24f6c3a4..5d2c2153 100644 --- a/astrbot/cli/__main__.py +++ b/astrbot/cli/__main__.py @@ -5,7 +5,7 @@ from importlib.metadata import version import click -from astrbot.base import LOGO +from astrbot_api import LOGO from .commands import conf, init, plug, run diff --git a/astrbot/cli/commands/cmd_plug.py b/astrbot/cli/commands/cmd_plug.py index 5118ae37..649c138e 100644 --- a/astrbot/cli/commands/cmd_plug.py +++ b/astrbot/cli/commands/cmd_plug.py @@ -3,8 +3,11 @@ import shutil from pathlib import Path import click +from astrbot_api.abc import IAstrbotPaths -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from ..utils import ( PluginStatus, diff --git a/astrbot/cli/utils/basic.py b/astrbot/cli/utils/basic.py index d73be4f5..c3d0d156 100644 --- a/astrbot/cli/utils/basic.py +++ b/astrbot/cli/utils/basic.py @@ -2,9 +2,11 @@ import warnings from pathlib import Path import click +from astrbot_api.abc import IAstrbotPaths -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container +AstrbotPaths = sync_base_container.get(type[IAstrbotPaths]) def check_astrbot_root(path: str | Path) -> bool: """检查路径是否为 AstrBot 根目录""" diff --git a/astrbot/core/config/astrbot_config.py b/astrbot/core/config/astrbot_config.py index 08e68a25..905c5a61 100644 --- a/astrbot/core/config/astrbot_config.py +++ b/astrbot/core/config/astrbot_config.py @@ -3,10 +3,14 @@ import json import logging import os -from astrbot.base import AstrbotPaths +from astrbot_api.abc import IAstrbotPaths + +from astrbot_sdk import sync_base_container from .default import DEFAULT_CONFIG, DEFAULT_VALUE_MAP +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) + ASTRBOT_CONFIG_PATH = str(AstrbotPaths.astrbot_root / "cmd_config.json") logger = logging.getLogger("astrbot") diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 7f9823ee..4f4dc885 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -2,7 +2,11 @@ from importlib.metadata import version -from astrbot.base import AstrbotPaths +from astrbot_api.abc import IAstrbotPaths + +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) # 警告,请使用version函数获取版本,此变量兼容保留 VERSION = version("astrbot") diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index c2cdfdca..db22302a 100644 --- a/astrbot/core/message/components.py +++ b/astrbot/core/message/components.py @@ -27,13 +27,14 @@ import os import uuid from enum import Enum +from astrbot_api.abc import IAstrbotPaths from pydantic.v1 import BaseModel -from astrbot.base import AstrbotPaths from astrbot.core import astrbot_config, file_token_service, logger from astrbot.core.utils.io import download_file, download_image_by_url, file_to_base64 +from astrbot_sdk import sync_base_container - +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) class ComponentType(str, Enum): # Basic Segment Types Plain = "Plain" # plain text message diff --git a/astrbot/core/platform/sources/webchat/webchat_adapter.py b/astrbot/core/platform/sources/webchat/webchat_adapter.py index 2c851a58..533ad9f0 100644 --- a/astrbot/core/platform/sources/webchat/webchat_adapter.py +++ b/astrbot/core/platform/sources/webchat/webchat_adapter.py @@ -5,8 +5,9 @@ import uuid from collections.abc import Awaitable, Callable from typing import Any +from astrbot_api.abc import IAstrbotPaths + from astrbot import logger -from astrbot.base import AstrbotPaths from astrbot.core.message.components import Image, Plain, Record from astrbot.core.message.message_event_result import MessageChain from astrbot.core.platform import ( @@ -17,11 +18,13 @@ from astrbot.core.platform import ( PlatformMetadata, ) from astrbot.core.platform.astr_message_event import MessageSesion +from astrbot_sdk import sync_base_container from ...register import register_platform_adapter from .webchat_event import WebChatMessageEvent from .webchat_queue_mgr import WebChatQueueMgr, webchat_queue_mgr +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) class QueueListener: def __init__(self, webchat_queue_mgr: WebChatQueueMgr, callback: Callable) -> None: diff --git a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py index fc5ff102..21c82191 100644 --- a/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py +++ b/astrbot/core/platform/sources/wechatpadpro/wechatpadpro_adapter.py @@ -8,11 +8,14 @@ import traceback import aiohttp import anyio import websockets +from astrbot_api.abc import IAstrbotPaths from astrbot import logger from astrbot.api.message_components import At, Image, Plain, Record from astrbot.api.platform import Platform, PlatformMetadata -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from astrbot.core.message.message_event_result import MessageChain from astrbot.core.platform.astr_message_event import MessageSesion from astrbot.core.platform.astrbot_message import ( diff --git a/astrbot/core/provider/sources/dashscope_tts.py b/astrbot/core/provider/sources/dashscope_tts.py index 8d2475c9..951e5fa1 100644 --- a/astrbot/core/provider/sources/dashscope_tts.py +++ b/astrbot/core/provider/sources/dashscope_tts.py @@ -15,7 +15,11 @@ except ( ): # pragma: no cover - older dashscope versions without Qwen TTS support MultiModalConversation = None -from astrbot.base import AstrbotPaths +from astrbot_api.abc import IAstrbotPaths + +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from ..entities import ProviderType from ..provider import TTSProvider diff --git a/astrbot/core/provider/sources/edge_tts_source.py b/astrbot/core/provider/sources/edge_tts_source.py index 75225e87..f2fe4d1c 100644 --- a/astrbot/core/provider/sources/edge_tts_source.py +++ b/astrbot/core/provider/sources/edge_tts_source.py @@ -4,8 +4,11 @@ import subprocess import uuid import edge_tts +from astrbot_api.abc import IAstrbotPaths -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from astrbot.core import logger from ..entities import ProviderType diff --git a/astrbot/core/provider/sources/volcengine_tts.py b/astrbot/core/provider/sources/volcengine_tts.py index 1bdcbf7c..097d9ed9 100644 --- a/astrbot/core/provider/sources/volcengine_tts.py +++ b/astrbot/core/provider/sources/volcengine_tts.py @@ -5,9 +5,12 @@ import traceback import uuid import aiohttp +from astrbot_api.abc import IAstrbotPaths from astrbot import logger -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from ..entities import ProviderType from ..provider import TTSProvider diff --git a/astrbot/core/provider/sources/whisper_api_source.py b/astrbot/core/provider/sources/whisper_api_source.py index c6b57ce9..1393dded 100644 --- a/astrbot/core/provider/sources/whisper_api_source.py +++ b/astrbot/core/provider/sources/whisper_api_source.py @@ -1,9 +1,12 @@ import os import uuid +from astrbot_api.abc import IAstrbotPaths from openai import NOT_GIVEN, AsyncOpenAI -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from astrbot.core import logger from astrbot.core.utils.io import download_file from astrbot.core.utils.tencent_record_helper import tencent_silk_to_wav diff --git a/astrbot/core/star/config.py b/astrbot/core/star/config.py index 2ddb70e7..53bdfce1 100644 --- a/astrbot/core/star/config.py +++ b/astrbot/core/star/config.py @@ -3,7 +3,11 @@ import json import os -from astrbot.base import AstrbotPaths +from astrbot_api.abc import IAstrbotPaths + +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) def load_config(namespace: str) -> dict | bool: diff --git a/astrbot/core/utils/astrbot_path.py b/astrbot/core/utils/astrbot_path.py index b3b79a5d..7a313c01 100644 --- a/astrbot/core/utils/astrbot_path.py +++ b/astrbot/core/utils/astrbot_path.py @@ -10,8 +10,11 @@ import os import warnings -from astrbot.base import AstrbotPaths +from astrbot_api.abc import IAstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths = sync_base_container.get(type[IAstrbotPaths]) def get_astrbot_path() -> str: """获取Astrbot项目路径 -- 请勿使用本函数!!! -- 仅供兼容旧代码使用""" diff --git a/astrbot/core/utils/io.py b/astrbot/core/utils/io.py index 6eb2a28a..328cba36 100644 --- a/astrbot/core/utils/io.py +++ b/astrbot/core/utils/io.py @@ -12,14 +12,16 @@ from pathlib import Path import aiohttp import certifi import psutil +from astrbot_api.abc import IAstrbotPaths from PIL import Image -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container from .astrbot_path import get_astrbot_data_path logger = logging.getLogger("astrbot") +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) def on_error(func, path, exc_info): """A callback of the rmtree function.""" diff --git a/astrbot/dashboard/routes/chat.py b/astrbot/dashboard/routes/chat.py index b1175143..05c5dec2 100644 --- a/astrbot/dashboard/routes/chat.py +++ b/astrbot/dashboard/routes/chat.py @@ -4,10 +4,13 @@ import os import uuid from contextlib import asynccontextmanager +from astrbot_api.abc import IAstrbotPaths from quart import Response as QuartResponse from quart import g, make_response, request -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from astrbot.core import logger from astrbot.core.core_lifecycle import AstrBotCoreLifecycle from astrbot.core.db import BaseDatabase diff --git a/astrbot/dashboard/server.py b/astrbot/dashboard/server.py index ad041e1e..73006466 100644 --- a/astrbot/dashboard/server.py +++ b/astrbot/dashboard/server.py @@ -5,15 +5,16 @@ import socket import jwt import psutil +from astrbot_api.abc import IAstrbotPaths from quart import Quart, g, jsonify, request from quart.logging import default_handler -from astrbot.base import AstrbotPaths from astrbot.core import logger from astrbot.core.config.default import VERSION from astrbot.core.core_lifecycle import AstrBotCoreLifecycle from astrbot.core.db import BaseDatabase from astrbot.core.utils.io import get_local_ip_addresses +from astrbot_sdk import sync_base_container from .routes import * from .routes.route import Response, RouteContext @@ -22,7 +23,7 @@ from .routes.t2i import T2iRoute APP: Quart = None - +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) class AstrBotDashboard: def __init__( self, diff --git a/astrbot_api/src/astrbot_api/__init__.py b/astrbot_api/src/astrbot_api/__init__.py index 06655012..1460dd90 100644 --- a/astrbot_api/src/astrbot_api/__init__.py +++ b/astrbot_api/src/astrbot_api/__init__.py @@ -1,2 +1,7 @@ -def hello() -> str: - return "Hello from astrbot-api!" +from .abc import IAstrbotPaths +from .const import LOGO + +__all__ = [ + "IAstrbotPaths", + "LOGO", +] diff --git a/astrbot_api/src/astrbot_api/abc.py b/astrbot_api/src/astrbot_api/abc.py index 5841d136..174fa50d 100644 --- a/astrbot_api/src/astrbot_api/abc.py +++ b/astrbot_api/src/astrbot_api/abc.py @@ -3,7 +3,12 @@ from __future__ import annotations import json from abc import ABC, abstractmethod from collections.abc import AsyncGenerator, Callable, Generator -from contextlib import AbstractAsyncContextManager, AbstractContextManager, asynccontextmanager, contextmanager +from contextlib import ( + AbstractAsyncContextManager, + AbstractContextManager, + asynccontextmanager, + contextmanager, +) from importlib.abc import Traversable from pathlib import Path from typing import ClassVar @@ -17,8 +22,18 @@ from .models import AstrbotPluginMetadata # region 核心运行时协议 + +class IAstrbotContainerMgr(ABC): + """AstrBot 容器管理器的抽象基类.""" + + + + + class IAstrbotPaths(ABC): """路径管理的抽象基类.""" + astrbot_root: ClassVar[Path] + """Astrbot 根目录路径.""" @abstractmethod def __init__(self, name: str) -> None: @@ -86,12 +101,6 @@ class IAstrbotPaths(ABC): - - - - - - class AstrbotPluginBaseSession(ABC): """插件会话的基类.""" diff --git a/astrbot/base/const.py b/astrbot_api/src/astrbot_api/const.py similarity index 100% rename from astrbot/base/const.py rename to astrbot_api/src/astrbot_api/const.py diff --git a/astrbot_sdk/pyproject.toml b/astrbot_sdk/pyproject.toml index ab4ce68a..22e869d7 100644 --- a/astrbot_sdk/pyproject.toml +++ b/astrbot_sdk/pyproject.toml @@ -10,6 +10,8 @@ authors = [ requires-python = ">=3.10" dependencies = [ "astrbot-api", + "dishka>=1.7.2", + "dotenv>=0.9.9", ] [build-system] diff --git a/astrbot_sdk/src/astrbot_sdk/__init__.py b/astrbot_sdk/src/astrbot_sdk/__init__.py index fe539e22..1ffebdce 100644 --- a/astrbot_sdk/src/astrbot_sdk/__init__.py +++ b/astrbot_sdk/src/astrbot_sdk/__init__.py @@ -1,2 +1,23 @@ -def hello() -> str: - return "Hello from astrbot-sdk!" +from dishka import ( + AsyncContainer, + Container, + Provider, + make_async_container, + make_container, +) + +from .base import AstrbotBaseProvider + + + + + + +base_provider: Provider = AstrbotBaseProvider() +async_base_container: AsyncContainer = make_async_container(base_provider) +sync_base_container: Container = make_container(base_provider) + +__all__ = [ + "async_base_container", + "sync_base_container", +] diff --git a/astrbot_sdk/src/astrbot_sdk/base/README.md b/astrbot_sdk/src/astrbot_sdk/base/README.md new file mode 100644 index 00000000..523d1cf2 --- /dev/null +++ b/astrbot_sdk/src/astrbot_sdk/base/README.md @@ -0,0 +1,3 @@ +# 基础类的实现 + +- 单向导出 diff --git a/astrbot_sdk/src/astrbot_sdk/base/__init__.py b/astrbot_sdk/src/astrbot_sdk/base/__init__.py new file mode 100644 index 00000000..d4858d5f --- /dev/null +++ b/astrbot_sdk/src/astrbot_sdk/base/__init__.py @@ -0,0 +1,3 @@ +from .provider import AstrbotBaseProvider + +__all__ = ["AstrbotBaseProvider"] diff --git a/astrbot/base/paths.py b/astrbot_sdk/src/astrbot_sdk/base/paths.py similarity index 90% rename from astrbot/base/paths.py rename to astrbot_sdk/src/astrbot_sdk/base/paths.py index 3cd21fe8..d02cc3a9 100644 --- a/astrbot/base/paths.py +++ b/astrbot_sdk/src/astrbot_sdk/base/paths.py @@ -1,21 +1,15 @@ from __future__ import annotations -import os -from contextlib import ( - asynccontextmanager, - contextmanager, -) -from os import getenv +from collections.abc import AsyncGenerator, Generator +from contextlib import asynccontextmanager, contextmanager +from os import chdir, getenv from pathlib import Path -from typing import TYPE_CHECKING, ClassVar +from typing import ClassVar from dotenv import load_dotenv from packaging.utils import NormalizedName, canonicalize_name -from astrbot.base.abc import IAstrbotPaths - -if TYPE_CHECKING: - from collections.abc import AsyncGenerator, Generator +from astrbot_api import IAstrbotPaths class AstrbotPaths(IAstrbotPaths): @@ -120,10 +114,10 @@ class AstrbotPaths(IAstrbotPaths): original_cwd = Path.cwd() target_dir = self.root / cwd try: - os.chdir(target_dir) + chdir(target_dir) yield target_dir finally: - os.chdir(original_cwd) + chdir(original_cwd) # 上面类型标注没错,这里mypy报错,但是这不应该错误,直接忽略掉 @asynccontextmanager @@ -132,7 +126,7 @@ class AstrbotPaths(IAstrbotPaths): original_cwd = Path.cwd() target_dir = self.root / cwd try: - os.chdir(target_dir) + chdir(target_dir) yield target_dir finally: - os.chdir(original_cwd) + chdir(original_cwd) diff --git a/astrbot_sdk/src/astrbot_sdk/base/provider.py b/astrbot_sdk/src/astrbot_sdk/base/provider.py new file mode 100644 index 00000000..df0d50d7 --- /dev/null +++ b/astrbot_sdk/src/astrbot_sdk/base/provider.py @@ -0,0 +1,19 @@ +from dishka import Provider, Scope, provide + +from astrbot_api import IAstrbotPaths + +from .paths import AstrbotPaths + + +class AstrbotBaseProvider(Provider): + scope = Scope.APP # 基础Provider的作用域设为APP + + @provide + def get_astrbot_paths_cls(self) -> type[IAstrbotPaths]: + return AstrbotPaths + + + + + + diff --git a/astrbot_sdk/src/astrbot_sdk/star.py b/astrbot_sdk/src/astrbot_sdk/star.py deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/python_interpreter/main.py b/packages/python_interpreter/main.py index 05804e5b..e82ab1a5 100644 --- a/packages/python_interpreter/main.py +++ b/packages/python_interpreter/main.py @@ -9,12 +9,15 @@ from collections import defaultdict import aiodocker import aiohttp +from astrbot_api.abc import IAstrbotPaths from astrbot.api import llm_tool, logger, star from astrbot.api.event import AstrMessageEvent, MessageEventResult, filter from astrbot.api.message_components import File, Image from astrbot.api.provider import ProviderRequest -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from astrbot.core.utils.io import download_file, download_image_by_url PROMPT = """ diff --git a/packages/reminder/main.py b/packages/reminder/main.py index 52d45e6b..cce5ed99 100644 --- a/packages/reminder/main.py +++ b/packages/reminder/main.py @@ -5,10 +5,13 @@ import uuid import zoneinfo from apscheduler.schedulers.asyncio import AsyncIOScheduler +from astrbot_api.abc import IAstrbotPaths from astrbot.api import llm_tool, logger, star from astrbot.api.event import AstrMessageEvent, MessageEventResult, filter -from astrbot.base import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) from astrbot.core.utils.astrbot_path import get_astrbot_data_path diff --git a/pyproject.toml b/pyproject.toml index 300f0b03..40c1c861 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,8 @@ dependencies = [ "xinference-client", "dotenv>=0.9.9", "astrbot-api", + "astrbot-sdk", + "dishka>=1.7.2", ] [dependency-groups] @@ -113,12 +115,14 @@ members = [ [tool.uv.sources] astrbot-api = { workspace = true } +astrbot-sdk = { workspace = true } + [tool.pyright] typeCheckingMode = "basic" pythonVersion = "3.10" reportMissingTypeStubs = false reportMissingImports = false -include = ["astrbot","packages"] +include = ["astrbot","astrbot_api","astrbot_sdk","packages"] exclude = ["dashboard", "node_modules", "dist", "data", "tests"] [tool.hatch.version] diff --git a/tests/test_paths.py b/tests/test_paths.py index fff5f5cb..83cfc36e 100644 --- a/tests/test_paths.py +++ b/tests/test_paths.py @@ -8,8 +8,11 @@ from pathlib import Path from typing import TYPE_CHECKING import pytest +from astrbot_api.abc import IAstrbotPaths -from astrbot.base.paths import AstrbotPaths +from astrbot_sdk import sync_base_container + +AstrbotPaths: type[IAstrbotPaths] = sync_base_container.get(type[IAstrbotPaths]) if TYPE_CHECKING: from collections.abc import Generator