@segmentstream/ontology 0.176.0 → 0.178.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 +45 -0
- package/dist/agent-chat.js +8 -5
- package/dist/service/chat/chat-draft.d.ts +24 -1
- package/dist/service/chat/chat-draft.js +102 -20
- package/dist/service/chat/delivery.js +31 -0
- package/dist/service/chat/providers/custom.js +25 -6
- package/dist/workspace-channel.d.ts +46 -7
- package/dist/workspace-channel.js +35 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,50 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.178.0
|
|
4
|
+
|
|
5
|
+
`agent_chat.draft` asks the agent for a draft in an email thread's chat (`chats: "thread"` on the
|
|
6
|
+
email channel), which it refused before.
|
|
7
|
+
|
|
8
|
+
- The draft is for the sender of the mail it answers: the `message` named, or else the thread's
|
|
9
|
+
latest mail a person wrote. That sender, by the member the mail was recorded as, is the person
|
|
10
|
+
the drafting session acts as, with the roles they hold now. When the mail names no member, or
|
|
11
|
+
that member is no longer active, the request is refused and nothing is recorded; it never falls
|
|
12
|
+
back to the requester's roles.
|
|
13
|
+
- The draft goes to everyone in the thread, as the agent's own reply to that mail does: to its
|
|
14
|
+
sender, copying the thread's other people without the mailbox, as the thread reads when the
|
|
15
|
+
draft is asked for, and is read by the members among them. The ontology names them to the agent
|
|
16
|
+
in `to`, `cc` and `participants`, in place of one `recipient`, and the agent holds the draft with
|
|
17
|
+
them, so its approval replies to all in the thread. Asking reads the thread through the mailbox's
|
|
18
|
+
connection and sends nothing; a thread that cannot be read is refused.
|
|
19
|
+
- An agent SDK released before such drafts refuses a request without a `recipient`, and the
|
|
20
|
+
ontology says that the workspace's agent predates drafts in a chat several people share, rather
|
|
21
|
+
than have it hold a draft for its person alone. Upgrade both SDKs to draft in thread chats; a
|
|
22
|
+
person's own chat drafts as before.
|
|
23
|
+
|
|
24
|
+
## 0.177.0
|
|
25
|
+
|
|
26
|
+
Workspace channels take files by link, with a file limit of their own and the provider's
|
|
27
|
+
refusals.
|
|
28
|
+
|
|
29
|
+
- **Files by link.** Each file `send` receives is `{ filename, contentType, size, url, read() }`.
|
|
30
|
+
`url` is a short-lived link to the file in the workspace's file storage, to hand to a provider
|
|
31
|
+
that fetches files itself. `read()` reads the bytes once and checks their size. The ontology and
|
|
32
|
+
`segmentstream__attachments_send` no longer put files' bytes in the send route's request, so a
|
|
33
|
+
message can carry more than 3 MiB.
|
|
34
|
+
- **`limits.fileBytes`.** The most bytes of files one message carries, up to 64 MiB, 3 MiB when
|
|
35
|
+
absent. Files past it are refused (`file_too_large`) and nothing is sent.
|
|
36
|
+
- **`file` on the connection.** The channel's connection takes `file`, one of the files `send` was
|
|
37
|
+
given, as the whole body of a `POST`, `PUT` or `PATCH`. SegmentStream reads it from file storage,
|
|
38
|
+
up to 64 MiB, so its bytes never pass through the function.
|
|
39
|
+
- **Refusals.** `send` may return `{ refusal }`: `outside_reply_window`, `recipient_unreachable`
|
|
40
|
+
or `recipient_opted_out`. The message sent nothing, and the platform records the refusal as
|
|
41
|
+
WhatsApp's.
|
|
42
|
+
|
|
43
|
+
Action required for a workspace channel whose `send` reads `file.bytes`: call `await file.read()`,
|
|
44
|
+
or hand `file.url` to the provider. `bytes` is deprecated and present only while a message's files
|
|
45
|
+
are at most 3 MiB together. Upgrade the agent and ontology SDKs together; each still works with
|
|
46
|
+
the other's previous release.
|
|
47
|
+
|
|
3
48
|
## 0.176.0
|
|
4
49
|
|
|
5
50
|
The ontology's replies to mail, a draft a member approves and a staff reply from the inbox, are
|
package/dist/agent-chat.js
CHANGED
|
@@ -260,11 +260,14 @@ export const AgentChat = entity("agent_chat", {
|
|
|
260
260
|
description: "Ask the agent to draft a reply in a person's private chat, such as to a message it " +
|
|
261
261
|
"stayed silent on while the chat was handed over: message names the person's message by " +
|
|
262
262
|
"its id in the chat store, their latest when omitted, and note says what to write, in " +
|
|
263
|
-
"your words.
|
|
264
|
-
"
|
|
265
|
-
"
|
|
266
|
-
"
|
|
267
|
-
"
|
|
263
|
+
"your words. In an email thread's chat the person is the sender of the mail it answers, " +
|
|
264
|
+
"the latest a person wrote when omitted, and the draft goes to everyone in the thread. " +
|
|
265
|
+
"The agent writes it in a session of its own, acting as the person and reading only, and " +
|
|
266
|
+
"holds it as an agent_draft with mode requested and the key this returns, which a member " +
|
|
267
|
+
"then approves, edits, rejects or sends back like any draft. Nothing is sent. The person " +
|
|
268
|
+
"and the agent's chat with them never see the note. Refused for a group chat, another " +
|
|
269
|
+
"chat with no one person, a person who is no active member, the agent's own sessions and " +
|
|
270
|
+
"its tasks.",
|
|
268
271
|
hints: { destructive: false, irreversible: false, idempotent: false },
|
|
269
272
|
input: Schema.Struct({
|
|
270
273
|
chat: self.ref,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type { ChatDraftInput } from "../../chat.js";
|
|
3
|
-
import { PermissionDenied, type ActionError } from "../../errors.js";
|
|
3
|
+
import { ActionRejected, PermissionDenied, type ActionError } from "../../errors.js";
|
|
4
4
|
import type { Principal } from "../../runtime.js";
|
|
5
5
|
import type { CallerContext } from "../caller-context.js";
|
|
6
6
|
import type { OntologyBackend } from "../runtime-contract.js";
|
|
@@ -15,6 +15,16 @@ export interface DraftChat {
|
|
|
15
15
|
readonly target: Destination;
|
|
16
16
|
readonly chatId: string;
|
|
17
17
|
readonly threadId: string | null;
|
|
18
|
+
/**
|
|
19
|
+
* The chat is a thread several people share, such as a mailbox's thread with `chats: "thread"`:
|
|
20
|
+
* the draft is for the writer of the message it answers, and goes to everyone in the thread.
|
|
21
|
+
*/
|
|
22
|
+
readonly shared: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** Whom a reply in a thread several people share goes to and copies, as a reply to all of it. */
|
|
25
|
+
export interface DraftAudience {
|
|
26
|
+
readonly to: ReadonlyArray<string>;
|
|
27
|
+
readonly cc: ReadonlyArray<string>;
|
|
18
28
|
}
|
|
19
29
|
/**
|
|
20
30
|
* Asks the agent to draft a reply in a person's private chat (`agent_chat.draft`), usually to a
|
|
@@ -24,6 +34,12 @@ export interface DraftChat {
|
|
|
24
34
|
* `agent_draft` keyed `request:<request>`, with mode `requested`. Nothing is sent, here or there:
|
|
25
35
|
* only an approval of that draft sends it. Nothing is recorded before the agent writes the draft,
|
|
26
36
|
* so a request the agent could not take leaves nothing behind.
|
|
37
|
+
*
|
|
38
|
+
* In a thread several people share, such as a mailbox's thread with `chats: "thread"`, the person
|
|
39
|
+
* is the writer of the message the draft answers, and the draft goes to everyone in the thread, as
|
|
40
|
+
* the agent's own reply there does: the request names them in `to` and `cc`, with the members who
|
|
41
|
+
* read it in `participants`, in place of one `recipient`, so that an agent from before such drafts
|
|
42
|
+
* refuses it rather than hold a draft for its person alone.
|
|
27
43
|
*/
|
|
28
44
|
export declare function chatDrafter(input: {
|
|
29
45
|
readonly agent: AgentChannelTransport;
|
|
@@ -35,6 +51,13 @@ export declare function chatDrafter(input: {
|
|
|
35
51
|
readonly inboxChat: (chat: string) => Effect.Effect<DraftChat | undefined, ActionError | PermissionDenied>;
|
|
36
52
|
/** The kind of the agent's channel `name`, such as telegram. */
|
|
37
53
|
readonly channelKind: (name: string) => string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Whom a reply in a thread several people share goes to now, as a reply to all of it, read from
|
|
56
|
+
* the chat's provider; refused when it cannot be read.
|
|
57
|
+
*/
|
|
58
|
+
readonly audience: (chat: DraftChat) => Effect.Effect<DraftAudience, ActionRejected>;
|
|
59
|
+
/** The members among the addresses a reply goes to on `channel`, who read it in the chat. */
|
|
60
|
+
readonly readers: (channel: string, addresses: ReadonlyArray<string>) => Effect.Effect<ReadonlyArray<string>>;
|
|
38
61
|
}): (request: ChatDraftInput) => Effect.Effect<{
|
|
39
62
|
request: `${string}-${string}-${string}-${string}-${string}`;
|
|
40
63
|
key: string;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
|
+
import { AGENT_DRAFT_PARTICIPANTS_LIMIT } from "../../agent-draft.js";
|
|
4
|
+
import { EMAIL_RECIPIENTS_LIMIT, emailRecipients } from "../../email.js";
|
|
3
5
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
4
6
|
import { isAllowed } from "../../permissions.js";
|
|
5
7
|
import { chatCursorOf } from "../../runtime/chat-messages.js";
|
|
@@ -13,17 +15,46 @@ export const REQUESTED_DRAFT_KEY = "request:";
|
|
|
13
15
|
const AGENT_DRAFT_TIMEOUT_MS = 15_000;
|
|
14
16
|
/** How far back the person's latest message is looked for. */
|
|
15
17
|
const LATEST_MESSAGES = 100;
|
|
18
|
+
/** A message a person wrote in the chat, as it reached the provider, still there. */
|
|
19
|
+
function written(message) {
|
|
20
|
+
return (message.direction === "inbound" &&
|
|
21
|
+
message.author === "human" &&
|
|
22
|
+
message.deletedAt === null &&
|
|
23
|
+
message.replacedBy === null);
|
|
24
|
+
}
|
|
16
25
|
/**
|
|
17
26
|
* The person's message a draft answers: one a person wrote in the chat, recorded as it reached the
|
|
18
27
|
* provider. The chat has one person, so a message recorded without its writer's user, as one that
|
|
19
28
|
* arrives while staff hold the chat is, is theirs; one recorded as another user's is not.
|
|
20
29
|
*/
|
|
21
30
|
function answerable(message, person) {
|
|
22
|
-
return (message
|
|
23
|
-
message.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
return (written(message) &&
|
|
32
|
+
(message.userId === null || message.userId.toLowerCase() === person.toLowerCase()));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Whom a draft in a thread several people share goes to: the writer of the message it answers, by
|
|
36
|
+
* their address on the channel, in `to`, and everyone else a reply to all of the thread reaches in
|
|
37
|
+
* `cc`, as the agent's own reply to that message goes. Without the writer's address, the thread's
|
|
38
|
+
* reply to all as it is. `undefined` when that is no reply the channel can send.
|
|
39
|
+
*/
|
|
40
|
+
function draftRecipients(audience, sender) {
|
|
41
|
+
const writer = sender?.trim().toLowerCase();
|
|
42
|
+
const to = writer === undefined || writer === "" ? audience.to : [writer];
|
|
43
|
+
// Each address once, whatever its case: the thread's own may differ from the writer's.
|
|
44
|
+
const taken = new Set(to.map((address) => address.trim().toLowerCase()));
|
|
45
|
+
const cc = [...audience.to, ...audience.cc].filter((address) => {
|
|
46
|
+
const key = address.trim().toLowerCase();
|
|
47
|
+
return taken.has(key) ? false : (taken.add(key), true);
|
|
48
|
+
});
|
|
49
|
+
try {
|
|
50
|
+
return emailRecipients({
|
|
51
|
+
to,
|
|
52
|
+
cc: cc.slice(0, Math.max(0, EMAIL_RECIPIENTS_LIMIT - to.length)),
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
27
58
|
}
|
|
28
59
|
/**
|
|
29
60
|
* Asks the agent to draft a reply in a person's private chat (`agent_chat.draft`), usually to a
|
|
@@ -33,6 +64,12 @@ function answerable(message, person) {
|
|
|
33
64
|
* `agent_draft` keyed `request:<request>`, with mode `requested`. Nothing is sent, here or there:
|
|
34
65
|
* only an approval of that draft sends it. Nothing is recorded before the agent writes the draft,
|
|
35
66
|
* so a request the agent could not take leaves nothing behind.
|
|
67
|
+
*
|
|
68
|
+
* In a thread several people share, such as a mailbox's thread with `chats: "thread"`, the person
|
|
69
|
+
* is the writer of the message the draft answers, and the draft goes to everyone in the thread, as
|
|
70
|
+
* the agent's own reply there does: the request names them in `to` and `cc`, with the members who
|
|
71
|
+
* read it in `participants`, in place of one `recipient`, so that an agent from before such drafts
|
|
72
|
+
* refuses it rather than hold a draft for its person alone.
|
|
36
73
|
*/
|
|
37
74
|
export function chatDrafter(input) {
|
|
38
75
|
const { backend, principal, agent } = input;
|
|
@@ -70,12 +107,16 @@ export function chatDrafter(input) {
|
|
|
70
107
|
message: "Only a person's private chat the inbox registered is drafted in.",
|
|
71
108
|
});
|
|
72
109
|
}
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
110
|
+
const shared = chat.shared;
|
|
111
|
+
const member = (id) => id === null || id === undefined || backend.agents === undefined
|
|
112
|
+
? Effect.succeed(undefined)
|
|
113
|
+
: backend.agents.member(id);
|
|
114
|
+
// The draft acts as the person, with the roles they hold now, as their own session does. A
|
|
115
|
+
// thread several people share has no one person: its draft acts as the writer of the message
|
|
116
|
+
// it answers.
|
|
117
|
+
const chatPerson = shared ? undefined : chat.target.participantIds?.[0];
|
|
118
|
+
const own = shared ? undefined : yield* member(chatPerson);
|
|
119
|
+
if (!shared && (chatPerson === undefined || own === undefined)) {
|
|
79
120
|
return yield* new ActionRejected({
|
|
80
121
|
message: "The chat's person is no longer an active member.",
|
|
81
122
|
});
|
|
@@ -94,15 +135,29 @@ export function chatDrafter(input) {
|
|
|
94
135
|
});
|
|
95
136
|
}
|
|
96
137
|
const coordinates = { channel, chatId: chat.chatId };
|
|
138
|
+
// In a thread several people share, a message any of them wrote; else one of its person's.
|
|
139
|
+
const answers = (found) => chatPerson === undefined ? written(found) : answerable(found, chatPerson);
|
|
97
140
|
const recent = yield* store.read({ ...coordinates, limit: LATEST_MESSAGES });
|
|
98
141
|
const message = request.message === undefined
|
|
99
|
-
? recent.messages.find(
|
|
142
|
+
? recent.messages.find(answers)
|
|
100
143
|
: (yield* store.read({ ...coordinates, id: request.message, limit: 1 })).messages[0];
|
|
101
|
-
if (message === undefined || !
|
|
144
|
+
if (message === undefined || !answers(message)) {
|
|
145
|
+
return yield* new ActionRejected({
|
|
146
|
+
message: shared
|
|
147
|
+
? request.message === undefined
|
|
148
|
+
? "The chat holds no message of a person to answer."
|
|
149
|
+
: "The message is no message a person wrote in the chat."
|
|
150
|
+
: request.message === undefined
|
|
151
|
+
? "The chat holds no message of its person to answer."
|
|
152
|
+
: "The message is no message of the chat's person.",
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// In a thread, the message's writer, by the user it was recorded as, as they are now: never
|
|
156
|
+
// the requester, whose roles are not the person's.
|
|
157
|
+
const person = shared ? yield* member(message.userId) : own;
|
|
158
|
+
if (person === undefined) {
|
|
102
159
|
return yield* new ActionRejected({
|
|
103
|
-
message:
|
|
104
|
-
? "The chat holds no message of its person to answer."
|
|
105
|
-
: "The message is no message of the chat's person.",
|
|
160
|
+
message: "The message's writer is not an active member.",
|
|
106
161
|
});
|
|
107
162
|
}
|
|
108
163
|
if (agent.origin === undefined || agent.identityToken === undefined) {
|
|
@@ -110,6 +165,25 @@ export function chatDrafter(input) {
|
|
|
110
165
|
message: "The agent drafts replies only on the production deployment.",
|
|
111
166
|
});
|
|
112
167
|
}
|
|
168
|
+
// In a thread, the draft goes to everyone in it as a reply to all, as the agent's own reply
|
|
169
|
+
// to the message does, read now with the chat the draft is written from.
|
|
170
|
+
const recipients = shared
|
|
171
|
+
? draftRecipients(yield* input.audience(chat), message.senderId)
|
|
172
|
+
: undefined;
|
|
173
|
+
if (shared && recipients === undefined) {
|
|
174
|
+
return yield* new ActionRejected({
|
|
175
|
+
message: "The thread names no one a reply to all can go to.",
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
// Read by the members among them, its person first, as the agent's own reply is.
|
|
179
|
+
const participants = recipients === undefined
|
|
180
|
+
? undefined
|
|
181
|
+
: [
|
|
182
|
+
...new Set([
|
|
183
|
+
person.principal.id,
|
|
184
|
+
...(yield* input.readers(channel, [...recipients.to, ...recipients.cc])),
|
|
185
|
+
]),
|
|
186
|
+
].slice(0, AGENT_DRAFT_PARTICIPANTS_LIMIT);
|
|
113
187
|
// The draft is written from the chat as it is now: a message the person writes after the
|
|
114
188
|
// newest one is newer than the draft.
|
|
115
189
|
const newest = recent.messages[0];
|
|
@@ -141,7 +215,11 @@ export function chatDrafter(input) {
|
|
|
141
215
|
...(file.size === undefined ? {} : { size: file.size }),
|
|
142
216
|
})),
|
|
143
217
|
},
|
|
144
|
-
recipient:
|
|
218
|
+
// A thread's draft names everyone it goes to in place of one recipient: an agent from
|
|
219
|
+
// before such drafts refuses the request, rather than hold one for its person alone.
|
|
220
|
+
...(recipients === undefined
|
|
221
|
+
? { recipient: chat.target.external_id }
|
|
222
|
+
: { to: recipients.to, cc: recipients.cc, participants }),
|
|
145
223
|
reply_thread_id: chat.threadId ?? "",
|
|
146
224
|
reply_to: replyTo,
|
|
147
225
|
basis: newest === undefined ? chatCursorOf(message) : chatCursorOf(newest),
|
|
@@ -165,11 +243,15 @@ export function chatDrafter(input) {
|
|
|
165
243
|
message: "The agent could not be reached to draft a reply. Try again.",
|
|
166
244
|
});
|
|
167
245
|
}
|
|
168
|
-
// An agent from before drafts on request knows only redrafts, and refuses this request
|
|
246
|
+
// An agent from before drafts on request knows only redrafts, and refuses this request; one
|
|
247
|
+
// from before drafts in a thread several people share refuses a request without a recipient.
|
|
169
248
|
if (answered.value === 404 || answered.value === 400) {
|
|
170
249
|
return yield* new ActionRejected({
|
|
171
|
-
message:
|
|
172
|
-
"
|
|
250
|
+
message: shared
|
|
251
|
+
? "The workspace's agent cannot draft replies in a chat several people share: its " +
|
|
252
|
+
"@segmentstream/agent predates it."
|
|
253
|
+
: "The workspace's agent cannot draft replies on request: its @segmentstream/agent " +
|
|
254
|
+
"predates it.",
|
|
173
255
|
});
|
|
174
256
|
}
|
|
175
257
|
if (answered.value < 200 || answered.value >= 300) {
|
|
@@ -493,6 +493,35 @@ chats) {
|
|
|
493
493
|
requestId: context.requestId,
|
|
494
494
|
error: describeChatError(cause, (text) => context.identityToken ? text.split(context.identityToken).join("[redacted]") : text),
|
|
495
495
|
}));
|
|
496
|
+
/**
|
|
497
|
+
* Whom a draft asked for in a thread several people share goes to: the people of the thread's
|
|
498
|
+
* latest message, as a reply to all, read through the chat's connection as a reply there reads
|
|
499
|
+
* them when it is sent. It sends nothing.
|
|
500
|
+
*/
|
|
501
|
+
const draftAudience = (inbox) => Effect.gen(function* () {
|
|
502
|
+
const provider = channelProvider(ontology.manifest.chat, inbox.target.provider);
|
|
503
|
+
const connection = chatConnection(ontology.manifest.chat, inbox.target.provider);
|
|
504
|
+
if (inbox.threadId === null || provider?.threadRecipients === undefined || !connection) {
|
|
505
|
+
return yield* new ActionRejected({
|
|
506
|
+
message: "Drafting a reply in a chat several people share requires a configured connection " +
|
|
507
|
+
"for the chat's provider.",
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
return yield* provider
|
|
511
|
+
.threadRecipients({
|
|
512
|
+
transport: connections({
|
|
513
|
+
identityToken: context.identityToken,
|
|
514
|
+
audit: {
|
|
515
|
+
action: "agent_chat.draft",
|
|
516
|
+
principalId: context.principal.id,
|
|
517
|
+
requestId: context.requestId,
|
|
518
|
+
},
|
|
519
|
+
}),
|
|
520
|
+
connection,
|
|
521
|
+
threadId: inbox.threadId,
|
|
522
|
+
})
|
|
523
|
+
.pipe(Effect.tapError((error) => error.cause === undefined ? Effect.void : report("recipients", error.cause)));
|
|
524
|
+
});
|
|
496
525
|
// The agent, which sends through a workspace channel, with this request's identity token.
|
|
497
526
|
const agentTransport = {
|
|
498
527
|
origin: agent.origin,
|
|
@@ -531,6 +560,8 @@ chats) {
|
|
|
531
560
|
requestId: context.requestId,
|
|
532
561
|
inboxChat: (chat) => inboxChat(chat, CHAT_DRAFT_PERMISSION),
|
|
533
562
|
channelKind: (name) => chatKind(ontology.manifest.chat, name),
|
|
563
|
+
audience: draftAudience,
|
|
564
|
+
readers: readersOf,
|
|
534
565
|
}),
|
|
535
566
|
sendReview: reviewSender({
|
|
536
567
|
ontology,
|
|
@@ -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,
|
|
4
|
-
import {
|
|
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
|
|
45
|
-
...(files.length === 0 ? {} : { files: workspaceChannelFiles(
|
|
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
|
|
111
|
-
|
|
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.
|
|
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
|
-
/**
|
|
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
|
-
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
|
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
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "0.178.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",
|