activitysmith-cli 0.1.5 → 0.1.7

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.
Files changed (3) hide show
  1. package/README.md +114 -33
  2. package/package.json +2 -2
  3. package/src/cli.js +309 -12
package/README.md CHANGED
@@ -40,67 +40,145 @@ activitysmith --help
40
40
 
41
41
  ```bash
42
42
  activitysmith push \
43
- --title "Build Failed" \
44
- --message "CI pipeline failed on main branch" \
45
- --channels "devs,ops"
43
+ --title "Build Failed 🚨" \
44
+ --message "CI pipeline failed on main branch"
46
45
  ```
47
46
 
48
- ### Start Live Activity
47
+ ## Live Activities
48
+
49
+ Live Activities come in two UI types, but the lifecycle stays the same:
50
+ start the activity, keep the returned `activity_id`, update it as state
51
+ changes, then end it when the work is done.
52
+
53
+ - `segmented_progress`: best for jobs tracked in steps
54
+ - `progress`: best for jobs tracked as a percentage or numeric range
55
+
56
+ ### Shared flow
57
+
58
+ 1. Run `activitysmith activity start ...`.
59
+ 2. Save the returned `activity_id`.
60
+ 3. Run `activitysmith activity update ...` as progress changes.
61
+ 4. Run `activitysmith activity end ...` when the work is finished.
62
+
63
+ You can use `--content-state <json>` for the examples below, or build the same
64
+ payload with flags as documented in `Content State Options`.
65
+
66
+ ### Segmented Progress Type
67
+
68
+ Use `segmented_progress` when progress is easier to follow as steps instead of a
69
+ raw percentage. It fits jobs like deployments, backups, ETL pipelines, and
70
+ checklists where "step 2 of 3" is more useful than "67%".
71
+ `numberOfSteps` is dynamic, so you can increase or decrease it later if the
72
+ workflow changes.
73
+
74
+ #### Start
75
+
76
+ <p align="center">
77
+ <img src="https://cdn.activitysmith.com/features/start-live-activity.png" alt="Segmented progress start example" width="680" />
78
+ </p>
49
79
 
50
80
  ```bash
51
81
  activitysmith activity start \
52
- --content-state '{"title":"Deploy","subtitle":"start","numberOfSteps":4,"currentStep":1,"type":"segmented_progress","color":"yellow"}' \
82
+ --content-state '{"title":"Nightly database backup","subtitle":"create snapshot","numberOfSteps":3,"currentStep":1,"type":"segmented_progress","color":"yellow"}' \
53
83
  --channels "devs,ops"
54
84
  ```
55
85
 
56
- Or use flags:
86
+ #### Update
87
+
88
+ <p align="center">
89
+ <img src="https://cdn.activitysmith.com/features/update-live-activity.png" alt="Segmented progress update example" width="680" />
90
+ </p>
57
91
 
58
92
  ```bash
59
- activitysmith activity start \
60
- --title "Deploy" \
61
- --subtitle "start" \
62
- --number-of-steps 4 \
63
- --current-step 1 \
64
- --type segmented_progress \
65
- --color yellow \
66
- --channels "devs,ops"
93
+ activitysmith activity update \
94
+ --activity-id "<activityId>" \
95
+ --content-state '{"title":"Nightly database backup","subtitle":"upload archive","numberOfSteps":4,"currentStep":2}'
67
96
  ```
68
97
 
69
- ### Update Live Activity
98
+ #### End
99
+
100
+ <p align="center">
101
+ <img src="https://cdn.activitysmith.com/features/end-live-activity.png" alt="Segmented progress end example" width="680" />
102
+ </p>
70
103
 
71
104
  ```bash
72
- activitysmith activity update \
105
+ activitysmith activity end \
73
106
  --activity-id "<activityId>" \
74
- --content-state '{"title":"Deploy","subtitle":"step 2","currentStep":2}'
107
+ --content-state '{"title":"Nightly database backup","subtitle":"verify restore","numberOfSteps":4,"currentStep":4,"autoDismissMinutes":2}'
108
+ ```
109
+
110
+ ### Progress Type
111
+
112
+ Use `progress` when the state is naturally continuous. It fits charging,
113
+ downloads, sync jobs, uploads, timers, and any flow where a percentage or
114
+ numeric range is the clearest signal.
115
+
116
+ #### Start
117
+
118
+ <p align="center">
119
+ <img src="https://cdn.activitysmith.com/features/progress-live-activity-start.png" alt="Progress start example" width="680" />
120
+ </p>
121
+
122
+ ```bash
123
+ activitysmith activity start \
124
+ --content-state '{"title":"EV Charging","subtitle":"Added 30 mi range","percentage":15,"type":"progress","color":"lime"}'
75
125
  ```
76
126
 
77
- Or use flags:
127
+ #### Update
128
+
129
+ <p align="center">
130
+ <img src="https://cdn.activitysmith.com/features/progress-live-activity-update.png" alt="Progress update example" width="680" />
131
+ </p>
78
132
 
79
133
  ```bash
80
134
  activitysmith activity update \
81
135
  --activity-id "<activityId>" \
82
- --title "Deploy" \
83
- --subtitle "step 2" \
84
- --current-step 2
136
+ --content-state '{"title":"EV Charging","subtitle":"Added 120 mi range","percentage":60}'
85
137
  ```
86
138
 
87
- ### End Live Activity
139
+ #### End
140
+
141
+ <p align="center">
142
+ <img src="https://cdn.activitysmith.com/features/progress-live-activity-end.png" alt="Progress end example" width="680" />
143
+ </p>
88
144
 
89
145
  ```bash
90
146
  activitysmith activity end \
91
147
  --activity-id "<activityId>" \
92
- --content-state '{"title":"Deploy","subtitle":"done","currentStep":4,"autoDismissMinutes":3}'
148
+ --content-state '{"title":"EV Charging","subtitle":"Added 200 mi range","percentage":100,"autoDismissMinutes":2}'
93
149
  ```
94
150
 
95
- Or use flags:
151
+ ### Channels
152
+
153
+ Channels are used to target specific team members or devices. Can be used for both push notifications and live activities.
96
154
 
97
155
  ```bash
98
- activitysmith activity end \
99
- --activity-id "<activityId>" \
100
- --title "Deploy" \
101
- --subtitle "done" \
102
- --current-step 4 \
103
- --auto-dismiss-minutes 3
156
+ activitysmith push \
157
+ --title "Build Failed 🚨" \
158
+ --message "CI pipeline failed on main branch" \
159
+ --channels "devs,ops"
160
+ ```
161
+
162
+ ### Push Notification Redirection and Actions
163
+
164
+ Push notification redirection and actions are optional and can be used to redirect the user to a specific URL when they tap the notification or to trigger a specific action when they long-press the notification.
165
+ Webhooks are executed by ActivitySmith backend.
166
+
167
+ ```bash
168
+ activitysmith push \
169
+ --title "Build Failed 🚨" \
170
+ --message "CI pipeline failed on main branch" \
171
+ --redirection "https://github.com/org/repo/actions/runs/123456789" \
172
+ --actions '[{"title":"Open Failing Run","type":"open_url","url":"https://github.com/org/repo/actions/runs/123456789"},{"title":"Create Incident","type":"webhook","url":"https://hooks.example.com/incidents/create","method":"POST","body":{"service":"payments-api","severity":"high","source":"activitysmith-cli"}}]'
173
+ ```
174
+
175
+ You can also load actions from a file:
176
+
177
+ ```bash
178
+ activitysmith push \
179
+ --title "Build Failed 🚨" \
180
+ --message "CI pipeline failed on main branch" \
181
+ --actions-file "./actions.json"
104
182
  ```
105
183
 
106
184
  ### JSON Output
@@ -130,6 +208,9 @@ Or use flags to build the payload:
130
208
  - `--type <type>`
131
209
  - `--number-of-steps <number>`
132
210
  - `--current-step <number>`
211
+ - `--percentage <number>`
212
+ - `--value <number>`
213
+ - `--upper-limit <number>`
133
214
  - `--color <color>`
134
215
  - `--step-color <color>`
135
216
  - `--auto-dismiss-minutes <number>`
@@ -140,9 +221,9 @@ Targeting options:
140
221
 
141
222
  Required fields:
142
223
 
143
- - `activity start`: `title`, `numberOfSteps`, `currentStep`, `type`
144
- - `activity update`: `title`, `currentStep`
145
- - `activity end`: `title`, `currentStep`
224
+ - `activity start`: `--title`, `--type`, plus either `--number-of-steps` and `--current-step`, or `--percentage`, or `--value` with `--upper-limit`
225
+ - `activity update`: `--title`, plus either `--current-step`, or `--percentage`, or `--value` with `--upper-limit`
226
+ - `activity end`: `--title`, plus either `--current-step`, or `--percentage`, or `--value` with `--upper-limit`
146
227
 
147
228
  ## Output
148
229
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "activitysmith-cli",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Command-line interface for ActivitySmith. Send push notifications, start, update and end live activities directly from your terminal.",
5
5
  "keywords": [
6
6
  "activitysmith",
@@ -40,7 +40,7 @@
40
40
  "lint": "node --check src/cli.js"
41
41
  },
42
42
  "dependencies": {
43
- "activitysmith": "0.1.1",
43
+ "activitysmith": "^0.1.5",
44
44
  "commander": "^12.1.0"
45
45
  }
46
46
  }
package/src/cli.js CHANGED
@@ -29,6 +29,14 @@ const parseIntegerOption = (label) => (value) => {
29
29
  return parsed;
30
30
  };
31
31
 
32
+ const parseNumberOption = (label) => (value) => {
33
+ const parsed = Number(value);
34
+ if (!Number.isFinite(parsed)) {
35
+ throw new InvalidArgumentError(`${label} must be a number`);
36
+ }
37
+ return parsed;
38
+ };
39
+
32
40
  const parseChannelsOption = (value) => {
33
41
  if (typeof value !== "string") {
34
42
  throw new InvalidArgumentError("channels must be a comma-separated string");
@@ -46,6 +54,25 @@ const parseChannelsOption = (value) => {
46
54
  return channels;
47
55
  };
48
56
 
57
+ const normalizeHttpsUrl = (value, label) => {
58
+ if (typeof value !== "string" || value.trim().length === 0) {
59
+ throw new Error(`${label} must be a non-empty string`);
60
+ }
61
+
62
+ let parsed;
63
+ try {
64
+ parsed = new URL(value.trim());
65
+ } catch {
66
+ throw new Error(`${label} must be a valid URL`);
67
+ }
68
+
69
+ if (parsed.protocol !== "https:") {
70
+ throw new Error(`${label} must use https`);
71
+ }
72
+
73
+ return parsed.toString();
74
+ };
75
+
49
76
  const addContentStateOptions = (command, { includeAutoDismiss } = {}) => {
50
77
  command
51
78
  .option("--content-state <json>", "Content state as JSON string")
@@ -63,6 +90,21 @@ const addContentStateOptions = (command, { includeAutoDismiss } = {}) => {
63
90
  "Content state current step",
64
91
  parseIntegerOption("current-step")
65
92
  )
93
+ .option(
94
+ "--percentage <number>",
95
+ "Content state percentage",
96
+ parseNumberOption("percentage")
97
+ )
98
+ .option(
99
+ "--value <number>",
100
+ "Content state value",
101
+ parseNumberOption("value")
102
+ )
103
+ .option(
104
+ "--upper-limit <number>",
105
+ "Content state upper limit",
106
+ parseNumberOption("upper-limit")
107
+ )
66
108
  .option("--color <color>", "Content state color")
67
109
  .option("--step-color <color>", "Content state step color");
68
110
 
@@ -129,6 +171,235 @@ const readJsonFile = async (filePath, label) => {
129
171
  }
130
172
  };
131
173
 
174
+ const parseJsonArrayString = (value, label) => {
175
+ let parsed;
176
+ try {
177
+ parsed = JSON.parse(value);
178
+ } catch {
179
+ throw new Error(`${label} must be valid JSON`);
180
+ }
181
+
182
+ if (!Array.isArray(parsed)) {
183
+ throw new Error(`${label} must be a JSON array`);
184
+ }
185
+
186
+ return parsed;
187
+ };
188
+
189
+ const readJsonArrayFile = async (filePath, label) => {
190
+ const resolvedPath = resolve(process.cwd(), filePath);
191
+ let text = "";
192
+
193
+ try {
194
+ text = await readFile(resolvedPath, "utf8");
195
+ } catch {
196
+ throw new Error(`${label} could not be read at ${resolvedPath}`);
197
+ }
198
+
199
+ return parseJsonArrayString(text, label);
200
+ };
201
+
202
+ const isNonEmptyString = (value) =>
203
+ typeof value === "string" && value.trim().length > 0;
204
+
205
+ const hasOwn = (object, key) =>
206
+ Object.prototype.hasOwnProperty.call(object, key);
207
+
208
+ const validateContentState = (contentState, mode) => {
209
+ const normalizedType =
210
+ typeof contentState.type === "string" ? contentState.type.trim() : undefined;
211
+
212
+ if (!isNonEmptyString(contentState.title)) {
213
+ throw new Error("contentState.title is required");
214
+ }
215
+
216
+ if (
217
+ normalizedType !== undefined &&
218
+ normalizedType !== "segmented_progress" &&
219
+ normalizedType !== "progress"
220
+ ) {
221
+ throw new Error(
222
+ "contentState.type must be one of: segmented_progress, progress"
223
+ );
224
+ }
225
+
226
+ const hasNumberOfSteps = hasOwn(contentState, "numberOfSteps");
227
+ const hasCurrentStep = hasOwn(contentState, "currentStep");
228
+ const hasPercentage = hasOwn(contentState, "percentage");
229
+ const hasValue = hasOwn(contentState, "value");
230
+ const hasUpperLimit = hasOwn(contentState, "upperLimit");
231
+ const hasStepColor = hasOwn(contentState, "stepColor");
232
+
233
+ if (hasValue !== hasUpperLimit) {
234
+ throw new Error(
235
+ "contentState.value and contentState.upperLimit must be provided together"
236
+ );
237
+ }
238
+
239
+ if (hasNumberOfSteps && contentState.numberOfSteps < 1) {
240
+ throw new Error("contentState.numberOfSteps must be at least 1");
241
+ }
242
+
243
+ if (hasCurrentStep && contentState.currentStep < 1) {
244
+ throw new Error("contentState.currentStep must be at least 1");
245
+ }
246
+
247
+ if (
248
+ hasPercentage &&
249
+ (contentState.percentage < 0 || contentState.percentage > 100)
250
+ ) {
251
+ throw new Error("contentState.percentage must be between 0 and 100");
252
+ }
253
+
254
+ if (hasUpperLimit && contentState.upperLimit <= 0) {
255
+ throw new Error("contentState.upperLimit must be greater than 0");
256
+ }
257
+
258
+ const hasSegmentedFields = hasNumberOfSteps || hasCurrentStep || hasStepColor;
259
+ const hasProgressFields = hasPercentage || hasValue || hasUpperLimit;
260
+
261
+ if (hasSegmentedFields && hasProgressFields) {
262
+ throw new Error(
263
+ "Do not mix segmented_progress fields with progress fields in the same contentState"
264
+ );
265
+ }
266
+
267
+ const effectiveType = normalizedType;
268
+
269
+ if (mode === "start") {
270
+ if (!effectiveType) {
271
+ throw new Error("contentState.type is required for activity start");
272
+ }
273
+
274
+ if (effectiveType === "segmented_progress") {
275
+ if (!hasNumberOfSteps || !hasCurrentStep) {
276
+ throw new Error(
277
+ "segmented_progress start requires contentState.numberOfSteps and contentState.currentStep"
278
+ );
279
+ }
280
+ return;
281
+ }
282
+
283
+ if (!hasPercentage && !hasValue) {
284
+ throw new Error(
285
+ "progress start requires contentState.percentage, or contentState.value with contentState.upperLimit"
286
+ );
287
+ }
288
+ return;
289
+ }
290
+
291
+ if (effectiveType === "segmented_progress") {
292
+ if (!hasCurrentStep) {
293
+ throw new Error(
294
+ `segmented_progress ${mode} requires contentState.currentStep`
295
+ );
296
+ }
297
+ return;
298
+ }
299
+
300
+ if (effectiveType === "progress") {
301
+ if (!hasPercentage && !hasValue) {
302
+ throw new Error(
303
+ `progress ${mode} requires contentState.percentage, or contentState.value with contentState.upperLimit`
304
+ );
305
+ }
306
+ return;
307
+ }
308
+
309
+ if (!hasSegmentedFields && !hasProgressFields) {
310
+ throw new Error(
311
+ `contentState for activity ${mode} must include either segmented_progress fields or progress fields`
312
+ );
313
+ }
314
+
315
+ if (hasSegmentedFields && !hasCurrentStep) {
316
+ throw new Error(
317
+ `segmented_progress ${mode} requires contentState.currentStep`
318
+ );
319
+ }
320
+
321
+ if (hasProgressFields && !hasPercentage && !hasValue) {
322
+ throw new Error(
323
+ `progress ${mode} requires contentState.percentage, or contentState.value with contentState.upperLimit`
324
+ );
325
+ }
326
+ };
327
+
328
+ const parsePushAction = (value, index) => {
329
+ const label = `actions[${index}]`;
330
+ assertPlainObject(value, label);
331
+
332
+ if (typeof value.title !== "string" || value.title.trim().length === 0) {
333
+ throw new Error(`${label}.title is required`);
334
+ }
335
+
336
+ if (typeof value.type !== "string") {
337
+ throw new Error(`${label}.type is required`);
338
+ }
339
+
340
+ const normalizedType = value.type.trim();
341
+ if (normalizedType !== "open_url" && normalizedType !== "webhook") {
342
+ throw new Error(`${label}.type must be one of: open_url, webhook`);
343
+ }
344
+
345
+ const action = {
346
+ title: value.title.trim(),
347
+ type: normalizedType,
348
+ url: normalizeHttpsUrl(value.url, `${label}.url`),
349
+ };
350
+
351
+ if (value.method !== undefined) {
352
+ if (typeof value.method !== "string") {
353
+ throw new Error(`${label}.method must be a string`);
354
+ }
355
+ const normalizedMethod = value.method.trim().toUpperCase();
356
+ if (normalizedMethod !== "GET" && normalizedMethod !== "POST") {
357
+ throw new Error(`${label}.method must be one of: GET, POST`);
358
+ }
359
+ action.method = normalizedMethod;
360
+ }
361
+
362
+ if (value.body !== undefined) {
363
+ assertPlainObject(value.body, `${label}.body`);
364
+ action.body = value.body;
365
+ }
366
+
367
+ if (normalizedType === "open_url" && (value.method !== undefined || value.body !== undefined)) {
368
+ throw new Error(`${label} with type=open_url cannot include method or body`);
369
+ }
370
+
371
+ return action;
372
+ };
373
+
374
+ const loadPushActions = async (options) => {
375
+ if (options.actions && options.actionsFile) {
376
+ throw new Error("Provide either --actions or --actions-file, not both.");
377
+ }
378
+
379
+ let actions;
380
+ if (options.actionsFile) {
381
+ actions = await readJsonArrayFile(options.actionsFile, "actions-file");
382
+ }
383
+
384
+ if (options.actions) {
385
+ actions = parseJsonArrayString(options.actions, "actions");
386
+ }
387
+
388
+ if (actions === undefined) {
389
+ return undefined;
390
+ }
391
+
392
+ if (actions.length === 0) {
393
+ throw new Error("actions must contain at least one action");
394
+ }
395
+
396
+ if (actions.length > 4) {
397
+ throw new Error("actions can contain at most 4 actions");
398
+ }
399
+
400
+ return actions.map(parsePushAction);
401
+ };
402
+
132
403
  const buildContentStateFromOptions = (options) => {
133
404
  const contentState = {};
134
405
 
@@ -152,6 +423,18 @@ const buildContentStateFromOptions = (options) => {
152
423
  contentState.currentStep = options.currentStep;
153
424
  }
154
425
 
426
+ if (options.percentage !== undefined) {
427
+ contentState.percentage = options.percentage;
428
+ }
429
+
430
+ if (options.value !== undefined) {
431
+ contentState.value = options.value;
432
+ }
433
+
434
+ if (options.upperLimit !== undefined) {
435
+ contentState.upperLimit = options.upperLimit;
436
+ }
437
+
155
438
  if (options.color !== undefined) {
156
439
  contentState.color = options.color;
157
440
  }
@@ -171,6 +454,7 @@ const toApiContentState = (contentState) => {
171
454
  const keyMap = {
172
455
  numberOfSteps: "number_of_steps",
173
456
  currentStep: "current_step",
457
+ upperLimit: "upper_limit",
174
458
  stepColor: "step_color",
175
459
  autoDismissMinutes: "auto_dismiss_minutes",
176
460
  };
@@ -216,7 +500,7 @@ const toApiLiveActivityEndRequest = (activityId, contentState) => ({
216
500
  content_state: toApiContentState(contentState),
217
501
  });
218
502
 
219
- const loadContentState = async (options) => {
503
+ const loadContentState = async (options, mode) => {
220
504
  let contentState = {};
221
505
 
222
506
  if (options.contentStateFile) {
@@ -244,6 +528,8 @@ const loadContentState = async (options) => {
244
528
  );
245
529
  }
246
530
 
531
+ validateContentState(contentState, mode);
532
+
247
533
  return contentState;
248
534
  };
249
535
 
@@ -366,6 +652,9 @@ program
366
652
  .requiredOption("--title <title>", "Push title")
367
653
  .option("--message <message>", "Push message")
368
654
  .option("--subtitle <subtitle>", "Push subtitle")
655
+ .option("--redirection <url>", "HTTPS URL opened when notification is tapped")
656
+ .option("--actions <json>", "Actions JSON array (max 4)")
657
+ .option("--actions-file <path>", "Path to actions JSON array file")
369
658
  .option(
370
659
  "--channels <channels>",
371
660
  "Comma-separated channel slugs (optional)",
@@ -377,16 +666,24 @@ program
377
666
  try {
378
667
  const apiKey = requireApiKey(globalOptions);
379
668
  const client = createClient(apiKey);
669
+ const actions = await loadPushActions(options);
670
+
671
+ const pushNotificationRequest = withTargetChannels(
672
+ {
673
+ title: options.title,
674
+ message: options.message,
675
+ subtitle: options.subtitle,
676
+ redirection:
677
+ options.redirection !== undefined
678
+ ? normalizeHttpsUrl(options.redirection, "redirection")
679
+ : undefined,
680
+ actions,
681
+ },
682
+ options.channels
683
+ );
380
684
 
381
685
  const response = await client.notifications.sendPushNotification({
382
- pushNotificationRequest: withTargetChannels(
383
- {
384
- title: options.title,
385
- message: options.message,
386
- subtitle: options.subtitle,
387
- },
388
- options.channels
389
- ),
686
+ pushNotificationRequest,
390
687
  });
391
688
 
392
689
  outputResult(response, globalOptions, [
@@ -423,7 +720,7 @@ addContentStateOptions(
423
720
  try {
424
721
  const apiKey = requireApiKey(globalOptions);
425
722
  const client = createClient(apiKey);
426
- const contentState = await loadContentState(options);
723
+ const contentState = await loadContentState(options, "start");
427
724
 
428
725
  const response = await client.liveActivities.startLiveActivity({
429
726
  liveActivityStartRequest: withTargetChannels(
@@ -454,7 +751,7 @@ addContentStateOptions(
454
751
  try {
455
752
  const apiKey = requireApiKey(globalOptions);
456
753
  const client = createClient(apiKey);
457
- const contentState = await loadContentState(options);
754
+ const contentState = await loadContentState(options, "update");
458
755
 
459
756
  const response = await client.liveActivities.updateLiveActivity({
460
757
  liveActivityUpdateRequest: toApiLiveActivityUpdateRequest(
@@ -484,7 +781,7 @@ addContentStateOptions(
484
781
  try {
485
782
  const apiKey = requireApiKey(globalOptions);
486
783
  const client = createClient(apiKey);
487
- const contentState = await loadContentState(options);
784
+ const contentState = await loadContentState(options, "end");
488
785
 
489
786
  const response = await client.liveActivities.endLiveActivity({
490
787
  liveActivityEndRequest: toApiLiveActivityEndRequest(