refactor: use cron turn naming internally
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
"""Shared metadata helpers for scheduled automation turns."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Mapping
|
||||
|
||||
from nanobot.cron.types import CronJob
|
||||
|
||||
AUTOMATION_TRIGGER_META = "_automation_trigger"
|
||||
AUTOMATION_DEFER_UNTIL_IDLE_META = "_defer_until_session_idle"
|
||||
AUTOMATION_HISTORY_META = "_automation_turn"
|
||||
|
||||
|
||||
def automation_trigger(metadata: Mapping[str, Any] | None) -> dict[str, Any] | None:
|
||||
"""Return structured automation trigger metadata when present."""
|
||||
raw = (metadata or {}).get(AUTOMATION_TRIGGER_META)
|
||||
return raw if isinstance(raw, dict) else None
|
||||
|
||||
|
||||
def is_automation_turn(metadata: Mapping[str, Any] | None) -> bool:
|
||||
return automation_trigger(metadata) is not None
|
||||
|
||||
|
||||
def defer_until_session_idle(metadata: Mapping[str, Any] | None) -> bool:
|
||||
return bool(
|
||||
is_automation_turn(metadata)
|
||||
and (metadata or {}).get(AUTOMATION_DEFER_UNTIL_IDLE_META) is True
|
||||
)
|
||||
|
||||
|
||||
def automation_run_id(metadata: Mapping[str, Any] | None) -> str | None:
|
||||
trigger = automation_trigger(metadata)
|
||||
if not trigger:
|
||||
return None
|
||||
value = trigger.get("run_id")
|
||||
return value if isinstance(value, str) and value else None
|
||||
|
||||
|
||||
def is_bound_agent_job(job: CronJob) -> bool:
|
||||
"""True for new session-bound user automations, excluding legacy delivery payloads."""
|
||||
payload = job.payload
|
||||
if payload.kind != "agent_turn" or not payload.session_key:
|
||||
return False
|
||||
return not (
|
||||
payload.deliver
|
||||
or payload.channel
|
||||
or payload.to
|
||||
or payload.channel_meta
|
||||
)
|
||||
@@ -14,7 +14,7 @@ from typing import Any, Callable, Coroutine, Literal
|
||||
from filelock import FileLock
|
||||
from loguru import logger
|
||||
|
||||
from nanobot.cron.automation import is_bound_agent_job
|
||||
from nanobot.cron.session_turns import is_bound_cron_job
|
||||
from nanobot.cron.types import (
|
||||
CronJob,
|
||||
CronJobState,
|
||||
@@ -499,17 +499,17 @@ class CronService:
|
||||
jobs = store.jobs if include_disabled else [j for j in store.jobs if j.enabled]
|
||||
return sorted(jobs, key=lambda j: j.state.next_run_at_ms or float('inf'))
|
||||
|
||||
def list_bound_agent_jobs_for_session(
|
||||
def list_bound_cron_jobs_for_session(
|
||||
self,
|
||||
session_key: str,
|
||||
*,
|
||||
include_disabled: bool = True,
|
||||
) -> list[CronJob]:
|
||||
"""Return user-created bound automation jobs owned by *session_key*."""
|
||||
"""Return user-created bound cron jobs owned by *session_key*."""
|
||||
return [
|
||||
job
|
||||
for job in self.list_jobs(include_disabled=include_disabled)
|
||||
if is_bound_agent_job(job)
|
||||
if is_bound_cron_job(job)
|
||||
and job.payload.session_key == session_key
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Shared metadata helpers for scheduled cron session turns."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Mapping
|
||||
|
||||
from nanobot.cron.types import CronJob
|
||||
|
||||
CRON_TRIGGER_META = "_cron_trigger"
|
||||
CRON_DEFER_UNTIL_IDLE_META = "_cron_defer_until_session_idle"
|
||||
CRON_HISTORY_META = "_cron_turn"
|
||||
|
||||
|
||||
def cron_trigger(metadata: Mapping[str, Any] | None) -> dict[str, Any] | None:
|
||||
"""Return structured cron trigger metadata when present."""
|
||||
raw = (metadata or {}).get(CRON_TRIGGER_META)
|
||||
return raw if isinstance(raw, dict) else None
|
||||
|
||||
|
||||
def is_cron_turn(metadata: Mapping[str, Any] | None) -> bool:
|
||||
return cron_trigger(metadata) is not None
|
||||
|
||||
|
||||
def defer_cron_until_session_idle(metadata: Mapping[str, Any] | None) -> bool:
|
||||
return bool(
|
||||
is_cron_turn(metadata)
|
||||
and (metadata or {}).get(CRON_DEFER_UNTIL_IDLE_META) is True
|
||||
)
|
||||
|
||||
|
||||
def cron_run_id(metadata: Mapping[str, Any] | None) -> str | None:
|
||||
trigger = cron_trigger(metadata)
|
||||
if not trigger:
|
||||
return None
|
||||
value = trigger.get("run_id")
|
||||
return value if isinstance(value, str) and value else None
|
||||
|
||||
|
||||
def is_bound_cron_job(job: CronJob) -> bool:
|
||||
"""True for new session-bound cron jobs, excluding legacy delivery payloads."""
|
||||
payload = job.payload
|
||||
if payload.kind != "agent_turn" or not payload.session_key:
|
||||
return False
|
||||
return not (
|
||||
payload.deliver
|
||||
or payload.channel
|
||||
or payload.to
|
||||
or payload.channel_meta
|
||||
)
|
||||
Reference in New Issue
Block a user