@flowmails/flowmails-sdk 0.2.1 → 2.0.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/dist/index.d.cts CHANGED
@@ -77,6 +77,29 @@ interface CreateWaitOptions {
77
77
  */
78
78
  timeoutMs?: number;
79
79
  }
80
+ /**
81
+ * How a wait's resolution is delivered besides the poll.
82
+ *
83
+ * The wait's resolution is also delivered as the `wait.resolved`
84
+ * webhook event, on the account's verified outbound webhook — signed,
85
+ * retried and logged. An agent with **no long-running process** (a
86
+ * serverless function, a CI job, a webhook-driven service) therefore
87
+ * does not have to write a polling loop to learn that a mail landed.
88
+ * Polling the wait keeps working either way.
89
+ *
90
+ * ## Why the name is a literal here
91
+ *
92
+ * The SDK is **dependency-free** by design — it is published to npm
93
+ * and the workspace package that holds the contract is private, so
94
+ * this package cannot import the constant the contract file holds.
95
+ *
96
+ * The two are therefore kept in step by a **test**, not by an import:
97
+ * `test/web-agent-sdk-cli-mcp-push-vocabulary.test.mjs` asserts that
98
+ * this string, the CLI's copy and the MCP tool descriptions are all
99
+ * the same value as the shared contract's `AGENT_WAIT_PUSH_EVENT`. A
100
+ * rename is a failing test, not a drift.
101
+ */
102
+ declare const WAIT_RESOLVED_EVENT = "wait.resolved";
80
103
  /** A durable wait, as the platform reports it. */
81
104
  interface WaitStatus {
82
105
  waitId: string;
@@ -89,6 +112,17 @@ interface WaitStatus {
89
112
  resolvedMessageId: string | null;
90
113
  /** ISO-8601 deadline. A wait past it reads as `expired`. */
91
114
  expiresAt: string | number;
115
+ /**
116
+ * How this wait's resolution is also delivered.
117
+ *
118
+ * The event name is the same one the CLI and the MCP tool
119
+ * descriptions name — a client that reads one of them reads the
120
+ * same fact as a client that reads another.
121
+ */
122
+ push?: {
123
+ /** The webhook event this wait's resolution is delivered as. */
124
+ event: typeof WAIT_RESOLVED_EVENT;
125
+ };
92
126
  }
93
127
  /**
94
128
  * A newly-opened wait.
@@ -142,7 +176,10 @@ interface AgentDomain {
142
176
  name: string;
143
177
  routingMode: "catchall" | "rules";
144
178
  localParts: string[];
145
- /** False when the per-account receive worker is not deployed yet. */
179
+ /**
180
+ * False when the domain's route table is not bound in the mail
181
+ * worker yet — its mail is not being accepted.
182
+ */
146
183
  ready: boolean;
147
184
  }
148
185
  /** Result of {@link AgentAddresses.create}. */
@@ -192,6 +229,57 @@ interface ThreadDetail {
192
229
  /** The chain, oldest first. */
193
230
  messages: ThreadMessage[];
194
231
  }
232
+ /**
233
+ * The agent's own record of what it did with one message.
234
+ *
235
+ * The event log says a mail arrived; this says whether the agent
236
+ * already handled it. An agent that restarts reads it back to tell
237
+ * "I never saw this" from "I saw it and never finished".
238
+ */
239
+ interface MessageState {
240
+ /** The SMTP `Message-ID` — the id the event log carried. */
241
+ messageId: string;
242
+ /** The platform domain id whose mailbox received the message. */
243
+ domainId: string;
244
+ /**
245
+ * `pending` → `processing` → `done` | `failed`.
246
+ *
247
+ * The platform records what the agent wrote and never moves a row
248
+ * on its own — there is no rule engine and no sweeper behind it.
249
+ */
250
+ state: "pending" | "processing" | "done" | "failed";
251
+ /** Short labels the agent attached to the message. */
252
+ labels: string[];
253
+ /** A free-text note about what the agent did. */
254
+ note: string | null;
255
+ /** Who holds the claim, when the message is `processing`. */
256
+ claimedBy: string | null;
257
+ /** ISO-8601 timestamp of the claim. */
258
+ claimedAt: string | number | null;
259
+ /** ISO-8601 timestamp of the last write. */
260
+ updatedAt: string | number;
261
+ }
262
+ /** Options for {@link AgentMessages.claim}. */
263
+ interface ClaimMessageOptions {
264
+ /** The platform domain id whose mailbox received the message. */
265
+ domainId: string;
266
+ /**
267
+ * Which worker of yours holds the claim, e.g. `worker-1`.
268
+ * Defaults to the credential.
269
+ */
270
+ claimedBy?: string;
271
+ }
272
+ /** Options for {@link AgentMessages.complete}. */
273
+ interface CompleteMessageOptions {
274
+ /** The platform domain id whose mailbox received the message. */
275
+ domainId: string;
276
+ /** `done` when finished with it, `failed` when it did not succeed. */
277
+ state: "done" | "failed";
278
+ /** Short labels to attach, e.g. `["invoice"]`. */
279
+ labels?: string[];
280
+ /** A short free-text note about what the agent did. */
281
+ note?: string;
282
+ }
195
283
  /**
196
284
  * One search hit.
197
285
  *
@@ -282,6 +370,23 @@ interface SendOptions {
282
370
  html?: string;
283
371
  /** Attachments. */
284
372
  attachments?: SendAttachment[];
373
+ /**
374
+ * Send **one message per recipient** — the default — or one shared
375
+ * message for everybody.
376
+ *
377
+ * `true` (the default): every recipient gets their own message. They
378
+ * cannot see each other, one refused address does not hold up the
379
+ * others, and {@link SendResult.recipients} reports what happened to
380
+ * **each** address. `false`: everybody is on one envelope — one
381
+ * message, one relay call, one bill from a provider that charges per
382
+ * message — and then a single refused address refuses the whole
383
+ * send, because there was only ever one send.
384
+ *
385
+ * The default is per-recipient because a partial failure has to be
386
+ * observable. With one shared envelope the answer is all-or-nothing
387
+ * by construction.
388
+ */
389
+ perRecipient?: boolean;
285
390
  /**
286
391
  * Idempotency key — the same key under the same account and the
287
392
  * same request returns the **original** result and does not send
@@ -307,6 +412,14 @@ interface SendOptions {
307
412
  * it to {@link AgentMessages.get} to read a reply, or to
308
413
  * {@link AgentMessages.status} to poll delivery.
309
414
  *
415
+ * ## `status` is the send's verdict
416
+ *
417
+ * `status` is the **aggregate** over every recipient:
418
+ *
419
+ * - `"sent"` — everybody was accepted;
420
+ * - `"partial"` — some were accepted and some were refused;
421
+ * - `"failed"` — nobody was accepted.
422
+ *
310
423
  * ## A send that failed
311
424
  *
312
425
  * The backend answers `201` for a send the relay **refused** — the
@@ -317,9 +430,11 @@ interface SendOptions {
317
430
  *
318
431
  * Two fields now say what happened:
319
432
  *
320
- * - `error` — the typed slug (`"send_failed"`), or `null` when the
321
- * send did not fail. **It is never `null` on a failed send**, so
322
- * "failed with no reason" is not a state the wire can be in.
433
+ * - `error` — the typed slug (`"send_failed"`), or `null` when every
434
+ * recipient was accepted. **It is never `null` on a failed or a
435
+ * partial send**, so "failed with no reason" is not a state the
436
+ * wire can be in, and "check `result.error` before treating a 201
437
+ * as sent" stays true.
323
438
  * - `sendFeedback` — the relay's own reason, when it gave one
324
439
  * (`null` when it did not).
325
440
  *
@@ -329,7 +444,7 @@ interface SendOptions {
329
444
  interface SendResult {
330
445
  /** Numeric email row id (stringified for JSON safety). */
331
446
  id: string;
332
- /** The row's lifecycle state at send time. */
447
+ /** The send's aggregate state at send time. */
333
448
  status: string;
334
449
  /**
335
450
  * The SMTP `Message-ID` the relay assigned (the receive worker's
@@ -343,13 +458,46 @@ interface SendResult {
343
458
  */
344
459
  sendFeedback?: string | null;
345
460
  /**
346
- * The typed failure slug (`"send_failed"`), or `null` when the
347
- * send did not fail.
461
+ * The typed failure slug (`"send_failed"`), or `null` when the send
462
+ * did not fail.
348
463
  *
349
464
  * A failed send always carries one: `status: "failed"` with
350
- * `error: null` is not a state the backend can answer.
465
+ * `error: null` is not a state the backend can answer. A **partial**
466
+ * send carries it too — `status: "partial"` says *some* recipients
467
+ * were refused, and `error` says "not all of them got through".
351
468
  */
352
469
  error?: string | null;
470
+ /**
471
+ * One entry per recipient — what happened to **each** address.
472
+ *
473
+ * This is the per-recipient detail a multi-recipient send needs:
474
+ * "the first two went out and the third was refused because X" is a
475
+ * fact the audit log must not flatten, and the answer must not
476
+ * flatten it either.
477
+ *
478
+ * Absent on a body written by an older backend, which had no such
479
+ * field. On those, `status` is the whole answer.
480
+ */
481
+ recipients?: SendRecipientResult[];
482
+ }
483
+ /**
484
+ * One recipient's outcome inside a send.
485
+ *
486
+ * In per-recipient mode each address is its own message, its own row
487
+ * and its own relay call — so this entry is that message: its id, its
488
+ * verdict, and the relay's reason when the relay refused it.
489
+ */
490
+ interface SendRecipientResult {
491
+ /** The address this entry is about. */
492
+ to: string;
493
+ /** The platform's row id for the message that carried it. */
494
+ id: string;
495
+ /** Whether the relay accepted this recipient's message. */
496
+ status: "sent" | "failed";
497
+ /** The SMTP `Message-ID`, when the relay assigned one. */
498
+ messageId?: string;
499
+ /** The relay's reason, when the relay gave one. */
500
+ sendFeedback?: string | null;
353
501
  }
354
502
  /**
355
503
  * The outbound half of a domain — what the platform last recorded
@@ -364,16 +512,26 @@ interface AccountSendingState {
364
512
  /**
365
513
  * True only when the domain can send right now.
366
514
  *
367
- * Read it **with** `state`: `canSend: false` with
368
- * `state: "unknown"` means "we have not looked", while
369
- * `canSend: false` with `state: "not_onboarded"` means the domain
370
- * is not on Cloudflare Email Sending and no send will succeed.
515
+ * Read it **with** `configured` and `state`: `configured: false`
516
+ * means sending was never set up (it is optional), while a
517
+ * configured domain can still be temporarily `canSend: false` with
518
+ * `state: "setup_required"`, `"misconfigured"`, etc.
371
519
  */
372
520
  canSend: boolean;
373
521
  /**
374
- * The outbound verdict, in the platform's vocabulary: `ready`,
375
- * `not_onboarded`, `disabled`, `unconfigured`, `unlocked`,
376
- * `misconfigured`, `no_credential`, `unknown` or `error`.
522
+ * Whether an outbound provider is configured for the bound domain.
523
+ *
524
+ * Sending is optional. `false` means sending was never set up — a
525
+ * different answer from "configured but currently broken", which also
526
+ * reads `canSend: false`. Enable it from the domain page in the
527
+ * dashboard.
528
+ */
529
+ configured: boolean;
530
+ /**
531
+ * The outbound verdict, in the platform's neutral vocabulary:
532
+ * `ready`, `setup_required`, `unverified`, `misconfigured`,
533
+ * `disabled`, `no_credential`, `credential_rejected`, `unknown` or
534
+ * `error`.
377
535
  */
378
536
  state: string;
379
537
  /** The detail the platform recorded with the verdict. */
@@ -403,8 +561,6 @@ interface AccountInfo {
403
561
  * sending. `null` when the credential carries no binding.
404
562
  */
405
563
  domain: string | null;
406
- /** The per-account receive worker the credential sends through. */
407
- workerHost: string | null;
408
564
  /**
409
565
  * Which credential type answered: `"sdk_key"` (the dashboard's
410
566
  * domain-bound API key) or `"agent_credential"` (a scoped
@@ -425,25 +581,22 @@ interface AccountInfo {
425
581
  sending: AccountSendingState | null;
426
582
  }
427
583
  /**
428
- * The account's receive worker, and the version of it.
584
+ * The shared mail worker, and the version of it.
429
585
  *
430
- * The receive worker lives in the customer's own Cloudflare account
431
- * and does the actual receiving and sending; it is versioned, and an
432
- * older one silently does less than the platform expects (no route
433
- * tables before `1.4.0`, no Message-ID lookup before `1.5.1`, no event
434
- * reporting before `1.6.0`). An agent that cannot see the version
435
- * cannot tell why its events are empty.
586
+ * The mail worker is **Flowmails' own** deployment — one shared
587
+ * worker serves every tenant, isolated by the domain id on every row.
588
+ * It is versioned, and an older one silently does less than the
589
+ * platform expects (no route tables before `1.4.0`, no Message-ID
590
+ * lookup before `1.5.1`, no event reporting before `1.6.0`). An agent
591
+ * that cannot see the version cannot tell why its events are empty.
436
592
  */
437
593
  interface WorkerVersionInfo {
438
594
  /**
439
- * The version the platform last deployed for this account — `null`
440
- * when it has no record (a worker deployed outside the platform
441
- * leaves it stale, so read {@link lastDeployAt} with it).
595
+ * The version the platform last deployed — `null` when it has no
596
+ * record (read {@link lastDeployAt} with it).
442
597
  */
443
598
  version: string | null;
444
- /** The worker host this account's mail flows through. */
445
- workerHost: string | null;
446
- /** Whether the worker finished its own bootstrap (schema migrations). */
599
+ /** Whether the shared mail worker answered (its schema is migrated before deploy). */
447
600
  bootstrapped: boolean;
448
601
  /** When the platform last deployed this worker (ISO-8601), or `null`. */
449
602
  lastDeployAt: string | null;
@@ -457,13 +610,13 @@ interface FlowmailsOptions {
457
610
  baseURL?: string;
458
611
  /**
459
612
  * The **platform** origin, used by the listen half of the surface
460
- * (`fm.events`, `fm.messages`, `fm.waits`, `fm.addresses`). The
461
- * send path goes to {@link baseURL} (the SDK backend); the event
462
- * log, the durable wait and the message read live on the platform
463
- * worker, which is a different deployment.
613
+ * (`fm.events`, `fm.messages`, `fm.waits`, `fm.addresses`).
464
614
  *
465
- * Defaults to `https://flowmails.net`. Set it for a self-hosted or
466
- * staging deployment — the same API key authenticates both.
615
+ * @deprecated The whole surface is served by the SDK backend now —
616
+ * the agent platform moved there — so this defaults to the same
617
+ * origin as {@link baseURL}. It is kept so a client that set it
618
+ * keeps working against a private or staging deployment that
619
+ * splits the two. A client that does not set it gets one host.
467
620
  */
468
621
  platformURL?: string;
469
622
  /** Inject a `fetch` implementation (Workers environments, test harnesses). */
@@ -514,6 +667,20 @@ declare class Flowmails {
514
667
  * reason: `error` is the typed slug and `sendFeedback` is the
515
668
  * relay's own words. Check `result.error` before treating a `201`
516
669
  * as "sent".
670
+ *
671
+ * **A partial send is not a success either.** Several recipients
672
+ * each get their own message, and the relay can refuse some of them.
673
+ * `status` is then `"partial"` — `error` is still set, and
674
+ * `result.recipients` says exactly which addresses were refused and
675
+ * why:
676
+ *
677
+ * ```ts
678
+ * const result = await fm.send({ from, to: ["a@x.test", "b@x.test"], subject, text });
679
+ * if (result.status === "partial") {
680
+ * const refused = result.recipients?.filter((r) => r.status === "failed") ?? [];
681
+ * // retry only these
682
+ * }
683
+ * ```
517
684
  */
518
685
  send(opts: SendOptions): Promise<SendResult>;
519
686
  /**
@@ -533,7 +700,8 @@ declare class Flowmails {
533
700
  * ```ts
534
701
  * const me = await fm.me();
535
702
  * if (me.sending && !me.sending.canSend) {
536
- * // me.sending.state says why: "not_onboarded", "unknown", …
703
+ * // me.sending.configured: false means sending was never set up;
704
+ * // otherwise me.sending.state says why: "setup_required", …
537
705
  * }
538
706
  * ```
539
707
  *
@@ -542,10 +710,11 @@ declare class Flowmails {
542
710
  */
543
711
  me(): Promise<AccountInfo>;
544
712
  /**
545
- * The account's receive worker, and the version of it.
713
+ * The shared mail worker, and the version of it.
546
714
  *
547
- * The receive worker does the actual receiving and sending, and it
548
- * is versioned: `1.4.0` brought the route tables, `1.5.1` the
715
+ * The mail worker does the actual receiving and sending — it is
716
+ * Flowmails' own deployment, one shared worker for every tenant —
717
+ * and it is versioned: `1.4.0` brought the route tables, `1.5.1` the
549
718
  * Message-ID lookup, `1.6.0` the event reporting that fills the
550
719
  * log this SDK reads. An agent whose worker is older than the
551
720
  * platform expects sees an empty event log and has no way to tell
@@ -646,6 +815,28 @@ interface AgentMessages {
646
815
  }): Promise<MessageDetail>;
647
816
  /** Poll a sent message's delivery state by row id or `Message-ID`. */
648
817
  status(id: string): Promise<SendStatus>;
818
+ /**
819
+ * `claim_message`: take a message for processing.
820
+ *
821
+ * The platform records that this credential is working on the
822
+ * message now — a **mutex**, so two workers of the same account do
823
+ * not process the same message at once. It does not judge the mail
824
+ * and does not act on it.
825
+ *
826
+ * @throws {ValidationError} when `messageId` or `domainId` is
827
+ * missing.
828
+ */
829
+ claim(messageId: string, opts: ClaimMessageOptions): Promise<MessageState>;
830
+ /**
831
+ * `complete_message`: record the outcome (`done` or `failed`).
832
+ *
833
+ * The platform stores what the agent said and takes no further
834
+ * action on it.
835
+ *
836
+ * @throws {ValidationError} when `messageId`, `domainId` or
837
+ * `state` is missing, or `state` is neither `done` nor `failed`.
838
+ */
839
+ complete(messageId: string, opts: CompleteMessageOptions): Promise<MessageState>;
649
840
  /**
650
841
  * `search_emails`: look **back** through the mailbox.
651
842
  *
@@ -825,4 +1016,4 @@ declare class UpstreamError extends FlowmailsError {
825
1016
  constructor(code: string, upstreamMessage: string | undefined, status?: number);
826
1017
  }
827
1018
 
828
- export { type AccountInfo, type AccountSendingState, type AgentAddress, type AgentAddresses, type AgentDomain, type AgentEvent, type AgentEvents, type AgentMessages, type AgentThreads, type AgentWaits, AuthenticationError, type CreateAddressResult, type CreateWaitOptions, type CreateWaitResult, Flowmails, FlowmailsError, type FlowmailsOptions, type ListEventsOptions, type ListEventsResult, type MessageDetail, RateLimitError, type SearchMessageHit, type SearchMessagesOptions, type SearchMessagesResult, type SendAttachment, type SendOptions, type SendResult, type SendStatus, type ThreadDetail, type ThreadMessage, UpstreamError, ValidationError, type WaitStatus, type WorkerVersionInfo };
1019
+ export { type AccountInfo, type AccountSendingState, type AgentAddress, type AgentAddresses, type AgentDomain, type AgentEvent, type AgentEvents, type AgentMessages, type AgentThreads, type AgentWaits, AuthenticationError, type ClaimMessageOptions, type CompleteMessageOptions, type CreateAddressResult, type CreateWaitOptions, type CreateWaitResult, Flowmails, FlowmailsError, type FlowmailsOptions, type ListEventsOptions, type ListEventsResult, type MessageDetail, type MessageState, RateLimitError, type SearchMessageHit, type SearchMessagesOptions, type SearchMessagesResult, type SendAttachment, type SendOptions, type SendRecipientResult, type SendResult, type SendStatus, type ThreadDetail, type ThreadMessage, UpstreamError, ValidationError, WAIT_RESOLVED_EVENT, type WaitStatus, type WorkerVersionInfo };