@segmentstream/ontology 0.156.1 → 0.159.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.
Files changed (67) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +12 -9
  3. package/dist/agent-chat.d.ts +1 -1
  4. package/dist/agent-chat.js +8 -8
  5. package/dist/agent-draft.d.ts +287 -0
  6. package/dist/{agent-review.js → agent-draft.js} +103 -82
  7. package/dist/analyze.d.ts +1 -1
  8. package/dist/analyze.js +71 -14
  9. package/dist/channel-files.d.ts +70 -0
  10. package/dist/channel-files.js +58 -0
  11. package/dist/chat.d.ts +2 -2
  12. package/dist/earlier-entity-names.d.ts +35 -0
  13. package/dist/earlier-entity-names.js +88 -0
  14. package/dist/email.d.ts +2 -0
  15. package/dist/email.js +1 -0
  16. package/dist/gmail-send.d.ts +39 -0
  17. package/dist/gmail-send.js +179 -0
  18. package/dist/index.d.ts +1 -1
  19. package/dist/index.js +3 -1
  20. package/dist/migrate/apply.d.ts +3 -2
  21. package/dist/migrate/apply.js +11 -4
  22. package/dist/migrate/plan.js +42 -8
  23. package/dist/migrate/preview.js +4 -1
  24. package/dist/permissions.d.ts +5 -5
  25. package/dist/permissions.js +13 -10
  26. package/dist/replicated-entities.d.ts +1 -1
  27. package/dist/replicated-entities.js +4 -1
  28. package/dist/replication/bigquery.d.ts +24 -2
  29. package/dist/replication/bigquery.js +72 -12
  30. package/dist/runtime/agents.d.ts +1 -1
  31. package/dist/runtime/agents.js +43 -38
  32. package/dist/runtime/chat-messages.d.ts +2 -2
  33. package/dist/runtime/chat-messages.js +1 -1
  34. package/dist/runtime/service-binding.js +1 -1
  35. package/dist/service/agent/protocol.d.ts +4 -4
  36. package/dist/service/agent/protocol.js +7 -7
  37. package/dist/service/agent/server.js +14 -9
  38. package/dist/service/chat/envelope.d.ts +12 -12
  39. package/dist/service/chat/review-delivery.js +23 -22
  40. package/dist/service/chat/review-revision.d.ts +1 -1
  41. package/dist/service/chat/review-revision.js +13 -13
  42. package/dist/service/mcp/runtime-policy.js +1 -1
  43. package/dist/service/mcp/server.js +9 -1
  44. package/dist/service/mcp/tools.js +1 -1
  45. package/dist/service/operations.js +11 -5
  46. package/dist/service/runtime-contract.d.ts +3 -3
  47. package/dist/service/service.d.ts +6 -0
  48. package/dist/service/service.js +21 -3
  49. package/dist/slack-files.d.ts +53 -0
  50. package/dist/slack-files.js +183 -0
  51. package/dist/slack.d.ts +3 -0
  52. package/dist/slack.js +2 -0
  53. package/dist/storage/attachments.js +15 -16
  54. package/dist/storage/tables.d.ts +18 -11
  55. package/dist/storage/tables.js +87 -12
  56. package/dist/telegram-files.d.ts +81 -0
  57. package/dist/telegram-files.js +244 -0
  58. package/dist/telegram.d.ts +3 -1
  59. package/dist/telegram.js +2 -0
  60. package/dist/whatsapp-files.d.ts +54 -0
  61. package/dist/whatsapp-files.js +138 -0
  62. package/dist/whatsapp.d.ts +3 -0
  63. package/dist/whatsapp.js +2 -0
  64. package/dist/workspace-channel.d.ts +20 -0
  65. package/dist/workspace-channel.js +10 -0
  66. package/package.json +1 -1
  67. package/dist/agent-review.d.ts +0 -154
@@ -5,7 +5,7 @@ import { SqlClient } from "effect/unstable/sql";
5
5
  import { action } from "../action.js";
6
6
  import { AgentChat, agentChatKey } from "../agent-chat.js";
7
7
  import { chatChannelKind } from "../chat-channel.js";
8
- import { addressesOfReview, AgentReview, participantsOfReview, reviewAddresses, reviewParticipants, } from "../agent-review.js";
8
+ import { addressesOfDraft, AgentDraft, participantsOfDraft, draftAddresses, draftParticipants, } from "../agent-draft.js";
9
9
  import { AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, CHANNEL_USERS_TABLE, IDENTITY_NOTICES_TABLE, USER_TABLE, } from "../migrate/storage.js";
10
10
  import { inboxChannelsOf, inboxRolesOf, takeoverChannelsOf, takeoverProvidersOf, } from "../manifest.js";
11
11
  import { GUEST_ROLE } from "../names.js";
@@ -105,9 +105,9 @@ function identityOf(row) {
105
105
  };
106
106
  }
107
107
  /**
108
- * The action a held reply's record is written by, `agent_review.hold`. Only the agent holds replies,
108
+ * The action a held reply's record is written by, `agent_draft.hold`. Only the agent holds replies,
109
109
  * through the reviews route, so it is no action of the entity anyone can call: action triggers on
110
- * `agent_review` itself fire for it, with its input and the review as the result.
110
+ * `agent_draft` itself fire for it, with its input and the review as the result.
111
111
  */
112
112
  const HOLD_ACTION = "hold";
113
113
  /** The most waiting, and the most decided, held replies of one session its next turn learns about. */
@@ -122,7 +122,7 @@ const HOLD_DEFINITION = action({
122
122
  * `now()`; a pairing is consumed with one `UPDATE … WHERE "consumed_at" IS NULL`, so two concurrent
123
123
  * approvals cannot both succeed. Ids that are not UUIDs find nothing rather than fail the cast.
124
124
  * Chats the agent sees are `agent_chat` records, written as the platform with history. Holding a
125
- * reply is admitted, bounded and starts the workflows whose triggers on `agent_review` fire for it as
125
+ * reply is admitted, bounded and starts the workflows whose triggers on `agent_draft` fire for it as
126
126
  * an action is and does, with the `ActionTransactions` and `WorkflowStarter` it runs with.
127
127
  */
128
128
  export function sqlAgentStore(ontology, sql) {
@@ -130,13 +130,13 @@ export function sqlAgentStore(ontology, sql) {
130
130
  const memberPrincipal = (userId) => resolveMemberById(ontology, userId).pipe(Effect.provideService(SqlClient.SqlClient, sql));
131
131
  /**
132
132
  * Holds a reply for review once per key, in a transaction bounded and admitted as an action's,
133
- * and starts the workflows whose triggers on `agent_review` fire for it. `resolve` runs in the
133
+ * and starts the workflows whose triggers on `agent_draft` fire for it. `resolve` runs in the
134
134
  * transaction, after the key is locked and found unused: it names the reply, or the reply a redraft
135
135
  * replaces, which it marks superseded, or `discarded` when there is nothing to replace.
136
136
  */
137
137
  const hold = (key, memberId, resolve) => Effect.gen(function* () {
138
- // The triggers on `agent_review` itself, which fire for any of its actions: holding is one.
139
- const triggers = actionTriggers(ontology).filter((trigger) => trigger.entity === AgentReview.name &&
138
+ // The triggers on `agent_draft` itself, which fire for any of its actions: holding is one.
139
+ const triggers = actionTriggers(ontology).filter((trigger) => trigger.entity === AgentDraft.name &&
140
140
  (trigger.action === undefined || trigger.action === HOLD_ACTION));
141
141
  const starter = triggers.length > 0
142
142
  ? Option.getOrUndefined(yield* Effect.serviceOption(WorkflowStarter))
@@ -144,7 +144,7 @@ export function sqlAgentStore(ontology, sql) {
144
144
  // Admitted and bounded as an action's transaction is, by the same gate and limits.
145
145
  const admission = Option.getOrUndefined(yield* Effect.serviceOption(ActionTransactions));
146
146
  const limits = admission?.limits ?? ACTION_TRANSACTION_LIMITS;
147
- const qualified = `${AgentReview.name}.${HOLD_ACTION}`;
147
+ const qualified = `${AgentDraft.name}.${HOLD_ACTION}`;
148
148
  const actionId = randomUUID();
149
149
  if (memberId !== undefined && !isRecordId(memberId)) {
150
150
  return undefined;
@@ -169,7 +169,7 @@ export function sqlAgentStore(ontology, sql) {
169
169
  ? {
170
170
  onEdit: (edit) => edits.push(edit),
171
171
  onSnapshot: (snapshot) => {
172
- if (snapshot.entity === AgentReview.name) {
172
+ if (snapshot.entity === AgentDraft.name) {
173
173
  snapshots.set(snapshot.id, {
174
174
  before: snapshots.get(snapshot.id)?.before ?? snapshot.before,
175
175
  after: snapshot.after,
@@ -180,7 +180,7 @@ export function sqlAgentStore(ontology, sql) {
180
180
  : {}),
181
181
  });
182
182
  const [found] = yield* runtime.find({
183
- entity: AgentReview.name,
183
+ entity: AgentDraft.name,
184
184
  options: { where: { key }, limit: 1 },
185
185
  });
186
186
  if (found !== undefined) {
@@ -214,24 +214,24 @@ export function sqlAgentStore(ontology, sql) {
214
214
  },
215
215
  });
216
216
  const review = yield* runtime.create({
217
- entity: AgentReview.name,
217
+ entity: AgentDraft.name,
218
218
  values: {
219
219
  key: input.key,
220
- provider: input.provider,
220
+ channel_kind: input.provider,
221
221
  channel: input.channel,
222
222
  chat_id: input.chatId,
223
223
  recipient: input.recipient,
224
224
  // A reply to mail names whom it goes to: its person, unless the agent said more.
225
225
  ...(input.provider === "email"
226
226
  ? {
227
- to: reviewAddresses(input.to ?? [input.recipient]),
228
- cc: (input.cc ?? []).length === 0 ? null : reviewAddresses(input.cc ?? []),
227
+ to: draftAddresses(input.to ?? [input.recipient]),
228
+ cc: (input.cc ?? []).length === 0 ? null : draftAddresses(input.cc ?? []),
229
229
  }
230
230
  : {}),
231
231
  // A reply in a chat several people share is read by the members the agent named.
232
232
  participants: (input.participants ?? []).length === 0
233
233
  ? null
234
- : reviewParticipants(input.participants ?? []),
234
+ : draftParticipants(input.participants ?? []),
235
235
  chat_kind: chat?.kind === "group" || chat?.kind === "private" ? chat.kind : kindOf(input),
236
236
  thread_id: input.threadId === "" ? null : input.threadId,
237
237
  reply_to: input.replyTo,
@@ -251,7 +251,7 @@ export function sqlAgentStore(ontology, sql) {
251
251
  });
252
252
  if (resolved.kind === "revision") {
253
253
  yield* plain.update({
254
- entity: AgentReview.name,
254
+ entity: AgentDraft.name,
255
255
  id: String(resolved.previous.id),
256
256
  values: {
257
257
  status: "superseded",
@@ -261,7 +261,12 @@ export function sqlAgentStore(ontology, sql) {
261
261
  },
262
262
  });
263
263
  }
264
- const result = { review: String(review.id), status: "pending" };
264
+ const result = {
265
+ draft: String(review.id),
266
+ // @legacy ontology.agent-review-entity-name
267
+ review: String(review.id),
268
+ status: "pending",
269
+ };
265
270
  let starts = [];
266
271
  if (triggers.length > 0) {
267
272
  const principal = yield* memberPrincipal(input.memberId);
@@ -269,7 +274,7 @@ export function sqlAgentStore(ontology, sql) {
269
274
  if (principal !== undefined) {
270
275
  starts = yield* evaluateTriggers(triggers, {
271
276
  ontology,
272
- entity: AgentReview.name,
277
+ entity: AgentDraft.name,
273
278
  action: HOLD_ACTION,
274
279
  definition: HOLD_DEFINITION,
275
280
  generated: false,
@@ -280,12 +285,12 @@ export function sqlAgentStore(ontology, sql) {
280
285
  result,
281
286
  edits,
282
287
  held: snapshots,
283
- load: (id) => runtime.get({ entity: AgentReview.name, id }).pipe(Effect.map((record) => record), Effect.catchTag("NotFound", () => Effect.succeed(undefined))),
288
+ load: (id) => runtime.get({ entity: AgentDraft.name, id }).pipe(Effect.map((record) => record), Effect.catchTag("NotFound", () => Effect.succeed(undefined))),
284
289
  });
285
290
  yield* insertStarts(sql, starts);
286
291
  }
287
292
  }
288
- return { review: { id: result.review, status: "pending", created: true }, starts };
293
+ return { review: { id: result.draft, status: "pending", created: true }, starts };
289
294
  }), {
290
295
  qualified,
291
296
  actionId,
@@ -897,7 +902,7 @@ export function sqlAgentStore(ontology, sql) {
897
902
  // The reply the redraft replaces, while it still waits for this redraft: a member who
898
903
  // decided it, or asked for another redraft since, leaves this one nothing to replace.
899
904
  const [previous] = yield* runtime.find({
900
- entity: AgentReview.name,
905
+ entity: AgentDraft.name,
901
906
  options: { where: { id: input.review }, limit: 1, forUpdate: true },
902
907
  });
903
908
  if (previous === undefined ||
@@ -911,18 +916,18 @@ export function sqlAgentStore(ontology, sql) {
911
916
  previous,
912
917
  reply: {
913
918
  key: `revision:${input.revision}`,
914
- provider: String(previous.provider),
915
- channel: nullableText(previous.channel) ?? String(previous.provider),
919
+ provider: String(previous.channel_kind),
920
+ channel: nullableText(previous.channel) ?? String(previous.channel_kind),
916
921
  chatId: String(previous.chat_id),
917
922
  recipient: String(previous.recipient),
918
923
  // A redraft goes to the people the reply it replaces goes to, and is read by the
919
924
  // members who read that one.
920
- ...(addressesOfReview(previous.to).length === 0
925
+ ...(addressesOfDraft(previous.to).length === 0
921
926
  ? {}
922
- : { to: addressesOfReview(previous.to), cc: addressesOfReview(previous.cc) }),
923
- ...(participantsOfReview(previous.participants).length === 0
927
+ : { to: addressesOfDraft(previous.to), cc: addressesOfDraft(previous.cc) }),
928
+ ...(participantsOfDraft(previous.participants).length === 0
924
929
  ? {}
925
- : { participants: participantsOfReview(previous.participants) }),
930
+ : { participants: participantsOfDraft(previous.participants) }),
926
931
  threadId: nullableText(previous.thread_id) ?? "",
927
932
  replyTo: nullableText(previous.reply_to),
928
933
  memberId: String(previous.person_id),
@@ -940,19 +945,19 @@ export function sqlAgentStore(ontology, sql) {
940
945
  }
941
946
  yield* beginReplicationWrite(sql);
942
947
  const runtime = makeOntologyRuntime(ontology, sql, {
943
- action: `${AgentReview.name}.revise`,
948
+ action: `${AgentDraft.name}.revise`,
944
949
  actionId: randomUUID(),
945
950
  principalId: input.notedBy,
946
951
  });
947
952
  const [review] = yield* runtime.find({
948
- entity: AgentReview.name,
953
+ entity: AgentDraft.name,
949
954
  options: { where: { id: input.review }, limit: 1, forUpdate: true },
950
955
  });
951
956
  if (review === undefined || review.status !== "pending") {
952
957
  return false;
953
958
  }
954
959
  yield* runtime.update({
955
- entity: AgentReview.name,
960
+ entity: AgentDraft.name,
956
961
  id: String(review.id),
957
962
  values: {
958
963
  note: input.note,
@@ -971,12 +976,12 @@ export function sqlAgentStore(ontology, sql) {
971
976
  }
972
977
  yield* beginReplicationWrite(sql);
973
978
  const runtime = makeOntologyRuntime(ontology, sql, {
974
- action: `${AgentReview.name}.approve`,
979
+ action: `${AgentDraft.name}.approve`,
975
980
  actionId: randomUUID(),
976
981
  principalId: null,
977
982
  });
978
983
  const [review] = yield* runtime.find({
979
- entity: AgentReview.name,
984
+ entity: AgentDraft.name,
980
985
  options: { where: { id: input.review }, limit: 1, forUpdate: true },
981
986
  });
982
987
  if (review === undefined || review.status !== "pending") {
@@ -984,7 +989,7 @@ export function sqlAgentStore(ontology, sql) {
984
989
  }
985
990
  if (review.revision !== null && review.revision !== undefined) {
986
991
  yield* runtime.update({
987
- entity: AgentReview.name,
992
+ entity: AgentDraft.name,
988
993
  id: String(review.id),
989
994
  values: { revision: null, revision_state: null },
990
995
  });
@@ -999,12 +1004,12 @@ export function sqlAgentStore(ontology, sql) {
999
1004
  }
1000
1005
  yield* beginReplicationWrite(sql);
1001
1006
  const runtime = makeOntologyRuntime(ontology, sql, {
1002
- action: `${AgentReview.name}.${HOLD_ACTION}`,
1007
+ action: `${AgentDraft.name}.${HOLD_ACTION}`,
1003
1008
  actionId: randomUUID(),
1004
1009
  principalId: null,
1005
1010
  });
1006
1011
  const [previous] = yield* runtime.find({
1007
- entity: AgentReview.name,
1012
+ entity: AgentDraft.name,
1008
1013
  options: { where: { id: input.review }, limit: 1, forUpdate: true },
1009
1014
  });
1010
1015
  if (previous === undefined ||
@@ -1014,7 +1019,7 @@ export function sqlAgentStore(ontology, sql) {
1014
1019
  return false;
1015
1020
  }
1016
1021
  yield* runtime.update({
1017
- entity: AgentReview.name,
1022
+ entity: AgentDraft.name,
1018
1023
  id: String(previous.id),
1019
1024
  values: { revision_state: "failed" },
1020
1025
  });
@@ -1027,7 +1032,7 @@ export function sqlAgentStore(ontology, sql) {
1027
1032
  // reply reaches the session as the newest one.
1028
1033
  const [waiting, decided] = yield* Effect.all([
1029
1034
  runtime.find({
1030
- entity: AgentReview.name,
1035
+ entity: AgentDraft.name,
1031
1036
  options: {
1032
1037
  where: { session_id: sessionId, status: "pending" },
1033
1038
  orderBy: [{ field: "created_at", direction: "desc" }],
@@ -1035,7 +1040,7 @@ export function sqlAgentStore(ontology, sql) {
1035
1040
  },
1036
1041
  }),
1037
1042
  runtime.find({
1038
- entity: AgentReview.name,
1043
+ entity: AgentDraft.name,
1039
1044
  options: {
1040
1045
  where: { session_id: sessionId, status: { ne: "pending" } },
1041
1046
  orderBy: [{ field: "decided_at", direction: "desc" }],
@@ -27,7 +27,7 @@ export declare const CHAT_CLAIM_MINUTES = 10;
27
27
  /** The most rows one purge batch deletes or scrubs, and the most batches one purge runs. */
28
28
  export declare const CHAT_PURGE_BATCH = 1000;
29
29
  export declare const CHAT_PURGE_BATCHES = 20;
30
- /** A stored file of a message, by its `_agent_attachments` row. */
30
+ /** A stored file of a message, by its `_chat_attachments` row. */
31
31
  export interface ChatMessageFile {
32
32
  readonly id: string;
33
33
  readonly name?: string;
@@ -173,7 +173,7 @@ export type ChatMessageChange =
173
173
  * What a channel read of the message after recording it, with its routing state, in one write
174
174
  * that applies only while the message holds the `meta` it replaces (`null` for none): its `text`
175
175
  * with what was transcribed of it, such as a voice message's transcript after its caption, and
176
- * its stored files, by their `_agent_attachments` rows. Never an edit: `edited_at` stays.
176
+ * its stored files, by their `_chat_attachments` rows. Never an edit: `edited_at` stays.
177
177
  */
178
178
  | (Target & {
179
179
  readonly change: "content";
@@ -283,7 +283,7 @@ export function sqlChatMessageStore(sql) {
283
283
  const [storage] = yield* rows(`SELECT to_regclass('"${ATTACHMENTS_TABLE}"') IS NOT NULL AS "present"`);
284
284
  if (storage?.present === true || storage?.present === "t") {
285
285
  yield* rows(`UPDATE "${ATTACHMENTS_TABLE}" SET "state" = 'discarded', "expires_at" = now() ` +
286
- `WHERE "id"::text = ANY($1::text[]) AND "conversation" = $2 ` +
286
+ `WHERE "id"::text = ANY($1::text[]) AND "chat" = $2 ` +
287
287
  `AND "state" IN ('received', 'ready')`, [ids, `${channel}:${chatId}`]);
288
288
  }
289
289
  });
@@ -258,7 +258,7 @@ export function backendLayer(ontology, blobStorage, workflowStarter, deployment,
258
258
  resolveIdentity: (input) => agents
259
259
  .resolveIdentity(input)
260
260
  .pipe(withStarter, Effect.provideService(ActionTransactions, actionTransactions)),
261
- // Holding a reply starts the workflows triggered on `agent_review`, as an action would.
261
+ // Holding a reply starts the workflows triggered on `agent_draft`, as an action would.
262
262
  ...(agents.holdReply === undefined
263
263
  ? {}
264
264
  : {
@@ -139,8 +139,8 @@ export declare const ChatHandoffRequest: Schema.Struct<{
139
139
  readonly reason: Schema.String;
140
140
  }>;
141
141
  /**
142
- * `POST /api/ontology/agent/reviews`: a reply the agent held for a member to review, for the member
143
- * it answers. `key` names the reply, so a report repeated after a failure finds its review.
142
+ * `POST /api/ontology/agent/drafts`: a reply the agent kept for a member to review, for the member
143
+ * it answers. `key` names the reply, so a report repeated after a failure finds its draft.
144
144
  */
145
145
  export declare const ReviewRequest: Schema.Struct<{
146
146
  readonly key: Schema.String;
@@ -161,7 +161,7 @@ export declare const ReviewRequest: Schema.Struct<{
161
161
  }>;
162
162
  export type ReviewRequest = typeof ReviewRequest.Type;
163
163
  /**
164
- * `POST /api/ontology/agent/reviews/revision`: what became of a redraft a member asked for: the new
164
+ * `POST /api/ontology/agent/drafts/revision`: what became of a redraft a member asked for: the new
165
165
  * draft, or, without `text`, that the agent could not write one.
166
166
  */
167
167
  export declare const RevisionRequest: Schema.Struct<{
@@ -170,7 +170,7 @@ export declare const RevisionRequest: Schema.Struct<{
170
170
  readonly text: Schema.optionalKey<Schema.String>;
171
171
  }>;
172
172
  /**
173
- * `POST /api/ontology/agent/reviews/session`: the replies an agent session held for review, so its
173
+ * `POST /api/ontology/agent/drafts/session`: the drafts an agent session kept for review, so its
174
174
  * next turn learns which the person saw.
175
175
  */
176
176
  export declare const SessionReviewsRequest: Schema.Struct<{
@@ -1,6 +1,6 @@
1
1
  import { randomInt } from "node:crypto";
2
2
  import { Schema } from "effect";
3
- import { AGENT_REVIEW_PARTICIPANTS_LIMIT } from "../../agent-review.js";
3
+ import { AGENT_DRAFT_PARTICIPANTS_LIMIT } from "../../agent-draft.js";
4
4
  import { ChatChannelName } from "../../chat-channel.js";
5
5
  import { EMAIL_RECIPIENTS_LIMIT } from "../../email.js";
6
6
  import { EMAIL_PATTERN } from "../../property.js";
@@ -183,8 +183,8 @@ export const ChatHandoffRequest = Schema.Struct({
183
183
  }),
184
184
  });
185
185
  /**
186
- * `POST /api/ontology/agent/reviews`: a reply the agent held for a member to review, for the member
187
- * it answers. `key` names the reply, so a report repeated after a failure finds its review.
186
+ * `POST /api/ontology/agent/drafts`: a reply the agent kept for a member to review, for the member
187
+ * it answers. `key` names the reply, so a report repeated after a failure finds its draft.
188
188
  */
189
189
  export const ReviewRequest = Schema.Struct({
190
190
  key: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512)).annotate({
@@ -208,7 +208,7 @@ export const ReviewRequest = Schema.Struct({
208
208
  description: "A reply to mail: the addresses it copies, in Cc, the thread's other people.",
209
209
  })),
210
210
  participants: Schema.optionalKey(Schema.Array(Schema.String.check(Schema.isUUID()))
211
- .check(Schema.isMaxLength(AGENT_REVIEW_PARTICIPANTS_LIMIT))
211
+ .check(Schema.isMaxLength(AGENT_DRAFT_PARTICIPANTS_LIMIT))
212
212
  .annotate({
213
213
  description: "A reply in a chat several people share, such as an email thread: the members who " +
214
214
  "read it, by user id, as they read the message it answers; its member alone when absent.",
@@ -234,12 +234,12 @@ export const ReviewRequest = Schema.Struct({
234
234
  }),
235
235
  });
236
236
  /**
237
- * `POST /api/ontology/agent/reviews/revision`: what became of a redraft a member asked for: the new
237
+ * `POST /api/ontology/agent/drafts/revision`: what became of a redraft a member asked for: the new
238
238
  * draft, or, without `text`, that the agent could not write one.
239
239
  */
240
240
  export const RevisionRequest = Schema.Struct({
241
241
  review_id: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(64)).annotate({
242
- description: "The review the redraft replaces.",
242
+ description: "The draft the redraft replaces.",
243
243
  }),
244
244
  revision: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(64)).annotate({
245
245
  description: "The redraft, as the member's request named it.",
@@ -249,7 +249,7 @@ export const RevisionRequest = Schema.Struct({
249
249
  }),
250
250
  });
251
251
  /**
252
- * `POST /api/ontology/agent/reviews/session`: the replies an agent session held for review, so its
252
+ * `POST /api/ontology/agent/drafts/session`: the drafts an agent session kept for review, so its
253
253
  * next turn learns which the person saw.
254
254
  */
255
255
  export const SessionReviewsRequest = Schema.Struct({
@@ -25,6 +25,9 @@ const PAIRINGS_PATH = `${AGENT_PATH}/pairings`;
25
25
  const IDENTITIES_PATH = `${AGENT_PATH}/identities`;
26
26
  const CHATS_SEEN_PATH = `${AGENT_PATH}/chats/seen`;
27
27
  const CHATS_HANDOFF_PATH = `${AGENT_PATH}/chats/handoff`;
28
+ const DRAFTS_PATH = `${AGENT_PATH}/drafts`;
29
+ // @legacy agent.review-runtime-paths
30
+ // The drafts' routes under their earlier name, which agent SDKs released before them call.
28
31
  const REVIEWS_PATH = `${AGENT_PATH}/reviews`;
29
32
  /** Where a signed-in member gets an agent token, and where the agent asks whom one stands for. */
30
33
  export const AGENT_TOKENS_PATH = `${AGENT_PATH}/tokens`;
@@ -346,9 +349,9 @@ function seeChat(store) {
346
349
  });
347
350
  }
348
351
  /**
349
- * `POST /api/ontology/agent/reviews` (runtime): the agent held a reply for review. It becomes a
350
- * pending `agent_review` for the member it answers, once per key; 404 when that member is not an
351
- * active member, so the reply is neither sent nor held.
352
+ * `POST /api/ontology/agent/drafts` (runtime): the agent kept a reply for review. It becomes a
353
+ * pending `agent_draft` for the member it answers, once per key; 404 when that member is not an
354
+ * active member, so the reply is neither sent nor kept.
352
355
  */
353
356
  function holdReply(store) {
354
357
  return (request) => Effect.gen(function* () {
@@ -418,8 +421,8 @@ function holdReply(store) {
418
421
  });
419
422
  }
420
423
  /**
421
- * `POST /api/ontology/agent/reviews/revision` (runtime): the agent redrafted a reply a member sent
422
- * back, or could not. The new draft becomes a pending review for the same person and chat that
424
+ * `POST /api/ontology/agent/drafts/revision` (runtime): the agent redrafted a reply a member sent
425
+ * back, or could not. The new draft becomes a pending draft for the same person and chat that
423
426
  * replaces the earlier one, which becomes `superseded`, once per redraft; a redraft the member no
424
427
  * longer waits for, because the reply was decided or redrafted again since, replaces nothing.
425
428
  */
@@ -465,7 +468,7 @@ function revision(store) {
465
468
  });
466
469
  }
467
470
  /**
468
- * `POST /api/ontology/agent/reviews/session` (runtime): the replies the session held for review,
471
+ * `POST /api/ontology/agent/drafts/session` (runtime): the drafts the session kept for review,
469
472
  * newest first, with what became of each. Nothing a member wrote about them is in it.
470
473
  */
471
474
  function sessionReviews(store) {
@@ -618,9 +621,11 @@ export function agentLayer(options) {
618
621
  yield* router.add("GET", `${AGENT_PATH}${CHAT_MESSAGE_PATHS.purge}`, purge);
619
622
  yield* router.add("POST", `${AGENT_PATH}${CHAT_MESSAGE_PATHS.purge}`, purge);
620
623
  }
621
- yield* router.add("POST", REVIEWS_PATH, asRuntime(holdReply(store)));
622
- yield* router.add("POST", `${REVIEWS_PATH}/session`, asRuntime(sessionReviews(store)));
623
- yield* router.add("POST", `${REVIEWS_PATH}/revision`, asRuntime(revision(store)));
624
+ for (const path of [DRAFTS_PATH, REVIEWS_PATH]) {
625
+ yield* router.add("POST", path, asRuntime(holdReply(store)));
626
+ yield* router.add("POST", `${path}/session`, asRuntime(sessionReviews(store)));
627
+ yield* router.add("POST", `${path}/revision`, asRuntime(revision(store)));
628
+ }
624
629
  yield* router.add("DELETE", `${IDENTITIES_PATH}/:id`, asMember((_, member) => revokeIdentity(store, member)));
625
630
  if (store.web !== undefined && backend.oauth !== undefined) {
626
631
  yield* router.add("POST", AGENT_TOKENS_PATH, asMember(issueAgentToken(store, store.web, fixedWindowLimiter(AGENT_TOKEN_LIMIT))));
@@ -54,6 +54,12 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
54
54
  readonly thread_id: string;
55
55
  readonly message_id?: string | undefined;
56
56
  readonly channel?: string | undefined;
57
+ readonly location?: {
58
+ readonly latitude: number;
59
+ readonly longitude: number;
60
+ readonly horizontal_accuracy?: number | undefined;
61
+ readonly live_period?: number | undefined;
62
+ } | undefined;
57
63
  readonly attachments?: readonly {
58
64
  readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
59
65
  readonly file_id: string;
@@ -64,12 +70,6 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
64
70
  readonly reply_to_message_id?: string | undefined;
65
71
  readonly callback_query_id?: string | undefined;
66
72
  readonly sender?: string | undefined;
67
- readonly location?: {
68
- readonly latitude: number;
69
- readonly longitude: number;
70
- readonly horizontal_accuracy?: number | undefined;
71
- readonly live_period?: number | undefined;
72
- } | undefined;
73
73
  } | {
74
74
  readonly kind: "private" | "group";
75
75
  readonly provider: "slack" | "whatsapp";
@@ -108,6 +108,12 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
108
108
  readonly thread_id: string;
109
109
  readonly message_id?: string | undefined;
110
110
  readonly channel?: string | undefined;
111
+ readonly location?: {
112
+ readonly latitude: number;
113
+ readonly longitude: number;
114
+ readonly horizontal_accuracy?: number | undefined;
115
+ readonly live_period?: number | undefined;
116
+ } | undefined;
111
117
  readonly attachments?: readonly {
112
118
  readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
113
119
  readonly file_id: string;
@@ -118,12 +124,6 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
118
124
  readonly reply_to_message_id?: string | undefined;
119
125
  readonly callback_query_id?: string | undefined;
120
126
  readonly sender?: string | undefined;
121
- readonly location?: {
122
- readonly latitude: number;
123
- readonly longitude: number;
124
- readonly horizontal_accuracy?: number | undefined;
125
- readonly live_period?: number | undefined;
126
- } | undefined;
127
127
  } | {
128
128
  readonly kind: "private" | "group";
129
129
  readonly provider: "slack" | "whatsapp";
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { addressesOfReview, AGENT_REVIEW_TEXT_LIMIT, participantsOfReview, } from "../../agent-review.js";
2
+ import { addressesOfDraft, AGENT_DRAFT_TEXT_LIMIT, participantsOfDraft, } from "../../agent-draft.js";
3
3
  import { EMAIL_RECIPIENTS_LIMIT, emailRecipients } from "../../email.js";
4
4
  import { ActionRejected, PermissionDenied } from "../../errors.js";
5
5
  import { effectiveScope } from "../../permissions.js";
@@ -14,10 +14,10 @@ import { deliveryArchiveOf } from "./store-delivery.js";
14
14
  * recipient, and its `cc`, whatever their order and case.
15
15
  */
16
16
  function sameRecipients(recipients, review) {
17
- const held = addressesOfReview(review.to);
17
+ const held = addressesOfDraft(review.to);
18
18
  const set = (list) => [...new Set(list.map((address) => address.toLowerCase()))].sort().join(",");
19
19
  return (set(recipients.to) ===
20
- set(held.length > 0 ? held : typeof review.recipient === "string" ? [review.recipient] : []) && set(recipients.cc) === set(addressesOfReview(review.cc)));
20
+ set(held.length > 0 ? held : typeof review.recipient === "string" ? [review.recipient] : []) && set(recipients.cc) === set(addressesOfDraft(review.cc)));
21
21
  }
22
22
  /**
23
23
  * Sends a reply the agent held for review, once a member approved it: the draft or the member's
@@ -31,7 +31,7 @@ function sameRecipients(recipients, review) {
31
31
  */
32
32
  export function reviewSender(input) {
33
33
  const { backend, principal } = input;
34
- const report = (phase, cause) => Effect.logWarning("ontology.agent_review.delivery_diagnostic").pipe(Effect.annotateLogs({
34
+ const report = (phase, cause) => Effect.logWarning("ontology.agent_draft.delivery_diagnostic").pipe(Effect.annotateLogs({
35
35
  phase,
36
36
  requestId: input.requestId,
37
37
  error: describeChatError(cause, (text) => input.identityToken ? text.split(input.identityToken).join("[redacted]") : text),
@@ -40,30 +40,30 @@ export function reviewSender(input) {
40
40
  // A role may hold the approval within a scope. The send happens before the action's
41
41
  // transaction, whose write guard comes too late for it: a review outside the scope is
42
42
  // refused here, read through the approval's own scope.
43
- const approvable = effectiveScope("agent_review:approve", principal);
43
+ const approvable = effectiveScope("agent_draft:approve", principal);
44
44
  if (approvable !== undefined) {
45
45
  yield* backend
46
46
  .get({
47
- entity: "agent_review",
47
+ entity: "agent_draft",
48
48
  id: send.review,
49
49
  reader: {
50
50
  ...principal,
51
- permissions: ["agent_review:read"],
52
- scopes: [{ permission: "agent_review:read", where: approvable }],
51
+ permissions: ["agent_draft:read"],
52
+ scopes: [{ permission: "agent_draft:read", where: approvable }],
53
53
  },
54
54
  })
55
55
  .pipe(Effect.mapError(() => new PermissionDenied({
56
- message: "The review is outside the records agent_review:approve covers.",
57
- permission: "agent_review:approve",
56
+ message: "The draft is outside the records agent_draft:approve covers.",
57
+ permission: "agent_draft:approve",
58
58
  })));
59
59
  }
60
60
  const review = yield* backend
61
- .get({ entity: "agent_review", id: send.review })
62
- .pipe(Effect.mapError(() => new ActionRejected({ message: "The review does not exist." })));
61
+ .get({ entity: "agent_draft", id: send.review })
62
+ .pipe(Effect.mapError(() => new ActionRejected({ message: "The draft does not exist." })));
63
63
  const draft = String(review.draft ?? "");
64
64
  const text = send.text ?? draft;
65
65
  const status = String(review.status);
66
- const email = String(review.provider) === "email";
66
+ const email = String(review.channel_kind) === "email";
67
67
  // Only a reply to mail names whom it goes to beside its chat.
68
68
  if (!email && (send.to !== undefined || send.cc !== undefined)) {
69
69
  return yield* new ActionRejected({
@@ -72,7 +72,7 @@ export function reviewSender(input) {
72
72
  }
73
73
  // A reply to mail goes to the review's `to` and `cc`, the thread's people, unless the member
74
74
  // names others; one held before replies named them goes to its recipient alone.
75
- const held = addressesOfReview(review.to);
75
+ const held = addressesOfDraft(review.to);
76
76
  const recipients = email
77
77
  ? yield* Effect.try({
78
78
  try: () => emailRecipients({
@@ -82,7 +82,7 @@ export function reviewSender(input) {
82
82
  : typeof review.recipient === "string"
83
83
  ? [review.recipient]
84
84
  : []),
85
- cc: send.cc ?? addressesOfReview(review.cc),
85
+ cc: send.cc ?? addressesOfDraft(review.cc),
86
86
  }),
87
87
  catch: (cause) => new ActionRejected({
88
88
  message: `A reply to mail goes to 1 to ${EMAIL_RECIPIENTS_LIMIT} plain addresses ` +
@@ -114,17 +114,17 @@ export function reviewSender(input) {
114
114
  // @legacy ontology.reviews-without-channel
115
115
  const providerName = typeof review.channel === "string" && review.channel !== ""
116
116
  ? review.channel
117
- : String(review.provider);
117
+ : String(review.channel_kind);
118
118
  const provider = channelProvider(input.ontology.manifest.chat, providerName);
119
119
  const connection = chatConnection(input.ontology.manifest.chat, providerName) ?? "";
120
120
  const channelKind = chatKind(input.ontology.manifest.chat, providerName);
121
121
  // A workspace channel needs no connection of the ontology's: the agent sends through it.
122
122
  if (provider === undefined ||
123
123
  (!connection && channelKind !== WORKSPACE_CHANNEL_KIND) ||
124
- channelKind !== String(review.provider)) {
124
+ channelKind !== String(review.channel_kind)) {
125
125
  return yield* new ActionRejected({
126
126
  message: `Sending a reviewed reply needs chat.${providerName}.connection in defineOntology, ` +
127
- `of kind ${String(review.provider)}: the connection of the bot, number or mailbox ` +
127
+ `of kind ${String(review.channel_kind)}: the connection of the bot, number or mailbox ` +
128
128
  "the person talks to.",
129
129
  });
130
130
  }
@@ -147,7 +147,7 @@ export function reviewSender(input) {
147
147
  catch: (cause) => new ActionRejected({ message: "The Markdown could not be rendered.", cause }),
148
148
  });
149
149
  if (text.trim() === "" ||
150
- text.length > AGENT_REVIEW_TEXT_LIMIT ||
150
+ text.length > AGENT_DRAFT_TEXT_LIMIT ||
151
151
  finalText.length > provider.limit ||
152
152
  plainText.length > provider.limit ||
153
153
  !provider.isTargetId(externalId, kind) ||
@@ -158,7 +158,7 @@ export function reviewSender(input) {
158
158
  "sent or truncated.",
159
159
  });
160
160
  }
161
- // A guest is no user; `agent_review` is not theirs to approve.
161
+ // A guest is no user; `agent_draft` is not theirs to approve.
162
162
  if (principal.kind === "guest") {
163
163
  return yield* new PermissionDenied({ message: "The approver is unknown." });
164
164
  }
@@ -191,7 +191,7 @@ export function reviewSender(input) {
191
191
  participantIds: [
192
192
  ...new Set([
193
193
  ...(person === null ? [] : [person]),
194
- ...participantsOfReview(review.participants),
194
+ ...participantsOfDraft(review.participants),
195
195
  ]),
196
196
  ],
197
197
  };
@@ -208,6 +208,7 @@ export function reviewSender(input) {
208
208
  const operationId = String(review.id);
209
209
  const scope = deliveryScopeDigest({
210
210
  via: "service",
211
+ // The delivery keys keep the entity's first name: a renamed key would send an approval again.
211
212
  subjectId: `agent_review:${operationId}`,
212
213
  clientId: null,
213
214
  });
@@ -319,7 +320,7 @@ export function reviewSender(input) {
319
320
  transport: input.connections({
320
321
  identityToken: input.identityToken,
321
322
  audit: {
322
- action: "agent_review.approve",
323
+ action: "agent_draft.approve",
323
324
  principalId: principal.id,
324
325
  requestId: input.requestId,
325
326
  },