@aidenhq/sdk 0.5.18 → 0.5.19
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/dist/src/schedule.d.ts +28 -7
- package/dist/src/schedule.js +201 -123
- package/package.json +1 -1
package/dist/src/schedule.d.ts
CHANGED
|
@@ -42,11 +42,32 @@ export declare function parseSchedule(text: string, fmt?: ClockFormat, opts?: {
|
|
|
42
42
|
*/
|
|
43
43
|
export declare function nextRuns(crons: string[], tz: string, from: Date, count?: number): Date[];
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
45
|
+
* A repeat as the agent that read the owner's words states it, and the board checks it: `every` a cadence ("2 hours",
|
|
46
|
+
* "30 minutes", "1 hour"), `between` a window the repeats stay inside (the end excluded), `days` the days it runs on, and
|
|
47
|
+
* `timezone` the IANA zone its clock times are read in. `every` and `between` go together (a repeat with no window would run
|
|
48
|
+
* through the night); `days` and `timezone` may be left out (every day, the caller's zone). `at` is a time of day for a
|
|
49
|
+
* schedule without `every` ("every weekday at 08:00"). The board never reads the owner's prose: this shape is the schedule.
|
|
48
50
|
*/
|
|
49
|
-
export
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
export interface ScheduleSpec {
|
|
52
|
+
every?: string;
|
|
53
|
+
between?: [string, string];
|
|
54
|
+
days?: string[];
|
|
55
|
+
at?: string;
|
|
56
|
+
/** A five-field cron line (or several, one trigger each): the structured alternative to every/between/at. */
|
|
57
|
+
crons?: string[];
|
|
58
|
+
timezone?: string;
|
|
59
|
+
}
|
|
60
|
+
/** A validated spec: the same `Parsed` a plain-English schedule gives, its zone, and `english`, the canonical phrase that reads back
|
|
61
|
+
* to the same cron lines (what the board stores, so the owner can open and re-edit it later). */
|
|
62
|
+
export type SpecParsed = Parsed & {
|
|
63
|
+
timezone?: string;
|
|
64
|
+
english: string;
|
|
65
|
+
};
|
|
66
|
+
/** The shape a refusal names, with two examples that work (a tool error or a 400 is a string). */
|
|
67
|
+
export declare const SCHEDULE_SPEC_SHAPE = "a schedule like { every: \"2 hours\", between: [\"09:00\", \"17:00\"], days: [\"weekdays\"], timezone: \"Asia/Dubai\" }";
|
|
68
|
+
/**
|
|
69
|
+
* Validate a structured schedule and turn it into the same `{ crons, words }` a plain-English schedule gives: one cron line
|
|
70
|
+
* per group of times, window end excluded, day list in the weekday field. A repeat needs `between`; a field that isn't valid is
|
|
71
|
+
* refused with the accepted shape and two examples. No owner prose is read here: `timezone` is checked and returned as the zone.
|
|
72
|
+
*/
|
|
73
|
+
export declare function scheduleSpec(spec: ScheduleSpec, fmt?: ClockFormat): SpecParsed | Refusal;
|
package/dist/src/schedule.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// Schedules: one plain-English parser for the routine `schedules` config and the board's schedule fields and preview, and one
|
|
2
|
+
// validator for the structured schedule the chat path takes (an agent reads the owner's words; the board checks the object).
|
|
3
|
+
// English or a five-field cron in, cron lines plus the schedule in words out. Deterministic and dependency-free; for the
|
|
4
|
+
// plain-English parser the time zone stays outside it (callers pass it to Paperclip). The board imports @aidenhq/sdk/schedule;
|
|
4
5
|
// compile and the kernel import this source.
|
|
5
6
|
//
|
|
6
7
|
// Paperclip's scheduler takes lists, ranges and steps, and a day of month AND a weekday must both match (the first Monday is days 1-7
|
|
@@ -312,6 +313,58 @@ function daysLabel(dow) {
|
|
|
312
313
|
const list = dow.split(",").flatMap((p) => (/^\d-\d$/.test(p) ? Array.from({ length: Number(p[2]) - Number(p[0]) + 1 }, (_, i) => Number(p[0]) + i) : [Number(p)]));
|
|
313
314
|
return list.length === 1 ? `Every ${LONG[list[0]]}` : andList(list.map((d) => SHORT[d]));
|
|
314
315
|
}
|
|
316
|
+
/** The days as the parser reads them back: "weekdays", "weekends", or the weekday names it accepts. */
|
|
317
|
+
const englishDays = (list) => list.join(",") === "1,2,3,4,5" ? "weekdays" : list.join(",") === "0,6" ? "weekends" : andList(list.map((d) => LONG[d]));
|
|
318
|
+
/** A time of day as 24-hour "HH:MM": the form the parser reads back in either clock format. */
|
|
319
|
+
const h24 = (t) => `${String(Math.floor(t / 60)).padStart(2, "0")}:${String(t % 60).padStart(2, "0")}`;
|
|
320
|
+
/** A cron weekday field as the 0-6 days it means, through the parser's own expansion (steps, ranges, names); null if it isn't a weekday field. */
|
|
321
|
+
function expandCronDay(field) {
|
|
322
|
+
const days = expand(field, 4);
|
|
323
|
+
return days ? [...new Set(days.map((d) => d % 7))].sort((a, b) => a - b) : null;
|
|
324
|
+
}
|
|
325
|
+
/** A weekday field as its number field ("1-5"), names and lists alike; null for one that isn't weekday days. */
|
|
326
|
+
function cronDayField(field) {
|
|
327
|
+
const days = expandCronDay(field);
|
|
328
|
+
if (!days?.length)
|
|
329
|
+
return null;
|
|
330
|
+
const out = [];
|
|
331
|
+
for (let i = 0; i < days.length;) {
|
|
332
|
+
let j = i;
|
|
333
|
+
while (days[j + 1] === days[j] + 1)
|
|
334
|
+
j++;
|
|
335
|
+
if (j - i >= 2)
|
|
336
|
+
out.push(`${days[i]}-${days[j]}`);
|
|
337
|
+
else
|
|
338
|
+
for (let k = i; k <= j; k++)
|
|
339
|
+
out.push(String(days[k]));
|
|
340
|
+
i = j + 1;
|
|
341
|
+
}
|
|
342
|
+
return out.join(",");
|
|
343
|
+
}
|
|
344
|
+
/** Cron lines as one phrase the parser reads back to the same lines: one line is its own phrase; several must share their day fields. */
|
|
345
|
+
function editableEnglish(lines, fmt) {
|
|
346
|
+
if (lines.length === 1)
|
|
347
|
+
return lines[0];
|
|
348
|
+
// What the lines mean, not how they were spelled: "MON-FRI" is "1-5", and the caller's order is not the parser's.
|
|
349
|
+
const fields = lines.map((l) => (isRefusal(parseSchedule(l, fmt)) ? null : parseSchedule(l, fmt).crons)).flatMap((cs) => (cs?.length === 1 ? [cs[0].split(/\s+/)] : []));
|
|
350
|
+
if (fields.length !== lines.length)
|
|
351
|
+
return null;
|
|
352
|
+
const day = (f) => [f[2], f[3], f[4] === "*" ? "*" : expandCronDay(f[4])?.join(",")].join(" ");
|
|
353
|
+
if (fields.some((f) => day(f) !== day(fields[0])))
|
|
354
|
+
return null;
|
|
355
|
+
const times = [...new Set(lines.flatMap((l) => timesOf([l]) ?? []))].sort((a, b) => a - b);
|
|
356
|
+
const [, , dom, mon, dow] = fields[0];
|
|
357
|
+
const days = dow === "*" ? [0, 1, 2, 3, 4, 5, 6] : (expandCronDay(dow) ?? []);
|
|
358
|
+
// A date ("every month on the 1st at 9:00 and 16:30") and a weekday set each read back; a no-day schedule is "every day".
|
|
359
|
+
const when = dom !== "*" ? `every month on the ${ordinal(Number(dom))}` : mon !== "*" ? null : days.length === 7 ? "every day" : `on ${englishDays(days)}`;
|
|
360
|
+
if (!when)
|
|
361
|
+
return null;
|
|
362
|
+
const english = `${when} at ${times.map(h24).join(", ")}`;
|
|
363
|
+
const back = parseSchedule(english, fmt);
|
|
364
|
+
// The phrase must mean the same moments, whichever equivalent spelling the caller used.
|
|
365
|
+
const signature = (cs) => cs.flatMap((c) => { const f = c.split(/\s+/); const d = f[4] === "*" ? ["*"] : (expandCronDay(f[4]) ?? []); return (timesOf([c]) ?? []).map((t) => `${f[2]} ${f[3]} ${d.join(",")}@${t}`); }).sort().join("|");
|
|
366
|
+
return !isRefusal(back) && signature(back.crons) === signature(lines) ? english : null;
|
|
367
|
+
}
|
|
315
368
|
/**
|
|
316
369
|
* Turn English (or a five-field cron) into cron lines. `fmt` is the owner's clock format for the words. A phrase the grammar can't say
|
|
317
370
|
* exactly is refused with the reason and two examples that work.
|
|
@@ -523,131 +576,156 @@ export function nextRuns(crons, tz, from, count = 3) {
|
|
|
523
576
|
}
|
|
524
577
|
return out;
|
|
525
578
|
}
|
|
526
|
-
|
|
527
|
-
const
|
|
528
|
-
const
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if (
|
|
542
|
-
return
|
|
543
|
-
|
|
579
|
+
/** The shape a refusal names, with two examples that work (a tool error or a 400 is a string). */
|
|
580
|
+
export const SCHEDULE_SPEC_SHAPE = 'a schedule like { every: "2 hours", between: ["09:00", "17:00"], days: ["weekdays"], timezone: "Asia/Dubai" }';
|
|
581
|
+
const SPEC_EXAMPLES = [
|
|
582
|
+
'{ every: "2 hours", between: ["09:00","17:00"], days: ["weekdays"], timezone: "Asia/Dubai" }',
|
|
583
|
+
'{ every: "30 minutes", between: ["09:00","17:00"], days: ["weekdays"], timezone: "Asia/Dubai" }',
|
|
584
|
+
];
|
|
585
|
+
const specRefusal = (error) => refuse(error, SPEC_EXAMPLES);
|
|
586
|
+
const DAY_NAMES = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
|
|
587
|
+
/** The names each weekday accepts, whole: a range or a typo is refused, never read as its first day. */
|
|
588
|
+
const DAY_ALIASES = [["sun", "sunday", "sundays"], ["mon", "monday", "mondays"], ["tue", "tues", "tuesday", "tuesdays"], ["wed", "wednesday", "wednesdays"], ["thu", "thur", "thurs", "thursday", "thursdays"], ["fri", "friday", "fridays"], ["sat", "saturday", "saturdays"]];
|
|
589
|
+
/** A day list in the structured spec: "weekdays", "weekends", "daily"/"every day", or one weekday's name. */
|
|
590
|
+
function specDay(day) {
|
|
591
|
+
const d = day.trim().toLowerCase();
|
|
592
|
+
if (d === "weekdays" || d === "weekday")
|
|
593
|
+
return -1;
|
|
594
|
+
if (d === "weekends" || d === "weekend")
|
|
595
|
+
return -2;
|
|
596
|
+
if (d === "daily" || d === "every day" || d === "everyday" || d === "day")
|
|
597
|
+
return -3;
|
|
598
|
+
const i = DAY_ALIASES.findIndex((names) => names.includes(d));
|
|
599
|
+
return i >= 0 ? i : null;
|
|
544
600
|
}
|
|
545
|
-
const MONTH_WORD = "(?:january|february|march|april|may|june|july|august|september|october|november|december|jan|feb|mar|apr|jun|jul|aug|sept?|oct|nov|dec)";
|
|
546
|
-
const ZONE_ABBR = "(?:utc|gmt|[ecmp][sd]t|ist|cet|cest|bst|jst|gst|sgt|hkt|aest|aedt|eet|msk)";
|
|
547
|
-
/**
|
|
548
|
-
* Words after a found schedule that narrow it in a way the grammar can't say ("Pacific time", "only in December",
|
|
549
|
-
* "for the next week"): the owner meant something narrower or elsewhere than what was found, so the match is refused, not
|
|
550
|
-
* cut off. Plain trailing words ("summarise my PRs", "and send it to me") are not qualifiers.
|
|
551
|
-
*/
|
|
552
|
-
const ZONE_WORD = "(?:pacific|eastern|central|mountain|atlantic|alaska|hawaii|local|standard|daylight|british|greenwich|gulf|india|indian|japan|singapore|dubai|london|paris|berlin|tokyo|sydney|new\\s+york|los\\s+angeles)";
|
|
553
|
-
/** Places a schedule would silently ignore ("every weekday at 9 in London"): named cities and countries, not app or channel names. */
|
|
554
|
-
const PLACE_WORD = "(?:london|paris|berlin|tokyo|sydney|melbourne|dubai|abu\\s+dhabi|singapore|new\\s+york|nyc|los\\s+angeles|san\\s+francisco|chicago|toronto|vancouver|boston|seattle|austin|denver|miami|mumbai|delhi|bangalore|bengaluru|hong\\s+kong|seoul|shanghai|beijing|amsterdam|madrid|barcelona|rome|milan|zurich|lisbon|dublin|stockholm|istanbul|cairo|johannesburg|nairobi|lagos|s[aã]o\\s+paulo|mexico\\s+city|india|japan|china|germany|france|spain|italy|canada|australia|brazil|ireland|the\\s+uk|the\\s+us|the\\s+usa|europe|asia|africa)";
|
|
555
|
-
const QUALIFIER = new RegExp(`^(?:only\\s+(?:in|on|during|for|every|each|from|between|until|weekdays?|weekends?|${DAY}|${MONTH_WORD})\\b|except|excluding|unless|until|till|during` +
|
|
556
|
-
`|for\\s+(?:the\\s+)?(?:next|first|rest|coming)\\b|for\\s+(?:an?|one|two|three|\\d+)\\s+(?:days?|weeks?|months?|years?|hours?)\\b` +
|
|
557
|
-
`|in\\s+(?:the\\s+)?(?:morning|afternoon|evening|night|summer|winter|spring|fall|autumn|${MONTH_WORD}\\b)` +
|
|
558
|
-
`|(?:in\\s+|at\\s+)?${ZONE_ABBR}\\b|(?:in\\s+|my\\s+|your\\s+|our\\s+)?${ZONE_WORD}(?:\\s+(?:standard|daylight))?\\s+(?:time\\b|zone\\b)|time\\s?zone\\b` +
|
|
559
|
-
`|in\\s+(?:${ZONE_WORD}|${PLACE_WORD})\\b|(?:america|europe|asia|africa|australia|pacific|atlantic|indian)/[a-z_]+)`);
|
|
560
|
-
/**
|
|
561
|
-
* A conditional opening ("while keeping the report brief", "if anything changed", "only when it's a holiday") is an instruction for
|
|
562
|
-
* the run unless what follows narrows the schedule ("while it's a holiday", "if the first Monday is busy"): then the match is
|
|
563
|
-
* refused. `only` is read with the conditional, not as a qualifier on its own ("only include open PRs" is an instruction).
|
|
564
|
-
*/
|
|
565
|
-
const CONDITIONAL = /^(?:only\s+)?(?:when|whenever|while|if|once|provided|as\s+long\s+as)\b/;
|
|
566
|
-
const TIME_SCOPE = new RegExp(`\\b(?:${DAY}|${MONTH_WORD}|weekdays?|weekends?|business\\s+days?|holidays?|time|hour|noon|midnight|morning|afternoon|evening|night|summer|winter|spring|fall|autumn|${ZONE_ABBR}|${ZONE_WORD})\\b|\\d`, "i");
|
|
567
|
-
const NEGATION = /\b(?:don['’]?t|do\s+not|doesn['’]?t|never|stop|stopping|cancel(?:l?ing)?|no\s+longer|not\s+anymore|without|remove|delete|disable|pause|instead\s+of)\b/i;
|
|
568
|
-
/** A single quote that could open a quotation: at a word boundary, before a word, and not a closing curly one. */
|
|
569
|
-
const opensQuote = (s, at) => s[at] !== "’" && /(?:^|[\s(\[{])/.test(s[at - 1] ?? " ") && /\S/.test(s[at + 1] ?? " ");
|
|
570
|
-
/** A single quote that could close one: a closing curly apostrophe, or one before a space, punctuation or the end. */
|
|
571
|
-
const closesQuote = (s, at) => s[at] === "’" || !/\S/.test(s[at + 1] ?? " ");
|
|
572
|
-
/** The first single quote after `at` that could close one. */
|
|
573
|
-
const closerAfter = (s, at) => {
|
|
574
|
-
for (let k = at + 1; k < s.length; k++)
|
|
575
|
-
if ("'‘’".includes(s[k]) && closesQuote(s, k))
|
|
576
|
-
return k;
|
|
577
|
-
return -1;
|
|
578
|
-
};
|
|
579
601
|
/**
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
*
|
|
602
|
+
* Validate a structured schedule and turn it into the same `{ crons, words }` a plain-English schedule gives: one cron line
|
|
603
|
+
* per group of times, window end excluded, day list in the weekday field. A repeat needs `between`; a field that isn't valid is
|
|
604
|
+
* refused with the accepted shape and two examples. No owner prose is read here: `timezone` is checked and returned as the zone.
|
|
583
605
|
*/
|
|
584
|
-
function
|
|
585
|
-
if (
|
|
586
|
-
return
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
if (
|
|
596
|
-
|
|
597
|
-
|
|
606
|
+
export function scheduleSpec(spec, fmt = "12h") {
|
|
607
|
+
if (!spec || typeof spec !== "object" || Array.isArray(spec))
|
|
608
|
+
return specRefusal(`a schedule is an object; give ${SCHEDULE_SPEC_SHAPE}`);
|
|
609
|
+
const extra = Object.keys(spec).filter((k) => !["every", "between", "days", "at", "crons", "timezone"].includes(k));
|
|
610
|
+
if (extra.length)
|
|
611
|
+
return specRefusal(`"${extra[0]}" isn't a schedule field (every, between, days, at, crons, timezone); give ${SCHEDULE_SPEC_SHAPE}`);
|
|
612
|
+
const zone = spec.timezone;
|
|
613
|
+
if (zone !== undefined) {
|
|
614
|
+
if (typeof zone !== "string" || !zone.trim())
|
|
615
|
+
return specRefusal("timezone must be an IANA name such as Asia/Dubai");
|
|
616
|
+
// Intl also takes a fixed offset ("+04:00"): the spec takes a named zone, which keeps its daylight-saving rules.
|
|
617
|
+
if (/^[+-]?\d/.test(zone.trim()) || /^(?:utc|gmt)?[+-]\d/.test(zone.trim().toLowerCase()))
|
|
618
|
+
return specRefusal(`"${zone}" is an offset, not a zone; use an IANA name such as Asia/Dubai or Europe/London`);
|
|
619
|
+
try {
|
|
620
|
+
new Intl.DateTimeFormat("en-US", { timeZone: zone.trim() });
|
|
621
|
+
}
|
|
622
|
+
catch {
|
|
623
|
+
return specRefusal(`"${zone}" isn't a time zone; use an IANA name such as Asia/Dubai or Europe/London`);
|
|
598
624
|
}
|
|
599
|
-
else if (open > 0 && closesQuote(before, at))
|
|
600
|
-
open--;
|
|
601
625
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
const
|
|
621
|
-
if (
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
626
|
+
const clock = (t) => clockWords(Math.floor(t / 60), t % 60, fmt);
|
|
627
|
+
// Cron lines are structure too: exactly the five-field cron the shared parser reads, never English, and measured like a parsed phrase.
|
|
628
|
+
if (spec.crons !== undefined) {
|
|
629
|
+
if (spec.every !== undefined || spec.between !== undefined || spec.at !== undefined || spec.days !== undefined)
|
|
630
|
+
return specRefusal("give crons alone, or every/between/at and days, not both");
|
|
631
|
+
if (!Array.isArray(spec.crons) || !spec.crons.length || spec.crons.length > 12)
|
|
632
|
+
return specRefusal("crons is a list of one to twelve five-field cron lines");
|
|
633
|
+
const lines = [];
|
|
634
|
+
for (const c of spec.crons) {
|
|
635
|
+
// The parser's own grammar, not a narrower one: fields of letters and symbols, spaces between them collapsed.
|
|
636
|
+
const one = typeof c === "string" ? c.trim().split(/\s+/) : [];
|
|
637
|
+
if (one.length !== 5 || one.some((f) => !/^[\w*,/-]+$/.test(f)))
|
|
638
|
+
return specRefusal(`"${String(c)}" isn't a five-field cron line (minute hour day month weekday)`);
|
|
639
|
+
const p = parseSchedule(one.join(" "), fmt);
|
|
640
|
+
if (isRefusal(p))
|
|
641
|
+
return specRefusal(`"${c.trim()}" isn't a valid cron line: ${p.error}`);
|
|
642
|
+
// The parser's own spelling: the same line, however the caller wrote its day names or spacing.
|
|
643
|
+
const cron = p.crons[0];
|
|
644
|
+
const f = cron.split(/\s+/);
|
|
645
|
+
if (f[4] !== "*") {
|
|
646
|
+
// The parser echoes a named weekday as written ("MON-FRI"): store its number, so two spellings of one day set are one line.
|
|
647
|
+
const days = cronDayField(f[4]);
|
|
648
|
+
if (!days)
|
|
649
|
+
return specRefusal(`"${c.trim()}" isn't a valid cron line: its weekday isn't one`);
|
|
650
|
+
f[4] = days;
|
|
651
|
+
}
|
|
652
|
+
lines.push(f.join(" "));
|
|
653
|
+
}
|
|
654
|
+
// One trigger each: two that fire at the same instant are a double run, which the gap rule refuses everywhere else.
|
|
655
|
+
if (minGapOf(lines) === 0)
|
|
656
|
+
return specRefusal("two cron lines fire at the same instant; give one trigger per moment");
|
|
657
|
+
// What the board stores must read back to the same lines, so the owner can reopen and save it unchanged: one line is itself
|
|
658
|
+
// English to the parser; several are, only when they share their day fields (one "on <days> at <times>" phrase).
|
|
659
|
+
const english = editableEnglish(lines, fmt);
|
|
660
|
+
if (!english)
|
|
661
|
+
return specRefusal("crons that don't share their days can't be edited as one schedule; give one cron line");
|
|
662
|
+
return { ...measured(lines, lines.map((c) => describeCron(c, fmt)).join(" and ")), english, ...(zone ? { timezone: zone.trim() } : {}) };
|
|
663
|
+
}
|
|
664
|
+
// The days: every day when none are given.
|
|
665
|
+
let list = [0, 1, 2, 3, 4, 5, 6];
|
|
666
|
+
if (spec.days !== undefined) {
|
|
667
|
+
if (!Array.isArray(spec.days) || !spec.days.length)
|
|
668
|
+
return specRefusal("days is a list of weekdays, e.g. [\"weekdays\"] or [\"mon\",\"wed\",\"fri\"]");
|
|
669
|
+
const out = new Set();
|
|
670
|
+
for (const d of spec.days) {
|
|
671
|
+
const i = typeof d === "string" ? specDay(d) : null;
|
|
672
|
+
if (i === null)
|
|
673
|
+
return specRefusal(`"${String(d)}" isn't a day; use "weekdays", "weekends", "daily" or weekday names (mon … sun)`);
|
|
674
|
+
if (i === -1)
|
|
675
|
+
[1, 2, 3, 4, 5].forEach((x) => out.add(x));
|
|
676
|
+
else if (i === -2)
|
|
677
|
+
[0, 6].forEach((x) => out.add(x));
|
|
678
|
+
else if (i === -3)
|
|
679
|
+
[0, 1, 2, 3, 4, 5, 6].forEach((x) => out.add(x));
|
|
680
|
+
else
|
|
681
|
+
out.add(i);
|
|
650
682
|
}
|
|
683
|
+
if (!out.size)
|
|
684
|
+
return specRefusal("days must name at least one day");
|
|
685
|
+
list = [...out].sort((a, b) => a - b);
|
|
686
|
+
}
|
|
687
|
+
const df = list.length === 7 ? "*" : dowField(list);
|
|
688
|
+
if (spec.every !== undefined) {
|
|
689
|
+
const raw = typeof spec.every === "string" ? spec.every.trim() : "";
|
|
690
|
+
const m = /^(?:every\s+)?(\d+)\s*(minutes?|mins?|hours?|hrs?|h)$/i.exec(raw);
|
|
691
|
+
if (!m)
|
|
692
|
+
return specRefusal(`every must be a cadence such as "2 hours" or "30 minutes"; give ${SCHEDULE_SPEC_SHAPE}`);
|
|
693
|
+
const n = Number(m[1]);
|
|
694
|
+
const unit = m[2].toLowerCase().startsWith("m") ? "min" : "hour";
|
|
695
|
+
if (unit === "min" && n < MIN_GAP_MINUTES)
|
|
696
|
+
return specRefusal(`a schedule runs at most every ${MIN_GAP_MINUTES} minutes, not every ${n}`);
|
|
697
|
+
if (unit === "min" && 60 % n !== 0)
|
|
698
|
+
return specRefusal(`every ${n} minutes doesn't divide the hour evenly; use 15, 20, 30 or 60`);
|
|
699
|
+
if (unit === "hour" && 24 % n !== 0)
|
|
700
|
+
return specRefusal(`every ${n} hours doesn't divide the day evenly; use 1, 2, 3, 4, 6, 8, 12 or 24`);
|
|
701
|
+
if (spec.at !== undefined)
|
|
702
|
+
return specRefusal("give either every (a repeat inside between) or at (a time of day), not both");
|
|
703
|
+
if (spec.between === undefined)
|
|
704
|
+
return specRefusal(`a repeat needs between: the window it stays inside, e.g. ${SCHEDULE_SPEC_SHAPE}`);
|
|
705
|
+
if (!Array.isArray(spec.between) || spec.between.length !== 2 || spec.between.some((x) => typeof x !== "string"))
|
|
706
|
+
return specRefusal('between is two times of day, e.g. ["09:00", "17:00"]');
|
|
707
|
+
const [a, b] = [clockOf(spec.between[0]), clockOf(spec.between[1])];
|
|
708
|
+
if (a === null || b === null)
|
|
709
|
+
return specRefusal(`between must be two times of day such as "09:00" and "17:00"`);
|
|
710
|
+
if (b <= a)
|
|
711
|
+
return specRefusal("between must end after it starts, both the same day (a window can't cross midnight)");
|
|
712
|
+
const step = unit === "min" ? n : n * 60;
|
|
713
|
+
const times = [];
|
|
714
|
+
for (let t = a; t < b; t += step)
|
|
715
|
+
times.push(t);
|
|
716
|
+
const lead = unit === "min" ? `Every ${n} min` : n === 1 ? "Every hour" : `Every ${n} hours`;
|
|
717
|
+
const where = [df === "*" ? "" : daysLabel(df), `${clock(a)} to ${clock(b)}`].filter(Boolean);
|
|
718
|
+
const english = [`every ${unit === "min" ? `${n} minutes` : n === 1 ? "hour" : `${n} hours`}`, df === "*" ? "" : `on ${englishDays(list)}`, `between ${h24(a)} and ${h24(b)}`].filter(Boolean).join(" ");
|
|
719
|
+
return { ...measured(cronLines(times, `* * ${df}`), [lead, ...where].join(", ")), english, ...(zone ? { timezone: zone.trim() } : {}) };
|
|
651
720
|
}
|
|
652
|
-
|
|
721
|
+
if (spec.between !== undefined)
|
|
722
|
+
return specRefusal("between goes with every: 'every 30 minutes between 9:00 and 17:00'");
|
|
723
|
+
if (spec.at === undefined)
|
|
724
|
+
return specRefusal(`say when it runs: every with between, or at a time of day; give ${SCHEDULE_SPEC_SHAPE}`);
|
|
725
|
+
const t = typeof spec.at === "string" ? clockOf(spec.at) : null;
|
|
726
|
+
if (t === null)
|
|
727
|
+
return specRefusal(`at must be a time of day such as "08:00" or "8am"`);
|
|
728
|
+
const when = `at ${clock(t)}`;
|
|
729
|
+
const english = `${df === "*" ? "every day" : `on ${englishDays(list)}`} at ${h24(t)}`;
|
|
730
|
+
return { ...measured(cronLines([t], `* * ${df}`), df === "*" ? `Every day ${when}` : `${daysLabel(df)} ${when}`), english, ...(zone ? { timezone: zone.trim() } : {}) };
|
|
653
731
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidenhq/sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.19",
|
|
4
4
|
"description": "The shared code of AidenHQ modules: contract types, engine IO, the caller check and fence, write executors, the reviewer side, the judges' models and call, the source writer and the vendor MCP pass-through.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|