@segmentstream/ontology/email
34 public exports from @segmentstream/ontology/email.
Public exports
ChannelCall
One HTTP request through a connection: one of its APIs, a path relative to its base URL.
interface ChannelCall {
readonly api: string;
readonly method: "GET" | "POST" | "PUT";
readonly path: string;
readonly headers: ReadonlyArray<readonly [
string,
string
]>;
readonly body: Uint8Array;
readonly file?: ChannelCallFile;
}ChannelDeliverFile
A file the ontology hands an agent's channel to deliver with a message: by a short-lived link to the workspace's file storage, granted just before the message is handed over, which the channel reads it by. `size` is the revision's the ontology checked: bytes of another size are not it.
interface ChannelDeliverFile {
readonly filename: string;
readonly content_type: string;
readonly size: number;
readonly url: string;
}ChannelReply
The provider's answer, as the connection relayed it.
interface ChannelReply {
readonly status: number;
readonly headers: ReadonlyArray<readonly [
string,
string
]>;
readonly body: Uint8Array;
}ChannelRequest
Sends one call. It rejects when the connection could not relay it; whether that proves the provider never saw it is for the caller's `definite` to say.
type ChannelRequest = (call: ChannelCall) => Promise<ChannelReply>;
EMAIL_ATTACHMENTS_LIMIT
The most bytes of files one reply carries, together: Gmail takes 25 MB of attachments in a message. A sender whose own transport takes less, such as a connection proxy, bounds them lower.
const EMAIL_ATTACHMENTS_LIMIT = 25000000
EMAIL_FILES_BYTES
The most bytes of files one email carries, together: below the 25 MB Gmail takes, with room for the text and the encoding.
const EMAIL_FILES_BYTES: number
EMAIL_MESSAGE_LIMIT
The most characters of a reply the mail channel sends in one message.
const EMAIL_MESSAGE_LIMIT = 100000
EMAIL_RECIPIENTS_LIMIT
The most addresses one reply goes to, in `To` and `Cc` together.
const EMAIL_RECIPIENTS_LIMIT = 50
EmailAttachment
A file a reply carries.
interface EmailAttachment {
readonly filename: string;
readonly contentType: string;
readonly bytes: Uint8Array;
}EmailDeliver
One reply for the agent's email channel to send in a thread (`POST` {@link emailDeliverRoute}).
interface EmailDeliver {
readonly thread_id: string;
readonly to: ReadonlyArray<string>;
readonly cc: ReadonlyArray<string>;
readonly text: string;
readonly key: string;
readonly files?: ReadonlyArray<ChannelDeliverFile>;
}EmailDelivered
The agent's answer: `200 { message_id }` once Gmail took the reply. A `4xx` with `{ error }` sent nothing (`404` alone: an agent without the route); anything else may have sent it.
interface EmailDelivered {
readonly message_id: string | null;
}EmailParties
The people of one message, as its `From`, `To` and `Cc` headers name them.
interface EmailParties {
readonly from: string | null | undefined;
readonly to: string | null | undefined;
readonly cc: string | null | undefined;
}EmailRecipients
Whom a reply goes to: the people in `To`, and those it copies in `Cc`.
interface EmailRecipients {
readonly to: ReadonlyArray<string>;
readonly cc: ReadonlyArray<string>;
}EmailReply
What a reply in an email thread needs from the message it answers.
interface EmailReply {
readonly to: string | ReadonlyArray<string>;
readonly cc?: ReadonlyArray<string>;
readonly subject: string | null;
readonly inReplyTo?: string | null;
readonly references?: string | null;
readonly text: string;
readonly html: string;
readonly autoSubmitted?: boolean;
readonly replyKey?: string;
readonly attachments?: ReadonlyArray<EmailAttachment>;
}GmailError
Gmail answered with a status the sender does not continue from. `detail` keeps Google's own explanation, such as a missing scope, for operators; whoever logs it redacts it.
class GmailError extends Error {
readonly operation: string;
readonly status: number;
readonly detail?: string | undefined;
readonly name = "GmailError";
constructor(operation: string, status: number, detail?: string | undefined);
}GmailMaybeSent
A failure after the message's last byte went to Gmail: the upload may have completed and the message been sent, whatever the failure says, so it is never retried as unsent. `cause` is what failed, such as the probe of what Gmail held.
class GmailMaybeSent extends Error {
readonly cause: unknown;
readonly name = "GmailMaybeSent";
constructor(cause: unknown);
}MAILBOX_SIGNATURE_LIFETIME_MS
How long a mailbox's signature is kept before it is read from Gmail again.
const MAILBOX_SIGNATURE_LIFETIME_MS: number
REPLY_KEY
A reply's key, as {@link EmailReply.replyKey} takes it.
const REPLY_KEY: RegExp
REPLY_KEY_HEADER
The header that names a reply, as {@link EmailReply.replyKey} sets it.
const REPLY_KEY_HEADER = "X-SegmentStream-Reply"
base64Url
Bytes as URL-safe base64 without padding, as the Gmail API takes a raw message.
function base64Url(text: string): string;
emailDeliverRoute
Where the agent's email channel `name` delivers a reply the ontology sends from its mailbox: a draft a member approved or a staff reply from the inbox. The agent renders and sends it as its own replies, with the mailbox's signature and quote, and only the deployment's own functions may call it.
function emailDeliverRoute(name: string): string;
emailRecipients
A reply's recipients as it sends them: each address trimmed and in lower case, once, an address in `To` left out of `Cc`. Throws when `to` is empty, an address is not a plain one, or there are more than {@link EMAIL_RECIPIENTS_LIMIT} together.
function emailRecipients(input: {
readonly to: string | ReadonlyArray<string>;
readonly cc?: ReadonlyArray<string>;
}): EmailRecipients;emailReplyMessage
The reply as an RFC 5322 message with a plain-text and an HTML part, followed by its files, for a mailbox to send in the same thread. It names no sender: the mailbox sends from its own address.
function emailReplyMessage(reply: EmailReply): string;
gmailExplanation
Google's explanation of a refused call: its error message and reasons.
function gmailExplanation(reply: Pick<ChannelReply, "body">): string | undefined;
gmailJson
A reply's body as a JSON object, or an empty one.
function gmailJson(reply: Pick<ChannelReply, "body">): Record<string, unknown>;
headerAddresses
The plain addresses an address header lists (`To`, `Cc`, `From`), in lower case and once each: `Name <a@example.com>` and a bare `a@example.com` alike, a group's members for a group, and nothing a quoted name or a comment holds. Anything that is no plain address is left out.
function headerAddresses(value: string | null | undefined): ReadonlyArray<string>;
isEmailAddress
Whether `value` is a plain address a reply may go to.
function isEmailAddress(value: string): boolean;
mailboxSignature
The process's reader of mailboxes' signatures, which the agent's and the ontology's mail share.
const mailboxSignature: (input: {
readonly request: ChannelRequest;
readonly mailbox: string;
}) => Promise<string | undefined>mailboxSignatures
A reader of mailboxes' own signatures, each kept for {@link MAILBOX_SIGNATURE_LIFETIME_MS} by its `mailbox` key, such as the connection's name. It reads the mailbox's send-as addresses (`users.settings.sendAs.list`, which `gmail.readonly` and `gmail.settings.basic` both cover) and resolves the signature of its own address, the primary one replies leave from, or `undefined` when it has none. Throws {@link GmailError} when Gmail refuses the read; a failed read is not kept.
function mailboxSignatures(options?: {
readonly now?: () => number;
}): (input: {
readonly request: ChannelRequest;
readonly mailbox: string;
}) => Promise<string | undefined>;renderEmailHtml
A reply's Markdown as the HTML part of an email. HTML the text holds is escaped, so a reply can carry no markup, image or script the platform did not render from Markdown itself.
function renderEmailHtml(text: string): string;
replyAllRecipients
Whom a reply to all of a message goes to, as the email channel's agent replies to all: the message's sender in `To`, and the other addresses of its `To` and `Cc` in `Cc`, without the mailbox's own addresses. A reply to the mailbox's own message goes to whom that message went to, its `To`, and copies its `Cc`. At most {@link EMAIL_RECIPIENTS_LIMIT} together; `undefined` when the message names no one else.
function replyAllRecipients(message: EmailParties, own: ReadonlyArray<string>): EmailRecipients | undefined;
replySubject
The subject a reply in a thread carries: the thread's own, prefixed `Re:` once.
function replySubject(subject: string | null | undefined): string;
sendGmailMessage
Sends `message`, an RFC 822 message, in `threadId` and resolves Gmail's answer, which names the message's `id`. A message one JSON request carries goes at once; a larger one is uploaded. A chunk whose answer was lost asks Gmail how much it holds and goes on from there, so no byte is sent twice and no message twice. Throws {@link GmailError} for a status it does not continue from.
function sendGmailMessage(options: {
readonly request: ChannelRequest;
readonly message: Uint8Array;
readonly threadId: string;
}): Promise<Record<string, unknown>>;withEmailSignature
A reply's HTML ending with the mailbox's own signature, as the mailbox's owner wrote it in their mail program: the platform adds no words, separator or quoted mail of its own.
function withEmailSignature(html: string, signature: string | undefined): string;