CheddahBot/cheddahbot/tools/calendar_tool.py

63 lines
2.3 KiB
Python

"""Calendar/reminder tools: schedule tasks, set reminders."""
from __future__ import annotations
from . import tool
@tool(
"remember_this", "Save an important fact or instruction to long-term memory", category="memory"
)
def remember_this(text: str, ctx: dict | None = None) -> str:
if ctx and ctx.get("memory"):
ctx["memory"].remember(text)
return f"Saved to memory: {text}"
return "Memory system not available"
@tool("search_memory", "Search through saved memories", category="memory")
def search_memory(query: str, ctx: dict | None = None) -> str:
if ctx and ctx.get("memory"):
results = ctx["memory"].search(query)
if results:
return "\n".join(f"- [{r.get('score', 0):.2f}] {r['text']}" for r in results)
return "No matching memories found."
return "Memory system not available"
@tool("log_note", "Add a timestamped note to today's daily log", category="memory")
def log_note(text: str, ctx: dict | None = None) -> str:
if ctx and ctx.get("memory"):
ctx["memory"].log_daily(text)
return f"Logged: {text}"
return "Memory system not available"
@tool("schedule_task", "Schedule a recurring or one-time task", category="scheduling")
def schedule_task(name: str, prompt: str, schedule: str, ctx: dict | None = None) -> str:
"""Schedule a task. Schedule format: cron expression or 'once:YYYY-MM-DDTHH:MM'."""
if ctx and ctx.get("db"):
task_id = ctx["db"].add_scheduled_task(name, prompt, schedule)
return f"Scheduled task '{name}' (id={task_id}) with schedule: {schedule}"
return "Database not available"
@tool("list_tasks", "List all scheduled tasks", category="scheduling")
def list_tasks(ctx: dict | None = None) -> str:
if ctx and ctx.get("db"):
tasks = (
ctx["db"]
._conn.execute(
"SELECT id, name, schedule, enabled, next_run FROM scheduled_tasks ORDER BY id"
)
.fetchall()
)
if not tasks:
return "No scheduled tasks."
lines = []
for t in tasks:
status = "enabled" if t["enabled"] else "disabled"
lines.append(f"[{t['id']}] {t['name']} - {t['schedule']} ({status})")
return "\n".join(lines)
return "Database not available"