包文件→ .shtukbase/packages/automations/checks/t02_automation.ts
checks/t02_automation.ts
77 行, 3.6 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // ACT-R02. An automation says when, what and how often: a trigger of a known kind with what that kind needs, a
- // reaction that leads to an existing action or agent page, a mode, and for `mode: auto` explicit limits.
- import { main } from '../../collections/checks/lib/runner.ts';
- import { type Project, type Rec, type Result, addr, count, filled, map, pageExists, recordsOf, target, text } from '../../agents/lib/factlib.ts';
- export const RULE = {
- id: 'ACT-R02', title: 'An automation says when, what and how many times',
- scope: 'all automations',
- };
- /** Trigger kind → the keys it needs (one of them when the list has `|`). */
- const NEEDS: Record<string, string[][]> = {
- clock: [['at', 'every', 'cron']], schedule: [['at', 'every', 'cron']],
- change: [['within']], phase: [['contour'], ['phase']], sensor: [['sensor', 'watch']],
- 'run-finished': [['automation']], run: [['automation']],
- manual: [], hand: [], answer: [],
- };
- function trigger(r: Rec, res: Result, at: string): void {
- const t = map(r.fm.trigger);
- if (!t) {
- res.hole(at, 'no event (trigger)', 'write trigger: {kind: clock, at: "03:00"} or another kind', 'no-trigger');
- return;
- }
- const kind = text(t.kind);
- if (!(kind in NEEDS)) {
- res.hole(at, `event kind "${kind || '—'}" is unknown`, `set kind: ${Object.keys(NEEDS).join(' | ')}`, 'bad-trigger');
- return;
- }
- for (const any of NEEDS[kind])
- if (!any.some((k) => filled(t[k])))
- res.hole(at, `event ${kind} without ${any.join(' or ')}`, `add ${any.join(' or ')} to trigger`, 'bad-trigger');
- }
- function reaction(p: Project, r: Rec, res: Result, at: string): void {
- const re = map(r.fm.reaction);
- const ref = re ? (filled(re.action) ? re.action : re.agent) : null;
- if (!filled(ref)) {
- res.hole(at, 'no response (reaction.action or reaction.agent)', 'write reaction: {action: /actions/<name>}', 'no-reaction');
- return;
- }
- const t = target(r, ref);
- if (!t || !pageExists(p, t)) res.hole(at, `the response points to ${t ?? text(ref)}, but there is no such page`, 'fix the link or create the action page', 'missing-action');
- }
- function limits(r: Rec, res: Result, at: string): void {
- const mode = text(r.fm.mode);
- if (mode !== 'manual' && mode !== 'auto') {
- res.hole(at, `mode "${mode || '—'}"`, 'set mode: manual or auto', 'bad-mode');
- return;
- }
- const l = map(r.fm.limits);
- const hour = l ? count(l.perHour) : null;
- const day = l ? count(l.perDay) : null;
- if (l) for (const k of ['perHour', 'perDay'])
- if (filled(l[k]) && count(l[k]) === null) res.hole(at, `limits.${k}: "${text(l[k])}" is not a whole number above zero`, 'set a whole number', 'bad-limit');
- if (mode === 'auto' && hour === null && day === null)
- res.hole(at, 'the automation runs by itself, but there is no limit', 'write limits: {perHour: N, perDay: M}', 'no-limit');
- if (hour !== null && day !== null && hour > day)
- res.hole(at, `per hour (${hour}) is more than per day (${day})`, 'fix the limits', 'bad-limit');
- }
- export function check(p: Project, res: Result): void {
- const recs = recordsOf(p, 'AUTOMATION');
- res.scope = recs.map((r) => addr(r.id));
- res.scopeText = `automations ${p.coll('AUTOMATION') || '—'} (${recs.length})`;
- if (!res.part('automations', p.coll('AUTOMATION') ? null : 'no automations collection')) return;
- for (const r of recs) {
- const at = addr(r.id);
- if (text(r.fm.type) !== 'Automation') res.hole(at, `type "${text(r.fm.type) || '—'}", not Automation`, 'set type: Automation', 'bad-type');
- trigger(r, res, at);
- reaction(p, r, res, at);
- limits(r, res, at);
- }
- }
- if (import.meta.main) main({ RULE, check });