activitysmith-cli 1.15.0 → 1.16.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 CHANGED
@@ -25,11 +25,14 @@ For example, a Codex agent can work on your computer, send a Push Notification w
25
25
  ## Quickstart
26
26
 
27
27
  1. Run `activitysmith auth login` and approve access in your browser.
28
- 2. Run `activitysmith --help` to inspect available commands.
28
+ 2. Run `activitysmith devices list` to check that your iPhone is paired.
29
+ 3. Run `activitysmith --help` to inspect available commands.
29
30
 
30
31
  ```bash
31
32
  activitysmith auth login
32
33
 
34
+ activitysmith devices list
35
+
33
36
  activitysmith push --title "Hello"
34
37
  ```
35
38
 
@@ -86,6 +89,44 @@ Or pass the key directly:
86
89
  activitysmith --api-key "YOUR-API-KEY" push --title "Hello"
87
90
  ```
88
91
 
92
+ ### Create an API key from the CLI
93
+
94
+ Create an API key for CI, servers, and scripts without opening the web app. The command uses the browser sign-in. The first time, it opens your browser so you can approve API key access.
95
+
96
+ ```bash
97
+ activitysmith keys create --name "GitHub Actions"
98
+ ```
99
+
100
+ The key is shown only once. Add `--raw` to print only the key, then pipe it straight into a secret store. The key never appears in your terminal:
101
+
102
+ ```bash
103
+ activitysmith keys create --name "GitHub Actions" --raw | gh secret set ACTIVITYSMITH_API_KEY
104
+ ```
105
+
106
+ Add `--channels` to limit the key to some Channels:
107
+
108
+ ```bash
109
+ activitysmith keys create --name "Ops alerts" --channels ops,alerts
110
+ ```
111
+
112
+ An API key cannot create another key. This command always uses the browser sign-in, even when `ACTIVITYSMITH_API_KEY` is set.
113
+
114
+ ## Paired Devices
115
+
116
+ List the iPhones and iPads paired to your account. Active devices come first:
117
+
118
+ ```bash
119
+ activitysmith devices list
120
+ ```
121
+
122
+ If no device is paired, the command prints the App Store link. Install ActivitySmith on your iPhone, sign in with the same account, and allow notifications.
123
+
124
+ Add `--include-inactive` to also list devices that can no longer receive deliveries:
125
+
126
+ ```bash
127
+ activitysmith devices list --include-inactive
128
+ ```
129
+
89
130
  ## Push Notifications
90
131
 
91
132
  ### Send a Push Notification
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "activitysmith-cli",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
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/auth.js CHANGED
@@ -16,6 +16,10 @@ export const CLIENT_NAME = "ActivitySmith CLI";
16
16
  export const CLIENT_ID = "https://activitysmith.com/.well-known/oauth-client/activitysmith-cli.json";
17
17
  // Sending Push Notifications, Live Activities, badges, and widget values. Nothing else.
18
18
  export const LOGIN_SCOPES = "mcp:write";
19
+ // Asked for only by `activitysmith keys create`, which signs in again so the user approves it in the browser.
20
+ export const API_KEYS_SCOPE = "api_keys:write";
21
+
22
+ export const hasScope = (credentials, scope) => String(credentials?.scope ?? "").split(/\s+/).includes(scope);
19
23
  const LOGIN_TIMEOUT_MS = 5 * 60 * 1000;
20
24
  // Refresh a little early so a token does not expire mid-request.
21
25
  const ACCESS_TOKEN_REFRESH_MARGIN_MS = 60 * 1000;
@@ -95,13 +99,13 @@ export const discoverOAuthServer = async ({ issuer, fetchImpl }) => {
95
99
  return metadata;
96
100
  };
97
101
 
98
- export const buildAuthorizeUrl = ({ authorizationEndpoint, redirectUri, challenge, state, resource }) => {
102
+ export const buildAuthorizeUrl = ({ authorizationEndpoint, redirectUri, challenge, state, resource, scope = LOGIN_SCOPES }) => {
99
103
  const url = new URL(authorizationEndpoint);
100
104
  url.search = new URLSearchParams({
101
105
  response_type: "code",
102
106
  client_id: CLIENT_ID,
103
107
  redirect_uri: redirectUri,
104
- scope: LOGIN_SCOPES,
108
+ scope,
105
109
  state,
106
110
  code_challenge: challenge,
107
111
  code_challenge_method: "S256",
@@ -255,6 +259,7 @@ export const login = async ({
255
259
  log = (line) => console.error(line),
256
260
  now = Date.now,
257
261
  timeoutMs = LOGIN_TIMEOUT_MS,
262
+ scope = LOGIN_SCOPES,
258
263
  } = {}) => {
259
264
  let previous = null;
260
265
  try {
@@ -275,6 +280,7 @@ export const login = async ({
275
280
  challenge,
276
281
  state,
277
282
  resource,
283
+ scope,
278
284
  });
279
285
  const opened = noBrowser ? false : await launchBrowser(authorizeUrl);
280
286
  log(opened ? "Opened your browser to sign in to ActivitySmith." : "Open this URL in a browser to sign in to ActivitySmith:");
package/src/cli.js CHANGED
@@ -2,7 +2,17 @@
2
2
 
3
3
  import { Command, InvalidArgumentError, Option } from "commander";
4
4
  import { loadMetadata } from "./metadata.js";
5
- import { credentialsPath, getAccessToken, login, logout, readCredentials } from "./auth.js";
5
+ import {
6
+ API_KEYS_SCOPE,
7
+ DEFAULT_API_RESOURCE,
8
+ LOGIN_SCOPES,
9
+ credentialsPath,
10
+ getAccessToken,
11
+ hasScope,
12
+ login,
13
+ logout,
14
+ readCredentials,
15
+ } from "./auth.js";
6
16
  import ActivitySmith from "activitysmith";
7
17
  import { createRequire } from "module";
8
18
  import { readFile } from "fs/promises";
@@ -1702,6 +1712,131 @@ addLiveActivityActionOptions(addContentStateOptions(
1702
1712
  { includeAutoDismiss: true, includeAutoDismissSeconds: true }
1703
1713
  ));
1704
1714
 
1715
+ const APP_STORE_URL = "https://apps.apple.com/us/app/activitysmith/id6752254835";
1716
+
1717
+ // Account endpoints the generated SDK does not cover. Same API and SDK header as the SDK client, so
1718
+ // errors print the same way and usage shows the CLI version.
1719
+ const apiRequest = async (path, { token, method = "GET", body } = {}) => {
1720
+ const response = await fetch(`${DEFAULT_API_RESOURCE}${path}`, {
1721
+ method,
1722
+ headers: {
1723
+ Authorization: `Bearer ${token}`,
1724
+ Accept: "application/json",
1725
+ "X-ActivitySmith-SDK": `cli-v${version}`,
1726
+ ...(body ? { "Content-Type": "application/json" } : {}),
1727
+ },
1728
+ ...(body ? { body: JSON.stringify(body) } : {}),
1729
+ });
1730
+ if (!response.ok) {
1731
+ const error = new Error(`Request failed with status ${response.status}`);
1732
+ error.response = response;
1733
+ throw error;
1734
+ }
1735
+ return response.json();
1736
+ };
1737
+
1738
+ const describeDevice = (device) => {
1739
+ const name = device.device_name || device.device_type || "Device";
1740
+ const system = [device.system_name, device.system_version].filter(Boolean).join(" ");
1741
+ const details = [system, device.is_active ? "active" : "inactive", device.user_email].filter(Boolean);
1742
+ return `- ${name} (${details.join(", ")}) ID: ${device.device_id}`;
1743
+ };
1744
+
1745
+ const devicesCommand = program
1746
+ .command("devices")
1747
+ .description("List the iPhones and iPads paired to your account");
1748
+
1749
+ devicesCommand
1750
+ .command("list", { isDefault: true })
1751
+ .description("List paired devices, active ones first")
1752
+ .option("--include-inactive", "Also list devices that can no longer receive deliveries")
1753
+ .action(async (options) => {
1754
+ const globalOptions = program.opts();
1755
+
1756
+ try {
1757
+ const token = await requireApiKey(globalOptions);
1758
+ const query = options.includeInactive ? "?include_inactive=true" : "";
1759
+ const result = await apiRequest(`/account/devices${query}`, { token });
1760
+ const devices = Array.isArray(result.devices) ? result.devices : [];
1761
+ const active = devices.filter((device) => device.is_active).length;
1762
+ outputResult(
1763
+ result,
1764
+ globalOptions,
1765
+ devices.length === 0
1766
+ ? [
1767
+ "No paired devices.",
1768
+ "Install ActivitySmith from the App Store, sign in with this account, and allow notifications:",
1769
+ APP_STORE_URL,
1770
+ ]
1771
+ : [
1772
+ `${devices.length} ${devices.length === 1 ? "device" : "devices"}, ${active} active:`,
1773
+ ...devices.map(describeDevice),
1774
+ result.meta?.has_more ? "More devices exist. Use --json to see the meta.next_offset value." : null,
1775
+ ]
1776
+ );
1777
+ } catch (error) {
1778
+ await handleError(error, globalOptions);
1779
+ }
1780
+ });
1781
+
1782
+ const keysCommand = program
1783
+ .command("keys")
1784
+ .description("Create API keys for CI, servers, and scripts");
1785
+
1786
+ keysCommand
1787
+ .command("create")
1788
+ .description(
1789
+ "Create an API key with the browser sign-in. The first time, approve API key access in the browser"
1790
+ )
1791
+ .requiredOption("--name <name>", 'Key name, such as "GitHub Actions"')
1792
+ .option("--channels <slugs>", "Comma-separated channel slugs. The key reaches only these Channels")
1793
+ .option("--raw", "Print only the key on standard output, for piping it into a secret store")
1794
+ .action(async (options) => {
1795
+ const globalOptions = program.opts();
1796
+
1797
+ try {
1798
+ // API keys cannot create keys, so this always uses the browser sign-in, even when an API key is set.
1799
+ let stored = null;
1800
+ try {
1801
+ stored = readCredentials();
1802
+ } catch {
1803
+ // An unreadable sign-in is replaced by the login below.
1804
+ }
1805
+ if (!hasScope(stored, API_KEYS_SCOPE)) {
1806
+ console.error("Approve API key access in the browser so the CLI can create API keys.");
1807
+ await login({ scope: `${LOGIN_SCOPES} ${API_KEYS_SCOPE}` });
1808
+ }
1809
+ const token = await getAccessToken();
1810
+ usingSignIn = true;
1811
+ const channels = String(options.channels ?? "")
1812
+ .split(",")
1813
+ .map((slug) => slug.trim())
1814
+ .filter(Boolean);
1815
+ const result = await apiRequest("/account/api-keys", {
1816
+ token,
1817
+ method: "POST",
1818
+ body: { name: options.name, ...(channels.length > 0 ? { channels } : {}) },
1819
+ });
1820
+ const key = result.api_key?.key;
1821
+ if (!key) {
1822
+ throw new Error("The API did not return a key. Run the command again.");
1823
+ }
1824
+
1825
+ if (options.raw) {
1826
+ process.stdout.write(`${key}\n`);
1827
+ console.error(`Created API key "${result.api_key.name}". Store it as a secret and never commit it.`);
1828
+ return;
1829
+ }
1830
+ outputResult(result, globalOptions, [
1831
+ `Created API key "${result.api_key.name}".`,
1832
+ `Key: ${key}`,
1833
+ "This is the only time the key is shown. Store it as a secret named ACTIVITYSMITH_API_KEY and never commit it.",
1834
+ ]);
1835
+ } catch (error) {
1836
+ await handleError(error, globalOptions);
1837
+ }
1838
+ });
1839
+
1705
1840
  const authCommand = program
1706
1841
  .command("auth")
1707
1842
  .description("Sign in through the browser and manage the saved sign-in");