@camstack/types 1.2.542 → 1.2.543
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 +6 -0
- package/dist/automation/condition-eval.d.ts +13 -0
- package/dist/automation/hold.d.ts +171 -0
- package/dist/automation/index.d.ts +3 -1
- package/dist/automation/step-condition.d.ts +13 -2
- package/dist/automation/trace.d.ts +45 -0
- package/dist/automation/when.d.ts +35 -0
- package/dist/capabilities/actions.cap.d.ts +15 -0
- package/dist/capabilities/core-blocks.cap.d.ts +45 -0
- package/dist/capabilities/schedulers.cap.d.ts +15 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1052 -518
- package/dist/index.mjs +1042 -519
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -32418,6 +32418,316 @@ function zoneOnlySite(timeZone) {
|
|
|
32418
32418
|
};
|
|
32419
32419
|
}
|
|
32420
32420
|
//#endregion
|
|
32421
|
+
//#region src/automation/hold.ts
|
|
32422
|
+
/**
|
|
32423
|
+
* Hold semantics — "for N seconds" on a leaf of the `when` tree (#498).
|
|
32424
|
+
*
|
|
32425
|
+
* ONE primitive, `for: { ms, mode, tolerance? }`, shared by `device-becomes`,
|
|
32426
|
+
* `device-is` and `expression`:
|
|
32427
|
+
*
|
|
32428
|
+
* - `held`: the condition stays true for `ms`;
|
|
32429
|
+
* - `stable`: the value does not move by more than `tolerance` (absolute, in
|
|
32430
|
+
* the field's base unit) for `ms` while the condition holds.
|
|
32431
|
+
*
|
|
32432
|
+
* {@link HoldCore} is the pure state machine (an input observation → a reading).
|
|
32433
|
+
* {@link HoldTracker} keys cores by a leaf path for the STATE leaves the tree
|
|
32434
|
+
* evaluator judges. {@link BecomesWatch} wraps a core with a timer for the
|
|
32435
|
+
* `device-becomes` EVENT leaf: the deadline is armed when the hold starts and
|
|
32436
|
+
* cancelled when it breaks, and the leaf fires ONCE per episode.
|
|
32437
|
+
*
|
|
32438
|
+
* Hold state is memory only. A hold that is already running when the watcher
|
|
32439
|
+
* first looks (a restart) is INHERITED: nobody knows when it began, so it never
|
|
32440
|
+
* fires for that episode and a state leaf reads it as unknown until the window
|
|
32441
|
+
* has elapsed from the moment the watcher started (D49: not-known is not false).
|
|
32442
|
+
*/
|
|
32443
|
+
var HOLD_MAX_MS = 864e5;
|
|
32444
|
+
var HoldModeSchema = zod.z.enum(["held", "stable"]);
|
|
32445
|
+
var HoldSpecSchema = zod.z.object({
|
|
32446
|
+
/** How long the condition (or the value) must hold. */
|
|
32447
|
+
ms: zod.z.number().int().min(1).max(HOLD_MAX_MS),
|
|
32448
|
+
mode: HoldModeSchema,
|
|
32449
|
+
/** `stable` only: the largest move of the value that still counts as unchanged (absolute, base unit). */
|
|
32450
|
+
tolerance: zod.z.number().min(0).optional()
|
|
32451
|
+
});
|
|
32452
|
+
/** What a trace says about a hold. */
|
|
32453
|
+
var HoldTraceSchema = zod.z.object({
|
|
32454
|
+
phase: zod.z.enum([
|
|
32455
|
+
"pending",
|
|
32456
|
+
"cancelled",
|
|
32457
|
+
"fired",
|
|
32458
|
+
"reseeded"
|
|
32459
|
+
]),
|
|
32460
|
+
ms: zod.z.number().int(),
|
|
32461
|
+
mode: HoldModeSchema,
|
|
32462
|
+
/** When the hold started (ms epoch). */
|
|
32463
|
+
since: zod.z.number().optional(),
|
|
32464
|
+
/** When it completes (ms epoch), for a pending hold. */
|
|
32465
|
+
deadline: zod.z.number().optional()
|
|
32466
|
+
});
|
|
32467
|
+
/**
|
|
32468
|
+
* The ONE reader of a leaf's hold. A leaf stored before #498 carries `forMs`
|
|
32469
|
+
* (a `held` window); it reads as `for: { ms, mode: 'held' }`. `0` is no hold.
|
|
32470
|
+
*/
|
|
32471
|
+
function holdOf(leaf) {
|
|
32472
|
+
if (leaf.for !== void 0) return leaf.for;
|
|
32473
|
+
if (leaf.forMs !== void 0 && leaf.forMs > 0) return {
|
|
32474
|
+
ms: leaf.forMs,
|
|
32475
|
+
mode: "held"
|
|
32476
|
+
};
|
|
32477
|
+
}
|
|
32478
|
+
/** `30 s`, `5 min`, `1 h 5 min` — a hold window for a human. */
|
|
32479
|
+
function describeHoldMs(ms) {
|
|
32480
|
+
const s = Math.round(ms / 1e3);
|
|
32481
|
+
if (s < 60) return `${s} s`;
|
|
32482
|
+
const m = Math.floor(s / 60);
|
|
32483
|
+
const rest = s % 60;
|
|
32484
|
+
if (m < 60) return rest === 0 ? `${m} min` : `${m} min ${rest} s`;
|
|
32485
|
+
const h = Math.floor(m / 60);
|
|
32486
|
+
return m % 60 === 0 ? `${h} h` : `${h} h ${m % 60} min`;
|
|
32487
|
+
}
|
|
32488
|
+
/** The operators a `device-becomes` leaf may carry: a threshold or a range. */
|
|
32489
|
+
var BecomesOperatorSchema = zod.z.enum([
|
|
32490
|
+
"eq",
|
|
32491
|
+
"ne",
|
|
32492
|
+
"gt",
|
|
32493
|
+
"gte",
|
|
32494
|
+
"lt",
|
|
32495
|
+
"lte",
|
|
32496
|
+
"between",
|
|
32497
|
+
"outside"
|
|
32498
|
+
]);
|
|
32499
|
+
function compareValues(actual, operator, expected) {
|
|
32500
|
+
switch (operator) {
|
|
32501
|
+
case "eq": return actual === expected;
|
|
32502
|
+
case "ne": return actual !== expected;
|
|
32503
|
+
case "gt": return typeof actual === "number" && typeof expected === "number" && actual > expected;
|
|
32504
|
+
case "gte": return typeof actual === "number" && typeof expected === "number" && actual >= expected;
|
|
32505
|
+
case "lt": return typeof actual === "number" && typeof expected === "number" && actual < expected;
|
|
32506
|
+
case "lte": return typeof actual === "number" && typeof expected === "number" && actual <= expected;
|
|
32507
|
+
case "contains": return typeof actual === "string" && typeof expected === "string" && actual.includes(expected);
|
|
32508
|
+
case "in": return Array.isArray(expected) && expected.some((v) => v === actual);
|
|
32509
|
+
case "between":
|
|
32510
|
+
case "outside": {
|
|
32511
|
+
if (typeof actual !== "number" || !Array.isArray(expected)) return false;
|
|
32512
|
+
const [low, high] = expected;
|
|
32513
|
+
if (typeof low !== "number" || typeof high !== "number") return false;
|
|
32514
|
+
const inside = actual >= low && actual <= high;
|
|
32515
|
+
return operator === "between" ? inside : !inside;
|
|
32516
|
+
}
|
|
32517
|
+
}
|
|
32518
|
+
}
|
|
32519
|
+
function moved(value, anchor, tolerance) {
|
|
32520
|
+
if (typeof value === "number" && typeof anchor === "number") return Math.abs(value - anchor) > tolerance;
|
|
32521
|
+
return value !== anchor;
|
|
32522
|
+
}
|
|
32523
|
+
var HoldCore = class {
|
|
32524
|
+
spec;
|
|
32525
|
+
seen = false;
|
|
32526
|
+
since = null;
|
|
32527
|
+
inherited = false;
|
|
32528
|
+
anchor = void 0;
|
|
32529
|
+
constructor(spec) {
|
|
32530
|
+
this.spec = spec;
|
|
32531
|
+
}
|
|
32532
|
+
/**
|
|
32533
|
+
* One KNOWN observation. `holds` is whether the condition is true now; `value`
|
|
32534
|
+
* is the field (used by `stable`). A read that failed is not an observation —
|
|
32535
|
+
* the caller simply does not call this (D49: it never cancels a hold).
|
|
32536
|
+
*/
|
|
32537
|
+
observe(holds, value, now) {
|
|
32538
|
+
const first = !this.seen;
|
|
32539
|
+
this.seen = true;
|
|
32540
|
+
const wasRunning = this.since !== null;
|
|
32541
|
+
let restarted = false;
|
|
32542
|
+
if (!holds) {
|
|
32543
|
+
this.since = null;
|
|
32544
|
+
this.inherited = false;
|
|
32545
|
+
this.anchor = void 0;
|
|
32546
|
+
} else if (this.spec.mode === "held") {
|
|
32547
|
+
if (this.since === null) {
|
|
32548
|
+
this.since = now;
|
|
32549
|
+
this.inherited = first;
|
|
32550
|
+
restarted = true;
|
|
32551
|
+
}
|
|
32552
|
+
} else if (this.since === null || moved(value, this.anchor, this.spec.tolerance ?? 0)) {
|
|
32553
|
+
this.anchor = value;
|
|
32554
|
+
this.since = now;
|
|
32555
|
+
this.inherited = first;
|
|
32556
|
+
restarted = true;
|
|
32557
|
+
}
|
|
32558
|
+
const transition = !holds ? wasRunning ? "cancelled" : "none" : restarted ? wasRunning ? "cancelled" : "started" : "none";
|
|
32559
|
+
return {
|
|
32560
|
+
...this.read(now),
|
|
32561
|
+
transition
|
|
32562
|
+
};
|
|
32563
|
+
}
|
|
32564
|
+
/** The reading at `now` with no new observation. */
|
|
32565
|
+
read(now) {
|
|
32566
|
+
if (this.since === null) return {
|
|
32567
|
+
phase: "idle",
|
|
32568
|
+
since: null,
|
|
32569
|
+
remainingMs: this.spec.ms
|
|
32570
|
+
};
|
|
32571
|
+
const remainingMs = Math.max(0, this.since + this.spec.ms - now);
|
|
32572
|
+
if (remainingMs === 0) return {
|
|
32573
|
+
phase: "met",
|
|
32574
|
+
since: this.since,
|
|
32575
|
+
remainingMs: 0
|
|
32576
|
+
};
|
|
32577
|
+
return {
|
|
32578
|
+
phase: this.inherited ? "inherited" : "pending",
|
|
32579
|
+
since: this.since,
|
|
32580
|
+
remainingMs
|
|
32581
|
+
};
|
|
32582
|
+
}
|
|
32583
|
+
};
|
|
32584
|
+
/** Hold cores of the state leaves of one tree, keyed by the leaf's path. */
|
|
32585
|
+
var HoldTracker = class {
|
|
32586
|
+
cores = /* @__PURE__ */ new Map();
|
|
32587
|
+
observe(key, spec, input, now) {
|
|
32588
|
+
let core = this.cores.get(key);
|
|
32589
|
+
if (core === void 0) {
|
|
32590
|
+
core = new HoldCore(spec);
|
|
32591
|
+
this.cores.set(key, core);
|
|
32592
|
+
}
|
|
32593
|
+
return core.observe(input.holds, input.value, now);
|
|
32594
|
+
}
|
|
32595
|
+
};
|
|
32596
|
+
/** Is the leaf's predicate true for `value`? `undefined` for "any change" (no predicate). */
|
|
32597
|
+
function predicateOf(spec, value) {
|
|
32598
|
+
if (spec.becomes !== void 0) return value === spec.becomes;
|
|
32599
|
+
if (spec.operator !== void 0) return compareValues(value, spec.operator, spec.value);
|
|
32600
|
+
}
|
|
32601
|
+
/**
|
|
32602
|
+
* The runtime of a `device-becomes` leaf. It fires on a TRANSITION and never on a
|
|
32603
|
+
* re-report; with a hold it arms a timer for the deadline and fires once per
|
|
32604
|
+
* episode.
|
|
32605
|
+
*/
|
|
32606
|
+
var BecomesWatch = class {
|
|
32607
|
+
spec;
|
|
32608
|
+
deps;
|
|
32609
|
+
previous = void 0;
|
|
32610
|
+
seeded = false;
|
|
32611
|
+
core;
|
|
32612
|
+
handle = void 0;
|
|
32613
|
+
consumed = false;
|
|
32614
|
+
disposed = false;
|
|
32615
|
+
constructor(spec, deps) {
|
|
32616
|
+
this.spec = spec;
|
|
32617
|
+
this.deps = deps;
|
|
32618
|
+
const hold = spec.hold;
|
|
32619
|
+
this.core = hold === void 0 ? void 0 : new HoldCore(predicateOf(spec, void 0) === void 0 ? {
|
|
32620
|
+
...hold,
|
|
32621
|
+
mode: "stable"
|
|
32622
|
+
} : hold);
|
|
32623
|
+
}
|
|
32624
|
+
dispose() {
|
|
32625
|
+
this.disposed = true;
|
|
32626
|
+
this.disarm();
|
|
32627
|
+
}
|
|
32628
|
+
disarm() {
|
|
32629
|
+
if (this.handle !== void 0) this.deps.timers.clear(this.handle);
|
|
32630
|
+
this.handle = void 0;
|
|
32631
|
+
}
|
|
32632
|
+
trace(phase, reading) {
|
|
32633
|
+
const hold = this.spec.hold;
|
|
32634
|
+
const ms = hold?.ms ?? 0;
|
|
32635
|
+
const base = {
|
|
32636
|
+
phase,
|
|
32637
|
+
ms,
|
|
32638
|
+
mode: this.core === void 0 || hold === void 0 ? "held" : hold.mode
|
|
32639
|
+
};
|
|
32640
|
+
if (reading.since === null) return base;
|
|
32641
|
+
return {
|
|
32642
|
+
...base,
|
|
32643
|
+
since: reading.since,
|
|
32644
|
+
...phase === "pending" ? { deadline: reading.since + ms } : {}
|
|
32645
|
+
};
|
|
32646
|
+
}
|
|
32647
|
+
/** A KNOWN value. A failed read is not one: do not call this with it. */
|
|
32648
|
+
observe(value) {
|
|
32649
|
+
if (this.disposed) return;
|
|
32650
|
+
const now = this.deps.timers.now();
|
|
32651
|
+
const previous = this.previous;
|
|
32652
|
+
const first = !this.seeded;
|
|
32653
|
+
this.seeded = true;
|
|
32654
|
+
this.previous = value;
|
|
32655
|
+
const holds = predicateOf(this.spec, value);
|
|
32656
|
+
if (this.core === void 0) {
|
|
32657
|
+
if (first) return;
|
|
32658
|
+
if (holds === void 0 ? value !== previous : holds && predicateOf(this.spec, previous) !== true) this.deps.emit({
|
|
32659
|
+
kind: "fire",
|
|
32660
|
+
value
|
|
32661
|
+
});
|
|
32662
|
+
return;
|
|
32663
|
+
}
|
|
32664
|
+
const reading = this.core.observe(holds ?? true, value, now);
|
|
32665
|
+
if (reading.transition === "cancelled" || reading.phase === "idle") {
|
|
32666
|
+
if (reading.transition === "cancelled" && this.handle !== void 0) this.deps.emit({
|
|
32667
|
+
kind: "hold",
|
|
32668
|
+
value,
|
|
32669
|
+
hold: this.trace("cancelled", reading)
|
|
32670
|
+
});
|
|
32671
|
+
this.disarm();
|
|
32672
|
+
}
|
|
32673
|
+
if (reading.phase === "inherited") {
|
|
32674
|
+
this.consumed = true;
|
|
32675
|
+
if (reading.transition === "started") this.deps.emit({
|
|
32676
|
+
kind: "hold",
|
|
32677
|
+
value,
|
|
32678
|
+
hold: this.trace("reseeded", reading)
|
|
32679
|
+
});
|
|
32680
|
+
return;
|
|
32681
|
+
}
|
|
32682
|
+
if (reading.transition !== "none" && reading.phase === "pending") {
|
|
32683
|
+
this.consumed = false;
|
|
32684
|
+
this.disarm();
|
|
32685
|
+
this.handle = this.deps.timers.set(() => {
|
|
32686
|
+
this.due();
|
|
32687
|
+
}, reading.remainingMs);
|
|
32688
|
+
this.deps.emit({
|
|
32689
|
+
kind: "hold",
|
|
32690
|
+
value,
|
|
32691
|
+
hold: this.trace("pending", reading)
|
|
32692
|
+
});
|
|
32693
|
+
}
|
|
32694
|
+
}
|
|
32695
|
+
/** The deadline: re-read the field (a missed event must not fire a broken hold), then fire once. */
|
|
32696
|
+
async due() {
|
|
32697
|
+
this.handle = void 0;
|
|
32698
|
+
if (this.disposed || this.core === void 0) return;
|
|
32699
|
+
const read = await this.deps.read();
|
|
32700
|
+
if (this.disposed) return;
|
|
32701
|
+
if (read.ok) this.observe(read.value);
|
|
32702
|
+
if (this.handle !== void 0 || this.disposed) return;
|
|
32703
|
+
const now = this.deps.timers.now();
|
|
32704
|
+
const reading = this.core.read(now);
|
|
32705
|
+
if (reading.phase === "pending") {
|
|
32706
|
+
this.handle = this.deps.timers.set(() => {
|
|
32707
|
+
this.due();
|
|
32708
|
+
}, reading.remainingMs);
|
|
32709
|
+
return;
|
|
32710
|
+
}
|
|
32711
|
+
if (reading.phase !== "met" || this.consumed) return;
|
|
32712
|
+
this.consumed = true;
|
|
32713
|
+
this.deps.emit({
|
|
32714
|
+
kind: "fire",
|
|
32715
|
+
value: this.previous,
|
|
32716
|
+
hold: this.trace("fired", reading)
|
|
32717
|
+
});
|
|
32718
|
+
}
|
|
32719
|
+
};
|
|
32720
|
+
/** One line for a trace or a log: what a hold did. */
|
|
32721
|
+
function describeHold(hold) {
|
|
32722
|
+
const window = `${hold.mode === "stable" ? "stable" : "held"} for ${describeHoldMs(hold.ms)}`;
|
|
32723
|
+
switch (hold.phase) {
|
|
32724
|
+
case "pending": return `hold pending: ${window}${hold.deadline === void 0 ? "" : `, due ${new Date(hold.deadline).toISOString()}`}`;
|
|
32725
|
+
case "cancelled": return `hold cancelled: ${window} was broken before it completed`;
|
|
32726
|
+
case "fired": return `hold fired: ${window} completed`;
|
|
32727
|
+
case "reseeded": return `hold re-seeded: ${window} was already running when watching began, so it will not fire for this episode`;
|
|
32728
|
+
}
|
|
32729
|
+
}
|
|
32730
|
+
//#endregion
|
|
32421
32731
|
//#region src/automation/when.ts
|
|
32422
32732
|
/**
|
|
32423
32733
|
* The `when` tree — the ONE place an automation says when it runs.
|
|
@@ -32475,18 +32785,52 @@ var DeviceBecomesLeafSchema = zod.z.object({
|
|
|
32475
32785
|
deviceId: zod.z.number().int().nonnegative(),
|
|
32476
32786
|
cap: zod.z.string().min(1),
|
|
32477
32787
|
fieldPath: zod.z.string().min(1),
|
|
32478
|
-
/** Fire when the field takes this value. Omit to fire on any change. */
|
|
32788
|
+
/** Fire when the field takes this value. Omit (with no `operator`) to fire on any change. */
|
|
32479
32789
|
becomes: zod.z.union([
|
|
32480
32790
|
zod.z.string(),
|
|
32481
32791
|
zod.z.number(),
|
|
32482
32792
|
zod.z.boolean()
|
|
32483
32793
|
]).optional(),
|
|
32484
|
-
/**
|
|
32794
|
+
/**
|
|
32795
|
+
* A threshold or a range instead of an equality: "crosses above 3000", "enters
|
|
32796
|
+
* 18..24". The leaf fires on the TRANSITION into the predicate, never on a re-report.
|
|
32797
|
+
* `value` is in the field's base unit; `between` / `outside` take `[low, high]`.
|
|
32798
|
+
*/
|
|
32799
|
+
operator: BecomesOperatorSchema.optional(),
|
|
32800
|
+
value: zod.z.union([
|
|
32801
|
+
zod.z.number(),
|
|
32802
|
+
zod.z.string(),
|
|
32803
|
+
zod.z.boolean(),
|
|
32804
|
+
zod.z.array(zod.z.number()).length(2)
|
|
32805
|
+
]).optional(),
|
|
32806
|
+
/**
|
|
32807
|
+
* Hold: the transition (or, with no predicate, the new value) must stay for
|
|
32808
|
+
* `for.ms` before the leaf fires, ONCE per episode. See `hold.ts`.
|
|
32809
|
+
*/
|
|
32810
|
+
for: HoldSpecSchema.optional(),
|
|
32811
|
+
/** @deprecated every leaf fires on a transition now; read as always on. */
|
|
32485
32812
|
edge: zod.z.boolean().optional(),
|
|
32486
|
-
/**
|
|
32487
|
-
forMs: zod.z.number().int().min(0).max(
|
|
32813
|
+
/** @deprecated legacy `held` window — read through `holdOf` as `for: { ms, mode: 'held' }`. */
|
|
32814
|
+
forMs: zod.z.number().int().min(0).max(HOLD_MAX_MS).optional(),
|
|
32488
32815
|
/** Collapse a burst into one occurrence. */
|
|
32489
32816
|
debounceMs: zod.z.number().int().min(0).max(6e5).optional()
|
|
32817
|
+
}).superRefine((leaf, ctx) => {
|
|
32818
|
+
if (leaf.becomes !== void 0 && leaf.operator !== void 0) ctx.addIssue({
|
|
32819
|
+
code: "custom",
|
|
32820
|
+
message: "a leaf names `becomes` or `operator`, not both",
|
|
32821
|
+
path: ["operator"]
|
|
32822
|
+
});
|
|
32823
|
+
if (leaf.operator !== void 0 && leaf.value === void 0) ctx.addIssue({
|
|
32824
|
+
code: "custom",
|
|
32825
|
+
message: "an operator needs the value to compare to",
|
|
32826
|
+
path: ["value"]
|
|
32827
|
+
});
|
|
32828
|
+
const range = leaf.operator === "between" || leaf.operator === "outside";
|
|
32829
|
+
if (leaf.operator !== void 0 && range !== Array.isArray(leaf.value)) ctx.addIssue({
|
|
32830
|
+
code: "custom",
|
|
32831
|
+
message: range ? "a range takes [low, high]" : "only a range takes [low, high]",
|
|
32832
|
+
path: ["value"]
|
|
32833
|
+
});
|
|
32490
32834
|
});
|
|
32491
32835
|
var DeviceEventLeafSchema = zod.z.object({
|
|
32492
32836
|
kind: zod.z.literal("device-event"),
|
|
@@ -32551,7 +32895,9 @@ var DeviceIsLeafSchema = zod.z.object({
|
|
|
32551
32895
|
QuantityValueSchema
|
|
32552
32896
|
]),
|
|
32553
32897
|
/** The unit the person picked for `value` (display only; `value` is in the base unit). */
|
|
32554
|
-
unit: zod.z.string().min(1).optional()
|
|
32898
|
+
unit: zod.z.string().min(1).optional(),
|
|
32899
|
+
/** The comparison must hold (`held`) or the value stay put (`stable`) this long. */
|
|
32900
|
+
for: HoldSpecSchema.optional()
|
|
32555
32901
|
});
|
|
32556
32902
|
/**
|
|
32557
32903
|
* True when the camera currently has a PARKED object matching `subjects` in at
|
|
@@ -32574,7 +32920,9 @@ var StationaryPresentLeafSchema = zod.z.object({
|
|
|
32574
32920
|
var ExpressionLeafSchema = zod.z.object({
|
|
32575
32921
|
kind: zod.z.literal("expression"),
|
|
32576
32922
|
expr: zod.z.string().min(1).max(require_implemented_methods.MAX_EXPRESSION_SOURCE_LENGTH),
|
|
32577
|
-
bindings: zod.z.record(zod.z.string().regex(require_implemented_methods.EXPRESSION_IDENTIFIER_RE), ExpressionBindingSourceSchema)
|
|
32923
|
+
bindings: zod.z.record(zod.z.string().regex(require_implemented_methods.EXPRESSION_IDENTIFIER_RE), ExpressionBindingSourceSchema),
|
|
32924
|
+
/** The expression must stay true this long. An expression has no single value, so only `held`. */
|
|
32925
|
+
for: HoldSpecSchema.optional()
|
|
32578
32926
|
}).superRefine((src, ctx) => {
|
|
32579
32927
|
const err = require_implemented_methods.validateExpressionSource(src);
|
|
32580
32928
|
if (err !== null) ctx.addIssue({
|
|
@@ -32582,6 +32930,11 @@ var ExpressionLeafSchema = zod.z.object({
|
|
|
32582
32930
|
message: err,
|
|
32583
32931
|
path: ["expr"]
|
|
32584
32932
|
});
|
|
32933
|
+
if (src.for?.mode === "stable") ctx.addIssue({
|
|
32934
|
+
code: "custom",
|
|
32935
|
+
message: "an expression is held, not stable: it has no single value to keep still",
|
|
32936
|
+
path: ["for", "mode"]
|
|
32937
|
+
});
|
|
32585
32938
|
});
|
|
32586
32939
|
var AutomationWhenSchema = zod.z.lazy(() => zod.z.discriminatedUnion("kind", [
|
|
32587
32940
|
zod.z.object({
|
|
@@ -33151,6 +33504,535 @@ function validateRecipeBounds(recipe) {
|
|
|
33151
33504
|
return validateWhenTree(recipe.when);
|
|
33152
33505
|
}
|
|
33153
33506
|
//#endregion
|
|
33507
|
+
//#region src/automation/condition-eval.ts
|
|
33508
|
+
/**
|
|
33509
|
+
* Evaluating an automation's condition tree against the ConditionMirror.
|
|
33510
|
+
*
|
|
33511
|
+
* D49: a trigger is a one-shot event, so its gate must never hang on a read
|
|
33512
|
+
* that can fail. Every field a condition leaf names is therefore kept in
|
|
33513
|
+
* memory — filled at start, updated off device state-changed events, reconciled
|
|
33514
|
+
* on a timer — and evaluation reads that memory ONLY. A value the mirror does
|
|
33515
|
+
* not hold is UNKNOWN, which is `fail` with a reason, never a throw and never a
|
|
33516
|
+
* `pass`. A failed refresh changes nothing: the previous value stays.
|
|
33517
|
+
*
|
|
33518
|
+
* The evaluator returns a node per tree position (containers, leaves, and the
|
|
33519
|
+
* ones a short-circuit never reached) with a stable path and a human reason, so
|
|
33520
|
+
* the editor can paint each block.
|
|
33521
|
+
*/
|
|
33522
|
+
/**
|
|
33523
|
+
* The key a `stableId → device id` map answers on. A pair (`addonId`, `stableId`)
|
|
33524
|
+
* is a device; a bare stableId is only a device while no other addon shares it,
|
|
33525
|
+
* so the map's producer puts the bare key in ONLY when it is unique.
|
|
33526
|
+
*/
|
|
33527
|
+
function stableIdKey(stableId, addonId) {
|
|
33528
|
+
return addonId === void 0 ? stableId : `${addonId}\u0000${stableId}`;
|
|
33529
|
+
}
|
|
33530
|
+
/**
|
|
33531
|
+
* The `stableIdKey → device id` map a mirror refresh hands back, from a device
|
|
33532
|
+
* listing. Every device is reachable by its (addonId, stableId) pair; the bare
|
|
33533
|
+
* stableId is offered only while NO other device shares it, so a legacy binding
|
|
33534
|
+
* that names no addon reads nothing rather than the wrong device.
|
|
33535
|
+
*/
|
|
33536
|
+
function stableIdMapOf(devices) {
|
|
33537
|
+
const out = /* @__PURE__ */ new Map();
|
|
33538
|
+
const bare = /* @__PURE__ */ new Map();
|
|
33539
|
+
for (const d of devices) {
|
|
33540
|
+
if (d.stableId === void 0) continue;
|
|
33541
|
+
if (d.addonId !== void 0) out.set(stableIdKey(d.stableId, d.addonId), d.id);
|
|
33542
|
+
bare.set(d.stableId, bare.has(d.stableId) ? null : d.id);
|
|
33543
|
+
}
|
|
33544
|
+
for (const [stableId, id] of bare) if (id !== null) out.set(stableId, id);
|
|
33545
|
+
return out;
|
|
33546
|
+
}
|
|
33547
|
+
/** The cap whose runtime-state slice IS the occupancy snapshot, parked objects included. */
|
|
33548
|
+
var ZONE_ANALYTICS_CAP = "zone-analytics";
|
|
33549
|
+
function collectInto(cond, out) {
|
|
33550
|
+
switch (cond.kind) {
|
|
33551
|
+
case "all":
|
|
33552
|
+
case "any":
|
|
33553
|
+
for (const child of cond.children) collectInto(child, out);
|
|
33554
|
+
return;
|
|
33555
|
+
case "not":
|
|
33556
|
+
collectInto(cond.child, out);
|
|
33557
|
+
return;
|
|
33558
|
+
case "device-is":
|
|
33559
|
+
out.push({
|
|
33560
|
+
kind: "device",
|
|
33561
|
+
deviceId: cond.deviceId,
|
|
33562
|
+
cap: cond.cap
|
|
33563
|
+
});
|
|
33564
|
+
return;
|
|
33565
|
+
case "stationary-present":
|
|
33566
|
+
out.push({
|
|
33567
|
+
kind: "device",
|
|
33568
|
+
deviceId: cond.deviceId,
|
|
33569
|
+
cap: ZONE_ANALYTICS_CAP
|
|
33570
|
+
});
|
|
33571
|
+
return;
|
|
33572
|
+
case "expression":
|
|
33573
|
+
for (const binding of Object.values(cond.bindings)) if (binding.kind === "global") out.push({
|
|
33574
|
+
kind: "stable",
|
|
33575
|
+
stableId: binding.sourceStableId,
|
|
33576
|
+
cap: binding.cap,
|
|
33577
|
+
...binding.addonId !== void 0 ? { addonId: binding.addonId } : {}
|
|
33578
|
+
});
|
|
33579
|
+
return;
|
|
33580
|
+
default: return;
|
|
33581
|
+
}
|
|
33582
|
+
}
|
|
33583
|
+
/** Every (device, cap) a `when` tree's STATE leaves read, deduplicated. */
|
|
33584
|
+
function collectWhenMirrorSources(cond) {
|
|
33585
|
+
if (cond === void 0) return [];
|
|
33586
|
+
const all = [];
|
|
33587
|
+
collectInto(cond, all);
|
|
33588
|
+
const seen = /* @__PURE__ */ new Set();
|
|
33589
|
+
return all.filter((s) => {
|
|
33590
|
+
const key = s.kind === "device" ? `d|${s.deviceId}|${s.cap}` : `s|${stableIdKey(s.stableId, s.addonId)}|${s.cap}`;
|
|
33591
|
+
if (seen.has(key)) return false;
|
|
33592
|
+
seen.add(key);
|
|
33593
|
+
return true;
|
|
33594
|
+
});
|
|
33595
|
+
}
|
|
33596
|
+
/** Legacy name, for blocks generated before the single tree: an old condition tree. */
|
|
33597
|
+
function collectMirrorSources(cond) {
|
|
33598
|
+
return collectWhenMirrorSources(cond === void 0 ? void 0 : convertLegacyCondition(cond));
|
|
33599
|
+
}
|
|
33600
|
+
function sliceKey(deviceId, cap) {
|
|
33601
|
+
return `${deviceId}|${cap}`;
|
|
33602
|
+
}
|
|
33603
|
+
function pick$1(slice, fieldPath) {
|
|
33604
|
+
let cursor = slice;
|
|
33605
|
+
for (const part of fieldPath.split(".")) {
|
|
33606
|
+
if (cursor === null || typeof cursor !== "object") return void 0;
|
|
33607
|
+
cursor = Reflect.get(cursor, part);
|
|
33608
|
+
}
|
|
33609
|
+
return cursor;
|
|
33610
|
+
}
|
|
33611
|
+
function isSliceObject(value) {
|
|
33612
|
+
return typeof value === "object" && value !== null;
|
|
33613
|
+
}
|
|
33614
|
+
var ConditionMirror = class {
|
|
33615
|
+
sources;
|
|
33616
|
+
slices = /* @__PURE__ */ new Map();
|
|
33617
|
+
stableIds = /* @__PURE__ */ new Map();
|
|
33618
|
+
watched;
|
|
33619
|
+
constructor(sources) {
|
|
33620
|
+
this.sources = sources;
|
|
33621
|
+
this.watched = new Set(sources.filter((s) => s.kind === "device").map((s) => sliceKey(s.deviceId, s.cap)));
|
|
33622
|
+
}
|
|
33623
|
+
/** A `device.state-changed` event. Slices nobody reads are ignored. */
|
|
33624
|
+
apply(deviceId, cap, slice) {
|
|
33625
|
+
const key = sliceKey(deviceId, cap);
|
|
33626
|
+
if (!isSliceObject(slice)) return;
|
|
33627
|
+
if (!this.watched.has(key) && !this.isStableTarget(deviceId, cap)) return;
|
|
33628
|
+
this.slices.set(key, slice);
|
|
33629
|
+
}
|
|
33630
|
+
isStableTarget(deviceId, cap) {
|
|
33631
|
+
for (const s of this.sources) if (s.kind === "stable" && s.cap === cap && this.stableIds.get(stableIdKey(s.stableId, s.addonId)) === deviceId) return true;
|
|
33632
|
+
return false;
|
|
33633
|
+
}
|
|
33634
|
+
read(deviceId, cap, fieldPath) {
|
|
33635
|
+
const slice = this.slices.get(sliceKey(deviceId, cap));
|
|
33636
|
+
return slice === void 0 ? void 0 : pick$1(slice, fieldPath);
|
|
33637
|
+
}
|
|
33638
|
+
/** The whole slice the mirror holds for a device cap — `undefined` when it holds none. */
|
|
33639
|
+
sliceOf(deviceId, cap) {
|
|
33640
|
+
return this.slices.get(sliceKey(deviceId, cap));
|
|
33641
|
+
}
|
|
33642
|
+
readStable(stableId, cap, fieldPath, addonId) {
|
|
33643
|
+
const deviceId = this.stableIds.get(stableIdKey(stableId, addonId));
|
|
33644
|
+
return deviceId === void 0 ? void 0 : this.read(deviceId, cap, fieldPath);
|
|
33645
|
+
}
|
|
33646
|
+
/** Fill / reconcile. A read that fails or answers nothing keeps the old value. */
|
|
33647
|
+
async refresh(fetchers) {
|
|
33648
|
+
let updated = 0;
|
|
33649
|
+
let kept = 0;
|
|
33650
|
+
if (this.sources.some((s) => s.kind === "stable")) try {
|
|
33651
|
+
this.stableIds = await fetchers.fetchStableIds();
|
|
33652
|
+
} catch {}
|
|
33653
|
+
const targets = /* @__PURE__ */ new Map();
|
|
33654
|
+
for (const s of this.sources) {
|
|
33655
|
+
const deviceId = s.kind === "device" ? s.deviceId : this.stableIds.get(stableIdKey(s.stableId, s.addonId));
|
|
33656
|
+
if (deviceId === void 0) {
|
|
33657
|
+
kept += 1;
|
|
33658
|
+
continue;
|
|
33659
|
+
}
|
|
33660
|
+
targets.set(sliceKey(deviceId, s.cap), {
|
|
33661
|
+
deviceId,
|
|
33662
|
+
cap: s.cap
|
|
33663
|
+
});
|
|
33664
|
+
}
|
|
33665
|
+
for (const [key, target] of targets) try {
|
|
33666
|
+
const slice = await fetchers.fetchSlice(target.deviceId, target.cap);
|
|
33667
|
+
if (isSliceObject(slice)) {
|
|
33668
|
+
this.slices.set(key, slice);
|
|
33669
|
+
updated += 1;
|
|
33670
|
+
} else kept += 1;
|
|
33671
|
+
} catch {
|
|
33672
|
+
kept += 1;
|
|
33673
|
+
}
|
|
33674
|
+
return {
|
|
33675
|
+
updated,
|
|
33676
|
+
kept
|
|
33677
|
+
};
|
|
33678
|
+
}
|
|
33679
|
+
};
|
|
33680
|
+
var OPERATOR_SYMBOL = {
|
|
33681
|
+
eq: "=",
|
|
33682
|
+
ne: "≠",
|
|
33683
|
+
gt: ">",
|
|
33684
|
+
gte: "≥",
|
|
33685
|
+
lt: "<",
|
|
33686
|
+
lte: "≤",
|
|
33687
|
+
contains: "contains",
|
|
33688
|
+
in: "in"
|
|
33689
|
+
};
|
|
33690
|
+
var compare = compareValues;
|
|
33691
|
+
function toTraceValue(value) {
|
|
33692
|
+
if (value === void 0 || value === null) return null;
|
|
33693
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
33694
|
+
if (Array.isArray(value) && value.every((v) => typeof v === "string" || typeof v === "number")) return value;
|
|
33695
|
+
return JSON.stringify(value);
|
|
33696
|
+
}
|
|
33697
|
+
function show(value) {
|
|
33698
|
+
return Array.isArray(value) ? `[${value.join(", ")}]` : String(value);
|
|
33699
|
+
}
|
|
33700
|
+
function markSkipped(cond, path, why, out) {
|
|
33701
|
+
out.push({
|
|
33702
|
+
path,
|
|
33703
|
+
kind: cond.kind,
|
|
33704
|
+
outcome: "not-evaluated",
|
|
33705
|
+
reason: why
|
|
33706
|
+
});
|
|
33707
|
+
if (cond.kind === "all" || cond.kind === "any") cond.children.forEach((c, i) => {
|
|
33708
|
+
markSkipped(c, `${path}.${i}`, why, out);
|
|
33709
|
+
});
|
|
33710
|
+
else if (cond.kind === "not") markSkipped(cond.child, `${path}.0`, why, out);
|
|
33711
|
+
}
|
|
33712
|
+
function parkedObjectsOf(value) {
|
|
33713
|
+
if (!Array.isArray(value)) return [];
|
|
33714
|
+
const rows = [];
|
|
33715
|
+
for (const item of value) {
|
|
33716
|
+
const id = Reflect.get(Object(item), "id");
|
|
33717
|
+
const className = Reflect.get(Object(item), "className");
|
|
33718
|
+
if (typeof id === "string" && typeof className === "string") rows.push({
|
|
33719
|
+
id,
|
|
33720
|
+
className
|
|
33721
|
+
});
|
|
33722
|
+
}
|
|
33723
|
+
return rows;
|
|
33724
|
+
}
|
|
33725
|
+
/** `zoneId`s whose `trackIds` list the parked object (`stationary:<id>`, as the snapshot builds it). */
|
|
33726
|
+
function zonesOfParked(zones, parkedId) {
|
|
33727
|
+
if (!Array.isArray(zones)) return [];
|
|
33728
|
+
const out = [];
|
|
33729
|
+
for (const zone of zones) {
|
|
33730
|
+
const zoneId = Reflect.get(Object(zone), "zoneId");
|
|
33731
|
+
const trackIds = Reflect.get(Object(zone), "trackIds");
|
|
33732
|
+
if (typeof zoneId === "string" && Array.isArray(trackIds) && trackIds.includes(`stationary:${parkedId}`)) out.push(zoneId);
|
|
33733
|
+
}
|
|
33734
|
+
return out;
|
|
33735
|
+
}
|
|
33736
|
+
/**
|
|
33737
|
+
* Is a matching parked object there NOW? Read off the mirrored zone-analytics
|
|
33738
|
+
* snapshot: `frame` is always present in a snapshot, so its absence means the
|
|
33739
|
+
* slice was never read (UNKNOWN, a fail that says so), while a snapshot without
|
|
33740
|
+
* `stationaryObjects` is a definite "nothing parked" (the provider omits an
|
|
33741
|
+
* empty list).
|
|
33742
|
+
*/
|
|
33743
|
+
function judgeStationaryPresent(cond, reader) {
|
|
33744
|
+
if (reader.read(cond.deviceId, ZONE_ANALYTICS_CAP, "frame") === void 0) return {
|
|
33745
|
+
pass: false,
|
|
33746
|
+
why: `device ${cond.deviceId} occupancy snapshot unknown`,
|
|
33747
|
+
unknown: true
|
|
33748
|
+
};
|
|
33749
|
+
const parked = parkedObjectsOf(reader.read(cond.deviceId, ZONE_ANALYTICS_CAP, "stationaryObjects"));
|
|
33750
|
+
if (parked.length === 0) return {
|
|
33751
|
+
pass: false,
|
|
33752
|
+
why: `no parked objects on device ${cond.deviceId}`
|
|
33753
|
+
};
|
|
33754
|
+
const zones = reader.read(cond.deviceId, ZONE_ANALYTICS_CAP, "zones");
|
|
33755
|
+
const matching = [];
|
|
33756
|
+
for (const obj of parked) {
|
|
33757
|
+
if (cond.subjects !== void 0 && !anySubjectMatches(cond.subjects, { className: obj.className }).matches) continue;
|
|
33758
|
+
if (cond.zones !== void 0) {
|
|
33759
|
+
const inZones = zonesOfParked(zones, obj.id);
|
|
33760
|
+
if (!cond.zones.some((z) => inZones.includes(z))) continue;
|
|
33761
|
+
}
|
|
33762
|
+
matching.push(obj);
|
|
33763
|
+
}
|
|
33764
|
+
const needed = cond.minCount ?? 1;
|
|
33765
|
+
const first = matching[0];
|
|
33766
|
+
if (first !== void 0 && matching.length >= needed) return {
|
|
33767
|
+
pass: true,
|
|
33768
|
+
why: needed === 1 ? `parked ${first.className} (${first.id}) on device ${cond.deviceId}` : `${matching.length} parked matching on device ${cond.deviceId} (need ${needed})`
|
|
33769
|
+
};
|
|
33770
|
+
if (matching.length > 0) return {
|
|
33771
|
+
pass: false,
|
|
33772
|
+
why: `only ${matching.length} of ${needed} needed parked objects match on device ${cond.deviceId}`
|
|
33773
|
+
};
|
|
33774
|
+
return {
|
|
33775
|
+
pass: false,
|
|
33776
|
+
why: `${parked.length} parked on device ${cond.deviceId}, none matching the class/zone filter`
|
|
33777
|
+
};
|
|
33778
|
+
}
|
|
33779
|
+
function noteDue(state, hold) {
|
|
33780
|
+
if (hold?.due === void 0) return;
|
|
33781
|
+
state.holdDueInMs = state.holdDueInMs === void 0 ? hold.due : Math.min(state.holdDueInMs, hold.due);
|
|
33782
|
+
}
|
|
33783
|
+
/** What a state leaf's hold says, given whether the leaf's own condition is true NOW. */
|
|
33784
|
+
function judgeHold(hold, reading, conditionTrue) {
|
|
33785
|
+
const window = `${hold.mode === "stable" ? "stable" : "held"} for ${describeHoldMs(hold.ms)}`;
|
|
33786
|
+
if (reading === void 0) return {
|
|
33787
|
+
pass: false,
|
|
33788
|
+
unknown: true,
|
|
33789
|
+
why: `${window}: nothing keeps this leaf's history here`
|
|
33790
|
+
};
|
|
33791
|
+
switch (reading.phase) {
|
|
33792
|
+
case "met": return {
|
|
33793
|
+
pass: true,
|
|
33794
|
+
unknown: false,
|
|
33795
|
+
why: `${window}: met`
|
|
33796
|
+
};
|
|
33797
|
+
case "pending": return {
|
|
33798
|
+
due: reading.remainingMs,
|
|
33799
|
+
pass: false,
|
|
33800
|
+
unknown: false,
|
|
33801
|
+
why: `${window}: ${describeHoldMs(hold.ms - reading.remainingMs)} so far`
|
|
33802
|
+
};
|
|
33803
|
+
case "inherited": return {
|
|
33804
|
+
pass: false,
|
|
33805
|
+
unknown: true,
|
|
33806
|
+
why: `${window}: it was already true when watching began, so how long it has held is not known`
|
|
33807
|
+
};
|
|
33808
|
+
case "idle": return {
|
|
33809
|
+
pass: false,
|
|
33810
|
+
unknown: false,
|
|
33811
|
+
why: conditionTrue ? `${window}: just changed` : `${window}: the condition is not true`
|
|
33812
|
+
};
|
|
33813
|
+
}
|
|
33814
|
+
}
|
|
33815
|
+
function evalEventLeaf(cond, path, state, out) {
|
|
33816
|
+
const occurred = state.firedPath === path;
|
|
33817
|
+
out.push({
|
|
33818
|
+
path,
|
|
33819
|
+
kind: cond.kind,
|
|
33820
|
+
outcome: occurred ? "pass" : "fail",
|
|
33821
|
+
reason: occurred ? `${cond.kind} occurred → pass` : `${cond.kind} is an instant and did not occur now → fail`
|
|
33822
|
+
});
|
|
33823
|
+
return occurred;
|
|
33824
|
+
}
|
|
33825
|
+
function evalNode(cond, path, reader, now, out, state) {
|
|
33826
|
+
if (isEventLeaf(cond)) return evalEventLeaf(cond, path, state, out);
|
|
33827
|
+
switch (cond.kind) {
|
|
33828
|
+
case "device-is": {
|
|
33829
|
+
const observed = reader.read(cond.deviceId, cond.cap, cond.fieldPath);
|
|
33830
|
+
const base = {
|
|
33831
|
+
path,
|
|
33832
|
+
kind: cond.kind,
|
|
33833
|
+
deviceId: cond.deviceId,
|
|
33834
|
+
cap: cond.cap,
|
|
33835
|
+
fieldPath: cond.fieldPath,
|
|
33836
|
+
operator: cond.operator,
|
|
33837
|
+
expected: toTraceValue(cond.value),
|
|
33838
|
+
observed: toTraceValue(observed)
|
|
33839
|
+
};
|
|
33840
|
+
if (isQuantityValue(cond.value)) {
|
|
33841
|
+
out.push({
|
|
33842
|
+
...base,
|
|
33843
|
+
outcome: "fail",
|
|
33844
|
+
reason: `${cond.fieldPath} expected value's unit was never converted → fail`
|
|
33845
|
+
});
|
|
33846
|
+
return false;
|
|
33847
|
+
}
|
|
33848
|
+
if (observed === void 0) {
|
|
33849
|
+
state.unknown = true;
|
|
33850
|
+
out.push({
|
|
33851
|
+
...base,
|
|
33852
|
+
outcome: "fail",
|
|
33853
|
+
reason: `${cond.fieldPath} value unknown → fail`
|
|
33854
|
+
});
|
|
33855
|
+
return false;
|
|
33856
|
+
}
|
|
33857
|
+
const compared = compare(observed, cond.operator, cond.value);
|
|
33858
|
+
const hold = cond.for === void 0 ? void 0 : judgeHold(cond.for, state.holds?.observe(path, cond.for, {
|
|
33859
|
+
holds: compared,
|
|
33860
|
+
value: observed
|
|
33861
|
+
}, now), compared);
|
|
33862
|
+
if (hold?.unknown === true) state.unknown = true;
|
|
33863
|
+
noteDue(state, hold);
|
|
33864
|
+
const ok = compared && (hold === void 0 || hold.pass);
|
|
33865
|
+
out.push({
|
|
33866
|
+
...base,
|
|
33867
|
+
outcome: ok ? "pass" : "fail",
|
|
33868
|
+
reason: `${cond.fieldPath} ${show(observed)}, expected ${OPERATOR_SYMBOL[cond.operator]} ${show(cond.value)}${hold === void 0 ? "" : ` (${hold.why})`} → ${ok ? "pass" : "fail"}`
|
|
33869
|
+
});
|
|
33870
|
+
return ok;
|
|
33871
|
+
}
|
|
33872
|
+
case "stationary-present": {
|
|
33873
|
+
const verdict = judgeStationaryPresent(cond, reader);
|
|
33874
|
+
if (verdict.unknown === true) state.unknown = true;
|
|
33875
|
+
out.push({
|
|
33876
|
+
path,
|
|
33877
|
+
kind: cond.kind,
|
|
33878
|
+
outcome: verdict.pass ? "pass" : "fail",
|
|
33879
|
+
reason: `${verdict.why} → ${verdict.pass ? "pass" : "fail"}`,
|
|
33880
|
+
deviceId: cond.deviceId,
|
|
33881
|
+
cap: ZONE_ANALYTICS_CAP
|
|
33882
|
+
});
|
|
33883
|
+
return verdict.pass;
|
|
33884
|
+
}
|
|
33885
|
+
case "expression": {
|
|
33886
|
+
const values = {};
|
|
33887
|
+
for (const [alias, binding] of Object.entries(cond.bindings)) {
|
|
33888
|
+
if (binding.kind === "literal") {
|
|
33889
|
+
values[alias] = require_implemented_methods.toExpressionValue(binding.value) ?? null;
|
|
33890
|
+
continue;
|
|
33891
|
+
}
|
|
33892
|
+
const raw = binding.kind === "global" ? reader.readStable(binding.sourceStableId, binding.cap, binding.fieldPath, binding.addonId) : void 0;
|
|
33893
|
+
if (raw === void 0) {
|
|
33894
|
+
out.push({
|
|
33895
|
+
path,
|
|
33896
|
+
kind: cond.kind,
|
|
33897
|
+
outcome: "fail",
|
|
33898
|
+
reason: `${alias} value unknown → fail`
|
|
33899
|
+
});
|
|
33900
|
+
state.unknown = true;
|
|
33901
|
+
return false;
|
|
33902
|
+
}
|
|
33903
|
+
values[alias] = require_implemented_methods.toExpressionValue(raw) ?? null;
|
|
33904
|
+
}
|
|
33905
|
+
const result = require_implemented_methods.evaluateExpressionSource(cond.expr, values, now);
|
|
33906
|
+
if (!result.ok) {
|
|
33907
|
+
out.push({
|
|
33908
|
+
path,
|
|
33909
|
+
kind: cond.kind,
|
|
33910
|
+
outcome: "fail",
|
|
33911
|
+
reason: `${cond.expr}: ${result.error} → fail`
|
|
33912
|
+
});
|
|
33913
|
+
return false;
|
|
33914
|
+
}
|
|
33915
|
+
const truth = result.value === true;
|
|
33916
|
+
const hold = cond.for === void 0 ? void 0 : judgeHold(cond.for, state.holds?.observe(path, cond.for, {
|
|
33917
|
+
holds: truth,
|
|
33918
|
+
value: truth
|
|
33919
|
+
}, now), truth);
|
|
33920
|
+
if (hold?.unknown === true) state.unknown = true;
|
|
33921
|
+
noteDue(state, hold);
|
|
33922
|
+
const ok = truth && (hold === void 0 || hold.pass);
|
|
33923
|
+
out.push({
|
|
33924
|
+
path,
|
|
33925
|
+
kind: cond.kind,
|
|
33926
|
+
outcome: ok ? "pass" : "fail",
|
|
33927
|
+
reason: `${cond.expr} with ${JSON.stringify(values)}${hold === void 0 ? "" : ` (${hold.why})`} → ${ok ? "pass" : "fail"}`
|
|
33928
|
+
});
|
|
33929
|
+
return ok;
|
|
33930
|
+
}
|
|
33931
|
+
case "not": {
|
|
33932
|
+
const at = out.length;
|
|
33933
|
+
out.push({
|
|
33934
|
+
path,
|
|
33935
|
+
kind: "not",
|
|
33936
|
+
outcome: "not-evaluated",
|
|
33937
|
+
reason: ""
|
|
33938
|
+
});
|
|
33939
|
+
const inner = evalNode(cond.child, `${path}.0`, reader, now, out, state);
|
|
33940
|
+
const ok = !inner;
|
|
33941
|
+
out.splice(at, 1, {
|
|
33942
|
+
path,
|
|
33943
|
+
kind: "not",
|
|
33944
|
+
outcome: ok ? "pass" : "fail",
|
|
33945
|
+
reason: `not (${inner ? "pass" : "fail"}) → ${ok ? "pass" : "fail"}`
|
|
33946
|
+
});
|
|
33947
|
+
return ok;
|
|
33948
|
+
}
|
|
33949
|
+
case "all":
|
|
33950
|
+
case "any": {
|
|
33951
|
+
const isAll = cond.kind === "all";
|
|
33952
|
+
const at = out.length;
|
|
33953
|
+
out.push({
|
|
33954
|
+
path,
|
|
33955
|
+
kind: cond.kind,
|
|
33956
|
+
outcome: "not-evaluated",
|
|
33957
|
+
reason: ""
|
|
33958
|
+
});
|
|
33959
|
+
let decided = null;
|
|
33960
|
+
for (let i = 0; i < cond.children.length; i += 1) {
|
|
33961
|
+
const child = cond.children[i];
|
|
33962
|
+
if (child === void 0) continue;
|
|
33963
|
+
if (decided !== null) {
|
|
33964
|
+
markSkipped(child, `${path}.${i}`, `short-circuited by ${path}.${decided}`, out);
|
|
33965
|
+
continue;
|
|
33966
|
+
}
|
|
33967
|
+
const ok = evalNode(child, `${path}.${i}`, reader, now, out, state);
|
|
33968
|
+
if (isAll ? !ok : ok) decided = i;
|
|
33969
|
+
}
|
|
33970
|
+
const ok = isAll ? decided === null : decided !== null;
|
|
33971
|
+
const reason = isAll ? ok ? `all ${cond.children.length} passed → pass` : `${path}.${decided} failed → fail` : ok ? `${path}.${decided} passed → pass` : `none of ${cond.children.length} passed → fail`;
|
|
33972
|
+
out.splice(at, 1, {
|
|
33973
|
+
path,
|
|
33974
|
+
kind: cond.kind,
|
|
33975
|
+
outcome: ok ? "pass" : "fail",
|
|
33976
|
+
reason
|
|
33977
|
+
});
|
|
33978
|
+
return ok;
|
|
33979
|
+
}
|
|
33980
|
+
}
|
|
33981
|
+
}
|
|
33982
|
+
/** Evaluate a `when` tree: the fired event leaf is true, every other event leaf false. */
|
|
33983
|
+
function evaluateWhenTree(when, reader, now, options = {}) {
|
|
33984
|
+
const nodes = [];
|
|
33985
|
+
const state = {
|
|
33986
|
+
firedPath: options.firedPath,
|
|
33987
|
+
holds: options.holds,
|
|
33988
|
+
unknown: false,
|
|
33989
|
+
holdDueInMs: void 0
|
|
33990
|
+
};
|
|
33991
|
+
return {
|
|
33992
|
+
pass: evalNode(when, options.rootPath ?? "when", reader, now, nodes, state),
|
|
33993
|
+
unknown: state.unknown,
|
|
33994
|
+
nodes,
|
|
33995
|
+
...state.holdDueInMs === void 0 ? {} : { holdDueInMs: state.holdDueInMs }
|
|
33996
|
+
};
|
|
33997
|
+
}
|
|
33998
|
+
/** Does any state leaf of the tree carry a `for`? */
|
|
33999
|
+
function hasHoldLeaves(when) {
|
|
34000
|
+
let found = false;
|
|
34001
|
+
forEachLeaf(when, (leaf) => {
|
|
34002
|
+
if ((leaf.kind === "device-is" || leaf.kind === "expression") && leaf.for !== void 0) found = true;
|
|
34003
|
+
});
|
|
34004
|
+
return found;
|
|
34005
|
+
}
|
|
34006
|
+
/**
|
|
34007
|
+
* Feed the clock of EVERY `for` leaf, each on its own: a short-circuit in the
|
|
34008
|
+
* tree (an event leaf that is false right now) must not stop a hold from
|
|
34009
|
+
* counting. Returns the soonest a pending hold completes (ms), or undefined.
|
|
34010
|
+
*/
|
|
34011
|
+
function observeHoldLeaves(when, reader, now, holds, rootPath = "when") {
|
|
34012
|
+
let due;
|
|
34013
|
+
forEachLeaf(when, (leaf, path) => {
|
|
34014
|
+
if (leaf.kind !== "device-is" && leaf.kind !== "expression" || leaf.for === void 0) return;
|
|
34015
|
+
const state = {
|
|
34016
|
+
firedPath: void 0,
|
|
34017
|
+
holds,
|
|
34018
|
+
unknown: false,
|
|
34019
|
+
holdDueInMs: void 0
|
|
34020
|
+
};
|
|
34021
|
+
evalNode(leaf, path, reader, now, [], state);
|
|
34022
|
+
if (state.holdDueInMs !== void 0) due = due === void 0 ? state.holdDueInMs : Math.min(due, state.holdDueInMs);
|
|
34023
|
+
}, rootPath);
|
|
34024
|
+
return due;
|
|
34025
|
+
}
|
|
34026
|
+
/** Legacy name, for blocks generated before the single tree. No tree is a pass with no nodes. */
|
|
34027
|
+
function evaluateConditionTree(cond, reader, now) {
|
|
34028
|
+
if (cond === void 0) return {
|
|
34029
|
+
pass: true,
|
|
34030
|
+
unknown: false,
|
|
34031
|
+
nodes: []
|
|
34032
|
+
};
|
|
34033
|
+
return evaluateWhenTree(convertLegacyCondition(cond), reader, now, { rootPath: "conditions" });
|
|
34034
|
+
}
|
|
34035
|
+
//#endregion
|
|
33154
34036
|
//#region src/automation/trigger-variables.ts
|
|
33155
34037
|
var DETECTION_DESCRIPTIONS = {
|
|
33156
34038
|
deviceId: "the camera that saw it",
|
|
@@ -33290,44 +34172,67 @@ function emitLeaf(trigger, path) {
|
|
|
33290
34172
|
),
|
|
33291
34173
|
)`;
|
|
33292
34174
|
case "device-becomes": {
|
|
33293
|
-
const
|
|
34175
|
+
const hold = holdOf(trigger);
|
|
33294
34176
|
return ` // event leaf ${path}: device state — the mirror is the authority, the
|
|
33295
34177
|
// event only WAKES the evaluation, so a dropped event delays and never loses.
|
|
34178
|
+
// It fires on a TRANSITION (never on a re-report or the reconcile); a hold is
|
|
34179
|
+
// a timer armed when the transition happens and fired ONCE per episode.
|
|
33296
34180
|
{
|
|
33297
|
-
let previous: unknown = undefined
|
|
33298
34181
|
let debounce: ReturnType<typeof setTimeout> | undefined
|
|
33299
|
-
|
|
34182
|
+
const watch = new BecomesWatch(${j({
|
|
34183
|
+
...trigger.becomes === void 0 ? {} : { becomes: trigger.becomes },
|
|
34184
|
+
...trigger.operator === void 0 ? {} : {
|
|
34185
|
+
operator: trigger.operator,
|
|
34186
|
+
value: trigger.value
|
|
34187
|
+
},
|
|
34188
|
+
...hold === void 0 ? {} : { hold }
|
|
34189
|
+
})}, {
|
|
34190
|
+
timers: {
|
|
34191
|
+
set: (fn, ms) => setTimeout(fn, ms),
|
|
34192
|
+
clear: (handle) => clearTimeout(handle),
|
|
34193
|
+
now: () => Date.now(),
|
|
34194
|
+
},
|
|
34195
|
+
read: () => readField(${j(trigger.deviceId)}, ${j(trigger.cap)}, ${j(trigger.fieldPath)}),
|
|
34196
|
+
emit: (effect) => {
|
|
34197
|
+
const occurrence = {
|
|
34198
|
+
kind: 'device-becomes',
|
|
34199
|
+
leaf: ${at},
|
|
34200
|
+
at: Date.now(),
|
|
34201
|
+
deviceId: ${j(trigger.deviceId)},
|
|
34202
|
+
cap: ${j(trigger.cap)},
|
|
34203
|
+
field: ${j(trigger.fieldPath)},
|
|
34204
|
+
to: toTraceScalar(effect.value),
|
|
34205
|
+
...(effect.hold === undefined ? {} : { hold: effect.hold }),
|
|
34206
|
+
}
|
|
34207
|
+
if (effect.kind === 'fire') {
|
|
34208
|
+
if (effect.hold !== undefined) {
|
|
34209
|
+
ctx.logger.info(describeHold(effect.hold), { tags: { deviceId: ${j(trigger.deviceId)} }, leaf: ${at} })
|
|
34210
|
+
}
|
|
34211
|
+
void run(occurrence, effect.hold === undefined ? 'fired' : describeHold(effect.hold))
|
|
34212
|
+
return
|
|
34213
|
+
}
|
|
34214
|
+
ctx.logger.debug(describeHold(effect.hold), { tags: { deviceId: ${j(trigger.deviceId)} }, leaf: ${at} })
|
|
34215
|
+
record(
|
|
34216
|
+
skipTrace(occurrence, describeHold(effect.hold), 'skipped-trigger', undefined, effect.hold.phase === 'pending'),
|
|
34217
|
+
)
|
|
34218
|
+
},
|
|
34219
|
+
})
|
|
34220
|
+
disposers.push(() => watch.dispose())
|
|
33300
34221
|
const check = async (): Promise<void> => {
|
|
33301
|
-
const
|
|
33302
|
-
|
|
33303
|
-
|
|
33304
|
-
const changed = value !== previous
|
|
33305
|
-
previous = value
|
|
33306
|
-
if (!matches) { heldSince = undefined; return }
|
|
33307
|
-
if (${trigger.edge === true ? "true" : "false"} && !changed) return
|
|
33308
|
-
const forMs = ${j(trigger.forMs ?? 0)}
|
|
33309
|
-
if (forMs > 0) {
|
|
33310
|
-
if (heldSince === undefined) { heldSince = Date.now(); return }
|
|
33311
|
-
if (Date.now() - heldSince < forMs) return
|
|
33312
|
-
}
|
|
33313
|
-
heldSince = undefined
|
|
33314
|
-
void run({
|
|
33315
|
-
kind: 'device-becomes',
|
|
33316
|
-
leaf: ${at},
|
|
33317
|
-
at: Date.now(),
|
|
33318
|
-
deviceId: ${j(trigger.deviceId)},
|
|
33319
|
-
cap: ${j(trigger.cap)},
|
|
33320
|
-
field: ${j(trigger.fieldPath)},
|
|
33321
|
-
to: value,
|
|
33322
|
-
})
|
|
34222
|
+
const read = await readField(${j(trigger.deviceId)}, ${j(trigger.cap)}, ${j(trigger.fieldPath)})
|
|
34223
|
+
if (!read.ok) return // a failed read changes nothing, and never cancels a hold (D49)
|
|
34224
|
+
watch.observe(read.value)
|
|
33323
34225
|
}
|
|
33324
34226
|
const wake = (): void => {
|
|
33325
34227
|
if (debounce !== undefined) clearTimeout(debounce)
|
|
33326
34228
|
debounce = setTimeout(() => { void check() }, ${j(trigger.debounceMs ?? 0)})
|
|
33327
34229
|
}
|
|
34230
|
+
disposers.push(() => { if (debounce !== undefined) clearTimeout(debounce) })
|
|
33328
34231
|
disposers.push(
|
|
33329
34232
|
ctx.eventBus.subscribe({ category: 'device.state-changed', deviceId: ${j(trigger.deviceId)} }, wake),
|
|
33330
34233
|
)
|
|
34234
|
+
// Seed what the field holds NOW without firing: a restart is not a transition.
|
|
34235
|
+
void check()
|
|
33331
34236
|
const reconcile = setInterval(() => { void check() }, RECONCILE_MS)
|
|
33332
34237
|
disposers.push(() => clearInterval(reconcile))
|
|
33333
34238
|
}`;
|
|
@@ -33413,20 +34318,20 @@ function emitLeafBody(action) {
|
|
|
33413
34318
|
}
|
|
33414
34319
|
}
|
|
33415
34320
|
/** A step's `if` (#472) and its else list, as the fields of a RunnableStep. */
|
|
33416
|
-
function emitGuard(action) {
|
|
34321
|
+
function emitGuard(action, index) {
|
|
33417
34322
|
if (action.kind !== "wait" && action.kind !== "cap") return "";
|
|
33418
34323
|
if (action.if === void 0) return "";
|
|
33419
34324
|
const otherwise = (action.else ?? []).map((el) => {
|
|
33420
34325
|
const body = emitLeafBody(el);
|
|
33421
34326
|
return `{ meta: ${j(stepMetaOf(el))}, run: async (scope) => {\n${body ?? ""}\n } }`;
|
|
33422
34327
|
}).join(", ");
|
|
33423
|
-
return ` guard: () => evaluateCondition(${j(action.if)}),${otherwise === "" ? "" : `\n otherwise: [${otherwise}],`}
|
|
34328
|
+
return ` guard: () => evaluateCondition(${j(action.if)}, ${j(`actions.${index}.if`)}),${otherwise === "" ? "" : `\n otherwise: [${otherwise}],`}
|
|
33424
34329
|
`;
|
|
33425
34330
|
}
|
|
33426
34331
|
function emitAction(action, index) {
|
|
33427
34332
|
const step = (body) => ` {
|
|
33428
34333
|
meta: STEP_META[${index}]!,
|
|
33429
|
-
${emitGuard(action)} run: async (scope) => {
|
|
34334
|
+
${emitGuard(action, index)} run: async (scope) => {
|
|
33430
34335
|
${body}
|
|
33431
34336
|
},
|
|
33432
34337
|
},`;
|
|
@@ -33466,6 +34371,10 @@ function generateAutomationBlock(input) {
|
|
|
33466
34371
|
const runMode = recipe.runMode ?? { mode: "single" };
|
|
33467
34372
|
const stepMeta = recipe.actions.map(stepMetaOf);
|
|
33468
34373
|
const hasDomainTarget = recipe.actions.some((a) => a.kind === "cap" && normalizeCapStep(a).target.kind === "domain");
|
|
34374
|
+
const guardHoldTrees = recipe.actions.flatMap((a, i) => (a.kind === "wait" || a.kind === "cap") && a.if !== void 0 && hasHoldLeaves(a.if) ? [{
|
|
34375
|
+
path: `actions.${i}.if`,
|
|
34376
|
+
when: a.if
|
|
34377
|
+
}] : []);
|
|
33469
34378
|
const eventLeaves = [];
|
|
33470
34379
|
forEachLeaf(recipe.when, (leaf, path) => {
|
|
33471
34380
|
if (isEventLeaf(leaf)) eventLeaves.push({
|
|
@@ -33489,18 +34398,22 @@ function generateAutomationBlock(input) {
|
|
|
33489
34398
|
import {
|
|
33490
34399
|
ACTION_TARGET_INVALIDATION_EVENTS,
|
|
33491
34400
|
ActionTargetResolver,
|
|
34401
|
+
BecomesWatch,
|
|
33492
34402
|
ConditionMirror,
|
|
34403
|
+
HoldTracker,
|
|
33493
34404
|
DetectionTriggerFilter,
|
|
33494
34405
|
EventCategory,
|
|
33495
34406
|
RunGate,
|
|
33496
34407
|
buildTriggerTrace,
|
|
33497
34408
|
collectWhenMirrorSources,
|
|
33498
34409
|
coreBlockIdFromAddonId,
|
|
34410
|
+
describeHold,
|
|
33499
34411
|
evaluateWhenTree,
|
|
33500
34412
|
evaluateStationaryTrigger,
|
|
33501
34413
|
isSkippedTrace,
|
|
33502
34414
|
mergeSkipTraces,
|
|
33503
34415
|
nextOccurrence,
|
|
34416
|
+
observeHoldLeaves,
|
|
33504
34417
|
stableIdMapOf,
|
|
33505
34418
|
resolveStepArgs,
|
|
33506
34419
|
resolveStepArgsWithTemplates,
|
|
@@ -33545,6 +34458,12 @@ const STEP_META: ReadonlyArray<{ kind: string; cap?: string; method?: string; ac
|
|
|
33545
34458
|
/** True when a step acts on a whole domain: only then does the block listen for registry changes. */
|
|
33546
34459
|
const HAS_DOMAIN_TARGET = ${hasDomainTarget}
|
|
33547
34460
|
|
|
34461
|
+
/** A state leaf of the tree carries a \`for\`: its clock is fed on every mirror update, so it counts while nothing fires. */
|
|
34462
|
+
const HAS_HOLD = ${hasHoldLeaves(recipe.when)}
|
|
34463
|
+
|
|
34464
|
+
/** Guards (a step's \`if\`) with a \`for\`: they keep their own mirror and clocks, fed the same way. */
|
|
34465
|
+
const GUARD_HOLD_TREES: ReadonlyArray<{ path: string; when: AutomationWhen }> = ${j(guardHoldTrees)}
|
|
34466
|
+
|
|
33548
34467
|
/** setTimeout cannot arm past 2^31-1 ms; a longer wait re-arms on wake. */
|
|
33549
34468
|
const MAX_TIMER_MS = 2_000_000_000
|
|
33550
34469
|
|
|
@@ -33601,6 +34520,12 @@ export default function block(ctx: AddonContext) {
|
|
|
33601
34520
|
const gate = new RunGate(RUN_MODE)
|
|
33602
34521
|
const MIRROR_SOURCES = collectWhenMirrorSources(WHEN)
|
|
33603
34522
|
const mirror = new ConditionMirror(MIRROR_SOURCES)
|
|
34523
|
+
const holds = new HoldTracker()
|
|
34524
|
+
const guardMirror = new ConditionMirror(
|
|
34525
|
+
collectWhenMirrorSources({ kind: 'all', children: GUARD_HOLD_TREES.map((t) => t.when) }),
|
|
34526
|
+
)
|
|
34527
|
+
const guardHolds = new HoldTracker()
|
|
34528
|
+
let holdTimer: ReturnType<typeof setTimeout> | undefined
|
|
33604
34529
|
const BLOCK_ID = coreBlockIdFromAddonId(ctx.id)
|
|
33605
34530
|
/** The cooldown stamp: memory is the authority at fire time (D49); the hub ledger is read once at start. */
|
|
33606
34531
|
let lastRunStartedAt: number | null = null
|
|
@@ -33710,6 +34635,40 @@ export default function block(ctx: AddonContext) {
|
|
|
33710
34635
|
if (report.kept > 0) {
|
|
33711
34636
|
ctx.logger.debug('condition mirror kept stale values for sources it could not read', report)
|
|
33712
34637
|
}
|
|
34638
|
+
if (GUARD_HOLD_TREES.length > 0) await guardMirror.refresh(mirrorFetchers())
|
|
34639
|
+
}
|
|
34640
|
+
|
|
34641
|
+
function mirrorFetchers() {
|
|
34642
|
+
return {
|
|
34643
|
+
fetchSlice: (deviceId: number, cap: string) => ctx.api.deviceState.getCapSlice.query({ deviceId, capName: cap }),
|
|
34644
|
+
fetchStableIds: async () => {
|
|
34645
|
+
const all = await ctx.api.deviceManager.listAll.query({})
|
|
34646
|
+
return stableIdMapOf(all)
|
|
34647
|
+
},
|
|
34648
|
+
}
|
|
34649
|
+
}
|
|
34650
|
+
|
|
34651
|
+
/** Feed the clock of every \`for\` (the tree's and the guards'), and arm a timer for the
|
|
34652
|
+
* soonest pending one so a hold completes ON TIME, not at the next reconcile. */
|
|
34653
|
+
function feedHolds(): void {
|
|
34654
|
+
if (stopped) return
|
|
34655
|
+
const now = Date.now()
|
|
34656
|
+
const dues: number[] = []
|
|
34657
|
+
if (HAS_HOLD) {
|
|
34658
|
+
const due = observeHoldLeaves(WHEN, mirror, now, holds)
|
|
34659
|
+
if (due !== undefined) dues.push(due)
|
|
34660
|
+
}
|
|
34661
|
+
for (const tree of GUARD_HOLD_TREES) {
|
|
34662
|
+
const due = observeHoldLeaves(tree.when, guardMirror, now, guardHolds, tree.path)
|
|
34663
|
+
if (due !== undefined) dues.push(due)
|
|
34664
|
+
}
|
|
34665
|
+
if (holdTimer !== undefined) clearTimeout(holdTimer)
|
|
34666
|
+
holdTimer = undefined
|
|
34667
|
+
if (dues.length === 0) return
|
|
34668
|
+
holdTimer = setTimeout(() => {
|
|
34669
|
+
feedHolds()
|
|
34670
|
+
evaluateStateTree()
|
|
34671
|
+
}, Math.min(Math.max(Math.min(...dues), 0), MAX_TIMER_MS))
|
|
33713
34672
|
}
|
|
33714
34673
|
|
|
33715
34674
|
/** The next moment a schedule leaf fires, from the CURRENT site. A sun spec on
|
|
@@ -33740,28 +34699,39 @@ export default function block(ctx: AddonContext) {
|
|
|
33740
34699
|
}
|
|
33741
34700
|
}
|
|
33742
34701
|
|
|
33743
|
-
/** Read one cap field off the state mirror.
|
|
34702
|
+
/** Read one cap field off the state mirror. \`ok: false\` when the device,
|
|
33744
34703
|
* the cap or the field cannot be resolved — the caller decides what that
|
|
33745
|
-
* means
|
|
33746
|
-
*
|
|
34704
|
+
* means (the device-state trigger changes nothing), and it is logged at
|
|
34705
|
+
* debug with the device. (Conditions read the ConditionMirror instead.) */
|
|
33747
34706
|
async function readField(
|
|
33748
34707
|
deviceId: number,
|
|
33749
34708
|
cap: string,
|
|
33750
34709
|
fieldPath: string,
|
|
33751
|
-
): Promise<unknown> {
|
|
34710
|
+
): Promise<{ readonly ok: true; readonly value: unknown } | { readonly ok: false }> {
|
|
33752
34711
|
try {
|
|
33753
34712
|
const slice = await ctx.api.deviceState.getCapSlice.query({ deviceId, capName: cap })
|
|
33754
34713
|
let cursor: unknown = slice
|
|
33755
34714
|
for (const part of fieldPath.split('.')) {
|
|
33756
|
-
if (cursor === null || typeof cursor !== 'object') return
|
|
33757
|
-
cursor = (cursor
|
|
34715
|
+
if (cursor === null || typeof cursor !== 'object') return { ok: false }
|
|
34716
|
+
cursor = Reflect.get(cursor, part)
|
|
33758
34717
|
}
|
|
33759
|
-
return
|
|
33760
|
-
|
|
33761
|
-
|
|
34718
|
+
if (cursor === undefined) return { ok: false }
|
|
34719
|
+
return { ok: true, value: cursor }
|
|
34720
|
+
} catch (err: unknown) {
|
|
34721
|
+
ctx.logger.debug('device state read failed — the leaf keeps what it knew', {
|
|
34722
|
+
tags: { deviceId },
|
|
34723
|
+
cap,
|
|
34724
|
+
error: err instanceof Error ? err.message : String(err),
|
|
34725
|
+
})
|
|
34726
|
+
return { ok: false }
|
|
33762
34727
|
}
|
|
33763
34728
|
}
|
|
33764
34729
|
|
|
34730
|
+
/** A trigger var keeps primitives only. */
|
|
34731
|
+
function toTraceScalar(value: unknown): string | number | boolean | null {
|
|
34732
|
+
return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ? value : null
|
|
34733
|
+
}
|
|
34734
|
+
|
|
33765
34735
|
/** WHO a step acts on (D983): picked devices, a \`$var\`, or a whole domain
|
|
33766
34736
|
* resolved NOW. The cache is dropped on every registry event that can change
|
|
33767
34737
|
* it; an empty answer is logged by the resolver and traced as \`empty\`. */
|
|
@@ -33865,14 +34835,11 @@ export default function block(ctx: AddonContext) {
|
|
|
33865
34835
|
}
|
|
33866
34836
|
|
|
33867
34837
|
/** A step's \`if\` (#472), read on a mirror filled for exactly its leaves, NOW (D49: unread = unknown). */
|
|
33868
|
-
function evaluateCondition(when: AutomationWhen) {
|
|
33869
|
-
|
|
33870
|
-
|
|
33871
|
-
|
|
33872
|
-
|
|
33873
|
-
return stableIdMapOf(all)
|
|
33874
|
-
},
|
|
33875
|
-
})
|
|
34838
|
+
function evaluateCondition(when: AutomationWhen, path: string) {
|
|
34839
|
+
const history = GUARD_HOLD_TREES.some((t) => t.path === path)
|
|
34840
|
+
? { mirror: guardMirror, holds: guardHolds, rootPath: path }
|
|
34841
|
+
: undefined
|
|
34842
|
+
return evaluateStepCondition(when, mirrorFetchers(), Date.now, history)
|
|
33876
34843
|
}
|
|
33877
34844
|
|
|
33878
34845
|
/** The THEN half, through the ONE shared step runner: stops at the first
|
|
@@ -33959,6 +34926,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
33959
34926
|
try {
|
|
33960
34927
|
const firedLeaf = trigger['leaf']
|
|
33961
34928
|
const evaluation = evaluateWhenTree(WHEN, mirror, Date.now(), {
|
|
34929
|
+
holds,
|
|
33962
34930
|
...(typeof firedLeaf === 'string' ? { firedPath: firedLeaf } : {}),
|
|
33963
34931
|
})
|
|
33964
34932
|
nodes = [...evaluation.nodes]
|
|
@@ -34073,6 +35041,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
34073
35041
|
const ready: Promise<void> = (async () => {
|
|
34074
35042
|
await loadState()
|
|
34075
35043
|
await refreshMirror()
|
|
35044
|
+
feedHolds()
|
|
34076
35045
|
stateWatching = true
|
|
34077
35046
|
evaluateStateTree()
|
|
34078
35047
|
})()
|
|
@@ -34087,7 +35056,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
34087
35056
|
let stateWatching = false
|
|
34088
35057
|
function evaluateStateTree(): void {
|
|
34089
35058
|
if (!STATE_ONLY || !stateWatching || stopped) return
|
|
34090
|
-
const evaluation = evaluateWhenTree(WHEN, mirror, Date.now())
|
|
35059
|
+
const evaluation = evaluateWhenTree(WHEN, mirror, Date.now(), { holds })
|
|
34091
35060
|
if (evaluation.unknown) return
|
|
34092
35061
|
const was = treeWasTrue
|
|
34093
35062
|
treeWasTrue = evaluation.pass
|
|
@@ -34096,7 +35065,7 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
34096
35065
|
}
|
|
34097
35066
|
}
|
|
34098
35067
|
|
|
34099
|
-
if (MIRROR_SOURCES.length > 0) {
|
|
35068
|
+
if (MIRROR_SOURCES.length > 0 || GUARD_HOLD_TREES.length > 0) {
|
|
34100
35069
|
disposers.push(
|
|
34101
35070
|
ctx.eventBus.subscribe({ category: EventCategory.DeviceStateChanged }, (event) => {
|
|
34102
35071
|
const data = event.data
|
|
@@ -34104,12 +35073,17 @@ ${recipe.actions.map((a, i) => emitAction(a, i)).join("\n")}
|
|
|
34104
35073
|
const capName = data['capName']
|
|
34105
35074
|
if (typeof deviceId === 'number' && typeof capName === 'string') {
|
|
34106
35075
|
mirror.apply(deviceId, capName, data['slice'])
|
|
35076
|
+
guardMirror.apply(deviceId, capName, data['slice'])
|
|
35077
|
+
feedHolds()
|
|
34107
35078
|
evaluateStateTree()
|
|
34108
35079
|
}
|
|
34109
35080
|
}),
|
|
34110
35081
|
)
|
|
34111
35082
|
const mirrorReconcile = setInterval(() => {
|
|
34112
|
-
void refreshMirror().then(
|
|
35083
|
+
void refreshMirror().then(() => {
|
|
35084
|
+
feedHolds()
|
|
35085
|
+
evaluateStateTree()
|
|
35086
|
+
})
|
|
34113
35087
|
}, RECONCILE_MS)
|
|
34114
35088
|
disposers.push(() => clearInterval(mirrorReconcile))
|
|
34115
35089
|
}
|
|
@@ -34118,6 +35092,7 @@ ${eventLeaves.map(({ leaf, path }) => emitLeaf(leaf, path)).join("\n\n")}
|
|
|
34118
35092
|
|
|
34119
35093
|
return () => {
|
|
34120
35094
|
stopped = true
|
|
35095
|
+
if (holdTimer !== undefined) clearTimeout(holdTimer)
|
|
34121
35096
|
queue.length = 0
|
|
34122
35097
|
flushSkip()
|
|
34123
35098
|
for (const running of runningTokens) {
|
|
@@ -34130,465 +35105,6 @@ ${eventLeaves.map(({ leaf, path }) => emitLeaf(leaf, path)).join("\n\n")}
|
|
|
34130
35105
|
`;
|
|
34131
35106
|
}
|
|
34132
35107
|
//#endregion
|
|
34133
|
-
//#region src/automation/condition-eval.ts
|
|
34134
|
-
/**
|
|
34135
|
-
* Evaluating an automation's condition tree against the ConditionMirror.
|
|
34136
|
-
*
|
|
34137
|
-
* D49: a trigger is a one-shot event, so its gate must never hang on a read
|
|
34138
|
-
* that can fail. Every field a condition leaf names is therefore kept in
|
|
34139
|
-
* memory — filled at start, updated off device state-changed events, reconciled
|
|
34140
|
-
* on a timer — and evaluation reads that memory ONLY. A value the mirror does
|
|
34141
|
-
* not hold is UNKNOWN, which is `fail` with a reason, never a throw and never a
|
|
34142
|
-
* `pass`. A failed refresh changes nothing: the previous value stays.
|
|
34143
|
-
*
|
|
34144
|
-
* The evaluator returns a node per tree position (containers, leaves, and the
|
|
34145
|
-
* ones a short-circuit never reached) with a stable path and a human reason, so
|
|
34146
|
-
* the editor can paint each block.
|
|
34147
|
-
*/
|
|
34148
|
-
/**
|
|
34149
|
-
* The key a `stableId → device id` map answers on. A pair (`addonId`, `stableId`)
|
|
34150
|
-
* is a device; a bare stableId is only a device while no other addon shares it,
|
|
34151
|
-
* so the map's producer puts the bare key in ONLY when it is unique.
|
|
34152
|
-
*/
|
|
34153
|
-
function stableIdKey(stableId, addonId) {
|
|
34154
|
-
return addonId === void 0 ? stableId : `${addonId}\u0000${stableId}`;
|
|
34155
|
-
}
|
|
34156
|
-
/**
|
|
34157
|
-
* The `stableIdKey → device id` map a mirror refresh hands back, from a device
|
|
34158
|
-
* listing. Every device is reachable by its (addonId, stableId) pair; the bare
|
|
34159
|
-
* stableId is offered only while NO other device shares it, so a legacy binding
|
|
34160
|
-
* that names no addon reads nothing rather than the wrong device.
|
|
34161
|
-
*/
|
|
34162
|
-
function stableIdMapOf(devices) {
|
|
34163
|
-
const out = /* @__PURE__ */ new Map();
|
|
34164
|
-
const bare = /* @__PURE__ */ new Map();
|
|
34165
|
-
for (const d of devices) {
|
|
34166
|
-
if (d.stableId === void 0) continue;
|
|
34167
|
-
if (d.addonId !== void 0) out.set(stableIdKey(d.stableId, d.addonId), d.id);
|
|
34168
|
-
bare.set(d.stableId, bare.has(d.stableId) ? null : d.id);
|
|
34169
|
-
}
|
|
34170
|
-
for (const [stableId, id] of bare) if (id !== null) out.set(stableId, id);
|
|
34171
|
-
return out;
|
|
34172
|
-
}
|
|
34173
|
-
/** The cap whose runtime-state slice IS the occupancy snapshot, parked objects included. */
|
|
34174
|
-
var ZONE_ANALYTICS_CAP = "zone-analytics";
|
|
34175
|
-
function collectInto(cond, out) {
|
|
34176
|
-
switch (cond.kind) {
|
|
34177
|
-
case "all":
|
|
34178
|
-
case "any":
|
|
34179
|
-
for (const child of cond.children) collectInto(child, out);
|
|
34180
|
-
return;
|
|
34181
|
-
case "not":
|
|
34182
|
-
collectInto(cond.child, out);
|
|
34183
|
-
return;
|
|
34184
|
-
case "device-is":
|
|
34185
|
-
out.push({
|
|
34186
|
-
kind: "device",
|
|
34187
|
-
deviceId: cond.deviceId,
|
|
34188
|
-
cap: cond.cap
|
|
34189
|
-
});
|
|
34190
|
-
return;
|
|
34191
|
-
case "stationary-present":
|
|
34192
|
-
out.push({
|
|
34193
|
-
kind: "device",
|
|
34194
|
-
deviceId: cond.deviceId,
|
|
34195
|
-
cap: ZONE_ANALYTICS_CAP
|
|
34196
|
-
});
|
|
34197
|
-
return;
|
|
34198
|
-
case "expression":
|
|
34199
|
-
for (const binding of Object.values(cond.bindings)) if (binding.kind === "global") out.push({
|
|
34200
|
-
kind: "stable",
|
|
34201
|
-
stableId: binding.sourceStableId,
|
|
34202
|
-
cap: binding.cap,
|
|
34203
|
-
...binding.addonId !== void 0 ? { addonId: binding.addonId } : {}
|
|
34204
|
-
});
|
|
34205
|
-
return;
|
|
34206
|
-
default: return;
|
|
34207
|
-
}
|
|
34208
|
-
}
|
|
34209
|
-
/** Every (device, cap) a `when` tree's STATE leaves read, deduplicated. */
|
|
34210
|
-
function collectWhenMirrorSources(cond) {
|
|
34211
|
-
if (cond === void 0) return [];
|
|
34212
|
-
const all = [];
|
|
34213
|
-
collectInto(cond, all);
|
|
34214
|
-
const seen = /* @__PURE__ */ new Set();
|
|
34215
|
-
return all.filter((s) => {
|
|
34216
|
-
const key = s.kind === "device" ? `d|${s.deviceId}|${s.cap}` : `s|${stableIdKey(s.stableId, s.addonId)}|${s.cap}`;
|
|
34217
|
-
if (seen.has(key)) return false;
|
|
34218
|
-
seen.add(key);
|
|
34219
|
-
return true;
|
|
34220
|
-
});
|
|
34221
|
-
}
|
|
34222
|
-
/** Legacy name, for blocks generated before the single tree: an old condition tree. */
|
|
34223
|
-
function collectMirrorSources(cond) {
|
|
34224
|
-
return collectWhenMirrorSources(cond === void 0 ? void 0 : convertLegacyCondition(cond));
|
|
34225
|
-
}
|
|
34226
|
-
function sliceKey(deviceId, cap) {
|
|
34227
|
-
return `${deviceId}|${cap}`;
|
|
34228
|
-
}
|
|
34229
|
-
function pick$1(slice, fieldPath) {
|
|
34230
|
-
let cursor = slice;
|
|
34231
|
-
for (const part of fieldPath.split(".")) {
|
|
34232
|
-
if (cursor === null || typeof cursor !== "object") return void 0;
|
|
34233
|
-
cursor = Reflect.get(cursor, part);
|
|
34234
|
-
}
|
|
34235
|
-
return cursor;
|
|
34236
|
-
}
|
|
34237
|
-
function isSliceObject(value) {
|
|
34238
|
-
return typeof value === "object" && value !== null;
|
|
34239
|
-
}
|
|
34240
|
-
var ConditionMirror = class {
|
|
34241
|
-
sources;
|
|
34242
|
-
slices = /* @__PURE__ */ new Map();
|
|
34243
|
-
stableIds = /* @__PURE__ */ new Map();
|
|
34244
|
-
watched;
|
|
34245
|
-
constructor(sources) {
|
|
34246
|
-
this.sources = sources;
|
|
34247
|
-
this.watched = new Set(sources.filter((s) => s.kind === "device").map((s) => sliceKey(s.deviceId, s.cap)));
|
|
34248
|
-
}
|
|
34249
|
-
/** A `device.state-changed` event. Slices nobody reads are ignored. */
|
|
34250
|
-
apply(deviceId, cap, slice) {
|
|
34251
|
-
const key = sliceKey(deviceId, cap);
|
|
34252
|
-
if (!isSliceObject(slice)) return;
|
|
34253
|
-
if (!this.watched.has(key) && !this.isStableTarget(deviceId, cap)) return;
|
|
34254
|
-
this.slices.set(key, slice);
|
|
34255
|
-
}
|
|
34256
|
-
isStableTarget(deviceId, cap) {
|
|
34257
|
-
for (const s of this.sources) if (s.kind === "stable" && s.cap === cap && this.stableIds.get(stableIdKey(s.stableId, s.addonId)) === deviceId) return true;
|
|
34258
|
-
return false;
|
|
34259
|
-
}
|
|
34260
|
-
read(deviceId, cap, fieldPath) {
|
|
34261
|
-
const slice = this.slices.get(sliceKey(deviceId, cap));
|
|
34262
|
-
return slice === void 0 ? void 0 : pick$1(slice, fieldPath);
|
|
34263
|
-
}
|
|
34264
|
-
/** The whole slice the mirror holds for a device cap — `undefined` when it holds none. */
|
|
34265
|
-
sliceOf(deviceId, cap) {
|
|
34266
|
-
return this.slices.get(sliceKey(deviceId, cap));
|
|
34267
|
-
}
|
|
34268
|
-
readStable(stableId, cap, fieldPath, addonId) {
|
|
34269
|
-
const deviceId = this.stableIds.get(stableIdKey(stableId, addonId));
|
|
34270
|
-
return deviceId === void 0 ? void 0 : this.read(deviceId, cap, fieldPath);
|
|
34271
|
-
}
|
|
34272
|
-
/** Fill / reconcile. A read that fails or answers nothing keeps the old value. */
|
|
34273
|
-
async refresh(fetchers) {
|
|
34274
|
-
let updated = 0;
|
|
34275
|
-
let kept = 0;
|
|
34276
|
-
if (this.sources.some((s) => s.kind === "stable")) try {
|
|
34277
|
-
this.stableIds = await fetchers.fetchStableIds();
|
|
34278
|
-
} catch {}
|
|
34279
|
-
const targets = /* @__PURE__ */ new Map();
|
|
34280
|
-
for (const s of this.sources) {
|
|
34281
|
-
const deviceId = s.kind === "device" ? s.deviceId : this.stableIds.get(stableIdKey(s.stableId, s.addonId));
|
|
34282
|
-
if (deviceId === void 0) {
|
|
34283
|
-
kept += 1;
|
|
34284
|
-
continue;
|
|
34285
|
-
}
|
|
34286
|
-
targets.set(sliceKey(deviceId, s.cap), {
|
|
34287
|
-
deviceId,
|
|
34288
|
-
cap: s.cap
|
|
34289
|
-
});
|
|
34290
|
-
}
|
|
34291
|
-
for (const [key, target] of targets) try {
|
|
34292
|
-
const slice = await fetchers.fetchSlice(target.deviceId, target.cap);
|
|
34293
|
-
if (isSliceObject(slice)) {
|
|
34294
|
-
this.slices.set(key, slice);
|
|
34295
|
-
updated += 1;
|
|
34296
|
-
} else kept += 1;
|
|
34297
|
-
} catch {
|
|
34298
|
-
kept += 1;
|
|
34299
|
-
}
|
|
34300
|
-
return {
|
|
34301
|
-
updated,
|
|
34302
|
-
kept
|
|
34303
|
-
};
|
|
34304
|
-
}
|
|
34305
|
-
};
|
|
34306
|
-
var OPERATOR_SYMBOL = {
|
|
34307
|
-
eq: "=",
|
|
34308
|
-
ne: "≠",
|
|
34309
|
-
gt: ">",
|
|
34310
|
-
gte: "≥",
|
|
34311
|
-
lt: "<",
|
|
34312
|
-
lte: "≤",
|
|
34313
|
-
contains: "contains",
|
|
34314
|
-
in: "in"
|
|
34315
|
-
};
|
|
34316
|
-
function compare(actual, operator, expected) {
|
|
34317
|
-
switch (operator) {
|
|
34318
|
-
case "eq": return actual === expected;
|
|
34319
|
-
case "ne": return actual !== expected;
|
|
34320
|
-
case "gt": return typeof actual === "number" && typeof expected === "number" && actual > expected;
|
|
34321
|
-
case "gte": return typeof actual === "number" && typeof expected === "number" && actual >= expected;
|
|
34322
|
-
case "lt": return typeof actual === "number" && typeof expected === "number" && actual < expected;
|
|
34323
|
-
case "lte": return typeof actual === "number" && typeof expected === "number" && actual <= expected;
|
|
34324
|
-
case "contains": return typeof actual === "string" && typeof expected === "string" && actual.includes(expected);
|
|
34325
|
-
case "in": return Array.isArray(expected) && expected.some((v) => v === actual);
|
|
34326
|
-
}
|
|
34327
|
-
}
|
|
34328
|
-
function toTraceValue(value) {
|
|
34329
|
-
if (value === void 0 || value === null) return null;
|
|
34330
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
34331
|
-
if (Array.isArray(value) && value.every((v) => typeof v === "string" || typeof v === "number")) return value;
|
|
34332
|
-
return JSON.stringify(value);
|
|
34333
|
-
}
|
|
34334
|
-
function show(value) {
|
|
34335
|
-
return Array.isArray(value) ? `[${value.join(", ")}]` : String(value);
|
|
34336
|
-
}
|
|
34337
|
-
function markSkipped(cond, path, why, out) {
|
|
34338
|
-
out.push({
|
|
34339
|
-
path,
|
|
34340
|
-
kind: cond.kind,
|
|
34341
|
-
outcome: "not-evaluated",
|
|
34342
|
-
reason: why
|
|
34343
|
-
});
|
|
34344
|
-
if (cond.kind === "all" || cond.kind === "any") cond.children.forEach((c, i) => {
|
|
34345
|
-
markSkipped(c, `${path}.${i}`, why, out);
|
|
34346
|
-
});
|
|
34347
|
-
else if (cond.kind === "not") markSkipped(cond.child, `${path}.0`, why, out);
|
|
34348
|
-
}
|
|
34349
|
-
function parkedObjectsOf(value) {
|
|
34350
|
-
if (!Array.isArray(value)) return [];
|
|
34351
|
-
const rows = [];
|
|
34352
|
-
for (const item of value) {
|
|
34353
|
-
const id = Reflect.get(Object(item), "id");
|
|
34354
|
-
const className = Reflect.get(Object(item), "className");
|
|
34355
|
-
if (typeof id === "string" && typeof className === "string") rows.push({
|
|
34356
|
-
id,
|
|
34357
|
-
className
|
|
34358
|
-
});
|
|
34359
|
-
}
|
|
34360
|
-
return rows;
|
|
34361
|
-
}
|
|
34362
|
-
/** `zoneId`s whose `trackIds` list the parked object (`stationary:<id>`, as the snapshot builds it). */
|
|
34363
|
-
function zonesOfParked(zones, parkedId) {
|
|
34364
|
-
if (!Array.isArray(zones)) return [];
|
|
34365
|
-
const out = [];
|
|
34366
|
-
for (const zone of zones) {
|
|
34367
|
-
const zoneId = Reflect.get(Object(zone), "zoneId");
|
|
34368
|
-
const trackIds = Reflect.get(Object(zone), "trackIds");
|
|
34369
|
-
if (typeof zoneId === "string" && Array.isArray(trackIds) && trackIds.includes(`stationary:${parkedId}`)) out.push(zoneId);
|
|
34370
|
-
}
|
|
34371
|
-
return out;
|
|
34372
|
-
}
|
|
34373
|
-
/**
|
|
34374
|
-
* Is a matching parked object there NOW? Read off the mirrored zone-analytics
|
|
34375
|
-
* snapshot: `frame` is always present in a snapshot, so its absence means the
|
|
34376
|
-
* slice was never read (UNKNOWN, a fail that says so), while a snapshot without
|
|
34377
|
-
* `stationaryObjects` is a definite "nothing parked" (the provider omits an
|
|
34378
|
-
* empty list).
|
|
34379
|
-
*/
|
|
34380
|
-
function judgeStationaryPresent(cond, reader) {
|
|
34381
|
-
if (reader.read(cond.deviceId, ZONE_ANALYTICS_CAP, "frame") === void 0) return {
|
|
34382
|
-
pass: false,
|
|
34383
|
-
why: `device ${cond.deviceId} occupancy snapshot unknown`,
|
|
34384
|
-
unknown: true
|
|
34385
|
-
};
|
|
34386
|
-
const parked = parkedObjectsOf(reader.read(cond.deviceId, ZONE_ANALYTICS_CAP, "stationaryObjects"));
|
|
34387
|
-
if (parked.length === 0) return {
|
|
34388
|
-
pass: false,
|
|
34389
|
-
why: `no parked objects on device ${cond.deviceId}`
|
|
34390
|
-
};
|
|
34391
|
-
const zones = reader.read(cond.deviceId, ZONE_ANALYTICS_CAP, "zones");
|
|
34392
|
-
const matching = [];
|
|
34393
|
-
for (const obj of parked) {
|
|
34394
|
-
if (cond.subjects !== void 0 && !anySubjectMatches(cond.subjects, { className: obj.className }).matches) continue;
|
|
34395
|
-
if (cond.zones !== void 0) {
|
|
34396
|
-
const inZones = zonesOfParked(zones, obj.id);
|
|
34397
|
-
if (!cond.zones.some((z) => inZones.includes(z))) continue;
|
|
34398
|
-
}
|
|
34399
|
-
matching.push(obj);
|
|
34400
|
-
}
|
|
34401
|
-
const needed = cond.minCount ?? 1;
|
|
34402
|
-
const first = matching[0];
|
|
34403
|
-
if (first !== void 0 && matching.length >= needed) return {
|
|
34404
|
-
pass: true,
|
|
34405
|
-
why: needed === 1 ? `parked ${first.className} (${first.id}) on device ${cond.deviceId}` : `${matching.length} parked matching on device ${cond.deviceId} (need ${needed})`
|
|
34406
|
-
};
|
|
34407
|
-
if (matching.length > 0) return {
|
|
34408
|
-
pass: false,
|
|
34409
|
-
why: `only ${matching.length} of ${needed} needed parked objects match on device ${cond.deviceId}`
|
|
34410
|
-
};
|
|
34411
|
-
return {
|
|
34412
|
-
pass: false,
|
|
34413
|
-
why: `${parked.length} parked on device ${cond.deviceId}, none matching the class/zone filter`
|
|
34414
|
-
};
|
|
34415
|
-
}
|
|
34416
|
-
function evalEventLeaf(cond, path, state, out) {
|
|
34417
|
-
const occurred = state.firedPath === path;
|
|
34418
|
-
out.push({
|
|
34419
|
-
path,
|
|
34420
|
-
kind: cond.kind,
|
|
34421
|
-
outcome: occurred ? "pass" : "fail",
|
|
34422
|
-
reason: occurred ? `${cond.kind} occurred → pass` : `${cond.kind} is an instant and did not occur now → fail`
|
|
34423
|
-
});
|
|
34424
|
-
return occurred;
|
|
34425
|
-
}
|
|
34426
|
-
function evalNode(cond, path, reader, now, out, state) {
|
|
34427
|
-
if (isEventLeaf(cond)) return evalEventLeaf(cond, path, state, out);
|
|
34428
|
-
switch (cond.kind) {
|
|
34429
|
-
case "device-is": {
|
|
34430
|
-
const observed = reader.read(cond.deviceId, cond.cap, cond.fieldPath);
|
|
34431
|
-
const base = {
|
|
34432
|
-
path,
|
|
34433
|
-
kind: cond.kind,
|
|
34434
|
-
deviceId: cond.deviceId,
|
|
34435
|
-
cap: cond.cap,
|
|
34436
|
-
fieldPath: cond.fieldPath,
|
|
34437
|
-
operator: cond.operator,
|
|
34438
|
-
expected: toTraceValue(cond.value),
|
|
34439
|
-
observed: toTraceValue(observed)
|
|
34440
|
-
};
|
|
34441
|
-
if (isQuantityValue(cond.value)) {
|
|
34442
|
-
out.push({
|
|
34443
|
-
...base,
|
|
34444
|
-
outcome: "fail",
|
|
34445
|
-
reason: `${cond.fieldPath} expected value's unit was never converted → fail`
|
|
34446
|
-
});
|
|
34447
|
-
return false;
|
|
34448
|
-
}
|
|
34449
|
-
if (observed === void 0) {
|
|
34450
|
-
state.unknown = true;
|
|
34451
|
-
out.push({
|
|
34452
|
-
...base,
|
|
34453
|
-
outcome: "fail",
|
|
34454
|
-
reason: `${cond.fieldPath} value unknown → fail`
|
|
34455
|
-
});
|
|
34456
|
-
return false;
|
|
34457
|
-
}
|
|
34458
|
-
const ok = compare(observed, cond.operator, cond.value);
|
|
34459
|
-
out.push({
|
|
34460
|
-
...base,
|
|
34461
|
-
outcome: ok ? "pass" : "fail",
|
|
34462
|
-
reason: `${cond.fieldPath} ${show(observed)}, expected ${OPERATOR_SYMBOL[cond.operator]} ${show(cond.value)} → ${ok ? "pass" : "fail"}`
|
|
34463
|
-
});
|
|
34464
|
-
return ok;
|
|
34465
|
-
}
|
|
34466
|
-
case "stationary-present": {
|
|
34467
|
-
const verdict = judgeStationaryPresent(cond, reader);
|
|
34468
|
-
if (verdict.unknown === true) state.unknown = true;
|
|
34469
|
-
out.push({
|
|
34470
|
-
path,
|
|
34471
|
-
kind: cond.kind,
|
|
34472
|
-
outcome: verdict.pass ? "pass" : "fail",
|
|
34473
|
-
reason: `${verdict.why} → ${verdict.pass ? "pass" : "fail"}`,
|
|
34474
|
-
deviceId: cond.deviceId,
|
|
34475
|
-
cap: ZONE_ANALYTICS_CAP
|
|
34476
|
-
});
|
|
34477
|
-
return verdict.pass;
|
|
34478
|
-
}
|
|
34479
|
-
case "expression": {
|
|
34480
|
-
const values = {};
|
|
34481
|
-
for (const [alias, binding] of Object.entries(cond.bindings)) {
|
|
34482
|
-
if (binding.kind === "literal") {
|
|
34483
|
-
values[alias] = require_implemented_methods.toExpressionValue(binding.value) ?? null;
|
|
34484
|
-
continue;
|
|
34485
|
-
}
|
|
34486
|
-
const raw = binding.kind === "global" ? reader.readStable(binding.sourceStableId, binding.cap, binding.fieldPath, binding.addonId) : void 0;
|
|
34487
|
-
if (raw === void 0) {
|
|
34488
|
-
out.push({
|
|
34489
|
-
path,
|
|
34490
|
-
kind: cond.kind,
|
|
34491
|
-
outcome: "fail",
|
|
34492
|
-
reason: `${alias} value unknown → fail`
|
|
34493
|
-
});
|
|
34494
|
-
state.unknown = true;
|
|
34495
|
-
return false;
|
|
34496
|
-
}
|
|
34497
|
-
values[alias] = require_implemented_methods.toExpressionValue(raw) ?? null;
|
|
34498
|
-
}
|
|
34499
|
-
const result = require_implemented_methods.evaluateExpressionSource(cond.expr, values, now);
|
|
34500
|
-
if (!result.ok) {
|
|
34501
|
-
out.push({
|
|
34502
|
-
path,
|
|
34503
|
-
kind: cond.kind,
|
|
34504
|
-
outcome: "fail",
|
|
34505
|
-
reason: `${cond.expr}: ${result.error} → fail`
|
|
34506
|
-
});
|
|
34507
|
-
return false;
|
|
34508
|
-
}
|
|
34509
|
-
const ok = result.value === true;
|
|
34510
|
-
out.push({
|
|
34511
|
-
path,
|
|
34512
|
-
kind: cond.kind,
|
|
34513
|
-
outcome: ok ? "pass" : "fail",
|
|
34514
|
-
reason: `${cond.expr} with ${JSON.stringify(values)} → ${ok ? "pass" : "fail"}`
|
|
34515
|
-
});
|
|
34516
|
-
return ok;
|
|
34517
|
-
}
|
|
34518
|
-
case "not": {
|
|
34519
|
-
const at = out.length;
|
|
34520
|
-
out.push({
|
|
34521
|
-
path,
|
|
34522
|
-
kind: "not",
|
|
34523
|
-
outcome: "not-evaluated",
|
|
34524
|
-
reason: ""
|
|
34525
|
-
});
|
|
34526
|
-
const inner = evalNode(cond.child, `${path}.0`, reader, now, out, state);
|
|
34527
|
-
const ok = !inner;
|
|
34528
|
-
out.splice(at, 1, {
|
|
34529
|
-
path,
|
|
34530
|
-
kind: "not",
|
|
34531
|
-
outcome: ok ? "pass" : "fail",
|
|
34532
|
-
reason: `not (${inner ? "pass" : "fail"}) → ${ok ? "pass" : "fail"}`
|
|
34533
|
-
});
|
|
34534
|
-
return ok;
|
|
34535
|
-
}
|
|
34536
|
-
case "all":
|
|
34537
|
-
case "any": {
|
|
34538
|
-
const isAll = cond.kind === "all";
|
|
34539
|
-
const at = out.length;
|
|
34540
|
-
out.push({
|
|
34541
|
-
path,
|
|
34542
|
-
kind: cond.kind,
|
|
34543
|
-
outcome: "not-evaluated",
|
|
34544
|
-
reason: ""
|
|
34545
|
-
});
|
|
34546
|
-
let decided = null;
|
|
34547
|
-
for (let i = 0; i < cond.children.length; i += 1) {
|
|
34548
|
-
const child = cond.children[i];
|
|
34549
|
-
if (child === void 0) continue;
|
|
34550
|
-
if (decided !== null) {
|
|
34551
|
-
markSkipped(child, `${path}.${i}`, `short-circuited by ${path}.${decided}`, out);
|
|
34552
|
-
continue;
|
|
34553
|
-
}
|
|
34554
|
-
const ok = evalNode(child, `${path}.${i}`, reader, now, out, state);
|
|
34555
|
-
if (isAll ? !ok : ok) decided = i;
|
|
34556
|
-
}
|
|
34557
|
-
const ok = isAll ? decided === null : decided !== null;
|
|
34558
|
-
const reason = isAll ? ok ? `all ${cond.children.length} passed → pass` : `${path}.${decided} failed → fail` : ok ? `${path}.${decided} passed → pass` : `none of ${cond.children.length} passed → fail`;
|
|
34559
|
-
out.splice(at, 1, {
|
|
34560
|
-
path,
|
|
34561
|
-
kind: cond.kind,
|
|
34562
|
-
outcome: ok ? "pass" : "fail",
|
|
34563
|
-
reason
|
|
34564
|
-
});
|
|
34565
|
-
return ok;
|
|
34566
|
-
}
|
|
34567
|
-
}
|
|
34568
|
-
}
|
|
34569
|
-
/** Evaluate a `when` tree: the fired event leaf is true, every other event leaf false. */
|
|
34570
|
-
function evaluateWhenTree(when, reader, now, options = {}) {
|
|
34571
|
-
const nodes = [];
|
|
34572
|
-
const state = {
|
|
34573
|
-
firedPath: options.firedPath,
|
|
34574
|
-
unknown: false
|
|
34575
|
-
};
|
|
34576
|
-
return {
|
|
34577
|
-
pass: evalNode(when, options.rootPath ?? "when", reader, now, nodes, state),
|
|
34578
|
-
unknown: state.unknown,
|
|
34579
|
-
nodes
|
|
34580
|
-
};
|
|
34581
|
-
}
|
|
34582
|
-
/** Legacy name, for blocks generated before the single tree. No tree is a pass with no nodes. */
|
|
34583
|
-
function evaluateConditionTree(cond, reader, now) {
|
|
34584
|
-
if (cond === void 0) return {
|
|
34585
|
-
pass: true,
|
|
34586
|
-
unknown: false,
|
|
34587
|
-
nodes: []
|
|
34588
|
-
};
|
|
34589
|
-
return evaluateWhenTree(convertLegacyCondition(cond), reader, now, { rootPath: "conditions" });
|
|
34590
|
-
}
|
|
34591
|
-
//#endregion
|
|
34592
35108
|
//#region src/automation/detection-trigger.ts
|
|
34593
35109
|
/**
|
|
34594
35110
|
* The `detection` trigger's per-event decision: does this track-lifecycle
|
|
@@ -35224,7 +35740,9 @@ var AutomationTriggerTraceSchema = zod.z.object({
|
|
|
35224
35740
|
/** Why an event was rejected ("class person not in [dog]") or what fired. */
|
|
35225
35741
|
reason: zod.z.string(),
|
|
35226
35742
|
/** deviceId, className, trackId, score … — only what the trigger exposes. */
|
|
35227
|
-
vars: zod.z.record(zod.z.string(), TraceValueSchema)
|
|
35743
|
+
vars: zod.z.record(zod.z.string(), TraceValueSchema),
|
|
35744
|
+
/** A hold (`for`) this occurrence went through: pending, cancelled, fired or re-seeded. */
|
|
35745
|
+
hold: HoldTraceSchema.optional()
|
|
35228
35746
|
});
|
|
35229
35747
|
var AutomationConditionTraceNodeSchema = zod.z.object({
|
|
35230
35748
|
/** `when` for the root, `when.<i>` for its i-th child, … (`conditions…` on an old trace). */
|
|
@@ -35459,12 +35977,14 @@ function buildTriggerTrace(trigger, outcome, reason) {
|
|
|
35459
35977
|
}
|
|
35460
35978
|
const kind = typeof trigger["kind"] === "string" ? trigger["kind"] : "unknown";
|
|
35461
35979
|
const path = typeof trigger["leaf"] === "string" ? trigger["leaf"] : void 0;
|
|
35980
|
+
const hold = HoldTraceSchema.safeParse(trigger["hold"]);
|
|
35462
35981
|
return {
|
|
35463
35982
|
kind,
|
|
35464
35983
|
...path === void 0 ? {} : { path },
|
|
35465
35984
|
outcome,
|
|
35466
35985
|
reason,
|
|
35467
|
-
vars
|
|
35986
|
+
vars,
|
|
35987
|
+
...hold.success ? { hold: hold.data } : {}
|
|
35468
35988
|
};
|
|
35469
35989
|
}
|
|
35470
35990
|
//#endregion
|
|
@@ -35705,10 +36225,13 @@ var ActionRunChangedSchema = zod.z.object({
|
|
|
35705
36225
|
* nothing, and never decides a branch).
|
|
35706
36226
|
*/
|
|
35707
36227
|
/** Fill a mirror for the tree's leaves from `fetchers` and judge it NOW. */
|
|
35708
|
-
async function evaluateStepCondition(when, fetchers, now = Date.now) {
|
|
35709
|
-
const mirror = new ConditionMirror(collectWhenMirrorSources(when));
|
|
36228
|
+
async function evaluateStepCondition(when, fetchers, now = Date.now, history) {
|
|
36229
|
+
const mirror = history?.mirror ?? new ConditionMirror(collectWhenMirrorSources(when));
|
|
35710
36230
|
await mirror.refresh(fetchers);
|
|
35711
|
-
const evaluation = evaluateWhenTree(when, mirror, now()
|
|
36231
|
+
const evaluation = evaluateWhenTree(when, mirror, now(), { ...history === void 0 ? {} : {
|
|
36232
|
+
holds: history.holds,
|
|
36233
|
+
rootPath: history.rootPath
|
|
36234
|
+
} });
|
|
35712
36235
|
const first = evaluation.nodes.filter((n) => n.outcome !== "pass").map((n) => n.reason)[0];
|
|
35713
36236
|
if (evaluation.unknown) return {
|
|
35714
36237
|
result: "unknown",
|
|
@@ -135838,6 +136361,8 @@ exports.BaseDevice = BaseDevice;
|
|
|
135838
136361
|
exports.BaseDeviceProvider = BaseDeviceProvider;
|
|
135839
136362
|
exports.BatchTooLargeError = require_sleep.BatchTooLargeError;
|
|
135840
136363
|
exports.BatteryStatusSchema = BatteryStatusSchema;
|
|
136364
|
+
exports.BecomesOperatorSchema = BecomesOperatorSchema;
|
|
136365
|
+
exports.BecomesWatch = BecomesWatch;
|
|
135841
136366
|
exports.BinaryStatusSchema = BinaryStatusSchema;
|
|
135842
136367
|
exports.BirthDecisionCropSourceSchema = BirthDecisionCropSourceSchema;
|
|
135843
136368
|
exports.BirthDecisionQueryInputSchema = BirthDecisionQueryInputSchema;
|
|
@@ -136611,8 +137136,12 @@ exports.HeroRowModelSchema = HeroRowModelSchema;
|
|
|
136611
137136
|
exports.HfModelResolutionSchema = HfModelResolutionSchema;
|
|
136612
137137
|
exports.HistoryPointSchema = HistoryPointSchema;
|
|
136613
137138
|
exports.HistoryResolutionEnum = HistoryResolutionEnum;
|
|
137139
|
+
exports.HoldCore = HoldCore;
|
|
136614
137140
|
exports.HoldOverrideSchema = HoldOverrideSchema;
|
|
137141
|
+
exports.HoldSpecSchema = HoldSpecSchema;
|
|
136615
137142
|
exports.HoldTargetStatusSchema = HoldTargetStatusSchema;
|
|
137143
|
+
exports.HoldTraceSchema = HoldTraceSchema;
|
|
137144
|
+
exports.HoldTracker = HoldTracker;
|
|
136616
137145
|
exports.HttpsLeafInfoSchema = HttpsLeafInfoSchema;
|
|
136617
137146
|
exports.HubAddressRoleSchema = HubAddressRoleSchema;
|
|
136618
137147
|
exports.HumidifierStatusSchema = HumidifierStatusSchema;
|
|
@@ -138475,6 +139004,8 @@ exports.deriveRootFaceRefineRect = deriveRootFaceRefineRect;
|
|
|
138475
139004
|
exports.deriveRootSupplies = deriveRootSupplies;
|
|
138476
139005
|
exports.describeComposableCaps = describeComposableCaps;
|
|
138477
139006
|
exports.describeCustomActions = require_sleep.describeCustomActions;
|
|
139007
|
+
exports.describeHold = describeHold;
|
|
139008
|
+
exports.describeHoldMs = describeHoldMs;
|
|
138478
139009
|
exports.describeModelVariant = describeModelVariant;
|
|
138479
139010
|
exports.describeOnboardClamp = describeOnboardClamp;
|
|
138480
139011
|
exports.describeOnboardRefusal = describeOnboardRefusal;
|
|
@@ -138663,6 +139194,7 @@ exports.groupStatusCapability = groupStatusCapability;
|
|
|
138663
139194
|
exports.groupTemplate = groupTemplate;
|
|
138664
139195
|
exports.guardInstanceDelete = guardInstanceDelete;
|
|
138665
139196
|
exports.hasEventLeaves = hasEventLeaves;
|
|
139197
|
+
exports.hasHoldLeaves = hasHoldLeaves;
|
|
138666
139198
|
exports.hasModelBoundOverrides = hasModelBoundOverrides;
|
|
138667
139199
|
exports.hasMotionTrigger = hasMotionTrigger;
|
|
138668
139200
|
exports.hasOwnRunningPowerDefaults = hasOwnRunningPowerDefaults;
|
|
@@ -138681,6 +139213,7 @@ exports.historyFieldsFor = historyFieldsFor;
|
|
|
138681
139213
|
exports.historyItemFieldPath = require_sleep.historyItemFieldPath;
|
|
138682
139214
|
exports.historyRetentionFromSettings = require_implemented_methods.historyRetentionFromSettings;
|
|
138683
139215
|
exports.historyUnitFor = require_sleep.historyUnitFor;
|
|
139216
|
+
exports.holdOf = holdOf;
|
|
138684
139217
|
exports.holdRefusalOf = holdRefusalOf;
|
|
138685
139218
|
exports.holdVarSlots = holdVarSlots;
|
|
138686
139219
|
exports.htmlToText = htmlToText;
|
|
@@ -138973,6 +139506,7 @@ exports.oauthIntegrationCapability = oauthIntegrationCapability;
|
|
|
138973
139506
|
exports.oauthLandingUrl = oauthLandingUrl;
|
|
138974
139507
|
exports.objectInputDeclaresAddonId = objectInputDeclaresAddonId;
|
|
138975
139508
|
exports.objectInputDeclaresRequiredDeviceId = objectInputDeclaresRequiredDeviceId;
|
|
139509
|
+
exports.observeHoldLeaves = observeHoldLeaves;
|
|
138976
139510
|
exports.occupancyScope = occupancyScope;
|
|
138977
139511
|
exports.occurrenceOn = occurrenceOn;
|
|
138978
139512
|
exports.omitTransientConfig = require_sleep.omitTransientConfig;
|