activitysmith-cli 1.0.0 → 1.1.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 +290 -29
- package/package.json +3 -3
- package/src/cli.js +213 -7
package/README.md
CHANGED
|
@@ -85,7 +85,24 @@ activitysmith push \
|
|
|
85
85
|
--title "Build Failed 🚨" \
|
|
86
86
|
--message "CI pipeline failed on main branch" \
|
|
87
87
|
--redirection "https://github.com/org/repo/actions/runs/123456789" \
|
|
88
|
-
--actions '[
|
|
88
|
+
--actions '[
|
|
89
|
+
{
|
|
90
|
+
"title": "Open Failing Run",
|
|
91
|
+
"type": "open_url",
|
|
92
|
+
"url": "https://github.com/org/repo/actions/runs/123456789"
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
"title": "Create Incident",
|
|
96
|
+
"type": "webhook",
|
|
97
|
+
"url": "https://hooks.example.com/incidents/create",
|
|
98
|
+
"method": "POST",
|
|
99
|
+
"body": {
|
|
100
|
+
"service": "payments-api",
|
|
101
|
+
"severity": "high",
|
|
102
|
+
"source": "activitysmith-cli"
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]'
|
|
89
106
|
```
|
|
90
107
|
|
|
91
108
|
You can also load actions from a file:
|
|
@@ -99,14 +116,127 @@ activitysmith push \
|
|
|
99
116
|
|
|
100
117
|
## Live Activities
|
|
101
118
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
<p align="center">
|
|
120
|
+
<img src="https://cdn.activitysmith.com/features/metrics-live-activity-action.png" alt="Live Activities example" width="680" />
|
|
121
|
+
</p>
|
|
122
|
+
|
|
123
|
+
ActivitySmith supports two ways to drive Live Activities:
|
|
124
|
+
|
|
125
|
+
- Recommended: stream updates with `activitysmith activity stream ...`
|
|
126
|
+
- Advanced: manual lifecycle control with `activity start`, `activity update`, and `activity end`
|
|
127
|
+
|
|
128
|
+
Use stream updates when you want the easiest, stateless flow. You do not need to
|
|
129
|
+
store `activity_id` or manage lifecycle state yourself. Send the latest state
|
|
130
|
+
for a stable `stream_key` and ActivitySmith will start or update the Live
|
|
131
|
+
Activity for you. When the tracked process is over, call `activity end-stream`.
|
|
132
|
+
|
|
133
|
+
Use the manual lifecycle commands when you need direct control over a specific
|
|
134
|
+
Live Activity instance.
|
|
135
|
+
|
|
136
|
+
Live Activity UI types:
|
|
137
|
+
|
|
138
|
+
- `metrics`: best for live operational stats like server CPU and memory, queue depth, or replica lag
|
|
139
|
+
- `segmented_progress`: best for step-based workflows like deployments, backups, and ETL pipelines
|
|
140
|
+
- `progress`: best for continuous jobs like uploads, reindexes, and long-running migrations tracked as a percentage
|
|
141
|
+
|
|
142
|
+
### Recommended: Stream updates
|
|
143
|
+
|
|
144
|
+
Use a stable `stream_key` to identify the system or workflow you are tracking,
|
|
145
|
+
such as a server, deployment, build pipeline, cron job, or charging session.
|
|
146
|
+
This is especially useful for cron jobs and other scheduled tasks where you do
|
|
147
|
+
not want to store `activity_id` between runs.
|
|
148
|
+
|
|
149
|
+
#### Metrics
|
|
150
|
+
|
|
151
|
+
<p align="center">
|
|
152
|
+
<img src="https://cdn.activitysmith.com/features/metrics-live-activity-start.png" alt="Metrics stream example" width="680" />
|
|
153
|
+
</p>
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
activitysmith activity stream prod-web-1 \
|
|
157
|
+
--content-state '{
|
|
158
|
+
"title": "Server Health",
|
|
159
|
+
"subtitle": "prod-web-1",
|
|
160
|
+
"type": "metrics",
|
|
161
|
+
"metrics": [
|
|
162
|
+
{ "label": "CPU", "value": 9, "unit": "%" },
|
|
163
|
+
{ "label": "MEM", "value": 45, "unit": "%" }
|
|
164
|
+
]
|
|
165
|
+
}'
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Segmented progress
|
|
169
|
+
|
|
170
|
+
<p align="center">
|
|
171
|
+
<img src="https://cdn.activitysmith.com/features/update-live-activity.png" alt="Segmented progress stream example" width="680" />
|
|
172
|
+
</p>
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
activitysmith activity stream nightly-backup \
|
|
176
|
+
--content-state '{
|
|
177
|
+
"title": "Nightly Backup",
|
|
178
|
+
"subtitle": "upload archive",
|
|
179
|
+
"type": "segmented_progress",
|
|
180
|
+
"numberOfSteps": 3,
|
|
181
|
+
"currentStep": 2
|
|
182
|
+
}'
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
#### Progress
|
|
186
|
+
|
|
187
|
+
<p align="center">
|
|
188
|
+
<img src="https://cdn.activitysmith.com/features/progress-live-activity.png" alt="Progress stream example" width="680" />
|
|
189
|
+
</p>
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
activitysmith activity stream search-reindex \
|
|
193
|
+
--content-state '{
|
|
194
|
+
"title": "Search Reindex",
|
|
195
|
+
"subtitle": "catalog-v2",
|
|
196
|
+
"type": "progress",
|
|
197
|
+
"percentage": 42
|
|
198
|
+
}'
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Run `activitysmith activity stream <stream-key> ...` again with the same
|
|
202
|
+
`stream_key` whenever the state changes.
|
|
203
|
+
|
|
204
|
+
#### End a stream
|
|
205
|
+
|
|
206
|
+
Use this when the tracked process is finished and you no longer want the Live
|
|
207
|
+
Activity on devices. `content_state` is optional here; include it if you want
|
|
208
|
+
to end the stream with a final state.
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
activitysmith activity end-stream prod-web-1 \
|
|
212
|
+
--content-state '{
|
|
213
|
+
"title": "Server Health",
|
|
214
|
+
"subtitle": "prod-web-1",
|
|
215
|
+
"type": "metrics",
|
|
216
|
+
"metrics": [
|
|
217
|
+
{ "label": "CPU", "value": 7, "unit": "%" },
|
|
218
|
+
{ "label": "MEM", "value": 38, "unit": "%" }
|
|
219
|
+
]
|
|
220
|
+
}'
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
If you later send another `activity stream` request with the same `stream_key`,
|
|
224
|
+
ActivitySmith starts a new Live Activity for that stream again.
|
|
105
225
|
|
|
106
|
-
|
|
107
|
-
- `progress`: best for jobs tracked as a percentage or numeric range
|
|
226
|
+
Stream responses include an `operation` field:
|
|
108
227
|
|
|
109
|
-
|
|
228
|
+
- `started`: ActivitySmith started a new Live Activity for this `stream_key`
|
|
229
|
+
- `updated`: ActivitySmith updated the current Live Activity
|
|
230
|
+
- `rotated`: ActivitySmith ended the previous Live Activity and started a new one
|
|
231
|
+
- `noop`: the incoming state matched the current state, so no update was sent
|
|
232
|
+
- `paused`: the stream is paused, so no Live Activity was started or updated
|
|
233
|
+
- `ended`: returned by `activity end-stream` after the stream is ended
|
|
234
|
+
|
|
235
|
+
### Advanced: Manual lifecycle control
|
|
236
|
+
|
|
237
|
+
Use these commands when you want to manage the Live Activity lifecycle yourself.
|
|
238
|
+
|
|
239
|
+
#### Shared flow
|
|
110
240
|
|
|
111
241
|
1. Run `activitysmith activity start ...`.
|
|
112
242
|
2. Save the returned `activity_id`.
|
|
@@ -116,13 +246,77 @@ changes, then end it when the work is done.
|
|
|
116
246
|
You can use `--content-state <json>` for the examples below, or build the same
|
|
117
247
|
payload with flags as documented in `Content State Options`.
|
|
118
248
|
|
|
249
|
+
### Metrics Type
|
|
250
|
+
|
|
251
|
+
Use `metrics` when you want to keep a small set of live stats visible, such as
|
|
252
|
+
server health, queue pressure, or database load.
|
|
253
|
+
|
|
254
|
+
#### Start
|
|
255
|
+
|
|
256
|
+
<p align="center">
|
|
257
|
+
<img src="https://cdn.activitysmith.com/features/metrics-live-activity-start.png" alt="Metrics start example" width="680" />
|
|
258
|
+
</p>
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
activitysmith activity start \
|
|
262
|
+
--content-state '{
|
|
263
|
+
"title": "Server Health",
|
|
264
|
+
"subtitle": "prod-web-1",
|
|
265
|
+
"type": "metrics",
|
|
266
|
+
"metrics": [
|
|
267
|
+
{ "label": "CPU", "value": 9, "unit": "%" },
|
|
268
|
+
{ "label": "MEM", "value": 45, "unit": "%" }
|
|
269
|
+
]
|
|
270
|
+
}'
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
#### Update
|
|
274
|
+
|
|
275
|
+
<p align="center">
|
|
276
|
+
<img src="https://cdn.activitysmith.com/features/metrics-live-activity-update.png" alt="Metrics update example" width="680" />
|
|
277
|
+
</p>
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
activitysmith activity update \
|
|
281
|
+
--activity-id "<activityId>" \
|
|
282
|
+
--content-state '{
|
|
283
|
+
"title": "Server Health",
|
|
284
|
+
"subtitle": "prod-web-1",
|
|
285
|
+
"type": "metrics",
|
|
286
|
+
"metrics": [
|
|
287
|
+
{ "label": "CPU", "value": 76, "unit": "%" },
|
|
288
|
+
{ "label": "MEM", "value": 52, "unit": "%" }
|
|
289
|
+
]
|
|
290
|
+
}'
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
#### End
|
|
294
|
+
|
|
295
|
+
<p align="center">
|
|
296
|
+
<img src="https://cdn.activitysmith.com/features/metrics-live-activity-end.png" alt="Metrics end example" width="680" />
|
|
297
|
+
</p>
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
activitysmith activity end \
|
|
301
|
+
--activity-id "<activityId>" \
|
|
302
|
+
--content-state '{
|
|
303
|
+
"title": "Server Health",
|
|
304
|
+
"subtitle": "prod-web-1",
|
|
305
|
+
"type": "metrics",
|
|
306
|
+
"metrics": [
|
|
307
|
+
{ "label": "CPU", "value": 7, "unit": "%" },
|
|
308
|
+
{ "label": "MEM", "value": 38, "unit": "%" }
|
|
309
|
+
],
|
|
310
|
+
"autoDismissMinutes": 2
|
|
311
|
+
}'
|
|
312
|
+
```
|
|
313
|
+
|
|
119
314
|
### Segmented Progress Type
|
|
120
315
|
|
|
121
316
|
Use `segmented_progress` when progress is easier to follow as steps instead of a
|
|
122
317
|
raw percentage. It fits jobs like deployments, backups, ETL pipelines, and
|
|
123
|
-
checklists where "step 2 of 3" is more useful than "67%".
|
|
124
|
-
|
|
125
|
-
workflow changes.
|
|
318
|
+
checklists where "step 2 of 3" is more useful than "67%". `numberOfSteps` is
|
|
319
|
+
dynamic, so you can increase or decrease it later if the workflow changes.
|
|
126
320
|
|
|
127
321
|
#### Start
|
|
128
322
|
|
|
@@ -132,8 +326,14 @@ workflow changes.
|
|
|
132
326
|
|
|
133
327
|
```bash
|
|
134
328
|
activitysmith activity start \
|
|
135
|
-
--content-state '{
|
|
136
|
-
|
|
329
|
+
--content-state '{
|
|
330
|
+
"title": "Nightly database backup",
|
|
331
|
+
"subtitle": "create snapshot",
|
|
332
|
+
"numberOfSteps": 3,
|
|
333
|
+
"currentStep": 1,
|
|
334
|
+
"type": "segmented_progress",
|
|
335
|
+
"color": "yellow"
|
|
336
|
+
}'
|
|
137
337
|
```
|
|
138
338
|
|
|
139
339
|
#### Update
|
|
@@ -145,7 +345,12 @@ activitysmith activity start \
|
|
|
145
345
|
```bash
|
|
146
346
|
activitysmith activity update \
|
|
147
347
|
--activity-id "<activityId>" \
|
|
148
|
-
--content-state '{
|
|
348
|
+
--content-state '{
|
|
349
|
+
"title": "Nightly database backup",
|
|
350
|
+
"subtitle": "upload archive",
|
|
351
|
+
"numberOfSteps": 3,
|
|
352
|
+
"currentStep": 2
|
|
353
|
+
}'
|
|
149
354
|
```
|
|
150
355
|
|
|
151
356
|
#### End
|
|
@@ -157,7 +362,13 @@ activitysmith activity update \
|
|
|
157
362
|
```bash
|
|
158
363
|
activitysmith activity end \
|
|
159
364
|
--activity-id "<activityId>" \
|
|
160
|
-
--content-state '{
|
|
365
|
+
--content-state '{
|
|
366
|
+
"title": "Nightly database backup",
|
|
367
|
+
"subtitle": "verify restore",
|
|
368
|
+
"numberOfSteps": 3,
|
|
369
|
+
"currentStep": 3,
|
|
370
|
+
"autoDismissMinutes": 2
|
|
371
|
+
}'
|
|
161
372
|
```
|
|
162
373
|
|
|
163
374
|
### Progress Type
|
|
@@ -174,7 +385,12 @@ numeric range is the clearest signal.
|
|
|
174
385
|
|
|
175
386
|
```bash
|
|
176
387
|
activitysmith activity start \
|
|
177
|
-
--content-state '{
|
|
388
|
+
--content-state '{
|
|
389
|
+
"title": "EV Charging",
|
|
390
|
+
"subtitle": "Added 30 mi range",
|
|
391
|
+
"type": "progress",
|
|
392
|
+
"percentage": 15
|
|
393
|
+
}'
|
|
178
394
|
```
|
|
179
395
|
|
|
180
396
|
#### Update
|
|
@@ -186,7 +402,11 @@ activitysmith activity start \
|
|
|
186
402
|
```bash
|
|
187
403
|
activitysmith activity update \
|
|
188
404
|
--activity-id "<activityId>" \
|
|
189
|
-
--content-state '{
|
|
405
|
+
--content-state '{
|
|
406
|
+
"title": "EV Charging",
|
|
407
|
+
"subtitle": "Added 120 mi range",
|
|
408
|
+
"percentage": 60
|
|
409
|
+
}'
|
|
190
410
|
```
|
|
191
411
|
|
|
192
412
|
#### End
|
|
@@ -198,23 +418,40 @@ activitysmith activity update \
|
|
|
198
418
|
```bash
|
|
199
419
|
activitysmith activity end \
|
|
200
420
|
--activity-id "<activityId>" \
|
|
201
|
-
--content-state '{
|
|
421
|
+
--content-state '{
|
|
422
|
+
"title": "EV Charging",
|
|
423
|
+
"subtitle": "Added 200 mi range",
|
|
424
|
+
"percentage": 100,
|
|
425
|
+
"autoDismissMinutes": 2
|
|
426
|
+
}'
|
|
202
427
|
```
|
|
203
428
|
|
|
204
429
|
### Live Activity Action
|
|
205
430
|
|
|
206
|
-
Just like Actionable Push Notifications, Live Activities can have a button that opens
|
|
431
|
+
Just like Actionable Push Notifications, Live Activities can have a button that opens a URL in a browser or triggers a webhook. Webhooks are executed by the ActivitySmith backend.
|
|
207
432
|
|
|
208
433
|
<p align="center">
|
|
209
|
-
<img src="https://cdn.activitysmith.com/features/live-activity-
|
|
434
|
+
<img src="https://cdn.activitysmith.com/features/metrics-live-activity-action.png" alt="Metrics Live Activity with action" width="680" />
|
|
210
435
|
</p>
|
|
211
436
|
|
|
212
437
|
#### Open URL action
|
|
213
438
|
|
|
214
439
|
```bash
|
|
215
440
|
activitysmith activity start \
|
|
216
|
-
--content-state '{
|
|
217
|
-
|
|
441
|
+
--content-state '{
|
|
442
|
+
"title": "Server Health",
|
|
443
|
+
"subtitle": "prod-web-1",
|
|
444
|
+
"type": "metrics",
|
|
445
|
+
"metrics": [
|
|
446
|
+
{ "label": "CPU", "value": 76, "unit": "%" },
|
|
447
|
+
{ "label": "MEM", "value": 52, "unit": "%" }
|
|
448
|
+
]
|
|
449
|
+
}' \
|
|
450
|
+
--action '{
|
|
451
|
+
"title": "Open Dashboard",
|
|
452
|
+
"type": "open_url",
|
|
453
|
+
"url": "https://ops.example.com/servers/prod-web-1"
|
|
454
|
+
}'
|
|
218
455
|
```
|
|
219
456
|
|
|
220
457
|
#### Webhook action
|
|
@@ -222,8 +459,25 @@ activitysmith activity start \
|
|
|
222
459
|
```bash
|
|
223
460
|
activitysmith activity update \
|
|
224
461
|
--activity-id "<activityId>" \
|
|
225
|
-
--content-state '{
|
|
226
|
-
|
|
462
|
+
--content-state '{
|
|
463
|
+
"title": "Server Health",
|
|
464
|
+
"subtitle": "prod-web-1",
|
|
465
|
+
"type": "metrics",
|
|
466
|
+
"metrics": [
|
|
467
|
+
{ "label": "CPU", "value": 91, "unit": "%" },
|
|
468
|
+
{ "label": "MEM", "value": 57, "unit": "%" }
|
|
469
|
+
]
|
|
470
|
+
}' \
|
|
471
|
+
--action '{
|
|
472
|
+
"title": "Restart Service",
|
|
473
|
+
"type": "webhook",
|
|
474
|
+
"url": "https://ops.example.com/hooks/servers/prod-web-1/restart",
|
|
475
|
+
"method": "POST",
|
|
476
|
+
"body": {
|
|
477
|
+
"server_id": "prod-web-1",
|
|
478
|
+
"requested_by": "activitysmith-cli"
|
|
479
|
+
}
|
|
480
|
+
}'
|
|
227
481
|
```
|
|
228
482
|
|
|
229
483
|
## Channels
|
|
@@ -246,12 +500,17 @@ The CLI installs two bin names:
|
|
|
246
500
|
|
|
247
501
|
## Content State Options
|
|
248
502
|
|
|
249
|
-
For `activity start|update|end`, you can pass content state via JSON:
|
|
503
|
+
For `activity stream|start|update|end|end-stream`, you can pass content state via JSON:
|
|
250
504
|
|
|
251
505
|
- `--content-state <json>`
|
|
252
506
|
- `--content-state-file <path>`
|
|
253
507
|
|
|
254
|
-
|
|
508
|
+
For `metrics`, you can also pass the metrics array directly:
|
|
509
|
+
|
|
510
|
+
- `--metrics <json-array>`
|
|
511
|
+
- `--metrics-file <path>`
|
|
512
|
+
|
|
513
|
+
Or use flags to build the rest of the payload:
|
|
255
514
|
|
|
256
515
|
- `--title <title>`
|
|
257
516
|
- `--subtitle <subtitle>`
|
|
@@ -272,13 +531,15 @@ Live Activity action options:
|
|
|
272
531
|
|
|
273
532
|
Targeting options:
|
|
274
533
|
|
|
275
|
-
- `--channels <comma-separated-slugs>` (for `push` and `activity start`)
|
|
534
|
+
- `--channels <comma-separated-slugs>` (for `push`, `activity stream`, and `activity start`)
|
|
276
535
|
|
|
277
536
|
Required fields:
|
|
278
537
|
|
|
279
|
-
- `activity
|
|
280
|
-
- `activity
|
|
281
|
-
- `activity
|
|
538
|
+
- `activity stream`: `--title`, `--type`, plus `--metrics`, `--number-of-steps` and `--current-step`, `--percentage`, or `--value` with `--upper-limit`
|
|
539
|
+
- `activity start`: `--title`, `--type`, plus `--metrics`, `--number-of-steps` and `--current-step`, `--percentage`, or `--value` with `--upper-limit`
|
|
540
|
+
- `activity update`: `--title`, plus `--metrics`, `--current-step`, `--percentage`, or `--value` with `--upper-limit`
|
|
541
|
+
- `activity end`: `--title`, plus `--metrics`, `--current-step`, `--percentage`, or `--value` with `--upper-limit`
|
|
542
|
+
- `activity end-stream`: no content state is required, but if you provide one it follows the same rules as `activity end`
|
|
282
543
|
|
|
283
544
|
## Output
|
|
284
545
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "activitysmith-cli",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Command-line interface for ActivitySmith. Send push notifications
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Command-line interface for ActivitySmith. Send push notifications and manage Live Activities from your terminal.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"activitysmith",
|
|
7
7
|
"live-activities",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"lint": "node --check src/cli.js"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"activitysmith": "^1.
|
|
43
|
+
"activitysmith": "^1.1.0",
|
|
44
44
|
"commander": "^12.1.0"
|
|
45
45
|
}
|
|
46
46
|
}
|
package/src/cli.js
CHANGED
|
@@ -77,6 +77,8 @@ const addContentStateOptions = (command, { includeAutoDismiss } = {}) => {
|
|
|
77
77
|
command
|
|
78
78
|
.option("--content-state <json>", "Content state as JSON string")
|
|
79
79
|
.option("--content-state-file <path>", "Content state JSON file path")
|
|
80
|
+
.option("--metrics <json>", "Content state metrics as JSON array")
|
|
81
|
+
.option("--metrics-file <path>", "Content state metrics JSON file path")
|
|
80
82
|
.option("--title <title>", "Content state title")
|
|
81
83
|
.option("--subtitle <subtitle>", "Content state subtitle")
|
|
82
84
|
.option("--type <type>", "Content state type")
|
|
@@ -221,10 +223,11 @@ const validateContentState = (contentState, mode) => {
|
|
|
221
223
|
if (
|
|
222
224
|
normalizedType !== undefined &&
|
|
223
225
|
normalizedType !== "segmented_progress" &&
|
|
224
|
-
normalizedType !== "progress"
|
|
226
|
+
normalizedType !== "progress" &&
|
|
227
|
+
normalizedType !== "metrics"
|
|
225
228
|
) {
|
|
226
229
|
throw new Error(
|
|
227
|
-
"contentState.type must be one of: segmented_progress, progress"
|
|
230
|
+
"contentState.type must be one of: segmented_progress, progress, metrics"
|
|
228
231
|
);
|
|
229
232
|
}
|
|
230
233
|
|
|
@@ -234,6 +237,7 @@ const validateContentState = (contentState, mode) => {
|
|
|
234
237
|
const hasValue = hasOwn(contentState, "value");
|
|
235
238
|
const hasUpperLimit = hasOwn(contentState, "upperLimit");
|
|
236
239
|
const hasStepColor = hasOwn(contentState, "stepColor");
|
|
240
|
+
const hasMetrics = hasOwn(contentState, "metrics");
|
|
237
241
|
|
|
238
242
|
if (hasValue !== hasUpperLimit) {
|
|
239
243
|
throw new Error(
|
|
@@ -269,11 +273,36 @@ const validateContentState = (contentState, mode) => {
|
|
|
269
273
|
);
|
|
270
274
|
}
|
|
271
275
|
|
|
276
|
+
if (hasMetrics && (hasSegmentedFields || hasProgressFields)) {
|
|
277
|
+
throw new Error(
|
|
278
|
+
"Do not mix metrics fields with segmented_progress or progress fields in the same contentState"
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (hasMetrics) {
|
|
283
|
+
if (!Array.isArray(contentState.metrics) || contentState.metrics.length === 0) {
|
|
284
|
+
throw new Error("contentState.metrics must be a non-empty array");
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
contentState.metrics.forEach((metric, index) => {
|
|
288
|
+
assertPlainObject(metric, `contentState.metrics[${index}]`);
|
|
289
|
+
if (!isNonEmptyString(metric.label)) {
|
|
290
|
+
throw new Error(`contentState.metrics[${index}].label is required`);
|
|
291
|
+
}
|
|
292
|
+
if (!Number.isFinite(metric.value)) {
|
|
293
|
+
throw new Error(`contentState.metrics[${index}].value must be a number`);
|
|
294
|
+
}
|
|
295
|
+
if (metric.unit !== undefined && typeof metric.unit !== "string") {
|
|
296
|
+
throw new Error(`contentState.metrics[${index}].unit must be a string`);
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
272
301
|
const effectiveType = normalizedType;
|
|
273
302
|
|
|
274
|
-
if (mode === "start") {
|
|
303
|
+
if (mode === "start" || mode === "stream") {
|
|
275
304
|
if (!effectiveType) {
|
|
276
|
-
throw new Error(
|
|
305
|
+
throw new Error(`contentState.type is required for activity ${mode}`);
|
|
277
306
|
}
|
|
278
307
|
|
|
279
308
|
if (effectiveType === "segmented_progress") {
|
|
@@ -285,9 +314,16 @@ const validateContentState = (contentState, mode) => {
|
|
|
285
314
|
return;
|
|
286
315
|
}
|
|
287
316
|
|
|
317
|
+
if (effectiveType === "metrics") {
|
|
318
|
+
if (!hasMetrics) {
|
|
319
|
+
throw new Error(`metrics ${mode} requires contentState.metrics`);
|
|
320
|
+
}
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
|
|
288
324
|
if (!hasPercentage && !hasValue) {
|
|
289
325
|
throw new Error(
|
|
290
|
-
|
|
326
|
+
`progress ${mode} requires contentState.percentage, or contentState.value with contentState.upperLimit`
|
|
291
327
|
);
|
|
292
328
|
}
|
|
293
329
|
return;
|
|
@@ -311,12 +347,23 @@ const validateContentState = (contentState, mode) => {
|
|
|
311
347
|
return;
|
|
312
348
|
}
|
|
313
349
|
|
|
314
|
-
if (
|
|
350
|
+
if (effectiveType === "metrics") {
|
|
351
|
+
if (!hasMetrics) {
|
|
352
|
+
throw new Error(`metrics ${mode} requires contentState.metrics`);
|
|
353
|
+
}
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (!hasSegmentedFields && !hasProgressFields && !hasMetrics) {
|
|
315
358
|
throw new Error(
|
|
316
|
-
`contentState for activity ${mode} must include
|
|
359
|
+
`contentState for activity ${mode} must include metrics, segmented_progress fields, or progress fields`
|
|
317
360
|
);
|
|
318
361
|
}
|
|
319
362
|
|
|
363
|
+
if (hasMetrics) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
320
367
|
if (hasSegmentedFields && !hasCurrentStep) {
|
|
321
368
|
throw new Error(
|
|
322
369
|
`segmented_progress ${mode} requires contentState.currentStep`
|
|
@@ -487,6 +534,22 @@ const buildContentStateFromOptions = (options) => {
|
|
|
487
534
|
return contentState;
|
|
488
535
|
};
|
|
489
536
|
|
|
537
|
+
const loadMetrics = async (options) => {
|
|
538
|
+
if (options.metrics && options.metricsFile) {
|
|
539
|
+
throw new Error("Provide either --metrics or --metrics-file, not both.");
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (options.metricsFile) {
|
|
543
|
+
return readJsonArrayFile(options.metricsFile, "metrics-file");
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (options.metrics) {
|
|
547
|
+
return parseJsonArrayString(options.metrics, "metrics");
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
return undefined;
|
|
551
|
+
};
|
|
552
|
+
|
|
490
553
|
const toApiContentState = (contentState) => {
|
|
491
554
|
const keyMap = {
|
|
492
555
|
numberOfSteps: "number_of_steps",
|
|
@@ -561,6 +624,32 @@ const toApiLiveActivityEndRequest = (activityId, contentState, action) => {
|
|
|
561
624
|
return request;
|
|
562
625
|
};
|
|
563
626
|
|
|
627
|
+
const toApiLiveActivityStreamRequest = (contentState, action) => {
|
|
628
|
+
const request = {
|
|
629
|
+
content_state: toApiContentState(contentState),
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
if (action !== undefined) {
|
|
633
|
+
request.action = action;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
return request;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const toApiLiveActivityStreamDeleteRequest = (contentState, action) => {
|
|
640
|
+
const request = {};
|
|
641
|
+
|
|
642
|
+
if (contentState !== undefined) {
|
|
643
|
+
request.content_state = toApiContentState(contentState);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
if (action !== undefined) {
|
|
647
|
+
request.action = action;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
return request;
|
|
651
|
+
};
|
|
652
|
+
|
|
564
653
|
const loadContentState = async (options, mode) => {
|
|
565
654
|
let contentState = {};
|
|
566
655
|
|
|
@@ -583,6 +672,11 @@ const loadContentState = async (options, mode) => {
|
|
|
583
672
|
const fromFlags = buildContentStateFromOptions(options);
|
|
584
673
|
contentState = { ...contentState, ...fromFlags };
|
|
585
674
|
|
|
675
|
+
const metrics = await loadMetrics(options);
|
|
676
|
+
if (metrics !== undefined) {
|
|
677
|
+
contentState.metrics = metrics;
|
|
678
|
+
}
|
|
679
|
+
|
|
586
680
|
if (Object.keys(contentState).length === 0) {
|
|
587
681
|
throw new Error(
|
|
588
682
|
"contentState is required. Provide --content-state, --content-state-file, or field flags."
|
|
@@ -594,6 +688,41 @@ const loadContentState = async (options, mode) => {
|
|
|
594
688
|
return contentState;
|
|
595
689
|
};
|
|
596
690
|
|
|
691
|
+
const loadOptionalContentState = async (options, mode) => {
|
|
692
|
+
let contentState = {};
|
|
693
|
+
|
|
694
|
+
if (options.contentStateFile) {
|
|
695
|
+
const fromFile = await readJsonFile(
|
|
696
|
+
options.contentStateFile,
|
|
697
|
+
"content-state-file"
|
|
698
|
+
);
|
|
699
|
+
contentState = { ...contentState, ...fromFile };
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (options.contentState) {
|
|
703
|
+
const fromString = parseJsonString(
|
|
704
|
+
options.contentState,
|
|
705
|
+
"content-state"
|
|
706
|
+
);
|
|
707
|
+
contentState = { ...contentState, ...fromString };
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const fromFlags = buildContentStateFromOptions(options);
|
|
711
|
+
contentState = { ...contentState, ...fromFlags };
|
|
712
|
+
|
|
713
|
+
const metrics = await loadMetrics(options);
|
|
714
|
+
if (metrics !== undefined) {
|
|
715
|
+
contentState.metrics = metrics;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
if (Object.keys(contentState).length === 0) {
|
|
719
|
+
return undefined;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
validateContentState(contentState, mode);
|
|
723
|
+
return contentState;
|
|
724
|
+
};
|
|
725
|
+
|
|
597
726
|
const formatError = (error) => {
|
|
598
727
|
if (!error) {
|
|
599
728
|
return "Unknown error";
|
|
@@ -778,6 +907,47 @@ const activityCommand = program
|
|
|
778
907
|
.command("activity")
|
|
779
908
|
.description("Manage Live Activities");
|
|
780
909
|
|
|
910
|
+
addLiveActivityActionOptions(addContentStateOptions(
|
|
911
|
+
activityCommand
|
|
912
|
+
.command("stream")
|
|
913
|
+
.description("Send a stateless Live Activity stream update")
|
|
914
|
+
.argument("<stream-key>", "Stable stream key")
|
|
915
|
+
.option(
|
|
916
|
+
"--channels <channels>",
|
|
917
|
+
"Comma-separated channel slugs (optional)",
|
|
918
|
+
parseChannelsOption
|
|
919
|
+
)
|
|
920
|
+
.action(async (streamKey, options) => {
|
|
921
|
+
const globalOptions = program.opts();
|
|
922
|
+
|
|
923
|
+
try {
|
|
924
|
+
const apiKey = requireApiKey(globalOptions);
|
|
925
|
+
const client = createClient(apiKey);
|
|
926
|
+
const contentState = await loadContentState(options, "stream");
|
|
927
|
+
const action = await loadLiveActivityAction(options);
|
|
928
|
+
|
|
929
|
+
const response = await client.liveActivities.stream(
|
|
930
|
+
streamKey,
|
|
931
|
+
withTargetChannels(
|
|
932
|
+
toApiLiveActivityStreamRequest(contentState, action),
|
|
933
|
+
options.channels
|
|
934
|
+
)
|
|
935
|
+
);
|
|
936
|
+
|
|
937
|
+
const activityId = response?.activityId ?? response?.activity_id;
|
|
938
|
+
const operation = response?.operation;
|
|
939
|
+
outputResult(response, globalOptions, [
|
|
940
|
+
"Live Activity stream reconciled.",
|
|
941
|
+
streamKey ? `Stream key: ${streamKey}` : null,
|
|
942
|
+
operation ? `Operation: ${operation}` : null,
|
|
943
|
+
activityId ? `Activity ID: ${activityId}` : null,
|
|
944
|
+
]);
|
|
945
|
+
} catch (error) {
|
|
946
|
+
await handleError(error, globalOptions);
|
|
947
|
+
}
|
|
948
|
+
})
|
|
949
|
+
));
|
|
950
|
+
|
|
781
951
|
addLiveActivityActionOptions(addContentStateOptions(
|
|
782
952
|
activityCommand
|
|
783
953
|
.command("start")
|
|
@@ -879,5 +1049,41 @@ addLiveActivityActionOptions(addContentStateOptions(
|
|
|
879
1049
|
{ includeAutoDismiss: true }
|
|
880
1050
|
));
|
|
881
1051
|
|
|
1052
|
+
addLiveActivityActionOptions(addContentStateOptions(
|
|
1053
|
+
activityCommand
|
|
1054
|
+
.command("end-stream")
|
|
1055
|
+
.description("End a stateless Live Activity stream")
|
|
1056
|
+
.argument("<stream-key>", "Stable stream key")
|
|
1057
|
+
.action(async (streamKey, options) => {
|
|
1058
|
+
const globalOptions = program.opts();
|
|
1059
|
+
|
|
1060
|
+
try {
|
|
1061
|
+
const apiKey = requireApiKey(globalOptions);
|
|
1062
|
+
const client = createClient(apiKey);
|
|
1063
|
+
const contentState = await loadOptionalContentState(options, "end");
|
|
1064
|
+
const action = await loadLiveActivityAction(options);
|
|
1065
|
+
|
|
1066
|
+
const request =
|
|
1067
|
+
contentState !== undefined || action !== undefined
|
|
1068
|
+
? toApiLiveActivityStreamDeleteRequest(contentState, action)
|
|
1069
|
+
: undefined;
|
|
1070
|
+
|
|
1071
|
+
const response = await client.liveActivities.endStream(streamKey, request);
|
|
1072
|
+
|
|
1073
|
+
const activityId = response?.activityId ?? response?.activity_id;
|
|
1074
|
+
const operation = response?.operation;
|
|
1075
|
+
outputResult(response, globalOptions, [
|
|
1076
|
+
"Live Activity stream ended.",
|
|
1077
|
+
streamKey ? `Stream key: ${streamKey}` : null,
|
|
1078
|
+
operation ? `Operation: ${operation}` : null,
|
|
1079
|
+
activityId ? `Activity ID: ${activityId}` : null,
|
|
1080
|
+
]);
|
|
1081
|
+
} catch (error) {
|
|
1082
|
+
await handleError(error, globalOptions);
|
|
1083
|
+
}
|
|
1084
|
+
}),
|
|
1085
|
+
{ includeAutoDismiss: true }
|
|
1086
|
+
));
|
|
1087
|
+
|
|
882
1088
|
program.showHelpAfterError(true);
|
|
883
1089
|
program.parseAsync(process.argv);
|