mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-04-30 06:45:55 +00:00
fix(cron): add optional name parameter to separate job label from message (#2680)
This commit is contained in:
parent
44c7992095
commit
f4904c4bdf
@ -13,6 +13,10 @@ from nanobot.cron.types import CronJob, CronJobState, CronSchedule
|
|||||||
@tool_parameters(
|
@tool_parameters(
|
||||||
tool_parameters_schema(
|
tool_parameters_schema(
|
||||||
action=StringSchema("Action to perform", enum=["add", "list", "remove"]),
|
action=StringSchema("Action to perform", enum=["add", "list", "remove"]),
|
||||||
|
name=StringSchema(
|
||||||
|
"Optional short human-readable label for the job "
|
||||||
|
"(e.g., 'weather-monitor', 'daily-standup'). Defaults to first 30 chars of message."
|
||||||
|
),
|
||||||
message=StringSchema(
|
message=StringSchema(
|
||||||
"Instruction for the agent to execute when the job triggers "
|
"Instruction for the agent to execute when the job triggers "
|
||||||
"(e.g., 'Send a reminder to WeChat: xxx' or 'Check system status and report')"
|
"(e.g., 'Send a reminder to WeChat: xxx' or 'Check system status and report')"
|
||||||
@ -93,6 +97,7 @@ class CronTool(Tool):
|
|||||||
async def execute(
|
async def execute(
|
||||||
self,
|
self,
|
||||||
action: str,
|
action: str,
|
||||||
|
name: str | None = None,
|
||||||
message: str = "",
|
message: str = "",
|
||||||
every_seconds: int | None = None,
|
every_seconds: int | None = None,
|
||||||
cron_expr: str | None = None,
|
cron_expr: str | None = None,
|
||||||
@ -105,7 +110,7 @@ class CronTool(Tool):
|
|||||||
if action == "add":
|
if action == "add":
|
||||||
if self._in_cron_context.get():
|
if self._in_cron_context.get():
|
||||||
return "Error: cannot schedule new jobs from within a cron job execution"
|
return "Error: cannot schedule new jobs from within a cron job execution"
|
||||||
return self._add_job(message, every_seconds, cron_expr, tz, at, deliver)
|
return self._add_job(name, message, every_seconds, cron_expr, tz, at, deliver)
|
||||||
elif action == "list":
|
elif action == "list":
|
||||||
return self._list_jobs()
|
return self._list_jobs()
|
||||||
elif action == "remove":
|
elif action == "remove":
|
||||||
@ -114,6 +119,7 @@ class CronTool(Tool):
|
|||||||
|
|
||||||
def _add_job(
|
def _add_job(
|
||||||
self,
|
self,
|
||||||
|
name: str | None,
|
||||||
message: str,
|
message: str,
|
||||||
every_seconds: int | None,
|
every_seconds: int | None,
|
||||||
cron_expr: str | None,
|
cron_expr: str | None,
|
||||||
@ -158,7 +164,7 @@ class CronTool(Tool):
|
|||||||
return "Error: either every_seconds, cron_expr, or at is required"
|
return "Error: either every_seconds, cron_expr, or at is required"
|
||||||
|
|
||||||
job = self._cron.add_job(
|
job = self._cron.add_job(
|
||||||
name=message[:30],
|
name=name or message[:30],
|
||||||
schedule=schedule,
|
schedule=schedule,
|
||||||
message=message,
|
message=message,
|
||||||
deliver=deliver,
|
deliver=deliver,
|
||||||
|
|||||||
@ -299,7 +299,7 @@ def test_add_cron_job_defaults_to_tool_timezone(tmp_path) -> None:
|
|||||||
tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
|
tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
|
||||||
tool.set_context("telegram", "chat-1")
|
tool.set_context("telegram", "chat-1")
|
||||||
|
|
||||||
result = tool._add_job("Morning standup", None, "0 8 * * *", None, None)
|
result = tool._add_job(None, "Morning standup", None, "0 8 * * *", None, None)
|
||||||
|
|
||||||
assert result.startswith("Created job")
|
assert result.startswith("Created job")
|
||||||
job = tool._cron.list_jobs()[0]
|
job = tool._cron.list_jobs()[0]
|
||||||
@ -310,7 +310,7 @@ def test_add_at_job_uses_default_timezone_for_naive_datetime(tmp_path) -> None:
|
|||||||
tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
|
tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai")
|
||||||
tool.set_context("telegram", "chat-1")
|
tool.set_context("telegram", "chat-1")
|
||||||
|
|
||||||
result = tool._add_job("Morning reminder", None, None, None, "2026-03-25T08:00:00")
|
result = tool._add_job(None, "Morning reminder", None, None, None, "2026-03-25T08:00:00")
|
||||||
|
|
||||||
assert result.startswith("Created job")
|
assert result.startswith("Created job")
|
||||||
job = tool._cron.list_jobs()[0]
|
job = tool._cron.list_jobs()[0]
|
||||||
@ -322,7 +322,7 @@ def test_add_job_delivers_by_default(tmp_path) -> None:
|
|||||||
tool = _make_tool(tmp_path)
|
tool = _make_tool(tmp_path)
|
||||||
tool.set_context("telegram", "chat-1")
|
tool.set_context("telegram", "chat-1")
|
||||||
|
|
||||||
result = tool._add_job("Morning standup", 60, None, None, None)
|
result = tool._add_job(None, "Morning standup", 60, None, None, None)
|
||||||
|
|
||||||
assert result.startswith("Created job")
|
assert result.startswith("Created job")
|
||||||
job = tool._cron.list_jobs()[0]
|
job = tool._cron.list_jobs()[0]
|
||||||
@ -333,7 +333,7 @@ def test_add_job_can_disable_delivery(tmp_path) -> None:
|
|||||||
tool = _make_tool(tmp_path)
|
tool = _make_tool(tmp_path)
|
||||||
tool.set_context("telegram", "chat-1")
|
tool.set_context("telegram", "chat-1")
|
||||||
|
|
||||||
result = tool._add_job("Background refresh", 60, None, None, None, deliver=False)
|
result = tool._add_job(None, "Background refresh", 60, None, None, None, deliver=False)
|
||||||
|
|
||||||
assert result.startswith("Created job")
|
assert result.startswith("Created job")
|
||||||
job = tool._cron.list_jobs()[0]
|
job = tool._cron.list_jobs()[0]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user