@segmentstream/ontology 0.98.1 → 0.99.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/README.md +8 -5
- package/dist/agent-review.d.ts +4 -4
- package/dist/agent-review.js +10 -10
- package/dist/app-feedback.js +1 -1
- package/dist/groups.d.ts +11 -4
- package/dist/groups.js +54 -11
- package/dist/permissions.d.ts +1 -1
- package/dist/permissions.js +1 -1
- package/dist/runtime-source.js +3 -3
- package/dist/service/agent/{conversation-routes.d.ts → archive-routes.d.ts} +7 -4
- package/dist/service/agent/{conversation-routes.js → archive-routes.js} +8 -5
- package/dist/service/agent/attachments.d.ts +2 -2
- package/dist/service/agent/attachments.js +2 -2
- package/dist/service/agent/authentication.d.ts +1 -1
- package/dist/service/agent/authentication.js +7 -5
- package/dist/service/agent/{conversations.d.ts → chat-archive.d.ts} +11 -11
- package/dist/service/agent/{conversations.js → chat-archive.js} +12 -12
- package/dist/service/agent/protocol.js +2 -2
- package/dist/service/chat/archive.d.ts +2 -2
- package/dist/service/chat/archive.js +1 -1
- package/dist/service/chat/attribution.d.ts +1 -1
- package/dist/service/chat/delivery.d.ts +2 -2
- package/dist/service/chat/delivery.js +2 -2
- package/dist/service/chat/idempotency.d.ts +3 -3
- package/dist/service/chat/reads.d.ts +3 -3
- package/dist/service/chat/reads.js +10 -10
- package/dist/service/chat/resolve.d.ts +6 -6
- package/dist/service/chat/resolve.js +7 -7
- package/dist/service/chat/review-delivery.d.ts +1 -1
- package/dist/service/chat/review-delivery.js +4 -4
- package/dist/service/chat/routes.d.ts +2 -2
- package/dist/service/chat/routes.js +7 -7
- package/dist/service/errors.js +3 -3
- package/dist/service/mcp/server.d.ts +3 -3
- package/dist/service/mcp/server.js +15 -15
- package/dist/service/mcp/tools.d.ts +2 -2
- package/dist/service/mcp/tools.js +12 -14
- package/dist/service/member-file-routes.js +26 -2
- package/dist/service/member-files.d.ts +7 -7
- package/dist/service/member-files.js +2 -2
- package/dist/service/service.js +9 -9
- package/dist/storage/attachments.d.ts +3 -3
- package/dist/storage/attachments.js +16 -15
- package/dist/storage/tables.d.ts +4 -0
- package/dist/storage/tables.js +4 -0
- package/package.json +1 -1
|
@@ -17,8 +17,8 @@ export type ChatDeliveryFactory = (input: {
|
|
|
17
17
|
readonly envelope?: RuntimeChatEnvelope;
|
|
18
18
|
}) => ChatDelivery["Service"];
|
|
19
19
|
/**
|
|
20
|
-
* Whether a post would land in the
|
|
21
|
-
* person, not the
|
|
20
|
+
* Whether a post would land in the chat it was asked from. A private destination names the
|
|
21
|
+
* person, not the chat: a Slack direct message is `D…` while the person is `U…`, so a
|
|
22
22
|
* private source matches a private destination on the same provider by its `peer`.
|
|
23
23
|
*/
|
|
24
24
|
export declare function sameChat(source: NonNullable<RuntimeChatEnvelope["source"]>, target: Pick<Destination, "provider" | "external_id" | "kind">): boolean;
|
|
@@ -13,8 +13,8 @@ import { reviewSender } from "./review-delivery.js";
|
|
|
13
13
|
/** The channels a runtime source can name, which a verified pairing's provider is one of. */
|
|
14
14
|
const CHANNEL_PROVIDERS = new Set(["telegram", "slack", "whatsapp"]);
|
|
15
15
|
/**
|
|
16
|
-
* Whether a post would land in the
|
|
17
|
-
* person, not the
|
|
16
|
+
* Whether a post would land in the chat it was asked from. A private destination names the
|
|
17
|
+
* person, not the chat: a Slack direct message is `D…` while the person is `U…`, so a
|
|
18
18
|
* private source matches a private destination on the same provider by its `peer`.
|
|
19
19
|
*/
|
|
20
20
|
export function sameChat(source, target) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ChatPreview } from "../../chat.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ChatRequester } from "./attribution.js";
|
|
3
3
|
interface DeliveryScopeIdentifiers {
|
|
4
|
-
readonly via:
|
|
4
|
+
readonly via: ChatRequester["via"];
|
|
5
5
|
readonly subjectId: string;
|
|
6
6
|
readonly clientId: string | null;
|
|
7
7
|
}
|
|
@@ -15,5 +15,5 @@ interface DeliveryScopeIdentifiers {
|
|
|
15
15
|
export declare function deliveryScopeDigest(identifiers: DeliveryScopeIdentifiers): string;
|
|
16
16
|
export declare const deliveryKeyDigest: (idempotencyKey: string) => string;
|
|
17
17
|
/** Bind the exact post and public attribution fields; ignore unrelated record properties. */
|
|
18
|
-
export declare function deliveryFingerprint(preview: ChatPreview, requester:
|
|
18
|
+
export declare function deliveryFingerprint(preview: ChatPreview, requester: ChatRequester): string;
|
|
19
19
|
export {};
|
|
@@ -3,12 +3,12 @@ import { Effect } from "effect";
|
|
|
3
3
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
4
4
|
import { CurrentPrincipal } from "../authentication.js";
|
|
5
5
|
import type { OntologyBackend } from "../runtime-contract.js";
|
|
6
|
-
import type {
|
|
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
9
|
/** The providers whose chats are listed, as `listedChatProviders` gives them. */
|
|
10
10
|
readonly providers: ReadonlyArray<string>;
|
|
11
|
-
readonly
|
|
11
|
+
readonly chatHistory?: ChatArchiveTransport;
|
|
12
12
|
readonly attribution?: ChatAttributionSettings;
|
|
13
13
|
}): {
|
|
14
14
|
list: (input: typeof ChatList.Type) => Effect.Effect<{
|
|
@@ -26,5 +26,5 @@ export declare function chatReads(backend: OntologyBackend["Service"], options:
|
|
|
26
26
|
readonly [x: string]: unknown;
|
|
27
27
|
}[];
|
|
28
28
|
readonly nextCursor: string | null;
|
|
29
|
-
}, ActionRejected | PermissionDenied | import("../agent/
|
|
29
|
+
}, ActionRejected | PermissionDenied | import("../agent/chat-archive.js").ChatArchiveUnavailable, CurrentPrincipal>;
|
|
30
30
|
};
|
|
@@ -4,11 +4,11 @@ import { isAllowed } from "../../permissions.js";
|
|
|
4
4
|
import { CurrentPrincipal } from "../authentication.js";
|
|
5
5
|
import { currentCaller } from "../caller-context.js";
|
|
6
6
|
import { chatAttribution } from "./attribution.js";
|
|
7
|
-
import { chatResolver,
|
|
8
|
-
/** The providers whose archived
|
|
7
|
+
import { chatResolver, groupChatKey, isChatMember } from "./resolve.js";
|
|
8
|
+
/** The providers whose archived chats are keyed by the chat a destination names. */
|
|
9
9
|
const HISTORY_PROVIDERS = ["telegram"];
|
|
10
10
|
export function chatReads(backend, options) {
|
|
11
|
-
const { providers,
|
|
11
|
+
const { providers, chatHistory, attribution } = options;
|
|
12
12
|
return {
|
|
13
13
|
list: (input) => Effect.gen(function* () {
|
|
14
14
|
const principal = yield* CurrentPrincipal;
|
|
@@ -40,9 +40,9 @@ export function chatReads(backend, options) {
|
|
|
40
40
|
history: (input, identityToken) => Effect.gen(function* () {
|
|
41
41
|
const principal = yield* CurrentPrincipal;
|
|
42
42
|
if (principal.kind !== "member") {
|
|
43
|
-
return yield* new PermissionDenied({ message: "Only members may read
|
|
43
|
+
return yield* new PermissionDenied({ message: "Only members may read chatHistory." });
|
|
44
44
|
}
|
|
45
|
-
// History reads Telegram chats only. Other channels archive a
|
|
45
|
+
// History reads Telegram chats only. Other channels archive a chat under their
|
|
46
46
|
// session's address with private coordinates, not under the chat a destination names, so
|
|
47
47
|
// their history would be incomplete until the archive keys them the same way.
|
|
48
48
|
const target = yield* chatResolver(backend, principal, HISTORY_PROVIDERS, {
|
|
@@ -57,15 +57,15 @@ export function chatReads(backend, options) {
|
|
|
57
57
|
? !isAllowed("agent_chat:read", principal) && !member
|
|
58
58
|
: !target.participantIds.includes(principal.id)) {
|
|
59
59
|
return yield* new PermissionDenied({
|
|
60
|
-
message: "This
|
|
60
|
+
message: "This chat is not readable by the caller.",
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
|
-
if (!
|
|
63
|
+
if (!chatHistory) {
|
|
64
64
|
return yield* new ActionRejected({
|
|
65
|
-
message: "The
|
|
65
|
+
message: "The chat archive is not configured.",
|
|
66
66
|
});
|
|
67
67
|
}
|
|
68
|
-
return yield*
|
|
68
|
+
return yield* chatHistory({
|
|
69
69
|
principal,
|
|
70
70
|
identityToken,
|
|
71
71
|
search: {
|
|
@@ -74,7 +74,7 @@ export function chatReads(backend, options) {
|
|
|
74
74
|
...(input.before === undefined ? {} : { before: input.before }),
|
|
75
75
|
...(input.limit === undefined ? {} : { limit: input.limit }),
|
|
76
76
|
},
|
|
77
|
-
memberGroups: member ? [
|
|
77
|
+
memberGroups: member ? [groupChatKey(target)] : [],
|
|
78
78
|
});
|
|
79
79
|
}),
|
|
80
80
|
};
|
|
@@ -2,7 +2,7 @@ import { Effect } from "effect";
|
|
|
2
2
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
3
3
|
import type { Principal } from "../../runtime.js";
|
|
4
4
|
import type { OntologyBackend } from "../runtime-contract.js";
|
|
5
|
-
import type {
|
|
5
|
+
import type { ChatArchiveTransport } from "../agent/chat-archive.js";
|
|
6
6
|
export interface Destination {
|
|
7
7
|
readonly id: string;
|
|
8
8
|
readonly title: string;
|
|
@@ -11,20 +11,20 @@ export interface Destination {
|
|
|
11
11
|
readonly kind: "group" | "private";
|
|
12
12
|
readonly participantIds: ReadonlyArray<string>;
|
|
13
13
|
}
|
|
14
|
-
/** A group chat's
|
|
15
|
-
export declare const
|
|
14
|
+
/** A group chat's chat in the archive, as attachments name it: `provider:externalId`. */
|
|
15
|
+
export declare const groupChatKey: (chat: {
|
|
16
16
|
readonly provider?: unknown;
|
|
17
17
|
readonly external_id?: unknown;
|
|
18
18
|
}) => string;
|
|
19
|
-
/** The
|
|
19
|
+
/** The chats of the group chats a member is in; none for any other caller. */
|
|
20
20
|
export declare function memberGroupChats(backend: OntologyBackend["Service"], principal: Principal): Effect.Effect<readonly string[], ActionRejected, never>;
|
|
21
21
|
/** Whether a member is in one group chat, which lets them read its history without agent_chat:read. */
|
|
22
22
|
export declare function isChatMember(backend: OntologyBackend["Service"], principal: Principal, chatId: string): Effect.Effect<boolean, ActionRejected, never>;
|
|
23
23
|
/**
|
|
24
|
-
* A
|
|
24
|
+
* A chat transport that also reads the group chats the member is in, the way a member
|
|
25
25
|
* reads those chats' attachments: being in a chat is enough, `agent_chat:read` reads them all.
|
|
26
26
|
*/
|
|
27
|
-
export declare function withChatMembership(transport:
|
|
27
|
+
export declare function withChatMembership(transport: ChatArchiveTransport, backend: OntologyBackend["Service"]): ChatArchiveTransport;
|
|
28
28
|
/**
|
|
29
29
|
* Resolves the chats of `providers`: their group chats in the registry, and each member's private
|
|
30
30
|
* chat on each provider they have a live identity on, `<provider>:private:<member id>`.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
3
3
|
import { isAllowed } from "../../permissions.js";
|
|
4
|
-
import {
|
|
4
|
+
import { ChatArchiveUnavailable } from "../agent/chat-archive.js";
|
|
5
5
|
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
6
6
|
const denied = () => new PermissionDenied({ message: "Chat discovery requires chat read or post permission." });
|
|
7
7
|
/** Exhaust exact matches, with a fail-closed scan budget so a later page cannot hide ambiguity. */
|
|
@@ -31,15 +31,15 @@ function records(backend, entity, query) {
|
|
|
31
31
|
const memberOf = (principal) => ({
|
|
32
32
|
members: { some: { id: { eq: principal.id } } },
|
|
33
33
|
});
|
|
34
|
-
/** A group chat's
|
|
35
|
-
export const
|
|
36
|
-
/** The
|
|
34
|
+
/** A group chat's chat in the archive, as attachments name it: `provider:externalId`. */
|
|
35
|
+
export const groupChatKey = (chat) => `${String(chat.provider)}:${String(chat.external_id)}`;
|
|
36
|
+
/** The chats of the group chats a member is in; none for any other caller. */
|
|
37
37
|
export function memberGroupChats(backend, principal) {
|
|
38
38
|
return Effect.gen(function* () {
|
|
39
39
|
if (principal.kind !== "member") {
|
|
40
40
|
return [];
|
|
41
41
|
}
|
|
42
|
-
return (yield* records(backend, "agent_chat", { where: memberOf(principal) })).map(
|
|
42
|
+
return (yield* records(backend, "agent_chat", { where: memberOf(principal) })).map(groupChatKey);
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
/** Whether a member is in one group chat, which lets them read its history without agent_chat:read. */
|
|
@@ -54,13 +54,13 @@ export function isChatMember(backend, principal, chatId) {
|
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
|
-
* A
|
|
57
|
+
* A chat transport that also reads the group chats the member is in, the way a member
|
|
58
58
|
* reads those chats' attachments: being in a chat is enough, `agent_chat:read` reads them all.
|
|
59
59
|
*/
|
|
60
60
|
export function withChatMembership(transport, backend) {
|
|
61
61
|
return (input) => input.memberGroups !== undefined || input.principal.kind !== "member"
|
|
62
62
|
? transport(input)
|
|
63
|
-
: memberGroupChats(backend, input.principal).pipe(Effect.mapError((cause) => new
|
|
63
|
+
: memberGroupChats(backend, input.principal).pipe(Effect.mapError((cause) => new ChatArchiveUnavailable({
|
|
64
64
|
diagnostic: "chat_membership_unavailable",
|
|
65
65
|
cause,
|
|
66
66
|
})), Effect.flatMap((memberGroups) => transport({ ...input, memberGroups })));
|
|
@@ -9,7 +9,7 @@ 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
|
|
12
|
+
* edit, to the person's chat through the ontology's chat connection for its provider, as
|
|
13
13
|
* 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
|
|
@@ -7,7 +7,7 @@ import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./i
|
|
|
7
7
|
import { chatConnection, chatProvider } 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
|
|
10
|
+
* edit, to the person's chat through the ontology's chat connection for its provider, as
|
|
11
11
|
* 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
|
|
@@ -72,12 +72,12 @@ export function reviewSender(input) {
|
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
const kind = review.chat_kind === "group" ? "group" : "private";
|
|
75
|
-
// The archive keys the
|
|
76
|
-
// differs where a channel names a
|
|
75
|
+
// The archive keys the chat by its chat id; the provider takes the recipient, which
|
|
76
|
+
// differs where a channel names a chat otherwise, as WhatsApp's threads do.
|
|
77
77
|
const chatId = String(review.chat_id);
|
|
78
78
|
const externalId = typeof review.recipient === "string" && review.recipient !== "" ? review.recipient : chatId;
|
|
79
79
|
const threadId = typeof review.thread_id === "string" && review.thread_id !== "" ? review.thread_id : null;
|
|
80
|
-
// A private
|
|
80
|
+
// A private chat has one person: the reply needs no quote of their message, which
|
|
81
81
|
// they may have deleted since.
|
|
82
82
|
const replyTo = kind === "group" && typeof review.reply_to === "string" && review.reply_to !== ""
|
|
83
83
|
? review.reply_to
|
|
@@ -8,7 +8,7 @@ import type { BuiltApi } from "../api.js";
|
|
|
8
8
|
import type { AfterCommitHook } from "../config.js";
|
|
9
9
|
import type { ConnectionTransportFactory } from "../connections.js";
|
|
10
10
|
import type { ReplicationKick } from "../replication.js";
|
|
11
|
-
import { type
|
|
11
|
+
import { type ChatArchiveTransport } from "../agent/chat-archive.js";
|
|
12
12
|
import type { ChatDeliveryFactory } from "./delivery.js";
|
|
13
13
|
export declare function chatRoutes(options: {
|
|
14
14
|
readonly built: BuiltApi;
|
|
@@ -16,7 +16,7 @@ export declare function chatRoutes(options: {
|
|
|
16
16
|
readonly connections: ConnectionTransportFactory;
|
|
17
17
|
readonly replicate: ReplicationKick;
|
|
18
18
|
readonly chatDelivery: ChatDeliveryFactory;
|
|
19
|
-
readonly
|
|
19
|
+
readonly chatHistory?: ChatArchiveTransport;
|
|
20
20
|
readonly verifyToken: TokenVerifier;
|
|
21
21
|
readonly verifyRuntime?: RuntimeTokenVerifier;
|
|
22
22
|
readonly exchangeSession?: SessionExchange;
|
|
@@ -7,7 +7,7 @@ import { runtimeCredential } from "../runtime-identity.js";
|
|
|
7
7
|
import { RequestId, failureResponse } from "../errors.js";
|
|
8
8
|
import { OntologyBackend } from "../runtime-contract.js";
|
|
9
9
|
import { identityToken, makeOperations } from "../operations.js";
|
|
10
|
-
import {
|
|
10
|
+
import { ChatSearch } from "../agent/chat-archive.js";
|
|
11
11
|
import { listedChatProviders } from "./providers/index.js";
|
|
12
12
|
import { chatReads } from "./reads.js";
|
|
13
13
|
import { withChatMembership } from "./resolve.js";
|
|
@@ -16,10 +16,10 @@ export function chatRoutes(options) {
|
|
|
16
16
|
return Layer.effectDiscard(Effect.gen(function* () {
|
|
17
17
|
const router = yield* HttpRouter.HttpRouter;
|
|
18
18
|
const backend = yield* OntologyBackend;
|
|
19
|
-
const
|
|
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
|
-
|
|
22
|
+
chatHistory,
|
|
23
23
|
attribution: options.built.plan.ontology.config.chat?.attribution,
|
|
24
24
|
});
|
|
25
25
|
const operations = makeOperations(backend, options.built.plan, options.hooks, options.connections, options.replicate, options.chatDelivery);
|
|
@@ -41,13 +41,13 @@ export function chatRoutes(options) {
|
|
|
41
41
|
result = yield* reads.history(yield* decode(ChatHistory), identityToken(request));
|
|
42
42
|
break;
|
|
43
43
|
case "search": {
|
|
44
|
-
if (!
|
|
44
|
+
if (!chatHistory) {
|
|
45
45
|
return yield* new ActionRejected({
|
|
46
|
-
message: "The
|
|
46
|
+
message: "The chat archive is not configured.",
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
|
-
result = yield*
|
|
50
|
-
search: yield* decode(
|
|
49
|
+
result = yield* chatHistory({
|
|
50
|
+
search: yield* decode(ChatSearch),
|
|
51
51
|
principal: yield* CurrentPrincipal,
|
|
52
52
|
identityToken: identityToken(request),
|
|
53
53
|
});
|
package/dist/service/errors.js
CHANGED
|
@@ -620,12 +620,12 @@ export function expectedFailure(error, requestId) {
|
|
|
620
620
|
},
|
|
621
621
|
};
|
|
622
622
|
}
|
|
623
|
-
if (Predicate.isTagged(error, "
|
|
623
|
+
if (Predicate.isTagged(error, "ChatArchiveUnavailable")) {
|
|
624
624
|
return {
|
|
625
625
|
status: 502,
|
|
626
626
|
body: {
|
|
627
|
-
error: "
|
|
628
|
-
detail: "The
|
|
627
|
+
error: "chat_archive_unavailable",
|
|
628
|
+
detail: "The chat archive is unavailable. Try again shortly.",
|
|
629
629
|
requestId,
|
|
630
630
|
retryable: true,
|
|
631
631
|
},
|
|
@@ -7,7 +7,7 @@ import type { BuiltApi } from "../api.js";
|
|
|
7
7
|
import { type TokenVerifier } from "../authentication.js";
|
|
8
8
|
import type { PageCatalogV1 } from "../../navigation-catalog.js";
|
|
9
9
|
import type { AfterCommitHook } from "../config.js";
|
|
10
|
-
import { type
|
|
10
|
+
import { type ChatArchiveTransport } from "../agent/chat-archive.js";
|
|
11
11
|
import type { ConnectionTransportFactory } from "../connections.js";
|
|
12
12
|
import { RequestId } from "../errors.js";
|
|
13
13
|
import type { ReplicationKick } from "../replication.js";
|
|
@@ -20,7 +20,7 @@ export declare const MCP_PROTOCOL_VERSIONS: readonly ["2025-11-25", "2025-06-18"
|
|
|
20
20
|
export declare const MCP_STREAM_MAX_DURATION_MS = 45000;
|
|
21
21
|
/**
|
|
22
22
|
* When to offer feedback, for a caller who may send it. Platform guidance for the agent; the agent
|
|
23
|
-
* words the offer itself, in the
|
|
23
|
+
* words the offer itself, in the chat's language.
|
|
24
24
|
*/
|
|
25
25
|
export declare const FEEDBACK_INSTRUCTIONS: string;
|
|
26
26
|
export declare const AUTOMATIC_FAILURE_REPORT_INSTRUCTIONS: string;
|
|
@@ -39,7 +39,7 @@ export interface McpServerOptions {
|
|
|
39
39
|
readonly metricQueries: MetricQueries;
|
|
40
40
|
/** Authenticated exact-text delivery for the built-in chat action. */
|
|
41
41
|
readonly chatDelivery?: ChatDeliveryFactory | undefined;
|
|
42
|
-
readonly
|
|
42
|
+
readonly chatHistory?: ChatArchiveTransport | undefined;
|
|
43
43
|
/** The workspace whose workflows' tasks authenticate here; without it the task header is refused. */
|
|
44
44
|
readonly workspaceId?: string | undefined;
|
|
45
45
|
/** How `discover` asks the ontology's decision model: AI Gateway by default. Tests replace it. */
|
|
@@ -23,7 +23,7 @@ import { suggestNames, suggestionDetail } from "../name-suggestions.js";
|
|
|
23
23
|
import { DiscoverInput } from "../discovery-schemas.js";
|
|
24
24
|
import { NavigationInput } from "../navigation-schemas.js";
|
|
25
25
|
import { createNavigation, NavigationError } from "../navigation.js";
|
|
26
|
-
import {
|
|
26
|
+
import { ChatSearch } from "../agent/chat-archive.js";
|
|
27
27
|
import { GetDimensionsInput, GetDimensionValuesInput, ListMetricsInput, listMetrics, metricDimensions, planDimensionValues, VALUES_DEFAULT, } from "./metric-tools.js";
|
|
28
28
|
import { RequestId, errorBodyForWire, expectedFailure, internalError, isStorageFailure, schemaIssues, storageUnavailable, } from "../errors.js";
|
|
29
29
|
import { publicOrigin } from "../oauth/protocol.js";
|
|
@@ -277,7 +277,7 @@ function toolFailure(cause, requestId) {
|
|
|
277
277
|
}));
|
|
278
278
|
}
|
|
279
279
|
function runTool(server, tool, args, request, preview) {
|
|
280
|
-
const { built, operations, queryMetrics,
|
|
280
|
+
const { built, operations, queryMetrics, chatHistory, discovery, memberFiles, chats } = server;
|
|
281
281
|
return Effect.gen(function* () {
|
|
282
282
|
const { target } = tool;
|
|
283
283
|
const caller = yield* currentCaller;
|
|
@@ -440,15 +440,15 @@ function runTool(server, tool, args, request, preview) {
|
|
|
440
440
|
}
|
|
441
441
|
case "member-file":
|
|
442
442
|
return structured(yield* memberFiles(target.operation, args, identityToken(request)));
|
|
443
|
-
case "
|
|
444
|
-
if (!
|
|
443
|
+
case "chat-search": {
|
|
444
|
+
if (!chatHistory) {
|
|
445
445
|
return yield* new ActionRejected({
|
|
446
|
-
message: "The
|
|
446
|
+
message: "The chat archive is not configured.",
|
|
447
447
|
});
|
|
448
448
|
}
|
|
449
|
-
const search = yield* decodeArguments(
|
|
449
|
+
const search = yield* decodeArguments(ChatSearch, args);
|
|
450
450
|
const principal = yield* CurrentPrincipal;
|
|
451
|
-
return structured(yield*
|
|
451
|
+
return structured(yield* chatHistory({ search, principal, identityToken: identityToken(request) }));
|
|
452
452
|
}
|
|
453
453
|
case "history": {
|
|
454
454
|
const { id, ...params } = yield* decodeArguments(HistoryArguments, args);
|
|
@@ -619,7 +619,7 @@ function readResource({ built, tools }, uri) {
|
|
|
619
619
|
}
|
|
620
620
|
/**
|
|
621
621
|
* When to offer feedback, for a caller who may send it. Platform guidance for the agent; the agent
|
|
622
|
-
* words the offer itself, in the
|
|
622
|
+
* words the offer itself, in the chat's language.
|
|
623
623
|
*/
|
|
624
624
|
export const FEEDBACK_INSTRUCTIONS = "Feedback: this app accepts feedback for its builders. Offer to send it when the user wants " +
|
|
625
625
|
"something no permitted tool, field or data provides, an operation is refused for lack of " +
|
|
@@ -628,7 +628,7 @@ export const FEEDBACK_INSTRUCTIONS = "Feedback: this app accepts feedback for it
|
|
|
628
628
|
"once per problem. Before submitting, look for the user's open app_feedback with the same " +
|
|
629
629
|
"subject. Submit with app_feedback_submit only after the user agrees to the title and summary you " +
|
|
630
630
|
"show in their language. At the " +
|
|
631
|
-
"start of a
|
|
631
|
+
"start of a chat, find the user's app_feedback with responded_at set and seen_at empty, " +
|
|
632
632
|
"tell them the builders' response, then call app_feedback_acknowledge with its responded_at.";
|
|
633
633
|
export const AUTOMATIC_FAILURE_REPORT_INSTRUCTIONS = "Feedback: During requested work, this workspace preauthorizes minimal technical failure reports. " +
|
|
634
634
|
"Automatically record unexpected or persistent tool/connection failures through app_feedback_submit; " +
|
|
@@ -637,7 +637,7 @@ export const AUTOMATIC_FAILURE_REPORT_INSTRUCTIONS = "Feedback: During requested
|
|
|
637
637
|
"reason and that the report was recorded in one or two sentences; do not merely offer to report it, " +
|
|
638
638
|
"and never claim it was recorded before a successful submission or existing-item read. " +
|
|
639
639
|
"Feature/change requests and sharing task contents or files still require the member's agreement " +
|
|
640
|
-
"to the title and summary. At
|
|
640
|
+
"to the title and summary. At chat start, read the caller's unseen builder responses and " +
|
|
641
641
|
"acknowledge only the response you have relayed.";
|
|
642
642
|
/**
|
|
643
643
|
* Fixed guidance keeps initialization independent of ontology size: the feedback paragraph depends
|
|
@@ -663,7 +663,7 @@ function instructions(feedback, files, automaticFailureReports, byMeaning, metri
|
|
|
663
663
|
"Use the authenticated tool annotations for confirmation: permitted actions classified as " +
|
|
664
664
|
"non-destructive and reversible need no extra approval. Destructive, irreversible or " +
|
|
665
665
|
"external actions (openWorldHint) require confirmation before preparation; idempotence " +
|
|
666
|
-
"alone does not waive it. Present confirmations in the
|
|
666
|
+
"alone does not waive it. Present confirmations in the chat's language using " +
|
|
667
667
|
"action, entity, field and option labels and readable record titles, never tool names, ids or JSON.",
|
|
668
668
|
...(feedback
|
|
669
669
|
? [
|
|
@@ -841,16 +841,16 @@ export function mcpLayer(options) {
|
|
|
841
841
|
const router = yield* HttpRouter.HttpRouter;
|
|
842
842
|
const backend = yield* OntologyBackend;
|
|
843
843
|
const warehouseQueries = yield* Effect.serviceOption(WarehouseQueries);
|
|
844
|
-
const tools = mcpTools(options.built, options.
|
|
844
|
+
const tools = mcpTools(options.built, options.chatHistory !== undefined, {
|
|
845
845
|
attachments: backend.attachments !== undefined,
|
|
846
846
|
files: backend.blobs !== undefined,
|
|
847
847
|
}, options.navigation !== undefined);
|
|
848
848
|
const operations = makeOperations(backend, options.built.plan, options.hooks, options.connections, options.replicate, options.chatDelivery);
|
|
849
|
-
const
|
|
849
|
+
const chatHistory = options.chatHistory && withChatMembership(options.chatHistory, backend);
|
|
850
850
|
const server = {
|
|
851
851
|
chats: chatReads(backend, {
|
|
852
852
|
providers: listedChatProviders(options.built.plan.ontology.manifest.chat),
|
|
853
|
-
|
|
853
|
+
chatHistory,
|
|
854
854
|
attribution: options.built.plan.ontology.config.chat?.attribution,
|
|
855
855
|
}),
|
|
856
856
|
built: options.built,
|
|
@@ -864,7 +864,7 @@ export function mcpLayer(options) {
|
|
|
864
864
|
operations,
|
|
865
865
|
memberFiles: memberFileOperations(backend, options.built.plan, operations),
|
|
866
866
|
queryMetrics: options.metricQueries.model,
|
|
867
|
-
|
|
867
|
+
chatHistory,
|
|
868
868
|
decide: options.decide,
|
|
869
869
|
};
|
|
870
870
|
const post = handlePost(server, options.verifyToken, options.verifyRuntime, backend, options.workspaceId);
|
|
@@ -36,7 +36,7 @@ export type McpToolTarget = {
|
|
|
36
36
|
} | {
|
|
37
37
|
readonly kind: "list-metrics" | "get-dimensions" | "get-dimension-values";
|
|
38
38
|
} | {
|
|
39
|
-
readonly kind: "
|
|
39
|
+
readonly kind: "chat-search";
|
|
40
40
|
} | {
|
|
41
41
|
readonly kind: "chat-list" | "chat-history";
|
|
42
42
|
} | {
|
|
@@ -93,7 +93,7 @@ export declare const CHAT_POST_META = "segmentstream/chat-post";
|
|
|
93
93
|
* as they have no record routes. Throws {@link InvalidOntology} when two tools share a name, which
|
|
94
94
|
* `check` already refuses.
|
|
95
95
|
*/
|
|
96
|
-
export declare function mcpTools(built: BuiltApi,
|
|
96
|
+
export declare function mcpTools(built: BuiltApi, chatHistory?: boolean, storage?: {
|
|
97
97
|
attachments?: boolean;
|
|
98
98
|
files?: boolean;
|
|
99
99
|
}, navigation?: boolean): ReadonlyArray<McpTool>;
|
|
@@ -8,7 +8,7 @@ import { metricDescription, metricsFor, modelMetricDescription, modelMetricsFor,
|
|
|
8
8
|
import { actionInputJsonSchema } from "../../runtime/generated-actions.js";
|
|
9
9
|
import { uploadFields } from "../../storage/attachments.js";
|
|
10
10
|
import { MEMBER_FILE_SCHEMAS } from "../member-files.js";
|
|
11
|
-
import {
|
|
11
|
+
import { ChatSearch } from "../agent/chat-archive.js";
|
|
12
12
|
import { declaredChatProviders } from "../chat/providers/index.js";
|
|
13
13
|
import { runtimeAllows } from "./runtime-policy.js";
|
|
14
14
|
import { confirmationFacts, needsConfirmation, withConfirmation, } from "./confirmation.js";
|
|
@@ -709,7 +709,7 @@ function metricTools(manifest) {
|
|
|
709
709
|
* as they have no record routes. Throws {@link InvalidOntology} when two tools share a name, which
|
|
710
710
|
* `check` already refuses.
|
|
711
711
|
*/
|
|
712
|
-
export function mcpTools(built,
|
|
712
|
+
export function mcpTools(built, chatHistory = false, storage = {}, navigation = false) {
|
|
713
713
|
const { manifest } = built.plan.ontology;
|
|
714
714
|
const tools = [
|
|
715
715
|
...GLOBAL_TOOLS,
|
|
@@ -798,8 +798,8 @@ export function mcpTools(built, conversations = false, storage = {}, navigation
|
|
|
798
798
|
});
|
|
799
799
|
tools.push({
|
|
800
800
|
name: "chat_history",
|
|
801
|
-
title: "Read chat
|
|
802
|
-
description: "Read messages for an exact chat ID/title, with
|
|
801
|
+
title: "Read chat history",
|
|
802
|
+
description: "Read messages for an exact chat ID/title, with their own pagination. " +
|
|
803
803
|
"Reads a group chat you are in (every group with agent_chat:read) or your own private chat.",
|
|
804
804
|
inputSchema: encodedJsonSchema(ChatHistory),
|
|
805
805
|
annotations: READ,
|
|
@@ -809,24 +809,22 @@ export function mcpTools(built, conversations = false, storage = {}, navigation
|
|
|
809
809
|
});
|
|
810
810
|
tools.push({
|
|
811
811
|
name: "chat_search",
|
|
812
|
-
title: "Search stored
|
|
813
|
-
description:
|
|
814
|
-
? "Search retained
|
|
812
|
+
title: "Search stored chats",
|
|
813
|
+
description: chatHistory
|
|
814
|
+
? "Search retained chat messages, decisions and tool calls by text, provider chat ID, " +
|
|
815
815
|
"sender ID, topic and date range. Returns the group chats you are in (every group with " +
|
|
816
816
|
"agent_chat:read) and DMs in which you participated. Dates are ISO 8601; before is the " +
|
|
817
817
|
"nextCursor of the previous page. " +
|
|
818
818
|
"Workspace administrators can read all DMs through the platform API."
|
|
819
|
-
: "Search retained
|
|
820
|
-
inputSchema: encodedJsonSchema(
|
|
819
|
+
: "Search retained chat messages. Requires an available archive and chat archive read access.",
|
|
820
|
+
inputSchema: encodedJsonSchema(ChatSearch),
|
|
821
821
|
annotations: READ,
|
|
822
822
|
permissions: [],
|
|
823
|
-
target: { kind: "
|
|
824
|
-
inputSchemaFor: (caller) => "kind" in caller && caller.kind === "member"
|
|
825
|
-
? encodedJsonSchema(ConversationSearch)
|
|
826
|
-
: undefined,
|
|
823
|
+
target: { kind: "chat-search" },
|
|
824
|
+
inputSchemaFor: (caller) => "kind" in caller && caller.kind === "member" ? encodedJsonSchema(ChatSearch) : undefined,
|
|
827
825
|
});
|
|
828
826
|
const descriptions = {
|
|
829
|
-
"attachments-list": "List chat attachments by
|
|
827
|
+
"attachments-list": "List chat attachments by chat (provider:chat ID), sender member UUID and ISO time range. " +
|
|
830
828
|
"Returns a bounded page and nextCursor; use before to resume. Includes expired metadata. " +
|
|
831
829
|
"Administrators with agent_chat:update may access any workspace chat; active group participants " +
|
|
832
830
|
"and DM senders may access their files.",
|
|
@@ -18,9 +18,14 @@ export function memberFileRoutes(options) {
|
|
|
18
18
|
const params = yield* HttpRouter.params;
|
|
19
19
|
let args = params;
|
|
20
20
|
if (kind === "attachments-list") {
|
|
21
|
-
|
|
21
|
+
// @legacy ontology.attachment-conversation-field
|
|
22
|
+
// CLIs before `chat` filter by `conversation` and print each attachment's.
|
|
23
|
+
const { conversation, ...query } = Object.fromEntries(new URL(request.url, "https://app.invalid").searchParams);
|
|
22
24
|
args = {
|
|
23
25
|
...query,
|
|
26
|
+
...(query.chat === undefined && conversation !== undefined
|
|
27
|
+
? { chat: conversation }
|
|
28
|
+
: {}),
|
|
24
29
|
...(query.limit === undefined ? {} : { limit: Number(query.limit) }),
|
|
25
30
|
};
|
|
26
31
|
}
|
|
@@ -32,7 +37,7 @@ export function memberFileRoutes(options) {
|
|
|
32
37
|
: body;
|
|
33
38
|
}
|
|
34
39
|
const answer = yield* run(kind, args, identityToken(request));
|
|
35
|
-
return HttpServerResponse.jsonUnsafe(answer, {
|
|
40
|
+
return HttpServerResponse.jsonUnsafe(withEarlierChatField(kind, answer), {
|
|
36
41
|
headers: { "cache-control": "private, no-store" },
|
|
37
42
|
});
|
|
38
43
|
}).pipe(Effect.catchCause((cause) => Cause.hasInterruptsOnly(cause)
|
|
@@ -50,3 +55,22 @@ export function memberFileRoutes(options) {
|
|
|
50
55
|
yield* router.add("GET", "/api/ontology/files/:entity/:id", route("files-download"));
|
|
51
56
|
}));
|
|
52
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* @legacy ontology.attachment-conversation-field
|
|
60
|
+
* Each listed attachment's chat also under `conversation`, which CLIs before `chat` print.
|
|
61
|
+
*/
|
|
62
|
+
function withEarlierChatField(kind, answer) {
|
|
63
|
+
if (kind !== "attachments-list" || typeof answer !== "object" || answer === null) {
|
|
64
|
+
return answer;
|
|
65
|
+
}
|
|
66
|
+
const list = answer;
|
|
67
|
+
return list.attachments === undefined
|
|
68
|
+
? answer
|
|
69
|
+
: {
|
|
70
|
+
...answer,
|
|
71
|
+
attachments: list.attachments.map((attachment) => ({
|
|
72
|
+
...attachment,
|
|
73
|
+
conversation: attachment.chat,
|
|
74
|
+
})),
|
|
75
|
+
};
|
|
76
|
+
}
|