@segmentstream/ontology 0.98.1 → 0.99.1
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 +1246 -0
- package/README.md +8 -5
- package/dist/agent-review.d.ts +4 -4
- package/dist/agent-review.js +10 -10
- package/dist/analyze.js +40 -5
- package/dist/app-feedback.js +1 -1
- package/dist/feedback-config.js +17 -8
- 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 +2 -1
|
@@ -3,7 +3,7 @@ import { PermissionDenied } from "../../errors.js";
|
|
|
3
3
|
import { isAllowed } from "../../permissions.js";
|
|
4
4
|
import { describeError } from "../metric-query-platform.js";
|
|
5
5
|
const Filter = Schema.String.check(Schema.isMaxLength(1000));
|
|
6
|
-
export const
|
|
6
|
+
export const ChatSearch = Schema.Struct({
|
|
7
7
|
q: Schema.optionalKey(Filter),
|
|
8
8
|
provider: Schema.optionalKey(Filter),
|
|
9
9
|
chatId: Schema.optionalKey(Filter),
|
|
@@ -14,28 +14,28 @@ export const ConversationSearch = Schema.Struct({
|
|
|
14
14
|
before: Schema.optionalKey(Filter),
|
|
15
15
|
limit: Schema.optionalKey(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 }))),
|
|
16
16
|
});
|
|
17
|
-
export const
|
|
17
|
+
export const ChatPage = Schema.Struct({
|
|
18
18
|
events: Schema.Array(Schema.Record(Schema.String, Schema.Unknown)),
|
|
19
19
|
nextCursor: Schema.NullOr(Schema.String),
|
|
20
20
|
});
|
|
21
|
-
export class
|
|
21
|
+
export class ChatArchiveUnavailable extends Schema.TaggedError()("ChatArchiveUnavailable", { diagnostic: Schema.String, cause: Schema.optionalKey(Schema.Unknown) }) {
|
|
22
22
|
}
|
|
23
23
|
/** The authenticated ontology service forwards its resolved member, never a caller-supplied ID. */
|
|
24
|
-
export function
|
|
24
|
+
export function platformChatArchive(environment, fetchImpl = fetch) {
|
|
25
25
|
return ({ search, principal, identityToken, memberGroups }) => {
|
|
26
26
|
if (principal.kind !== "member") {
|
|
27
27
|
return Effect.fail(new PermissionDenied({
|
|
28
|
-
message: "Only members may read
|
|
29
|
-
permission: "
|
|
28
|
+
message: "Only members may read chats.",
|
|
29
|
+
permission: "chat:participant",
|
|
30
30
|
}));
|
|
31
31
|
}
|
|
32
32
|
if (!identityToken) {
|
|
33
|
-
return Effect.fail(new
|
|
33
|
+
return Effect.fail(new ChatArchiveUnavailable({ diagnostic: "runtime_identity_missing" }));
|
|
34
34
|
}
|
|
35
35
|
return Effect.tryPromise({
|
|
36
36
|
try: async (signal) => {
|
|
37
37
|
const base = `${environment.segmentstreamUrl}/api/internal/v1/workspaces/`;
|
|
38
|
-
const response = await fetchImpl(`${base}${encodeURIComponent(environment.workspaceId)}/agent/
|
|
38
|
+
const response = await fetchImpl(`${base}${encodeURIComponent(environment.workspaceId)}/agent/chats/search`, {
|
|
39
39
|
method: "POST",
|
|
40
40
|
redirect: "error",
|
|
41
41
|
headers: {
|
|
@@ -55,13 +55,13 @@ export function platformConversations(environment, fetchImpl = fetch) {
|
|
|
55
55
|
signal: AbortSignal.any([signal, AbortSignal.timeout(15_000)]),
|
|
56
56
|
});
|
|
57
57
|
if (!response.ok) {
|
|
58
|
-
throw new Error(`
|
|
58
|
+
throw new Error(`Chat archive returned HTTP ${response.status}; ` +
|
|
59
59
|
`request ${response.headers.get("x-request-id") ?? "unknown"}.`);
|
|
60
60
|
}
|
|
61
|
-
return Schema.decodeUnknownSync(
|
|
61
|
+
return Schema.decodeUnknownSync(ChatPage)(await response.json());
|
|
62
62
|
},
|
|
63
|
-
catch: (cause) => new
|
|
64
|
-
}).pipe(Effect.tapError((error) => Effect.logWarning("ontology.
|
|
63
|
+
catch: (cause) => new ChatArchiveUnavailable({ diagnostic: "archive_request_failed", cause }),
|
|
64
|
+
}).pipe(Effect.tapError((error) => Effect.logWarning("ontology.chat_archive.read_failed").pipe(Effect.annotateLogs({
|
|
65
65
|
diagnostic: error.diagnostic,
|
|
66
66
|
error: describeError(error.cause, (text) => text.split(identityToken).join("[redacted]")),
|
|
67
67
|
}))));
|
|
@@ -103,12 +103,12 @@ export const ReviewRequest = Schema.Struct({
|
|
|
103
103
|
description: "The agent's key for the reply: its session and content part.",
|
|
104
104
|
}),
|
|
105
105
|
provider: Provider,
|
|
106
|
-
chat_id: ChatId.annotate({ description: "The
|
|
106
|
+
chat_id: ChatId.annotate({ description: "The chat's id on the channel." }),
|
|
107
107
|
recipient: ChatId.annotate({
|
|
108
108
|
description: "Whom the provider sends the reply to: the chat's id, or the person's number.",
|
|
109
109
|
}),
|
|
110
110
|
thread_id: Schema.String.check(Schema.isMaxLength(MAX_EXTERNAL_ID_LENGTH)).annotate({
|
|
111
|
-
description: "The
|
|
111
|
+
description: "The chat's thread or topic; empty without one.",
|
|
112
112
|
}),
|
|
113
113
|
reply_to: Schema.NullOr(ChatId).annotate({
|
|
114
114
|
description: "The member's message the reply answers, when the channel replies to one.",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import type { OntologyEnvironment } from "../config.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { ChatRequester } from "./attribution.js";
|
|
4
4
|
export interface DeliveryEvent {
|
|
5
5
|
readonly eventId: string;
|
|
6
6
|
readonly operationId: string;
|
|
@@ -26,7 +26,7 @@ export interface DeliveryEvent {
|
|
|
26
26
|
readonly argsSummary: string | null;
|
|
27
27
|
readonly status: "requested" | "sent" | "uncertain" | "failed";
|
|
28
28
|
readonly attachments: readonly [];
|
|
29
|
-
readonly requestedBy:
|
|
29
|
+
readonly requestedBy: ChatRequester;
|
|
30
30
|
readonly url: string | null;
|
|
31
31
|
readonly at: string;
|
|
32
32
|
}
|
|
@@ -34,7 +34,7 @@ export function platformDeliveryArchive(environment, fetchImpl = fetch) {
|
|
|
34
34
|
}
|
|
35
35
|
const base = `${environment.segmentstreamUrl}/api/internal/v1/workspaces/` +
|
|
36
36
|
encodeURIComponent(environment.workspaceId);
|
|
37
|
-
const response = await fetchImpl(`${base}/agent/
|
|
37
|
+
const response = await fetchImpl(`${base}/agent/chats/events`, {
|
|
38
38
|
method: "POST",
|
|
39
39
|
redirect: "error",
|
|
40
40
|
headers: {
|
|
@@ -15,7 +15,7 @@ export declare function deliveryRequester(caller: CallerContext, principal: Prin
|
|
|
15
15
|
clientName: string | null;
|
|
16
16
|
attribution: string;
|
|
17
17
|
}, never, never>;
|
|
18
|
-
export interface
|
|
18
|
+
export interface ChatRequester {
|
|
19
19
|
readonly id: string;
|
|
20
20
|
readonly kind: "member" | "service";
|
|
21
21
|
readonly name: string;
|
|
@@ -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>;
|