From e987cb6f6b7ac7796ff00268e4e97300d4a1f18f Mon Sep 17 00:00:00 2001 From: autobee-sparticle Date: Fri, 20 Mar 2026 15:47:48 +0900 Subject: [PATCH 01/17] =?UTF-8?q?fix(novare):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E7=B2=92=E5=BA=A6=E9=99=8D=E7=BA=A7=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=AD=96=E7=95=A5=EF=BC=8C=E8=A7=A3=E5=86=B3=E8=AF=A6?= =?UTF-8?q?=E7=BB=86=E4=BD=8D=E7=BD=AE=E6=8C=87=E5=AE=9A=E6=97=B6=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=8C=B9=E9=85=8D=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当用户指定过于详细的位置信息(如"3階執務スペース、フォーラム側窓側")时, find_device_by_area 可能无法匹配到设备区域。新增降级搜索策略: - 第1步:去除方位修饰语,保留核心区域名重新搜索 - 第2步:进一步简化到楼层级别搜索 - 降级成功时告知用户搜索范围变化并确认 Closes #2201 (mygpt-frontend) Co-authored-by: zhuchao Co-authored-by: Claude Opus 4.6 --- prompt/novare.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/prompt/novare.md b/prompt/novare.md index 49d0d13..d1257e3 100644 --- a/prompt/novare.md +++ b/prompt/novare.md @@ -74,6 +74,13 @@ - dxcore_update_device_status(device_id="[B设备id]",running_control=0) → 灯光亮度调整为0 **响应**:"已为您关闭Define Room4的灯光" +### 位置降级搜索场景 +**用户**:"3階執務スペース、フォーラム側窓側の照明をつけて" +- find_device_by_area(description="3階執務スペース、フォーラム側窓側", device_type="light") → 返回无结果 +- find_device_by_area(description="3階執務スペース", device_type="light") → 降级搜索,找到设备 +- 告知用户是基于"3階執務スペース"范围搜索到的结果,并确认是否操作 +**响应**:"「3階執務スペース、フォーラム側窓側」では見つかりませんでしたが、3階執務スペースエリアで照明が見つかりました。こちらの照明を操作しますか?" + @@ -92,6 +99,17 @@ ▪ 主动向用户确认:向用户列出所有候选房间,并提示用户选择或明确具体是哪一个。确认提示语可参考:“请问您想查询的是以下哪个房间?[列出候选房间列表]”。 ▪ 理解用户二次确认:等待用户回复后,根据其选择再次调用查询工具获取最终信息。用户对候选房间的指明(如回复“第一个”或重复房间名)应视为对该房间的确认。 4. 处理无匹配结果:如果工具返回未找到任何相关房间,应明确告知用户这一情况,并建议用户检查房间名称是否正确或提供更多线索。 + 5. **位置粒度降级搜索(詳細な位置指定で見つからない場合)**: + 用户指定了详细的位置信息(如包含方位、区域细节),但工具返回无匹配结果时,自动执行降级搜索: + - **第1步**:从位置描述中去除方位修饰语(側、付近、奥、手前、寄り等)和细节描述,保留核心区域名重新搜索 + - 例: "3階執務スペース、フォーラム側窓側" → find_device_by_area(description="3階執務スペース") + - 例: "2階会議室A、入口付近" → find_device_by_area(description="2階会議室A") + - **第2步**:如果仍无结果,进一步简化到楼层+大区域级别 + - 例: "3階執務スペース" → find_device_by_area(description="3階") + - **降级成功时的回复**:告知用户是基于更广范围的搜索结果,让用户确认 + - 回复格式: "「{元の位置}」では見つかりませんでしたが、{簡略化した位置}エリアで以下の設備が見つかりました。こちらでよろしいですか?" + - **全部失败时**:告知用户未找到设备,建议提供其他位置信息或直接指定房间名 + - 回复格式: "申し訳ございません、該当エリアでは操作可能な設備が見つかりませんでした。お部屋の名前をお教えいただけますか?" 3. 更新设备(此操作需要确认) - **条件**:用户意图为控制设备或调节参数(如开关、温度、风速), 需要进行确认。 @@ -105,6 +123,7 @@ - 通过 find_employee_location(name="[当前用户名字/邮箱]") 获取用户的sensor_id - 然后通过 find_iot_device(target_sensor_id="[当前用户的sensor_id]", device_type="[目标设备类型]") 查找他附近的设备 - 找到设备后告知用户找到的设备信息,并确认是否执行操作 + - **位置指定但匹配失败时**:如果用户指定了详细位置(如"3階執務スペース、フォーラム側窓側の照明をつけて"),但 find_device_by_area 返回无匹配结果,应按照规则 2 第 5 点的**位置粒度降级搜索**策略执行,而不是直接回复"找不到设备" 3. **空调温度调节确认方式**: - 如果用户说"有点热"、"调低点"、"太热了"等,表示要降温: 1. 先查询当前室温 From 85519da5a525577aa108fa5df658ddc7d32ea549 Mon Sep 17 00:00:00 2001 From: autobee-sparticle Date: Thu, 26 Mar 2026 10:14:13 +0900 Subject: [PATCH 02/17] =?UTF-8?q?fix(memory):=20=E6=94=B9=E8=BF=9B=20Memor?= =?UTF-8?q?y=20=E6=8F=90=E5=8F=96=20prompt=20=E4=BD=BF=E7=94=A8=E5=A4=A7?= =?UTF-8?q?=E7=99=BD=E8=AF=9D=20(#20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 fact extraction prompt 中的技术性格式改为自然语言: - "Contact: [name] (relationship, referred as [nick])" → "[name] is a [relationship], also called [nick]" - 移除 "DEFAULT when user says" 等技术标记 - 添加 Plain Language Rule 明确要求输出通俗易懂的文本 - 更新所有示例为自然语言风格 Co-authored-by: zhuchao Co-authored-by: Claude Opus 4.6 (1M context) --- prompt/FACT_RETRIEVAL_PROMPT.md | 64 ++++++++++++++++----------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/prompt/FACT_RETRIEVAL_PROMPT.md b/prompt/FACT_RETRIEVAL_PROMPT.md index 1c2f93e..d075528 100644 --- a/prompt/FACT_RETRIEVAL_PROMPT.md +++ b/prompt/FACT_RETRIEVAL_PROMPT.md @@ -8,16 +8,17 @@ Types of Information to Remember: 4. Remember Activity and Service Preferences: Recall preferences for dining, travel, hobbies, and other services. 5. Monitor Health and Wellness Preferences: Keep a record of dietary restrictions, fitness routines, and other wellness-related information. 6. Store Professional Details: Remember job titles, work habits, career goals, and other professional information. -7. **Manage Relationships and Contacts**: CRITICAL - Keep track of people the user frequently interacts with. This includes: - - Full names of contacts (always record the complete name when mentioned) - - Short names, nicknames, or abbreviations the user uses to refer to the same person - - Relationship context (family, friend, colleague, client, etc.) +7. **Manage Relationships and People**: CRITICAL - Keep track of people the user frequently interacts with. This includes: + - Full names (always record the complete name when mentioned) + - Nicknames or short names the user uses for the same person + - Relationship (family, friend, colleague, client, etc.) - When a user mentions a short name and you have previously learned the full name, record BOTH to establish the connection - - Examples of connections to track: "Mike" → "Michael Johnson", "Tom" → "Thomas Anderson", "Lee" → "Lee Ming", "田中" → "田中一郎" - - **Handle Multiple People with Same Surname**: When there are multiple people with the same surname (e.g., "滨田太郎" and "滨田清水"), track which one the user most recently referred to with just the surname ("滨田"). Record this as the default/active reference. - - **Format for surname disambiguation**: "Contact: [Full Name] (relationship, also referred as [Surname]) - DEFAULT when user says '[Surname]'" + - Examples: "Mike" → "Michael Johnson", "Tom" → "Thomas Anderson", "Lee" → "Lee Ming", "田中" → "田中一郎" + - **Handle Multiple People with Same Surname**: When there are multiple people with the same surname (e.g., "滨田太郎" and "滨田清水"), track which one the user most recently referred to with just the surname. 8. Miscellaneous Information Management: Keep track of favorite books, movies, brands, and other miscellaneous details that the user shares. +**IMPORTANT - Plain Language Rule**: All extracted facts MUST be written in plain, everyday language that anyone can understand. Do NOT use structured formats like "Contact:", "referred as", "DEFAULT when user says" etc. Write facts as natural sentences or short notes. + Here are some few shot examples: Input: Hi. @@ -39,49 +40,49 @@ Input: Me favourite movies are Inception and Interstellar. Output: {{"facts" : ["Favourite movies are Inception and Interstellar"]}} Input: I had dinner with Michael Johnson yesterday. -Output: {{"facts" : ["Had dinner with Michael Johnson", "Contact: Michael Johnson"]}} +Output: {{"facts" : ["Had dinner with Michael Johnson", "Michael Johnson is an acquaintance"]}} Input: I'm meeting Mike for lunch tomorrow. He's my colleague. -Output: {{"facts" : ["Meeting Mike for lunch tomorrow", "Contact: Michael Johnson (colleague, referred as Mike)"]}} +Output: {{"facts" : ["Meeting Mike for lunch tomorrow", "Michael Johnson is a colleague, also called Mike"]}} Input: Have you seen Tom recently? I think Thomas Anderson is back from his business trip. -Output: {{"facts" : ["Contact: Thomas Anderson (referred as Tom)", "Thomas Anderson was on a business trip"]}} +Output: {{"facts" : ["Thomas Anderson is also called Tom", "Thomas Anderson was on a business trip"]}} Input: My friend Lee called me today. -Output: {{"facts" : ["Friend Lee called today", "Contact: Lee (friend)"]}} +Output: {{"facts" : ["Friend Lee called today", "Lee is a friend"]}} Input: Lee's full name is Lee Ming. We work together. -Output: {{"facts" : ["Contact: Lee Ming (colleague, also referred as Lee)", "Works with Lee Ming"]}} +Output: {{"facts" : ["Lee Ming is a colleague, also called Lee", "Works with Lee Ming"]}} Input: I need to call my mom later. -Output: {{"facts" : ["Need to call mom", "Contact: mom (family, mother)"]}} +Output: {{"facts" : ["Need to call mom later"]}} Input: I met with Director Sato yesterday. We discussed the new project. -Output: {{"facts" : ["Met with Director Sato yesterday", "Contact: Director Sato (boss/supervisor)"]}} +Output: {{"facts" : ["Met with Director Sato yesterday", "Director Sato is a boss/supervisor"]}} Input: I know two people named 滨田: 滨田太郎 and 滨田清水. -Output: {{"facts" : ["Contact: 滨田太郎", "Contact: 滨田清水"]}} +Output: {{"facts" : ["滨田太郎という知り合いがいる", "滨田清水という知り合いがいる"]}} Input: I had lunch with 滨田太郎 today. -Output: {{"facts" : ["Had lunch with 滨田太郎 today", "Contact: 滨田太郎 (also referred as 滨田) - DEFAULT when user says '滨田'"]}} +Output: {{"facts" : ["今日滨田太郎とランチした", "滨田太郎は「滨田」とも呼ばれている"]}} Input: 滨田 called me yesterday. -Output: {{"facts" : ["滨田太郎 called yesterday", "Contact: 滨田太郎 (also referred as 滨田) - DEFAULT when user says '滨田'"]}} +Output: {{"facts" : ["昨日滨田太郎から電話があった"]}} Input: I'm meeting 滨田清水 next week. -Output: {{"facts" : ["Meeting 滨田清水 next week", "Contact: 滨田清水 (also referred as 滨田) - DEFAULT when user says '滨田'"]}} +Output: {{"facts" : ["来週滨田清水と会う予定"]}} Input: 滨田 wants to discuss the project. -Output: {{"facts" : ["滨田清水 wants to discuss the project", "Contact: 滨田清水 (also referred as 滨田) - DEFAULT when user says '滨田'"]}} +Output: {{"facts" : ["滨田清水がプロジェクトについて話したい"]}} Input: There are two Mikes in my team: Mike Smith and Mike Johnson. -Output: {{"facts" : ["Contact: Mike Smith (colleague)", "Contact: Mike Johnson (colleague)"]}} +Output: {{"facts" : ["Mike Smith is a colleague", "Mike Johnson is a colleague"]}} Input: Mike Smith helped me with the bug fix. -Output: {{"facts" : ["Mike Smith helped with bug fix", "Contact: Mike Smith (colleague, also referred as Mike) - DEFAULT when user says 'Mike'"]}} +Output: {{"facts" : ["Mike Smith helped with bug fix", "Mike Smith is also called Mike"]}} Input: Mike is coming to the meeting tomorrow. -Output: {{"facts" : ["Mike Smith is coming to the meeting tomorrow", "Contact: Mike Smith (colleague, also referred as Mike) - DEFAULT when user says 'Mike'"]}} +Output: {{"facts" : ["Mike Smith is coming to the meeting tomorrow"]}} Input: 私は林檎好きです Output: {{"facts" : ["林檎が好き"]}} @@ -113,17 +114,16 @@ Remember the following: - For colloquial or grammatically informal expressions (common in spoken Japanese, Chinese, Korean, etc.), understand the full intended meaning and record it in a clear, semantically complete form. - In Japanese, spoken language often omits particles (e.g., が, を, に). When extracting facts, include the necessary particles to make the meaning unambiguous. For example: "私は林檎好きです" should be understood as "林檎が好き" (likes apples), not literally "私は林檎好き". - When the user expresses a preference or opinion in casual speech, record the core preference/opinion clearly. Remove the subject pronoun (私は/I) since facts are about the user by default, but keep all other semantic components intact. -- **CRITICAL for Contact/Relationship Tracking**: - - ALWAYS use the "Contact: [name] (relationship/context)" format when recording people - - When you see a short name that matches a known full name, record as "Contact: [Full Name] (relationship, also referred as [Short Name])" - - Record relationship types explicitly: family, friend, colleague, boss, client, neighbor, etc. - - For family members, also record the specific relation: (mother, father, sister, brother, spouse, etc.) +- **CRITICAL for People/Relationship Tracking**: + - Write people-related facts in plain, natural language. Do NOT use structured formats like "Contact:", "referred as", or "DEFAULT when user says". + - Good examples: "Michael Johnson is a colleague, also called Mike", "田中さんは友達", "滨田太郎は「滨田」とも呼ばれている" + - Bad examples: "Contact: Michael Johnson (colleague, referred as Mike)", "Contact: 滨田太郎 (also referred as 滨田) - DEFAULT when user says '滨田'" + - Record relationship types naturally: "is a friend", "is a colleague", "is family (mother)", etc. + - For nicknames: "also called [nickname]" or "[full name]は「[nickname]」とも呼ばれている" - **Handling Multiple People with Same Name/Surname**: - - When multiple contacts share the same surname or short name (e.g., multiple "滨田" or "Mike"), track which person was most recently referenced - - When user explicitly mentions the full name (e.g., "滨田太郎"), mark this person as the DEFAULT for the short form - - Use the format: "Contact: [Full Name] (relationship, also referred as [Short Name]) - DEFAULT when user says '[Short Name]'" - - When the user subsequently uses just the short name/surname, resolve to the most recently marked DEFAULT person - - When a different person with the same name is explicitly mentioned, update the DEFAULT marker to the new person + - When multiple people share the same surname, track which person was most recently referenced + - When user explicitly mentions a full name, remember this as the person currently associated with the short name + - When the user subsequently uses just the short name/surname, resolve to the most recently associated person Following is a conversation between the user and the assistant. You have to extract the relevant facts and preferences about the user, if any, from the conversation and return them in the json format as shown above. You should detect the language of the user input and record the facts in the same language. \ No newline at end of file From 18bf296aa09641a64ace0124c0ce3d98ad756732 Mon Sep 17 00:00:00 2001 From: autobee-sparticle Date: Thu, 26 Mar 2026 20:12:39 +0900 Subject: [PATCH 03/17] feat: move enable_thinking control from docker-compose to request body (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add page number * feat: add skill feature memory 添加 skill 功能的 feature memory,记录技能包管理服务和 Hook 系统的核心信息。 Co-Authored-By: Claude Opus 4.5 * feat(skill): add feature memory with changelog and decisions 添加 skill 功能的完整记忆文档: Changelog: - 2025-Q4: 初始实现 (GRPC 层 + 内置 skills) - 2026-Q1: API 完善 (REST API + Hook 系统) Design Decisions: - 001: Skill 架构设计 (目录结构、Hook 系统) - 002: 上传安全措施 (ZipSlip、路径遍历防护) Co-Authored-By: Claude Opus 4.5 * soffice sharp 支持 * shell_env support * feat: move enable_thinking control from docker-compose to request body Remove DEFAULT_THINKING_ENABLE environment variable from docker-compose and settings.py. The enable_thinking flag is now solely controlled via request body (default: false), as felo-mygpt already passes this config from RobotConfig database. Closes sparticleinc/felo-mygpt#2473 Co-Authored-By: Claude Opus 4.6 (1M context) --------- Co-authored-by: 朱潮 Co-authored-by: Claude Opus 4.5 Co-authored-by: zhuchao --- .features/skill/MEMORY.md | 121 ++++++++++++++++++ .features/skill/changelog/2025-Q4.md | 38 ++++++ .features/skill/changelog/2026-Q1.md | 36 ++++++ .features/skill/decisions/001-architecture.md | 46 +++++++ .features/skill/decisions/002-security.md | 35 +++++ Dockerfile | 9 +- Dockerfile.modelscope | 9 +- agent/prompt_loader.py | 43 +++++-- docker-compose-with-pgsql.yml | 1 - docker-compose.yml | 1 - prompt/system_prompt.md | 12 ++ utils/api_models.py | 3 +- utils/settings.py | 2 - 13 files changed, 338 insertions(+), 18 deletions(-) create mode 100644 .features/skill/MEMORY.md create mode 100644 .features/skill/changelog/2025-Q4.md create mode 100644 .features/skill/changelog/2026-Q1.md create mode 100644 .features/skill/decisions/001-architecture.md create mode 100644 .features/skill/decisions/002-security.md diff --git a/.features/skill/MEMORY.md b/.features/skill/MEMORY.md new file mode 100644 index 0000000..7d6fc1a --- /dev/null +++ b/.features/skill/MEMORY.md @@ -0,0 +1,121 @@ +# Skill 功能 + +> 负责范围:技能包管理服务 - 核心实现 +> 最后更新:2025-02-11 + +## 当前状态 + +Skill 系统支持两种来源:官方 skills (`./skills/`) 和用户 skills (`projects/uploads/{bot_id}/skills/`)。支持 Hook 系统和 MCP 服务器配置,通过 SKILL.md 或 plugin.json 定义元数据。 + +## 核心文件 + +- `routes/skill_manager.py` - Skill 上传/删除/列表 API +- `agent/plugin_hook_loader.py` - Hook 系统实现 +- `agent/deep_assistant.py` - `CustomSkillsMiddleware` +- `agent/prompt_loader.py` - PrePrompt hooks + MCP 配置合并 +- `skills/` - 官方 skills 目录 +- `skills_developing/` - 开发中 skills + +## 最近重要事项 + +- 2025-02-11: 初始化 skill 功能 memory + +## Gotchas(开发必读) + +- ⚠️ 执行脚本必须使用绝对路径 +- ⚠️ MCP 配置优先级:Skill MCP > 默认 MCP > 用户参数 +- ⚠️ 上传大小限制:50MB(ZIP),解压后最大 500MB +- ⚠️ 压缩比例检查:最大 100:1(防止 zip 炸弹) +- ⚠️ 符号链接检查:禁止解压包含符号链接的文件 + +## Skill 目录结构 + +``` +skill-name/ +├── SKILL.md # 核心指令文档(必需) +├── skill.yaml # 元数据配置(可选) +├── .claude-plugin/ +│ └── plugin.json # Hook 和 MCP 配置(可选) +└── scripts/ # 可执行脚本(可选) + └── script.py +``` + +## Hook 系统 + +| Hook 类型 | 执行时机 | 用途 | +|-----------|---------|------| +| `PrePrompt` | system_prompt 加载时 | 动态注入用户上下文 | +| `PostAgent` | agent 执行后 | 处理响应结果 | +| `PreSave` | 保存消息前 | 内容过滤/修改 | + +## API 接口 + +| 端点 | 方法 | 功能 | +|------|------|------| +| `GET /api/v1/skill/list` | - | 返回官方 + 用户 skills | +| `POST /api/v1/skill/upload` | - | ZIP 上传,解压到用户目录 | +| `DELETE /api/v1/skill/remove` | - | 删除用户 skill | + +## 内置 Skills + +| Skill 名称 | 功能描述 | +|-----------|---------| +| `excel-analysis` | Excel 数据分析、透视表、图表 | +| `managing-scripts` | 管理可复用脚本库 | +| `rag-retrieve` | RAG 知识库检索 | +| `jina-ai` | Jina AI Reader/Search | +| `user-context-loader` | Hook 机制示例 | + +## plugin.json 格式 + +```json +{ + "name": "skill-name", + "description": "描述", + "hooks": { + "PrePrompt": [{"type": "command", "command": "python hooks/pre_prompt.py"}], + "PostAgent": [...], + "PreSave": [...] + }, + "mcpServers": { + "server-name": { + "command": "...", + "args": [...] + } + } +} +``` + +## Skill 加载优先级 + +1. Skill MCP 配置(最高) +2. 默认 MCP 配置 (`mcp/mcp_settings.json`) +3. 用户传入参数(覆盖所有) + +## 安全措施 + +- ZipSlip 防护:检查解压路径 +- 路径遍历防护:验证 `bot_id` 和 `skill_name` 格式 +- 大小限制:上传 50MB,解压后 500MB +- 压缩比限制:最大 100:1 + +## 设计原则 + +- **渐进式加载**:按需加载,避免一次性读取所有 +- **绝对路径优先**:执行脚本必须使用绝对路径 +- **通用化设计**:脚本应参数化,解决一类问题 +- **安全优先**:完整的上传验证链 + +## 配置项 + +```bash +SKILLS_DIR=./skills # 官方 skills 目录 +BACKEND_HOST=xxx # RAG API 主机 +MASTERKEY=xxx # 认证密钥 +``` + +## 索引 + +- 设计决策:`decisions/` +- 变更历史:`changelog/` +- 相关文档:`docs/` diff --git a/.features/skill/changelog/2025-Q4.md b/.features/skill/changelog/2025-Q4.md new file mode 100644 index 0000000..b43c00a --- /dev/null +++ b/.features/skill/changelog/2025-Q4.md @@ -0,0 +1,38 @@ +# 2025-Q4 Skill Changelog + +## 版本 0.1.0 - 初始实现 + +### 2025-10-31 +- **新增**: agent skills 支持,测试阶段代码 +- **文件**: `chat_handler.py`, `knowledge_chat_cc_service.py` +- **作者**: Alex + +### 2025-11-03 +- **新增**: 内置 skills (pptx, docx, pdf, xlsx) +- **新增**: jina skill - 规范 jina 网络搜索 +- **解决**: "prompt too long" 问题 + +### 2025-11-13 +- **新增**: cc agent task 任务添加默认 skills +- **文件**: `task_handler.py`, `knowledge_task_cc_service.py` + +### 2025-11-19 +- **新增**: skill-creator 内置技能 + +### 2025-11-20 +- **新增**: EFS 类型接口,新增上传 skill +- **功能**: 支持 skill 包上传 + +### 2025-11-21 +- **新增**: EFS 删除 skill 接口 +- **移除**: skill 查询接口(暂存) + +### 2025-11-22 +- **新增**: GRPC chat 接口,skills 参数支持 + +### 2025-11-26 +- **新增**: skill 上传支持 `.skill` 后缀(测试) + +### 2025-11-28 +- **优化**: 默认挂载的 skill 改为合并逻辑 +- **优化**: 代码结构优化 diff --git a/.features/skill/changelog/2026-Q1.md b/.features/skill/changelog/2026-Q1.md new file mode 100644 index 0000000..9c0ea7e --- /dev/null +++ b/.features/skill/changelog/2026-Q1.md @@ -0,0 +1,36 @@ +# 2026-Q1 Skill Changelog + +## 版本 0.2.0 - API 完善 + +### 2026-01-07 +- **新增**: Skills 列表查询 API(能力管理页面) +- **新增**: 技能管理 API with authentication +- **文件**: `routes/skill_manager.py` +- **作者**: claude[bot], 朱潮 + +### 2026-01-09 +- **重构**: 移除 catalog agent,合并到 general agent +- **说明**: 简化架构,统一使用 general_agent +- **作者**: 朱潮 + +### 2026-01-10 +- **修复**: SKILL.md 的 name 字段解析逻辑 +- **新增**: 支持非标准 YAML 格式 +- **新增**: 目录名称不匹配时自动重命名 +- **作者**: Alex + +### 2026-01-13 +- **修复**: multipart form data format for catalog service +- **作者**: 朱潮 + +### 2026-01-28 +- **新增**: enable_thinking, enable_memory, skills to agent_bot_config +- **作者**: 朱潮 + +### 2026-01-30 +- **修复**: skill router 正确注册 +- **作者**: 朱潮 + +### 2026-02-11 +- **新增**: 初始化 skill feature memory +- **作者**: 朱潮 diff --git a/.features/skill/decisions/001-architecture.md b/.features/skill/decisions/001-architecture.md new file mode 100644 index 0000000..e899950 --- /dev/null +++ b/.features/skill/decisions/001-architecture.md @@ -0,0 +1,46 @@ +# 001: Skill 架构设计 + +## 状态 +已采纳 (Accepted) + +## 上下文 +需要为 QWEN_AGENT 模式的机器人提供可扩展的技能(插件/工具)支持,允许动态加载自定义功能。 + +## 决策 + +### 目录结构设计 +``` +skill-name/ +├── SKILL.md # 核心指令文档(必需) +├── skill.yaml # 元数据配置(可选) +├── .claude-plugin/ +│ └── plugin.json # Hook 和 MCP 配置(可选) +└── scripts/ # 可执行脚本(可选) +``` + +### Hook 系统 +| Hook 类型 | 执行时机 | 用途 | +|-----------|---------|------| +| `PrePrompt` | system_prompt 加载时 | 动态注入用户上下文 | +| `PostAgent` | agent 执行后 | 处理响应结果 | +| `PreSave` | 保存消息前 | 内容过滤/修改 | + +### 技能来源 +1. **官方 skills**: `./skills/` 目录 +2. **用户 skills**: `projects/uploads/{bot_id}/skills/` + +## 结果 + +### 正面影响 +- 渐进式加载,按需读取 +- 支持多种元数据格式(优先级: plugin.json > SKILL.md) +- 完整的 Hook 扩展机制 +- MCP 服���器配置支持 + +### 负面影响 +- 需要管理文件系统权限 +- 技能包格式验证复杂度增加 + +## 替代方案 +1. 使用数据库存储(拒绝:文件更灵活) +2. 仅支持单一格式(拒绝:用户多样性需求) diff --git a/.features/skill/decisions/002-security.md b/.features/skill/decisions/002-security.md new file mode 100644 index 0000000..dc671c5 --- /dev/null +++ b/.features/skill/decisions/002-security.md @@ -0,0 +1,35 @@ +# 002: Skill 上传安全措施 + +## 状态 +已采纳 (Accepted) + +## 上下文 +用户可以上传 ZIP 格式的技能包,需要防范常见的安全攻击。 + +## 决策 + +### 安全防护措施 + +| 威胁 | 防护措施 | +|------|---------| +| ZipSlip 攻击 | 检查每个文件的解压路径 | +| 路径遍历 | 验证 `bot_id` 和 `skill_name` 格式 | +| Zip 炸弹 | 压缩比检查(最大 100:1) | +| 磁盘空间滥用 | 上传 50MB,解压后最大 500MB | +| 符号链接攻击 | 禁止解压包含符号链接的文件 | + +### 限制规则 +```python +MAX_UPLOAD_SIZE = 50 * 1024 * 1024 # 50MB +MAX_EXTRACTED_SIZE = 500 * 1024 * 1024 # 500MB +MAX_COMPRESSION_RATIO = 100 # 100:1 +``` + +## 结果 +- 完整的上传验证链 +- 防止恶意文件攻击 +- 资源使用可控 + +## 替代方案 +1. 使用沙箱容器解压(拒绝:复杂度高) +2. 仅允许预定义技能(拒绝:限制用户自定义能力) diff --git a/Dockerfile b/Dockerfile index 976dfdb..f178064 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ WORKDIR /app ENV PYTHONPATH=/app ENV PYTHONUNBUFFERED=1 -# 安装系统依赖 +# 安装系统依赖(含 LibreOffice 和 sharp 所需的 libvips) RUN apt-get update && apt-get install -y \ curl \ wget \ @@ -16,6 +16,11 @@ RUN apt-get update && apt-get install -y \ ca-certificates \ libpq-dev \ chromium \ + libreoffice-writer-nogui \ + libreoffice-calc-nogui \ + libreoffice-impress-nogui \ + libvips-dev \ + fonts-noto-cjk \ && rm -rf /var/lib/apt/lists/* # 安装Node.js (支持npx命令) @@ -35,7 +40,7 @@ RUN pip install --no-cache-dir -r requirements.txt # 安装 Playwright 并下载 Chromium RUN pip install --no-cache-dir playwright && \ playwright install chromium -RUN npm install -g playwright && \ +RUN npm install -g playwright sharp && \ npx playwright install chromium # 复制应用代码 diff --git a/Dockerfile.modelscope b/Dockerfile.modelscope index f5fbd55..b1dd0f0 100644 --- a/Dockerfile.modelscope +++ b/Dockerfile.modelscope @@ -8,7 +8,7 @@ WORKDIR /app ENV PYTHONPATH=/app ENV PYTHONUNBUFFERED=1 -# 安装系统依赖 +# 安装系统依赖(含 LibreOffice 和 sharp 所需的 libvips) RUN sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources && \ apt-get update && apt-get install -y \ curl \ @@ -17,6 +17,11 @@ RUN sed -i 's|http://deb.debian.org|http://mirrors.aliyun.com|g' /etc/apt/source ca-certificates \ libpq-dev \ chromium \ + libreoffice-writer-nogui \ + libreoffice-calc-nogui \ + libreoffice-impress-nogui \ + libvips-dev \ + fonts-noto-cjk \ && rm -rf /var/lib/apt/lists/* # 安装Node.js (支持npx命令) @@ -36,7 +41,7 @@ RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ -r req # 安装 Playwright 并下载 Chromium RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ playwright && \ playwright install chromium -RUN npm install -g playwright && \ +RUN npm install -g playwright sharp && \ npx playwright install chromium # 安装modelscope diff --git a/agent/prompt_loader.py b/agent/prompt_loader.py index d5bfe6b..6fe7142 100644 --- a/agent/prompt_loader.py +++ b/agent/prompt_loader.py @@ -138,33 +138,59 @@ async def load_system_prompt_async(config) -> str: -def replace_mcp_placeholders(mcp_settings: List[Dict], dataset_dir: str, bot_id: str, dataset_ids: List[str]) -> List[Dict]: +def replace_mcp_placeholders(mcp_settings: List[Dict], dataset_dir: str, bot_id: str, dataset_ids: List[str], shell_env: Optional[Dict[str, str]] = None) -> List[Dict]: """ 替换 MCP 配置中的占位符 + + 支持的占位符来源(优先级从高到低): + 1. 内置变量: {dataset_dir}, {bot_id}, {dataset_ids} + 2. shell_env 中的自定义环境变量 + 3. 系统环境变量 (os.environ) """ if not mcp_settings or not isinstance(mcp_settings, list): return mcp_settings dataset_id_str = ','.join(dataset_ids) if dataset_ids else '' + # 构建占位符映射:系统环境变量 < shell_env < 内置变量(优先级递增) + import re + placeholders = {} + placeholders.update(os.environ) + if shell_env: + placeholders.update(shell_env) + placeholders.update({ + 'dataset_dir': dataset_dir, + 'bot_id': bot_id, + 'dataset_ids': dataset_id_str, + }) + + def _safe_format(s: str) -> str: + """安全地替换字符串中的占位符,未匹配的占位符保持原样""" + try: + def _replacer(match): + key = match.group(1) + return placeholders.get(key, match.group(0)) + return re.sub(r'\{(\w+)\}', _replacer, s) + except Exception: + return s + def replace_placeholders_in_obj(obj): """递归替换对象中的占位符""" if isinstance(obj, dict): for key, value in obj.items(): if key == 'args' and isinstance(value, list): - # 特别处理 args 列表 - obj[key] = [item.format(dataset_dir=dataset_dir, bot_id=bot_id, dataset_ids=dataset_id_str) if isinstance(item, str) else item + obj[key] = [_safe_format(item) if isinstance(item, str) else item for item in value] elif isinstance(value, (dict, list)): obj[key] = replace_placeholders_in_obj(value) elif isinstance(value, str): - obj[key] = value.format(dataset_dir=dataset_dir, bot_id=bot_id, dataset_ids=dataset_id_str) + obj[key] = _safe_format(value) elif isinstance(obj, list): - return [replace_placeholders_in_obj(item) if isinstance(item, (dict, list)) else - item.format(dataset_dir=dataset_dir, bot_id=bot_id, dataset_ids=dataset_id_str) if isinstance(item, str) else item + return [replace_placeholders_in_obj(item) if isinstance(item, (dict, list)) else + _safe_format(item) if isinstance(item, str) else item for item in obj] return obj - + return replace_placeholders_in_obj(mcp_settings) async def load_mcp_settings_async(config) -> List[Dict]: @@ -269,7 +295,8 @@ async def load_mcp_settings_async(config) -> List[Dict]: # 替换 MCP 配置中的 {dataset_dir} 占位符 if dataset_dir is None: dataset_dir = "" - merged_settings = replace_mcp_placeholders(merged_settings, dataset_dir, bot_id, dataset_ids) + shell_env = getattr(config, 'shell_env', None) or {} + merged_settings = replace_mcp_placeholders(merged_settings, dataset_dir, bot_id, dataset_ids, shell_env) return merged_settings diff --git a/docker-compose-with-pgsql.yml b/docker-compose-with-pgsql.yml index e0332dd..cf998f0 100644 --- a/docker-compose-with-pgsql.yml +++ b/docker-compose-with-pgsql.yml @@ -31,7 +31,6 @@ services: # 应用配置 - BACKEND_HOST=http://api-dev.gbase.ai - MAX_CONTEXT_TOKENS=262144 - - DEFAULT_THINKING_ENABLE=true # PostgreSQL 配置 - CHECKPOINT_DB_URL=postgresql://postgres:E5ACJo6zJub4QS@postgres:5432/agent_db - R2_UPLOAD_CONFIG=/app/config/local-upload.yaml diff --git a/docker-compose.yml b/docker-compose.yml index d0886a6..16a757e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,6 @@ services: # 应用配置 - BACKEND_HOST=http://api-dev.gbase.ai - MAX_CONTEXT_TOKENS=262144 - - DEFAULT_THINKING_ENABLE=true - R2_UPLOAD_CONFIG=/app/config/s3-upload-sparticle.yaml volumes: # 挂载项目数据目录 diff --git a/prompt/system_prompt.md b/prompt/system_prompt.md index 632af9e..32439de 100644 --- a/prompt/system_prompt.md +++ b/prompt/system_prompt.md @@ -80,3 +80,15 @@ Trace Id: {trace_id} - Even when the user writes in a different language, you MUST still reply in [{language}]. - Do NOT mix languages. Do NOT fall back to English or any other language under any circumstances. - Technical terms, code identifiers, file paths, and tool names may remain in their original form, but all surrounding text MUST be in [{language}]. + +**Citation Requirement (RAG Only)**: When answering questions based on `rag_retrieve` tool results, you MUST add XML citation tags for factual claims derived from the knowledge base. + +**MANDATORY FORMAT**: `The cited factual claim ` + +**Citation Rules**: +- The citation tag MUST be placed immediately after the factual claim or paragraph +- The `file` attribute MUST use the exact `File ID` from `rag_retrieve` document +- The `page` attribute MUST use the exact `Page Number` from `rag_retrieve` document +- If multiple sources support the same claim, include separate citation tags for each source +- Example: `According to the policy, returns are accepted within 30 days .` +- This requirement ONLY applies when using `rag_retrieve` results to answer questions. diff --git a/utils/api_models.py b/utils/api_models.py index 5985056..645fba6 100644 --- a/utils/api_models.py +++ b/utils/api_models.py @@ -5,7 +5,6 @@ API data models and response schemas. from typing import Dict, List, Optional, Any, AsyncGenerator from pydantic import BaseModel, Field, field_validator, ConfigDict -from utils.settings import DEFAULT_THINKING_ENABLE class Message(BaseModel): role: str @@ -52,7 +51,7 @@ class ChatRequest(BaseModel): mcp_settings: Optional[List[Dict]] = None user_identifier: Optional[str] = "" session_id: Optional[str] = None - enable_thinking: Optional[bool] = DEFAULT_THINKING_ENABLE + enable_thinking: Optional[bool] = False skills: Optional[List[str]] = None enable_memory: Optional[bool] = False shell_env: Optional[Dict[str, str]] = None diff --git a/utils/settings.py b/utils/settings.py index 61c38bc..ebb60d5 100644 --- a/utils/settings.py +++ b/utils/settings.py @@ -35,8 +35,6 @@ SENTENCE_TRANSFORMER_MODEL = os.getenv("SENTENCE_TRANSFORMER_MODEL", "TaylorAI/g TOOL_OUTPUT_MAX_LENGTH = int(SUMMARIZATION_MAX_TOKENS/4) TOOL_OUTPUT_TRUNCATION_STRATEGY = os.getenv("TOOL_OUTPUT_TRUNCATION_STRATEGY", "smart") -# THINKING ENABLE -DEFAULT_THINKING_ENABLE = os.getenv("DEFAULT_THINKING_ENABLE", "true") == "true" # WebDAV Authentication From 0c9c36cc54e6d4e1458ce1c2f1413a726b0aa994 Mon Sep 17 00:00:00 2001 From: autobee-sparticle Date: Fri, 27 Mar 2026 18:34:12 +0900 Subject: [PATCH 04/17] =?UTF-8?q?fix:=20=E7=A6=81=E6=AD=A2=20NOVARE=20?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=8E=A7=E5=88=B6=E4=BA=8C=E6=AC=A1=E7=A1=AE?= =?UTF-8?q?=E8=AE=A4=E5=BE=AA=E7=8E=AF=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add page number * feat: add skill feature memory 添加 skill 功能的 feature memory,记录技能包管理服务和 Hook 系统的核心信息。 Co-Authored-By: Claude Opus 4.5 * feat(skill): add feature memory with changelog and decisions 添加 skill 功能的完整记忆文档: Changelog: - 2025-Q4: 初始实现 (GRPC 层 + 内置 skills) - 2026-Q1: API 完善 (REST API + Hook 系统) Design Decisions: - 001: Skill 架构设计 (目录结构、Hook 系统) - 002: 上传安全措施 (ZipSlip、路径遍历防护) Co-Authored-By: Claude Opus 4.5 * soffice sharp 支持 * shell_env support * fix: 禁止 NOVARE 设备控制二次确认循环 - 添加"禁止二次确认"最高优先级规则,明确用户确认后必须立即执行工具调用 - 扩展确认关键词列表,增加更多日语确认表达(お願いします、はい、うん等) - 添加正确/错误流程示例,防止模型循环询问确认 - 强化规则指南中的确认执行逻辑 Fixes: sparticleinc/mygpt-frontend#2303 Co-Authored-By: Claude Opus 4.6 (1M context) --------- Co-authored-by: 朱潮 Co-authored-by: Claude Opus 4.5 Co-authored-by: zhuchao --- prompt/novare.md | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/prompt/novare.md b/prompt/novare.md index d1257e3..709646a 100644 --- a/prompt/novare.md +++ b/prompt/novare.md @@ -161,8 +161,8 @@ - 如果用户指定了具体档位(如"调到强"),直接使用指定档位 - **边界情况**:如果已达到最高档(强)或最低档(弱)无法继续调整,告知用户并主动建议调整温度 - 回复格式:"風量は既に『強/弱』になっていますので、これ以上調整できません。代わりに温度を調整しますか?" - 6. **若用户已明确确认**:直接调用【设备控制】工具执行操作。 - 7. **若用户未确认且为新请求**:向用户发送确认提示:"即将为您 [操作内容] [设备名称] [具体参数],是否确认?",待用户确认后再执行。 + 6. **若用户已明确确认**:**立即**调用【设备控制】工具执行操作,不做任何额外确认或复述。确认后的唯一动作就是调用工具。 + 7. **若用户未确认且为新请求**:向用户发送确认提示:"即将为您 [操作内容] [设备名称] [具体参数],是否确认?",待用户确认后再执行。每个操作只确认一次。 4. 查询人员信息/wowtalk账号/人员位置 - **条件**:用户意图为查找某人、员工、同事或房间位置。 @@ -209,15 +209,43 @@ - 影响范围大的操作:影响整个房间或楼层的设备控制 ### 用户确认意图推理 -- 用户明确确认:如回复“确认”、“好的”、“是的”、“拜托了”、“よろしく”、“请”、“please”等肯定性语气的内容。 -- 用户意图重申:用户完整或核心重复当前待执行的操作指令。(例如,提示“room302の照明1台を明るさ50%に調整してもよろしいですか?”,用户回复“room302の照明を明るさ50%に変更”) +- 用户明确确认:如回复”确认”、”好的”、”是的”、”拜托了”、”よろしく”、”请”、”please”、”お願いします”、”お願い”、”はい”、”うん”、”ええ”、”了解”、”OK”、”分かりました”、”そうしてください”、”それでお願い”等肯定性语气的内容。 +- 用户意图重申:用户完整或核心重复当前待执行的操作指令。(例如,提示”room302の照明1台を明るさ50%に調整してもよろしいですか?”,用户回复”room302の照明を明るさ50%に変更”) - 同一设备免重复确认:如果用户在当前会话中已经对某个设备的操作进行过确认,后续针对**同一设备**的操作可直接执行,无需再次确认。判定标准为: - 1. **同一设备的不同操作**:用户已确认过对某设备的控制操作后,后续对该设备的其他操作无需再次确认(如已确认关闭Define Room4的灯光,之后用户说"把灯打开",可直接执行) - 2. **同一轮对话意图**:用户在一轮连续交互中围绕同一目标发出的多步操作(如用户确认"关闭Define Room4的灯光"后,系统依次关闭该房间内多个灯光设备,无需逐个确认) + 1. **同一设备的不同操作**:用户已确认过对某设备的控制操作后,后续对该设备的其他操作无需再次确认(如已确认关闭Define Room4的灯光,之后用户说”把灯打开”,可直接执行) + 2. **同一轮对话意图**:用户在一轮连续交互中围绕同一目标发出的多步操作(如用户确认”关闭Define Room4的灯光”后,系统依次关闭该房间内多个灯光设备,无需逐个确认) 3. **同一指令的延续执行**:用户确认某操作后,该操作因技术原因需要分步执行的后续步骤(如批量控制多个设备时,确认一次即可全部执行) - 4. **上下文明确的追加操作**:用户在已确认的操作基础上追加相同类型的操作,且目标明确无歧义(如已确认打开A房间空调后,用户说"B房间也一样",可直接执行) + 4. **上下文明确的追加操作**:用户在已确认的操作基础上追加相同类型的操作,且目标明确无歧义(如已确认打开A房间空调后,用户说”B房间也一样”,可直接执行) - 不同事项仍需确认:当操作涉及**未曾确认过的新设备**,或操作类型发生本质变化时(如从设备控制切换到消息通知),仍需重新确认 +### ⚠️ 禁止二次确认(最高优先级规则) +**对于同一个操作请求,最多只能向用户确认一次。用户确认后,必须立即调用工具执行,绝对禁止再次询问确认。** + +核心规则: +1. **一次确认,立即执行**:当你向用户发出确认提示后,用户回复确认,你的下一步动作**必须且只能是**调用对应的工具(如 dxcore_update_device_status)执行操作。不允许生成任何额外的确认、复述或再次询问。 +2. **禁止循环确认**:如果聊天记录中已经存在你发出的确认提示和用户的确认回复,则该操作已被确认,不得以任何理由再次要求确认。 +3. **确认后禁止的行为**: + - ❌ 再次询问”もう一度確認いただけますか?” + - ❌ 再次复述操作内容并要求确认 + - ❌ 以不同措辞重新询问同一操作的确认 + - ❌ 生成过渡性文字后再次要求确认 + +**正确流程示例**: +``` +用户: “Dr3の照明を30%にして” +AI: “ディファインルーム3の照明を30%に調整してもよろしいですか?” +用户: “お願いします” +AI: [立即调用 dxcore_update_device_status 执行] → “照明を30%に調整しました。” +``` + +**错误流程(绝对禁止)**: +``` +用户: “Dr3の照明を30%にして” +AI: “ディファインルーム3の照明を30%に調整してもよろしいですか?” +用户: “お願いします” +AI: “もう一度確認いただければ実行いたします” ← ❌ 禁止! +``` + ## 上下文推理示例 ### 设备控制场景 From 2bc071645f5e0878ce7ce6663040f7d1297e9646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Mon, 30 Mar 2026 21:22:19 +0800 Subject: [PATCH 05/17] merge from onprem --- prompt/system_prompt.md | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/prompt/system_prompt.md b/prompt/system_prompt.md index c1d4812..c978779 100644 --- a/prompt/system_prompt.md +++ b/prompt/system_prompt.md @@ -91,15 +91,3 @@ Trace Id: {trace_id} - Even when the user writes in a different language, you MUST still reply in [{language}]. - Do NOT mix languages. Do NOT fall back to English or any other language under any circumstances. - Technical terms, code identifiers, file paths, and tool names may remain in their original form, but all surrounding text MUST be in [{language}]. - -**Citation Requirement (RAG Only)**: When answering questions based on `rag_retrieve` tool results, you MUST add XML citation tags for factual claims derived from the knowledge base. - -**MANDATORY FORMAT**: `The cited factual claim ` - -**Citation Rules**: -- The citation tag MUST be placed immediately after the factual claim or paragraph -- The `file` attribute MUST use the exact `File ID` from `rag_retrieve` document -- The `page` attribute MUST use the exact `Page Number` from `rag_retrieve` document -- If multiple sources support the same claim, include separate citation tags for each source -- Example: `According to the policy, returns are accepted within 30 days .` -- This requirement ONLY applies when using `rag_retrieve` results to answer questions. From 3b9c7165a93a701f41219ceacd2c33a335a432a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Mon, 30 Mar 2026 23:17:47 +0800 Subject: [PATCH 06/17] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E8=B0=83=E5=BA=A6=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=EF=BC=88schedule-job=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 schedule-job skill,支持 cron 周期任务和一次性定时任务 - 新增 schedule_manager.py CLI 工具(list/add/edit/delete/toggle/logs) - 新增 ScheduleExecutor 全局异步调度器,每 60s 扫描到期任务并调用 agent 执行 - 任务数据存储在 projects/robot/{bot_id}/users/{user_id}/tasks.yaml - 执行结果写入 task_logs/execution.log - 集成到 FastAPI lifespan 生命周期管理 - 添加 croniter、pyyaml 依赖 Co-Authored-By: Claude Opus 4.6 (1M context) --- fastapi_app.py | 19 + plans/schedule-job.md | 249 +++++++++++ poetry.lock | 18 +- pyproject.toml | 2 + requirements.txt | 1 + services/schedule_executor.py | 306 +++++++++++++ .../schedule-job/.claude-plugin/plugin.json | 7 + skills/schedule-job/SKILL.md | 144 ++++++ .../schedule-job/scripts/schedule_manager.py | 421 ++++++++++++++++++ utils/settings.py | 14 + 10 files changed, 1180 insertions(+), 1 deletion(-) create mode 100644 plans/schedule-job.md create mode 100644 services/schedule_executor.py create mode 100644 skills/schedule-job/.claude-plugin/plugin.json create mode 100644 skills/schedule-job/SKILL.md create mode 100644 skills/schedule-job/scripts/schedule_manager.py diff --git a/fastapi_app.py b/fastapi_app.py index 20977f6..1f05332 100644 --- a/fastapi_app.py +++ b/fastapi_app.py @@ -108,6 +108,7 @@ async def lifespan(app: FastAPI): close_global_mem0 ) from utils.settings import CHECKPOINT_CLEANUP_ENABLED, MEM0_ENABLED + from utils.settings import SCHEDULE_ENABLED # 1. 初始化共享的数据库连接池 db_pool_manager = await init_global_db_pool() @@ -141,10 +142,28 @@ async def lifespan(app: FastAPI): db_pool_manager.start_cleanup_scheduler() logger.info("Checkpoint cleanup scheduler started") + # 6. 启动定时任务调度器 + schedule_executor = None + if SCHEDULE_ENABLED: + try: + from services.schedule_executor import get_schedule_executor + schedule_executor = get_schedule_executor() + schedule_executor.start() + logger.info("Schedule executor started") + except Exception as e: + logger.warning(f"Schedule executor start failed (non-fatal): {e}") + yield # 关闭时清理(按相反顺序) logger.info("Shutting down...") + # 关闭定时任务调度器 + if schedule_executor: + try: + await schedule_executor.stop() + logger.info("Schedule executor stopped") + except Exception as e: + logger.warning(f"Schedule executor stop failed (non-fatal): {e}") # 关闭 Mem0 if MEM0_ENABLED: try: diff --git a/plans/schedule-job.md b/plans/schedule-job.md new file mode 100644 index 0000000..b1b27ae --- /dev/null +++ b/plans/schedule-job.md @@ -0,0 +1,249 @@ +# feat: Schedule Job 定时任务系统 + +## 概述 + +为每个用户的每个 bot 提供定时任务能力,支持 cron 周期任务和一次性定时任务。到期后系统自动调用 Agent 执行任务,结果写入日志并通过 PostAgent Hook 推送通知。 + +## 背景 + +### 当前状态 + +系统已有的后台任务机制: +- **Huey 任务队列** (`task_queue/`): SQLite 后端,用于文件处理 +- **AsyncIO 后台任务**: `asyncio.create_task()` 用于非阻塞操作 +- **Checkpoint 清理调度器** (`agent/db_pool_manager.py`): asyncio loop 方式 + +**缺失能力**: +- 无法为用户设置周期性或定时触发的 Agent 任务 +- 无 cron 式调度器 +- 无用户级任务管理接口 + +### 设计目标 + +- 用户通过 AI 对话即可创建/管理定时任务 +- 任务数据以 YAML 文件存储在用户专属目录,便于查看和备份 +- 全局调度器以 asyncio 后台任务运行,无需额外进程 +- 复用现有 `create_agent_and_generate_response()` 执行任务 + +## 架构设计 + +``` +┌─────────────────┐ ┌──────────────────────┐ ┌────────────────────┐ +│ AI Agent 对话 │ │ 全局异步调度器 │ │ Agent 执行引擎 │ +│ (Shell脚本工具) │────▶│ (asyncio loop) │────▶│ create_agent_and_ │ +│ 增删改查 tasks │ │ 每60s扫描tasks.yaml │ │ generate_response │ +└─────────────────┘ └──────────────────────┘ └────────────────────┘ + │ │ │ + ▼ ▼ ▼ + tasks.yaml tasks.yaml task_logs/ + (用户任务数据) (更新执行时间) (执行结果日志) +``` + +## 数据模型 + +### tasks.yaml + +**存储路径**: `projects/robot/{bot_id}/users/{user_id}/tasks.yaml` + +```yaml +tasks: + - id: "task_20260330143000_abc123" # 自动生成 + name: "每日新闻摘要" # 任务名称 + type: "cron" # cron | once + schedule: "0 9 * * *" # cron 表达式(type=cron 时) + scheduled_at: null # ISO 8601 UTC(type=once 时) + timezone: "Asia/Tokyo" # 用户时区,用于 cron 解析 + message: "请帮我总结今天的科技新闻" # 发送给 agent 的消息 + status: "active" # active | paused | done | expired + created_at: "2026-03-30T05:30:00Z" + last_executed_at: null # 上次执行时间(UTC) + next_run_at: "2026-03-31T00:00:00Z" # 下次执行时间(UTC,调度器用此判断) + execution_count: 0 # 已执行次数 +``` + +**关键设计决策**: +- 所有时间统一 UTC 存储,cron 表达式结合 timezone 字段在本地时间计算后转 UTC +- `next_run_at` 预计算,调度器只需简单比较时间戳即可判断是否到期 +- 一次性任务执行后 status 自动变为 `done` + +### execution.log + +**存储路径**: `projects/robot/{bot_id}/users/{user_id}/task_logs/execution.log` + +```yaml +- task_id: "task_20260330143000_abc123" + task_name: "每日新闻摘要" + executed_at: "2026-03-31T00:00:15Z" + status: "success" # success | error + response: "今天的科技新闻摘要:..." + duration_ms: 12500 +``` + +保留最近 100 条日志,自动清理旧记录。 + +## 新增文件 + +### 1. `skills/schedule-job/scripts/schedule_manager.py` + +Shell 命令行工具(argparse CLI),AI 通过 shell 调用来管理用户的定时任务。 + +**环境变量输入**(通过 plugin hook 机制传入): +- `BOT_ID`: 当前 bot ID +- `USER_IDENTIFIER`: 当前用户标识 + +**支持的命令**: + +| 命令 | 说明 | 示例 | +|------|------|------| +| `list` | 列出任务 | `list --format brief` | +| `add` | 添加任务 | `add --name "每日新闻" --type cron --schedule "0 9 * * *" --timezone "Asia/Tokyo" --message "..."` | +| `edit` | 编辑任务 | `edit --schedule "0 10 * * 1-5"` | +| `delete` | 删除任务 | `delete ` | +| `toggle` | 暂停/恢复 | `toggle ` | +| `logs` | 查看日志 | `logs --task-id --limit 10` | + +**核心逻辑**: +- 一次性任务 `--scheduled-at` 接受 ISO 8601 格式(带时区偏移),内部转 UTC +- cron 任务通过 `croniter` + timezone 计算 `next_run_at` +- 自动创建 `users/{user_id}/` 目录 + +### 2. `skills/schedule-job/SKILL.md` + +Skill 描述文件,注入 AI prompt,告诉 AI: +- 如何使用 schedule_manager.py CLI +- cron 表达式语法说明 +- 时区映射规则(语言 → 时区) +- 使用示例 + +### 3. `skills/schedule-job/.claude-plugin/plugin.json` + +```json +{ + "hooks": { + "PrePrompt": { + "command": "python scripts/schedule_manager.py list --format brief" + } + } +} +``` + +通过 PrePrompt hook,AI 每次对话时自动看到用户当前的任务列表。 + +### 4. `services/schedule_executor.py` + +全局异步调度器,核心类 `ScheduleExecutor`。 + +**运行机制**:参考 `db_pool_manager.py` 的 `_cleanup_loop()` 模式 + +``` +启动 → asyncio.create_task(_scan_loop) + ↓ + 每 SCAN_INTERVAL 秒 + ↓ + 遍历 projects/robot/*/users/*/tasks.yaml + ↓ + 找到 status=active && next_run_at <= now 的任务 + ↓ + asyncio.create_task(_execute_task) → 受 Semaphore 并发控制 + ↓ + 构建 AgentConfig → create_agent_and_generate_response(stream=False) + ↓ + 写入 execution.log → 更新 tasks.yaml (next_run_at / status) +``` + +**并发与防重复**: +- `_executing_tasks: set` 记录正在执行的任务 ID,防止同一任务被重复触发 +- `asyncio.Semaphore(MAX_CONCURRENT)` 限制最大并发数(默认 5) + +**任务执行后更新**: +- cron 任务:使用 croniter 计算下次 UTC 时间写入 `next_run_at` +- once 任务:将 status 设为 `done`,清空 `next_run_at` +- 失败时也更新 `next_run_at`,避免无限重试 + +**Agent 调用构建**: +```python +bot_config = await fetch_bot_config(bot_id) +project_dir = create_project_directory(dataset_ids, bot_id, skills) +config = AgentConfig( + bot_id=bot_id, + user_identifier=user_id, + session_id=f"schedule_{task_id}", # 专用 session + stream=False, + tool_response=False, + messages=[{"role": "user", "content": task["message"]}], + ... # 其余参数从 bot_config 获取 +) +result = await create_agent_and_generate_response(config) +``` + +## 修改文件 + +### `pyproject.toml` + +新增依赖: +```toml +"croniter (>=2.0.0,<4.0.0)", +"pyyaml (>=6.0,<7.0)", +``` + +### `utils/settings.py` + +新增环境变量: +```python +SCHEDULE_ENABLED = os.getenv("SCHEDULE_ENABLED", "true") == "true" +SCHEDULE_SCAN_INTERVAL = int(os.getenv("SCHEDULE_SCAN_INTERVAL", "60")) # 扫描间隔(秒) +SCHEDULE_MAX_CONCURRENT = int(os.getenv("SCHEDULE_MAX_CONCURRENT", "5")) # 最大并发数 +``` + +### `fastapi_app.py` + +在 lifespan 中集成调度器生命周期: +- **startup**: `schedule_executor.start()` — 在 checkpoint 清理调度器之后启动 +- **shutdown**: `await schedule_executor.stop()` — 在 Mem0 关闭之前停止 + +## 复用的现有组件 + +| 组件 | 路径 | 用途 | +|------|------|------| +| `create_agent_and_generate_response()` | `routes/chat.py:246` | 执行 agent 调用 | +| `fetch_bot_config()` | `utils/fastapi_utils.py` | 获取 bot 配置 | +| `create_project_directory()` | `utils/fastapi_utils.py` | 创建项目目录 | +| `AgentConfig` | `agent/agent_config.py` | 构建执行配置 | +| `_cleanup_loop()` 模式 | `agent/db_pool_manager.py:240` | asyncio 后台循环参考 | +| `execute_hooks('PostAgent')` | `agent/plugin_hook_loader.py` | 执行结果通知 | + +## 验证方式 + +```bash +# 1. 启动服务 +poetry run uvicorn fastapi_app:app --host 0.0.0.0 --port 8001 + +# 2. CLI 创建测试任务 +BOT_ID= USER_IDENTIFIER=test_user \ + poetry run python skills/schedule-job/scripts/schedule_manager.py add \ + --name "测试任务" --type once \ + --scheduled-at "$(date -u -v+2M '+%Y-%m-%dT%H:%M:%SZ')" \ + --message "你好,这是一个定时任务测试" + +# 3. 检查 tasks.yaml +cat projects/robot//users/test_user/tasks.yaml + +# 4. 等待调度器执行(最多 60 秒),观察应用日志 + +# 5. 检查执行结果 +cat projects/robot//users/test_user/task_logs/execution.log + +# 6. 通过 API 对话测试 skill +curl -X POST http://localhost:8001/api/v2/chat/completions \ + -H 'authorization: Bearer ' \ + -H 'content-type: application/json' \ + -d '{"messages":[{"role":"user","content":"帮我创建一个每天早上9点的定时任务"}],"stream":false,"bot_id":"","user_identifier":"test_user"}' +``` + +## 后续扩展方向 + +- **任务失败重试**: 可增加 `max_retries` 和 `retry_delay` 字段 +- **任务执行超时**: 为单个任务设置超时限制 +- **通知渠道**: 集成飞书/邮件等 PostAgent Hook 推送执行结果 +- **Web 管理界面**: 提供 REST API 查询/管理定时任务 +- **任务模板**: 预设常用任务模板(每日新闻、天气播报等) diff --git a/poetry.lock b/poetry.lock index 91a8dd0..64027d2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -677,6 +677,22 @@ files = [ ] markers = {main = "platform_system == \"Windows\"", dev = "sys_platform == \"win32\""} +[[package]] +name = "croniter" +version = "3.0.4" +description = "croniter provides iteration for datetime object with cron like format" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,>=2.6" +groups = ["main"] +files = [ + {file = "croniter-3.0.4-py2.py3-none-any.whl", hash = "sha256:96e14cdd5dcb479dd48d7db14b53d8434b188dfb9210448bef6f65663524a6f0"}, + {file = "croniter-3.0.4.tar.gz", hash = "sha256:f9dcd4bdb6c97abedb6f09d6ed3495b13ede4d4544503fa580b6372a56a0c520"}, +] + +[package.dependencies] +python-dateutil = "*" +pytz = ">2021.1" + [[package]] name = "cryptography" version = "46.0.5" @@ -6619,4 +6635,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt [metadata] lock-version = "2.1" python-versions = ">=3.12,<4.0" -content-hash = "dbe40b78bc1b7796331da5e14512ae6992f783cadca977bc66b098642d1e8cd9" +content-hash = "3ed06e25ab7936d04d523544c24df6e8678eda0e99388ed1e4de0acbb8e3e63e" diff --git a/pyproject.toml b/pyproject.toml index a14f34b..f9143c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,8 @@ dependencies = [ "json-repair (>=0.29.0,<0.30.0)", "tiktoken (>=0.5.0,<1.0.0)", "wsgidav (>=4.3.3,<5.0.0)", + "croniter (>=2.0.0,<4.0.0)", + "pyyaml (>=6.0,<7.0)", ] [tool.poetry.requires-plugins] diff --git a/requirements.txt b/requirements.txt index a7a07e5..0479a06 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,6 +19,7 @@ chardet==5.2.0 ; python_version >= "3.12" and python_version < "4.0" charset-normalizer==3.4.4 ; python_version >= "3.12" and python_version < "4.0" click==8.3.0 ; python_version >= "3.12" and python_version < "4.0" colorama==0.4.6 ; python_version >= "3.12" and python_version < "4.0" and platform_system == "Windows" +croniter==3.0.4 ; python_version >= "3.12" and python_version < "4.0" cryptography==46.0.5 ; python_version >= "3.12" and python_version < "4.0" daytona-api-client-async==0.127.0 ; python_version >= "3.12" and python_version < "4.0" daytona-api-client==0.127.0 ; python_version >= "3.12" and python_version < "4.0" diff --git a/services/schedule_executor.py b/services/schedule_executor.py new file mode 100644 index 0000000..f926be8 --- /dev/null +++ b/services/schedule_executor.py @@ -0,0 +1,306 @@ +""" +全局定时任务调度器 + +扫描所有 projects/robot/{bot_id}/users/{user_id}/tasks.yaml 文件, +找到到期的任务并调用 create_agent_and_generate_response 执行。 +""" + +import asyncio +import logging +import time +import yaml +from datetime import datetime, timezone, timedelta +from pathlib import Path +from typing import Optional + +logger = logging.getLogger('app') + + +class ScheduleExecutor: + """定时任务调度器,以 asyncio 后台任务运行""" + + def __init__(self, scan_interval: int = 60, max_concurrent: int = 5): + self._scan_interval = scan_interval + self._max_concurrent = max_concurrent + self._task: Optional[asyncio.Task] = None + self._stop_event = asyncio.Event() + self._executing_tasks: set = set() # 正在执行的任务 ID,防重复 + self._semaphore: Optional[asyncio.Semaphore] = None + + def start(self): + """启动调度器""" + if self._task is not None and not self._task.done(): + logger.warning("Schedule executor is already running") + return + + self._stop_event.clear() + self._semaphore = asyncio.Semaphore(self._max_concurrent) + self._task = asyncio.create_task(self._scan_loop()) + logger.info( + f"Schedule executor started: interval={self._scan_interval}s, " + f"max_concurrent={self._max_concurrent}" + ) + + async def stop(self): + """停止调度器""" + self._stop_event.set() + if self._task and not self._task.done(): + self._task.cancel() + try: + await self._task + except asyncio.CancelledError: + pass + logger.info("Schedule executor stopped") + + async def _scan_loop(self): + """主扫描循环""" + while not self._stop_event.is_set(): + try: + await self._scan_and_execute() + except asyncio.CancelledError: + break + except Exception as e: + logger.error(f"Schedule scan error: {e}") + + # 等待下一次扫描或停止信号 + try: + await asyncio.wait_for( + self._stop_event.wait(), + timeout=self._scan_interval + ) + break # 收到停止信号 + except asyncio.TimeoutError: + pass # 超时继续下一轮扫描 + + async def _scan_and_execute(self): + """扫描所有 tasks.yaml,找到到期任务并触发执行""" + now = datetime.now(timezone.utc) + robot_dir = Path("projects/robot") + + if not robot_dir.exists(): + return + + tasks_files = list(robot_dir.glob("*/users/*/tasks.yaml")) + if not tasks_files: + return + + for tasks_file in tasks_files: + try: + with open(tasks_file, 'r', encoding='utf-8') as f: + data = yaml.safe_load(f) + + if not data or not data.get("tasks"): + continue + + # 从路径提取 bot_id 和 user_id + parts = tasks_file.parts + # 路径格式: .../projects/robot/{bot_id}/users/{user_id}/tasks.yaml + bot_id = parts[-4] + user_id = parts[-2] + + for task in data["tasks"]: + if task.get("status") != "active": + continue + if task["id"] in self._executing_tasks: + continue + + next_run_str = task.get("next_run_at") + if not next_run_str: + continue + + try: + next_run = datetime.fromisoformat(next_run_str) + if next_run.tzinfo is None: + next_run = next_run.replace(tzinfo=timezone.utc) + except (ValueError, TypeError): + logger.warning(f"Invalid next_run_at for task {task['id']}: {next_run_str}") + continue + + if next_run <= now: + # 到期,触发执行 + asyncio.create_task( + self._execute_task(bot_id, user_id, task, tasks_file) + ) + + except Exception as e: + logger.error(f"Error reading {tasks_file}: {e}") + + async def _execute_task(self, bot_id: str, user_id: str, task: dict, tasks_file: Path): + """执行单个到期任务""" + task_id = task["id"] + self._executing_tasks.add(task_id) + start_time = time.time() + + try: + async with self._semaphore: + logger.info(f"Executing scheduled task: {task_id} ({task.get('name', '')}) for bot={bot_id} user={user_id}") + + # 调用 agent + response_text = await self._call_agent(bot_id, user_id, task) + + # 写入日志 + duration_ms = int((time.time() - start_time) * 1000) + self._write_log(bot_id, user_id, task, response_text, "success", duration_ms) + + # 更新 tasks.yaml + self._update_task_after_execution(task_id, tasks_file) + + logger.info(f"Task {task_id} completed in {duration_ms}ms") + + except Exception as e: + duration_ms = int((time.time() - start_time) * 1000) + logger.error(f"Task {task_id} execution failed: {e}") + self._write_log(bot_id, user_id, task, f"ERROR: {e}", "error", duration_ms) + # 即使失败也更新 next_run_at,避免无限重试 + self._update_task_after_execution(task_id, tasks_file) + finally: + self._executing_tasks.discard(task_id) + + async def _call_agent(self, bot_id: str, user_id: str, task: dict) -> str: + """构建 AgentConfig 并调用 agent""" + from routes.chat import create_agent_and_generate_response + from utils.fastapi_utils import fetch_bot_config, create_project_directory + from agent.agent_config import AgentConfig + + bot_config = await fetch_bot_config(bot_id) + project_dir = create_project_directory( + bot_config.get("dataset_ids", []), + bot_id, + bot_config.get("skills") + ) + + messages = [{"role": "user", "content": task["message"]}] + + config = AgentConfig( + bot_id=bot_id, + api_key=bot_config.get("api_key"), + model_name=bot_config.get("model", "qwen3-next"), + model_server=bot_config.get("model_server", ""), + language=bot_config.get("language", "ja"), + system_prompt=bot_config.get("system_prompt"), + mcp_settings=bot_config.get("mcp_settings", []), + user_identifier=user_id, + session_id=f"schedule_{task['id']}", + project_dir=project_dir, + stream=False, + tool_response=False, + messages=messages, + dataset_ids=bot_config.get("dataset_ids", []), + enable_memori=bot_config.get("enable_memory", False), + shell_env=bot_config.get("shell_env") or {}, + ) + + result = await create_agent_and_generate_response(config) + return result.choices[0]["message"]["content"] + + def _update_task_after_execution(self, task_id: str, tasks_file: Path): + """执行后更新 tasks.yaml""" + try: + with open(tasks_file, 'r', encoding='utf-8') as f: + data = yaml.safe_load(f) + + if not data or not data.get("tasks"): + return + + now_utc = datetime.now(timezone.utc).isoformat() + + for task in data["tasks"]: + if task["id"] != task_id: + continue + + task["last_executed_at"] = now_utc + task["execution_count"] = task.get("execution_count", 0) + 1 + + if task["type"] == "once": + task["status"] = "done" + task["next_run_at"] = None + elif task["type"] == "cron" and task.get("schedule"): + # 计算下次执行时间 + task["next_run_at"] = self._compute_next_run( + task["schedule"], + task.get("timezone", "UTC") + ) + break + + with open(tasks_file, 'w', encoding='utf-8') as f: + yaml.dump(data, f, allow_unicode=True, default_flow_style=False, sort_keys=False) + + except Exception as e: + logger.error(f"Failed to update task {task_id}: {e}") + + def _compute_next_run(self, schedule: str, tz: str) -> str: + """计算 cron 任务的下次执行 UTC 时间""" + from croniter import croniter + + # 时区偏移映射 + tz_offsets = { + 'Asia/Shanghai': 8, + 'Asia/Tokyo': 9, + 'UTC': 0, + 'America/New_York': -5, + 'America/Los_Angeles': -8, + 'Europe/London': 0, + 'Europe/Berlin': 1, + } + + offset_hours = tz_offsets.get(tz, 0) + offset = timedelta(hours=offset_hours) + + now_utc = datetime.now(timezone.utc) + now_local = (now_utc + offset).replace(tzinfo=None) + + cron = croniter(schedule, now_local) + next_local = cron.get_next(datetime) + + next_utc = next_local - offset + return next_utc.replace(tzinfo=timezone.utc).isoformat() + + def _write_log(self, bot_id: str, user_id: str, task: dict, + response: str, status: str, duration_ms: int): + """写入执行日志""" + logs_dir = Path("projects/robot") / bot_id / "users" / user_id / "task_logs" + logs_dir.mkdir(parents=True, exist_ok=True) + log_file = logs_dir / "execution.log" + + log_entry = { + "task_id": task["id"], + "task_name": task.get("name", ""), + "executed_at": datetime.now(timezone.utc).isoformat(), + "status": status, + "response": response[:2000] if response else "", # 截断过长响应 + "duration_ms": duration_ms, + } + + # 追加写入 YAML 列表 + existing_logs = [] + if log_file.exists(): + try: + with open(log_file, 'r', encoding='utf-8') as f: + existing_logs = yaml.safe_load(f) or [] + except Exception: + existing_logs = [] + + existing_logs.append(log_entry) + + # 保留最近 100 条日志 + if len(existing_logs) > 100: + existing_logs = existing_logs[-100:] + + with open(log_file, 'w', encoding='utf-8') as f: + yaml.dump(existing_logs, f, allow_unicode=True, default_flow_style=False, sort_keys=False) + + +# 全局单例 +_executor: Optional[ScheduleExecutor] = None + + +def get_schedule_executor() -> ScheduleExecutor: + """获取全局调度器实例""" + global _executor + if _executor is None: + from utils.settings import SCHEDULE_SCAN_INTERVAL, SCHEDULE_MAX_CONCURRENT + _executor = ScheduleExecutor( + scan_interval=SCHEDULE_SCAN_INTERVAL, + max_concurrent=SCHEDULE_MAX_CONCURRENT, + ) + return _executor diff --git a/skills/schedule-job/.claude-plugin/plugin.json b/skills/schedule-job/.claude-plugin/plugin.json new file mode 100644 index 0000000..5d8bf66 --- /dev/null +++ b/skills/schedule-job/.claude-plugin/plugin.json @@ -0,0 +1,7 @@ +{ + "hooks": { + "PrePrompt": { + "command": "python scripts/schedule_manager.py list --format brief" + } + } +} diff --git a/skills/schedule-job/SKILL.md b/skills/schedule-job/SKILL.md new file mode 100644 index 0000000..b085ded --- /dev/null +++ b/skills/schedule-job/SKILL.md @@ -0,0 +1,144 @@ +--- +name: schedule-job +description: 定时任务管理 - 为用户创建、管理和查看定时任务(支持 cron 周期任务和一次性任务) +--- + +# Schedule Job - 定时任务管理 + +管理用户的定时任务,支持 cron 周期性任务和一次性定时任务。任务到期后系统自动执行 AI 对话。 + +## Quick Start + +用户请求创建定时任务时: +1. 确认任务类型(周期 cron / 一次性 once) +2. 确定时间(cron 表达式或具体时间)和时区 +3. 确定发送给 AI 的消息内容 +4. 调用 schedule_manager.py 创建任务 + +## Instructions + +### 工具路径 + +所有操作通过 shell 命令执行: +```bash +python {skill_dir}/scripts/schedule_manager.py [options] +``` + +### 可用命令 + +#### 列出任务 +```bash +python {skill_dir}/scripts/schedule_manager.py list +python {skill_dir}/scripts/schedule_manager.py list --format brief +``` + +#### 添加 Cron 周期任务 +```bash +python {skill_dir}/scripts/schedule_manager.py add \ + --name "任务名称" \ + --type cron \ + --schedule "0 9 * * *" \ + --timezone "Asia/Tokyo" \ + --message "请帮我总结今天的科技新闻" +``` + +#### 添加一次性任务 +```bash +python {skill_dir}/scripts/schedule_manager.py add \ + --name "会议提醒" \ + --type once \ + --scheduled-at "2026-04-01T10:00:00+09:00" \ + --message "提醒我10点有会议" +``` + +#### 编辑任务 +```bash +python {skill_dir}/scripts/schedule_manager.py edit \ + --schedule "0 10 * * 1-5" \ + --message "新的消息内容" +``` + +#### 删除任务 +```bash +python {skill_dir}/scripts/schedule_manager.py delete +``` + +#### 暂停/恢复任务 +```bash +python {skill_dir}/scripts/schedule_manager.py toggle +``` + +#### 查看执行日志 +```bash +python {skill_dir}/scripts/schedule_manager.py logs --limit 10 +python {skill_dir}/scripts/schedule_manager.py logs --task-id +``` + +### 时区映射 + +根据用户语言自动推荐时区: +- 中文 (zh) → Asia/Shanghai (UTC+8) +- 日语 (ja/jp) → Asia/Tokyo (UTC+9) +- 英语 (en) → UTC + +### Cron 表达式说明 + +标准 5 字段格式:`分 时 日 月 星期` + +常用示例: +| 表达式 | 含义 | +|--------|------| +| `0 9 * * *` | 每天 9:00 | +| `0 9 * * 1-5` | 周一到周五 9:00 | +| `30 8 * * 1` | 每周一 8:30 | +| `0 */2 * * *` | 每 2 小时 | +| `0 9,18 * * *` | 每天 9:00 和 18:00 | + +**注意**: cron 表达式的时间基于 --timezone 指定的时区。 + +### 一次性任务时间格式 + +支持 ISO 8601 格式(推荐带时区偏移): +- `2026-04-01T10:00:00+09:00` (日本时间) +- `2026-04-01T01:00:00Z` (UTC) +- `2026-04-01T08:00:00+08:00` (中国时间) + +## Examples + +**用户**: "帮我设置一个每天早上9点的新闻总结任务" + +```bash +python {skill_dir}/scripts/schedule_manager.py add \ + --name "每日新闻总结" \ + --type cron \ + --schedule "0 9 * * *" \ + --timezone "Asia/Tokyo" \ + --message "请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5" +``` + +**用户**: "提醒我明天下午3点开会" + +```bash +python {skill_dir}/scripts/schedule_manager.py add \ + --name "开会提醒" \ + --type once \ + --scheduled-at "2026-03-31T15:00:00+09:00" \ + --message "提醒:你现在有一个会议要参加" +``` + +**用户**: "把每日新闻任务改到早上10点" + +```bash +# 先查看任务列表获取 task_id +python {skill_dir}/scripts/schedule_manager.py list +# 然后编辑 +python {skill_dir}/scripts/schedule_manager.py edit --schedule "0 10 * * *" +``` + +## Guidelines + +- 创建任务前先用 `list` 确认用户已有的任务,避免创建重复任务 +- 根据用户语言自动设置合适的时区 +- message 内容应该是完整的、可独立执行的指令,因为 AI 执行时没有对话上下文 +- 一次性任务的时间不能是过去的时间 +- 编辑任务时只修改用户要求改的字段,不要改动其他字段 diff --git a/skills/schedule-job/scripts/schedule_manager.py b/skills/schedule-job/scripts/schedule_manager.py new file mode 100644 index 0000000..c4536e9 --- /dev/null +++ b/skills/schedule-job/scripts/schedule_manager.py @@ -0,0 +1,421 @@ +#!/usr/bin/env python3 +""" +定时任务管理 CLI 工具 +用于增删改查用户的定时任务,数据存储在 tasks.yaml 文件中。 + +环境变量: + BOT_ID: 当前 bot ID + USER_IDENTIFIER: 当前用户标识 +""" + +import argparse +import os +import sys +import yaml +import json +import string +import random +from datetime import datetime, timezone, timedelta +from pathlib import Path + +try: + from croniter import croniter +except ImportError: + print("Error: croniter is required. Install with: pip install croniter", file=sys.stderr) + sys.exit(1) + + +# 语言到时区的映射 +LANGUAGE_TIMEZONE_MAP = { + 'zh': 'Asia/Shanghai', + 'ja': 'Asia/Tokyo', + 'jp': 'Asia/Tokyo', + 'en': 'UTC', +} + +# 时区到 UTC 偏移(小时) +TIMEZONE_OFFSET_MAP = { + 'Asia/Shanghai': 8, + 'Asia/Tokyo': 9, + 'UTC': 0, + 'America/New_York': -5, + 'America/Los_Angeles': -8, + 'Europe/London': 0, + 'Europe/Berlin': 1, +} + + +def get_tasks_dir(bot_id: str, user_id: str) -> Path: + """获取用户任务目录路径""" + base = Path(os.getenv("PROJECT_ROOT", ".")) + return base / "projects" / "robot" / bot_id / "users" / user_id + + +def get_tasks_file(bot_id: str, user_id: str) -> Path: + """获取 tasks.yaml 文件路径""" + return get_tasks_dir(bot_id, user_id) / "tasks.yaml" + + +def get_logs_dir(bot_id: str, user_id: str) -> Path: + """获取任务日志目录""" + return get_tasks_dir(bot_id, user_id) / "task_logs" + + +def load_tasks(bot_id: str, user_id: str) -> dict: + """加载 tasks.yaml""" + tasks_file = get_tasks_file(bot_id, user_id) + if not tasks_file.exists(): + return {"tasks": []} + with open(tasks_file, 'r', encoding='utf-8') as f: + data = yaml.safe_load(f) + return data if data and "tasks" in data else {"tasks": []} + + +def save_tasks(bot_id: str, user_id: str, data: dict): + """保存 tasks.yaml""" + tasks_file = get_tasks_file(bot_id, user_id) + tasks_file.parent.mkdir(parents=True, exist_ok=True) + with open(tasks_file, 'w', encoding='utf-8') as f: + yaml.dump(data, f, allow_unicode=True, default_flow_style=False, sort_keys=False) + + +def generate_task_id() -> str: + """生成唯一任务 ID""" + ts = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S") + rand = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6)) + return f"task_{ts}_{rand}" + + +def parse_timezone_offset(tz: str) -> int: + """获取时区的 UTC 偏移小时数""" + return TIMEZONE_OFFSET_MAP.get(tz, 0) + + +def compute_next_run_cron(schedule: str, tz: str, after: datetime = None) -> str: + """ + 根据 cron 表达式和时区计算下次执行的 UTC 时间。 + + cron 表达式是基于用户本地时间的,需要先在本地时间计算下次触发,再转换为 UTC。 + """ + offset_hours = parse_timezone_offset(tz) + offset = timedelta(hours=offset_hours) + + # 当前 UTC 时间 + now_utc = after or datetime.now(timezone.utc) + # 转为用户本地时间(naive) + now_local = (now_utc + offset).replace(tzinfo=None) + + # 在本地时间上计算下次 cron 触发 + cron = croniter(schedule, now_local) + next_local = cron.get_next(datetime) + + # 转回 UTC + next_utc = next_local - offset + return next_utc.replace(tzinfo=timezone.utc).isoformat() + + +def parse_scheduled_at(scheduled_at_str: str) -> str: + """解析一次性任务的时间字符串,返回 UTC ISO 格式""" + # 尝试解析带时区偏移的 ISO 格式 + try: + dt = datetime.fromisoformat(scheduled_at_str) + if dt.tzinfo is None: + # 无时区信息,假设 UTC + dt = dt.replace(tzinfo=timezone.utc) + return dt.astimezone(timezone.utc).isoformat() + except ValueError: + pass + + # 尝试解析常见格式 + for fmt in ["%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M"]: + try: + dt = datetime.strptime(scheduled_at_str, fmt).replace(tzinfo=timezone.utc) + return dt.isoformat() + except ValueError: + continue + + raise ValueError(f"无法解析时间格式: {scheduled_at_str}") + + +def cmd_list(args, bot_id: str, user_id: str): + """列出所有任务""" + data = load_tasks(bot_id, user_id) + tasks = data.get("tasks", []) + + if not tasks: + print("当前没有定时任务。") + return + + if args.format == "brief": + # 简洁格式,用于 PrePrompt hook + print(f"定时任务列表 ({len(tasks)} 个):") + for t in tasks: + status_icon = {"active": "✅", "paused": "⏸️", "done": "✔️", "expired": "⏰"}.get(t["status"], "❓") + if t["type"] == "cron": + print(f" {status_icon} [{t['id']}] {t['name']} | cron: {t['schedule']} ({t.get('timezone', 'UTC')}) | 已执行{t.get('execution_count', 0)}次") + else: + print(f" {status_icon} [{t['id']}] {t['name']} | 一次性: {t.get('scheduled_at', 'N/A')}") + else: + # 详细格式 + for t in tasks: + print(f"--- 任务: {t['name']} ---") + print(f" ID: {t['id']}") + print(f" 类型: {t['type']}") + print(f" 状态: {t['status']}") + if t["type"] == "cron": + print(f" Cron: {t['schedule']}") + print(f" 时区: {t.get('timezone', 'UTC')}") + else: + print(f" 计划时间: {t.get('scheduled_at', 'N/A')}") + print(f" 消息: {t['message']}") + print(f" 下次执行: {t.get('next_run_at', 'N/A')}") + print(f" 上次执行: {t.get('last_executed_at', 'N/A')}") + print(f" 执行次数: {t.get('execution_count', 0)}") + print(f" 创建时间: {t.get('created_at', 'N/A')}") + print() + + +def cmd_add(args, bot_id: str, user_id: str): + """添加任务""" + data = load_tasks(bot_id, user_id) + + task_id = generate_task_id() + now_utc = datetime.now(timezone.utc).isoformat() + + task = { + "id": task_id, + "name": args.name, + "type": args.type, + "schedule": None, + "scheduled_at": None, + "timezone": args.timezone or "UTC", + "message": args.message, + "status": "active", + "created_at": now_utc, + "last_executed_at": None, + "next_run_at": None, + "execution_count": 0, + } + + if args.type == "cron": + if not args.schedule: + print("Error: cron 类型任务必须提供 --schedule 参数", file=sys.stderr) + sys.exit(1) + # 验证 cron 表达式 + try: + croniter(args.schedule) + except (ValueError, KeyError) as e: + print(f"Error: 无效的 cron 表达式 '{args.schedule}': {e}", file=sys.stderr) + sys.exit(1) + task["schedule"] = args.schedule + task["next_run_at"] = compute_next_run_cron(args.schedule, task["timezone"]) + + elif args.type == "once": + if not args.scheduled_at: + print("Error: once 类型任务必须提供 --scheduled-at 参数", file=sys.stderr) + sys.exit(1) + try: + utc_time = parse_scheduled_at(args.scheduled_at) + except ValueError as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(1) + task["scheduled_at"] = utc_time + task["next_run_at"] = utc_time + + data["tasks"].append(task) + save_tasks(bot_id, user_id, data) + print(f"任务已创建: {task_id}") + print(f" 名称: {args.name}") + print(f" 类型: {args.type}") + print(f" 下次执行 (UTC): {task['next_run_at']}") + + +def cmd_edit(args, bot_id: str, user_id: str): + """编辑任务""" + data = load_tasks(bot_id, user_id) + + task = None + for t in data["tasks"]: + if t["id"] == args.task_id: + task = t + break + + if not task: + print(f"Error: 任务 {args.task_id} 不存在", file=sys.stderr) + sys.exit(1) + + if args.name: + task["name"] = args.name + if args.message: + task["message"] = args.message + if args.schedule: + try: + croniter(args.schedule) + except (ValueError, KeyError) as e: + print(f"Error: 无效的 cron 表达式 '{args.schedule}': {e}", file=sys.stderr) + sys.exit(1) + task["schedule"] = args.schedule + if args.timezone: + task["timezone"] = args.timezone + if args.scheduled_at: + try: + task["scheduled_at"] = parse_scheduled_at(args.scheduled_at) + task["next_run_at"] = task["scheduled_at"] + except ValueError as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(1) + + # 重新计算 next_run_at(如果是 cron 且修改了 schedule 或 timezone) + if task["type"] == "cron" and task.get("schedule"): + task["next_run_at"] = compute_next_run_cron(task["schedule"], task.get("timezone", "UTC")) + + save_tasks(bot_id, user_id, data) + print(f"任务已更新: {args.task_id}") + + +def cmd_delete(args, bot_id: str, user_id: str): + """删除任务""" + data = load_tasks(bot_id, user_id) + original_count = len(data["tasks"]) + data["tasks"] = [t for t in data["tasks"] if t["id"] != args.task_id] + + if len(data["tasks"]) == original_count: + print(f"Error: 任务 {args.task_id} 不存在", file=sys.stderr) + sys.exit(1) + + save_tasks(bot_id, user_id, data) + print(f"任务已删除: {args.task_id}") + + +def cmd_toggle(args, bot_id: str, user_id: str): + """暂停/恢复任务""" + data = load_tasks(bot_id, user_id) + + for t in data["tasks"]: + if t["id"] == args.task_id: + if t["status"] == "active": + t["status"] = "paused" + print(f"任务已暂停: {args.task_id}") + elif t["status"] == "paused": + t["status"] = "active" + # 恢复时重新计算 next_run_at + if t["type"] == "cron" and t.get("schedule"): + t["next_run_at"] = compute_next_run_cron(t["schedule"], t.get("timezone", "UTC")) + print(f"任务已恢复: {args.task_id}") + else: + print(f"任务状态为 {t['status']},无法切换", file=sys.stderr) + sys.exit(1) + save_tasks(bot_id, user_id, data) + return + + print(f"Error: 任务 {args.task_id} 不存在", file=sys.stderr) + sys.exit(1) + + +def cmd_logs(args, bot_id: str, user_id: str): + """查看执行日志""" + logs_dir = get_logs_dir(bot_id, user_id) + log_file = logs_dir / "execution.log" + + if not log_file.exists(): + print("暂无执行日志。") + return + + with open(log_file, 'r', encoding='utf-8') as f: + logs = yaml.safe_load(f) + + if not logs: + print("暂无执行日志。") + return + + # 按任务 ID 过滤 + if args.task_id: + logs = [l for l in logs if l.get("task_id") == args.task_id] + + # 限制数量 + limit = args.limit or 10 + logs = logs[-limit:] + + if not logs: + print("没有匹配的执行日志。") + return + + for log in logs: + status_icon = "✅" if log.get("status") == "success" else "❌" + print(f"{status_icon} [{log.get('executed_at', 'N/A')}] {log.get('task_name', 'N/A')}") + response = log.get("response", "") + # 截断过长的响应 + if len(response) > 200: + response = response[:200] + "..." + print(f" {response}") + if log.get("duration_ms"): + print(f" 耗时: {log['duration_ms']}ms") + print() + + +def main(): + bot_id = os.getenv("BOT_ID", "") + user_id = os.getenv("USER_IDENTIFIER", "") + + if not bot_id or not user_id: + print("Error: BOT_ID 和 USER_IDENTIFIER 环境变量必须设置", file=sys.stderr) + sys.exit(1) + + parser = argparse.ArgumentParser(description="定时任务管理工具") + subparsers = parser.add_subparsers(dest="command", help="可用命令") + + # list + p_list = subparsers.add_parser("list", help="列出所有任务") + p_list.add_argument("--format", choices=["brief", "detail"], default="detail", help="输出格式") + + # add + p_add = subparsers.add_parser("add", help="添加任务") + p_add.add_argument("--name", required=True, help="任务名称") + p_add.add_argument("--type", required=True, choices=["cron", "once"], help="任务类型") + p_add.add_argument("--schedule", help="Cron 表达式 (cron 类型必填)") + p_add.add_argument("--scheduled-at", help="执行时间 ISO 8601 格式 (once 类型必填)") + p_add.add_argument("--timezone", help="时区 (如 Asia/Tokyo),默认 UTC") + p_add.add_argument("--message", required=True, help="发送给 AI 的消息内容") + + # edit + p_edit = subparsers.add_parser("edit", help="编辑任务") + p_edit.add_argument("task_id", help="任务 ID") + p_edit.add_argument("--name", help="新任务名称") + p_edit.add_argument("--schedule", help="新 Cron 表达式") + p_edit.add_argument("--scheduled-at", help="新执行时间") + p_edit.add_argument("--timezone", help="新时区") + p_edit.add_argument("--message", help="新消息内容") + + # delete + p_delete = subparsers.add_parser("delete", help="删除任务") + p_delete.add_argument("task_id", help="任务 ID") + + # toggle + p_toggle = subparsers.add_parser("toggle", help="暂停/恢复任务") + p_toggle.add_argument("task_id", help="任务 ID") + + # logs + p_logs = subparsers.add_parser("logs", help="查看执行日志") + p_logs.add_argument("--task-id", help="按任务 ID 过滤") + p_logs.add_argument("--limit", type=int, default=10, help="显示条数") + + args = parser.parse_args() + + if not args.command: + parser.print_help() + sys.exit(1) + + commands = { + "list": cmd_list, + "add": cmd_add, + "edit": cmd_edit, + "delete": cmd_delete, + "toggle": cmd_toggle, + "logs": cmd_logs, + } + + commands[args.command](args, bot_id, user_id) + + +if __name__ == "__main__": + main() diff --git a/utils/settings.py b/utils/settings.py index 61c38bc..b2ce36b 100644 --- a/utils/settings.py +++ b/utils/settings.py @@ -84,4 +84,18 @@ MEM0_ENABLED = os.getenv("MEM0_ENABLED", "true") == "true" # 召回记忆数量 MEM0_SEMANTIC_SEARCH_TOP_K = int(os.getenv("MEM0_SEMANTIC_SEARCH_TOP_K", "20")) +# ============================================================ +# Schedule Job 定时任务配置 +# ============================================================ + +# 是否启用定时任务调度器 +SCHEDULE_ENABLED = os.getenv("SCHEDULE_ENABLED", "true") == "true" + +# 调度器扫描间隔(秒) +SCHEDULE_SCAN_INTERVAL = int(os.getenv("SCHEDULE_SCAN_INTERVAL", "60")) + +# 最大并发执行任务数 +SCHEDULE_MAX_CONCURRENT = int(os.getenv("SCHEDULE_MAX_CONCURRENT", "5")) + + os.environ["OPENAI_API_KEY"] = "your_api_key" From 9cc0d724308e5da10bc6e0258541cc4a5b9b4408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 10:00:16 +0800 Subject: [PATCH 07/17] update schedule --- skills/schedule-job/.claude-plugin/plugin.json | 9 ++++++--- skills/schedule-job/scripts/schedule_manager.py | 7 +++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/skills/schedule-job/.claude-plugin/plugin.json b/skills/schedule-job/.claude-plugin/plugin.json index 5d8bf66..93a1323 100644 --- a/skills/schedule-job/.claude-plugin/plugin.json +++ b/skills/schedule-job/.claude-plugin/plugin.json @@ -1,7 +1,10 @@ { "hooks": { - "PrePrompt": { - "command": "python scripts/schedule_manager.py list --format brief" - } + "PrePrompt": [ + { + "type": "command", + "command": "python scripts/schedule_manager.py list --format brief" + } + ] } } diff --git a/skills/schedule-job/scripts/schedule_manager.py b/skills/schedule-job/scripts/schedule_manager.py index c4536e9..97cc914 100644 --- a/skills/schedule-job/scripts/schedule_manager.py +++ b/skills/schedule-job/scripts/schedule_manager.py @@ -47,8 +47,7 @@ TIMEZONE_OFFSET_MAP = { def get_tasks_dir(bot_id: str, user_id: str) -> Path: """获取用户任务目录路径""" - base = Path(os.getenv("PROJECT_ROOT", ".")) - return base / "projects" / "robot" / bot_id / "users" / user_id + return "users" / user_id def get_tasks_file(bot_id: str, user_id: str) -> Path: @@ -354,11 +353,11 @@ def cmd_logs(args, bot_id: str, user_id: str): def main(): - bot_id = os.getenv("BOT_ID", "") + bot_id = os.getenv("ASSISTANT_ID", "") user_id = os.getenv("USER_IDENTIFIER", "") if not bot_id or not user_id: - print("Error: BOT_ID 和 USER_IDENTIFIER 环境变量必须设置", file=sys.stderr) + print("Error: ASSISTANT_ID 和 USER_IDENTIFIER 环境变量必须设置", file=sys.stderr) sys.exit(1) parser = argparse.ArgumentParser(description="定时任务管理工具") From 4090b4d734cc2ec40f6e3cafba56a3bad34d40ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 10:06:07 +0800 Subject: [PATCH 08/17] =?UTF-8?q?=E5=B0=86=20users=20=E6=94=B9=E4=B8=BA=20?= =?UTF-8?q?Path(users)=EF=BC=8C=E4=BD=BF=20/=20=E8=BF=90=E7=AE=97=E7=AC=A6?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=AD=A3=E7=A1=AE=E6=8B=BC=E6=8E=A5=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/schedule-job/scripts/schedule_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skills/schedule-job/scripts/schedule_manager.py b/skills/schedule-job/scripts/schedule_manager.py index 97cc914..0aba21c 100644 --- a/skills/schedule-job/scripts/schedule_manager.py +++ b/skills/schedule-job/scripts/schedule_manager.py @@ -47,7 +47,7 @@ TIMEZONE_OFFSET_MAP = { def get_tasks_dir(bot_id: str, user_id: str) -> Path: """获取用户任务目录路径""" - return "users" / user_id + return Path("users") / user_id def get_tasks_file(bot_id: str, user_id: str) -> Path: From 393c4e413845d210dcaf0ab5921bc2209899a6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 11:19:59 +0800 Subject: [PATCH 09/17] =?UTF-8?q?schedule=20=E9=80=9A=E8=BF=87=20aiohttp?= =?UTF-8?q?=20POST=20=E8=AF=B7=E6=B1=82=E8=B0=83=E7=94=A8=20=20=20http://1?= =?UTF-8?q?27.0.0.1:8001/api/v2/chat/completions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/schedule_executor.py | 61 ++++++++++++++++------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/services/schedule_executor.py b/services/schedule_executor.py index f926be8..07430cf 100644 --- a/services/schedule_executor.py +++ b/services/schedule_executor.py @@ -9,6 +9,8 @@ import asyncio import logging import time import yaml +import aiohttp +import json from datetime import datetime, timezone, timedelta from pathlib import Path from typing import Optional @@ -136,7 +138,7 @@ class ScheduleExecutor: logger.info(f"Executing scheduled task: {task_id} ({task.get('name', '')}) for bot={bot_id} user={user_id}") # 调用 agent - response_text = await self._call_agent(bot_id, user_id, task) + response_text = await self._call_agent_v2(bot_id, user_id, task) # 写入日志 duration_ms = int((time.time() - start_time) * 1000) @@ -156,42 +158,35 @@ class ScheduleExecutor: finally: self._executing_tasks.discard(task_id) - async def _call_agent(self, bot_id: str, user_id: str, task: dict) -> str: - """构建 AgentConfig 并调用 agent""" - from routes.chat import create_agent_and_generate_response - from utils.fastapi_utils import fetch_bot_config, create_project_directory - from agent.agent_config import AgentConfig + async def _call_agent_v2(self, bot_id: str, user_id: str, task: dict) -> str: + """通过 HTTP 调用 /api/v2/chat/completions 接口""" + from utils.fastapi_utils import generate_v2_auth_token - bot_config = await fetch_bot_config(bot_id) - project_dir = create_project_directory( - bot_config.get("dataset_ids", []), - bot_id, - bot_config.get("skills") - ) + url = f"http://127.0.0.1:8001/api/v2/chat/completions" + auth_token = generate_v2_auth_token(bot_id) - messages = [{"role": "user", "content": task["message"]}] + payload = { + "messages": [{"role": "user", "content": task["message"]}], + "stream": False, + "bot_id": bot_id, + "tool_response": False, + "session_id": f"schedule_{task['id']}", + "user_identifier": user_id, + } - config = AgentConfig( - bot_id=bot_id, - api_key=bot_config.get("api_key"), - model_name=bot_config.get("model", "qwen3-next"), - model_server=bot_config.get("model_server", ""), - language=bot_config.get("language", "ja"), - system_prompt=bot_config.get("system_prompt"), - mcp_settings=bot_config.get("mcp_settings", []), - user_identifier=user_id, - session_id=f"schedule_{task['id']}", - project_dir=project_dir, - stream=False, - tool_response=False, - messages=messages, - dataset_ids=bot_config.get("dataset_ids", []), - enable_memori=bot_config.get("enable_memory", False), - shell_env=bot_config.get("shell_env") or {}, - ) + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {auth_token}", + } - result = await create_agent_and_generate_response(config) - return result.choices[0]["message"]["content"] + async with aiohttp.ClientSession() as session: + async with session.post(url, json=payload, headers=headers, timeout=aiohttp.ClientTimeout(total=300)) as resp: + if resp.status != 200: + body = await resp.text() + raise RuntimeError(f"API returned {resp.status}: {body}") + data = await resp.json() + + return data["choices"][0]["message"]["content"] def _update_task_after_execution(self, task_id: str, tasks_file: Path): """执行后更新 tasks.yaml""" From c2f7148f9807e455e81ffc2675b93e8a159c09f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 14:37:20 +0800 Subject: [PATCH 10/17] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=88=B0pre=20prompt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent/plugin_hook_loader.py | 8 +++++++- skills/schedule-job/scripts/schedule_manager.py | 2 +- skills_developing/user-context-loader/README.md | 4 ++-- skills_developing/user-context-loader/hooks/pre_prompt.py | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/agent/plugin_hook_loader.py b/agent/plugin_hook_loader.py index 8fd37fe..c1629f3 100644 --- a/agent/plugin_hook_loader.py +++ b/agent/plugin_hook_loader.py @@ -165,12 +165,18 @@ async def _execute_command(skill_path: str, command: str, hook_type: str, config try: # 设置环境变量,传递给子进程 env = os.environ.copy() - env['BOT_ID'] = getattr(config, 'bot_id', '') + env['ASSISTANT_ID'] = getattr(config, 'bot_id', '') env['USER_IDENTIFIER'] = getattr(config, 'user_identifier', '') + env['TRACE_ID'] = getattr(config, 'trace_id', '') env['SESSION_ID'] = getattr(config, 'session_id', '') env['LANGUAGE'] = getattr(config, 'language', '') env['HOOK_TYPE'] = hook_type + # 合并 config 中的自定义 shell 环境变量 + shell_env = getattr(config, 'shell_env', None) + if shell_env: + env.update(shell_env) + # 对于 PreSave,传递 content if hook_type == 'PreSave': env['CONTENT'] = kwargs.get('content', '') diff --git a/skills/schedule-job/scripts/schedule_manager.py b/skills/schedule-job/scripts/schedule_manager.py index 0aba21c..c5a3fc7 100644 --- a/skills/schedule-job/scripts/schedule_manager.py +++ b/skills/schedule-job/scripts/schedule_manager.py @@ -4,7 +4,7 @@ 用于增删改查用户的定时任务,数据存储在 tasks.yaml 文件中。 环境变量: - BOT_ID: 当前 bot ID + ASSISTANT_ID: 当前 bot ID USER_IDENTIFIER: 当前用户标识 """ diff --git a/skills_developing/user-context-loader/README.md b/skills_developing/user-context-loader/README.md index 2ebf88b..9177f2a 100644 --- a/skills_developing/user-context-loader/README.md +++ b/skills_developing/user-context-loader/README.md @@ -80,7 +80,7 @@ Hook 脚本通过子进程执行,通过环境变量接收参数,通过 stdou | 环境变量 | 说明 | 适用于 | |---------|------|--------| -| `BOT_ID` | Bot ID | 所有 hook | +| `ASSISTANT_ID` | Bot ID | 所有 hook | | `USER_IDENTIFIER` | 用户标识 | 所有 hook | | `SESSION_ID` | 会话 ID | 所有 hook | | `LANGUAGE` | 语言代码 | 所有 hook | @@ -99,7 +99,7 @@ import sys def main(): user_identifier = os.environ.get('USER_IDENTIFIER', '') - bot_id = os.environ.get('BOT_ID', '') + bot_id = os.environ.get('ASSISTANT_ID', '') # 输出要注入到 prompt 中的内容 print(f"## User Context\n\n用户: {user_identifier}") diff --git a/skills_developing/user-context-loader/hooks/pre_prompt.py b/skills_developing/user-context-loader/hooks/pre_prompt.py index e8fea63..c19b855 100644 --- a/skills_developing/user-context-loader/hooks/pre_prompt.py +++ b/skills_developing/user-context-loader/hooks/pre_prompt.py @@ -11,7 +11,7 @@ import sys def main(): """从环境变量读取参数并输出注入内容""" user_identifier = os.environ.get('USER_IDENTIFIER', '') - bot_id = os.environ.get('BOT_ID', '') + bot_id = os.environ.get('ASSISTANT_ID', '') # 示例:根据 user_identifier 查询用户上下文 # 这里只是演示,实际应该从数据库或其他服务获取 From bc59234eaca641ec86cd83f72cee59b8b1a3f646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 16:11:09 +0800 Subject: [PATCH 11/17] update skill --- skills/schedule-job/SKILL.md | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/skills/schedule-job/SKILL.md b/skills/schedule-job/SKILL.md index b085ded..587657a 100644 --- a/skills/schedule-job/SKILL.md +++ b/skills/schedule-job/SKILL.md @@ -39,7 +39,7 @@ python {skill_dir}/scripts/schedule_manager.py add \ --type cron \ --schedule "0 9 * * *" \ --timezone "Asia/Tokyo" \ - --message "请帮我总结今天的科技新闻" + --message "请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5。完成后,根据当前启用的skill选择合适的通知方式将结果推送给用户。" ``` #### 添加一次性任务 @@ -48,7 +48,7 @@ python {skill_dir}/scripts/schedule_manager.py add \ --name "会议提醒" \ --type once \ --scheduled-at "2026-04-01T10:00:00+09:00" \ - --message "提醒我10点有会议" + --message "用户有一个日程提醒到期了,日程内容是:「10点参加会议」。请根据日程内容组织合适的提醒话术,然后根据当前启用的skill选择合适的通知方式将提醒发送给用户。" ``` #### 编辑任务 @@ -103,6 +103,26 @@ python {skill_dir}/scripts/schedule_manager.py logs --task-id - `2026-04-01T01:00:00Z` (UTC) - `2026-04-01T08:00:00+08:00` (中国时间) +## Message 编写规范 + +message 是定时触发后发送给 AI agent 的执行指令。agent 执行时没有对话上下文,因此 message 必须包含: + +1. **任务内容**:具体要做什么 +2. **通知指令**:要求 agent 根据当前启用的 skill 选择合适的通知方式将结果推送给用户 +3. **语言**:message 的语言必须与用户当前对话使用的语言一致(如用户说日语则用日语编写 message) + +### 日程提醒场景(一次性任务) + +❌ 错误写法:`"提醒:你现在有一个会议要参加哦,别忘了!"` + +✅ 正确写法:`"用户有一个日程提醒到期了,日程内容是:「下午3点参加产品评审会议」。请你根据日程内容组织合适的提醒话术,然后根据当前启用的skill选择合适的通知方式将提醒发送给用户。"` + +### 定时任务场景(周期任务) + +❌ 错误写法:`"执行:获取最新新闻"` + +✅ 正确写法:`"请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5。完成后,根据当前启用的skill选择合适的通知方式将结果推送给用户。"` + ## Examples **用户**: "帮我设置一个每天早上9点的新闻总结任务" @@ -113,7 +133,7 @@ python {skill_dir}/scripts/schedule_manager.py add \ --type cron \ --schedule "0 9 * * *" \ --timezone "Asia/Tokyo" \ - --message "请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5" + --message "请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5。完成后,根据当前启用的skill选择合适的通知方式将结果推送给用户。" ``` **用户**: "提醒我明天下午3点开会" @@ -123,7 +143,7 @@ python {skill_dir}/scripts/schedule_manager.py add \ --name "开会提醒" \ --type once \ --scheduled-at "2026-03-31T15:00:00+09:00" \ - --message "提醒:你现在有一个会议要参加" + --message "用户有一个日程提醒到期了,日程内容是:「明天下午3点开会」。请根据日程内容组织合适的提醒话术,然后根据当前启用的skill选择合适的通知方式将提醒发送给用户。" ``` **用户**: "把每日新闻任务改到早上10点" @@ -139,6 +159,8 @@ python {skill_dir}/scripts/schedule_manager.py edit --schedule "0 10 * - 创建任务前先用 `list` 确认用户已有的任务,避免创建重复任务 - 根据用户语言自动设置合适的时区 -- message 内容应该是完整的、可独立执行的指令,因为 AI 执行时没有对话上下文 +- message 内容是发给 AI agent 的完整执行指令,必须包含任务内容和通知指令两部分(参考上方 Message 编写规范) +- 不要在 message 中硬编码具体的通知渠道(如"发邮件"),应使用"根据当前启用的skill选择合适的通知方式"让 agent 自行决定 +- message 的语言必须与用户当前对话使用的语言一致 - 一次性任务的时间不能是过去的时间 - 编辑任务时只修改用户要求改的字段,不要改动其他字段 From daa5bf345a802aa5e7b444cec870f5f4ee9e51be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 16:23:03 +0800 Subject: [PATCH 12/17] update schedule skill --- skills/schedule-job/SKILL.md | 141 ++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 70 deletions(-) diff --git a/skills/schedule-job/SKILL.md b/skills/schedule-job/SKILL.md index 587657a..5f600ba 100644 --- a/skills/schedule-job/SKILL.md +++ b/skills/schedule-job/SKILL.md @@ -1,166 +1,167 @@ --- name: schedule-job -description: 定时任务管理 - 为用户创建、管理和查看定时任务(支持 cron 周期任务和一次性任务) +description: Scheduled Task Management - Create, manage, and view scheduled tasks for users (supports cron recurring tasks and one-time tasks) --- -# Schedule Job - 定时任务管理 +# Schedule Job - Scheduled Task Management -管理用户的定时任务,支持 cron 周期性任务和一次性定时任务。任务到期后系统自动执行 AI 对话。 +Manage scheduled tasks for users, supporting cron recurring tasks and one-time scheduled tasks. The system automatically executes AI conversations when tasks are due. ## Quick Start -用户请求创建定时任务时: -1. 确认任务类型(周期 cron / 一次性 once) -2. 确定时间(cron 表达式或具体时间)和时区 -3. 确定发送给 AI 的消息内容 -4. 调用 schedule_manager.py 创建任务 +When a user requests to create a scheduled task: +1. Confirm the task type (recurring cron / one-time once) +2. Determine the time (cron expression or specific time) and timezone +3. Confirm notification method: Check the currently enabled skills in the context and recommend a suitable notification method to the user (e.g., email, Telegram, etc.) for confirmation. If no notification skills are available, proactively ask the user: "How would you like to be notified when the scheduled task completes?" +4. Compose the message content to send to the AI (refer to Message Writing Guidelines) +5. Call schedule_manager.py to create the task ## Instructions -### 工具路径 +### Tool Path -所有操作通过 shell 命令执行: +All operations are executed via shell commands: ```bash python {skill_dir}/scripts/schedule_manager.py [options] ``` -### 可用命令 +### Available Commands -#### 列出任务 +#### List Tasks ```bash python {skill_dir}/scripts/schedule_manager.py list python {skill_dir}/scripts/schedule_manager.py list --format brief ``` -#### 添加 Cron 周期任务 +#### Add Cron Recurring Task ```bash python {skill_dir}/scripts/schedule_manager.py add \ - --name "任务名称" \ + --name "Task Name" \ --type cron \ --schedule "0 9 * * *" \ --timezone "Asia/Tokyo" \ - --message "请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5。完成后,根据当前启用的skill选择合适的通知方式将结果推送给用户。" + --message "Please search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user." ``` -#### 添加一次性任务 +#### Add One-Time Task ```bash python {skill_dir}/scripts/schedule_manager.py add \ - --name "会议提醒" \ + --name "Meeting Reminder" \ --type once \ --scheduled-at "2026-04-01T10:00:00+09:00" \ - --message "用户有一个日程提醒到期了,日程内容是:「10点参加会议」。请根据日程内容组织合适的提醒话术,然后根据当前启用的skill选择合适的通知方式将提醒发送给用户。" + --message "A scheduled reminder for the user has arrived. The schedule content is: 'Meeting at 10:00'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user." ``` -#### 编辑任务 +#### Edit Task ```bash python {skill_dir}/scripts/schedule_manager.py edit \ --schedule "0 10 * * 1-5" \ - --message "新的消息内容" + --message "New message content" ``` -#### 删除任务 +#### Delete Task ```bash python {skill_dir}/scripts/schedule_manager.py delete ``` -#### 暂停/恢复任务 +#### Pause/Resume Task ```bash python {skill_dir}/scripts/schedule_manager.py toggle ``` -#### 查看执行日志 +#### View Execution Logs ```bash python {skill_dir}/scripts/schedule_manager.py logs --limit 10 python {skill_dir}/scripts/schedule_manager.py logs --task-id ``` -### 时区映射 +### Timezone Mapping -根据用户语言自动推荐时区: -- 中文 (zh) → Asia/Shanghai (UTC+8) -- 日语 (ja/jp) → Asia/Tokyo (UTC+9) -- 英语 (en) → UTC +Automatically recommend timezone based on user language: +- Chinese (zh) → Asia/Shanghai (UTC+8) +- Japanese (ja/jp) → Asia/Tokyo (UTC+9) +- English (en) → UTC -### Cron 表达式说明 +### Cron Expression Reference -标准 5 字段格式:`分 时 日 月 星期` +Standard 5-field format: `minute hour day-of-month month day-of-week` -常用示例: -| 表达式 | 含义 | -|--------|------| -| `0 9 * * *` | 每天 9:00 | -| `0 9 * * 1-5` | 周一到周五 9:00 | -| `30 8 * * 1` | 每周一 8:30 | -| `0 */2 * * *` | 每 2 小时 | -| `0 9,18 * * *` | 每天 9:00 和 18:00 | +Common examples: +| Expression | Meaning | +|-----------|---------| +| `0 9 * * *` | Every day at 9:00 | +| `0 9 * * 1-5` | Monday to Friday at 9:00 | +| `30 8 * * 1` | Every Monday at 8:30 | +| `0 */2 * * *` | Every 2 hours | +| `0 9,18 * * *` | Every day at 9:00 and 18:00 | -**注意**: cron 表达式的时间基于 --timezone 指定的时区。 +**Note**: Cron expression times are based on the timezone specified by --timezone. -### 一次性任务时间格式 +### One-Time Task Time Format -支持 ISO 8601 格式(推荐带时区偏移): -- `2026-04-01T10:00:00+09:00` (日本时间) +Supports ISO 8601 format (timezone offset recommended): +- `2026-04-01T10:00:00+09:00` (Japan Time) - `2026-04-01T01:00:00Z` (UTC) -- `2026-04-01T08:00:00+08:00` (中国时间) +- `2026-04-01T08:00:00+08:00` (China Time) -## Message 编写规范 +## Message Writing Guidelines -message 是定时触发后发送给 AI agent 的执行指令。agent 执行时没有对话上下文,因此 message 必须包含: +The message is an execution instruction sent to the AI agent when the scheduled task triggers. The agent has no conversation context at execution time, so the message must include: -1. **任务内容**:具体要做什么 -2. **通知指令**:要求 agent 根据当前启用的 skill 选择合适的通知方式将结果推送给用户 -3. **语言**:message 的语言必须与用户当前对话使用的语言一致(如用户说日语则用日语编写 message) +1. **Task Content**: What specifically needs to be done +2. **Notification Instruction**: Based on the notification method confirmed by the user, explicitly specify the notification channel in the message (e.g., "notify the user via email"). If the user has not specified, write "select an appropriate notification method based on the currently enabled skills to push the results to the user" +3. **Language**: The message language must match the language the user is currently using in the conversation (e.g., if the user speaks Japanese, write the message in Japanese) -### 日程提醒场景(一次性任务) +### Schedule Reminder Scenario (One-Time Task) -❌ 错误写法:`"提醒:你现在有一个会议要参加哦,别忘了!"` +❌ Wrong: `"Reminder: You have a meeting to attend now, don't forget!"` -✅ 正确写法:`"用户有一个日程提醒到期了,日程内容是:「下午3点参加产品评审会议」。请你根据日程内容组织合适的提醒话术,然后根据当前启用的skill选择合适的通知方式将提醒发送给用户。"` +✅ Correct: `"A scheduled reminder for the user has arrived. The schedule content is: 'Product review meeting at 3:00 PM'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user."` -### 定时任务场景(周期任务) +### Recurring Task Scenario (Cron Task) -❌ 错误写法:`"执行:获取最新新闻"` +❌ Wrong: `"Execute: Get latest news"` -✅ 正确写法:`"请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5。完成后,根据当前启用的skill选择合适的通知方式将结果推送给用户。"` +✅ Correct: `"Please search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user."` ## Examples -**用户**: "帮我设置一个每天早上9点的新闻总结任务" +**User**: "Set up a daily news summary task at 9 AM" ```bash python {skill_dir}/scripts/schedule_manager.py add \ - --name "每日新闻总结" \ + --name "Daily News Summary" \ --type cron \ --schedule "0 9 * * *" \ --timezone "Asia/Tokyo" \ - --message "请帮我搜索并总结今天的重要科技新闻,用简洁的方式列出 Top 5。完成后,根据当前启用的skill选择合适的通知方式将结果推送给用户。" + --message "Please search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user." ``` -**用户**: "提醒我明天下午3点开会" +**User**: "Remind me about the meeting tomorrow at 3 PM" ```bash python {skill_dir}/scripts/schedule_manager.py add \ - --name "开会提醒" \ + --name "Meeting Reminder" \ --type once \ --scheduled-at "2026-03-31T15:00:00+09:00" \ - --message "用户有一个日程提醒到期了,日程内容是:「明天下午3点开会」。请根据日程内容组织合适的提醒话术,然后根据当前启用的skill选择合适的通知方式将提醒发送给用户。" + --message "A scheduled reminder for the user has arrived. The schedule content is: 'Meeting tomorrow at 3:00 PM'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user." ``` -**用户**: "把每日新闻任务改到早上10点" +**User**: "Change the daily news task to 10 AM" ```bash -# 先查看任务列表获取 task_id +# First check the task list to get the task_id python {skill_dir}/scripts/schedule_manager.py list -# 然后编辑 +# Then edit python {skill_dir}/scripts/schedule_manager.py edit --schedule "0 10 * * *" ``` ## Guidelines -- 创建任务前先用 `list` 确认用户已有的任务,避免创建重复任务 -- 根据用户语言自动设置合适的时区 -- message 内容是发给 AI agent 的完整执行指令,必须包含任务内容和通知指令两部分(参考上方 Message 编写规范) -- 不要在 message 中硬编码具体的通知渠道(如"发邮件"),应使用"根据当前启用的skill选择合适的通知方式"让 agent 自行决定 -- message 的语言必须与用户当前对话使用的语言一致 -- 一次性任务的时间不能是过去的时间 -- 编辑任务时只修改用户要求改的字段,不要改动其他字段 +- Before creating a task, use `list` to check existing tasks and avoid duplicates +- Automatically set the appropriate timezone based on user language +- **Must confirm notification method before creating a task**: Check the currently enabled skills, recommend available notification channels to the user and confirm. If no notification skills are available, proactively ask the user how they would like to receive results +- Message content is a complete execution instruction for the AI agent and must include both task content and notification instructions (refer to the Message Writing Guidelines above) +- The message language must match the language the user is currently using in the conversation +- One-time task times cannot be in the past +- When editing a task, only modify the fields the user requested — do not change other fields From d38a7377304b5e0ba14cd35bd914b77268d746d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 19:30:40 +0800 Subject: [PATCH 13/17] =?UTF-8?q?=E4=B8=A4=E4=B8=AA=20Dockerfile=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=20npm=20=E5=AE=89=E8=A3=85=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E9=87=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=20nodemailer=20=20=20?= =?UTF-8?q?=E5=92=8C=20dotenv=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- Dockerfile.modelscope | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index f178064..d17ca36 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,7 +40,7 @@ RUN pip install --no-cache-dir -r requirements.txt # 安装 Playwright 并下载 Chromium RUN pip install --no-cache-dir playwright && \ playwright install chromium -RUN npm install -g playwright sharp && \ +RUN npm install -g playwright sharp nodemailer dotenv && \ npx playwright install chromium # 复制应用代码 diff --git a/Dockerfile.modelscope b/Dockerfile.modelscope index b1dd0f0..9cd3a3a 100644 --- a/Dockerfile.modelscope +++ b/Dockerfile.modelscope @@ -41,7 +41,7 @@ RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ -r req # 安装 Playwright 并下载 Chromium RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ playwright && \ playwright install chromium -RUN npm install -g playwright sharp && \ +RUN npm install -g playwright sharp nodemailer dotenv && \ npx playwright install chromium # 安装modelscope From 85a262257e31393e8d008ac535ffe33cbc148fc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 31 Mar 2026 20:56:20 +0800 Subject: [PATCH 14/17] update schedule skill --- skills/schedule-job/SKILL.md | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/skills/schedule-job/SKILL.md b/skills/schedule-job/SKILL.md index 5f600ba..16d645e 100644 --- a/skills/schedule-job/SKILL.md +++ b/skills/schedule-job/SKILL.md @@ -40,7 +40,7 @@ python {skill_dir}/scripts/schedule_manager.py add \ --type cron \ --schedule "0 9 * * *" \ --timezone "Asia/Tokyo" \ - --message "Please search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user." + --message "[Scheduled Task Triggered] Please execute the following task immediately: Search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user." ``` #### Add One-Time Task @@ -49,7 +49,7 @@ python {skill_dir}/scripts/schedule_manager.py add \ --name "Meeting Reminder" \ --type once \ --scheduled-at "2026-04-01T10:00:00+09:00" \ - --message "A scheduled reminder for the user has arrived. The schedule content is: 'Meeting at 10:00'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user." + --message "[Scheduled Task Triggered] A scheduled reminder has arrived. The schedule content is: 'Meeting at 10:00'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user." ``` #### Edit Task @@ -108,21 +108,32 @@ Supports ISO 8601 format (timezone offset recommended): The message is an execution instruction sent to the AI agent when the scheduled task triggers. The agent has no conversation context at execution time, so the message must include: -1. **Task Content**: What specifically needs to be done -2. **Notification Instruction**: Based on the notification method confirmed by the user, explicitly specify the notification channel in the message (e.g., "notify the user via email"). If the user has not specified, write "select an appropriate notification method based on the currently enabled skills to push the results to the user" -3. **Language**: The message language must match the language the user is currently using in the conversation (e.g., if the user speaks Japanese, write the message in Japanese) +1. **Task Trigger Indicator**: Start with "[Scheduled Task Triggered]" (or equivalent in the user's language) to clearly indicate this is a triggered scheduled task execution, NOT a request to create a task +2. **Task Content**: What specifically needs to be done (use imperative verbs like "Please execute immediately", "Please do") +3. **Notification Instruction**: Based on the notification method confirmed by the user, explicitly specify the notification channel in the message (e.g., "notify the user via email"). If the user has not specified, write "select an appropriate notification method based on the currently enabled skills to push the results to the user" +4. **Language**: The message language must match the language the user is currently using in the conversation (e.g., if the user speaks Japanese, write the message in Japanese) + +### Important: Avoid Ambiguity + +The message will be executed by an AI agent when the scheduled time arrives. The message must clearly indicate this is an **execution instruction**, not a request to **create a scheduled task**. ### Schedule Reminder Scenario (One-Time Task) ❌ Wrong: `"Reminder: You have a meeting to attend now, don't forget!"` -✅ Correct: `"A scheduled reminder for the user has arrived. The schedule content is: 'Product review meeting at 3:00 PM'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user."` +❌ Wrong: `"It's tomorrow at 3 PM, please remind the user to attend the meeting"` (Agent will think this is a request to create a reminder) + +✅ Correct: `"[Scheduled Task Triggered] A scheduled reminder has arrived. The schedule content is: 'Product review meeting at 3:00 PM'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user."` ### Recurring Task Scenario (Cron Task) +❌ Wrong: `"It's 8:40 PM every day, please remind the user to write a daily report. Please send message via Lark Webhook, Webhook URL: xxx"` (Agent will think this is a request to create a daily task) + ❌ Wrong: `"Execute: Get latest news"` -✅ Correct: `"Please search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user."` +✅ Correct: `"[Scheduled Task Triggered] Please execute the following task immediately: Search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user."` + +✅ Correct: `"[Scheduled Task Triggered] This is a daily reminder task. Please execute immediately: Remind the user to write a daily report. Please send the message via Lark Webhook, Webhook URL: xxx"` ## Examples @@ -134,7 +145,7 @@ python {skill_dir}/scripts/schedule_manager.py add \ --type cron \ --schedule "0 9 * * *" \ --timezone "Asia/Tokyo" \ - --message "Please search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user." + --message "[Scheduled Task Triggered] Please execute the following task immediately: Search and summarize today's important tech news, listing the Top 5 in a concise format. After completion, select an appropriate notification method based on the currently enabled skills to push the results to the user." ``` **User**: "Remind me about the meeting tomorrow at 3 PM" @@ -143,8 +154,8 @@ python {skill_dir}/scripts/schedule_manager.py add \ python {skill_dir}/scripts/schedule_manager.py add \ --name "Meeting Reminder" \ --type once \ - --scheduled-at "2026-03-31T15:00:00+09:00" \ - --message "A scheduled reminder for the user has arrived. The schedule content is: 'Meeting tomorrow at 3:00 PM'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user." + --scheduled-at "2026-04-01T15:00:00+09:00" \ + --message "[Scheduled Task Triggered] A scheduled reminder has arrived. The schedule content is: 'Meeting at 3:00 PM'. Please compose an appropriate reminder message based on the schedule content, then select an appropriate notification method based on the currently enabled skills to send the reminder to the user." ``` **User**: "Change the daily news task to 10 AM" From fd0fbc422dc6339b6a7f814bbf66a4154e0a8e90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Wed, 1 Apr 2026 10:27:21 +0800 Subject: [PATCH 15/17] uuid2str --- agent/plugin_hook_loader.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/agent/plugin_hook_loader.py b/agent/plugin_hook_loader.py index c1629f3..271ebbf 100644 --- a/agent/plugin_hook_loader.py +++ b/agent/plugin_hook_loader.py @@ -165,11 +165,11 @@ async def _execute_command(skill_path: str, command: str, hook_type: str, config try: # 设置环境变量,传递给子进程 env = os.environ.copy() - env['ASSISTANT_ID'] = getattr(config, 'bot_id', '') - env['USER_IDENTIFIER'] = getattr(config, 'user_identifier', '') - env['TRACE_ID'] = getattr(config, 'trace_id', '') - env['SESSION_ID'] = getattr(config, 'session_id', '') - env['LANGUAGE'] = getattr(config, 'language', '') + env['ASSISTANT_ID'] = str(getattr(config, 'bot_id', '')) + env['USER_IDENTIFIER'] = str(getattr(config, 'user_identifier', '')) + env['TRACE_ID'] = str(getattr(config, 'trace_id', '')) + env['SESSION_ID'] = str(getattr(config, 'session_id', '')) + env['LANGUAGE'] = str(getattr(config, 'language', '')) env['HOOK_TYPE'] = hook_type # 合并 config 中的自定义 shell 环境变量 From d6ee567758301103e6f6089583f03c00a0c389a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Wed, 1 Apr 2026 10:37:03 +0800 Subject: [PATCH 16/17] =?UTF-8?q?schedule=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../schedule-job/scripts/schedule_manager.py | 199 +++++++++--------- 1 file changed, 100 insertions(+), 99 deletions(-) diff --git a/skills/schedule-job/scripts/schedule_manager.py b/skills/schedule-job/scripts/schedule_manager.py index c5a3fc7..0de5711 100644 --- a/skills/schedule-job/scripts/schedule_manager.py +++ b/skills/schedule-job/scripts/schedule_manager.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 """ -定时任务管理 CLI 工具 -用于增删改查用户的定时任务,数据存储在 tasks.yaml 文件中。 +Scheduled Task Manager CLI Tool +Add, delete, modify, and query user scheduled tasks. Data is stored in tasks.yaml. -环境变量: - ASSISTANT_ID: 当前 bot ID - USER_IDENTIFIER: 当前用户标识 +Environment variables: + ASSISTANT_ID: Current bot ID + USER_IDENTIFIER: Current user identifier """ import argparse @@ -25,7 +25,7 @@ except ImportError: sys.exit(1) -# 语言到时区的映射 +# Language to timezone mapping LANGUAGE_TIMEZONE_MAP = { 'zh': 'Asia/Shanghai', 'ja': 'Asia/Tokyo', @@ -33,7 +33,7 @@ LANGUAGE_TIMEZONE_MAP = { 'en': 'UTC', } -# 时区到 UTC 偏移(小时) +# Timezone to UTC offset (hours) TIMEZONE_OFFSET_MAP = { 'Asia/Shanghai': 8, 'Asia/Tokyo': 9, @@ -46,22 +46,22 @@ TIMEZONE_OFFSET_MAP = { def get_tasks_dir(bot_id: str, user_id: str) -> Path: - """获取用户任务目录路径""" + """Get user task directory path""" return Path("users") / user_id def get_tasks_file(bot_id: str, user_id: str) -> Path: - """获取 tasks.yaml 文件路径""" + """Get tasks.yaml file path""" return get_tasks_dir(bot_id, user_id) / "tasks.yaml" def get_logs_dir(bot_id: str, user_id: str) -> Path: - """获取任务日志目录""" + """Get task logs directory""" return get_tasks_dir(bot_id, user_id) / "task_logs" def load_tasks(bot_id: str, user_id: str) -> dict: - """加载 tasks.yaml""" + """Load tasks.yaml""" tasks_file = get_tasks_file(bot_id, user_id) if not tasks_file.exists(): return {"tasks": []} @@ -71,7 +71,7 @@ def load_tasks(bot_id: str, user_id: str) -> dict: def save_tasks(bot_id: str, user_id: str, data: dict): - """保存 tasks.yaml""" + """Save tasks.yaml""" tasks_file = get_tasks_file(bot_id, user_id) tasks_file.parent.mkdir(parents=True, exist_ok=True) with open(tasks_file, 'w', encoding='utf-8') as f: @@ -79,53 +79,54 @@ def save_tasks(bot_id: str, user_id: str, data: dict): def generate_task_id() -> str: - """生成唯一任务 ID""" + """Generate unique task ID""" ts = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S") rand = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6)) return f"task_{ts}_{rand}" def parse_timezone_offset(tz: str) -> int: - """获取时区的 UTC 偏移小时数""" + """Get UTC offset hours for a timezone""" return TIMEZONE_OFFSET_MAP.get(tz, 0) def compute_next_run_cron(schedule: str, tz: str, after: datetime = None) -> str: """ - 根据 cron 表达式和时区计算下次执行的 UTC 时间。 + Calculate next execution UTC time based on cron expression and timezone. - cron 表达式是基于用户本地时间的,需要先在本地时间计算下次触发,再转换为 UTC。 + The cron expression is based on user's local time, so we first calculate + the next trigger in local time, then convert to UTC. """ offset_hours = parse_timezone_offset(tz) offset = timedelta(hours=offset_hours) - # 当前 UTC 时间 + # Current UTC time now_utc = after or datetime.now(timezone.utc) - # 转为用户本地时间(naive) + # Convert to user's local time (naive) now_local = (now_utc + offset).replace(tzinfo=None) - # 在本地时间上计算下次 cron 触发 + # Calculate next cron trigger in local time cron = croniter(schedule, now_local) next_local = cron.get_next(datetime) - # 转回 UTC + # Convert back to UTC next_utc = next_local - offset return next_utc.replace(tzinfo=timezone.utc).isoformat() def parse_scheduled_at(scheduled_at_str: str) -> str: - """解析一次性任务的时间字符串,返回 UTC ISO 格式""" - # 尝试解析带时区偏移的 ISO 格式 + """Parse one-time task time string, return UTC ISO format""" + # Try to parse ISO format with timezone offset try: dt = datetime.fromisoformat(scheduled_at_str) if dt.tzinfo is None: - # 无时区信息,假设 UTC + # No timezone info, assume UTC dt = dt.replace(tzinfo=timezone.utc) return dt.astimezone(timezone.utc).isoformat() except ValueError: pass - # 尝试解析常见格式 + # Try to parse common formats for fmt in ["%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M"]: try: dt = datetime.strptime(scheduled_at_str, fmt).replace(tzinfo=timezone.utc) @@ -133,49 +134,49 @@ def parse_scheduled_at(scheduled_at_str: str) -> str: except ValueError: continue - raise ValueError(f"无法解析时间格式: {scheduled_at_str}") + raise ValueError(f"Cannot parse time format: {scheduled_at_str}") def cmd_list(args, bot_id: str, user_id: str): - """列出所有任务""" + """List all tasks""" data = load_tasks(bot_id, user_id) tasks = data.get("tasks", []) if not tasks: - print("当前没有定时任务。") + print("No scheduled tasks.") return if args.format == "brief": - # 简洁格式,用于 PrePrompt hook - print(f"定时任务列表 ({len(tasks)} 个):") + # Brief format for PrePrompt hook + print(f"Scheduled tasks ({len(tasks)}):") for t in tasks: status_icon = {"active": "✅", "paused": "⏸️", "done": "✔️", "expired": "⏰"}.get(t["status"], "❓") if t["type"] == "cron": - print(f" {status_icon} [{t['id']}] {t['name']} | cron: {t['schedule']} ({t.get('timezone', 'UTC')}) | 已执行{t.get('execution_count', 0)}次") + print(f" {status_icon} [{t['id']}] {t['name']} | cron: {t['schedule']} ({t.get('timezone', 'UTC')}) | executed {t.get('execution_count', 0)} times") else: - print(f" {status_icon} [{t['id']}] {t['name']} | 一次性: {t.get('scheduled_at', 'N/A')}") + print(f" {status_icon} [{t['id']}] {t['name']} | once: {t.get('scheduled_at', 'N/A')}") else: - # 详细格式 + # Detail format for t in tasks: - print(f"--- 任务: {t['name']} ---") + print(f"--- Task: {t['name']} ---") print(f" ID: {t['id']}") - print(f" 类型: {t['type']}") - print(f" 状态: {t['status']}") + print(f" Type: {t['type']}") + print(f" Status: {t['status']}") if t["type"] == "cron": print(f" Cron: {t['schedule']}") - print(f" 时区: {t.get('timezone', 'UTC')}") + print(f" Timezone: {t.get('timezone', 'UTC')}") else: - print(f" 计划时间: {t.get('scheduled_at', 'N/A')}") - print(f" 消息: {t['message']}") - print(f" 下次执行: {t.get('next_run_at', 'N/A')}") - print(f" 上次执行: {t.get('last_executed_at', 'N/A')}") - print(f" 执行次数: {t.get('execution_count', 0)}") - print(f" 创建时间: {t.get('created_at', 'N/A')}") + print(f" Scheduled at: {t.get('scheduled_at', 'N/A')}") + print(f" Message: {t['message']}") + print(f" Next run: {t.get('next_run_at', 'N/A')}") + print(f" Last run: {t.get('last_executed_at', 'N/A')}") + print(f" Executions: {t.get('execution_count', 0)}") + print(f" Created at: {t.get('created_at', 'N/A')}") print() def cmd_add(args, bot_id: str, user_id: str): - """添加任务""" + """Add a task""" data = load_tasks(bot_id, user_id) task_id = generate_task_id() @@ -198,20 +199,20 @@ def cmd_add(args, bot_id: str, user_id: str): if args.type == "cron": if not args.schedule: - print("Error: cron 类型任务必须提供 --schedule 参数", file=sys.stderr) + print("Error: --schedule is required for cron type tasks", file=sys.stderr) sys.exit(1) - # 验证 cron 表达式 + # Validate cron expression try: croniter(args.schedule) except (ValueError, KeyError) as e: - print(f"Error: 无效的 cron 表达式 '{args.schedule}': {e}", file=sys.stderr) + print(f"Error: Invalid cron expression '{args.schedule}': {e}", file=sys.stderr) sys.exit(1) task["schedule"] = args.schedule task["next_run_at"] = compute_next_run_cron(args.schedule, task["timezone"]) elif args.type == "once": if not args.scheduled_at: - print("Error: once 类型任务必须提供 --scheduled-at 参数", file=sys.stderr) + print("Error: --scheduled-at is required for once type tasks", file=sys.stderr) sys.exit(1) try: utc_time = parse_scheduled_at(args.scheduled_at) @@ -223,14 +224,14 @@ def cmd_add(args, bot_id: str, user_id: str): data["tasks"].append(task) save_tasks(bot_id, user_id, data) - print(f"任务已创建: {task_id}") - print(f" 名称: {args.name}") - print(f" 类型: {args.type}") - print(f" 下次执行 (UTC): {task['next_run_at']}") + print(f"Task created: {task_id}") + print(f" Name: {args.name}") + print(f" Type: {args.type}") + print(f" Next run (UTC): {task['next_run_at']}") def cmd_edit(args, bot_id: str, user_id: str): - """编辑任务""" + """Edit a task""" data = load_tasks(bot_id, user_id) task = None @@ -240,7 +241,7 @@ def cmd_edit(args, bot_id: str, user_id: str): break if not task: - print(f"Error: 任务 {args.task_id} 不存在", file=sys.stderr) + print(f"Error: Task {args.task_id} not found", file=sys.stderr) sys.exit(1) if args.name: @@ -251,7 +252,7 @@ def cmd_edit(args, bot_id: str, user_id: str): try: croniter(args.schedule) except (ValueError, KeyError) as e: - print(f"Error: 无效的 cron 表达式 '{args.schedule}': {e}", file=sys.stderr) + print(f"Error: Invalid cron expression '{args.schedule}': {e}", file=sys.stderr) sys.exit(1) task["schedule"] = args.schedule if args.timezone: @@ -264,91 +265,91 @@ def cmd_edit(args, bot_id: str, user_id: str): print(f"Error: {e}", file=sys.stderr) sys.exit(1) - # 重新计算 next_run_at(如果是 cron 且修改了 schedule 或 timezone) + # Recalculate next_run_at (if cron and schedule or timezone changed) if task["type"] == "cron" and task.get("schedule"): task["next_run_at"] = compute_next_run_cron(task["schedule"], task.get("timezone", "UTC")) save_tasks(bot_id, user_id, data) - print(f"任务已更新: {args.task_id}") + print(f"Task updated: {args.task_id}") def cmd_delete(args, bot_id: str, user_id: str): - """删除任务""" + """Delete a task""" data = load_tasks(bot_id, user_id) original_count = len(data["tasks"]) data["tasks"] = [t for t in data["tasks"] if t["id"] != args.task_id] if len(data["tasks"]) == original_count: - print(f"Error: 任务 {args.task_id} 不存在", file=sys.stderr) + print(f"Error: Task {args.task_id} not found", file=sys.stderr) sys.exit(1) save_tasks(bot_id, user_id, data) - print(f"任务已删除: {args.task_id}") + print(f"Task deleted: {args.task_id}") def cmd_toggle(args, bot_id: str, user_id: str): - """暂停/恢复任务""" + """Pause/resume task""" data = load_tasks(bot_id, user_id) for t in data["tasks"]: if t["id"] == args.task_id: if t["status"] == "active": t["status"] = "paused" - print(f"任务已暂停: {args.task_id}") + print(f"Task paused: {args.task_id}") elif t["status"] == "paused": t["status"] = "active" - # 恢复时重新计算 next_run_at + # Recalculate next_run_at when resuming if t["type"] == "cron" and t.get("schedule"): t["next_run_at"] = compute_next_run_cron(t["schedule"], t.get("timezone", "UTC")) - print(f"任务已恢复: {args.task_id}") + print(f"Task resumed: {args.task_id}") else: - print(f"任务状态为 {t['status']},无法切换", file=sys.stderr) + print(f"Task status is {t['status']}, cannot toggle", file=sys.stderr) sys.exit(1) save_tasks(bot_id, user_id, data) return - print(f"Error: 任务 {args.task_id} 不存在", file=sys.stderr) + print(f"Error: Task {args.task_id} not found", file=sys.stderr) sys.exit(1) def cmd_logs(args, bot_id: str, user_id: str): - """查看执行日志""" + """View execution logs""" logs_dir = get_logs_dir(bot_id, user_id) log_file = logs_dir / "execution.log" if not log_file.exists(): - print("暂无执行日志。") + print("No execution logs yet.") return with open(log_file, 'r', encoding='utf-8') as f: logs = yaml.safe_load(f) if not logs: - print("暂无执行日志。") + print("No execution logs yet.") return - # 按任务 ID 过滤 + # Filter by task ID if args.task_id: logs = [l for l in logs if l.get("task_id") == args.task_id] - # 限制数量 + # Limit count limit = args.limit or 10 logs = logs[-limit:] if not logs: - print("没有匹配的执行日志。") + print("No matching logs found.") return for log in logs: status_icon = "✅" if log.get("status") == "success" else "❌" print(f"{status_icon} [{log.get('executed_at', 'N/A')}] {log.get('task_name', 'N/A')}") response = log.get("response", "") - # 截断过长的响应 + # Truncate long responses if len(response) > 200: response = response[:200] + "..." print(f" {response}") if log.get("duration_ms"): - print(f" 耗时: {log['duration_ms']}ms") + print(f" Duration: {log['duration_ms']}ms") print() @@ -357,46 +358,46 @@ def main(): user_id = os.getenv("USER_IDENTIFIER", "") if not bot_id or not user_id: - print("Error: ASSISTANT_ID 和 USER_IDENTIFIER 环境变量必须设置", file=sys.stderr) + print("Error: ASSISTANT_ID and USER_IDENTIFIER environment variables must be set", file=sys.stderr) sys.exit(1) - parser = argparse.ArgumentParser(description="定时任务管理工具") - subparsers = parser.add_subparsers(dest="command", help="可用命令") + parser = argparse.ArgumentParser(description="Scheduled task manager") + subparsers = parser.add_subparsers(dest="command", help="Available commands") # list - p_list = subparsers.add_parser("list", help="列出所有任务") - p_list.add_argument("--format", choices=["brief", "detail"], default="detail", help="输出格式") + p_list = subparsers.add_parser("list", help="List all tasks") + p_list.add_argument("--format", choices=["brief", "detail"], default="detail", help="Output format") # add - p_add = subparsers.add_parser("add", help="添加任务") - p_add.add_argument("--name", required=True, help="任务名称") - p_add.add_argument("--type", required=True, choices=["cron", "once"], help="任务类型") - p_add.add_argument("--schedule", help="Cron 表达式 (cron 类型必填)") - p_add.add_argument("--scheduled-at", help="执行时间 ISO 8601 格式 (once 类型必填)") - p_add.add_argument("--timezone", help="时区 (如 Asia/Tokyo),默认 UTC") - p_add.add_argument("--message", required=True, help="发送给 AI 的消息内容") + p_add = subparsers.add_parser("add", help="Add a task") + p_add.add_argument("--name", required=True, help="Task name") + p_add.add_argument("--type", required=True, choices=["cron", "once"], help="Task type") + p_add.add_argument("--schedule", help="Cron expression (required for cron type)") + p_add.add_argument("--scheduled-at", help="Execution time in ISO 8601 format (required for once type)") + p_add.add_argument("--timezone", help="Timezone (e.g. Asia/Tokyo), default UTC") + p_add.add_argument("--message", required=True, help="Message content to send to AI") # edit - p_edit = subparsers.add_parser("edit", help="编辑任务") - p_edit.add_argument("task_id", help="任务 ID") - p_edit.add_argument("--name", help="新任务名称") - p_edit.add_argument("--schedule", help="新 Cron 表达式") - p_edit.add_argument("--scheduled-at", help="新执行时间") - p_edit.add_argument("--timezone", help="新时区") - p_edit.add_argument("--message", help="新消息内容") + p_edit = subparsers.add_parser("edit", help="Edit a task") + p_edit.add_argument("task_id", help="Task ID") + p_edit.add_argument("--name", help="New task name") + p_edit.add_argument("--schedule", help="New cron expression") + p_edit.add_argument("--scheduled-at", help="New execution time") + p_edit.add_argument("--timezone", help="New timezone") + p_edit.add_argument("--message", help="New message content") # delete - p_delete = subparsers.add_parser("delete", help="删除任务") - p_delete.add_argument("task_id", help="任务 ID") + p_delete = subparsers.add_parser("delete", help="Delete a task") + p_delete.add_argument("task_id", help="Task ID") # toggle - p_toggle = subparsers.add_parser("toggle", help="暂停/恢复任务") - p_toggle.add_argument("task_id", help="任务 ID") + p_toggle = subparsers.add_parser("toggle", help="Pause/resume a task") + p_toggle.add_argument("task_id", help="Task ID") # logs - p_logs = subparsers.add_parser("logs", help="查看执行日志") - p_logs.add_argument("--task-id", help="按任务 ID 过滤") - p_logs.add_argument("--limit", type=int, default=10, help="显示条数") + p_logs = subparsers.add_parser("logs", help="View execution logs") + p_logs.add_argument("--task-id", help="Filter by task ID") + p_logs.add_argument("--limit", type=int, default=10, help="Number of entries to show") args = parser.parse_args() From 213e541697a5d735726ae75a4c46258892bb0da1 Mon Sep 17 00:00:00 2001 From: autobee-sparticle Date: Wed, 1 Apr 2026 22:04:31 +0900 Subject: [PATCH 17/17] fix: resolve PrePrompt Hook env var crash and increase Mem0 pool size (#24) 1. Fix "all environment values must be bytes or str" error in hook execution by ensuring all env values are converted to str (getattr may return None when attribute exists but is None). Also sanitize shell_env values. 2. Increase MEM0_POOL_SIZE default from 20 to 50 to address "connection pool exhausted" errors under high concurrency. Fixes: sparticleinc/felo-mygpt#2519 Co-authored-by: zhuchao Co-authored-by: Claude Opus 4.6 (1M context) --- agent/plugin_hook_loader.py | 21 ++++++++++++--------- utils/settings.py | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/agent/plugin_hook_loader.py b/agent/plugin_hook_loader.py index 7be815c..9ad8a10 100644 --- a/agent/plugin_hook_loader.py +++ b/agent/plugin_hook_loader.py @@ -165,27 +165,30 @@ async def _execute_command(skill_path: str, command: str, hook_type: str, config """ try: # 设置环境变量,传递给子进程 + # 注意:subprocess 要求所有 env 值必须是 str 类型, + # getattr 可能返回 None(属性存在但值为 None),需要确保转换为 str env = os.environ.copy() - env['ASSISTANT_ID'] = getattr(config, 'bot_id', '') - env['USER_IDENTIFIER'] = getattr(config, 'user_identifier', '') - env['TRACE_ID'] = getattr(config, 'trace_id', '') - env['SESSION_ID'] = getattr(config, 'session_id', '') - env['LANGUAGE'] = getattr(config, 'language', '') + env['ASSISTANT_ID'] = str(getattr(config, 'bot_id', '') or '') + env['USER_IDENTIFIER'] = str(getattr(config, 'user_identifier', '') or '') + env['TRACE_ID'] = str(getattr(config, 'trace_id', '') or '') + env['SESSION_ID'] = str(getattr(config, 'session_id', '') or '') + env['LANGUAGE'] = str(getattr(config, 'language', '') or '') env['HOOK_TYPE'] = hook_type # 合并 config 中的自定义 shell 环境变量 shell_env = getattr(config, 'shell_env', None) if shell_env: - env.update(shell_env) + # 确保所有自定义环境变量值也是字符串 + env.update({k: str(v) if v is not None else '' for k, v in shell_env.items()}) # 对于 PreSave,传递 content if hook_type == 'PreSave': - env['CONTENT'] = kwargs.get('content', '') - env['ROLE'] = kwargs.get('role', '') + env['CONTENT'] = str(kwargs.get('content', '') or '') + env['ROLE'] = str(kwargs.get('role', '') or '') # 对于 PostAgent,传递 response if hook_type == 'PostAgent': - env['RESPONSE'] = kwargs.get('response', '') + env['RESPONSE'] = str(kwargs.get('response', '') or '') metadata = kwargs.get('metadata', {}) env['METADATA'] = json.dumps(metadata) if metadata else '' diff --git a/utils/settings.py b/utils/settings.py index 915cc5a..16af267 100644 --- a/utils/settings.py +++ b/utils/settings.py @@ -57,7 +57,7 @@ CHECKPOINT_DB_URL = os.getenv("CHECKPOINT_DB_URL", "postgresql://moshui:@localho # 连接池大小 # 同时可以持有的最大连接数 CHECKPOINT_POOL_SIZE = int(os.getenv("CHECKPOINT_POOL_SIZE", "20")) -MEM0_POOL_SIZE = int(os.getenv("MEM0_POOL_SIZE", "20")) +MEM0_POOL_SIZE = int(os.getenv("MEM0_POOL_SIZE", "50")) # Checkpoint 自动清理配置 # 是否启用自动清理旧 session