Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: 定时任务频道改为枚举值

douran4bc5183388bd
diff --git a/jiuwenclaw/gateway/cron/__init__.py b/jiuwenclaw/gateway/cron/__init__.py
index caaffb6e7d..57693cf7ed 100644
--- a/jiuwenclaw/gateway/cron/__init__.py
+++ b/jiuwenclaw/gateway/cron/__init__.py
@@ -5,7 +5,7 @@ This package provides:
- An asyncio scheduler that wakes the agent before push time and pushes results to channels
"""
-from .models import CronJob, CronTarget
+from .models import CronJob, CronTarget, CronTargetChannel
from .store import CronJobStore
from .scheduler import CronSchedulerService
from .controller import CronController
@@ -13,6 +13,7 @@ from .controller import CronController
__all__ = [
"CronJob",
"CronTarget",
+ "CronTargetChannel",
"CronJobStore",
"CronSchedulerService",
"CronController",
diff --git a/jiuwenclaw/gateway/cron/controller.py b/jiuwenclaw/gateway/cron/controller.py
index 1a51682792..c2859342f2 100644
--- a/jiuwenclaw/gateway/cron/controller.py
+++ b/jiuwenclaw/gateway/cron/controller.py
@@ -6,6 +6,7 @@ from typing import Any, ClassVar, List
from openjiuwen.core.foundation.tool import LocalFunction, Tool, ToolCard
from zoneinfo import ZoneInfo
+from jiuwenclaw.gateway.cron.models import CronTargetChannel
from jiuwenclaw.gateway.cron.scheduler import CronSchedulerService, _cron_next_push_dt
from jiuwenclaw.gateway.cron.store import CronJobStore
@@ -63,6 +64,16 @@ class CronController:
_DESCRIPTION_TIME_KEYWORDS = ("每天", "每周", "每月", "上午", "下午", "早上", "晚上", "凌晨")
+ @staticmethod
+ def _normalize_targets(raw: Any) -> str:
+ """将 targets 规范为 CronTargetChannel 枚举值,非法则默认 web。"""
+ s = str(raw or "").strip() or "web"
+ try:
+ CronTargetChannel(s)
+ return s
+ except ValueError:
+ return CronTargetChannel.WEB.value
+
@classmethod
def _normalize_description(cls, description: str, name: str) -> str:
"""若 description 含时间/频率用语且 name 为纯任务,则只保留任务内容(用 name)。"""
@@ -92,7 +103,8 @@ class CronController:
enabled = bool(params.get("enabled", True))
description = str(params.get("description") or "")
wake_offset_seconds = params.get("wake_offset_seconds", None)
- targets = params.get("targets") or "web"
+ raw_targets = params.get("targets") or "web"
+ targets = self._normalize_targets(raw_targets)
# if not targets:
# from jiuwenclaw.agentserver.request_context import get_current_request
@@ -119,6 +131,8 @@ class CronController:
async def update_job(self, job_id: str, patch: dict[str, Any]) -> dict[str, Any]:
patch = dict(patch or {})
+ if "targets" in patch:
+ patch["targets"] = self._normalize_targets(patch["targets"])
existing = await self._store.get_job(job_id)
if existing is None:
raise KeyError("job not found")
@@ -278,8 +292,9 @@ class CronController:
},
"targets": {
"type": "string",
- "description": "推送频道,如网页用web, 飞书用feishu, 小艺用xiaoyi",
- "default": "web"
+ "enum": [e.value for e in CronTargetChannel],
+ "description": "推送频道:web=网页, feishu=飞书, xiaoyi=小艺, dingtalk=钉钉",
+ "default": CronTargetChannel.WEB.value,
},
"enabled": {
"type": "boolean",
@@ -310,6 +325,13 @@ class CronController:
"patch": {
"type": "object",
"description": "Fields to update (name, enabled, cron_expr, timezone, description, wake_offset_seconds, targets)",
+ "properties": {
+ "targets": {
+ "type": "string",
+ "enum": [e.value for e in CronTargetChannel],
+ "description": "推送频道:web/feishu/xiaoyi/dingtalk",
+ },
+ },
},
},
"required": ["job_id", "patch"],
diff --git a/jiuwenclaw/gateway/cron/models.py b/jiuwenclaw/gateway/cron/models.py
index 6e70ccdf95..5692d80135 100644
--- a/jiuwenclaw/gateway/cron/models.py
+++ b/jiuwenclaw/gateway/cron/models.py
@@ -1,9 +1,29 @@
from __future__ import annotations
from dataclasses import dataclass, field
+from enum import Enum
from typing import Any
+class CronTargetChannel(str, Enum):
+ """推送频道枚举。"""
+
+ WEB = "web"
+ FEISHU = "feishu"
+ XIAOYI = "xiaoyi"
+ DINGTALK = "dingtalk"
+
+
+def _normalize_targets_str(raw: str) -> str:
+ """将 targets 字符串规范为 CronTargetChannel 枚举值,非法则默认 web。"""
+ s = (raw or "").strip() or "web"
+ try:
+ CronTargetChannel(s)
+ return s
+ except ValueError:
+ return CronTargetChannel.WEB.value
+
+
@dataclass(frozen=True)
class CronTarget:
"""Where to push cron results."""
@@ -106,6 +126,8 @@ class CronJob:
if not targets_str:
raise ValueError("targets is required")
+ targets_str = _normalize_targets_str(targets_str)
+
return CronJob(
id=job_id,
name=name,
diff --git a/jiuwenclaw/resources/cron_test.py b/jiuwenclaw/resources/cron_test.py
index 299ac908c0..732c07bc5a 100644
--- a/jiuwenclaw/resources/cron_test.py
+++ b/jiuwenclaw/resources/cron_test.py
@@ -90,12 +90,7 @@ async def main() -> None:
"timezone": "Asia/Shanghai",
"wake_offset_seconds": 60,
"description": "你叫什么名字?",
- "targets": [
- {
- "channel_id": "web",
- "session_id": "cron-demo-session",
- }
- ],
+ "targets": "web",
}
job = await controller.create_job(params)