@segmentstream/ontology 0.117.0 → 0.118.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,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.118.0
4
+
5
+ A reply the agent holds for review is linked to its chat: `agent_review` gains `agentChat`, the
6
+ chat's `agent_chat` record when the registry has it (a group the agent is in, or a person's private
7
+ chat in the inbox), whose inverse on `agent_chat` is `reviews`. `agent_chat` `find` with
8
+ `where: { reviews: { some: { status: "pending" } } }` lists the chats where replies wait for staff.
9
+ `chatKind` now comes from that record, or from the chat's id, so a reply held in a Slack channel is
10
+ a group's and its approval reaches the channel. A workspace relationship whose inverse on
11
+ `agent_chat` is `reviews` must be renamed.
12
+
3
13
  ## 0.117.0
4
14
 
5
15
  Staff reply in the inbox: `agent_chat.postMessage({ chat: <record id>, text, idempotencyKey })` to a
@@ -53,6 +53,7 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_review", {
53
53
  }, {
54
54
  readonly person: import("./relationship.js").OneRelationship<false, false, "user">;
55
55
  readonly decidedBy: import("./relationship.js").OneRelationship<true, false, "user">;
56
+ readonly agentChat: import("./relationship.js").OneRelationship<true, false, "agent_chat">;
56
57
  }, import("./entity.js").InvocableActions<{
57
58
  readonly approve: import("./action.js").Action<Schema.Struct<{
58
59
  readonly review: Schema.Codec<string, string, never, never>;
@@ -93,5 +94,6 @@ export declare const AgentReview: import("./entity.js").Entity<"agent_review", {
93
94
  readonly decidedAt: string | null;
94
95
  readonly personId: string;
95
96
  readonly decidedById: string | null;
97
+ readonly agentChatId: string | null;
96
98
  }>, Readonly<Record<never, never>>, "actions">;
97
99
  export type AgentReview = typeof AgentReview.Type;
@@ -134,6 +134,14 @@ export const AgentReview = entity("agent_review", {
134
134
  phrase: ["was decided by", "decided"],
135
135
  description: "The member who approved or rejected the reply.",
136
136
  }),
137
+ agentChat: rel.one("agent_chat", {
138
+ optional: true,
139
+ inverse: "reviews",
140
+ onDelete: "setNull",
141
+ phrase: ["was held in", "holds"],
142
+ description: "The chat the reply was held in, as the agent_chat registry has it: a group the agent " +
143
+ "is in, or a person's private chat in the inbox. Empty when the chat is not registered.",
144
+ }),
137
145
  },
138
146
  indexes: [{ properties: ["status"] }],
139
147
  }).actions((self) => ({
@@ -56,6 +56,17 @@ function messageDue(lastMessageAt, now) {
56
56
  const last = typeof lastMessageAt === "string" ? Date.parse(lastMessageAt) : Number.NaN;
57
57
  return Number.isNaN(last) || now.getTime() - last >= CHAT_MESSAGE_RECORD_INTERVAL_MS;
58
58
  }
59
+ /**
60
+ * Whether a held reply's chat is a group, by its id, for a chat the registry does not have:
61
+ * Telegram's groups and channels have negative ids, and Slack's channels and group messages start
62
+ * with C or G. Every other chat with the agent is a person's own.
63
+ */
64
+ function kindOf(input) {
65
+ return (input.provider === "telegram" && input.chatId.startsWith("-")) ||
66
+ (input.provider === "slack" && /^[CG]/u.test(input.chatId))
67
+ ? "group"
68
+ : "private";
69
+ }
59
70
  function nullableText(value) {
60
71
  return value === null || value === undefined ? null : String(value);
61
72
  }
@@ -561,6 +572,14 @@ export function sqlAgentStore(ontology, sql) {
561
572
  if (member === undefined) {
562
573
  return undefined;
563
574
  }
575
+ // The registry keys a chat by its channel's name and its id, as the archive does.
576
+ const [chat] = yield* runtime.find({
577
+ entity: AgentChat.name,
578
+ options: {
579
+ where: { provider: input.channel, external_id: input.chatId },
580
+ limit: 1,
581
+ },
582
+ });
564
583
  const review = yield* runtime.create({
565
584
  entity: AgentReview.name,
566
585
  values: {
@@ -569,10 +588,7 @@ export function sqlAgentStore(ontology, sql) {
569
588
  channel: input.channel,
570
589
  chat_id: input.chatId,
571
590
  recipient: input.recipient,
572
- // Telegram's groups and channels have negative ids; WhatsApp chats are private.
573
- chat_kind: input.provider === "telegram" && input.chatId.startsWith("-")
574
- ? "group"
575
- : "private",
591
+ chat_kind: chat?.kind === "group" || chat?.kind === "private" ? chat.kind : kindOf(input),
576
592
  thread_id: input.threadId === "" ? null : input.threadId,
577
593
  reply_to: input.replyTo,
578
594
  session_id: input.sessionId,
@@ -585,6 +601,7 @@ export function sqlAgentStore(ontology, sql) {
585
601
  message_id: null,
586
602
  decided_at: null,
587
603
  person: input.memberId,
604
+ agent_chat: chat === undefined ? null : String(chat.id),
588
605
  },
589
606
  });
590
607
  return { id: String(review.id), status: "pending", created: true };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.117.0",
3
+ "version": "0.118.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",