remove worked example, refactor calendar create_event seed
This commit is contained in:
1 parent
18f3d6fee5
commit
f21baa7ebd
8 files changed
+971
-72
No files matched your search
@@ -579,7 +579,10 @@ CLI, unit tests (24) + integration tests (2).
|
|||||||
at first fire) is written to the runtime store `data/skills/`, so the committed
|
at first fire) is written to the runtime store `data/skills/`, so the committed
|
||||||
seed never holds secrets, and a generated body with the same name replaces the
|
seed never holds secrets, and a generated body with the same name replaces the
|
||||||
seed. `calendar.next_event` (Nextcloud CalDAV, recurring events expanded
|
seed. `calendar.next_event` (Nextcloud CalDAV, recurring events expanded
|
||||||
server-side) is the reference seed; `simplex.next_message` (read via the
|
server-side) is the reference read seed and `calendar.create_event` (CalDAV
|
||||||
|
PUT; derives the title/date/time from the query, asks for missing fields and
|
||||||
|
resumes, resolves the target calendar against the configured default) is the
|
||||||
|
reference write seed; `simplex.next_message` (read via the
|
||||||
forwarding bridge) and `simplex.connect_link` (show/create the forwarding
|
forwarding bridge) and `simplex.connect_link` (show/create the forwarding
|
||||||
bot's contact link) are the messenger seeds; `tests/test_seed_skills.py` keeps
|
bot's contact link) are the messenger seeds; `tests/test_seed_skills.py` keeps
|
||||||
them honest.
|
them honest.
|
||||||
|
|||||||
-66
@@ -362,69 +362,3 @@ A generated skill is accepted only if:
|
|||||||
13. If it is a messaging skill, it uses a bridge service over HTTP (never a
|
13. If it is a messaging skill, it uses a bridge service over HTTP (never a
|
||||||
service's native protocol), resolves recipients with a SemIf sub-decision,
|
service's native protocol), resolves recipients with a SemIf sub-decision,
|
||||||
and sends only on explicit user intent.
|
and sends only on explicit user intent.
|
||||||
|
|
||||||
## Worked example
|
|
||||||
|
|
||||||
A skill that tracks a parcel over HTTP — for real. It reads the tracking number
|
|
||||||
out of the query (a per-request argument), uses the endpoint and token supplied
|
|
||||||
by the runner, performs the real lookup, and returns the service's real result:
|
|
||||||
|
|
||||||
```python
|
|
||||||
import json
|
|
||||||
import re
|
|
||||||
import urllib.error
|
|
||||||
import urllib.request
|
|
||||||
|
|
||||||
from semif_agent.skills import ActionResult
|
|
||||||
|
|
||||||
INTEGRATION = {
|
|
||||||
"service": "parcel_tracking",
|
|
||||||
"transport": "http",
|
|
||||||
"config_vars": ["tracking_api_url", "tracking_api_token"],
|
|
||||||
}
|
|
||||||
|
|
||||||
CONTRACT = {
|
|
||||||
"tracking_api_url": "Base URL of the parcel-tracking API, e.g. https://api.example.org.",
|
|
||||||
"tracking_api_token": "Bearer token for the tracking API, if it requires one.",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def act(ctx, request):
|
|
||||||
# A per-request argument: the tracking number comes from the query, not config.
|
|
||||||
match = re.search(r"\b[A-Z]{2}\d{9}[A-Z]{2}\b", request.text or "")
|
|
||||||
if match is None:
|
|
||||||
return ActionResult(
|
|
||||||
action_log="parcel_tracking: no tracking number found in the request",
|
|
||||||
new_state=request.text,
|
|
||||||
needs_input="What is the tracking number?",
|
|
||||||
)
|
|
||||||
tracking_id = match.group(0)
|
|
||||||
base = ctx.config["tracking_api_url"].rstrip("/")
|
|
||||||
token = ctx.config.get("tracking_api_token", "")
|
|
||||||
headers = {"Authorization": f"Bearer {token}"} if token else {}
|
|
||||||
url = f"{base}/track/{tracking_id}"
|
|
||||||
try:
|
|
||||||
req = urllib.request.Request(url, headers=headers)
|
|
||||||
with urllib.request.urlopen(req, timeout=15) as response:
|
|
||||||
payload = json.loads(response.read().decode("utf-8"))
|
|
||||||
except urllib.error.HTTPError as exc:
|
|
||||||
return ActionResult(
|
|
||||||
action_log=f"parcel_tracking: {tracking_id} lookup returned HTTP {exc.code}: {exc.reason}",
|
|
||||||
new_state=request.text,
|
|
||||||
)
|
|
||||||
except (urllib.error.URLError, TimeoutError, ValueError) as exc:
|
|
||||||
return ActionResult(
|
|
||||||
action_log=f"parcel_tracking: could not reach {url}: {exc}",
|
|
||||||
new_state=request.text,
|
|
||||||
)
|
|
||||||
status = payload.get("status", "unknown")
|
|
||||||
report = f"Parcel {tracking_id}: {status}"
|
|
||||||
if payload.get("location"):
|
|
||||||
report += f" ({payload['location']})"
|
|
||||||
return ActionResult(action_log=report, new_state=report)
|
|
||||||
```
|
|
||||||
|
|
||||||
Write skill bodies in this shape: read the request and derive the action's
|
|
||||||
arguments, declare `INTEGRATION` and `CONTRACT`, perform the real operation in
|
|
||||||
`act`, keep both stdlib-only, read operational data from `ctx.config`, SemIf any
|
|
||||||
candidate set, and return the real result in `action_log` / `new_state`.
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"nextcloud_url": "Base URL of the Nextcloud instance, e.g. https://cloud.example.com (no path, no trailing slash needed).",
|
||||||
|
"nextcloud_username": "The Nextcloud username whose calendars are used.",
|
||||||
|
"nextcloud_app_password": "A Nextcloud app password (Settings > Security > Devices & sessions) used for CalDAV Basic auth; the account password only works when two-factor auth is off.",
|
||||||
|
"nextcloud_default_calendar": "Name of the calendar new events go to by default, e.g. personal (a request may still name another calendar)."
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"description": "Create a calendar event from a request such as \"add Ashley's dance recital for 5pm Tuesday\"."
|
||||||
|
}
|
||||||
@@ -0,0 +1,626 @@
|
|||||||
|
"""Create a calendar event on the user's Nextcloud calendar from a request.
|
||||||
|
|
||||||
|
Real integration: CalDAV against the user's Nextcloud (SabreDAV). Connection
|
||||||
|
details and the default calendar come from `ctx.config` (`nextcloud_url`,
|
||||||
|
`nextcloud_username`, `nextcloud_app_password`, `nextcloud_default_calendar`).
|
||||||
|
|
||||||
|
`act` reads `request.text` and derives the event's per-request arguments — the
|
||||||
|
title, the start date/time, the duration, and (when named) the target calendar.
|
||||||
|
Missing arguments are asked one at a time and the answer is stashed in
|
||||||
|
`request.meta`; because `act` is single-phase and re-runs from the top after a
|
||||||
|
`needs_input` pause, the body re-parses the query each pass and never re-asks a
|
||||||
|
question it already has an answer for. Calendars are discovered over PROPFIND and
|
||||||
|
a named target is resolved with a SemIf sub-decision when more than one matches;
|
||||||
|
otherwise the configured default calendar is used. The event is created with a
|
||||||
|
real CalDAV PUT (iCalendar, UTC timestamps, `If-None-Match: *`) and the real
|
||||||
|
outcome is reported.
|
||||||
|
|
||||||
|
Parsing is deliberately bounded and stdlib-only (no text generation is available
|
||||||
|
to a skill body): when a required field cannot be derived confidently the body
|
||||||
|
asks rather than guessing.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import re
|
||||||
|
import urllib.error
|
||||||
|
import urllib.parse
|
||||||
|
import urllib.request
|
||||||
|
import uuid
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
from datetime import datetime, time, timedelta, timezone
|
||||||
|
|
||||||
|
from semif_agent.decisions import DecisionRequest, Option
|
||||||
|
from semif_agent.skills import ActionResult
|
||||||
|
|
||||||
|
INTEGRATION = {
|
||||||
|
"service": "nextcloud_calendar",
|
||||||
|
"transport": "caldav",
|
||||||
|
"config_vars": [
|
||||||
|
"nextcloud_url",
|
||||||
|
"nextcloud_username",
|
||||||
|
"nextcloud_app_password",
|
||||||
|
"nextcloud_default_calendar",
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
CONTRACT = {
|
||||||
|
"nextcloud_url": "Base URL of the Nextcloud instance, e.g. https://cloud.example.com (no path, no trailing slash needed).",
|
||||||
|
"nextcloud_username": "The Nextcloud username whose calendars are used.",
|
||||||
|
"nextcloud_app_password": "A Nextcloud app password (Settings > Security > Devices & sessions) used for CalDAV Basic auth; the account password only works when two-factor auth is off.",
|
||||||
|
"nextcloud_default_calendar": "Name of the calendar new events go to by default, e.g. personal (a request may still name another calendar).",
|
||||||
|
}
|
||||||
|
|
||||||
|
DAV = "DAV:"
|
||||||
|
CALDAV = "urn:ietf:params:xml:ns:caldav"
|
||||||
|
TIMEOUT_SECONDS = 30
|
||||||
|
DEFAULT_DURATION_MINUTES = 60
|
||||||
|
|
||||||
|
PROPFIND_BODY = (
|
||||||
|
'<?xml version="1.0" encoding="utf-8"?>'
|
||||||
|
'<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">'
|
||||||
|
"<d:prop><d:displayname/><d:resourcetype/></d:prop>"
|
||||||
|
"</d:propfind>"
|
||||||
|
).encode("utf-8")
|
||||||
|
|
||||||
|
_WEEKDAYS = {
|
||||||
|
"monday": 0,
|
||||||
|
"mon": 0,
|
||||||
|
"tuesday": 1,
|
||||||
|
"tue": 1,
|
||||||
|
"tues": 1,
|
||||||
|
"wednesday": 2,
|
||||||
|
"wed": 2,
|
||||||
|
"thursday": 3,
|
||||||
|
"thu": 3,
|
||||||
|
"thur": 3,
|
||||||
|
"thurs": 3,
|
||||||
|
"friday": 4,
|
||||||
|
"fri": 4,
|
||||||
|
"saturday": 5,
|
||||||
|
"sat": 5,
|
||||||
|
"sunday": 6,
|
||||||
|
"sun": 6,
|
||||||
|
}
|
||||||
|
_WEEKDAY_RE = re.compile(
|
||||||
|
r"\b(" + "|".join(sorted(_WEEKDAYS, key=len, reverse=True)) + r")\b", re.I
|
||||||
|
)
|
||||||
|
|
||||||
|
_MONTHS = {
|
||||||
|
"jan": 1,
|
||||||
|
"feb": 2,
|
||||||
|
"mar": 3,
|
||||||
|
"apr": 4,
|
||||||
|
"may": 5,
|
||||||
|
"jun": 6,
|
||||||
|
"jul": 7,
|
||||||
|
"aug": 8,
|
||||||
|
"sep": 9,
|
||||||
|
"oct": 10,
|
||||||
|
"nov": 11,
|
||||||
|
"dec": 12,
|
||||||
|
}
|
||||||
|
_MONTH_ALT = (
|
||||||
|
r"jan(?:uary)?|feb(?:ruary)?|mar(?:ch)?|apr(?:il)?|may|jun(?:e)?|jul(?:y)?"
|
||||||
|
r"|aug(?:ust)?|sep(?:t|tember)?|oct(?:ober)?|nov(?:ember)?|dec(?:ember)?"
|
||||||
|
)
|
||||||
|
|
||||||
|
_LEADING_NOISE = re.compile(
|
||||||
|
r"^\s*(please\s+)?(add|create|schedule|put|make|set\s+up|new|insert|book|a|an|the)\b[\s:,-]*",
|
||||||
|
re.I,
|
||||||
|
)
|
||||||
|
_TITLE_NOISE = [
|
||||||
|
re.compile(r"\b(to|on|in)\s+(my|the)\s+[A-Za-z0-9_'-]+\s+calendar\b", re.I),
|
||||||
|
re.compile(r"\b(to|on|in)\s+(my|the)\s+calendar\b", re.I),
|
||||||
|
re.compile(r"\b(new\s+|calendar\s+)*event\b", re.I),
|
||||||
|
re.compile(r"\b(called|titled|named)\b", re.I),
|
||||||
|
]
|
||||||
|
_TRAILING_NOISE = re.compile(
|
||||||
|
r"[\s,;:-]*\b(for|on|at|to|the|my|a|an|in|of)\b[\s,;:-]*$", re.I
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CalDavError(Exception):
|
||||||
|
"""A real failure talking to the calendar service."""
|
||||||
|
|
||||||
|
|
||||||
|
def _now():
|
||||||
|
"""Current local time (aware). A test seam; the body never freezes time."""
|
||||||
|
return datetime.now().astimezone()
|
||||||
|
|
||||||
|
|
||||||
|
# ---- transport ----
|
||||||
|
|
||||||
|
|
||||||
|
def _request(ctx, method, path, body=None, depth="0", content_type="application/xml; charset=utf-8", extra_headers=None):
|
||||||
|
url = str(ctx.config.get("nextcloud_url", "") or "").strip().rstrip("/")
|
||||||
|
username = str(ctx.config.get("nextcloud_username", "") or "").strip()
|
||||||
|
password = str(ctx.config.get("nextcloud_app_password", "") or "")
|
||||||
|
if not url:
|
||||||
|
raise CalDavError("nextcloud_url is not configured")
|
||||||
|
if not username or not password:
|
||||||
|
raise CalDavError("nextcloud_username and nextcloud_app_password are not configured")
|
||||||
|
token = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("ascii")
|
||||||
|
headers = {"Authorization": f"Basic {token}", "Depth": depth}
|
||||||
|
if body is not None:
|
||||||
|
headers["Content-Type"] = content_type
|
||||||
|
if extra_headers:
|
||||||
|
headers.update(extra_headers)
|
||||||
|
request = urllib.request.Request(
|
||||||
|
url + path, data=body, method=method, headers=headers
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(request, timeout=TIMEOUT_SECONDS) as response:
|
||||||
|
return response.status, response.read()
|
||||||
|
except urllib.error.HTTPError as exc:
|
||||||
|
detail = exc.read().decode("utf-8", "replace").strip()[:200]
|
||||||
|
raise CalDavError(f"{method} {path}: HTTP {exc.code} {detail}") from exc
|
||||||
|
except (urllib.error.URLError, OSError) as exc:
|
||||||
|
raise CalDavError(f"{method} {path}: {exc}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
# ---- CalDAV discovery ----
|
||||||
|
|
||||||
|
|
||||||
|
def _multistatus(body):
|
||||||
|
try:
|
||||||
|
root = ET.fromstring(body)
|
||||||
|
except ET.ParseError as exc:
|
||||||
|
raise CalDavError(f"unparseable CalDAV XML: {exc}") from exc
|
||||||
|
if root.tag != f"{{{DAV}}}multistatus":
|
||||||
|
raise CalDavError(f"unexpected CalDAV response root {root.tag!r}")
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
|
def _response_prop(response):
|
||||||
|
return response.find(f"{{{DAV}}}propstat/{{{DAV}}}prop")
|
||||||
|
|
||||||
|
|
||||||
|
def _discover_calendars(ctx):
|
||||||
|
username = str(ctx.config.get("nextcloud_username", "") or "").strip()
|
||||||
|
if not username:
|
||||||
|
raise CalDavError("nextcloud_username is not configured")
|
||||||
|
home = f"/remote.php/dav/calendars/{urllib.parse.quote(username)}/"
|
||||||
|
_status, body = _request(ctx, "PROPFIND", home, PROPFIND_BODY, depth="1")
|
||||||
|
calendars = []
|
||||||
|
for response in _multistatus(body).findall(f"{{{DAV}}}response"):
|
||||||
|
prop = _response_prop(response)
|
||||||
|
if prop is None:
|
||||||
|
continue
|
||||||
|
resourcetype = prop.find(f"{{{DAV}}}resourcetype")
|
||||||
|
if resourcetype is None or resourcetype.find(f"{{{CALDAV}}}calendar") is None:
|
||||||
|
continue
|
||||||
|
href = response.find(f"{{{DAV}}}href")
|
||||||
|
path = urllib.parse.urlparse((href.text if href is not None else "") or "").path
|
||||||
|
name = urllib.parse.unquote(path.rstrip("/").rsplit("/", 1)[-1])
|
||||||
|
if not name:
|
||||||
|
continue
|
||||||
|
display = prop.find(f"{{{DAV}}}displayname")
|
||||||
|
label = (display.text or "").strip() if display is not None else ""
|
||||||
|
calendars.append({"name": name, "label": label or name})
|
||||||
|
return calendars
|
||||||
|
|
||||||
|
|
||||||
|
def _calendar_path(ctx, calendar):
|
||||||
|
username = str(ctx.config.get("nextcloud_username", "") or "").strip()
|
||||||
|
return (
|
||||||
|
f"/remote.php/dav/calendars/{urllib.parse.quote(username)}/"
|
||||||
|
f"{urllib.parse.quote(calendar)}/"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ---- request -> arguments ----
|
||||||
|
|
||||||
|
|
||||||
|
def _remove_spans(text, spans):
|
||||||
|
for start, end in sorted(spans, reverse=True):
|
||||||
|
text = text[:start] + " " + text[end:]
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def _clean_title(text):
|
||||||
|
text = re.sub(r"\s{2,}", " ", text).strip()
|
||||||
|
previous = None
|
||||||
|
while previous != text:
|
||||||
|
previous = text
|
||||||
|
text = _LEADING_NOISE.sub("", text)
|
||||||
|
for pattern in _TITLE_NOISE:
|
||||||
|
text = pattern.sub(" ", text)
|
||||||
|
text = _TRAILING_NOISE.sub("", text)
|
||||||
|
text = re.sub(r"\s{2,}", " ", text).strip()
|
||||||
|
return text.strip(" \t,;:-.")
|
||||||
|
|
||||||
|
|
||||||
|
def _extract_title(text, spans):
|
||||||
|
return _clean_title(_remove_spans(text or "", spans))
|
||||||
|
|
||||||
|
|
||||||
|
def _month_number(token):
|
||||||
|
return _MONTHS.get(token.lower()[:3])
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_month_day(month, day, year, now):
|
||||||
|
if not (month and 1 <= day <= 31):
|
||||||
|
return None
|
||||||
|
given_year = year is not None
|
||||||
|
year = year or now.year
|
||||||
|
try:
|
||||||
|
candidate = datetime(year, month, day).date()
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
if not given_year and candidate < now.date():
|
||||||
|
try:
|
||||||
|
candidate = datetime(year + 1, month, day).date()
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
return candidate
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_when(text, now):
|
||||||
|
"""Best-effort extraction of date/time/duration from free text.
|
||||||
|
|
||||||
|
Returns a dict with `date` (datetime.date | None), `time` (datetime.time |
|
||||||
|
None), `all_day` (bool), `duration_minutes` (int | None), `from_weekday`
|
||||||
|
(bool) and `spans` (matched ranges, so the title can be cleaned).
|
||||||
|
"""
|
||||||
|
text = text or ""
|
||||||
|
result = {
|
||||||
|
"date": None,
|
||||||
|
"time": None,
|
||||||
|
"all_day": False,
|
||||||
|
"duration_minutes": None,
|
||||||
|
"from_weekday": False,
|
||||||
|
}
|
||||||
|
spans = []
|
||||||
|
|
||||||
|
match = re.search(r"\ball[\s-]?day\b|\bwhole\s+day\b", text, re.I)
|
||||||
|
if match:
|
||||||
|
result["all_day"] = True
|
||||||
|
spans.append(match.span())
|
||||||
|
|
||||||
|
match = re.search(r"\bfor\s+(\d+(?:\.\d+)?)\s*(hours?|hrs?|h)\b", text, re.I)
|
||||||
|
if match:
|
||||||
|
result["duration_minutes"] = int(round(float(match.group(1)) * 60))
|
||||||
|
spans.append(match.span())
|
||||||
|
else:
|
||||||
|
match = re.search(r"\bfor\s+(\d+)\s*(minutes?|mins?|m)\b", text, re.I)
|
||||||
|
if match:
|
||||||
|
result["duration_minutes"] = int(match.group(1))
|
||||||
|
spans.append(match.span())
|
||||||
|
else:
|
||||||
|
for phrase, minutes in (
|
||||||
|
(r"\bfor\s+half\s+an?\s+hour\b", 30),
|
||||||
|
(r"\bfor\s+an?\s+hour\b", 60),
|
||||||
|
(r"\bfor\s+a\s+couple\s+of\s+hours\b", 120),
|
||||||
|
):
|
||||||
|
match = re.search(phrase, text, re.I)
|
||||||
|
if match:
|
||||||
|
result["duration_minutes"] = minutes
|
||||||
|
spans.append(match.span())
|
||||||
|
break
|
||||||
|
|
||||||
|
date = None
|
||||||
|
match = re.search(r"\b(\d{4})-(\d{1,2})-(\d{1,2})\b", text)
|
||||||
|
if match:
|
||||||
|
try:
|
||||||
|
date = datetime(
|
||||||
|
int(match.group(1)), int(match.group(2)), int(match.group(3))
|
||||||
|
).date()
|
||||||
|
spans.append(match.span())
|
||||||
|
except ValueError:
|
||||||
|
date = None
|
||||||
|
|
||||||
|
if date is None:
|
||||||
|
match = re.search(
|
||||||
|
r"\b(" + _MONTH_ALT + r")\s+(\d{1,2})(?:st|nd|rd|th)?(?:,?\s*(\d{4}))?\b",
|
||||||
|
text,
|
||||||
|
re.I,
|
||||||
|
)
|
||||||
|
if match:
|
||||||
|
month = _month_number(match.group(1))
|
||||||
|
year = int(match.group(3)) if match.group(3) else None
|
||||||
|
date = _resolve_month_day(month, int(match.group(2)), year, now)
|
||||||
|
if date is not None:
|
||||||
|
spans.append(match.span())
|
||||||
|
|
||||||
|
if date is None:
|
||||||
|
match = re.search(
|
||||||
|
r"\b(\d{1,2})(?:st|nd|rd|th)?\s+(?:of\s+)?(" + _MONTH_ALT + r")(?:,?\s*(\d{4}))?\b",
|
||||||
|
text,
|
||||||
|
re.I,
|
||||||
|
)
|
||||||
|
if match:
|
||||||
|
month = _month_number(match.group(2))
|
||||||
|
year = int(match.group(3)) if match.group(3) else None
|
||||||
|
date = _resolve_month_day(month, int(match.group(1)), year, now)
|
||||||
|
if date is not None:
|
||||||
|
spans.append(match.span())
|
||||||
|
|
||||||
|
if date is None:
|
||||||
|
match = re.search(r"\b(\d{1,2})/(\d{1,2})(?:/(\d{2,4}))?\b", text)
|
||||||
|
if match:
|
||||||
|
first, second = int(match.group(1)), int(match.group(2))
|
||||||
|
month, day = (first, second) if first <= 12 else (second, first)
|
||||||
|
year = int(match.group(3)) if match.group(3) else None
|
||||||
|
if year is not None and year < 100:
|
||||||
|
year += 2000
|
||||||
|
date = _resolve_month_day(month, day, year, now)
|
||||||
|
if date is not None:
|
||||||
|
spans.append(match.span())
|
||||||
|
|
||||||
|
if date is None:
|
||||||
|
match = re.search(
|
||||||
|
r"\b(today|tonight|tomorrow|tmrw|tomorow|next\s+week)\b", text, re.I
|
||||||
|
)
|
||||||
|
if match:
|
||||||
|
word = re.sub(r"\s+", " ", match.group(1).lower())
|
||||||
|
if word in ("today", "tonight"):
|
||||||
|
date = now.date()
|
||||||
|
elif word in ("tomorrow", "tmrw", "tomorow"):
|
||||||
|
date = now.date() + timedelta(days=1)
|
||||||
|
else:
|
||||||
|
date = now.date() + timedelta(days=7)
|
||||||
|
spans.append(match.span())
|
||||||
|
|
||||||
|
if date is None:
|
||||||
|
match = _WEEKDAY_RE.search(text)
|
||||||
|
if match:
|
||||||
|
target = _WEEKDAYS[match.group(1).lower()]
|
||||||
|
date = now.date() + timedelta(days=(target - now.date().weekday()) % 7)
|
||||||
|
prefix = text[max(0, match.start() - 6):match.start()]
|
||||||
|
if re.search(r"\bnext\s*$", prefix, re.I):
|
||||||
|
date += timedelta(days=7)
|
||||||
|
spans.append((match.start() - len("next "), match.end()))
|
||||||
|
else:
|
||||||
|
spans.append(match.span())
|
||||||
|
result["from_weekday"] = True
|
||||||
|
|
||||||
|
parsed_time = None
|
||||||
|
match = re.search(r"\bnoon\b", text, re.I)
|
||||||
|
if match:
|
||||||
|
parsed_time = time(12, 0)
|
||||||
|
spans.append(match.span())
|
||||||
|
elif re.search(r"\bmidnight\b", text, re.I):
|
||||||
|
parsed_time = time(0, 0)
|
||||||
|
spans.append(re.search(r"\bmidnight\b", text, re.I).span())
|
||||||
|
else:
|
||||||
|
match = re.search(r"\b(\d{1,2})(?::(\d{2}))?\s*([ap])\.?m\.?\b", text, re.I)
|
||||||
|
if match:
|
||||||
|
hour = int(match.group(1)) % 12
|
||||||
|
minute = int(match.group(2) or 0)
|
||||||
|
if match.group(3).lower() == "p":
|
||||||
|
hour += 12
|
||||||
|
if hour < 24 and minute < 60:
|
||||||
|
parsed_time = time(hour, minute)
|
||||||
|
spans.append(match.span())
|
||||||
|
else:
|
||||||
|
match = re.search(r"\b(\d{1,2}):(\d{2})\b", text)
|
||||||
|
if match:
|
||||||
|
hour, minute = int(match.group(1)), int(match.group(2))
|
||||||
|
if hour < 24 and minute < 60:
|
||||||
|
parsed_time = time(hour, minute)
|
||||||
|
spans.append(match.span())
|
||||||
|
else:
|
||||||
|
match = re.search(r"\bat\s+(\d{1,2})\b", text, re.I)
|
||||||
|
if match and 13 <= int(match.group(1)) < 24:
|
||||||
|
parsed_time = time(int(match.group(1)), 0)
|
||||||
|
spans.append(match.span())
|
||||||
|
|
||||||
|
result["date"] = date
|
||||||
|
result["time"] = parsed_time
|
||||||
|
result["spans"] = spans
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# ---- calendar resolution ----
|
||||||
|
|
||||||
|
|
||||||
|
def _mentioned_calendars(calendars, text):
|
||||||
|
low = (text or "").lower()
|
||||||
|
found = []
|
||||||
|
for calendar in calendars:
|
||||||
|
for token in {calendar["name"].lower(), calendar["label"].lower()}:
|
||||||
|
if len(token) < 3:
|
||||||
|
continue
|
||||||
|
pattern = (
|
||||||
|
rf"\b{re.escape(token)}\b\s+calendar\b"
|
||||||
|
rf"|\bcalendar\b\s+\b{re.escape(token)}\b"
|
||||||
|
)
|
||||||
|
if re.search(pattern, low):
|
||||||
|
found.append(calendar)
|
||||||
|
break
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_calendar(ctx, request, calendars, decisions):
|
||||||
|
default_name = str(ctx.config.get("nextcloud_default_calendar", "") or "").strip().lower()
|
||||||
|
default = None
|
||||||
|
for calendar in calendars:
|
||||||
|
if default_name and default_name in (calendar["name"].lower(), calendar["label"].lower()):
|
||||||
|
default = calendar
|
||||||
|
break
|
||||||
|
|
||||||
|
mentioned = _mentioned_calendars(calendars, request.text)
|
||||||
|
if len(mentioned) == 1:
|
||||||
|
return mentioned[0]["name"]
|
||||||
|
if len(mentioned) > 1:
|
||||||
|
return _choose_calendar(ctx, request, mentioned, default, decisions)
|
||||||
|
if default is not None:
|
||||||
|
return default["name"]
|
||||||
|
if len(calendars) == 1:
|
||||||
|
return calendars[0]["name"]
|
||||||
|
return _choose_calendar(ctx, request, calendars, default, decisions)
|
||||||
|
|
||||||
|
|
||||||
|
def _choose_calendar(ctx, request, candidates, default, decisions):
|
||||||
|
options = []
|
||||||
|
for calendar in candidates:
|
||||||
|
label = calendar["label"]
|
||||||
|
if default is not None and calendar["name"] == default["name"]:
|
||||||
|
label = f"{label} (default)"
|
||||||
|
options.append(Option(calendar["name"], label))
|
||||||
|
decision = DecisionRequest(
|
||||||
|
state=request.text,
|
||||||
|
question="Which calendar should the event go on?",
|
||||||
|
options=options,
|
||||||
|
)
|
||||||
|
result = ctx.engine.call(decision)
|
||||||
|
decisions.append((decision, result))
|
||||||
|
return result.selected
|
||||||
|
|
||||||
|
|
||||||
|
# ---- iCalendar ----
|
||||||
|
|
||||||
|
|
||||||
|
def _escape_ics(value):
|
||||||
|
return (
|
||||||
|
str(value)
|
||||||
|
.replace("\\", "\\\\")
|
||||||
|
.replace(";", "\\;")
|
||||||
|
.replace(",", "\\,")
|
||||||
|
.replace("\r\n", "\\n")
|
||||||
|
.replace("\n", "\\n")
|
||||||
|
.replace("\r", "\\n")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _utc_stamp(moment):
|
||||||
|
return moment.astimezone(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
||||||
|
|
||||||
|
|
||||||
|
def _build_ics(uid, summary, start, end, all_day, stamp, description=""):
|
||||||
|
lines = [
|
||||||
|
"BEGIN:VCALENDAR",
|
||||||
|
"VERSION:2.0",
|
||||||
|
"PRODID:-//semif-agent//calendar.create_event//EN",
|
||||||
|
"CALSCALE:GREGORIAN",
|
||||||
|
"BEGIN:VEVENT",
|
||||||
|
f"UID:{uid}",
|
||||||
|
f"DTSTAMP:{stamp}",
|
||||||
|
]
|
||||||
|
if all_day:
|
||||||
|
lines.append(f"DTSTART;VALUE=DATE:{start.strftime('%Y%m%d')}")
|
||||||
|
lines.append(f"DTEND;VALUE=DATE:{end.strftime('%Y%m%d')}")
|
||||||
|
else:
|
||||||
|
lines.append(f"DTSTART:{_utc_stamp(start)}")
|
||||||
|
lines.append(f"DTEND:{_utc_stamp(end)}")
|
||||||
|
lines.append(f"SUMMARY:{_escape_ics(summary)}")
|
||||||
|
if description:
|
||||||
|
lines.append(f"DESCRIPTION:{_escape_ics(description)}")
|
||||||
|
lines += ["END:VEVENT", "END:VCALENDAR", ""]
|
||||||
|
return "\r\n".join(lines).encode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
# ---- skill phases ----
|
||||||
|
|
||||||
|
|
||||||
|
def _ask(request, field, question):
|
||||||
|
request.meta["create_event_awaiting"] = field
|
||||||
|
return ActionResult(
|
||||||
|
action_log=f"calendar.create_event: waiting for {field}",
|
||||||
|
new_state=request.text,
|
||||||
|
needs_input=question,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def act(ctx, request):
|
||||||
|
answers = request.meta.setdefault("create_event_answers", {})
|
||||||
|
awaiting = request.meta.get("create_event_awaiting")
|
||||||
|
if request.user_input is not None and awaiting:
|
||||||
|
if answers.get(awaiting) != request.user_input.strip():
|
||||||
|
answers[awaiting] = request.user_input.strip()
|
||||||
|
request.meta.pop("create_event_awaiting", None)
|
||||||
|
|
||||||
|
now = _now()
|
||||||
|
parsed = _parse_when(request.text, now)
|
||||||
|
title = _extract_title(request.text, parsed["spans"])
|
||||||
|
|
||||||
|
if answers.get("title"):
|
||||||
|
title = answers["title"].strip()
|
||||||
|
for key in ("date", "time"):
|
||||||
|
if answers.get(key):
|
||||||
|
extra = _parse_when(answers[key], now)
|
||||||
|
if extra["date"] is not None:
|
||||||
|
parsed["date"] = extra["date"]
|
||||||
|
parsed["from_weekday"] = extra["from_weekday"]
|
||||||
|
if extra["time"] is not None:
|
||||||
|
parsed["time"] = extra["time"]
|
||||||
|
if extra["all_day"]:
|
||||||
|
parsed["all_day"] = True
|
||||||
|
if extra["duration_minutes"]:
|
||||||
|
parsed["duration_minutes"] = extra["duration_minutes"]
|
||||||
|
|
||||||
|
if not title:
|
||||||
|
return _ask(request, "title", "What should I call the event?")
|
||||||
|
if parsed["date"] is None:
|
||||||
|
return _ask(request, "date", f"What day should I schedule {title!r}?")
|
||||||
|
if not parsed["all_day"] and parsed["time"] is None:
|
||||||
|
return _ask(request, "time", f"What time should {title!r} start? (or say 'all day')")
|
||||||
|
|
||||||
|
local_tz = now.tzinfo or timezone.utc
|
||||||
|
all_day = bool(parsed["all_day"])
|
||||||
|
if all_day:
|
||||||
|
start_date = parsed["date"]
|
||||||
|
end_date = start_date + timedelta(days=1)
|
||||||
|
start = datetime.combine(start_date, time(0, 0), tzinfo=local_tz)
|
||||||
|
end = datetime.combine(end_date, time(0, 0), tzinfo=local_tz)
|
||||||
|
else:
|
||||||
|
start = datetime.combine(parsed["date"], parsed["time"], tzinfo=local_tz)
|
||||||
|
if start <= now and parsed.get("from_weekday"):
|
||||||
|
start += timedelta(days=7)
|
||||||
|
duration = timedelta(minutes=parsed["duration_minutes"] or DEFAULT_DURATION_MINUTES)
|
||||||
|
end = start + duration
|
||||||
|
|
||||||
|
try:
|
||||||
|
calendars = _discover_calendars(ctx)
|
||||||
|
except CalDavError as exc:
|
||||||
|
return ActionResult(
|
||||||
|
action_log=f"calendar.create_event: calendar discovery failed: {exc}",
|
||||||
|
new_state=request.text,
|
||||||
|
)
|
||||||
|
if not calendars:
|
||||||
|
return ActionResult(
|
||||||
|
action_log="calendar.create_event: no calendars found on the account",
|
||||||
|
new_state=request.text,
|
||||||
|
)
|
||||||
|
|
||||||
|
decisions = []
|
||||||
|
calendar = _resolve_calendar(ctx, request, calendars, decisions)
|
||||||
|
|
||||||
|
uid = f"{uuid.uuid4().hex}@semif-agent"
|
||||||
|
body = _build_ics(uid, title, start, end, all_day, _utc_stamp(now))
|
||||||
|
path = _calendar_path(ctx, calendar) + urllib.parse.quote(uid, safe="") + ".ics"
|
||||||
|
try:
|
||||||
|
_request(
|
||||||
|
ctx,
|
||||||
|
"PUT",
|
||||||
|
path,
|
||||||
|
body=body,
|
||||||
|
depth="0",
|
||||||
|
content_type="text/calendar; charset=utf-8",
|
||||||
|
extra_headers={"If-None-Match": "*"},
|
||||||
|
)
|
||||||
|
except CalDavError as exc:
|
||||||
|
return ActionResult(
|
||||||
|
action_log=(
|
||||||
|
f"calendar.create_event: could not create {title!r} on "
|
||||||
|
f"{calendar!r}: {exc}"
|
||||||
|
),
|
||||||
|
new_state=request.text,
|
||||||
|
decisions=decisions,
|
||||||
|
)
|
||||||
|
|
||||||
|
if all_day:
|
||||||
|
when_text = f"{start_date.isoformat()} (all day)"
|
||||||
|
else:
|
||||||
|
when_text = (
|
||||||
|
f"{start.strftime('%Y-%m-%d %H:%M %Z')}–"
|
||||||
|
f"{end.strftime('%H:%M %Z')}"
|
||||||
|
)
|
||||||
|
report = f"Created {title!r} on calendar {calendar!r}: {when_text} (UID {uid})."
|
||||||
|
return ActionResult(
|
||||||
|
action_log=f"calendar.create_event: {report}",
|
||||||
|
new_state=report,
|
||||||
|
decisions=decisions,
|
||||||
|
)
|
||||||
@@ -0,0 +1,319 @@
|
|||||||
|
"""Hermetic mechanics test for calendar.create_event.
|
||||||
|
|
||||||
|
Run from this folder: `python skill.test.py`. No external network: a loopback
|
||||||
|
http.server plays a Nextcloud CalDAV endpoint (PROPFIND discovery + PUT create).
|
||||||
|
This proves the body derives the event title/date/time from the request, asks for
|
||||||
|
a missing field and resumes, resolves the target calendar (default or named, with
|
||||||
|
a SemIf sub-decision when ambiguous), writes a real iCalendar PUT, and fails
|
||||||
|
honestly when the server errors. It does NOT prove the live integration — only a
|
||||||
|
real run against the user's Nextcloud does.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import urllib.parse
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
|
||||||
|
from semif_agent.decisions import DecisionResult, Request
|
||||||
|
from semif_agent.skills import ActionContext
|
||||||
|
|
||||||
|
import skill
|
||||||
|
|
||||||
|
FIXED_NOW = datetime(2026, 10, 7, 10, 0, tzinfo=timezone.utc)
|
||||||
|
skill._now = lambda: FIXED_NOW
|
||||||
|
|
||||||
|
|
||||||
|
def next_weekday(now, weekday):
|
||||||
|
return now.date() + timedelta(days=(weekday - now.date().weekday()) % 7)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeEngine:
|
||||||
|
"""Test-only engine: picks the requested option id, else the first."""
|
||||||
|
|
||||||
|
def __init__(self, pick=None):
|
||||||
|
self.pick = pick
|
||||||
|
self.decisions = []
|
||||||
|
|
||||||
|
def call(self, decision):
|
||||||
|
self.decisions.append(decision)
|
||||||
|
option_ids = [option.id for option in decision.options]
|
||||||
|
chosen = self.pick if self.pick in option_ids else option_ids[0]
|
||||||
|
probabilities = [
|
||||||
|
1.0 if option_id == chosen else 0.0 for option_id in option_ids
|
||||||
|
]
|
||||||
|
return DecisionResult(
|
||||||
|
request=decision, option_ids=option_ids, probabilities=probabilities
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CalDavHandler(BaseHTTPRequestHandler):
|
||||||
|
calendars = []
|
||||||
|
put_status = 201
|
||||||
|
requests = []
|
||||||
|
puts = []
|
||||||
|
|
||||||
|
def _read_body(self):
|
||||||
|
length = int(self.headers.get("Content-Length") or 0)
|
||||||
|
return self.rfile.read(length) if length else b""
|
||||||
|
|
||||||
|
def _send(self, payload, status, content_type):
|
||||||
|
body = payload if isinstance(payload, bytes) else payload.encode("utf-8")
|
||||||
|
self.send_response(status)
|
||||||
|
self.send_header("Content-Type", content_type)
|
||||||
|
self.send_header("Content-Length", str(len(body)))
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(body)
|
||||||
|
|
||||||
|
def do_PROPFIND(self):
|
||||||
|
self._read_body()
|
||||||
|
self.requests.append(("PROPFIND", self.path))
|
||||||
|
responses = []
|
||||||
|
for calendar in self.calendars:
|
||||||
|
responses.append(
|
||||||
|
f'<d:response><d:href>/remote.php/dav/calendars/alice/'
|
||||||
|
f'{calendar["name"]}/</d:href>'
|
||||||
|
f"<d:propstat><d:prop>"
|
||||||
|
f'<d:displayname>{calendar["label"]}</d:displayname>'
|
||||||
|
f"<d:resourcetype><d:collection/><c:calendar/></d:resourcetype>"
|
||||||
|
f"</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat>"
|
||||||
|
f"</d:response>"
|
||||||
|
)
|
||||||
|
body = (
|
||||||
|
'<?xml version="1.0" encoding="utf-8"?>'
|
||||||
|
'<d:multistatus xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">'
|
||||||
|
+ "".join(responses)
|
||||||
|
+ "</d:multistatus>"
|
||||||
|
)
|
||||||
|
self._send(body, 207, "application/xml; charset=utf-8")
|
||||||
|
|
||||||
|
def do_PUT(self):
|
||||||
|
body = self._read_body()
|
||||||
|
self.puts.append(
|
||||||
|
{"path": self.path, "body": body, "headers": dict(self.headers)}
|
||||||
|
)
|
||||||
|
self._send(b"", self.put_status, "text/plain")
|
||||||
|
|
||||||
|
def log_message(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def start_server(calendars=None, put_status=201):
|
||||||
|
handler = type(
|
||||||
|
"Handler",
|
||||||
|
(CalDavHandler,),
|
||||||
|
{
|
||||||
|
"calendars": calendars
|
||||||
|
if calendars is not None
|
||||||
|
else [
|
||||||
|
{"name": "personal", "label": "Personal"},
|
||||||
|
{"name": "work", "label": "Work"},
|
||||||
|
],
|
||||||
|
"put_status": put_status,
|
||||||
|
"requests": [],
|
||||||
|
"puts": [],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
server = ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||||
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||||
|
return server, handler, f"http://127.0.0.1:{server.server_address[1]}"
|
||||||
|
|
||||||
|
|
||||||
|
def config(url, default="personal", token=""):
|
||||||
|
return {
|
||||||
|
"nextcloud_url": url,
|
||||||
|
"nextcloud_username": "alice",
|
||||||
|
"nextcloud_app_password": "secret",
|
||||||
|
"nextcloud_default_calendar": default,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def run(handler, url, text, default="personal", engine=None):
|
||||||
|
engine = engine or FakeEngine()
|
||||||
|
ctx = ActionContext(engine=engine, config=config(url, default))
|
||||||
|
request = Request(text)
|
||||||
|
action = skill.act(ctx, request)
|
||||||
|
return action, request, engine
|
||||||
|
|
||||||
|
|
||||||
|
def test_example_creates_event_on_default_calendar():
|
||||||
|
server, handler, url = start_server()
|
||||||
|
try:
|
||||||
|
action, _request, engine = run(
|
||||||
|
handler, url, "add Ashley's dance recital to the calendar for 5pm Tuesday"
|
||||||
|
)
|
||||||
|
assert action.needs_input is None, action.needs_input
|
||||||
|
assert engine.decisions == [], "the default calendar needs no SemIf decision"
|
||||||
|
assert len(handler.puts) == 1, handler.puts
|
||||||
|
put = handler.puts[0]
|
||||||
|
assert put["path"].startswith("/remote.php/dav/calendars/alice/personal/"), put
|
||||||
|
assert put["path"].endswith(".ics"), put
|
||||||
|
assert put["headers"].get("If-None-Match") == "*"
|
||||||
|
body = put["body"].decode("utf-8")
|
||||||
|
assert "SUMMARY:Ashley's dance recital" in body, body
|
||||||
|
tuesday = next_weekday(FIXED_NOW, 1)
|
||||||
|
assert f"DTSTART:{tuesday.strftime('%Y%m%d')}T170000Z" in body, body
|
||||||
|
assert f"DTEND:{tuesday.strftime('%Y%m%d')}T180000Z" in body, body
|
||||||
|
assert "Ashley's dance recital" in action.action_log, action.action_log
|
||||||
|
print(action.action_log)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_missing_time_asks_then_creates():
|
||||||
|
server, handler, url = start_server()
|
||||||
|
try:
|
||||||
|
action, request, engine = run(
|
||||||
|
handler, url, "add dentist appointment on 2026-11-10"
|
||||||
|
)
|
||||||
|
assert action.needs_input, "a missing start time must be asked"
|
||||||
|
assert "time" in action.needs_input.lower(), action.needs_input
|
||||||
|
assert handler.puts == []
|
||||||
|
request.user_input = "2pm"
|
||||||
|
ctx = ActionContext(engine=engine, config=config(url))
|
||||||
|
action = skill.act(ctx, request)
|
||||||
|
assert action.needs_input is None, action.needs_input
|
||||||
|
assert len(handler.puts) == 1, handler.puts
|
||||||
|
body = handler.puts[0]["body"].decode("utf-8")
|
||||||
|
assert "SUMMARY:dentist appointment" in body, body
|
||||||
|
assert "DTSTART:20261110T140000Z" in body, body
|
||||||
|
print(action.action_log)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_missing_day_asks_then_creates():
|
||||||
|
server, handler, url = start_server()
|
||||||
|
try:
|
||||||
|
action, request, engine = run(handler, url, "add dentist appointment at 2pm")
|
||||||
|
assert action.needs_input, "a missing day must be asked"
|
||||||
|
assert "day" in action.needs_input.lower(), action.needs_input
|
||||||
|
request.user_input = "2026-11-10"
|
||||||
|
ctx = ActionContext(engine=engine, config=config(url))
|
||||||
|
action = skill.act(ctx, request)
|
||||||
|
assert action.needs_input is None, action.needs_input
|
||||||
|
body = handler.puts[0]["body"].decode("utf-8")
|
||||||
|
assert "DTSTART:20261110T140000Z" in body, body
|
||||||
|
print(action.action_log)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_missing_title_asks():
|
||||||
|
server, handler, url = start_server()
|
||||||
|
try:
|
||||||
|
action, _request, _engine = run(handler, url, "schedule on 2026-11-10 at 2pm")
|
||||||
|
assert action.needs_input, "a missing title must be asked"
|
||||||
|
assert "call the event" in action.needs_input.lower(), action.needs_input
|
||||||
|
assert handler.puts == []
|
||||||
|
print(action.needs_input)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_named_calendar_is_used():
|
||||||
|
server, handler, url = start_server()
|
||||||
|
try:
|
||||||
|
action, _request, engine = run(
|
||||||
|
handler, url, "add team sync to my work calendar on 2026-11-10 at 9am"
|
||||||
|
)
|
||||||
|
assert action.needs_input is None, action.needs_input
|
||||||
|
assert engine.decisions == [], "a uniquely named calendar needs no decision"
|
||||||
|
assert handler.puts[0]["path"].startswith(
|
||||||
|
"/remote.php/dav/calendars/alice/work/"
|
||||||
|
), handler.puts[0]
|
||||||
|
body = handler.puts[0]["body"].decode("utf-8")
|
||||||
|
assert "SUMMARY:team sync" in body, body
|
||||||
|
print(action.action_log)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_default_uses_semif_decision():
|
||||||
|
server, handler, url = start_server()
|
||||||
|
try:
|
||||||
|
engine = FakeEngine(pick="work")
|
||||||
|
action, _request, engine = run(
|
||||||
|
handler,
|
||||||
|
url,
|
||||||
|
"add standup on 2026-11-10 at 9am",
|
||||||
|
default="missing",
|
||||||
|
engine=engine,
|
||||||
|
)
|
||||||
|
assert action.needs_input is None, action.needs_input
|
||||||
|
assert len(engine.decisions) == 1, engine.decisions
|
||||||
|
assert action.decisions and len(action.decisions) == 1, action.decisions
|
||||||
|
assert handler.puts[0]["path"].startswith(
|
||||||
|
"/remote.php/dav/calendars/alice/work/"
|
||||||
|
), handler.puts[0]
|
||||||
|
print(action.action_log)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_all_day_event():
|
||||||
|
server, handler, url = start_server()
|
||||||
|
try:
|
||||||
|
action, _request, _engine = run(
|
||||||
|
handler, url, "add conference all day on 2026-11-10"
|
||||||
|
)
|
||||||
|
assert action.needs_input is None, action.needs_input
|
||||||
|
body = handler.puts[0]["body"].decode("utf-8")
|
||||||
|
assert "DTSTART;VALUE=DATE:20261110" in body, body
|
||||||
|
assert "DTEND;VALUE=DATE:20261111" in body, body
|
||||||
|
assert "SUMMARY:conference" in body, body
|
||||||
|
print(action.action_log)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_server_error_is_reported_honestly():
|
||||||
|
server, handler, url = start_server(put_status=500)
|
||||||
|
try:
|
||||||
|
action, request, _engine = run(
|
||||||
|
handler, url, "add standup on 2026-11-10 at 9am"
|
||||||
|
)
|
||||||
|
assert "500" in action.action_log, action.action_log
|
||||||
|
assert action.new_state == request.text, "a failed create must not fake a result"
|
||||||
|
print(action.action_log)
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_unreachable_server_fails_honestly():
|
||||||
|
action, request, _engine = run(
|
||||||
|
None, "http://127.0.0.1:1", "add standup on 2026-11-10 at 9am"
|
||||||
|
)
|
||||||
|
assert action.action_log.startswith("calendar.create_event:"), action.action_log
|
||||||
|
assert action.new_state == request.text
|
||||||
|
print(action.action_log)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
test_example_creates_event_on_default_calendar()
|
||||||
|
test_missing_time_asks_then_creates()
|
||||||
|
test_missing_day_asks_then_creates()
|
||||||
|
test_missing_title_asks()
|
||||||
|
test_named_calendar_is_used()
|
||||||
|
test_unknown_default_uses_semif_decision()
|
||||||
|
test_all_day_event()
|
||||||
|
test_server_error_is_reported_honestly()
|
||||||
|
test_unreachable_server_fails_honestly()
|
||||||
|
print("ok")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except AssertionError as exc:
|
||||||
|
print(f"FAILED: {exc}")
|
||||||
|
sys.exit(1)
|
||||||
@@ -31,6 +31,7 @@ def seed_id(seed: Path) -> str:
|
|||||||
def test_expected_seeds_exist():
|
def test_expected_seeds_exist():
|
||||||
ids = {seed_id(seed) for seed in SEED_DIRS}
|
ids = {seed_id(seed) for seed in SEED_DIRS}
|
||||||
assert "calendar.next_event" in ids
|
assert "calendar.next_event" in ids
|
||||||
|
assert "calendar.create_event" in ids
|
||||||
assert "simplex.next_message" in ids
|
assert "simplex.next_message" in ids
|
||||||
assert "simplex.connect_link" in ids
|
assert "simplex.connect_link" in ids
|
||||||
|
|
||||||
|
|||||||
+12
-5
@@ -477,10 +477,14 @@ def test_scheduler_loads_committed_seed_skills(tmp_path):
|
|||||||
trace=TraceLog(str(tmp_path / "runs.jsonl")),
|
trace=TraceLog(str(tmp_path / "runs.jsonl")),
|
||||||
)
|
)
|
||||||
calendar = scheduler.tree["calendar"]
|
calendar = scheduler.tree["calendar"]
|
||||||
assert [s.name for s in calendar] == ["next_event"]
|
assert [s.name for s in calendar] == ["create_event", "next_event"]
|
||||||
assert calendar[0].description.startswith("Report the next")
|
next_event = next(s for s in calendar if s.name == "next_event")
|
||||||
assert calendar[0].status == "ready"
|
assert next_event.description.startswith("Report the next")
|
||||||
assert calendar[0].integration["transport"] == "caldav"
|
assert next_event.status == "ready"
|
||||||
|
assert next_event.integration["transport"] == "caldav"
|
||||||
|
create_event = next(s for s in calendar if s.name == "create_event")
|
||||||
|
assert create_event.description.startswith("Create a calendar event")
|
||||||
|
assert create_event.integration["transport"] == "caldav"
|
||||||
|
|
||||||
|
|
||||||
def test_load_config_anchors_runtime_paths_to_checkout(tmp_path):
|
def test_load_config_anchors_runtime_paths_to_checkout(tmp_path):
|
||||||
@@ -535,7 +539,10 @@ def test_committed_seeds_load_from_a_foreign_cwd(tmp_path, monkeypatch):
|
|||||||
"connect_link",
|
"connect_link",
|
||||||
"next_message",
|
"next_message",
|
||||||
]
|
]
|
||||||
assert [s.name for s in scheduler.tree["calendar"]] == ["next_event"]
|
assert [s.name for s in scheduler.tree["calendar"]] == [
|
||||||
|
"create_event",
|
||||||
|
"next_event",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_navigate_empty_tree_short_circuits(tmp_path):
|
def test_navigate_empty_tree_short_circuits(tmp_path):
|
||||||
|
|||||||
Reference in new issue
Block a user