@segmentstream/ontology 0.160.0 → 0.162.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,53 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.162.0
4
+
5
+ A draft can carry record files. `agent_draft.files` lists each file the reply carries as
6
+ `{ entity, id, property, revisionId, filename, contentType, size }`: the revision its person could
7
+ read when the agent held the reply, snapshotted by `POST /api/ontology/agent/drafts` from its new
8
+ `files` (up to 10 record references, read as that person). A draft of files alone has empty text.
9
+ `POST /api/ontology/agent/drafts/files` runs the same check without holding anything, for the
10
+ agent to ask before it promises a file in a reviewed chat.
11
+
12
+ Approving a draft sends its files with the text in the reply's one delivery, each at the revision
13
+ held and read again as the person: by email in the reply's one message (`EMAIL_FILES_BYTES`), and
14
+ through a workspace channel in the `files` of its one `send`. Before anything is reserved, approval
15
+ refuses with the action's `refusal`: `files_unsupported` (the channel sends no files with a reply
16
+ yet, as Telegram, Slack and WhatsApp until a later release, or the chat is still on the archive in
17
+ Cloud), `too_many_files`, `file_too_large` or `file_unavailable`. The delivery binds the files'
18
+ revisions, and the reply's row of the chat store lists them by name, type and size. Chat providers
19
+ declare their `files` limits and a `refuseFiles` check, and `ChatSend` takes `files`.
20
+
21
+ ## 0.161.0
22
+
23
+ Agent drafts name their turn, know whether the person wrote after it, and are decided once.
24
+
25
+ - `agent_draft` has three new managed fields: `turnId`, the agent's turn that wrote the reply;
26
+ `basis`, where that turn had read the chat to (the chat store's place `<time>~<id>` of the
27
+ newest message it took); and `message`, the reply's message in the chat store once an approval
28
+ reserved its delivery.
29
+ - `agent_draft.newer({ drafts })` tells, for up to 50 drafts, whether the person wrote in the chat
30
+ after each draft's turn: `true`, `false`, or `null` when it cannot be told. It needs
31
+ `agent_draft:read`.
32
+ - `agent_draft.approve` takes `unlessNewer: true`: the approval is refused, with nothing reserved
33
+ or sent, when the person wrote after the draft's turn (`refusal: "newer_message"`) or when that
34
+ cannot be told (`refusal: "basis_unknown"`). It is checked under the chat's lock, in the
35
+ transaction that reserves the delivery. Use it for approvals no person reads first, such as a
36
+ workflow's.
37
+ - `agent_draft.dismiss({ draft, reason? })` sets a pending draft aside without judging it, under
38
+ its own permission `agent_draft:dismiss`. Its status, `dismissed`, is final.
39
+ - An approval and a rejection or dismissal of one draft are ordered by the draft's lock: once an
40
+ approval reserved the delivery, a rejection or dismissal is refused (`refusal: "draft_decided"`),
41
+ and an approval of a draft decided meanwhile is refused before anything is sent. Before the chat
42
+ store holds a chat, where they cannot be ordered, the draft records the delivery as the
43
+ approver's, and its history keeps the rejection.
44
+ - Docs: approval by a workflow, with an `after` trigger on the hold, the workspace's own rule and
45
+ `unlessNewer`. A workflow holding `agent_draft:approve` sends replies without a person.
46
+
47
+ Action required: a reader of `agent_draft.status` in workspace code that lists the statuses should
48
+ take `dismissed`, and any status it does not know, as a draft the person never received. Grant
49
+ `agent_draft:dismiss` to the roles that should dismiss drafts.
50
+
3
51
  ## 0.160.0
4
52
 
5
53
  The agent's decided drafts are deleted on the chat store's retention schedule. The chat store's
@@ -26,6 +26,9 @@ export declare const AGENT_DRAFT_STATUSES: readonly [{
26
26
  }, {
27
27
  readonly value: "superseded";
28
28
  readonly description: "Redrafted at a member's request: the draft that revises it replaces it.";
29
+ }, {
30
+ readonly value: "dismissed";
31
+ readonly description: string;
29
32
  }];
30
33
  export declare const AGENT_DRAFT_REVISION_STATES: readonly [{
31
34
  readonly value: "requested";
@@ -43,8 +46,27 @@ export declare const AGENT_DRAFT_MODES: readonly [{
43
46
  }];
44
47
  /** The longest reply a draft holds; a provider may accept less in one message. */
45
48
  export declare const AGENT_DRAFT_TEXT_LIMIT = 20000;
49
+ /** The most files a draft holds, as many as `attachments_send` sends at once. */
50
+ export declare const AGENT_DRAFT_FILES_LIMIT = 10;
51
+ /**
52
+ * A record file a draft holds, snapshotted when the reply was held: the revision the person may
53
+ * read, which approval sends whatever the record holds by then, with what the inbox shows of it.
54
+ */
55
+ export interface AgentDraftFile {
56
+ readonly entity: string;
57
+ readonly id: string;
58
+ readonly property: string;
59
+ readonly revisionId: string;
60
+ readonly filename: string;
61
+ readonly contentType: string;
62
+ readonly size: number;
63
+ }
64
+ /** The files a draft holds, in order; none for a value that is no list of them. */
65
+ export declare function draftFilesOf(value: unknown): ReadonlyArray<AgentDraftFile>;
46
66
  /** The longest reason a member gives for rejecting a reply. */
47
67
  export declare const AGENT_DRAFT_REASON_LIMIT = 2000;
68
+ /** The most drafts one `newer` call answers for. */
69
+ export declare const AGENT_DRAFT_NEWER_LIMIT = 50;
48
70
  /** The longest note a member gives the agent for redrafting a reply. */
49
71
  export declare const AGENT_DRAFT_NOTE_LIMIT = 2000;
50
72
  /** The most members a draft names as the readers of its reply, as many as a reply to mail goes to. */
@@ -70,9 +92,13 @@ export declare const AgentDraft: import("./entity.js").Entity<"agent_draft", {
70
92
  readonly threadId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
71
93
  readonly replyTo: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
72
94
  readonly sessionId: import("./property.js").Property<string, false, false>;
95
+ readonly turnId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
96
+ readonly basis: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
97
+ readonly message: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
73
98
  readonly draft: import("./property.js").Property<string, false, false>;
99
+ readonly files: Schema.optionalKey<import("./property.js").Property<Schema.Json, false, false>>;
74
100
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
75
- readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded", false, false>;
101
+ readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed", false, false>;
76
102
  readonly mode: import("./property.js").Property<"always" | "whenUnsure", false, false>;
77
103
  readonly confidence: Schema.optionalKey<import("./property.js").Property<number, false, false>>;
78
104
  readonly messageId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
@@ -93,6 +119,7 @@ export declare const AgentDraft: import("./entity.js").Entity<"agent_draft", {
93
119
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
94
120
  readonly to: Schema.optionalKey<Schema.$Array<Schema.String>>;
95
121
  readonly cc: Schema.optionalKey<Schema.$Array<Schema.String>>;
122
+ readonly unlessNewer: Schema.optionalKey<Schema.Boolean>;
96
123
  }>, {
97
124
  to?: readonly string[] | undefined;
98
125
  cc?: readonly string[] | undefined;
@@ -110,6 +137,18 @@ export declare const AgentDraft: import("./entity.js").Entity<"agent_draft", {
110
137
  review: string;
111
138
  status: "rejected";
112
139
  }>;
140
+ readonly dismiss: import("./action.js").Action<Schema.Struct<{
141
+ readonly draft: Schema.Codec<string, string, never, never>;
142
+ readonly reason: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
143
+ }>, {
144
+ draft: string;
145
+ status: "dismissed";
146
+ }>;
147
+ readonly newer: import("./action.js").Action<Schema.Struct<{
148
+ readonly drafts: Schema.$Array<Schema.Codec<string, string, never, never>>;
149
+ }>, {
150
+ drafts: readonly import("./chat.js").DraftNewer[];
151
+ }>;
113
152
  readonly revise: import("./action.js").Action<Schema.Struct<{
114
153
  readonly draft: Schema.Codec<string, string, never, never>;
115
154
  readonly note: import("./property.js").Property<string, false, false>;
@@ -137,9 +176,13 @@ export declare const AgentDraft: import("./entity.js").Entity<"agent_draft", {
137
176
  readonly threadId: string | null;
138
177
  readonly replyTo: string | null;
139
178
  readonly sessionId: string;
179
+ readonly turnId: string | null;
180
+ readonly basis: string | null;
181
+ readonly message: string | null;
140
182
  readonly draft: string;
183
+ readonly files: Schema.Json;
141
184
  readonly text: string | null;
142
- readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded";
185
+ readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed";
143
186
  readonly mode: "always" | "whenUnsure";
144
187
  readonly confidence: number | null;
145
188
  readonly messageId: string | null;
@@ -169,9 +212,13 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_draft", {
169
212
  readonly threadId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
170
213
  readonly replyTo: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
171
214
  readonly sessionId: import("./property.js").Property<string, false, false>;
215
+ readonly turnId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
216
+ readonly basis: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
217
+ readonly message: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
172
218
  readonly draft: import("./property.js").Property<string, false, false>;
219
+ readonly files: Schema.optionalKey<import("./property.js").Property<Schema.Json, false, false>>;
173
220
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
174
- readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded", false, false>;
221
+ readonly status: import("./property.js").Property<"pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed", false, false>;
175
222
  readonly mode: import("./property.js").Property<"always" | "whenUnsure", false, false>;
176
223
  readonly confidence: Schema.optionalKey<import("./property.js").Property<number, false, false>>;
177
224
  readonly messageId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
@@ -192,6 +239,7 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_draft", {
192
239
  readonly text: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
193
240
  readonly to: Schema.optionalKey<Schema.$Array<Schema.String>>;
194
241
  readonly cc: Schema.optionalKey<Schema.$Array<Schema.String>>;
242
+ readonly unlessNewer: Schema.optionalKey<Schema.Boolean>;
195
243
  }>, {
196
244
  to?: readonly string[] | undefined;
197
245
  cc?: readonly string[] | undefined;
@@ -209,6 +257,18 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_draft", {
209
257
  review: string;
210
258
  status: "rejected";
211
259
  }>;
260
+ readonly dismiss: import("./action.js").Action<Schema.Struct<{
261
+ readonly draft: Schema.Codec<string, string, never, never>;
262
+ readonly reason: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
263
+ }>, {
264
+ draft: string;
265
+ status: "dismissed";
266
+ }>;
267
+ readonly newer: import("./action.js").Action<Schema.Struct<{
268
+ readonly drafts: Schema.$Array<Schema.Codec<string, string, never, never>>;
269
+ }>, {
270
+ drafts: readonly import("./chat.js").DraftNewer[];
271
+ }>;
212
272
  readonly revise: import("./action.js").Action<Schema.Struct<{
213
273
  readonly draft: Schema.Codec<string, string, never, never>;
214
274
  readonly note: import("./property.js").Property<string, false, false>;
@@ -236,9 +296,13 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_draft", {
236
296
  readonly threadId: string | null;
237
297
  readonly replyTo: string | null;
238
298
  readonly sessionId: string;
299
+ readonly turnId: string | null;
300
+ readonly basis: string | null;
301
+ readonly message: string | null;
239
302
  readonly draft: string;
303
+ readonly files: Schema.Json;
240
304
  readonly text: string | null;
241
- readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded";
305
+ readonly status: "pending" | "sent" | "uncertain" | "failed" | "rejected" | "superseded" | "dismissed";
242
306
  readonly mode: "always" | "whenUnsure";
243
307
  readonly confidence: number | null;
244
308
  readonly messageId: string | null;
@@ -274,6 +338,9 @@ export declare const AGENT_REVIEW_STATUSES: readonly [{
274
338
  }, {
275
339
  readonly value: "superseded";
276
340
  readonly description: "Redrafted at a member's request: the draft that revises it replaces it.";
341
+ }, {
342
+ readonly value: "dismissed";
343
+ readonly description: string;
277
344
  }];
278
345
  /** @deprecated Use {@link AGENT_DRAFT_MODES}. */
279
346
  export declare const AGENT_REVIEW_MODES: readonly [{
@@ -29,6 +29,11 @@ export const AGENT_DRAFT_STATUSES = [
29
29
  value: "superseded",
30
30
  description: "Redrafted at a member's request: the draft that revises it replaces it.",
31
31
  },
32
+ {
33
+ value: "dismissed",
34
+ description: "Dismissed without a judgement on the reply, such as after the person wrote again: the " +
35
+ "person never sees it.",
36
+ },
32
37
  ];
33
38
  export const AGENT_DRAFT_REVISION_STATES = [
34
39
  { value: "requested", description: "A member asked the agent to redraft it; it is redrafting." },
@@ -43,8 +48,59 @@ export const AGENT_DRAFT_MODES = [
43
48
  ];
44
49
  /** The longest reply a draft holds; a provider may accept less in one message. */
45
50
  export const AGENT_DRAFT_TEXT_LIMIT = 20_000;
51
+ /** The most files a draft holds, as many as `attachments_send` sends at once. */
52
+ export const AGENT_DRAFT_FILES_LIMIT = 10;
53
+ const NAME = /^[a-z][a-z0-9_]{0,63}$/;
54
+ const RECORD_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
55
+ /** The files a draft holds, in order; none for a value that is no list of them. */
56
+ export function draftFilesOf(value) {
57
+ const list = typeof value === "string" ? parsed(value) : value;
58
+ if (!Array.isArray(list)) {
59
+ return [];
60
+ }
61
+ return list.flatMap((item) => {
62
+ const file = item;
63
+ return file !== null &&
64
+ typeof file === "object" &&
65
+ typeof file.entity === "string" &&
66
+ NAME.test(file.entity) &&
67
+ typeof file.id === "string" &&
68
+ RECORD_ID.test(file.id) &&
69
+ typeof file.property === "string" &&
70
+ NAME.test(file.property) &&
71
+ typeof file.revisionId === "string" &&
72
+ file.revisionId !== "" &&
73
+ typeof file.filename === "string" &&
74
+ typeof file.contentType === "string" &&
75
+ typeof file.size === "number" &&
76
+ Number.isSafeInteger(file.size) &&
77
+ file.size >= 0
78
+ ? [
79
+ {
80
+ entity: file.entity,
81
+ id: file.id.toLowerCase(),
82
+ property: file.property,
83
+ revisionId: file.revisionId,
84
+ filename: file.filename,
85
+ contentType: file.contentType,
86
+ size: file.size,
87
+ },
88
+ ]
89
+ : [];
90
+ });
91
+ }
92
+ function parsed(text) {
93
+ try {
94
+ return JSON.parse(text);
95
+ }
96
+ catch {
97
+ return undefined;
98
+ }
99
+ }
46
100
  /** The longest reason a member gives for rejecting a reply. */
47
101
  export const AGENT_DRAFT_REASON_LIMIT = 2_000;
102
+ /** The most drafts one `newer` call answers for. */
103
+ export const AGENT_DRAFT_NEWER_LIMIT = 50;
48
104
  /** The longest note a member gives the agent for redrafting a reply. */
49
105
  export const AGENT_DRAFT_NOTE_LIMIT = 2_000;
50
106
  /** The longest list of addresses a draft keeps in `to` or `cc`: as many as a reply goes to. */
@@ -80,6 +136,22 @@ export function participantsOfDraft(value) {
80
136
  .map((id) => id.toLowerCase())),
81
137
  ];
82
138
  }
139
+ /**
140
+ * Refuses a decision on a draft that no longer waits, or whose delivery an approval reserved: the
141
+ * reservation and a rejection or dismissal take the draft's lock in turn, so whichever comes first
142
+ * decides, and the other is refused.
143
+ */
144
+ function undecided(review, decision) {
145
+ if (review.status === "pending" && (review.message === null || review.message === undefined)) {
146
+ return Effect.void;
147
+ }
148
+ return Effect.fail(new ActionRejected({
149
+ message: review.status === "pending"
150
+ ? `An approval is delivering the reply; it cannot be ${decision}.`
151
+ : `The reply was already decided (${review.status}); it cannot be ${decision}.`,
152
+ refusal: "draft_decided",
153
+ }));
154
+ }
83
155
  export const AgentDraft = entity("agent_draft", {
84
156
  // The id of the entity's first name: a changed id would be a new entity, without its records.
85
157
  id: "ent_agent_review",
@@ -156,11 +228,42 @@ export const AgentDraft = entity("agent_draft", {
156
228
  description: "The person's message the reply answers, when the provider replies to one.",
157
229
  }),
158
230
  sessionId: p.text({ managed: true, description: "The agent's session that wrote the reply." }),
231
+ turnId: p.text({
232
+ optional: true,
233
+ managed: true,
234
+ maxLength: 256,
235
+ label: "Turn id",
236
+ description: "The agent's turn that wrote the reply, so that workspace records, such as notes for " +
237
+ "staff, can relate to it. Empty on a draft held before drafts named their turn.",
238
+ }),
239
+ basis: p.text({
240
+ optional: true,
241
+ managed: true,
242
+ maxLength: 128,
243
+ description: "Where the turn that wrote the reply had read the chat to: the newest message it took, " +
244
+ "as the chat store's place in the chat, <time>~<id>. A message the person wrote after " +
245
+ "it is one the reply did not see. Empty until the chat store holds the turn's messages.",
246
+ }),
247
+ message: p.text({
248
+ optional: true,
249
+ managed: true,
250
+ maxLength: 64,
251
+ description: "The reply's message in the chat store once an approval reserved its delivery, by the " +
252
+ "message's id. Empty while it waits, and for a reply delivered before the chat store " +
253
+ "held the chat.",
254
+ }),
159
255
  draft: p.text({
160
256
  managed: true,
161
257
  maxLength: AGENT_DRAFT_TEXT_LIMIT,
162
258
  display: "multiline",
163
- description: "The reply as the agent wrote it, in Markdown.",
259
+ description: "The reply as the agent wrote it, in Markdown; empty when the reply is only its files.",
260
+ }),
261
+ files: p.json({
262
+ optional: true,
263
+ managed: true,
264
+ description: "Record files the reply carries, in order, which approval sends with its text: each " +
265
+ "{ entity, id, property, revisionId, filename, contentType, size }, the revision the " +
266
+ "person could read when the reply was held. Empty when the reply is text alone.",
164
267
  }),
165
268
  text: p.text({
166
269
  optional: true,
@@ -290,6 +393,12 @@ export const AgentDraft = entity("agent_draft", {
290
393
  description: "A reply to mail: the addresses it copies, in place of the draft's cc; [] copies " +
291
394
  "no one, and omitting it keeps them.",
292
395
  })),
396
+ unlessNewer: Schema.optionalKey(Schema.Boolean.annotate({
397
+ description: "true refuses the approval, with refusal newer_message and nothing sent, when the " +
398
+ "person wrote in the chat after the turn that wrote the reply, or basis_unknown when " +
399
+ "that cannot be told. For approvals without a person reading the chat, such as a " +
400
+ "workflow's.",
401
+ })),
293
402
  }),
294
403
  connections: [],
295
404
  prepare: ({ input }) => Effect.gen(function* () {
@@ -305,19 +414,29 @@ export const AgentDraft = entity("agent_draft", {
305
414
  ...(input.text === undefined ? {} : { text: input.text }),
306
415
  ...(input.to === undefined ? {} : { to: input.to }),
307
416
  ...(input.cc === undefined ? {} : { cc: input.cc }),
417
+ ...(input.unlessNewer === true ? { unlessNewer: true } : {}),
308
418
  });
309
419
  }),
310
420
  handler: ({ input, prepared, principal }) => Effect.gen(function* () {
311
421
  const review = yield* self.get(input.draft, { forUpdate: true });
312
422
  const recipients = prepared.recipients;
313
- // The delivery happened, or definitely did not: the record says what the provider did,
314
- // and whom a reply to mail went to, even when another member rejected the draft meanwhile.
423
+ // A replay finds the outcome recorded and keeps who decided. Otherwise this approval is
424
+ // the decision: on the chat store its reservation refused while the draft was decided, and
425
+ // a rejection or dismissal refuses once it is reserved. Only a delivery before the store
426
+ // held the chat can meet one taken meanwhile; the record then says what the provider did,
427
+ // as this approval's, and the history keeps the other decision before it.
428
+ const recorded = review.status === "sent" || review.status === "uncertain" || review.status === "failed";
315
429
  yield* self.update(review, {
316
430
  status: prepared.status,
317
431
  text: prepared.text,
318
432
  messageId: prepared.messageId,
319
- decidedAt: review.decidedAt ?? new Date().toISOString(),
320
- decidedBy: review.decidedById ?? principal.id,
433
+ ...(prepared.message === undefined || prepared.message === null
434
+ ? {}
435
+ : { message: prepared.message }),
436
+ decidedAt: recorded
437
+ ? (review.decidedAt ?? new Date().toISOString())
438
+ : new Date().toISOString(),
439
+ decidedBy: recorded ? (review.decidedById ?? principal.id) : principal.id,
321
440
  ...(recipients === undefined
322
441
  ? {}
323
442
  : {
@@ -355,11 +474,7 @@ export const AgentDraft = entity("agent_draft", {
355
474
  if (review.status === "rejected") {
356
475
  return result;
357
476
  }
358
- if (review.status !== "pending") {
359
- return yield* new ActionRejected({
360
- message: "The reply was already approved and sent; it cannot be rejected.",
361
- });
362
- }
477
+ yield* undecided(review, "rejected");
363
478
  const reason = input.reason?.trim();
364
479
  yield* self.update(review, {
365
480
  status: "rejected",
@@ -370,6 +485,58 @@ export const AgentDraft = entity("agent_draft", {
370
485
  return result;
371
486
  }),
372
487
  }),
488
+ dismiss: action({
489
+ description: "Dismiss the agent's draft reply without judging it, such as when the person wrote again " +
490
+ "after it: the person never sees it. Say why in reason; it stays with the draft and never " +
491
+ "reaches the person or the agent's chat with them. Only a draft that still waits can be " +
492
+ "dismissed, and dismissing one already dismissed returns it.",
493
+ hints: { destructive: true, irreversible: false, idempotent: true },
494
+ input: Schema.Struct({
495
+ draft: self.ref,
496
+ reason: Schema.optionalKey(p.text({
497
+ maxLength: AGENT_DRAFT_REASON_LIMIT,
498
+ description: "Why the reply is no longer to be sent.",
499
+ })),
500
+ }),
501
+ handler: ({ input, principal }) => Effect.gen(function* () {
502
+ const review = yield* self.get(input.draft, { forUpdate: true });
503
+ const result = { draft: review.id, status: "dismissed" };
504
+ if (review.status === "dismissed") {
505
+ return result;
506
+ }
507
+ yield* undecided(review, "dismissed");
508
+ const reason = input.reason?.trim();
509
+ yield* self.update(review, {
510
+ status: "dismissed",
511
+ decidedAt: new Date().toISOString(),
512
+ decidedBy: principal.id,
513
+ ...(reason === undefined || reason === "" ? {} : { reason }),
514
+ });
515
+ return result;
516
+ }),
517
+ }),
518
+ newer: action({
519
+ description: "Tell, for drafts that wait, whether the person wrote in the chat after the turn that " +
520
+ "wrote each reply: newer true when they did, so the reply did not see it; false when they " +
521
+ "did not; null when it cannot be told, as for a draft whose chat the chat store does not " +
522
+ "hold. approve with unlessNewer refuses exactly when newer is true or null.",
523
+ permission: "agent_draft:read",
524
+ hints: { readOnly: true, destructive: false, irreversible: false, idempotent: true },
525
+ input: Schema.Struct({
526
+ drafts: Schema.Array(self.ref).check(Schema.isMinLength(1), Schema.isMaxLength(AGENT_DRAFT_NEWER_LIMIT)),
527
+ }),
528
+ connections: [],
529
+ prepare: ({ input }) => Effect.gen(function* () {
530
+ const delivery = yield* Effect.serviceOption(ChatDelivery);
531
+ const newer = Option.isSome(delivery) ? delivery.value.draftsNewer : undefined;
532
+ if (newer === undefined) {
533
+ return yield* new ActionRejected({
534
+ message: "Reading drafts' chats requires the authenticated ontology service.",
535
+ });
536
+ }
537
+ return { drafts: yield* newer(input.drafts.map(String)) };
538
+ }),
539
+ }),
373
540
  revise: action({
374
541
  description: "Ask the agent to redraft its draft reply, with what to change in note, in the " +
375
542
  "member's words. The agent writes it again in a session of its own, which reads the chat " +
package/dist/chat.d.ts CHANGED
@@ -210,6 +210,16 @@ export interface ReviewSendInput {
210
210
  readonly to?: ReadonlyArray<string>;
211
211
  /** A reply to mail: the addresses it copies, in place of the review's own. */
212
212
  readonly cc?: ReadonlyArray<string>;
213
+ /**
214
+ * Refuse the approval, before anything is reserved or sent, when the person wrote in the chat
215
+ * after the turn that wrote the reply, or when that cannot be told.
216
+ */
217
+ readonly unlessNewer?: boolean;
218
+ }
219
+ /** Whether the person wrote after a draft's turn; `null` when it cannot be told. */
220
+ export interface DraftNewer {
221
+ readonly draft: string;
222
+ readonly newer: boolean | null;
213
223
  }
214
224
  /** What sending an approved reply did; `duplicate` when an earlier approval already sent it. */
215
225
  export interface ReviewSendResult {
@@ -218,6 +228,8 @@ export interface ReviewSendResult {
218
228
  readonly text: string;
219
229
  readonly messageId: string | null;
220
230
  readonly duplicate: boolean;
231
+ /** The reply's message in the workspace chat store, by its id; absent before the store held it. */
232
+ readonly message?: string | null;
221
233
  /** A reply to mail: whom it was sent to, or attempted; absent on another channel. */
222
234
  readonly recipients?: {
223
235
  readonly to: ReadonlyArray<string>;
@@ -243,6 +255,8 @@ declare const ChatDelivery_base: Context.ServiceClass<ChatDelivery, "@segmentstr
243
255
  readonly send?: (input: ChatSendInput) => Effect.Effect<ChatPostResult, ActionError>;
244
256
  /** Sends a reply a member approved, as the agent and without attribution, at most once. */
245
257
  readonly sendReview?: (input: ReviewSendInput) => Effect.Effect<ReviewSendResult, ActionError>;
258
+ /** Whether the person wrote after each draft's turn, as `approve`'s `unlessNewer` tells it. */
259
+ readonly draftsNewer?: (drafts: ReadonlyArray<string>) => Effect.Effect<ReadonlyArray<DraftNewer>, ActionError>;
246
260
  /** Asks the agent to redraft a reply it held for review, in a session of its own. */
247
261
  readonly reviseReview?: (input: ReviewReviseInput) => Effect.Effect<ReviewReviseResult, ActionError>;
248
262
  /** Adds a chat's messages from before the agent recorded it, each in its place by its time. */
package/dist/errors.d.ts CHANGED
@@ -68,7 +68,10 @@ declare const ActionRejected_base: Schema.Class<ActionRejected, Schema.TaggedStr
68
68
  * Why a chat's provider refused a message, for code to act on: `outside_reply_window` (the person
69
69
  * last wrote longer ago than the channel lets a free-form message reach them; WhatsApp takes a
70
70
  * template), `recipient_unreachable` (the person blocked the sender or cannot receive it) or
71
- * `recipient_opted_out` (the person stopped such messages).
71
+ * `recipient_opted_out` (the person stopped such messages). A draft's decision says why it was
72
+ * refused too: `newer_message` (the person wrote after the turn that wrote it, refused by
73
+ * `unlessNewer`), `basis_unknown` (whether they did cannot be told) or `draft_decided` (it was
74
+ * decided, or an approval is delivering it).
72
75
  */
73
76
  readonly refusal: Schema.optionalKey<Schema.String>;
74
77
  }>, import("effect/Cause").YieldableError>;
package/dist/errors.js CHANGED
@@ -45,7 +45,10 @@ export class ActionRejected extends Schema.TaggedError()("ActionRejected", {
45
45
  * Why a chat's provider refused a message, for code to act on: `outside_reply_window` (the person
46
46
  * last wrote longer ago than the channel lets a free-form message reach them; WhatsApp takes a
47
47
  * template), `recipient_unreachable` (the person blocked the sender or cannot receive it) or
48
- * `recipient_opted_out` (the person stopped such messages).
48
+ * `recipient_opted_out` (the person stopped such messages). A draft's decision says why it was
49
+ * refused too: `newer_message` (the person wrote after the turn that wrote it, refused by
50
+ * `unlessNewer`), `basis_unknown` (whether they did cannot be told) or `draft_decided` (it was
51
+ * decided, or an approval is delivering it).
49
52
  */
50
53
  refusal: Schema.optionalKey(Schema.String),
51
54
  }) {
package/dist/index.d.ts CHANGED
@@ -59,14 +59,14 @@ export type { BatchOptions, BatchRecordResult, BatchResult, BatchReturning, Batc
59
59
  export { User } from "./user.js";
60
60
  export type { UserHandle, UserMemberWrites } from "./user.js";
61
61
  export { AgentChat } from "./agent-chat.js";
62
- export { AgentDraft, AGENT_DRAFT_MODES, AGENT_DRAFT_STATUSES, AGENT_DRAFT_TEXT_LIMIT, AgentReview, AGENT_REVIEW_MODES, AGENT_REVIEW_STATUSES, AGENT_REVIEW_TEXT_LIMIT, } from "./agent-draft.js";
62
+ export { AgentDraft, AGENT_DRAFT_FILES_LIMIT, AGENT_DRAFT_MODES, AGENT_DRAFT_NEWER_LIMIT, AGENT_DRAFT_STATUSES, AGENT_DRAFT_TEXT_LIMIT, AgentReview, AGENT_REVIEW_MODES, AGENT_REVIEW_STATUSES, AGENT_REVIEW_TEXT_LIMIT, draftFilesOf, type AgentDraftFile, } from "./agent-draft.js";
63
63
  export { ATTACHABLE_IDENTITY_PROVIDERS, UserIdentity, USER_IDENTITY_ORIGINS, } from "./user-identity.js";
64
64
  export type { AttachableIdentityProvider, UserIdentityOrigin } from "./user-identity.js";
65
65
  export { AppFeedback, AppFeedbackFile, APP_FEEDBACK_ENTITIES, APP_FEEDBACK_FILE_MAX_BYTES, } from "./app-feedback.js";
66
66
  export { ChatDelivery, ChatHistoryImportInput, ChatHistoryImportResult, ChatHistoryMessage, ChatPostInput, ChatSendInput, ChatTemplate, ChatTemplateMedia, ChatPostResult, } from "./chat.js";
67
67
  export { CHAT_CHANNEL_KINDS, CHAT_CHANNEL_NAME, CHAT_KINDS, WORKSPACE_CHANNEL_KIND, channelIdentityProvider, } from "./chat-channel.js";
68
68
  export type { ChatChannelKind, ChatKind } from "./chat-channel.js";
69
- export type { InboxOptions, ReviewReviseInput, ReviewReviseResult, ReviewSendInput, ReviewSendResult, } from "./chat.js";
69
+ export type { DraftNewer, InboxOptions, ReviewReviseInput, ReviewReviseResult, ReviewSendInput, ReviewSendResult, } from "./chat.js";
70
70
  export { DEFAULT_QUERY_LIMIT, entityQueryResultSchema, entityQuerySchema, entityQuerySchemas, entityRecordSchema, FILTER_OPERATORS, LIST_FILTER_OPERATORS, MANY_QUANTIFIERS, MAX_IN_VALUES, MAX_QUERY_LIMIT, OrderBy, Query, queryFields, queryJsonSchema, queryLinks, QueryResult, queryResultJsonSchema, queryResultSchema, SortDirection, WHERE_COMBINATORS, } from "./query.js";
71
71
  export type { EntityQuerySchemas, FieldFilter, FilterOperator, JsonRecord, ListFilterOperator, ManyFilter, QueryField, QueryLink, StorageType, Where, } from "./query.js";
72
72
  export { InvalidQuery, InvalidValues, MemberNotFound, PrincipalRejected, StoredRecordMismatch, UnknownAction, UnknownRole, ValidationIssue, UndeclaredWrite, WriteOutsideAction, } from "./runtime/errors.js";
package/dist/index.js CHANGED
@@ -34,9 +34,9 @@ export { EMAIL_MARKUP_VALUES, isEmailDocument, unsafeEmailHtml } from "./emails.
34
34
  export { OntologyActions, OntologyReads, OntologyRuntime, OntologyWrites } from "./runtime.js";
35
35
  export { User } from "./user.js";
36
36
  export { AgentChat } from "./agent-chat.js";
37
- export { AgentDraft, AGENT_DRAFT_MODES, AGENT_DRAFT_STATUSES, AGENT_DRAFT_TEXT_LIMIT,
37
+ export { AgentDraft, AGENT_DRAFT_FILES_LIMIT, AGENT_DRAFT_MODES, AGENT_DRAFT_NEWER_LIMIT, AGENT_DRAFT_STATUSES, AGENT_DRAFT_TEXT_LIMIT,
38
38
  // @legacy ontology.agent-review-entity-name
39
- AgentReview, AGENT_REVIEW_MODES, AGENT_REVIEW_STATUSES, AGENT_REVIEW_TEXT_LIMIT, } from "./agent-draft.js";
39
+ AgentReview, AGENT_REVIEW_MODES, AGENT_REVIEW_STATUSES, AGENT_REVIEW_TEXT_LIMIT, draftFilesOf, } from "./agent-draft.js";
40
40
  export { ATTACHABLE_IDENTITY_PROVIDERS, UserIdentity, USER_IDENTITY_ORIGINS, } from "./user-identity.js";
41
41
  export { AppFeedback, AppFeedbackFile, APP_FEEDBACK_ENTITIES, APP_FEEDBACK_FILE_MAX_BYTES, } from "./app-feedback.js";
42
42
  export { ChatDelivery, ChatHistoryImportInput, ChatHistoryImportResult, ChatHistoryMessage, ChatPostInput, ChatSendInput, ChatTemplate, ChatTemplateMedia, ChatPostResult, } from "./chat.js";