JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(tui): 定时任务结果通知删除cron标签
kdgmpw1918ff748933
diff --git a/docs/en/ScheduledTasks.md b/docs/en/ScheduledTasks.mdindex c02f4a2604..7305ee582d 100644--- a/docs/en/ScheduledTasks.md+++ b/docs/en/ScheduledTasks.md@@ -123,8 +123,21 @@ Optional **`timeout_seconds`** (60–259200) overrides the per-run timeout:**Result push**-- With `targets=tui`, the final `[cron] <job name> result: …` is still **broadcast** to all connected TUI windows when the run completes.-- If no TUI is connected at completion time, the broadcast may be missed; keep the gateway running or inspect the job via `/cron show`.+All `targets` channels (`web`, `tui`, Feishu, DingTalk, WeCom, etc.) share the same `_push_to_targets` delivery path and body formatting:++| Scenario | Push body format |+|---|---|+| **Successful completion** | `{job name} result:\n\n{agent output}` (no `[cron]` prefix) |+| **Failure / timeout / no valid report** | Status text starting with `[cron]` (e.g. `[cron] 任务执行失败: …`), not wrapped as `{job name} result:` |+| **In-progress placeholder** | `{job name} 正在执行中,结果稍后补发(push_at=…)` |++Channel differences are mainly **routing**, not body format:++- **`web`**: delivered to the Web chat panel; placeholders can be replaced by final results via `payload.cron.is_placeholder` and `run_id`.+- **`tui`**: intentionally omits `session_id`; Gateway **broadcasts** to every connected TUI window. Broadcast may be missed if no TUI is online at completion time.+- **Feishu / DingTalk / WeCom and other IM channels**: route via the job’s bound `session_id` and `metadata`; group-created jobs may use IMOutboundPipeline routing.++Keep the gateway running, or inspect jobs via `/cron show` or the Web Cron panel.**Team completion detection**diff --git "a/docs/zh/\345\256\232\346\227\266\344\273\273\345\212\241.md" "b/docs/zh/\345\256\232\346\227\266\344\273\273\345\212\241.md"index c5323aa0eb..22bd3e2f6d 100644--- "a/docs/zh/\345\256\232\346\227\266\344\273\273\345\212\241.md"+++ "b/docs/zh/\345\256\232\346\227\266\344\273\273\345\212\241.md"@@ -124,8 +124,21 @@ TUI / Web 创建任务时可传 `mode=`;前端通过 `cron.job.meta` 拉取当**结果推送**-- `targets=tui` 时,任务**结束后**仍按既有规则 **广播** 最终 `[cron] 任务名 result: …` 到所有已连接 TUI 窗口(不绑 session)。-- 若完成瞬间没有任何 TUI 在线,广播可能丢失;请保持 Gateway 运行或稍后通过 `/cron show` 查看任务配置。+所有 `targets`(`web`、`tui`、飞书、钉钉、企业微信等)共用同一套 `_push_to_targets` 推送逻辑,正文格式如下:++| 场景 | 推送正文格式 |+|---|---|+| **正常完成** | `{任务名} result:\n\n{Agent 返回正文}`(无 `[cron]` 前缀) |+| **执行失败 / 超时 / 无有效报告等** | 以 `[cron]` 开头的状态文案(如 `[cron] 任务执行失败: …`),不再套 `{任务名} result:` 包装 |+| **执行中占位** | `{任务名} 正在执行中,结果稍后补发(push_at=…)` |++各频道差异主要在 **路由**,不在正文格式:++- **`web`**:推到 Web 聊天面板;占位消息可通过 `payload.cron.is_placeholder` 与正式结果关联替换。+- **`tui`**:故意不绑定 `session_id`,Gateway **广播**到所有已连接 TUI 窗口;若完成瞬间无 TUI 在线,广播可能丢失。+- **飞书 / 钉钉 / 企业微信等 IM**:优先使用创建任务时绑定的 `session_id` 与 `metadata` 路由到对应会话;群聊创建的任务可走 IMOutboundPipeline 路由决策。++请保持 Gateway 运行;也可通过 `/cron show` 或 Web 定时任务面板查看任务配置。**结束判定(Team)**diff --git a/jiuwenswarm/channels/web/frontend/src/hooks/useWebSocket.ts b/jiuwenswarm/channels/web/frontend/src/hooks/useWebSocket.tsindex b8417dd16e..f182bf380e 100644--- a/jiuwenswarm/channels/web/frontend/src/hooks/useWebSocket.ts+++ b/jiuwenswarm/channels/web/frontend/src/hooks/useWebSocket.ts@@ -1456,9 +1456,12 @@ export function useWebSocket(options: UseWebSocketOptions): UseWebSocketReturn {const cronMeta = payload.cron as Record<string, unknown> | undefined;const cronRunId =typeof cronMeta?.run_id === 'string' ? cronMeta.run_id.trim() : '';- const isCronPlaceholderContent = /^\[cron\].*正在执行中/.test(content);+ const isCronPlaceholderContent =+ cronMeta?.is_placeholder === true ||+ /正在执行中,结果稍后补发/.test(content) ||+ /^\[cron\].*正在执行中/.test(content);- // 正式结果:替换同 run_id 的占位气泡,或最近的 [cron]…正在执行中…+ // 正式结果:替换同 run_id 的占位气泡,或最近的定时任务「正在执行中」占位if (!isCronPlaceholderContent) {let placeholderId: string | null = null;if (cronRunId) {@@ -1469,7 +1472,10 @@ export function useWebSocket(options: UseWebSocketOptions): UseWebSocketReturn {for (let i = messages.length - 1; i >= 0; i -= 1) {const msg = messages[i];if (msg.role !== 'assistant' || typeof msg.content !== 'string') continue;- if (/^\[cron\].*正在执行中/.test(msg.content)) {+ if (+ /正在执行中,结果稍后补发/.test(msg.content) ||+ /^\[cron\].*正在执行中/.test(msg.content)+ ) {placeholderId = msg.id;break;}diff --git a/jiuwenswarm/gateway/cron/scheduler.py b/jiuwenswarm/gateway/cron/scheduler.pyindex f1c99a12a0..5bb92a15cd 100644--- a/jiuwenswarm/gateway/cron/scheduler.py+++ b/jiuwenswarm/gateway/cron/scheduler.py@@ -208,7 +208,7 @@ def _format_cron_broadcast_text(*, job_name: str, text: str, is_placeholder: booif is_placeholder or body.startswith("[cron]"):return bodyname = str(job_name or "").strip() or "cron"- return f"[cron] {name} result:\n\n{body}"+ return f"{name} result:\n\n{body}"def _cron_next_push_dt(cron_expr: str, base_dt: datetime) -> datetime:@@ -955,7 +955,7 @@ class CronSchedulerService:return# Not ready: send placeholder- placeholder = f"[cron] {job.name} 正在执行中,结果稍后补发(push_at={state.push_at_iso})"+ placeholder = f"{job.name} 正在执行中,结果稍后补发(push_at={state.push_at_iso})"await self._push_to_targets(job, state, text=placeholder, is_placeholder=True)state.placeholder_sent = Truediff --git a/tests/unit_tests/gateway/test_cron_scheduler.py b/tests/unit_tests/gateway/test_cron_scheduler.pyindex dcf5a2609b..c7ab2c39ac 100644--- a/tests/unit_tests/gateway/test_cron_scheduler.py+++ b/tests/unit_tests/gateway/test_cron_scheduler.py@@ -459,7 +459,7 @@ class TestHandleEventStoreValidation:# push_update delivered successfullyassert len(handler.published) == 1content = _cron_published_content(handler.published[0])- assert content == f"[cron] {job.name} result:\n\nresult: 9am now"+ assert content == f"{job.name} result:\n\nresult: 9am now"@pytest.mark.asyncioasync def test_wake_executes_normally_when_job_present(self, tmp_path):@@ -1297,11 +1297,11 @@ class TestCronBroadcastText:job_name="agent-core-commit-review",text="## 审查完成",is_placeholder=False,- ) == "[cron] agent-core-commit-review result:\n\n## 审查完成"+ ) == "agent-core-commit-review result:\n\n## 审查完成"@staticmethod- def test_keeps_existing_placeholder_and_cron_prefix():- placeholder = "[cron] agent-core-commit-review 正在执行中"+ def test_keeps_placeholder_unchanged_and_passes_through_cron_prefixed_status():+ placeholder = "agent-core-commit-review 正在执行中,结果稍后补发(push_at=2026-01-01T09:00:00+08:00)"assert _CronSchedulerTeamTestApi.format_cron_broadcast_text(job_name="agent-core-commit-review",text=placeholder,