Skip to content

Commit a27635c

Browse files
committed
Squashed 'astrbot-sdk/' changes from 44b67256b..3cf8142fb
3cf8142fb chore: refresh vendor snapshot [skip ci] ffe5fd101 Merge pull request #110 from united-pooh:dev 15f7adf4b refactor: update documentation and code references from v4 to astrbot-sdk a75fe8903 feat: 增强 wrap_client_exception 函数的异常处理逻辑,确保重建异常时的正确返回 7553253ed Merge pull request #109 from united-pooh/dev 7167f6ccf feat: 添加错误处理包装函数,增强客户端异常处理能力 REVERT: 44b67256b chore: refresh vendor snapshot [skip ci] git-subtree-dir: astrbot-sdk git-subtree-split: 3cf8142fb3dde637755495d62e1a0525887e87cf
1 parent 0bade43 commit a27635c

26 files changed

Lines changed: 657 additions & 212 deletions

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "hatchling.build"
55
[project]
66
name = "astrbot-sdk"
77
version = "0.1.0"
8-
description = "AstrBot SDK with v4 runtime, worker protocol, and plugin tooling"
8+
description = "AstrBot SDK with s5r runtime, worker protocol, and plugin tooling"
99
readme = "README.md"
1010
requires-python = ">=3.12"
1111
classifiers = [

src/astrbot_sdk/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""AstrBot SDK 的顶层公共 API。
22
3-
这里仅重新导出 v4 推荐直接导入的稳定入口。
3+
这里仅重新导出 astrbot-sdk 推荐直接导入的稳定入口。
44
55
新插件应直接使用此模块的导出:
66
from astrbot_sdk import Star, Context, MessageEvent
77
from astrbot_sdk.decorators import on_command, on_message
88
9-
迁移期适配入口位于独立模块;此处只暴露 v4 原生主入口。
9+
迁移期适配入口位于独立模块;此处只暴露 astrbot-sdk 原生主入口。
1010
"""
1111

1212
from .clients.managers import (

src/astrbot_sdk/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ def _render_init_readme(*, plugin_name: str) -> str:
719719
f"""\
720720
# {plugin_name}
721721
722-
一个最小可运行的 AstrBot SDK v4 插件。
722+
一个最小可运行的 AstrBot SDK 插件。
723723
724724
## 目录结构
725725

src/astrbot_sdk/clients/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""原生v4能力客户端
1+
"""原生 astrbot-sdk 能力客户端
22
33
这些客户端为 Context 提供了用于调用远程能力的狭窄且具类型化 (typed) 的接口。
44
它们负责处理能力名称、载荷格式化(payload shaping)以及结果解码,且不会暴露协议或传输层的具体细节。

src/astrbot_sdk/clients/_errors.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from __future__ import annotations
2+
3+
from ..errors import AstrBotError
4+
5+
6+
def client_call_label(
7+
client_name: str,
8+
method_name: str,
9+
details: str | None = None,
10+
) -> str:
11+
label = f"{client_name}.{method_name}"
12+
if details:
13+
return f"{label} ({details})"
14+
return label
15+
16+
17+
def wrap_client_exception(
18+
*,
19+
client_name: str,
20+
method_name: str,
21+
exc: Exception,
22+
details: str | None = None,
23+
) -> Exception:
24+
message = f"{client_call_label(client_name, method_name, details)} failed: {exc}"
25+
if isinstance(exc, AstrBotError):
26+
return AstrBotError(
27+
code=exc.code,
28+
message=message,
29+
hint=exc.hint,
30+
retryable=exc.retryable,
31+
docs_url=exc.docs_url,
32+
details=exc.details,
33+
)
34+
try:
35+
rebuilt = exc.__class__(message)
36+
except Exception:
37+
return RuntimeError(message)
38+
if isinstance(rebuilt, Exception):
39+
return rebuilt
40+
return RuntimeError(message)
41+
42+
43+
__all__ = ["client_call_label", "wrap_client_exception"]

src/astrbot_sdk/clients/http.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ async def handle_http_request(request_id: str, payload: dict, cancel_token):
4141

4242
from ..decorators import get_capability_meta
4343
from ..errors import AstrBotError
44+
from ._errors import wrap_client_exception
4445
from ._proxy import CapabilityProxy
4546

4647

@@ -116,16 +117,23 @@ async def register_api(
116117
if methods is None:
117118
methods = ["GET"]
118119
resolved_handler = _resolve_handler_capability(handler_capability, handler)
119-
120-
await self._proxy.call(
121-
"http.register_api",
122-
{
123-
"route": route,
124-
"methods": methods,
125-
"handler_capability": resolved_handler,
126-
"description": description,
127-
},
128-
)
120+
try:
121+
await self._proxy.call(
122+
"http.register_api",
123+
{
124+
"route": route,
125+
"methods": methods,
126+
"handler_capability": resolved_handler,
127+
"description": description,
128+
},
129+
)
130+
except Exception as exc:
131+
raise wrap_client_exception(
132+
client_name="HTTPClient",
133+
method_name="register_api",
134+
details=f"route={route!r}, methods={list(methods)!r}",
135+
exc=exc,
136+
) from exc
129137

130138
async def unregister_api(
131139
self, route: str, methods: list[str] | None = None
@@ -141,11 +149,18 @@ async def unregister_api(
141149
"""
142150
if methods is None:
143151
methods = []
144-
145-
await self._proxy.call(
146-
"http.unregister_api",
147-
{"route": route, "methods": methods},
148-
)
152+
try:
153+
await self._proxy.call(
154+
"http.unregister_api",
155+
{"route": route, "methods": methods},
156+
)
157+
except Exception as exc:
158+
raise wrap_client_exception(
159+
client_name="HTTPClient",
160+
method_name="unregister_api",
161+
details=f"route={route!r}, methods={list(methods)!r}",
162+
exc=exc,
163+
) from exc
149164

150165
async def list_apis(self) -> list[dict[str, Any]]:
151166
"""列出当前插件注册的所有 API。
@@ -158,8 +173,15 @@ async def list_apis(self) -> list[dict[str, Any]]:
158173
for api in apis:
159174
print(f"{api['route']}: {api['methods']}")
160175
"""
161-
output = await self._proxy.call(
162-
"http.list_apis",
163-
{},
164-
)
176+
try:
177+
output = await self._proxy.call(
178+
"http.list_apis",
179+
{},
180+
)
181+
except Exception as exc:
182+
raise wrap_client_exception(
183+
client_name="HTTPClient",
184+
method_name="list_apis",
185+
exc=exc,
186+
) from exc
165187
return output.get("apis", [])

src/astrbot_sdk/clients/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""大语言模型客户端模块。
22
3-
提供 v4 原生的 LLM 能力调用接口。
3+
提供 astrbot-sdk 原生的 LLM 能力调用接口。
44
55
设计边界:
66
- `chat()` 是便捷文本接口,返回最终文本

0 commit comments

Comments
 (0)