@segmentstream/ontology 0.176.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,29 @@
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
+
3
27
  ## 0.176.0
4
28
 
5
29
  The ontology's replies to mail, a draft a member approves and a staff reply from the inbox, are
@@ -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
  };
@@ -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.176.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",