inboxer 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/LICENSE +21 -0
- package/README.md +376 -2
- package/dist/app.d.ts +6 -0
- package/dist/app.js +56 -0
- package/dist/auth.d.ts +33 -0
- package/dist/auth.js +111 -0
- package/dist/config.d.ts +23 -0
- package/dist/config.js +90 -0
- package/dist/core/address.d.ts +6 -0
- package/dist/core/address.js +16 -0
- package/dist/core/attachments.d.ts +9 -0
- package/dist/core/attachments.js +30 -0
- package/dist/core/context.d.ts +10 -0
- package/dist/core/context.js +21 -0
- package/dist/core/mail.d.ts +170 -0
- package/dist/core/mail.js +656 -0
- package/dist/core/sync.d.ts +16 -0
- package/dist/core/sync.js +44 -0
- package/dist/core/text.d.ts +8 -0
- package/dist/core/text.js +45 -0
- package/dist/core/threads.d.ts +21 -0
- package/dist/core/threads.js +81 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +51 -0
- package/dist/mcp.d.ts +5 -0
- package/dist/mcp.js +198 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.js +11 -0
- package/dist/provider/resend.d.ts +8 -0
- package/dist/provider/resend.js +184 -0
- package/dist/provider/types.d.ts +77 -0
- package/dist/provider/types.js +6 -0
- package/dist/store.d.ts +54 -0
- package/dist/store.js +148 -0
- package/dist/types.d.ts +138 -0
- package/dist/types.js +1 -0
- package/dist/ui/assets.d.ts +8 -0
- package/dist/ui/assets.js +13 -0
- package/dist/ui/routes.d.ts +11 -0
- package/dist/ui/routes.js +232 -0
- package/dist/ui/static/app.js +27 -0
- package/dist/ui/static/htmx.min.js +1 -0
- package/dist/ui/static/style.css +179 -0
- package/dist/ui/strings.d.ts +67 -0
- package/dist/ui/strings.js +67 -0
- package/dist/ui/views.d.ts +45 -0
- package/dist/ui/views.js +275 -0
- package/package.json +81 -4
- package/skills/inboxer/SKILL.md +118 -0
- package/src/app.ts +56 -0
- package/src/auth.ts +119 -0
- package/src/config.ts +92 -0
- package/src/core/address.ts +19 -0
- package/src/core/attachments.ts +33 -0
- package/src/core/context.ts +28 -0
- package/src/core/mail.ts +737 -0
- package/src/core/sync.ts +51 -0
- package/src/core/text.ts +52 -0
- package/src/core/threads.ts +102 -0
- package/src/index.ts +93 -0
- package/src/mcp.ts +233 -0
- package/src/provider/index.ts +14 -0
- package/src/provider/resend.ts +235 -0
- package/src/provider/types.ts +82 -0
- package/src/store.ts +187 -0
- package/src/types.ts +140 -0
- package/src/ui/assets.ts +21 -0
- package/src/ui/routes.ts +252 -0
- package/src/ui/static/app.js +27 -0
- package/src/ui/static/htmx.min.js +1 -0
- package/src/ui/static/style.css +179 -0
- package/src/ui/strings.ts +73 -0
- package/src/ui/views.ts +315 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Webhook, WebhookVerificationError } from "svix";
|
|
2
|
+
import { WebhookSignatureError, } from "./types.js";
|
|
3
|
+
const API = "https://api.resend.com";
|
|
4
|
+
const PAGE_LIMIT = 100;
|
|
5
|
+
const WEBHOOK_STATUS = {
|
|
6
|
+
"email.sent": "sent",
|
|
7
|
+
"email.delivered": "delivered",
|
|
8
|
+
"email.bounced": "bounced",
|
|
9
|
+
"email.failed": "failed",
|
|
10
|
+
"email.suppressed": "failed",
|
|
11
|
+
};
|
|
12
|
+
/** `last_event` of a sent email. Engagement events imply delivery; pending ones carry no status yet. */
|
|
13
|
+
const LAST_EVENT_STATUS = {
|
|
14
|
+
queued: null,
|
|
15
|
+
scheduled: null,
|
|
16
|
+
delivery_delayed: null,
|
|
17
|
+
sent: "sent",
|
|
18
|
+
delivered: "delivered",
|
|
19
|
+
opened: "delivered",
|
|
20
|
+
clicked: "delivered",
|
|
21
|
+
complained: "delivered",
|
|
22
|
+
bounced: "bounced",
|
|
23
|
+
failed: "failed",
|
|
24
|
+
suppressed: "failed",
|
|
25
|
+
canceled: "failed",
|
|
26
|
+
};
|
|
27
|
+
export function createResendProvider(config) {
|
|
28
|
+
const { apiKey, webhookSecret } = config;
|
|
29
|
+
if (typeof apiKey !== "string" || !apiKey)
|
|
30
|
+
throw new Error("provider.apiKey is required for the resend provider");
|
|
31
|
+
if (typeof webhookSecret !== "string" || !webhookSecret) {
|
|
32
|
+
throw new Error("provider.webhookSecret is required for the resend provider");
|
|
33
|
+
}
|
|
34
|
+
const webhook = new Webhook(webhookSecret);
|
|
35
|
+
async function request(method, path, body, headers = {}) {
|
|
36
|
+
const res = await fetch(API + path, {
|
|
37
|
+
method,
|
|
38
|
+
headers: {
|
|
39
|
+
Authorization: `Bearer ${apiKey}`,
|
|
40
|
+
"User-Agent": "inboxer",
|
|
41
|
+
...(body === undefined ? {} : { "Content-Type": "application/json" }),
|
|
42
|
+
...headers,
|
|
43
|
+
},
|
|
44
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
45
|
+
});
|
|
46
|
+
if (!res.ok) {
|
|
47
|
+
throw new Error(`Resend ${method} ${path} failed: ${res.status} ${await res.text()}`);
|
|
48
|
+
}
|
|
49
|
+
return (await res.json());
|
|
50
|
+
}
|
|
51
|
+
async function* paginate(path, since) {
|
|
52
|
+
let after = null;
|
|
53
|
+
while (true) {
|
|
54
|
+
const query = `limit=${PAGE_LIMIT}` + (after ? `&after=${encodeURIComponent(after)}` : "");
|
|
55
|
+
const page = await request("GET", `${path}?${query}`);
|
|
56
|
+
for (const item of page.data) {
|
|
57
|
+
if (parseResendDate(item.created_at) < since)
|
|
58
|
+
return;
|
|
59
|
+
yield item;
|
|
60
|
+
}
|
|
61
|
+
if (!page.has_more || page.data.length === 0)
|
|
62
|
+
return;
|
|
63
|
+
after = page.data[page.data.length - 1].id;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
name: "resend",
|
|
68
|
+
parseWebhook({ headers, body }) {
|
|
69
|
+
try {
|
|
70
|
+
webhook.verify(body, {
|
|
71
|
+
"svix-id": headers.get("svix-id") ?? "",
|
|
72
|
+
"svix-timestamp": headers.get("svix-timestamp") ?? "",
|
|
73
|
+
"svix-signature": headers.get("svix-signature") ?? "",
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
if (err instanceof WebhookVerificationError)
|
|
78
|
+
throw new WebhookSignatureError(err.message);
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
const payload = JSON.parse(body);
|
|
82
|
+
if (payload.type === "email.received")
|
|
83
|
+
return [{ type: "received", providerId: payload.data.email_id }];
|
|
84
|
+
const status = WEBHOOK_STATUS[payload.type];
|
|
85
|
+
if (!status)
|
|
86
|
+
return [];
|
|
87
|
+
return [{ type: "status", providerId: payload.data.email_id, status, messageId: payload.data.message_id ?? null }];
|
|
88
|
+
},
|
|
89
|
+
async getInbound(providerId) {
|
|
90
|
+
const email = await request("GET", `/emails/receiving/${encodeURIComponent(providerId)}`);
|
|
91
|
+
const headers = normalizeHeaders(email.headers ?? {});
|
|
92
|
+
return {
|
|
93
|
+
providerId: email.id,
|
|
94
|
+
messageId: email.message_id ?? headers["message-id"] ?? null,
|
|
95
|
+
inReplyTo: headers["in-reply-to"]?.trim() || null,
|
|
96
|
+
references: (headers["references"] ?? "").split(/\s+/).filter(Boolean),
|
|
97
|
+
from: email.from,
|
|
98
|
+
to: email.to ?? [],
|
|
99
|
+
cc: email.cc ?? [],
|
|
100
|
+
bcc: email.bcc ?? [],
|
|
101
|
+
replyTo: email.reply_to ?? [],
|
|
102
|
+
deliveredTo: email.received_for ?? [],
|
|
103
|
+
subject: email.subject ?? "",
|
|
104
|
+
text: email.text,
|
|
105
|
+
html: email.html,
|
|
106
|
+
headers,
|
|
107
|
+
attachments: (email.attachments ?? []).map(toAttachmentMeta),
|
|
108
|
+
receivedAt: parseResendDate(email.created_at).toISOString(),
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
async *listInbound(since) {
|
|
112
|
+
for await (const item of paginate("/emails/receiving", since)) {
|
|
113
|
+
yield item.id;
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
async *listOutbound(since) {
|
|
117
|
+
for await (const item of paginate("/emails", since)) {
|
|
118
|
+
const status = LAST_EVENT_STATUS[item.last_event];
|
|
119
|
+
if (status === undefined)
|
|
120
|
+
throw new Error(`Unknown Resend last_event: ${item.last_event}`);
|
|
121
|
+
if (status)
|
|
122
|
+
yield { providerId: item.id, status };
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
async send(message, idempotencyKey) {
|
|
126
|
+
const body = {
|
|
127
|
+
from: message.from,
|
|
128
|
+
to: message.to,
|
|
129
|
+
subject: message.subject,
|
|
130
|
+
text: message.text,
|
|
131
|
+
html: message.html,
|
|
132
|
+
headers: message.headers,
|
|
133
|
+
};
|
|
134
|
+
if (message.cc.length)
|
|
135
|
+
body.cc = message.cc;
|
|
136
|
+
if (message.bcc.length)
|
|
137
|
+
body.bcc = message.bcc;
|
|
138
|
+
if (message.replyTo.length)
|
|
139
|
+
body.reply_to = message.replyTo;
|
|
140
|
+
if (message.attachments.length) {
|
|
141
|
+
body.attachments = message.attachments.map(a => ({
|
|
142
|
+
filename: a.filename,
|
|
143
|
+
content: Buffer.from(a.content).toString("base64"),
|
|
144
|
+
content_type: a.contentType,
|
|
145
|
+
}));
|
|
146
|
+
}
|
|
147
|
+
const res = await request("POST", "/emails", body, { "Idempotency-Key": idempotencyKey });
|
|
148
|
+
return { providerId: res.id };
|
|
149
|
+
},
|
|
150
|
+
async getAttachment(providerMessageId, attachmentId) {
|
|
151
|
+
const { download_url } = await request("GET", `/emails/receiving/${encodeURIComponent(providerMessageId)}/attachments/${encodeURIComponent(attachmentId)}`);
|
|
152
|
+
const res = await fetch(download_url);
|
|
153
|
+
if (!res.ok)
|
|
154
|
+
throw new Error(`Resend attachment ${attachmentId} download failed: ${res.status}`);
|
|
155
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function toAttachmentMeta(a) {
|
|
160
|
+
const contentId = a.content_id ? a.content_id.replace(/^<|>$/g, "") : null;
|
|
161
|
+
return {
|
|
162
|
+
id: a.id,
|
|
163
|
+
filename: a.filename,
|
|
164
|
+
content_type: a.content_type,
|
|
165
|
+
size: a.size ?? null,
|
|
166
|
+
inline: a.content_disposition === "inline" && !!contentId,
|
|
167
|
+
content_id: contentId,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
function normalizeHeaders(headers) {
|
|
171
|
+
const out = {};
|
|
172
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
173
|
+
out[key.toLowerCase()] = Array.isArray(value) ? value.join(", ") : String(value);
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
/** The sent-email list uses `YYYY-MM-DD HH:MM:SS.ffffff+00`; the receiving API uses ISO 8601. */
|
|
178
|
+
export function parseResendDate(value) {
|
|
179
|
+
const iso = value.includes("T") ? value : value.replace(" ", "T").replace(/\+00$/, "Z");
|
|
180
|
+
const date = new Date(iso);
|
|
181
|
+
if (Number.isNaN(date.getTime()))
|
|
182
|
+
throw new Error(`Unparseable Resend date: ${value}`);
|
|
183
|
+
return date;
|
|
184
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { AttachmentMeta } from "../types.ts";
|
|
2
|
+
export type DeliveryStatus = "sent" | "delivered" | "bounced" | "failed";
|
|
3
|
+
export type ProviderEvent = {
|
|
4
|
+
type: "received";
|
|
5
|
+
providerId: string;
|
|
6
|
+
} | {
|
|
7
|
+
type: "status";
|
|
8
|
+
providerId: string;
|
|
9
|
+
status: DeliveryStatus;
|
|
10
|
+
messageId: string | null;
|
|
11
|
+
};
|
|
12
|
+
export interface InboundMessage {
|
|
13
|
+
providerId: string;
|
|
14
|
+
messageId: string | null;
|
|
15
|
+
inReplyTo: string | null;
|
|
16
|
+
references: string[];
|
|
17
|
+
from: string;
|
|
18
|
+
to: string[];
|
|
19
|
+
cc: string[];
|
|
20
|
+
bcc: string[];
|
|
21
|
+
replyTo: string[];
|
|
22
|
+
/** Envelope recipients, which include addresses that only appear in Bcc. */
|
|
23
|
+
deliveredTo: string[];
|
|
24
|
+
subject: string;
|
|
25
|
+
text: string | null;
|
|
26
|
+
html: string | null;
|
|
27
|
+
headers: Record<string, string>;
|
|
28
|
+
attachments: AttachmentMeta[];
|
|
29
|
+
receivedAt: string;
|
|
30
|
+
}
|
|
31
|
+
export interface OutboundMessage {
|
|
32
|
+
from: string;
|
|
33
|
+
to: string[];
|
|
34
|
+
cc: string[];
|
|
35
|
+
bcc: string[];
|
|
36
|
+
replyTo: string[];
|
|
37
|
+
subject: string;
|
|
38
|
+
text: string;
|
|
39
|
+
html: string;
|
|
40
|
+
headers: Record<string, string>;
|
|
41
|
+
attachments: OutboundAttachment[];
|
|
42
|
+
}
|
|
43
|
+
export interface OutboundAttachment {
|
|
44
|
+
filename: string;
|
|
45
|
+
contentType: string;
|
|
46
|
+
content: Uint8Array;
|
|
47
|
+
}
|
|
48
|
+
export interface OutboundStatus {
|
|
49
|
+
providerId: string;
|
|
50
|
+
status: DeliveryStatus;
|
|
51
|
+
}
|
|
52
|
+
export interface WebhookRequest {
|
|
53
|
+
headers: Headers;
|
|
54
|
+
body: string;
|
|
55
|
+
}
|
|
56
|
+
/** Everything Inboxer needs from an email service. Implementations normalize the service's shapes. */
|
|
57
|
+
export interface MailProvider {
|
|
58
|
+
/** Stable identifier, stored on every message and used as the webhook route: `/webhooks/<name>`. */
|
|
59
|
+
readonly name: string;
|
|
60
|
+
/** Verifies the signature and returns the events Inboxer cares about. Throws `WebhookSignatureError`. */
|
|
61
|
+
parseWebhook(req: WebhookRequest): ProviderEvent[];
|
|
62
|
+
getInbound(providerId: string): Promise<InboundMessage>;
|
|
63
|
+
/** Ids of inbound messages received at or after `since`. */
|
|
64
|
+
listInbound(since: Date): AsyncIterable<string>;
|
|
65
|
+
/** Delivery status of outbound messages created at or after `since`. */
|
|
66
|
+
listOutbound(since: Date): AsyncIterable<OutboundStatus>;
|
|
67
|
+
/** Sends once per `idempotencyKey`; a retry with the same key must not send again. */
|
|
68
|
+
send(message: OutboundMessage, idempotencyKey: string): Promise<{
|
|
69
|
+
providerId: string;
|
|
70
|
+
}>;
|
|
71
|
+
/** The bytes of an inbound attachment. */
|
|
72
|
+
getAttachment(providerMessageId: string, attachmentId: string): Promise<Uint8Array>;
|
|
73
|
+
}
|
|
74
|
+
export type ProviderFactory = (config: Record<string, unknown>) => MailProvider;
|
|
75
|
+
export declare class WebhookSignatureError extends Error {
|
|
76
|
+
constructor(message: string);
|
|
77
|
+
}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import DeepBase from "deepbase";
|
|
2
|
+
import type { Actor, AuditEntry, Contact, Context, LoginLink, Message, Session, Thread } from "./types.ts";
|
|
3
|
+
export declare function sha1(value: string): string;
|
|
4
|
+
export declare function sha256(value: string): string;
|
|
5
|
+
export declare function nid(): string;
|
|
6
|
+
/**
|
|
7
|
+
* DeepBase paths and the indexes kept beside them. DeepBase has no secondary indexes, so every
|
|
8
|
+
* index is written in the same serialized operation as the record it points to.
|
|
9
|
+
*/
|
|
10
|
+
export declare class Store {
|
|
11
|
+
readonly db: DeepBase;
|
|
12
|
+
private queue;
|
|
13
|
+
constructor(dataDir: string);
|
|
14
|
+
connect(): Promise<void>;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
/** Runs `fn` after every previously queued write. Inboxer runs in one process, so this makes read-modify-write atomic. */
|
|
17
|
+
serialize<T>(fn: () => Promise<T>): Promise<T>;
|
|
18
|
+
getMessage(id: string): Promise<Message | null>;
|
|
19
|
+
requireMessage(id: string): Promise<Message>;
|
|
20
|
+
putMessage(message: Message): Promise<unknown>;
|
|
21
|
+
allMessages(): Promise<Message[]>;
|
|
22
|
+
getThread(id: string): Promise<Thread | null>;
|
|
23
|
+
requireThread(id: string): Promise<Thread>;
|
|
24
|
+
putThread(thread: Thread): Promise<unknown>;
|
|
25
|
+
deleteThread(id: string): Promise<unknown>;
|
|
26
|
+
allThreads(): Promise<Thread[]>;
|
|
27
|
+
getContact(address: string): Promise<Contact | null>;
|
|
28
|
+
putContact(contact: Contact): Promise<unknown>;
|
|
29
|
+
allContacts(): Promise<Contact[]>;
|
|
30
|
+
/** Kept apart from `threads/`, whose summary is rebuilt from the messages on every change. */
|
|
31
|
+
getThreadContext(threadId: string): Promise<Context | null>;
|
|
32
|
+
putThreadContext(threadId: string, context: Context): Promise<unknown>;
|
|
33
|
+
allThreadContexts(): Promise<Record<string, Context>>;
|
|
34
|
+
draftIds(): Promise<string[]>;
|
|
35
|
+
setDraft(id: string, pending: boolean): Promise<unknown>;
|
|
36
|
+
lookupMessageId(messageId: string): Promise<string | null>;
|
|
37
|
+
indexMessageId(messageId: string, id: string): Promise<unknown>;
|
|
38
|
+
lookupProviderId(provider: string, providerId: string): Promise<string | null>;
|
|
39
|
+
indexProviderId(provider: string, providerId: string, id: string): Promise<unknown>;
|
|
40
|
+
audit(action: string, actor: Actor, target: string | null, detail?: Record<string, unknown>): Promise<unknown>;
|
|
41
|
+
auditEntries(): Promise<AuditEntry[]>;
|
|
42
|
+
getLoginLink(hash: string): Promise<LoginLink | null>;
|
|
43
|
+
putLoginLink(hash: string, link: LoginLink): Promise<unknown>;
|
|
44
|
+
deleteLoginLink(hash: string): Promise<unknown>;
|
|
45
|
+
getSession(id: string): Promise<Session | null>;
|
|
46
|
+
putSession(session: Session): Promise<unknown>;
|
|
47
|
+
deleteSession(id: string): Promise<unknown>;
|
|
48
|
+
allSessions(): Promise<Session[]>;
|
|
49
|
+
getSyncCursor(): Promise<string | null>;
|
|
50
|
+
setSyncCursor(at: string): Promise<unknown>;
|
|
51
|
+
}
|
|
52
|
+
export declare class NotFoundError extends Error {
|
|
53
|
+
constructor(message: string);
|
|
54
|
+
}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import DeepBase from "deepbase";
|
|
3
|
+
import SqliteDriver from "deepbase-sqlite";
|
|
4
|
+
export function sha1(value) {
|
|
5
|
+
return createHash("sha1").update(value).digest("hex");
|
|
6
|
+
}
|
|
7
|
+
export function sha256(value) {
|
|
8
|
+
return createHash("sha256").update(value).digest("hex");
|
|
9
|
+
}
|
|
10
|
+
export function nid() {
|
|
11
|
+
return randomBytes(12).toString("base64url");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* DeepBase paths and the indexes kept beside them. DeepBase has no secondary indexes, so every
|
|
15
|
+
* index is written in the same serialized operation as the record it points to.
|
|
16
|
+
*/
|
|
17
|
+
export class Store {
|
|
18
|
+
db;
|
|
19
|
+
queue = Promise.resolve();
|
|
20
|
+
constructor(dataDir) {
|
|
21
|
+
this.db = new DeepBase(new SqliteDriver({ path: dataDir, name: "inboxer" }));
|
|
22
|
+
}
|
|
23
|
+
async connect() {
|
|
24
|
+
await this.db.connect();
|
|
25
|
+
}
|
|
26
|
+
async close() {
|
|
27
|
+
await this.queue.catch(() => { });
|
|
28
|
+
await this.db.disconnect();
|
|
29
|
+
}
|
|
30
|
+
/** Runs `fn` after every previously queued write. Inboxer runs in one process, so this makes read-modify-write atomic. */
|
|
31
|
+
serialize(fn) {
|
|
32
|
+
const run = this.queue.then(fn, fn);
|
|
33
|
+
this.queue = run.catch(() => { });
|
|
34
|
+
return run;
|
|
35
|
+
}
|
|
36
|
+
getMessage(id) {
|
|
37
|
+
return this.db.get("messages", id);
|
|
38
|
+
}
|
|
39
|
+
async requireMessage(id) {
|
|
40
|
+
const message = await this.getMessage(id);
|
|
41
|
+
if (!message)
|
|
42
|
+
throw new NotFoundError(`Message ${id} not found`);
|
|
43
|
+
return message;
|
|
44
|
+
}
|
|
45
|
+
putMessage(message) {
|
|
46
|
+
return this.db.set("messages", message.id, message);
|
|
47
|
+
}
|
|
48
|
+
async allMessages() {
|
|
49
|
+
return Object.values((await this.db.get("messages")) ?? {});
|
|
50
|
+
}
|
|
51
|
+
getThread(id) {
|
|
52
|
+
return this.db.get("threads", id);
|
|
53
|
+
}
|
|
54
|
+
async requireThread(id) {
|
|
55
|
+
const thread = await this.getThread(id);
|
|
56
|
+
if (!thread)
|
|
57
|
+
throw new NotFoundError(`Thread ${id} not found`);
|
|
58
|
+
return thread;
|
|
59
|
+
}
|
|
60
|
+
putThread(thread) {
|
|
61
|
+
return this.db.set("threads", thread.id, thread);
|
|
62
|
+
}
|
|
63
|
+
deleteThread(id) {
|
|
64
|
+
return this.db.del("threads", id);
|
|
65
|
+
}
|
|
66
|
+
async allThreads() {
|
|
67
|
+
return Object.values((await this.db.get("threads")) ?? {});
|
|
68
|
+
}
|
|
69
|
+
getContact(address) {
|
|
70
|
+
return this.db.get("contacts", sha1(address));
|
|
71
|
+
}
|
|
72
|
+
putContact(contact) {
|
|
73
|
+
return this.db.set("contacts", sha1(contact.address), contact);
|
|
74
|
+
}
|
|
75
|
+
async allContacts() {
|
|
76
|
+
return Object.values((await this.db.get("contacts")) ?? {});
|
|
77
|
+
}
|
|
78
|
+
/** Kept apart from `threads/`, whose summary is rebuilt from the messages on every change. */
|
|
79
|
+
getThreadContext(threadId) {
|
|
80
|
+
return this.db.get("thread_context", threadId);
|
|
81
|
+
}
|
|
82
|
+
putThreadContext(threadId, context) {
|
|
83
|
+
return this.db.set("thread_context", threadId, context);
|
|
84
|
+
}
|
|
85
|
+
async allThreadContexts() {
|
|
86
|
+
return (await this.db.get("thread_context")) ?? {};
|
|
87
|
+
}
|
|
88
|
+
async draftIds() {
|
|
89
|
+
return this.db.keys("drafts");
|
|
90
|
+
}
|
|
91
|
+
setDraft(id, pending) {
|
|
92
|
+
return pending ? this.db.set("drafts", id, true) : this.db.del("drafts", id);
|
|
93
|
+
}
|
|
94
|
+
lookupMessageId(messageId) {
|
|
95
|
+
return this.db.get("msgid", sha1(messageId));
|
|
96
|
+
}
|
|
97
|
+
indexMessageId(messageId, id) {
|
|
98
|
+
return this.db.set("msgid", sha1(messageId), id);
|
|
99
|
+
}
|
|
100
|
+
lookupProviderId(provider, providerId) {
|
|
101
|
+
return this.db.get("ext", provider, sha1(providerId));
|
|
102
|
+
}
|
|
103
|
+
indexProviderId(provider, providerId, id) {
|
|
104
|
+
return this.db.set("ext", provider, sha1(providerId), id);
|
|
105
|
+
}
|
|
106
|
+
audit(action, actor, target, detail) {
|
|
107
|
+
const entry = { action, actor: `${actor.kind}:${actor.name}`, target, at: new Date().toISOString() };
|
|
108
|
+
if (detail)
|
|
109
|
+
entry.detail = detail;
|
|
110
|
+
return this.db.set("audit", `${Date.now().toString(36)}${nid()}`, entry);
|
|
111
|
+
}
|
|
112
|
+
async auditEntries() {
|
|
113
|
+
return Object.values((await this.db.get("audit")) ?? {});
|
|
114
|
+
}
|
|
115
|
+
getLoginLink(hash) {
|
|
116
|
+
return this.db.get("login_links", hash);
|
|
117
|
+
}
|
|
118
|
+
putLoginLink(hash, link) {
|
|
119
|
+
return this.db.set("login_links", hash, link);
|
|
120
|
+
}
|
|
121
|
+
deleteLoginLink(hash) {
|
|
122
|
+
return this.db.del("login_links", hash);
|
|
123
|
+
}
|
|
124
|
+
getSession(id) {
|
|
125
|
+
return this.db.get("sessions", id);
|
|
126
|
+
}
|
|
127
|
+
putSession(session) {
|
|
128
|
+
return this.db.set("sessions", session.id, session);
|
|
129
|
+
}
|
|
130
|
+
deleteSession(id) {
|
|
131
|
+
return this.db.del("sessions", id);
|
|
132
|
+
}
|
|
133
|
+
async allSessions() {
|
|
134
|
+
return Object.values((await this.db.get("sessions")) ?? {});
|
|
135
|
+
}
|
|
136
|
+
getSyncCursor() {
|
|
137
|
+
return this.db.get("sync", "last_started_at");
|
|
138
|
+
}
|
|
139
|
+
setSyncCursor(at) {
|
|
140
|
+
return this.db.set("sync", "last_started_at", at);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
export class NotFoundError extends Error {
|
|
144
|
+
constructor(message) {
|
|
145
|
+
super(message);
|
|
146
|
+
this.name = "NotFoundError";
|
|
147
|
+
}
|
|
148
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
export interface MailboxConfig {
|
|
2
|
+
address: string;
|
|
3
|
+
name: string;
|
|
4
|
+
signatureHtml?: string;
|
|
5
|
+
signatureText?: string;
|
|
6
|
+
/** MCP tokens that only reach this mailbox, by name. */
|
|
7
|
+
tokens?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
export interface ProviderConfig {
|
|
10
|
+
type: string;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
export interface InboxerConfig {
|
|
14
|
+
publicUrl: string;
|
|
15
|
+
dataDir: string;
|
|
16
|
+
syncIntervalMin: number;
|
|
17
|
+
threadWindowDays: number;
|
|
18
|
+
mailboxes: Record<string, MailboxConfig>;
|
|
19
|
+
provider: ProviderConfig;
|
|
20
|
+
mcp: {
|
|
21
|
+
tokens: Record<string, string>;
|
|
22
|
+
maxAttachmentMb: number;
|
|
23
|
+
};
|
|
24
|
+
ui: {
|
|
25
|
+
cookieSecret: string;
|
|
26
|
+
loginLinkTtlMin: number;
|
|
27
|
+
undoSec: number;
|
|
28
|
+
maxUploadMb: number;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export declare const CATCHALL = "catchall";
|
|
32
|
+
export type Direction = "in" | "out";
|
|
33
|
+
export type MessageStatus = "received" | "draft" | "sent" | "delivered" | "bounced" | "failed" | "discarded";
|
|
34
|
+
/** Mailbox ids an actor may reach; null reaches every mailbox, the catch-all included. */
|
|
35
|
+
export type Scope = string[] | null;
|
|
36
|
+
export type Actor = {
|
|
37
|
+
kind: "agent" | "ui" | "cli";
|
|
38
|
+
name: string;
|
|
39
|
+
scope: Scope;
|
|
40
|
+
};
|
|
41
|
+
export interface AttachmentMeta {
|
|
42
|
+
id: string;
|
|
43
|
+
filename: string;
|
|
44
|
+
content_type: string;
|
|
45
|
+
size: number | null;
|
|
46
|
+
/** Shown inside the body (e.g. an embedded image referenced by `cid:`), not offered as a file. */
|
|
47
|
+
inline: boolean;
|
|
48
|
+
content_id: string | null;
|
|
49
|
+
}
|
|
50
|
+
export interface Message {
|
|
51
|
+
id: string;
|
|
52
|
+
provider: string;
|
|
53
|
+
provider_id: string | null;
|
|
54
|
+
mailbox: string;
|
|
55
|
+
direction: Direction;
|
|
56
|
+
status: MessageStatus;
|
|
57
|
+
message_id: string | null;
|
|
58
|
+
in_reply_to: string | null;
|
|
59
|
+
references: string[];
|
|
60
|
+
thread_id: string;
|
|
61
|
+
from: string;
|
|
62
|
+
to: string[];
|
|
63
|
+
cc: string[];
|
|
64
|
+
bcc: string[];
|
|
65
|
+
reply_to: string[];
|
|
66
|
+
subject: string;
|
|
67
|
+
text: string;
|
|
68
|
+
html: string | null;
|
|
69
|
+
headers: Record<string, string>;
|
|
70
|
+
attachments: AttachmentMeta[];
|
|
71
|
+
labels: Record<string, true>;
|
|
72
|
+
note: string | null;
|
|
73
|
+
created_by: "agent" | "ui" | null;
|
|
74
|
+
read: boolean;
|
|
75
|
+
archived: boolean;
|
|
76
|
+
created_at: string;
|
|
77
|
+
sent_at: string | null;
|
|
78
|
+
version: number;
|
|
79
|
+
edited_by_human: boolean;
|
|
80
|
+
/** A send or discard a human started, carried out at `at` unless undone first. */
|
|
81
|
+
pending: PendingAction | null;
|
|
82
|
+
}
|
|
83
|
+
export interface PendingAction {
|
|
84
|
+
action: "send" | "discard";
|
|
85
|
+
at: string;
|
|
86
|
+
by: string;
|
|
87
|
+
}
|
|
88
|
+
export interface Thread {
|
|
89
|
+
id: string;
|
|
90
|
+
mailbox: string;
|
|
91
|
+
subject: string;
|
|
92
|
+
norm_subject: string;
|
|
93
|
+
participants: string[];
|
|
94
|
+
last_at: string;
|
|
95
|
+
last_direction: Direction;
|
|
96
|
+
has_draft: boolean;
|
|
97
|
+
unread: number;
|
|
98
|
+
archived: boolean;
|
|
99
|
+
labels: Record<string, true>;
|
|
100
|
+
message_ids: string[];
|
|
101
|
+
}
|
|
102
|
+
export type MetaValue = string | number | boolean;
|
|
103
|
+
/** Context a consumer attaches to a contact or a thread. Inboxer stores it without interpreting it. */
|
|
104
|
+
export interface Context {
|
|
105
|
+
meta: Record<string, MetaValue>;
|
|
106
|
+
note: string | null;
|
|
107
|
+
updated_at: string;
|
|
108
|
+
updated_by: string;
|
|
109
|
+
}
|
|
110
|
+
export interface Contact extends Context {
|
|
111
|
+
address: string;
|
|
112
|
+
}
|
|
113
|
+
export interface AuditEntry {
|
|
114
|
+
action: string;
|
|
115
|
+
actor: string;
|
|
116
|
+
target: string | null;
|
|
117
|
+
at: string;
|
|
118
|
+
detail?: Record<string, unknown>;
|
|
119
|
+
}
|
|
120
|
+
export interface LoginLink {
|
|
121
|
+
created_at: string;
|
|
122
|
+
expires_at: string;
|
|
123
|
+
created_by: string;
|
|
124
|
+
label: string | null;
|
|
125
|
+
/** App path to open after signing in. */
|
|
126
|
+
next: string | null;
|
|
127
|
+
scope: Scope;
|
|
128
|
+
}
|
|
129
|
+
export interface Session {
|
|
130
|
+
id: string;
|
|
131
|
+
created_at: string;
|
|
132
|
+
last_seen: string;
|
|
133
|
+
user_agent: string | null;
|
|
134
|
+
ip: string | null;
|
|
135
|
+
label: string | null;
|
|
136
|
+
created_by: string;
|
|
137
|
+
scope: Scope;
|
|
138
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CATCHALL = "catchall";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
function load(file, type) {
|
|
4
|
+
const body = readFileSync(new URL(`./static/${file}`, import.meta.url), "utf8");
|
|
5
|
+
const hash = createHash("sha256").update(body).digest("hex").slice(0, 10);
|
|
6
|
+
return { body, type, url: `/static/${file}?v=${hash}` };
|
|
7
|
+
}
|
|
8
|
+
/** Static files with content-hashed URLs, so they can be cached forever and change on every deploy. */
|
|
9
|
+
export const ASSETS = {
|
|
10
|
+
"style.css": load("style.css", "text/css; charset=utf-8"),
|
|
11
|
+
"htmx.min.js": load("htmx.min.js", "text/javascript; charset=utf-8"),
|
|
12
|
+
"app.js": load("app.js", "text/javascript; charset=utf-8"),
|
|
13
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Hono } from "hono";
|
|
2
|
+
import type { Access } from "../auth.ts";
|
|
3
|
+
import { type Mail } from "../core/mail.ts";
|
|
4
|
+
import { type InboxerConfig, type Session } from "../types.ts";
|
|
5
|
+
export type UiEnv = {
|
|
6
|
+
Variables: {
|
|
7
|
+
session: Session;
|
|
8
|
+
secret: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare function registerUi(app: Hono<UiEnv>, mail: Mail, access: Access, cfg: InboxerConfig): void;
|