@zabaca/zbc 0.24.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/package.json +3 -2
- package/src/commands/add.ts +78 -5
- package/src/commands/list.ts +6 -3
- package/src/commands/update.ts +42 -0
- package/src/engine/destroy.ts +23 -2
- package/src/engine/resolve.ts +5 -0
- package/src/utils/subtree.ts +37 -7
- package/src/utils/vendor-stamp.ts +92 -1
- package/src/utils/vendored-app.ts +353 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/walgit/worker/mcp.ts +1 -2
- package/templates/apps/warehouse/worker/router.ts +1 -2
- package/templates/apps/warehouse/worker/test-setup.ts +1 -2
- package/templates/infra/modules/cloudflare-access/index.ts +245 -28
- package/templates/infra/modules/cloudflare-access/registry.json +1 -1
- package/templates/infra/modules/d1/index.ts +133 -4
- package/templates/infra/modules/d1/registry.json +4 -1
- package/templates/infra/modules/queue/index.ts +313 -0
- package/templates/infra/modules/queue/registry.json +13 -0
- package/templates/infra/modules/r2/registry.json +1 -1
- package/templates/infra/src/define-module.ts +19 -2
- package/templates/infra/src/types.ts +24 -0
- package/templates/apps/inbox/package.json +0 -26
- package/templates/apps/inbox/public/index.html +0 -379
- package/templates/apps/inbox/registry.json +0 -9
- package/templates/apps/inbox/tsconfig.json +0 -19
- package/templates/apps/inbox/worker/api.ts +0 -197
- package/templates/apps/inbox/worker/commands.ts +0 -299
- package/templates/apps/inbox/worker/inbox-do.ts +0 -577
- package/templates/apps/inbox/worker/index.ts +0 -195
- package/templates/apps/inbox/worker/mcp.ts +0 -228
- package/templates/apps/inbox/worker/shared.ts +0 -117
- package/templates/apps/inbox/wrangler.jsonc +0 -48
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import type { DraftFields, InboxStore } from './inbox-do'
|
|
2
|
-
import { type MailEnv, emitWebhook, makeSnippet, newId, normalizeMsgId, sendMail } from './shared'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Every mutation the inbox supports, once. The two adapters — `api.ts` (the
|
|
6
|
-
* JSON routes) and `mcp.ts` (the MCP tools) — parse their own input shape,
|
|
7
|
-
* call one of these, and render the `Outcome`; neither holds mutation logic
|
|
8
|
-
* of its own. A Worker cannot fetch its own routes, so this is a module of
|
|
9
|
-
* functions rather than one adapter calling the other.
|
|
10
|
-
*
|
|
11
|
-
* The shape is uniform: `(env?, store, input) -> Outcome`, where `Outcome`
|
|
12
|
-
* carries the HTTP status the JSON route answers with. The MCP adapter reads
|
|
13
|
-
* the same status: >= 400 becomes a tool error, anything else the tool result.
|
|
14
|
-
*
|
|
15
|
-
* Reads (list/search/get/…) are NOT here — they are single `store.*` calls
|
|
16
|
-
* and both adapters make them directly.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* A command's result, in the JSON adapter's vocabulary because that adapter
|
|
21
|
-
* is the reference: `status` is the HTTP status `/api/*` answers with, `body`
|
|
22
|
-
* the JSON it renders.
|
|
23
|
-
*/
|
|
24
|
-
export interface Outcome {
|
|
25
|
-
status: number
|
|
26
|
-
body: Record<string, unknown>
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface SendBody {
|
|
30
|
-
to: string
|
|
31
|
-
subject: string
|
|
32
|
-
text?: string
|
|
33
|
-
html?: string
|
|
34
|
-
from?: string
|
|
35
|
-
/** RFC 5322 Message-ID of the message being replied to → In-Reply-To/References. */
|
|
36
|
-
inReplyTo?: string
|
|
37
|
-
/**
|
|
38
|
-
* ISO timestamp: instead of sending now, queue the message for the DO
|
|
39
|
-
* alarm to dispatch at (or shortly after) this time.
|
|
40
|
-
*/
|
|
41
|
-
sendAt?: string
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface SendEnv extends MailEnv {
|
|
45
|
-
DEFAULT_FROM?: string
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Send now, or queue for the DO alarm when `sendAt` is set. Validates,
|
|
50
|
-
* dispatches, records the outbound row, and emits the message.sent webhook.
|
|
51
|
-
*/
|
|
52
|
-
export async function send(env: SendEnv, store: InboxStore, body: SendBody): Promise<Outcome> {
|
|
53
|
-
if (!body.to || !body.subject || (!body.text && !body.html)) {
|
|
54
|
-
return { status: 400, body: { error: 'required: to, subject, and text or html' } }
|
|
55
|
-
}
|
|
56
|
-
const from = body.from ?? env.DEFAULT_FROM
|
|
57
|
-
if (!from) {
|
|
58
|
-
return { status: 400, body: { error: 'no from address (set DEFAULT_FROM or pass from)' } }
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Email Sending only accepts whitelisted custom headers (a Message-ID of
|
|
62
|
-
// our own is rejected), so the wire Message-ID is provider-chosen and
|
|
63
|
-
// unknown to us. We store a synthetic one for OUR send chain; recipient
|
|
64
|
-
// replies thread via the subject+participant fallback.
|
|
65
|
-
const id = newId()
|
|
66
|
-
const messageId = `${id}@${from.split('@')[1] ?? 'localhost'}`
|
|
67
|
-
const inReplyTo = normalizeMsgId(body.inReplyTo)
|
|
68
|
-
const text = body.text ?? ''
|
|
69
|
-
const snippet = makeSnippet(text, body.html ?? '')
|
|
70
|
-
|
|
71
|
-
// sendAt → queue for the DO alarm instead of dispatching now.
|
|
72
|
-
if (body.sendAt !== undefined) {
|
|
73
|
-
const at = new Date(body.sendAt)
|
|
74
|
-
if (Number.isNaN(at.getTime())) {
|
|
75
|
-
return { status: 400, body: { error: 'sendAt is not a valid date' } }
|
|
76
|
-
}
|
|
77
|
-
await store.insert({
|
|
78
|
-
id,
|
|
79
|
-
status: 'scheduled',
|
|
80
|
-
scheduled_at: at.toISOString(),
|
|
81
|
-
direction: 'outbound',
|
|
82
|
-
in_reply_to: inReplyTo,
|
|
83
|
-
message_id: messageId,
|
|
84
|
-
from_addr: from,
|
|
85
|
-
to_addr: body.to,
|
|
86
|
-
subject: body.subject,
|
|
87
|
-
date: at.toISOString(),
|
|
88
|
-
snippet,
|
|
89
|
-
text_body: text,
|
|
90
|
-
html_body: body.html ?? '',
|
|
91
|
-
r2_key: '',
|
|
92
|
-
attachments_json: '[]',
|
|
93
|
-
size: text.length + (body.html?.length ?? 0),
|
|
94
|
-
})
|
|
95
|
-
return { status: 202, body: { ok: true, id, status: 'scheduled', sendAt: at.toISOString() } }
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
try {
|
|
99
|
-
await sendMail(env, {
|
|
100
|
-
from,
|
|
101
|
-
to: body.to,
|
|
102
|
-
subject: body.subject,
|
|
103
|
-
text: body.text,
|
|
104
|
-
html: body.html,
|
|
105
|
-
inReplyTo,
|
|
106
|
-
})
|
|
107
|
-
} catch (err) {
|
|
108
|
-
return { status: 502, body: { error: `send failed: ${(err as Error).message}` } }
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Record the outbound message so the inbox shows both sides of a
|
|
112
|
-
// conversation. No raw MIME (we never see the provider's final wire form),
|
|
113
|
-
// so r2_key stays empty and /raw 404s for these.
|
|
114
|
-
const { threadId } = await store.insert({
|
|
115
|
-
id,
|
|
116
|
-
direction: 'outbound',
|
|
117
|
-
in_reply_to: inReplyTo,
|
|
118
|
-
message_id: messageId,
|
|
119
|
-
from_addr: from,
|
|
120
|
-
to_addr: body.to,
|
|
121
|
-
subject: body.subject,
|
|
122
|
-
date: new Date().toISOString(),
|
|
123
|
-
snippet,
|
|
124
|
-
text_body: text,
|
|
125
|
-
html_body: body.html ?? '',
|
|
126
|
-
r2_key: '',
|
|
127
|
-
attachments_json: '[]',
|
|
128
|
-
size: text.length + (body.html?.length ?? 0),
|
|
129
|
-
})
|
|
130
|
-
await emitWebhook(env, 'message.sent', {
|
|
131
|
-
id,
|
|
132
|
-
threadId,
|
|
133
|
-
from,
|
|
134
|
-
to: body.to,
|
|
135
|
-
subject: body.subject,
|
|
136
|
-
snippet,
|
|
137
|
-
direction: 'outbound',
|
|
138
|
-
})
|
|
139
|
-
return { status: 200, body: { ok: true, id, from, to: body.to, messageId, threadId } }
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/** Send an existing draft: validate completeness, dispatch, flip to sent. */
|
|
143
|
-
export async function sendDraft(
|
|
144
|
-
env: SendEnv,
|
|
145
|
-
store: InboxStore,
|
|
146
|
-
draftId: string,
|
|
147
|
-
): Promise<Outcome> {
|
|
148
|
-
const draft = await store.get(draftId)
|
|
149
|
-
if (!draft || draft.status !== 'draft') return { status: 404, body: { error: 'not a draft' } }
|
|
150
|
-
const from = draft.from_addr || env.DEFAULT_FROM
|
|
151
|
-
if (!from) {
|
|
152
|
-
return { status: 400, body: { error: 'no from address (set DEFAULT_FROM or the draft from)' } }
|
|
153
|
-
}
|
|
154
|
-
if (!draft.to_addr || !draft.subject || (!draft.text_body && !draft.html_body)) {
|
|
155
|
-
return { status: 400, body: { error: 'draft incomplete: needs to, subject, and text or html' } }
|
|
156
|
-
}
|
|
157
|
-
try {
|
|
158
|
-
await sendMail(env, {
|
|
159
|
-
from,
|
|
160
|
-
to: draft.to_addr,
|
|
161
|
-
subject: draft.subject,
|
|
162
|
-
text: draft.text_body || undefined,
|
|
163
|
-
html: draft.html_body || undefined,
|
|
164
|
-
inReplyTo: draft.in_reply_to,
|
|
165
|
-
})
|
|
166
|
-
} catch (err) {
|
|
167
|
-
return { status: 502, body: { error: `send failed: ${(err as Error).message}` } }
|
|
168
|
-
}
|
|
169
|
-
// Persist the from that was actually used (may have come from
|
|
170
|
-
// DEFAULT_FROM), then flip the row to sent — thread resolves NOW.
|
|
171
|
-
if (from !== draft.from_addr) await store.updateDraft(draftId, { from_addr: from })
|
|
172
|
-
const sent = await store.markDraftSent(draftId, new Date().toISOString())
|
|
173
|
-
if (!sent) return { status: 500, body: { error: 'draft vanished during send' } }
|
|
174
|
-
await emitWebhook(env, 'message.sent', {
|
|
175
|
-
id: draftId,
|
|
176
|
-
threadId: sent.threadId,
|
|
177
|
-
from,
|
|
178
|
-
to: draft.to_addr,
|
|
179
|
-
subject: draft.subject,
|
|
180
|
-
snippet: draft.snippet,
|
|
181
|
-
direction: 'outbound',
|
|
182
|
-
})
|
|
183
|
-
return {
|
|
184
|
-
status: 200,
|
|
185
|
-
body: { ok: true, id: draftId, from, to: draft.to_addr, threadId: sent.threadId },
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* A draft's fields as an adapter hands them in. Every one is optional: a
|
|
191
|
-
* draft may be partial, and completeness is enforced at send time by
|
|
192
|
-
* {@link sendDraft}.
|
|
193
|
-
*/
|
|
194
|
-
export interface DraftBody {
|
|
195
|
-
to?: string
|
|
196
|
-
subject?: string
|
|
197
|
-
text?: string
|
|
198
|
-
html?: string
|
|
199
|
-
from?: string
|
|
200
|
-
/** RFC 5322 Message-ID of the message being replied to. */
|
|
201
|
-
inReplyTo?: string
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/** Store an outbound message without sending it. */
|
|
205
|
-
export async function createDraft(
|
|
206
|
-
env: SendEnv,
|
|
207
|
-
store: InboxStore,
|
|
208
|
-
body: DraftBody,
|
|
209
|
-
): Promise<Outcome> {
|
|
210
|
-
const id = newId()
|
|
211
|
-
const from = body.from ?? env.DEFAULT_FROM ?? ''
|
|
212
|
-
const text = body.text ?? ''
|
|
213
|
-
await store.insert({
|
|
214
|
-
id,
|
|
215
|
-
status: 'draft',
|
|
216
|
-
direction: 'outbound',
|
|
217
|
-
in_reply_to: normalizeMsgId(body.inReplyTo),
|
|
218
|
-
message_id: `${id}@${from.split('@')[1] || 'localhost'}`,
|
|
219
|
-
from_addr: from,
|
|
220
|
-
to_addr: body.to ?? '',
|
|
221
|
-
subject: body.subject ?? '',
|
|
222
|
-
date: new Date().toISOString(),
|
|
223
|
-
snippet: makeSnippet(text, body.html ?? ''),
|
|
224
|
-
text_body: text,
|
|
225
|
-
html_body: body.html ?? '',
|
|
226
|
-
r2_key: '',
|
|
227
|
-
attachments_json: '[]',
|
|
228
|
-
size: text.length + (body.html?.length ?? 0),
|
|
229
|
-
})
|
|
230
|
-
return { status: 201, body: { ok: true, id, status: 'draft' } }
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Patch an unsent draft: only the fields present change, and `snippet` is
|
|
235
|
-
* recomputed whenever either body was touched. Which rows may be patched at
|
|
236
|
-
* all is the DO's rule (`updateDraft` refuses anything but a draft), not this
|
|
237
|
-
* command's — the command only translates the adapter's field names.
|
|
238
|
-
*
|
|
239
|
-
* Known wart, carried verbatim from the two adapter copies this replaced and
|
|
240
|
-
* left alone here so the refactor stays byte-identical: the snippet is derived
|
|
241
|
-
* from the PATCH, not from the patched row. Patching only `html` on a draft
|
|
242
|
-
* that already has a `text_body` therefore writes a snippet taken from the new
|
|
243
|
-
* html while the text part — the one `makeSnippet` would have preferred, and
|
|
244
|
-
* the one that actually goes out — is unchanged. Fixing it needs the command
|
|
245
|
-
* to read the row first (an extra DO round-trip), which is a behaviour change,
|
|
246
|
-
* not a refactor. `commands.test.ts` pins the current behaviour so a fix is a
|
|
247
|
-
* deliberate edit to that test rather than a silent one.
|
|
248
|
-
*/
|
|
249
|
-
export async function updateDraft(
|
|
250
|
-
store: InboxStore,
|
|
251
|
-
id: string,
|
|
252
|
-
patch: DraftBody,
|
|
253
|
-
): Promise<Outcome> {
|
|
254
|
-
const { text, html } = patch
|
|
255
|
-
const fields: DraftFields = {
|
|
256
|
-
...(patch.from !== undefined ? { from_addr: patch.from } : {}),
|
|
257
|
-
...(patch.to !== undefined ? { to_addr: patch.to } : {}),
|
|
258
|
-
...(patch.subject !== undefined ? { subject: patch.subject } : {}),
|
|
259
|
-
...(text !== undefined ? { text_body: text } : {}),
|
|
260
|
-
...(html !== undefined ? { html_body: html } : {}),
|
|
261
|
-
...(patch.inReplyTo !== undefined ? { in_reply_to: normalizeMsgId(patch.inReplyTo) } : {}),
|
|
262
|
-
...(text !== undefined || html !== undefined
|
|
263
|
-
? { snippet: makeSnippet(text ?? '', html ?? '') }
|
|
264
|
-
: {}),
|
|
265
|
-
}
|
|
266
|
-
const updated = await store.updateDraft(id, fields)
|
|
267
|
-
return updated
|
|
268
|
-
? { status: 200, body: { ok: true, id } }
|
|
269
|
-
: { status: 404, body: { error: 'not a draft' } }
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Delete a message of any status, plus its raw MIME blob.
|
|
274
|
-
*
|
|
275
|
-
* `store.delete` answers with three distinct values and all three matter:
|
|
276
|
-
* `null` (no such row), `''` (deleted, but it never had raw MIME — a draft,
|
|
277
|
-
* a scheduled row, or anything outbound) and a key (delete the R2 object
|
|
278
|
-
* too). The JSON route used to test `!r2Key`, which folded `''` into "not
|
|
279
|
-
* found" and answered 404 for a row it had just deleted; the MCP tool tested
|
|
280
|
-
* `=== null` and was right. This command takes the MCP reading.
|
|
281
|
-
*/
|
|
282
|
-
export async function deleteMessage(
|
|
283
|
-
env: { RAW: R2Bucket },
|
|
284
|
-
store: InboxStore,
|
|
285
|
-
id: string,
|
|
286
|
-
): Promise<Outcome> {
|
|
287
|
-
const r2Key = await store.delete(id)
|
|
288
|
-
if (r2Key === null) return { status: 404, body: { error: 'not found' } }
|
|
289
|
-
if (r2Key) await env.RAW.delete(r2Key)
|
|
290
|
-
return { status: 200, body: { ok: true } }
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/** Cancel a pending scheduled send, or dismiss a failed one. Deletes the row. */
|
|
294
|
-
export async function cancelScheduled(store: InboxStore, id: string): Promise<Outcome> {
|
|
295
|
-
const cancelled = await store.cancelScheduled(id)
|
|
296
|
-
return cancelled
|
|
297
|
-
? { status: 200, body: { ok: true } }
|
|
298
|
-
: { status: 404, body: { error: 'not a scheduled send' } }
|
|
299
|
-
}
|