@zabaca/zbc 0.12.0 → 0.14.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 +174 -0
- package/package.json +6 -2
- package/src/commands/add.ts +13 -0
- package/src/commands/init.ts +1 -0
- package/src/engine/apply.ts +60 -21
- package/src/engine/destroy.ts +183 -14
- package/src/engine/discover.ts +1 -1
- package/src/engine/resolve.ts +56 -11
- package/src/utils/copy-template.ts +10 -0
- package/templates/apps/inbox/package.json +4 -2
- package/templates/apps/inbox/tsconfig.json +3 -1
- package/templates/apps/inbox/worker/api.ts +197 -0
- package/templates/apps/inbox/worker/commands.ts +299 -0
- package/templates/apps/inbox/worker/inbox-do.ts +53 -10
- package/templates/apps/inbox/worker/index.ts +15 -202
- package/templates/apps/inbox/worker/mcp.ts +49 -100
- package/templates/apps/walgit/CONTEXT.md +135 -0
- package/templates/apps/walgit/Dockerfile +40 -18
- package/templates/apps/walgit/README.md +518 -55
- package/templates/apps/walgit/e2e/README.md +100 -6
- package/templates/apps/walgit/e2e/harness.ts +262 -1
- package/templates/apps/walgit/e2e/live.ts +588 -0
- package/templates/apps/walgit/e2e/scenarios.ts +367 -5
- package/templates/apps/walgit/e2e/suite.ts +5 -5
- package/templates/apps/walgit/examples/watch.ts +209 -0
- package/templates/apps/walgit/package.json +12 -3
- package/templates/apps/walgit/registry.json +6 -11
- package/templates/apps/walgit/shared/capabilities.ts +189 -0
- package/templates/apps/walgit/shared/container-env.ts +114 -0
- package/templates/apps/walgit/shared/credentials.ts +81 -0
- package/templates/apps/walgit/shared/events.ts +277 -0
- package/templates/apps/walgit/shared/landing.ts +1241 -0
- package/templates/apps/walgit/shared/llms.ts +463 -0
- package/templates/apps/walgit/shared/outbox.ts +266 -0
- package/templates/apps/walgit/shared/policy.ts +59 -0
- package/templates/apps/walgit/shared/protocol.ts +197 -0
- package/templates/apps/walgit/shared/provenance.ts +154 -0
- package/templates/apps/walgit/shared/ref-cache.ts +181 -0
- package/templates/apps/walgit/shared/telemetry.ts +219 -0
- package/templates/apps/walgit/src/announce-main.ts +37 -0
- package/templates/apps/walgit/src/announce.ts +98 -0
- package/templates/apps/walgit/src/append-only.ts +112 -0
- package/templates/apps/walgit/src/cache.ts +172 -0
- package/templates/apps/walgit/src/cli.ts +493 -0
- package/templates/apps/walgit/src/compact.ts +25 -23
- package/templates/apps/walgit/src/delete-repo.ts +237 -0
- package/templates/apps/walgit/src/expire.ts +248 -0
- package/templates/apps/walgit/src/gc.ts +8 -16
- package/templates/apps/walgit/src/git-backend.ts +29 -0
- package/templates/apps/walgit/src/git.ts +78 -0
- package/templates/apps/walgit/src/hook-main.ts +259 -17
- package/templates/apps/walgit/src/hooks.ts +4 -3
- package/templates/apps/walgit/src/http.ts +237 -45
- package/templates/apps/walgit/src/instructions.ts +240 -0
- package/templates/apps/walgit/src/keys.ts +126 -0
- package/templates/apps/walgit/src/limits.ts +158 -0
- package/templates/apps/walgit/src/materialize.bench.ts +10 -10
- package/templates/apps/walgit/src/materialize.ts +48 -40
- package/templates/apps/walgit/src/mkdir-lock.ts +66 -0
- package/templates/apps/walgit/src/orphans.ts +2 -5
- package/templates/apps/walgit/src/pending.ts +201 -0
- package/templates/apps/walgit/src/push-cert.ts +254 -0
- package/templates/apps/walgit/src/push.ts +233 -46
- package/templates/apps/walgit/src/reconcile.ts +5 -8
- package/templates/apps/walgit/src/repo.ts +22 -77
- package/templates/apps/walgit/src/server.ts +148 -26
- package/templates/apps/walgit/src/signers.ts +577 -0
- package/templates/apps/walgit/src/store-env.ts +5 -4
- package/templates/apps/walgit/src/store.ts +47 -24
- package/templates/apps/walgit/src/usage.ts +404 -0
- package/templates/apps/walgit/src/verify.ts +171 -0
- package/templates/apps/walgit/src/wal-index.ts +189 -11
- package/templates/apps/walgit/tsconfig.json +8 -2
- package/templates/apps/walgit/tsconfig.worker.json +24 -0
- package/templates/apps/walgit/worker/events-do.ts +259 -0
- package/templates/apps/walgit/worker/index.ts +572 -0
- package/templates/apps/walgit/wrangler.jsonc +95 -0
- package/templates/apps/warehouse/package.json +1 -1
- package/templates/infra/modules/cloudflare/index.ts +93 -67
- package/templates/infra/modules/cloudflare-access/index.ts +3 -3
- package/templates/infra/modules/cloudflare-api/index.ts +144 -32
- package/templates/infra/modules/cloudflare-api/registry.json +1 -1
- package/templates/infra/modules/cloudflare-email/index.ts +103 -144
- package/templates/infra/modules/cloudflare-email/registry.json +1 -1
- package/templates/infra/modules/cloudflare-token/index.ts +10 -30
- package/templates/infra/modules/cloudflare-token/registry.json +1 -1
- package/templates/infra/modules/cloudflare-tunnel/index.ts +3 -3
- package/templates/infra/modules/cloudflare-zone/index.ts +2 -2
- package/templates/infra/modules/fly/index.ts +158 -35
- package/templates/infra/modules/host-file/index.ts +6 -7
- package/templates/infra/modules/provision-core/index.ts +21 -15
- package/templates/infra/modules/r2/index.ts +38 -55
- package/templates/infra/modules/r2/registry.json +1 -1
- package/templates/infra/modules/remote-provision/index.ts +1 -3
- package/templates/infra/modules/turso/index.ts +45 -7
- package/templates/infra/modules/vm-provision/index.ts +10 -3
- package/templates/infra/package.json +1 -1
- package/templates/infra/src/context.ts +115 -0
- package/templates/infra/src/define-module.ts +8 -2
- package/templates/infra/src/index.ts +11 -1
- package/templates/infra/src/types.ts +72 -3
- package/src/commands/add-registry.test.ts +0 -92
- package/src/commands/secret.test.ts +0 -717
- package/src/commands/vendor-mode.test.ts +0 -183
- package/src/infra-types.ts +0 -38
- package/src/utils/subtree.test.ts +0 -127
- package/templates/apps/inbox/worker/send-op.ts +0 -174
- package/templates/apps/walgit/entrypoint.sh +0 -59
- package/templates/apps/walgit/fly.toml +0 -52
- package/templates/apps/walgit/src/ssh-shell.ts +0 -36
- package/templates/apps/walgit/sshd_config +0 -38
|
@@ -3,18 +3,23 @@
|
|
|
3
3
|
*
|
|
4
4
|
* - `email()` handler: Email Routing (catch-all) delivers inbound mail here.
|
|
5
5
|
* Raw MIME → R2; parsed metadata + bodies → the Inbox Durable Object.
|
|
6
|
-
* - `/api/*`: bearer-authed JSON API
|
|
6
|
+
* - `/api/*`: bearer-authed JSON API — handled by `api.ts`.
|
|
7
|
+
* - `/mcp`: the MCP server over the same operations — handled by `mcp.ts`.
|
|
7
8
|
* - Everything else: the static UI in public/ (ASSETS binding).
|
|
9
|
+
*
|
|
10
|
+
* This file is the runtime entry only: the Env, the bearer check, inbound mail
|
|
11
|
+
* and the dispatch. Both adapters live beside it so a test can load them —
|
|
12
|
+
* see the note atop `api.ts`.
|
|
8
13
|
*/
|
|
9
14
|
import PostalMime from 'postal-mime'
|
|
10
|
-
import {
|
|
15
|
+
import { type ApiEnv, handleApi, json } from './api'
|
|
16
|
+
import { Inbox, type InboxStore } from './inbox-do'
|
|
11
17
|
import { handleMcp } from './mcp'
|
|
12
|
-
import { type SendBody, performDraftSend, performSend } from './send-op'
|
|
13
18
|
import { type SendEmailBinding, emitWebhook, makeSnippet, newId, normalizeMsgId } from './shared'
|
|
14
19
|
|
|
15
20
|
export { Inbox }
|
|
16
21
|
|
|
17
|
-
export interface Env {
|
|
22
|
+
export interface Env extends ApiEnv {
|
|
18
23
|
ASSETS: { fetch: (request: Request) => Promise<Response> }
|
|
19
24
|
INBOX: DurableObjectNamespace<Inbox>
|
|
20
25
|
RAW: R2Bucket
|
|
@@ -53,10 +58,6 @@ async function authorized(request: Request, env: Env): Promise<boolean> {
|
|
|
53
58
|
return crypto.subtle.timingSafeEqual(a, b)
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
function json(data: unknown, status = 200): Response {
|
|
57
|
-
return Response.json(data, { status, headers: { 'cache-control': 'no-store' } })
|
|
58
|
-
}
|
|
59
|
-
|
|
60
61
|
function inboxStub(env: Env) {
|
|
61
62
|
return env.INBOX.get(env.INBOX.idFromName('inbox'))
|
|
62
63
|
}
|
|
@@ -179,204 +180,16 @@ export default {
|
|
|
179
180
|
return json({ error: 'unauthorized' }, 401)
|
|
180
181
|
}
|
|
181
182
|
|
|
182
|
-
|
|
183
|
+
// The DO stub reaches both adapters as an InboxStore: `DurableObjectStub<
|
|
184
|
+
// Inbox>` expands workers-types' recursive RPC generics inside mcp.ts's
|
|
185
|
+
// registerTool inference and trips TS2589. This is the one cast.
|
|
186
|
+
const store = inboxStub(env) as unknown as InboxStore
|
|
183
187
|
|
|
184
188
|
// POST/GET/DELETE /mcp — MCP server (Streamable HTTP), same bearer token
|
|
185
189
|
if (pathname === '/mcp') {
|
|
186
|
-
return handleMcp(request, { env,
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// GET /api/messages?limit=&cursor=&label=
|
|
190
|
-
if (pathname === '/api/messages' && request.method === 'GET') {
|
|
191
|
-
const limit = Number(url.searchParams.get('limit') ?? '50')
|
|
192
|
-
const cursor = url.searchParams.get('cursor') ?? undefined
|
|
193
|
-
const label = url.searchParams.get('label') ?? undefined
|
|
194
|
-
return json(await stub.list(Number.isFinite(limit) ? limit : 50, cursor, label))
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
// GET /api/search?q=&limit= — keyword match over subject + text body
|
|
198
|
-
if (pathname === '/api/search' && request.method === 'GET') {
|
|
199
|
-
const q = url.searchParams.get('q') ?? ''
|
|
200
|
-
if (!q.trim()) return json({ error: 'required: q' }, 400)
|
|
201
|
-
const limit = Number(url.searchParams.get('limit') ?? '50')
|
|
202
|
-
return json({ messages: await stub.search(q.trim(), Number.isFinite(limit) ? limit : 50) })
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// GET /api/threads?limit=&cursor= — newest-first, one row per thread
|
|
206
|
-
if (pathname === '/api/threads' && request.method === 'GET') {
|
|
207
|
-
const limit = Number(url.searchParams.get('limit') ?? '50')
|
|
208
|
-
const cursor = url.searchParams.get('cursor') ?? undefined
|
|
209
|
-
return json(await stub.listThreads(Number.isFinite(limit) ? limit : 50, cursor))
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// GET /api/threads/:id — all messages in the thread, oldest-first
|
|
213
|
-
const threadMatch = pathname.match(/^\/api\/threads\/([^/]+)$/)
|
|
214
|
-
if (threadMatch && request.method === 'GET') {
|
|
215
|
-
const messages = await stub.getThread(threadMatch[1]!)
|
|
216
|
-
return messages.length ? json({ messages }) : json({ error: 'not found' }, 404)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// POST /api/send — immediate, or queued for the DO alarm when sendAt set
|
|
220
|
-
if (pathname === '/api/send' && request.method === 'POST') {
|
|
221
|
-
let body: SendBody
|
|
222
|
-
try {
|
|
223
|
-
body = (await request.json()) as SendBody
|
|
224
|
-
} catch {
|
|
225
|
-
return json({ error: 'invalid JSON body' }, 400)
|
|
226
|
-
}
|
|
227
|
-
const outcome = await performSend(env, stub, body)
|
|
228
|
-
return json(outcome.body, outcome.status)
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// GET /api/scheduled — pending + failed scheduled sends, soonest first
|
|
232
|
-
if (pathname === '/api/scheduled' && request.method === 'GET') {
|
|
233
|
-
const limit = Number(url.searchParams.get('limit') ?? '50')
|
|
234
|
-
return json({ scheduled: await stub.listScheduled(Number.isFinite(limit) ? limit : 50) })
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// DELETE /api/scheduled/:id — cancel a pending (or dismiss a failed) send
|
|
238
|
-
const schedMatch = pathname.match(/^\/api\/scheduled\/([^/]+)$/)
|
|
239
|
-
if (schedMatch && request.method === 'DELETE') {
|
|
240
|
-
const cancelled = await stub.cancelScheduled(schedMatch[1]!)
|
|
241
|
-
return cancelled ? json({ ok: true }) : json({ error: 'not a scheduled send' }, 404)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// POST /api/drafts — store an outbound message without sending it
|
|
245
|
-
if (pathname === '/api/drafts' && request.method === 'POST') {
|
|
246
|
-
let body: SendBody
|
|
247
|
-
try {
|
|
248
|
-
body = (await request.json()) as SendBody
|
|
249
|
-
} catch {
|
|
250
|
-
return json({ error: 'invalid JSON body' }, 400)
|
|
251
|
-
}
|
|
252
|
-
// Drafts may be partial — completeness is enforced at send time.
|
|
253
|
-
const id = newId()
|
|
254
|
-
const from = body.from ?? env.DEFAULT_FROM ?? ''
|
|
255
|
-
const text = body.text ?? ''
|
|
256
|
-
await stub.insert({
|
|
257
|
-
id,
|
|
258
|
-
status: 'draft',
|
|
259
|
-
direction: 'outbound',
|
|
260
|
-
in_reply_to: normalizeMsgId(body.inReplyTo),
|
|
261
|
-
message_id: `${id}@${from.split('@')[1] || 'localhost'}`,
|
|
262
|
-
from_addr: from,
|
|
263
|
-
to_addr: body.to ?? '',
|
|
264
|
-
subject: body.subject ?? '',
|
|
265
|
-
date: new Date().toISOString(),
|
|
266
|
-
snippet: makeSnippet(text, body.html ?? ''),
|
|
267
|
-
text_body: text,
|
|
268
|
-
html_body: body.html ?? '',
|
|
269
|
-
r2_key: '',
|
|
270
|
-
attachments_json: '[]',
|
|
271
|
-
size: text.length + (body.html?.length ?? 0),
|
|
272
|
-
})
|
|
273
|
-
return json({ ok: true, id, status: 'draft' }, 201)
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// GET /api/drafts
|
|
277
|
-
if (pathname === '/api/drafts' && request.method === 'GET') {
|
|
278
|
-
const limit = Number(url.searchParams.get('limit') ?? '50')
|
|
279
|
-
return json({ drafts: await stub.listDrafts(Number.isFinite(limit) ? limit : 50) })
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// PUT /api/drafts/:id | POST /api/drafts/:id/send
|
|
283
|
-
const draftMatch = pathname.match(/^\/api\/drafts\/([^/]+)(\/send)?$/)
|
|
284
|
-
if (draftMatch) {
|
|
285
|
-
const [, draftId, sendSuffix] = draftMatch
|
|
286
|
-
|
|
287
|
-
if (!sendSuffix && request.method === 'PUT') {
|
|
288
|
-
let body: SendBody
|
|
289
|
-
try {
|
|
290
|
-
body = (await request.json()) as SendBody
|
|
291
|
-
} catch {
|
|
292
|
-
return json({ error: 'invalid JSON body' }, 400)
|
|
293
|
-
}
|
|
294
|
-
const text = body.text
|
|
295
|
-
const html = body.html
|
|
296
|
-
const updated = await stub.updateDraft(draftId!, {
|
|
297
|
-
...(body.from !== undefined ? { from_addr: body.from } : {}),
|
|
298
|
-
...(body.to !== undefined ? { to_addr: body.to } : {}),
|
|
299
|
-
...(body.subject !== undefined ? { subject: body.subject } : {}),
|
|
300
|
-
...(text !== undefined ? { text_body: text } : {}),
|
|
301
|
-
...(html !== undefined ? { html_body: html } : {}),
|
|
302
|
-
...(body.inReplyTo !== undefined ? { in_reply_to: normalizeMsgId(body.inReplyTo) } : {}),
|
|
303
|
-
...(text !== undefined || html !== undefined
|
|
304
|
-
? { snippet: makeSnippet(text ?? '', html ?? '') }
|
|
305
|
-
: {}),
|
|
306
|
-
})
|
|
307
|
-
return updated ? json({ ok: true, id: draftId }) : json({ error: 'not a draft' }, 404)
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (sendSuffix && request.method === 'POST') {
|
|
311
|
-
const outcome = await performDraftSend(env, stub, draftId!)
|
|
312
|
-
return json(outcome.body, outcome.status)
|
|
313
|
-
}
|
|
314
|
-
if (!sendSuffix && request.method === 'GET') {
|
|
315
|
-
const draft = await stub.get(draftId!)
|
|
316
|
-
return draft && draft.status === 'draft' ? json(draft) : json({ error: 'not a draft' }, 404)
|
|
317
|
-
}
|
|
318
|
-
if (!sendSuffix && request.method === 'DELETE') {
|
|
319
|
-
const draft = await stub.get(draftId!)
|
|
320
|
-
if (!draft || draft.status !== 'draft') return json({ error: 'not a draft' }, 404)
|
|
321
|
-
await stub.delete(draftId!)
|
|
322
|
-
return json({ ok: true })
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// GET /api/messages/:id/attachments/:n — raw bytes of one attachment.
|
|
327
|
-
// Re-parses the R2 MIME blob per request (cheap at the 5 MiB cap) rather
|
|
328
|
-
// than pre-splitting attachments at write time.
|
|
329
|
-
const attMatch = pathname.match(/^\/api\/messages\/([^/]+)\/attachments\/(\d+)$/)
|
|
330
|
-
if (attMatch && request.method === 'GET') {
|
|
331
|
-
const msg = await stub.get(attMatch[1]!)
|
|
332
|
-
if (!msg) return json({ error: 'not found' }, 404)
|
|
333
|
-
if (!msg.r2_key) return json({ error: 'no raw MIME (outbound message)' }, 404)
|
|
334
|
-
const obj = await env.RAW.get(msg.r2_key)
|
|
335
|
-
if (!obj) return json({ error: 'raw MIME missing from R2' }, 404)
|
|
336
|
-
const parsed = await PostalMime.parse(await obj.arrayBuffer())
|
|
337
|
-
const n = Number(attMatch[2])
|
|
338
|
-
const att = (parsed.attachments ?? [])[n]
|
|
339
|
-
if (!att) return json({ error: `no attachment at index ${n}` }, 404)
|
|
340
|
-
const body =
|
|
341
|
-
typeof att.content === 'string' ? new TextEncoder().encode(att.content) : att.content
|
|
342
|
-
return new Response(body, {
|
|
343
|
-
headers: {
|
|
344
|
-
'content-type': att.mimeType || 'application/octet-stream',
|
|
345
|
-
'content-disposition': `attachment; filename="${(att.filename ?? 'attachment').replace(/["\r\n]/g, '')}"`,
|
|
346
|
-
'cache-control': 'no-store',
|
|
347
|
-
},
|
|
348
|
-
})
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// /api/messages/:id[/raw]
|
|
352
|
-
const match = pathname.match(/^\/api\/messages\/([^/]+)(\/raw)?$/)
|
|
353
|
-
if (match) {
|
|
354
|
-
const [, id, rawSuffix] = match
|
|
355
|
-
|
|
356
|
-
if (rawSuffix && request.method === 'GET') {
|
|
357
|
-
const msg = await stub.get(id!)
|
|
358
|
-
if (!msg) return json({ error: 'not found' }, 404)
|
|
359
|
-
if (!msg.r2_key) return json({ error: 'no raw MIME (outbound message)' }, 404)
|
|
360
|
-
const obj = await env.RAW.get(msg.r2_key)
|
|
361
|
-
if (!obj) return json({ error: 'raw MIME missing from R2' }, 404)
|
|
362
|
-
return new Response(obj.body, {
|
|
363
|
-
headers: { 'content-type': 'message/rfc822', 'cache-control': 'no-store' },
|
|
364
|
-
})
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
if (!rawSuffix && request.method === 'GET') {
|
|
368
|
-
const msg = await stub.get(id!)
|
|
369
|
-
return msg ? json(msg) : json({ error: 'not found' }, 404)
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
if (!rawSuffix && request.method === 'DELETE') {
|
|
373
|
-
const r2Key = await stub.delete(id!)
|
|
374
|
-
if (!r2Key) return json({ error: 'not found' }, 404)
|
|
375
|
-
await env.RAW.delete(r2Key)
|
|
376
|
-
return json({ ok: true })
|
|
377
|
-
}
|
|
190
|
+
return handleMcp(request, { env, store })
|
|
378
191
|
}
|
|
379
192
|
|
|
380
|
-
return
|
|
193
|
+
return handleApi(request, { env, store })
|
|
381
194
|
},
|
|
382
195
|
}
|
|
@@ -2,16 +2,9 @@ import { StreamableHTTPTransport } from '@hono/mcp'
|
|
|
2
2
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
3
3
|
import { Hono } from 'hono'
|
|
4
4
|
import { z } from 'zod'
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
MessageFull,
|
|
9
|
-
MessageMeta,
|
|
10
|
-
ScheduledMeta,
|
|
11
|
-
ThreadMeta,
|
|
12
|
-
} from './inbox-do'
|
|
13
|
-
import { type SendEnv, performDraftSend, performSend } from './send-op'
|
|
14
|
-
import { makeSnippet, newId, normalizeMsgId } from './shared'
|
|
5
|
+
import * as commands from './commands'
|
|
6
|
+
import type { SendEnv } from './commands'
|
|
7
|
+
import type { InboxStore } from './inbox-do'
|
|
15
8
|
|
|
16
9
|
/**
|
|
17
10
|
* MCP server over the inbox — the same operations as /api/*, exposed as
|
|
@@ -21,55 +14,29 @@ import { makeSnippet, newId, normalizeMsgId } from './shared'
|
|
|
21
14
|
*
|
|
22
15
|
* Auth is the SAME bearer token as /api/* — the caller checks it before
|
|
23
16
|
* invoking this, and MCP clients send it as a normal Authorization header.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
17
|
+
*
|
|
18
|
+
* This file is wiring, not logic: every mutation is a call into `commands.ts`
|
|
19
|
+
* — the same function `api.ts` calls — and every read is a single `store.*`
|
|
20
|
+
* call. Nothing here decides what a mutation does.
|
|
26
21
|
*/
|
|
27
22
|
|
|
28
23
|
const text = (v: unknown) => ({
|
|
29
24
|
content: [{ type: 'text' as const, text: JSON.stringify(v, null, 2) }],
|
|
30
25
|
})
|
|
31
26
|
|
|
32
|
-
/** Unwrap a
|
|
27
|
+
/** Unwrap a command Outcome into MCP result-or-error. */
|
|
33
28
|
const outcome = (o: { status: number; body: Record<string, unknown> }) => {
|
|
34
29
|
if (o.status >= 400) throw new Error(String(o.body.error ?? `failed (${o.status})`))
|
|
35
30
|
return text(o.body)
|
|
36
31
|
}
|
|
37
32
|
|
|
38
|
-
/**
|
|
39
|
-
* The DO surface the tools use, with plain Promise returns. The raw
|
|
40
|
-
* DurableObjectStub<Inbox> type expands workers-types' recursive RPC
|
|
41
|
-
* serialization generics inside every registerTool inference and trips
|
|
42
|
-
* TS2589 — this narrow view is the same calls without the type machinery.
|
|
43
|
-
*/
|
|
44
|
-
interface InboxApi {
|
|
45
|
-
list(
|
|
46
|
-
limit: number,
|
|
47
|
-
cursor?: string,
|
|
48
|
-
label?: string,
|
|
49
|
-
): Promise<{ messages: MessageMeta[]; nextCursor: string | null }>
|
|
50
|
-
listThreads(
|
|
51
|
-
limit: number,
|
|
52
|
-
cursor?: string,
|
|
53
|
-
): Promise<{ threads: ThreadMeta[]; nextCursor: string | null }>
|
|
54
|
-
getThread(threadId: string): Promise<MessageFull[]>
|
|
55
|
-
get(id: string): Promise<MessageFull | null>
|
|
56
|
-
search(q: string, limit: number): Promise<MessageMeta[]>
|
|
57
|
-
insert(msg: InsertMessage): Promise<{ threadId: string }>
|
|
58
|
-
listDrafts(limit: number): Promise<MessageMeta[]>
|
|
59
|
-
updateDraft(id: string, fields: Record<string, string>): Promise<boolean>
|
|
60
|
-
listScheduled(limit: number): Promise<ScheduledMeta[]>
|
|
61
|
-
cancelScheduled(id: string): Promise<boolean>
|
|
62
|
-
delete(id: string): Promise<string | null>
|
|
63
|
-
}
|
|
64
|
-
|
|
65
33
|
interface McpDeps {
|
|
66
34
|
env: SendEnv & { RAW: R2Bucket }
|
|
67
|
-
|
|
35
|
+
store: InboxStore
|
|
68
36
|
}
|
|
69
37
|
|
|
70
38
|
export async function handleMcp(request: Request, deps: McpDeps): Promise<Response> {
|
|
71
|
-
const { env } = deps
|
|
72
|
-
const stub = deps.stub as unknown as InboxApi
|
|
39
|
+
const { env, store } = deps
|
|
73
40
|
const server = new McpServer({ name: 'inbox', version: '1.0.0' })
|
|
74
41
|
|
|
75
42
|
server.registerTool(
|
|
@@ -79,7 +46,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
79
46
|
'List conversation threads, newest first. Each row is the latest message of its thread plus thread_count. Page with cursor = the last id from the previous page.',
|
|
80
47
|
inputSchema: { limit: z.number().optional(), cursor: z.string().optional() },
|
|
81
48
|
},
|
|
82
|
-
async ({ limit, cursor }) => text(await
|
|
49
|
+
async ({ limit, cursor }) => text(await store.listThreads(limit ?? 50, cursor)),
|
|
83
50
|
)
|
|
84
51
|
server.registerTool(
|
|
85
52
|
'read_thread',
|
|
@@ -89,7 +56,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
89
56
|
inputSchema: { thread_id: z.string() },
|
|
90
57
|
},
|
|
91
58
|
async ({ thread_id }) => {
|
|
92
|
-
const messages = await
|
|
59
|
+
const messages = await store.getThread(thread_id)
|
|
93
60
|
if (!messages.length) throw new Error('thread not found')
|
|
94
61
|
return text({ messages })
|
|
95
62
|
},
|
|
@@ -105,7 +72,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
105
72
|
label: z.string().optional(),
|
|
106
73
|
},
|
|
107
74
|
},
|
|
108
|
-
async ({ limit, cursor, label }) => text(await
|
|
75
|
+
async ({ limit, cursor, label }) => text(await store.list(limit ?? 50, cursor, label)),
|
|
109
76
|
)
|
|
110
77
|
server.registerTool(
|
|
111
78
|
'read_message',
|
|
@@ -114,7 +81,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
114
81
|
inputSchema: { id: z.string() },
|
|
115
82
|
},
|
|
116
83
|
async ({ id }) => {
|
|
117
|
-
const msg = await
|
|
84
|
+
const msg = await store.get(id)
|
|
118
85
|
if (!msg) throw new Error('message not found')
|
|
119
86
|
return text(msg)
|
|
120
87
|
},
|
|
@@ -125,7 +92,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
125
92
|
description: 'Keyword search over subject + text body (case-insensitive), newest first.',
|
|
126
93
|
inputSchema: { q: z.string(), limit: z.number().optional() },
|
|
127
94
|
},
|
|
128
|
-
async ({ q, limit }) => text({ messages: await
|
|
95
|
+
async ({ q, limit }) => text({ messages: await store.search(q, limit ?? 50) }),
|
|
129
96
|
)
|
|
130
97
|
server.registerTool(
|
|
131
98
|
'send_email',
|
|
@@ -144,7 +111,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
144
111
|
},
|
|
145
112
|
async (input) =>
|
|
146
113
|
outcome(
|
|
147
|
-
await
|
|
114
|
+
await commands.send(env, store, {
|
|
148
115
|
to: input.to,
|
|
149
116
|
subject: input.subject,
|
|
150
117
|
text: input.text,
|
|
@@ -169,34 +136,22 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
169
136
|
in_reply_to: z.string().optional(),
|
|
170
137
|
},
|
|
171
138
|
},
|
|
172
|
-
async (input) =>
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
to_addr: input.to ?? '',
|
|
184
|
-
subject: input.subject ?? '',
|
|
185
|
-
date: new Date().toISOString(),
|
|
186
|
-
snippet: makeSnippet(bodyText, input.html ?? ''),
|
|
187
|
-
text_body: bodyText,
|
|
188
|
-
html_body: input.html ?? '',
|
|
189
|
-
r2_key: '',
|
|
190
|
-
attachments_json: '[]',
|
|
191
|
-
size: bodyText.length + (input.html?.length ?? 0),
|
|
192
|
-
})
|
|
193
|
-
return text({ ok: true, id, status: 'draft' })
|
|
194
|
-
},
|
|
139
|
+
async (input) =>
|
|
140
|
+
outcome(
|
|
141
|
+
await commands.createDraft(env, store, {
|
|
142
|
+
to: input.to,
|
|
143
|
+
subject: input.subject,
|
|
144
|
+
text: input.text,
|
|
145
|
+
html: input.html,
|
|
146
|
+
from: input.from,
|
|
147
|
+
inReplyTo: input.in_reply_to,
|
|
148
|
+
}),
|
|
149
|
+
),
|
|
195
150
|
)
|
|
196
151
|
server.registerTool(
|
|
197
152
|
'list_drafts',
|
|
198
153
|
{ description: 'List unsent drafts, newest first.' },
|
|
199
|
-
async () => text({ drafts: await
|
|
154
|
+
async () => text({ drafts: await store.listDrafts(50) }),
|
|
200
155
|
)
|
|
201
156
|
server.registerTool(
|
|
202
157
|
'update_draft',
|
|
@@ -212,23 +167,17 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
212
167
|
in_reply_to: z.string().optional(),
|
|
213
168
|
},
|
|
214
169
|
},
|
|
215
|
-
async ({ id, ...input }) =>
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
? { snippet: makeSnippet(input.text ?? '', input.html ?? '') }
|
|
227
|
-
: {}),
|
|
228
|
-
})
|
|
229
|
-
if (!updated) throw new Error('not a draft')
|
|
230
|
-
return text({ ok: true, id })
|
|
231
|
-
},
|
|
170
|
+
async ({ id, ...input }) =>
|
|
171
|
+
outcome(
|
|
172
|
+
await commands.updateDraft(store, id, {
|
|
173
|
+
to: input.to,
|
|
174
|
+
subject: input.subject,
|
|
175
|
+
text: input.text,
|
|
176
|
+
html: input.html,
|
|
177
|
+
from: input.from,
|
|
178
|
+
inReplyTo: input.in_reply_to,
|
|
179
|
+
}),
|
|
180
|
+
),
|
|
232
181
|
)
|
|
233
182
|
server.registerTool(
|
|
234
183
|
'send_draft',
|
|
@@ -236,7 +185,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
236
185
|
description: 'Send an existing draft (must have to, subject, and text or html).',
|
|
237
186
|
inputSchema: { id: z.string() },
|
|
238
187
|
},
|
|
239
|
-
async ({ id }) => outcome(await
|
|
188
|
+
async ({ id }) => outcome(await commands.sendDraft(env, store, id)),
|
|
240
189
|
)
|
|
241
190
|
server.registerTool(
|
|
242
191
|
'list_scheduled',
|
|
@@ -244,7 +193,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
244
193
|
description:
|
|
245
194
|
'List pending scheduled sends (and failed ones, with send_error), soonest first.',
|
|
246
195
|
},
|
|
247
|
-
async () => text({ scheduled: await
|
|
196
|
+
async () => text({ scheduled: await store.listScheduled(50) }),
|
|
248
197
|
)
|
|
249
198
|
server.registerTool(
|
|
250
199
|
'cancel_scheduled',
|
|
@@ -252,10 +201,7 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
252
201
|
description: 'Cancel a pending scheduled send (or dismiss a failed one). Deletes the row.',
|
|
253
202
|
inputSchema: { id: z.string() },
|
|
254
203
|
},
|
|
255
|
-
async ({ id }) =>
|
|
256
|
-
if (!(await stub.cancelScheduled(id))) throw new Error('not a scheduled send')
|
|
257
|
-
return text({ ok: true })
|
|
258
|
-
},
|
|
204
|
+
async ({ id }) => outcome(await commands.cancelScheduled(store, id)),
|
|
259
205
|
)
|
|
260
206
|
server.registerTool(
|
|
261
207
|
'delete_message',
|
|
@@ -264,10 +210,13 @@ export async function handleMcp(request: Request, deps: McpDeps): Promise<Respon
|
|
|
264
210
|
inputSchema: { id: z.string() },
|
|
265
211
|
},
|
|
266
212
|
async ({ id }) => {
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
213
|
+
const result = await commands.deleteMessage(env, store, id)
|
|
214
|
+
// The one place an adapter renders a command's error in its own words:
|
|
215
|
+
// this tool has always said 'message not found' where the JSON route
|
|
216
|
+
// says 'not found', and MCP clients see the string. The command decides
|
|
217
|
+
// WHETHER it is a 404; the tool only spells it.
|
|
218
|
+
if (result.status === 404) throw new Error('message not found')
|
|
219
|
+
return outcome(result)
|
|
271
220
|
},
|
|
272
221
|
)
|
|
273
222
|
|