@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
package/dist/manifest.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { CHAT_CHANNEL_KINDS, CHAT_CHANNEL_NAME } from "./chat-channel.js";
1
2
  import { balancesOf, metricExpression } from "./metric.js";
2
3
  import { Effect, Schema, SchemaTransformation } from "effect";
3
4
  import { acceptingEarlierManifestNames, withEarlierManifestNames } from "./legacy.js";
@@ -77,6 +78,9 @@ import { CHANNEL_PROVIDERS, DEFAULT_SIGN_IN_POLICY, isGuestBotCheckScheme, isGue
77
78
  * guest passes and the workspace secret its verification reads, and `confirmations: true`, written
78
79
  * only when declared. Older readers ignore both: they ask new guests for no challenge, and keep
79
80
  * refusing guests the actions that need a confirmation.
81
+ * Within version 15, `chat` may carry `channels`, written only when declared: the agent's channels
82
+ * not named after their kind, such as `sales_whatsapp`, each with its `kind` and connection. Older
83
+ * readers ignore it and send nothing through those channels.
80
84
  */
81
85
  export const MANIFEST_VERSION = 15;
82
86
  /** The first version whose documents use snake_case names on the wire. */
@@ -495,16 +499,21 @@ const OntologyManifestModel = Schema.Struct({
495
499
  /** The connections the actions call. Written only when there are some; absent reads as none. */
496
500
  connections: Schema.optionalKey(Schema.Array(ConnectionManifest)),
497
501
  /**
498
- * The providers whose chats the built-in exact chat post delivers to, each through its declared
499
- * connection, and the attribution it adds. Absent, or without a provider, delivers nowhere.
500
- * `email`, within version 15, is the mailbox reviewed replies to mail leave from; no chat post
501
- * goes to it.
502
+ * The agent's channels whose chats the built-in exact chat post delivers to, each through its
503
+ * declared connection, and the attribution it adds. Absent, or without a channel, delivers
504
+ * nowhere. The channel named after each kind has its key; `channels` holds the others by name,
505
+ * with their kinds. `email`, within version 15, is the mailbox reviewed replies to mail leave
506
+ * from; no chat post goes to it, nor to another channel of its kind.
502
507
  */
503
508
  chat: Schema.optionalKey(Schema.Struct({
504
509
  telegram: Schema.optionalKey(Schema.Struct({ connection: Schema.String })),
505
510
  slack: Schema.optionalKey(Schema.Struct({ connection: Schema.String })),
506
511
  whatsapp: Schema.optionalKey(Schema.Struct({ connection: Schema.String })),
507
512
  email: Schema.optionalKey(Schema.Struct({ connection: Schema.String })),
513
+ channels: Schema.optionalKey(Schema.Record(Schema.String.check(Schema.isPattern(CHAT_CHANNEL_NAME)), Schema.Struct({
514
+ kind: Schema.Literals(CHAT_CHANNEL_KINDS),
515
+ connection: Schema.String,
516
+ }))),
508
517
  attribution: Schema.optionalKey(Schema.Struct({
509
518
  template: Schema.optionalKey(Schema.String),
510
519
  separator: Schema.optionalKey(Schema.String),
@@ -409,6 +409,12 @@ export declare const MigrationPlan: Schema.Struct<{
409
409
  readonly slack?: {
410
410
  readonly connection: string;
411
411
  } | undefined;
412
+ readonly channels?: {
413
+ readonly [x: string]: {
414
+ readonly kind: "telegram" | "email" | "whatsapp" | "slack";
415
+ readonly connection: string;
416
+ };
417
+ } | undefined;
412
418
  readonly attribution?: {
413
419
  readonly template?: string | undefined;
414
420
  readonly separator?: string | undefined;
@@ -705,6 +711,12 @@ export declare const MigrationPlan: Schema.Struct<{
705
711
  readonly slack?: {
706
712
  readonly connection: string;
707
713
  } | undefined;
714
+ readonly channels?: {
715
+ readonly [x: string]: {
716
+ readonly kind: "telegram" | "email" | "whatsapp" | "slack";
717
+ readonly connection: string;
718
+ };
719
+ } | undefined;
708
720
  readonly attribution?: {
709
721
  readonly template?: string | undefined;
710
722
  readonly separator?: string | undefined;
@@ -1001,6 +1013,12 @@ export declare const MigrationPlan: Schema.Struct<{
1001
1013
  readonly slack?: {
1002
1014
  readonly connection: string;
1003
1015
  } | undefined;
1016
+ readonly channels?: {
1017
+ readonly [x: string]: {
1018
+ readonly kind: "telegram" | "email" | "whatsapp" | "slack";
1019
+ readonly connection: string;
1020
+ };
1021
+ } | undefined;
1004
1022
  readonly attribution?: {
1005
1023
  readonly template?: string | undefined;
1006
1024
  readonly separator?: string | undefined;
@@ -1297,6 +1315,12 @@ export declare const MigrationPlan: Schema.Struct<{
1297
1315
  readonly slack?: {
1298
1316
  readonly connection: string;
1299
1317
  } | undefined;
1318
+ readonly channels?: {
1319
+ readonly [x: string]: {
1320
+ readonly kind: "telegram" | "email" | "whatsapp" | "slack";
1321
+ readonly connection: string;
1322
+ };
1323
+ } | undefined;
1300
1324
  readonly attribution?: {
1301
1325
  readonly template?: string | undefined;
1302
1326
  readonly separator?: string | undefined;
@@ -1689,6 +1713,10 @@ export declare const MigrationPlan: Schema.Struct<{
1689
1713
  readonly email: Schema.optionalKey<Schema.Struct<{
1690
1714
  readonly connection: Schema.String;
1691
1715
  }>>;
1716
+ readonly channels: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Struct<{
1717
+ readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>;
1718
+ readonly connection: Schema.String;
1719
+ }>>>;
1692
1720
  readonly attribution: Schema.optionalKey<Schema.Struct<{
1693
1721
  readonly template: Schema.optionalKey<Schema.String>;
1694
1722
  readonly separator: Schema.optionalKey<Schema.String>;
@@ -1979,6 +2007,10 @@ export declare const MigrationPlan: Schema.Struct<{
1979
2007
  readonly email: Schema.optionalKey<Schema.Struct<{
1980
2008
  readonly connection: Schema.String;
1981
2009
  }>>;
2010
+ readonly channels: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Struct<{
2011
+ readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>;
2012
+ readonly connection: Schema.String;
2013
+ }>>>;
1982
2014
  readonly attribution: Schema.optionalKey<Schema.Struct<{
1983
2015
  readonly template: Schema.optionalKey<Schema.String>;
1984
2016
  readonly separator: Schema.optionalKey<Schema.String>;
@@ -2186,6 +2218,12 @@ export declare const MigrationPlan: Schema.Struct<{
2186
2218
  readonly slack?: {
2187
2219
  readonly connection: string;
2188
2220
  } | undefined;
2221
+ readonly channels?: {
2222
+ readonly [x: string]: {
2223
+ readonly kind: "telegram" | "email" | "whatsapp" | "slack";
2224
+ readonly connection: string;
2225
+ };
2226
+ } | undefined;
2189
2227
  readonly attribution?: {
2190
2228
  readonly template?: string | undefined;
2191
2229
  readonly separator?: string | undefined;
@@ -2482,6 +2520,12 @@ export declare const MigrationPlan: Schema.Struct<{
2482
2520
  readonly slack?: {
2483
2521
  readonly connection: string;
2484
2522
  } | undefined;
2523
+ readonly channels?: {
2524
+ readonly [x: string]: {
2525
+ readonly kind: "telegram" | "email" | "whatsapp" | "slack";
2526
+ readonly connection: string;
2527
+ };
2528
+ } | undefined;
2485
2529
  readonly attribution?: {
2486
2530
  readonly template?: string | undefined;
2487
2531
  readonly separator?: string | undefined;
@@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto";
3
3
  import { Effect } from "effect";
4
4
  import { SqlClient } from "effect/unstable/sql";
5
5
  import { AgentChat } from "../agent-chat.js";
6
+ import { chatChannelKind } from "../chat-channel.js";
6
7
  import { AgentReview } from "../agent-review.js";
7
8
  import { AGENT_IDENTITIES_TABLE, AGENT_PAIRINGS_TABLE, CHANNEL_USERS_TABLE, IDENTITY_NOTICES_TABLE, USER_TABLE, } from "../migrate/storage.js";
8
9
  import { MAX_QUERY_LIMIT } from "../query.js";
@@ -300,6 +301,7 @@ export function sqlAgentStore(ontology, sql) {
300
301
  ? principal
301
302
  : { ...principal, authenticatedVia: provider };
302
303
  }),
304
+ channelKind: (name) => chatChannelKind(ontology.manifest.chat?.channels, name),
303
305
  seeChat: ({ provider, externalId, title, memberId, leftMemberId, agentLeft, migratedFrom }) => sql
304
306
  .withTransaction(Effect.gen(function* () {
305
307
  yield* beginReplicationWrite(sql);
@@ -473,6 +475,7 @@ export function sqlAgentStore(ontology, sql) {
473
475
  values: {
474
476
  key: input.key,
475
477
  provider: input.provider,
478
+ channel: input.channel,
476
479
  chat_id: input.chatId,
477
480
  recipient: input.recipient,
478
481
  // Telegram's groups and channels have negative ids; WhatsApp chats are private.
@@ -2,6 +2,8 @@ import { Schema } from "effect";
2
2
  /** Decoded only at a verified runtime boundary; ordinary action arguments never establish provenance. */
3
3
  export declare const RuntimeSource: Schema.Union<readonly [Schema.Struct<{
4
4
  readonly provider: Schema.Literal<"telegram">;
5
+ /** The bot's channel when it is not the channel named `telegram`. */
6
+ readonly channel: Schema.optionalKey<Schema.String>;
5
7
  readonly chat_id: Schema.String;
6
8
  readonly thread_id: Schema.String;
7
9
  readonly message_id: Schema.optionalKey<Schema.String>;
@@ -23,6 +25,8 @@ export declare const RuntimeSource: Schema.Union<readonly [Schema.Struct<{
23
25
  }>>>;
24
26
  }>, Schema.Struct<{
25
27
  readonly provider: Schema.Literals<readonly ["slack", "whatsapp"]>;
28
+ /** The channel when it is not the one named after its kind. */
29
+ readonly channel: Schema.optionalKey<Schema.String>;
26
30
  readonly chat_id: Schema.String;
27
31
  readonly thread_id: Schema.String;
28
32
  readonly kind: Schema.Literals<readonly ["private", "group"]>;
@@ -1,4 +1,5 @@
1
1
  import { Schema } from "effect";
2
+ import { ChatChannelName } from "./chat-channel.js";
2
3
  const Id = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512));
3
4
  /**
4
5
  * The location a Telegram message shared, as the Bot API sends it: a live location's latest update
@@ -13,6 +14,8 @@ const TelegramLocation = Schema.Struct({
13
14
  /** A Telegram chat, with the references Telegram's own actions and attachments use. */
14
15
  const TelegramRuntimeSource = Schema.Struct({
15
16
  provider: Schema.Literal("telegram"),
17
+ /** The bot's channel when it is not the channel named `telegram`. */
18
+ channel: Schema.optionalKey(ChatChannelName),
16
19
  chat_id: Id,
17
20
  thread_id: Schema.String.check(Schema.isMaxLength(512)),
18
21
  message_id: Schema.optionalKey(Id),
@@ -44,6 +47,8 @@ const TelegramRuntimeSource = Schema.Struct({
44
47
  */
45
48
  const ChannelRuntimeSource = Schema.Struct({
46
49
  provider: Schema.Literals(["slack", "whatsapp"]),
50
+ /** The channel when it is not the one named after its kind. */
51
+ channel: Schema.optionalKey(ChatChannelName),
47
52
  chat_id: Id,
48
53
  thread_id: Schema.String.check(Schema.isMaxLength(512)),
49
54
  kind: Schema.Literals(["private", "group"]),
package/dist/runtime.d.ts CHANGED
@@ -11,7 +11,10 @@ import type { WorkloadExecution } from "./runtime/workload.js";
11
11
  * guest it acts for.
12
12
  */
13
13
  export interface ChatProvenance {
14
- /** The chat's provider: `web`, `telegram`, `slack`, `whatsapp` or `email`. */
14
+ /**
15
+ * The chat's channel, by its name as the archive keys it: `web`, or the agent's channel, such as
16
+ * `telegram` or `sales_whatsapp`.
17
+ */
15
18
  readonly provider: string;
16
19
  /** The chat's id there, such as `guest:<id>` or `member:<id>` on the web, or `-100123`. */
17
20
  readonly chatId: string;
@@ -128,6 +128,7 @@ export declare const ChatSeenRequest: Schema.Struct<{
128
128
  export declare const ReviewRequest: Schema.Struct<{
129
129
  readonly key: Schema.String;
130
130
  readonly provider: Schema.String;
131
+ readonly channel: Schema.optionalKey<Schema.String>;
131
132
  readonly chat_id: Schema.String;
132
133
  readonly recipient: Schema.String;
133
134
  readonly thread_id: Schema.String;
@@ -1,5 +1,6 @@
1
1
  import { randomInt } from "node:crypto";
2
2
  import { Schema } from "effect";
3
+ import { ChatChannelName } from "../../chat-channel.js";
3
4
  import { GUEST_USD_CEILINGS, MAX_GUEST_USAGE_REPORT_USD } from "../../runtime/agent-counters.js";
4
5
  /** How long a member has to open the connect link, sign in and approve the pairing. */
5
6
  export const PAIRING_TTL_MS = 10 * 60 * 1000;
@@ -156,6 +157,10 @@ export const ReviewRequest = Schema.Struct({
156
157
  description: "The agent's key for the reply: its session and content part.",
157
158
  }),
158
159
  provider: Provider,
160
+ channel: Schema.optionalKey(ChatChannelName.annotate({
161
+ description: "The channel the chat is on, by name, when it is not the one named after its kind, " +
162
+ "such as sales_whatsapp: approval sends the reply through its connection.",
163
+ })),
159
164
  chat_id: ChatId.annotate({ description: "The chat's id on the channel." }),
160
165
  recipient: ChatId.annotate({
161
166
  description: "Whom the provider sends the reply to: the chat's id, or the person's number.",
@@ -3,6 +3,7 @@ import { randomUUID } from "node:crypto";
3
3
  import { Effect, Layer, Redacted, Result, Schema } from "effect";
4
4
  import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
5
5
  import { HttpApiBuilder, HttpApiSecurity } from "effect/unstable/httpapi";
6
+ import { isChatChannelKind } from "../../chat-channel.js";
6
7
  import { API_KEY_PREFIX } from "../../runtime/principals.js";
7
8
  import { schemaIssues } from "../../runtime/validation.js";
8
9
  import { BASE_PATH } from "../api.js";
@@ -305,9 +306,18 @@ function holdReply(store) {
305
306
  return toServerResponse(notFound(requestId, "This ontology service holds no replies."));
306
307
  }
307
308
  const body = decoded.success;
309
+ // A channel named after a kind is that kind.
310
+ if (body.channel !== undefined &&
311
+ isChatChannelKind(body.channel) &&
312
+ body.channel !== body.provider) {
313
+ return invalidBody(requestId, [
314
+ { path: ["channel"], message: "A channel named after a kind is that kind." },
315
+ ]);
316
+ }
308
317
  const review = yield* store.holdReply({
309
318
  key: body.key,
310
319
  provider: body.provider,
320
+ channel: body.channel ?? body.provider,
311
321
  chatId: body.chat_id,
312
322
  recipient: body.recipient,
313
323
  threadId: body.thread_id,
@@ -324,6 +334,7 @@ function holdReply(store) {
324
334
  yield* Effect.logInfo("ontology.agent.reply_held").pipe(Effect.annotateLogs({
325
335
  requestId,
326
336
  provider: body.provider,
337
+ channel: body.channel ?? body.provider,
327
338
  reviewId: review.id,
328
339
  created: review.created,
329
340
  mode: body.mode,
@@ -208,10 +208,11 @@ export interface CredentialOptions {
208
208
  /**
209
209
  * The chat a verified runtime caller names, kept as the principal's when it can be the chat it acts
210
210
  * in: on the web, the member's or guest's own chat, which their agent token names; on a messenger,
211
- * a chat of the provider the member wrote from. Anything else is left out, never refused: it only
212
- * attributes.
211
+ * a chat on a channel of the kind the member wrote from. The chat names its channel by name, as
212
+ * the archive does; `kindOf` answers the channel's kind, as the ontology's `chat` declares it.
213
+ * Anything else is left out, never refused: it only attributes.
213
214
  */
214
- export declare function actingChat(principal: Principal, provider: string | undefined, chat: ChatProvenance | undefined): ChatProvenance | undefined;
215
+ export declare function actingChat(principal: Principal, provider: string | undefined, chat: ChatProvenance | undefined, kindOf?: (channel: string) => string | undefined): ChatProvenance | undefined;
215
216
  /** Resolves a bearer credential to its current principal and verified credential provenance. */
216
217
  export declare function authenticate(credential: Redacted.Redacted<string>, verifyToken: TokenVerifier, options?: CredentialOptions): Effect.Effect<Authenticated, never, OntologyBackend>;
217
218
  /** A browser's request, which may authenticate with the app's Neon Auth session cookie. */
@@ -453,10 +453,11 @@ export function sessionMayAuthenticate(request) {
453
453
  /**
454
454
  * The chat a verified runtime caller names, kept as the principal's when it can be the chat it acts
455
455
  * in: on the web, the member's or guest's own chat, which their agent token names; on a messenger,
456
- * a chat of the provider the member wrote from. Anything else is left out, never refused: it only
457
- * attributes.
456
+ * a chat on a channel of the kind the member wrote from. The chat names its channel by name, as
457
+ * the archive does; `kindOf` answers the channel's kind, as the ontology's `chat` declares it.
458
+ * Anything else is left out, never refused: it only attributes.
458
459
  */
459
- export function actingChat(principal, provider, chat) {
460
+ export function actingChat(principal, provider, chat, kindOf = (channel) => channel) {
460
461
  if (chat === undefined) {
461
462
  return undefined;
462
463
  }
@@ -466,7 +467,9 @@ export function actingChat(principal, provider, chat) {
466
467
  ? chat
467
468
  : undefined;
468
469
  }
469
- return provider === undefined || provider === chat.provider ? chat : undefined;
470
+ // The member's identity is of a kind; a channel of another name, such as `sales_whatsapp`,
471
+ // counts as the kind the ontology's chat declares for it.
472
+ return provider === undefined || provider === kindOf(chat.provider) ? chat : undefined;
470
473
  }
471
474
  /**
472
475
  * The member a verified runtime caller acts for: the member `X-SegmentStream-Act-As` names, with the
@@ -494,7 +497,9 @@ function actingMember(actAs, provider, grant, guests = false, chat) {
494
497
  : provider === WEB_PROVIDER
495
498
  ? yield* webMember(backend.agents, actAs, grant)
496
499
  : yield* backend.agents.actingMember(actAs, provider);
497
- const acting = member === undefined ? undefined : actingChat(member, provider, chat);
500
+ const acting = member === undefined
501
+ ? undefined
502
+ : actingChat(member, provider, chat, backend.agents?.channelKind);
498
503
  const withChat = acting === undefined ? {} : { chat: acting };
499
504
  if (member?.kind === "guest") {
500
505
  return guests
@@ -7,7 +7,7 @@ import { isAllowed } from "../../permissions.js";
7
7
  import { describeChatError } from "./diagnostics.js";
8
8
  import { deliveryRequester } from "./attribution.js";
9
9
  import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./idempotency.js";
10
- import { chatConnection, chatProvider, listedChatProviders } from "./providers/index.js";
10
+ import { channelProvider, chatConnection, chatKind, listedChatProviders, } from "./providers/index.js";
11
11
  import { chatResolver } from "./resolve.js";
12
12
  import { reviewSender } from "./review-delivery.js";
13
13
  /** The channels a runtime source can name, which a verified pairing's provider is one of. */
@@ -18,7 +18,8 @@ const CHANNEL_PROVIDERS = new Set(["telegram", "slack", "whatsapp"]);
18
18
  * private source matches a private destination on the same provider by its `peer`.
19
19
  */
20
20
  export function sameChat(source, target) {
21
- if (source.provider !== target.provider) {
21
+ // The target's provider is the channel it is on, by name; the source's is its kind.
22
+ if ((source.channel ?? source.provider) !== target.provider) {
22
23
  return false;
23
24
  }
24
25
  if (source.chat_id === target.external_id) {
@@ -58,9 +59,12 @@ export function makeChatDelivery(ontology, connections, archive, previews = {})
58
59
  message: "The requester must still be paired and active.",
59
60
  });
60
61
  }
61
- const target = yield* chatResolver(context.backend, context.principal, providers).resolve(input.chat);
62
- // A chat is a destination only on a provider the ontology declares a connection for.
63
- const provider = chatProvider(target.provider);
62
+ const target = yield* chatResolver(context.backend, context.principal, providers, {
63
+ kindOf: (channel) => chatKind(ontology.manifest.chat, channel),
64
+ }).resolve(input.chat);
65
+ // A chat is a destination only on a channel the ontology declares a connection for; the
66
+ // channel's kind sends it.
67
+ const provider = channelProvider(ontology.manifest.chat, target.provider);
64
68
  const connection = chatConnection(ontology.manifest.chat, target.provider);
65
69
  const replyTo = input.replyTo ?? null;
66
70
  if (provider === undefined || !connection) {
@@ -8,6 +8,7 @@ declare const RuntimeEnvelope: Schema.Struct<{
8
8
  readonly operation_id: Schema.optionalKey<Schema.String>;
9
9
  readonly source: Schema.optionalKey<Schema.Union<readonly [Schema.Struct<{
10
10
  readonly provider: Schema.Literal<"telegram">;
11
+ readonly channel: Schema.optionalKey<Schema.String>;
11
12
  readonly chat_id: Schema.String;
12
13
  readonly thread_id: Schema.String;
13
14
  readonly message_id: Schema.optionalKey<Schema.String>;
@@ -29,6 +30,7 @@ declare const RuntimeEnvelope: Schema.Struct<{
29
30
  }>>>;
30
31
  }>, Schema.Struct<{
31
32
  readonly provider: Schema.Literals<readonly ["slack", "whatsapp"]>;
33
+ readonly channel: Schema.optionalKey<Schema.String>;
32
34
  readonly chat_id: Schema.String;
33
35
  readonly thread_id: Schema.String;
34
36
  readonly kind: Schema.Literals<readonly ["private", "group"]>;
@@ -51,6 +53,7 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
51
53
  readonly chat_id: string;
52
54
  readonly thread_id: string;
53
55
  readonly message_id?: string | undefined;
56
+ readonly channel?: string | undefined;
54
57
  readonly attachments?: readonly {
55
58
  readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
56
59
  readonly file_id: string;
@@ -73,6 +76,7 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
73
76
  readonly chat_id: string;
74
77
  readonly thread_id: string;
75
78
  readonly message_id?: string | undefined;
79
+ readonly channel?: string | undefined;
76
80
  readonly sender?: string | undefined;
77
81
  readonly peer?: string | undefined;
78
82
  } | undefined;
@@ -103,6 +107,7 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
103
107
  readonly chat_id: string;
104
108
  readonly thread_id: string;
105
109
  readonly message_id?: string | undefined;
110
+ readonly channel?: string | undefined;
106
111
  readonly attachments?: readonly {
107
112
  readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
108
113
  readonly file_id: string;
@@ -125,6 +130,7 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
125
130
  readonly chat_id: string;
126
131
  readonly thread_id: string;
127
132
  readonly message_id?: string | undefined;
133
+ readonly channel?: string | undefined;
128
134
  readonly sender?: string | undefined;
129
135
  readonly peer?: string | undefined;
130
136
  } | undefined;
@@ -12,15 +12,25 @@ export declare const CHAT_PROVIDERS: {
12
12
  readonly email: ChatProvider;
13
13
  };
14
14
  export type ChatProviderName = keyof typeof CHAT_PROVIDERS;
15
- /** The provider a destination's chat is on, when the platform posts to it. */
16
- export declare function chatProvider(name: string): ChatProvider | undefined;
17
- /** The connection the ontology declares for posting to a provider's chats, if it declares one. */
18
- export declare function chatConnection(chat: OntologyManifest["chat"], provider: string): string | undefined;
19
- /** The providers the ontology declares a posting connection for: the destinations it posts to. */
20
- export declare function declaredChatProviders(chat: OntologyManifest["chat"]): ReadonlyArray<ChatProviderName>;
15
+ /** The provider of a kind of channel, when the platform posts to it. */
16
+ export declare function chatProvider(kind: string): ChatProvider | undefined;
21
17
  /**
22
- * The providers whose chats the chat tools list, read and resolve: Telegram's, whose chats the
23
- * tools always listed, and each declared one. Posting still requires the target's provider to
24
- * be declared.
18
+ * The kind of the agent's channel `name` the ontology's `chat` declares: the name itself for the
19
+ * channel named after its kind, which a chat on it before channels had names also is.
20
+ */
21
+ export declare function chatKind(chat: OntologyManifest["chat"], name: string): string | undefined;
22
+ /** The provider of the agent's channel `name`, by its kind, when the platform posts to it. */
23
+ export declare function channelProvider(chat: OntologyManifest["chat"], name: string): ChatProvider | undefined;
24
+ /** The connection the ontology declares for the agent's channel `name`, if it declares one. */
25
+ export declare function chatConnection(chat: OntologyManifest["chat"], name: string): string | undefined;
26
+ /**
27
+ * The channels the ontology declares a posting connection for, by name: the destinations it posts
28
+ * to. The channel named after each kind comes first, then the others.
29
+ */
30
+ export declare function declaredChatProviders(chat: OntologyManifest["chat"]): ReadonlyArray<string>;
31
+ /**
32
+ * The channels whose chats the chat tools list, read and resolve, by name: Telegram's, whose
33
+ * chats the tools always listed, and each declared one. Posting still requires the target's
34
+ * channel to be declared.
25
35
  */
26
36
  export declare function listedChatProviders(chat: OntologyManifest["chat"]): ReadonlyArray<string>;
@@ -14,22 +14,42 @@ export const CHAT_PROVIDERS = {
14
14
  };
15
15
  const NAMES = Object.keys(CHAT_PROVIDERS);
16
16
  const isChatProvider = (name) => Object.hasOwn(CHAT_PROVIDERS, name);
17
- /** The provider a destination's chat is on, when the platform posts to it. */
18
- export function chatProvider(name) {
19
- return isChatProvider(name) ? CHAT_PROVIDERS[name] : undefined;
17
+ /** The provider of a kind of channel, when the platform posts to it. */
18
+ export function chatProvider(kind) {
19
+ return isChatProvider(kind) ? CHAT_PROVIDERS[kind] : undefined;
20
20
  }
21
- /** The connection the ontology declares for posting to a provider's chats, if it declares one. */
22
- export function chatConnection(chat, provider) {
23
- return isChatProvider(provider) ? chat?.[provider]?.connection : undefined;
21
+ /**
22
+ * The kind of the agent's channel `name` the ontology's `chat` declares: the name itself for the
23
+ * channel named after its kind, which a chat on it before channels had names also is.
24
+ */
25
+ export function chatKind(chat, name) {
26
+ return isChatProvider(name) ? name : chat?.channels?.[name]?.kind;
27
+ }
28
+ /** The provider of the agent's channel `name`, by its kind, when the platform posts to it. */
29
+ export function channelProvider(chat, name) {
30
+ const kind = chatKind(chat, name);
31
+ return kind === undefined ? undefined : chatProvider(kind);
24
32
  }
25
- /** The providers the ontology declares a posting connection for: the destinations it posts to. */
33
+ /** The connection the ontology declares for the agent's channel `name`, if it declares one. */
34
+ export function chatConnection(chat, name) {
35
+ return isChatProvider(name) ? chat?.[name]?.connection : chat?.channels?.[name]?.connection;
36
+ }
37
+ /**
38
+ * The channels the ontology declares a posting connection for, by name: the destinations it posts
39
+ * to. The channel named after each kind comes first, then the others.
40
+ */
26
41
  export function declaredChatProviders(chat) {
27
- return NAMES.filter((name) => chat?.[name] !== undefined && CHAT_PROVIDERS[name].posts !== false);
42
+ return [
43
+ ...NAMES.filter((name) => chat?.[name] !== undefined && CHAT_PROVIDERS[name].posts !== false),
44
+ ...Object.entries(chat?.channels ?? {})
45
+ .filter(([, channel]) => chatProvider(channel.kind)?.posts !== false)
46
+ .map(([name]) => name),
47
+ ];
28
48
  }
29
49
  /**
30
- * The providers whose chats the chat tools list, read and resolve: Telegram's, whose chats the
31
- * tools always listed, and each declared one. Posting still requires the target's provider to
32
- * be declared.
50
+ * The channels whose chats the chat tools list, read and resolve, by name: Telegram's, whose
51
+ * chats the tools always listed, and each declared one. Posting still requires the target's
52
+ * channel to be declared.
33
53
  */
34
54
  export function listedChatProviders(chat) {
35
55
  return [...new Set(["telegram", ...declaredChatProviders(chat)])];
@@ -6,18 +6,21 @@ import type { OntologyBackend } from "../runtime-contract.js";
6
6
  import type { ChatArchiveTransport } from "../agent/chat-archive.js";
7
7
  import type { ChatList, ChatHistory } from "./schemas.js";
8
8
  export declare function chatReads(backend: OntologyBackend["Service"], options: {
9
- /** The providers whose chats are listed, as `listedChatProviders` gives them. */
9
+ /** The channels whose chats are listed, as `listedChatProviders` gives them. */
10
10
  readonly providers: ReadonlyArray<string>;
11
+ /** The kind of each channel, as `chatKind` gives it; the name by default. */
12
+ readonly kindOf?: (channel: string) => string | undefined;
11
13
  readonly chatHistory?: ChatArchiveTransport;
12
14
  readonly attribution?: ChatAttributionSettings;
13
15
  }): {
14
16
  list: (input: typeof ChatList.Type) => Effect.Effect<{
15
17
  items: {
18
+ external_id: string;
19
+ attribution: string | null;
20
+ kind?: "telegram" | "email" | "whatsapp" | "slack" | undefined;
16
21
  id: string;
17
22
  title: string;
18
23
  provider: string;
19
- external_id: string;
20
- attribution: string | null;
21
24
  }[];
22
25
  next_cursor: string | null;
23
26
  }, ActionRejected | PermissionDenied | import("../../errors.js").NotFound, CurrentPrincipal>;
@@ -1,3 +1,4 @@
1
+ import { isChatChannelKind } from "../../chat-channel.js";
1
2
  import { Effect } from "effect";
2
3
  import { ActionRejected, PermissionDenied } from "../../errors.js";
3
4
  import { isAllowed } from "../../permissions.js";
@@ -5,16 +6,20 @@ import { CurrentPrincipal } from "../authentication.js";
5
6
  import { currentCaller } from "../caller-context.js";
6
7
  import { chatAttribution } from "./attribution.js";
7
8
  import { chatResolver, groupChatKey, isChatMember } from "./resolve.js";
8
- /** The providers whose archived chats are keyed by the chat a destination names. */
9
- const HISTORY_PROVIDERS = ["telegram"];
9
+ /** The kinds of channel whose archived chats are keyed by the chat a destination names. */
10
+ const HISTORY_KINDS = new Set(["telegram"]);
10
11
  export function chatReads(backend, options) {
11
12
  const { providers, chatHistory, attribution } = options;
13
+ const kindOf = options.kindOf ?? ((channel) => channel);
14
+ const history = [
15
+ ...new Set(["telegram", ...providers.filter((name) => HISTORY_KINDS.has(kindOf(name) ?? ""))]),
16
+ ];
12
17
  return {
13
18
  list: (input) => Effect.gen(function* () {
14
19
  const principal = yield* CurrentPrincipal;
15
20
  const caller = yield* currentCaller;
16
21
  const query = input.query?.toLowerCase();
17
- const items = (yield* chatResolver(backend, principal, providers).list(input.chat))
22
+ const items = (yield* chatResolver(backend, principal, providers, { kindOf }).list(input.chat))
18
23
  .filter((item) => query === undefined || item.title.toLowerCase().includes(query))
19
24
  .sort((a, b) => a.id.localeCompare(b.id));
20
25
  const user = yield* backend.get({ entity: "user", id: principal.id });
@@ -25,13 +30,19 @@ export function chatReads(backend, options) {
25
30
  });
26
31
  }
27
32
  const limit = input.limit ?? 50;
28
- const selected = items.slice(start, start + limit).map((item) => ({
29
- id: item.id,
30
- title: item.title,
31
- provider: item.provider,
32
- external_id: item.external_id,
33
- attribution: chatAttribution(caller, String(user.name), attribution),
34
- }));
33
+ const selected = items.slice(start, start + limit).map((item) => {
34
+ // The kind of the channel the chat is on, so that a reader renders a post to a
35
+ // channel of another name, such as sales_whatsapp, as that kind does.
36
+ const kind = kindOf(item.provider);
37
+ return {
38
+ id: item.id,
39
+ title: item.title,
40
+ provider: item.provider,
41
+ ...(isChatChannelKind(kind) ? { kind } : {}),
42
+ external_id: item.external_id,
43
+ attribution: chatAttribution(caller, String(user.name), attribution),
44
+ };
45
+ });
35
46
  return {
36
47
  items: selected,
37
48
  next_cursor: start + limit < items.length ? selected.at(-1).id : null,
@@ -42,11 +53,12 @@ export function chatReads(backend, options) {
42
53
  if (principal.kind !== "member") {
43
54
  return yield* new PermissionDenied({ message: "Only members may read chatHistory." });
44
55
  }
45
- // History reads Telegram chats only. Other channels archive a chat under their
46
- // session's address with private coordinates, not under the chat a destination names, so
47
- // their history would be incomplete until the archive keys them the same way.
48
- const target = yield* chatResolver(backend, principal, HISTORY_PROVIDERS, {
56
+ // History reads the chats of Telegram channels only. Other channels archive a chat under
57
+ // their session's address with private coordinates, not under the chat a destination
58
+ // names, so their history would be incomplete until the archive keys them the same way.
59
+ const target = yield* chatResolver(backend, principal, history, {
49
60
  memberGroups: true,
61
+ kindOf,
50
62
  }).resolve(input.chat);
51
63
  // A group's members read its history, as they read its attachments; agent_chat:read reads
52
64
  // every group. A private chat is read only by its participant.
@@ -26,12 +26,16 @@ export declare function isChatMember(backend: OntologyBackend["Service"], princi
26
26
  */
27
27
  export declare function withChatMembership(transport: ChatArchiveTransport, backend: OntologyBackend["Service"]): ChatArchiveTransport;
28
28
  /**
29
- * Resolves the chats of `providers`: their group chats in the registry, and each member's private
30
- * chat on each provider they have a live identity on, `<provider>:private:<member id>`.
29
+ * Resolves the chats of `providers`, the agent's channels by name: their group chats in the
30
+ * registry, and each member's private chat on each channel whose kind they have a live identity
31
+ * on, `<channel>:private:<member id>`. People are not per channel: one identity of a kind reaches
32
+ * the member on every channel of that kind.
31
33
  */
32
34
  export declare function chatResolver(backend: OntologyBackend["Service"], principal: Principal, providers: ReadonlyArray<string>, options?: {
33
35
  /** Also resolve the group chats a member is in without chat read or post permission. */
34
36
  readonly memberGroups?: boolean;
37
+ /** The kind of each channel, whose identities reach its private chats; the name by default. */
38
+ readonly kindOf?: (channel: string) => string | undefined;
35
39
  }): {
36
40
  list: (chat?: string) => Effect.Effect<Destination[], ActionRejected | PermissionDenied, never>;
37
41
  resolve: (chat: string) => Effect.Effect<Destination, ActionRejected | PermissionDenied, never>;