From 5b0a841cc65678fb84b944ca7bbb90d17249d3aa Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sun, 9 Aug 2026 13:41:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(hsr):=20=E5=85=BC=E5=AE=B9=20Config=20V?= =?UTF-8?q?2=20=E5=AE=BF=E4=B8=BB=E5=A5=91=E7=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- packages/automas_script_hsr/pyproject.toml | 2 +- .../automas_script_hsr/runtime/autoproxy.py | 24 ++++-- .../src/automas_script_hsr/runtime/manager.py | 27 ++++-- .../src/automas_script_hsr/runtime/notify.py | 82 +++++++++++++++---- tests/test_package_metadata.py | 6 +- uv.lock | 2 +- 7 files changed, 111 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6bf433..fe5cb31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ ### 发行 -- `automas-script-hsr` 升级到 `0.1.8`;`automas-hsr-adapter-sra`、 +- `automas-script-hsr` 升级到 `0.1.9`;`automas-hsr-adapter-sra`、 `automas-hsr-adapter-m7a` 与一键安装元包 `automas-hsr` 升级到 `0.1.9`。 - 适配器最低依赖提升为 core `0.1.8`;元包最低版本锁定为 core `0.1.8`、 SRA/M7A `0.1.9`。 diff --git a/packages/automas_script_hsr/pyproject.toml b/packages/automas_script_hsr/pyproject.toml index 4be01bb..dca1d24 100644 --- a/packages/automas_script_hsr/pyproject.toml +++ b/packages/automas_script_hsr/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "automas-script-hsr" -version = "0.1.8" +version = "0.1.9" description = "HSR orchestration and adapter contracts for AUTO-MAS" readme = { file = "README.md", content-type = "text/markdown" } requires-python = ">=3.12" diff --git a/packages/automas_script_hsr/src/automas_script_hsr/runtime/autoproxy.py b/packages/automas_script_hsr/src/automas_script_hsr/runtime/autoproxy.py index c36ebcf..3cf8baf 100644 --- a/packages/automas_script_hsr/src/automas_script_hsr/runtime/autoproxy.py +++ b/packages/automas_script_hsr/src/automas_script_hsr/runtime/autoproxy.py @@ -478,10 +478,16 @@ async def _push_user_statistics_notification(self) -> None: ) except Exception as e: logger.exception(f"推送 HSR 用户统计通知时出现异常: {e}") - await Config.send_websocket_message( + from app.core.ws import Publisher, protocol + from app.models.schema import WSTaskNoticeData + + await Publisher.send( id=self.task_info.task_id, - type="Info", - data={"Error": f"推送 HSR 用户统计通知时出现异常: {e}"}, + type=protocol.TASK_NOTICE, + data=WSTaskNoticeData( + level="error", + message=f"推送 HSR 用户统计通知时出现异常: {e}", + ), ) def _queue_eow_completion_if_confirmed( @@ -1799,8 +1805,14 @@ async def on_crash(self, e: Exception): self.error_message = str(e) self._mark_current_user_abnormal(f"HSR 用户任务异常: {e}") logger.exception(f"HSR 用户「{self.cur_user_item.name}」任务出现异常:{e}") - await Config.send_websocket_message( + from app.core.ws import Publisher, protocol + from app.models.schema import WSTaskNoticeData + + await Publisher.send( id=self.task_info.task_id, - type="Info", - data={"Error": f"HSR 用户「{self.cur_user_item.name}」任务出现异常:{e}"}, + type=protocol.TASK_NOTICE, + data=WSTaskNoticeData( + level="error", + message=f"HSR 用户「{self.cur_user_item.name}」任务出现异常:{e}", + ), ) diff --git a/packages/automas_script_hsr/src/automas_script_hsr/runtime/manager.py b/packages/automas_script_hsr/src/automas_script_hsr/runtime/manager.py index 8f8463c..eb3aa46 100644 --- a/packages/automas_script_hsr/src/automas_script_hsr/runtime/manager.py +++ b/packages/automas_script_hsr/src/automas_script_hsr/runtime/manager.py @@ -572,10 +572,13 @@ async def main_task(self): if self.check_result != "Pass": logger.error(f"HSR 配置检查未通过:{self.check_result}") self._append_log(f"HSR 配置检查未通过:{self.check_result}") - await Config.send_websocket_message( + from app.core.ws import Publisher, protocol + from app.models.schema import WSTaskNoticeData + + await Publisher.send( id=self.task_info.task_id, - type="Info", - data={"Error": self.check_result}, + type=protocol.TASK_NOTICE, + data=WSTaskNoticeData(level="error", message=self.check_result), ) return @@ -788,10 +791,13 @@ async def _send_notification_error(self, message: str) -> None: """通知失败时尽量提示前端;提示失败不影响任务收尾。""" try: - await Config.send_websocket_message( + from app.core.ws import Publisher, protocol + from app.models.schema import WSTaskNoticeData + + await Publisher.send( id=self.task_info.task_id, - type="Info", - data={"Error": message}, + type=protocol.TASK_NOTICE, + data=WSTaskNoticeData(level="error", message=message), ) except Exception as e: # noqa: BLE001 logger.warning(f"发送 HSR 通知错误提示失败:{e}") @@ -906,8 +912,11 @@ async def on_crash(self, e: Exception): self.script_info.status = "异常" logger.exception(f"HSR 任务出现异常:{e}") self._append_log(f"HSR 任务出现异常:{e}") - await Config.send_websocket_message( + from app.core.ws import Publisher, protocol + from app.models.schema import WSTaskNoticeData + + await Publisher.send( id=self.task_info.task_id, - type="Info", - data={"Error": f"HSR 任务出现异常:{e}"}, + type=protocol.TASK_NOTICE, + data=WSTaskNoticeData(level="error", message=f"HSR 任务出现异常:{e}"), ) diff --git a/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py b/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py index f63489a..a8eee26 100644 --- a/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py +++ b/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py @@ -35,6 +35,58 @@ autoescape=select_autoescape(("html", "xml")), ) +_NOTIFY_V2_FIELDS = { + "SendTaskResultTime": "send_task_result_time", + "IfSendStatistic": "if_send_statistic", + "IfSendMail": "if_send_mail", + "ToAddress": "to_address", + "IfServerChan": "if_server_chan", + "ServerChanKey": "server_chan_key", + "IfKoishiSupport": "if_koishi_support", +} + + +def _global_notify_value(key: str, default: Any = None) -> Any: + """Read Config V2 notification fields, then the legacy Config wire.""" + + setting = getattr(Config, "setting", None) + notify = getattr(setting, "notify", None) + v2_name = _NOTIFY_V2_FIELDS.get(key) + if notify is not None and v2_name is not None: + value = getattr(notify, v2_name, None) + if value is not None: + return value + + getter = getattr(Config, "get", None) + if callable(getter): + try: + return getter("Notify", key) + except Exception: + pass + return default + + +def _global_custom_webhooks() -> list[Any]: + """Return Config V2 custom webhooks, with a legacy collection fallback.""" + + setting = getattr(Config, "setting", None) + collection = getattr(setting, "custom_webhooks", None) + values = getattr(collection, "values", None) + if callable(values): + try: + return list(values()) + except Exception: + pass + + legacy_collection = getattr(Config, "Notify_CustomWebhooks", None) + values = getattr(legacy_collection, "values", None) + if callable(values): + try: + return list(values()) + except Exception: + pass + return [] + def render_hsr_mail_template(name: str, context: dict[str, Any]) -> str: """渲染 HSR 插件随包分发的邮件模板。""" @@ -67,9 +119,9 @@ async def push_notification( logger.info(f"开始推送通知, 模式: {mode}, 标题: {title}") if mode == "代理结果" and ( - Config.get("Notify", "SendTaskResultTime") == "任何时刻" + _global_notify_value("SendTaskResultTime") == "任何时刻" or ( - Config.get("Notify", "SendTaskResultTime") == "仅失败时" + _global_notify_value("SendTaskResultTime") == "仅失败时" and message["uncompleted_count"] != 0 ) ): @@ -81,22 +133,22 @@ async def push_notification( message_html = render_hsr_mail_template("result.html", message) serverchan_message = message_text.replace("\n", "\n\n") - if Config.get("Notify", "IfSendMail"): + if _global_notify_value("IfSendMail"): await Notify.send_mail( - "网页", title, message_html, Config.get("Notify", "ToAddress") + "网页", title, message_html, _global_notify_value("ToAddress", "") ) - if Config.get("Notify", "IfServerChan"): + if _global_notify_value("IfServerChan"): await Notify.ServerChanPush( title, f"{serverchan_message}\n\nAUTO-MAS 敬上", - Config.get("Notify", "ServerChanKey"), + _global_notify_value("ServerChanKey", ""), ) - for webhook in Config.Notify_CustomWebhooks.values(): + for webhook in _global_custom_webhooks(): await Notify.WebhookPush(title, f"{message_text}\n\nAUTO-MAS 敬上", webhook) - if Config.get("Notify", "IfKoishiSupport"): + if _global_notify_value("IfKoishiSupport"): await Notify.send_koishi(f"{title}\n\n{message_text}\n\nAUTO-MAS 敬上") elif mode == "统计信息": @@ -108,25 +160,25 @@ async def push_notification( message_html = render_hsr_mail_template("statistics.html", message) serverchan_message = message_text.replace("\n", "\n\n") - if Config.get("Notify", "IfSendStatistic"): - if Config.get("Notify", "IfSendMail"): + if _global_notify_value("IfSendStatistic"): + if _global_notify_value("IfSendMail"): await Notify.send_mail( - "网页", title, message_html, Config.get("Notify", "ToAddress") + "网页", title, message_html, _global_notify_value("ToAddress", "") ) - if Config.get("Notify", "IfServerChan"): + if _global_notify_value("IfServerChan"): await Notify.ServerChanPush( title, f"{serverchan_message}\n\nAUTO-MAS 敬上", - Config.get("Notify", "ServerChanKey"), + _global_notify_value("ServerChanKey", ""), ) - for webhook in Config.Notify_CustomWebhooks.values(): + for webhook in _global_custom_webhooks(): await Notify.WebhookPush( title, f"{message_text}\n\nAUTO-MAS 敬上", webhook ) - if Config.get("Notify", "IfKoishiSupport"): + if _global_notify_value("IfKoishiSupport"): await Notify.send_koishi(f"{title}\n\n{message_text}\n\nAUTO-MAS 敬上") if ( diff --git a/tests/test_package_metadata.py b/tests/test_package_metadata.py index 6f404b3..a365a6a 100644 --- a/tests/test_package_metadata.py +++ b/tests/test_package_metadata.py @@ -20,7 +20,7 @@ }, "automas_script_hsr": { "name": "automas-script-hsr", - "version": "0.1.8", + "version": "0.1.9", "entry_point": "automas_script_hsr.plugin:Plugin", "dependencies": {"jinja2>=3.1", "pydantic>=2"}, }, @@ -86,6 +86,10 @@ def test_internal_dependency_floors_match_release_matrix(self) -> None: versions = { expected["name"]: expected["version"] for expected in PACKAGES.values() } + # The 0.1.9 core release keeps the public adapter contracts backward + # compatible, so existing adapters may continue declaring a 0.1.8 + # minimum while the core distribution itself advances to 0.1.9. + versions["automas-script-hsr"] = "0.1.8" providers = { "automas_hsr": ( "automas-script-hsr", diff --git a/uv.lock b/uv.lock index 6521ec1..3f85566 100644 --- a/uv.lock +++ b/uv.lock @@ -71,7 +71,7 @@ requires-dist = [{ name = "automas-script-hsr", editable = "packages/automas_scr [[package]] name = "automas-script-hsr" -version = "0.1.8" +version = "0.1.9" source = { editable = "packages/automas_script_hsr" } dependencies = [ { name = "jinja2" }, From e95d07b52897185491b56b77e704e56b6446b1b0 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sun, 9 Aug 2026 13:44:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(hsr):=20=E8=B7=B3=E8=BF=87=E7=BC=BA?= =?UTF-8?q?=E5=B0=91=E5=87=AD=E6=8D=AE=E7=9A=84=E9=80=9A=E7=9F=A5=E6=B8=A0?= =?UTF-8?q?=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/automas_script_hsr/runtime/notify.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py b/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py index a8eee26..c3d3a60 100644 --- a/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py +++ b/packages/automas_script_hsr/src/automas_script_hsr/runtime/notify.py @@ -133,16 +133,16 @@ async def push_notification( message_html = render_hsr_mail_template("result.html", message) serverchan_message = message_text.replace("\n", "\n\n") - if _global_notify_value("IfSendMail"): - await Notify.send_mail( - "网页", title, message_html, _global_notify_value("ToAddress", "") - ) + to_address = _global_notify_value("ToAddress") + if _global_notify_value("IfSendMail") and to_address: + await Notify.send_mail("网页", title, message_html, to_address) - if _global_notify_value("IfServerChan"): + server_chan_key = _global_notify_value("ServerChanKey") + if _global_notify_value("IfServerChan") and server_chan_key: await Notify.ServerChanPush( title, f"{serverchan_message}\n\nAUTO-MAS 敬上", - _global_notify_value("ServerChanKey", ""), + server_chan_key, ) for webhook in _global_custom_webhooks(): @@ -161,16 +161,16 @@ async def push_notification( serverchan_message = message_text.replace("\n", "\n\n") if _global_notify_value("IfSendStatistic"): - if _global_notify_value("IfSendMail"): - await Notify.send_mail( - "网页", title, message_html, _global_notify_value("ToAddress", "") - ) + to_address = _global_notify_value("ToAddress") + if _global_notify_value("IfSendMail") and to_address: + await Notify.send_mail("网页", title, message_html, to_address) - if _global_notify_value("IfServerChan"): + server_chan_key = _global_notify_value("ServerChanKey") + if _global_notify_value("IfServerChan") and server_chan_key: await Notify.ServerChanPush( title, f"{serverchan_message}\n\nAUTO-MAS 敬上", - _global_notify_value("ServerChanKey", ""), + server_chan_key, ) for webhook in _global_custom_webhooks():