feat(cron): enable and disable jobs from the tool

This commit is contained in:
david-dev666 authored and Xubin Ren committed 2026-10-25 14:08:36 +08:00
1 parent 60fd7c1a64
commit f971712f4f
5 files changed
+163 -9

No files matched your search

+30 -8
View File
@@ -11,17 +11,18 @@ from typing import Any
from nanobot.agent.tools.base import Tool, ToolResult, tool_parameters
from nanobot.agent.tools.context import ToolContext, current_request_context
from nanobot.agent.tools.schema import (
BooleanSchema,
IntegerSchema,
StringSchema,
tool_parameters_schema,
)
from nanobot.cron.service import CronService
from nanobot.cron.session_turns import is_cron_turn
from nanobot.cron.session_turns import is_bound_cron_job, is_cron_turn
from nanobot.cron.types import CronJob, CronJobState, CronSchedule
from nanobot.session.keys import UNIFIED_SESSION_KEY
_CRON_PARAMETERS = tool_parameters_schema(
action=StringSchema("Action to perform", enum=["add", "list", "remove", "update"]),
action=StringSchema("Action to perform", enum=["add", "list", "remove", "update", "enable", "disable"]),
name=StringSchema("Job label; generated from message for add if omitted."),
message=StringSchema(
"REQUIRED when action='add': non-empty instruction to execute when the job triggers. "
@@ -41,7 +42,10 @@ _CRON_PARAMETERS = tool_parameters_schema(
"ISO datetime for one-time execution (e.g. '2026-02-12T10:30:00'). "
"Naive values use the tool's default timezone."
),
job_id=StringSchema("REQUIRED when action='remove' or action='update'. Job ID (obtain via action='list')."),
job_id=StringSchema(
"REQUIRED when action='remove', 'update', 'enable', or 'disable'. Job ID (obtain via action='list')."
),
include_disabled=BooleanSchema(description="Include disabled jobs when action='list'."),
required=["action"],
description=(
"add requires exactly one schedule: every_seconds, cron_expr, or at. "
@@ -109,7 +113,7 @@ class CronTool(Tool):
@property
def description(self) -> str:
return (
"Schedule reminders and recurring tasks. Actions: add, list, remove, update. "
"Schedule reminders and recurring tasks. Actions: add, list, remove, update, enable, disable. "
f"New cron schedules and naive ISO times default to {self._default_timezone}. "
"Updates retain the existing cron timezone when tz is omitted."
)
@@ -119,7 +123,7 @@ class CronTool(Tool):
action = params.get("action")
if action == "add" and not str(params.get("message") or "").strip():
errors.append("message is required when action='add'")
if action in {"remove", "update"} and not str(params.get("job_id") or "").strip():
if action in {"remove", "update", "enable", "disable"} and not str(params.get("job_id") or "").strip():
errors.append(f"job_id is required when action='{action}'")
return errors
@@ -133,6 +137,7 @@ class CronTool(Tool):
tz: str | None = None,
at: str | None = None,
job_id: str | None = None,
include_disabled: bool = False,
) -> str:
if action == "add":
request = current_request_context()
@@ -140,11 +145,13 @@ class CronTool(Tool):
return ToolResult.error("Error: cannot schedule new jobs from within a cron job execution")
return self._add_job(name, message or "", every_seconds, cron_expr, tz, at)
elif action == "list":
return self._list_jobs()
return self._list_jobs(include_disabled=include_disabled)
elif action == "remove":
return self._remove_job(job_id)
elif action == "update":
return self._update_job(job_id, name, message, every_seconds, cron_expr, tz, at)
elif action in {"enable", "disable"}:
return self._set_job_enabled(job_id, enabled=action == "enable")
return f"Unknown action: {action}"
def _add_job(
@@ -303,14 +310,16 @@ class CronTool(Tool):
return "Dream memory consolidation for long-term memory."
return "System-managed internal job."
def _list_jobs(self) -> str:
jobs = self._cron.list_jobs()
def _list_jobs(self, include_disabled: bool = False) -> str:
jobs = self._cron.list_jobs(include_disabled=include_disabled)
if not jobs:
return "No scheduled jobs."
lines: list[str] = []
for j in jobs:
timing = self._format_timing(j.schedule)
parts = [f"- {j.name} (id: {j.id}, {timing})"]
if include_disabled:
parts.append(f" Status: {'enabled' if j.enabled else 'disabled'}")
if j.payload.kind == "system_event":
parts.append(f" Purpose: {self._system_job_purpose(j)}")
parts.append(" Protected: visible for inspection, but cannot be removed.")
@@ -318,6 +327,19 @@ class CronTool(Tool):
lines.append("\n".join(parts))
return "Scheduled jobs:\n" + "\n".join(lines)
def _set_job_enabled(self, job_id: str | None, *, enabled: bool) -> str:
if not job_id:
return ToolResult.error("Error: job_id is required for enable or disable")
job = self._cron.get_job(job_id)
if job is None:
return ToolResult.error(f"Error: job {job_id} not found")
if job.payload.kind == "system_event":
return ToolResult.error("Error: system-managed cron jobs are protected; change their configuration instead")
if enabled and not is_bound_cron_job(job):
return ToolResult.error("Error: job has no linked chat; recreate it from a chat session")
self._cron.enable_job(job_id, enabled=enabled)
return f"{'Enabled' if enabled else 'Disabled'} job {job_id}"
def _remove_job(self, job_id: str | None) -> str:
if not job_id:
return ToolResult.error("Error: job_id is required for remove")