from __future__ import annotations from typing import TYPE_CHECKING from xhs_bridge.human_guard import PUBLISH_TOOLS from xhs_bridge.publish_history_store import PublishHistoryStore if TYPE_CHECKING: from xhs_bridge.gui.app_context import AppContext def process_due_scheduled(ctx: AppContext) -> None: """检查并执行到期的定时发布(每次最多一条)。""" if ctx.is_task_running(): return store = PublishHistoryStore() due = store.list_due_scheduled() if not due: return rec = due[0] if rec.tool_name not in PUBLISH_TOOLS: store.mark_published(rec.id, "未知发布类型", success=False) return status = ctx.service().status() if not status.get("engine_running"): return if not store.mark_publishing(rec.id): return arguments = dict(rec.arguments or {}) record_id = rec.id tool_name = rec.tool_name def task(): return ctx.service().call_tool( tool_name, arguments, check_license=True, on_progress=None, record_history=False, ) def on_ok(result: object) -> None: text = str(result) ok = "❌" not in text and "失败" not in text store.mark_published(record_id, text, success=ok) def on_err(msg: str) -> None: store.mark_published(record_id, msg, success=False) ctx.run_task( task, on_ok, on_err=on_err, title=f"定时发布 — {tool_name}", request={"record_id": record_id, "arguments": arguments}, log_response=True, set_busy=False, block_if_busy=False, )