@segmentstream/ontology 0.210.2 → 0.211.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 +6 -0
- package/README.md +9 -0
- package/dist/agent-chat.d.ts +8 -0
- package/dist/agent-chat.js +18 -1
- package/dist/chat.d.ts +11 -0
- package/dist/chat.js +9 -0
- package/dist/runtime/agents.js +6 -4
- package/dist/service/chat/delivery.js +37 -0
- package/dist/service/chat/providers/telegram-group.d.ts +6 -0
- package/dist/service/chat/providers/telegram-group.js +62 -0
- package/dist/service/runtime-contract.d.ts +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.211.0
|
|
4
|
+
|
|
5
|
+
Register a group an existing Telegram bot already belongs to with `agent_chat.registerGroup`.
|
|
6
|
+
The declared channel verifies the expected bot and its membership before returning a registry ID.
|
|
7
|
+
The group can receive its first real message later, with its routing and membership intact.
|
|
8
|
+
|
|
3
9
|
## 0.210.2
|
|
4
10
|
|
|
5
11
|
Report verified caller types with SQL activity so builders can distinguish workspace app sessions, API keys, agents and platform operations in Usage.
|
package/README.md
CHANGED
|
@@ -1548,6 +1548,15 @@ its id, with their files as the chat's attachments, for a caller with `agent_cha
|
|
|
1548
1548
|
`agent_chat:read` and `agent_chat:update`, granted explicitly; wildcard entity grants do not
|
|
1549
1549
|
cover this built-in registry.
|
|
1550
1550
|
|
|
1551
|
+
Before transferring an existing bot, a workspace administrator can call
|
|
1552
|
+
`agent_chat.registerGroup` with `{ channel: "telegram", chatId: "-100123", botId: "12345" }`.
|
|
1553
|
+
Use the deployed channel name and the expected bot's numeric ID. The configured connection
|
|
1554
|
+
reads the bot identity, group and bot membership through Telegram. On success the action
|
|
1555
|
+
returns the canonical chat record's `id` and provider `title`; repeating it reuses the record.
|
|
1556
|
+
The next real message uses that same record. The action requires unscoped
|
|
1557
|
+
`agent_chat:register_group` from a member, OAuth client or service credential. Registering a
|
|
1558
|
+
group leaves chat activity, participants, routing, send permissions and the bot's webhook alone.
|
|
1559
|
+
|
|
1551
1560
|
`agent_chat.send` (MCP `chat_send`) sends exact text directly through the ontology service's
|
|
1552
1561
|
`ChatDelivery`, as the agent by default, under `agent_chat:send`, or with `as: "me"` in the
|
|
1553
1562
|
caller's own voice, signed through `chat.attribution`, under `agent_chat:post_message`. Either
|
package/dist/agent-chat.d.ts
CHANGED
|
@@ -76,6 +76,14 @@ export declare const AgentChat: import("./entity.js").Entity<"agent_chat", {
|
|
|
76
76
|
readonly to: readonly string[];
|
|
77
77
|
readonly cc: readonly string[];
|
|
78
78
|
}>;
|
|
79
|
+
readonly registerGroup: import("./action.js").Action<Schema.Struct<{
|
|
80
|
+
readonly channel: Schema.String;
|
|
81
|
+
readonly chatId: Schema.String;
|
|
82
|
+
readonly botId: Schema.String;
|
|
83
|
+
}>, {
|
|
84
|
+
readonly id: string;
|
|
85
|
+
readonly title: string;
|
|
86
|
+
}>;
|
|
79
87
|
readonly importHistory: import("./action.js").Action<Schema.Struct<{
|
|
80
88
|
readonly chat: Schema.optionalKey<Schema.String>;
|
|
81
89
|
readonly channel: Schema.optionalKey<Schema.String>;
|
package/dist/agent-chat.js
CHANGED
|
@@ -4,7 +4,7 @@ import { action } from "./action.js";
|
|
|
4
4
|
import { AGENT_DRAFT_NOTE_LIMIT } from "./agent-draft.js";
|
|
5
5
|
import { entity } from "./entity.js";
|
|
6
6
|
import { ActionRejected } from "./errors.js";
|
|
7
|
-
import { ChatDelivery, ChatHistoryImportInput, ChatPostInput, ChatSendInput } from "./chat.js";
|
|
7
|
+
import { ChatDelivery, ChatGroupRegistration, ChatHistoryImportInput, ChatPostInput, ChatSendInput, } from "./chat.js";
|
|
8
8
|
import { p } from "./property.js";
|
|
9
9
|
import { rel } from "./relationship.js";
|
|
10
10
|
export const TELEGRAM = "telegram";
|
|
@@ -352,6 +352,23 @@ export const AgentChat = entity("agent_chat", {
|
|
|
352
352
|
return yield* recipients({ chat: String(input.chat) });
|
|
353
353
|
}),
|
|
354
354
|
}),
|
|
355
|
+
registerGroup: action({
|
|
356
|
+
description: "Register an existing group before its next message. Supply the declared Telegram channel, " +
|
|
357
|
+
"the group's numeric ID and the expected bot's numeric ID. The provider verifies membership. " +
|
|
358
|
+
"Returns the registry ID and current title. Requires unscoped agent_chat:register_group.",
|
|
359
|
+
input: ChatGroupRegistration,
|
|
360
|
+
hints: { destructive: false, idempotent: true },
|
|
361
|
+
connections: [],
|
|
362
|
+
prepare: ({ input }) => Effect.gen(function* () {
|
|
363
|
+
const delivery = yield* Effect.serviceOption(ChatDelivery);
|
|
364
|
+
if (Option.isNone(delivery) || delivery.value.registerGroup === undefined) {
|
|
365
|
+
return yield* new ActionRejected({
|
|
366
|
+
message: "Group registration requires the chat registry.",
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
return yield* delivery.value.registerGroup(input);
|
|
370
|
+
}),
|
|
371
|
+
}),
|
|
355
372
|
importHistory: action({
|
|
356
373
|
description: "Import a chat's messages from before the agent recorded it, such as a client's earlier " +
|
|
357
374
|
"chat kept elsewhere: each joins the chat in its place by its time, which the " +
|
package/dist/chat.d.ts
CHANGED
|
@@ -36,6 +36,12 @@ export interface InboxOptions<R = unknown> {
|
|
|
36
36
|
}
|
|
37
37
|
import { Context, Effect, Schema } from "effect";
|
|
38
38
|
import type { ActionError } from "./errors.js";
|
|
39
|
+
export declare const ChatGroupRegistration: Schema.Struct<{
|
|
40
|
+
readonly channel: Schema.String;
|
|
41
|
+
readonly chatId: Schema.String;
|
|
42
|
+
readonly botId: Schema.String;
|
|
43
|
+
}>;
|
|
44
|
+
export type ChatGroupRegistration = typeof ChatGroupRegistration.Type;
|
|
39
45
|
/**
|
|
40
46
|
* A text post in the caller's own voice, signed with their name, as `agent_chat.previewMessage`
|
|
41
47
|
* renders it: what `agent_chat.send` takes with `as: "me"`, without a template or buttons.
|
|
@@ -331,6 +337,11 @@ declare const ChatDelivery_base: Context.ServiceClass<ChatDelivery, "@segmentstr
|
|
|
331
337
|
readonly recipients?: (input: {
|
|
332
338
|
readonly chat: string;
|
|
333
339
|
}) => Effect.Effect<ChatRecipients, ActionError>;
|
|
340
|
+
/** Registers a group after verifying the configured bot's current membership at the provider. */
|
|
341
|
+
readonly registerGroup?: (input: ChatGroupRegistration) => Effect.Effect<{
|
|
342
|
+
readonly id: string;
|
|
343
|
+
readonly title: string;
|
|
344
|
+
}, ActionError>;
|
|
334
345
|
/** Adds a chat's messages from before the agent recorded it, each in its place by its time. */
|
|
335
346
|
readonly importHistory?: (input: ChatHistoryImportInput) => Effect.Effect<ChatHistoryImportResult, ActionError>;
|
|
336
347
|
/** The channels whose private chats staff may take over, as the ontology's inbox lists them. */
|
package/dist/chat.js
CHANGED
|
@@ -15,6 +15,15 @@ const ChatMessageFiles = Schema.Array(Schema.Struct({
|
|
|
15
15
|
description: "Stored files the message carries after its text, each a record and its file property, " +
|
|
16
16
|
"which the caller may read: content by default. Each is sent at the revision read now.",
|
|
17
17
|
});
|
|
18
|
+
export const ChatGroupRegistration = Schema.Struct({
|
|
19
|
+
channel: Identifier.annotate({ description: "The Telegram channel declared by this workspace." }),
|
|
20
|
+
chatId: Schema.String.check(Schema.isPattern(/^-[1-9]\d{0,15}$/)).annotate({
|
|
21
|
+
description: "The exact numeric Telegram group ID, including the minus sign.",
|
|
22
|
+
}),
|
|
23
|
+
botId: Schema.String.check(Schema.isPattern(/^[1-9]\d{0,15}$/)).annotate({
|
|
24
|
+
description: "The expected Telegram bot's numeric ID. Must match the channel's bot.",
|
|
25
|
+
}),
|
|
26
|
+
});
|
|
18
27
|
/**
|
|
19
28
|
* A text post in the caller's own voice, signed with their name, as `agent_chat.previewMessage`
|
|
20
29
|
* renders it: what `agent_chat.send` takes with `as: "me"`, without a template or buttons.
|
package/dist/runtime/agents.js
CHANGED
|
@@ -671,7 +671,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
671
671
|
registeredAt: chat ? new Date(Number(chat.registered_ms)).toISOString() : null,
|
|
672
672
|
changedAt: chat?.changed_ms == null ? null : new Date(Number(chat.changed_ms)).toISOString(),
|
|
673
673
|
}))),
|
|
674
|
-
seeChat: ({ provider, externalId, title, memberId, leftMemberId, agentLeft, migratedFrom }) => sql
|
|
674
|
+
seeChat: ({ provider, externalId, title, memberId, leftMemberId, agentLeft, migratedFrom, registeredBy, }) => sql
|
|
675
675
|
.withTransaction(Effect.gen(function* () {
|
|
676
676
|
yield* beginReplicationWrite(sql);
|
|
677
677
|
const previous = migratedFrom === undefined || migratedFrom === externalId ? undefined : migratedFrom;
|
|
@@ -681,9 +681,11 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
681
681
|
yield* rows(`SELECT pg_advisory_xact_lock(hashtext($1))`, [`chat:${provider}:${id}`]);
|
|
682
682
|
}
|
|
683
683
|
const runtime = makeOntologyRuntime(ontology, sql, {
|
|
684
|
-
action:
|
|
684
|
+
action: registeredBy === undefined
|
|
685
|
+
? `${AgentChat.name}.seen`
|
|
686
|
+
: `${AgentChat.name}.register_group`,
|
|
685
687
|
actionId: randomUUID(),
|
|
686
|
-
principalId: null,
|
|
688
|
+
principalId: registeredBy ?? null,
|
|
687
689
|
});
|
|
688
690
|
const now = new Date();
|
|
689
691
|
const chatOf = (id) => runtime
|
|
@@ -783,7 +785,7 @@ export function sqlAgentStore(ontology, sql) {
|
|
|
783
785
|
}
|
|
784
786
|
const changes = {
|
|
785
787
|
...(found?.title === title ? {} : { title }),
|
|
786
|
-
...(messageDue(found?.last_message_at, now)
|
|
788
|
+
...(registeredBy === undefined && messageDue(found?.last_message_at, now)
|
|
787
789
|
? { last_message_at: now.toISOString() }
|
|
788
790
|
: {}),
|
|
789
791
|
};
|
|
@@ -5,6 +5,7 @@ import { Cause, Effect } from "effect";
|
|
|
5
5
|
import { chatOperationId, chatPreviewDigest, } from "../../chat.js";
|
|
6
6
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
7
7
|
import { effectiveScope, isAllowed } from "../../permissions.js";
|
|
8
|
+
import { verifyTelegramGroup } from "./providers/telegram-group.js";
|
|
8
9
|
import { describeChatError } from "./diagnostics.js";
|
|
9
10
|
import { deliveryRequester } from "./attribution.js";
|
|
10
11
|
import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./idempotency.js";
|
|
@@ -610,6 +611,42 @@ chats) {
|
|
|
610
611
|
};
|
|
611
612
|
return {
|
|
612
613
|
takeoverChannels,
|
|
614
|
+
registerGroup: (input) => Effect.gen(function* () {
|
|
615
|
+
const permission = "agent_chat:register_group";
|
|
616
|
+
if (effectiveScope(permission, context.principal) !== undefined ||
|
|
617
|
+
(context.caller.kind !== "oauth" &&
|
|
618
|
+
context.caller.kind !== "member" &&
|
|
619
|
+
context.caller.kind !== "service")) {
|
|
620
|
+
return yield* new PermissionDenied({
|
|
621
|
+
permission,
|
|
622
|
+
message: "Registering a group requires an unscoped administrative grant.",
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
const connection = chatConnection(ontology.manifest.chat, input.channel);
|
|
626
|
+
if (!context.backend.agents ||
|
|
627
|
+
!connection ||
|
|
628
|
+
chatKind(ontology.manifest.chat, input.channel) !== "telegram") {
|
|
629
|
+
return yield* new ActionRejected({
|
|
630
|
+
message: "Group registration requires a declared Telegram channel and chat registry.",
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
const transport = connections({
|
|
634
|
+
identityToken: context.identityToken,
|
|
635
|
+
audit: {
|
|
636
|
+
action: "agent_chat.register_group",
|
|
637
|
+
principalId: context.principal.id,
|
|
638
|
+
requestId: context.requestId,
|
|
639
|
+
},
|
|
640
|
+
});
|
|
641
|
+
const title = yield* verifyTelegramGroup(transport, connection, input);
|
|
642
|
+
const { id } = yield* context.backend.agents.seeChat({
|
|
643
|
+
provider: input.channel,
|
|
644
|
+
externalId: input.chatId,
|
|
645
|
+
title,
|
|
646
|
+
registeredBy: context.principal.id,
|
|
647
|
+
});
|
|
648
|
+
return { id, title };
|
|
649
|
+
}),
|
|
613
650
|
...(history === undefined
|
|
614
651
|
? {}
|
|
615
652
|
: {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { ChatGroupRegistration } from "../../../chat.js";
|
|
3
|
+
import type { ConnectionTransport } from "../../../connection.js";
|
|
4
|
+
import { ActionRejected } from "../../../errors.js";
|
|
5
|
+
/** Read the group and this bot's own membership. No member or routing fact comes from the caller. */
|
|
6
|
+
export declare function verifyTelegramGroup(transport: ConnectionTransport["Service"], connection: string, input: ChatGroupRegistration): Effect.Effect<string, ActionRejected, never>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { ActionRejected } from "../../../errors.js";
|
|
3
|
+
import { telegramBotId } from "../../channel/providers/telegram.js";
|
|
4
|
+
import { record } from "./provider.js";
|
|
5
|
+
/** Read the group and this bot's own membership. No member or routing fact comes from the caller. */
|
|
6
|
+
export function verifyTelegramGroup(transport, connection, input) {
|
|
7
|
+
const read = (method, params) => Effect.gen(function* () {
|
|
8
|
+
const response = yield* transport
|
|
9
|
+
.request({
|
|
10
|
+
connection,
|
|
11
|
+
api: "bot",
|
|
12
|
+
method: "GET",
|
|
13
|
+
path: `/${method}?${new URLSearchParams(params)}`,
|
|
14
|
+
headers: [],
|
|
15
|
+
body: new Uint8Array(),
|
|
16
|
+
})
|
|
17
|
+
.pipe(Effect.mapError((cause) => new ActionRejected({ message: `Telegram ${method} is unavailable.`, cause })));
|
|
18
|
+
const failed = (cause) => new ActionRejected({
|
|
19
|
+
message: `Telegram could not verify the group with ${method} (HTTP ${response.status}).`,
|
|
20
|
+
cause,
|
|
21
|
+
});
|
|
22
|
+
const body = yield* Effect.try({
|
|
23
|
+
try: () => record(JSON.parse(new TextDecoder().decode(response.body))),
|
|
24
|
+
catch: failed,
|
|
25
|
+
});
|
|
26
|
+
if (response.status !== 200 || body.ok !== true) {
|
|
27
|
+
return yield* failed(typeof body.description === "string" ? new Error(body.description) : undefined);
|
|
28
|
+
}
|
|
29
|
+
return record(body.result);
|
|
30
|
+
});
|
|
31
|
+
return Effect.gen(function* () {
|
|
32
|
+
// Start with a fresh identity for each registration, including after the connection rotates.
|
|
33
|
+
const botId = yield* telegramBotId(connection)(transport);
|
|
34
|
+
if (botId !== input.botId) {
|
|
35
|
+
return yield* new ActionRejected({
|
|
36
|
+
message: "The channel is connected to a different Telegram bot.",
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const chat = yield* read("getChat", { chat_id: input.chatId });
|
|
40
|
+
if (!Number.isSafeInteger(chat.id) ||
|
|
41
|
+
String(chat.id) !== input.chatId ||
|
|
42
|
+
(chat.type !== "group" && chat.type !== "supergroup") ||
|
|
43
|
+
typeof chat.title !== "string" ||
|
|
44
|
+
chat.title.trim() === "") {
|
|
45
|
+
return yield* new ActionRejected({ message: "Telegram did not return the specified group." });
|
|
46
|
+
}
|
|
47
|
+
const member = yield* read("getChatMember", { chat_id: input.chatId, user_id: botId });
|
|
48
|
+
const user = record(member.user);
|
|
49
|
+
if (!Number.isSafeInteger(user.id) ||
|
|
50
|
+
String(user.id) !== botId ||
|
|
51
|
+
user.is_bot !== true ||
|
|
52
|
+
(member.status !== "creator" &&
|
|
53
|
+
member.status !== "administrator" &&
|
|
54
|
+
member.status !== "member" &&
|
|
55
|
+
!(member.status === "restricted" && member.is_member === true))) {
|
|
56
|
+
return yield* new ActionRejected({
|
|
57
|
+
message: "Telegram did not confirm this bot belongs to the group.",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return chat.title;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -716,6 +716,8 @@ export interface AgentStore {
|
|
|
716
716
|
readonly leftMemberId?: string;
|
|
717
717
|
readonly agentLeft?: true;
|
|
718
718
|
readonly migratedFrom?: string;
|
|
719
|
+
/** The administrator of a provider-verified registration. Such a check is not a message. */
|
|
720
|
+
readonly registeredBy?: string;
|
|
719
721
|
}) => Effect.Effect<SeenChat>;
|
|
720
722
|
/**
|
|
721
723
|
* Records a person's private chat with the agent for the inbox: creates the `agent_chat` of kind
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.211.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",
|