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

feat: 定时任务/心跳支持飞书

Haowei_Duc1d5f8a03ba9
diff --git "a/assets/images/\345\221\275\344\273\244\350\241\214\350\247\243\346\236\220.jpg" "b/assets/images/\345\221\275\344\273\244\350\241\214\350\247\243\346\236\220.jpg"
new file mode 100644
index 0000000000..ffa23554d3
Binary files /dev/null and "b/assets/images/\345\221\275\344\273\244\350\241\214\350\247\243\346\236\220.jpg" differ
diff --git "a/assets/images/\345\256\232\346\227\266\344\273\273\345\212\2411.png" "b/assets/images/\345\256\232\346\227\266\344\273\273\345\212\2411.png"
new file mode 100644
index 0000000000..e9dcdb4e97
Binary files /dev/null and "b/assets/images/\345\256\232\346\227\266\344\273\273\345\212\2411.png" differ
diff --git "a/assets/images/\345\256\232\346\227\266\344\273\273\345\212\2412.png" "b/assets/images/\345\256\232\346\227\266\344\273\273\345\212\2412.png"
new file mode 100644
index 0000000000..05cfbffba9
Binary files /dev/null and "b/assets/images/\345\256\232\346\227\266\344\273\273\345\212\2412.png" differ
diff --git "a/docs/\345\221\275\344\273\244\350\241\214\346\216\247\345\210\266\346\214\207\344\273\244.md" "b/docs/\345\221\275\344\273\244\350\241\214\346\216\247\345\210\266\346\214\207\344\273\244.md"
new file mode 100644
index 0000000000..cc9c5d5b05
--- /dev/null
+++ "b/docs/\345\221\275\344\273\244\350\241\214\346\216\247\345\210\266\346\214\207\344\273\244.md"
@@ -0,0 +1,77 @@
+## 命令行 / 通道控制指令说明
+
+JiuwenClaw 支持通过「特殊前缀指令」控制会话和模式,最常用的是:
+
+- `/new_session`:为当前通道切换到一个全新的 `session_id`
+- `/mode plan` / `/mode agent`:切换当前通道的工作模式
+
+这些指令由 Gateway 层的 `MessageHandler` 解析,**不会发给 Agent 本身**。
+
+---
+
+### 1. `/new_session` —— 新建会话 ID
+
+**作用:**
+
+- 针对受控通道(当前支持:`feishu` / `xiaoyi` / `dingtalk`),为该通道生成一个新的 `session_id`,形如:
+ - `feishu_<毫秒时间戳hex>_<随机hex>`
+ - `xiaoyi_<毫秒时间戳hex>_<随机hex>`
+ - `dingtalk_<毫秒时间戳hex>_<随机hex>`
+- 后续从该通道进来的所有普通聊天消息,都会被强制覆盖为这个新的 `session_id`,从而在 `workspace/session/` 下形成一个新的会话目录。
+
+**使用方式:**
+
+- 在通道(飞书 / 小艺 / 钉钉)中,直接发送一条消息:
+
+ ```text
+ /new_session
+ ```
+![](../assets/images/命令行解析.jpg)
+
+- Gateway 会:
+ 1. 拦截这条消息(不转发给 Agent)
+ 2. 为该 `channel_id` 生成新的 `session_id`
+ 3. 在当前会话里回复一条系统提示,例如:
+ `session_id 已变更为 feishu_17f2b4b32e0_ab12cd`
+
+**注意事项:**
+
+- `\new_session` 只会修改 **后续消息绑定的 session_id**,不会立刻创建目录;目录在真正用到(如 todo / 文件写入 / 显式创建 session)时按需生成。
+
+---
+
+### 2. `/mode` —— 切换通道模式(plan / agent)
+
+**作用:**
+
+- 为当前通道设置一个逻辑「模式」,用于告诉 Agent 是偏向:
+ - `plan`:偏规划、解释、拆解任务(默认)
+ - `agent`:偏自动执行、直接动手
+- 模式信息会写入消息的 `params["mode"]` 中,Agent 在构造提示词和行为策略时可以使用。
+
+**使用方式:**
+
+- 在受控通道中发送:
+
+ ```text
+ /mode plan
+ ```
+
+ 或
+
+ ```text
+ /mode agent
+ ```
+
+- Gateway 会:
+ 1. 识别这条消息为控制指令,不转发给 Agent
+ 2. 更新内部 `ChannelControlState.mode`
+ 3. 给当前会话回复一条系统提示,例如:
+ `mode 已变更为 agent`
+
+**生效范围:**
+
+- 模式是 **按通道维度** 存储的(`channel_id` → `mode`),同一通道的所有后续消息,都会自动带上当前模式。
+- 前端 `config.yaml` 中的 `default_mode` 可设初始值,`MessageHandler` 启动时会读取。
+
+
diff --git "a/docs/\345\256\232\346\227\266\344\273\273\345\212\241\345\277\253\351\200\237\344\270\212\346\211\213.md" "b/docs/\345\256\232\346\227\266\344\273\273\345\212\241\345\277\253\351\200\237\344\270\212\346\211\213.md"
new file mode 100644
index 0000000000..a218a20018
--- /dev/null
+++ "b/docs/\345\256\232\346\227\266\344\273\273\345\212\241\345\277\253\351\200\237\344\270\212\346\211\213.md"
@@ -0,0 +1,58 @@
+## 定时任务(Cron)快速上手
+
+本文介绍如何在 JiuwenClaw 中创建和管理一个最简单的定时任务,并让它按固定时间把结果推送到指定频道(如 Web、飞书)。
+
+---
+
+### 1. 定时任务能做什么?
+
+- **定时执行一句话指令**:例如“每天早上 9 点汇总昨天的待办完成情况”。
+- **让 Agent 自己去跑任务**:定时跑搜索、生成日报等。
+- **多通道推送**:结果可以推到 Web 或 飞书 频道(使用飞书频道需要设置chat id, 详情见。
+
+---
+
+### 2. 在 Web 面板创建一个最简单的定时任务
+
+1. 打开 Web 界面,进入 **「定时任务 / Cron」** 面板。
+2. 点击 **「新建任务」**,按下表填写:
+
+ - **name(任务名称)**:例如 `daily_todo_summary`
+ - **cron_expr(Cron 表达式)**:
+ - 每天早上 9:00:`0 9 * * *`
+ - 每小时第 15 分钟:`15 * * * *`
+ - **timezone(时区)**:一般用 `Asia/Shanghai`
+ - **targets(推送频道)**:从下拉中选择:
+ - `web`:推到 Web 面板
+ - `feishu`:推到飞书通道
+ - **enabled(是否启用)**:勾选(`true`)
+ - **description(任务内容)**:一句自然语言,描述到点时要让 Agent 做什么,例如:
+ `生成一段简短的中文文本,说明当前时间点的健康打卡提醒。`
+ - **wake_offset_seconds(提前秒数,可选)**:默认 300,表示提前 5 分钟唤醒 Agent 做准备。
+![](../assets/images/定时任务1.png)
+
+3. 保存后,任务会写入 `~/.jiuwenclaw/workspace/cron_jobs.json`,调度器自动生效。
+
+
+---
+
+### 3. 常见 Cron 表达式示例
+
+- **每天 9:00**:`0 9 * * *`
+- **每天 18:30**:`30 18 * * *`
+- **每周一 9:00**:`0 9 * * 1`
+- **每小时整点**:`0 * * * *`
+
+Cron 表达式格式为:
+`分 时 日 月 周`(5 段,空格分隔)
+
+---
+
+### 4. 在聊天中通过工具创建任务(可选)
+
+当 Agent 具备 `cron_create_job` 工具能力时,你也可以直接在聊天中说:
+
+> “帮我创建一个定时任务,每天 9 点在 Web 上提醒我喝水。”
+
+Agent 会自动解析出 `cron_expr` / `description` / `targets` 等字段,并调用 `cron_create_job` 创建任务,等价于在前端面板上点“新建任务”并填写表单。
+![](../assets/images/定时任务2.png)
diff --git a/jiuwenclaw/app.py b/jiuwenclaw/app.py
index 5ee54611df..e79247323f 100644
--- a/jiuwenclaw/app.py
+++ b/jiuwenclaw/app.py
@@ -1142,6 +1142,7 @@ async def _run() -> None:
encrypt_key = str(feishu_conf.get("encrypt_key") or "").strip()
verification_token = str(feishu_conf.get("verification_token") or "").strip()
allow_from = feishu_conf.get("allow_from") or []
+ chat_id = str(feishu_conf.get("chat_id") or "").strip()
enabled_raw = feishu_conf.get("enabled", None)
if enabled_raw is None:
@@ -1161,6 +1162,7 @@ async def _run() -> None:
encrypt_key=encrypt_key,
verification_token=verification_token,
allow_from=allow_from,
+ chat_id=chat_id,
)
feishu_channel = FeishuChannel(feishu_config, _DummyBus())
channel_manager.register_channel(feishu_channel)
diff --git a/jiuwenclaw/channel/feishu.py b/jiuwenclaw/channel/feishu.py
index 2df795d67d..49192256d3 100644
--- a/jiuwenclaw/channel/feishu.py
+++ b/jiuwenclaw/channel/feishu.py
@@ -12,7 +12,7 @@ from pydantic import BaseModel, Field
from jiuwenclaw.utils import logger
from jiuwenclaw.channel.base import RobotMessageRouter, BaseChannel
-from jiuwenclaw.schema.message import Message, ReqMethod
+from jiuwenclaw.schema.message import Message, ReqMethod, EventType
class FeishuConfig(BaseModel):
@@ -24,6 +24,7 @@ class FeishuConfig(BaseModel):
encrypt_key: str = "" # 事件订阅的加密密钥(可选)
verification_token: str = "" # 事件订阅的验证令牌(可选)
allow_from: list[str] = Field(default_factory=list) # 允许的用户的open_id列表
+ chat_id: str = "" # 可选:固定推送目标 chat_id(群聊 oc_xxx 或个人 open_id)
try:
@@ -411,7 +412,17 @@ class FeishuChannel(BaseChannel):
try:
receive_id, id_type = self._extract_receive_info(msg)
- content_str = self._extract_message_content(msg)
+
+ # 心跳/系统事件:优先从 payload["heartbeat"] 读取内容
+ payload = getattr(msg, "payload", None) or {}
+ if (
+ msg.event_type == EventType.HEARTBEAT_RELAY
+ and isinstance(payload, dict)
+ and payload.get("heartbeat")
+ ):
+ content_str = str(payload.get("heartbeat"))
+ else:
+ content_str = self._extract_message_content(msg)
if not content_str.strip():
logger.warning("飞书发送:消息内容为空,跳过发送")
@@ -449,7 +460,17 @@ class FeishuChannel(BaseChannel):
receive_id = feishu_open_id
id_type = "open_id"
- # 2) 兼容旧逻辑:无 metadata 时用 session_id / id
+ # 2) 若 metadata 中没有平台身份,则使用配置中的 chat_id 作为固定推送目标
+ # print('this is in _extract_receive_info')
+ logger.info('this is in _extract_receive_info, chat_id is %s', self.config.chat_id)
+ if not receive_id:
+ cfg_chat_id = getattr(self.config, "chat_id", "") or ""
+ cfg_chat_id = cfg_chat_id.strip()
+ if cfg_chat_id:
+ receive_id = cfg_chat_id
+ id_type = "chat_id" if cfg_chat_id.startswith("oc_") else "open_id"
+
+ # 3) 仍然没有,则回退到 session_id / id(兼容旧逻辑)
if not receive_id:
receive_id = getattr(msg, "session_id", None) or msg.id or ""
if receive_id.startswith("oc_"):
diff --git a/jiuwenclaw/gateway/cron/controller.py b/jiuwenclaw/gateway/cron/controller.py
index c2859342f2..bfdd424cb2 100644
--- a/jiuwenclaw/gateway/cron/controller.py
+++ b/jiuwenclaw/gateway/cron/controller.py
@@ -293,7 +293,7 @@ class CronController:
"targets": {
"type": "string",
"enum": [e.value for e in CronTargetChannel],
- "description": "推送频道:web=网页, feishu=飞书, xiaoyi=小艺, dingtalk=钉钉",
+ "description": "推送频道:web=网页, feishu=飞书",
"default": CronTargetChannel.WEB.value,
},
"enabled": {
@@ -329,7 +329,7 @@ class CronController:
"targets": {
"type": "string",
"enum": [e.value for e in CronTargetChannel],
- "description": "推送频道:web/feishu/xiaoyi/dingtalk",
+ "description": "推送频道:web/feishu",
},
},
},
diff --git a/jiuwenclaw/gateway/cron/models.py b/jiuwenclaw/gateway/cron/models.py
index 5692d80135..10b6bb4037 100644
--- a/jiuwenclaw/gateway/cron/models.py
+++ b/jiuwenclaw/gateway/cron/models.py
@@ -10,8 +10,8 @@ class CronTargetChannel(str, Enum):
WEB = "web"
FEISHU = "feishu"
- XIAOYI = "xiaoyi"
- DINGTALK = "dingtalk"
+ # XIAOYI = "xiaoyi"
+ # DINGTALK = "dingtalk"
def _normalize_targets_str(raw: str) -> str: