activitysmith-cli 1.19.0 → 1.20.1
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 +61 -17
- package/package.json +1 -1
- package/src/approvals.js +218 -0
- package/src/cli.js +151 -41
package/README.md
CHANGED
|
@@ -710,9 +710,7 @@ activitysmith badge 0
|
|
|
710
710
|
|
|
711
711
|
## Approvals
|
|
712
712
|
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
Let your agents ask for your approval before they act, such as sending an email to customers or deploying to production. The request reaches your iPhone as a Live Activity or an actionable Push Notification, both with two buttons. The agent waits until you tap one and continues only when you approve.
|
|
713
|
+
Let your agents ask for your approval before they act, such as sending an email to customers or deploying to production. The request reaches your iPhone as a Time Sensitive Push Notification. Tap it to open the request on a review screen in the ActivitySmith app. Approve or reject it, or write a reply. The agent waits for your answer and continues only when you approve.
|
|
716
714
|
|
|
717
715
|
```bash
|
|
718
716
|
activitysmith approval request "Send the pricing update email to 214 customers?" \
|
|
@@ -732,10 +730,12 @@ Exit codes for `approval request` and `approval wait`:
|
|
|
732
730
|
- `1`: Rejected, timed out after 30 minutes, or cancelled. Do not continue.
|
|
733
731
|
- `2`: The request failed, for example because no device is paired.
|
|
734
732
|
- `3`: Still pending after `--timeout`, or sent with `--no-wait`.
|
|
733
|
+
- `4`: Replied. The reply text follows `Reply:`. Follow the reply instead of the original action.
|
|
734
|
+
- `5`: Chosen. The chosen option follows `Chosen:` and `Option ID:`. Do that option.
|
|
735
735
|
|
|
736
736
|
### Show the exact command
|
|
737
737
|
|
|
738
|
-
Add `--command` with the exact command that waits for approval. The
|
|
738
|
+
Add `--command` with the exact command that waits for approval. The review screen shows the command and the current directory:
|
|
739
739
|
|
|
740
740
|
```bash
|
|
741
741
|
activitysmith approval request "Run the database migration?" \
|
|
@@ -743,7 +743,61 @@ activitysmith approval request "Run the database migration?" \
|
|
|
743
743
|
--command "npm run db:migrate -- --env production"
|
|
744
744
|
```
|
|
745
745
|
|
|
746
|
-
Add `--details` for
|
|
746
|
+
Add `--details` for context, up to 4,000 characters. Add `--scope` to show where the action applies, such as `environment:production`.
|
|
747
|
+
|
|
748
|
+
### Add changes, links, and screenshots
|
|
749
|
+
|
|
750
|
+
Give the review screen what you need to decide:
|
|
751
|
+
|
|
752
|
+
```bash
|
|
753
|
+
activitysmith approval request "Deploy the new checkout to production?" \
|
|
754
|
+
--approve-label "Deploy" \
|
|
755
|
+
--git-changes main...HEAD \
|
|
756
|
+
--link "Pull request=https://github.com/acme/shop/pull/42" \
|
|
757
|
+
--screenshot "shots/checkout.png=New checkout"
|
|
758
|
+
```
|
|
759
|
+
|
|
760
|
+
- `--git-changes` adds a summary of `git diff`: how many files changed, the lines added and removed, and the 2 most changed files. Without a range, it uses the uncommitted changes.
|
|
761
|
+
- `--change <path:added:removed>` adds one file by hand, such as `src/app.ts:12:3`. Repeat it to add more files. Each file adds to the same summary.
|
|
762
|
+
- `--link <title=url>` adds an https link that opens in the app. Add up to 5.
|
|
763
|
+
- `--screenshot <file=title>` uploads a PNG, JPEG, or WebP file of up to 2 MB. The title is optional. Add up to 5.
|
|
764
|
+
|
|
765
|
+
Screenshots are deleted 7 days after the decision.
|
|
766
|
+
|
|
767
|
+
### Offer options
|
|
768
|
+
|
|
769
|
+
Ask the user to pick one of 2 to 4 options instead of approve or reject. Give each option an ID and a label. Add `--recommended` to mark the option you would pick:
|
|
770
|
+
|
|
771
|
+
```bash
|
|
772
|
+
activitysmith approval request "Which settings layout should I ship?" \
|
|
773
|
+
--option "tabs=Tabs across the top" \
|
|
774
|
+
--option "sidebar=Sidebar navigation" \
|
|
775
|
+
--recommended sidebar \
|
|
776
|
+
--option-screenshot tabs=shots/tabs.png \
|
|
777
|
+
--option-screenshot sidebar=shots/sidebar.png
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
The review screen letters the options A to D. When you choose one, the command prints the option and exits with code `5`:
|
|
781
|
+
|
|
782
|
+
```text
|
|
783
|
+
Chosen: Sidebar navigation
|
|
784
|
+
Option ID: sidebar
|
|
785
|
+
Do the chosen option.
|
|
786
|
+
```
|
|
787
|
+
|
|
788
|
+
Without an ID, as in `--option "Sidebar navigation"`, the options get the IDs `a`, `b`, `c`, and `d`.
|
|
789
|
+
|
|
790
|
+
### Read a reply
|
|
791
|
+
|
|
792
|
+
On the review screen, you can write a reply instead of a decision, such as "Send it tomorrow at 9 instead." The command then prints the reply and exits with code `4`:
|
|
793
|
+
|
|
794
|
+
```text
|
|
795
|
+
Replied. Read the reply and follow it. Do not run the original action unless the reply says to.
|
|
796
|
+
Reply:
|
|
797
|
+
Send it tomorrow at 9 instead.
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
Add `--json` to get the full approval with `status`, `reply`, `selected_option_id`, and `selected_option`.
|
|
747
801
|
|
|
748
802
|
### Wait in steps
|
|
749
803
|
|
|
@@ -761,19 +815,9 @@ activitysmith approval get apr_Xk3pQ8mV2nR7tL9w
|
|
|
761
815
|
activitysmith approval cancel apr_Xk3pQ8mV2nR7tL9w
|
|
762
816
|
```
|
|
763
817
|
|
|
764
|
-
Cancel a request when the action is no longer needed. Its
|
|
765
|
-
|
|
766
|
-
### Change how the request looks
|
|
818
|
+
Cancel a request when the action is no longer needed. Its Push Notification is removed from your devices.
|
|
767
819
|
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
```bash
|
|
771
|
-
activitysmith approval request "Refund order #4821 for $129?" \
|
|
772
|
-
--approve-label "Refund" \
|
|
773
|
-
--badge "Billing" \
|
|
774
|
-
--color orange \
|
|
775
|
-
--icon creditcard
|
|
776
|
-
```
|
|
820
|
+
`--delivery`, `--color`, `--icon`, and `--badge` are no longer used. The CLI still accepts them so older scripts keep working, and ignores them.
|
|
777
821
|
|
|
778
822
|
## Metadata
|
|
779
823
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "activitysmith-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.20.1",
|
|
4
4
|
"description": "Command-line interface for ActivitySmith. Send Push Notifications, start, update, and end Live Activities, and set App Icon Badge Counts from your terminal.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"activitysmith",
|
package/src/approvals.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
|
|
5
|
+
// Limits from the backend approval review contract (backend docs/approval-review.md).
|
|
6
|
+
export const APPROVAL_LIMITS = {
|
|
7
|
+
minOptions: 2,
|
|
8
|
+
maxOptions: 4,
|
|
9
|
+
optionLabel: 80,
|
|
10
|
+
maxTopFiles: 2,
|
|
11
|
+
topFileName: 500,
|
|
12
|
+
maxFilesChanged: 1_000_000,
|
|
13
|
+
maxChangedLines: 1_000_000_000,
|
|
14
|
+
maxLinks: 5,
|
|
15
|
+
linkTitle: 120,
|
|
16
|
+
linkUrl: 2000,
|
|
17
|
+
maxScreenshots: 5,
|
|
18
|
+
screenshotTitle: 60,
|
|
19
|
+
screenshotBytes: 2 * 1024 * 1024,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const OPTION_ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;
|
|
23
|
+
const DEFAULT_OPTION_IDS = ["a", "b", "c", "d"];
|
|
24
|
+
|
|
25
|
+
// --option "left=Keep the left layout" or --option "Keep the left layout" (ID a, b, c, d by position).
|
|
26
|
+
export const parseOptionArgument = (value) => {
|
|
27
|
+
const separator = value.indexOf("=");
|
|
28
|
+
const id = separator > 0 ? value.slice(0, separator).trim() : "";
|
|
29
|
+
if (separator > 0 && OPTION_ID_PATTERN.test(id)) {
|
|
30
|
+
return { id, label: value.slice(separator + 1).trim() };
|
|
31
|
+
}
|
|
32
|
+
return { id: null, label: value.trim() };
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const buildApprovalOptions = (entries, { recommended, screenshots = {} } = {}) => {
|
|
36
|
+
if (entries.length === 0) {
|
|
37
|
+
if (recommended) throw new Error("--recommended needs --option.");
|
|
38
|
+
if (Object.keys(screenshots).length > 0) throw new Error("--option-screenshot needs --option.");
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (entries.length < APPROVAL_LIMITS.minOptions || entries.length > APPROVAL_LIMITS.maxOptions) {
|
|
42
|
+
throw new Error(`Give 2 to 4 --option values, not ${entries.length}.`);
|
|
43
|
+
}
|
|
44
|
+
const options = entries.map((entry, index) => ({ id: entry.id ?? DEFAULT_OPTION_IDS[index], label: entry.label }));
|
|
45
|
+
const ids = new Set();
|
|
46
|
+
for (const option of options) {
|
|
47
|
+
if (!option.label) throw new Error(`Option ${option.id} needs a label.`);
|
|
48
|
+
if (option.label.length > APPROVAL_LIMITS.optionLabel) {
|
|
49
|
+
throw new Error(`Option ${option.id} label is longer than ${APPROVAL_LIMITS.optionLabel} characters.`);
|
|
50
|
+
}
|
|
51
|
+
if (ids.has(option.id)) throw new Error(`Option ID ${option.id} is used twice.`);
|
|
52
|
+
ids.add(option.id);
|
|
53
|
+
}
|
|
54
|
+
if (recommended && !ids.has(recommended)) {
|
|
55
|
+
throw new Error(`--recommended ${recommended} is not one of the option IDs: ${[...ids].join(", ")}.`);
|
|
56
|
+
}
|
|
57
|
+
for (const id of Object.keys(screenshots)) {
|
|
58
|
+
if (!ids.has(id)) throw new Error(`--option-screenshot ${id} is not one of the option IDs: ${[...ids].join(", ")}.`);
|
|
59
|
+
}
|
|
60
|
+
return options.map((option) => (option.id === recommended ? { ...option, recommended: true } : option));
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// --option-screenshot "left=shots/left.png"
|
|
64
|
+
export const parseOptionScreenshotArgument = (value) => {
|
|
65
|
+
const separator = value.indexOf("=");
|
|
66
|
+
const id = separator > 0 ? value.slice(0, separator).trim() : "";
|
|
67
|
+
const path = separator > 0 ? value.slice(separator + 1).trim() : "";
|
|
68
|
+
if (!id || !path) throw new Error("--option-screenshot must be <option-id>=<file>.");
|
|
69
|
+
return { id, path };
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// --screenshot "shots/after.png" or --screenshot "shots/after.png=New settings page"
|
|
73
|
+
export const parseScreenshotArgument = (value) => {
|
|
74
|
+
const separator = value.indexOf("=");
|
|
75
|
+
const path = (separator > 0 ? value.slice(0, separator) : value).trim();
|
|
76
|
+
const title = separator > 0 ? value.slice(separator + 1).trim() : "";
|
|
77
|
+
if (!path) throw new Error("--screenshot needs a file path.");
|
|
78
|
+
if (title.length > APPROVAL_LIMITS.screenshotTitle) {
|
|
79
|
+
throw new Error(`Screenshot title "${title}" is longer than ${APPROVAL_LIMITS.screenshotTitle} characters.`);
|
|
80
|
+
}
|
|
81
|
+
return { path, ...(title ? { title } : {}) };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// --change "src/app.ts:12:3" (path, lines added, lines removed). Paths may contain colons.
|
|
85
|
+
export const parseChangeArgument = (value) => {
|
|
86
|
+
const match = /^(.+):(\d+):(\d+)$/.exec(value.trim());
|
|
87
|
+
if (!match || !match[1].trim()) throw new Error(`--change must be <path>:<added>:<removed>, such as src/app.ts:12:3. Got "${value}".`);
|
|
88
|
+
return { path: match[1].trim(), added: Number(match[2]), removed: Number(match[3]) };
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// --link "Pull request=https://github.com/acme/app/pull/42" or --link "https://...".
|
|
92
|
+
export const parseLinkArgument = (value) => {
|
|
93
|
+
const trimmed = value.trim();
|
|
94
|
+
const separator = trimmed.indexOf("=https://");
|
|
95
|
+
const title = separator > 0 ? trimmed.slice(0, separator).trim() : "";
|
|
96
|
+
const url = separator > 0 ? trimmed.slice(separator + 1).trim() : trimmed;
|
|
97
|
+
let parsed;
|
|
98
|
+
try {
|
|
99
|
+
parsed = new URL(url);
|
|
100
|
+
} catch {
|
|
101
|
+
throw new Error(`--link must be <title>=<https URL> or an https URL. Got "${value}".`);
|
|
102
|
+
}
|
|
103
|
+
if (parsed.protocol !== "https:") throw new Error(`Links must use https. Got "${url}".`);
|
|
104
|
+
if (url.length > APPROVAL_LIMITS.linkUrl) throw new Error(`Link URL is longer than ${APPROVAL_LIMITS.linkUrl} characters.`);
|
|
105
|
+
if (title.length > APPROVAL_LIMITS.linkTitle) throw new Error(`Link title is longer than ${APPROVAL_LIMITS.linkTitle} characters.`);
|
|
106
|
+
return { title: title || url.slice(0, APPROVAL_LIMITS.linkTitle), url };
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Parses `git diff --numstat -z --no-renames`. Binary files report "-" and count as 0 lines.
|
|
110
|
+
export const parseGitNumstat = (output) =>
|
|
111
|
+
output
|
|
112
|
+
.split("\0")
|
|
113
|
+
.filter(Boolean)
|
|
114
|
+
.map((record) => {
|
|
115
|
+
const [added, removed, ...pathParts] = record.split("\t");
|
|
116
|
+
return {
|
|
117
|
+
path: pathParts.join("\t").replace(/^\n+/, ""),
|
|
118
|
+
added: Number.parseInt(added, 10) || 0,
|
|
119
|
+
removed: Number.parseInt(removed, 10) || 0,
|
|
120
|
+
};
|
|
121
|
+
})
|
|
122
|
+
.filter((file) => file.path);
|
|
123
|
+
|
|
124
|
+
const execFileAsync = promisify(execFile);
|
|
125
|
+
|
|
126
|
+
// File changes from git: the uncommitted changes against HEAD, or a range such as main...HEAD.
|
|
127
|
+
export const readGitChanges = async (range, { cwd = process.cwd() } = {}) => {
|
|
128
|
+
const args = ["diff", "--numstat", "-z", "--no-renames", typeof range === "string" && range ? range : "HEAD"];
|
|
129
|
+
try {
|
|
130
|
+
const { stdout } = await execFileAsync("git", args, { cwd, maxBuffer: 16 * 1024 * 1024 });
|
|
131
|
+
return parseGitNumstat(stdout);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
const reason = String(error.stderr || error.message || "").trim().split("\n")[0];
|
|
134
|
+
throw new Error(`Could not read changes with git ${args.join(" ")}: ${reason}`);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// The name the review screen shows: the last path component. Full paths never leave the machine.
|
|
139
|
+
export const changedFileName = (path) => {
|
|
140
|
+
const trimmed = path.trim().replace(/[\\/]+$/, "");
|
|
141
|
+
const name = trimmed.slice(Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\")) + 1) || trimmed;
|
|
142
|
+
return name.slice(0, APPROVAL_LIMITS.topFileName);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// Reduces changed files to what the review screen shows: the file count, the line totals, and the
|
|
146
|
+
// names of the 2 most changed files (added plus removed). Returns undefined when nothing changed.
|
|
147
|
+
export const buildApprovalChanges = (files) => {
|
|
148
|
+
const changed = files.filter((file) => file.path && file.path.trim());
|
|
149
|
+
if (changed.length === 0) return undefined;
|
|
150
|
+
// Array sort is stable, so files with the same line count keep their order.
|
|
151
|
+
const topFiles = [...changed]
|
|
152
|
+
.sort((a, b) => b.added + b.removed - (a.added + a.removed))
|
|
153
|
+
.slice(0, APPROVAL_LIMITS.maxTopFiles)
|
|
154
|
+
.map((file) => changedFileName(file.path));
|
|
155
|
+
return {
|
|
156
|
+
files_changed: Math.min(APPROVAL_LIMITS.maxFilesChanged, changed.length),
|
|
157
|
+
lines_added: Math.min(APPROVAL_LIMITS.maxChangedLines, changed.reduce((total, file) => total + file.added, 0)),
|
|
158
|
+
lines_removed: Math.min(APPROVAL_LIMITS.maxChangedLines, changed.reduce((total, file) => total + file.removed, 0)),
|
|
159
|
+
top_files: topFiles,
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// Reads the type from the file signature, as the API does, so a renamed file fails before upload.
|
|
164
|
+
export const detectImageType = (data) => {
|
|
165
|
+
if (data.length > 8 && data[0] === 0x89 && data.subarray(1, 4).toString("ascii") === "PNG") return "image/png";
|
|
166
|
+
if (data.length > 3 && data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) return "image/jpeg";
|
|
167
|
+
if (data.length > 12 && data.subarray(0, 4).toString("ascii") === "RIFF" && data.subarray(8, 12).toString("ascii") === "WEBP") {
|
|
168
|
+
return "image/webp";
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export const readScreenshotFile = async (path) => {
|
|
174
|
+
let data;
|
|
175
|
+
try {
|
|
176
|
+
data = await readFile(path);
|
|
177
|
+
} catch (error) {
|
|
178
|
+
throw new Error(`Could not read screenshot ${path}: ${error.code === "ENOENT" ? "no such file" : error.message}`);
|
|
179
|
+
}
|
|
180
|
+
const contentType = detectImageType(data);
|
|
181
|
+
if (!contentType) throw new Error(`Screenshot ${path} is not a PNG, JPEG, or WebP image.`);
|
|
182
|
+
if (data.length > APPROVAL_LIMITS.screenshotBytes) {
|
|
183
|
+
throw new Error(`Screenshot ${path} is ${(data.length / 1024 / 1024).toFixed(1)} MB. The limit is 2 MB.`);
|
|
184
|
+
}
|
|
185
|
+
return { data, contentType };
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// One line for each final status. Exit codes are in APPROVAL_EXIT in cli.js.
|
|
189
|
+
export const approvalResultLines = (approval) => {
|
|
190
|
+
switch (approval?.status) {
|
|
191
|
+
case "approved":
|
|
192
|
+
return ["Approved."];
|
|
193
|
+
case "rejected":
|
|
194
|
+
return ["Rejected. Do not continue with the action."];
|
|
195
|
+
case "replied":
|
|
196
|
+
return [
|
|
197
|
+
"Replied. Read the reply and follow it. Do not run the original action unless the reply says to.",
|
|
198
|
+
"Reply:",
|
|
199
|
+
approval.reply ?? "",
|
|
200
|
+
];
|
|
201
|
+
case "chosen": {
|
|
202
|
+
const option = approval.selected_option ?? approval.options?.find((item) => item.id === approval.selected_option_id);
|
|
203
|
+
return [
|
|
204
|
+
`Chosen: ${option?.label ?? approval.selected_option_id}`,
|
|
205
|
+
`Option ID: ${approval.selected_option_id}`,
|
|
206
|
+
"Do the chosen option.",
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
case "timed_out":
|
|
210
|
+
return ["Timed out. No decision within 30 minutes. Do not continue with the action."];
|
|
211
|
+
case "cancelled":
|
|
212
|
+
return ["Cancelled. Do not continue with the action."];
|
|
213
|
+
case "pending":
|
|
214
|
+
return ["Pending."];
|
|
215
|
+
default:
|
|
216
|
+
return [`Status: ${approval?.status}`];
|
|
217
|
+
}
|
|
218
|
+
};
|
package/src/cli.js
CHANGED
|
@@ -3,6 +3,19 @@
|
|
|
3
3
|
import { Command, InvalidArgumentError, Option } from "commander";
|
|
4
4
|
import { agentHeaders } from "./agent.js";
|
|
5
5
|
import { loadMetadata } from "./metadata.js";
|
|
6
|
+
import {
|
|
7
|
+
APPROVAL_LIMITS,
|
|
8
|
+
approvalResultLines,
|
|
9
|
+
buildApprovalChanges,
|
|
10
|
+
buildApprovalOptions,
|
|
11
|
+
parseChangeArgument,
|
|
12
|
+
parseLinkArgument,
|
|
13
|
+
parseOptionArgument,
|
|
14
|
+
parseOptionScreenshotArgument,
|
|
15
|
+
parseScreenshotArgument,
|
|
16
|
+
readGitChanges,
|
|
17
|
+
readScreenshotFile,
|
|
18
|
+
} from "./approvals.js";
|
|
6
19
|
import {
|
|
7
20
|
API_KEYS_SCOPE,
|
|
8
21
|
DEFAULT_API_RESOURCE,
|
|
@@ -1790,7 +1803,8 @@ const APP_STORE_URL = "https://apps.apple.com/us/app/activitysmith/id6752254835"
|
|
|
1790
1803
|
|
|
1791
1804
|
// Account endpoints the generated SDK does not cover. Same API and SDK header as the SDK client, so
|
|
1792
1805
|
// errors print the same way and usage shows the CLI version.
|
|
1793
|
-
|
|
1806
|
+
// `rawBody` with `contentType` sends bytes as they are, such as a screenshot upload.
|
|
1807
|
+
const apiRequest = async (path, { token, method = "GET", body, rawBody, contentType } = {}) => {
|
|
1794
1808
|
const response = await fetch(`${DEFAULT_API_RESOURCE}${path}`, {
|
|
1795
1809
|
method,
|
|
1796
1810
|
headers: {
|
|
@@ -1798,9 +1812,9 @@ const apiRequest = async (path, { token, method = "GET", body } = {}) => {
|
|
|
1798
1812
|
Accept: "application/json",
|
|
1799
1813
|
"X-ActivitySmith-SDK": `cli-v${version}`,
|
|
1800
1814
|
...agentHeaders(),
|
|
1801
|
-
...(body ? { "Content-Type": "application/json" } : {}),
|
|
1815
|
+
...(rawBody ? { "Content-Type": contentType } : body ? { "Content-Type": "application/json" } : {}),
|
|
1802
1816
|
},
|
|
1803
|
-
...(body ? { body: JSON.stringify(body) } : {}),
|
|
1817
|
+
...(rawBody ? { body: rawBody } : body ? { body: JSON.stringify(body) } : {}),
|
|
1804
1818
|
});
|
|
1805
1819
|
if (!response.ok) {
|
|
1806
1820
|
const error = new Error(`Request failed with status ${response.status}`);
|
|
@@ -1913,12 +1927,19 @@ keysCommand
|
|
|
1913
1927
|
});
|
|
1914
1928
|
|
|
1915
1929
|
// Approvals. Agents ask the user for approval before an action the user wants to approve, such as
|
|
1916
|
-
// sending an email or deploying, and go ahead only when this exits 0.
|
|
1930
|
+
// sending an email or deploying, and go ahead only when this exits 0. The request is a Push
|
|
1931
|
+
// Notification that opens a review screen in the iOS app (backend docs/approval-review.md).
|
|
1917
1932
|
const APPROVAL_SCOPE_TYPES = ["working_directory", "repository", "project", "environment", "customer", "account", "resource"];
|
|
1918
|
-
const APPROVAL_COLORS = ["lime", "green", "cyan", "blue", "purple", "magenta", "red", "orange", "yellow", "gray"];
|
|
1919
1933
|
// Each request waits at most this long on the server, then the CLI asks again.
|
|
1920
1934
|
const APPROVAL_POLL_SECONDS = 45;
|
|
1921
|
-
const APPROVAL_EXIT = { approved: 0, not_approved: 1, error: 2, pending: 3 };
|
|
1935
|
+
const APPROVAL_EXIT = { approved: 0, not_approved: 1, error: 2, pending: 3, replied: 4, chosen: 5 };
|
|
1936
|
+
// Approvals are always Push Notifications now. The API ignores these, so old scripts keep working.
|
|
1937
|
+
const DEPRECATED_APPROVAL_FLAGS = [
|
|
1938
|
+
["delivery", "--delivery"],
|
|
1939
|
+
["color", "--color"],
|
|
1940
|
+
["icon", "--icon"],
|
|
1941
|
+
["badge", "--badge"],
|
|
1942
|
+
];
|
|
1922
1943
|
|
|
1923
1944
|
const parseApprovalScope = (value) => {
|
|
1924
1945
|
const separator = value.indexOf(":");
|
|
@@ -1932,13 +1953,6 @@ const parseApprovalScope = (value) => {
|
|
|
1932
1953
|
return { type, value: scopeValue };
|
|
1933
1954
|
};
|
|
1934
1955
|
|
|
1935
|
-
const parseApprovalColor = (value) => {
|
|
1936
|
-
if (!APPROVAL_COLORS.includes(value)) {
|
|
1937
|
-
throw new InvalidArgumentError(`color must be one of ${APPROVAL_COLORS.join(", ")}`);
|
|
1938
|
-
}
|
|
1939
|
-
return value;
|
|
1940
|
-
};
|
|
1941
|
-
|
|
1942
1956
|
const parsePositiveSeconds = (value) => {
|
|
1943
1957
|
const parsed = Number(value);
|
|
1944
1958
|
if (!Number.isInteger(parsed) || parsed < 1) {
|
|
@@ -1947,11 +1961,20 @@ const parsePositiveSeconds = (value) => {
|
|
|
1947
1961
|
return parsed;
|
|
1948
1962
|
};
|
|
1949
1963
|
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1964
|
+
// Wraps a parser for a repeatable option so commander reports its errors as invalid arguments.
|
|
1965
|
+
const repeatable = (parse) => (value, previous = []) => {
|
|
1966
|
+
try {
|
|
1967
|
+
return [...previous, parse(value)];
|
|
1968
|
+
} catch (error) {
|
|
1969
|
+
throw new InvalidArgumentError(error.message);
|
|
1970
|
+
}
|
|
1971
|
+
};
|
|
1972
|
+
|
|
1973
|
+
const APPROVAL_EXIT_BY_STATUS = {
|
|
1974
|
+
approved: APPROVAL_EXIT.approved,
|
|
1975
|
+
replied: APPROVAL_EXIT.replied,
|
|
1976
|
+
chosen: APPROVAL_EXIT.chosen,
|
|
1977
|
+
pending: APPROVAL_EXIT.pending,
|
|
1955
1978
|
};
|
|
1956
1979
|
|
|
1957
1980
|
// Waits until the approval is decided, or until `timeoutSeconds` pass, then prints the result and exits.
|
|
@@ -1976,28 +1999,67 @@ const waitForApproval = async (approvalId, token, timeoutSeconds, globalOptions)
|
|
|
1976
1999
|
]);
|
|
1977
2000
|
process.exit(APPROVAL_EXIT.pending);
|
|
1978
2001
|
}
|
|
1979
|
-
outputResult(result, globalOptions,
|
|
1980
|
-
process.exit(status
|
|
2002
|
+
outputResult(result, globalOptions, approvalResultLines(result.approval));
|
|
2003
|
+
process.exit(APPROVAL_EXIT_BY_STATUS[status] ?? APPROVAL_EXIT.not_approved);
|
|
1981
2004
|
};
|
|
1982
2005
|
|
|
2006
|
+
// Uploads each screenshot file once, even when --screenshot and --option-screenshot name the same file.
|
|
2007
|
+
const uploadApprovalScreenshots = async (screenshotArgs, optionScreenshotArgs, token, globalOptions) => {
|
|
2008
|
+
const entries = [...screenshotArgs];
|
|
2009
|
+
for (const { path } of optionScreenshotArgs) {
|
|
2010
|
+
if (!entries.some((entry) => entry.path === path)) entries.push({ path });
|
|
2011
|
+
}
|
|
2012
|
+
if (entries.length > APPROVAL_LIMITS.maxScreenshots) {
|
|
2013
|
+
throw new Error(`An approval can show at most ${APPROVAL_LIMITS.maxScreenshots} screenshots, not ${entries.length}.`);
|
|
2014
|
+
}
|
|
2015
|
+
const files = await Promise.all(entries.map((entry) => readScreenshotFile(entry.path)));
|
|
2016
|
+
|
|
2017
|
+
const idByPath = new Map();
|
|
2018
|
+
const screenshots = [];
|
|
2019
|
+
for (const [index, entry] of entries.entries()) {
|
|
2020
|
+
if (!globalOptions.json) console.error(`Uploading screenshot ${entry.path}`);
|
|
2021
|
+
const result = await apiRequest("/approvals/screenshots", {
|
|
2022
|
+
token,
|
|
2023
|
+
method: "POST",
|
|
2024
|
+
rawBody: files[index].data,
|
|
2025
|
+
contentType: files[index].contentType,
|
|
2026
|
+
});
|
|
2027
|
+
const id = result.screenshot?.id;
|
|
2028
|
+
if (!id) throw new Error(`The API did not return a screenshot ID for ${entry.path}. Run the command again.`);
|
|
2029
|
+
idByPath.set(entry.path, id);
|
|
2030
|
+
screenshots.push({ id, ...(entry.title ? { title: entry.title } : {}) });
|
|
2031
|
+
}
|
|
2032
|
+
const screenshotIdByOption = Object.fromEntries(optionScreenshotArgs.map(({ id, path }) => [id, idByPath.get(path)]));
|
|
2033
|
+
return { screenshots, screenshotIdByOption };
|
|
2034
|
+
};
|
|
2035
|
+
|
|
2036
|
+
// A mistyped flag is a failed request (exit 2), not a rejection (exit 1).
|
|
2037
|
+
const exitWithApprovalError = (error) => process.exit(error.exitCode === 0 ? 0 : APPROVAL_EXIT.error);
|
|
2038
|
+
|
|
1983
2039
|
const approvalCommand = program
|
|
1984
2040
|
.command("approval")
|
|
1985
2041
|
.description("Let your agents ask for your approval on your iPhone before they act")
|
|
1986
2042
|
.addHelpText(
|
|
1987
2043
|
"after",
|
|
1988
2044
|
`
|
|
2045
|
+
The request is a Push Notification. A tap opens a review screen where you approve, reject,
|
|
2046
|
+
choose an option, or reply with text.
|
|
2047
|
+
|
|
1989
2048
|
Exit codes for request and wait:
|
|
1990
2049
|
0 Approved. Continue with the action.
|
|
1991
2050
|
1 Rejected, timed out, or cancelled. Do not continue.
|
|
1992
2051
|
2 The request failed, for example no paired device.
|
|
1993
|
-
3 Still pending after --timeout or with --no-wait. Run approval wait with the approval ID
|
|
2052
|
+
3 Still pending after --timeout or with --no-wait. Run approval wait with the approval ID.
|
|
2053
|
+
4 Replied. The reply text follows "Reply:". Follow it instead of the original action.
|
|
2054
|
+
5 Chosen. The chosen option follows "Chosen:" and "Option ID:". Do that option.`
|
|
1994
2055
|
);
|
|
1995
2056
|
|
|
1996
2057
|
approvalCommand
|
|
1997
2058
|
.command("request")
|
|
1998
|
-
.description("Send an approval request
|
|
2059
|
+
.description("Send an approval request to the iPhone and wait for the decision")
|
|
2060
|
+
.exitOverride(exitWithApprovalError)
|
|
1999
2061
|
.argument("<question>", 'The decision to make, such as "Send the pricing update email to 214 customers?"')
|
|
2000
|
-
.option("--details <text>", "
|
|
2062
|
+
.option("--details <text>", "Context that helps decide, up to 4,000 characters. Shown in full on the review screen")
|
|
2001
2063
|
.option("--approve-label <label>", "Approve button title, a concrete verb such as Send or Deploy", "Approve")
|
|
2002
2064
|
.option("--reject-label <label>", "Reject button title, such as Cancel or Don't Send", "Reject")
|
|
2003
2065
|
.option("--command <command>", "The exact command waiting for approval. Do not shorten or rewrite it")
|
|
@@ -2007,35 +2069,75 @@ approvalCommand
|
|
|
2007
2069
|
`Where the action applies, such as environment:production. Types: ${APPROVAL_SCOPE_TYPES.join(", ")} (default: working_directory:<current directory> with --command)`,
|
|
2008
2070
|
parseApprovalScope
|
|
2009
2071
|
)
|
|
2010
|
-
.
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2072
|
+
.option(
|
|
2073
|
+
"--option <[id=]label>",
|
|
2074
|
+
"A choice instead of approve and reject. Repeat 2 to 4 times. IDs default to a, b, c, d",
|
|
2075
|
+
repeatable(parseOptionArgument)
|
|
2014
2076
|
)
|
|
2015
|
-
.option("--
|
|
2016
|
-
.option(
|
|
2017
|
-
|
|
2077
|
+
.option("--recommended <option-id>", "The option you would pick. At most one")
|
|
2078
|
+
.option(
|
|
2079
|
+
"--option-screenshot <option-id=file>",
|
|
2080
|
+
"A screenshot that shows one option. Repeatable",
|
|
2081
|
+
repeatable(parseOptionScreenshotArgument)
|
|
2082
|
+
)
|
|
2083
|
+
.option(
|
|
2084
|
+
"--screenshot <file[=title]>",
|
|
2085
|
+
"A PNG, JPEG, or WebP up to 2 MB to show on the review screen. Repeatable, 5 at most",
|
|
2086
|
+
repeatable(parseScreenshotArgument)
|
|
2087
|
+
)
|
|
2088
|
+
.option(
|
|
2089
|
+
"--git-changes [range]",
|
|
2090
|
+
"Show the file count, line totals, and the 2 most changed file names from git diff: uncommitted changes, or a range such as main...HEAD"
|
|
2091
|
+
)
|
|
2092
|
+
.option("--change <path:added:removed>", "A changed file and its line counts, such as src/app.ts:12:3. Repeatable. Adds to the same summary", repeatable(parseChangeArgument))
|
|
2093
|
+
.option("--link <[title=]url>", "An https link to open from the review screen, such as a pull request. Repeatable, 5 at most", repeatable(parseLinkArgument))
|
|
2018
2094
|
.option("--channels <slugs>", "Comma-separated channel slugs. Omit to ask on every paired device")
|
|
2019
2095
|
.option("--timeout <seconds>", "Stop waiting after this many seconds and exit 3 if still pending", parsePositiveSeconds)
|
|
2020
2096
|
.option("--no-wait", "Send the request, print the approval ID, and exit without waiting")
|
|
2097
|
+
.option("--delivery <surface>", "Deprecated and ignored. Approvals are always Push Notifications")
|
|
2098
|
+
.option("--color <color>", "Deprecated and ignored")
|
|
2099
|
+
.option("--icon <symbol>", "Deprecated and ignored")
|
|
2100
|
+
.option("--badge <title>", "Deprecated and ignored")
|
|
2021
2101
|
.action(async (question, options) => {
|
|
2022
2102
|
const globalOptions = program.opts();
|
|
2023
2103
|
|
|
2024
2104
|
try {
|
|
2025
|
-
const
|
|
2105
|
+
const ignored = DEPRECATED_APPROVAL_FLAGS.filter(([key]) => options[key] !== undefined).map(([, flag]) => flag);
|
|
2106
|
+
if (ignored.length > 0 && !globalOptions.json) {
|
|
2107
|
+
console.error(`Ignoring ${ignored.join(", ")}: approvals are always Push Notifications.`);
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2026
2110
|
const tool = options.tool ?? (options.command ? "shell" : undefined);
|
|
2027
|
-
if (options.command && !tool) throw new Error("Provide --tool with --command.");
|
|
2028
2111
|
const scope = options.scope ?? (options.command ? { type: "working_directory", value: process.cwd() } : undefined);
|
|
2029
|
-
const
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2112
|
+
const optionScreenshotArgs = options.optionScreenshot ?? [];
|
|
2113
|
+
const optionScreenshotPaths = Object.fromEntries(optionScreenshotArgs.map(({ id, path }) => [id, path]));
|
|
2114
|
+
// Validate everything before uploading or sending anything.
|
|
2115
|
+
const approvalOptions = buildApprovalOptions(options.option ?? [], {
|
|
2116
|
+
recommended: options.recommended,
|
|
2117
|
+
screenshots: optionScreenshotPaths,
|
|
2118
|
+
});
|
|
2119
|
+
const links = options.link ?? [];
|
|
2120
|
+
if (links.length > APPROVAL_LIMITS.maxLinks) {
|
|
2121
|
+
throw new Error(`An approval can show at most ${APPROVAL_LIMITS.maxLinks} links, not ${links.length}.`);
|
|
2122
|
+
}
|
|
2123
|
+
const changedFiles = [
|
|
2124
|
+
...(options.gitChanges !== undefined ? await readGitChanges(options.gitChanges) : []),
|
|
2125
|
+
...(options.change ?? []),
|
|
2126
|
+
];
|
|
2127
|
+
const changes = buildApprovalChanges(changedFiles);
|
|
2034
2128
|
const channels = String(options.channels ?? "")
|
|
2035
2129
|
.split(",")
|
|
2036
2130
|
.map((slug) => slug.trim())
|
|
2037
2131
|
.filter(Boolean);
|
|
2038
2132
|
|
|
2133
|
+
const token = await requireApiKey(globalOptions);
|
|
2134
|
+
const { screenshots, screenshotIdByOption } = await uploadApprovalScreenshots(
|
|
2135
|
+
options.screenshot ?? [],
|
|
2136
|
+
optionScreenshotArgs,
|
|
2137
|
+
token,
|
|
2138
|
+
globalOptions
|
|
2139
|
+
);
|
|
2140
|
+
|
|
2039
2141
|
const result = await apiRequest("/approvals", {
|
|
2040
2142
|
token,
|
|
2041
2143
|
method: "POST",
|
|
@@ -2044,10 +2146,18 @@ approvalCommand
|
|
|
2044
2146
|
...(options.details ? { details: options.details } : {}),
|
|
2045
2147
|
...(tool ? { execution_context: { tool, ...(options.command ? { command: options.command } : {}) } } : {}),
|
|
2046
2148
|
...(scope ? { scope } : {}),
|
|
2047
|
-
|
|
2149
|
+
...(changes ? { changes } : {}),
|
|
2150
|
+
...(links.length > 0 ? { links } : {}),
|
|
2151
|
+
...(screenshots.length > 0 ? { screenshots } : {}),
|
|
2152
|
+
...(approvalOptions
|
|
2153
|
+
? {
|
|
2154
|
+
options: approvalOptions.map((option) =>
|
|
2155
|
+
screenshotIdByOption[option.id] ? { ...option, screenshot_id: screenshotIdByOption[option.id] } : option
|
|
2156
|
+
),
|
|
2157
|
+
}
|
|
2158
|
+
: {}),
|
|
2048
2159
|
approve_label: options.approveLabel,
|
|
2049
2160
|
reject_label: options.rejectLabel,
|
|
2050
|
-
...(Object.keys(appearance).length > 0 ? { appearance } : {}),
|
|
2051
2161
|
...(channels.length > 0 ? { target_channel_slugs: channels } : {}),
|
|
2052
2162
|
},
|
|
2053
2163
|
});
|
|
@@ -2073,6 +2183,7 @@ approvalCommand
|
|
|
2073
2183
|
approvalCommand
|
|
2074
2184
|
.command("wait")
|
|
2075
2185
|
.description("Wait for the decision on an approval request")
|
|
2186
|
+
.exitOverride(exitWithApprovalError)
|
|
2076
2187
|
.argument("<approval-id>", "Approval ID from approval request")
|
|
2077
2188
|
.option("--timeout <seconds>", "Stop waiting after this many seconds and exit 3 if still pending", parsePositiveSeconds)
|
|
2078
2189
|
.action(async (approvalId, options) => {
|
|
@@ -2096,9 +2207,8 @@ approvalCommand
|
|
|
2096
2207
|
try {
|
|
2097
2208
|
const token = await requireApiKey(globalOptions);
|
|
2098
2209
|
const result = await apiRequest(`/approvals/${encodeURIComponent(approvalId)}`, { token });
|
|
2099
|
-
const status = result.approval?.status;
|
|
2100
2210
|
outputResult(result, globalOptions, [
|
|
2101
|
-
|
|
2211
|
+
...approvalResultLines(result.approval),
|
|
2102
2212
|
`Approval ID: ${approvalId}`,
|
|
2103
2213
|
result.approval?.question ? `Question: ${result.approval.question}` : null,
|
|
2104
2214
|
]);
|