@segmentstream/ontology 0.200.2 → 0.201.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,29 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.201.1
4
+
5
+ `chat_send` and `chat_post_message` tell agents to mention the particular people a group-chat
6
+ message is for, @username on Telegram and <@ID> on Slack, from their `user_identity` records:
7
+ a name alone notifies no one.
8
+
9
+ ## 0.201.0
10
+
11
+ Staff choose whom a reply to mail goes to. In an inbox chat on a mailbox, `agent_chat.send` takes
12
+ `to` and `cc` in place of the chat's default, which the new read-only `agent_chat.recipients`
13
+ reads without sending, under the same permission and scope as the reply. `to` is required whenever
14
+ `cc` is given. Addresses are plain addresses, at most 50 together, refused with `recipients_invalid`.
15
+ A person's own chat keeps its person among them (`recipient_required`). Any other chat, a template
16
+ and the agent's sessions are refused with `recipients_unsupported`. Each refusal comes before the
17
+ idempotency key is reserved. The key binds the recipients, and the reply is read in the chat by its
18
+ person and the members it reached.
19
+
20
+ `agent_draft.approve` with other `to` or `cc` is now read by the members the reply reached, not
21
+ those the draft named, and refuses with the same refusal codes.
22
+
23
+ Naming recipients lets a role that holds `agent_chat:post_message` or `agent_chat:send` mail any
24
+ address from the mailbox, as `agent_draft:approve` already could. A workspace that must keep
25
+ replies within a domain sends through its own action, which checks the addresses and calls `send`.
26
+
3
27
  ## 0.200.2
4
28
 
5
29
  Allow authorized members to import chat history into a hosted agent preview for testing.
@@ -71,6 +71,12 @@ export declare const AgentChat: import("./entity.js").Entity<"agent_chat", {
71
71
  }>>>;
72
72
  readonly idempotencyKey: Schema.String;
73
73
  }>, import("./chat.js").ChatPreview>;
74
+ readonly recipients: import("./action.js").Action<Schema.Struct<{
75
+ readonly chat: Schema.Codec<string, string, never, never>;
76
+ }>, {
77
+ readonly to: readonly string[];
78
+ readonly cc: readonly string[];
79
+ }>;
74
80
  readonly importHistory: import("./action.js").Action<Schema.Struct<{
75
81
  readonly chat: Schema.optionalKey<Schema.String>;
76
82
  readonly channel: Schema.optionalKey<Schema.String>;
@@ -132,6 +138,8 @@ export declare const AgentChat: import("./entity.js").Entity<"agent_chat", {
132
138
  readonly id: Schema.String;
133
139
  readonly property: Schema.optionalKey<Schema.String>;
134
140
  }>>>;
141
+ readonly to: Schema.optionalKey<Schema.$Array<Schema.String>>;
142
+ readonly cc: Schema.optionalKey<Schema.$Array<Schema.String>>;
135
143
  readonly as: Schema.optionalKey<Schema.Literals<readonly ["agent", "me"]>>;
136
144
  readonly idempotencyKey: Schema.String;
137
145
  }>, {
@@ -338,6 +338,27 @@ export const AgentChat = entity("agent_chat", {
338
338
  return yield* delivery.value.preview(input);
339
339
  }),
340
340
  }),
341
+ recipients: action({
342
+ description: "Read whom a reply in an inbox chat on a mailbox goes to by default, as send delivers it " +
343
+ "when it names no to or cc: in an email thread's chat everyone of its latest mail, as a " +
344
+ "reply to all, and in a person's own chat that person. It reads the thread from the " +
345
+ "mailbox and sends nothing. Requires agent_chat:send or agent_chat:post_message, within " +
346
+ "their scope. Another chat is refused with refusal recipients_unsupported.",
347
+ permission: ["agent_chat:send", "agent_chat:post_message"],
348
+ input: Schema.Struct({ chat: self.ref }),
349
+ connections: [],
350
+ hints: { readOnly: true, destructive: false, irreversible: false, idempotent: true },
351
+ prepare: ({ input }) => Effect.gen(function* () {
352
+ const delivery = yield* Effect.serviceOption(ChatDelivery);
353
+ const recipients = Option.isSome(delivery) ? delivery.value.recipients : undefined;
354
+ if (recipients === undefined) {
355
+ return yield* new ActionRejected({
356
+ message: "Reading a reply's recipients requires the authenticated ontology service.",
357
+ });
358
+ }
359
+ return yield* recipients({ chat: String(input.chat) });
360
+ }),
361
+ }),
341
362
  importHistory: action({
342
363
  description: "Import a chat's messages from before the agent recorded it, such as a client's earlier " +
343
364
  "chat kept elsewhere: each joins the chat in its place by its time, which the " +
@@ -371,6 +392,9 @@ export const AgentChat = entity("agent_chat", {
371
392
  "sender attribution, except in your own reply in the inbox where its attribution is none. " +
372
393
  "Nothing else chooses the sender. text is Markdown (bold, italic, code, lists, " +
373
394
  "[text](url)); each channel renders it natively and degrades constructs it can't show. " +
395
+ "To address particular people in a group chat, mention each so they are notified: " +
396
+ "@username on Telegram, <@ID> on Slack, from their user_identity records; a name alone " +
397
+ "notifies no one. " +
374
398
  "Ambiguous titles require an explicit ID; a scoped grant names the chat by its agent_chat " +
375
399
  "record. Confirm the full final text, attribution and destination before calling. The " +
376
400
  "agent's tasks cannot call it, and its sessions do not send as the agent: a task's workflow " +
@@ -387,7 +411,12 @@ export const AgentChat = entity("agent_chat", {
387
411
  "the message with refusal buttons_unsupported before spending the key: WhatsApp shows one " +
388
412
  "link, or up to three workspace buttons with labels of at most 20 characters, under at most " +
389
413
  "1,024 characters. files sends stored files after the text in the same delivery; files a " +
390
- "channel cannot send are refused before the key is spent.",
414
+ "channel cannot send are refused before the key is spent. In an inbox chat on a mailbox, " +
415
+ "to and cc name whom the reply goes to in place of the default agent_chat.recipients " +
416
+ "reads, to required with cc; confirm its recipients with the member first. A person's own " +
417
+ "chat keeps its person among them (refusal recipient_required); addresses that are no " +
418
+ "plain addresses, or more than 50, are refused with recipients_invalid, and another chat " +
419
+ "refuses them with recipients_unsupported, each before the key is spent.",
391
420
  // Each voice needs its own permission, which the delivery checks: either opens the action.
392
421
  permission: ["agent_chat:send", "agent_chat:post_message"],
393
422
  input: ChatSendInput,
@@ -408,7 +437,9 @@ export const AgentChat = entity("agent_chat", {
408
437
  description: "Post to an eligible chat ID or exact title, as send with as me does. text is Markdown " +
409
438
  "(bold, italic, code, lists, [text](url)); each channel renders it natively and degrades " +
410
439
  "constructs it can't show. The exact Markdown source is posted; the platform adds the " +
411
- "sender attribution. Ambiguous titles require an explicit ID. Confirm the full final text, " +
440
+ "sender attribution. To address particular people in a group chat, mention each: @username " +
441
+ "on Telegram, <@ID> on Slack, from their user_identity records. Ambiguous titles require " +
442
+ "an explicit ID. Confirm the full final text, " +
412
443
  "attribution and destination before calling. Reuse the same idempotency key on replay; an " +
413
444
  "uncertain operation is never resent.",
414
445
  input: ChatPostInput,
package/dist/analyze.js CHANGED
@@ -3792,11 +3792,12 @@ function checkScope(input) {
3792
3792
  const unscopable = entities
3793
3793
  .flatMap((entity) => entity.actions.map((action) => ({ entity, action })))
3794
3794
  .find(({ entity, action }) => {
3795
- // The built-in agent voice checks its exact destination record in prepare, before sending.
3795
+ // The built-in agent voice checks its exact destination record in prepare, before sending,
3796
+ // as the read of whom a reply there goes to checks it before reading.
3796
3797
  const scopedDelivery = permission === "agent_chat:send" &&
3797
3798
  entity.builtIn &&
3798
3799
  entity.name === AgentChat.name &&
3799
- action.name === "send";
3800
+ (action.name === "send" || action.name === "recipients");
3800
3801
  return (openingPermissions(action).includes(permission) && writesNothing(action) && !scopedDelivery);
3801
3802
  });
3802
3803
  if (unscopable !== undefined) {
package/dist/chat.d.ts CHANGED
@@ -106,11 +106,6 @@ export declare const ChatButtonRows: Schema.$Array<Schema.$Array<Schema.Union<re
106
106
  readonly token: Schema.String;
107
107
  }>]>>>;
108
108
  export type ChatButtonRows = typeof ChatButtonRows.Type;
109
- /**
110
- * Whose voice `agent_chat.send` writes in: `agent`, the default, as the agent, without attribution,
111
- * under `agent_chat:send`; or `me`, as the caller, signed with their name as the workspace's
112
- * `chat.attribution` lays it out, under `agent_chat:post_message`. Input never names another sender.
113
- */
114
109
  export declare const ChatVoice: Schema.Literals<readonly ["agent", "me"]>;
115
110
  export type ChatVoice = typeof ChatVoice.Type;
116
111
  /**
@@ -151,10 +146,18 @@ export declare const ChatSendInput: Schema.Struct<{
151
146
  readonly id: Schema.String;
152
147
  readonly property: Schema.optionalKey<Schema.String>;
153
148
  }>>>;
149
+ readonly to: Schema.optionalKey<Schema.$Array<Schema.String>>;
150
+ readonly cc: Schema.optionalKey<Schema.$Array<Schema.String>>;
154
151
  readonly as: Schema.optionalKey<Schema.Literals<readonly ["agent", "me"]>>;
155
152
  readonly idempotencyKey: Schema.String;
156
153
  }>;
157
154
  export type ChatSendInput = typeof ChatSendInput.Type;
155
+ /** Whom a reply to mail in an inbox chat goes to, as `agent_chat.recipients` reads it. */
156
+ export declare const ChatRecipients: Schema.Struct<{
157
+ readonly to: Schema.$Array<Schema.String>;
158
+ readonly cc: Schema.$Array<Schema.String>;
159
+ }>;
160
+ export type ChatRecipients = typeof ChatRecipients.Type;
158
161
  export declare const ChatPostResult: Schema.Struct<{
159
162
  readonly status: Schema.Literals<readonly ["sent", "uncertain"]>;
160
163
  readonly message_id: Schema.NullOr<Schema.String>;
@@ -324,6 +327,10 @@ declare const ChatDelivery_base: Context.ServiceClass<ChatDelivery, "@segmentstr
324
327
  readonly reviseReview?: (input: ReviewReviseInput) => Effect.Effect<ReviewReviseResult, ActionError>;
325
328
  /** Asks the agent to draft a reply in a chat, in a session of its own; nothing is sent. */
326
329
  readonly draft?: (input: ChatDraftInput) => Effect.Effect<ChatDraftResult, ActionError>;
330
+ /** Whom a reply in an inbox chat on a mailbox goes to by default; it sends nothing. */
331
+ readonly recipients?: (input: {
332
+ readonly chat: string;
333
+ }) => Effect.Effect<ChatRecipients, ActionError>;
327
334
  /** Adds a chat's messages from before the agent recorded it, each in its place by its time. */
328
335
  readonly importHistory?: (input: ChatHistoryImportInput) => Effect.Effect<ChatHistoryImportResult, ActionError>;
329
336
  /** The channels whose private chats staff may take over, as the ontology's inbox lists them. */
package/dist/chat.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { Context, Schema } from "effect";
3
3
  import { BLOB_UPLOAD_ANNOTATION, BlobUpload } from "./blob.js";
4
+ import { EMAIL_RECIPIENTS_LIMIT } from "./email.js";
4
5
  import { CHAT_FILES } from "./files.js";
5
6
  const Identifier = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512));
6
7
  /** Stored files a message carries after its text, each a record and its file property. */
@@ -109,6 +110,8 @@ export const ChatButtonRows = Schema.Array(Schema.Array(ChatButton).check(Schema
109
110
  * under `agent_chat:send`; or `me`, as the caller, signed with their name as the workspace's
110
111
  * `chat.attribution` lays it out, under `agent_chat:post_message`. Input never names another sender.
111
112
  */
113
+ /** Addresses a reply to mail names in `To` or `Cc`, at most as many as a reply goes to. */
114
+ const ChatRecipientList = Schema.Array(Schema.String.check(Schema.isMaxLength(320))).check(Schema.isMaxLength(EMAIL_RECIPIENTS_LIMIT));
112
115
  export const ChatVoice = Schema.Literals(["agent", "me"]);
113
116
  /**
114
117
  * What `agent_chat.send` takes: a message's text, or on WhatsApp an approved template instead, in
@@ -130,12 +133,24 @@ export const ChatSendInput = Schema.Struct({
130
133
  "workspace's button action. A channel that cannot show them as given refuses the message.",
131
134
  })),
132
135
  files: Schema.optionalKey(ChatMessageFiles),
136
+ to: Schema.optionalKey(ChatRecipientList.check(Schema.isMinLength(1)).annotate({
137
+ description: "A reply to mail in an inbox chat: the addresses it goes to, in place of those the " +
138
+ "chat's reply goes to by default, which agent_chat.recipients reads. Required with cc.",
139
+ })),
140
+ cc: Schema.optionalKey(ChatRecipientList.annotate({
141
+ description: "A reply to mail in an inbox chat: the addresses it copies; none when omitted.",
142
+ })),
133
143
  as: Schema.optionalKey(ChatVoice.annotate({
134
144
  description: "Whose voice the message is in: agent, the default, as the agent, without attribution, " +
135
145
  "under agent_chat:send; me, as you, signed with your name, under agent_chat:post_message.",
136
146
  })),
137
147
  idempotencyKey: Identifier,
138
148
  });
149
+ /** Whom a reply to mail in an inbox chat goes to, as `agent_chat.recipients` reads it. */
150
+ export const ChatRecipients = Schema.Struct({
151
+ to: Schema.Array(Schema.String),
152
+ cc: Schema.Array(Schema.String),
153
+ });
139
154
  export const ChatPostResult = Schema.Struct({
140
155
  status: Schema.Literals(["sent", "uncertain"]),
141
156
  message_id: Schema.NullOr(Schema.String),
@@ -1,5 +1,7 @@
1
1
  import { type TelegramPreviewContext } from "../../telegram.js";
2
- import { ChatDelivery } from "../../chat.js";
2
+ import { Effect } from "effect";
3
+ import { ChatDelivery, type ChatSendInput } from "../../chat.js";
4
+ import { ActionRejected } from "../../errors.js";
3
5
  import type { Ontology } from "../../ontology.js";
4
6
  import type { Principal } from "../../runtime.js";
5
7
  import type { CallerContext } from "../caller-context.js";
@@ -9,7 +11,8 @@ import type { DeliveryArchive } from "./archive.js";
9
11
  import type { RuntimeChatEnvelope } from "./envelope.js";
10
12
  import type { ChatArchiveTransport } from "../agent/chat-archive.js";
11
13
  import { type Destination } from "./resolve.js";
12
- import { type AgentChannelTransport } from "./providers/provider.js";
14
+ import { type AgentChannelTransport, type ChatProvider } from "./providers/provider.js";
15
+ import { type EmailRecipients } from "../../email.js";
13
16
  import { type HistoryArchive } from "./history-import.js";
14
17
  export type ChatDeliveryFactory = (input: {
15
18
  readonly backend: OntologyBackend["Service"];
@@ -19,6 +22,28 @@ export type ChatDeliveryFactory = (input: {
19
22
  readonly requestId: string;
20
23
  readonly envelope?: RuntimeChatEnvelope;
21
24
  }) => ChatDelivery["Service"];
25
+ /**
26
+ * A private chat the ontology's inbox registered, as a post's destination: the provider sends to
27
+ * its `recipient` in its thread, and the archive keeps the post under the chat's own id.
28
+ */
29
+ interface InboxChat {
30
+ readonly target: Destination;
31
+ readonly chatId: string;
32
+ readonly threadId: string | null;
33
+ /**
34
+ * The chat is a thread several people share, such as a mailbox's thread with `chats: "thread"`:
35
+ * a reply goes to the people of its latest message, read from the provider when it is sent, and
36
+ * is read by the members among them.
37
+ */
38
+ readonly shared: boolean;
39
+ }
40
+ /**
41
+ * Whom a reply to mail goes to when the caller names it, in place of the chat's default: only in
42
+ * an inbox chat on a provider that takes recipients, with a text, `to` given whenever `cc` is.
43
+ * `undefined` when the caller names no one, and the reply goes where it always went. A person's
44
+ * own chat keeps what is sent there as sent to them, so its person stays among the recipients.
45
+ */
46
+ export declare function chosenRecipients(input: Pick<ChatSendInput, "to" | "cc" | "template">, inbox: InboxChat | undefined, provider: Pick<ChatProvider, "recipients">): Effect.Effect<EmailRecipients | undefined, ActionRejected>;
22
47
  /**
23
48
  * Whether a post would land in the chat it was asked from. A private destination names the
24
49
  * person, not the chat: a Slack direct message is `D…` while the person is `U…`, so a
@@ -44,3 +69,4 @@ agent?: {
44
69
  history?: HistoryArchive | "workspace",
45
70
  /** The chat archive's reads, which find the chat a person writes from on a channel. */
46
71
  chats?: ChatArchiveTransport): ChatDeliveryFactory;
72
+ export {};
@@ -13,13 +13,50 @@ import { deliveryArchiveOf } from "./store-delivery.js";
13
13
  import { readDraftFiles, snapshotDraftFiles } from "./draft-files.js";
14
14
  import { channelProvider, chatConnection, chatKind, listedChatProviders, } from "./providers/index.js";
15
15
  import { chatResolver } from "./resolve.js";
16
- import { buttonLines } from "./providers/provider.js";
16
+ import { buttonLines, } from "./providers/provider.js";
17
+ import { EMAIL_RECIPIENTS_LIMIT, emailRecipients } from "../../email.js";
17
18
  import { reviewSender } from "./review-delivery.js";
18
19
  import { chatPeopleQuery, historyImporter } from "./history-import.js";
19
20
  import { reviewReviser } from "./review-revision.js";
20
21
  import { CHAT_DRAFT_PERMISSION, chatDrafter } from "./chat-draft.js";
21
22
  import { channelIdentityProvider, WORKSPACE_CHANNEL_KIND } from "../../chat-channel.js";
22
23
  const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
24
+ /** Why a message cannot name whom it goes to, before anything is reserved or sent. */
25
+ const recipientsRefused = (refusal, message, cause) => new ActionRejected({
26
+ message: `${message} Nothing was sent.`,
27
+ refusal,
28
+ ...(cause === undefined ? {} : { cause }),
29
+ });
30
+ /**
31
+ * Whom a reply to mail goes to when the caller names it, in place of the chat's default: only in
32
+ * an inbox chat on a provider that takes recipients, with a text, `to` given whenever `cc` is.
33
+ * `undefined` when the caller names no one, and the reply goes where it always went. A person's
34
+ * own chat keeps what is sent there as sent to them, so its person stays among the recipients.
35
+ */
36
+ export function chosenRecipients(input, inbox, provider) {
37
+ return Effect.gen(function* () {
38
+ if (input.to === undefined && input.cc === undefined) {
39
+ return undefined;
40
+ }
41
+ if (provider.recipients !== true || inbox === undefined || input.template !== undefined) {
42
+ return yield* recipientsRefused("recipients_unsupported", "Only a text reply in an inbox chat on a mailbox names whom it goes to.");
43
+ }
44
+ const to = input.to;
45
+ if (to === undefined) {
46
+ return yield* recipientsRefused("recipients_invalid", "cc needs to beside it.");
47
+ }
48
+ const recipients = yield* Effect.try({
49
+ try: () => emailRecipients({ to, cc: input.cc ?? [] }),
50
+ catch: (cause) => recipientsRefused("recipients_invalid", `A reply to mail goes to 1 to ${EMAIL_RECIPIENTS_LIMIT} plain addresses together, in ` +
51
+ "to and cc.", cause),
52
+ });
53
+ const person = inbox.target.external_id.trim().toLowerCase();
54
+ if (!inbox.shared && ![...recipients.to, ...recipients.cc].includes(person)) {
55
+ return yield* recipientsRefused("recipient_required", "A reply in a person's own chat goes to that person, in to or cc.");
56
+ }
57
+ return recipients;
58
+ });
59
+ }
23
60
  /** The channels a runtime source can name, which a verified pairing's provider is one of. */
24
61
  const CHANNEL_PROVIDERS = new Set(["telegram", "slack", "whatsapp"]);
25
62
  /**
@@ -108,7 +145,11 @@ chats) {
108
145
  // A session posts for its member only the exact text they approved in their chat, whose
109
146
  // frozen preview binds no template, buttons or files.
110
147
  if (context.caller.kind === "runtime" &&
111
- (template !== undefined || buttons !== undefined || input.files !== undefined)) {
148
+ (template !== undefined ||
149
+ buttons !== undefined ||
150
+ input.files !== undefined ||
151
+ input.to !== undefined ||
152
+ input.cc !== undefined)) {
112
153
  return yield* new ActionRejected({
113
154
  message: "The agent's sessions post only the text a member approved, as written.",
114
155
  });
@@ -187,6 +228,8 @@ chats) {
187
228
  message: "Chat posting requires a configured connection for the chat's provider.",
188
229
  });
189
230
  }
231
+ // Whom a reply to mail goes to, when the caller names it in place of the chat's default.
232
+ const recipients = yield* chosenRecipients(input, inbox, provider);
190
233
  // The agent's own message carries no attribution, as a reply a member approved does not,
191
234
  // and neither does a member's unsigned reply in the inbox; without one, no separator.
192
235
  const signature = asAgent ? "" : requester.attribution;
@@ -340,7 +383,8 @@ chats) {
340
383
  inbox,
341
384
  preview,
342
385
  buttons,
343
- fingerprint: deliveryFingerprint(preview, requester, undefined, buttons),
386
+ recipients,
387
+ fingerprint: deliveryFingerprint(preview, requester, undefined, buttons, recipients),
344
388
  compose: Effect.succeed({
345
389
  preview,
346
390
  payload: undefined,
@@ -510,6 +554,12 @@ chats) {
510
554
  }
511
555
  return readers;
512
556
  });
557
+ /**
558
+ * Who reads a reply to the addresses it goes to: a person's own chat stays theirs, and the
559
+ * members among the addresses read it too, as a thread's chat's members read the replies that
560
+ * reach them.
561
+ */
562
+ const replyReaders = (target, addresses) => readersOf(target.provider, addresses).pipe(Effect.map((readers) => [...new Set([...target.participantIds, ...readers])]));
513
563
  const report = (phase, cause) => Effect.logWarning("ontology.chat.delivery_diagnostic").pipe(Effect.annotateLogs({
514
564
  phase,
515
565
  requestId: context.requestId,
@@ -520,13 +570,13 @@ chats) {
520
570
  * latest message, as a reply to all, read through the chat's connection as a reply there reads
521
571
  * them when it is sent. It sends nothing.
522
572
  */
523
- const draftAudience = (inbox) => Effect.gen(function* () {
573
+ const threadAudience = (inbox, action) => Effect.gen(function* () {
524
574
  const provider = channelProvider(ontology.manifest.chat, inbox.target.provider);
525
575
  const connection = chatConnection(ontology.manifest.chat, inbox.target.provider);
526
576
  if (inbox.threadId === null || provider?.threadRecipients === undefined || !connection) {
527
577
  return yield* new ActionRejected({
528
- message: "Drafting a reply in a chat several people share requires a configured connection " +
529
- "for the chat's provider.",
578
+ message: "A reply in a chat several people share requires a configured connection for the " +
579
+ "chat's provider.",
530
580
  });
531
581
  }
532
582
  return yield* provider
@@ -534,7 +584,7 @@ chats) {
534
584
  transport: connections({
535
585
  identityToken: context.identityToken,
536
586
  audit: {
537
- action: "agent_chat.draft",
587
+ action,
538
588
  principalId: context.principal.id,
539
589
  requestId: context.requestId,
540
590
  },
@@ -582,12 +632,13 @@ chats) {
582
632
  requestId: context.requestId,
583
633
  inboxChat: (chat) => inboxChat(chat, CHAT_DRAFT_PERMISSION),
584
634
  channelKind: (name) => chatKind(ontology.manifest.chat, name),
585
- audience: draftAudience,
635
+ audience: (inbox) => threadAudience(inbox, "agent_chat.draft"),
586
636
  readers: readersOf,
587
637
  }),
588
638
  sendReview: reviewSender({
589
639
  ontology,
590
640
  connections,
641
+ readers: readersOf,
591
642
  agent: agentTransport,
592
643
  cloud,
593
644
  previews: destinationPreviews,
@@ -596,6 +647,35 @@ chats) {
596
647
  identityToken: context.identityToken,
597
648
  requestId: context.requestId,
598
649
  }),
650
+ recipients: ({ chat }) => Effect.gen(function* () {
651
+ // The agent's sessions never name whom a message goes to, so they have nothing to read.
652
+ if (context.caller.kind === "runtime") {
653
+ return yield* recipientsRefused("recipients_unsupported", "The agent's sessions post only the text a member approved, as written.");
654
+ }
655
+ // Either voice may name the recipients of a reply it sends, within its own scope.
656
+ const permission = isAllowed("agent_chat:post_message", context.principal)
657
+ ? "agent_chat:post_message"
658
+ : "agent_chat:send";
659
+ if (!isAllowed(permission, context.principal)) {
660
+ return yield* new PermissionDenied({
661
+ message: "agent_chat:send or agent_chat:post_message is required.",
662
+ permission,
663
+ });
664
+ }
665
+ const inbox = yield* inboxChat(chat, permission);
666
+ const provider = inbox === undefined
667
+ ? undefined
668
+ : channelProvider(ontology.manifest.chat, inbox.target.provider);
669
+ if (inbox === undefined || provider?.recipients !== true) {
670
+ return yield* recipientsRefused("recipients_unsupported", "Only a reply in an inbox chat on a mailbox names whom it goes to.");
671
+ }
672
+ // A thread's reply goes to all of its latest message's people; a person's own chat's
673
+ // reply to its person alone.
674
+ const audience = inbox.shared
675
+ ? yield* threadAudience(inbox, "agent_chat.recipients")
676
+ : { to: [inbox.target.external_id.trim().toLowerCase()], cc: [] };
677
+ return { to: [...audience.to], cc: [...audience.cc] };
678
+ }),
599
679
  preview: (input) => check({ ...input, as: "me" }, false).pipe(Effect.flatMap(({ compose }) => compose), Effect.map(({ preview }) => preview)),
600
680
  validate: (input) => check(input).pipe(Effect.asVoid),
601
681
  // @legacy ontology.chat-post-message
@@ -609,6 +689,7 @@ chats) {
609
689
  const checked = yield* check(input);
610
690
  const { target, requester, connection, provider, inbox, compose } = checked;
611
691
  const buttons = "buttons" in checked ? checked.buttons : undefined;
692
+ const recipients = "recipients" in checked ? checked.recipients : undefined;
612
693
  const scope = deliveryScopeDigest({
613
694
  via: requester.via,
614
695
  subjectId: requester.id,
@@ -654,7 +735,7 @@ chats) {
654
735
  const references = named.map((file) => [file.entity, file.id.toLowerCase(), file.property ?? "content", ""]);
655
736
  const fingerprint = references.length === 0 || !("preview" in checked) || checked.preview === undefined
656
737
  ? checked.fingerprint
657
- : deliveryFingerprint(checked.preview, requester, undefined, buttons, undefined, references);
738
+ : deliveryFingerprint(checked.preview, requester, undefined, buttons, recipients, references);
658
739
  if (named.length > 0 && (input.template !== undefined || archive === cloud)) {
659
740
  // @legacy agent.cloud-chat-archive Files go only through the workspace chat store.
660
741
  return yield* new ActionRejected({
@@ -665,7 +746,10 @@ chats) {
665
746
  // A message read when it is sent, a template, its files, or the people of a shared
666
747
  // thread's reply, may read differently by the time its key is replayed, or not at all: a
667
748
  // replay recovers the first attempt's receipt before it.
668
- if ((input.template !== undefined || named.length > 0 || inbox?.shared === true) &&
749
+ if ((input.template !== undefined ||
750
+ named.length > 0 ||
751
+ inbox?.shared === true ||
752
+ recipients !== undefined) &&
669
753
  archive.find !== undefined) {
670
754
  const found = yield* archive
671
755
  .find({ delivery: { scope, key, fingerprint }, identityToken: context.identityToken })
@@ -718,16 +802,17 @@ chats) {
718
802
  requestId: context.requestId,
719
803
  },
720
804
  });
721
- // A shared thread's reply goes to all of its latest message's people and is read by the
722
- // members among them, as the agent's own reply there is.
723
- const audience = inbox?.shared === true && inbox.threadId !== null && provider.threadRecipients
724
- ? yield* provider
725
- .threadRecipients({ transport, connection, threadId: inbox.threadId })
726
- .pipe(Effect.tapError((error) => error.cause === undefined ? Effect.void : report("recipients", error.cause)))
727
- : undefined;
805
+ // A reply goes to whom the caller named, else a shared thread's reply to all of its latest
806
+ // message's people; either is read by the members among them, as the agent's own reply is.
807
+ const audience = recipients ??
808
+ (inbox?.shared === true && inbox.threadId !== null && provider.threadRecipients
809
+ ? yield* provider
810
+ .threadRecipients({ transport, connection, threadId: inbox.threadId })
811
+ .pipe(Effect.tapError((error) => error.cause === undefined ? Effect.void : report("recipients", error.cause)))
812
+ : undefined);
728
813
  const participantIds = audience === undefined
729
814
  ? target.participantIds
730
- : yield* readersOf(target.provider, [...audience.to, ...audience.cc]);
815
+ : yield* replyReaders(target, [...audience.to, ...audience.cc]);
731
816
  // The message as sent without formatting: the text, then any attribution.
732
817
  const plainText = preview.attribution === ""
733
818
  ? preview.text
@@ -833,9 +918,10 @@ chats) {
833
918
  ...(buttons === undefined ? {} : { buttons }),
834
919
  ...(sendFiles.length === 0 ? {} : { files: sendFiles }),
835
920
  ...(inbox === undefined ? {} : { threadId: inbox.threadId, chatId: inbox.chatId }),
836
- ...(audience === undefined
837
- ? {}
838
- : { recipients: { to: audience.to, cc: audience.cc }, thread: audience.thread }),
921
+ ...(audience === undefined ? {} : { recipients: { to: audience.to, cc: audience.cc } }),
922
+ // Without the thread's headers, as when the caller named the recipients, the provider
923
+ // reads the thread itself.
924
+ ...(audience?.thread === undefined ? {} : { thread: audience.thread }),
839
925
  // Files go in the person's chat, which their account is not on Slack.
840
926
  ...(inbox === undefined && sendFiles.length > 0 && place !== undefined
841
927
  ? { chatId: place.chatId }
@@ -363,6 +363,7 @@ export const emailProvider = {
363
363
  payload: (finalText) => ({ html: finalText }),
364
364
  // The files go in the reply's one message, under the bound every email sender keeps.
365
365
  files: { count: AGENT_DRAFT_FILES_LIMIT, bytes: EMAIL_FILES_BYTES },
366
+ recipients: true,
366
367
  send: sendEmail,
367
368
  threadRecipients: emailThreadRecipients,
368
369
  };
@@ -134,8 +134,8 @@ export interface ChatSend {
134
134
  */
135
135
  readonly files?: ReadonlyArray<ChatSendFile>;
136
136
  /**
137
- * Whom a reply to mail goes to, in `To` and `Cc`, in place of the destination alone; others
138
- * ignore it.
137
+ * Whom a reply to mail goes to, in `To` and `Cc`, in place of the destination alone, for a
138
+ * provider that declares `recipients`.
139
139
  */
140
140
  readonly recipients?: {
141
141
  readonly to: ReadonlyArray<string>;
@@ -197,6 +197,12 @@ export interface ChatProvider {
197
197
  * file with `files_unsupported`.
198
198
  */
199
199
  readonly files?: ChatFileLimits;
200
+ /**
201
+ * Whether a message names whom it goes to beside its chat, in `To` and `Cc`, as a reply to mail
202
+ * does: absent for a provider whose message goes to its chat, which refuses them with
203
+ * `recipients_unsupported`.
204
+ */
205
+ readonly recipients?: true;
200
206
  /**
201
207
  * Why the provider cannot send files of these types and sizes beyond its `files` limits, or
202
208
  * undefined when it can. It is checked before the message is reserved, as `refuseButtons` is.
@@ -153,6 +153,12 @@ export declare function deliverAgentReply<E = never>(transport: AgentReplyTransp
153
153
  */
154
154
  export declare function reviewSender(input: AgentReplyTransport & {
155
155
  readonly principal: Principal;
156
+ /**
157
+ * The members among the addresses a reply to mail goes to, who read it in a thread's chat:
158
+ * an approval that names other recipients is read by those it reaches, not those the draft
159
+ * named.
160
+ */
161
+ readonly readers?: (channel: string, addresses: ReadonlyArray<string>) => Effect.Effect<ReadonlyArray<string>>;
156
162
  }): (send: ReviewSendInput) => Effect.Effect<{
157
163
  files?: readonly AgentDraftFile[] | undefined;
158
164
  recipients?: EmailRecipients | undefined;
@@ -406,6 +406,7 @@ export function reviewSender(input) {
406
406
  if (!email && (send.to !== undefined || send.cc !== undefined)) {
407
407
  return yield* new ActionRejected({
408
408
  message: "Only a reply to mail takes to and cc; this reply goes to its chat.",
409
+ refusal: "recipients_unsupported",
409
410
  });
410
411
  }
411
412
  // A reply to mail goes to the review's `to` and `cc`, the thread's people, unless the member
@@ -425,6 +426,7 @@ export function reviewSender(input) {
425
426
  catch: (cause) => new ActionRejected({
426
427
  message: `A reply to mail goes to 1 to ${EMAIL_RECIPIENTS_LIMIT} plain addresses ` +
427
428
  "together, in to and cc; nothing was sent.",
429
+ refusal: "recipients_invalid",
428
430
  cause,
429
431
  }),
430
432
  })
@@ -468,13 +470,20 @@ export function reviewSender(input) {
468
470
  }
469
471
  const operationId = String(review.id);
470
472
  const person = typeof review.person_id === "string" ? review.person_id : null;
473
+ // The channel the chat is on, by name. A review held before channels had names has none:
474
+ // it is on the channel named after its kind.
475
+ // @legacy ontology.reviews-without-channel
476
+ const channel = typeof review.channel === "string" && review.channel !== ""
477
+ ? review.channel
478
+ : String(review.channel_kind);
479
+ // A reply whose recipients the member changed is read by the members it reaches.
480
+ const participants = recipients !== undefined &&
481
+ (send.to !== undefined || send.cc !== undefined) &&
482
+ input.readers !== undefined
483
+ ? yield* input.readers(channel, [...recipients.to, ...recipients.cc])
484
+ : participantsOfDraft(review.participants);
471
485
  const prepared = yield* prepareAgentReply(input, {
472
- // The channel the chat is on, by name. A review held before channels had names has none:
473
- // it is on the channel named after its kind.
474
- // @legacy ontology.reviews-without-channel
475
- channel: typeof review.channel === "string" && review.channel !== ""
476
- ? review.channel
477
- : String(review.channel_kind),
486
+ channel,
478
487
  channelKind: String(review.channel_kind),
479
488
  declared: true,
480
489
  chatId: String(review.chat_id),
@@ -487,7 +496,7 @@ export function reviewSender(input) {
487
496
  files,
488
497
  ...(recipients === undefined ? {} : { recipients }),
489
498
  person,
490
- participants: participantsOfDraft(review.participants),
499
+ participants,
491
500
  // The agent's session holds its draft: the session sees an edit, a redraft it never wrote,
492
501
  // and a reply sent without some of its files as a message it did not write, and its own
493
502
  // draft sent as written as its own.
@@ -33,6 +33,8 @@ export declare const ChatSendWire: Schema.encodeKeys<Schema.Struct<{
33
33
  readonly id: Schema.String;
34
34
  readonly property: Schema.optionalKey<Schema.String>;
35
35
  }>>>;
36
+ readonly to: Schema.optionalKey<Schema.$Array<Schema.String>>;
37
+ readonly cc: Schema.optionalKey<Schema.$Array<Schema.String>>;
36
38
  readonly as: Schema.optionalKey<Schema.Literals<readonly ["agent", "me"]>>;
37
39
  readonly idempotencyKey: Schema.String;
38
40
  }>, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.200.2",
3
+ "version": "0.201.1",
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",