run402 4.7.0 → 4.9.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/cli.mjs +6 -6
- package/lib/admin.mjs +17 -24
- package/lib/apps.mjs +68 -50
- package/lib/argparse.mjs +68 -1
- package/lib/assets.mjs +6 -8
- package/lib/billing.mjs +15 -11
- package/lib/branches.mjs +20 -17
- package/lib/ci.mjs +2 -0
- package/lib/cloud.mjs +25 -18
- package/lib/command-manifest.mjs +352 -0
- package/lib/contracts.mjs +47 -10
- package/lib/deploy-v2.mjs +21 -8
- package/lib/functions.mjs +99 -54
- package/lib/grants.mjs +39 -20
- package/lib/jobs.mjs +15 -9
- package/lib/notifications.mjs +431 -11
- package/lib/org.mjs +86 -48
- package/lib/projects.mjs +67 -38
- package/lib/secrets.mjs +98 -40
- package/lib/snapshots.mjs +20 -18
- package/lib/wallets.mjs +4 -3
- package/lib/webhook-secret.mjs +6 -0
- package/package.json +1 -1
- package/sdk/dist/namespaces/admin.d.ts +254 -4
- package/sdk/dist/namespaces/admin.d.ts.map +1 -1
- package/sdk/dist/namespaces/admin.js +134 -4
- package/sdk/dist/namespaces/admin.js.map +1 -1
- package/sdk/dist/namespaces/projects.d.ts +2 -1
- package/sdk/dist/namespaces/projects.d.ts.map +1 -1
- package/sdk/dist/namespaces/projects.js +8 -12
- package/sdk/dist/namespaces/projects.js.map +1 -1
package/lib/notifications.mjs
CHANGED
|
@@ -1,32 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* run402 notifications — Operator health notifications.
|
|
2
|
+
* run402 notifications — Operator health notifications + Telegram channel/rules.
|
|
3
3
|
*
|
|
4
4
|
* Wraps the v1.55 `add-operator-health-notifications` API surface:
|
|
5
5
|
*
|
|
6
6
|
* - run402 notifications list [--type ...] [--since ...] [--limit N] [--after <cursor>]
|
|
7
7
|
* - run402 notifications get <id>
|
|
8
8
|
* - run402 notifications preferences [get|set ...]
|
|
9
|
-
* - run402 notifications test
|
|
9
|
+
* - run402 notifications test [--source app|platform] [--type <event_type>]
|
|
10
10
|
* - run402 webhook-secret rotate
|
|
11
11
|
*
|
|
12
|
+
* ...and the `notification-channel-routing-telegram` cascade (self-serve
|
|
13
|
+
* Telegram push + per-rule routing on top of the substrate above):
|
|
14
|
+
*
|
|
15
|
+
* - run402 notifications channels connect telegram [--label <name>]
|
|
16
|
+
* - run402 notifications channels list
|
|
17
|
+
* - run402 notifications channels revoke <binding_id>
|
|
18
|
+
* - run402 notifications rules add --binding <id> [--project <id>] [--source app|platform] [--type a,b] [--class a,b]
|
|
19
|
+
* - run402 notifications rules list
|
|
20
|
+
* - run402 notifications rules rm <rule_id>
|
|
21
|
+
*
|
|
12
22
|
* All commands use the allowance-wallet auth path (SIWX). The assurance
|
|
13
|
-
* ladder is enforced server-side; CLI surfaces 403 errors
|
|
14
|
-
*
|
|
23
|
+
* ladder is enforced server-side; CLI surfaces 403/412/503 errors verbatim
|
|
24
|
+
* (message + code + next_actions) via reportSdkError — no client-side
|
|
25
|
+
* special-casing needed, e.g. a not-yet-provisioned Telegram bot surfaces as
|
|
26
|
+
* 503 TELEGRAM_CHANNEL_NOT_CONFIGURED with its next_actions intact.
|
|
27
|
+
*
|
|
28
|
+
* `channels`/`rules` are nested groups dispatched via `if (sub === ...)`
|
|
29
|
+
* (not `case`), mirroring cli/lib/org.mjs's `member`/`invite` groups — see
|
|
30
|
+
* sync.test.ts's parseNotificationsGroupActions for why.
|
|
15
31
|
*/
|
|
16
32
|
|
|
17
33
|
import { allowanceAuthHeaders } from "./config.mjs";
|
|
18
34
|
import { getSdk } from "./sdk.mjs";
|
|
19
35
|
import { reportSdkError, fail } from "./sdk-errors.mjs";
|
|
20
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
assertAllowedValue,
|
|
38
|
+
assertKnownFlags,
|
|
39
|
+
flagValue,
|
|
40
|
+
normalizeArgv,
|
|
41
|
+
positionalArgs,
|
|
42
|
+
requirePositionalCount,
|
|
43
|
+
} from "./argparse.mjs";
|
|
21
44
|
|
|
22
|
-
const HELP = `run402 notifications — Operator health notifications
|
|
45
|
+
const HELP = `run402 notifications — Operator health notifications + Telegram channel/rules
|
|
23
46
|
|
|
24
47
|
Usage:
|
|
25
48
|
run402 notifications list [--type <event_type>] [--since <iso>] [--limit N] [--after <cursor>]
|
|
26
49
|
run402 notifications get <id>
|
|
27
50
|
run402 notifications preferences
|
|
28
51
|
run402 notifications preferences set <key>=<value> [<key>=<value> ...]
|
|
29
|
-
run402 notifications test
|
|
52
|
+
run402 notifications test [--source app|platform] [--type <event_type>]
|
|
53
|
+
run402 notifications channels connect telegram [--label <name>]
|
|
54
|
+
run402 notifications channels list
|
|
55
|
+
run402 notifications channels revoke <binding_id>
|
|
56
|
+
run402 notifications rules add --binding <binding_id> [--project <id>] [--source app|platform] [--type a,b] [--class a,b]
|
|
57
|
+
run402 notifications rules list
|
|
58
|
+
run402 notifications rules rm <rule_id>
|
|
30
59
|
|
|
31
60
|
Examples:
|
|
32
61
|
run402 notifications list --limit 10
|
|
@@ -36,13 +65,112 @@ Examples:
|
|
|
36
65
|
run402 notifications preferences set digest_cadence=weekly digest_day_of_week=1
|
|
37
66
|
run402 notifications preferences set webhook_url=https://my-receiver.example.com/hook
|
|
38
67
|
run402 notifications test
|
|
68
|
+
run402 notifications test --source app --type signature_failed
|
|
69
|
+
run402 notifications channels connect telegram --label "kychon alerts"
|
|
70
|
+
run402 notifications channels list
|
|
71
|
+
run402 notifications rules add --binding bnd_1 --project prj_abc --source app --type signature_failed
|
|
39
72
|
|
|
40
73
|
Auth ladder (enforced server-side):
|
|
41
74
|
- SIWX wallet: read own notifications + preferences
|
|
42
75
|
- email_verified: cross-wallet rollup reads + multi-wallet preference updates
|
|
43
|
-
- operator_passkey: webhook URL changes, webhook secret rotation
|
|
76
|
+
- operator_passkey: webhook URL changes, webhook secret rotation, Telegram
|
|
77
|
+
channel connect/revoke, routing rule add/rm (channel
|
|
78
|
+
connect additionally requires a VERIFIED operator email)
|
|
79
|
+
|
|
80
|
+
Telegram channels + routing rules teach their own rule model in detail via
|
|
81
|
+
'run402 notifications channels --help' / 'run402 notifications rules --help'
|
|
82
|
+
(AND'd match dimensions, wildcards, one rule -> one chat, no rules -> no
|
|
83
|
+
Telegram traffic).
|
|
44
84
|
`;
|
|
45
85
|
|
|
86
|
+
const CHANNELS_HELP = `run402 notifications channels — Telegram channel bindings
|
|
87
|
+
|
|
88
|
+
Usage:
|
|
89
|
+
run402 notifications channels connect telegram [--label <name>]
|
|
90
|
+
run402 notifications channels list
|
|
91
|
+
run402 notifications channels revoke <binding_id>
|
|
92
|
+
|
|
93
|
+
connect telegram:
|
|
94
|
+
Creates a PENDING binding and prints two single-use, 15-minute deep links:
|
|
95
|
+
a private-chat link and a group-chat link. Open ONE of them in Telegram and
|
|
96
|
+
tap Start — whichever is tapped first consumes the code. This command then
|
|
97
|
+
POLLS 'channels list' (every few seconds, printing progress to stderr)
|
|
98
|
+
until the binding flips to active, or the code expires, whichever happens
|
|
99
|
+
first. Requires operator_passkey assurance AND a VERIFIED operator email
|
|
100
|
+
(bindings are addressed to it — see 'run402 agent verify-email').
|
|
101
|
+
|
|
102
|
+
--label <name> Human-readable name for the chat (e.g. "kychon alerts"),
|
|
103
|
+
1-64 characters.
|
|
104
|
+
|
|
105
|
+
list:
|
|
106
|
+
Shows every notification channel — email, webhook, and every live
|
|
107
|
+
(non-revoked) Telegram binding — for the authenticated wallet.
|
|
108
|
+
|
|
109
|
+
revoke <binding_id>:
|
|
110
|
+
Revokes a Telegram binding. Requires operator_passkey assurance. A
|
|
111
|
+
missing / already-revoked / another operator's binding id all return the
|
|
112
|
+
same not-found error (no existence oracle).
|
|
113
|
+
|
|
114
|
+
Until the platform's dedicated Telegram bot is provisioned on this
|
|
115
|
+
deployment, 'connect telegram' returns 503 TELEGRAM_CHANNEL_NOT_CONFIGURED
|
|
116
|
+
with a next_actions entry pointing at the operator to finish setup.
|
|
117
|
+
|
|
118
|
+
Examples:
|
|
119
|
+
run402 notifications channels connect telegram
|
|
120
|
+
run402 notifications channels connect telegram --label "kychon alerts"
|
|
121
|
+
run402 notifications channels list
|
|
122
|
+
run402 notifications channels revoke bnd_1a2b3c
|
|
123
|
+
`;
|
|
124
|
+
|
|
125
|
+
const RULES_HELP = `run402 notifications rules — Telegram routing rules
|
|
126
|
+
|
|
127
|
+
Usage:
|
|
128
|
+
run402 notifications rules add --binding <binding_id> [--project <id>] [--source app|platform] [--type a,b] [--class a,b]
|
|
129
|
+
run402 notifications rules list
|
|
130
|
+
run402 notifications rules rm <rule_id>
|
|
131
|
+
|
|
132
|
+
The rule model:
|
|
133
|
+
- One rule routes to exactly ONE Telegram binding (one chat) — "N
|
|
134
|
+
destinations" is N rules, never a fan-out list on a single rule.
|
|
135
|
+
- Every match dimension you set is ANDed: --project + --source + --type +
|
|
136
|
+
--class must ALL match an event for the rule to fire.
|
|
137
|
+
- An OMITTED dimension is a WILDCARD (matches anything for that
|
|
138
|
+
dimension). 'rules add --binding bnd_1' with no other flags matches
|
|
139
|
+
EVERY event routed to that operator.
|
|
140
|
+
- --type and --class accept comma-separated lists and match if the event
|
|
141
|
+
is ANY of the listed values, e.g.
|
|
142
|
+
--type signature_failed,signature_expired.
|
|
143
|
+
- --source is "app" (your deployed function's events.emit(...) calls from
|
|
144
|
+
@run402/functions) or "platform" (deploys, lifecycle, verification,
|
|
145
|
+
...). Omit it to match both.
|
|
146
|
+
- Multiple rules OR together across an event; if two of YOUR rules both
|
|
147
|
+
resolve to the SAME binding for one event, only one message is sent
|
|
148
|
+
(deduped per chat, not per rule).
|
|
149
|
+
- NO RULES = NO TELEGRAM TRAFFIC for that operator. The Telegram channel
|
|
150
|
+
is opt-in per event, per rule — there is no "send everything" default,
|
|
151
|
+
unlike the always-on email channel.
|
|
152
|
+
- Rules govern the Telegram channel ONLY (v1). The mandatory email floor
|
|
153
|
+
(security / recovery / billing_critical / destructive_lifecycle /
|
|
154
|
+
verification classes) is completely untouched by this command and can
|
|
155
|
+
NEVER be silenced by adding or omitting a rule.
|
|
156
|
+
|
|
157
|
+
Requires operator_passkey assurance for add/rm. 'rules add' rejects an
|
|
158
|
+
unusable telegram_binding_id (revoked / not yours / nonexistent) with the
|
|
159
|
+
same 404 either way (authorize-before-reveal) — run 'channels list' first to
|
|
160
|
+
confirm the binding is 'active'.
|
|
161
|
+
|
|
162
|
+
Examples:
|
|
163
|
+
run402 notifications rules add --binding bnd_1
|
|
164
|
+
run402 notifications rules add --binding bnd_1 --project prj_abc --source app --type signature_failed
|
|
165
|
+
run402 notifications rules add --binding bnd_1 --class security,recovery
|
|
166
|
+
run402 notifications rules list
|
|
167
|
+
run402 notifications rules rm rule_9b21fa0a
|
|
168
|
+
`;
|
|
169
|
+
|
|
170
|
+
// ---------------------------------------------------------------------------
|
|
171
|
+
// Operator health notifications (v1.55) — list / get / preferences / test.
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
|
|
46
174
|
async function list(args) {
|
|
47
175
|
const parsedArgs = normalizeArgv(args);
|
|
48
176
|
const valueFlags = ["--type", "--since", "--limit", "--after"];
|
|
@@ -133,25 +261,317 @@ async function preferences(args) {
|
|
|
133
261
|
|
|
134
262
|
async function test(args) {
|
|
135
263
|
const parsedArgs = normalizeArgv(args);
|
|
136
|
-
|
|
137
|
-
|
|
264
|
+
const valueFlags = ["--source", "--type"];
|
|
265
|
+
assertKnownFlags(parsedArgs, [...valueFlags, "--help", "-h"], valueFlags);
|
|
266
|
+
const extra = positionalArgs(parsedArgs, valueFlags);
|
|
138
267
|
if (extra.length > 0) {
|
|
139
268
|
fail({ code: "BAD_USAGE", message: `Unexpected argument for notifications test: ${extra[0]}` });
|
|
140
269
|
}
|
|
270
|
+
const source = flagValue(parsedArgs, "--source");
|
|
271
|
+
if (source !== null) assertAllowedValue(source, ["app", "platform"], "--source");
|
|
272
|
+
const eventType = flagValue(parsedArgs, "--type");
|
|
141
273
|
allowanceAuthHeaders("/agent/v1/notifications/test");
|
|
274
|
+
const opts = {};
|
|
275
|
+
if (source) opts.source = source;
|
|
276
|
+
if (eventType) opts.eventType = eventType;
|
|
142
277
|
try {
|
|
143
|
-
const data = await getSdk().admin.testNotification();
|
|
278
|
+
const data = await getSdk().admin.testNotification(opts);
|
|
144
279
|
console.log(JSON.stringify(data, null, 2));
|
|
145
280
|
} catch (err) {
|
|
146
281
|
reportSdkError(err);
|
|
147
282
|
}
|
|
148
283
|
}
|
|
149
284
|
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
// Telegram channel bindings — notification-channel-routing-telegram.
|
|
287
|
+
// ---------------------------------------------------------------------------
|
|
288
|
+
|
|
289
|
+
const TELEGRAM_CONNECT_POLL_INTERVAL_MS = 3000;
|
|
290
|
+
/** Fallback wait bound if the server's code_expires_at is somehow
|
|
291
|
+
* unparseable — the connect code's real TTL is 15 minutes server-side. */
|
|
292
|
+
const TELEGRAM_CONNECT_FALLBACK_TIMEOUT_MS = 20 * 60 * 1000;
|
|
293
|
+
/** Print a progress line every Nth poll tick (~15s at the interval above) —
|
|
294
|
+
* frequent enough to reassure, not so frequent it floods stderr. */
|
|
295
|
+
const TELEGRAM_CONNECT_PROGRESS_EVERY_N_TICKS = 5;
|
|
296
|
+
|
|
297
|
+
function sleep(ms) {
|
|
298
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Poll `GET /agent/v1/notifications/channels` until `bindingId` shows
|
|
303
|
+
* `status: "active"`, or the connect code's `codeExpiresAt` passes.
|
|
304
|
+
* Transient poll failures (network blips) are logged and retried rather
|
|
305
|
+
* than aborting the whole connect flow — bounded by the same expiry.
|
|
306
|
+
*/
|
|
307
|
+
async function pollTelegramBindingActive(bindingId, codeExpiresAt) {
|
|
308
|
+
const parsedExpiry = Date.parse(codeExpiresAt);
|
|
309
|
+
const deadlineMs = Number.isFinite(parsedExpiry) ? parsedExpiry : Date.now() + TELEGRAM_CONNECT_FALLBACK_TIMEOUT_MS;
|
|
310
|
+
let tick = 0;
|
|
311
|
+
for (;;) {
|
|
312
|
+
await sleep(TELEGRAM_CONNECT_POLL_INTERVAL_MS);
|
|
313
|
+
tick += 1;
|
|
314
|
+
let channels;
|
|
315
|
+
try {
|
|
316
|
+
channels = await getSdk().admin.channels.list();
|
|
317
|
+
} catch (err) {
|
|
318
|
+
console.error(` (poll failed, retrying: ${err?.message || err})`);
|
|
319
|
+
if (Date.now() >= deadlineMs) return { active: false, timedOut: true, binding: null };
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const binding = (channels.telegram || []).find((b) => b.id === bindingId) || null;
|
|
323
|
+
if (binding && binding.status === "active") {
|
|
324
|
+
return { active: true, binding };
|
|
325
|
+
}
|
|
326
|
+
if (Date.now() >= deadlineMs) {
|
|
327
|
+
return { active: false, timedOut: true, binding };
|
|
328
|
+
}
|
|
329
|
+
if (tick % TELEGRAM_CONNECT_PROGRESS_EVERY_N_TICKS === 1) {
|
|
330
|
+
const remainingSec = Math.max(0, Math.round((deadlineMs - Date.now()) / 1000));
|
|
331
|
+
const status = binding ? binding.status : "pending";
|
|
332
|
+
console.error(` waiting for you to tap the link in Telegram... (status: ${status}, ${remainingSec}s left)`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async function channelsConnect(args) {
|
|
338
|
+
const a = normalizeArgv(args);
|
|
339
|
+
assertKnownFlags(a, ["--label", "--help", "-h"], ["--label"]);
|
|
340
|
+
const label = flagValue(a, "--label");
|
|
341
|
+
const positionals = positionalArgs(a, ["--label"]);
|
|
342
|
+
if (positionals.length === 0) {
|
|
343
|
+
fail({
|
|
344
|
+
code: "BAD_USAGE",
|
|
345
|
+
message: "Usage: run402 notifications channels connect telegram [--label <name>]",
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
if (positionals[0] !== "telegram") {
|
|
349
|
+
fail({
|
|
350
|
+
code: "BAD_USAGE",
|
|
351
|
+
message: `Unknown channel type: ${positionals[0]}. Only 'telegram' is supported.`,
|
|
352
|
+
details: { channel_type: positionals[0] },
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
if (positionals.length > 1) {
|
|
356
|
+
fail({ code: "BAD_USAGE", message: `Unexpected argument for notifications channels connect: ${positionals[1]}` });
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
allowanceAuthHeaders("/agent/v1/notifications/channels/telegram");
|
|
360
|
+
let pending;
|
|
361
|
+
try {
|
|
362
|
+
pending = await getSdk().admin.channels.connectTelegram(label ? { label } : {});
|
|
363
|
+
} catch (err) {
|
|
364
|
+
reportSdkError(err);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
console.error("");
|
|
369
|
+
console.error("Open ONE of these links in Telegram and tap Start to connect this channel:");
|
|
370
|
+
console.error(` Private chat: ${pending.connect_url}`);
|
|
371
|
+
console.error(` Group chat: ${pending.connect_group_url}`);
|
|
372
|
+
console.error(` Expires: ${pending.code_expires_at}`);
|
|
373
|
+
console.error("");
|
|
374
|
+
console.error("Waiting for you to tap the link...");
|
|
375
|
+
|
|
376
|
+
const outcome = await pollTelegramBindingActive(pending.binding_id, pending.code_expires_at);
|
|
377
|
+
if (outcome.active) {
|
|
378
|
+
console.error("Connected.");
|
|
379
|
+
console.log(JSON.stringify({ ...pending, connected: true, binding: outcome.binding }, null, 2));
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
console.error("Timed out waiting for you to tap the link — the connect code has expired.");
|
|
383
|
+
console.error("Run `run402 notifications channels connect telegram` again for a fresh link.");
|
|
384
|
+
console.log(JSON.stringify({ ...pending, connected: false, timed_out: true }, null, 2));
|
|
385
|
+
process.exit(1);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async function channelsList() {
|
|
389
|
+
allowanceAuthHeaders("/agent/v1/notifications/channels");
|
|
390
|
+
try {
|
|
391
|
+
console.log(JSON.stringify(await getSdk().admin.channels.list(), null, 2));
|
|
392
|
+
} catch (err) {
|
|
393
|
+
reportSdkError(err);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
async function channelsRevoke(args) {
|
|
398
|
+
const a = normalizeArgv(args);
|
|
399
|
+
assertKnownFlags(a, ["--help", "-h"]);
|
|
400
|
+
const [bindingId] = requirePositionalCount(a, [], {
|
|
401
|
+
min: 1,
|
|
402
|
+
max: 1,
|
|
403
|
+
command: "run402 notifications channels revoke <binding_id>",
|
|
404
|
+
missing: "Missing <binding_id>.",
|
|
405
|
+
});
|
|
406
|
+
allowanceAuthHeaders("/agent/v1/notifications/channels/telegram");
|
|
407
|
+
try {
|
|
408
|
+
console.log(JSON.stringify(await getSdk().admin.channels.revokeTelegram(bindingId), null, 2));
|
|
409
|
+
} catch (err) {
|
|
410
|
+
reportSdkError(err);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
async function runChannels(args) {
|
|
415
|
+
const channelsAction = args[0];
|
|
416
|
+
const rest = args.slice(1);
|
|
417
|
+
if (!channelsAction || channelsAction === "--help" || channelsAction === "-h") {
|
|
418
|
+
console.log(CHANNELS_HELP);
|
|
419
|
+
process.exit(channelsAction ? 0 : 1);
|
|
420
|
+
}
|
|
421
|
+
if (rest.includes("--help") || rest.includes("-h")) {
|
|
422
|
+
console.log(CHANNELS_HELP);
|
|
423
|
+
process.exit(0);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (channelsAction === "connect") {
|
|
427
|
+
await channelsConnect(rest);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (channelsAction === "list") {
|
|
431
|
+
await channelsList();
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (channelsAction === "revoke") {
|
|
435
|
+
await channelsRevoke(rest);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
fail({
|
|
439
|
+
code: "BAD_USAGE",
|
|
440
|
+
message: `Unknown 'notifications channels' action: ${channelsAction}. Try connect | list | revoke.`,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// ---------------------------------------------------------------------------
|
|
445
|
+
// Telegram routing rules — notification-channel-routing-telegram.
|
|
446
|
+
// ---------------------------------------------------------------------------
|
|
447
|
+
|
|
448
|
+
function splitCsv(value) {
|
|
449
|
+
return value
|
|
450
|
+
.split(",")
|
|
451
|
+
.map((s) => s.trim())
|
|
452
|
+
.filter((s) => s.length > 0);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
async function rulesAdd(args) {
|
|
456
|
+
const a = normalizeArgv(args);
|
|
457
|
+
const valueFlags = ["--binding", "--project", "--source", "--type", "--class"];
|
|
458
|
+
assertKnownFlags(a, [...valueFlags, "--help", "-h"], valueFlags);
|
|
459
|
+
const positionals = positionalArgs(a, valueFlags);
|
|
460
|
+
if (positionals.length > 0) {
|
|
461
|
+
fail({ code: "BAD_USAGE", message: `Unexpected argument for notifications rules add: ${positionals[0]}` });
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const bindingId = flagValue(a, "--binding");
|
|
465
|
+
if (!bindingId) {
|
|
466
|
+
fail({
|
|
467
|
+
code: "BAD_USAGE",
|
|
468
|
+
message:
|
|
469
|
+
"Usage: run402 notifications rules add --binding <binding_id> [--project <id>] [--source app|platform] [--type a,b] [--class a,b]",
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
const projectId = flagValue(a, "--project");
|
|
473
|
+
const source = flagValue(a, "--source");
|
|
474
|
+
if (source !== null) assertAllowedValue(source, ["app", "platform"], "--source");
|
|
475
|
+
const typeRaw = flagValue(a, "--type");
|
|
476
|
+
const classRaw = flagValue(a, "--class");
|
|
477
|
+
|
|
478
|
+
const input = { telegramBindingId: bindingId };
|
|
479
|
+
if (projectId) input.projectId = projectId;
|
|
480
|
+
if (source) input.source = source;
|
|
481
|
+
if (typeRaw !== null) input.eventTypes = splitCsv(typeRaw);
|
|
482
|
+
if (classRaw !== null) input.classes = splitCsv(classRaw);
|
|
483
|
+
|
|
484
|
+
allowanceAuthHeaders("/agent/v1/notifications/rules");
|
|
485
|
+
try {
|
|
486
|
+
console.log(JSON.stringify(await getSdk().admin.rules.create(input), null, 2));
|
|
487
|
+
} catch (err) {
|
|
488
|
+
reportSdkError(err);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
async function rulesList() {
|
|
493
|
+
allowanceAuthHeaders("/agent/v1/notifications/rules");
|
|
494
|
+
try {
|
|
495
|
+
console.log(JSON.stringify(await getSdk().admin.rules.list(), null, 2));
|
|
496
|
+
} catch (err) {
|
|
497
|
+
reportSdkError(err);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
async function rulesRm(args) {
|
|
502
|
+
const a = normalizeArgv(args);
|
|
503
|
+
assertKnownFlags(a, ["--help", "-h"]);
|
|
504
|
+
const [ruleId] = requirePositionalCount(a, [], {
|
|
505
|
+
min: 1,
|
|
506
|
+
max: 1,
|
|
507
|
+
command: "run402 notifications rules rm <rule_id>",
|
|
508
|
+
missing: "Missing <rule_id>.",
|
|
509
|
+
});
|
|
510
|
+
allowanceAuthHeaders("/agent/v1/notifications/rules");
|
|
511
|
+
try {
|
|
512
|
+
console.log(JSON.stringify(await getSdk().admin.rules.delete(ruleId), null, 2));
|
|
513
|
+
} catch (err) {
|
|
514
|
+
reportSdkError(err);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
async function runRules(args) {
|
|
519
|
+
const rulesAction = args[0];
|
|
520
|
+
const rest = args.slice(1);
|
|
521
|
+
if (!rulesAction || rulesAction === "--help" || rulesAction === "-h") {
|
|
522
|
+
console.log(RULES_HELP);
|
|
523
|
+
process.exit(rulesAction ? 0 : 1);
|
|
524
|
+
}
|
|
525
|
+
if (rest.includes("--help") || rest.includes("-h")) {
|
|
526
|
+
console.log(RULES_HELP);
|
|
527
|
+
process.exit(0);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (rulesAction === "add") {
|
|
531
|
+
await rulesAdd(rest);
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (rulesAction === "list") {
|
|
535
|
+
await rulesList();
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (rulesAction === "rm") {
|
|
539
|
+
await rulesRm(rest);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
fail({
|
|
543
|
+
code: "BAD_USAGE",
|
|
544
|
+
message: `Unknown 'notifications rules' action: ${rulesAction}. Try add | list | rm.`,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// ---------------------------------------------------------------------------
|
|
549
|
+
// Dispatch.
|
|
550
|
+
// ---------------------------------------------------------------------------
|
|
551
|
+
|
|
150
552
|
export async function run(sub, args) {
|
|
151
553
|
if (!sub || sub === "--help" || sub === "-h") {
|
|
152
554
|
console.log(HELP);
|
|
153
555
|
process.exit(0);
|
|
154
556
|
}
|
|
557
|
+
// Nested groups use `if (sub === ...)` (not `case`) so the sync test
|
|
558
|
+
// extracts their leaf actions via the dedicated channelsAction/rulesAction
|
|
559
|
+
// parsers, mirroring cli/lib/org.mjs's member/invite groups.
|
|
560
|
+
if (sub === "channels") {
|
|
561
|
+
await runChannels(args ?? []);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (sub === "rules") {
|
|
565
|
+
await runRules(args ?? []);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
// Flat subcommands share the module-level HELP: --help/-h anywhere in the
|
|
569
|
+
// argv must short-circuit BEFORE the handlers touch auth/config (the
|
|
570
|
+
// cli-help.test.mjs contract) — same pre-switch check as cli/lib/org.mjs.
|
|
571
|
+
if (Array.isArray(args) && (args.includes("--help") || args.includes("-h"))) {
|
|
572
|
+
console.log(HELP);
|
|
573
|
+
process.exit(0);
|
|
574
|
+
}
|
|
155
575
|
switch (sub) {
|
|
156
576
|
case "list":
|
|
157
577
|
await list(args);
|