@segmentstream/ontology 0.177.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 +21 -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/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
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
|
+
|
|
3
24
|
## 0.177.0
|
|
4
25
|
|
|
5
26
|
Workspace channels take files by link, with a file limit of their own and the provider's
|
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,
|
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",
|