@segmentstream/ontology 0.108.0 → 0.109.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
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.109.0
|
|
4
|
+
|
|
5
|
+
`emailReplyMessage` from `@segmentstream/ontology/email` takes `attachments` (file name, MIME type
|
|
6
|
+
and bytes): the reply becomes `multipart/mixed`, its text and HTML first, with every threading
|
|
7
|
+
header, `Auto-Submitted` and reply key it had, and refuses more than `EMAIL_ATTACHMENTS_LIMIT`
|
|
8
|
+
(25 MB, Gmail's limit) of files together.
|
|
9
|
+
|
|
10
|
+
A reviewed reply approved for an email chat is now marked `Auto-Submitted: auto-replied` and
|
|
11
|
+
named `X-SegmentStream-Reply: review-<review id>`, as the email channel's own replies are, so no
|
|
12
|
+
autoresponder answers it and the channel does not take it for staff's mail.
|
|
13
|
+
|
|
14
|
+
The agent routes now admit an email chat (`email:<thread>:<address>`), whose address holds `@`,
|
|
15
|
+
`.` and `+`: storing the attachments of an email, reading them and sending stored files in an email
|
|
16
|
+
chat were refused before.
|
|
17
|
+
|
|
3
18
|
## 0.108.0
|
|
4
19
|
|
|
5
20
|
A workspace can send its own invitation. `signIn.invitationEmail` names one of the ontology's
|
package/dist/email.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/** The most characters of a reply the mail channel sends in one message. */
|
|
2
2
|
export declare const EMAIL_MESSAGE_LIMIT = 100000;
|
|
3
|
+
/**
|
|
4
|
+
* The most bytes of files one reply carries, together: Gmail takes 25 MB of attachments in a
|
|
5
|
+
* message. A sender whose own transport takes less, such as a connection proxy, bounds them lower.
|
|
6
|
+
*/
|
|
7
|
+
export declare const EMAIL_ATTACHMENTS_LIMIT = 25000000;
|
|
3
8
|
/**
|
|
4
9
|
* A reply's Markdown as the HTML part of an email. HTML the text holds is escaped, so a reply can
|
|
5
10
|
* carry no markup, image or script the platform did not render from Markdown itself.
|
|
@@ -7,6 +12,12 @@ export declare const EMAIL_MESSAGE_LIMIT = 100000;
|
|
|
7
12
|
export declare function renderEmailHtml(text: string): string;
|
|
8
13
|
/** The subject a reply in a thread carries: the thread's own, prefixed `Re:` once. */
|
|
9
14
|
export declare function replySubject(subject: string | null | undefined): string;
|
|
15
|
+
/** A file a reply carries. */
|
|
16
|
+
export interface EmailAttachment {
|
|
17
|
+
readonly filename: string;
|
|
18
|
+
readonly contentType: string;
|
|
19
|
+
readonly bytes: Uint8Array;
|
|
20
|
+
}
|
|
10
21
|
/** What a reply in an email thread needs from the message it answers. */
|
|
11
22
|
export interface EmailReply {
|
|
12
23
|
/** The address the reply goes to: a lower-case address without a name. */
|
|
@@ -31,12 +42,17 @@ export interface EmailReply {
|
|
|
31
42
|
* failure can find it in the thread instead of sending it twice. Letters, digits, `-` and `_`.
|
|
32
43
|
*/
|
|
33
44
|
readonly replyKey?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Files the reply carries after its text, at most {@link EMAIL_ATTACHMENTS_LIMIT} bytes
|
|
47
|
+
* together. With files the message is `multipart/mixed`, its text first.
|
|
48
|
+
*/
|
|
49
|
+
readonly attachments?: ReadonlyArray<EmailAttachment>;
|
|
34
50
|
}
|
|
35
51
|
/** The header that names a reply, as {@link EmailReply.replyKey} sets it. */
|
|
36
52
|
export declare const REPLY_KEY_HEADER = "X-SegmentStream-Reply";
|
|
37
53
|
/**
|
|
38
|
-
* The reply as an RFC 5322 message with a plain-text and an HTML part,
|
|
39
|
-
* the same thread. It names no sender: the mailbox sends from its own address.
|
|
54
|
+
* The reply as an RFC 5322 message with a plain-text and an HTML part, followed by its files, for
|
|
55
|
+
* a mailbox to send in the same thread. It names no sender: the mailbox sends from its own address.
|
|
40
56
|
*/
|
|
41
57
|
export declare function emailReplyMessage(reply: EmailReply): string;
|
|
42
58
|
/** Bytes as URL-safe base64 without padding, as the Gmail API takes a raw message. */
|
package/dist/email.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Marked } from "marked";
|
|
2
2
|
/** The most characters of a reply the mail channel sends in one message. */
|
|
3
3
|
export const EMAIL_MESSAGE_LIMIT = 100_000;
|
|
4
|
+
/**
|
|
5
|
+
* The most bytes of files one reply carries, together: Gmail takes 25 MB of attachments in a
|
|
6
|
+
* message. A sender whose own transport takes less, such as a connection proxy, bounds them lower.
|
|
7
|
+
*/
|
|
8
|
+
export const EMAIL_ATTACHMENTS_LIMIT = 25_000_000;
|
|
4
9
|
const HTML_ESCAPES = {
|
|
5
10
|
"&": "&",
|
|
6
11
|
"<": "<",
|
|
@@ -42,17 +47,37 @@ function messageId(value) {
|
|
|
42
47
|
return flat !== undefined && /^<[^<>\s]+>$/.test(flat) ? flat : undefined;
|
|
43
48
|
}
|
|
44
49
|
/** Base64 in lines of 76 characters, as MIME bodies carry it. */
|
|
45
|
-
function base64Lines(
|
|
46
|
-
return (Buffer.from(
|
|
50
|
+
function base64Lines(content) {
|
|
51
|
+
return ((typeof content === "string" ? Buffer.from(content, "utf8") : Buffer.from(content))
|
|
47
52
|
.toString("base64")
|
|
48
53
|
.match(/.{1,76}/g)
|
|
49
54
|
?.join("\r\n") ?? "");
|
|
50
55
|
}
|
|
56
|
+
/** A MIME type as a header carries it, or `application/octet-stream` for anything else. */
|
|
57
|
+
function mediaType(value) {
|
|
58
|
+
const type = value.trim().toLowerCase();
|
|
59
|
+
return /^[a-z0-9][a-z0-9!#$&^_.+-]{0,126}\/[a-z0-9][a-z0-9!#$&^_.+-]{0,126}$/.test(type)
|
|
60
|
+
? type
|
|
61
|
+
: "application/octet-stream";
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A file name as a MIME parameter: printable ASCII in quotes, followed by the same name in UTF-8
|
|
65
|
+
* (RFC 2231) when it holds anything else, so mail programs show it as it was named.
|
|
66
|
+
*/
|
|
67
|
+
function fileNameParameter(parameter, filename) {
|
|
68
|
+
const flat = filename.replace(/[\r\n\t"\\]+/g, " ").trim() || "attachment";
|
|
69
|
+
const ascii = flat.replace(/[^\x20-\x7e]/g, "_");
|
|
70
|
+
if (ascii === flat) {
|
|
71
|
+
return `${parameter}="${flat}"`;
|
|
72
|
+
}
|
|
73
|
+
const encoded = encodeURIComponent(flat).replace(/['()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
74
|
+
return `${parameter}="${ascii}"; ${parameter}*=UTF-8''${encoded}`;
|
|
75
|
+
}
|
|
51
76
|
/** The header that names a reply, as {@link EmailReply.replyKey} sets it. */
|
|
52
77
|
export const REPLY_KEY_HEADER = "X-SegmentStream-Reply";
|
|
53
78
|
/**
|
|
54
|
-
* The reply as an RFC 5322 message with a plain-text and an HTML part,
|
|
55
|
-
* the same thread. It names no sender: the mailbox sends from its own address.
|
|
79
|
+
* The reply as an RFC 5322 message with a plain-text and an HTML part, followed by its files, for
|
|
80
|
+
* a mailbox to send in the same thread. It names no sender: the mailbox sends from its own address.
|
|
56
81
|
*/
|
|
57
82
|
export function emailReplyMessage(reply) {
|
|
58
83
|
if (!/^[^\s<>@,;:"]+@[^\s<>@,;:"]+$/.test(reply.to)) {
|
|
@@ -61,6 +86,11 @@ export function emailReplyMessage(reply) {
|
|
|
61
86
|
if (reply.replyKey !== undefined && !/^[A-Za-z0-9_-]{1,128}$/.test(reply.replyKey)) {
|
|
62
87
|
throw new Error("The reply's key holds characters a header cannot carry.");
|
|
63
88
|
}
|
|
89
|
+
const attachments = reply.attachments ?? [];
|
|
90
|
+
const size = attachments.reduce((total, file) => total + file.bytes.byteLength, 0);
|
|
91
|
+
if (size > EMAIL_ATTACHMENTS_LIMIT) {
|
|
92
|
+
throw new Error(`A reply carries at most ${EMAIL_ATTACHMENTS_LIMIT} bytes of files.`);
|
|
93
|
+
}
|
|
64
94
|
const boundary = `segmentstream-${crypto.randomUUID()}`;
|
|
65
95
|
const parent = messageId(reply.inReplyTo);
|
|
66
96
|
const references = [
|
|
@@ -79,10 +109,10 @@ export function emailReplyMessage(reply) {
|
|
|
79
109
|
...(reply.autoSubmitted === true ? ["Auto-Submitted: auto-replied"] : []),
|
|
80
110
|
...(reply.replyKey === undefined ? [] : [`${REPLY_KEY_HEADER}: ${reply.replyKey}`]),
|
|
81
111
|
"MIME-Version: 1.0",
|
|
82
|
-
`Content-Type: multipart/alternative; boundary="${boundary}"`,
|
|
83
112
|
];
|
|
84
|
-
|
|
85
|
-
|
|
113
|
+
// The text and the same text as HTML: the whole message without files, its first part with them.
|
|
114
|
+
const text = [
|
|
115
|
+
`Content-Type: multipart/alternative; boundary="${boundary}"`,
|
|
86
116
|
"",
|
|
87
117
|
`--${boundary}`,
|
|
88
118
|
"Content-Type: text/plain; charset=UTF-8",
|
|
@@ -95,6 +125,26 @@ export function emailReplyMessage(reply) {
|
|
|
95
125
|
"",
|
|
96
126
|
base64Lines(reply.html),
|
|
97
127
|
`--${boundary}--`,
|
|
128
|
+
];
|
|
129
|
+
if (attachments.length === 0) {
|
|
130
|
+
return [...headers, ...text, ""].join("\r\n");
|
|
131
|
+
}
|
|
132
|
+
const mixed = `segmentstream-${crypto.randomUUID()}`;
|
|
133
|
+
return [
|
|
134
|
+
...headers,
|
|
135
|
+
`Content-Type: multipart/mixed; boundary="${mixed}"`,
|
|
136
|
+
"",
|
|
137
|
+
`--${mixed}`,
|
|
138
|
+
...text,
|
|
139
|
+
...attachments.flatMap((file) => [
|
|
140
|
+
`--${mixed}`,
|
|
141
|
+
`Content-Type: ${mediaType(file.contentType)}; ${fileNameParameter("name", file.filename)}`,
|
|
142
|
+
`Content-Disposition: attachment; ${fileNameParameter("filename", file.filename)}`,
|
|
143
|
+
"Content-Transfer-Encoding: base64",
|
|
144
|
+
"",
|
|
145
|
+
base64Lines(file.bytes),
|
|
146
|
+
]),
|
|
147
|
+
`--${mixed}--`,
|
|
98
148
|
"",
|
|
99
149
|
].join("\r\n");
|
|
100
150
|
}
|
|
@@ -9,7 +9,11 @@ export const CHAT_HEADER = "x-segmentstream-agent-chat";
|
|
|
9
9
|
export const SESSION_HEADER = "x-segmentstream-agent-session";
|
|
10
10
|
/** @legacy agent.conversation-header The chat's header from agents built before it took the name. */
|
|
11
11
|
const EARLIER_CHAT_HEADER = "x-segmentstream-agent-conversation";
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* `provider:chat id`. An email chat is a Gmail thread and the person's address
|
|
14
|
+
* (`email:<thread>:<address>`), so an id may hold an address's `@`, `.` and `+`.
|
|
15
|
+
*/
|
|
16
|
+
const CHAT = /^[a-z][a-z0-9_-]{0,31}:[A-Za-z0-9:_.@+-]{1,320}$/;
|
|
13
17
|
const SESSION = /^[A-Za-z0-9:_.-]{1,200}$/;
|
|
14
18
|
/** Authenticate the runtime and resolve its member; attachment storage also checks chat access. */
|
|
15
19
|
export function authenticateAgentCaller(input) {
|
|
@@ -5,7 +5,9 @@ import { type ChatProvider, type ChatSend, type ProviderOutcome } from "./provid
|
|
|
5
5
|
* reads the thread's subject and its last message first, so the reply carries `Re:`, `In-Reply-To`
|
|
6
6
|
* and `References` and the person's mail program shows it in the same conversation; then
|
|
7
7
|
* `POST /users/me/messages/send` with the thread id, never retried. A reply sends no attribution
|
|
8
|
-
* and names no sender, so it leaves from the mailbox's own address.
|
|
8
|
+
* and names no sender, so it leaves from the mailbox's own address. It carries
|
|
9
|
+
* `Auto-Submitted: auto-replied` and `X-SegmentStream-Reply: review-<review id>`, as the email
|
|
10
|
+
* channel's replies carry their own keys. `sent` means Gmail accepted it.
|
|
9
11
|
*/
|
|
10
12
|
export declare function sendEmail(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
11
13
|
/**
|
|
@@ -30,7 +30,9 @@ function headersOf(message) {
|
|
|
30
30
|
* reads the thread's subject and its last message first, so the reply carries `Re:`, `In-Reply-To`
|
|
31
31
|
* and `References` and the person's mail program shows it in the same conversation; then
|
|
32
32
|
* `POST /users/me/messages/send` with the thread id, never retried. A reply sends no attribution
|
|
33
|
-
* and names no sender, so it leaves from the mailbox's own address.
|
|
33
|
+
* and names no sender, so it leaves from the mailbox's own address. It carries
|
|
34
|
+
* `Auto-Submitted: auto-replied` and `X-SegmentStream-Reply: review-<review id>`, as the email
|
|
35
|
+
* channel's replies carry their own keys. `sent` means Gmail accepted it.
|
|
34
36
|
*/
|
|
35
37
|
export function sendEmail(input) {
|
|
36
38
|
return Effect.gen(function* () {
|
|
@@ -67,6 +69,9 @@ export function sendEmail(input) {
|
|
|
67
69
|
const list = Array.isArray(messages) ? messages : [];
|
|
68
70
|
const first = headersOf(list[0]);
|
|
69
71
|
const last = headersOf(list.at(-1));
|
|
72
|
+
// The agent's reply, as the email channel sends its own: marked as a program's, so no
|
|
73
|
+
// autoresponder answers it, and named by its review, so the channel knows it for the agent's.
|
|
74
|
+
const key = `review-${input.destination.id}`;
|
|
70
75
|
const raw = emailReplyMessage({
|
|
71
76
|
to: input.destination.external_id,
|
|
72
77
|
subject: first.get("subject") ?? null,
|
|
@@ -74,6 +79,8 @@ export function sendEmail(input) {
|
|
|
74
79
|
references: last.get("references") ?? null,
|
|
75
80
|
text: input.plainText,
|
|
76
81
|
html: input.finalText,
|
|
82
|
+
autoSubmitted: true,
|
|
83
|
+
...(/^[A-Za-z0-9_-]{1,128}$/.test(key) ? { replyKey: key } : {}),
|
|
77
84
|
});
|
|
78
85
|
const payload = { threadId, html: input.finalText, text: input.plainText };
|
|
79
86
|
const result = yield* input.transport
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.109.0",
|
|
4
4
|
"description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "SegmentStream",
|