@parcha/agentrun-serve 0.1.0-beta.41 → 0.1.0-beta.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +74 -0
- package/NOTICE +2 -0
- package/README.md +85 -19
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.js +3 -1
- package/dist/calendar/address.d.ts +49 -0
- package/dist/calendar/address.js +153 -0
- package/dist/calendar/fetch.d.ts +24 -0
- package/dist/calendar/fetch.js +146 -0
- package/dist/calendar/ics-faux.d.ts +39 -0
- package/dist/calendar/ics-faux.js +236 -0
- package/dist/calendar/labels.d.ts +3 -0
- package/dist/calendar/labels.js +61 -0
- package/dist/calendar/names.d.ts +52 -0
- package/dist/calendar/names.js +54 -0
- package/dist/calendar/parse.d.ts +108 -0
- package/dist/calendar/parse.js +625 -0
- package/dist/calendar/routes.d.ts +17 -0
- package/dist/calendar/routes.js +81 -0
- package/dist/calendar/tools.d.ts +23 -0
- package/dist/calendar/tools.js +124 -0
- package/dist/cli.js +3 -1
- package/dist/computer-record.d.ts +55 -0
- package/dist/computer-record.js +315 -0
- package/dist/computer-setting.d.ts +1 -0
- package/dist/computer-setting.js +15 -2
- package/dist/computers.js +2 -14
- package/dist/console/assets/index-BrcDSBD8.css +1 -0
- package/dist/console/assets/index-Bz6IWncl.js +711 -0
- package/dist/console/index.html +2 -2
- package/dist/decisions.js +0 -4
- package/dist/eval-feed.d.ts +2 -2
- package/dist/eval-feed.js +9 -5
- package/dist/feed/parse.d.ts +3 -2
- package/dist/feed/parse.js +9 -18
- package/dist/feed/pointer.d.ts +2 -0
- package/dist/feed/pointer.js +17 -0
- package/dist/file-types.js +1 -0
- package/dist/host-command.d.ts +3 -0
- package/dist/host-command.js +18 -0
- package/dist/host.d.ts +3 -0
- package/dist/host.js +3 -0
- package/dist/import-sources.js +25 -5
- package/dist/learn/audit.d.ts +123 -0
- package/dist/learn/audit.js +159 -0
- package/dist/learn/check.d.ts +14 -0
- package/dist/learn/check.js +194 -14
- package/dist/learn/compile.d.ts +75 -78
- package/dist/learn/compile.js +175 -176
- package/dist/learn/folder.d.ts +9 -0
- package/dist/learn/folder.js +13 -2
- package/dist/learn/promote.d.ts +138 -196
- package/dist/learn/promote.js +68 -184
- package/dist/learn/replay-calls.d.ts +5 -0
- package/dist/learn/replay-calls.js +12 -1
- package/dist/learn/secrets.d.ts +8 -0
- package/dist/learn/secrets.js +12 -0
- package/dist/learn/skills.js +2 -4
- package/dist/learn/workflow-nodes.d.ts +1 -2
- package/dist/learn/workflow-nodes.js +2 -3
- package/dist/learned-nodes.d.ts +9 -34
- package/dist/learned-nodes.js +11 -23
- package/dist/learned-workflow.d.ts +6 -8
- package/dist/learned-workflow.js +18 -57
- package/dist/learning-batch.d.ts +13 -5
- package/dist/learning-batch.js +34 -8
- package/dist/learning-cost.d.ts +11 -4
- package/dist/learning-cost.js +4 -2
- package/dist/learning-routes.js +1 -1
- package/dist/learning-setting.d.ts +38 -0
- package/dist/learning-setting.js +83 -0
- package/dist/learning-words.d.ts +3 -0
- package/dist/learning-words.js +3 -0
- package/dist/learning.d.ts +3 -2
- package/dist/learning.js +5 -3
- package/dist/mcp-oauth.d.ts +8 -0
- package/dist/mcp-oauth.js +7 -0
- package/dist/mcp-private-hosts.d.ts +13 -0
- package/dist/mcp-private-hosts.js +16 -0
- package/dist/openapi.d.ts +1 -0
- package/dist/openapi.js +195 -59
- package/dist/run-files.d.ts +6 -2
- package/dist/run-files.js +29 -7
- package/dist/run-launch.d.ts +6 -3
- package/dist/run-launch.js +2 -0
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-reads-over-store.d.ts +5 -1
- package/dist/run-reads-over-store.js +8 -4
- package/dist/run-recordings.d.ts +20 -0
- package/dist/run-recordings.js +133 -0
- package/dist/runner/adapter-loop.d.ts +5 -1
- package/dist/runner/adapter-loop.js +20 -8
- package/dist/runner/adapter-run-spec.d.ts +16 -1
- package/dist/runner/adapter-run-spec.js +36 -11
- package/dist/runner/audit.d.ts +64 -0
- package/dist/runner/audit.js +338 -0
- package/dist/runner/beside.js +2 -2
- package/dist/runner/cli-run-spec.js +25 -12
- package/dist/runner/feedback-round.d.ts +9 -25
- package/dist/runner/feedback-round.js +19 -40
- package/dist/runner/harness-loops.js +1 -15
- package/dist/runner/journal-kinds.d.ts +0 -7
- package/dist/runner/journal-kinds.js +0 -7
- package/dist/runner/learning-dispatch.d.ts +0 -16
- package/dist/runner/learning-dispatch.js +4 -47
- package/dist/runner/learning-end.d.ts +62 -13
- package/dist/runner/learning-end.js +180 -74
- package/dist/runner/learning-factory.d.ts +1 -1
- package/dist/runner/learning-factory.js +1 -4
- package/dist/runner/learning-loop.d.ts +2 -38
- package/dist/runner/learning-loop.js +8 -116
- package/dist/runner/main.d.ts +11 -0
- package/dist/runner/main.js +30 -3
- package/dist/runner/mcp-run.d.ts +2 -2
- package/dist/runner/promotion.d.ts +78 -50
- package/dist/runner/promotion.js +371 -503
- package/dist/runner/reflect-hook.d.ts +15 -0
- package/dist/runner/reflect-hook.js +28 -14
- package/dist/runner/store-secrets.js +3 -1
- package/dist/runner/workflow-first.d.ts +6 -11
- package/dist/runner/workflow-first.js +24 -25
- package/dist/runs-over-store.d.ts +1 -1
- package/dist/sdk/runs.js +2 -1
- package/dist/secrets.js +2 -2
- package/dist/server.d.ts +4 -3
- package/dist/server.js +132 -26
- package/dist/store/agent-body.d.ts +5 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store/sqlite/store.js +1 -0
- package/dist/store/store.d.ts +3 -0
- package/dist/store-reads/learning.d.ts +18 -7
- package/dist/store-reads/learning.js +36 -23
- package/dist/store-reads/loop.d.ts +5 -39
- package/dist/store-reads/loop.js +21 -80
- package/dist/store-view.d.ts +5 -0
- package/dist/store-view.js +16 -2
- package/dist/template-eval.d.ts +7 -0
- package/dist/template-eval.js +54 -6
- package/dist/templates.d.ts +3 -1
- package/dist/templates.js +14 -1
- package/dist/timeline/computer.js +2 -1
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +7 -0
- package/dist/tools.js +11 -0
- package/dist/trigger-feed.js +67 -18
- package/dist/validate.js +7 -2
- package/dist/workspace/computer-for-claim.d.ts +7 -0
- package/dist/workspace/computer-for-claim.js +12 -2
- package/dist/workspace/paths.d.ts +4 -0
- package/dist/workspace/paths.js +9 -0
- package/dist/workspace/resolve-computer.d.ts +3 -1
- package/dist/workspace/run-tools.d.ts +1 -1
- package/dist/workspace/run-tools.js +14 -3
- package/launcher/agentrun-serve +31 -24
- package/package.json +10 -9
- package/templates/README.md +11 -2
- package/dist/console/assets/index-C3gNPz58.css +0 -1
- package/dist/console/assets/index-COGhx_2Z.js +0 -711
- package/dist/learn/general.d.ts +0 -69
- package/dist/learn/general.js +0 -75
- package/dist/learn/refit.d.ts +0 -48
- package/dist/learn/refit.js +0 -264
- package/dist/runner/step-guard.d.ts +0 -62
- package/dist/runner/step-guard.js +0 -93
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
// A calendar's iCalendar file, parsed (ical.js) and expanded into the events of a window. ical.js reads the file and
|
|
2
|
+
// walks each RRULE; the rest is done here, in absolute time, so every date-time is read in its own zone:
|
|
3
|
+
// - Zones: a TZID that names an IANA zone (America/New_York, or a vendor's path ending in one) is read by the runtime's
|
|
4
|
+
// time zone data; any other (Outlook's "India Standard Time") by the file's own VTIMEZONE of that TZID; a UTC time
|
|
5
|
+
// (…Z) is that instant; a floating time (no zone) and a TZID read by neither are read in the calendar's own zone
|
|
6
|
+
// (X-WR-TIMEZONE, when it names an IANA zone), else the server's.
|
|
7
|
+
// - Recurrence: an RRULE is walked in the event's wall-clock time (so a 09:30 meeting stays at 09:30 across a DST
|
|
8
|
+
// change, as calendars keep it), its UNTIL read as an instant; RDATEs are added and EXDATEs taken out, each in its
|
|
9
|
+
// own zone (an EXDATE that is a date takes out that day's instance). A RECURRENCE-ID override replaces the instance
|
|
10
|
+
// it names wherever it moved it (in or out of the window), and a cancelled one removes it; one with
|
|
11
|
+
// RANGE=THISANDFUTURE does so for its instance and every later one (moved by as much, its length and fields, or
|
|
12
|
+
// cancelled). An RDATE that is a PERIOD keeps its own end or duration. A cancelled series, or a cancelled single
|
|
13
|
+
// event, is left out. A series is walked until no later step can place an instance before its SERIES_MAX-th earliest
|
|
14
|
+
// in the window (a THISANDFUTURE range may move later steps' instances earlier), and keeps those up to it.
|
|
15
|
+
// - Work: one read (eventsIn, summaryOf) has READ_STEPS_MAX rule steps (and READ_WORK_MS) to expand every series,
|
|
16
|
+
// and past them stops with words (RepeatsError, TOO_MANY_REPEATS), never a partial list. A rule whose UNTIL is
|
|
17
|
+
// before the window is not walked at all, and a plain rule (no COUNT; no BY parts, or a weekly one's BYDAY, or a monthly one's day parts) is started
|
|
18
|
+
// whole periods on, just before the window, instead of from its DTSTART years back.
|
|
19
|
+
// - An event's start and end: ISO date-times with the offset of the event's own zone at that instant (a UTC event's in
|
|
20
|
+
// the calendar's zone), or, all day, dates (its end the day after its last, as iCalendar has it).
|
|
21
|
+
import ICAL from "ical.js";
|
|
22
|
+
import { ATTENDEES_MAX, DESCRIPTION_MAX, EMAIL_MAX, EVENTS_MAX, LOCATION_MAX, NAME_MAX, READ_STEPS_MAX, READ_WORK_MS, SERIES_MAX, TITLE_MAX, TOO_MANY_REPEATS, WINDOW_DAYS_DEFAULT, WINDOW_DAYS_MAX } from "./names.js";
|
|
23
|
+
const DAY = 86_400_000;
|
|
24
|
+
const HOUR = 3_600_000;
|
|
25
|
+
const formatters = new Map();
|
|
26
|
+
/** `name` as an IANA zone the runtime knows (a vendor's path ending in one, /mozilla.org/…/Europe/Madrid, read as it), or
|
|
27
|
+
* undefined. */
|
|
28
|
+
export function ianaZone(name) {
|
|
29
|
+
if (!name)
|
|
30
|
+
return undefined;
|
|
31
|
+
const candidates = [name.trim(), /([A-Za-z]+(?:\/[A-Za-z0-9_+-]+){1,2})$/.exec(name.trim())?.[1]].filter((one) => Boolean(one));
|
|
32
|
+
for (const one of candidates) {
|
|
33
|
+
if (one.toUpperCase() === "UTC" || one === "Etc/UTC" || one === "GMT")
|
|
34
|
+
return "UTC";
|
|
35
|
+
if (!one.includes("/"))
|
|
36
|
+
continue;
|
|
37
|
+
try {
|
|
38
|
+
return new Intl.DateTimeFormat("en-US", { timeZone: one }).resolvedOptions().timeZone;
|
|
39
|
+
}
|
|
40
|
+
catch { }
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
/** The server's own zone. */
|
|
45
|
+
export const serverZone = () => Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
|
46
|
+
function formatter(zone) {
|
|
47
|
+
let one = formatters.get(zone);
|
|
48
|
+
if (!one)
|
|
49
|
+
formatters.set(zone, (one = new Intl.DateTimeFormat("en-US", { timeZone: zone, hourCycle: "h23", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit" })));
|
|
50
|
+
return one;
|
|
51
|
+
}
|
|
52
|
+
/** The IANA zones read so far, by name, and the names a TZID was read as (ianaZone): each kept for every read after. */
|
|
53
|
+
const ianaZones = new Map();
|
|
54
|
+
const ianaNames = new Map();
|
|
55
|
+
/** The most days of offsets one zone keeps (a few years); past it they are forgotten and found again. */
|
|
56
|
+
const ZONE_DAYS_MAX = 20_000;
|
|
57
|
+
/** An IANA zone. An instant's offset is the runtime's (Intl, some 6 µs a call), kept by UTC day: a day whose first and
|
|
58
|
+
* last second have one offset has it all day, and only a day a change falls in asks the runtime at each instant. */
|
|
59
|
+
function iana(name) {
|
|
60
|
+
const known = ianaZones.get(name);
|
|
61
|
+
if (known)
|
|
62
|
+
return known;
|
|
63
|
+
const exact = (instant) => {
|
|
64
|
+
const parts = Object.fromEntries(formatter(name).formatToParts(new Date(instant)).map((part) => [part.type, part.value]));
|
|
65
|
+
const wall = Date.UTC(Number(parts.year), Number(parts.month) - 1, Number(parts.day), Number(parts.hour) % 24, Number(parts.minute), Number(parts.second));
|
|
66
|
+
return Math.round((wall - Math.floor(instant / 1000) * 1000) / 60_000);
|
|
67
|
+
};
|
|
68
|
+
const days = new Map();
|
|
69
|
+
const offsetAt = (instant) => {
|
|
70
|
+
const day = Math.floor(instant / DAY);
|
|
71
|
+
let all = days.get(day);
|
|
72
|
+
if (all === undefined) {
|
|
73
|
+
const first = exact(day * DAY);
|
|
74
|
+
all = first === exact((day + 1) * DAY - 1000) ? first : null;
|
|
75
|
+
if (days.size >= ZONE_DAYS_MAX)
|
|
76
|
+
days.clear();
|
|
77
|
+
days.set(day, all);
|
|
78
|
+
}
|
|
79
|
+
return all ?? exact(instant);
|
|
80
|
+
};
|
|
81
|
+
const zone = { name, offsetAt, toInstant: (wall) => placed(wall, offsetAt) };
|
|
82
|
+
ianaZones.set(name, zone);
|
|
83
|
+
return zone;
|
|
84
|
+
}
|
|
85
|
+
/** A wall-clock time placed in a zone, as RFC 5545 (3.3.5) reads it: the instant the zone's clock reads it at; the first,
|
|
86
|
+
* when a fall-back reads it twice; and, when a spring-forward skips it, the instant it is at the offset from before the
|
|
87
|
+
* change (02:30 on a day that jumps 02:00 to 03:00 is 03:30 the new time). The offsets either side are a day off `wall`,
|
|
88
|
+
* past any change near it: no zone is more than a day off UTC, or changes twice in two days. */
|
|
89
|
+
function placed(wall, offsetAt) {
|
|
90
|
+
const before = offsetAt(wall - DAY);
|
|
91
|
+
const after = offsetAt(wall + DAY);
|
|
92
|
+
if (before === after)
|
|
93
|
+
return wall - before * 60_000;
|
|
94
|
+
const read = [before, after].map((offset) => wall - offset * 60_000).filter((instant) => wall - instant === offsetAt(instant) * 60_000);
|
|
95
|
+
return read.length > 0 ? Math.min(...read) : wall - before * 60_000;
|
|
96
|
+
}
|
|
97
|
+
/** A VTIMEZONE of the file, by ical.js: an instant's offset from the file's changes (ical.js keeps each as the UTC time it
|
|
98
|
+
* takes effect, with the offsets before and after, and adds years as later ones are asked for), and a wall-clock time
|
|
99
|
+
* placed as an IANA zone's is. */
|
|
100
|
+
function vtimezone(component, tzid) {
|
|
101
|
+
let tz;
|
|
102
|
+
try {
|
|
103
|
+
tz = new ICAL.Timezone(component);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
// When each change takes effect, ascending, and the offset (minutes) from then on; `first`, the offset before them all.
|
|
109
|
+
let starts = [];
|
|
110
|
+
let offsets = [];
|
|
111
|
+
let first = 0;
|
|
112
|
+
let known = -1;
|
|
113
|
+
const offsetAt = (instant) => {
|
|
114
|
+
tz._ensureCoverage(new Date(instant).getUTCFullYear() + 1);
|
|
115
|
+
const changes = tz.changes;
|
|
116
|
+
if (changes.length !== known) {
|
|
117
|
+
known = changes.length;
|
|
118
|
+
const sorted = changes.map((c) => ({ at: Date.UTC(c.year, c.month - 1, c.day, c.hour, c.minute, c.second), to: Math.round(c.utcOffset / 60), from: Math.round(c.prevUtcOffset / 60) })).sort((a, b) => a.at - b.at);
|
|
119
|
+
starts = sorted.map((c) => c.at);
|
|
120
|
+
offsets = sorted.map((c) => c.to);
|
|
121
|
+
first = sorted[0]?.from ?? 0;
|
|
122
|
+
}
|
|
123
|
+
// The last change at or before `instant`.
|
|
124
|
+
let low = 0;
|
|
125
|
+
let high = starts.length;
|
|
126
|
+
while (low < high) {
|
|
127
|
+
const mid = (low + high) >> 1;
|
|
128
|
+
if (starts[mid] <= instant)
|
|
129
|
+
low = mid + 1;
|
|
130
|
+
else
|
|
131
|
+
high = mid;
|
|
132
|
+
}
|
|
133
|
+
return low === 0 ? first : offsets[low - 1];
|
|
134
|
+
};
|
|
135
|
+
return { name: tzid, offsetAt, toInstant: (wall) => placed(wall, offsetAt) };
|
|
136
|
+
}
|
|
137
|
+
const UTC = { name: "UTC", offsetAt: () => 0, toInstant: (wall) => wall };
|
|
138
|
+
const wallOf = (time) => Date.UTC(time.year, time.month - 1, time.day, time.isDate ? 0 : time.hour, time.isDate ? 0 : time.minute, time.isDate ? 0 : time.second);
|
|
139
|
+
/** The zone of a value with `tzid` (or none), in `host`'s calendar. */
|
|
140
|
+
function zoneOf(tzid, utc, host) {
|
|
141
|
+
if (utc)
|
|
142
|
+
return UTC;
|
|
143
|
+
if (!tzid)
|
|
144
|
+
return host.zone;
|
|
145
|
+
if (!ianaNames.has(tzid) && ianaNames.size < 1_000)
|
|
146
|
+
ianaNames.set(tzid, ianaZone(tzid));
|
|
147
|
+
const named = ianaNames.has(tzid) ? ianaNames.get(tzid) : ianaZone(tzid);
|
|
148
|
+
if (named)
|
|
149
|
+
return named === "UTC" ? UTC : iana(named);
|
|
150
|
+
return host.tzs.get(tzid) ?? host.zone;
|
|
151
|
+
}
|
|
152
|
+
/** `time`, a value of `prop`, read. */
|
|
153
|
+
function stampOf(time, prop, host) {
|
|
154
|
+
const tzid = prop?.getParameter("tzid");
|
|
155
|
+
const utc = !time.isDate && time.zone?.tzid === "UTC" && !tzid;
|
|
156
|
+
return { wall: wallOf(time), date: time.isDate, zone: zoneOf(typeof tzid === "string" ? tzid : undefined, utc, host), utc };
|
|
157
|
+
}
|
|
158
|
+
/** A stamp's instant: a date's is its midnight in the calendar's zone. */
|
|
159
|
+
const instantOf = (stamp, host) => (stamp.date ? host.zone.toInstant(stamp.wall) : stamp.zone.toInstant(stamp.wall));
|
|
160
|
+
/** `instant` as an ISO date-time with `zone`'s offset there, to the second. */
|
|
161
|
+
export function isoIn(instant, zone) {
|
|
162
|
+
const offset = zone.offsetAt(instant);
|
|
163
|
+
const wall = new Date(instant + offset * 60_000).toISOString().slice(0, 19);
|
|
164
|
+
const sign = offset < 0 ? "-" : "+";
|
|
165
|
+
const abs = Math.abs(offset);
|
|
166
|
+
return `${wall}${sign}${String(Math.floor(abs / 60)).padStart(2, "0")}:${String(abs % 60).padStart(2, "0")}`;
|
|
167
|
+
}
|
|
168
|
+
/** The calendar's zone, by name. */
|
|
169
|
+
export const zoneNamed = (name) => (name === "UTC" ? UTC : iana(name));
|
|
170
|
+
const dateOf = (wall) => new Date(wall).toISOString().slice(0, 10);
|
|
171
|
+
/** The file read: its name and zone, its time zones, and its events grouped by series (UID). Throws, in words, on a
|
|
172
|
+
* file that is not an iCalendar calendar. */
|
|
173
|
+
export function parseCalendar(text, host) {
|
|
174
|
+
const body = text.replace(/^/, "").trimStart();
|
|
175
|
+
if (!/^BEGIN:VCALENDAR\b/i.test(body))
|
|
176
|
+
throw new Error(`${host} didn't answer an iCalendar file (one that starts BEGIN:VCALENDAR): check that the address is the calendar's iCal address`);
|
|
177
|
+
let root;
|
|
178
|
+
try {
|
|
179
|
+
root = new ICAL.Component(ICAL.parse(body));
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
throw new Error(`${host}'s calendar file doesn't read as iCalendar (${String(error?.message ?? error).slice(0, 200)})`);
|
|
183
|
+
}
|
|
184
|
+
const calendar = root.name === "vcalendar" ? root : root.getFirstSubcomponent("vcalendar") ?? root;
|
|
185
|
+
const text1 = (value) => (typeof value === "string" && value.trim() ? value.trim() : null);
|
|
186
|
+
const name = text1(calendar.getFirstPropertyValue("x-wr-calname"));
|
|
187
|
+
const zoneName = ianaZone(text1(calendar.getFirstPropertyValue("x-wr-timezone"))) ?? ianaZone(serverZone()) ?? "UTC";
|
|
188
|
+
const tzs = new Map();
|
|
189
|
+
for (const one of calendar.getAllSubcomponents("vtimezone")) {
|
|
190
|
+
const tzid = text1(one.getFirstPropertyValue("tzid"));
|
|
191
|
+
const zone = tzid ? vtimezone(one, tzid) : undefined;
|
|
192
|
+
if (tzid && zone)
|
|
193
|
+
tzs.set(tzid, zone);
|
|
194
|
+
}
|
|
195
|
+
const hosted = { zone: zoneNamed(zoneName), tzs };
|
|
196
|
+
const byUid = new Map();
|
|
197
|
+
const lone = [];
|
|
198
|
+
const events = calendar.getAllSubcomponents("vevent");
|
|
199
|
+
for (const event of events) {
|
|
200
|
+
if (event.hasProperty("recurrence-id"))
|
|
201
|
+
continue;
|
|
202
|
+
const uid = text1(event.getFirstPropertyValue("uid"));
|
|
203
|
+
const series = { main: event, overrides: [], host: hosted };
|
|
204
|
+
if (uid && !byUid.has(uid))
|
|
205
|
+
byUid.set(uid, series);
|
|
206
|
+
else
|
|
207
|
+
lone.push(series);
|
|
208
|
+
}
|
|
209
|
+
for (const event of events) {
|
|
210
|
+
if (!event.hasProperty("recurrence-id"))
|
|
211
|
+
continue;
|
|
212
|
+
const uid = text1(event.getFirstPropertyValue("uid"));
|
|
213
|
+
const series = uid ? byUid.get(uid) : undefined;
|
|
214
|
+
if (series)
|
|
215
|
+
series.overrides.push(event);
|
|
216
|
+
else
|
|
217
|
+
lone.push({ main: event, overrides: [], host: hosted });
|
|
218
|
+
}
|
|
219
|
+
return { name, zone: zoneName, series: [...byUid.values(), ...lone] };
|
|
220
|
+
}
|
|
221
|
+
/** A call's window that does not read, or is refused, in words. */
|
|
222
|
+
export class WindowError extends Error {
|
|
223
|
+
}
|
|
224
|
+
/** A call's `from` and `to` read as a window in `zone`, at `now`: a date is that whole day in the zone (to's day
|
|
225
|
+
* included), a date-time with an offset that instant, one without it the wall-clock time in the zone. Absent: from now,
|
|
226
|
+
* WINDOW_DAYS_DEFAULT days on from `from`. Throws, in words, on one that does not read (a part that names no time, Feb 30
|
|
227
|
+
* or 25:00, included), a `to` not after `from`, or a window over WINDOW_DAYS_MAX days. */
|
|
228
|
+
export function windowOf(args, zoneName, now) {
|
|
229
|
+
const zone = zoneNamed(zoneName);
|
|
230
|
+
const read = (value, field, end) => {
|
|
231
|
+
if (value === undefined || value === null || value === "")
|
|
232
|
+
return undefined;
|
|
233
|
+
if (typeof value !== "string")
|
|
234
|
+
throw new WindowError(`${field} is a date (2026-11-17) or an ISO date-time (2026-11-17T09:00:00-05:00), as text`);
|
|
235
|
+
const date = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
|
|
236
|
+
if (date) {
|
|
237
|
+
const wall = Date.UTC(Number(date[1]), Number(date[2]) - 1, Number(date[3]));
|
|
238
|
+
if (dateOf(wall) !== value)
|
|
239
|
+
throw new WindowError(`${field} ${value} is not a date`);
|
|
240
|
+
return zone.toInstant(end ? wall + DAY : wall);
|
|
241
|
+
}
|
|
242
|
+
const time = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?$/i.exec(value);
|
|
243
|
+
if (!time)
|
|
244
|
+
throw new WindowError(`${field} is a date (2026-11-17) or an ISO date-time (2026-11-17T09:00:00-05:00), not ${JSON.stringify(value.slice(0, 60))}`);
|
|
245
|
+
// Each part names a time, or it is refused: Date.UTC and Date.parse would roll Feb 30 to Mar 2, and 24:00 to the next
|
|
246
|
+
// day, a window the call never asked for.
|
|
247
|
+
const [hour, minute, second] = [Number(time[4]), Number(time[5]), Number(time[6] ?? 0)];
|
|
248
|
+
const offset = /^[+-](\d{2}):?(\d{2})$/.exec(time[7] ?? "");
|
|
249
|
+
if (dateOf(Date.UTC(Number(time[1]), Number(time[2]) - 1, Number(time[3]))) !== `${time[1]}-${time[2]}-${time[3]}` || hour > 23 || minute > 59 || second > 59 || (offset && (Number(offset[1]) > 23 || Number(offset[2]) > 59))) {
|
|
250
|
+
throw new WindowError(`${field} ${value} is not a date-time`);
|
|
251
|
+
}
|
|
252
|
+
if (time[7]) {
|
|
253
|
+
const at = Date.parse(value.replace(" ", "T"));
|
|
254
|
+
if (Number.isNaN(at))
|
|
255
|
+
throw new WindowError(`${field} ${value} is not a date-time`);
|
|
256
|
+
return at;
|
|
257
|
+
}
|
|
258
|
+
return zone.toInstant(Date.UTC(Number(time[1]), Number(time[2]) - 1, Number(time[3]), hour, minute, second));
|
|
259
|
+
};
|
|
260
|
+
const from = read(args.from, "from", false) ?? now;
|
|
261
|
+
const to = read(args.to, "to", true) ?? from + WINDOW_DAYS_DEFAULT * DAY;
|
|
262
|
+
if (to <= from)
|
|
263
|
+
throw new WindowError(`to (${isoIn(to, zone)}) is not after from (${isoIn(from, zone)})`);
|
|
264
|
+
// A day of a DST change is 23 or 25 hours: an hour's slack keeps a window of WINDOW_DAYS_MAX whole days in.
|
|
265
|
+
if (to - from > WINDOW_DAYS_MAX * DAY + 3_600_000)
|
|
266
|
+
throw new WindowError(`the window is at most ${WINDOW_DAYS_MAX} days, and from ${isoIn(from, zone)} to ${isoIn(to, zone)} is ${Math.ceil((to - from - 3_600_000) / DAY)}: ask for ${WINDOW_DAYS_MAX} days or fewer, one window after another`);
|
|
267
|
+
return { start: from, end: to };
|
|
268
|
+
}
|
|
269
|
+
const cancelled = (component) => String(component.getFirstPropertyValue("status") ?? "").toUpperCase() === "CANCELLED";
|
|
270
|
+
/** A component's start, and its length (an all-day event's in days, as wall-clock time; any other's in time). */
|
|
271
|
+
function spanOf(component, host) {
|
|
272
|
+
const startProp = component.getFirstProperty("dtstart");
|
|
273
|
+
const startTime = startProp?.getFirstValue();
|
|
274
|
+
if (!startProp || !startTime || typeof startTime !== "object" || !("year" in startTime))
|
|
275
|
+
return undefined;
|
|
276
|
+
const start = stampOf(startTime, startProp, host);
|
|
277
|
+
const endProp = component.getFirstProperty("dtend");
|
|
278
|
+
const endTime = endProp?.getFirstValue();
|
|
279
|
+
if (endProp && endTime && typeof endTime === "object" && "year" in endTime) {
|
|
280
|
+
const end = stampOf(endTime, endProp, host);
|
|
281
|
+
const length = start.date ? end.wall - start.wall : instantOf(end, host) - instantOf(start, host);
|
|
282
|
+
return { start, length: Math.max(0, length) };
|
|
283
|
+
}
|
|
284
|
+
const duration = component.getFirstPropertyValue("duration");
|
|
285
|
+
if (duration && typeof duration === "object" && "toSeconds" in duration)
|
|
286
|
+
return { start, length: Math.max(0, duration.toSeconds() * 1000) };
|
|
287
|
+
return { start, length: start.date ? DAY : 0 };
|
|
288
|
+
}
|
|
289
|
+
/** One instance of a component, at its own start (a single event, or an override). */
|
|
290
|
+
function single(component, main, host, recurring) {
|
|
291
|
+
const span = spanOf(component, host);
|
|
292
|
+
if (!span)
|
|
293
|
+
return undefined;
|
|
294
|
+
const start = instantOf(span.start, host);
|
|
295
|
+
const end = span.start.date ? host.zone.toInstant(span.start.wall + span.length) : start + span.length;
|
|
296
|
+
return { start, end, startStamp: span.start, endWall: span.start.wall + span.length, component, main, recurring, date: span.start.date };
|
|
297
|
+
}
|
|
298
|
+
/** Whether an instance is in the window: it overlaps it, or, taking no time, starts in it. */
|
|
299
|
+
const inWindow = (one, window) => (one.end > one.start ? one.start < window.end && one.end > window.start : one.start >= window.start && one.start < window.end);
|
|
300
|
+
/** A read's budget: READ_STEPS_MAX steps and READ_WORK_MS on the monotonic clock, or a test's own (ReadOptions.work). */
|
|
301
|
+
const budgetOf = (work = {}) => {
|
|
302
|
+
const clock = work.clock ?? (() => performance.now());
|
|
303
|
+
return { steps: work.steps ?? READ_STEPS_MAX, until: clock() + (work.ms ?? READ_WORK_MS), clock };
|
|
304
|
+
};
|
|
305
|
+
/** A read stopped at its work budget: the calendar has too many repeating events to read. Its message is the words. */
|
|
306
|
+
export class RepeatsError extends Error {
|
|
307
|
+
by;
|
|
308
|
+
/** Which part of the budget ran out: its steps, or its time (the ceiling for rule shapes whose steps cost far more). */
|
|
309
|
+
constructor(by) {
|
|
310
|
+
super(TOO_MANY_REPEATS);
|
|
311
|
+
this.by = by;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const spend = (budget) => {
|
|
315
|
+
budget.steps -= 1;
|
|
316
|
+
if (budget.steps < 0)
|
|
317
|
+
throw new RepeatsError("steps");
|
|
318
|
+
if (budget.clock() > budget.until)
|
|
319
|
+
throw new RepeatsError("time");
|
|
320
|
+
};
|
|
321
|
+
/** A rule's period, of the frequencies a rule is started later in by whole periods (fastForward). */
|
|
322
|
+
const PERIOD_MS = { SECONDLY: 1000, MINUTELY: 60_000, HOURLY: HOUR, DAILY: DAY, WEEKLY: 7 * DAY };
|
|
323
|
+
/** Where to walk `rule` from instead of its DTSTART (`first`, a wall-clock time), when a plain rule starts whole periods
|
|
324
|
+
* before `before` (a wall-clock time no step before which reaches the window): a later start, a whole number of periods
|
|
325
|
+
* on, one period and more before `before`. Its own instants are the rule's from there, the first one (the new start)
|
|
326
|
+
* and its period's earlier days aside, which fall before `before`. undefined: walk it from its DTSTART. */
|
|
327
|
+
function fastForward(rule, first, isDate, before) {
|
|
328
|
+
if (rule.count)
|
|
329
|
+
return undefined;
|
|
330
|
+
const parts = Object.entries(rule.parts ?? {}).filter(([, values]) => Array.isArray(values) && values.length).map(([part]) => part);
|
|
331
|
+
const interval = Math.max(1, rule.interval || 1);
|
|
332
|
+
const d = new Date(first);
|
|
333
|
+
if (rule.freq === "MONTHLY") {
|
|
334
|
+
// The month's own days (BYDAY, BYMONTHDAY, BYSETPOS) are found in each month whatever day it starts on; a plain monthly
|
|
335
|
+
// rule takes DTSTART's day, which every month has only up to the 28th.
|
|
336
|
+
if (parts.some((part) => part !== "BYDAY" && part !== "BYMONTHDAY" && part !== "BYSETPOS"))
|
|
337
|
+
return undefined;
|
|
338
|
+
if (!parts.length && d.getUTCDate() > 28)
|
|
339
|
+
return undefined;
|
|
340
|
+
const b = new Date(before);
|
|
341
|
+
const months = (b.getUTCFullYear() - d.getUTCFullYear()) * 12 + (b.getUTCMonth() - d.getUTCMonth()) - interval - 1;
|
|
342
|
+
const k = Math.floor(months / interval) * interval;
|
|
343
|
+
if (k <= 0)
|
|
344
|
+
return undefined;
|
|
345
|
+
return floating(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + k, parts.length ? 1 : d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()), isDate);
|
|
346
|
+
}
|
|
347
|
+
const unit = PERIOD_MS[rule.freq];
|
|
348
|
+
if (!unit)
|
|
349
|
+
return undefined;
|
|
350
|
+
// A weekly rule's weekdays (BYDAY without a number) fall the same in every week.
|
|
351
|
+
const weekdays = rule.freq === "WEEKLY" && parts.length === 1 && parts[0] === "BYDAY" && (rule.parts.BYDAY ?? []).every((day) => /^[A-Z]{2}$/.test(day));
|
|
352
|
+
if (parts.length && !weekdays)
|
|
353
|
+
return undefined;
|
|
354
|
+
const period = interval * unit;
|
|
355
|
+
const k = Math.floor((before - first) / period) - 1;
|
|
356
|
+
if (k <= 0)
|
|
357
|
+
return undefined;
|
|
358
|
+
return floating(first + k * period, isDate);
|
|
359
|
+
}
|
|
360
|
+
/** A series' instances in `window`, its overrides in their place; none for a cancelled series. Each step of a rule spends
|
|
361
|
+
* from `budget`. */
|
|
362
|
+
function instancesOf(series, window, budget) {
|
|
363
|
+
const { main, overrides, host } = series;
|
|
364
|
+
if (cancelled(main))
|
|
365
|
+
return [];
|
|
366
|
+
const isRecurring = main.hasProperty("rrule") || main.hasProperty("rdate");
|
|
367
|
+
// A lone override, or a single event.
|
|
368
|
+
if (!isRecurring) {
|
|
369
|
+
const one = single(main, main, host, main.hasProperty("recurrence-id"));
|
|
370
|
+
return one && !cancelled(main) && inWindow(one, window) ? [one] : [];
|
|
371
|
+
}
|
|
372
|
+
const span = spanOf(main, host);
|
|
373
|
+
if (!span)
|
|
374
|
+
return [];
|
|
375
|
+
const { start: first, length } = span;
|
|
376
|
+
// A value of `prop` as a wall-clock time in the series' zone: as written when it has no zone of its own (as DTSTART).
|
|
377
|
+
const wallIn = (stamp, prop) => {
|
|
378
|
+
if (stamp.date || (!prop.getParameter("tzid") && !stamp.utc))
|
|
379
|
+
return stamp.wall;
|
|
380
|
+
const instant = instantOf(stamp, host);
|
|
381
|
+
return instant + first.zone.offsetAt(instant) * 60_000;
|
|
382
|
+
};
|
|
383
|
+
// The overrides, by the instance each replaces; the THISANDFUTURE ones also by where their range starts.
|
|
384
|
+
const replaced = new Set();
|
|
385
|
+
const futures = [];
|
|
386
|
+
const out = [];
|
|
387
|
+
// The earliest SERIES_MAX starts placed in the window so far, ascending: once there are as many, a step whose instance,
|
|
388
|
+
// and every later step's, starts after the last of them ends the walk.
|
|
389
|
+
const earliest = [];
|
|
390
|
+
const keep = (one) => {
|
|
391
|
+
out.push(one);
|
|
392
|
+
if (earliest.length === SERIES_MAX && one.start >= earliest[SERIES_MAX - 1])
|
|
393
|
+
return;
|
|
394
|
+
let low = 0;
|
|
395
|
+
let high = earliest.length;
|
|
396
|
+
while (low < high) {
|
|
397
|
+
const mid = (low + high) >> 1;
|
|
398
|
+
if (earliest[mid] <= one.start)
|
|
399
|
+
low = mid + 1;
|
|
400
|
+
else
|
|
401
|
+
high = mid;
|
|
402
|
+
}
|
|
403
|
+
earliest.splice(low, 0, one.start);
|
|
404
|
+
if (earliest.length > SERIES_MAX)
|
|
405
|
+
earliest.pop();
|
|
406
|
+
};
|
|
407
|
+
for (const override of overrides) {
|
|
408
|
+
const prop = override.getFirstProperty("recurrence-id");
|
|
409
|
+
const value = prop?.getFirstValue();
|
|
410
|
+
if (!prop || !value || typeof value !== "object" || !("year" in value))
|
|
411
|
+
continue;
|
|
412
|
+
const read = stampOf(value, prop, host);
|
|
413
|
+
// A RECURRENCE-ID with no zone of its own is in the series' (as its DTSTART is).
|
|
414
|
+
const stamp = prop.getParameter("tzid") || read.utc || read.date ? read : { ...read, zone: first.zone };
|
|
415
|
+
const from = instantOf(stamp, host);
|
|
416
|
+
replaced.add(from);
|
|
417
|
+
if (String(prop.getParameter("range") ?? "").toUpperCase() === "THISANDFUTURE") {
|
|
418
|
+
const own = spanOf(override, host);
|
|
419
|
+
const dtstart = override.getFirstProperty("dtstart");
|
|
420
|
+
const shift = own && dtstart ? wallIn(own.start, dtstart) - wallIn(stamp, prop) : 0;
|
|
421
|
+
const timed = own && (override.hasProperty("dtend") || override.hasProperty("duration"));
|
|
422
|
+
futures.push({ from, shift, length: timed ? own.length : length, component: override, cancelled: cancelled(override) });
|
|
423
|
+
}
|
|
424
|
+
if (cancelled(override))
|
|
425
|
+
continue;
|
|
426
|
+
const moved = single(override, main, host, true);
|
|
427
|
+
if (moved && inWindow(moved, window))
|
|
428
|
+
keep(moved);
|
|
429
|
+
}
|
|
430
|
+
futures.sort((a, b) => a.from - b.from);
|
|
431
|
+
// The latest THISANDFUTURE range an instance (by the instant it had) is in.
|
|
432
|
+
const futureAt = (at) => {
|
|
433
|
+
let found;
|
|
434
|
+
for (const future of futures)
|
|
435
|
+
if (future.from <= at)
|
|
436
|
+
found = future;
|
|
437
|
+
return found;
|
|
438
|
+
};
|
|
439
|
+
// The EXDATEs: instants, and days (an EXDATE that is a date takes out that day's instance).
|
|
440
|
+
const exInstants = new Set();
|
|
441
|
+
const exDays = new Set();
|
|
442
|
+
for (const prop of main.getAllProperties("exdate")) {
|
|
443
|
+
for (const value of prop.getValues()) {
|
|
444
|
+
if (!value || typeof value !== "object" || !("year" in value))
|
|
445
|
+
continue;
|
|
446
|
+
const stamp = stampOf(value, prop, host);
|
|
447
|
+
if (stamp.date && !first.date)
|
|
448
|
+
exDays.add(dateOf(stamp.wall));
|
|
449
|
+
else
|
|
450
|
+
exInstants.add(instantOf(prop.getParameter("tzid") || stamp.utc || stamp.date ? stamp : { ...stamp, zone: first.zone }, host));
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const seen = new Set();
|
|
454
|
+
// The instance at `wall`, `own` long when it has its own length (an RDATE's PERIOD), else its range's or the series'.
|
|
455
|
+
// Returns the instant `wall` is, before any THISANDFUTURE range moves it.
|
|
456
|
+
const take = (wall, own) => {
|
|
457
|
+
const at = instantOf({ ...first, wall }, host);
|
|
458
|
+
if (seen.has(at))
|
|
459
|
+
return at;
|
|
460
|
+
seen.add(at);
|
|
461
|
+
if (replaced.has(at) || exInstants.has(at) || exDays.has(dateOf(first.date ? wall : wall + first.zone.offsetAt(at) * 60_000 - (first.utc ? 0 : first.zone.offsetAt(at) * 60_000))))
|
|
462
|
+
return at;
|
|
463
|
+
const future = futureAt(at);
|
|
464
|
+
if (future?.cancelled)
|
|
465
|
+
return at;
|
|
466
|
+
const placed = future ? wall + future.shift : wall;
|
|
467
|
+
const stamp = { ...first, wall: placed };
|
|
468
|
+
const start = future?.shift ? instantOf(stamp, host) : at;
|
|
469
|
+
const span = own ?? future?.length ?? length;
|
|
470
|
+
const end = first.date ? host.zone.toInstant(placed + span) : start + span;
|
|
471
|
+
const one = { start, end, startStamp: stamp, endWall: placed + span, component: future?.component ?? main, main, recurring: true, date: first.date };
|
|
472
|
+
if (inWindow(one, window))
|
|
473
|
+
keep(one);
|
|
474
|
+
return at;
|
|
475
|
+
};
|
|
476
|
+
// The window's edges as wall-clock times in the series' zone, and the steps that cannot reach it: one whose wall-clock
|
|
477
|
+
// time is before `before` ends before the window (however long, however far a THISANDFUTURE range moves it on), and one
|
|
478
|
+
// at or past `after` starts after it (however far one moves it back). Each edge in the zone's smaller (before) or larger
|
|
479
|
+
// (after) offset of the day around it, so a DST change there moves no instance out of reach.
|
|
480
|
+
const wallZone = first.date ? host.zone : first.zone;
|
|
481
|
+
const longest = Math.max(length, ...futures.map((future) => future.length));
|
|
482
|
+
const reachBack = longest + Math.max(0, ...futures.map((future) => future.shift));
|
|
483
|
+
const reachOn = -Math.min(0, ...futures.map((future) => future.shift));
|
|
484
|
+
const before = window.start + Math.min(wallZone.offsetAt(window.start), wallZone.offsetAt(window.start - reachBack - DAY)) * 60_000 - reachBack;
|
|
485
|
+
const after = window.end + Math.max(wallZone.offsetAt(window.end), wallZone.offsetAt(window.end + reachOn + DAY)) * 60_000 + reachOn;
|
|
486
|
+
// How much earlier than its step's own instant an instance may start: as far back as a THISANDFUTURE range moves one
|
|
487
|
+
// (reachOn), and the hours a DST change between the step's wall-clock time and the moved one may add. A rule's steps
|
|
488
|
+
// come in time order, so once a step's instant less this is past the SERIES_MAX-th earliest start, no later step's
|
|
489
|
+
// instance can come before it.
|
|
490
|
+
const earlier = reachOn + (futures.length ? 3 * HOUR : 0);
|
|
491
|
+
// Each RRULE walked in wall-clock time from DTSTART, its UNTIL read as the wall-clock time of its instant.
|
|
492
|
+
for (const prop of main.getAllProperties("rrule")) {
|
|
493
|
+
const rule = prop.getFirstValue().clone();
|
|
494
|
+
if (rule.until) {
|
|
495
|
+
const until = stampOf(rule.until, null, host);
|
|
496
|
+
const instant = until.utc ? until.wall : instantOf({ ...until, zone: first.zone }, host);
|
|
497
|
+
rule.until = floating(first.date ? (until.date ? until.wall : instant + host.zone.offsetAt(instant) * 60_000) : instant + first.zone.offsetAt(instant) * 60_000, first.date);
|
|
498
|
+
// Ended before the window: not walked at all.
|
|
499
|
+
if (wallOf(rule.until) < before)
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
let iterator;
|
|
503
|
+
try {
|
|
504
|
+
iterator = rule.iterator(fastForward(rule, first.wall, first.date, before) ?? floating(first.wall, first.date));
|
|
505
|
+
}
|
|
506
|
+
catch {
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
for (;;) {
|
|
510
|
+
spend(budget);
|
|
511
|
+
let next;
|
|
512
|
+
try {
|
|
513
|
+
next = iterator.next();
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
if (!next)
|
|
519
|
+
break;
|
|
520
|
+
const wall = wallOf(next);
|
|
521
|
+
if (wall >= after)
|
|
522
|
+
break;
|
|
523
|
+
if (wall < before)
|
|
524
|
+
continue;
|
|
525
|
+
const at = take(wall);
|
|
526
|
+
if (earliest.length === SERIES_MAX && at - earlier > earliest[SERIES_MAX - 1])
|
|
527
|
+
break;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
// DTSTART is an instance of a series whose rule may not reach it (RFC 5545), and each RDATE is one: a PERIOD's with its
|
|
531
|
+
// own end or duration.
|
|
532
|
+
if (!main.hasProperty("rrule"))
|
|
533
|
+
take(first.wall);
|
|
534
|
+
for (const prop of main.getAllProperties("rdate")) {
|
|
535
|
+
for (const value of prop.getValues()) {
|
|
536
|
+
const period = value && typeof value === "object" && "start" in value ? value : undefined;
|
|
537
|
+
const time = (period ? period.start : value);
|
|
538
|
+
if (!time || typeof time !== "object" || !("year" in time))
|
|
539
|
+
continue;
|
|
540
|
+
spend(budget);
|
|
541
|
+
const stamp = stampOf(time, prop, host);
|
|
542
|
+
let own;
|
|
543
|
+
if (period?.end && typeof period.end === "object" && "year" in period.end) {
|
|
544
|
+
const end = stampOf(period.end, prop, host);
|
|
545
|
+
own = Math.max(0, stamp.date ? end.wall - stamp.wall : instantOf(end, host) - instantOf(stamp, host));
|
|
546
|
+
}
|
|
547
|
+
else if (period?.duration && typeof period.duration === "object" && "toSeconds" in period.duration) {
|
|
548
|
+
own = Math.max(0, period.duration.toSeconds() * 1000);
|
|
549
|
+
}
|
|
550
|
+
take(wallIn(stamp, prop), own);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
// Past its SERIES_MAX-th earliest start the series' instances were not all read: those up to it are, and are kept.
|
|
554
|
+
if (earliest.length < SERIES_MAX)
|
|
555
|
+
return out;
|
|
556
|
+
const last = earliest[SERIES_MAX - 1];
|
|
557
|
+
return out.filter((one) => one.start <= last);
|
|
558
|
+
}
|
|
559
|
+
/** A floating ical.js time of a wall-clock time. */
|
|
560
|
+
function floating(wall, isDate) {
|
|
561
|
+
const d = new Date(wall);
|
|
562
|
+
return new ICAL.Time({ year: d.getUTCFullYear(), month: d.getUTCMonth() + 1, day: d.getUTCDate(), hour: isDate ? 0 : d.getUTCHours(), minute: isDate ? 0 : d.getUTCMinutes(), second: isDate ? 0 : d.getUTCSeconds(), isDate }, ICAL.Timezone.localTimezone);
|
|
563
|
+
}
|
|
564
|
+
const asIs = (text) => text;
|
|
565
|
+
/** `text` scrubbed, then cut to `max` characters, ending in "…" when cut; null for none. */
|
|
566
|
+
function cut(value, max, scrub) {
|
|
567
|
+
if (typeof value !== "string")
|
|
568
|
+
return null;
|
|
569
|
+
const text = scrub(value.replace(/\r\n?/g, "\n")).trim();
|
|
570
|
+
if (!text)
|
|
571
|
+
return null;
|
|
572
|
+
return text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
573
|
+
}
|
|
574
|
+
/** A person of an ORGANIZER or ATTENDEE property. */
|
|
575
|
+
function personOf(prop, scrub) {
|
|
576
|
+
const value = String(prop.getFirstValue() ?? "");
|
|
577
|
+
const email = /^mailto:/i.test(value) ? value.replace(/^mailto:/i, "").trim() : null;
|
|
578
|
+
const cn = prop.getParameter("cn");
|
|
579
|
+
return { name: cut(typeof cn === "string" ? cn.replace(/^"|"$/g, "") : null, NAME_MAX, scrub), email: cut(email, EMAIL_MAX, scrub) };
|
|
580
|
+
}
|
|
581
|
+
/** A field of an instance: its override's, else its series'. */
|
|
582
|
+
function fieldOf(one, name) {
|
|
583
|
+
return one.component.getFirstPropertyValue(name) ?? (one.component === one.main ? null : one.main.getFirstPropertyValue(name));
|
|
584
|
+
}
|
|
585
|
+
/** An instance as an answer's event. */
|
|
586
|
+
function eventOf(one, host, scrub) {
|
|
587
|
+
const zone = one.startStamp.utc ? host.zone : one.startStamp.zone;
|
|
588
|
+
const start = one.date ? dateOf(one.startStamp.wall) : isoIn(one.start, zone);
|
|
589
|
+
const end = one.date ? dateOf(one.endWall) : isoIn(one.end, zone);
|
|
590
|
+
const people = one.component.getAllProperties("attendee").length ? one.component : one.main;
|
|
591
|
+
const attendees = people.getAllProperties("attendee").map((prop) => personOf(prop, scrub));
|
|
592
|
+
const organizer = (one.component.getFirstProperty("organizer") ?? one.main.getFirstProperty("organizer"));
|
|
593
|
+
const status = String(fieldOf(one, "status") ?? "").toUpperCase() === "TENTATIVE" ? "tentative" : "confirmed";
|
|
594
|
+
return {
|
|
595
|
+
title: cut(fieldOf(one, "summary"), TITLE_MAX, scrub)?.replace(/\s*\n\s*/g, " ") ?? "(no title)",
|
|
596
|
+
start, end, all_day: one.date,
|
|
597
|
+
location: cut(fieldOf(one, "location"), LOCATION_MAX, scrub),
|
|
598
|
+
description: cut(fieldOf(one, "description"), DESCRIPTION_MAX, scrub),
|
|
599
|
+
status, recurring: one.recurring,
|
|
600
|
+
organizer: organizer ? personOf(organizer, scrub) : null,
|
|
601
|
+
attendees: attendees.slice(0, ATTENDEES_MAX),
|
|
602
|
+
...(attendees.length > ATTENDEES_MAX ? { attendees_omitted: attendees.length - ATTENDEES_MAX } : {}),
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
/** The calendar's events in `window`, sorted by start (then title), at most EVENTS_MAX; `truncated` when there were more.
|
|
606
|
+
* One read, under one work budget: past it, RepeatsError. */
|
|
607
|
+
export function eventsIn(calendar, window, options = {}) {
|
|
608
|
+
const budget = budgetOf(options.work);
|
|
609
|
+
const all = calendar.series.flatMap((series) => instancesOf(series, window, budget).map((one) => ({ one, host: series.host })));
|
|
610
|
+
all.sort((a, b) => a.one.start - b.one.start || a.one.end - b.one.end);
|
|
611
|
+
const events = all.slice(0, EVENTS_MAX).map(({ one, host }) => eventOf(one, host, options.scrub ?? asIs));
|
|
612
|
+
events.sort((a, b) => 0 || (a.start === b.start ? a.title.localeCompare(b.title) : 0));
|
|
613
|
+
return { events, truncated: all.length > EVENTS_MAX };
|
|
614
|
+
}
|
|
615
|
+
/** How many events the calendar holds (its series and single events, not cancelled), and its next one from `now` within
|
|
616
|
+
* WINDOW_DAYS_MAX days: what a test of it says. One read, under one work budget: past it, RepeatsError. */
|
|
617
|
+
export function summaryOf(calendar, now, options = {}) {
|
|
618
|
+
const budget = budgetOf(options.work);
|
|
619
|
+
const count = calendar.series.filter((series) => !cancelled(series.main) && !series.main.hasProperty("recurrence-id")).length;
|
|
620
|
+
const window = { start: now, end: now + WINDOW_DAYS_MAX * DAY };
|
|
621
|
+
const upcoming = calendar.series.flatMap((series) => instancesOf(series, window, budget).filter((one) => one.start >= now).map((one) => ({ one, host: series.host })));
|
|
622
|
+
upcoming.sort((a, b) => a.one.start - b.one.start);
|
|
623
|
+
const first = upcoming[0];
|
|
624
|
+
return { count, ...(first ? { next: eventOf(first.one, first.host, options.scrub ?? asIs) } : {}) };
|
|
625
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type CalendarStore } from "./address.js";
|
|
2
|
+
import type { CalendarNetwork, CalendarReach } from "./fetch.js";
|
|
3
|
+
/** The routes, by their operation names. */
|
|
4
|
+
export declare const CALENDAR_ROUTES: ReadonlySet<string>;
|
|
5
|
+
/** The store and seal key; where a read may go beyond public hosts (the server's: a local install's loopback, the owner's
|
|
6
|
+
* private hosts), read at each use, and the network it goes through (McpServersOverStore.outboundReach); the clock a
|
|
7
|
+
* test's next event is found by. */
|
|
8
|
+
export type CalendarRouteDeps = {
|
|
9
|
+
store: CalendarStore;
|
|
10
|
+
sealKey: Buffer;
|
|
11
|
+
reach: () => Promise<CalendarReach>;
|
|
12
|
+
network?: CalendarNetwork;
|
|
13
|
+
now?: () => number;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
};
|
|
16
|
+
/** One route's answer (the dispatch sends it 200; DELETE's is undefined, 204). A body that does not read is 422. */
|
|
17
|
+
export declare function calendarRoute(deps: CalendarRouteDeps, name: string, body?: unknown): Promise<unknown>;
|