@vite-hub/webhooks 0.4.1 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -6
- package/dist/database-C5cwO1an.d.ts +1517 -0
- package/dist/database.d.ts +2 -2
- package/dist/database.js +196 -14
- package/dist/{errors-1y5fWGlX.js → errors-DT8uyCh7.js} +39 -4
- package/dist/index.d.ts +128 -4
- package/dist/index.js +246 -4
- package/dist/members-BjDcAAhY.d.ts +343 -0
- package/dist/members-Cr_Nlyg4.js +308 -0
- package/dist/object-CAK6UYR5.js +10 -0
- package/dist/{operations-7ycAhDCw.d.ts → operations-8hqTi0DH.d.ts} +8 -4
- package/dist/operations.d.ts +13 -4
- package/dist/operations.js +189 -99
- package/dist/{queue-name-CGeTSHId.d.ts → queue-name-CLdaJFPZ.d.ts} +0 -2
- package/dist/{queue-name-RURp9SQf.js → queue-name-jmgeppja.js} +1 -2
- package/dist/runtime-BFiCJOMB.js +914 -0
- package/dist/{runtime-mqwMGfyX.d.ts → runtime-BdILDcW1.d.ts} +94 -17
- package/dist/runtime.d.ts +3 -3
- package/dist/runtime.js +3 -3
- package/dist/table-names-DAUip4v2.js +12 -0
- package/dist/vite.d.ts +91 -16
- package/dist/vite.js +192 -55
- package/package.json +9 -9
- package/dist/database-COdjz9Vi.d.ts +0 -608
- package/dist/object-BJb1W_q5.js +0 -7
- package/dist/runtime-DVBMk9vu.js +0 -486
- package/dist/table-names-DVvicXhh.js +0 -8
package/dist/index.js
CHANGED
|
@@ -1,5 +1,248 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { M as unsealWebhooksText, N as defineWebhook, T as checkWebhookUrl, _ as requireMemberEndpoints, k as sealWebhooksText, y as sendDynamicWebhook } from "./runtime-BFiCJOMB.js";
|
|
2
|
+
import { a as getMemberEndpoint, c as memberEndpointHosts, d as removeMemberEndpoints, f as sendToMemberEndpoint, i as getMemberDeliveryState, n as disableMemberEndpoint, o as listMemberDeliveries, r as enableMemberEndpoint, s as listMemberEndpoints, t as createMemberEndpoint, u as removeMemberEndpoint } from "./members-Cr_Nlyg4.js";
|
|
3
|
+
import { t as webhooksErrors } from "./errors-DT8uyCh7.js";
|
|
4
|
+
// src/pairing.ts
|
|
5
|
+
/** How long a pairing code works: 10 minutes. */
|
|
6
|
+
const memberPairingLifetime = 6e5;
|
|
7
|
+
const inboxes = /* @__PURE__ */ new Map([["Claude Code", "claude-code"], ["Codex", "codex"]]);
|
|
8
|
+
const t3ThreadLink = /^t3-thread:\/\/v2\/[\w.~-]{1,128}\/[\w.~:-]{1,256}$/;
|
|
9
|
+
/**
|
|
10
|
+
* The T3 thread link of a pair post, or `null`. Apps put it in an `href`, such as **Open thread**, so anything but a T3
|
|
11
|
+
* thread link is dropped.
|
|
12
|
+
*/
|
|
13
|
+
function memberPairingAddress(value) {
|
|
14
|
+
return typeof value === "string" && t3ThreadLink.test(value) ? value : null;
|
|
15
|
+
}
|
|
16
|
+
const hex = (bytes) => [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
17
|
+
const random = (bytes) => hex(crypto.getRandomValues(new Uint8Array(bytes)));
|
|
18
|
+
function pairingCode() {
|
|
19
|
+
const alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
20
|
+
const letters = [...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(12))].map((byte) => alphabet[byte % 32]).join("");
|
|
21
|
+
return `${letters.slice(0, 4)}-${letters.slice(4, 8)}-${letters.slice(8)}`;
|
|
22
|
+
}
|
|
23
|
+
async function codeKey(code) {
|
|
24
|
+
return hex(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`vitehub:webhooks:pairing:${code.trim().toUpperCase()}`)));
|
|
25
|
+
}
|
|
26
|
+
const label = (id) => `webhooks-pairing:${id}`;
|
|
27
|
+
function toPairing(row) {
|
|
28
|
+
return {
|
|
29
|
+
state: row.state,
|
|
30
|
+
owner: row.owner,
|
|
31
|
+
agent: row.agent,
|
|
32
|
+
host: row.host,
|
|
33
|
+
address: row.address,
|
|
34
|
+
expiresAt: row.expiresAt
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function storeError(error) {
|
|
38
|
+
if (webhooksErrors.WEBHOOKS_STORE_FAILED.is(error) || webhooksErrors.WEBHOOKS_DATABASE_MISSING.is(error)) return error;
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
function sealedFields(text) {
|
|
42
|
+
const value = JSON.parse(text);
|
|
43
|
+
const secret = typeof value === "object" && value !== null ? Reflect.get(value, "secret") : void 0;
|
|
44
|
+
const url = typeof value === "object" && value !== null ? Reflect.get(value, "url") : void 0;
|
|
45
|
+
if (typeof secret !== "string") throw webhooksErrors.WEBHOOKS_STORE_FAILED({ action: "read a pairing code" });
|
|
46
|
+
return {
|
|
47
|
+
secret,
|
|
48
|
+
...typeof url === "string" ? { url } : {}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Mints a pairing code for `owner` and returns `[error, { code, secret, expiresAt }]`. Give both to the member's agent,
|
|
53
|
+
* such as in a setup prompt: it signs its webhook schedule with `secret`, then posts the code with its URL to a route
|
|
54
|
+
* that calls `pairMemberEndpoint()`. The code works once, for 10 minutes, and only the owner may allow it. Starting a
|
|
55
|
+
* new pairing deletes the owner's finished codes.
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { startMemberPairing } from "@vite-hub/webhooks"
|
|
59
|
+
*
|
|
60
|
+
* const [error, pairing] = await startMemberPairing("user_ana")
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
async function startMemberPairing(owner) {
|
|
64
|
+
const { endpoints, developmentKey } = requireMemberEndpoints();
|
|
65
|
+
if (typeof owner !== "string" || !owner.trim()) return [webhooksErrors.WEBHOOKS_ENDPOINT_INVALID({ problem: "`owner` must be non-empty text" }), void 0];
|
|
66
|
+
const code = pairingCode();
|
|
67
|
+
const id = await codeKey(code);
|
|
68
|
+
const secret = `whsec_${random(32)}`;
|
|
69
|
+
const [keyError, sealed] = await sealWebhooksText(label(id), JSON.stringify({ secret }), developmentKey);
|
|
70
|
+
if (keyError) return [keyError, void 0];
|
|
71
|
+
const now = Date.now();
|
|
72
|
+
const expiresAt = now + memberPairingLifetime;
|
|
73
|
+
try {
|
|
74
|
+
await endpoints.insertPairing({
|
|
75
|
+
id,
|
|
76
|
+
owner,
|
|
77
|
+
state: "waiting",
|
|
78
|
+
agent: null,
|
|
79
|
+
host: null,
|
|
80
|
+
address: null,
|
|
81
|
+
sealed,
|
|
82
|
+
expiresAt,
|
|
83
|
+
createdAt: now
|
|
84
|
+
});
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return [storeError(error), void 0];
|
|
87
|
+
}
|
|
88
|
+
return [void 0, {
|
|
89
|
+
code,
|
|
90
|
+
secret,
|
|
91
|
+
expiresAt
|
|
92
|
+
}];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Pairs the agent that posted `input`: `{ code, url, thread? }` for a receiver URL, such as the `webhookUrl` of a T3
|
|
96
|
+
* task, or `{ code, inbox }` for an agent that reads its inbox, `"Claude Code"` or `"Codex"`. The URL must pass
|
|
97
|
+
* `checkWebhookUrl()` with `hosts`, which defaults to the T3 Code relay; a refused URL uses up the code. `thread` is kept
|
|
98
|
+
* as the endpoint's address only when it is a T3 thread link. Returns `[error, result]`, where a code that is unknown,
|
|
99
|
+
* too old, used, or refused is a `status`, not an error. Input that is not one of the two shapes is an error.
|
|
100
|
+
*/
|
|
101
|
+
async function pairMemberEndpoint(input, options = {}) {
|
|
102
|
+
const { endpoints, developmentKey } = requireMemberEndpoints();
|
|
103
|
+
const field = (name) => typeof input === "object" && input !== null ? Reflect.get(input, name) : void 0;
|
|
104
|
+
const code = field("code");
|
|
105
|
+
const url = field("url");
|
|
106
|
+
const inbox = field("inbox");
|
|
107
|
+
const invalid = (problem) => [webhooksErrors.WEBHOOKS_ENDPOINT_INVALID({ problem }), void 0];
|
|
108
|
+
if (typeof code !== "string" || !code || code.length > 64) return invalid("`code` must be the code of the setup prompt");
|
|
109
|
+
if (typeof url === "string" === (typeof inbox === "string")) return invalid("pass `url`, the agent's receiver URL, or `inbox`, \"Claude Code\" or \"Codex\"");
|
|
110
|
+
const agent = typeof inbox === "string" ? inboxes.get(inbox) : "t3";
|
|
111
|
+
if (!agent) return invalid("`inbox` must be \"Claude Code\" or \"Codex\"");
|
|
112
|
+
const now = Date.now();
|
|
113
|
+
const id = await codeKey(code);
|
|
114
|
+
try {
|
|
115
|
+
const row = await endpoints.pairing(id);
|
|
116
|
+
if (!row || row.expiresAt < now) return [void 0, { status: "expired" }];
|
|
117
|
+
if (row.state !== "waiting") return [void 0, { status: "used" }];
|
|
118
|
+
if (typeof inbox === "string") {
|
|
119
|
+
const moved = await endpoints.movePairing(id, "waiting", "paired", now, { agent });
|
|
120
|
+
return [void 0, moved ? {
|
|
121
|
+
status: "paired",
|
|
122
|
+
pairing: toPairing(moved)
|
|
123
|
+
} : { status: "used" }];
|
|
124
|
+
}
|
|
125
|
+
const [refused, checked] = await checkWebhookUrl(url, { hosts: options.hosts ?? memberEndpointHosts });
|
|
126
|
+
if (refused) {
|
|
127
|
+
await endpoints.movePairing(id, "waiting", "refused", now);
|
|
128
|
+
return [void 0, {
|
|
129
|
+
status: "refused",
|
|
130
|
+
reason: refused.why
|
|
131
|
+
}];
|
|
132
|
+
}
|
|
133
|
+
const [openError, opened] = await unsealWebhooksText(label(id), row.sealed, developmentKey);
|
|
134
|
+
if (openError) return [openError, void 0];
|
|
135
|
+
const [sealError, sealed] = await sealWebhooksText(label(id), JSON.stringify({
|
|
136
|
+
secret: sealedFields(opened).secret,
|
|
137
|
+
url: checked.href
|
|
138
|
+
}), developmentKey);
|
|
139
|
+
if (sealError) return [sealError, void 0];
|
|
140
|
+
const moved = await endpoints.movePairing(id, "waiting", "paired", now, {
|
|
141
|
+
agent,
|
|
142
|
+
host: checked.host,
|
|
143
|
+
address: memberPairingAddress(field("thread")),
|
|
144
|
+
sealed
|
|
145
|
+
});
|
|
146
|
+
return [void 0, moved ? {
|
|
147
|
+
status: "paired",
|
|
148
|
+
pairing: toPairing(moved)
|
|
149
|
+
} : { status: "used" }];
|
|
150
|
+
} catch (error) {
|
|
151
|
+
return [storeError(error), void 0];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Uses up a waiting code without pairing, such as when its pair post came over plain http, where anyone on the way could
|
|
156
|
+
* read it. Returns `[error, refused]`.
|
|
157
|
+
*/
|
|
158
|
+
async function refuseMemberPairing(code) {
|
|
159
|
+
const { endpoints } = requireMemberEndpoints();
|
|
160
|
+
try {
|
|
161
|
+
return [void 0, Boolean(await endpoints.movePairing(await codeKey(code), "waiting", "refused", Date.now()))];
|
|
162
|
+
} catch (error) {
|
|
163
|
+
return [storeError(error), void 0];
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** The pairing of `code` while it works, when `owner` minted it, as `[error, pairing]`; `null` otherwise. */
|
|
167
|
+
async function getMemberPairing(owner, code) {
|
|
168
|
+
const { endpoints } = requireMemberEndpoints();
|
|
169
|
+
try {
|
|
170
|
+
const row = await endpoints.pairing(await codeKey(code));
|
|
171
|
+
return [void 0, row && row.owner === owner && row.expiresAt >= Date.now() ? toPairing(row) : null];
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return [storeError(error), void 0];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** Deletes the code of `owner`, such as when they close the setup prompt. */
|
|
177
|
+
async function cancelMemberPairing(owner, code) {
|
|
178
|
+
const { endpoints } = requireMemberEndpoints();
|
|
179
|
+
try {
|
|
180
|
+
await endpoints.removePairing(await codeKey(code), owner);
|
|
181
|
+
return [void 0, void 0];
|
|
182
|
+
} catch (error) {
|
|
183
|
+
return [storeError(error), void 0];
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Creates the endpoint of the agent that paired with `code`, when `owner` minted it, and uses up the code. Returns
|
|
188
|
+
* `[error, { status: "allowed", endpoint, pairing }]`, `status: "not-owner"` when another member minted the code, or
|
|
189
|
+
* `status: "expired"` when it is unknown, too old, used, or not paired yet.
|
|
190
|
+
* The endpoint's address is the agent's T3 thread link when it gave one, so a send with that address reaches it.
|
|
191
|
+
*
|
|
192
|
+
* ```ts
|
|
193
|
+
* import { allowMemberPairing } from "@vite-hub/webhooks"
|
|
194
|
+
*
|
|
195
|
+
* const [error, allowed] = await allowMemberPairing("user_ana", code, { name: "T3 Code thread" })
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
async function allowMemberPairing(owner, code, options) {
|
|
199
|
+
const { endpoints, developmentKey } = requireMemberEndpoints();
|
|
200
|
+
const id = await codeKey(code);
|
|
201
|
+
let used;
|
|
202
|
+
try {
|
|
203
|
+
const row = await endpoints.pairing(id);
|
|
204
|
+
if (row && row.owner !== owner) return [void 0, { status: "not-owner" }];
|
|
205
|
+
if (!row) return [void 0, { status: "expired" }];
|
|
206
|
+
used = await endpoints.movePairing(id, "paired", "used", Date.now());
|
|
207
|
+
} catch (error) {
|
|
208
|
+
return [storeError(error), void 0];
|
|
209
|
+
}
|
|
210
|
+
if (!used) return [void 0, { status: "expired" }];
|
|
211
|
+
const pairing = toPairing(used);
|
|
212
|
+
const member = {
|
|
213
|
+
owner,
|
|
214
|
+
name: typeof options.name === "function" ? options.name(pairing) : options.name,
|
|
215
|
+
...options.ownerName ? { ownerName: options.ownerName } : {},
|
|
216
|
+
...options.ownerEmail ? { ownerEmail: options.ownerEmail } : {}
|
|
217
|
+
};
|
|
218
|
+
let created;
|
|
219
|
+
if (used.host) {
|
|
220
|
+
const [openError, opened] = await unsealWebhooksText(label(id), used.sealed, developmentKey);
|
|
221
|
+
if (openError) return [openError, void 0];
|
|
222
|
+
const target = sealedFields(opened);
|
|
223
|
+
if (!target.url) throw webhooksErrors.WEBHOOKS_STORE_FAILED({ action: "read a pairing code" });
|
|
224
|
+
created = await createMemberEndpoint({
|
|
225
|
+
...member,
|
|
226
|
+
agent: used.agent ?? "t3",
|
|
227
|
+
...used.address ? { address: used.address } : {},
|
|
228
|
+
kind: "url",
|
|
229
|
+
url: target.url,
|
|
230
|
+
secret: target.secret,
|
|
231
|
+
hosts: options.hosts ?? memberEndpointHosts
|
|
232
|
+
});
|
|
233
|
+
} else created = await createMemberEndpoint({
|
|
234
|
+
...member,
|
|
235
|
+
...used.agent ? { agent: used.agent } : {},
|
|
236
|
+
kind: "inbox"
|
|
237
|
+
});
|
|
238
|
+
if (created[0]) return [created[0], void 0];
|
|
239
|
+
return [void 0, {
|
|
240
|
+
status: "allowed",
|
|
241
|
+
endpoint: created[1],
|
|
242
|
+
pairing
|
|
243
|
+
}];
|
|
244
|
+
}
|
|
245
|
+
// src/index.ts
|
|
3
246
|
/**
|
|
4
247
|
* Sends `body` as JSON to the webhook `name` of `server/webhooks/<name>.ts`, signed with its secret, and returns
|
|
5
248
|
* `[error, { id, status }]`. The delivery waits in the Database and goes out through the Queue, so it survives a restart
|
|
@@ -18,5 +261,4 @@ import { _ as defineWebhook, f as sendDynamicWebhook } from "./runtime-DVBMk9vu.
|
|
|
18
261
|
function sendWebhook(name, body, options) {
|
|
19
262
|
return sendDynamicWebhook(name, body, options);
|
|
20
263
|
}
|
|
21
|
-
|
|
22
|
-
export { defineWebhook, sendWebhook };
|
|
264
|
+
export { allowMemberPairing, cancelMemberPairing, checkWebhookUrl, createMemberEndpoint, defineWebhook, disableMemberEndpoint, enableMemberEndpoint, getMemberDeliveryState, getMemberEndpoint, getMemberPairing, listMemberDeliveries, listMemberEndpoints, memberEndpointHosts, memberPairingAddress, memberPairingLifetime, pairMemberEndpoint, refuseMemberPairing, removeMemberEndpoint, removeMemberEndpoints, sealWebhooksText, sendToMemberEndpoint, sendWebhook, startMemberPairing, unsealWebhooksText };
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { a as MemberLastDelivery, d as WebhookDeliveryState, n as ListWebhookDeliveriesOptions, r as MemberEndpoint, t as ListMemberEndpointsOptions, u as WebhookDelivery } from "./database-C5cwO1an.js";
|
|
2
|
+
import { Result } from "@vite-hub/runtime";
|
|
3
|
+
declare const webhooksErrors: import("@vite-hub/runtime").ErrorCatalog<{
|
|
4
|
+
readonly WEBHOOKS_CONTRIBUTION_INVALID: {
|
|
5
|
+
readonly why: ({ plugin }: {
|
|
6
|
+
plugin: string;
|
|
7
|
+
}) => string;
|
|
8
|
+
readonly fix: "Set `vitehub.webhooks` to `{ memberEndpoints: true, reason: \"...\" }`, or to a function that returns it, or `undefined` when the feature is off.";
|
|
9
|
+
};
|
|
10
|
+
readonly WEBHOOKS_DATABASE_OFF: {
|
|
11
|
+
readonly why: ({ users }: {
|
|
12
|
+
users: string;
|
|
13
|
+
}) => string;
|
|
14
|
+
readonly fix: "Turn on `database` in `vitehub()`, or remove the files of `server/webhooks/` and turn off the features that use member endpoints.";
|
|
15
|
+
};
|
|
16
|
+
readonly WEBHOOKS_DEBOUNCE_TOO_LONG: {
|
|
17
|
+
readonly why: ({ debounce }: {
|
|
18
|
+
debounce: string;
|
|
19
|
+
}) => string;
|
|
20
|
+
readonly fix: "Set `debounce` to 24 hours or less, such as `\"30m\"` or `\"1d\"`.";
|
|
21
|
+
};
|
|
22
|
+
readonly WEBHOOKS_DEFINITION_INVALID: {
|
|
23
|
+
readonly why: ({ message }: {
|
|
24
|
+
message: string;
|
|
25
|
+
}) => string;
|
|
26
|
+
readonly fix: "Default-export `defineWebhook({ url: \"T3_WEBHOOK_URL\", secret: \"T3_WEBHOOK_SECRET\" })` from each file of `server/webhooks/`. `url` and `secret` are names of server variables, `signature` is `\"github\"` or `\"stripe\"`, `debounce` is a duration such as `\"30m\"`, and `events` lists `\"invocation.failed\"`, `\"workflow.failed\"`, or `\"schedule.failed\"`.";
|
|
27
|
+
};
|
|
28
|
+
readonly WEBHOOKS_ENDPOINTS_OFF: {
|
|
29
|
+
readonly why: "Member endpoints are not on in this server. No feature of the app uses them, or the code runs outside the ViteHub server.";
|
|
30
|
+
readonly fix: "Turn on a feature that uses member endpoints, such as `send: true` in `defineComments()`. In a test, call `installWebhooks({ webhooks: {}, store, endpoints })` from `@vite-hub/webhooks/runtime` first.";
|
|
31
|
+
};
|
|
32
|
+
readonly WEBHOOKS_FILE_INVALID: {
|
|
33
|
+
readonly why: ({ message }: {
|
|
34
|
+
message: string;
|
|
35
|
+
}) => string;
|
|
36
|
+
readonly fix: "Name each file of `server/webhooks/` with lowercase letters, digits, and dashes, such as `t3.ts` or `slack-alerts.ts`, and keep one file for each webhook name.";
|
|
37
|
+
};
|
|
38
|
+
readonly WEBHOOKS_NOT_FOUND: {
|
|
39
|
+
readonly why: ({ name }: {
|
|
40
|
+
name: string;
|
|
41
|
+
}) => string;
|
|
42
|
+
readonly fix: "Add `server/webhooks/<name>.ts` with `export default defineWebhook({ ... })`, or pass the name of a file that exists. The generated types list every name.";
|
|
43
|
+
};
|
|
44
|
+
readonly WEBHOOKS_NOT_INSTALLED: {
|
|
45
|
+
readonly why: "Webhooks is not running in this server. The app has no `server/webhooks/` files, or the code runs outside the ViteHub server.";
|
|
46
|
+
readonly fix: "Add `server/webhooks/<name>.ts` with `defineWebhook()`, turn on `queue` and `database` in `vitehub()`, and call `sendWebhook()` from server code. In a test, call `installWebhooks()` from `@vite-hub/webhooks/runtime` first.";
|
|
47
|
+
};
|
|
48
|
+
readonly WEBHOOKS_QUEUE_OFF: {
|
|
49
|
+
readonly why: ({ files }: {
|
|
50
|
+
files: string;
|
|
51
|
+
}) => string;
|
|
52
|
+
readonly fix: "Turn on `queue` in `vitehub()`, or remove the files of `server/webhooks/`.";
|
|
53
|
+
};
|
|
54
|
+
readonly WEBHOOKS_BODY_INVALID: {
|
|
55
|
+
readonly why: ({ webhook }: {
|
|
56
|
+
webhook: string;
|
|
57
|
+
}) => string;
|
|
58
|
+
readonly fix: "Pass a plain object, an array, a string, a number, a boolean, or `null`. Convert a `Date` to an ISO string and a `bigint` to a string first.";
|
|
59
|
+
};
|
|
60
|
+
readonly WEBHOOKS_DATABASE_MISSING: {
|
|
61
|
+
readonly why: "Webhooks has no Database in this server.";
|
|
62
|
+
readonly fix: "Turn on `database` in `vitehub()` and add the Default Database.";
|
|
63
|
+
};
|
|
64
|
+
readonly WEBHOOKS_DELIVERY_ACTIVE: {
|
|
65
|
+
readonly why: ({ id, state }: {
|
|
66
|
+
id: string;
|
|
67
|
+
state: string;
|
|
68
|
+
}) => string;
|
|
69
|
+
readonly fix: "Wait until the delivery is sent, dead, or cancelled. To send a pending delivery now, flush it.";
|
|
70
|
+
};
|
|
71
|
+
readonly WEBHOOKS_DELIVERY_FAILED: {
|
|
72
|
+
readonly why: ({ reason, webhook }: {
|
|
73
|
+
reason: string;
|
|
74
|
+
webhook: string;
|
|
75
|
+
}) => string;
|
|
76
|
+
readonly fix: "Check that the receiver is up and answers with a 2xx status within 10 seconds. The Queue tries the delivery again, and `last_error` of the delivery shows the last failure.";
|
|
77
|
+
};
|
|
78
|
+
readonly WEBHOOKS_DELIVERY_NOT_FOUND: {
|
|
79
|
+
readonly why: ({ id }: {
|
|
80
|
+
id: string;
|
|
81
|
+
}) => string;
|
|
82
|
+
readonly fix: "Pass the id of a delivery that `listWebhookDeliveries()` returns.";
|
|
83
|
+
};
|
|
84
|
+
readonly WEBHOOKS_DELIVERY_NOT_OWNER: {
|
|
85
|
+
readonly why: ({ id }: {
|
|
86
|
+
id: string;
|
|
87
|
+
}) => string;
|
|
88
|
+
readonly fix: "Ask the member to redeliver it. In the ViteHub Console, the member signs in with the email of their app account, which must be verified. An app sends again as the member with `redeliverWebhookDelivery(id, { caller: { id: user.id } })`.";
|
|
89
|
+
};
|
|
90
|
+
readonly WEBHOOKS_DELIVERY_NOT_PENDING: {
|
|
91
|
+
readonly why: ({ id, state }: {
|
|
92
|
+
id: string;
|
|
93
|
+
state: string;
|
|
94
|
+
}) => string;
|
|
95
|
+
readonly fix: "Flush or cancel only a pending delivery. To send a sent, dead, or cancelled delivery again, redeliver it.";
|
|
96
|
+
};
|
|
97
|
+
readonly WEBHOOKS_ENDPOINT_DISABLED: {
|
|
98
|
+
readonly why: ({ id }: {
|
|
99
|
+
id: string;
|
|
100
|
+
}) => string;
|
|
101
|
+
readonly fix: "Ask the member to connect their agent again. That replaces the endpoint with a new one.";
|
|
102
|
+
};
|
|
103
|
+
readonly WEBHOOKS_ENDPOINT_INVALID: {
|
|
104
|
+
readonly why: ({ problem }: {
|
|
105
|
+
problem: string;
|
|
106
|
+
}) => string;
|
|
107
|
+
readonly fix: "Pass `{ owner, name, kind: \"url\", url, secret }` for a receiver URL, or `{ owner, name, kind: \"inbox\" }` for an agent that reads its inbox. `owner` and `name` are non-empty text.";
|
|
108
|
+
};
|
|
109
|
+
readonly WEBHOOKS_ENDPOINT_NOT_FOUND: {
|
|
110
|
+
readonly why: ({ match }: {
|
|
111
|
+
match: string;
|
|
112
|
+
}) => string;
|
|
113
|
+
readonly fix: "Create one with `createMemberEndpoint()` first. `getMemberEndpoint(owner)` returns the endpoint of a member.";
|
|
114
|
+
};
|
|
115
|
+
readonly WEBHOOKS_KEY_INVALID: {
|
|
116
|
+
readonly why: ({ problem }: {
|
|
117
|
+
problem: string;
|
|
118
|
+
}) => string;
|
|
119
|
+
readonly fix: "Set `VITEHUB_WEBHOOKS_KEY` to 32 random bytes in base64, from `openssl rand -base64 32`. A new key cannot open the endpoints that the old key sealed, so members connect again after a change.";
|
|
120
|
+
};
|
|
121
|
+
readonly WEBHOOKS_KEY_UNSET: {
|
|
122
|
+
readonly why: "Webhooks seals the URL and secret of each member endpoint with `VITEHUB_WEBHOOKS_KEY`, but this server has no value for it.";
|
|
123
|
+
readonly fix: "Set `VITEHUB_WEBHOOKS_KEY` to the output of `openssl rand -base64 32`, as a secret on the host. `vite dev` generates one in `.vitehub/data/webhooks-key`.";
|
|
124
|
+
};
|
|
125
|
+
readonly WEBHOOKS_SECRET_UNSET: {
|
|
126
|
+
readonly why: ({ name, webhook }: {
|
|
127
|
+
name: string;
|
|
128
|
+
webhook: string;
|
|
129
|
+
}) => string;
|
|
130
|
+
readonly fix: "Set the variable to the signing secret that the receiver checks: in `.env` for `vite dev`, and as a secret on the host for production.";
|
|
131
|
+
};
|
|
132
|
+
readonly WEBHOOKS_STORE_FAILED: {
|
|
133
|
+
readonly why: ({ action }: {
|
|
134
|
+
action: string;
|
|
135
|
+
}) => string;
|
|
136
|
+
readonly fix: "Apply the Webhooks table with `vitehub database generate` and `vitehub database migrate`. `error.cause` holds the Database error.";
|
|
137
|
+
};
|
|
138
|
+
readonly WEBHOOKS_URL_INVALID: {
|
|
139
|
+
readonly why: ({ name, webhook }: {
|
|
140
|
+
name: string;
|
|
141
|
+
webhook: string;
|
|
142
|
+
}) => string;
|
|
143
|
+
readonly fix: "Set the variable to the full URL of the receiver, such as `https://example.com/hooks/vitehub`.";
|
|
144
|
+
};
|
|
145
|
+
readonly WEBHOOKS_URL_REFUSED: {
|
|
146
|
+
readonly why: ({ reason }: {
|
|
147
|
+
reason: string;
|
|
148
|
+
}) => string;
|
|
149
|
+
readonly fix: "Use an `https` URL on a public host, with no user name or password, such as `https://relay.t3.codes/hooks/...`. A member endpoint also needs a host of its allowlist.";
|
|
150
|
+
};
|
|
151
|
+
readonly WEBHOOKS_URL_UNSET: {
|
|
152
|
+
readonly why: ({ name, webhook }: {
|
|
153
|
+
name: string;
|
|
154
|
+
webhook: string;
|
|
155
|
+
}) => string;
|
|
156
|
+
readonly fix: "Set the variable to the URL of the receiver: in `.env` for `vite dev`, and as a secret on the host for production. Keep it secret when the URL holds a token.";
|
|
157
|
+
};
|
|
158
|
+
readonly WEBHOOKS_WAKE_FAILED: {
|
|
159
|
+
readonly why: ({ webhook }: {
|
|
160
|
+
webhook: string;
|
|
161
|
+
}) => string;
|
|
162
|
+
readonly fix: "Read `error.cause` for the Queue error. Flush the pending delivery when the Queue is available again.";
|
|
163
|
+
};
|
|
164
|
+
}>;
|
|
165
|
+
/** The codes that `sendWebhook()` returns. Definition errors throw instead. */
|
|
166
|
+
type SendWebhookErrorCode = "WEBHOOKS_BODY_INVALID" | "WEBHOOKS_DATABASE_MISSING" | "WEBHOOKS_SECRET_UNSET" | "WEBHOOKS_STORE_FAILED" | "WEBHOOKS_URL_INVALID" | "WEBHOOKS_URL_UNSET" | "WEBHOOKS_WAKE_FAILED";
|
|
167
|
+
/** The codes that `createMemberEndpoint()` returns. */
|
|
168
|
+
type CreateMemberEndpointErrorCode = "WEBHOOKS_DATABASE_MISSING" | "WEBHOOKS_ENDPOINT_INVALID" | "WEBHOOKS_KEY_INVALID" | "WEBHOOKS_KEY_UNSET" | "WEBHOOKS_STORE_FAILED" | "WEBHOOKS_URL_REFUSED";
|
|
169
|
+
/** The codes that `getMemberEndpoint()`, `listMemberEndpoints()`, `removeMemberEndpoint()`, `disableMemberEndpoint()`, and `enableMemberEndpoint()` return. */
|
|
170
|
+
type MemberEndpointErrorCode = "WEBHOOKS_DATABASE_MISSING" | "WEBHOOKS_ENDPOINT_NOT_FOUND" | "WEBHOOKS_STORE_FAILED";
|
|
171
|
+
/** The codes that `startMemberPairing()`, `pairMemberEndpoint()`, `getMemberPairing()`, `cancelMemberPairing()`, and `allowMemberPairing()` return. */
|
|
172
|
+
type MemberPairingErrorCode = "WEBHOOKS_DATABASE_MISSING" | "WEBHOOKS_ENDPOINT_INVALID" | "WEBHOOKS_KEY_INVALID" | "WEBHOOKS_KEY_UNSET" | "WEBHOOKS_STORE_FAILED";
|
|
173
|
+
/** The codes that `sendToMemberEndpoint()` returns. A failed post is not an error: it returns `status: "failed"`. */
|
|
174
|
+
type SendToMemberEndpointErrorCode = "WEBHOOKS_BODY_INVALID" | "WEBHOOKS_DATABASE_MISSING" | "WEBHOOKS_ENDPOINT_DISABLED" | "WEBHOOKS_ENDPOINT_NOT_FOUND" | "WEBHOOKS_KEY_INVALID" | "WEBHOOKS_KEY_UNSET" | "WEBHOOKS_STORE_FAILED" | "WEBHOOKS_URL_REFUSED" | "WEBHOOKS_WAKE_FAILED";
|
|
175
|
+
/**
|
|
176
|
+
* The codes that the delivery functions of `@vite-hub/webhooks/runtime` return. The member codes come only from a
|
|
177
|
+
* delivery to a member endpoint.
|
|
178
|
+
*/
|
|
179
|
+
type WebhookDeliveryErrorCode = "WEBHOOKS_DATABASE_MISSING" | "WEBHOOKS_DELIVERY_ACTIVE" | "WEBHOOKS_DELIVERY_NOT_FOUND" | "WEBHOOKS_DELIVERY_NOT_OWNER" | "WEBHOOKS_DELIVERY_NOT_PENDING" | "WEBHOOKS_ENDPOINT_DISABLED" | "WEBHOOKS_ENDPOINT_NOT_FOUND" | "WEBHOOKS_KEY_INVALID" | "WEBHOOKS_KEY_UNSET" | "WEBHOOKS_STORE_FAILED" | "WEBHOOKS_WAKE_FAILED";
|
|
180
|
+
/** The hosts that a member endpoint URL may use when `createMemberEndpoint()` gets no `hosts`: the T3 Code relay. */
|
|
181
|
+
declare const memberEndpointHosts: readonly string[];
|
|
182
|
+
/** What `createMemberEndpoint()` accepts. */
|
|
183
|
+
type CreateMemberEndpointOptions = {
|
|
184
|
+
/** The member, such as the Auth user id. A member has one endpoint per `address`: a new one replaces the old one. */
|
|
185
|
+
owner: string;
|
|
186
|
+
/**
|
|
187
|
+
* Where the agent can be reached, such as the T3 thread link `t3-thread://v2/<environment>/<thread>` it paired from.
|
|
188
|
+
* A send with this `address` reaches this endpoint. Without it, the endpoint has none.
|
|
189
|
+
*/
|
|
190
|
+
address?: string;
|
|
191
|
+
/** The name of the member that admins read in the Console, such as `"Ana Ruiz"`. */
|
|
192
|
+
ownerName?: string;
|
|
193
|
+
/**
|
|
194
|
+
* The email of the member, only when it is verified. The ViteHub Console signs in with its own accounts, so a Console
|
|
195
|
+
* account acts as the owner only when its verified email equals this one and Auth still resolves it to `owner`.
|
|
196
|
+
*/
|
|
197
|
+
ownerEmail?: string;
|
|
198
|
+
/** A name that people read, such as `"Launch plan thread"`. */
|
|
199
|
+
name: string;
|
|
200
|
+
/** The agent that receives, such as `"t3"`, `"claude-code"`, or `"codex"`. */
|
|
201
|
+
agent?: string;
|
|
202
|
+
} & ({
|
|
203
|
+
/** Webhooks posts each send to `url`, signed with `secret` as `x-hub-signature-256`. */
|
|
204
|
+
kind: "url";
|
|
205
|
+
url: string;
|
|
206
|
+
secret: string;
|
|
207
|
+
/** The hosts that `url` may use. Default: `memberEndpointHosts`, the T3 Code relay. */
|
|
208
|
+
hosts?: readonly string[];
|
|
209
|
+
} | {
|
|
210
|
+
/** Webhooks posts nothing. The member's agent reads the deliveries, such as through an MCP tool. */
|
|
211
|
+
kind: "inbox";
|
|
212
|
+
});
|
|
213
|
+
/** Options of `sendToMemberEndpoint()`. */
|
|
214
|
+
interface SendToMemberEndpointOptions {
|
|
215
|
+
/** A label for the receiver, sent as `x-vitehub-event`, such as `"comments.send"`. */
|
|
216
|
+
event: string;
|
|
217
|
+
/**
|
|
218
|
+
* The idempotency key, such as the ids of the comments that the send holds. A send with the key of a pending,
|
|
219
|
+
* sending, or sent delivery of the same endpoint returns that delivery and posts nothing.
|
|
220
|
+
*/
|
|
221
|
+
key: string;
|
|
222
|
+
/**
|
|
223
|
+
* The address of the agent that should get the send, such as the T3 thread that wrote what the comments are on. The
|
|
224
|
+
* member's endpoint with this address gets it; without one, their newest endpoint does.
|
|
225
|
+
*/
|
|
226
|
+
address?: string;
|
|
227
|
+
/** Send only to this endpoint of the member, with no address or newest-endpoint fallback. */
|
|
228
|
+
endpointId?: string;
|
|
229
|
+
/**
|
|
230
|
+
* The hosts that the endpoint's URL may use now, such as the current `hosts` of the app. A URL endpoint on another
|
|
231
|
+
* host gets `WEBHOOKS_URL_REFUSED` and no delivery, so narrowing the list also stops endpoints that it allowed before.
|
|
232
|
+
*/
|
|
233
|
+
hosts?: readonly string[];
|
|
234
|
+
/** Seal the body at rest and omit it from delivery inspection. */
|
|
235
|
+
sealBody?: boolean;
|
|
236
|
+
/** A preallocated unique delivery ID, for correlating a durable record before sending. */
|
|
237
|
+
deliveryId?: string;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* What `sendToMemberEndpoint()` returns. `status` is `"sent"` when the receiver answered 2xx, `"inbox"` when the
|
|
241
|
+
* delivery waits in an inbox, `"pending"` when the Queue tries again, and `"failed"` when it was not delivered.
|
|
242
|
+
*/
|
|
243
|
+
interface SendToMemberEndpointResult {
|
|
244
|
+
id: string;
|
|
245
|
+
status: "sent" | "inbox" | "pending" | "failed";
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Creates the endpoint of a member, so `sendToMemberEndpoint(owner, ...)` reaches their own agent, and returns
|
|
249
|
+
* `[error, endpoint]`. A `"url"` endpoint must pass `checkWebhookUrl()` with `hosts`, which defaults to the T3 Code
|
|
250
|
+
* relay. Webhooks seals the URL and secret with `VITEHUB_WEBHOOKS_KEY` before it stores them, and returns only the host.
|
|
251
|
+
* A member has one endpoint per `address`: a new one replaces the one with the same address, and its id changes.
|
|
252
|
+
*
|
|
253
|
+
* ```ts
|
|
254
|
+
* import { createMemberEndpoint } from "@vite-hub/webhooks"
|
|
255
|
+
*
|
|
256
|
+
* const [error, endpoint] = await createMemberEndpoint({
|
|
257
|
+
* owner: "user_ana",
|
|
258
|
+
* name: "Launch plan thread",
|
|
259
|
+
* agent: "t3",
|
|
260
|
+
* kind: "url",
|
|
261
|
+
* url: "https://relay.t3.codes/hooks/abc123",
|
|
262
|
+
* secret: "whsec_9f2c",
|
|
263
|
+
* })
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
declare function createMemberEndpoint(options: CreateMemberEndpointOptions): Promise<Result<MemberEndpoint, CreateMemberEndpointErrorCode>>;
|
|
267
|
+
/**
|
|
268
|
+
* The endpoint that a send to a member reaches, or `null` when they have none, as `[error, endpoint]`: the one with
|
|
269
|
+
* `endpointId` or `address`, else their newest. It never holds the URL or the secret.
|
|
270
|
+
*/
|
|
271
|
+
declare function getMemberEndpoint(owner: string, options?: {
|
|
272
|
+
address?: string;
|
|
273
|
+
endpointId?: string;
|
|
274
|
+
}): Promise<Result<MemberEndpoint | null, MemberEndpointErrorCode>>;
|
|
275
|
+
/** Removes the endpoint `id` of a member. Another member's endpoint gives `WEBHOOKS_ENDPOINT_NOT_FOUND`. */
|
|
276
|
+
declare function removeMemberEndpoint(owner: string, id: string): Promise<Result<MemberEndpoint, MemberEndpointErrorCode>>;
|
|
277
|
+
/**
|
|
278
|
+
* Removes every endpoint of a member, such as when they disconnect their agents, and returns `[error, removed]`. A member
|
|
279
|
+
* with no endpoint gets an empty list.
|
|
280
|
+
*/
|
|
281
|
+
declare function removeMemberEndpoints(owner: string): Promise<Result<MemberEndpoint[], MemberEndpointErrorCode>>;
|
|
282
|
+
/** A member endpoint with the newest delivery to it, as the Console lists them. */
|
|
283
|
+
interface MemberEndpointWithLastDelivery extends MemberEndpoint {
|
|
284
|
+
/** The newest delivery to the endpoint, or `null` before the first send. */
|
|
285
|
+
last: MemberLastDelivery | null;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* A page of member endpoints, the oldest first, with the newest delivery to each, as `[error, endpoints]`. The rows never
|
|
289
|
+
* hold the URL or the secret. Check that the caller may read them, such as an admin, before you call it.
|
|
290
|
+
*/
|
|
291
|
+
declare function listMemberEndpoints(options?: ListMemberEndpointsOptions): Promise<Result<MemberEndpointWithLastDelivery[], MemberEndpointErrorCode>>;
|
|
292
|
+
/**
|
|
293
|
+
* Disables a member endpoint, so it gets no more sends, without the owner. `by` names who did it, such as the admin,
|
|
294
|
+
* for the Console. Check that the caller may do this before you call it. `enableMemberEndpoint()` undoes it, and so
|
|
295
|
+
* does the member when they connect their agent again.
|
|
296
|
+
*/
|
|
297
|
+
declare function disableMemberEndpoint(id: string, options?: {
|
|
298
|
+
by?: string;
|
|
299
|
+
}): Promise<Result<MemberEndpoint, MemberEndpointErrorCode>>;
|
|
300
|
+
/** Enables a member endpoint that an admin disabled, so it gets sends again. Check that the caller may do this first. */
|
|
301
|
+
declare function enableMemberEndpoint(id: string): Promise<Result<MemberEndpoint, MemberEndpointErrorCode>>;
|
|
302
|
+
/**
|
|
303
|
+
* Sends `body` as JSON to the endpoint of a member and returns `[error, { id, status }]`. It posts in the request,
|
|
304
|
+
* with a 10 second timeout and the same headers and signature as `sendWebhook()`, so it needs no Queue. When the app
|
|
305
|
+
* has Queue, a failed post is tried again through it, and `status` is `"pending"`. Without Queue, it is `"failed"`.
|
|
306
|
+
*
|
|
307
|
+
* A send with the `key` of a pending, sending, or sent delivery returns that delivery and posts nothing.
|
|
308
|
+
*
|
|
309
|
+
* ```ts
|
|
310
|
+
* import { sendToMemberEndpoint } from "@vite-hub/webhooks"
|
|
311
|
+
*
|
|
312
|
+
* const [error, sent] = await sendToMemberEndpoint("user_ana", { threads: ["thr_1", "thr_2"] }, {
|
|
313
|
+
* event: "comments.send",
|
|
314
|
+
* key: "thr_1:msg_8,thr_2:msg_3",
|
|
315
|
+
* })
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
declare function sendToMemberEndpoint(owner: string, body: unknown, options: SendToMemberEndpointOptions): Promise<Result<SendToMemberEndpointResult, SendToMemberEndpointErrorCode>>;
|
|
319
|
+
/** What `listMemberDeliveries()` returns: the endpoint it read, and its deliveries, the newest first. */
|
|
320
|
+
interface MemberDeliveries {
|
|
321
|
+
endpoint: MemberEndpoint;
|
|
322
|
+
deliveries: WebhookDelivery[];
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* The deliveries to the endpoint that a send to the member with `address` reaches, the newest first, as
|
|
326
|
+
* `[error, { endpoint, deliveries }]`, or `null` when the member has no endpoint. An agent that pairs as an inbox reads
|
|
327
|
+
* its sends here: pass its `endpointId` for an exact lookup with no fallback, and the last delivery as `before` for
|
|
328
|
+
* the next page. An empty `address` prefers the unaddressed endpoint. Check that the caller acts as `owner` first.
|
|
329
|
+
* Sealed bodies are omitted, as in the delivery log.
|
|
330
|
+
*
|
|
331
|
+
* ```ts
|
|
332
|
+
* import { listMemberDeliveries } from "@vite-hub/webhooks"
|
|
333
|
+
*
|
|
334
|
+
* const [error, inbox] = await listMemberDeliveries("user_ana", { limit: 20 })
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
declare function listMemberDeliveries(owner: string, options?: {
|
|
338
|
+
address?: string;
|
|
339
|
+
endpointId?: string;
|
|
340
|
+
} & Pick<ListWebhookDeliveriesOptions, "limit" | "before">): Promise<Result<MemberDeliveries | null, MemberEndpointErrorCode>>;
|
|
341
|
+
/** Reads only the state of a known member delivery, never its body. */
|
|
342
|
+
declare function getMemberDeliveryState(id: string): Promise<WebhookDeliveryState | undefined>;
|
|
343
|
+
export { webhooksErrors as C, WebhookDeliveryErrorCode as S, CreateMemberEndpointErrorCode as _, SendToMemberEndpointResult as a, SendToMemberEndpointErrorCode as b, enableMemberEndpoint as c, listMemberDeliveries as d, listMemberEndpoints as f, sendToMemberEndpoint as g, removeMemberEndpoints as h, SendToMemberEndpointOptions as i, getMemberDeliveryState as l, removeMemberEndpoint as m, MemberDeliveries as n, createMemberEndpoint as o, memberEndpointHosts as p, MemberEndpointWithLastDelivery as r, disableMemberEndpoint as s, CreateMemberEndpointOptions as t, getMemberEndpoint as u, MemberEndpointErrorCode as v, SendWebhookErrorCode as x, MemberPairingErrorCode as y };
|