@segmentstream/ontology 0.175.0 → 0.177.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,48 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.177.0
4
+
5
+ Workspace channels take files by link, with a file limit of their own and the provider's
6
+ refusals.
7
+
8
+ - **Files by link.** Each file `send` receives is `{ filename, contentType, size, url, read() }`.
9
+ `url` is a short-lived link to the file in the workspace's file storage, to hand to a provider
10
+ that fetches files itself. `read()` reads the bytes once and checks their size. The ontology and
11
+ `segmentstream__attachments_send` no longer put files' bytes in the send route's request, so a
12
+ message can carry more than 3 MiB.
13
+ - **`limits.fileBytes`.** The most bytes of files one message carries, up to 64 MiB, 3 MiB when
14
+ absent. Files past it are refused (`file_too_large`) and nothing is sent.
15
+ - **`file` on the connection.** The channel's connection takes `file`, one of the files `send` was
16
+ given, as the whole body of a `POST`, `PUT` or `PATCH`. SegmentStream reads it from file storage,
17
+ up to 64 MiB, so its bytes never pass through the function.
18
+ - **Refusals.** `send` may return `{ refusal }`: `outside_reply_window`, `recipient_unreachable`
19
+ or `recipient_opted_out`. The message sent nothing, and the platform records the refusal as
20
+ WhatsApp's.
21
+
22
+ Action required for a workspace channel whose `send` reads `file.bytes`: call `await file.read()`,
23
+ or hand `file.url` to the provider. `bytes` is deprecated and present only while a message's files
24
+ are at most 3 MiB together. Upgrade the agent and ontology SDKs together; each still works with
25
+ the other's previous release.
26
+
27
+ ## 0.176.0
28
+
29
+ The ontology's replies to mail, a draft a member approves and a staff reply from the inbox, are
30
+ handed to the agent's email channel of the chat's name, which sends them as its own replies: in the
31
+ thread, from the mailbox, keyed so a lost answer is never sent twice, and signed and quoted by the
32
+ channel's `signature` and `quote`, so every reply from the mailbox looks the same. The reply's files
33
+ go by short-lived links to the workspace's file storage, checked against the revision the draft
34
+ holds before anything is reserved; the channel refuses a file whose size differs and sends nothing.
35
+ The channel serves this at `POST /eve/segmentstream/v1/email/<name>/deliver`, for the deployment's
36
+ own functions only.
37
+
38
+ An agent from before the route answers 404, and the ontology sends the reply from the mailbox
39
+ itself, as before: signed by `signature` on its `chat` entry and never quoted.
40
+
41
+ Action required: upgrade `@segmentstream/agent` together with `@segmentstream/ontology`, and set
42
+ `signature` (and `quote`, if wanted) on `channels.email` in `agent/segmentstream.json`. The
43
+ ontology's `chat.email.signature` now signs only replies sent for an agent from before the route; it
44
+ is deprecated and stops working on 2027-05-27.
45
+
3
46
  ## 0.175.0
4
47
 
5
48
  Allow workspace operators to enable bounded, tool-free answers to unlinked senders in individual Telegram and Slack group chats. Dedicated connections can use a verified inviter's existing permission to open a newly joined group.
package/dist/email.d.ts CHANGED
@@ -95,6 +95,40 @@ export interface EmailReply {
95
95
  }
96
96
  /** The header that names a reply, as {@link EmailReply.replyKey} sets it. */
97
97
  export declare const REPLY_KEY_HEADER = "X-SegmentStream-Reply";
98
+ /** A reply's key, as {@link EmailReply.replyKey} takes it. */
99
+ export declare const REPLY_KEY: RegExp;
100
+ /**
101
+ * Where the agent's email channel `name` delivers a reply the ontology sends from its mailbox: a
102
+ * draft a member approved or a staff reply from the inbox. The agent renders and sends it as its
103
+ * own replies, with the mailbox's signature and quote, and only the deployment's own functions may
104
+ * call it.
105
+ */
106
+ export declare function emailDeliverRoute(name: string): string;
107
+ /** One reply for the agent's email channel to send in a thread (`POST` {@link emailDeliverRoute}). */
108
+ export interface EmailDeliver {
109
+ readonly thread_id: string;
110
+ /** Whom the reply goes to, and copies; plain addresses. */
111
+ readonly to: ReadonlyArray<string>;
112
+ readonly cc: ReadonlyArray<string>;
113
+ /** The exact reply, Markdown with the attribution the ontology added. */
114
+ readonly text: string;
115
+ /** The reply's key (`X-SegmentStream-Reply`), so a send after a lost answer is found, not repeated. */
116
+ readonly key: string;
117
+ /** The reply's files, each by a short-lived link to the workspace's file storage. */
118
+ readonly files?: ReadonlyArray<{
119
+ readonly filename: string;
120
+ readonly content_type: string;
121
+ readonly size: number;
122
+ readonly url: string;
123
+ }>;
124
+ }
125
+ /**
126
+ * The agent's answer: `200 { message_id }` once Gmail took the reply. A `4xx` with `{ error }` sent
127
+ * nothing (`404` alone: an agent without the route); anything else may have sent it.
128
+ */
129
+ export interface EmailDelivered {
130
+ readonly message_id: string | null;
131
+ }
98
132
  /**
99
133
  * The reply as an RFC 5322 message with a plain-text and an HTML part, followed by its files, for
100
134
  * a mailbox to send in the same thread. It names no sender: the mailbox sends from its own address.
package/dist/email.js CHANGED
@@ -235,6 +235,17 @@ function fileNameParameter(parameter, filename) {
235
235
  }
236
236
  /** The header that names a reply, as {@link EmailReply.replyKey} sets it. */
237
237
  export const REPLY_KEY_HEADER = "X-SegmentStream-Reply";
238
+ /** A reply's key, as {@link EmailReply.replyKey} takes it. */
239
+ export const REPLY_KEY = /^[A-Za-z0-9_-]{1,128}$/;
240
+ /**
241
+ * Where the agent's email channel `name` delivers a reply the ontology sends from its mailbox: a
242
+ * draft a member approved or a staff reply from the inbox. The agent renders and sends it as its
243
+ * own replies, with the mailbox's signature and quote, and only the deployment's own functions may
244
+ * call it.
245
+ */
246
+ export function emailDeliverRoute(name) {
247
+ return `/eve/segmentstream/v1/email/${name}/deliver`;
248
+ }
238
249
  /**
239
250
  * The reply as an RFC 5322 message with a plain-text and an HTML part, followed by its files, for
240
251
  * a mailbox to send in the same thread. It names no sender: the mailbox sends from its own address.
@@ -159,6 +159,9 @@ export function readDraftFiles(input) {
159
159
  contentType: file.contentType,
160
160
  size: file.size,
161
161
  bytes,
162
+ // The bytes are checked against the revision before anything is reserved; a provider that
163
+ // has another sender read them, as mail through the agent's channel, grants it a link.
164
+ link: () => Effect.runPromise(blobs.download(file.entity, file.id, file.property, { revisionId: file.revisionId }, member.principal)).then(({ url }) => url),
162
165
  });
163
166
  }
164
167
  return read;
@@ -1,7 +1,7 @@
1
1
  import { Effect } from "effect";
2
2
  import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
3
- import { renderWorkspaceChannelText, WORKSPACE_CHANNEL_ID, WORKSPACE_CHANNEL_FILES_BYTES, WORKSPACE_CHANNEL_TEXT_LIMIT, workspaceChannelFiles, workspaceChannelSendRoute, } from "../../../workspace-channel.js";
4
- import { filesWithBytes, record, } from "./provider.js";
3
+ import { isWorkspaceChannelRefusal, renderWorkspaceChannelText, WORKSPACE_CHANNEL_ID, WORKSPACE_CHANNEL_MAXIMUM_FILES_BYTES, WORKSPACE_CHANNEL_TEXT_LIMIT, workspaceChannelFiles, workspaceChannelSendRoute, } from "../../../workspace-channel.js";
4
+ import { record } from "./provider.js";
5
5
  /** How long the agent has to send one message through a workspace channel. */
6
6
  const AGENT_SEND_TIMEOUT_MS = 30_000;
7
7
  /**
@@ -31,6 +31,22 @@ export function sendThroughAgent(input) {
31
31
  cause: new Error("The agent's workspace channel is reachable only from the production deployment."),
32
32
  };
33
33
  }
34
+ // Each link is granted just before the message goes, as the person may still read its file.
35
+ const links = yield* Effect.tryPromise(() => Promise.all(files.map(async (file) => {
36
+ if (file.link === undefined) {
37
+ throw new Error("A file of the message has no link to read it by.");
38
+ }
39
+ return {
40
+ filename: file.filename,
41
+ contentType: file.contentType,
42
+ size: file.size,
43
+ url: await file.link(),
44
+ ...(file.bytes === undefined ? {} : { bytes: file.bytes }),
45
+ };
46
+ }))).pipe(Effect.exit);
47
+ if (links._tag === "Failure") {
48
+ return { payload, status: "failed", messageId: null, url: null, cause: links.cause };
49
+ }
34
50
  const body = {
35
51
  operation_id: input.operationId ?? input.destination.id,
36
52
  // A reviewed reply names its chat. A chat post names only the person: the ontology knows
@@ -41,8 +57,8 @@ export function sendThroughAgent(input) {
41
57
  reply_to: input.replyTo,
42
58
  text: input.finalText,
43
59
  // Every agent with this route reads `files`, and refuses them with `files_unsupported`
44
- // when the channel's code takes none: nothing is sent then.
45
- ...(files.length === 0 ? {} : { files: workspaceChannelFiles(filesWithBytes(files)) }),
60
+ // when the channel's code takes none, or `file_too_large` past its limit: nothing is sent.
61
+ ...(files.length === 0 ? {} : { files: workspaceChannelFiles(links.value) }),
46
62
  };
47
63
  const answered = yield* Effect.tryPromise(async () => {
48
64
  const response = await agent.fetch(new Request(new URL(workspaceChannelSendRoute(input.destination.provider), agent.origin), {
@@ -75,6 +91,8 @@ export function sendThroughAgent(input) {
75
91
  messageId: null,
76
92
  url: null,
77
93
  cause: new Error(`The agent's workspace channel refused the message (${typeof answer.error === "string" ? answer.error : `status ${status}`}).`),
94
+ // The provider's refusal, as the channel's `send` named it, is recorded as any channel's.
95
+ ...(isWorkspaceChannelRefusal(answer.error) ? { refusal: answer.error } : {}),
78
96
  };
79
97
  }
80
98
  const id = answer.message_id;
@@ -107,7 +125,8 @@ export const customProvider = {
107
125
  return { finalText, plainText: finalText };
108
126
  },
109
127
  payload: (finalText) => ({ text: finalText }),
110
- // The files go in the one message the channel's code sends, within the contract's bound.
111
- files: { count: AGENT_DRAFT_FILES_LIMIT, bytes: WORKSPACE_CHANNEL_FILES_BYTES },
128
+ // The files go by link in the one message the channel's code sends, within the platform's
129
+ // bound; the channel refuses those past its own `limits.fileBytes`, sending nothing.
130
+ files: { count: AGENT_DRAFT_FILES_LIMIT, bytes: WORKSPACE_CHANNEL_MAXIMUM_FILES_BYTES },
112
131
  send: sendThroughAgent,
113
132
  };
@@ -1,6 +1,6 @@
1
1
  import { Effect } from "effect";
2
2
  import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
3
- import { base64Url, EMAIL_FILES_BYTES, EMAIL_MESSAGE_LIMIT, emailReplyMessage, GmailError, gmailExplanation, GmailMaybeSent, renderEmailHtml, replyAllRecipients, sendGmailMessage, withEmailSignature, } from "../../../email.js";
3
+ import { base64Url, emailDeliverRoute, EMAIL_FILES_BYTES, EMAIL_MESSAGE_LIMIT, emailReplyMessage, GmailError, gmailExplanation, GmailMaybeSent, renderEmailHtml, replyAllRecipients, sendGmailMessage, withEmailSignature, } from "../../../email.js";
4
4
  import { mailboxSignature } from "../../../gmail-send.js";
5
5
  import { ActionRejected } from "../../../errors.js";
6
6
  import { EMAIL_PATTERN } from "../../../property.js";
@@ -105,6 +105,97 @@ function signatureOf(input) {
105
105
  catch: (cause) => cause,
106
106
  }).pipe(Effect.catch((cause) => Effect.logWarning("ontology.chat.email_signature_unavailable").pipe(Effect.annotateLogs({ error: describeChatError(cause, (text) => text) }), Effect.as(undefined))));
107
107
  }
108
+ /** How long the agent's email channel has to send one reply, files uploaded in chunks included. */
109
+ const AGENT_DELIVER_TIMEOUT_MS = 90_000;
110
+ /**
111
+ * One reply through the agent's email channel (`POST` {@link emailDeliverRoute} on the
112
+ * deployment's production origin, with the service's Vercel OIDC token), never retried: the agent
113
+ * renders it as its own replies, with the mailbox's signature and quote, reads each file by its
114
+ * link and sends it in the thread. Undefined when the agent has no such route (`404`), so the
115
+ * ontology sends it itself. The agent's other `4xx` sent nothing; a timeout, a `5xx` or an answer
116
+ * without its shape may have, and is uncertain.
117
+ */
118
+ function deliverThroughAgent(input, reply) {
119
+ return Effect.gen(function* () {
120
+ const agent = input.agent;
121
+ const payload = {
122
+ threadId: reply.threadId,
123
+ to: reply.to,
124
+ cc: reply.cc,
125
+ text: input.plainText,
126
+ ...(reply.files.length === 0 ? {} : { files: reply.files.map((file) => file.filename) }),
127
+ };
128
+ const failed = (cause) => ({
129
+ payload,
130
+ status: "failed",
131
+ messageId: null,
132
+ url: null,
133
+ cause,
134
+ });
135
+ // Each link is granted just before the reply goes, as the person may still read its file.
136
+ const links = yield* Effect.tryPromise(() => Promise.all(reply.files.map(async (file) => {
137
+ if (file.link === undefined) {
138
+ throw new Error("A file of the reply has no link to read it by.");
139
+ }
140
+ return {
141
+ filename: file.filename,
142
+ content_type: file.contentType,
143
+ size: file.size,
144
+ url: await file.link(),
145
+ };
146
+ }))).pipe(Effect.exit);
147
+ if (links._tag === "Failure") {
148
+ return failed(links.cause);
149
+ }
150
+ const body = {
151
+ thread_id: reply.threadId,
152
+ to: reply.to,
153
+ cc: reply.cc,
154
+ text: input.plainText,
155
+ key: reply.key,
156
+ ...(links.value.length === 0 ? {} : { files: links.value }),
157
+ };
158
+ const answered = yield* Effect.tryPromise(async () => {
159
+ const response = await agent.fetch(new Request(new URL(emailDeliverRoute(input.destination.provider), agent.origin), {
160
+ method: "POST",
161
+ headers: {
162
+ authorization: `Bearer ${agent.identityToken}`,
163
+ "content-type": "application/json",
164
+ },
165
+ body: JSON.stringify(body),
166
+ signal: AbortSignal.timeout(AGENT_DELIVER_TIMEOUT_MS),
167
+ }));
168
+ return { status: response.status, text: await response.text() };
169
+ }).pipe(Effect.exit);
170
+ if (answered._tag === "Failure") {
171
+ return { payload, status: "uncertain", messageId: null, url: null, cause: answered.cause };
172
+ }
173
+ const { status, text } = answered.value;
174
+ if (status === 404) {
175
+ return undefined;
176
+ }
177
+ let answer = {};
178
+ try {
179
+ answer = record(JSON.parse(text));
180
+ }
181
+ catch {
182
+ // The status still says what happened; the body only adds the message's id.
183
+ }
184
+ if (status >= 400 && status < 500) {
185
+ return failed(new Error(`The agent's email channel refused the reply (${typeof answer.error === "string" ? answer.error : `status ${status}`}).`));
186
+ }
187
+ const id = answer.message_id;
188
+ return status >= 200 && status < 300 && typeof id === "string" && GMAIL_ID.test(id)
189
+ ? { payload, status: "sent", messageId: id, url: null }
190
+ : {
191
+ payload,
192
+ status: "uncertain",
193
+ messageId: null,
194
+ url: null,
195
+ cause: new Error(`The agent's email channel answered with status ${status}.`),
196
+ };
197
+ });
198
+ }
108
199
  /**
109
200
  * One reply in the person's email thread, from the mailbox the ontology's `chat.email` names, to
110
201
  * its `recipients`, `To` and `Cc`, or else to the destination alone. It reads the thread's subject
@@ -130,6 +221,33 @@ export function sendEmail(input) {
130
221
  if (threadId === null || !GMAIL_ID.test(threadId)) {
131
222
  return failed(new Error("An email reply needs the Gmail thread it answers."));
132
223
  }
224
+ // The agent's reply, as the email channel sends its own: marked as a program's, so no
225
+ // autoresponder answers it, and named by its review, or by the post's own operation, so the
226
+ // channel knows it for the workspace's and never records it again as staff's mail.
227
+ const key = input.operationId !== undefined && input.operationId !== input.destination.id
228
+ ? `post-${input.operationId}`
229
+ : `review-${input.destination.id}`;
230
+ const to = input.recipients?.to ?? [input.destination.external_id];
231
+ const cc = input.recipients?.cc ?? [];
232
+ const sendFiles = input.files ?? [];
233
+ // The agent's email channel renders and sends it as its own replies, signature and quote
234
+ // included; an agent from before its deliver route answers 404, and the mailbox is used here.
235
+ if (input.agent?.origin !== undefined && input.agent.identityToken !== undefined) {
236
+ const delivered = yield* deliverThroughAgent(input, {
237
+ threadId,
238
+ to,
239
+ cc,
240
+ key,
241
+ files: sendFiles,
242
+ });
243
+ if (delivered !== undefined) {
244
+ return delivered;
245
+ }
246
+ }
247
+ // @legacy ontology.email-own-sender The ontology's own mail sender, for an agent without the
248
+ // email channel's deliver route: the files' bytes, read and checked before the reply was
249
+ // reserved, go in its message.
250
+ const files = sendFiles;
133
251
  // The headers read with the reply's people, or else the thread as it is now.
134
252
  const thread = input.thread !== undefined
135
253
  ? undefined
@@ -157,19 +275,10 @@ export function sendEmail(input) {
157
275
  const messages = thread?._tag === "Success" ? record(parse(thread.value.body)).messages : [];
158
276
  return (Array.isArray(messages) ? messages : []).filter((message) => !labelsOf(message).includes("DRAFT"));
159
277
  })());
160
- // The agent's reply, as the email channel sends its own: marked as a program's, so no
161
- // autoresponder answers it, and named by its review, or by the post's own operation, so the
162
- // channel knows it for the workspace's and never records it again as staff's mail.
163
- const key = input.operationId !== undefined && input.operationId !== input.destination.id
164
- ? `post-${input.operationId}`
165
- : `review-${input.destination.id}`;
166
278
  // With the channel's `signature`, the HTML ends with the mailbox's own signature.
167
279
  const html = input.signature === true
168
280
  ? withEmailSignature(input.finalText, yield* signatureOf(input))
169
281
  : input.finalText;
170
- const to = input.recipients?.to ?? [input.destination.external_id];
171
- const cc = input.recipients?.cc ?? [];
172
- const files = input.files ?? [];
173
282
  const raw = emailReplyMessage({
174
283
  to,
175
284
  ...(cc.length === 0 ? {} : { cc }),
@@ -47,27 +47,66 @@ export interface WorkspaceChannelSend {
47
47
  /**
48
48
  * Files the message carries after its text, in order, which a channel whose code takes files
49
49
  * (`limits.files`) sends with it; one that takes none answers `422 { error: "files_unsupported" }`
50
- * and sends nothing. At most {@link WORKSPACE_CHANNEL_FILES_BYTES} bytes together.
50
+ * and sends nothing. One whose files together exceed the channel's `limits.fileBytes` answers
51
+ * `422 { error: "file_too_large" }` and sends nothing.
51
52
  */
52
53
  readonly files?: ReadonlyArray<WorkspaceChannelFile>;
53
54
  }
54
- /** One file of {@link WorkspaceChannelSend}, its bytes in base64. */
55
+ /**
56
+ * One file of {@link WorkspaceChannelSend}, by a short-lived signed read of it in the workspace's
57
+ * file storage: the bytes never travel in the request, so a file may be larger than a request.
58
+ */
55
59
  export interface WorkspaceChannelFile {
56
60
  readonly filename: string;
57
61
  readonly content_type: string;
58
- readonly data: string;
62
+ /** Its size in bytes: the channel refuses a read of any other size and sends nothing. */
63
+ readonly size: number;
64
+ /**
65
+ * An `https` link that reads the file without credentials, valid about a minute. Absent only
66
+ * from an ontology before links, which sends `data`.
67
+ */
68
+ readonly url?: string;
69
+ /**
70
+ * @legacy workspace-channel.base64-files The bytes in base64, beside `url`, for an agent before
71
+ * links; only while a message's files together are at most {@link WORKSPACE_CHANNEL_FILES_BYTES}.
72
+ */
73
+ readonly data?: string;
59
74
  }
60
- /** The most bytes of files one message through a workspace channel carries, before encoding. */
75
+ /**
76
+ * The most bytes of files one message through a workspace channel carries unless the channel's
77
+ * code takes more (`limits.fileBytes`).
78
+ */
61
79
  export declare const WORKSPACE_CHANNEL_FILES_BYTES: number;
62
- /** The files of a {@link WorkspaceChannelSend}, as the send route takes them. */
80
+ /**
81
+ * The most a channel's `limits.fileBytes` may be: what SegmentStream's connection proxy sends from
82
+ * the workspace's file storage in one request, so the channel's code can always send a file it
83
+ * takes through its connection.
84
+ */
85
+ export declare const WORKSPACE_CHANNEL_MAXIMUM_FILES_BYTES: number;
86
+ /**
87
+ * Why a workspace channel's provider refused a message, as its `send` names it: `422 { error }` with
88
+ * one of these sent nothing, and the ontology records the refusal as a first-party channel's.
89
+ */
90
+ export declare const WORKSPACE_CHANNEL_REFUSALS: readonly ["outside_reply_window", "recipient_unreachable", "recipient_opted_out"];
91
+ export type WorkspaceChannelRefusal = (typeof WORKSPACE_CHANNEL_REFUSALS)[number];
92
+ /** Whether `value` is one of {@link WORKSPACE_CHANNEL_REFUSALS}. */
93
+ export declare function isWorkspaceChannelRefusal(value: unknown): value is WorkspaceChannelRefusal;
94
+ /**
95
+ * The files of a {@link WorkspaceChannelSend}, as the send route takes them: each by its link, and
96
+ * its bytes too while the message's files are within {@link WORKSPACE_CHANNEL_FILES_BYTES}.
97
+ */
63
98
  export declare function workspaceChannelFiles(files: ReadonlyArray<{
64
99
  readonly filename: string;
65
100
  readonly contentType: string;
66
- readonly bytes: Uint8Array;
101
+ readonly size: number;
102
+ readonly url: string;
103
+ readonly bytes?: Uint8Array;
67
104
  }>): ReadonlyArray<WorkspaceChannelFile>;
68
105
  /**
69
106
  * The agent's answer: `200 { message_id }` once the channel's `send` returned, with the provider's
70
- * id when it gave one. A `4xx` with `{ error }` sent nothing; anything else may have sent it.
107
+ * id when it gave one. A `4xx` with `{ error }` sent nothing, and an `error` among
108
+ * {@link WORKSPACE_CHANNEL_REFUSALS} says why the provider refused it; anything else may have sent
109
+ * it.
71
110
  */
72
111
  export interface WorkspaceChannelSent {
73
112
  readonly message_id: string | null;
@@ -34,13 +34,45 @@ export const WORKSPACE_CHANNEL_TEXT_LIMIT = 20_000;
34
34
  export function renderWorkspaceChannelText(text, attribution, separator) {
35
35
  return attribution === "" ? text : `${text}${separator}${attribution}`;
36
36
  }
37
- /** The most bytes of files one message through a workspace channel carries, before encoding. */
37
+ /**
38
+ * The most bytes of files one message through a workspace channel carries unless the channel's
39
+ * code takes more (`limits.fileBytes`).
40
+ */
38
41
  export const WORKSPACE_CHANNEL_FILES_BYTES = 3 * 1024 * 1024;
39
- /** The files of a {@link WorkspaceChannelSend}, as the send route takes them. */
42
+ /**
43
+ * The most a channel's `limits.fileBytes` may be: what SegmentStream's connection proxy sends from
44
+ * the workspace's file storage in one request, so the channel's code can always send a file it
45
+ * takes through its connection.
46
+ */
47
+ export const WORKSPACE_CHANNEL_MAXIMUM_FILES_BYTES = 64 * 1024 * 1024;
48
+ /**
49
+ * Why a workspace channel's provider refused a message, as its `send` names it: `422 { error }` with
50
+ * one of these sent nothing, and the ontology records the refusal as a first-party channel's.
51
+ */
52
+ export const WORKSPACE_CHANNEL_REFUSALS = [
53
+ "outside_reply_window",
54
+ "recipient_unreachable",
55
+ "recipient_opted_out",
56
+ ];
57
+ /** Whether `value` is one of {@link WORKSPACE_CHANNEL_REFUSALS}. */
58
+ export function isWorkspaceChannelRefusal(value) {
59
+ return WORKSPACE_CHANNEL_REFUSALS.includes(value);
60
+ }
61
+ /**
62
+ * The files of a {@link WorkspaceChannelSend}, as the send route takes them: each by its link, and
63
+ * its bytes too while the message's files are within {@link WORKSPACE_CHANNEL_FILES_BYTES}.
64
+ */
40
65
  export function workspaceChannelFiles(files) {
66
+ // @legacy workspace-channel.base64-files An agent before links reads only the bytes.
67
+ const inline = files.every((file) => file.bytes !== undefined) &&
68
+ files.reduce((total, file) => total + file.size, 0) <= WORKSPACE_CHANNEL_FILES_BYTES;
41
69
  return files.map((file) => ({
42
70
  filename: file.filename,
43
71
  content_type: file.contentType,
44
- data: Buffer.from(file.bytes).toString("base64"),
72
+ size: file.size,
73
+ url: file.url,
74
+ ...(inline && file.bytes !== undefined
75
+ ? { data: Buffer.from(file.bytes).toString("base64") }
76
+ : {}),
45
77
  }));
46
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.175.0",
3
+ "version": "0.177.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",