@switchbot/openapi-cli 2.7.2 → 3.0.0
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/README.md +383 -101
- package/dist/commands/agent-bootstrap.js +47 -2
- package/dist/commands/auth.js +354 -0
- package/dist/commands/config.js +30 -0
- package/dist/commands/devices.js +0 -1
- package/dist/commands/doctor.js +184 -7
- package/dist/commands/events.js +3 -3
- package/dist/commands/explain.js +1 -2
- package/dist/commands/install.js +246 -0
- package/dist/commands/mcp.js +796 -3
- package/dist/commands/plan.js +110 -14
- package/dist/commands/policy.js +469 -0
- package/dist/commands/rules.js +657 -0
- package/dist/commands/schema.js +0 -2
- package/dist/commands/status-sync.js +131 -0
- package/dist/commands/uninstall.js +237 -0
- package/dist/config.js +14 -0
- package/dist/credentials/backends/file.js +101 -0
- package/dist/credentials/backends/linux.js +129 -0
- package/dist/credentials/backends/macos.js +129 -0
- package/dist/credentials/backends/windows.js +215 -0
- package/dist/credentials/keychain.js +88 -0
- package/dist/credentials/prime.js +52 -0
- package/dist/devices/catalog.js +4 -10
- package/dist/index.js +23 -1
- package/dist/install/default-steps.js +257 -0
- package/dist/install/preflight.js +212 -0
- package/dist/install/steps.js +67 -0
- package/dist/lib/command-keywords.js +17 -0
- package/dist/lib/devices.js +0 -1
- package/dist/policy/add-rule.js +124 -0
- package/dist/policy/diff.js +91 -0
- package/dist/policy/examples/policy.example.yaml +99 -0
- package/dist/policy/format.js +57 -0
- package/dist/policy/load.js +61 -0
- package/dist/policy/migrate.js +67 -0
- package/dist/policy/schema/v0.2.json +302 -0
- package/dist/policy/schema.js +18 -0
- package/dist/policy/validate.js +262 -0
- package/dist/rules/action.js +205 -0
- package/dist/rules/audit-query.js +89 -0
- package/dist/rules/cron-scheduler.js +186 -0
- package/dist/rules/destructive.js +52 -0
- package/dist/rules/engine.js +567 -0
- package/dist/rules/matcher.js +230 -0
- package/dist/rules/pid-file.js +95 -0
- package/dist/rules/quiet-hours.js +45 -0
- package/dist/rules/suggest.js +95 -0
- package/dist/rules/throttle.js +78 -0
- package/dist/rules/types.js +34 -0
- package/dist/rules/webhook-listener.js +223 -0
- package/dist/rules/webhook-token.js +90 -0
- package/dist/status-sync/manager.js +268 -0
- package/dist/utils/audit.js +12 -2
- package/package.json +12 -4
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
export const SUPPORTED_POLICY_SCHEMA_VERSIONS = ['0.2'];
|
|
4
|
+
export const CURRENT_POLICY_SCHEMA_VERSION = '0.2';
|
|
5
|
+
const schemaCache = new Map();
|
|
6
|
+
export function loadPolicySchema(version = CURRENT_POLICY_SCHEMA_VERSION) {
|
|
7
|
+
const cached = schemaCache.get(version);
|
|
8
|
+
if (cached)
|
|
9
|
+
return cached;
|
|
10
|
+
const url = new URL(`./schema/v${version}.json`, import.meta.url);
|
|
11
|
+
const raw = readFileSync(fileURLToPath(url), 'utf-8');
|
|
12
|
+
const parsed = JSON.parse(raw);
|
|
13
|
+
schemaCache.set(version, parsed);
|
|
14
|
+
return parsed;
|
|
15
|
+
}
|
|
16
|
+
export function isSupportedPolicySchemaVersion(v) {
|
|
17
|
+
return typeof v === 'string' && SUPPORTED_POLICY_SCHEMA_VERSIONS.includes(v);
|
|
18
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { Ajv2020 } from 'ajv/dist/2020.js';
|
|
3
|
+
import { isMap, isSeq, isScalar } from 'yaml';
|
|
4
|
+
import { loadPolicyFile } from './load.js';
|
|
5
|
+
import { loadPolicySchema, CURRENT_POLICY_SCHEMA_VERSION, SUPPORTED_POLICY_SCHEMA_VERSIONS, isSupportedPolicySchemaVersion, } from './schema.js';
|
|
6
|
+
import { destructiveVerbOf, DESTRUCTIVE_COMMANDS } from '../rules/destructive.js';
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const addFormats = require('ajv-formats');
|
|
9
|
+
const validators = new Map();
|
|
10
|
+
function getValidator(version) {
|
|
11
|
+
const cached = validators.get(version);
|
|
12
|
+
if (cached)
|
|
13
|
+
return cached;
|
|
14
|
+
const ajv = new Ajv2020({ allErrors: true, strict: false, allowUnionTypes: true });
|
|
15
|
+
addFormats(ajv);
|
|
16
|
+
const schema = loadPolicySchema(version);
|
|
17
|
+
const validate = ajv.compile(schema);
|
|
18
|
+
const compiled = { ajv, validate };
|
|
19
|
+
validators.set(version, compiled);
|
|
20
|
+
return compiled;
|
|
21
|
+
}
|
|
22
|
+
function instancePathToSegments(instancePath) {
|
|
23
|
+
if (!instancePath)
|
|
24
|
+
return [];
|
|
25
|
+
return instancePath
|
|
26
|
+
.slice(1)
|
|
27
|
+
.split('/')
|
|
28
|
+
.map((s) => s.replace(/~1/g, '/').replace(/~0/g, '~'));
|
|
29
|
+
}
|
|
30
|
+
function getNodeAt(doc, segments) {
|
|
31
|
+
let current = doc.contents;
|
|
32
|
+
for (const seg of segments) {
|
|
33
|
+
if (isMap(current)) {
|
|
34
|
+
const pair = current.items.find((p) => {
|
|
35
|
+
const k = p.key;
|
|
36
|
+
if (isScalar(k))
|
|
37
|
+
return String(k.value) === seg;
|
|
38
|
+
return false;
|
|
39
|
+
});
|
|
40
|
+
if (!pair)
|
|
41
|
+
return null;
|
|
42
|
+
current = pair.value;
|
|
43
|
+
}
|
|
44
|
+
else if (isSeq(current)) {
|
|
45
|
+
const idx = Number(seg);
|
|
46
|
+
if (!Number.isInteger(idx))
|
|
47
|
+
return null;
|
|
48
|
+
current = current.items[idx];
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return current ?? null;
|
|
55
|
+
}
|
|
56
|
+
function getKeyNodeAt(doc, parentSegments, key) {
|
|
57
|
+
const parent = parentSegments.length === 0 ? doc.contents : getNodeAt(doc, parentSegments);
|
|
58
|
+
if (!parent || !isMap(parent))
|
|
59
|
+
return null;
|
|
60
|
+
const pair = parent.items.find((p) => isScalar(p.key) && String(p.key.value) === key);
|
|
61
|
+
return pair?.key ?? null;
|
|
62
|
+
}
|
|
63
|
+
function locateError(doc, lineCounter, err) {
|
|
64
|
+
const segments = instancePathToSegments(err.instancePath);
|
|
65
|
+
if (err.keyword === 'additionalProperties') {
|
|
66
|
+
const bad = err.params.additionalProperty;
|
|
67
|
+
if (bad) {
|
|
68
|
+
const keyNode = getKeyNodeAt(doc, segments, bad);
|
|
69
|
+
const range = keyNode?.range;
|
|
70
|
+
if (range) {
|
|
71
|
+
const pos = lineCounter.linePos(range[0]);
|
|
72
|
+
return { line: pos.line, col: pos.col };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (err.keyword === 'required' || err.keyword === 'dependentRequired') {
|
|
77
|
+
const node = getNodeAt(doc, segments);
|
|
78
|
+
const range = node?.range;
|
|
79
|
+
if (range) {
|
|
80
|
+
const pos = lineCounter.linePos(range[0]);
|
|
81
|
+
return { line: pos.line, col: pos.col };
|
|
82
|
+
}
|
|
83
|
+
return { line: 1, col: 1 };
|
|
84
|
+
}
|
|
85
|
+
const node = getNodeAt(doc, segments);
|
|
86
|
+
const range = node?.range;
|
|
87
|
+
if (!range)
|
|
88
|
+
return {};
|
|
89
|
+
const pos = lineCounter.linePos(range[0]);
|
|
90
|
+
return { line: pos.line, col: pos.col };
|
|
91
|
+
}
|
|
92
|
+
function humanMessage(err) {
|
|
93
|
+
const path = err.instancePath || '(root)';
|
|
94
|
+
switch (err.keyword) {
|
|
95
|
+
case 'required':
|
|
96
|
+
return `missing required property "${err.params.missingProperty}"`;
|
|
97
|
+
case 'additionalProperties':
|
|
98
|
+
return `unknown property "${err.params.additionalProperty}"`;
|
|
99
|
+
case 'dependentRequired': {
|
|
100
|
+
const { property, missingProperty } = err.params;
|
|
101
|
+
const parent = path === '(root)' ? '' : `${path}: `;
|
|
102
|
+
return `${parent}when "${property}" is set, "${missingProperty}" is also required`;
|
|
103
|
+
}
|
|
104
|
+
case 'pattern':
|
|
105
|
+
return `${path} does not match pattern ${err.params.pattern}`;
|
|
106
|
+
case 'const':
|
|
107
|
+
return `${path} must be exactly ${JSON.stringify(err.params.allowedValue)}`;
|
|
108
|
+
case 'enum':
|
|
109
|
+
return `${path} must be one of ${JSON.stringify(err.params.allowedValues)}`;
|
|
110
|
+
case 'type':
|
|
111
|
+
return `${path} must be ${err.params.type}`;
|
|
112
|
+
case 'not':
|
|
113
|
+
return `${path} is not allowed here`;
|
|
114
|
+
default:
|
|
115
|
+
return `${path} ${err.message ?? 'is invalid'}`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function hintFor(err) {
|
|
119
|
+
if (err.keyword === 'pattern' && err.instancePath.startsWith('/aliases/')) {
|
|
120
|
+
return 'paste the deviceId from `switchbot devices list --format=tsv`, e.g. 01-202407090924-26354212';
|
|
121
|
+
}
|
|
122
|
+
if (err.keyword === 'not' && err.instancePath.startsWith('/confirmations/never_confirm/')) {
|
|
123
|
+
return 'destructive actions (lock/unlock/delete*/factoryReset) cannot be pre-approved in policy.yaml';
|
|
124
|
+
}
|
|
125
|
+
if (err.keyword === 'const' && err.instancePath === '/version') {
|
|
126
|
+
const supported = SUPPORTED_POLICY_SCHEMA_VERSIONS.map((v) => `"${v}"`).join(' / ');
|
|
127
|
+
return `this CLI supports policy schema versions ${supported}; run \`switchbot policy migrate\` to upgrade an older file`;
|
|
128
|
+
}
|
|
129
|
+
if (err.keyword === 'required' && err.instancePath === '') {
|
|
130
|
+
const missing = err.params.missingProperty;
|
|
131
|
+
if (missing === 'version')
|
|
132
|
+
return `add \`version: "${CURRENT_POLICY_SCHEMA_VERSION}"\` at the top of the file`;
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
function readDeclaredVersion(data) {
|
|
137
|
+
if (data && typeof data === 'object' && 'version' in data) {
|
|
138
|
+
const v = data.version;
|
|
139
|
+
if (typeof v === 'string')
|
|
140
|
+
return v;
|
|
141
|
+
}
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
function unsupportedVersionResult(loaded, declared) {
|
|
145
|
+
const supported = SUPPORTED_POLICY_SCHEMA_VERSIONS.map((v) => `"${v}"`).join(' / ');
|
|
146
|
+
const isLegacy = declared === '0.1';
|
|
147
|
+
const hint = isLegacy
|
|
148
|
+
? `v0.1 policy support was removed in v3.0. Run \`switchbot policy migrate\` with CLI ≤2.15 first, then upgrade.`
|
|
149
|
+
: `supported versions: ${supported}. upgrade the CLI or downgrade the file.`;
|
|
150
|
+
return {
|
|
151
|
+
policyPath: loaded.path,
|
|
152
|
+
schemaVersion: CURRENT_POLICY_SCHEMA_VERSION,
|
|
153
|
+
valid: false,
|
|
154
|
+
errors: [
|
|
155
|
+
{
|
|
156
|
+
path: '/version',
|
|
157
|
+
line: 1,
|
|
158
|
+
col: 1,
|
|
159
|
+
keyword: 'unsupported-version',
|
|
160
|
+
message: `policy schema version "${declared}" is not supported by this CLI`,
|
|
161
|
+
hint,
|
|
162
|
+
schemaPath: '#/properties/version',
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Walk `automation.rules[].then[]` and flag any command string whose verb
|
|
169
|
+
* appears in DESTRUCTIVE_COMMANDS. Uses the YAML doc (not the data tree) to
|
|
170
|
+
* get accurate line/col on the offending node.
|
|
171
|
+
*
|
|
172
|
+
* This is deliberately a post-ajv pass rather than a schema rule because
|
|
173
|
+
* JSON Schema cannot parse a command string and compare the verb slot to a
|
|
174
|
+
* blocklist. Keeping it in JS also lets `src/rules/destructive.ts` be the
|
|
175
|
+
* single source of truth shared with the runtime executor.
|
|
176
|
+
*/
|
|
177
|
+
function collectDestructiveRuleErrors(loaded) {
|
|
178
|
+
const data = loaded.data;
|
|
179
|
+
const rules = data?.automation?.rules;
|
|
180
|
+
if (!Array.isArray(rules))
|
|
181
|
+
return [];
|
|
182
|
+
const out = [];
|
|
183
|
+
for (let ri = 0; ri < rules.length; ri++) {
|
|
184
|
+
const rule = rules[ri];
|
|
185
|
+
const actions = Array.isArray(rule?.then) ? rule.then : [];
|
|
186
|
+
for (let ai = 0; ai < actions.length; ai++) {
|
|
187
|
+
const cmd = actions[ai]?.command;
|
|
188
|
+
if (typeof cmd !== 'string')
|
|
189
|
+
continue;
|
|
190
|
+
const verb = destructiveVerbOf(cmd);
|
|
191
|
+
if (!verb)
|
|
192
|
+
continue;
|
|
193
|
+
const instancePath = `/automation/rules/${ri}/then/${ai}/command`;
|
|
194
|
+
const segments = instancePath.slice(1).split('/');
|
|
195
|
+
const node = getNodeAt(loaded.doc, segments);
|
|
196
|
+
const range = node?.range;
|
|
197
|
+
let line;
|
|
198
|
+
let col;
|
|
199
|
+
if (range) {
|
|
200
|
+
const pos = loaded.lineCounter.linePos(range[0]);
|
|
201
|
+
line = pos.line;
|
|
202
|
+
col = pos.col;
|
|
203
|
+
}
|
|
204
|
+
const ruleName = typeof rule?.name === 'string' ? rule.name : `#${ri}`;
|
|
205
|
+
out.push({
|
|
206
|
+
path: instancePath,
|
|
207
|
+
line,
|
|
208
|
+
col,
|
|
209
|
+
keyword: 'rule-destructive-action',
|
|
210
|
+
message: `rule "${ruleName}" action #${ai} uses destructive command "${verb}"`,
|
|
211
|
+
hint: `destructive verbs (${DESTRUCTIVE_COMMANDS.join(', ')}) cannot be pre-approved in automation rules; run them via the interactive CLI so the confirmation gate fires`,
|
|
212
|
+
schemaPath: '#/properties/automation/properties/rules/items/properties/then/items/properties/command',
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
218
|
+
export function validateLoadedPolicy(loaded) {
|
|
219
|
+
const declared = readDeclaredVersion(loaded.data);
|
|
220
|
+
if (declared !== undefined && !isSupportedPolicySchemaVersion(declared)) {
|
|
221
|
+
return unsupportedVersionResult(loaded, declared);
|
|
222
|
+
}
|
|
223
|
+
const version = isSupportedPolicySchemaVersion(declared)
|
|
224
|
+
? declared
|
|
225
|
+
: CURRENT_POLICY_SCHEMA_VERSION;
|
|
226
|
+
const { validate } = getValidator(version);
|
|
227
|
+
const ok = validate(loaded.data);
|
|
228
|
+
const errors = [];
|
|
229
|
+
if (!ok && validate.errors) {
|
|
230
|
+
for (const err of validate.errors) {
|
|
231
|
+
const { line, col } = locateError(loaded.doc, loaded.lineCounter, err);
|
|
232
|
+
errors.push({
|
|
233
|
+
path: err.instancePath || '',
|
|
234
|
+
line,
|
|
235
|
+
col,
|
|
236
|
+
keyword: err.keyword,
|
|
237
|
+
message: humanMessage(err),
|
|
238
|
+
hint: hintFor(err),
|
|
239
|
+
schemaPath: err.schemaPath,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// v0.2-only post-hook: destructive verbs like `unlock` / `factoryReset`
|
|
244
|
+
// cannot be pre-approved via rules, even if ajv considers the command
|
|
245
|
+
// string well-formed. Schema can't express this because `command` is a
|
|
246
|
+
// free-form string; we parse the verb in JS and append errors.
|
|
247
|
+
if (version === '0.2') {
|
|
248
|
+
const ruleErrors = collectDestructiveRuleErrors(loaded);
|
|
249
|
+
errors.push(...ruleErrors);
|
|
250
|
+
}
|
|
251
|
+
const valid = ok === true && errors.length === 0;
|
|
252
|
+
return {
|
|
253
|
+
policyPath: loaded.path,
|
|
254
|
+
schemaVersion: version,
|
|
255
|
+
valid,
|
|
256
|
+
errors,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
export function validatePolicyFile(policyPath) {
|
|
260
|
+
const loaded = loadPolicyFile(policyPath);
|
|
261
|
+
return validateLoadedPolicy(loaded);
|
|
262
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rule action executor — the only place that calls into `executeCommand`
|
|
3
|
+
* from the rules pipeline.
|
|
4
|
+
*
|
|
5
|
+
* Responsibilities:
|
|
6
|
+
* 1. Parse the `command` string into a `{ deviceId, verb, parameter }`
|
|
7
|
+
* tuple, rejecting shapes the PoC doesn't understand.
|
|
8
|
+
* 2. Enforce the destructive-command blocklist as a second line of
|
|
9
|
+
* defence (the validator should have caught it at load time — this
|
|
10
|
+
* protects against hand-crafted engine inputs).
|
|
11
|
+
* 3. Resolve `action.device` (alias or deviceId) into the `<id>`
|
|
12
|
+
* slot.
|
|
13
|
+
* 4. Branch on `dry_run`: dry-run writes audit with kind
|
|
14
|
+
* `rule-fire-dry` and returns without touching the API.
|
|
15
|
+
* 5. Live run delegates to `executeCommand`, then re-writes audit
|
|
16
|
+
* with the rule-scoped kind + fireId so `rules tail` / `replay`
|
|
17
|
+
* can correlate multi-action fires.
|
|
18
|
+
*/
|
|
19
|
+
import { executeCommand } from '../lib/devices.js';
|
|
20
|
+
import { writeAudit } from '../utils/audit.js';
|
|
21
|
+
import { isDestructiveCommand } from './destructive.js';
|
|
22
|
+
const DEVICES_COMMAND_RE = /^devices\s+command\s+(\S+)\s+(\S+)(?:\s+(.*))?$/;
|
|
23
|
+
export function parseRuleCommand(cmd) {
|
|
24
|
+
const m = DEVICES_COMMAND_RE.exec(cmd.trim());
|
|
25
|
+
if (!m)
|
|
26
|
+
return null;
|
|
27
|
+
const deviceIdSlot = m[1];
|
|
28
|
+
const verb = m[2];
|
|
29
|
+
const rest = (m[3] ?? '').trim();
|
|
30
|
+
return {
|
|
31
|
+
deviceIdSlot,
|
|
32
|
+
verb,
|
|
33
|
+
parameterTokens: rest.length === 0 ? [] : rest.split(/\s+/),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Alias-first resolver — falls back to the raw value (assumed deviceId). */
|
|
37
|
+
export function resolveActionDevice(explicit, slot, aliases) {
|
|
38
|
+
// Explicit device field on the action wins.
|
|
39
|
+
const candidate = explicit ?? (slot && slot !== '<id>' ? slot : null);
|
|
40
|
+
if (!candidate)
|
|
41
|
+
return null;
|
|
42
|
+
if (aliases[candidate])
|
|
43
|
+
return aliases[candidate];
|
|
44
|
+
return candidate;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Render a parameter for SwitchBot's command API. For the PoC we pass
|
|
48
|
+
* the raw token string for single-token args, join with `:` for
|
|
49
|
+
* multi-token args (matches the CLI's `devices command` convention),
|
|
50
|
+
* and `undefined` when no tokens were supplied (the SDK substitutes
|
|
51
|
+
* `'default'`).
|
|
52
|
+
*/
|
|
53
|
+
function renderParameter(tokens) {
|
|
54
|
+
if (tokens.length === 0)
|
|
55
|
+
return undefined;
|
|
56
|
+
if (tokens.length === 1)
|
|
57
|
+
return tokens[0];
|
|
58
|
+
return tokens.join(':');
|
|
59
|
+
}
|
|
60
|
+
export async function executeRuleAction(action, ctx) {
|
|
61
|
+
const parsed = parseRuleCommand(action.command);
|
|
62
|
+
if (!parsed) {
|
|
63
|
+
writeAudit({
|
|
64
|
+
t: new Date().toISOString(),
|
|
65
|
+
kind: 'rule-fire',
|
|
66
|
+
deviceId: 'unknown',
|
|
67
|
+
command: action.command,
|
|
68
|
+
parameter: null,
|
|
69
|
+
commandType: 'command',
|
|
70
|
+
dryRun: true,
|
|
71
|
+
result: 'error',
|
|
72
|
+
error: 'unparseable-command',
|
|
73
|
+
rule: {
|
|
74
|
+
name: ctx.rule.name,
|
|
75
|
+
triggerSource: ctx.rule.when.source,
|
|
76
|
+
fireId: ctx.fireId,
|
|
77
|
+
reason: 'unparseable-command',
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
return { ok: false, error: 'unparseable-command', blocked: true };
|
|
81
|
+
}
|
|
82
|
+
if (isDestructiveCommand(action.command)) {
|
|
83
|
+
writeAudit({
|
|
84
|
+
t: new Date().toISOString(),
|
|
85
|
+
kind: 'rule-fire',
|
|
86
|
+
deviceId: resolveActionDevice(action.device, parsed.deviceIdSlot, ctx.aliases) ?? 'unknown',
|
|
87
|
+
command: action.command,
|
|
88
|
+
parameter: null,
|
|
89
|
+
commandType: 'command',
|
|
90
|
+
dryRun: true,
|
|
91
|
+
result: 'error',
|
|
92
|
+
error: `destructive-verb:${parsed.verb}`,
|
|
93
|
+
rule: {
|
|
94
|
+
name: ctx.rule.name,
|
|
95
|
+
triggerSource: ctx.rule.when.source,
|
|
96
|
+
fireId: ctx.fireId,
|
|
97
|
+
reason: `destructive verb "${parsed.verb}" refused at runtime`,
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
return { ok: false, error: `destructive-verb:${parsed.verb}`, blocked: true, verb: parsed.verb };
|
|
101
|
+
}
|
|
102
|
+
const deviceId = resolveActionDevice(action.device, parsed.deviceIdSlot, ctx.aliases);
|
|
103
|
+
if (!deviceId || deviceId === '<id>') {
|
|
104
|
+
writeAudit({
|
|
105
|
+
t: new Date().toISOString(),
|
|
106
|
+
kind: 'rule-fire',
|
|
107
|
+
deviceId: 'unknown',
|
|
108
|
+
command: action.command,
|
|
109
|
+
parameter: null,
|
|
110
|
+
commandType: 'command',
|
|
111
|
+
dryRun: true,
|
|
112
|
+
result: 'error',
|
|
113
|
+
error: 'missing-device',
|
|
114
|
+
rule: {
|
|
115
|
+
name: ctx.rule.name,
|
|
116
|
+
triggerSource: ctx.rule.when.source,
|
|
117
|
+
fireId: ctx.fireId,
|
|
118
|
+
reason: 'action omitted `device` and command used `<id>` placeholder',
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
return { ok: false, error: 'missing-device', verb: parsed.verb };
|
|
122
|
+
}
|
|
123
|
+
const dryRun = ctx.globalDryRun === true || ctx.rule.dry_run === true;
|
|
124
|
+
const parameter = renderParameter(parsed.parameterTokens);
|
|
125
|
+
if (dryRun) {
|
|
126
|
+
writeAudit({
|
|
127
|
+
t: new Date().toISOString(),
|
|
128
|
+
kind: 'rule-fire-dry',
|
|
129
|
+
deviceId,
|
|
130
|
+
command: parsed.verb,
|
|
131
|
+
parameter: parameter ?? 'default',
|
|
132
|
+
commandType: 'command',
|
|
133
|
+
dryRun: true,
|
|
134
|
+
result: 'ok',
|
|
135
|
+
rule: {
|
|
136
|
+
name: ctx.rule.name,
|
|
137
|
+
triggerSource: ctx.rule.when.source,
|
|
138
|
+
matchedDevice: deviceId,
|
|
139
|
+
fireId: ctx.fireId,
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
return { ok: true, dryRun: true, deviceId, verb: parsed.verb };
|
|
143
|
+
}
|
|
144
|
+
if (ctx.skipApiCall) {
|
|
145
|
+
writeAudit({
|
|
146
|
+
t: new Date().toISOString(),
|
|
147
|
+
kind: 'rule-fire',
|
|
148
|
+
deviceId,
|
|
149
|
+
command: parsed.verb,
|
|
150
|
+
parameter: parameter ?? 'default',
|
|
151
|
+
commandType: 'command',
|
|
152
|
+
dryRun: false,
|
|
153
|
+
result: 'ok',
|
|
154
|
+
rule: {
|
|
155
|
+
name: ctx.rule.name,
|
|
156
|
+
triggerSource: ctx.rule.when.source,
|
|
157
|
+
matchedDevice: deviceId,
|
|
158
|
+
fireId: ctx.fireId,
|
|
159
|
+
reason: 'api-skipped',
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
return { ok: true, deviceId, verb: parsed.verb };
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
await executeCommand(deviceId, parsed.verb, parameter, 'command', ctx.httpClient);
|
|
166
|
+
writeAudit({
|
|
167
|
+
t: new Date().toISOString(),
|
|
168
|
+
kind: 'rule-fire',
|
|
169
|
+
deviceId,
|
|
170
|
+
command: parsed.verb,
|
|
171
|
+
parameter: parameter ?? 'default',
|
|
172
|
+
commandType: 'command',
|
|
173
|
+
dryRun: false,
|
|
174
|
+
result: 'ok',
|
|
175
|
+
rule: {
|
|
176
|
+
name: ctx.rule.name,
|
|
177
|
+
triggerSource: ctx.rule.when.source,
|
|
178
|
+
matchedDevice: deviceId,
|
|
179
|
+
fireId: ctx.fireId,
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
return { ok: true, deviceId, verb: parsed.verb };
|
|
183
|
+
}
|
|
184
|
+
catch (err) {
|
|
185
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
186
|
+
writeAudit({
|
|
187
|
+
t: new Date().toISOString(),
|
|
188
|
+
kind: 'rule-fire',
|
|
189
|
+
deviceId,
|
|
190
|
+
command: parsed.verb,
|
|
191
|
+
parameter: parameter ?? 'default',
|
|
192
|
+
commandType: 'command',
|
|
193
|
+
dryRun: false,
|
|
194
|
+
result: 'error',
|
|
195
|
+
error: msg,
|
|
196
|
+
rule: {
|
|
197
|
+
name: ctx.rule.name,
|
|
198
|
+
triggerSource: ctx.rule.when.source,
|
|
199
|
+
matchedDevice: deviceId,
|
|
200
|
+
fireId: ctx.fireId,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
return { ok: false, error: msg, deviceId, verb: parsed.verb };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared filters + aggregations over the audit log for
|
|
3
|
+
* `switchbot rules tail` and `switchbot rules replay`.
|
|
4
|
+
*
|
|
5
|
+
* All functions are pure — no I/O, no clock reads — so they can be
|
|
6
|
+
* unit-tested with fixture arrays. The CLI entry points handle file
|
|
7
|
+
* reading, `--follow` tailing, and human vs JSON rendering.
|
|
8
|
+
*/
|
|
9
|
+
/** The subset of audit kinds the rules engine emits. */
|
|
10
|
+
export const RULE_AUDIT_KINDS = [
|
|
11
|
+
'rule-fire',
|
|
12
|
+
'rule-fire-dry',
|
|
13
|
+
'rule-throttled',
|
|
14
|
+
'rule-webhook-rejected',
|
|
15
|
+
];
|
|
16
|
+
/** Keep entries that are rule-engine emitted and match the filter. */
|
|
17
|
+
export function filterRuleAudits(entries, filter = {}) {
|
|
18
|
+
const kinds = new Set(filter.kinds ?? RULE_AUDIT_KINDS);
|
|
19
|
+
const out = [];
|
|
20
|
+
for (const e of entries) {
|
|
21
|
+
if (!kinds.has(e.kind))
|
|
22
|
+
continue;
|
|
23
|
+
if (filter.sinceMs !== undefined) {
|
|
24
|
+
const ms = Date.parse(e.t);
|
|
25
|
+
if (!Number.isFinite(ms) || ms < filter.sinceMs)
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (filter.ruleName !== undefined) {
|
|
29
|
+
if (e.rule?.name !== filter.ruleName)
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
out.push(e);
|
|
33
|
+
}
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
/** Aggregate a filtered stream into per-rule counters. */
|
|
37
|
+
export function aggregateRuleAudits(entries) {
|
|
38
|
+
const byRule = new Map();
|
|
39
|
+
let webhookRejectedCount = 0;
|
|
40
|
+
for (const e of entries) {
|
|
41
|
+
if (e.kind === 'rule-webhook-rejected' && !e.rule) {
|
|
42
|
+
webhookRejectedCount++;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const name = e.rule?.name;
|
|
46
|
+
if (!name)
|
|
47
|
+
continue;
|
|
48
|
+
let s = byRule.get(name);
|
|
49
|
+
if (!s) {
|
|
50
|
+
s = {
|
|
51
|
+
rule: name,
|
|
52
|
+
fires: 0,
|
|
53
|
+
driesFires: 0,
|
|
54
|
+
throttled: 0,
|
|
55
|
+
errors: 0,
|
|
56
|
+
errorRate: 0,
|
|
57
|
+
firstAt: null,
|
|
58
|
+
lastAt: null,
|
|
59
|
+
triggerSource: null,
|
|
60
|
+
};
|
|
61
|
+
byRule.set(name, s);
|
|
62
|
+
}
|
|
63
|
+
if (e.kind === 'rule-fire')
|
|
64
|
+
s.fires++;
|
|
65
|
+
else if (e.kind === 'rule-fire-dry')
|
|
66
|
+
s.driesFires++;
|
|
67
|
+
else if (e.kind === 'rule-throttled')
|
|
68
|
+
s.throttled++;
|
|
69
|
+
if (e.result === 'error')
|
|
70
|
+
s.errors++;
|
|
71
|
+
if (!s.firstAt || e.t < s.firstAt)
|
|
72
|
+
s.firstAt = e.t;
|
|
73
|
+
if (!s.lastAt || e.t > s.lastAt)
|
|
74
|
+
s.lastAt = e.t;
|
|
75
|
+
const source = e.rule?.triggerSource;
|
|
76
|
+
if (source) {
|
|
77
|
+
if (s.triggerSource === null)
|
|
78
|
+
s.triggerSource = source;
|
|
79
|
+
else if (s.triggerSource !== source)
|
|
80
|
+
s.triggerSource = 'mixed';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const s of byRule.values()) {
|
|
84
|
+
const denom = s.fires + s.driesFires;
|
|
85
|
+
s.errorRate = denom === 0 ? 0 : s.errors / denom;
|
|
86
|
+
}
|
|
87
|
+
const summaries = [...byRule.values()].sort((a, b) => b.fires + b.driesFires - (a.fires + a.driesFires));
|
|
88
|
+
return { total: entries.length, summaries, webhookRejectedCount };
|
|
89
|
+
}
|