@flowmails/flowmails-sdk 2.0.0 → 2.1.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/CHANGELOG.md CHANGED
@@ -6,6 +6,22 @@
6
6
  optional field was removed from the credential record the platform
7
7
  writes.
8
8
 
9
+ ### Added
10
+
11
+ - `fm.messages.listStates(domainId, opts?)` — the **read-back** half of
12
+ the agent's own per-message state (`GET /api/agent/message-states`).
13
+ `claim` and `complete` write that record; this reads it, so a
14
+ restarted agent can tell "I never saw this" from "I saw it and never
15
+ finished". The rows belong to the calling credential, and
16
+ `domainId` is required. Added before 2.0.0 was published, so it
17
+ ships in 2.0.0 rather than in a later minor.
18
+ - `ValidationError.extra` — the other fields of the platform's error
19
+ body, when it sent any. The claim conflict (`409` from
20
+ `POST /api/agent/messages/:messageId/claim`) names the credential
21
+ holding the message in `claimedBy` / `claimedAt`; dropping the body
22
+ left a caller with "Validation failed: message already claimed" and
23
+ no way to tell which worker had it.
24
+
9
25
  ### Removed
10
26
 
11
27
  - `GET /api/v1/me` no longer answers `workerHost`.
package/README.md CHANGED
@@ -190,6 +190,17 @@ const hits = await fm.messages.search(domainId, { q: "invoice" });
190
190
  // Read the conversation a message belongs to.
191
191
  const thread = await fm.threads.get(threadId);
192
192
 
193
+ // Read a message's Markdown projection and its extracted facts — the
194
+ // compact mode omits the raw HTML, which is most of a marketing mail.
195
+ const mail = await fm.messages.get(messageId, { format: "markdown" });
196
+ mail.markdown; // clean Markdown
197
+ mail.extracted?.verificationCodes; // [{ code, source }]
198
+ mail.extracted?.magicLinks; // [{ url, label? }]
199
+
200
+ // Read one attachment as text (text, CSV, Markdown, JSON, XML, .docx, PDF).
201
+ const file = await fm.messages.attachmentText(messageId, attachmentId);
202
+ if (file.supported) console.log(file.text);
203
+
193
204
  // Wait for a message without holding a request open.
194
205
  const wait = await fm.waits.create({ address: "billing@yourdomain.com" });
195
206
  const status = await fm.waits.get(wait.waitId); // pending → resolved | expired
@@ -202,10 +213,43 @@ The body is **fetched**, never pushed: an event carries the routing
202
213
  facts and the subject line, and `fm.messages.get(messageId)` is the
203
214
  ask. The SDK never hands you a message you did not ask for.
204
215
 
216
+ ## The agent's own record
217
+
218
+ The event log says a mail **arrived**. It does not say whether you
219
+ **handled** it. That is your own record, and the SDK keeps it for you
220
+ so a restarted agent can tell "I never saw this" from "I saw it and
221
+ never finished":
222
+
223
+ ```ts
224
+ // Take a message for processing — a mutex, not a decision.
225
+ await fm.messages.claim(messageId, { domainId });
226
+
227
+ // Say how it turned out.
228
+ await fm.messages.complete(messageId, {
229
+ domainId,
230
+ state: "done",
231
+ labels: ["invoice"],
232
+ note: "answered by email",
233
+ });
234
+
235
+ // Read it all back — the resume-after-restart read.
236
+ const { states } = await fm.messages.listStates(domainId, {
237
+ state: "processing",
238
+ });
239
+ ```
240
+
241
+ Three facts worth knowing: the rows belong to **the calling
242
+ credential** (another credential of the same tenant does not read
243
+ them), `domainId` is **required** (the state is scoped to the domain
244
+ whose mailbox received the message), and the platform **records** what
245
+ you wrote — it never moves a row on its own, because there is no rule
246
+ engine behind it.
247
+
205
248
  ## Limitations (v2.0)
206
249
 
207
250
  - Custom domain (HTTP/HTTPS) attachment hosting is not configurable by SDK callers; the SDK backend always sends via R2.
208
- - The webhook delivery-status events (`message.delivered` / `message.bounced`) do not exist yet — and neither does bounce detection. The receive worker has no DSN ingestion, so a send's status is `sent` (the relay accepted it) or `failed` (the relay rejected it) and nothing more. The vocabulary holds `delivered` / `bounced` / `complained` / `delayed`, but nothing writes them.
251
+ - Bounce **suppression** is automatic (a hard bounce, `5.x.x`, suppresses the recipient) but the suppression list is not on the SDK surface: it is readable and removable over the mailstore's tenant-scoped `/api/suppressions` endpoints, and the dashboard is the operator's view. There is no "suppress this address" call, because a suppression is derived from mail rather than from a claim.
252
+ - A deferred bounce (`4.x.x`) is a lifecycle state (`delayed`), not an event: only `message.delivered` and `message.bounced` are pushed.
209
253
 
210
254
  ## License
211
255
 
package/dist/index.cjs CHANGED
@@ -24,10 +24,23 @@ var AuthenticationError = class extends FlowmailsError {
24
24
  };
25
25
  var ValidationError = class extends FlowmailsError {
26
26
  detail;
27
- constructor(code, detail, status = 400) {
27
+ /**
28
+ * The rest of the platform's error body, when it carried more than
29
+ * `error` / `detail`.
30
+ *
31
+ * The claim conflict is the case that matters: a `409` from
32
+ * `POST /api/agent/messages/:messageId/claim` names the credential
33
+ * holding the live claim (`claimedBy`, `claimedAt`). Dropping those
34
+ * left a caller with "Validation failed: message already claimed" and
35
+ * no way to tell which worker holds the message — the one fact the
36
+ * mutex exists to communicate.
37
+ */
38
+ extra;
39
+ constructor(code, detail, status = 400, extra) {
28
40
  super(code, `Validation failed: ${code}${detail ? ` \u2014 ${detail}` : ""}`, status);
29
41
  this.name = "ValidationError";
30
42
  this.detail = detail;
43
+ this.extra = extra;
31
44
  }
32
45
  };
33
46
  var RateLimitError = class extends FlowmailsError {
@@ -56,6 +69,18 @@ var UpstreamError = class extends FlowmailsError {
56
69
 
57
70
  // src/client.ts
58
71
  var WAIT_RESOLVED_EVENT = "wait.resolved";
72
+ var DRAFT_STATUS_NAMES = [
73
+ "pending_review",
74
+ "approved",
75
+ "rejected",
76
+ "sent"
77
+ ];
78
+ var MESSAGE_STATE_NAMES = [
79
+ "pending",
80
+ "processing",
81
+ "done",
82
+ "failed"
83
+ ];
59
84
  var Flowmails = class {
60
85
  apiKey;
61
86
  baseURL;
@@ -238,10 +263,33 @@ var Flowmails = class {
238
263
  if (!messageId) {
239
264
  throw new ValidationError("message_id_required", "messageId is required");
240
265
  }
266
+ if (opts.format !== void 0 && opts.format !== "full" && opts.format !== "markdown") {
267
+ throw new ValidationError(
268
+ "invalid_format",
269
+ 'format must be "full" or "markdown"'
270
+ );
271
+ }
241
272
  return this.request(
242
273
  this.platformURL,
243
274
  "GET",
244
275
  `/api/agent/messages/${encodeURIComponent(messageId)}`,
276
+ { query: { domainId: opts.domainId, format: opts.format } }
277
+ );
278
+ },
279
+ attachmentText: async (messageId, attachmentId, opts = {}) => {
280
+ if (!messageId) {
281
+ throw new ValidationError("message_id_required", "messageId is required");
282
+ }
283
+ if (!attachmentId) {
284
+ throw new ValidationError(
285
+ "attachment_id_required",
286
+ "attachmentId is required"
287
+ );
288
+ }
289
+ return this.request(
290
+ this.platformURL,
291
+ "GET",
292
+ `/api/agent/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachmentId)}/text`,
245
293
  { query: { domainId: opts.domainId } }
246
294
  );
247
295
  },
@@ -367,6 +415,39 @@ var Flowmails = class {
367
415
  }
368
416
  }
369
417
  );
418
+ },
419
+ /**
420
+ * Read the agent's own per-message state back.
421
+ *
422
+ * The state belongs to the **calling credential** — the platform
423
+ * stores it under a fingerprint of the bearer — so a sibling
424
+ * credential of the same tenant does not read it by accident.
425
+ * `domainId` is required because the state is scoped to the
426
+ * domain whose mailbox received the message.
427
+ */
428
+ listStates: async (domainId, opts = {}) => {
429
+ if (!domainId) {
430
+ throw new ValidationError("domain_id_required", "domainId is required");
431
+ }
432
+ if (opts.state !== void 0 && !MESSAGE_STATE_NAMES.includes(opts.state)) {
433
+ throw new ValidationError(
434
+ "invalid_state",
435
+ `state must be one of: ${MESSAGE_STATE_NAMES.join(", ")}`
436
+ );
437
+ }
438
+ return this.request(
439
+ this.platformURL,
440
+ "GET",
441
+ "/api/agent/message-states",
442
+ {
443
+ query: {
444
+ domainId,
445
+ state: opts.state,
446
+ messageId: opts.messageId,
447
+ limit: opts.limit
448
+ }
449
+ }
450
+ );
370
451
  }
371
452
  };
372
453
  /**
@@ -390,6 +471,81 @@ var Flowmails = class {
390
471
  );
391
472
  }
392
473
  };
474
+ /**
475
+ * The draft queue — a message written for a **person** to review.
476
+ *
477
+ * Nothing here sends. `create` writes a `pending_review` row and
478
+ * `list` / `get` report what a reviewer did with it. A human
479
+ * approves it in the dashboard, and the approval is the send — so
480
+ * the platform's audited send path, quota and domain rules apply
481
+ * unchanged.
482
+ *
483
+ * ```ts
484
+ * const draft = await fm.drafts.create({ from, to, subject, text });
485
+ * const queue = await fm.drafts.list({ status: "pending_review" });
486
+ * ```
487
+ */
488
+ drafts = {
489
+ create: async (opts) => {
490
+ if (!opts?.from) {
491
+ throw new ValidationError("from_required", "from is required");
492
+ }
493
+ if (!opts.to || Array.isArray(opts.to) && opts.to.length === 0) {
494
+ throw new ValidationError("to_required", "to is required");
495
+ }
496
+ if (!opts.subject) {
497
+ throw new ValidationError("subject_required", "subject is required");
498
+ }
499
+ if (!opts.useAiContext && !opts.text && !opts.html) {
500
+ throw new ValidationError(
501
+ "body_required",
502
+ "text or html is required, unless useAiContext is set"
503
+ );
504
+ }
505
+ if (opts.useAiContext && !opts.replyToMessageId) {
506
+ throw new ValidationError(
507
+ "reply_parent_required",
508
+ "useAiContext requires replyToMessageId"
509
+ );
510
+ }
511
+ return this.request(
512
+ this.platformURL,
513
+ "POST",
514
+ "/api/agent/drafts",
515
+ { body: opts }
516
+ );
517
+ },
518
+ list: async (opts = {}) => {
519
+ if (opts.status !== void 0 && !DRAFT_STATUS_NAMES.includes(opts.status)) {
520
+ throw new ValidationError(
521
+ "invalid_status",
522
+ `status must be one of: ${DRAFT_STATUS_NAMES.join(", ")}`
523
+ );
524
+ }
525
+ return this.request(
526
+ this.platformURL,
527
+ "GET",
528
+ "/api/agent/drafts",
529
+ {
530
+ query: {
531
+ status: opts.status,
532
+ domainId: opts.domainId,
533
+ limit: opts.limit
534
+ }
535
+ }
536
+ );
537
+ },
538
+ get: async (draftId) => {
539
+ if (!draftId) {
540
+ throw new ValidationError("draft_id_required", "draftId is required");
541
+ }
542
+ return this.request(
543
+ this.platformURL,
544
+ "GET",
545
+ `/api/agent/drafts/${encodeURIComponent(draftId)}`
546
+ );
547
+ }
548
+ };
393
549
  /**
394
550
  * The durable wait — the one thing an agent cannot build for
395
551
  * itself.
@@ -505,7 +661,12 @@ var Flowmails = class {
505
661
  const code = payload.error ?? "request_failed";
506
662
  if (res.status === 401) throw new AuthenticationError(code, res.status);
507
663
  if (res.status === 400 || res.status === 404 || res.status === 422 || res.status === 409) {
508
- throw new ValidationError(code, payload.detail, res.status);
664
+ throw new ValidationError(
665
+ code,
666
+ payload.detail,
667
+ res.status,
668
+ readErrorExtra(payload)
669
+ );
509
670
  }
510
671
  if (res.status === 429) {
511
672
  const facts = readQuotaFacts(res, payload);
@@ -551,6 +712,14 @@ function readQuotaFacts(res, payload) {
551
712
  }
552
713
  return { limit, remaining, resetAt };
553
714
  }
715
+ function readErrorExtra(payload) {
716
+ const extra = {};
717
+ for (const [key, value] of Object.entries(payload)) {
718
+ if (key === "error" || key === "detail") continue;
719
+ extra[key] = value;
720
+ }
721
+ return Object.keys(extra).length > 0 ? extra : void 0;
722
+ }
554
723
  function buildUrl(origin, path, query) {
555
724
  const url = new URL(`${origin}${path}`);
556
725
  if (query) {
@@ -577,6 +746,7 @@ function sleep(ms) {
577
746
  }
578
747
 
579
748
  exports.AuthenticationError = AuthenticationError;
749
+ exports.DRAFT_STATUS_NAMES = DRAFT_STATUS_NAMES;
580
750
  exports.Flowmails = Flowmails;
581
751
  exports.FlowmailsError = FlowmailsError;
582
752
  exports.RateLimitError = RateLimitError;