@flowmails/flowmails-sdk 0.2.1 → 2.1.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/CHANGELOG.md +63 -0
- package/README.md +89 -8
- package/dist/index.cjs +275 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +572 -52
- package/dist/index.d.ts +572 -52
- package/dist/index.js +274 -12
- package/dist/index.js.map +1 -1
- package/openapi.json +4019 -0
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -22,10 +22,23 @@ var AuthenticationError = class extends FlowmailsError {
|
|
|
22
22
|
};
|
|
23
23
|
var ValidationError = class extends FlowmailsError {
|
|
24
24
|
detail;
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* The rest of the platform's error body, when it carried more than
|
|
27
|
+
* `error` / `detail`.
|
|
28
|
+
*
|
|
29
|
+
* The claim conflict is the case that matters: a `409` from
|
|
30
|
+
* `POST /api/agent/messages/:messageId/claim` names the credential
|
|
31
|
+
* holding the live claim (`claimedBy`, `claimedAt`). Dropping those
|
|
32
|
+
* left a caller with "Validation failed: message already claimed" and
|
|
33
|
+
* no way to tell which worker holds the message — the one fact the
|
|
34
|
+
* mutex exists to communicate.
|
|
35
|
+
*/
|
|
36
|
+
extra;
|
|
37
|
+
constructor(code, detail, status = 400, extra) {
|
|
26
38
|
super(code, `Validation failed: ${code}${detail ? ` \u2014 ${detail}` : ""}`, status);
|
|
27
39
|
this.name = "ValidationError";
|
|
28
40
|
this.detail = detail;
|
|
41
|
+
this.extra = extra;
|
|
29
42
|
}
|
|
30
43
|
};
|
|
31
44
|
var RateLimitError = class extends FlowmailsError {
|
|
@@ -53,6 +66,19 @@ var UpstreamError = class extends FlowmailsError {
|
|
|
53
66
|
};
|
|
54
67
|
|
|
55
68
|
// src/client.ts
|
|
69
|
+
var WAIT_RESOLVED_EVENT = "wait.resolved";
|
|
70
|
+
var DRAFT_STATUS_NAMES = [
|
|
71
|
+
"pending_review",
|
|
72
|
+
"approved",
|
|
73
|
+
"rejected",
|
|
74
|
+
"sent"
|
|
75
|
+
];
|
|
76
|
+
var MESSAGE_STATE_NAMES = [
|
|
77
|
+
"pending",
|
|
78
|
+
"processing",
|
|
79
|
+
"done",
|
|
80
|
+
"failed"
|
|
81
|
+
];
|
|
56
82
|
var Flowmails = class {
|
|
57
83
|
apiKey;
|
|
58
84
|
baseURL;
|
|
@@ -69,10 +95,7 @@ var Flowmails = class {
|
|
|
69
95
|
/\/+$/,
|
|
70
96
|
""
|
|
71
97
|
);
|
|
72
|
-
this.platformURL = (opts.platformURL ??
|
|
73
|
-
/\/+$/,
|
|
74
|
-
""
|
|
75
|
-
);
|
|
98
|
+
this.platformURL = (opts.platformURL ?? this.baseURL).replace(/\/+$/, "");
|
|
76
99
|
this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
77
100
|
this.maxRetries = opts.maxRetries ?? 2;
|
|
78
101
|
this.requestId = opts.requestId;
|
|
@@ -94,6 +117,20 @@ var Flowmails = class {
|
|
|
94
117
|
* reason: `error` is the typed slug and `sendFeedback` is the
|
|
95
118
|
* relay's own words. Check `result.error` before treating a `201`
|
|
96
119
|
* as "sent".
|
|
120
|
+
*
|
|
121
|
+
* **A partial send is not a success either.** Several recipients
|
|
122
|
+
* each get their own message, and the relay can refuse some of them.
|
|
123
|
+
* `status` is then `"partial"` — `error` is still set, and
|
|
124
|
+
* `result.recipients` says exactly which addresses were refused and
|
|
125
|
+
* why:
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* const result = await fm.send({ from, to: ["a@x.test", "b@x.test"], subject, text });
|
|
129
|
+
* if (result.status === "partial") {
|
|
130
|
+
* const refused = result.recipients?.filter((r) => r.status === "failed") ?? [];
|
|
131
|
+
* // retry only these
|
|
132
|
+
* }
|
|
133
|
+
* ```
|
|
97
134
|
*/
|
|
98
135
|
async send(opts) {
|
|
99
136
|
const body = {
|
|
@@ -117,7 +154,13 @@ var Flowmails = class {
|
|
|
117
154
|
// It is deliberately **not** renamed: the backend's body is
|
|
118
155
|
// camelCase already, and a rename here would be one more place
|
|
119
156
|
// for the two sides to disagree.
|
|
120
|
-
idempotencyKey: opts.idempotencyKey
|
|
157
|
+
idempotencyKey: opts.idempotencyKey,
|
|
158
|
+
// The delivery mode travels under the same name as on the wire.
|
|
159
|
+
// It is deliberately **not** defaulted here: the backend owns the
|
|
160
|
+
// default, so a new SDK talking to an older backend and an old
|
|
161
|
+
// SDK talking to a new backend both behave the way the backend
|
|
162
|
+
// says they should.
|
|
163
|
+
perRecipient: opts.perRecipient
|
|
121
164
|
};
|
|
122
165
|
return this.request(this.baseURL, "POST", "/api/v1/messages", {
|
|
123
166
|
body
|
|
@@ -140,7 +183,8 @@ var Flowmails = class {
|
|
|
140
183
|
* ```ts
|
|
141
184
|
* const me = await fm.me();
|
|
142
185
|
* if (me.sending && !me.sending.canSend) {
|
|
143
|
-
* // me.sending.
|
|
186
|
+
* // me.sending.configured: false means sending was never set up;
|
|
187
|
+
* // otherwise me.sending.state says why: "setup_required", …
|
|
144
188
|
* }
|
|
145
189
|
* ```
|
|
146
190
|
*
|
|
@@ -151,10 +195,11 @@ var Flowmails = class {
|
|
|
151
195
|
return this.request(this.baseURL, "GET", "/api/v1/me");
|
|
152
196
|
}
|
|
153
197
|
/**
|
|
154
|
-
* The
|
|
198
|
+
* The shared mail worker, and the version of it.
|
|
155
199
|
*
|
|
156
|
-
* The
|
|
157
|
-
*
|
|
200
|
+
* The mail worker does the actual receiving and sending — it is
|
|
201
|
+
* Flowmails' own deployment, one shared worker for every tenant —
|
|
202
|
+
* and it is versioned: `1.4.0` brought the route tables, `1.5.1` the
|
|
158
203
|
* Message-ID lookup, `1.6.0` the event reporting that fills the
|
|
159
204
|
* log this SDK reads. An agent whose worker is older than the
|
|
160
205
|
* platform expects sees an empty event log and has no way to tell
|
|
@@ -216,10 +261,33 @@ var Flowmails = class {
|
|
|
216
261
|
if (!messageId) {
|
|
217
262
|
throw new ValidationError("message_id_required", "messageId is required");
|
|
218
263
|
}
|
|
264
|
+
if (opts.format !== void 0 && opts.format !== "full" && opts.format !== "markdown") {
|
|
265
|
+
throw new ValidationError(
|
|
266
|
+
"invalid_format",
|
|
267
|
+
'format must be "full" or "markdown"'
|
|
268
|
+
);
|
|
269
|
+
}
|
|
219
270
|
return this.request(
|
|
220
271
|
this.platformURL,
|
|
221
272
|
"GET",
|
|
222
273
|
`/api/agent/messages/${encodeURIComponent(messageId)}`,
|
|
274
|
+
{ query: { domainId: opts.domainId, format: opts.format } }
|
|
275
|
+
);
|
|
276
|
+
},
|
|
277
|
+
attachmentText: async (messageId, attachmentId, opts = {}) => {
|
|
278
|
+
if (!messageId) {
|
|
279
|
+
throw new ValidationError("message_id_required", "messageId is required");
|
|
280
|
+
}
|
|
281
|
+
if (!attachmentId) {
|
|
282
|
+
throw new ValidationError(
|
|
283
|
+
"attachment_id_required",
|
|
284
|
+
"attachmentId is required"
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
return this.request(
|
|
288
|
+
this.platformURL,
|
|
289
|
+
"GET",
|
|
290
|
+
`/api/agent/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachmentId)}/text`,
|
|
223
291
|
{ query: { domainId: opts.domainId } }
|
|
224
292
|
);
|
|
225
293
|
},
|
|
@@ -241,6 +309,79 @@ var Flowmails = class {
|
|
|
241
309
|
`/api/v1/messages/${encodeURIComponent(id)}`
|
|
242
310
|
);
|
|
243
311
|
},
|
|
312
|
+
/**
|
|
313
|
+
* Claim a message for processing.
|
|
314
|
+
*
|
|
315
|
+
* The event log says a mail arrived; it does not say whether the
|
|
316
|
+
* agent already handled it. This writes that fact down, so a
|
|
317
|
+
* restarted agent can tell "I never saw this" from "I saw it and
|
|
318
|
+
* never finished".
|
|
319
|
+
*
|
|
320
|
+
* The claim is a **mutex, not a decision**: the platform records
|
|
321
|
+
* that this credential is working on the message and refuses a
|
|
322
|
+
* second live claim by another credential of the same account. It
|
|
323
|
+
* does not judge whether the mail matters and does not act on it.
|
|
324
|
+
*
|
|
325
|
+
* ```ts
|
|
326
|
+
* await fm.messages.claim(event.messageId, { domainId });
|
|
327
|
+
* // …process…
|
|
328
|
+
* await fm.messages.complete(event.messageId, { domainId, state: "done" });
|
|
329
|
+
* ```
|
|
330
|
+
*
|
|
331
|
+
* @throws {ValidationError} when `messageId` or `domainId` is
|
|
332
|
+
* missing.
|
|
333
|
+
*/
|
|
334
|
+
claim: async (messageId, opts) => {
|
|
335
|
+
if (!messageId) {
|
|
336
|
+
throw new ValidationError("message_id_required", "messageId is required");
|
|
337
|
+
}
|
|
338
|
+
if (!opts?.domainId) {
|
|
339
|
+
throw new ValidationError("domain_id_required", "domainId is required");
|
|
340
|
+
}
|
|
341
|
+
return this.request(
|
|
342
|
+
this.platformURL,
|
|
343
|
+
"POST",
|
|
344
|
+
`/api/agent/messages/${encodeURIComponent(messageId)}/claim`,
|
|
345
|
+
{ body: { domainId: opts.domainId, claimedBy: opts.claimedBy } }
|
|
346
|
+
);
|
|
347
|
+
},
|
|
348
|
+
/**
|
|
349
|
+
* Record how a message turned out.
|
|
350
|
+
*
|
|
351
|
+
* `done` or `failed`, optionally with labels and a note. The
|
|
352
|
+
* platform stores what the agent said and takes no further action
|
|
353
|
+
* on it.
|
|
354
|
+
*
|
|
355
|
+
* @throws {ValidationError} when `messageId`, `domainId` or
|
|
356
|
+
* `state` is missing, or `state` is neither `done` nor `failed`.
|
|
357
|
+
*/
|
|
358
|
+
complete: async (messageId, opts) => {
|
|
359
|
+
if (!messageId) {
|
|
360
|
+
throw new ValidationError("message_id_required", "messageId is required");
|
|
361
|
+
}
|
|
362
|
+
if (!opts?.domainId) {
|
|
363
|
+
throw new ValidationError("domain_id_required", "domainId is required");
|
|
364
|
+
}
|
|
365
|
+
if (opts.state !== "done" && opts.state !== "failed") {
|
|
366
|
+
throw new ValidationError(
|
|
367
|
+
"invalid_state",
|
|
368
|
+
"state must be 'done' or 'failed'"
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
return this.request(
|
|
372
|
+
this.platformURL,
|
|
373
|
+
"POST",
|
|
374
|
+
`/api/agent/messages/${encodeURIComponent(messageId)}/complete`,
|
|
375
|
+
{
|
|
376
|
+
body: {
|
|
377
|
+
domainId: opts.domainId,
|
|
378
|
+
state: opts.state,
|
|
379
|
+
labels: opts.labels,
|
|
380
|
+
note: opts.note
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
);
|
|
384
|
+
},
|
|
244
385
|
/**
|
|
245
386
|
* Look back through the mailbox.
|
|
246
387
|
*
|
|
@@ -272,6 +413,39 @@ var Flowmails = class {
|
|
|
272
413
|
}
|
|
273
414
|
}
|
|
274
415
|
);
|
|
416
|
+
},
|
|
417
|
+
/**
|
|
418
|
+
* Read the agent's own per-message state back.
|
|
419
|
+
*
|
|
420
|
+
* The state belongs to the **calling credential** — the platform
|
|
421
|
+
* stores it under a fingerprint of the bearer — so a sibling
|
|
422
|
+
* credential of the same tenant does not read it by accident.
|
|
423
|
+
* `domainId` is required because the state is scoped to the
|
|
424
|
+
* domain whose mailbox received the message.
|
|
425
|
+
*/
|
|
426
|
+
listStates: async (domainId, opts = {}) => {
|
|
427
|
+
if (!domainId) {
|
|
428
|
+
throw new ValidationError("domain_id_required", "domainId is required");
|
|
429
|
+
}
|
|
430
|
+
if (opts.state !== void 0 && !MESSAGE_STATE_NAMES.includes(opts.state)) {
|
|
431
|
+
throw new ValidationError(
|
|
432
|
+
"invalid_state",
|
|
433
|
+
`state must be one of: ${MESSAGE_STATE_NAMES.join(", ")}`
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
return this.request(
|
|
437
|
+
this.platformURL,
|
|
438
|
+
"GET",
|
|
439
|
+
"/api/agent/message-states",
|
|
440
|
+
{
|
|
441
|
+
query: {
|
|
442
|
+
domainId,
|
|
443
|
+
state: opts.state,
|
|
444
|
+
messageId: opts.messageId,
|
|
445
|
+
limit: opts.limit
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
);
|
|
275
449
|
}
|
|
276
450
|
};
|
|
277
451
|
/**
|
|
@@ -295,6 +469,81 @@ var Flowmails = class {
|
|
|
295
469
|
);
|
|
296
470
|
}
|
|
297
471
|
};
|
|
472
|
+
/**
|
|
473
|
+
* The draft queue — a message written for a **person** to review.
|
|
474
|
+
*
|
|
475
|
+
* Nothing here sends. `create` writes a `pending_review` row and
|
|
476
|
+
* `list` / `get` report what a reviewer did with it. A human
|
|
477
|
+
* approves it in the dashboard, and the approval is the send — so
|
|
478
|
+
* the platform's audited send path, quota and domain rules apply
|
|
479
|
+
* unchanged.
|
|
480
|
+
*
|
|
481
|
+
* ```ts
|
|
482
|
+
* const draft = await fm.drafts.create({ from, to, subject, text });
|
|
483
|
+
* const queue = await fm.drafts.list({ status: "pending_review" });
|
|
484
|
+
* ```
|
|
485
|
+
*/
|
|
486
|
+
drafts = {
|
|
487
|
+
create: async (opts) => {
|
|
488
|
+
if (!opts?.from) {
|
|
489
|
+
throw new ValidationError("from_required", "from is required");
|
|
490
|
+
}
|
|
491
|
+
if (!opts.to || Array.isArray(opts.to) && opts.to.length === 0) {
|
|
492
|
+
throw new ValidationError("to_required", "to is required");
|
|
493
|
+
}
|
|
494
|
+
if (!opts.subject) {
|
|
495
|
+
throw new ValidationError("subject_required", "subject is required");
|
|
496
|
+
}
|
|
497
|
+
if (!opts.useAiContext && !opts.text && !opts.html) {
|
|
498
|
+
throw new ValidationError(
|
|
499
|
+
"body_required",
|
|
500
|
+
"text or html is required, unless useAiContext is set"
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
if (opts.useAiContext && !opts.replyToMessageId) {
|
|
504
|
+
throw new ValidationError(
|
|
505
|
+
"reply_parent_required",
|
|
506
|
+
"useAiContext requires replyToMessageId"
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
return this.request(
|
|
510
|
+
this.platformURL,
|
|
511
|
+
"POST",
|
|
512
|
+
"/api/agent/drafts",
|
|
513
|
+
{ body: opts }
|
|
514
|
+
);
|
|
515
|
+
},
|
|
516
|
+
list: async (opts = {}) => {
|
|
517
|
+
if (opts.status !== void 0 && !DRAFT_STATUS_NAMES.includes(opts.status)) {
|
|
518
|
+
throw new ValidationError(
|
|
519
|
+
"invalid_status",
|
|
520
|
+
`status must be one of: ${DRAFT_STATUS_NAMES.join(", ")}`
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
return this.request(
|
|
524
|
+
this.platformURL,
|
|
525
|
+
"GET",
|
|
526
|
+
"/api/agent/drafts",
|
|
527
|
+
{
|
|
528
|
+
query: {
|
|
529
|
+
status: opts.status,
|
|
530
|
+
domainId: opts.domainId,
|
|
531
|
+
limit: opts.limit
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
);
|
|
535
|
+
},
|
|
536
|
+
get: async (draftId) => {
|
|
537
|
+
if (!draftId) {
|
|
538
|
+
throw new ValidationError("draft_id_required", "draftId is required");
|
|
539
|
+
}
|
|
540
|
+
return this.request(
|
|
541
|
+
this.platformURL,
|
|
542
|
+
"GET",
|
|
543
|
+
`/api/agent/drafts/${encodeURIComponent(draftId)}`
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
};
|
|
298
547
|
/**
|
|
299
548
|
* The durable wait — the one thing an agent cannot build for
|
|
300
549
|
* itself.
|
|
@@ -410,7 +659,12 @@ var Flowmails = class {
|
|
|
410
659
|
const code = payload.error ?? "request_failed";
|
|
411
660
|
if (res.status === 401) throw new AuthenticationError(code, res.status);
|
|
412
661
|
if (res.status === 400 || res.status === 404 || res.status === 422 || res.status === 409) {
|
|
413
|
-
throw new ValidationError(
|
|
662
|
+
throw new ValidationError(
|
|
663
|
+
code,
|
|
664
|
+
payload.detail,
|
|
665
|
+
res.status,
|
|
666
|
+
readErrorExtra(payload)
|
|
667
|
+
);
|
|
414
668
|
}
|
|
415
669
|
if (res.status === 429) {
|
|
416
670
|
const facts = readQuotaFacts(res, payload);
|
|
@@ -456,6 +710,14 @@ function readQuotaFacts(res, payload) {
|
|
|
456
710
|
}
|
|
457
711
|
return { limit, remaining, resetAt };
|
|
458
712
|
}
|
|
713
|
+
function readErrorExtra(payload) {
|
|
714
|
+
const extra = {};
|
|
715
|
+
for (const [key, value] of Object.entries(payload)) {
|
|
716
|
+
if (key === "error" || key === "detail") continue;
|
|
717
|
+
extra[key] = value;
|
|
718
|
+
}
|
|
719
|
+
return Object.keys(extra).length > 0 ? extra : void 0;
|
|
720
|
+
}
|
|
459
721
|
function buildUrl(origin, path, query) {
|
|
460
722
|
const url = new URL(`${origin}${path}`);
|
|
461
723
|
if (query) {
|
|
@@ -481,6 +743,6 @@ function sleep(ms) {
|
|
|
481
743
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
482
744
|
}
|
|
483
745
|
|
|
484
|
-
export { AuthenticationError, Flowmails, FlowmailsError, RateLimitError, UpstreamError, ValidationError };
|
|
746
|
+
export { AuthenticationError, DRAFT_STATUS_NAMES, Flowmails, FlowmailsError, RateLimitError, UpstreamError, ValidationError, WAIT_RESOLVED_EVENT };
|
|
485
747
|
//# sourceMappingURL=index.js.map
|
|
486
748
|
//# sourceMappingURL=index.js.map
|