Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`。
Expand Down
2 changes: 1 addition & 1 deletion packages/automas_script_hsr/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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}",
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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}"),
)
Original file line number Diff line number Diff line change
Expand Up @@ -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 插件随包分发的邮件模板。"""
Expand Down Expand Up @@ -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
)
):
Expand All @@ -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", "")
Comment thread
qiyinxi marked this conversation as resolved.
Outdated
)

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 == "统计信息":
Expand All @@ -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 (
Expand Down
6 changes: 5 additions & 1 deletion tests/test_package_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
},
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading