@segmentstream/ontology 0.167.0 → 0.168.1

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,25 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.168.1
4
+
5
+ The replication schema adds a content-free erasure journal, `_ontology_replication_erasures`.
6
+ Each row is a deleted record whose replicated history the platform erases from the workspace
7
+ warehouse, together with the audit of that erasure: when the record was deleted, when its history
8
+ left the warehouse, by which warehouse job, how many rows that job removed, and when a later check
9
+ found nothing left. This release queues nothing yet; preview databases start with an empty journal.
10
+
11
+ ## 0.168.0
12
+
13
+ A mailbox in `chat` takes `signature: true` beside its connection: the ontology's replies to mail,
14
+ a draft a member approves and a staff reply from the inbox, end their HTML with the mailbox's own
15
+ signature, as its owner set it in Gmail for its address. The signature is read through the
16
+ connection with `users.settings.sendAs` and kept for 10 minutes per mailbox; a reply whose
17
+ signature Gmail refuses to read goes unsigned. Off when absent; `check` refuses it on a channel
18
+ that is no mailbox.
19
+
20
+ `@segmentstream/ontology/email` exports the reader the agent shares, `mailboxSignature` (and
21
+ `mailboxSignatures` for a reader with its own clock), and `withEmailSignature`.
22
+
3
23
  ## 0.167.0
4
24
 
5
25
  Staff answer an email thread's chat (`chats: "thread"`) from the inbox. `agent_chat.postMessage`,
package/dist/analyze.d.ts CHANGED
@@ -273,6 +273,12 @@ export interface ChatChannelConfig {
273
273
  * long as the chat's messages (`retentionDays`); otherwise it expires 24 hours after it arrives.
274
274
  */
275
275
  readonly files?: ChatFilesConfig;
276
+ /**
277
+ * On a mailbox (`kind` `email`) only: whether the ontology's replies to mail, a draft a member
278
+ * approves and staff's replies from the inbox, end with the mailbox's own signature, as the
279
+ * mailbox's owner set it in Gmail. Off when absent.
280
+ */
281
+ readonly signature?: boolean;
276
282
  }
277
283
  /** `files` of a chat channel: whether its chats keep their files with their messages. */
278
284
  export interface ChatFilesConfig {
@@ -298,6 +304,8 @@ export interface ChatConfig {
298
304
  readonly email?: {
299
305
  readonly connection: AnyConnectionHandle;
300
306
  readonly files?: ChatFilesConfig;
307
+ /** Whether replies to mail end with the mailbox's own signature; see {@link ChatChannelConfig}. */
308
+ readonly signature?: boolean;
301
309
  };
302
310
  readonly attribution?: ChatAttributionSettings;
303
311
  /**
package/dist/analyze.js CHANGED
@@ -192,7 +192,10 @@ export function analyze(config, options = {}) {
192
192
  const malformed = entries.find(([name, value]) => {
193
193
  const named = !CHAT_PROVIDER_KEYS.includes(name);
194
194
  return (!isPlainObject(value) ||
195
- Object.keys(value).some((key) => key !== "connection" && key !== "kind" && key !== "files") ||
195
+ Object.keys(value).some((key) => key !== "connection" && key !== "kind" && key !== "files" && key !== "signature") ||
196
+ // Only a mailbox has a signature of its own.
197
+ (value.signature !== undefined &&
198
+ (typeof value.signature !== "boolean" || (named ? value.kind : name) !== "email")) ||
196
199
  (value.files !== undefined &&
197
200
  (!isPlainObject(value.files) ||
198
201
  Object.keys(value.files).some((key) => key !== "keep") ||
@@ -213,11 +216,14 @@ export function analyze(config, options = {}) {
213
216
  entry.connection,
214
217
  entry.kind ?? name,
215
218
  entry.files?.keep === true,
219
+ entry.signature === true,
216
220
  ];
217
221
  })
218
222
  : [];
219
223
  // Written only for a channel that keeps its files: older readers keep none.
220
224
  const keptFiles = (keep) => (keep ? { files: { keep: true } } : {});
225
+ // Written only for a mailbox that signs its replies: older readers sign none.
226
+ const signed = (signature) => (signature ? { signature: true } : {});
221
227
  const handles = channels.flatMap(([name, handle, kind]) => handle === undefined ? [] : [[name, handle, kind]]);
222
228
  const unlisted = handles.find(([, handle]) => !(config.connections ?? []).includes(handle));
223
229
  const shared = handles.find(([, handle], index) => handles.findIndex(([, other]) => other === handle) !== index);
@@ -230,7 +236,7 @@ export function analyze(config, options = {}) {
230
236
  report("invalid-connection", "chat", `chat.${malformed}.connection must reference a declared connection handle, and a ` +
231
237
  `channel not named after its kind needs kind: ${CHAT_KINDS.join(", ")}; a ` +
232
238
  `${WORKSPACE_CHANNEL_KIND} channel takes none, as the agent sends through it. ` +
233
- `files takes keep, true or false.`);
239
+ `files takes keep, true or false, and a mailbox (email) takes signature, true or false.`);
234
240
  }
235
241
  else if (unlisted !== undefined) {
236
242
  report("invalid-connection", `chat.${unlisted[0]}.connection`, "The chat channel connection must be listed in connections.");
@@ -243,16 +249,19 @@ export function analyze(config, options = {}) {
243
249
  chat = {
244
250
  ...Object.fromEntries(channels
245
251
  .filter(([name]) => CHAT_PROVIDER_KEYS.includes(name))
246
- .flatMap(([name, handle, , keep]) => handle === undefined ? [] : [[name, { connection: handle.key, ...keptFiles(keep) }]])),
252
+ .flatMap(([name, handle, , keep, signature]) => handle === undefined
253
+ ? []
254
+ : [[name, { connection: handle.key, ...keptFiles(keep), ...signed(signature) }]])),
247
255
  ...(named.length === 0
248
256
  ? {}
249
257
  : {
250
- channels: Object.fromEntries(named.map(([name, handle, kind, keep]) => [
258
+ channels: Object.fromEntries(named.map(([name, handle, kind, keep, signature]) => [
251
259
  name,
252
260
  {
253
261
  kind,
254
262
  ...(handle === undefined ? {} : { connection: handle.key }),
255
263
  ...keptFiles(keep),
264
+ ...signed(signature),
256
265
  },
257
266
  ])),
258
267
  }),
package/dist/email.d.ts CHANGED
@@ -48,6 +48,11 @@ export declare const EMAIL_ATTACHMENTS_LIMIT = 25000000;
48
48
  * carry no markup, image or script the platform did not render from Markdown itself.
49
49
  */
50
50
  export declare function renderEmailHtml(text: string): string;
51
+ /**
52
+ * A reply's HTML ending with the mailbox's own signature, as the mailbox's owner wrote it in their
53
+ * mail program: the platform adds no words, separator or quoted mail of its own.
54
+ */
55
+ export declare function withEmailSignature(html: string, signature: string | undefined): string;
51
56
  /** The subject a reply in a thread carries: the thread's own, prefixed `Re:` once. */
52
57
  export declare function replySubject(subject: string | null | undefined): string;
53
58
  /** A file a reply carries. */
@@ -98,4 +103,4 @@ export declare function emailReplyMessage(reply: EmailReply): string;
98
103
  /** Bytes as URL-safe base64 without padding, as the Gmail API takes a raw message. */
99
104
  export declare function base64Url(text: string): string;
100
105
  export type { ChannelCall, ChannelReply, ChannelRequest } from "./channel-files.js";
101
- export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, GmailMaybeSent, gmailJson, sendGmailMessage, } from "./gmail-send.js";
106
+ export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, GmailMaybeSent, gmailJson, mailboxSignature, mailboxSignatures, MAILBOX_SIGNATURE_LIFETIME_MS, sendGmailMessage, } from "./gmail-send.js";
package/dist/email.js CHANGED
@@ -178,6 +178,18 @@ export function renderEmailHtml(text) {
178
178
  const body = markdown.parse(text);
179
179
  return `<!doctype html><html><body>${body}</body></html>`;
180
180
  }
181
+ /**
182
+ * A reply's HTML ending with the mailbox's own signature, as the mailbox's owner wrote it in their
183
+ * mail program: the platform adds no words, separator or quoted mail of its own.
184
+ */
185
+ export function withEmailSignature(html, signature) {
186
+ if (signature === undefined || signature.trim() === "") {
187
+ return html;
188
+ }
189
+ const block = `<br><div>${signature}</div>`;
190
+ const end = html.lastIndexOf("</body>");
191
+ return end === -1 ? `${html}${block}` : `${html.slice(0, end)}${block}${html.slice(end)}`;
192
+ }
181
193
  /** The subject a reply in a thread carries: the thread's own, prefixed `Re:` once. */
182
194
  export function replySubject(subject) {
183
195
  const flat = (subject ?? "").replace(/[\r\n]+/g, " ").trim();
@@ -302,4 +314,4 @@ export function emailReplyMessage(reply) {
302
314
  export function base64Url(text) {
303
315
  return Buffer.from(text, "utf8").toString("base64url");
304
316
  }
305
- export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, GmailMaybeSent, gmailJson, sendGmailMessage, } from "./gmail-send.js";
317
+ export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, GmailMaybeSent, gmailJson, mailboxSignature, mailboxSignatures, MAILBOX_SIGNATURE_LIFETIME_MS, sendGmailMessage, } from "./gmail-send.js";
@@ -47,3 +47,23 @@ export declare function sendGmailMessage(options: {
47
47
  readonly message: Uint8Array;
48
48
  readonly threadId: string;
49
49
  }): Promise<Record<string, unknown>>;
50
+ /** How long a mailbox's signature is kept before it is read from Gmail again. */
51
+ export declare const MAILBOX_SIGNATURE_LIFETIME_MS: number;
52
+ /**
53
+ * A reader of mailboxes' own signatures, each kept for {@link MAILBOX_SIGNATURE_LIFETIME_MS} by its
54
+ * `mailbox` key, such as the connection's name. It reads the mailbox's send-as addresses
55
+ * (`users.settings.sendAs.list`, which `gmail.readonly` and `gmail.settings.basic` both cover) and
56
+ * resolves the signature of its own address, the primary one replies leave from, or `undefined`
57
+ * when it has none. Throws {@link GmailError} when Gmail refuses the read; a failed read is not kept.
58
+ */
59
+ export declare function mailboxSignatures(options?: {
60
+ readonly now?: () => number;
61
+ }): (input: {
62
+ readonly request: ChannelRequest;
63
+ readonly mailbox: string;
64
+ }) => Promise<string | undefined>;
65
+ /** The process's reader of mailboxes' signatures, which the agent's and the ontology's mail share. */
66
+ export declare const mailboxSignature: (input: {
67
+ readonly request: ChannelRequest;
68
+ readonly mailbox: string;
69
+ }) => Promise<string | undefined>;
@@ -199,3 +199,48 @@ export async function sendGmailMessage(options) {
199
199
  offset = held === undefined ? 0 : Number(held) + 1;
200
200
  }
201
201
  }
202
+ /** How long a mailbox's signature is kept before it is read from Gmail again. */
203
+ export const MAILBOX_SIGNATURE_LIFETIME_MS = 10 * 60 * 1000;
204
+ /** How many mailboxes' signatures one process keeps; the oldest read is forgotten first. */
205
+ const MAILBOX_SIGNATURES_KEPT = 100;
206
+ /**
207
+ * A reader of mailboxes' own signatures, each kept for {@link MAILBOX_SIGNATURE_LIFETIME_MS} by its
208
+ * `mailbox` key, such as the connection's name. It reads the mailbox's send-as addresses
209
+ * (`users.settings.sendAs.list`, which `gmail.readonly` and `gmail.settings.basic` both cover) and
210
+ * resolves the signature of its own address, the primary one replies leave from, or `undefined`
211
+ * when it has none. Throws {@link GmailError} when Gmail refuses the read; a failed read is not kept.
212
+ */
213
+ export function mailboxSignatures(options = {}) {
214
+ const now = options.now ?? Date.now;
215
+ const kept = new Map();
216
+ return async (input) => {
217
+ const found = kept.get(input.mailbox);
218
+ if (found !== undefined && found.until > now()) {
219
+ return found.signature;
220
+ }
221
+ const reply = await input.request({
222
+ api: "gmail",
223
+ method: "GET",
224
+ path: "/users/me/settings/sendAs",
225
+ headers: [],
226
+ body: new Uint8Array(),
227
+ });
228
+ if (reply.status < 200 || reply.status >= 300) {
229
+ throw new GmailError("settings.sendAs.list", reply.status, gmailExplanation(reply));
230
+ }
231
+ const addresses = gmailJson(reply).sendAs;
232
+ const own = (Array.isArray(addresses) ? addresses : []).find((address) => address?.isPrimary === true);
233
+ const signature = typeof own?.signature === "string" && own.signature.trim() !== "" ? own.signature : undefined;
234
+ kept.delete(input.mailbox);
235
+ kept.set(input.mailbox, { signature, until: now() + MAILBOX_SIGNATURE_LIFETIME_MS });
236
+ for (const mailbox of kept.keys()) {
237
+ if (kept.size <= MAILBOX_SIGNATURES_KEPT) {
238
+ break;
239
+ }
240
+ kept.delete(mailbox);
241
+ }
242
+ return signature;
243
+ };
244
+ }
245
+ /** The process's reader of mailboxes' signatures, which the agent's and the ontology's mail share. */
246
+ export const mailboxSignature = mailboxSignatures();