inboxer 0.0.0-stage → 0.1.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 +388 -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 +87 -0
- package/dist/core/address.d.ts +6 -0
- package/dist/core/address.js +16 -0
- package/dist/core/context.d.ts +10 -0
- package/dist/core/context.js +21 -0
- package/dist/core/mail.d.ts +142 -0
- package/dist/core/mail.js +520 -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 +49 -0
- package/dist/mcp.d.ts +5 -0
- package/dist/mcp.js +175 -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 +166 -0
- package/dist/provider/types.d.ts +70 -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 +125 -0
- package/dist/types.js +1 -0
- package/dist/ui/assets.d.ts +8 -0
- package/dist/ui/assets.js +12 -0
- package/dist/ui/routes.d.ts +11 -0
- package/dist/ui/routes.js +186 -0
- package/dist/ui/static/htmx.min.js +1 -0
- package/dist/ui/static/style.css +144 -0
- package/dist/ui/strings.d.ts +63 -0
- package/dist/ui/strings.js +63 -0
- package/dist/ui/views.d.ts +40 -0
- package/dist/ui/views.js +216 -0
- package/package.json +79 -3
- package/skills/inboxer/SKILL.md +130 -0
- package/src/app.ts +56 -0
- package/src/auth.ts +119 -0
- package/src/config.ts +89 -0
- package/src/core/address.ts +19 -0
- package/src/core/context.ts +28 -0
- package/src/core/mail.ts +601 -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 +90 -0
- package/src/mcp.ts +211 -0
- package/src/provider/index.ts +14 -0
- package/src/provider/resend.ts +216 -0
- package/src/provider/types.ts +74 -0
- package/src/store.ts +187 -0
- package/src/types.ts +129 -0
- package/src/ui/assets.ts +20 -0
- package/src/ui/routes.ts +203 -0
- package/src/ui/static/htmx.min.js +1 -0
- package/src/ui/static/style.css +144 -0
- package/src/ui/strings.ts +69 -0
- package/src/ui/views.ts +253 -0
package/dist/config.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import { CATCHALL } from "./types.js";
|
|
3
|
+
/** Absolute URL of an app path under `publicUrl`. */
|
|
4
|
+
export function appUrl(cfg, path) {
|
|
5
|
+
return cfg.publicUrl.replace(/\/$/, "") + path;
|
|
6
|
+
}
|
|
7
|
+
/** UI path of a thread, scrolled to one of its drafts when given. */
|
|
8
|
+
export function threadPath(threadId, draftId) {
|
|
9
|
+
return `/threads/${encodeURIComponent(threadId)}` + (draftId ? `#draft-${encodeURIComponent(draftId)}` : "");
|
|
10
|
+
}
|
|
11
|
+
function fail(path, expected) {
|
|
12
|
+
throw new Error(`Invalid inboxer config: ${path} must be ${expected}`);
|
|
13
|
+
}
|
|
14
|
+
function requireString(value, path) {
|
|
15
|
+
if (typeof value !== "string" || !value)
|
|
16
|
+
fail(path, "a non-empty string");
|
|
17
|
+
}
|
|
18
|
+
function requirePositive(value, path) {
|
|
19
|
+
if (typeof value !== "number" || !(value > 0))
|
|
20
|
+
fail(path, "a positive number");
|
|
21
|
+
}
|
|
22
|
+
/** Asserts the shape `createInboxer` relies on. */
|
|
23
|
+
export function assertConfig(cfg, { requireProvider }) {
|
|
24
|
+
requireString(cfg.publicUrl, "publicUrl");
|
|
25
|
+
requireString(cfg.dataDir, "dataDir");
|
|
26
|
+
if (!isAbsolute(cfg.dataDir))
|
|
27
|
+
fail("dataDir", "an absolute path");
|
|
28
|
+
requirePositive(cfg.syncIntervalMin, "syncIntervalMin");
|
|
29
|
+
requirePositive(cfg.threadWindowDays, "threadWindowDays");
|
|
30
|
+
if (!cfg.mailboxes || typeof cfg.mailboxes !== "object")
|
|
31
|
+
fail("mailboxes", "an object keyed by mailbox id");
|
|
32
|
+
if (CATCHALL in cfg.mailboxes)
|
|
33
|
+
fail(`mailboxes.${CATCHALL}`, "absent (the id is reserved)");
|
|
34
|
+
for (const [id, mailbox] of Object.entries(cfg.mailboxes)) {
|
|
35
|
+
requireString(mailbox.address, `mailboxes.${id}.address`);
|
|
36
|
+
requireString(mailbox.name, `mailboxes.${id}.name`);
|
|
37
|
+
if (mailbox.tokens !== undefined && (!mailbox.tokens || typeof mailbox.tokens !== "object"))
|
|
38
|
+
fail(`mailboxes.${id}.tokens`, "an object of named tokens");
|
|
39
|
+
}
|
|
40
|
+
if (requireProvider)
|
|
41
|
+
requireString(cfg.provider?.type, "provider.type");
|
|
42
|
+
if (!cfg.mcp?.tokens || typeof cfg.mcp.tokens !== "object")
|
|
43
|
+
fail("mcp.tokens", "an object of named tokens");
|
|
44
|
+
for (const [name, token] of Object.entries(cfg.mcp.tokens))
|
|
45
|
+
requireString(token, `mcp.tokens.${name}`);
|
|
46
|
+
mcpTokens(cfg);
|
|
47
|
+
requireString(cfg.ui?.cookieSecret, "ui.cookieSecret");
|
|
48
|
+
requirePositive(cfg.ui?.loginLinkTtlMin, "ui.loginLinkTtlMin");
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Tokens in `mcp.tokens` reach every mailbox; tokens under `mailboxes.<id>.tokens` reach only that
|
|
52
|
+
* mailbox. The same name and token under several mailboxes reach all of them.
|
|
53
|
+
*/
|
|
54
|
+
export function mcpTokens(cfg) {
|
|
55
|
+
const byName = new Map();
|
|
56
|
+
const add = (name, token, mailbox, path) => {
|
|
57
|
+
requireString(token, path);
|
|
58
|
+
const known = byName.get(name);
|
|
59
|
+
if (known && known.token !== token)
|
|
60
|
+
fail(path, `the same token as every other entry named "${name}"`);
|
|
61
|
+
for (const other of byName.values()) {
|
|
62
|
+
if (other.name !== name && other.token === token)
|
|
63
|
+
fail(path, `a token not already used by "${other.name}"`);
|
|
64
|
+
}
|
|
65
|
+
const entry = known ?? { name, token: token, scope: [] };
|
|
66
|
+
if (mailbox === null)
|
|
67
|
+
entry.scope = null;
|
|
68
|
+
else if (entry.scope && !entry.scope.includes(mailbox))
|
|
69
|
+
entry.scope.push(mailbox);
|
|
70
|
+
byName.set(name, entry);
|
|
71
|
+
};
|
|
72
|
+
for (const [name, token] of Object.entries(cfg.mcp.tokens))
|
|
73
|
+
add(name, token, null, `mcp.tokens.${name}`);
|
|
74
|
+
for (const [id, mailbox] of Object.entries(cfg.mailboxes)) {
|
|
75
|
+
for (const [name, token] of Object.entries(mailbox.tokens ?? {}))
|
|
76
|
+
add(name, token, id, `mailboxes.${id}.tokens.${name}`);
|
|
77
|
+
}
|
|
78
|
+
return [...byName.values()];
|
|
79
|
+
}
|
|
80
|
+
/** Whether `scope` reaches `mailbox`. */
|
|
81
|
+
export function reaches(scope, mailbox) {
|
|
82
|
+
return scope === null || scope.includes(mailbox);
|
|
83
|
+
}
|
|
84
|
+
/** Whether `outer` reaches every mailbox `inner` does. */
|
|
85
|
+
export function covers(outer, inner) {
|
|
86
|
+
return outer === null || (inner !== null && inner.every(m => outer.includes(m)));
|
|
87
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Bare, lowercased address from `Name <a@b.com>` or `a@b.com`. */
|
|
2
|
+
export declare function bareAddress(value: string): string;
|
|
3
|
+
export declare function formatAddress(name: string, address: string): string;
|
|
4
|
+
/** Splits a comma-separated recipient field, keeping commas inside quoted names. */
|
|
5
|
+
export declare function splitAddresses(value: string): string[];
|
|
6
|
+
export declare function domainOf(address: string): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Bare, lowercased address from `Name <a@b.com>` or `a@b.com`. */
|
|
2
|
+
export function bareAddress(value) {
|
|
3
|
+
const match = /<([^<>]+)>\s*$/.exec(value);
|
|
4
|
+
return (match ? match[1] : value).trim().toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
export function formatAddress(name, address) {
|
|
7
|
+
return `"${name.replace(/["\\]/g, "\\$&")}" <${address}>`;
|
|
8
|
+
}
|
|
9
|
+
/** Splits a comma-separated recipient field, keeping commas inside quoted names. */
|
|
10
|
+
export function splitAddresses(value) {
|
|
11
|
+
return (value.match(/("[^"]*"|[^,])+/g) ?? []).map(part => part.trim()).filter(Boolean);
|
|
12
|
+
}
|
|
13
|
+
export function domainOf(address) {
|
|
14
|
+
const bare = bareAddress(address);
|
|
15
|
+
return bare.slice(bare.lastIndexOf("@") + 1);
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Actor, Context, MetaValue } from "../types.ts";
|
|
2
|
+
export interface ContextPatch {
|
|
3
|
+
/** Keys to set; `null` removes a key. Keys not mentioned are kept. */
|
|
4
|
+
meta?: Record<string, MetaValue | null>;
|
|
5
|
+
/** `null` clears the note. */
|
|
6
|
+
note?: string | null;
|
|
7
|
+
}
|
|
8
|
+
export declare function mergeContext(existing: Context | null, patch: ContextPatch, actor: Actor): Context;
|
|
9
|
+
/** Every pair must be present. Values compare as text, so `991` matches `"991"`. */
|
|
10
|
+
export declare function metaMatches(meta: Record<string, MetaValue> | undefined, wanted: Record<string, MetaValue>): boolean;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function mergeContext(existing, patch, actor) {
|
|
2
|
+
const meta = { ...(existing?.meta ?? {}) };
|
|
3
|
+
for (const [key, value] of Object.entries(patch.meta ?? {})) {
|
|
4
|
+
if (value === null)
|
|
5
|
+
delete meta[key];
|
|
6
|
+
else
|
|
7
|
+
meta[key] = value;
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
meta,
|
|
11
|
+
note: patch.note !== undefined ? patch.note : (existing?.note ?? null),
|
|
12
|
+
updated_at: new Date().toISOString(),
|
|
13
|
+
updated_by: `${actor.kind}:${actor.name}`,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** Every pair must be present. Values compare as text, so `991` matches `"991"`. */
|
|
17
|
+
export function metaMatches(meta, wanted) {
|
|
18
|
+
if (!meta)
|
|
19
|
+
return false;
|
|
20
|
+
return Object.entries(wanted).every(([key, value]) => key in meta && String(meta[key]) === String(value));
|
|
21
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { DeliveryStatus, MailProvider, ProviderEvent } from "../provider/types.ts";
|
|
2
|
+
import { type Store } from "../store.ts";
|
|
3
|
+
import { type Actor, type Contact, type Context, type InboxerConfig, type Message, type MetaValue, type Scope, type Thread } from "../types.ts";
|
|
4
|
+
import { type ContextPatch } from "./context.ts";
|
|
5
|
+
export declare class ConflictError extends Error {
|
|
6
|
+
readonly current: Message;
|
|
7
|
+
constructor(current: Message);
|
|
8
|
+
}
|
|
9
|
+
export declare class InvalidRequestError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
export type ThreadStatus = "unanswered" | "all" | "archived";
|
|
13
|
+
export interface ListThreadsInput {
|
|
14
|
+
mailbox?: string;
|
|
15
|
+
status?: ThreadStatus;
|
|
16
|
+
label?: string;
|
|
17
|
+
since?: string;
|
|
18
|
+
/** Threads whose context, or a participant's contact, has every pair. */
|
|
19
|
+
meta?: Record<string, MetaValue>;
|
|
20
|
+
limit?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface SearchInput {
|
|
23
|
+
query?: string;
|
|
24
|
+
meta?: Record<string, MetaValue>;
|
|
25
|
+
mailbox?: string;
|
|
26
|
+
from?: string;
|
|
27
|
+
since?: string;
|
|
28
|
+
until?: string;
|
|
29
|
+
limit?: number;
|
|
30
|
+
}
|
|
31
|
+
export interface CreateDraftInput {
|
|
32
|
+
mailbox?: string;
|
|
33
|
+
replyToId?: string;
|
|
34
|
+
replyAll?: boolean;
|
|
35
|
+
to?: string[];
|
|
36
|
+
cc?: string[];
|
|
37
|
+
bcc?: string[];
|
|
38
|
+
subject?: string;
|
|
39
|
+
text: string;
|
|
40
|
+
html?: string;
|
|
41
|
+
note?: string;
|
|
42
|
+
}
|
|
43
|
+
export interface DraftPatch {
|
|
44
|
+
to?: string[];
|
|
45
|
+
cc?: string[];
|
|
46
|
+
bcc?: string[];
|
|
47
|
+
subject?: string;
|
|
48
|
+
text?: string;
|
|
49
|
+
html?: string | null;
|
|
50
|
+
note?: string | null;
|
|
51
|
+
}
|
|
52
|
+
export interface MessageView {
|
|
53
|
+
id: string;
|
|
54
|
+
thread_id: string;
|
|
55
|
+
mailbox: string;
|
|
56
|
+
direction: Message["direction"];
|
|
57
|
+
status: Message["status"];
|
|
58
|
+
from: string;
|
|
59
|
+
to: string[];
|
|
60
|
+
cc: string[];
|
|
61
|
+
subject: string;
|
|
62
|
+
date: string;
|
|
63
|
+
text: string;
|
|
64
|
+
labels: string[];
|
|
65
|
+
read: boolean;
|
|
66
|
+
attachments: Message["attachments"];
|
|
67
|
+
note: string | null;
|
|
68
|
+
version: number;
|
|
69
|
+
edited_by_human: boolean;
|
|
70
|
+
}
|
|
71
|
+
/** The mail domain: everything the MCP tools, the UI and the webhook do goes through here. */
|
|
72
|
+
export declare class Mail {
|
|
73
|
+
readonly store: Store;
|
|
74
|
+
readonly provider: MailProvider;
|
|
75
|
+
readonly cfg: InboxerConfig;
|
|
76
|
+
private readonly ownAddresses;
|
|
77
|
+
constructor(store: Store, provider: MailProvider, cfg: InboxerConfig);
|
|
78
|
+
listMailboxes(scope: Scope): {
|
|
79
|
+
id: string;
|
|
80
|
+
address: string;
|
|
81
|
+
name: string;
|
|
82
|
+
}[];
|
|
83
|
+
requireMessage(id: string, scope: Scope): Promise<Message>;
|
|
84
|
+
requireThread(id: string, scope: Scope): Promise<Thread>;
|
|
85
|
+
/** Scoped actors only reach contacts that take part in one of their threads. */
|
|
86
|
+
private requireContactAccess;
|
|
87
|
+
private mailboxFor;
|
|
88
|
+
private sendingMailbox;
|
|
89
|
+
/** Stores an inbound message once per provider id and returns its id. */
|
|
90
|
+
ingest(providerId: string): Promise<string>;
|
|
91
|
+
applyEvent(event: ProviderEvent): Promise<void>;
|
|
92
|
+
/** Status updates never move backwards: a late `sent` does not overwrite `delivered`. */
|
|
93
|
+
applyStatus(providerId: string, status: DeliveryStatus, messageId: string | null): Promise<void>;
|
|
94
|
+
/** Matches a thread by its own context or by any participant's contact. */
|
|
95
|
+
private metaFilter;
|
|
96
|
+
listThreads(input: ListThreadsInput, scope: Scope): Promise<Thread[]>;
|
|
97
|
+
search(input: SearchInput, scope: Scope): Promise<MessageView[]>;
|
|
98
|
+
getThread(threadId: string, scope: Scope): Promise<{
|
|
99
|
+
thread: Thread;
|
|
100
|
+
context: Context | null;
|
|
101
|
+
contacts: Contact[];
|
|
102
|
+
messages: MessageView[];
|
|
103
|
+
}>;
|
|
104
|
+
/** The thread's own context and the contacts of its external participants that have one. */
|
|
105
|
+
contextOf(thread: Thread): Promise<{
|
|
106
|
+
context: Context | null;
|
|
107
|
+
contacts: Contact[];
|
|
108
|
+
}>;
|
|
109
|
+
getContact(address: string, scope: Scope): Promise<Contact | null>;
|
|
110
|
+
setContact(address: string, patch: ContextPatch, actor: Actor): Promise<Contact>;
|
|
111
|
+
setThreadContext(threadId: string, patch: ContextPatch, actor: Actor): Promise<Context>;
|
|
112
|
+
private merge;
|
|
113
|
+
listDrafts(mailbox: string | undefined, scope: Scope): Promise<Message[]>;
|
|
114
|
+
/** Message ids, or thread ids to apply the change to every message in the thread. */
|
|
115
|
+
private resolveMessageIds;
|
|
116
|
+
private updateMessages;
|
|
117
|
+
label(ids: string[], add: string[], remove: string[], actor: Actor): Promise<number>;
|
|
118
|
+
mark(ids: string[], flags: {
|
|
119
|
+
read?: boolean;
|
|
120
|
+
archived?: boolean;
|
|
121
|
+
}, actor: Actor): Promise<number>;
|
|
122
|
+
createDraft(input: CreateDraftInput, actor: Actor): Promise<Message>;
|
|
123
|
+
private createDraftUnlocked;
|
|
124
|
+
private requireDraft;
|
|
125
|
+
/**
|
|
126
|
+
* Optimistic concurrency: `version` must be the current one. An agent cannot overwrite a draft a
|
|
127
|
+
* human edited unless it passes `force`. A UI edit regenerates the HTML from the text at send time.
|
|
128
|
+
*/
|
|
129
|
+
updateDraft(id: string, version: number, patch: DraftPatch, actor: Actor, force?: boolean): Promise<Message>;
|
|
130
|
+
discardDraft(id: string, actor: Actor): Promise<void>;
|
|
131
|
+
sendDraft(id: string, actor: Actor): Promise<Message>;
|
|
132
|
+
/**
|
|
133
|
+
* The draft id is the idempotency key and the Message-ID is stored before the provider call,
|
|
134
|
+
* so retrying a failed send repeats the same request instead of sending twice.
|
|
135
|
+
*/
|
|
136
|
+
private sendDraftUnlocked;
|
|
137
|
+
/** Direct send. If the provider fails, the message stays as a pending draft. */
|
|
138
|
+
send(input: CreateDraftInput, actor: Actor): Promise<Message>;
|
|
139
|
+
reply(messageId: string, text: string, replyAll: boolean, actor: Actor): Promise<Message>;
|
|
140
|
+
getAttachmentUrl(messageId: string, attachmentId: string, scope: Scope): Promise<string>;
|
|
141
|
+
}
|
|
142
|
+
export declare function toView(m: Message): MessageView;
|