@segmentstream/ontology 0.140.0 → 0.142.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,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.142.0
4
+
5
+ Every `agent_chat` record has a unique `key`, `<provider>:<externalId>`, such as
6
+ `telegram:-100123` or `web:guest:<id>`, as the chat archive names the chat. The migration that adds
7
+ it keys the chats already recorded, and the agent keys every chat it records from then on. A
8
+ warehouse-backed entity relates to `AgentChat` on it, such as a model of analytics events a
9
+ website tagged with the chat's key: `rel.one(AgentChat, { inverse: "visits", on: { chat: "key" } })`.
10
+
11
+ ## 0.141.0
12
+
13
+ `chat_history` reads a Slack channel's history, every thread of it, as the archive now keeps a
14
+ Slack channel's threads under the channel; it refused Slack channels before. A Slack private
15
+ destination is still refused: read a direct message by the chat the inbox registered for it.
16
+
3
17
  ## 0.140.0
4
18
 
5
19
  Stored ontology properties accept `excludeFromReplication: true`. Deploy this option to omit
@@ -1,10 +1,16 @@
1
1
  import { Schema } from "effect";
2
2
  export declare const TELEGRAM = "telegram";
3
+ /**
4
+ * A chat's key: its channel and its id there, `<channel>:<id>`, as the chat archive and the agent's
5
+ * requests name the chat, such as `telegram:-100123` or `web:guest:<id>`.
6
+ */
7
+ export declare function agentChatKey(provider: string, externalId: string): string;
3
8
  /** Existing chat registry and membership storage; delivery lives in the ontology service. */
4
9
  export declare const AgentChat: import("./entity.js").Entity<"agent_chat", {
5
10
  readonly provider: import("./property.js").Property<string, false, false>;
6
11
  readonly kind: import("./property.js").Property<"private" | "group", true, false>;
7
12
  readonly externalId: import("./property.js").Property<string, false, false>;
13
+ readonly key: Schema.optionalKey<import("./property.js").Property<string, false, true>>;
8
14
  readonly title: import("./property.js").Property<string, false, false>;
9
15
  readonly recipient: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
10
16
  readonly threadId: Schema.optionalKey<import("./property.js").Property<string, false, false>>;
@@ -108,6 +114,7 @@ export declare const AgentChat: import("./entity.js").Entity<"agent_chat", {
108
114
  readonly provider: string;
109
115
  readonly kind: "private" | "group";
110
116
  readonly externalId: string;
117
+ readonly key: string | null;
111
118
  readonly title: string;
112
119
  readonly recipient: string | null;
113
120
  readonly threadId: string | null;
@@ -6,6 +6,13 @@ import { ChatDelivery, ChatHistoryImportInput, ChatPostInput, ChatSendInput } fr
6
6
  import { p } from "./property.js";
7
7
  import { rel } from "./relationship.js";
8
8
  export const TELEGRAM = "telegram";
9
+ /**
10
+ * A chat's key: its channel and its id there, `<channel>:<id>`, as the chat archive and the agent's
11
+ * requests name the chat, such as `telegram:-100123` or `web:guest:<id>`.
12
+ */
13
+ export function agentChatKey(provider, externalId) {
14
+ return `${provider}:${externalId}`;
15
+ }
9
16
  /** Existing chat registry and membership storage; delivery lives in the ontology service. */
10
17
  export const AgentChat = entity("agent_chat", {
11
18
  id: "ent_agent_chat",
@@ -27,6 +34,16 @@ export const AgentChat = entity("agent_chat", {
27
34
  description: "Whether the chat is a group chat or a person's private chat with the agent.",
28
35
  }),
29
36
  externalId: p.text({ managed: true, description: "The chat's id at the provider." }),
37
+ // Optional only for the records a deploy before it holds, which the migration that adds it
38
+ // keys: every record has one.
39
+ key: p.text({
40
+ optional: true,
41
+ unique: true,
42
+ managed: true,
43
+ description: "The chat's key, `<provider>:<externalId>`, such as `web:guest:<id>` or " +
44
+ "`telegram:-100123`, as the chat archive names the chat. Analytics events and other " +
45
+ "records tag a chat with it, so a warehouse-backed entity relates to the chat on it.",
46
+ }),
30
47
  title: p.text({
31
48
  managed: true,
32
49
  description: "The chat's name at the provider, kept current by the agent: a private chat's is its " +
@@ -297,6 +297,19 @@ export function planMigration(input) {
297
297
  ...retainedField(retainedAfter(renamed)),
298
298
  };
299
299
  }
300
+ /**
301
+ * What a property the platform adds to one of its built-in entities holds in the records the entity
302
+ * already has: the agent chat's `key` is its channel and its id there, as the runtime writes it for
303
+ * the chats it records from then on.
304
+ */
305
+ function builtInValues(entity, property) {
306
+ return entity === "agent_chat" && property === "key"
307
+ ? [
308
+ `UPDATE ${quoteIdentifier(tableName(entity))} SET "key" = "provider" || ':' || ` +
309
+ `"external_id" WHERE "key" IS NULL`,
310
+ ]
311
+ : [];
312
+ }
300
313
  /**
301
314
  * A deploy that drops a browser sign-in method is refused while an active member would lose access:
302
315
  * every established email member needs a SegmentStream identity to drop email, and every member with
@@ -1774,6 +1787,7 @@ function planEntityChanges(input) {
1774
1787
  sql: [
1775
1788
  ...(enumStorage === undefined ? [] : [createEnumStatement(enumStorage)]),
1776
1789
  alterTable(table, actions.join(", ")),
1790
+ ...builtInValues(entity, property.name),
1777
1791
  ],
1778
1792
  }));
1779
1793
  }
@@ -3,7 +3,7 @@ import { randomUUID } from "node:crypto";
3
3
  import { Effect, Option, Schema } from "effect";
4
4
  import { SqlClient } from "effect/unstable/sql";
5
5
  import { action } from "../action.js";
6
- import { AgentChat } from "../agent-chat.js";
6
+ import { AgentChat, agentChatKey } from "../agent-chat.js";
7
7
  import { chatChannelKind } from "../chat-channel.js";
8
8
  import { AgentReview } from "../agent-review.js";
9
9
  import { AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, CHANNEL_USERS_TABLE, IDENTITY_NOTICES_TABLE, USER_TABLE, } from "../migrate/storage.js";
@@ -560,7 +560,7 @@ export function sqlAgentStore(ontology, sql) {
560
560
  found = yield* runtime.update({
561
561
  entity: AgentChat.name,
562
562
  id: String(old.id),
563
- values: { external_id: externalId },
563
+ values: { external_id: externalId, key: agentChatKey(provider, externalId) },
564
564
  });
565
565
  }
566
566
  else if (old !== undefined && found !== undefined) {
@@ -592,7 +592,12 @@ export function sqlAgentStore(ontology, sql) {
592
592
  const chat = found === undefined
593
593
  ? yield* runtime.create({
594
594
  entity: AgentChat.name,
595
- values: { provider, external_id: externalId, ...changes },
595
+ values: {
596
+ provider,
597
+ external_id: externalId,
598
+ key: agentChatKey(provider, externalId),
599
+ ...changes,
600
+ },
596
601
  })
597
602
  : Object.keys(changes).length === 0
598
603
  ? found
@@ -688,6 +693,7 @@ export function sqlAgentStore(ontology, sql) {
688
693
  values: {
689
694
  provider: input.provider,
690
695
  external_id: input.externalId,
696
+ key: agentChatKey(input.provider, input.externalId),
691
697
  kind: "private",
692
698
  ...changes,
693
699
  },
@@ -828,6 +834,7 @@ export function sqlAgentStore(ontology, sql) {
828
834
  values: {
829
835
  provider: WEB_PROVIDER,
830
836
  external_id: externalId,
837
+ key: agentChatKey(WEB_PROVIDER, externalId),
831
838
  kind: "private",
832
839
  title: externalId,
833
840
  last_message_at: now.toISOString(),
@@ -6,8 +6,11 @@ import { CurrentPrincipal } from "../authentication.js";
6
6
  import { currentCaller } from "../caller-context.js";
7
7
  import { chatAttribution } from "./attribution.js";
8
8
  import { chatResolver, groupChatKey, isChatMember } from "./resolve.js";
9
- /** The kinds of channel whose archived chats are keyed by the chat a destination names. */
10
- const HISTORY_KINDS = new Set(["telegram"]);
9
+ /**
10
+ * The kinds of channel whose archived group chats are keyed by the chat a destination names: a
11
+ * Telegram chat, and a Slack channel, whose threads the archive keeps under it.
12
+ */
13
+ const HISTORY_KINDS = new Set(["telegram", "slack"]);
11
14
  const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
12
15
  /**
13
16
  * A private chat the inbox registered, by its record id, when the caller may read it: a member with
@@ -107,13 +110,20 @@ export function chatReads(backend, options) {
107
110
  if (principal.kind !== "member") {
108
111
  return yield* new PermissionDenied({ message: "Only members may read chatHistory." });
109
112
  }
110
- // History reads the chats of Telegram channels only. Other channels archive a chat under
111
- // their session's address with private coordinates, not under the chat a destination
112
- // names, so their history would be incomplete until the archive keys them the same way.
113
+ // History reads Telegram's chats and Slack's channels, which the archive keeps under the
114
+ // chat a destination names. Other channels' chats are read by the record the inbox
115
+ // registered for them, above.
113
116
  const target = yield* chatResolver(backend, principal, history, {
114
117
  memberGroups: true,
115
118
  kindOf,
116
119
  }).resolve(input.chat);
120
+ // A Slack private destination names its person, while the archive keeps their direct
121
+ // message under its own channel id.
122
+ if (target.kind !== "group" && kindOf(target.provider) !== "telegram") {
123
+ return yield* new ActionRejected({
124
+ message: "Read a direct message by the chat the inbox registered for it.",
125
+ });
126
+ }
117
127
  // A group's members read its history, as they read its attachments; agent_chat:read reads
118
128
  // every group. A private chat is read only by its participant.
119
129
  const member = target.kind === "group" &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.140.0",
3
+ "version": "0.142.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",