@segmentstream/ontology 0.110.0 → 0.111.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 (39) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +7 -1
  3. package/dist/agent-review.d.ts +2 -0
  4. package/dist/agent-review.js +11 -1
  5. package/dist/analyze.d.ts +34 -34
  6. package/dist/analyze.js +49 -26
  7. package/dist/chat-channel.d.ts +22 -0
  8. package/dist/chat-channel.js +35 -0
  9. package/dist/index.d.ts +3 -1
  10. package/dist/index.js +1 -0
  11. package/dist/manifest.d.ts +100 -24
  12. package/dist/manifest.js +13 -4
  13. package/dist/migrate/model.d.ts +44 -0
  14. package/dist/runtime/agents.js +3 -0
  15. package/dist/runtime-source.d.ts +4 -0
  16. package/dist/runtime-source.js +5 -0
  17. package/dist/runtime.d.ts +4 -1
  18. package/dist/service/agent/protocol.d.ts +1 -0
  19. package/dist/service/agent/protocol.js +5 -0
  20. package/dist/service/agent/server.js +11 -0
  21. package/dist/service/authentication.d.ts +4 -3
  22. package/dist/service/authentication.js +10 -5
  23. package/dist/service/chat/delivery.js +9 -5
  24. package/dist/service/chat/envelope.d.ts +6 -0
  25. package/dist/service/chat/providers/index.d.ts +19 -9
  26. package/dist/service/chat/providers/index.js +31 -11
  27. package/dist/service/chat/reads.d.ts +6 -3
  28. package/dist/service/chat/reads.js +26 -14
  29. package/dist/service/chat/resolve.d.ts +6 -2
  30. package/dist/service/chat/resolve.js +7 -3
  31. package/dist/service/chat/review-delivery.d.ts +2 -2
  32. package/dist/service/chat/review-delivery.js +16 -8
  33. package/dist/service/chat/routes.js +2 -1
  34. package/dist/service/chat/schemas.d.ts +2 -0
  35. package/dist/service/chat/schemas.js +8 -1
  36. package/dist/service/mcp/server.js +2 -1
  37. package/dist/service/runtime-contract.d.ts +9 -0
  38. package/dist/service/schemas.d.ts +8 -0
  39. package/package.json +1 -1
@@ -66,10 +66,13 @@ export function withChatMembership(transport, backend) {
66
66
  })), Effect.flatMap((memberGroups) => transport({ ...input, memberGroups })));
67
67
  }
68
68
  /**
69
- * Resolves the chats of `providers`: their group chats in the registry, and each member's private
70
- * chat on each provider they have a live identity on, `<provider>:private:<member id>`.
69
+ * Resolves the chats of `providers`, the agent's channels by name: their group chats in the
70
+ * registry, and each member's private chat on each channel whose kind they have a live identity
71
+ * on, `<channel>:private:<member id>`. People are not per channel: one identity of a kind reaches
72
+ * the member on every channel of that kind.
71
73
  */
72
74
  export function chatResolver(backend, principal, providers, options = {}) {
75
+ const kindOf = options.kindOf ?? ((channel) => channel);
73
76
  const canGroup = isAllowed("agent_chat:read", principal) || isAllowed("agent_chat:post_message", principal);
74
77
  const memberGroups = options.memberGroups === true && principal.kind === "member";
75
78
  const canDirectory = isAllowed("user:read", principal) || isAllowed("agent_chat:post_message", principal);
@@ -125,7 +128,8 @@ export function chatResolver(backend, principal, providers, options = {}) {
125
128
  }
126
129
  const identities = yield* backend.agents.listIdentities(member.principal.id);
127
130
  for (const provider of named === undefined ? providers : [named]) {
128
- const identity = identities.find((item) => item.provider === provider && item.revokedAt === null);
131
+ const kind = kindOf(provider);
132
+ const identity = identities.find((item) => kind !== undefined && item.provider === kind && item.revokedAt === null);
129
133
  if (identity !== undefined) {
130
134
  result.push({
131
135
  id: `${provider}:private:${member.principal.id}`,
@@ -9,8 +9,8 @@ import type { OntologyBackend } from "../runtime-contract.js";
9
9
  import type { DeliveryArchive } from "./archive.js";
10
10
  /**
11
11
  * Sends a reply the agent held for review, once a member approved it: the draft or the member's
12
- * edit, to the person's chat through the ontology's chat connection for its provider, as
13
- * the agent and without attribution. The archive's delivery claim, keyed by the review and not by
12
+ * edit, to the person's chat through the ontology's chat connection of the channel the chat is on,
13
+ * as the agent and without attribution. The archive's delivery claim, keyed by the review and not by
14
14
  * the member, makes it at most once: a replay of the same approval returns the delivery, and a
15
15
  * different approval, by another member or with another text, is refused. A provider that refused
16
16
  * it consumes the claim: the reply is never sent again.
@@ -4,11 +4,11 @@ import { ActionRejected, PermissionDenied } from "../../errors.js";
4
4
  import { effectiveScope } from "../../permissions.js";
5
5
  import { describeChatError } from "./diagnostics.js";
6
6
  import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./idempotency.js";
7
- import { chatConnection, chatProvider } from "./providers/index.js";
7
+ import { channelProvider, chatConnection, chatKind } from "./providers/index.js";
8
8
  /**
9
9
  * Sends a reply the agent held for review, once a member approved it: the draft or the member's
10
- * edit, to the person's chat through the ontology's chat connection for its provider, as
11
- * the agent and without attribution. The archive's delivery claim, keyed by the review and not by
10
+ * edit, to the person's chat through the ontology's chat connection of the channel the chat is on,
11
+ * as the agent and without attribution. The archive's delivery claim, keyed by the review and not by
12
12
  * the member, makes it at most once: a replay of the same approval returns the delivery, and a
13
13
  * different approval, by another member or with another text, is refused. A provider that refused
14
14
  * it consumes the claim: the reply is never sent again.
@@ -62,13 +62,21 @@ export function reviewSender(input) {
62
62
  : "The reply was already approved; it is never sent again.",
63
63
  });
64
64
  }
65
- const providerName = String(review.provider);
66
- const provider = chatProvider(providerName);
65
+ // The channel the chat is on, by name. A review held before channels had names has none:
66
+ // it is on the channel named after its kind.
67
+ // @legacy ontology.reviews-without-channel
68
+ const providerName = typeof review.channel === "string" && review.channel !== ""
69
+ ? review.channel
70
+ : String(review.provider);
71
+ const provider = channelProvider(input.ontology.manifest.chat, providerName);
67
72
  const connection = chatConnection(input.ontology.manifest.chat, providerName);
68
- if (provider === undefined || !connection) {
73
+ if (provider === undefined ||
74
+ !connection ||
75
+ chatKind(input.ontology.manifest.chat, providerName) !== String(review.provider)) {
69
76
  return yield* new ActionRejected({
70
- message: `Sending a reviewed reply needs chat.${providerName}.connection in defineOntology: ` +
71
- "the connection of the bot or number the person talks to.",
77
+ message: `Sending a reviewed reply needs chat.${providerName}.connection in defineOntology, ` +
78
+ `of kind ${String(review.provider)}: the connection of the bot, number or mailbox ` +
79
+ "the person talks to.",
72
80
  });
73
81
  }
74
82
  const kind = review.chat_kind === "group" ? "group" : "private";
@@ -8,7 +8,7 @@ import { RequestId, failureResponse } from "../errors.js";
8
8
  import { OntologyBackend } from "../runtime-contract.js";
9
9
  import { identityToken, makeOperations } from "../operations.js";
10
10
  import { ChatSearch } from "../agent/chat-archive.js";
11
- import { listedChatProviders } from "./providers/index.js";
11
+ import { chatKind, listedChatProviders } from "./providers/index.js";
12
12
  import { chatReads } from "./reads.js";
13
13
  import { withChatMembership } from "./resolve.js";
14
14
  import { ChatList, ChatHistory } from "./schemas.js";
@@ -19,6 +19,7 @@ export function chatRoutes(options) {
19
19
  const chatHistory = options.chatHistory && withChatMembership(options.chatHistory, backend);
20
20
  const reads = chatReads(backend, {
21
21
  providers: listedChatProviders(options.built.plan.ontology.manifest.chat),
22
+ kindOf: (channel) => chatKind(options.built.plan.ontology.manifest.chat, channel),
22
23
  chatHistory,
23
24
  attribution: options.built.plan.ontology.config.chat?.attribution,
24
25
  });
@@ -28,6 +28,7 @@ export declare const ChatDestination: Schema.Struct<{
28
28
  readonly id: Schema.String;
29
29
  readonly title: Schema.String;
30
30
  readonly provider: Schema.String;
31
+ readonly kind: Schema.optionalKey<Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>>;
31
32
  readonly external_id: Schema.String;
32
33
  readonly attribution: Schema.NullOr<Schema.String>;
33
34
  }>;
@@ -36,6 +37,7 @@ export declare const ChatListResult: Schema.Struct<{
36
37
  readonly id: Schema.String;
37
38
  readonly title: Schema.String;
38
39
  readonly provider: Schema.String;
40
+ readonly kind: Schema.optionalKey<Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>>;
39
41
  readonly external_id: Schema.String;
40
42
  readonly attribution: Schema.NullOr<Schema.String>;
41
43
  }>>;
@@ -1,5 +1,6 @@
1
1
  import { Schema } from "effect";
2
2
  import { ChatPostInput } from "../../chat.js";
3
+ import { CHAT_CHANNEL_KINDS } from "../../chat-channel.js";
3
4
  const Id = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512));
4
5
  export const ChatPostWire = ChatPostInput.pipe(Schema.encodeKeys({ replyTo: "reply_to", idempotencyKey: "idempotency_key" }));
5
6
  export const RuntimeChatPostWire = Schema.Struct({
@@ -21,7 +22,13 @@ export const ChatHistory = Schema.Struct({
21
22
  export const ChatDestination = Schema.Struct({
22
23
  id: Id,
23
24
  title: Schema.String,
24
- provider: Schema.String,
25
+ provider: Schema.String.annotate({
26
+ description: "The agent's channel the chat is on, by its name: its kind for the channel named after " +
27
+ "its kind, such as telegram.",
28
+ }),
29
+ kind: Schema.optionalKey(Schema.Literals(CHAT_CHANNEL_KINDS).annotate({
30
+ description: "The kind of that channel, which decides how a message to the chat renders.",
31
+ })),
25
32
  external_id: Id,
26
33
  attribution: Schema.NullOr(Schema.String),
27
34
  });
@@ -14,7 +14,7 @@ import { CONFIRMATION_PREVIEW_METHOD, previewLocale, withoutConfirmation } from
14
14
  import { consumeChatEnvelope, CurrentChatCall } from "../chat/envelope.js";
15
15
  import { ChatList, ChatHistory, ChatPostWire, RuntimeChatPostWire } from "../chat/schemas.js";
16
16
  import { ChatPostResult } from "../../chat.js";
17
- import { listedChatProviders } from "../chat/providers/index.js";
17
+ import { chatKind, listedChatProviders } from "../chat/providers/index.js";
18
18
  import { chatReads } from "../chat/reads.js";
19
19
  import { withChatMembership } from "../chat/resolve.js";
20
20
  import { catalogFor } from "../catalog.js";
@@ -850,6 +850,7 @@ export function mcpLayer(options) {
850
850
  const server = {
851
851
  chats: chatReads(backend, {
852
852
  providers: listedChatProviders(options.built.plan.ontology.manifest.chat),
853
+ kindOf: (channel) => chatKind(options.built.plan.ontology.manifest.chat, channel),
853
854
  chatHistory,
854
855
  attribution: options.built.plan.ontology.config.chat?.attribution,
855
856
  }),
@@ -599,6 +599,12 @@ export interface AgentStore {
599
599
  readonly member: (userId: string) => Effect.Effect<AgentMember | undefined>;
600
600
  /** The member a runtime caller acts for: active, and with a live identity. */
601
601
  readonly actingMember: (userId: string, provider?: string) => Effect.Effect<Principal | undefined>;
602
+ /**
603
+ * The kind of the agent's channel `name`, as the ontology's `chat` declares it: the name itself
604
+ * for the channel named after its kind, such as `telegram`; `undefined` for another name it does
605
+ * not declare. Without it, every channel is taken for the one named after its kind.
606
+ */
607
+ readonly channelKind?: (name: string) => string | undefined;
602
608
  /**
603
609
  * Records a message the agent saw in a group chat: creates the `agent_chat` or keeps its title
604
610
  * current, records when the chat last had a message (at most every 15 minutes, so that history
@@ -630,7 +636,10 @@ export interface AgentStore {
630
636
  /** A reply the agent held for review, as `POST /api/ontology/agent/reviews` reports it. */
631
637
  export interface HeldReplyInput {
632
638
  readonly key: string;
639
+ /** The kind of channel the chat is on, such as whatsapp. */
633
640
  readonly provider: string;
641
+ /** The channel the chat is on, by name: its kind for the channel named after its kind. */
642
+ readonly channel: string;
634
643
  readonly chatId: string;
635
644
  /** Whom the provider sends the reply to: the chat's id, or the person's id where it differs. */
636
645
  readonly recipient: string;
@@ -525,6 +525,10 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
525
525
  readonly email: Schema.optionalKey<Schema.Struct<{
526
526
  readonly connection: Schema.String;
527
527
  }>>;
528
+ readonly channels: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Struct<{
529
+ readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>;
530
+ readonly connection: Schema.String;
531
+ }>>>;
528
532
  readonly attribution: Schema.optionalKey<Schema.Struct<{
529
533
  readonly template: Schema.optionalKey<Schema.String>;
530
534
  readonly separator: Schema.optionalKey<Schema.String>;
@@ -828,6 +832,10 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
828
832
  readonly email: Schema.optionalKey<Schema.Struct<{
829
833
  readonly connection: Schema.String;
830
834
  }>>;
835
+ readonly channels: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Struct<{
836
+ readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>;
837
+ readonly connection: Schema.String;
838
+ }>>>;
831
839
  readonly attribution: Schema.optionalKey<Schema.Struct<{
832
840
  readonly template: Schema.optionalKey<Schema.String>;
833
841
  readonly separator: Schema.optionalKey<Schema.String>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.110.0",
3
+ "version": "0.111.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",