@segmentstream/ontology 0.164.0 → 0.165.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.
@@ -9,6 +9,7 @@ import { describeChatError } from "./diagnostics.js";
9
9
  import { deliveryRequester } from "./attribution.js";
10
10
  import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./idempotency.js";
11
11
  import { deliveryArchiveOf } from "./store-delivery.js";
12
+ import { readDraftFiles, snapshotDraftFiles } from "./draft-files.js";
12
13
  import { channelProvider, chatConnection, chatKind, listedChatProviders, } from "./providers/index.js";
13
14
  import { chatResolver } from "./resolve.js";
14
15
  import { buttonLines } from "./providers/provider.js";
@@ -509,7 +510,7 @@ chats) {
509
510
  function deliver(input, asAgent) {
510
511
  return Effect.gen(function* () {
511
512
  const checked = yield* check(input, true, asAgent);
512
- const { target, requester, connection, provider, inbox, fingerprint, compose } = checked;
513
+ const { target, requester, connection, provider, inbox, compose } = checked;
513
514
  const buttons = "buttons" in checked ? checked.buttons : undefined;
514
515
  const scope = deliveryScopeDigest({
515
516
  via: requester.via,
@@ -548,9 +549,25 @@ chats) {
548
549
  history_recorded: receipt.historyRecorded,
549
550
  };
550
551
  });
552
+ // A post's stored files, as the caller names them. The key binds those names: a retry after
553
+ // a file changed or went recovers the first attempt's receipt, read before the files are.
554
+ const named = "files" in input && input.files !== undefined ? input.files : [];
555
+ const references = named.map((file) => [file.entity, file.id.toLowerCase(), file.property ?? "content", ""]);
556
+ const fingerprint = references.length === 0 || !("preview" in checked) || checked.preview === undefined
557
+ ? checked.fingerprint
558
+ : deliveryFingerprint(checked.preview, requester, undefined, buttons, undefined, references);
559
+ if (named.length > 0 &&
560
+ (("template" in input && input.template !== undefined) || archive === cloud)) {
561
+ // @legacy agent.cloud-chat-archive Files go only through the workspace chat store.
562
+ return yield* new ActionRejected({
563
+ message: "The post's files cannot be sent (files_unsupported); nothing was sent.",
564
+ refusal: "files_unsupported",
565
+ });
566
+ }
551
567
  // A message read when it is sent, a template, may read differently by the time its key
552
568
  // is replayed, or not at all: a replay recovers the first attempt's receipt before it.
553
- if ("template" in input && input.template !== undefined && archive.find !== undefined) {
569
+ if ((("template" in input && input.template !== undefined) || named.length > 0) &&
570
+ archive.find !== undefined) {
554
571
  const found = yield* archive
555
572
  .find({ delivery: { scope, key, fingerprint }, identityToken: context.identityToken })
556
573
  .pipe(Effect.tapError((error) => report("find", error)), Effect.mapError(unreserved));
@@ -558,6 +575,41 @@ chats) {
558
575
  return yield* replayed(found);
559
576
  }
560
577
  }
578
+ // Each file read as the caller now, at the revision it holds, which the delivery records:
579
+ // every refusal comes before the key is reserved.
580
+ let heldFiles = [];
581
+ if (named.length > 0) {
582
+ const snapshot = yield* snapshotDraftFiles({
583
+ manifest: ontology.manifest,
584
+ backend: context.backend,
585
+ principal: context.principal,
586
+ channel: target.provider,
587
+ files: named,
588
+ });
589
+ if ("refusal" in snapshot) {
590
+ return yield* new ActionRejected({
591
+ message: `The post's files cannot be sent (${snapshot.refusal}); nothing was sent.`,
592
+ refusal: snapshot.refusal,
593
+ });
594
+ }
595
+ heldFiles = snapshot.files;
596
+ }
597
+ const bindings = heldFiles.map((file) => [file.entity, file.id, file.property, file.revisionId]);
598
+ // The files' bytes, or links to them, read as the caller once more before anything is sent.
599
+ const sendFiles = yield* readDraftFiles({
600
+ backend: context.backend,
601
+ person: null,
602
+ principal: context.principal,
603
+ files: heldFiles,
604
+ fetch,
605
+ ...(provider.files?.transfer === undefined ? {} : { transfer: provider.files.transfer }),
606
+ });
607
+ if (sendFiles === undefined) {
608
+ return yield* new ActionRejected({
609
+ message: "A file of the post is gone, or you may no longer read it; nothing was sent.",
610
+ refusal: "file_unavailable",
611
+ });
612
+ }
561
613
  const { preview, payload, summary } = yield* compose;
562
614
  // The message as sent without formatting: the text, then any attribution.
563
615
  const plainText = preview.attribution === ""
@@ -566,6 +618,17 @@ chats) {
566
618
  // A member who answers in the inbox writes as themselves, not as the agent.
567
619
  const staff = inbox !== undefined && context.caller.kind === "member" && !asAgent;
568
620
  const place = inbox ?? (yield* personalChat(target));
621
+ // Slack shares files only in a chat, never to a member's account: a post with files to a
622
+ // person needs the direct message they wrote in, refused before the key is reserved.
623
+ if (sendFiles.length > 0 &&
624
+ provider.provider === "slack" &&
625
+ target.kind === "private" &&
626
+ place === undefined) {
627
+ return yield* new ActionRejected({
628
+ message: "No direct message with this person is known to share files in; nothing was sent.",
629
+ refusal: "files_unsupported",
630
+ });
631
+ }
569
632
  const operationId = context.envelope?.operation_id ?? randomUUID();
570
633
  const event = {
571
634
  eventId: `chat-delivery:${operationId}:reserve`,
@@ -593,7 +656,15 @@ chats) {
593
656
  toolName: null,
594
657
  argsSummary: JSON.stringify(summary ?? payload ?? provider.payload(preview.finalText, buttons)),
595
658
  status: "requested",
596
- attachments: [],
659
+ // The files as the chat's history shows them, as the agent's own sends record them.
660
+ attachments: heldFiles.map((file, index) => ({
661
+ id: `chat-delivery:${operationId}:file:${index}`,
662
+ kind: "file",
663
+ filename: file.filename,
664
+ contentType: file.contentType,
665
+ size: file.size,
666
+ })),
667
+ ...(bindings.length === 0 ? {} : { files: bindings }),
597
668
  requestedBy: requester,
598
669
  url: null,
599
670
  at: new Date().toISOString(),
@@ -632,7 +703,12 @@ chats) {
632
703
  operationId,
633
704
  ...(payload === undefined ? {} : { payload }),
634
705
  ...(buttons === undefined ? {} : { buttons }),
706
+ ...(sendFiles.length === 0 ? {} : { files: sendFiles }),
635
707
  ...(inbox === undefined ? {} : { threadId: inbox.threadId, chatId: inbox.chatId }),
708
+ // Files go in the person's chat, which their account is not on Slack.
709
+ ...(inbox === undefined && sendFiles.length > 0 && place !== undefined
710
+ ? { chatId: place.chatId }
711
+ : {}),
636
712
  transport: connections({
637
713
  identityToken: context.identityToken,
638
714
  audit: {
@@ -51,6 +51,8 @@ export declare function snapshotDraftFiles(input: {
51
51
  export declare function readDraftFiles(input: {
52
52
  readonly backend: OntologyBackend["Service"];
53
53
  readonly person: string | null;
54
+ /** Reads the files as this principal in place of `person`, as a member's own post does. */
55
+ readonly principal?: Principal;
54
56
  readonly files: ReadonlyArray<AgentDraftFile>;
55
57
  readonly fetch: typeof fetch;
56
58
  /**
@@ -87,9 +87,11 @@ export function readDraftFiles(input) {
87
87
  return [];
88
88
  }
89
89
  const { blobs, agents } = input.backend;
90
- const member = input.person === null || agents === undefined
91
- ? undefined
92
- : yield* agents.member(input.person);
90
+ const member = input.principal !== undefined
91
+ ? { principal: input.principal }
92
+ : input.person === null || agents === undefined
93
+ ? undefined
94
+ : yield* agents.member(input.person);
93
95
  if (blobs === undefined || member === undefined) {
94
96
  return undefined;
95
97
  }
@@ -52,14 +52,14 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
52
52
  readonly provider: "telegram";
53
53
  readonly chat_id: string;
54
54
  readonly thread_id: string;
55
- readonly message_id?: string | undefined;
56
- readonly channel?: string | undefined;
57
55
  readonly location?: {
58
56
  readonly latitude: number;
59
57
  readonly longitude: number;
60
58
  readonly horizontal_accuracy?: number | undefined;
61
59
  readonly live_period?: number | undefined;
62
60
  } | undefined;
61
+ readonly channel?: string | undefined;
62
+ readonly message_id?: string | undefined;
63
63
  readonly attachments?: readonly {
64
64
  readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
65
65
  readonly file_id: string;
@@ -75,8 +75,8 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
75
75
  readonly provider: "slack" | "whatsapp";
76
76
  readonly chat_id: string;
77
77
  readonly thread_id: string;
78
- readonly message_id?: string | undefined;
79
78
  readonly channel?: string | undefined;
79
+ readonly message_id?: string | undefined;
80
80
  readonly sender?: string | undefined;
81
81
  readonly peer?: string | undefined;
82
82
  } | undefined;
@@ -106,14 +106,14 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
106
106
  readonly provider: "telegram";
107
107
  readonly chat_id: string;
108
108
  readonly thread_id: string;
109
- readonly message_id?: string | undefined;
110
- readonly channel?: string | undefined;
111
109
  readonly location?: {
112
110
  readonly latitude: number;
113
111
  readonly longitude: number;
114
112
  readonly horizontal_accuracy?: number | undefined;
115
113
  readonly live_period?: number | undefined;
116
114
  } | undefined;
115
+ readonly channel?: string | undefined;
116
+ readonly message_id?: string | undefined;
117
117
  readonly attachments?: readonly {
118
118
  readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
119
119
  readonly file_id: string;
@@ -129,8 +129,8 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
129
129
  readonly provider: "slack" | "whatsapp";
130
130
  readonly chat_id: string;
131
131
  readonly thread_id: string;
132
- readonly message_id?: string | undefined;
133
132
  readonly channel?: string | undefined;
133
+ readonly message_id?: string | undefined;
134
134
  readonly sender?: string | undefined;
135
135
  readonly peer?: string | undefined;
136
136
  } | undefined;
@@ -58,7 +58,7 @@ export declare function chatMessageEvent(message: ChatMessage): {
58
58
  }[];
59
59
  edited: boolean;
60
60
  deleted: boolean;
61
- delivery: "pending" | "read" | "sent" | "uncertain" | "failed" | "delivered" | null;
61
+ delivery: "pending" | "read" | "sent" | "failed" | "uncertain" | "delivered" | null;
62
62
  disposition: "skipped" | "claimed" | "turn" | null;
63
63
  replaced_by: string | null;
64
64
  };
@@ -39,16 +39,18 @@ export declare function reviewSender(input: {
39
39
  /** Reads a file's bytes by its download grant; `fetch` by default. */
40
40
  readonly fetch?: typeof fetch;
41
41
  }): (send: ReviewSendInput) => Effect.Effect<{
42
+ files?: readonly import("../../agent-draft.js").AgentDraftFile[] | undefined;
42
43
  recipients?: EmailRecipients | undefined;
43
44
  message?: string | undefined;
44
- status: "sent" | "uncertain" | "failed";
45
+ status: "sent" | "failed" | "uncertain";
45
46
  text: string;
46
47
  messageId: string | null;
47
48
  duplicate: true;
48
49
  } | {
50
+ files?: readonly import("../../agent-draft.js").AgentDraftFile[] | undefined;
49
51
  recipients?: EmailRecipients | undefined;
50
52
  message?: string | undefined;
51
- status: "sent" | "uncertain" | "failed";
53
+ status: "sent" | "failed" | "uncertain";
52
54
  text: string;
53
55
  messageId: string | null;
54
56
  duplicate: false;
@@ -106,10 +106,26 @@ export function reviewSender(input) {
106
106
  }),
107
107
  })
108
108
  : undefined;
109
+ const heldFiles = draftFilesOf(review.files);
110
+ // An approval may leave files out, never add one: each it names is one the draft holds.
111
+ const files = send.files === undefined
112
+ ? heldFiles
113
+ : heldFiles.filter((file) => send.files.some((named) => named.entity === file.entity &&
114
+ named.id.toLowerCase() === file.id &&
115
+ named.property === file.property &&
116
+ named.revisionId === file.revisionId));
117
+ if (send.files !== undefined && files.length !== send.files.length) {
118
+ return yield* new ActionRejected({
119
+ message: "An approval sends only files the draft holds; nothing was sent.",
120
+ refusal: "file_not_held",
121
+ });
122
+ }
109
123
  if (status !== "pending") {
110
124
  if ((status === "sent" || status === "uncertain") &&
111
125
  text === review.text &&
112
- (recipients === undefined || sameRecipients(recipients, review))) {
126
+ (recipients === undefined || sameRecipients(recipients, review)) &&
127
+ // The draft keeps the files it sent: another choice of them is another approval.
128
+ (send.files === undefined || files.length === heldFiles.length)) {
113
129
  return {
114
130
  status,
115
131
  text,
@@ -117,6 +133,7 @@ export function reviewSender(input) {
117
133
  duplicate: true,
118
134
  ...(typeof review.message === "string" ? { message: review.message } : {}),
119
135
  ...(recipients === undefined ? {} : { recipients }),
136
+ ...(files.length === 0 && send.files === undefined ? {} : { files }),
120
137
  };
121
138
  }
122
139
  return yield* new ActionRejected({
@@ -163,7 +180,6 @@ export function reviewSender(input) {
163
180
  try: () => provider.render(text, "", ""),
164
181
  catch: (cause) => new ActionRejected({ message: "The Markdown could not be rendered.", cause }),
165
182
  });
166
- const files = draftFilesOf(review.files);
167
183
  if ((text.trim() === "" && files.length === 0) ||
168
184
  text.length > AGENT_DRAFT_TEXT_LIMIT ||
169
185
  finalText.length > provider.limit ||
@@ -260,9 +276,11 @@ export function reviewSender(input) {
260
276
  kind: "outbound",
261
277
  messageId: null,
262
278
  replyToMessageId: replyTo,
263
- // The agent's session holds its draft: the session sees an edit, and a redraft it never
264
- // wrote, as a message it did not write, and its own draft sent as written as its own.
279
+ // The agent's session holds its draft: the session sees an edit, a redraft it never wrote,
280
+ // and a reply sent without some of its files as a message it did not write, and its own
281
+ // draft sent as written as its own.
265
282
  sessionId: text === draft &&
283
+ files.length === heldFiles.length &&
266
284
  (review.revises_id === null || review.revises_id === undefined) &&
267
285
  typeof review.session_id === "string"
268
286
  ? review.session_id
@@ -320,6 +338,7 @@ export function reviewSender(input) {
320
338
  messageId: earlier.messageId,
321
339
  duplicate: true,
322
340
  ...(recipients === undefined ? {} : { recipients }),
341
+ ...(files.length === 0 && send.files === undefined ? {} : { files }),
323
342
  };
324
343
  }
325
344
  // Each file as its person may read it now, the revision held: everything that can fail is
@@ -421,6 +440,7 @@ export function reviewSender(input) {
421
440
  duplicate: true,
422
441
  ...(message === null ? {} : { message }),
423
442
  ...(recipients === undefined ? {} : { recipients }),
443
+ ...(files.length === 0 && send.files === undefined ? {} : { files }),
424
444
  };
425
445
  }
426
446
  if (reserve.version !== 1 ||
@@ -483,6 +503,7 @@ export function reviewSender(input) {
483
503
  duplicate: false,
484
504
  ...(message === null ? {} : { message }),
485
505
  ...(recipients === undefined ? {} : { recipients }),
506
+ ...(files.length === 0 && send.files === undefined ? {} : { files }),
486
507
  };
487
508
  });
488
509
  }
@@ -3,6 +3,11 @@ export declare const ChatPostWire: Schema.encodeKeys<Schema.Struct<{
3
3
  readonly chat: Schema.String;
4
4
  readonly text: Schema.String;
5
5
  readonly replyTo: Schema.optionalKey<Schema.String>;
6
+ readonly files: Schema.optionalKey<Schema.$Array<Schema.Struct<{
7
+ readonly entity: Schema.String;
8
+ readonly id: Schema.String;
9
+ readonly property: Schema.optionalKey<Schema.String>;
10
+ }>>>;
6
11
  readonly idempotencyKey: Schema.String;
7
12
  }>, {
8
13
  readonly replyTo: "reply_to";
@@ -22,8 +22,8 @@ export declare function searchDocuments(documents: ReadonlyArray<SearchDocument>
22
22
  }[];
23
23
  readonly summary_truncated: boolean;
24
24
  readonly entity?: string | undefined;
25
- readonly metrics?: readonly string[] | undefined;
26
25
  readonly tools?: readonly string[] | undefined;
26
+ readonly metrics?: readonly string[] | undefined;
27
27
  readonly relevance?: number | undefined;
28
28
  }[];
29
29
  /** Bytewise canonical-name ordering is independent of a function instance's locale. */
@@ -29,7 +29,7 @@ export declare const UserIdentity: import("./entity.js").DraftEntity<"user_ident
29
29
  readonly subject: import("./property.js").Property<string, false, false>;
30
30
  readonly username: import("effect/Schema").optionalKey<import("./property.js").Property<string, false, false>>;
31
31
  readonly display: import("effect/Schema").optionalKey<import("./property.js").Property<string, false, false>>;
32
- readonly origin: import("./property.js").Property<"link" | "invite" | "attach" | "channel" | "sign_in" | "pairing", false, false>;
32
+ readonly origin: import("./property.js").Property<"link" | "invite" | "attach" | "sign_in" | "channel" | "pairing", false, false>;
33
33
  readonly revokedAt: import("effect/Schema").optionalKey<import("./property.js").Property<string, false, false>>;
34
34
  readonly accountKey: import("effect/Schema").optionalKey<import("./property.js").Property<string, false, true>>;
35
35
  readonly memberKey: import("effect/Schema").optionalKey<import("./property.js").Property<string, false, true>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.164.0",
3
+ "version": "0.165.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",