mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-07-13 12:38:02 +00:00
refactor(tools): split tool_implementations.py into src/tools/ package (#4423)
* test(tools): add shim protection test for tool_implementations split
Covers all 48 top-level functions (33 do_* + 15 _helpers) extracted from
the original module. Guards the upcoming split: the shim must re-export
every symbol so existing 'from src.tool_implementations import X' imports
keep working. Passes on baseline (pre-split).
* refactor(tools): add src/tools/ package with shared _common
Slice 1 Task 2 (#4082/#4071). Adds the package skeleton and moves the
shared _parse_tool_args helper into src/tools/_common.py. Domain modules
will import from here. tool_implementations.py is untouched at this step.
* refactor(tools): extract system domain into src/tools/system.py
Slice 1 (#4082/#4071), Task 3: move the system-domain tool functions
(do_manage_skills/_skill_dump/do_manage_tasks/do_manage_endpoints/
do_manage_mcp/do_manage_webhooks/do_manage_tokens/do_manage_settings/
do_api_call/do_app_api) and the app_api blocklist constants out of
tool_implementations.py into a new src/tools/system.py module.
tool_implementations.py re-imports all of them so it stays a working
backward-compatible facade (shim test stays green).
- do_manage_mcp resolves get_mcp_manager via a function-local import
from tool_implementations so the test that patches
src.tool_implementations.get_mcp_manager still applies post-move.
- do_app_api imports _internal_headers and _INTERNAL_BASE (still in
tool_implementations) function-locally to avoid a circular import.
- Repoint test_context_budget introspection assertion to the moved
code's new home in src/tools/system.py.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* refactor(tools): extract cookbook domain into src/tools/cookbook.py
Moves the model-serving (cookbook) tool domain out of tool_implementations.py
into src/tools/cookbook.py as part of slice 1 (#4082/#4071):
- 13 do_* tools: download/serve/list/stop/tail/search/adopt/cached models,
list downloads/cancel, list cookbook servers, serve presets
- 9 private helpers: _cookbook_servers, _resolve_cookbook_host,
_cookbook_env_for_host, _infer_serve_{port,host}, _ensure_served_endpoint,
_cookbook_register_task, _cookbook_apply_retry_suggestion,
_scan_running_model_processes, _cookbook_kill_session
- _MODEL_PROCESS_PATTERNS constant (used only by _scan_running_model_processes)
tool_implementations.py stays a backward-compatible facade via a re-import
from src.tools.cookbook; src/tools/__init__ re-exports the same symbols.
_internal_headers and _INTERNAL_BASE stay in tool_implementations.py (shared
by system.py's do_app_api and many cookbook funcs). Each cookbook function
that needs them does a function-local import to avoid a top-level circular
dependency, matching the system-domain split.
Verified: compileall clean; shim test green; cookbook-touching suite
(652 passed, 1 skipped); full suite 3587 passed, 2 failed
(pre-existing test_api_chat_security, unrelated).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* refactor(tools): extract search domain into src/tools/search.py
* refactor(tools): extract notes domain into src/tools/notes.py
* refactor(tools): extract calendar domain into src/tools/calendar.py
Repoints tests/test_caldav_bidirectional_sync.py source-introspection
to src/tools/calendar.py (do_manage_calendar moved there).
* refactor(tools): extract image domain into src/tools/image.py
* refactor(tools): extract research domain into src/tools/research.py
* refactor(tools): extract contacts domain into src/tools/contacts.py
* refactor(tools): extract vault domain into src/tools/vault.py
Repoints tests/test_vault_password_not_in_argv.py source-introspection
to src/tools/vault.py (the vault do_* helpers moved there).
* refactor(tools): collapse tool_implementations to clean re-export shim
Move shared _INTERNAL_BASE/_internal_headers to src/tools/_common.py and
drop the duplicate _parse_tool_args (already in _common). tool_implementations.py
is now a pure re-export facade (+ 3 pre-existing email-context helpers, out of
scope). Domain files' function-local imports of these names still resolve via
the facade re-export.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* fix(tools): port upstream cookbook workflow changes to split module
Rebase onto dev dropped c504214 ("Cookbook model workflow fixes") edits
to do_serve_model / do_tail_serve_output: the extraction commit moved
the pre-edit bodies into src/tools/cookbook.py and git auto-accepted the
deletion from tool_implementations.py, losing dev's changes. Restore them
in their post-split home:
- do_serve_model: add where/log_path/next_tools and the expanded
"Next required check" output message
- do_tail_serve_output: empty-output fallback message replacing
"(empty pane)"
(do_manage_settings web_fetch alias edit was already applied to
src/tools/system.py during the system-extract conflict resolution.)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* fix(tools): break admin_tools circular import in split facade
After rebasing onto dev (#3629 moved the admin manage_* tools into
src/agent_tools/admin_tools), the facade re-exported them via a top-level
`from src.agent_tools.admin_tools import ...`. But src.agent_tools.__init__
imports this facade at top level, so the eager import re-entered the
partially-initialized agent_tools package and broke collection.
Re-export the admin symbols (do_manage_endpoints/mcp/webhooks/tokens/
settings, _MCP_DENIED_COMMANDS, _validate_mcp_command) lazily through
module __getattr__ instead, and drop them from src/tools/__init__ (they
no longer live in the src.tools package). system.py now holds only the
skills/tasks/api bridges; admin tools live solely in admin_tools.py,
matching upstream.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* fix(tools): re-export dropped helpers through the split shim
Address review finding from #4423: the compatibility facade claimed to
preserve every original top-level symbol but omitted three helpers the
old src.tool_implementations exposed. Re-export them and pin them in
the shim protection test:
- _string_arg, _validate_cookbook_ssh_target <- src/tools/cookbook.py
- _mcp_allowed_commands <- src/agent_tools/admin_tools.py (lazily via
__getattr__, to keep the agent_tools.__init__ <-> facade import acyclic
after the #3629 admin-tools migration)
All three added to tests/test_tool_implementations_shim.py _EXPECTED so
the test contract now matches its "every original top-level function"
comment.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* test(tools): self-verify shim re-exports every domain do_*
The hand-maintained _EXPECTED list in the shim protection test can drift
silently when a new tool is added to a domain module but not re-exported
by the facade — exactly the omission a reviewer flagged post-split.
Add an auto-discovering test that enumerates every do_* from the domain
modules (incl. admin_tools) and asserts reachability through the shim,
so a forgotten re-export fails the build automatically.
Uses hasattr (not dir(ti)) because the admin symbols are re-exported
lazily via module __getattr__ and don't appear in dir(ti).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* test(tools): self-verify every in-repo facade import resolves
RaresKeY's P3 on the shim test was a claim-vs-reality gap: the docstring
said it protected "every from src.tool_implementations import X" but the
hand-maintained _EXPECTED list omitted three underscore helpers, so the
claim wasn't enforced. Re-exporting the three (cf1f5e3) fixed the known
gap; this closes the structural one.
Add test_every_facade_import_in_repo_resolves: ast-enumerate every
`from src.tool_implementations import X` site in src/ and tests/ and
assert hasattr(ti, X) for each. A forgotten re-export that anything in
the repo imports now fails the build automatically — including underscore
helpers, which the do_* discovery test does not cover.
Together with test_shim_reexports_every_domain_do_function, the shim
contract is now self-verifying. Demote _EXPECTED in the docstring to the
curated historical/downstream surface (the three helpers have no in-repo
consumer, so they stay manual by necessity) instead of "ground truth".
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* fix(tools): dedupe _parse_tool_args + align shim guard with route consumers
Addresses two P3s from review (RaresKeY, 2026-06-26):
1. maintainability — _common carried a full copy of _parse_tool_args
alongside the canonical src.tool_utils one; future parser fixes could
diverge. The two bodies were byte-identical in logic, so _common now
re-exports from tool_utils (a leaf module, no circular-import risk).
The single-source test is extended to assert _common._parse_tool_args
and tool_implementations._parse_tool_args are the same object as
tool_utils._parse_tool_args.
2. test — the shim guard's import-site scan only walked src/ and tests/,
missing routes/chat_routes.py's clear_active_email/set_active_email
imports, and _EXPECTED omitted the active-email facade helpers. The
scan now walks every first-party Python dir (pruning venvs/caches/data
in-place), and set/get/clear_active_email are added to _EXPECTED
(get_active_email has no in-repo importer, so the scan alone can't see
it).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: yuandonghao <yuandonghao@cohl.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,522 @@
|
||||
"""Calendar-domain tool implementations.
|
||||
|
||||
Extracted from tool_implementations.py as part of slice 1 (#4082/#4071).
|
||||
Holds the manage_calendar tool (CalDAV-backed event CRUD).
|
||||
``src.tool_implementations`` re-exports these for backward compatibility.
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from typing import Dict, Optional
|
||||
|
||||
from src.tools._common import _parse_tool_args
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def do_manage_calendar(content: str, owner: Optional[str] = None) -> Dict:
|
||||
"""Handle manage_calendar tool calls: list/create/update/delete calendar events (local SQLite)."""
|
||||
from datetime import datetime, timedelta
|
||||
from core.database import SessionLocal, CalendarCal, CalendarEvent, Note
|
||||
from routes.calendar_routes import (
|
||||
_ensure_default_calendar,
|
||||
_parse_dt,
|
||||
_parse_dt_pair,
|
||||
parse_due_for_user,
|
||||
_resolve_base_uid,
|
||||
_push_caldav_event_after_commit,
|
||||
_record_caldav_delete_tombstone,
|
||||
)
|
||||
import uuid as _uuid
|
||||
|
||||
try:
|
||||
args = _parse_tool_args(content)
|
||||
except ValueError:
|
||||
return {"error": "Invalid JSON arguments", "exit_code": 1}
|
||||
|
||||
# ── Batch normalization ──
|
||||
# Some models (e.g. deepseek-v4-flash) emit {"events": [{...}, ...]}
|
||||
# instead of individual create_event calls. Iterate and create each.
|
||||
if isinstance(args.get("events"), list) and not args.get("action"):
|
||||
results = []
|
||||
for ev in args["events"]:
|
||||
if not isinstance(ev, dict):
|
||||
continue
|
||||
# Normalize start/end from {dateTime: "..."} object to flat string
|
||||
for field, target in [("start", "dtstart"), ("end", "dtend")]:
|
||||
val = ev.pop(field, None)
|
||||
if val and target not in ev:
|
||||
ev[target] = val.get("dateTime", val) if isinstance(val, dict) else val
|
||||
ev.setdefault("action", "create_event")
|
||||
r = await do_manage_calendar(json.dumps(ev), owner=owner)
|
||||
results.append(r)
|
||||
created = [r for r in results if r.get("exit_code") == 0 and not r.get("error")]
|
||||
failed = [r for r in results if r.get("error")]
|
||||
|
||||
if not results:
|
||||
return {"error": "No events to create", "exit_code": 1}
|
||||
|
||||
# Surface both successes and failures
|
||||
parts = []
|
||||
if created:
|
||||
summaries = [r.get("response", "") for r in created]
|
||||
parts.append(f"Created {len(created)} event(s):\n" + "\n".join(summaries))
|
||||
if failed:
|
||||
first_error = failed[0].get("error", "Unknown error")
|
||||
parts.append(f"Failed to create {len(failed)} event(s). First error: {first_error}")
|
||||
|
||||
response = "\n\n".join(parts)
|
||||
# Non-zero exit code for partial or total failure
|
||||
exit_code = 0 if not failed else 1
|
||||
return {"response": response, "exit_code": exit_code, "created_count": len(created), "failed_count": len(failed)}
|
||||
|
||||
# Normalize action — some models emit hyphens ("list-calendars") instead
|
||||
# of underscores. Treat them as equivalent so we don't bounce a
|
||||
# cosmetic typo back to the model and waste a round-trip. Also accept
|
||||
# short forms (`create`, `update`, `delete`) as aliases for the
|
||||
# full `<verb>_event` names — models keep emitting the short forms.
|
||||
action = (args.get("action") or "list_events").replace("-", "_").strip().lower()
|
||||
_ACTION_ALIASES = {
|
||||
"create": "create_event",
|
||||
"update": "update_event",
|
||||
"delete": "delete_event",
|
||||
"list": "list_events",
|
||||
}
|
||||
action = _ACTION_ALIASES.get(action, action)
|
||||
db = SessionLocal()
|
||||
|
||||
def _calendar_query():
|
||||
q = db.query(CalendarCal)
|
||||
if owner is not None:
|
||||
q = q.filter(CalendarCal.owner == owner)
|
||||
return q
|
||||
|
||||
def _event_query():
|
||||
q = db.query(CalendarEvent).join(CalendarCal)
|
||||
if owner is not None:
|
||||
q = q.filter(CalendarCal.owner == owner)
|
||||
return q
|
||||
|
||||
def _reminder_minutes(raw_args) -> Optional[int]:
|
||||
raw = (
|
||||
raw_args.get("reminder_minutes")
|
||||
or raw_args.get("remind_before_minutes")
|
||||
or raw_args.get("alarm_minutes")
|
||||
or raw_args.get("reminder")
|
||||
or raw_args.get("alarm")
|
||||
)
|
||||
if raw in (None, ""):
|
||||
desc = str(raw_args.get("description") or "")
|
||||
if re.search(r"\b(remind|reminder|alarm)\b", desc, re.I):
|
||||
raw = desc
|
||||
if raw in (None, "", False):
|
||||
return None
|
||||
if raw is True:
|
||||
return 10
|
||||
if isinstance(raw, (int, float)):
|
||||
return max(0, int(raw))
|
||||
text = str(raw).strip().lower()
|
||||
if text in {"none", "no", "off", "false"}:
|
||||
return None
|
||||
m = re.search(r"(\d+)\s*(?:minutes?|mins?|m)\b", text)
|
||||
if m:
|
||||
return max(0, int(m.group(1)))
|
||||
m = re.search(r"(\d+)\s*(?:hours?|hrs?|h)\b", text)
|
||||
if m:
|
||||
return max(0, int(m.group(1)) * 60)
|
||||
if text.isdigit():
|
||||
return max(0, int(text))
|
||||
return None
|
||||
|
||||
def _event_description(raw_args, minutes_before: Optional[int]) -> str:
|
||||
desc = str(raw_args.get("description", "") or "")
|
||||
if minutes_before is None:
|
||||
return desc
|
||||
reminder_only = re.compile(
|
||||
r"^\s*(?:remind(?:er)?|alarm)\s*:?\s*\d+\s*"
|
||||
r"(?:minutes?|mins?|m|hours?|hrs?|h)\b.*$",
|
||||
re.I,
|
||||
)
|
||||
return "" if reminder_only.match(desc) else desc
|
||||
|
||||
def _parse_event_dt(raw: str) -> tuple[datetime, bool]:
|
||||
"""Parse agent event datetimes in the user's timezone when available."""
|
||||
return _parse_dt_pair(parse_due_for_user(raw))
|
||||
|
||||
def _first_nonempty_arg(*names: str):
|
||||
for name in names:
|
||||
value = args.get(name)
|
||||
if value not in (None, ""):
|
||||
return value
|
||||
return None
|
||||
|
||||
def _create_calendar_reminder(summary: str, location: str, dtstart: datetime,
|
||||
all_day: bool, minutes_before: int,
|
||||
is_utc: bool = False) -> tuple[Optional[str], Optional[str]]:
|
||||
remind_at = dtstart - timedelta(minutes=minutes_before)
|
||||
now = datetime.utcnow() if is_utc else datetime.now()
|
||||
if dtstart <= now:
|
||||
return None, "event already passed"
|
||||
if remind_at <= now:
|
||||
# If the requested "before" time already passed but the event is
|
||||
# still upcoming, create an immediate Note reminder instead of
|
||||
# silently dropping it.
|
||||
remind_at = now
|
||||
start_fmt = dtstart.strftime("%a %b %d") if all_day else dtstart.strftime("%a %b %d %H:%M")
|
||||
loc = f" @ {location}" if location else ""
|
||||
text = f"{summary}{loc} — {start_fmt}"
|
||||
due_date = remind_at.isoformat() + ("Z" if is_utc else "")
|
||||
expected_title = f"Reminder: {summary}"
|
||||
existing_q = db.query(Note).filter(
|
||||
Note.archived == False, # noqa: E712
|
||||
Note.due_date == due_date,
|
||||
)
|
||||
if owner is not None:
|
||||
existing_q = existing_q.filter(Note.owner == owner)
|
||||
target_title = re.sub(r"^\s*reminder\s*:\s*", "", expected_title.strip().lower())
|
||||
for existing in existing_q.limit(25).all():
|
||||
existing_title = re.sub(r"^\s*reminder\s*:\s*", "", (existing.title or "").strip().lower())
|
||||
if existing_title == target_title:
|
||||
return existing.id, "duplicate reminder already exists"
|
||||
note = Note(
|
||||
id=str(_uuid.uuid4()),
|
||||
owner=owner,
|
||||
title=expected_title,
|
||||
items=json.dumps([{"text": text, "done": False, "checked": False}]),
|
||||
note_type="todo",
|
||||
label="calendar",
|
||||
due_date=due_date,
|
||||
source="calendar",
|
||||
)
|
||||
db.add(note)
|
||||
return note.id, None
|
||||
|
||||
try:
|
||||
if action == "list_calendars":
|
||||
_ensure_default_calendar(db, owner)
|
||||
cals = _calendar_query().all()
|
||||
result = [{"name": c.name, "href": c.id} for c in cals]
|
||||
if result:
|
||||
lines = [f"Found {len(result)} calendar(s):"]
|
||||
for c in result:
|
||||
lines.append(f"- {c['name']} ({c['href'][:8]})")
|
||||
response_text = "\n".join(lines)
|
||||
else:
|
||||
response_text = "No calendars found."
|
||||
return {"response": response_text, "calendars": result, "exit_code": 0}
|
||||
|
||||
elif action == "list_events":
|
||||
try:
|
||||
start_raw = _first_nonempty_arg(
|
||||
"start", "start_date", "range_start", "from", "dtstart", "since"
|
||||
)
|
||||
end_raw = _first_nonempty_arg(
|
||||
"end", "end_date", "range_end", "to", "dtend", "until"
|
||||
)
|
||||
if start_raw:
|
||||
start_dt = _parse_dt(start_raw)
|
||||
else:
|
||||
start_dt = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
if end_raw:
|
||||
end_dt = _parse_dt(end_raw)
|
||||
else:
|
||||
end_dt = start_dt + timedelta(days=14)
|
||||
except ValueError as e:
|
||||
return {"error": f"Invalid date format: {e}", "exit_code": 1}
|
||||
|
||||
if end_dt <= start_dt:
|
||||
end_dt = start_dt + timedelta(days=1)
|
||||
|
||||
q = _event_query().filter(
|
||||
CalendarEvent.dtstart < end_dt,
|
||||
CalendarEvent.dtend > start_dt,
|
||||
CalendarEvent.status != "cancelled",
|
||||
)
|
||||
calendar_filter = args.get("calendar")
|
||||
if calendar_filter:
|
||||
q = q.filter(
|
||||
(CalendarEvent.calendar_id == calendar_filter) |
|
||||
(CalendarCal.name == calendar_filter)
|
||||
)
|
||||
rows = q.order_by(CalendarEvent.dtstart).all()
|
||||
events = []
|
||||
for ev in rows:
|
||||
if ev.all_day:
|
||||
s, e = ev.dtstart.strftime("%Y-%m-%d"), ev.dtend.strftime("%Y-%m-%d")
|
||||
else:
|
||||
suffix = "Z" if getattr(ev, "is_utc", False) else ""
|
||||
s, e = ev.dtstart.isoformat() + suffix, ev.dtend.isoformat() + suffix
|
||||
events.append({
|
||||
"uid": ev.uid, "summary": ev.summary or "", "dtstart": s, "dtend": e,
|
||||
"all_day": ev.all_day, "description": ev.description or "",
|
||||
"location": ev.location or "",
|
||||
"calendar": ev.calendar.name if ev.calendar else "",
|
||||
"calendar_href": ev.calendar_id,
|
||||
"event_type": ev.event_type or "",
|
||||
"importance": ev.importance or "normal",
|
||||
})
|
||||
if not events:
|
||||
response_text = f"No events between {start_dt.date().isoformat()} and {end_dt.date().isoformat()}."
|
||||
else:
|
||||
lines = [f"Found {len(events)} event(s) between {start_dt.date().isoformat()} and {end_dt.date().isoformat()}:"]
|
||||
for ev in events:
|
||||
when = ev["dtstart"]
|
||||
when_str = f"{when} (all day)" if ev.get("all_day") else f"{when} -> {ev.get('dtend', '')}"
|
||||
# Clickable anchor — opens the calendar on the event's day.
|
||||
line = f"- {when_str}: [{ev['summary']}](#event-{ev['uid']})"
|
||||
if ev.get("event_type"):
|
||||
line += f" #{ev['event_type']}"
|
||||
if ev.get("importance") and ev["importance"] != "normal":
|
||||
line += f" !{ev['importance']}"
|
||||
if ev.get("location"):
|
||||
line += f" @ {ev['location']}"
|
||||
if ev.get("calendar"):
|
||||
line += f" ({ev['calendar']})"
|
||||
if ev.get("description"):
|
||||
desc = ev["description"].strip().replace("\n", " ")
|
||||
if len(desc) > 120:
|
||||
desc = desc[:117] + "..."
|
||||
line += f"\n {desc}"
|
||||
lines.append(line)
|
||||
response_text = "\n".join(lines)
|
||||
return {"response": response_text, "events": events, "exit_code": 0}
|
||||
|
||||
elif action == "create_event":
|
||||
summary = args.get("summary")
|
||||
# Accept the various names models like to use for the start
|
||||
# field: dtstart (canonical), start, start_time, when.
|
||||
dtstart_str = (args.get("dtstart") or args.get("start")
|
||||
or args.get("start_time") or args.get("when"))
|
||||
if not summary or not dtstart_str:
|
||||
return {"error": "summary and dtstart are required", "exit_code": 1}
|
||||
|
||||
# Accept either an href OR a calendar name/short-id like "Main"
|
||||
# or "62e545d8" — saves the model from having to memorize hrefs
|
||||
# after a `list_calendars` call returned short prefixes.
|
||||
cal_href = args.get("calendar_href") or args.get("calendar")
|
||||
cal = None
|
||||
if cal_href:
|
||||
cal = (_calendar_query()
|
||||
.filter(CalendarCal.id == cal_href)
|
||||
.first())
|
||||
if not cal:
|
||||
# Try by name (case-insensitive) or by short-id prefix
|
||||
cal = (_calendar_query()
|
||||
.filter(CalendarCal.name.ilike(cal_href))
|
||||
.first())
|
||||
if not cal:
|
||||
cal = (_calendar_query()
|
||||
.filter(CalendarCal.id.like(f"{cal_href}%"))
|
||||
.first())
|
||||
if not cal:
|
||||
cal = _ensure_default_calendar(db, owner)
|
||||
|
||||
all_day = bool(args.get("all_day", False))
|
||||
try:
|
||||
dtstart, dtstart_is_utc = _parse_event_dt(dtstart_str)
|
||||
except ValueError as e:
|
||||
return {"error": f"Could not parse dtstart {dtstart_str!r}: {e}", "exit_code": 1}
|
||||
dtend_raw = args.get("dtend") or args.get("end") or args.get("end_time")
|
||||
if dtend_raw:
|
||||
try:
|
||||
dtend, dtend_is_utc = _parse_event_dt(dtend_raw)
|
||||
dtstart_is_utc = dtstart_is_utc or dtend_is_utc
|
||||
except ValueError as e:
|
||||
return {"error": f"Could not parse dtend {dtend_raw!r}: {e}", "exit_code": 1}
|
||||
else:
|
||||
# Support duration: "1h", "30m", "90min", "1hr30m"
|
||||
dur = (args.get("duration") or "").strip().lower()
|
||||
delta = None
|
||||
if dur:
|
||||
import re as _re_d
|
||||
h = _re_d.search(r'(\d+)\s*(?:h|hr|hours?)', dur)
|
||||
m = _re_d.search(r'(\d+)\s*(?:m|min|minutes?)', dur)
|
||||
secs = (int(h.group(1)) * 3600 if h else 0) + (int(m.group(1)) * 60 if m else 0)
|
||||
if secs > 0:
|
||||
delta = timedelta(seconds=secs)
|
||||
if delta is not None:
|
||||
dtend = dtstart + delta
|
||||
elif all_day:
|
||||
dtend = dtstart + timedelta(days=1)
|
||||
else:
|
||||
dtend = dtstart + timedelta(hours=1)
|
||||
|
||||
# Dedup: if a non-cancelled event with the same title + start time already
|
||||
# exists, return its UID instead of creating a fresh copy. Prevents the
|
||||
# email triage from multiplying events when several emails reference the
|
||||
# same meeting. Compare case-insensitively since LLM-extracted titles
|
||||
# can vary in capitalisation.
|
||||
from sqlalchemy import func as _func
|
||||
existing = (
|
||||
_event_query()
|
||||
.filter(
|
||||
CalendarEvent.dtstart == dtstart,
|
||||
CalendarEvent.status != "cancelled",
|
||||
_func.lower(CalendarEvent.summary) == summary.lower(),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing is not None:
|
||||
reminder_note_id = None
|
||||
reminder_skipped_reason = None
|
||||
minutes_before = _reminder_minutes(args)
|
||||
if minutes_before is not None:
|
||||
reminder_note_id, reminder_skipped_reason = _create_calendar_reminder(
|
||||
existing.summary or summary,
|
||||
existing.location or "",
|
||||
existing.dtstart,
|
||||
existing.all_day,
|
||||
minutes_before,
|
||||
bool(existing.is_utc),
|
||||
)
|
||||
if reminder_note_id:
|
||||
db.commit()
|
||||
reminder_text = ""
|
||||
if minutes_before is not None:
|
||||
reminder_text = (
|
||||
f"; reminder set {minutes_before} min before"
|
||||
if reminder_note_id
|
||||
else f"; reminder not set ({reminder_skipped_reason or 'reminder time already passed'})"
|
||||
)
|
||||
return {
|
||||
"response": (
|
||||
f"Event already exists: '{summary}' on {dtstart_str}"
|
||||
+ reminder_text
|
||||
),
|
||||
"uid": existing.uid,
|
||||
"reminder_note_id": reminder_note_id,
|
||||
"reminder_skipped_reason": reminder_skipped_reason,
|
||||
"duplicate": True,
|
||||
"exit_code": 0,
|
||||
}
|
||||
|
||||
# Optional tag/category and importance — friendly aliases.
|
||||
event_type = (args.get("event_type") or args.get("tag")
|
||||
or args.get("category") or args.get("type") or "") or None
|
||||
importance = args.get("importance") or "normal"
|
||||
minutes_before = _reminder_minutes(args)
|
||||
|
||||
uid = str(_uuid.uuid4())
|
||||
ev = CalendarEvent(
|
||||
uid=uid, calendar_id=cal.id, summary=summary,
|
||||
description=_event_description(args, minutes_before),
|
||||
location=args.get("location", "") or "",
|
||||
dtstart=dtstart, dtend=dtend, all_day=all_day,
|
||||
is_utc=dtstart_is_utc and not all_day,
|
||||
rrule=args.get("rrule", "") or "",
|
||||
event_type=event_type,
|
||||
importance=importance,
|
||||
caldav_sync_pending="create" if cal.source == "caldav" else None,
|
||||
)
|
||||
db.add(ev)
|
||||
reminder_note_id = None
|
||||
reminder_skipped_reason = None
|
||||
if minutes_before is not None:
|
||||
reminder_note_id, reminder_skipped_reason = _create_calendar_reminder(
|
||||
summary,
|
||||
args.get("location", "") or "",
|
||||
dtstart,
|
||||
all_day,
|
||||
minutes_before,
|
||||
dtstart_is_utc and not all_day,
|
||||
)
|
||||
db.commit()
|
||||
if cal.source == "caldav":
|
||||
await _push_caldav_event_after_commit(owner, uid, "create")
|
||||
tag_blurb = f" [{event_type}]" if event_type else ""
|
||||
if minutes_before is None:
|
||||
reminder_blurb = ""
|
||||
elif reminder_note_id:
|
||||
reminder_blurb = f" with reminder {minutes_before} min before"
|
||||
else:
|
||||
reminder_blurb = f" without reminder ({reminder_skipped_reason or 'reminder time already passed'})"
|
||||
# Return a clickable anchor so the agent can surface a link
|
||||
# that opens the calendar on that day. See the markdown
|
||||
# anchor convention ([Name](#event-<uid>)).
|
||||
return {
|
||||
"response": f"Created event [{summary}](#event-{uid}){tag_blurb} on {dtstart_str}{reminder_blurb}",
|
||||
"uid": uid,
|
||||
"anchor": f"[{summary}](#event-{uid})",
|
||||
"reminder_note_id": reminder_note_id,
|
||||
"reminder_skipped_reason": reminder_skipped_reason,
|
||||
"exit_code": 0,
|
||||
}
|
||||
|
||||
elif action == "update_event":
|
||||
uid = args.get("uid")
|
||||
if not uid:
|
||||
return {"error": "uid is required", "exit_code": 1}
|
||||
try:
|
||||
base_uid = _resolve_base_uid(uid)
|
||||
except ValueError as e:
|
||||
return {"error": str(e), "exit_code": 1}
|
||||
ev = _event_query().filter(CalendarEvent.uid == base_uid).first()
|
||||
if not ev:
|
||||
return {"error": f"Event {uid} not found", "exit_code": 1}
|
||||
if args.get("summary") is not None:
|
||||
ev.summary = args["summary"]
|
||||
if args.get("description") is not None:
|
||||
ev.description = args["description"]
|
||||
if args.get("location") is not None:
|
||||
ev.location = args["location"]
|
||||
if args.get("dtstart") is not None:
|
||||
# Anchor naive/natural-language input to the USER's timezone and
|
||||
# refresh is_utc, exactly like create_event. Parsing with the
|
||||
# raw server-local _parse_dt here (and never touching is_utc)
|
||||
# silently shifted an updated event by the user's UTC offset.
|
||||
_eff_all_day = (
|
||||
args["all_day"] if args.get("all_day") is not None else ev.all_day
|
||||
)
|
||||
ev.dtstart, _su = _parse_event_dt(args["dtstart"])
|
||||
ev.is_utc = bool(_su and not _eff_all_day)
|
||||
if args.get("dtend") is not None:
|
||||
ev.dtend, _eu = _parse_event_dt(args["dtend"])
|
||||
if args.get("all_day") is not None:
|
||||
ev.all_day = args["all_day"]
|
||||
# Tag/category + importance updates (any of these aliases).
|
||||
_tag = (args.get("event_type") or args.get("tag")
|
||||
or args.get("category") or args.get("type"))
|
||||
if _tag is not None:
|
||||
ev.event_type = _tag or None
|
||||
if args.get("importance") is not None:
|
||||
ev.importance = args["importance"]
|
||||
is_caldav = ev.calendar and ev.calendar.source == "caldav"
|
||||
if is_caldav:
|
||||
ev.caldav_sync_pending = "update"
|
||||
db.commit()
|
||||
if is_caldav:
|
||||
await _push_caldav_event_after_commit(owner, base_uid, "update")
|
||||
return {"response": f"Updated event {uid}", "exit_code": 0}
|
||||
|
||||
elif action == "delete_event":
|
||||
uid = args.get("uid")
|
||||
if not uid:
|
||||
return {"error": "uid is required", "exit_code": 1}
|
||||
try:
|
||||
base_uid = _resolve_base_uid(uid)
|
||||
except ValueError as e:
|
||||
return {"error": str(e), "exit_code": 1}
|
||||
ev = _event_query().filter(CalendarEvent.uid == base_uid).first()
|
||||
if not ev:
|
||||
return {"error": f"Event {uid} not found", "exit_code": 1}
|
||||
is_caldav = ev.calendar and ev.calendar.source == "caldav" and ev.remote_href
|
||||
if is_caldav:
|
||||
_record_caldav_delete_tombstone(db, ev, owner)
|
||||
db.delete(ev)
|
||||
db.commit()
|
||||
if is_caldav:
|
||||
await _push_caldav_event_after_commit(owner, base_uid, "delete")
|
||||
return {"response": f"Deleted event {uid}", "exit_code": 0}
|
||||
|
||||
else:
|
||||
return {
|
||||
"error": f"Unknown action: {action}. Use list_events, create_event, update_event, delete_event, list_calendars",
|
||||
"exit_code": 1,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"manage_calendar error: {e}")
|
||||
return {"error": str(e), "exit_code": 1}
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user