chesly 0.2.0 → 0.3.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 +63 -10
- package/dist/errors.js +2 -0
- package/dist/index.js +483 -409
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,9 +4,9 @@ Your chats from the terminal, for you and your agents. `chesly` is the command-l
|
|
|
4
4
|
[Chesly](https://chesly.app) API. It is built for agents (Claude Code, Codex, OpenClaw) as much as
|
|
5
5
|
for people: JSON when piped, stable exit codes, and it never prompts.
|
|
6
6
|
|
|
7
|
-
Read your chats, send messages,
|
|
8
|
-
Telegram, Signal, iMessage, Instagram, Messenger, X, LinkedIn,
|
|
9
|
-
Messages, Google Chat and email.
|
|
7
|
+
Read your chats, send messages, edit or delete the ones you sent, follow events as they happen,
|
|
8
|
+
and manage webhooks, on WhatsApp, Telegram, Signal, iMessage, Instagram, Messenger, X, LinkedIn,
|
|
9
|
+
Discord, Slack, Reddit, Google Messages, Google Chat and email.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -108,8 +108,9 @@ chesly accounts # your connected accounts and their statu
|
|
|
108
108
|
Email is left out of `chats`, `catchup` and `search` unless you ask for it with `--network email`.
|
|
109
109
|
`--network` takes any one network, and on `read` and `send` it says where to look a chat name up:
|
|
110
110
|
an email thread's name is its subject. The names are `whatsapp`, `telegram`, `signal`, `imessage`,
|
|
111
|
-
`instagram`, `messenger`, `twitter` (X), `linkedin`, `discord`, `slack`, `reddit`, `gmessages`
|
|
112
|
-
(Google Messages), `googlechat`
|
|
111
|
+
`instagram`, `messenger`, `line`, `twitter` (X), `linkedin`, `discord`, `slack`, `reddit`, `gmessages`
|
|
112
|
+
(Google Messages), `googlechat`, `grokbot` (Grok Bot), `chatgptdots` (OpenAI Dots), `muse` and `email`,
|
|
113
|
+
with `tiktok` and `wechat` coming soon; `chesly accounts` shows each account's.
|
|
113
114
|
|
|
114
115
|
```sh
|
|
115
116
|
chesly chats --network email # your email threads
|
|
@@ -183,6 +184,56 @@ with the first attempt's result instead of sending again. A file send's error al
|
|
|
183
184
|
upload as `media_id`: retry with `--media-id <that id>` instead of `--file`, since a new upload
|
|
184
185
|
would be a new media ID, which the same key refuses (422 `idempotency_key_reused`).
|
|
185
186
|
|
|
187
|
+
## Edit and delete messages
|
|
188
|
+
|
|
189
|
+
```sh
|
|
190
|
+
chesly edit msg_... - <<'EOF' # change the text of a message you sent
|
|
191
|
+
On my way, 10 minutes
|
|
192
|
+
EOF
|
|
193
|
+
chesly delete msg_... # delete a message you sent, for everyone in the chat
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
`delete` works on any message you sent: from Chesly (its apps or the API, such as a `chesly send`,
|
|
197
|
+
whose answer's `id` is the message ID) or from the network's own app (WhatsApp on your phone, say).
|
|
198
|
+
`edit` works only on messages you sent from Chesly: one you sent in the network's own app is
|
|
199
|
+
refused, as Chesly can't change its text on the network. Email can be neither edited nor unsent. `edit` takes text messages only, not photos, videos, files, voice notes,
|
|
200
|
+
stickers, locations, polls or calls. Its text comes on stdin like a send's, and the network caps it
|
|
201
|
+
as it caps a send: 1,000 characters on Instagram, 2,000 on Discord, 4,096 on Telegram, 10,000
|
|
202
|
+
elsewhere.
|
|
203
|
+
|
|
204
|
+
Each network's time limits apply. Past them the API refuses instead of pretending, because the
|
|
205
|
+
network would keep showing the old message:
|
|
206
|
+
|
|
207
|
+
| Network | Edit | Delete for everyone |
|
|
208
|
+
|---|---|---|
|
|
209
|
+
| WhatsApp | 15 minutes | 2 days |
|
|
210
|
+
| Signal | 24 hours, at most 10 edits | 24 hours |
|
|
211
|
+
| Telegram | 48 hours | no limit |
|
|
212
|
+
| Instagram | 15 minutes | no limit |
|
|
213
|
+
| Messenger | 15 minutes | 10 minutes |
|
|
214
|
+
| Google Messages | 15 minutes | 15 minutes |
|
|
215
|
+
| LinkedIn | 1 hour | 1 hour |
|
|
216
|
+
| iMessage | 15 minutes, at most 5 edits | 2 minutes |
|
|
217
|
+
| Discord, Slack, Google Chat, TikTok | no limit | no limit |
|
|
218
|
+
| X | no limit | not possible (X keeps it for the other person) |
|
|
219
|
+
| OpenAI Dots | no limit | not possible |
|
|
220
|
+
| WeChat | not possible | 2 minutes |
|
|
221
|
+
| LINE | not possible | 24 hours |
|
|
222
|
+
| Snapchat, Reddit | not possible | no limit |
|
|
223
|
+
| Grok Bot, Muse | not possible | not possible |
|
|
224
|
+
|
|
225
|
+
A refused edit exits 1 with `message_not_editable`, and a refused delete with
|
|
226
|
+
`message_not_deletable`. The error says why: not yours, not sent from Chesly (an edit), not text,
|
|
227
|
+
deleted, email, a network that takes none, or past the network's time limit, which it names.
|
|
228
|
+
|
|
229
|
+
`edit` prints the edited message, with its new `text` and `edited: true`. `delete` prints
|
|
230
|
+
`{"id":"msg_...","deleted":true}`, or "Deleted <id> for everyone in the chat." in a terminal. A
|
|
231
|
+
deleted message can't be brought back. Both are safe to run again: editing to the text a message
|
|
232
|
+
already has changes nothing, and deleting a message that is already deleted answers the same. An
|
|
233
|
+
account can edit or delete as many messages an hour as it can send (60 on WhatsApp, 30 elsewhere);
|
|
234
|
+
past that, the command exits 5 with `rate_limited` and `retry_after`. A read-only key can do
|
|
235
|
+
neither (403 `insufficient_scope`).
|
|
236
|
+
|
|
186
237
|
## Watch events
|
|
187
238
|
|
|
188
239
|
```sh
|
|
@@ -296,8 +347,8 @@ Sends:
|
|
|
296
347
|
telegram · 0/30 this hour · 12/150 today
|
|
297
348
|
```
|
|
298
349
|
|
|
299
|
-
An API plan is a flat monthly price, Personal or Pro: calls are counted, never billed, and
|
|
300
|
-
has its own
|
|
350
|
+
An API plan is a flat monthly price, Starter, Personal or Pro: calls are counted, never billed, and
|
|
351
|
+
each plan has its own limits and, on Personal and Pro, transcription minutes (https://web.chesly.app/api/billing). Minutes used
|
|
301
352
|
round down, and once they're used up the line says when they come back
|
|
302
353
|
(`600 of 600 min, used up until Nov 28`). A downgrade waits for the end of the period, and the plan
|
|
303
354
|
line says when (`Plan: Pro, active: changes to Personal on Jan 14`). The request limits apply to
|
|
@@ -317,6 +368,8 @@ each key and to all your keys together. With `--json` it prints the API's `GET /
|
|
|
317
368
|
| `chesly search <query> [--chat <chat>] [--network <name>] [--limit <n>]` | Searches your messages, in every chat or one (`GET /v1/messages/search`) |
|
|
318
369
|
| `chesly catchup [--chats <n>] [--per-chat <n>] [--network <name>]` | Your unread chats (not muted or archived) with their latest messages, in one call (`GET /v1/catch_up`) |
|
|
319
370
|
| `chesly send <chat> [text\|-] [--reply-to <msg_id>] [--file <path>\|--media-id <id>] [--wait] [--idempotency-key <key>] [--network <name>]` | Sends a message, a reply or a file (`POST /v1/media`, `POST /v1/chats/{chat_id}/messages`) |
|
|
371
|
+
| `chesly edit <message_id> <text\|->` | Changes the text of a message you sent from Chesly, for everyone in the chat (`PATCH /v1/messages/{message_id}`) |
|
|
372
|
+
| `chesly delete <message_id>` | Deletes a message you sent, from Chesly or the network's own app, for everyone in the chat (`DELETE /v1/messages/{message_id}`) |
|
|
320
373
|
| `chesly watch [--chat <chat>] [--account <acct_id>] [--types <a,b>] [--networks <a,b>] [--after <evt_id>]` | Prints events as they happen, one per line (`GET /v1/events/stream`) |
|
|
321
374
|
| `chesly webhooks list\|create\|delete\|rotate\|test\|deliveries` | Manages webhooks (`/v1/webhooks`) |
|
|
322
375
|
| `chesly keys list\|revoke <key_id>` | Lists or revokes API keys (`/v1/keys`) |
|
|
@@ -378,14 +431,14 @@ included, on every network but email (add `--network email` for email threads):
|
|
|
378
431
|
| 1 | error |
|
|
379
432
|
| 2 | usage |
|
|
380
433
|
| 3 | not signed in (no key, a malformed one, or the key was rejected) |
|
|
381
|
-
| 4 | not found (an unknown chat, webhook or key, or a name that matches no chat), or ambiguous (a name that matches several) |
|
|
434
|
+
| 4 | not found (an unknown chat, message, webhook or key, or a name that matches no chat), or ambiguous (a name that matches several) |
|
|
382
435
|
| 5 | not final, come back to it: rate limited (a 429: too many requests, a send cap, uploads held after a slow one, or too many open event streams), a request that took too long to arrive (a 408, such as an upload's `upload_timeout`), briefly unavailable (a 502, 503 or 504, from the API or a gateway), a first attempt still running (a 409 `idempotency_in_progress`), or a send the network didn't confirm (`delivery_unconfirmed`: check the chat before you send it again). `retry_after` says when, if the answer has it |
|
|
383
|
-
| 6 | payment required: the account needs an API plan, or this period's transcription minutes are used up |
|
|
436
|
+
| 6 | payment required: the account needs an API plan, or this period's transcription minutes are used up (Starter has none) |
|
|
384
437
|
|
|
385
438
|
- **MCP:** give Claude Code the Chesly MCP server with your key:
|
|
386
439
|
|
|
387
440
|
```sh
|
|
388
|
-
claude mcp add --transport http chesly https://
|
|
441
|
+
claude mcp add --transport http chesly https://mcp.chesly.app/mcp \
|
|
389
442
|
--header "Authorization: Bearer $(chesly key --reveal)"
|
|
390
443
|
```
|
|
391
444
|
|
package/dist/errors.js
CHANGED
|
@@ -74,6 +74,8 @@ var API_ERROR_CODES = [
|
|
|
74
74
|
{ code: "idempotency_key_reused", status: 422, title: "This Idempotency-Key was used for a different request." },
|
|
75
75
|
{ code: "idempotency_in_progress", status: 409, title: "A request with this Idempotency-Key is still running." },
|
|
76
76
|
{ code: "message_request", status: 409, title: "This chat is a message request." },
|
|
77
|
+
{ code: "message_not_editable", status: 409, title: "This message can't be edited." },
|
|
78
|
+
{ code: "message_not_deletable", status: 409, title: "This message can't be deleted." },
|
|
77
79
|
{ code: "send_limit_reached", status: 429, title: "This account's send limit is reached." },
|
|
78
80
|
{ code: "text_too_long", status: 400, title: "The text is too long for this network." },
|
|
79
81
|
{ code: "invalid_reply_to", status: 400, title: "reply_to must be a message in this chat." },
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import { STATUS_CODES } from "http";
|
|
|
9
9
|
// package.json
|
|
10
10
|
var package_default = {
|
|
11
11
|
name: "chesly",
|
|
12
|
-
version: "0.
|
|
12
|
+
version: "0.3.1",
|
|
13
13
|
description: "Chesly CLI: your chats from the terminal and your agents",
|
|
14
14
|
license: "MIT",
|
|
15
15
|
type: "module",
|
|
@@ -1221,6 +1221,7 @@ function keyTable(keys, inUse) {
|
|
|
1221
1221
|
}
|
|
1222
1222
|
var BILLING_URL = "https://web.chesly.app/api/billing";
|
|
1223
1223
|
var PLAN_NAMES = /* @__PURE__ */ new Map([
|
|
1224
|
+
["starter", "Starter"],
|
|
1224
1225
|
["personal", "Personal"],
|
|
1225
1226
|
["pro", "Pro"]
|
|
1226
1227
|
]);
|
|
@@ -1288,6 +1289,7 @@ function scheduledChange(change) {
|
|
|
1288
1289
|
return `changes to ${name} ${when.includes(":") ? "at" : "on"} ${when}`;
|
|
1289
1290
|
}
|
|
1290
1291
|
function transcriptionMinutes(plan) {
|
|
1292
|
+
if (plan.plan === "starter") return "not part of Starter";
|
|
1291
1293
|
const usedSeconds = Number(plan.transcription_seconds_this_period) || 0;
|
|
1292
1294
|
const used = count(Math.floor(usedSeconds / 60));
|
|
1293
1295
|
const included = Number(plan.transcription_seconds_included);
|
|
@@ -1365,6 +1367,11 @@ function parseNetwork(value) {
|
|
|
1365
1367
|
}
|
|
1366
1368
|
return network;
|
|
1367
1369
|
}
|
|
1370
|
+
function parseMessageId(value) {
|
|
1371
|
+
const id = value.trim();
|
|
1372
|
+
if (id === "") throw new InvalidArgumentError("Give a message ID (msg_\u2026).");
|
|
1373
|
+
return id;
|
|
1374
|
+
}
|
|
1368
1375
|
function parseList(value) {
|
|
1369
1376
|
const items = [...new Set(value.split(",").map((item) => item.trim()).filter(Boolean))];
|
|
1370
1377
|
if (items.length === 0) throw new InvalidArgumentError("Give one or more names, separated by commas.");
|
|
@@ -1466,144 +1473,492 @@ Examples:
|
|
|
1466
1473
|
});
|
|
1467
1474
|
}
|
|
1468
1475
|
|
|
1469
|
-
// src/commands/
|
|
1470
|
-
function
|
|
1471
|
-
program.command("
|
|
1476
|
+
// src/commands/delete.ts
|
|
1477
|
+
function registerDelete(program, io) {
|
|
1478
|
+
program.command("delete").description("Delete a message you sent, for everyone in the chat").argument("<message_id>", "the message: a msg_ ID from chesly send or chesly read", parseMessageId).addHelpText(
|
|
1472
1479
|
"after",
|
|
1473
1480
|
`
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1481
|
+
Deletes a message you sent, from Chesly or the network's own app, for
|
|
1482
|
+
everyone in the chat, within the time the network allows, such as 2 days
|
|
1483
|
+
on WhatsApp or 24 hours on Signal. A deleted message can't be brought back.
|
|
1484
|
+
Someone else's message, an email, or one past the network's limit is
|
|
1485
|
+
refused with message_not_deletable (exit 1), and the error says why. An
|
|
1486
|
+
unknown ID exits 4.
|
|
1487
|
+
|
|
1488
|
+
Prints { "id", "deleted": true }, or "Deleted <id> for everyone in the
|
|
1489
|
+
chat." in a terminal. Deleting a message that is already deleted answers
|
|
1490
|
+
the same, so the command is safe to run again.
|
|
1491
|
+
|
|
1492
|
+
Example:
|
|
1493
|
+
chesly delete msg_7Hq2mR9xLp4vT1sK8nWz.JGV3A`
|
|
1494
|
+
).action(async (messageId, _options, command) => {
|
|
1495
|
+
const out = new Output(io, command.optsWithGlobals());
|
|
1496
|
+
const { key } = await requireKey();
|
|
1497
|
+
await apiRequest(`/v1/messages/${encodeURIComponent(messageId)}`, { method: "DELETE", key });
|
|
1498
|
+
out.result({ id: messageId, deleted: true }, () => `Deleted ${messageId} for everyone in the chat.`);
|
|
1488
1499
|
});
|
|
1489
1500
|
}
|
|
1490
1501
|
|
|
1491
|
-
// src/commands/
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1502
|
+
// src/commands/send.ts
|
|
1503
|
+
import { randomUUID } from "crypto";
|
|
1504
|
+
|
|
1505
|
+
// src/resolve-chat.ts
|
|
1506
|
+
var LOOKUP_LIMIT = 10;
|
|
1507
|
+
var CHAT_ARGUMENT_HELP = `A <chat> is a chat ID (chat_\u2026) or a name. An exact name wins, in any case;
|
|
1508
|
+
so does the only partial match. No match, or several, exits 4, and JSON
|
|
1509
|
+
mode lists the chats a name could mean as "candidates". Names are looked up
|
|
1510
|
+
on every network but email; --network names the one to look in (an email
|
|
1511
|
+
thread's name is its subject: --network email).`;
|
|
1512
|
+
async function resolveChat(arg, key, options = {}) {
|
|
1513
|
+
const name = arg.trim();
|
|
1514
|
+
if (name.startsWith("chat_")) return { id: name, name: null };
|
|
1515
|
+
if (name === "") throw new CliError("usage_error", "Give a chat ID (chat_\u2026) or a chat name.", EXIT.USAGE);
|
|
1516
|
+
const answer = await apiRequest("/v1/chats", {
|
|
1517
|
+
key: await key(),
|
|
1518
|
+
query: { q: name, archived: "all", limit: LOOKUP_LIMIT, network: options.network, account_id: options.accountId }
|
|
1519
|
+
});
|
|
1520
|
+
const page = expectAnswer(answer, isChatPage, "a page of chats");
|
|
1521
|
+
const exact = page.chats.filter((chat) => sameName(chat.name, name));
|
|
1522
|
+
const candidates = exact.length > 0 ? exact : page.chats;
|
|
1523
|
+
const [first, ...others] = candidates;
|
|
1524
|
+
if (first === void 0) {
|
|
1525
|
+
throw new CliError("chat_not_found", `No chat matches "${name}".`, EXIT.NOT_FOUND, {
|
|
1526
|
+
hint: "Run `chesly chats` to list your chats, or pass a chat ID."
|
|
1527
|
+
});
|
|
1503
1528
|
}
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1529
|
+
if (others.length === 0) return { id: first.id, name: first.name ?? null };
|
|
1530
|
+
const count2 = `${page.next_cursor ? "at least " : ""}${candidates.length}`;
|
|
1531
|
+
throw new CliError("ambiguous_chat", `"${name}" matches ${count2} chats. Use one of their chat IDs instead.`, EXIT.NOT_FOUND, {
|
|
1532
|
+
extras: { candidates },
|
|
1533
|
+
hint: indent(chatTable(candidates), " ")
|
|
1534
|
+
});
|
|
1507
1535
|
}
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
await apiRequest(`/v1/keys/${encodeURIComponent(id)}`, { method: "DELETE", key: using, timeoutMs: REVOKE_TIMEOUT_MS });
|
|
1511
|
-
return "revoked";
|
|
1512
|
-
} catch (error) {
|
|
1513
|
-
if (!(error instanceof CheslyApiError)) return "active";
|
|
1514
|
-
if (error.status === 403) return "forbidden";
|
|
1515
|
-
if (self && (error.status === 401 || error.status === 404 && error.code === "key_not_found")) return "gone";
|
|
1516
|
-
return "active";
|
|
1517
|
-
}
|
|
1536
|
+
function sameName(chatName, name) {
|
|
1537
|
+
return (chatName ?? "").trim().localeCompare(name, void 0, { sensitivity: "accent" }) === 0;
|
|
1518
1538
|
}
|
|
1519
|
-
|
|
1520
|
-
|
|
1539
|
+
|
|
1540
|
+
// src/upload.ts
|
|
1541
|
+
import { openAsBlob } from "fs";
|
|
1542
|
+
import { stat } from "fs/promises";
|
|
1543
|
+
import { basename, extname } from "path";
|
|
1544
|
+
var MAX_UPLOAD_BYTES = 5e7;
|
|
1545
|
+
var MIME_TYPES = {
|
|
1546
|
+
jpg: "image/jpeg",
|
|
1547
|
+
jpeg: "image/jpeg",
|
|
1548
|
+
png: "image/png",
|
|
1549
|
+
gif: "image/gif",
|
|
1550
|
+
webp: "image/webp",
|
|
1551
|
+
heic: "image/heic",
|
|
1552
|
+
heif: "image/heif",
|
|
1553
|
+
avif: "image/avif",
|
|
1554
|
+
bmp: "image/bmp",
|
|
1555
|
+
tif: "image/tiff",
|
|
1556
|
+
tiff: "image/tiff",
|
|
1557
|
+
svg: "image/svg+xml",
|
|
1558
|
+
mp4: "video/mp4",
|
|
1559
|
+
m4v: "video/x-m4v",
|
|
1560
|
+
mov: "video/quicktime",
|
|
1561
|
+
webm: "video/webm",
|
|
1562
|
+
mkv: "video/x-matroska",
|
|
1563
|
+
"3gp": "video/3gpp",
|
|
1564
|
+
mp3: "audio/mpeg",
|
|
1565
|
+
m4a: "audio/mp4",
|
|
1566
|
+
aac: "audio/aac",
|
|
1567
|
+
wav: "audio/wav",
|
|
1568
|
+
flac: "audio/flac",
|
|
1569
|
+
ogg: "audio/ogg",
|
|
1570
|
+
oga: "audio/ogg",
|
|
1571
|
+
opus: "audio/ogg",
|
|
1572
|
+
amr: "audio/amr",
|
|
1573
|
+
pdf: "application/pdf",
|
|
1574
|
+
txt: "text/plain",
|
|
1575
|
+
md: "text/markdown",
|
|
1576
|
+
csv: "text/csv",
|
|
1577
|
+
json: "application/json",
|
|
1578
|
+
vcf: "text/vcard",
|
|
1579
|
+
ics: "text/calendar",
|
|
1580
|
+
zip: "application/zip",
|
|
1581
|
+
doc: "application/msword",
|
|
1582
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1583
|
+
xls: "application/vnd.ms-excel",
|
|
1584
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1585
|
+
ppt: "application/vnd.ms-powerpoint",
|
|
1586
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
|
1587
|
+
};
|
|
1588
|
+
var SLOWEST_UPLOAD_BYTES_PER_SECOND = 131072;
|
|
1589
|
+
async function readUpload(path) {
|
|
1521
1590
|
try {
|
|
1522
|
-
const
|
|
1523
|
-
|
|
1591
|
+
const info = await stat(path);
|
|
1592
|
+
if (!info.isFile()) throw new CliError("file_unreadable", `${path} is not a file.`, EXIT.USAGE);
|
|
1593
|
+
if (info.size > MAX_UPLOAD_BYTES) {
|
|
1594
|
+
throw new CliError("media_too_large", `${path} is over 50 MB, the most a message can carry.`);
|
|
1595
|
+
}
|
|
1596
|
+
return { blob: await openAsBlob(path, { type: mimeTypeOf(path) }), name: basename(path), size: info.size };
|
|
1524
1597
|
} catch (error) {
|
|
1525
|
-
|
|
1526
|
-
|
|
1598
|
+
if (error instanceof CliError) throw error;
|
|
1599
|
+
throw new CliError("file_unreadable", `Can't read ${path}: ${reasonOf2(error)}`, EXIT.USAGE);
|
|
1527
1600
|
}
|
|
1528
1601
|
}
|
|
1529
|
-
function
|
|
1530
|
-
const
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
`
|
|
1537
|
-
There is no keys create. New keys come from chesly login, or from the dashboard at
|
|
1538
|
-
https://web.chesly.app/api/keys. chesly key shows the key this CLI uses.
|
|
1539
|
-
|
|
1540
|
-
Examples:
|
|
1541
|
-
chesly keys list
|
|
1542
|
-
chesly keys revoke 0b5c9a52-6f1e-4c41-9a43-2b7f0d0e1a07`
|
|
1543
|
-
);
|
|
1544
|
-
keys.command("list").description("List your active API keys, marking the one this CLI uses").addHelpText(
|
|
1545
|
-
"after",
|
|
1546
|
-
`
|
|
1547
|
-
Prints the API's { "keys": [...] }, or a table in a terminal. A key shows only
|
|
1548
|
-
its first 12 and last 4 characters, never the whole key.
|
|
1549
|
-
|
|
1550
|
-
Example:
|
|
1551
|
-
chesly keys list --json`
|
|
1552
|
-
).action(async (_options, command) => {
|
|
1553
|
-
const out = new Output(io, command.optsWithGlobals());
|
|
1554
|
-
const { key } = await requireKey();
|
|
1555
|
-
const answer = expectAnswer(await apiRequest("/v1/keys", { key }), isKeyList, "a list of API keys");
|
|
1556
|
-
out.result(answer, () => answer.keys.length === 0 ? "No API keys." : keyTable(answer.keys, keyHint(key)));
|
|
1557
|
-
});
|
|
1558
|
-
keys.command("revoke").description("Revoke an API key; its next request is refused").argument("<key_id>", "the key: an ID from chesly keys list").addHelpText(
|
|
1559
|
-
"after",
|
|
1560
|
-
`
|
|
1561
|
-
Prints { "id", "revoked": true, "in_use" }, or a sentence in a terminal.
|
|
1562
|
-
"in_use" says it was the key this CLI uses, which then needs chesly login.
|
|
1563
|
-
An unknown ID exits 4; a read-only key can't revoke.
|
|
1564
|
-
|
|
1565
|
-
Example:
|
|
1566
|
-
chesly keys revoke 0b5c9a52-6f1e-4c41-9a43-2b7f0d0e1a07`
|
|
1567
|
-
).action(async (keyId, _options, command) => {
|
|
1568
|
-
const out = new Output(io, command.optsWithGlobals());
|
|
1569
|
-
const { key, source } = await requireKey();
|
|
1570
|
-
const id = keyId.trim();
|
|
1571
|
-
const me = expectAnswer(await apiRequest("/v1/me", { key }), isMe, "the details of this key");
|
|
1572
|
-
await apiRequest(`/v1/keys/${encodeURIComponent(id)}`, { method: "DELETE", key });
|
|
1573
|
-
const inUse = me.key.id === id;
|
|
1574
|
-
out.result({ id, revoked: true, in_use: inUse }, () => {
|
|
1575
|
-
if (!inUse) return `Revoked ${id}.`;
|
|
1576
|
-
if (source === "env") {
|
|
1577
|
-
return `Revoked ${id}. This was the key this CLI uses, from CHESLY_API_KEY: put another key there, or unset it and run chesly login.`;
|
|
1578
|
-
}
|
|
1579
|
-
return `Revoked ${id}. This was the key this CLI uses; run chesly login.`;
|
|
1580
|
-
});
|
|
1602
|
+
async function uploadFile(file, key) {
|
|
1603
|
+
const form = new FormData();
|
|
1604
|
+
form.append("file", file.blob, file.name);
|
|
1605
|
+
const upload = await apiRequest("/v1/media", {
|
|
1606
|
+
key,
|
|
1607
|
+
form,
|
|
1608
|
+
timeoutMs: 3e4 + Math.ceil(file.size / SLOWEST_UPLOAD_BYTES_PER_SECOND) * 1e3
|
|
1581
1609
|
});
|
|
1610
|
+
if (!isMediaUpload(upload)) throw new CliError("invalid_response", "The API sent no media ID for the upload.");
|
|
1611
|
+
return upload;
|
|
1612
|
+
}
|
|
1613
|
+
function mimeTypeOf(path) {
|
|
1614
|
+
return MIME_TYPES[extname(path).slice(1).toLowerCase()] ?? "application/octet-stream";
|
|
1615
|
+
}
|
|
1616
|
+
function reasonOf2(error) {
|
|
1617
|
+
const { code, message } = error ?? {};
|
|
1618
|
+
if (code === "ENOENT") return "no such file";
|
|
1619
|
+
if (code === "EACCES" || code === "EPERM") return "permission denied";
|
|
1620
|
+
return typeof message === "string" && message !== "" ? message : "it failed";
|
|
1582
1621
|
}
|
|
1583
1622
|
|
|
1584
|
-
// src/commands/
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1623
|
+
// src/commands/send.ts
|
|
1624
|
+
var WAIT_MS = 3e4;
|
|
1625
|
+
var POLL_MS = 1e3;
|
|
1626
|
+
var MIN_POLL_MS = 1e3;
|
|
1627
|
+
function registerSend(program, io) {
|
|
1628
|
+
program.command("send").description("Send a message to one of your chats: text, a reply, or a file with a caption").argument("<chat>", "a chat ID (chat_\u2026) or a chat name").argument("[text]", "the text to send; - reads it from stdin").option("--reply-to <msg_id>", "reply to this message: a msg_ ID from chesly read").option("--file <path>", "attach this file (at most 50 MB); the text becomes its caption").option("--media-id <id>", "attach a file uploaded before (a media_ ID), as a retry of a file send does").option("--wait", "wait up to 30 s until the network has it: sent, delivered or failed").option(
|
|
1629
|
+
"--idempotency-key <key>",
|
|
1630
|
+
"send at most once per key, however often the command runs (default: a new key)",
|
|
1631
|
+
parseIdempotencyKey
|
|
1632
|
+
).option("--network <name>", "look the chat name up on this network: email for an email thread", parseNetwork).addHelpText(
|
|
1589
1633
|
"after",
|
|
1590
1634
|
`
|
|
1591
|
-
|
|
1592
|
-
and stops: each sign-in makes a key, and an account has 25 at most. --force
|
|
1593
|
-
signs in anyway, and revokes the stored keys the new one replaces (any other
|
|
1594
|
-
replaced key that still works is named in a key_still_active warning);
|
|
1595
|
-
chesly logout revokes the stored key.
|
|
1635
|
+
${CHAT_ARGUMENT_HELP}
|
|
1596
1636
|
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
--
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1637
|
+
Prints the message the API answered: its JSON, or "Sent to <chat> \xB7 <status>"
|
|
1638
|
+
in a terminal. The status is queued until the network takes the message;
|
|
1639
|
+
with --wait the command waits up to 30 s in all for sent, delivered or failed
|
|
1640
|
+
(it asks once a second, or later when a refused ask's Retry-After says so,
|
|
1641
|
+
and each ask counts as an API call). A failure the network confirms exits 1
|
|
1642
|
+
(delivery_failed) with the reason. One it didn't confirm reads "unconfirmed":
|
|
1643
|
+
the message may have been delivered, so check the chat before you send it
|
|
1644
|
+
again; --wait goes on asking, and if it stays unconfirmed the command exits 5
|
|
1645
|
+
(delivery_unconfirmed). Once the message went out it is always printed, and
|
|
1646
|
+
following it failing is only a warning on stderr (delivery_wait_failed).
|
|
1604
1647
|
|
|
1605
|
-
|
|
1606
|
-
|
|
1648
|
+
Every send carries an Idempotency-Key, and the CLI's own retries (after a
|
|
1649
|
+
network error, a 502, 503 or 504, a 429, or while a first attempt is still
|
|
1650
|
+
running) reuse it, so the message goes out at most once. Each waits for the
|
|
1651
|
+
answer's Retry-After, at most 10 s, or 1 s and then 2 s without one; a 429
|
|
1652
|
+
that lifts later (a send cap) exits 5 at once. To retry later without
|
|
1653
|
+
sending twice, pass the same --idempotency-key again. A file send that gives
|
|
1654
|
+
up also names its upload: retry it with --media-id <id> instead of --file, as
|
|
1655
|
+
a new upload would make another media ID, which the same key refuses (422).
|
|
1656
|
+
|
|
1657
|
+
Pass the text on stdin with a quoted heredoc (- <<'EOF'), not as a
|
|
1658
|
+
double-quoted argument: inside double quotes the shell still expands $ and
|
|
1659
|
+
backticks, while the quoted EOF keeps the text exactly as written.
|
|
1660
|
+
|
|
1661
|
+
Examples:
|
|
1662
|
+
chesly send "Mom" - --wait <<'EOF'
|
|
1663
|
+
On my way
|
|
1664
|
+
EOF
|
|
1665
|
+
chesly send "Invoice 42" - --network email <<'EOF'
|
|
1666
|
+
Paid, thank you.
|
|
1667
|
+
EOF
|
|
1668
|
+
chesly send chat_7Hq2mR9xLp4vT1sK8nWz - --file report.pdf <<'EOF'
|
|
1669
|
+
The report is attached.
|
|
1670
|
+
EOF`
|
|
1671
|
+
).action(async (chat, text, options, command) => {
|
|
1672
|
+
const out = new Output(io, command.optsWithGlobals());
|
|
1673
|
+
const body = await messageText2(text, io);
|
|
1674
|
+
if (options.file !== void 0 && options.mediaId !== void 0) {
|
|
1675
|
+
throw new CliError("usage_error", "A message carries one attachment: give --file or --media-id, not both.", EXIT.USAGE);
|
|
1676
|
+
}
|
|
1677
|
+
if (body === void 0 && options.file === void 0 && options.mediaId === void 0) {
|
|
1678
|
+
throw new CliError("usage_error", "Give the text to send, - to read it from stdin, or --file.", EXIT.USAGE);
|
|
1679
|
+
}
|
|
1680
|
+
const file = options.file === void 0 ? void 0 : await readUpload(options.file);
|
|
1681
|
+
const key = keyOnce();
|
|
1682
|
+
const target = await resolveChat(chat, key, { network: options.network });
|
|
1683
|
+
const idempotencyKey = options.idempotencyKey ?? randomUUID();
|
|
1684
|
+
const uploaded = file === void 0 ? void 0 : await uploadFile(file, await key());
|
|
1685
|
+
const mediaId = uploaded?.id ?? options.mediaId;
|
|
1686
|
+
const again = {
|
|
1687
|
+
flags: uploaded === void 0 ? `--idempotency-key ${idempotencyKey}` : `--media-id ${shellQuote(uploaded.id)} --idempotency-key ${idempotencyKey} (instead of --file)`,
|
|
1688
|
+
extras: { idempotency_key: idempotencyKey, ...mediaId === void 0 ? {} : { media_id: mediaId } }
|
|
1689
|
+
};
|
|
1690
|
+
const answer = await idempotentPost(`/v1/chats/${encodeURIComponent(target.id)}/messages`, {
|
|
1691
|
+
key: await key(),
|
|
1692
|
+
idempotencyKey,
|
|
1693
|
+
body: {
|
|
1694
|
+
...body === void 0 ? {} : { text: body },
|
|
1695
|
+
...options.replyTo === void 0 ? {} : { reply_to: options.replyTo },
|
|
1696
|
+
...mediaId === void 0 ? {} : { media_id: mediaId }
|
|
1697
|
+
},
|
|
1698
|
+
again
|
|
1699
|
+
});
|
|
1700
|
+
if (!isMessage(answer)) {
|
|
1701
|
+
throw new AnnotatedError(
|
|
1702
|
+
new CliError("invalid_response", "The API sent no message for the send."),
|
|
1703
|
+
again.extras,
|
|
1704
|
+
`It may have gone out. Run the command again with ${again.flags} to see the API's answer.`
|
|
1705
|
+
);
|
|
1706
|
+
}
|
|
1707
|
+
const waited = options.wait ? await waitForDelivery(answer, await key()) : { message: answer, ranOut: false, failure: void 0 };
|
|
1708
|
+
const { message } = waited;
|
|
1709
|
+
const state = deliveryState(message.delivery);
|
|
1710
|
+
out.result(message, () => sentLine(target, message));
|
|
1711
|
+
if (waited.failure !== void 0) {
|
|
1712
|
+
out.warn(
|
|
1713
|
+
"delivery_wait_failed",
|
|
1714
|
+
`The message was sent, but following its delivery failed (${waited.failure}). chesly read shows it, and the command run again with ${again.flags} shows the API's answer.`,
|
|
1715
|
+
again.extras
|
|
1716
|
+
);
|
|
1717
|
+
} else if (waited.ranOut && state === "queued") {
|
|
1718
|
+
out.note(`Still queued after ${WAIT_MS / 1e3} s.`);
|
|
1719
|
+
}
|
|
1720
|
+
const delivery = message.delivery;
|
|
1721
|
+
if (state === "failed") {
|
|
1722
|
+
throw new CliError("delivery_failed", delivery?.error?.message ?? "The network did not take the message.", EXIT.ERROR, {
|
|
1723
|
+
extras: { message_id: message.id, delivery }
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
if (state === "unconfirmed") {
|
|
1727
|
+
throw new CliError(
|
|
1728
|
+
"delivery_unconfirmed",
|
|
1729
|
+
"The network didn't confirm the message, so it may have been delivered. Check the chat before you send it again.",
|
|
1730
|
+
EXIT.RETRY_LATER,
|
|
1731
|
+
{ extras: { message_id: message.id, delivery } }
|
|
1732
|
+
);
|
|
1733
|
+
}
|
|
1734
|
+
});
|
|
1735
|
+
}
|
|
1736
|
+
async function messageText2(text, io) {
|
|
1737
|
+
if (text !== "-") return text === "" ? void 0 : text;
|
|
1738
|
+
if (io.stdin.isTTY) {
|
|
1739
|
+
throw new CliError(
|
|
1740
|
+
"usage_error",
|
|
1741
|
+
"The text - reads stdin, and stdin is a terminal. Pipe the text in, or give it as an argument.",
|
|
1742
|
+
EXIT.USAGE
|
|
1743
|
+
);
|
|
1744
|
+
}
|
|
1745
|
+
const piped = (await io.stdin.read()).replace(/(?:\r?\n)+$/, "");
|
|
1746
|
+
return piped === "" ? void 0 : piped;
|
|
1747
|
+
}
|
|
1748
|
+
async function waitForDelivery(sent, key) {
|
|
1749
|
+
let message = sent;
|
|
1750
|
+
let failure;
|
|
1751
|
+
let pauseMs = POLL_MS;
|
|
1752
|
+
const deadline = Date.now() + WAIT_MS;
|
|
1753
|
+
while (!settled(message)) {
|
|
1754
|
+
if (deadline - Date.now() < pauseMs + MIN_POLL_MS) return { message, ranOut: failure === void 0, failure };
|
|
1755
|
+
await sleep(pauseMs);
|
|
1756
|
+
pauseMs = POLL_MS;
|
|
1757
|
+
try {
|
|
1758
|
+
const timeoutMs = deadline - Date.now();
|
|
1759
|
+
const polled = await apiRequest(`/v1/messages/${encodeURIComponent(message.id)}`, { key, timeoutMs });
|
|
1760
|
+
if (!isMessage(polled)) return { message, ranOut: false, failure: "the API sent an answer that is not a message" };
|
|
1761
|
+
message = polled;
|
|
1762
|
+
failure = void 0;
|
|
1763
|
+
} catch (error) {
|
|
1764
|
+
if (error instanceof CheslyNetworkError && error.timedOut) return { message, ranOut: true, failure: void 0 };
|
|
1765
|
+
failure = pollFailure(error);
|
|
1766
|
+
const passing = isTransient(error) || error instanceof CheslyApiError && error.status === 429;
|
|
1767
|
+
if (!passing) return { message, ranOut: false, failure };
|
|
1768
|
+
if (error instanceof CheslyApiError && error.retryAfter !== void 0) {
|
|
1769
|
+
pauseMs = Math.max(POLL_MS, error.retryAfter * 1e3);
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
return { message, ranOut: false, failure: void 0 };
|
|
1774
|
+
}
|
|
1775
|
+
function pollFailure(error) {
|
|
1776
|
+
if (error instanceof CheslyApiError) return `${error.status} ${error.code}`;
|
|
1777
|
+
if (error instanceof CheslyNetworkError) return "no answer from the API";
|
|
1778
|
+
return "an unexpected error";
|
|
1779
|
+
}
|
|
1780
|
+
function settled(message) {
|
|
1781
|
+
const state = deliveryState(message.delivery);
|
|
1782
|
+
return state === void 0 || state === "sent" || state === "delivered" || state === "failed";
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
// src/commands/edit.ts
|
|
1786
|
+
function registerEdit(program, io) {
|
|
1787
|
+
program.command("edit").description("Change the text of a message you sent from Chesly, for everyone in the chat").argument("<message_id>", "the message: a msg_ ID from chesly send or chesly read", parseMessageId).argument("<text>", "the new text; - reads it from stdin").addHelpText(
|
|
1788
|
+
"after",
|
|
1789
|
+
`
|
|
1790
|
+
Changes the text of a message you sent from Chesly (its apps or the API),
|
|
1791
|
+
for everyone in the chat. Text messages only, within the time the network
|
|
1792
|
+
allows edits, such as 15 minutes on WhatsApp, 24 hours on Signal or 48
|
|
1793
|
+
hours on Telegram. A message you sent in the network's own app, an email,
|
|
1794
|
+
or one past the network's limit is refused with message_not_editable
|
|
1795
|
+
(exit 1), and the error says why. An unknown ID exits 4.
|
|
1796
|
+
|
|
1797
|
+
Prints the edited message: its JSON, or the message as chesly read shows it
|
|
1798
|
+
in a terminal. Editing to the text the message already has changes nothing,
|
|
1799
|
+
so the command is safe to run again.
|
|
1800
|
+
|
|
1801
|
+
Pass the text on stdin with a quoted heredoc (- <<'EOF'), not as a
|
|
1802
|
+
double-quoted argument: inside double quotes the shell still expands $ and
|
|
1803
|
+
backticks, while the quoted EOF keeps the text exactly as written.
|
|
1804
|
+
|
|
1805
|
+
Example:
|
|
1806
|
+
chesly edit msg_7Hq2mR9xLp4vT1sK8nWz.JGV3A - <<'EOF'
|
|
1807
|
+
On my way, 10 minutes
|
|
1808
|
+
EOF`
|
|
1809
|
+
).action(async (messageId, text, _options, command) => {
|
|
1810
|
+
const out = new Output(io, command.optsWithGlobals());
|
|
1811
|
+
const body = await messageText2(text, io);
|
|
1812
|
+
if (body === void 0) throw new CliError("usage_error", "Give the new text, or - to read it from stdin.", EXIT.USAGE);
|
|
1813
|
+
const { key } = await requireKey();
|
|
1814
|
+
const answer = await apiRequest(`/v1/messages/${encodeURIComponent(messageId)}`, {
|
|
1815
|
+
method: "PATCH",
|
|
1816
|
+
key,
|
|
1817
|
+
body: { text: body }
|
|
1818
|
+
});
|
|
1819
|
+
const message = expectAnswer(answer, isMessage, "a message");
|
|
1820
|
+
out.result(message, () => transcript([message]));
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
// src/commands/key.ts
|
|
1825
|
+
function registerKey(program, io) {
|
|
1826
|
+
program.command("key").description("Show the API key in use, masked; --reveal prints the whole key, for MCP clients").option("--reveal", "print the whole key and nothing else (as JSON only with an explicit --json)").addHelpText(
|
|
1827
|
+
"after",
|
|
1828
|
+
`
|
|
1829
|
+
Example: add the Chesly MCP server to Claude Code
|
|
1830
|
+
claude mcp add --transport http chesly https://mcp.chesly.app/mcp \\
|
|
1831
|
+
--header "Authorization: Bearer $(chesly key --reveal)"`
|
|
1832
|
+
).action(async (options, command) => {
|
|
1833
|
+
const globals = command.optsWithGlobals();
|
|
1834
|
+
const { key, source } = await requireKey();
|
|
1835
|
+
if (options.reveal && globals.json !== true) {
|
|
1836
|
+
io.out(`${key}
|
|
1837
|
+
`);
|
|
1838
|
+
return;
|
|
1839
|
+
}
|
|
1840
|
+
const out = new Output(io, globals);
|
|
1841
|
+
const data = options.reveal ? { key, source } : { ...keyHint(key), source };
|
|
1842
|
+
out.result(data, () => `${maskKey(key)} (from ${describeSource(source)})`);
|
|
1843
|
+
});
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
// src/commands/keys.ts
|
|
1847
|
+
var REVOKE_TIMEOUT_MS = 1e4;
|
|
1848
|
+
async function revokeKey(key, fallback) {
|
|
1849
|
+
const { prefix, last4 } = keyHint(key);
|
|
1850
|
+
let id;
|
|
1851
|
+
try {
|
|
1852
|
+
const me = await apiRequest("/v1/me", { key, timeoutMs: REVOKE_TIMEOUT_MS });
|
|
1853
|
+
if (!isMe(me)) return { id: null, prefix, last4, outcome: "active" };
|
|
1854
|
+
id = me.key.id;
|
|
1855
|
+
} catch (error) {
|
|
1856
|
+
const refused = error instanceof CheslyApiError && error.status === 401;
|
|
1857
|
+
return { id: null, prefix, last4, outcome: refused ? "gone" : "active" };
|
|
1858
|
+
}
|
|
1859
|
+
let outcome = await deleteKey(id, key, true);
|
|
1860
|
+
if (outcome === "forbidden" && fallback !== void 0) outcome = await deleteKey(id, fallback, false);
|
|
1861
|
+
return { id, prefix, last4, outcome: outcome === "forbidden" ? "active" : outcome };
|
|
1862
|
+
}
|
|
1863
|
+
async function deleteKey(id, using, self) {
|
|
1864
|
+
try {
|
|
1865
|
+
await apiRequest(`/v1/keys/${encodeURIComponent(id)}`, { method: "DELETE", key: using, timeoutMs: REVOKE_TIMEOUT_MS });
|
|
1866
|
+
return "revoked";
|
|
1867
|
+
} catch (error) {
|
|
1868
|
+
if (!(error instanceof CheslyApiError)) return "active";
|
|
1869
|
+
if (error.status === 403) return "forbidden";
|
|
1870
|
+
if (self && (error.status === 401 || error.status === 404 && error.code === "key_not_found")) return "gone";
|
|
1871
|
+
return "active";
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
async function checkKey(key) {
|
|
1875
|
+
const { prefix, last4 } = keyHint(key);
|
|
1876
|
+
try {
|
|
1877
|
+
const me = await apiRequest("/v1/me", { key, timeoutMs: REVOKE_TIMEOUT_MS });
|
|
1878
|
+
return { id: isMe(me) ? me.key.id : null, prefix, last4, outcome: "active" };
|
|
1879
|
+
} catch (error) {
|
|
1880
|
+
const refused = error instanceof CheslyApiError && error.status === 401;
|
|
1881
|
+
return { id: null, prefix, last4, outcome: refused ? "gone" : "active" };
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
function stillActiveMessage(revocation, which = "This key") {
|
|
1885
|
+
const id = revocation.id === null ? "" : `, id ${revocation.id}`;
|
|
1886
|
+
return `${which} (${revocation.prefix}\u2026${revocation.last4}${id}) is still active; revoke it in the dashboard at web.chesly.app/api/keys.`;
|
|
1887
|
+
}
|
|
1888
|
+
function registerKeys(program, io) {
|
|
1889
|
+
const keys = program.command("keys").description("List your API keys, or revoke one").addHelpText(
|
|
1890
|
+
"after",
|
|
1891
|
+
`
|
|
1892
|
+
There is no keys create. New keys come from chesly login, or from the dashboard at
|
|
1893
|
+
https://web.chesly.app/api/keys. chesly key shows the key this CLI uses.
|
|
1894
|
+
|
|
1895
|
+
Examples:
|
|
1896
|
+
chesly keys list
|
|
1897
|
+
chesly keys revoke 0b5c9a52-6f1e-4c41-9a43-2b7f0d0e1a07`
|
|
1898
|
+
);
|
|
1899
|
+
keys.command("list").description("List your active API keys, marking the one this CLI uses").addHelpText(
|
|
1900
|
+
"after",
|
|
1901
|
+
`
|
|
1902
|
+
Prints the API's { "keys": [...] }, or a table in a terminal. A key shows only
|
|
1903
|
+
its first 12 and last 4 characters, never the whole key.
|
|
1904
|
+
|
|
1905
|
+
Example:
|
|
1906
|
+
chesly keys list --json`
|
|
1907
|
+
).action(async (_options, command) => {
|
|
1908
|
+
const out = new Output(io, command.optsWithGlobals());
|
|
1909
|
+
const { key } = await requireKey();
|
|
1910
|
+
const answer = expectAnswer(await apiRequest("/v1/keys", { key }), isKeyList, "a list of API keys");
|
|
1911
|
+
out.result(answer, () => answer.keys.length === 0 ? "No API keys." : keyTable(answer.keys, keyHint(key)));
|
|
1912
|
+
});
|
|
1913
|
+
keys.command("revoke").description("Revoke an API key; its next request is refused").argument("<key_id>", "the key: an ID from chesly keys list").addHelpText(
|
|
1914
|
+
"after",
|
|
1915
|
+
`
|
|
1916
|
+
Prints { "id", "revoked": true, "in_use" }, or a sentence in a terminal.
|
|
1917
|
+
"in_use" says it was the key this CLI uses, which then needs chesly login.
|
|
1918
|
+
An unknown ID exits 4; a read-only key can't revoke.
|
|
1919
|
+
|
|
1920
|
+
Example:
|
|
1921
|
+
chesly keys revoke 0b5c9a52-6f1e-4c41-9a43-2b7f0d0e1a07`
|
|
1922
|
+
).action(async (keyId, _options, command) => {
|
|
1923
|
+
const out = new Output(io, command.optsWithGlobals());
|
|
1924
|
+
const { key, source } = await requireKey();
|
|
1925
|
+
const id = keyId.trim();
|
|
1926
|
+
const me = expectAnswer(await apiRequest("/v1/me", { key }), isMe, "the details of this key");
|
|
1927
|
+
await apiRequest(`/v1/keys/${encodeURIComponent(id)}`, { method: "DELETE", key });
|
|
1928
|
+
const inUse = me.key.id === id;
|
|
1929
|
+
out.result({ id, revoked: true, in_use: inUse }, () => {
|
|
1930
|
+
if (!inUse) return `Revoked ${id}.`;
|
|
1931
|
+
if (source === "env") {
|
|
1932
|
+
return `Revoked ${id}. This was the key this CLI uses, from CHESLY_API_KEY: put another key there, or unset it and run chesly login.`;
|
|
1933
|
+
}
|
|
1934
|
+
return `Revoked ${id}. This was the key this CLI uses; run chesly login.`;
|
|
1935
|
+
});
|
|
1936
|
+
});
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
// src/commands/login.ts
|
|
1940
|
+
import { spawn as spawn2 } from "child_process";
|
|
1941
|
+
import { hostname } from "os";
|
|
1942
|
+
function registerLogin(program, io) {
|
|
1943
|
+
program.command("login").description("Sign in: approve this computer on web.chesly.app, or store a key with --key").option("--key <key>", "check an API key with the API, then store it (no browser); - reads it from stdin").option("--no-browser", "print the link only, without opening a browser").option("--force", "sign in with a new key even when the stored one works (which is then revoked)").option("--wait", "finish the sign-in --no-wait started: wait for the approval, then store the key").option("--no-wait", "start a sign-in, print its link and code, and exit at once").addHelpText(
|
|
1944
|
+
"after",
|
|
1945
|
+
`
|
|
1946
|
+
When the key it stored still works, chesly login says "Already signed in"
|
|
1947
|
+
and stops: each sign-in makes a key, and an account has 25 at most. --force
|
|
1948
|
+
signs in anyway, and revokes the stored keys the new one replaces (any other
|
|
1949
|
+
replaced key that still works is named in a key_still_active warning);
|
|
1950
|
+
chesly logout revokes the stored key.
|
|
1951
|
+
|
|
1952
|
+
Without --key, chesly login prints a link and a code, opens your browser
|
|
1953
|
+
(wherever there is one to show: not over SSH, not in CI, not with
|
|
1954
|
+
--no-browser), and waits until you approve the code on web.chesly.app.
|
|
1955
|
+
It never reads input. In JSON mode the link and the code are one line on
|
|
1956
|
+
stderr, and the result is one object on stdout:
|
|
1957
|
+
{"verification_uri_complete":"\u2026","user_code":"ABCD-EFGH","expires_in":600}
|
|
1958
|
+
{"status":"signed_in","stored_in":"keychain","key":{\u2026}}
|
|
1959
|
+
|
|
1960
|
+
Agents: a tool that shows a command's output only once it ends can sign in in
|
|
1961
|
+
two steps. chesly login --no-wait prints the link and the code and exits:
|
|
1607
1962
|
{"status":"pending","verification_uri_complete":"\u2026","user_code":"\u2026","expires_in":600}
|
|
1608
1963
|
Show them to your person, then run chesly login --wait, which waits for the
|
|
1609
1964
|
approval and stores the key. A --wait cut short can run again until the code
|
|
@@ -1962,48 +2317,13 @@ Example:
|
|
|
1962
2317
|
return lines.join("\n");
|
|
1963
2318
|
});
|
|
1964
2319
|
if (out.json) {
|
|
1965
|
-
for (const revocation of stillActive) {
|
|
1966
|
-
const { id, prefix, last4 } = revocation;
|
|
1967
|
-
out.warn("key_still_active", stillActiveMessage(revocation), { id, prefix, last4 });
|
|
1968
|
-
}
|
|
1969
|
-
}
|
|
1970
|
-
});
|
|
1971
|
-
}
|
|
1972
|
-
|
|
1973
|
-
// src/resolve-chat.ts
|
|
1974
|
-
var LOOKUP_LIMIT = 10;
|
|
1975
|
-
var CHAT_ARGUMENT_HELP = `A <chat> is a chat ID (chat_\u2026) or a name. An exact name wins, in any case;
|
|
1976
|
-
so does the only partial match. No match, or several, exits 4, and JSON
|
|
1977
|
-
mode lists the chats a name could mean as "candidates". Names are looked up
|
|
1978
|
-
on every network but email; --network names the one to look in (an email
|
|
1979
|
-
thread's name is its subject: --network email).`;
|
|
1980
|
-
async function resolveChat(arg, key, options = {}) {
|
|
1981
|
-
const name = arg.trim();
|
|
1982
|
-
if (name.startsWith("chat_")) return { id: name, name: null };
|
|
1983
|
-
if (name === "") throw new CliError("usage_error", "Give a chat ID (chat_\u2026) or a chat name.", EXIT.USAGE);
|
|
1984
|
-
const answer = await apiRequest("/v1/chats", {
|
|
1985
|
-
key: await key(),
|
|
1986
|
-
query: { q: name, archived: "all", limit: LOOKUP_LIMIT, network: options.network, account_id: options.accountId }
|
|
1987
|
-
});
|
|
1988
|
-
const page = expectAnswer(answer, isChatPage, "a page of chats");
|
|
1989
|
-
const exact = page.chats.filter((chat) => sameName(chat.name, name));
|
|
1990
|
-
const candidates = exact.length > 0 ? exact : page.chats;
|
|
1991
|
-
const [first, ...others] = candidates;
|
|
1992
|
-
if (first === void 0) {
|
|
1993
|
-
throw new CliError("chat_not_found", `No chat matches "${name}".`, EXIT.NOT_FOUND, {
|
|
1994
|
-
hint: "Run `chesly chats` to list your chats, or pass a chat ID."
|
|
1995
|
-
});
|
|
1996
|
-
}
|
|
1997
|
-
if (others.length === 0) return { id: first.id, name: first.name ?? null };
|
|
1998
|
-
const count2 = `${page.next_cursor ? "at least " : ""}${candidates.length}`;
|
|
1999
|
-
throw new CliError("ambiguous_chat", `"${name}" matches ${count2} chats. Use one of their chat IDs instead.`, EXIT.NOT_FOUND, {
|
|
2000
|
-
extras: { candidates },
|
|
2001
|
-
hint: indent(chatTable(candidates), " ")
|
|
2320
|
+
for (const revocation of stillActive) {
|
|
2321
|
+
const { id, prefix, last4 } = revocation;
|
|
2322
|
+
out.warn("key_still_active", stillActiveMessage(revocation), { id, prefix, last4 });
|
|
2323
|
+
}
|
|
2324
|
+
}
|
|
2002
2325
|
});
|
|
2003
2326
|
}
|
|
2004
|
-
function sameName(chatName, name) {
|
|
2005
|
-
return (chatName ?? "").trim().localeCompare(name, void 0, { sensitivity: "accent" }) === 0;
|
|
2006
|
-
}
|
|
2007
2327
|
|
|
2008
2328
|
// src/commands/read.ts
|
|
2009
2329
|
function registerRead(program, io) {
|
|
@@ -2067,254 +2387,6 @@ Examples:
|
|
|
2067
2387
|
});
|
|
2068
2388
|
}
|
|
2069
2389
|
|
|
2070
|
-
// src/commands/send.ts
|
|
2071
|
-
import { randomUUID } from "crypto";
|
|
2072
|
-
|
|
2073
|
-
// src/upload.ts
|
|
2074
|
-
import { openAsBlob } from "fs";
|
|
2075
|
-
import { stat } from "fs/promises";
|
|
2076
|
-
import { basename, extname } from "path";
|
|
2077
|
-
var MAX_UPLOAD_BYTES = 5e7;
|
|
2078
|
-
var MIME_TYPES = {
|
|
2079
|
-
jpg: "image/jpeg",
|
|
2080
|
-
jpeg: "image/jpeg",
|
|
2081
|
-
png: "image/png",
|
|
2082
|
-
gif: "image/gif",
|
|
2083
|
-
webp: "image/webp",
|
|
2084
|
-
heic: "image/heic",
|
|
2085
|
-
heif: "image/heif",
|
|
2086
|
-
avif: "image/avif",
|
|
2087
|
-
bmp: "image/bmp",
|
|
2088
|
-
tif: "image/tiff",
|
|
2089
|
-
tiff: "image/tiff",
|
|
2090
|
-
svg: "image/svg+xml",
|
|
2091
|
-
mp4: "video/mp4",
|
|
2092
|
-
m4v: "video/x-m4v",
|
|
2093
|
-
mov: "video/quicktime",
|
|
2094
|
-
webm: "video/webm",
|
|
2095
|
-
mkv: "video/x-matroska",
|
|
2096
|
-
"3gp": "video/3gpp",
|
|
2097
|
-
mp3: "audio/mpeg",
|
|
2098
|
-
m4a: "audio/mp4",
|
|
2099
|
-
aac: "audio/aac",
|
|
2100
|
-
wav: "audio/wav",
|
|
2101
|
-
flac: "audio/flac",
|
|
2102
|
-
ogg: "audio/ogg",
|
|
2103
|
-
oga: "audio/ogg",
|
|
2104
|
-
opus: "audio/ogg",
|
|
2105
|
-
amr: "audio/amr",
|
|
2106
|
-
pdf: "application/pdf",
|
|
2107
|
-
txt: "text/plain",
|
|
2108
|
-
md: "text/markdown",
|
|
2109
|
-
csv: "text/csv",
|
|
2110
|
-
json: "application/json",
|
|
2111
|
-
vcf: "text/vcard",
|
|
2112
|
-
ics: "text/calendar",
|
|
2113
|
-
zip: "application/zip",
|
|
2114
|
-
doc: "application/msword",
|
|
2115
|
-
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
2116
|
-
xls: "application/vnd.ms-excel",
|
|
2117
|
-
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
2118
|
-
ppt: "application/vnd.ms-powerpoint",
|
|
2119
|
-
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
|
2120
|
-
};
|
|
2121
|
-
var SLOWEST_UPLOAD_BYTES_PER_SECOND = 131072;
|
|
2122
|
-
async function readUpload(path) {
|
|
2123
|
-
try {
|
|
2124
|
-
const info = await stat(path);
|
|
2125
|
-
if (!info.isFile()) throw new CliError("file_unreadable", `${path} is not a file.`, EXIT.USAGE);
|
|
2126
|
-
if (info.size > MAX_UPLOAD_BYTES) {
|
|
2127
|
-
throw new CliError("media_too_large", `${path} is over 50 MB, the most a message can carry.`);
|
|
2128
|
-
}
|
|
2129
|
-
return { blob: await openAsBlob(path, { type: mimeTypeOf(path) }), name: basename(path), size: info.size };
|
|
2130
|
-
} catch (error) {
|
|
2131
|
-
if (error instanceof CliError) throw error;
|
|
2132
|
-
throw new CliError("file_unreadable", `Can't read ${path}: ${reasonOf2(error)}`, EXIT.USAGE);
|
|
2133
|
-
}
|
|
2134
|
-
}
|
|
2135
|
-
async function uploadFile(file, key) {
|
|
2136
|
-
const form = new FormData();
|
|
2137
|
-
form.append("file", file.blob, file.name);
|
|
2138
|
-
const upload = await apiRequest("/v1/media", {
|
|
2139
|
-
key,
|
|
2140
|
-
form,
|
|
2141
|
-
timeoutMs: 3e4 + Math.ceil(file.size / SLOWEST_UPLOAD_BYTES_PER_SECOND) * 1e3
|
|
2142
|
-
});
|
|
2143
|
-
if (!isMediaUpload(upload)) throw new CliError("invalid_response", "The API sent no media ID for the upload.");
|
|
2144
|
-
return upload;
|
|
2145
|
-
}
|
|
2146
|
-
function mimeTypeOf(path) {
|
|
2147
|
-
return MIME_TYPES[extname(path).slice(1).toLowerCase()] ?? "application/octet-stream";
|
|
2148
|
-
}
|
|
2149
|
-
function reasonOf2(error) {
|
|
2150
|
-
const { code, message } = error ?? {};
|
|
2151
|
-
if (code === "ENOENT") return "no such file";
|
|
2152
|
-
if (code === "EACCES" || code === "EPERM") return "permission denied";
|
|
2153
|
-
return typeof message === "string" && message !== "" ? message : "it failed";
|
|
2154
|
-
}
|
|
2155
|
-
|
|
2156
|
-
// src/commands/send.ts
|
|
2157
|
-
var WAIT_MS = 3e4;
|
|
2158
|
-
var POLL_MS = 1e3;
|
|
2159
|
-
var MIN_POLL_MS = 1e3;
|
|
2160
|
-
function registerSend(program, io) {
|
|
2161
|
-
program.command("send").description("Send a message to one of your chats: text, a reply, or a file with a caption").argument("<chat>", "a chat ID (chat_\u2026) or a chat name").argument("[text]", "the text to send; - reads it from stdin").option("--reply-to <msg_id>", "reply to this message: a msg_ ID from chesly read").option("--file <path>", "attach this file (at most 50 MB); the text becomes its caption").option("--media-id <id>", "attach a file uploaded before (a media_ ID), as a retry of a file send does").option("--wait", "wait up to 30 s until the network has it: sent, delivered or failed").option(
|
|
2162
|
-
"--idempotency-key <key>",
|
|
2163
|
-
"send at most once per key, however often the command runs (default: a new key)",
|
|
2164
|
-
parseIdempotencyKey
|
|
2165
|
-
).option("--network <name>", "look the chat name up on this network: email for an email thread", parseNetwork).addHelpText(
|
|
2166
|
-
"after",
|
|
2167
|
-
`
|
|
2168
|
-
${CHAT_ARGUMENT_HELP}
|
|
2169
|
-
|
|
2170
|
-
Prints the message the API answered: its JSON, or "Sent to <chat> \xB7 <status>"
|
|
2171
|
-
in a terminal. The status is queued until the network takes the message;
|
|
2172
|
-
with --wait the command waits up to 30 s in all for sent, delivered or failed
|
|
2173
|
-
(it asks once a second, or later when a refused ask's Retry-After says so,
|
|
2174
|
-
and each ask counts as an API call). A failure the network confirms exits 1
|
|
2175
|
-
(delivery_failed) with the reason. One it didn't confirm reads "unconfirmed":
|
|
2176
|
-
the message may have been delivered, so check the chat before you send it
|
|
2177
|
-
again; --wait goes on asking, and if it stays unconfirmed the command exits 5
|
|
2178
|
-
(delivery_unconfirmed). Once the message went out it is always printed, and
|
|
2179
|
-
following it failing is only a warning on stderr (delivery_wait_failed).
|
|
2180
|
-
|
|
2181
|
-
Every send carries an Idempotency-Key, and the CLI's own retries (after a
|
|
2182
|
-
network error, a 502, 503 or 504, a 429, or while a first attempt is still
|
|
2183
|
-
running) reuse it, so the message goes out at most once. Each waits for the
|
|
2184
|
-
answer's Retry-After, at most 10 s, or 1 s and then 2 s without one; a 429
|
|
2185
|
-
that lifts later (a send cap) exits 5 at once. To retry later without
|
|
2186
|
-
sending twice, pass the same --idempotency-key again. A file send that gives
|
|
2187
|
-
up also names its upload: retry it with --media-id <id> instead of --file, as
|
|
2188
|
-
a new upload would make another media ID, which the same key refuses (422).
|
|
2189
|
-
|
|
2190
|
-
Pass the text on stdin with a quoted heredoc (- <<'EOF'), not as a
|
|
2191
|
-
double-quoted argument: inside double quotes the shell still expands $ and
|
|
2192
|
-
backticks, while the quoted EOF keeps the text exactly as written.
|
|
2193
|
-
|
|
2194
|
-
Examples:
|
|
2195
|
-
chesly send "Mom" - --wait <<'EOF'
|
|
2196
|
-
On my way
|
|
2197
|
-
EOF
|
|
2198
|
-
chesly send "Invoice 42" - --network email <<'EOF'
|
|
2199
|
-
Paid, thank you.
|
|
2200
|
-
EOF
|
|
2201
|
-
chesly send chat_7Hq2mR9xLp4vT1sK8nWz - --file report.pdf <<'EOF'
|
|
2202
|
-
The report is attached.
|
|
2203
|
-
EOF`
|
|
2204
|
-
).action(async (chat, text, options, command) => {
|
|
2205
|
-
const out = new Output(io, command.optsWithGlobals());
|
|
2206
|
-
const body = await messageText2(text, io);
|
|
2207
|
-
if (options.file !== void 0 && options.mediaId !== void 0) {
|
|
2208
|
-
throw new CliError("usage_error", "A message carries one attachment: give --file or --media-id, not both.", EXIT.USAGE);
|
|
2209
|
-
}
|
|
2210
|
-
if (body === void 0 && options.file === void 0 && options.mediaId === void 0) {
|
|
2211
|
-
throw new CliError("usage_error", "Give the text to send, - to read it from stdin, or --file.", EXIT.USAGE);
|
|
2212
|
-
}
|
|
2213
|
-
const file = options.file === void 0 ? void 0 : await readUpload(options.file);
|
|
2214
|
-
const key = keyOnce();
|
|
2215
|
-
const target = await resolveChat(chat, key, { network: options.network });
|
|
2216
|
-
const idempotencyKey = options.idempotencyKey ?? randomUUID();
|
|
2217
|
-
const uploaded = file === void 0 ? void 0 : await uploadFile(file, await key());
|
|
2218
|
-
const mediaId = uploaded?.id ?? options.mediaId;
|
|
2219
|
-
const again = {
|
|
2220
|
-
flags: uploaded === void 0 ? `--idempotency-key ${idempotencyKey}` : `--media-id ${shellQuote(uploaded.id)} --idempotency-key ${idempotencyKey} (instead of --file)`,
|
|
2221
|
-
extras: { idempotency_key: idempotencyKey, ...mediaId === void 0 ? {} : { media_id: mediaId } }
|
|
2222
|
-
};
|
|
2223
|
-
const answer = await idempotentPost(`/v1/chats/${encodeURIComponent(target.id)}/messages`, {
|
|
2224
|
-
key: await key(),
|
|
2225
|
-
idempotencyKey,
|
|
2226
|
-
body: {
|
|
2227
|
-
...body === void 0 ? {} : { text: body },
|
|
2228
|
-
...options.replyTo === void 0 ? {} : { reply_to: options.replyTo },
|
|
2229
|
-
...mediaId === void 0 ? {} : { media_id: mediaId }
|
|
2230
|
-
},
|
|
2231
|
-
again
|
|
2232
|
-
});
|
|
2233
|
-
if (!isMessage(answer)) {
|
|
2234
|
-
throw new AnnotatedError(
|
|
2235
|
-
new CliError("invalid_response", "The API sent no message for the send."),
|
|
2236
|
-
again.extras,
|
|
2237
|
-
`It may have gone out. Run the command again with ${again.flags} to see the API's answer.`
|
|
2238
|
-
);
|
|
2239
|
-
}
|
|
2240
|
-
const waited = options.wait ? await waitForDelivery(answer, await key()) : { message: answer, ranOut: false, failure: void 0 };
|
|
2241
|
-
const { message } = waited;
|
|
2242
|
-
const state = deliveryState(message.delivery);
|
|
2243
|
-
out.result(message, () => sentLine(target, message));
|
|
2244
|
-
if (waited.failure !== void 0) {
|
|
2245
|
-
out.warn(
|
|
2246
|
-
"delivery_wait_failed",
|
|
2247
|
-
`The message was sent, but following its delivery failed (${waited.failure}). chesly read shows it, and the command run again with ${again.flags} shows the API's answer.`,
|
|
2248
|
-
again.extras
|
|
2249
|
-
);
|
|
2250
|
-
} else if (waited.ranOut && state === "queued") {
|
|
2251
|
-
out.note(`Still queued after ${WAIT_MS / 1e3} s.`);
|
|
2252
|
-
}
|
|
2253
|
-
const delivery = message.delivery;
|
|
2254
|
-
if (state === "failed") {
|
|
2255
|
-
throw new CliError("delivery_failed", delivery?.error?.message ?? "The network did not take the message.", EXIT.ERROR, {
|
|
2256
|
-
extras: { message_id: message.id, delivery }
|
|
2257
|
-
});
|
|
2258
|
-
}
|
|
2259
|
-
if (state === "unconfirmed") {
|
|
2260
|
-
throw new CliError(
|
|
2261
|
-
"delivery_unconfirmed",
|
|
2262
|
-
"The network didn't confirm the message, so it may have been delivered. Check the chat before you send it again.",
|
|
2263
|
-
EXIT.RETRY_LATER,
|
|
2264
|
-
{ extras: { message_id: message.id, delivery } }
|
|
2265
|
-
);
|
|
2266
|
-
}
|
|
2267
|
-
});
|
|
2268
|
-
}
|
|
2269
|
-
async function messageText2(text, io) {
|
|
2270
|
-
if (text !== "-") return text === "" ? void 0 : text;
|
|
2271
|
-
if (io.stdin.isTTY) {
|
|
2272
|
-
throw new CliError(
|
|
2273
|
-
"usage_error",
|
|
2274
|
-
"The text - reads stdin, and stdin is a terminal. Pipe the text in, or give it as an argument.",
|
|
2275
|
-
EXIT.USAGE
|
|
2276
|
-
);
|
|
2277
|
-
}
|
|
2278
|
-
const piped = (await io.stdin.read()).replace(/(?:\r?\n)+$/, "");
|
|
2279
|
-
return piped === "" ? void 0 : piped;
|
|
2280
|
-
}
|
|
2281
|
-
async function waitForDelivery(sent, key) {
|
|
2282
|
-
let message = sent;
|
|
2283
|
-
let failure;
|
|
2284
|
-
let pauseMs = POLL_MS;
|
|
2285
|
-
const deadline = Date.now() + WAIT_MS;
|
|
2286
|
-
while (!settled(message)) {
|
|
2287
|
-
if (deadline - Date.now() < pauseMs + MIN_POLL_MS) return { message, ranOut: failure === void 0, failure };
|
|
2288
|
-
await sleep(pauseMs);
|
|
2289
|
-
pauseMs = POLL_MS;
|
|
2290
|
-
try {
|
|
2291
|
-
const timeoutMs = deadline - Date.now();
|
|
2292
|
-
const polled = await apiRequest(`/v1/messages/${encodeURIComponent(message.id)}`, { key, timeoutMs });
|
|
2293
|
-
if (!isMessage(polled)) return { message, ranOut: false, failure: "the API sent an answer that is not a message" };
|
|
2294
|
-
message = polled;
|
|
2295
|
-
failure = void 0;
|
|
2296
|
-
} catch (error) {
|
|
2297
|
-
if (error instanceof CheslyNetworkError && error.timedOut) return { message, ranOut: true, failure: void 0 };
|
|
2298
|
-
failure = pollFailure(error);
|
|
2299
|
-
const passing = isTransient(error) || error instanceof CheslyApiError && error.status === 429;
|
|
2300
|
-
if (!passing) return { message, ranOut: false, failure };
|
|
2301
|
-
if (error instanceof CheslyApiError && error.retryAfter !== void 0) {
|
|
2302
|
-
pauseMs = Math.max(POLL_MS, error.retryAfter * 1e3);
|
|
2303
|
-
}
|
|
2304
|
-
}
|
|
2305
|
-
}
|
|
2306
|
-
return { message, ranOut: false, failure: void 0 };
|
|
2307
|
-
}
|
|
2308
|
-
function pollFailure(error) {
|
|
2309
|
-
if (error instanceof CheslyApiError) return `${error.status} ${error.code}`;
|
|
2310
|
-
if (error instanceof CheslyNetworkError) return "no answer from the API";
|
|
2311
|
-
return "an unexpected error";
|
|
2312
|
-
}
|
|
2313
|
-
function settled(message) {
|
|
2314
|
-
const state = deliveryState(message.delivery);
|
|
2315
|
-
return state === void 0 || state === "sent" || state === "delivered" || state === "failed";
|
|
2316
|
-
}
|
|
2317
|
-
|
|
2318
2390
|
// src/commands/usage.ts
|
|
2319
2391
|
function registerUsage(program, io) {
|
|
2320
2392
|
program.command("usage").description("Your API plan: this period's calls and minutes, its limits, and send caps").addHelpText(
|
|
@@ -2823,7 +2895,7 @@ Exit codes:
|
|
|
2823
2895
|
running (409), or a send the network didn't confirm (check the chat
|
|
2824
2896
|
before sending again); retry_after, when there is one, says when
|
|
2825
2897
|
6 payment required: an API plan is needed, or this period's transcription
|
|
2826
|
-
minutes are used up
|
|
2898
|
+
minutes are used up (Starter has none)
|
|
2827
2899
|
|
|
2828
2900
|
Examples:
|
|
2829
2901
|
chesly catchup your unread chats and their latest messages
|
|
@@ -2858,6 +2930,8 @@ function buildProgram(io, json) {
|
|
|
2858
2930
|
registerSearch(program, io);
|
|
2859
2931
|
registerCatchup(program, io);
|
|
2860
2932
|
registerSend(program, io);
|
|
2933
|
+
registerEdit(program, io);
|
|
2934
|
+
registerDelete(program, io);
|
|
2861
2935
|
registerWatch(program, io);
|
|
2862
2936
|
registerWebhooks(program, io);
|
|
2863
2937
|
registerKeys(program, io);
|