@moonbooth/sdk 0.0.0-stage → 0.2.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.ts +1776 -0
- package/dist/index.js +1098 -0
- package/package.json +25 -3
- package/README.md +0 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1098 @@
|
|
|
1
|
+
// TypeScript SDK for the ACT hosted API.
|
|
2
|
+
//
|
|
3
|
+
// ActClient wraps the REST API under /api/v1. It authenticates with a
|
|
4
|
+
// personal API token (Authorization: Bearer <token>). Create a token on
|
|
5
|
+
// the dashboard of the hosted service. A token carries the owning
|
|
6
|
+
// user's identity only; there are no scopes in v0.1.
|
|
7
|
+
//
|
|
8
|
+
// Response shapes mirror the JSON returned by the route handlers in
|
|
9
|
+
// apps/web/src/app/api/v1.
|
|
10
|
+
export const PACKAGE_NAME = "@moonbooth/sdk";
|
|
11
|
+
/** Structured error returned by the API: { error: { code, message, details? } }. */
|
|
12
|
+
export class ActApiError extends Error {
|
|
13
|
+
status;
|
|
14
|
+
code;
|
|
15
|
+
details;
|
|
16
|
+
constructor(status, code, message, details) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "ActApiError";
|
|
19
|
+
this.status = status;
|
|
20
|
+
this.code = code;
|
|
21
|
+
this.details = details;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
// Client
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
export class ActClient {
|
|
28
|
+
baseUrl;
|
|
29
|
+
token;
|
|
30
|
+
fetchFn;
|
|
31
|
+
constructor(options) {
|
|
32
|
+
if (!options.baseUrl)
|
|
33
|
+
throw new Error("ActClient requires a baseUrl.");
|
|
34
|
+
if (!options.token)
|
|
35
|
+
throw new Error("ActClient requires a token.");
|
|
36
|
+
this.baseUrl = options.baseUrl.replace(/\/+$/, "");
|
|
37
|
+
this.token = options.token;
|
|
38
|
+
this.fetchFn = options.fetchFn ?? ((url, init) => fetch(url, init));
|
|
39
|
+
}
|
|
40
|
+
/** The service base URL this client was constructed with. */
|
|
41
|
+
get serviceBaseUrl() {
|
|
42
|
+
return this.baseUrl;
|
|
43
|
+
}
|
|
44
|
+
async request(method, path, body) {
|
|
45
|
+
const init = {
|
|
46
|
+
method,
|
|
47
|
+
headers: {
|
|
48
|
+
authorization: `Bearer ${this.token}`,
|
|
49
|
+
...(body !== undefined ? { "content-type": "application/json" } : {}),
|
|
50
|
+
},
|
|
51
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
52
|
+
};
|
|
53
|
+
const res = await this.fetchFn(`${this.baseUrl}${path}`, init);
|
|
54
|
+
const data = (await res.json().catch(() => ({})));
|
|
55
|
+
if (!res.ok) {
|
|
56
|
+
const err = (data?.error ?? {});
|
|
57
|
+
throw new ActApiError(res.status, err.code ?? "unknown_error", err.message ?? `Request failed with status ${res.status}.`, err.details);
|
|
58
|
+
}
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
61
|
+
// Capabilities -----------------------------------------------------------
|
|
62
|
+
async createCapability(input) {
|
|
63
|
+
return this.request("POST", "/api/v1/capabilities", input);
|
|
64
|
+
}
|
|
65
|
+
async listCapabilities() {
|
|
66
|
+
return this.request("GET", "/api/v1/capabilities");
|
|
67
|
+
}
|
|
68
|
+
async getCapability(id) {
|
|
69
|
+
return this.request("GET", `/api/v1/capabilities/${encodeURIComponent(id)}`);
|
|
70
|
+
}
|
|
71
|
+
// Transfers --------------------------------------------------------------
|
|
72
|
+
async createTransfer(input) {
|
|
73
|
+
return this.request("POST", "/api/v1/transfers", input);
|
|
74
|
+
}
|
|
75
|
+
async getTransfer(id) {
|
|
76
|
+
return this.request("GET", `/api/v1/transfers/${encodeURIComponent(id)}`);
|
|
77
|
+
}
|
|
78
|
+
async listTransfers() {
|
|
79
|
+
return this.request("GET", "/api/v1/transfers");
|
|
80
|
+
}
|
|
81
|
+
/** Sender approval. The digest must be the digest the sender reviewed. */
|
|
82
|
+
async approveTransfer(id, digest, recipientEmail) {
|
|
83
|
+
return this.request("POST", `/api/v1/transfers/${encodeURIComponent(id)}/approve`, {
|
|
84
|
+
digest,
|
|
85
|
+
...(recipientEmail ? { recipientEmail } : {}),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async revokeTransfer(id) {
|
|
89
|
+
return this.request("POST", `/api/v1/transfers/${encodeURIComponent(id)}/revoke`, {});
|
|
90
|
+
}
|
|
91
|
+
async getPackage(id) {
|
|
92
|
+
return this.request("GET", `/api/v1/transfers/${encodeURIComponent(id)}/package`);
|
|
93
|
+
}
|
|
94
|
+
async acceptTransfer(id) {
|
|
95
|
+
return this.request("POST", `/api/v1/transfers/${encodeURIComponent(id)}/accept`, {});
|
|
96
|
+
}
|
|
97
|
+
async rejectTransfer(id, reason) {
|
|
98
|
+
return this.request("POST", `/api/v1/transfers/${encodeURIComponent(id)}/reject`, {
|
|
99
|
+
...(reason ? { reason } : {}),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
// Invitations --------------------------------------------------------------
|
|
103
|
+
async getOffer(token) {
|
|
104
|
+
return this.request("GET", `/api/v1/invitations/${encodeURIComponent(token)}`);
|
|
105
|
+
}
|
|
106
|
+
async receiveInvitation(token) {
|
|
107
|
+
return this.request("POST", `/api/v1/invitations/${encodeURIComponent(token)}/receive`, {});
|
|
108
|
+
}
|
|
109
|
+
// Share links --------------------------------------------------------------
|
|
110
|
+
/**
|
|
111
|
+
* Create a share link. Name a transferId (the transfer must already
|
|
112
|
+
* be approved and offered; an unapproved transfer fails with an
|
|
113
|
+
* ActApiError whose code is "needs_approval" and whose details carry
|
|
114
|
+
* the package digest and a contents summary), or a
|
|
115
|
+
* capabilityVersionId with the digest the sender reviewed (approval
|
|
116
|
+
* is recorded as part of creation).
|
|
117
|
+
*/
|
|
118
|
+
async createShareLink(input) {
|
|
119
|
+
return this.request("POST", "/api/v1/links", input);
|
|
120
|
+
}
|
|
121
|
+
async listShareLinks() {
|
|
122
|
+
return this.request("GET", "/api/v1/links");
|
|
123
|
+
}
|
|
124
|
+
async revokeShareLink(id) {
|
|
125
|
+
return this.request("POST", "/api/v1/links/revoke", { id });
|
|
126
|
+
}
|
|
127
|
+
/** Read the public rendezvous document for a link token. */
|
|
128
|
+
async inspectShareLink(token) {
|
|
129
|
+
return this.request("GET", `/api/v1/links/${encodeURIComponent(token)}`);
|
|
130
|
+
}
|
|
131
|
+
/** Claim a link as the token owner (an account claim). */
|
|
132
|
+
async claimShareLink(token) {
|
|
133
|
+
return this.request("POST", `/api/v1/links/${encodeURIComponent(token)}/claim`, {});
|
|
134
|
+
}
|
|
135
|
+
// Verification -------------------------------------------------------------
|
|
136
|
+
async requestVerification(transferId, profile) {
|
|
137
|
+
return this.request("POST", `/api/v1/transfers/${encodeURIComponent(transferId)}/verify`, profile !== undefined ? { profile } : {});
|
|
138
|
+
}
|
|
139
|
+
async getVerification(transferId) {
|
|
140
|
+
return this.request("GET", `/api/v1/transfers/${encodeURIComponent(transferId)}/verification`);
|
|
141
|
+
}
|
|
142
|
+
async getReceipt(receiptId) {
|
|
143
|
+
return this.request("GET", `/api/v1/receipts/${encodeURIComponent(receiptId)}`);
|
|
144
|
+
}
|
|
145
|
+
// Webhooks ---------------------------------------------------------------
|
|
146
|
+
//
|
|
147
|
+
// Account-owned outbound event delivery (Living Network Phase 2).
|
|
148
|
+
// A webhook registers a target URL and an event filter; the
|
|
149
|
+
// service POSTs a signed envelope when a matching event lands in a
|
|
150
|
+
// covered booth. The signing secret is returned once, by create
|
|
151
|
+
// and by rotateWebhookSecret; no other method returns it.
|
|
152
|
+
/** Register a webhook. The returned secret is shown once: store it now. */
|
|
153
|
+
async createWebhook(input) {
|
|
154
|
+
return this.request("POST", "/api/v1/webhooks", {
|
|
155
|
+
url: input.url,
|
|
156
|
+
...(input.eventTypes !== undefined ? { event_types: input.eventTypes } : {}),
|
|
157
|
+
...(input.boothId !== undefined ? { booth_id: input.boothId } : {}),
|
|
158
|
+
...(input.participantCredential !== undefined
|
|
159
|
+
? { participant_credential: input.participantCredential }
|
|
160
|
+
: {}),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async listWebhooks() {
|
|
164
|
+
return this.request("GET", "/api/v1/webhooks");
|
|
165
|
+
}
|
|
166
|
+
async getWebhook(id) {
|
|
167
|
+
return this.request("GET", `/api/v1/webhooks/${encodeURIComponent(id)}`);
|
|
168
|
+
}
|
|
169
|
+
async updateWebhook(id, patch) {
|
|
170
|
+
return this.request("PATCH", `/api/v1/webhooks/${encodeURIComponent(id)}`, {
|
|
171
|
+
...(patch.url !== undefined ? { url: patch.url } : {}),
|
|
172
|
+
...(patch.eventTypes !== undefined ? { event_types: patch.eventTypes } : {}),
|
|
173
|
+
...(patch.status !== undefined ? { status: patch.status } : {}),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/** Replace the signing secret. The new secret is shown once. */
|
|
177
|
+
async rotateWebhookSecret(id) {
|
|
178
|
+
return this.request("POST", `/api/v1/webhooks/${encodeURIComponent(id)}/rotate-secret`, {});
|
|
179
|
+
}
|
|
180
|
+
async deleteWebhook(id) {
|
|
181
|
+
return this.request("DELETE", `/api/v1/webhooks/${encodeURIComponent(id)}`);
|
|
182
|
+
}
|
|
183
|
+
/** Enqueue one signed test delivery to the webhook's target. */
|
|
184
|
+
async testWebhook(id) {
|
|
185
|
+
return this.request("POST", `/api/v1/webhooks/${encodeURIComponent(id)}/test`, {});
|
|
186
|
+
}
|
|
187
|
+
async listWebhookDeliveries(id, limit) {
|
|
188
|
+
const query = limit !== undefined ? `?limit=${encodeURIComponent(String(limit))}` : "";
|
|
189
|
+
return this.request("GET", `/api/v1/webhooks/${encodeURIComponent(id)}/deliveries${query}`);
|
|
190
|
+
}
|
|
191
|
+
// Persistent booths, namespaces, aliases ---------------------------------
|
|
192
|
+
//
|
|
193
|
+
// Living Network Phase 7. A persistent booth is account-owned and
|
|
194
|
+
// does not expire; the account administers it through these methods
|
|
195
|
+
// (an API token carries exactly its owner's powers over its owner's
|
|
196
|
+
// own booths). The account claims one namespace; a persistent booth
|
|
197
|
+
// carries at most one alias inside it. An alias is a locator, never
|
|
198
|
+
// a credential: resolving one returns the booth's public discovery
|
|
199
|
+
// document.
|
|
200
|
+
/** The account's namespace, or null when none is claimed. */
|
|
201
|
+
async getNamespace() {
|
|
202
|
+
return this.request("GET", "/api/v1/account/namespace");
|
|
203
|
+
}
|
|
204
|
+
/** Claim the account's one namespace. */
|
|
205
|
+
async claimNamespace(slug) {
|
|
206
|
+
return this.request("POST", "/api/v1/account/namespace", { slug });
|
|
207
|
+
}
|
|
208
|
+
/** Rename the namespace (at most once every 30 days). */
|
|
209
|
+
async renameNamespace(slug) {
|
|
210
|
+
return this.request("PATCH", "/api/v1/account/namespace", { slug });
|
|
211
|
+
}
|
|
212
|
+
/** Create a persistent booth. The creator credential is shown once. */
|
|
213
|
+
async createPersistentBooth(input = {}) {
|
|
214
|
+
return this.request("POST", "/api/v1/account/booths", {
|
|
215
|
+
...(input.displayName !== undefined ? { display_name: input.displayName } : {}),
|
|
216
|
+
...(input.objective !== undefined ? { objective: input.objective } : {}),
|
|
217
|
+
...(input.accessMode !== undefined ? { access_mode: input.accessMode } : {}),
|
|
218
|
+
...(input.password !== undefined ? { password: input.password } : {}),
|
|
219
|
+
...(input.invitations !== undefined ? { invitations: input.invitations } : {}),
|
|
220
|
+
...(input.parentBoothId !== undefined ? { parent_booth_id: input.parentBoothId } : {}),
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
async listOwnedBooths() {
|
|
224
|
+
return this.request("GET", "/api/v1/account/booths");
|
|
225
|
+
}
|
|
226
|
+
async getOwnedBooth(id) {
|
|
227
|
+
return this.request("GET", `/api/v1/account/booths/${encodeURIComponent(id)}`);
|
|
228
|
+
}
|
|
229
|
+
async configureOwnedBooth(id, patch) {
|
|
230
|
+
return this.request("PATCH", `/api/v1/account/booths/${encodeURIComponent(id)}`, {
|
|
231
|
+
...(patch.displayName !== undefined ? { display_name: patch.displayName } : {}),
|
|
232
|
+
...(patch.objective !== undefined ? { objective: patch.objective } : {}),
|
|
233
|
+
...(patch.limits !== undefined
|
|
234
|
+
? {
|
|
235
|
+
limits: {
|
|
236
|
+
...(patch.limits.maxParticipants !== undefined
|
|
237
|
+
? { max_participants: patch.limits.maxParticipants }
|
|
238
|
+
: {}),
|
|
239
|
+
...(patch.limits.retentionDays !== undefined
|
|
240
|
+
? { retention_days: patch.limits.retentionDays }
|
|
241
|
+
: {}),
|
|
242
|
+
},
|
|
243
|
+
}
|
|
244
|
+
: {}),
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
/** Delete a persistent booth. Records are retained; the booth stops resolving. */
|
|
248
|
+
async deleteOwnedBooth(id) {
|
|
249
|
+
return this.request("DELETE", `/api/v1/account/booths/${encodeURIComponent(id)}`);
|
|
250
|
+
}
|
|
251
|
+
async closeOwnedBooth(id) {
|
|
252
|
+
return this.request("POST", `/api/v1/account/booths/${encodeURIComponent(id)}/close`, {});
|
|
253
|
+
}
|
|
254
|
+
async archiveOwnedBooth(id) {
|
|
255
|
+
return this.request("POST", `/api/v1/account/booths/${encodeURIComponent(id)}/archive`, {});
|
|
256
|
+
}
|
|
257
|
+
async restoreOwnedBooth(id) {
|
|
258
|
+
return this.request("POST", `/api/v1/account/booths/${encodeURIComponent(id)}/restore`, {});
|
|
259
|
+
}
|
|
260
|
+
/** Rotate every live participant credential. The new credentials are shown once. */
|
|
261
|
+
async rotateOwnedBoothCredentials(id) {
|
|
262
|
+
return this.request("POST", `/api/v1/account/booths/${encodeURIComponent(id)}/rotate-credentials`, {});
|
|
263
|
+
}
|
|
264
|
+
async revokeOwnedBoothParticipant(id, participantId) {
|
|
265
|
+
return this.request("DELETE", `/api/v1/account/booths/${encodeURIComponent(id)}/participants/${encodeURIComponent(participantId)}`);
|
|
266
|
+
}
|
|
267
|
+
/** The full booth record export (works on a deleted booth too). */
|
|
268
|
+
async exportOwnedBooth(id) {
|
|
269
|
+
return this.request("GET", `/api/v1/account/booths/${encodeURIComponent(id)}/export`);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Phase 10: read the booth's publication policy — what this
|
|
273
|
+
* booth publishes to other booths. A null publication publishes
|
|
274
|
+
* nothing. Only account-owned booths can be cross-booth sources.
|
|
275
|
+
*/
|
|
276
|
+
async getPublicationPolicy(id) {
|
|
277
|
+
return this.request("GET", `/api/v1/account/booths/${encodeURIComponent(id)}/publication`);
|
|
278
|
+
}
|
|
279
|
+
/** Set (replace in full) the booth's publication policy. */
|
|
280
|
+
async setPublicationPolicy(id, input) {
|
|
281
|
+
return this.request("PUT", `/api/v1/account/booths/${encodeURIComponent(id)}/publication`, {
|
|
282
|
+
...(input.subjects !== undefined ? { subjects: input.subjects } : {}),
|
|
283
|
+
...(input.eventTypes !== undefined ? { event_types: input.eventTypes } : {}),
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
/** Clear the booth's publication policy; the booth publishes nothing. */
|
|
287
|
+
async clearPublicationPolicy(id) {
|
|
288
|
+
return this.request("DELETE", `/api/v1/account/booths/${encodeURIComponent(id)}/publication`);
|
|
289
|
+
}
|
|
290
|
+
async getBoothAlias(id) {
|
|
291
|
+
return this.request("GET", `/api/v1/account/booths/${encodeURIComponent(id)}/alias`);
|
|
292
|
+
}
|
|
293
|
+
async claimBoothAlias(id, slug) {
|
|
294
|
+
return this.request("POST", `/api/v1/account/booths/${encodeURIComponent(id)}/alias`, { slug });
|
|
295
|
+
}
|
|
296
|
+
async renameBoothAlias(id, slug) {
|
|
297
|
+
return this.request("PATCH", `/api/v1/account/booths/${encodeURIComponent(id)}/alias`, { slug });
|
|
298
|
+
}
|
|
299
|
+
async releaseBoothAlias(id) {
|
|
300
|
+
return this.request("DELETE", `/api/v1/account/booths/${encodeURIComponent(id)}/alias`);
|
|
301
|
+
}
|
|
302
|
+
/** Resolve an alias (@namespace/slug) to its booth's public discovery document. */
|
|
303
|
+
async resolveAlias(namespace, slug) {
|
|
304
|
+
return this.request("GET", `/api/v1/aliases/${encodeURIComponent(namespace)}/${encodeURIComponent(slug)}`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** Parse one server-sent event block (the text between blank lines). */
|
|
308
|
+
export function parseStreamFrame(raw) {
|
|
309
|
+
let id;
|
|
310
|
+
let event = "message";
|
|
311
|
+
const dataLines = [];
|
|
312
|
+
for (const line of raw.split("\n")) {
|
|
313
|
+
if (line.startsWith(":"))
|
|
314
|
+
continue; // comment / heartbeat
|
|
315
|
+
if (line.startsWith("id:"))
|
|
316
|
+
id = line.slice(3).trim();
|
|
317
|
+
else if (line.startsWith("event:"))
|
|
318
|
+
event = line.slice(6).trim();
|
|
319
|
+
else if (line.startsWith("data:"))
|
|
320
|
+
dataLines.push(line.slice(5).trim());
|
|
321
|
+
}
|
|
322
|
+
if (dataLines.length === 0)
|
|
323
|
+
return null;
|
|
324
|
+
let data;
|
|
325
|
+
try {
|
|
326
|
+
data = JSON.parse(dataLines.join("\n"));
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
if (event === "stream_open") {
|
|
332
|
+
return {
|
|
333
|
+
type: "open",
|
|
334
|
+
booth: String(data.booth ?? ""),
|
|
335
|
+
since: Number(data.since ?? 0),
|
|
336
|
+
mode: String(data.mode ?? "relevant"),
|
|
337
|
+
latest_seq: Number(data.latest_seq ?? 0),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
if (event === "stream_end") {
|
|
341
|
+
return {
|
|
342
|
+
type: "end",
|
|
343
|
+
reason: String(data.reason ?? ""),
|
|
344
|
+
next_since: Number(data.next_since ?? 0),
|
|
345
|
+
latest_seq: Number(data.latest_seq ?? 0),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
if (event === "stream_error") {
|
|
349
|
+
return { type: "error", code: String(data.code ?? "stream_error") };
|
|
350
|
+
}
|
|
351
|
+
if (event === "booth_event") {
|
|
352
|
+
const entry = data;
|
|
353
|
+
return { type: "event", seq: Number(id ?? entry.seq ?? 0), event: entry };
|
|
354
|
+
}
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
function serializeInvitationSpec(spec) {
|
|
358
|
+
return {
|
|
359
|
+
...(spec.role ? { role: spec.role } : {}),
|
|
360
|
+
...(spec.scopes ? { scopes: spec.scopes } : {}),
|
|
361
|
+
...(spec.label ? { label: spec.label } : {}),
|
|
362
|
+
...(spec.subjectRestrictions ? { subject_restrictions: spec.subjectRestrictions } : {}),
|
|
363
|
+
...(spec.maxRedemptions !== undefined ? { max_redemptions: spec.maxRedemptions } : {}),
|
|
364
|
+
...(spec.expiresInSeconds !== undefined ? { expires_in_seconds: spec.expiresInSeconds } : {}),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
/** Create a booth. Anonymous: no credential of any kind is sent. */
|
|
368
|
+
export async function createBooth(baseUrl, input = {}, fetchFn) {
|
|
369
|
+
const doFetch = fetchFn ?? ((url, init) => fetch(url, init));
|
|
370
|
+
const body = {};
|
|
371
|
+
if (input.ttlSeconds !== undefined)
|
|
372
|
+
body.ttl_seconds = input.ttlSeconds;
|
|
373
|
+
if (input.closeCondition !== undefined)
|
|
374
|
+
body.close_condition = input.closeCondition;
|
|
375
|
+
if (input.access !== undefined) {
|
|
376
|
+
body.access = {
|
|
377
|
+
mode: input.access.mode,
|
|
378
|
+
...(input.access.method ? { method: input.access.method } : {}),
|
|
379
|
+
...(input.access.password ? { password: input.access.password } : {}),
|
|
380
|
+
...(input.access.defaultScopes ? { default_scopes: input.access.defaultScopes } : {}),
|
|
381
|
+
...(input.access.invitations
|
|
382
|
+
? { invitations: input.access.invitations.map(serializeInvitationSpec) }
|
|
383
|
+
: {}),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
if (input.parent !== undefined) {
|
|
387
|
+
body.parent = {
|
|
388
|
+
booth_id: input.parent.boothId,
|
|
389
|
+
...(input.parent.credential ? { credential: input.parent.credential } : {}),
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
const res = await doFetch(`${baseUrl.replace(/\/+$/, "")}/api/v1/booths`, {
|
|
393
|
+
method: "POST",
|
|
394
|
+
headers: { "content-type": "application/json" },
|
|
395
|
+
body: JSON.stringify(body),
|
|
396
|
+
});
|
|
397
|
+
const data = (await res.json().catch(() => ({})));
|
|
398
|
+
if (!res.ok) {
|
|
399
|
+
const err = (data?.error ?? {});
|
|
400
|
+
throw new ActApiError(res.status, err.code ?? "unknown_error", err.message ?? `Request failed with status ${res.status}.`, err.details);
|
|
401
|
+
}
|
|
402
|
+
return data;
|
|
403
|
+
}
|
|
404
|
+
export class BoothClient {
|
|
405
|
+
baseUrl;
|
|
406
|
+
boothId;
|
|
407
|
+
credential;
|
|
408
|
+
fetchFn;
|
|
409
|
+
constructor(options) {
|
|
410
|
+
if (!options.baseUrl)
|
|
411
|
+
throw new Error("BoothClient requires a baseUrl.");
|
|
412
|
+
if (!options.boothId)
|
|
413
|
+
throw new Error("BoothClient requires a boothId.");
|
|
414
|
+
this.baseUrl = options.baseUrl.replace(/\/+$/, "");
|
|
415
|
+
this.boothId = options.boothId;
|
|
416
|
+
this.credential = options.credential;
|
|
417
|
+
this.fetchFn = options.fetchFn ?? ((url, init) => fetch(url, init));
|
|
418
|
+
}
|
|
419
|
+
/** The credential this client sends, once a join has provided one. */
|
|
420
|
+
get currentCredential() {
|
|
421
|
+
return this.credential;
|
|
422
|
+
}
|
|
423
|
+
async request(method, suffix, body, credentialOverride) {
|
|
424
|
+
const credential = credentialOverride ?? this.credential;
|
|
425
|
+
const init = {
|
|
426
|
+
method,
|
|
427
|
+
headers: {
|
|
428
|
+
...(credential ? { authorization: `Bearer ${credential}` } : {}),
|
|
429
|
+
...(body !== undefined ? { "content-type": "application/json" } : {}),
|
|
430
|
+
},
|
|
431
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
432
|
+
};
|
|
433
|
+
const res = await this.fetchFn(`${this.baseUrl}/api/v1/booths/${encodeURIComponent(this.boothId)}${suffix}`, init);
|
|
434
|
+
const data = (await res.json().catch(() => ({})));
|
|
435
|
+
if (!res.ok) {
|
|
436
|
+
const err = (data?.error ?? {});
|
|
437
|
+
throw new ActApiError(res.status, err.code ?? "unknown_error", err.message ?? `Request failed with status ${res.status}.`, err.details);
|
|
438
|
+
}
|
|
439
|
+
return data;
|
|
440
|
+
}
|
|
441
|
+
/** The booth's discovery document (no credential required). */
|
|
442
|
+
async discovery() {
|
|
443
|
+
return this.request("GET", "");
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Phase 8: publish this child booth's results to its parent. This
|
|
447
|
+
* client's credential is the caller's credential in this booth
|
|
448
|
+
* (the child) and must hold events.read; parentCredential proves
|
|
449
|
+
* standing in the parent (a parent participant credential or the
|
|
450
|
+
* parent's creator credential). What travels is named records
|
|
451
|
+
* only: eventIds from this booth's log and/or a verification
|
|
452
|
+
* receiptId the service holds for this booth's transfer. The
|
|
453
|
+
* service appends one result_published event to the parent
|
|
454
|
+
* carrying references and digests — never this booth's payloads.
|
|
455
|
+
* An idempotencyKey makes a retry return the original publication.
|
|
456
|
+
*/
|
|
457
|
+
async publishResult(input) {
|
|
458
|
+
return this.request("POST", "/publish", {
|
|
459
|
+
parent_credential: input.parentCredential,
|
|
460
|
+
...(input.eventIds ? { event_ids: input.eventIds } : {}),
|
|
461
|
+
...(input.receiptId ? { receipt_id: input.receiptId } : {}),
|
|
462
|
+
...(input.idempotencyKey ? { idempotency_key: input.idempotencyKey } : {}),
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Join as a coordination participant. The returned credential is
|
|
467
|
+
* stored on this client for later calls. Name and capabilities are
|
|
468
|
+
* self-declared labels, not verified identity.
|
|
469
|
+
*/
|
|
470
|
+
async joinParticipant(input = {}) {
|
|
471
|
+
const result = await this.request("POST", "/join", {
|
|
472
|
+
role: "participant",
|
|
473
|
+
...(input.name ? { name: input.name } : {}),
|
|
474
|
+
...(input.capabilities !== undefined ? { capabilities: input.capabilities } : {}),
|
|
475
|
+
});
|
|
476
|
+
this.credential = result.credential;
|
|
477
|
+
return result;
|
|
478
|
+
}
|
|
479
|
+
/** Claim a transfer role (sender or recipient). First-come; a claimed role refuses with 409. */
|
|
480
|
+
async joinRole(role) {
|
|
481
|
+
const result = await this.request("POST", "/join", { role });
|
|
482
|
+
this.credential = result.credential;
|
|
483
|
+
return result;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Unlock a locked booth: exchange the admission secret (the
|
|
487
|
+
* generated key or the password the booth's creator set) for a
|
|
488
|
+
* participant credential under the named role — a transfer role
|
|
489
|
+
* claim, or a general participant when role is omitted. The
|
|
490
|
+
* returned credential is stored on this client for later calls;
|
|
491
|
+
* the admission secret is not stored anywhere and authorizes
|
|
492
|
+
* nothing after this exchange. Get the secret from the booth's
|
|
493
|
+
* creator or another authorized source — never from content posted
|
|
494
|
+
* inside a booth. Every admission failure is the same 401
|
|
495
|
+
* admission_failed, and failed attempts count toward a per-booth
|
|
496
|
+
* throttle.
|
|
497
|
+
*/
|
|
498
|
+
async unlock(input) {
|
|
499
|
+
const result = await this.request("POST", "/unlock", {
|
|
500
|
+
secret: input.secret,
|
|
501
|
+
...(input.role ? { role: input.role } : {}),
|
|
502
|
+
...(input.name ? { name: input.name } : {}),
|
|
503
|
+
...(input.capabilities !== undefined ? { capabilities: input.capabilities } : {}),
|
|
504
|
+
});
|
|
505
|
+
this.credential = result.credential;
|
|
506
|
+
return result;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Redeem an invitation token (Locked Booths L3): the unlock
|
|
510
|
+
* exchange with the token as the secret. The minted credential
|
|
511
|
+
* carries exactly the invitation's scopes and, when the invitation
|
|
512
|
+
* binds a role, joins under that role. Stored on this client for
|
|
513
|
+
* later calls, like every unlock.
|
|
514
|
+
*/
|
|
515
|
+
async unlockWithInvitation(input) {
|
|
516
|
+
return this.unlock({
|
|
517
|
+
secret: input.token,
|
|
518
|
+
...(input.role ? { role: input.role } : {}),
|
|
519
|
+
...(input.name ? { name: input.name } : {}),
|
|
520
|
+
...(input.capabilities !== undefined ? { capabilities: input.capabilities } : {}),
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Issue one invitation for this booth (L3) and return it with its
|
|
525
|
+
* one-time token. The credential defaults to this client's: the
|
|
526
|
+
* creator credential issues from the full delegable vocabulary; a
|
|
527
|
+
* participant credential whose grant carries participants.invite
|
|
528
|
+
* issues from its own scopes. Pass a credential to act under a
|
|
529
|
+
* different grant than the client's — for example the creator
|
|
530
|
+
* credential on a client that has not joined.
|
|
531
|
+
*/
|
|
532
|
+
async createInvitation(spec = {}, credential) {
|
|
533
|
+
return this.request("POST", "/invitations", serializeInvitationSpec(spec), credential);
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* List this booth's invitations, metadata only — no token or hash
|
|
537
|
+
* is ever returned. Creator-management grant required: pass the
|
|
538
|
+
* creator credential, or construct the client with it.
|
|
539
|
+
*/
|
|
540
|
+
async listInvitations(credential) {
|
|
541
|
+
return this.request("GET", "/invitations", undefined, credential);
|
|
542
|
+
}
|
|
543
|
+
/** Revoke one invitation, effective immediately. Creator-management grant required. */
|
|
544
|
+
async revokeInvitation(invitationId, credential) {
|
|
545
|
+
return this.request("DELETE", `/invitations/${encodeURIComponent(invitationId)}`, undefined, credential);
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Rotate this booth's admission secret (Locked Booths L4). The old
|
|
549
|
+
* secret stops admitting new joins the moment this returns; the
|
|
550
|
+
* new generated key is shown once, in the result. Participants who
|
|
551
|
+
* already joined keep their credentials and access — rotation is
|
|
552
|
+
* about admission, not eviction. Omitting method keeps the booth's
|
|
553
|
+
* current method; a password rotation (or a switch to password)
|
|
554
|
+
* requires the new password, which the service never echoes.
|
|
555
|
+
* Creator-management grant required: pass the creator credential,
|
|
556
|
+
* or construct the client with it.
|
|
557
|
+
*/
|
|
558
|
+
async rotateAdmissionSecret(input = {}, credential) {
|
|
559
|
+
return this.request("POST", "/rotate-secret", { ...input }, credential);
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Protect this booth (Locked Booths L5): lock a booth that was
|
|
563
|
+
* created link-only, applying the create-time admission machinery
|
|
564
|
+
* to it atomically — the verifier and the locked policy land
|
|
565
|
+
* together or not at all. Omitting method mints a generated key,
|
|
566
|
+
* shown once in the result; a password lock requires the password,
|
|
567
|
+
* which the service never echoes. Participants who joined while
|
|
568
|
+
* the booth was link-only keep their credentials and access —
|
|
569
|
+
* locking closes admission behind them, it does not remove them.
|
|
570
|
+
* A booth that is already locked (or invitation-only) refuses.
|
|
571
|
+
* Creator-management grant required: pass the creator credential,
|
|
572
|
+
* or construct the client with it.
|
|
573
|
+
*/
|
|
574
|
+
async protectBooth(input = {}, credential) {
|
|
575
|
+
return this.request("POST", "/protect", { ...input }, credential);
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* List this booth's participant grants, metadata only — no
|
|
579
|
+
* credential or hash is ever returned. Creator-management grant
|
|
580
|
+
* required: pass the creator credential, or construct the client
|
|
581
|
+
* with it.
|
|
582
|
+
*/
|
|
583
|
+
async listParticipants(credential) {
|
|
584
|
+
return this.request("GET", "/participants", undefined, credential);
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Revoke one participant grant (L4), effective on the revoked
|
|
588
|
+
* credential's very next call. The participant's browser sessions
|
|
589
|
+
* and subscription row are revoked in the same step; revoking a
|
|
590
|
+
* transfer-role grant also releases the role claim.
|
|
591
|
+
* Creator-management grant required — participants.revoke is
|
|
592
|
+
* non-delegable, so no participant credential authorizes this.
|
|
593
|
+
*/
|
|
594
|
+
async revokeParticipant(participantId, credential) {
|
|
595
|
+
return this.request("DELETE", `/participants/${encodeURIComponent(participantId)}`, undefined, credential);
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Revoke every live grant one invitation issued (L4) — the
|
|
599
|
+
* distinct companion to revokeInvitation, which stops new
|
|
600
|
+
* redemptions and leaves issued grants standing. Each grant dies
|
|
601
|
+
* exactly as if revoked directly. Creator-management grant
|
|
602
|
+
* required.
|
|
603
|
+
*/
|
|
604
|
+
async revokeInvitationGrants(invitationId, credential) {
|
|
605
|
+
return this.request("POST", `/invitations/${encodeURIComponent(invitationId)}/revoke-grants`, {}, credential);
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* File an access request against this booth (L4): a bounded note
|
|
609
|
+
* for the booth's creator from a caller who cannot unlock it. No
|
|
610
|
+
* credential is sent — the filer is precisely a caller without
|
|
611
|
+
* one — and the request grants nothing and admits no one. The
|
|
612
|
+
* creator acts on it, if at all, by issuing an invitation.
|
|
613
|
+
*/
|
|
614
|
+
async requestAccess(input = {}) {
|
|
615
|
+
// The empty-string override suppresses the client's credential:
|
|
616
|
+
// an access request is filed by a caller who has none.
|
|
617
|
+
return this.request("POST", "/access-requests", { ...input }, "");
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* List this booth's access requests in filing order.
|
|
621
|
+
* Creator-management grant required: pass the creator credential,
|
|
622
|
+
* or construct the client with it.
|
|
623
|
+
*/
|
|
624
|
+
async listAccessRequests(credential) {
|
|
625
|
+
return this.request("GET", "/access-requests", undefined, credential);
|
|
626
|
+
}
|
|
627
|
+
async listEvents(options = {}) {
|
|
628
|
+
const q = new URLSearchParams();
|
|
629
|
+
if (options.since !== undefined)
|
|
630
|
+
q.set("since", String(options.since));
|
|
631
|
+
if (options.type)
|
|
632
|
+
q.set("type", options.type);
|
|
633
|
+
if (options.subject)
|
|
634
|
+
q.set("subject", options.subject);
|
|
635
|
+
const qs = q.toString();
|
|
636
|
+
return this.request("GET", `/events${qs ? `?${qs}` : ""}`);
|
|
637
|
+
}
|
|
638
|
+
async appendEvent(input) {
|
|
639
|
+
return this.request("POST", "/events", {
|
|
640
|
+
type: input.type,
|
|
641
|
+
subject: input.subject,
|
|
642
|
+
payload: input.payload,
|
|
643
|
+
...(input.idempotencyKey ? { idempotency_key: input.idempotencyKey } : {}),
|
|
644
|
+
...(input.inReplyTo ? { in_reply_to: input.inReplyTo } : {}),
|
|
645
|
+
...(input.relatedIds ? { related_ids: input.relatedIds } : {}),
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
async getState() {
|
|
649
|
+
return this.request("GET", "/state");
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Propose a commitment (Living Network Phase 3): a commitment record in
|
|
653
|
+
* the booth log. The named parties endorse it with acceptCommitment —
|
|
654
|
+
* never the proposer; endorsement here means a second credential
|
|
655
|
+
* stood behind the commitment, and "authorized" is reserved for account
|
|
656
|
+
* holders. Fulfillment is computed from the log: every named
|
|
657
|
+
* party endorsed and every checkable condition met.
|
|
658
|
+
*/
|
|
659
|
+
async proposeCommitment(input) {
|
|
660
|
+
return this.appendEvent({
|
|
661
|
+
type: "commitment",
|
|
662
|
+
subject: input.subject ?? input.subjects?.[0] ?? "commitment",
|
|
663
|
+
payload: {
|
|
664
|
+
title: input.title,
|
|
665
|
+
...(input.parties ? { parties: input.parties } : {}),
|
|
666
|
+
...(input.commitments ? { commitments: input.commitments } : {}),
|
|
667
|
+
...(input.conditions ? { conditions: input.conditions } : {}),
|
|
668
|
+
...(input.dueAt ? { due_at: input.dueAt } : {}),
|
|
669
|
+
...(input.subjects ? { subjects: input.subjects } : {}),
|
|
670
|
+
},
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Endorse a commitment. Refused from the commitment's proposer (403
|
|
675
|
+
* commitment_self_accept), mirroring decision acceptance: an
|
|
676
|
+
* endorsement is a second participant standing behind the commitment.
|
|
677
|
+
*/
|
|
678
|
+
async acceptCommitment(commitmentEventId) {
|
|
679
|
+
return this.appendEvent({
|
|
680
|
+
type: "commitment_accept",
|
|
681
|
+
subject: "commitment",
|
|
682
|
+
payload: { commitment_event_id: commitmentEventId },
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Cancel a commitment. Only the commitment's proposer or one of its endorsers
|
|
687
|
+
* may cancel (403 commitment_cancel_forbidden otherwise); a cancelled
|
|
688
|
+
* commitment is terminal.
|
|
689
|
+
*/
|
|
690
|
+
async cancelCommitment(commitmentEventId) {
|
|
691
|
+
return this.appendEvent({
|
|
692
|
+
type: "commitment_cancel",
|
|
693
|
+
subject: "commitment",
|
|
694
|
+
payload: { commitment_event_id: commitmentEventId },
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
/** The booth's commitments with their computed statuses, from the state document. */
|
|
698
|
+
async listCommitments() {
|
|
699
|
+
const state = await this.getState();
|
|
700
|
+
return state.commitments;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Declare a check (Living Network Phase 5): a set of typed
|
|
704
|
+
* assertions the service evaluates against the booth's log. The
|
|
705
|
+
* declaration is an ordinary log event; the result is computed
|
|
706
|
+
* on read by getCheck / listChecks and is never stored, so the
|
|
707
|
+
* same declaration re-evaluates as evidence lands. The service
|
|
708
|
+
* never runs package code — assertions are data, evaluated by
|
|
709
|
+
* the service's deterministic evaluator.
|
|
710
|
+
*/
|
|
711
|
+
async declareCheck(input) {
|
|
712
|
+
return this.appendEvent({
|
|
713
|
+
type: "check",
|
|
714
|
+
subject: input.subject ?? "check",
|
|
715
|
+
payload: {
|
|
716
|
+
title: input.title,
|
|
717
|
+
...(input.target ? { target: input.target } : {}),
|
|
718
|
+
...(input.assertions ? { assertions: input.assertions } : {}),
|
|
719
|
+
},
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
/** The booth's checks with their computed results. */
|
|
723
|
+
async listChecks() {
|
|
724
|
+
const result = await this.request("GET", "/checks");
|
|
725
|
+
return result.checks;
|
|
726
|
+
}
|
|
727
|
+
/** One check's computed result, by the check event's id. */
|
|
728
|
+
async getCheck(checkEventId) {
|
|
729
|
+
return this.request("GET", `/checks/${encodeURIComponent(checkEventId)}`);
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Set or clear the booth's close condition (Phase 3). Pass
|
|
733
|
+
* "commitment_terminal:<commitment event id>" to close the booth when the
|
|
734
|
+
* named commitment reaches a terminal state, "transfer_accepted" or
|
|
735
|
+
* "all_acks" for the standing conditions, or null to clear.
|
|
736
|
+
* Creator-management grant required: pass the creator credential,
|
|
737
|
+
* or construct the client with it.
|
|
738
|
+
*/
|
|
739
|
+
async setCloseCondition(closeCondition, credential) {
|
|
740
|
+
return this.request("POST", "/close-condition", { close_condition: closeCondition }, credential);
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* The changes feed: events relevant to this participant after the
|
|
744
|
+
* cursor, with reasons. waitSeconds holds the request open for a
|
|
745
|
+
* relevant event (25 seconds at most) and returns early when one
|
|
746
|
+
* lands. Delivery is at-least-once; resume with next_since.
|
|
747
|
+
*/
|
|
748
|
+
async getChanges(options = {}) {
|
|
749
|
+
const q = new URLSearchParams();
|
|
750
|
+
if (options.since !== undefined)
|
|
751
|
+
q.set("since", String(options.since));
|
|
752
|
+
if (options.until !== undefined)
|
|
753
|
+
q.set("until", String(options.until));
|
|
754
|
+
if (options.mode)
|
|
755
|
+
q.set("mode", options.mode);
|
|
756
|
+
if (options.waitSeconds !== undefined)
|
|
757
|
+
q.set("wait_seconds", String(options.waitSeconds));
|
|
758
|
+
const qs = q.toString();
|
|
759
|
+
return this.request("GET", `/changes${qs ? `?${qs}` : ""}`);
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Phase 13: live listening over the durable log — the changes
|
|
763
|
+
* feed as server-sent events. Yields one open frame, one event
|
|
764
|
+
* frame per relevant event as it lands, and a final end frame
|
|
765
|
+
* when the server closes the stream at its set lifetime. Live
|
|
766
|
+
* listening is best-effort with durable catch-up: a dropped
|
|
767
|
+
* stream loses nothing, and reconnecting with the last seq
|
|
768
|
+
* received (options.since) serves everything missed from the
|
|
769
|
+
* log before the stream goes live again. Pass options.signal
|
|
770
|
+
* to stop early; leaving the loop also closes the stream.
|
|
771
|
+
*/
|
|
772
|
+
async *streamChanges(options = {}) {
|
|
773
|
+
const q = new URLSearchParams();
|
|
774
|
+
if (options.mode)
|
|
775
|
+
q.set("mode", options.mode);
|
|
776
|
+
const qs = q.toString();
|
|
777
|
+
const headers = { accept: "text/event-stream" };
|
|
778
|
+
if (this.credential)
|
|
779
|
+
headers.authorization = `Bearer ${this.credential}`;
|
|
780
|
+
if (options.since !== undefined)
|
|
781
|
+
headers["last-event-id"] = String(options.since);
|
|
782
|
+
const res = await this.fetchFn(`${this.baseUrl}/api/v1/booths/${encodeURIComponent(this.boothId)}/stream${qs ? `?${qs}` : ""}`, {
|
|
783
|
+
method: "GET",
|
|
784
|
+
headers,
|
|
785
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
786
|
+
});
|
|
787
|
+
if (!res.ok) {
|
|
788
|
+
const data = (await res.json().catch(() => ({})));
|
|
789
|
+
const err = (data?.error ?? {});
|
|
790
|
+
throw new ActApiError(res.status, err.code ?? "unknown_error", err.message ?? `Request failed with status ${res.status}.`, err.details);
|
|
791
|
+
}
|
|
792
|
+
if (!res.body) {
|
|
793
|
+
throw new ActApiError(0, "stream_unavailable", "The stream response carried no body.");
|
|
794
|
+
}
|
|
795
|
+
const reader = res.body.getReader();
|
|
796
|
+
const decoder = new TextDecoder();
|
|
797
|
+
let buffer = "";
|
|
798
|
+
try {
|
|
799
|
+
for (;;) {
|
|
800
|
+
const { done, value } = await reader.read();
|
|
801
|
+
if (done)
|
|
802
|
+
break;
|
|
803
|
+
buffer += decoder.decode(value, { stream: true });
|
|
804
|
+
let cut = buffer.indexOf("\n\n");
|
|
805
|
+
while (cut >= 0) {
|
|
806
|
+
const raw = buffer.slice(0, cut);
|
|
807
|
+
buffer = buffer.slice(cut + 2);
|
|
808
|
+
const frame = parseStreamFrame(raw);
|
|
809
|
+
if (frame?.type === "error") {
|
|
810
|
+
throw new ActApiError(0, frame.code, "The booth stream reported an error.");
|
|
811
|
+
}
|
|
812
|
+
if (frame)
|
|
813
|
+
yield frame;
|
|
814
|
+
if (frame?.type === "end")
|
|
815
|
+
return;
|
|
816
|
+
cut = buffer.indexOf("\n\n");
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
finally {
|
|
821
|
+
try {
|
|
822
|
+
await reader.cancel();
|
|
823
|
+
}
|
|
824
|
+
catch {
|
|
825
|
+
/* already closed */
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
/** Record that this participant has seen events up to seq. An ack never resolves a conflict. */
|
|
830
|
+
async ack(seq) {
|
|
831
|
+
return this.request("POST", "/ack", { seq });
|
|
832
|
+
}
|
|
833
|
+
/** Replace this participant's subject subscriptions in full. */
|
|
834
|
+
async setSubscriptions(subjects) {
|
|
835
|
+
return this.request("POST", "/subscriptions", { subjects });
|
|
836
|
+
}
|
|
837
|
+
/** Read this participant's durable subscription (subjects, delivery mode, cursors). */
|
|
838
|
+
async getSubscription() {
|
|
839
|
+
return this.request("GET", "/subscriptions");
|
|
840
|
+
}
|
|
841
|
+
/** The participant's pending signals: what in this booth needs their attention, and why. */
|
|
842
|
+
async listSignals() {
|
|
843
|
+
return this.request("GET", "/signals");
|
|
844
|
+
}
|
|
845
|
+
/** Acknowledge a signal. This records that it was seen; it never changes the underlying condition. */
|
|
846
|
+
async ackSignal(signalKey) {
|
|
847
|
+
return this.request("POST", `/signals/${encodeURIComponent(signalKey)}/ack`, {});
|
|
848
|
+
}
|
|
849
|
+
/** Dismiss a signal: seen, and no action will be taken on it. */
|
|
850
|
+
async dismissSignal(signalKey) {
|
|
851
|
+
return this.request("POST", `/signals/${encodeURIComponent(signalKey)}/dismiss`, {});
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Declare a watcher: one typed condition on this participant's
|
|
855
|
+
* subscription (Phase 6). When the condition holds against the
|
|
856
|
+
* booth's log it derives as a signal with reason watcher_triggered.
|
|
857
|
+
* Declaring the same condition again returns the existing watcher
|
|
858
|
+
* with created false.
|
|
859
|
+
*/
|
|
860
|
+
async createWatcher(condition) {
|
|
861
|
+
return this.request("POST", "/watchers", { condition });
|
|
862
|
+
}
|
|
863
|
+
/** This participant's declared watchers in this booth. */
|
|
864
|
+
async listWatchers() {
|
|
865
|
+
return this.request("GET", "/watchers");
|
|
866
|
+
}
|
|
867
|
+
/** Revoke one of this participant's watchers, by its id. */
|
|
868
|
+
async revokeWatcher(watcherId) {
|
|
869
|
+
return this.request("DELETE", `/watchers/${encodeURIComponent(watcherId)}`);
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Phase 10: create a cross-booth subscription into this booth.
|
|
873
|
+
* This client's credential is the caller's credential in this
|
|
874
|
+
* booth (the destination) and must hold subscriptions.manage;
|
|
875
|
+
* sourceCredential proves standing in the source (a source
|
|
876
|
+
* participant holding events.read, or the source's creator
|
|
877
|
+
* credential) and is checked at creation, never stored. The
|
|
878
|
+
* source must be account-owned in v1, and its owner must have set
|
|
879
|
+
* a publication policy for anything to cross. subjects and
|
|
880
|
+
* eventTypes narrow what this subscription receives on top of the
|
|
881
|
+
* policy. An identical active subscription is returned with
|
|
882
|
+
* created false.
|
|
883
|
+
*/
|
|
884
|
+
async createCrossSubscription(input) {
|
|
885
|
+
return this.request("POST", "/cross-subscriptions", {
|
|
886
|
+
source_booth_id: input.sourceBoothId,
|
|
887
|
+
source_credential: input.sourceCredential,
|
|
888
|
+
...(input.subjects ? { subjects: input.subjects } : {}),
|
|
889
|
+
...(input.eventTypes !== undefined ? { event_types: input.eventTypes } : {}),
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
/** This participant's cross-booth subscriptions into this booth. */
|
|
893
|
+
async listCrossSubscriptions() {
|
|
894
|
+
return this.request("GET", "/cross-subscriptions");
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Revoke one of this participant's cross-booth subscriptions, by
|
|
898
|
+
* its id. Revoking stops further forwards from that source into
|
|
899
|
+
* this booth; events already forwarded stay in the log.
|
|
900
|
+
*/
|
|
901
|
+
async revokeCrossSubscription(subscriptionId) {
|
|
902
|
+
return this.request("DELETE", `/cross-subscriptions/${encodeURIComponent(subscriptionId)}`);
|
|
903
|
+
}
|
|
904
|
+
briefSuffix(options, format) {
|
|
905
|
+
const q = new URLSearchParams();
|
|
906
|
+
if (options.participant)
|
|
907
|
+
q.set("participant", options.participant);
|
|
908
|
+
if (options.subject)
|
|
909
|
+
q.set("subject", options.subject);
|
|
910
|
+
if (options.role)
|
|
911
|
+
q.set("role", options.role);
|
|
912
|
+
if (options.dependencyOf)
|
|
913
|
+
q.set("dependency_of", options.dependencyOf);
|
|
914
|
+
if (options.since !== undefined)
|
|
915
|
+
q.set("since", String(options.since));
|
|
916
|
+
if (options.maxRecords !== undefined)
|
|
917
|
+
q.set("max_records", String(options.maxRecords));
|
|
918
|
+
if (options.maxBytes !== undefined)
|
|
919
|
+
q.set("max_bytes", String(options.maxBytes));
|
|
920
|
+
if (format)
|
|
921
|
+
q.set("format", format);
|
|
922
|
+
const qs = q.toString();
|
|
923
|
+
return `/brief${qs ? `?${qs}` : ""}`;
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* The booth Brief (Living Network Phase 4): the state cut to the
|
|
927
|
+
* given filters, as a deterministic projection of the log.
|
|
928
|
+
* Accepted and proposed material sit in separate sections, every
|
|
929
|
+
* entry carries its source event id, and omitted counts what the
|
|
930
|
+
* filters and limits cut, with the reason. Nothing is stored and
|
|
931
|
+
* nothing is reworded.
|
|
932
|
+
*/
|
|
933
|
+
async getBrief(options = {}) {
|
|
934
|
+
return this.request("GET", this.briefSuffix(options));
|
|
935
|
+
}
|
|
936
|
+
/** The same Brief rendered as Markdown (the server's format=md form). */
|
|
937
|
+
async getBriefMarkdown(options = {}) {
|
|
938
|
+
const credential = this.credential;
|
|
939
|
+
const res = await this.fetchFn(`${this.baseUrl}/api/v1/booths/${encodeURIComponent(this.boothId)}${this.briefSuffix(options, "md")}`, {
|
|
940
|
+
method: "GET",
|
|
941
|
+
headers: { ...(credential ? { authorization: `Bearer ${credential}` } : {}) },
|
|
942
|
+
});
|
|
943
|
+
const text = await res.text();
|
|
944
|
+
if (!res.ok) {
|
|
945
|
+
let code = "unknown_error";
|
|
946
|
+
let message = `Request failed with status ${res.status}.`;
|
|
947
|
+
try {
|
|
948
|
+
const data = JSON.parse(text);
|
|
949
|
+
code = data.error?.code ?? code;
|
|
950
|
+
message = data.error?.message ?? message;
|
|
951
|
+
}
|
|
952
|
+
catch {
|
|
953
|
+
// The error body was not JSON; keep the status-based message.
|
|
954
|
+
}
|
|
955
|
+
throw new ActApiError(res.status, code, message);
|
|
956
|
+
}
|
|
957
|
+
return text;
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* The booth Replay (Living Network Phase 4): the state as of a
|
|
961
|
+
* sequence number, plus the event slice. With a participant, also
|
|
962
|
+
* the changes that reached them by then; their ack cursor is
|
|
963
|
+
* labeled as a current value, not as-of, and the response carries
|
|
964
|
+
* its causation note — sequence order is never causation.
|
|
965
|
+
*/
|
|
966
|
+
async getReplay(options = {}) {
|
|
967
|
+
const q = new URLSearchParams();
|
|
968
|
+
if (options.untilSeq !== undefined)
|
|
969
|
+
q.set("until_seq", String(options.untilSeq));
|
|
970
|
+
if (options.subject)
|
|
971
|
+
q.set("subject", options.subject);
|
|
972
|
+
if (options.participant)
|
|
973
|
+
q.set("participant", options.participant);
|
|
974
|
+
const qs = q.toString();
|
|
975
|
+
return this.request("GET", `/replay${qs ? `?${qs}` : ""}`);
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* Ask the Booth (Living Network Phase 4): an exact-subject lookup
|
|
979
|
+
* over the log with citations on every record. q is a substring
|
|
980
|
+
* match over subjects and summary fields — the response labels it
|
|
981
|
+
* as substring matching, not semantic search. At least one of
|
|
982
|
+
* subject, type, or q is required.
|
|
983
|
+
*/
|
|
984
|
+
async ask(options = {}) {
|
|
985
|
+
const q = new URLSearchParams();
|
|
986
|
+
if (options.subject)
|
|
987
|
+
q.set("subject", options.subject);
|
|
988
|
+
if (options.type)
|
|
989
|
+
q.set("type", options.type);
|
|
990
|
+
if (options.q)
|
|
991
|
+
q.set("q", options.q);
|
|
992
|
+
const qs = q.toString();
|
|
993
|
+
return this.request("GET", `/ask${qs ? `?${qs}` : ""}`);
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* The booth What-If evaluation (Living Network Phase 11):
|
|
997
|
+
* validate the hypothetical event inputs with the append path's
|
|
998
|
+
* own validation, append them in memory to a copy of the log
|
|
999
|
+
* (truncated at atSeq when given), and return the delta — new
|
|
1000
|
+
* conflicts, competing contracts, stale assumptions, affected
|
|
1001
|
+
* participants, commitment and check status changes — every entry
|
|
1002
|
+
* labeled hypothetical with its base seq. Nothing is written:
|
|
1003
|
+
* the booth's log and state are unchanged, and making any of it
|
|
1004
|
+
* real means posting the events. The impact graph covers
|
|
1005
|
+
* declared dependencies and subject links only; the evaluation
|
|
1006
|
+
* cannot see anything the log does not declare.
|
|
1007
|
+
*/
|
|
1008
|
+
async evaluateWhatIf(input) {
|
|
1009
|
+
return this.request("POST", "/whatif", {
|
|
1010
|
+
events: input.events,
|
|
1011
|
+
...(input.atSeq !== undefined ? { at_seq: input.atSeq } : {}),
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
capsuleBody(input) {
|
|
1015
|
+
return {
|
|
1016
|
+
...(input.selection
|
|
1017
|
+
? {
|
|
1018
|
+
selection: {
|
|
1019
|
+
...(input.selection.eventIds ? { event_ids: input.selection.eventIds } : {}),
|
|
1020
|
+
...(input.selection.subjects ? { subjects: input.selection.subjects } : {}),
|
|
1021
|
+
...(input.selection.sections ? { sections: input.selection.sections } : {}),
|
|
1022
|
+
},
|
|
1023
|
+
}
|
|
1024
|
+
: {}),
|
|
1025
|
+
...(input.preset ? { preset: input.preset } : {}),
|
|
1026
|
+
...(input.expectedDigest ? { expected_digest: input.expectedDigest } : {}),
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* The capsule selection preview (Living Network Phase 12): the
|
|
1031
|
+
* sensitive-content review step. Returns exactly what the
|
|
1032
|
+
* selection would include — the manifest entries, the Markdown
|
|
1033
|
+
* summary, and the digest the capsule would carry — and stores
|
|
1034
|
+
* nothing. Creation requires the same selection echoed back.
|
|
1035
|
+
*/
|
|
1036
|
+
async previewCapsule(input) {
|
|
1037
|
+
return this.request("POST", "/capsules/preview", this.capsuleBody(input));
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Create a capsule from the echoed selection (Living Network
|
|
1041
|
+
* Phase 12): a manifest built from the log, a Markdown summary
|
|
1042
|
+
* rendered from the same data, and the full selected records,
|
|
1043
|
+
* stored as one artifact. Pin expectedDigest to a preview's
|
|
1044
|
+
* digest and creation refuses (409 capsule_drift) when the log
|
|
1045
|
+
* has moved since the preview. The temporary_org preset exports
|
|
1046
|
+
* a temporary agent organization: the booth's full working
|
|
1047
|
+
* record plus an organization block (objective, roster,
|
|
1048
|
+
* completion check ids).
|
|
1049
|
+
*/
|
|
1050
|
+
async createCapsule(input) {
|
|
1051
|
+
const result = await this.request("POST", "/capsules", this.capsuleBody(input));
|
|
1052
|
+
return result.capsule;
|
|
1053
|
+
}
|
|
1054
|
+
/** One capsule created from this booth, with its summary. */
|
|
1055
|
+
async getCapsule(capsuleId) {
|
|
1056
|
+
const result = await this.request("GET", `/capsules/${encodeURIComponent(capsuleId)}`);
|
|
1057
|
+
return result.capsule;
|
|
1058
|
+
}
|
|
1059
|
+
/** The capsules created from this booth, newest first (ids, digests, counts). */
|
|
1060
|
+
async listCapsules() {
|
|
1061
|
+
const result = await this.request("GET", "/capsules");
|
|
1062
|
+
return result.capsules;
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Import a capsule into this booth (Living Network Phase 12).
|
|
1066
|
+
* This client is the TARGET booth; the caller's credential here
|
|
1067
|
+
* is the only one involved — a capsule grants no access to its
|
|
1068
|
+
* source booth and no source credential takes part. Records
|
|
1069
|
+
* land as new proposals authored by the caller, each carrying
|
|
1070
|
+
* provenance { capsule_id, origin_booth_id, origin_event_id }:
|
|
1071
|
+
* accepted decisions arrive proposed, contracts arrive as new
|
|
1072
|
+
* versions authored by the caller, and endorsement acts and
|
|
1073
|
+
* service-authored records do not cross (the result lists every
|
|
1074
|
+
* skip with its reason). Importing the same capsule into the
|
|
1075
|
+
* same booth twice replays instead of duplicating.
|
|
1076
|
+
*/
|
|
1077
|
+
async importCapsule(capsuleId) {
|
|
1078
|
+
return this.request("POST", `/capsules/${encodeURIComponent(capsuleId)}/import`, {});
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Export a skill from a capsule of this booth (Living Network
|
|
1082
|
+
* Phase 12, §20): package a capability the capsule names (a
|
|
1083
|
+
* capability_offer event's version — picked by eventId or
|
|
1084
|
+
* versionId, or the capsule's only offered version) again
|
|
1085
|
+
* through the standard createCapability path, under the
|
|
1086
|
+
* exporter's booth identity or an ephemeral booth user, with a
|
|
1087
|
+
* provenance.json file added to the package naming the capsule,
|
|
1088
|
+
* the origin booth and event, the origin version and digest,
|
|
1089
|
+
* and the verification receipt ids the log holds for the
|
|
1090
|
+
* capability.
|
|
1091
|
+
*/
|
|
1092
|
+
async exportCapsuleSkill(capsuleId, input = {}) {
|
|
1093
|
+
return this.request("POST", `/capsules/${encodeURIComponent(capsuleId)}/skill-export`, {
|
|
1094
|
+
...(input.eventId ? { event_id: input.eventId } : {}),
|
|
1095
|
+
...(input.versionId ? { version_id: input.versionId } : {}),
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
}
|