Skip to content

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

docs(heart-beat): 心跳文档中英文重构

xiaoxiawang3cfafec71ce7
diff --git a/docs/assets/images/heartbeat6.png b/docs/assets/images/heartbeat6.png
new file mode 100644
index 0000000000..90e91440c7
Binary files /dev/null and b/docs/assets/images/heartbeat6.png differ
diff --git a/docs/en/Heartbeat.md b/docs/en/Heartbeat.md
index 618f73689b..a336067488 100644
--- a/docs/en/Heartbeat.md
+++ b/docs/en/Heartbeat.md
@@ -1,22 +1,71 @@
# Heartbeat
-Heartbeat is jiuwenSwarm's periodic liveness probe and task runner. The gateway sends requests to AgentServer at a fixed interval to verify connectivity and agent availability. If **`workspace/HEARTBEAT.md`** is configured, the agent can also run predefined tasks on each beat. Results can be relayed to a chosen channel (default: web).
+[简体中文](../zh/心跳.md)
---
-## 1. Overview
+## Concept Overview
-- **Liveness**: Sends requests to AgentServer on an interval to confirm the service is healthy.
-- **Optional tasks**: If `workspace/HEARTBEAT.md` exists at the project root, the agent reads and executes the active task items in order and returns results. Without this file or empty tasks, only `HEARTBEAT_OK` is returned.
-- **Result relay**: Heartbeat responses can be forwarded to a configured channel (e.g., web UI) for visibility of the latest heartbeat status and content.
+### What is Heartbeat
+
+Heartbeat is a periodic liveness probe sent by the Gateway to AgentServer at fixed intervals, used to verify connectivity and agent availability. If **`workspace/HEARTBEAT.md`** is configured, it can also drive the Agent to periodically execute tasks listed in the file. Results can be relayed to a chosen channel (default: web).
+
+### Heartbeat vs Scheduled Tasks
+
+| Feature | Heartbeat | Scheduled Tasks |
+|---------|-----------|-----------------|
+| **Trigger Mode** | Fixed time interval (seconds), e.g., hourly | Cron expression with complex scheduling rules |
+| **Initiator** | Gateway actively sends probe requests to AgentServer | Independent Scheduler module |
+| **Task Definition** | Task list defined in `workspace/HEARTBEAT.md` file | Tasks created via web UI with query, interval_hours, etc. |
+| **Execution Mechanism** | HEARTBEAT.md content injected into chat request, following normal conversation flow | Independent execution sessions with history and status management |
+| **Primary Use** | Service liveness, periodic task execution, status monitoring | Schedule specific tasks with one-time/periodic execution |
+| **Result Relay** | Pushed to specified Channel via `heartbeat.relay` event | Stored in task execution history, viewable in web UI |
+
+### Heartbeat Workflow
+
+1. **Startup**: Gateway initializes `GatewayHeartbeatService` at startup, creating a periodic task based on configured `interval_seconds`.
+
+2. **Periodic Scheduling**: After startup, the service enters a main loop, executing a heartbeat (`_tick`) every `interval_seconds`.
+
+3. **Time Check**: Before executing the heartbeat, the service checks if the current time falls within the `active_hours` window; if not, the heartbeat is skipped.
+
+4. **Request Construction**: Constructs an E2A protocol request containing heartbeat identification and HEARTBEAT.md read instructions, sending it to AgentServer.
+
+5. **Agent Processing**: AgentServer identifies the heartbeat request, reads `workspace/HEARTBEAT.md` content, injects the task list into the query, and executes tasks following the normal conversation flow.
+
+6. **Result Relay**: After execution, if `relay_channel_id` is configured (e.g., `web`), the heartbeat response is pushed to the frontend via the `heartbeat.relay` event, updating heartbeat status and history.
+
+```
+Gateway ──(E2A heartbeat request)──→ AgentServer ──(read HEARTBEAT.md)──→ Agent ──(execute tasks)──→ Return results
+ │ │
+ └────────────────────────────────(heartbeat.relay event)────────────────────────────────────────┘
+```
---
-## 2. Configuration
+## Configuration
+
+Three configuration methods are available: web UI, config file, or environment variables.
+
+### 1. Web UI Heartbeat Panel
+
+Open **Heartbeat** in the left sidebar to view and modify heartbeat configuration:
+
+![](../assets/images/heartbeat1.png)
-Three configuration methods are available: config file, environment variables, or web UI.
+**Configuration Options:**
-### 2.1 Config file `config/config.yaml`
+| Option | Description | Default |
+|--------|-------------|---------|
+| **Interval** | Heartbeat interval in seconds, must be > 0 | 3600 |
+| **Relay Target** | Channel for relaying heartbeat results, typically `web` | web |
+| **Active Window** | Heartbeat only fires within this time range (`HH:MM` 24-hour format) | Always active if not configured |
+
+After modifying configuration, click save to write back to `config/config.yaml` and automatically restart the heartbeat service.
+
+![](../assets/images/heartbeat2.png)
+
+### 2. Config file `config/config.yaml`
Configure the `heartbeat` section in `config/config.yaml`:
@@ -38,7 +87,7 @@ heartbeat:
| `target` | Relay channel | Usually `web`, which pushes heartbeat responses to the web UI; empty or omitted = no relay |
| `active_hours` | Active window | `start`/`end` in `HH:MM` (24-hour format). Heartbeat only fires within `[start, end]`. Supports cross-midnight windows (e.g., 22:00–06:00). |
-### 2.2 Environment variables (override YAML)
+### 3. Environment variables (override YAML)
| Variable | Meaning | Example |
|----------|---------|---------|
@@ -48,42 +97,40 @@ heartbeat:
Environment variables take precedence over the `heartbeat` section in `config/config.yaml`.
-### 2.3 Web UI Heartbeat panel
+---
-Open **Heartbeat** in the left sidebar:
+## Viewing Heartbeat
+
+### Heartbeat History
+
+View the last 20 heartbeat records in the Heartbeat panel, including status (normal / warning), content, and timestamps.
-- View current heartbeat configuration (interval, relay target, active window)
-![](../assets/images/heartbeat1.png)
-- Edit and save configuration (writes `config/config.yaml` and restarts the heartbeat service)
-![](../assets/images/heartbeat2.png)
-- View the last 20 heartbeat records, including status (normal / warning), content, and timestamps
![](../assets/images/heartbeat3.png)
----
+### Popup Notifications
-## 3. HEARTBEAT.md and periodic tasks
+When `target` is set to `web`, each heartbeat response is pushed to the frontend via the `heartbeat.relay` event. If content is not `HEARTBEAT_OK`, a popup notification appears for viewing task results or error information.
-### 3.1 File location and role
+![](../assets/images/heartbeat4.png)
-- **Path**: In the web UI, this file appears on the right side of the Heartbeat panel. Click `Edit` to modify it.
-![](../assets/images/heartbeat5.png)
-- **Role**: If the file exists and contains tasks, each heartbeat reads this content, executes tasks in order, and returns results. Without this file or empty tasks, only `HEARTBEAT_OK` is returned.
+---
-### 3.2 Agent behavior
+## Examples
-The server reads `HEARTBEAT.md`, parses the task list, and sends a chat request to the agent following the normal conversation flow. If parsing fails or the task list is empty, `HEARTBEAT_OK` is returned directly. Otherwise, tasks are executed and responses returned.
+### Example: Hello World
----
+Add the following content to `HEARTBEAT.md`:
-## 4. Web UI display and events
+```markdown
+Please output "Hello Heartbeat!"
+```
-- **Status**: The Heartbeat panel and toolbar display the latest heartbeat info (e.g., `HEARTBEAT_OK`) and timestamp.
-- **Events**: When `target` is `web`, each heartbeat response is pushed to the frontend via the `heartbeat.relay` event for status and history updates. If content is not `HEARTBEAT_OK`, a popup notification appears for viewing task results or errors.
-![](../assets/images/heartbeat4.png)
+When heartbeat executes, the Agent reads this content and outputs `Hello Heartbeat!`. Results are relayed to the frontend via the `heartbeat.relay` event.
+![Execute heartbeat task](../assets/images/heartbeat6.png)
---
-## 5. FAQ
+## FAQ
**Q: I changed the heartbeat section in `config/config.yaml` but nothing happened.**
A: Config is read at startup. If you use the web UI Heartbeat panel, it rewrites YAML and automatically restarts the heartbeat service. If you edit YAML directly, restart the entire application (e.g., `jiuwenswarm-web`) for changes to take effect.
@@ -99,14 +146,20 @@ A: At the DeepAgent workspace root: `~/.jiuwenswarm/agent/workspace/HEARTBEAT.md
---
-## 6. Code and config index
+## Mechanism Introduction and Key Code
+
+### Agent Behavior Under Heartbeat
+
+The server reads `HEARTBEAT.md`, parses the task list, and sends a chat request to the agent following the normal conversation flow. If parsing fails or the task list is empty, `HEARTBEAT_OK` is returned directly. Otherwise, tasks are executed and responses returned. Heartbeat results are pushed to the frontend via the `heartbeat.relay` event for status updates and history logging; if content is not `HEARTBEAT_OK`, a popup notification is displayed.
-- Service: `jiuwenswarm/gateway/heartbeat/heartbeat.py`.
-- Config reading and writing: `jiuwenswarm/common/config.py` (`update_heartbeat_in_config`); `app.py` builds `HeartbeatConfig` from the `heartbeat` section in `~/.jiuwenswarm/config/config.yaml` and environment variables at startup.
-- Agent-side HEARTBEAT.md handling: `jiuwenswarm/server/runtime/agent_adapter/interface_deep.py` detects heartbeat sessions and reads `HEARTBEAT.md` to trigger tasks.
-- Frontend: `jiuwenswarm/channels/web/frontend/src/components/HeartbeatPanel/`, `heartbeat.get_conf` / `heartbeat.set_conf`, `heartbeat.relay` events.
+### Code and Config Index
-*Document version: v1.0*
-*Audience: jiuwenSwarm users*
-*Last updated: 2026-06-25*
-*Simplified Chinese: [心跳](../zh/心跳.md)*
\ No newline at end of file
+| Code Path | Function |
+|-----------|----------|
+| `jiuwenswarm/gateway/heartbeat/heartbeat.py` | Heartbeat service implementation, including periodic scheduling and E2A request construction |
+| `jiuwenswarm/common/config.py` | Config reading and writing, `update_heartbeat_in_config` function |
+| `jiuwenswarm/app.py` | Builds `HeartbeatConfig` from config file and environment variables at startup |
+| `jiuwenswarm/server/runtime/agent_adapter/interface_deep.py` | Agent-side HEARTBEAT.md handling, detects heartbeat sessions and triggers tasks |
+| `jiuwenswarm/channels/web/frontend/src/components/HeartbeatPanel/` | Frontend heartbeat panel components |
+| `heartbeat.get_conf` / `heartbeat.set_conf` | Frontend config read/write API |
+| `heartbeat.relay` | Heartbeat response relay event |
\ No newline at end of file
diff --git "a/docs/zh/\345\277\203\350\267\263.md" "b/docs/zh/\345\277\203\350\267\263.md"
index ca95df44f2..73afac46ed 100644
--- "a/docs/zh/\345\277\203\350\267\263.md"
+++ "b/docs/zh/\345\277\203\350\267\263.md"
@@ -1,22 +1,63 @@
-# Heartbeat 心跳功能使用说明
+# 心跳
-Heartbeat(心跳)是 网关按固定间隔向 AgentServer 发送的探活请求,用于检测连接与 Agent 是否可用;若配置了 `workspace/HEARTBEAT.md`,还可驱动 Agent 周期性执行其中列出的任务。并且支持用户选择频道返回 Agent 执行的结果(默认web)。
+[英文文档](../en/Heartbeat.md)
---
-## 1. 功能概述
+## 概念科普
-- **探活**:每隔一定时间向 AgentServer 发一次请求,确认服务正常。
-- **可选任务**:若项目根下存在 `workspace/HEARTBEAT.md`,Agent 会读取其中的「活跃的任务项」,按顺序执行并将结果返回;若无该文件或无任务,则仅返回 `HEARTBEAT_OK`。
-- **结果回传**:心跳响应可通过配置回传到指定 Channel(如 Web 前端),便于在界面上查看最近一次心跳状态与内容。
+### 什么是心跳机制
+
+Heartbeat(心跳)是网关按固定间隔向 AgentServer 发送的探活请求,用于检测连接与 Agent 是否可用;若配置了 `workspace/HEARTBEAT.md`,还可驱动 Agent 周期性执行其中列出的任务。并且支持用户选择频道返回 Agent 执行的结果(默认 web)。
+
+### 心跳与定时任务的对比
+
+| 特性 | 心跳 | 定时任务 |
+|------|------|----------|
+| **触发方式** | 固定时间间隔(秒),如每小时一次 | Cron 表达式,支持复杂调度规则 |
+| **发起方** | Gateway 主动向 AgentServer 发送探活请求 | 独立的 Scheduler 模块调度执行 |
+| **任务定义** | 通过 `workspace/HEARTBEAT.md` 文件定义任务列表 | 通过前端创建任务,包含 query、interval_hours 等参数 |
+| **执行机制** | 读取 HEARTBEAT.md 内容注入到 chat 请求中,走正常对话流程 | 创建独立的执行会话,支持执行历史记录和状态管理 |
+| **主要用途** | 服务探活、周期性任务执行、状态检测 | 定时触发特定任务,支持单次/周期性执行 |
+| **结果回传** | 通过 `heartbeat.relay` 事件推送到指定 Channel | 结果存储在任务执行历史中,可在前端查看 |
+
+### 心跳的工作流程
+
+1. **启动阶段**:Gateway 启动时初始化 `GatewayHeartbeatService`,根据配置的 `interval_seconds` 创建周期任务。
+
+2. **周期调度**:服务启动后进入主循环,每隔 `interval_seconds` 秒执行一次探活(`_tick`)。
+
+3. **时间检查**:执行探活前,根据 `active_hours` 配置判断当前时间是否在生效时间段内,不在则跳过本次心跳。
+
+4. **请求构造**:构造 E2A 协议请求,包含心跳标识和 HEARTBEAT.md 的读取指令,发送给 AgentServer。
+
+5. **Agent 处理**:AgentServer 识别到心跳请求后,读取 `workspace/HEARTBEAT.md` 文件内容,将任务列表注入到 query 中,走正常对话流程执行任务。
+
+6. **结果回传**:执行完成后,若配置了 `relay_channel_id`(如 `web`),心跳响应通过 `heartbeat.relay` 事件推送到前端,更新心跳状态和历史记录。
---
-## 2. 配置方式
+## 配置指导
+
+### 1. 前端「心跳」面板
+
+Web 端左侧导航进入 **心跳** 面板,可查看和修改心跳配置:
+
+![](../assets/images/heartbeat1.png)
+
+**配置项说明:**
+
+| 配置项 | 说明 | 默认值 |
+|--------|------|--------|
+| **心跳间隔** | 心跳发送的时间间隔(秒),必须 > 0 | 3600 |
+| **回传目标** | 心跳结果回传的 channel,一般为 `web` | web |
+| **生效时间段** | 心跳仅在此区间内发送,格式为 `HH:MM`(24小时制) | 不配置则全天生效 |
-配置方式共有三种,修改配置文件,修改环境变量,在web前端设置。
+修改配置后点击保存,会写回 `config/config.yaml` 的 `heartbeat` 段,并自动重启心跳服务。
-### 2.1 配置文件 `config/config.yaml`
+![](../assets/images/heartbeat2.png)
+
+### 2. 配置文件 `config/config.yaml`
在 `config/config.yaml` 中配置 `heartbeat` 段:
@@ -32,60 +73,56 @@ heartbeat:
end: 22:00
```
+| 字段 | 含义 | 说明 |
+|------|------|------|
+| `every` | 心跳间隔(秒) | 必须 > 0,例如 60 表示每分钟一次,3600 表示每小时一次。 |
+| `target` | 回传目标 channel | 一般为 `web`,表示把心跳响应通过 WebChannel 推到前端;留空或不存在则不回传。 |
+| `active_hours` | 生效时间段 | `start` / `end` 格式为 `HH:MM`(24小时制)。仅当当前时间在 [start, end] 内才发心跳;不配置则全天生效。支持跨午夜(如 22:00–06:00)。 |
-| 字段 | 含义 | 说明 |
-| -------------- | ------------ | --------------------------------------------------------------------------------------------- |
-| `every` | 心跳间隔(秒) | 必须 > 0,例如 60 表示每分钟一次,3600 表示每小时一次。 |
-| `target` | 回传目标 channel | 一般为 `web`,表示把心跳响应通过 WebChannel 推到前端;留空或不存在则不回传。 |
-| `active_hours` | 生效时间段 | `start` / `end` 格式为 `HH:MM`(24 小时制)。仅当当前时间在 [start, end] 内才发心跳;不配置则全天生效。支持跨午夜(如 22:00–06:00)。 |
+### 3. 环境变量(覆盖 YAML)
+| 变量名 | 含义 | 示例 |
+|--------|------|------|
+| `HEARTBEAT_INTERVAL` | 心跳间隔(秒) | `3600` |
+| `HEARTBEAT_RELAY_CHANNEL_ID` | 回传目标 channel | `web` |
+| `HEARTBEAT_TIMEOUT` | 单次心跳请求超时(秒) | `30` |
-### 2.2 环境变量(覆盖 YAML)
+环境变量优先级高于 `config/config.yaml` 中的 `heartbeat` 段。
+---
-| 变量名 | 含义 | 示例 |
-| ---------------------------- | ------------ | ------ |
-| `HEARTBEAT_INTERVAL` | 心跳间隔(秒) | `3600` |
-| `HEARTBEAT_RELAY_CHANNEL_ID` | 回传目标 channel | `web` |
-| `HEARTBEAT_TIMEOUT` | 单次心跳请求超时(秒) | `30` |
+## 查看心跳
+### 心跳历史记录
-环境变量优先级高于 `config/config.yaml` 中的 `heartbeat` 段。
+在心跳面板的「最近 20 条心跳消息」显示框里可以查看心跳历史记录,包括每次心跳的状态(正常 / 告警)、内容与时间。
-### 2.3 前端「心跳」面板
+![](../assets/images/heartbeat3.png)
-Web 端左侧导航进入 **心跳** 面板,可:
+### 弹窗提示
-- 查看当前心跳配置(间隔、回传目标、生效时间段)
-![](../assets/images/heartbeat1.png)
-- 修改上述配置并保存(会写回 `config/config.yaml` 的 `heartbeat` 段,并重启心跳服务)。
-![](../assets/images/heartbeat2.png)
-- 在 `最近 20 条心跳消息` 显示框里可以查看心跳历史记录,包括每次心跳的状态(正常 / 告警)、内容与时间。
-![](../assets/images/heartbeat3.png)
----
+当 `target` 设置为 `web` 时,每次心跳响应会通过 `heartbeat.relay` 事件推到前端。若内容非 `HEARTBEAT_OK`,则以弹窗形式提示,便于查看任务执行结果或异常信息。
-## 3. HEARTBEAT.md 与周期性任务
+![](../assets/images/heartbeat4.png)
-### 3.1 文件位置与作用
+---
-- **路径**:在web端,该文件在面板右侧,可以点击 `编辑` 进行修改。
-![](../assets/images/heartbeat5.png)
-- **作用**:若该文件存在且任务不为空,每次心跳时 Agent 会读取该段内容,按顺序执行任务,并把结果返回;若文件不存在或无任务,则仅返回 `HEARTBEAT_OK`。
+## 案例实践
-### 3.2 Agent 行为简述
+### 案例:打印 Hello World
-- 服务端实现会读取 `HEARTBEAT.md`,解析出任务列表,拼成一条 chat 请求发给 Agent,走正常对话流程;解析失败或任务列表为空时,直接返回 `HEARTBEAT_OK`;否则执行任务返回响应。
+在 `HEARTBEAT.md` 中添加以下内容:
----
+```markdown
+请输出 "Hello Heartbeat!"
+```
-## 4. 前端展示与事件
+心跳执行时,Agent 会读取该内容并输出 `Hello Heartbeat!`,结果会通过 `heartbeat.relay` 事件回传到前端。
+![执行心跳任务](../assets/images/heartbeat6.png)
-- **状态**:心跳面板和工具栏会显示最近心跳信息(如 `HEARTBEAT_OK`)与时间。
-- **事件**:当 `target` 为 `web` 时,每次心跳响应会通过 `heartbeat.relay` 事件推到前端,用于更新状态与历史记录;若内容非 `HEARTBEAT_OK`,则以弹窗形式提示,便于查看任务执行结果或异常信息。
-![](../assets/images/heartbeat4.png)
---
-## 5. 常见问题
+## 常见问题
**Q:修改了 `config/config.yaml` 的 heartbeat 段,为何没生效?**
A:应用启动时读取配置;若通过前端「心跳」面板修改,会写回 YAML 并自动重启心跳服务。若直接改 YAML,需重启整个应用(如重启 jiuwenswarm-web)后新配置才会生效。
@@ -101,11 +138,20 @@ A:必须放在 DeepAgent workspace 根目录下:`~/.jiuwenswarm/agent/worksp
---
-## 6. 相关代码与配置索引
+## 机制介绍与关键代码
+
+### 心跳机制下 Agent 行为简述
-- 心跳服务实现:`jiuwenswarm/gateway/heartbeat/heartbeat.py`。
-- 配置读取与写回:`jiuwenswarm/common/config.py` 的 `update_heartbeat_in_config`;应用启动时在 `app.py` 中从 `~/.jiuwenswarm/config/config.yaml` 的 `heartbeat` 段及环境变量构建 `HeartbeatConfig`。
-- Agent 侧 HEARTBEAT.md 处理:`jiuwenswarm/server/runtime/agent_adapter/interface_deep.py` 中确定当前是心跳会话,则读取 `HEARTBEAT.md` 并触发任务。
-- 前端:`jiuwenswarm/channels/web/frontend/src/components/HeartbeatPanel/`、`heartbeat.get_conf` / `heartbeat.set_conf`、`heartbeat.relay` 事件。
+服务端实现会读取 `HEARTBEAT.md`,解析出任务列表,拼成一条 chat 请求发给 Agent,走正常对话流程;解析失败或任务列表为空时,直接返回 `HEARTBEAT_OK`;否则执行任务返回响应。心跳响应结果通过 `heartbeat.relay` 事件推送到前端,用于更新状态与历史记录;若内容非 `HEARTBEAT_OK`,则以弹窗形式提示。
+### 相关代码与配置索引
+| 代码路径 | 功能说明 |
+|----------|----------|
+| `jiuwenswarm/gateway/heartbeat/heartbeat.py` | 心跳服务实现,包含周期调度和 E2A 请求构造 |
+| `jiuwenswarm/common/config.py` | 配置读取与写回,`update_heartbeat_in_config` 函数 |
+| `jiuwenswarm/app.py` | 应用启动时从配置文件和环境变量构建 `HeartbeatConfig` |
+| `jiuwenswarm/server/runtime/agent_adapter/interface_deep.py` | Agent 侧 HEARTBEAT.md 处理,识别心跳会话并触发任务 |
+| `jiuwenswarm/channels/web/frontend/src/components/HeartbeatPanel/` | 前端心跳面板组件 |
+| `heartbeat.get_conf` / `heartbeat.set_conf` | 前端配置读取与设置 API |
+| `heartbeat.relay` | 心跳响应推送事件 |
\ No newline at end of file