@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/CHANGELOG.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.0.0 — the worker's address is gone
|
|
4
|
+
|
|
5
|
+
**Breaking change.** One field was removed from two responses, and one
|
|
6
|
+
optional field was removed from the credential record the platform
|
|
7
|
+
writes.
|
|
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
|
+
|
|
25
|
+
### Removed
|
|
26
|
+
|
|
27
|
+
- `GET /api/v1/me` no longer answers `workerHost`.
|
|
28
|
+
- `GET /api/v1/worker-version` no longer answers `workerHost`, and it
|
|
29
|
+
**no longer 404s** when the deployment publishes no address — it
|
|
30
|
+
answers `{ version, bootstrapped, lastDeployAt, lastDeployError }`
|
|
31
|
+
from the platform's own probe of the shared worker.
|
|
32
|
+
- `AccountInfo.workerHost` and `WorkerVersionInfo.workerHost` are
|
|
33
|
+
gone from the TypeScript types.
|
|
34
|
+
|
|
35
|
+
### Why
|
|
36
|
+
|
|
37
|
+
The shared mail worker is ONE platform-owned deployment, reached
|
|
38
|
+
through the `MAIL_WORKER` service binding — it needs no address and no
|
|
39
|
+
bearer. The address was therefore the same string for every tenant and
|
|
40
|
+
carried no per-credential information: a per-row copy of a deployment
|
|
41
|
+
fact, kept alive only because the old per-account worker model still
|
|
42
|
+
had a place for it.
|
|
43
|
+
|
|
44
|
+
It also cost something real. A deployment whose worker publishes no
|
|
45
|
+
public hostname (the shared worker runs with `workers_dev = false`, so
|
|
46
|
+
it has none) could not issue an API key at all: creating one failed
|
|
47
|
+
with *"the shared mail worker is not configured on this deployment"*,
|
|
48
|
+
while the key's row was already written — leaving a key in the
|
|
49
|
+
dashboard that authenticated nothing and still occupied one slot of
|
|
50
|
+
the plan's `maxApiKeys` quota. And `/api/v1/worker-version` answered
|
|
51
|
+
`404 worker_not_found` on exactly those deployments, making a working
|
|
52
|
+
platform look broken.
|
|
53
|
+
|
|
54
|
+
### Migrating
|
|
55
|
+
|
|
56
|
+
- If you read `me().workerHost` or `workerVersion().workerHost`, drop
|
|
57
|
+
it. There is no replacement value: the worker is the same for every
|
|
58
|
+
tenant, and its **version** — `workerVersion().version` — is the fact
|
|
59
|
+
worth reporting.
|
|
60
|
+
- If you compared `workerHost` against a configured address to decide
|
|
61
|
+
whether the platform was configured, that check no longer exists (and
|
|
62
|
+
no longer needs to): a key that authenticates is the proof that the
|
|
63
|
+
platform is configured.
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The official JavaScript / TypeScript SDK for the **Flowmails SDK backend** (`https://sdk.flowmails.net`).
|
|
4
4
|
|
|
5
|
-
> **Status: public preview (
|
|
5
|
+
> **Status: public preview (v2.0.x)** — published from the Flowmails monorepo. v2.0 is a breaking release: the shared worker's address is gone from `fm.me()` and `fm.workerVersion()` (see `CHANGELOG.md`). The wire format and surface are stable within the 2.x line; pin the minor version (`~2.0`) in production.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -28,7 +28,7 @@ const result = await fm.send({
|
|
|
28
28
|
// html: "<p>Thanks for your order…</p>",
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
console.log(result); // { id: "
|
|
31
|
+
console.log(result); // { id: "8421", status: "sent", messageId: "<abc123@yourdomain.com>", … }
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
## A send the relay refused
|
|
@@ -48,6 +48,41 @@ if (result.status === "failed") {
|
|
|
48
48
|
`result.error` is never `null` on a failed send: *"failed with no
|
|
49
49
|
reason"* is not a state the wire can be in.
|
|
50
50
|
|
|
51
|
+
## Several recipients, and the partial case
|
|
52
|
+
|
|
53
|
+
Each recipient gets their **own** message by default — they cannot see
|
|
54
|
+
each other, and one refused address does not hold up the others. So the
|
|
55
|
+
relay can accept some and refuse others, and the answer says so:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const result = await fm.send({
|
|
59
|
+
from: "support@yourdomain.com",
|
|
60
|
+
to: ["a@example.com", "b@example.com", "c@example.com"],
|
|
61
|
+
subject: "Order #1234 confirmed",
|
|
62
|
+
text: "…",
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (result.status === "partial") {
|
|
66
|
+
// Some recipients got it and some were refused. `error` is still
|
|
67
|
+
// set — check it before treating a 201 as "sent".
|
|
68
|
+
for (const r of result.recipients ?? []) {
|
|
69
|
+
if (r.status === "failed") {
|
|
70
|
+
console.error(`${r.to}: ${r.sendFeedback}`); // retry only these
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`status` is the aggregate over every recipient — `"sent"`, `"partial"`
|
|
77
|
+
or `"failed"` — and `recipients` carries what happened to **each**
|
|
78
|
+
address: its row id, its own verdict and the relay's reason when it
|
|
79
|
+
was refused.
|
|
80
|
+
|
|
81
|
+
Pass `perRecipient: false` to put everybody on **one** shared envelope
|
|
82
|
+
instead: one message, one relay call, one bill from a provider that
|
|
83
|
+
charges per message — and then the send is all-or-nothing by
|
|
84
|
+
construction.
|
|
85
|
+
|
|
51
86
|
## Is my domain able to send?
|
|
52
87
|
|
|
53
88
|
```ts
|
|
@@ -65,7 +100,7 @@ not *"this domain cannot send"*.
|
|
|
65
100
|
|
|
66
101
|
## About Flowmails
|
|
67
102
|
|
|
68
|
-
[Flowmails](https://flowmails.net) is a
|
|
103
|
+
[Flowmails](https://flowmails.net) is a custom-domain email platform: mail (inbox, outbox, and routing rules) held in the platform's own storage and scoped to your domain, with a REST API and webhooks on top. Inbound routing and outbound sending are provider-pluggable — you connect your domain provider over OAuth and pick the outbound channel per domain (Cloudflare Email Sending or Resend). The dashboard, docs, and blog live at **<https://flowmails.net>** — sign up there to mint an API key and bind a domain before using this SDK.
|
|
69
104
|
|
|
70
105
|
## API keys
|
|
71
106
|
|
|
@@ -136,9 +171,11 @@ inside it cannot succeed.
|
|
|
136
171
|
|
|
137
172
|
## The read half
|
|
138
173
|
|
|
139
|
-
Sending is one half of the SDK; the other is **reading** —
|
|
140
|
-
|
|
141
|
-
|
|
174
|
+
Sending is one half of the SDK; the other is **reading** — the event
|
|
175
|
+
log, the durable wait and the message read. It used to talk to a
|
|
176
|
+
different origin (the platform worker); the agent platform moved to
|
|
177
|
+
the SDK backend, so the read half is on the **same** host as the send
|
|
178
|
+
half now.
|
|
142
179
|
|
|
143
180
|
```ts
|
|
144
181
|
// Check the credential without sending anything.
|
|
@@ -153,6 +190,17 @@ const hits = await fm.messages.search(domainId, { q: "invoice" });
|
|
|
153
190
|
// Read the conversation a message belongs to.
|
|
154
191
|
const thread = await fm.threads.get(threadId);
|
|
155
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
|
+
|
|
156
204
|
// Wait for a message without holding a request open.
|
|
157
205
|
const wait = await fm.waits.create({ address: "billing@yourdomain.com" });
|
|
158
206
|
const status = await fm.waits.get(wait.waitId); // pending → resolved | expired
|
|
@@ -165,10 +213,43 @@ The body is **fetched**, never pushed: an event carries the routing
|
|
|
165
213
|
facts and the subject line, and `fm.messages.get(messageId)` is the
|
|
166
214
|
ask. The SDK never hands you a message you did not ask for.
|
|
167
215
|
|
|
168
|
-
##
|
|
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
|
+
|
|
248
|
+
## Limitations (v2.0)
|
|
169
249
|
|
|
170
250
|
- Custom domain (HTTP/HTTPS) attachment hosting is not configurable by SDK callers; the SDK backend always sends via R2.
|
|
171
|
-
-
|
|
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.
|
|
172
253
|
|
|
173
254
|
## License
|
|
174
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
|
-
|
|
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 {
|
|
@@ -55,6 +68,19 @@ var UpstreamError = class extends FlowmailsError {
|
|
|
55
68
|
};
|
|
56
69
|
|
|
57
70
|
// src/client.ts
|
|
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
|
+
];
|
|
58
84
|
var Flowmails = class {
|
|
59
85
|
apiKey;
|
|
60
86
|
baseURL;
|
|
@@ -71,10 +97,7 @@ var Flowmails = class {
|
|
|
71
97
|
/\/+$/,
|
|
72
98
|
""
|
|
73
99
|
);
|
|
74
|
-
this.platformURL = (opts.platformURL ??
|
|
75
|
-
/\/+$/,
|
|
76
|
-
""
|
|
77
|
-
);
|
|
100
|
+
this.platformURL = (opts.platformURL ?? this.baseURL).replace(/\/+$/, "");
|
|
78
101
|
this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
79
102
|
this.maxRetries = opts.maxRetries ?? 2;
|
|
80
103
|
this.requestId = opts.requestId;
|
|
@@ -96,6 +119,20 @@ var Flowmails = class {
|
|
|
96
119
|
* reason: `error` is the typed slug and `sendFeedback` is the
|
|
97
120
|
* relay's own words. Check `result.error` before treating a `201`
|
|
98
121
|
* as "sent".
|
|
122
|
+
*
|
|
123
|
+
* **A partial send is not a success either.** Several recipients
|
|
124
|
+
* each get their own message, and the relay can refuse some of them.
|
|
125
|
+
* `status` is then `"partial"` — `error` is still set, and
|
|
126
|
+
* `result.recipients` says exactly which addresses were refused and
|
|
127
|
+
* why:
|
|
128
|
+
*
|
|
129
|
+
* ```ts
|
|
130
|
+
* const result = await fm.send({ from, to: ["a@x.test", "b@x.test"], subject, text });
|
|
131
|
+
* if (result.status === "partial") {
|
|
132
|
+
* const refused = result.recipients?.filter((r) => r.status === "failed") ?? [];
|
|
133
|
+
* // retry only these
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
99
136
|
*/
|
|
100
137
|
async send(opts) {
|
|
101
138
|
const body = {
|
|
@@ -119,7 +156,13 @@ var Flowmails = class {
|
|
|
119
156
|
// It is deliberately **not** renamed: the backend's body is
|
|
120
157
|
// camelCase already, and a rename here would be one more place
|
|
121
158
|
// for the two sides to disagree.
|
|
122
|
-
idempotencyKey: opts.idempotencyKey
|
|
159
|
+
idempotencyKey: opts.idempotencyKey,
|
|
160
|
+
// The delivery mode travels under the same name as on the wire.
|
|
161
|
+
// It is deliberately **not** defaulted here: the backend owns the
|
|
162
|
+
// default, so a new SDK talking to an older backend and an old
|
|
163
|
+
// SDK talking to a new backend both behave the way the backend
|
|
164
|
+
// says they should.
|
|
165
|
+
perRecipient: opts.perRecipient
|
|
123
166
|
};
|
|
124
167
|
return this.request(this.baseURL, "POST", "/api/v1/messages", {
|
|
125
168
|
body
|
|
@@ -142,7 +185,8 @@ var Flowmails = class {
|
|
|
142
185
|
* ```ts
|
|
143
186
|
* const me = await fm.me();
|
|
144
187
|
* if (me.sending && !me.sending.canSend) {
|
|
145
|
-
* // me.sending.
|
|
188
|
+
* // me.sending.configured: false means sending was never set up;
|
|
189
|
+
* // otherwise me.sending.state says why: "setup_required", …
|
|
146
190
|
* }
|
|
147
191
|
* ```
|
|
148
192
|
*
|
|
@@ -153,10 +197,11 @@ var Flowmails = class {
|
|
|
153
197
|
return this.request(this.baseURL, "GET", "/api/v1/me");
|
|
154
198
|
}
|
|
155
199
|
/**
|
|
156
|
-
* The
|
|
200
|
+
* The shared mail worker, and the version of it.
|
|
157
201
|
*
|
|
158
|
-
* The
|
|
159
|
-
*
|
|
202
|
+
* The mail worker does the actual receiving and sending — it is
|
|
203
|
+
* Flowmails' own deployment, one shared worker for every tenant —
|
|
204
|
+
* and it is versioned: `1.4.0` brought the route tables, `1.5.1` the
|
|
160
205
|
* Message-ID lookup, `1.6.0` the event reporting that fills the
|
|
161
206
|
* log this SDK reads. An agent whose worker is older than the
|
|
162
207
|
* platform expects sees an empty event log and has no way to tell
|
|
@@ -218,10 +263,33 @@ var Flowmails = class {
|
|
|
218
263
|
if (!messageId) {
|
|
219
264
|
throw new ValidationError("message_id_required", "messageId is required");
|
|
220
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
|
+
}
|
|
221
272
|
return this.request(
|
|
222
273
|
this.platformURL,
|
|
223
274
|
"GET",
|
|
224
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`,
|
|
225
293
|
{ query: { domainId: opts.domainId } }
|
|
226
294
|
);
|
|
227
295
|
},
|
|
@@ -243,6 +311,79 @@ var Flowmails = class {
|
|
|
243
311
|
`/api/v1/messages/${encodeURIComponent(id)}`
|
|
244
312
|
);
|
|
245
313
|
},
|
|
314
|
+
/**
|
|
315
|
+
* Claim a message for processing.
|
|
316
|
+
*
|
|
317
|
+
* The event log says a mail arrived; it does not say whether the
|
|
318
|
+
* agent already handled it. This writes that fact down, so a
|
|
319
|
+
* restarted agent can tell "I never saw this" from "I saw it and
|
|
320
|
+
* never finished".
|
|
321
|
+
*
|
|
322
|
+
* The claim is a **mutex, not a decision**: the platform records
|
|
323
|
+
* that this credential is working on the message and refuses a
|
|
324
|
+
* second live claim by another credential of the same account. It
|
|
325
|
+
* does not judge whether the mail matters and does not act on it.
|
|
326
|
+
*
|
|
327
|
+
* ```ts
|
|
328
|
+
* await fm.messages.claim(event.messageId, { domainId });
|
|
329
|
+
* // …process…
|
|
330
|
+
* await fm.messages.complete(event.messageId, { domainId, state: "done" });
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* @throws {ValidationError} when `messageId` or `domainId` is
|
|
334
|
+
* missing.
|
|
335
|
+
*/
|
|
336
|
+
claim: async (messageId, opts) => {
|
|
337
|
+
if (!messageId) {
|
|
338
|
+
throw new ValidationError("message_id_required", "messageId is required");
|
|
339
|
+
}
|
|
340
|
+
if (!opts?.domainId) {
|
|
341
|
+
throw new ValidationError("domain_id_required", "domainId is required");
|
|
342
|
+
}
|
|
343
|
+
return this.request(
|
|
344
|
+
this.platformURL,
|
|
345
|
+
"POST",
|
|
346
|
+
`/api/agent/messages/${encodeURIComponent(messageId)}/claim`,
|
|
347
|
+
{ body: { domainId: opts.domainId, claimedBy: opts.claimedBy } }
|
|
348
|
+
);
|
|
349
|
+
},
|
|
350
|
+
/**
|
|
351
|
+
* Record how a message turned out.
|
|
352
|
+
*
|
|
353
|
+
* `done` or `failed`, optionally with labels and a note. The
|
|
354
|
+
* platform stores what the agent said and takes no further action
|
|
355
|
+
* on it.
|
|
356
|
+
*
|
|
357
|
+
* @throws {ValidationError} when `messageId`, `domainId` or
|
|
358
|
+
* `state` is missing, or `state` is neither `done` nor `failed`.
|
|
359
|
+
*/
|
|
360
|
+
complete: async (messageId, opts) => {
|
|
361
|
+
if (!messageId) {
|
|
362
|
+
throw new ValidationError("message_id_required", "messageId is required");
|
|
363
|
+
}
|
|
364
|
+
if (!opts?.domainId) {
|
|
365
|
+
throw new ValidationError("domain_id_required", "domainId is required");
|
|
366
|
+
}
|
|
367
|
+
if (opts.state !== "done" && opts.state !== "failed") {
|
|
368
|
+
throw new ValidationError(
|
|
369
|
+
"invalid_state",
|
|
370
|
+
"state must be 'done' or 'failed'"
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
return this.request(
|
|
374
|
+
this.platformURL,
|
|
375
|
+
"POST",
|
|
376
|
+
`/api/agent/messages/${encodeURIComponent(messageId)}/complete`,
|
|
377
|
+
{
|
|
378
|
+
body: {
|
|
379
|
+
domainId: opts.domainId,
|
|
380
|
+
state: opts.state,
|
|
381
|
+
labels: opts.labels,
|
|
382
|
+
note: opts.note
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
);
|
|
386
|
+
},
|
|
246
387
|
/**
|
|
247
388
|
* Look back through the mailbox.
|
|
248
389
|
*
|
|
@@ -274,6 +415,39 @@ var Flowmails = class {
|
|
|
274
415
|
}
|
|
275
416
|
}
|
|
276
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
|
+
);
|
|
277
451
|
}
|
|
278
452
|
};
|
|
279
453
|
/**
|
|
@@ -297,6 +471,81 @@ var Flowmails = class {
|
|
|
297
471
|
);
|
|
298
472
|
}
|
|
299
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
|
+
};
|
|
300
549
|
/**
|
|
301
550
|
* The durable wait — the one thing an agent cannot build for
|
|
302
551
|
* itself.
|
|
@@ -412,7 +661,12 @@ var Flowmails = class {
|
|
|
412
661
|
const code = payload.error ?? "request_failed";
|
|
413
662
|
if (res.status === 401) throw new AuthenticationError(code, res.status);
|
|
414
663
|
if (res.status === 400 || res.status === 404 || res.status === 422 || res.status === 409) {
|
|
415
|
-
throw new ValidationError(
|
|
664
|
+
throw new ValidationError(
|
|
665
|
+
code,
|
|
666
|
+
payload.detail,
|
|
667
|
+
res.status,
|
|
668
|
+
readErrorExtra(payload)
|
|
669
|
+
);
|
|
416
670
|
}
|
|
417
671
|
if (res.status === 429) {
|
|
418
672
|
const facts = readQuotaFacts(res, payload);
|
|
@@ -458,6 +712,14 @@ function readQuotaFacts(res, payload) {
|
|
|
458
712
|
}
|
|
459
713
|
return { limit, remaining, resetAt };
|
|
460
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
|
+
}
|
|
461
723
|
function buildUrl(origin, path, query) {
|
|
462
724
|
const url = new URL(`${origin}${path}`);
|
|
463
725
|
if (query) {
|
|
@@ -484,10 +746,12 @@ function sleep(ms) {
|
|
|
484
746
|
}
|
|
485
747
|
|
|
486
748
|
exports.AuthenticationError = AuthenticationError;
|
|
749
|
+
exports.DRAFT_STATUS_NAMES = DRAFT_STATUS_NAMES;
|
|
487
750
|
exports.Flowmails = Flowmails;
|
|
488
751
|
exports.FlowmailsError = FlowmailsError;
|
|
489
752
|
exports.RateLimitError = RateLimitError;
|
|
490
753
|
exports.UpstreamError = UpstreamError;
|
|
491
754
|
exports.ValidationError = ValidationError;
|
|
755
|
+
exports.WAIT_RESOLVED_EVENT = WAIT_RESOLVED_EVENT;
|
|
492
756
|
//# sourceMappingURL=index.cjs.map
|
|
493
757
|
//# sourceMappingURL=index.cjs.map
|