@segmentstream/ontology 0.131.1 → 0.134.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 +66 -0
- package/README.md +10 -6
- package/dist/agent-chat.d.ts +24 -2
- package/dist/agent-chat.js +22 -1
- package/dist/agent-review.d.ts +2 -2
- package/dist/analyze.js +56 -18
- package/dist/app-feedback.js +1 -4
- package/dist/chat.d.ts +40 -0
- package/dist/chat.js +39 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/manifest.d.ts +49 -13
- package/dist/manifest.js +34 -8
- package/dist/migrate/model.d.ts +24 -8
- package/dist/service/agent/bot-check.d.ts +8 -7
- package/dist/service/agent/bot-check.js +10 -6
- package/dist/service/agent/web-tokens.d.ts +2 -2
- package/dist/service/chat/delivery.d.ts +4 -1
- package/dist/service/chat/delivery.js +16 -2
- package/dist/service/chat/envelope.d.ts +2 -2
- package/dist/service/chat/history-import.d.ts +72 -0
- package/dist/service/chat/history-import.js +210 -0
- package/dist/service/config.d.ts +3 -0
- package/dist/service/mcp/feedback-guidance.d.ts +16 -0
- package/dist/service/mcp/feedback-guidance.js +37 -0
- package/dist/service/mcp/server.d.ts +0 -6
- package/dist/service/mcp/server.js +3 -31
- package/dist/service/mcp/tools.js +14 -0
- package/dist/service/operations.js +3 -3
- package/dist/service/schemas.d.ts +6 -2
- package/dist/service/service.js +2 -1
- package/dist/sign-in.d.ts +30 -7
- package/dist/sign-in.js +1 -1
- package/dist/user-identity.d.ts +1 -1
- package/package.json +1 -1
package/dist/manifest.js
CHANGED
|
@@ -491,7 +491,14 @@ export const GuestManifest = Schema.Struct({
|
|
|
491
491
|
channels: Schema.Array(Schema.String),
|
|
492
492
|
origins: Schema.Array(Schema.String),
|
|
493
493
|
/** The challenge a new guest passes, and the workspace secret holding its secret key. */
|
|
494
|
-
botCheck: Schema.optionalKey(Schema.Struct({
|
|
494
|
+
botCheck: Schema.optionalKey(Schema.Struct({
|
|
495
|
+
scheme: Schema.String,
|
|
496
|
+
/** The connection holding the challenge's secret key, and its verification value. */
|
|
497
|
+
connection: Schema.optionalKey(Schema.String),
|
|
498
|
+
input: Schema.optionalKey(Schema.String),
|
|
499
|
+
/** The workspace secret holding it: the older form. */
|
|
500
|
+
secret: Schema.optionalKey(Schema.String),
|
|
501
|
+
})),
|
|
495
502
|
/** Guests may confirm actions that need a confirmation. */
|
|
496
503
|
confirmations: Schema.optionalKey(Schema.Boolean),
|
|
497
504
|
}).pipe(Schema.encodeKeys({ botCheck: "bot_check" }));
|
|
@@ -763,6 +770,12 @@ export function guestOf(manifest) {
|
|
|
763
770
|
})
|
|
764
771
|
: [];
|
|
765
772
|
const check = botCheck;
|
|
773
|
+
const connected = typeof check === "object" &&
|
|
774
|
+
check !== null &&
|
|
775
|
+
typeof check.connection === "string" &&
|
|
776
|
+
check.connection !== "" &&
|
|
777
|
+
typeof check.input === "string" &&
|
|
778
|
+
check.input !== "";
|
|
766
779
|
if (known.length === 0 || websites.length === 0) {
|
|
767
780
|
return undefined;
|
|
768
781
|
}
|
|
@@ -771,8 +784,7 @@ export function guestOf(manifest) {
|
|
|
771
784
|
(typeof check !== "object" ||
|
|
772
785
|
check === null ||
|
|
773
786
|
!isGuestBotCheckScheme(check.scheme) ||
|
|
774
|
-
typeof check.secret !== "string" ||
|
|
775
|
-
check.secret === "")) {
|
|
787
|
+
(!connected && (typeof check.secret !== "string" || check.secret === "")))) {
|
|
776
788
|
return undefined;
|
|
777
789
|
}
|
|
778
790
|
return {
|
|
@@ -780,7 +792,15 @@ export function guestOf(manifest) {
|
|
|
780
792
|
origins: websites,
|
|
781
793
|
...(check === undefined
|
|
782
794
|
? {}
|
|
783
|
-
:
|
|
795
|
+
: connected
|
|
796
|
+
? {
|
|
797
|
+
botCheck: {
|
|
798
|
+
scheme: check.scheme,
|
|
799
|
+
connection: check.connection,
|
|
800
|
+
input: check.input,
|
|
801
|
+
},
|
|
802
|
+
}
|
|
803
|
+
: { botCheck: { scheme: check.scheme, secret: check.secret } }),
|
|
784
804
|
...(confirmations === true ? { confirmations: true } : {}),
|
|
785
805
|
};
|
|
786
806
|
}
|
|
@@ -1055,10 +1075,16 @@ export function buildManifest(input) {
|
|
|
1055
1075
|
...(input.signIn.guest.botCheck === undefined
|
|
1056
1076
|
? {}
|
|
1057
1077
|
: {
|
|
1058
|
-
botCheck:
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1078
|
+
botCheck: input.signIn.guest.botCheck.connection === undefined
|
|
1079
|
+
? {
|
|
1080
|
+
scheme: input.signIn.guest.botCheck.scheme,
|
|
1081
|
+
secret: input.signIn.guest.botCheck.secret,
|
|
1082
|
+
}
|
|
1083
|
+
: {
|
|
1084
|
+
scheme: input.signIn.guest.botCheck.scheme,
|
|
1085
|
+
connection: input.signIn.guest.botCheck.connection,
|
|
1086
|
+
input: input.signIn.guest.botCheck.input,
|
|
1087
|
+
},
|
|
1062
1088
|
}),
|
|
1063
1089
|
...(input.signIn.guest.confirmations === true ? { confirmations: true } : {}),
|
|
1064
1090
|
},
|
package/dist/migrate/model.d.ts
CHANGED
|
@@ -528,7 +528,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
528
528
|
readonly origins: readonly string[];
|
|
529
529
|
readonly botCheck?: {
|
|
530
530
|
readonly scheme: string;
|
|
531
|
-
readonly
|
|
531
|
+
readonly connection?: string | undefined;
|
|
532
|
+
readonly input?: string | undefined;
|
|
533
|
+
readonly secret?: string | undefined;
|
|
532
534
|
} | undefined;
|
|
533
535
|
readonly confirmations?: boolean | undefined;
|
|
534
536
|
} | undefined;
|
|
@@ -879,7 +881,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
879
881
|
readonly origins: readonly string[];
|
|
880
882
|
readonly botCheck?: {
|
|
881
883
|
readonly scheme: string;
|
|
882
|
-
readonly
|
|
884
|
+
readonly connection?: string | undefined;
|
|
885
|
+
readonly input?: string | undefined;
|
|
886
|
+
readonly secret?: string | undefined;
|
|
883
887
|
} | undefined;
|
|
884
888
|
readonly confirmations?: boolean | undefined;
|
|
885
889
|
} | undefined;
|
|
@@ -1307,7 +1311,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
1307
1311
|
readonly confirmations?: boolean | undefined;
|
|
1308
1312
|
readonly bot_check?: {
|
|
1309
1313
|
readonly scheme: string;
|
|
1310
|
-
readonly
|
|
1314
|
+
readonly connection?: string | undefined;
|
|
1315
|
+
readonly input?: string | undefined;
|
|
1316
|
+
readonly secret?: string | undefined;
|
|
1311
1317
|
} | undefined;
|
|
1312
1318
|
} | undefined;
|
|
1313
1319
|
readonly browser?: {
|
|
@@ -1582,7 +1588,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
1582
1588
|
readonly confirmations?: boolean | undefined;
|
|
1583
1589
|
readonly bot_check?: {
|
|
1584
1590
|
readonly scheme: string;
|
|
1585
|
-
readonly
|
|
1591
|
+
readonly connection?: string | undefined;
|
|
1592
|
+
readonly input?: string | undefined;
|
|
1593
|
+
readonly secret?: string | undefined;
|
|
1586
1594
|
} | undefined;
|
|
1587
1595
|
} | undefined;
|
|
1588
1596
|
readonly browser?: {
|
|
@@ -1847,7 +1855,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
1847
1855
|
readonly origins: Schema.$Array<Schema.String>;
|
|
1848
1856
|
readonly botCheck: Schema.optionalKey<Schema.Struct<{
|
|
1849
1857
|
readonly scheme: Schema.String;
|
|
1850
|
-
readonly
|
|
1858
|
+
readonly connection: Schema.optionalKey<Schema.String>;
|
|
1859
|
+
readonly input: Schema.optionalKey<Schema.String>;
|
|
1860
|
+
readonly secret: Schema.optionalKey<Schema.String>;
|
|
1851
1861
|
}>>;
|
|
1852
1862
|
readonly confirmations: Schema.optionalKey<Schema.Boolean>;
|
|
1853
1863
|
}>, {
|
|
@@ -2194,7 +2204,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
2194
2204
|
readonly origins: Schema.$Array<Schema.String>;
|
|
2195
2205
|
readonly botCheck: Schema.optionalKey<Schema.Struct<{
|
|
2196
2206
|
readonly scheme: Schema.String;
|
|
2197
|
-
readonly
|
|
2207
|
+
readonly connection: Schema.optionalKey<Schema.String>;
|
|
2208
|
+
readonly input: Schema.optionalKey<Schema.String>;
|
|
2209
|
+
readonly secret: Schema.optionalKey<Schema.String>;
|
|
2198
2210
|
}>>;
|
|
2199
2211
|
readonly confirmations: Schema.optionalKey<Schema.Boolean>;
|
|
2200
2212
|
}>, {
|
|
@@ -2639,7 +2651,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
2639
2651
|
readonly origins: readonly string[];
|
|
2640
2652
|
readonly botCheck?: {
|
|
2641
2653
|
readonly scheme: string;
|
|
2642
|
-
readonly
|
|
2654
|
+
readonly connection?: string | undefined;
|
|
2655
|
+
readonly input?: string | undefined;
|
|
2656
|
+
readonly secret?: string | undefined;
|
|
2643
2657
|
} | undefined;
|
|
2644
2658
|
readonly confirmations?: boolean | undefined;
|
|
2645
2659
|
} | undefined;
|
|
@@ -2990,7 +3004,9 @@ export declare const MigrationPlan: Schema.Struct<{
|
|
|
2990
3004
|
readonly origins: readonly string[];
|
|
2991
3005
|
readonly botCheck?: {
|
|
2992
3006
|
readonly scheme: string;
|
|
2993
|
-
readonly
|
|
3007
|
+
readonly connection?: string | undefined;
|
|
3008
|
+
readonly input?: string | undefined;
|
|
3009
|
+
readonly secret?: string | undefined;
|
|
2994
3010
|
} | undefined;
|
|
2995
3011
|
readonly confirmations?: boolean | undefined;
|
|
2996
3012
|
} | undefined;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { Redacted } from "effect";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ResolvedGuestBotCheck } from "../../sign-in.js";
|
|
3
3
|
import { type WebhookVerification } from "../webhooks.js";
|
|
4
4
|
/**
|
|
5
5
|
* A website's bot check before a guest exists: the website shows the challenge `guest.botCheck`
|
|
6
6
|
* names, and the visitor's browser sends the challenge's token with its request for a guest's agent
|
|
7
|
-
* token. The ontology never holds the challenge's secret key:
|
|
8
|
-
* SegmentStream's backend checks the token with the provider, as it checks webhook
|
|
7
|
+
* token. The ontology never holds the challenge's secret key: a connection's verification value
|
|
8
|
+
* holds it, and SegmentStream's backend checks the token with the provider, as it checks webhook
|
|
9
|
+
* signatures.
|
|
9
10
|
*/
|
|
10
11
|
/** The longest challenge token the ontology passes on; providers issue shorter ones. */
|
|
11
12
|
export declare const MAX_BOT_CHECK_TOKEN_LENGTH = 2048;
|
|
@@ -15,7 +16,7 @@ export declare const MAX_BOT_CHECK_TOKEN_LENGTH = 2048;
|
|
|
15
16
|
*/
|
|
16
17
|
export type BotCheckVerification = WebhookVerification;
|
|
17
18
|
export interface BotCheckRequest {
|
|
18
|
-
readonly check:
|
|
19
|
+
readonly check: ResolvedGuestBotCheck;
|
|
19
20
|
/** The token the website's challenge gave the visitor. */
|
|
20
21
|
readonly token: string;
|
|
21
22
|
/** The visitor's network address, which the provider compares with the challenge's. */
|
|
@@ -28,9 +29,9 @@ export interface BotCheckRequest {
|
|
|
28
29
|
export type BotCheckVerifier = (request: BotCheckRequest) => Promise<BotCheckVerification>;
|
|
29
30
|
/**
|
|
30
31
|
* The verifier of a deployed ontology service: `POST
|
|
31
|
-
* /api/internal/v1/workspaces/{id}/
|
|
32
|
-
*
|
|
33
|
-
* names. Each check carries a new idempotency key, which the provider takes to tell a retried
|
|
32
|
+
* /api/internal/v1/workspaces/{id}/connections/{key}/inputs/{field}/verify-challenge` (or
|
|
33
|
+
* `.../secrets/{name}/verify-challenge` for the older form) on SegmentStream with the request's
|
|
34
|
+
* Vercel OIDC token, admitted only for the value the deployed ontology's `guest.botCheck` names. Each check carries a new idempotency key, which the provider takes to tell a retried
|
|
34
35
|
* check from a token used twice.
|
|
35
36
|
*/
|
|
36
37
|
export declare function deployedBotCheckVerifier(environment: {
|
|
@@ -3,22 +3,26 @@ import { deployedSecretVerifier } from "../webhooks.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* A website's bot check before a guest exists: the website shows the challenge `guest.botCheck`
|
|
5
5
|
* names, and the visitor's browser sends the challenge's token with its request for a guest's agent
|
|
6
|
-
* token. The ontology never holds the challenge's secret key:
|
|
7
|
-
* SegmentStream's backend checks the token with the provider, as it checks webhook
|
|
6
|
+
* token. The ontology never holds the challenge's secret key: a connection's verification value
|
|
7
|
+
* holds it, and SegmentStream's backend checks the token with the provider, as it checks webhook
|
|
8
|
+
* signatures.
|
|
8
9
|
*/
|
|
9
10
|
/** The longest challenge token the ontology passes on; providers issue shorter ones. */
|
|
10
11
|
export const MAX_BOT_CHECK_TOKEN_LENGTH = 2048;
|
|
11
12
|
/**
|
|
12
13
|
* The verifier of a deployed ontology service: `POST
|
|
13
|
-
* /api/internal/v1/workspaces/{id}/
|
|
14
|
-
*
|
|
15
|
-
* names. Each check carries a new idempotency key, which the provider takes to tell a retried
|
|
14
|
+
* /api/internal/v1/workspaces/{id}/connections/{key}/inputs/{field}/verify-challenge` (or
|
|
15
|
+
* `.../secrets/{name}/verify-challenge` for the older form) on SegmentStream with the request's
|
|
16
|
+
* Vercel OIDC token, admitted only for the value the deployed ontology's `guest.botCheck` names. Each check carries a new idempotency key, which the provider takes to tell a retried
|
|
16
17
|
* check from a token used twice.
|
|
17
18
|
*/
|
|
18
19
|
export function deployedBotCheckVerifier(environment, fetchImpl = fetch) {
|
|
19
20
|
const verify = deployedSecretVerifier(environment, "verify-challenge", fetchImpl);
|
|
20
21
|
return (request) => verify({
|
|
21
|
-
secret: request.check.secret,
|
|
22
|
+
secret: request.check.secret ?? "",
|
|
23
|
+
...(request.check.connection === undefined
|
|
24
|
+
? {}
|
|
25
|
+
: { input: { connection: request.check.connection, field: request.check.input } }),
|
|
22
26
|
identityToken: request.identityToken,
|
|
23
27
|
requestId: request.requestId,
|
|
24
28
|
}, {
|
|
@@ -4,7 +4,7 @@ import type { Principal } from "../../runtime.js";
|
|
|
4
4
|
import { RequestId } from "../errors.js";
|
|
5
5
|
import { type RateLimit, type RateLimiter } from "../rate-limit.js";
|
|
6
6
|
import type { AgentStore, AgentWebStore, OAuthStore } from "../runtime-contract.js";
|
|
7
|
-
import { type
|
|
7
|
+
import { type ResolvedGuest, type ResolvedGuestBotCheck } from "../../sign-in.js";
|
|
8
8
|
import { type BotCheckVerifier } from "./bot-check.js";
|
|
9
9
|
import { type AgentToolsOf } from "./protocol.js";
|
|
10
10
|
/**
|
|
@@ -59,7 +59,7 @@ export declare function visitorPreflight(websites: VisitorWebsites): (request: H
|
|
|
59
59
|
export declare const BOT_CHECK_LIMIT: RateLimit;
|
|
60
60
|
/** How a new guest passes `guest.botCheck`, when the ontology declares one. */
|
|
61
61
|
export interface VisitorBotCheck {
|
|
62
|
-
readonly check:
|
|
62
|
+
readonly check: ResolvedGuestBotCheck;
|
|
63
63
|
readonly verify: BotCheckVerifier;
|
|
64
64
|
/** The deployment's Vercel OIDC token for a request, which the backend's check admits. */
|
|
65
65
|
readonly identityToken: (request: HttpServerRequest.HttpServerRequest) => string | undefined;
|
|
@@ -9,6 +9,7 @@ import type { DeliveryArchive } from "./archive.js";
|
|
|
9
9
|
import type { RuntimeChatEnvelope } from "./envelope.js";
|
|
10
10
|
import { type Destination } from "./resolve.js";
|
|
11
11
|
import type { AgentChannelTransport } from "./providers/provider.js";
|
|
12
|
+
import { type HistoryArchive } from "./history-import.js";
|
|
12
13
|
export type ChatDeliveryFactory = (input: {
|
|
13
14
|
readonly backend: OntologyBackend["Service"];
|
|
14
15
|
readonly principal: Principal;
|
|
@@ -31,4 +32,6 @@ export declare function makeChatDelivery(ontology: Ontology, connections: Connec
|
|
|
31
32
|
agent?: {
|
|
32
33
|
readonly origin?: string | undefined;
|
|
33
34
|
readonly fetch?: AgentChannelTransport["fetch"];
|
|
34
|
-
}
|
|
35
|
+
},
|
|
36
|
+
/** The archive a chat's earlier history is imported to; without it nothing is. */
|
|
37
|
+
history?: HistoryArchive): ChatDeliveryFactory;
|
|
@@ -11,6 +11,7 @@ import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./i
|
|
|
11
11
|
import { channelProvider, chatConnection, chatKind, listedChatProviders, } from "./providers/index.js";
|
|
12
12
|
import { chatResolver } from "./resolve.js";
|
|
13
13
|
import { reviewSender } from "./review-delivery.js";
|
|
14
|
+
import { chatPeopleQuery, historyImporter } from "./history-import.js";
|
|
14
15
|
import { reviewReviser } from "./review-revision.js";
|
|
15
16
|
import { WORKSPACE_CHANNEL_KIND } from "../../chat-channel.js";
|
|
16
17
|
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
@@ -39,7 +40,9 @@ export function makeChatDelivery(ontology, connections, archive, previews = {},
|
|
|
39
40
|
* The app's agent at the deployment's production origin, which sends through a workspace
|
|
40
41
|
* channel; `fetch` by default. Without an origin nothing goes through one.
|
|
41
42
|
*/
|
|
42
|
-
agent = {}
|
|
43
|
+
agent = {},
|
|
44
|
+
/** The archive a chat's earlier history is imported to; without it nothing is. */
|
|
45
|
+
history) {
|
|
43
46
|
if (previews.settings) {
|
|
44
47
|
validateTelegramPreviews(previews.settings);
|
|
45
48
|
}
|
|
@@ -192,7 +195,7 @@ agent = {}) {
|
|
|
192
195
|
const people = (yield* context.backend
|
|
193
196
|
.find({
|
|
194
197
|
entity: "user",
|
|
195
|
-
query:
|
|
198
|
+
query: chatPeopleQuery(chat),
|
|
196
199
|
})
|
|
197
200
|
.pipe(Effect.mapError((cause) => new ActionRejected({ message: "The chat's person could not be read.", cause })))).items;
|
|
198
201
|
if (people.length !== 1) {
|
|
@@ -232,6 +235,17 @@ agent = {}) {
|
|
|
232
235
|
};
|
|
233
236
|
return {
|
|
234
237
|
takeoverChannels,
|
|
238
|
+
...(history === undefined
|
|
239
|
+
? {}
|
|
240
|
+
: {
|
|
241
|
+
importHistory: historyImporter({
|
|
242
|
+
backend: context.backend,
|
|
243
|
+
principal: context.principal,
|
|
244
|
+
identityToken: context.identityToken,
|
|
245
|
+
requestId: context.requestId,
|
|
246
|
+
archive: history,
|
|
247
|
+
}),
|
|
248
|
+
}),
|
|
235
249
|
reviseReview: reviewReviser({
|
|
236
250
|
agent: agentTransport,
|
|
237
251
|
backend: context.backend,
|
|
@@ -71,7 +71,7 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
|
|
|
71
71
|
readonly live_period?: number | undefined;
|
|
72
72
|
} | undefined;
|
|
73
73
|
} | {
|
|
74
|
-
readonly kind: "
|
|
74
|
+
readonly kind: "private" | "group";
|
|
75
75
|
readonly provider: "slack" | "whatsapp";
|
|
76
76
|
readonly chat_id: string;
|
|
77
77
|
readonly thread_id: string;
|
|
@@ -125,7 +125,7 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
|
|
|
125
125
|
readonly live_period?: number | undefined;
|
|
126
126
|
} | undefined;
|
|
127
127
|
} | {
|
|
128
|
-
readonly kind: "
|
|
128
|
+
readonly kind: "private" | "group";
|
|
129
129
|
readonly provider: "slack" | "whatsapp";
|
|
130
130
|
readonly chat_id: string;
|
|
131
131
|
readonly thread_id: string;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import type { ChatHistoryImportInput, ChatHistoryImportResult } from "../../chat.js";
|
|
3
|
+
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
4
|
+
import type { Principal } from "../../runtime.js";
|
|
5
|
+
import type { OntologyEnvironment } from "../config.js";
|
|
6
|
+
import type { OntologyBackend } from "../runtime-contract.js";
|
|
7
|
+
/** A chat's messages from before the archive recorded it, as the archive takes them. */
|
|
8
|
+
export interface HistoryImport {
|
|
9
|
+
readonly provider: string;
|
|
10
|
+
readonly chatId: string;
|
|
11
|
+
readonly chatType: "group" | "private";
|
|
12
|
+
readonly threadId: string | null;
|
|
13
|
+
readonly participantIds: ReadonlyArray<string>;
|
|
14
|
+
readonly messages: ReadonlyArray<{
|
|
15
|
+
readonly messageId: string;
|
|
16
|
+
readonly kind: "inbound" | "outbound";
|
|
17
|
+
readonly senderId: string | null;
|
|
18
|
+
readonly senderName: string | null;
|
|
19
|
+
readonly senderIsBot: boolean;
|
|
20
|
+
readonly text: string;
|
|
21
|
+
readonly replyToMessageId: string | null;
|
|
22
|
+
readonly at: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
declare const HistoryArchiveError_base: Schema.Class<HistoryArchiveError, Schema.TaggedStruct<"HistoryArchiveError", {
|
|
26
|
+
readonly diagnostic: Schema.String;
|
|
27
|
+
/** The archive refused the messages themselves: retrying them changes nothing. */
|
|
28
|
+
readonly invalid: Schema.Boolean;
|
|
29
|
+
readonly cause: Schema.optionalKey<Schema.Unknown>;
|
|
30
|
+
}>, import("effect/Cause").YieldableError>;
|
|
31
|
+
export declare class HistoryArchiveError extends HistoryArchiveError_base {
|
|
32
|
+
}
|
|
33
|
+
export type HistoryArchive = (input: {
|
|
34
|
+
readonly history: HistoryImport;
|
|
35
|
+
readonly identityToken: string | undefined;
|
|
36
|
+
}) => Effect.Effect<ChatHistoryImportResult, HistoryArchiveError>;
|
|
37
|
+
/** The platform's chat archive, which takes a chat's history from the deployment's runtime. */
|
|
38
|
+
export declare function platformHistoryArchive(environment: OntologyEnvironment, fetchImpl?: typeof fetch): HistoryArchive;
|
|
39
|
+
/**
|
|
40
|
+
* The users a chat's `members` links: a private chat's person. Queries name relationships as the
|
|
41
|
+
* stored ontology does, `agent_chats` for the inverse `agentChats` declares.
|
|
42
|
+
*/
|
|
43
|
+
export declare const chatPeopleQuery: (chat: string) => {
|
|
44
|
+
where: {
|
|
45
|
+
agent_chats: {
|
|
46
|
+
some: {
|
|
47
|
+
id: {
|
|
48
|
+
eq: string;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
limit: number;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Imports a chat's messages from before the archive recorded it, for a caller
|
|
57
|
+
* `agent_chat:import_history` admitted: a chat the inbox registered, named by its record, or one
|
|
58
|
+
* named by channel and chat id. A scoped grant covers registered chats within its scope alone.
|
|
59
|
+
*/
|
|
60
|
+
export declare function historyImporter(input: {
|
|
61
|
+
readonly backend: OntologyBackend["Service"];
|
|
62
|
+
readonly principal: Principal;
|
|
63
|
+
readonly identityToken: string | undefined;
|
|
64
|
+
readonly requestId: string;
|
|
65
|
+
readonly archive: HistoryArchive;
|
|
66
|
+
}): (request: ChatHistoryImportInput) => Effect.Effect<{
|
|
67
|
+
readonly imported: number;
|
|
68
|
+
readonly duplicates: number;
|
|
69
|
+
readonly skipped: number;
|
|
70
|
+
readonly expired: number;
|
|
71
|
+
}, ActionRejected | PermissionDenied, never>;
|
|
72
|
+
export {};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
3
|
+
import { effectiveScope } from "../../permissions.js";
|
|
4
|
+
import { describeChatError } from "./diagnostics.js";
|
|
5
|
+
export class HistoryArchiveError extends Schema.TaggedError()("HistoryArchiveError", {
|
|
6
|
+
diagnostic: Schema.String,
|
|
7
|
+
/** The archive refused the messages themselves: retrying them changes nothing. */
|
|
8
|
+
invalid: Schema.Boolean,
|
|
9
|
+
cause: Schema.optionalKey(Schema.Unknown),
|
|
10
|
+
}) {
|
|
11
|
+
}
|
|
12
|
+
const ImportAnswer = Schema.Struct({
|
|
13
|
+
imported: Schema.Number,
|
|
14
|
+
duplicates: Schema.Number,
|
|
15
|
+
skipped: Schema.Number,
|
|
16
|
+
expired: Schema.Number,
|
|
17
|
+
});
|
|
18
|
+
/** The platform's chat archive, which takes a chat's history from the deployment's runtime. */
|
|
19
|
+
export function platformHistoryArchive(environment, fetchImpl = fetch) {
|
|
20
|
+
return ({ history, identityToken }) => Effect.tryPromise({
|
|
21
|
+
try: async (signal) => {
|
|
22
|
+
if (!identityToken) {
|
|
23
|
+
throw new HistoryArchiveError({
|
|
24
|
+
diagnostic: "chat_history_runtime_identity_missing",
|
|
25
|
+
invalid: false,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
const response = await fetchImpl(`${environment.segmentstreamUrl}/api/internal/v1/workspaces/` +
|
|
29
|
+
`${encodeURIComponent(environment.workspaceId)}/agent/chats/history`, {
|
|
30
|
+
method: "POST",
|
|
31
|
+
redirect: "error",
|
|
32
|
+
headers: {
|
|
33
|
+
"content-type": "application/json",
|
|
34
|
+
"x-segmentstream-organization-id": environment.organizationId,
|
|
35
|
+
"x-segmentstream-runtime-identity": identityToken,
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify(history),
|
|
38
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(30_000)]),
|
|
39
|
+
});
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
await response.body?.cancel();
|
|
42
|
+
throw new HistoryArchiveError({
|
|
43
|
+
diagnostic: "chat_history_import_failed",
|
|
44
|
+
invalid: response.status === 400,
|
|
45
|
+
cause: new Error(`Archive HTTP ${response.status}; ` +
|
|
46
|
+
`request ${response.headers.get("x-request-id") ?? "unknown"}.`),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return Schema.decodeUnknownSync(ImportAnswer)(await response.json());
|
|
50
|
+
},
|
|
51
|
+
catch: (cause) => cause instanceof HistoryArchiveError
|
|
52
|
+
? cause
|
|
53
|
+
: new HistoryArchiveError({
|
|
54
|
+
diagnostic: "chat_history_import_failed",
|
|
55
|
+
invalid: false,
|
|
56
|
+
cause,
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
61
|
+
/**
|
|
62
|
+
* The users a chat's `members` links: a private chat's person. Queries name relationships as the
|
|
63
|
+
* stored ontology does, `agent_chats` for the inverse `agentChats` declares.
|
|
64
|
+
*/
|
|
65
|
+
export const chatPeopleQuery = (chat) => ({
|
|
66
|
+
where: { agent_chats: { some: { id: { eq: chat } } } },
|
|
67
|
+
limit: 2,
|
|
68
|
+
});
|
|
69
|
+
/** Providers the platform's own sessions use: no channel a workspace's chats are on. */
|
|
70
|
+
const RESERVED_PROVIDERS = new Set(["task", "revision", "expired"]);
|
|
71
|
+
/** The sender of every message the agent wrote, as the archive keeps the messages it writes now. */
|
|
72
|
+
const AGENT_SENDER = "agent";
|
|
73
|
+
const text = (value) => typeof value === "string" && value !== "" ? value : null;
|
|
74
|
+
/** A message as the archive keeps it: who wrote it, from its direction and author. */
|
|
75
|
+
function archived(message) {
|
|
76
|
+
const by = message.by ?? (message.direction === "inbound" ? "human" : "agent");
|
|
77
|
+
return {
|
|
78
|
+
messageId: message.id,
|
|
79
|
+
kind: message.direction,
|
|
80
|
+
// The agent's own messages have one sender, as the messages it writes now.
|
|
81
|
+
senderId: by === "agent" ? AGENT_SENDER : null,
|
|
82
|
+
senderName: message.name ?? null,
|
|
83
|
+
senderIsBot: by !== "human",
|
|
84
|
+
text: message.text,
|
|
85
|
+
replyToMessageId: message.replyTo ?? null,
|
|
86
|
+
at: message.at,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Imports a chat's messages from before the archive recorded it, for a caller
|
|
91
|
+
* `agent_chat:import_history` admitted: a chat the inbox registered, named by its record, or one
|
|
92
|
+
* named by channel and chat id. A scoped grant covers registered chats within its scope alone.
|
|
93
|
+
*/
|
|
94
|
+
export function historyImporter(input) {
|
|
95
|
+
const { backend, principal } = input;
|
|
96
|
+
return (request) => Effect.gen(function* () {
|
|
97
|
+
const scope = effectiveScope("agent_chat:import_history", principal);
|
|
98
|
+
const named = request.channel !== undefined || request.chatId !== undefined;
|
|
99
|
+
if ((request.chat === undefined) === !named || (named && request.chatId === undefined)) {
|
|
100
|
+
return yield* new ActionRejected({
|
|
101
|
+
message: "Name the chat by chat, or by channel and chatId.",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
let history;
|
|
105
|
+
if (request.chat !== undefined) {
|
|
106
|
+
if (!UUID.test(request.chat)) {
|
|
107
|
+
return yield* new ActionRejected({ message: "chat is an agent_chat record's id." });
|
|
108
|
+
}
|
|
109
|
+
const record = yield* backend
|
|
110
|
+
.get({
|
|
111
|
+
entity: "agent_chat",
|
|
112
|
+
id: request.chat,
|
|
113
|
+
...(scope === undefined
|
|
114
|
+
? {}
|
|
115
|
+
: {
|
|
116
|
+
reader: {
|
|
117
|
+
...principal,
|
|
118
|
+
permissions: ["agent_chat:read"],
|
|
119
|
+
scopes: [{ permission: "agent_chat:read", where: scope }],
|
|
120
|
+
},
|
|
121
|
+
}),
|
|
122
|
+
})
|
|
123
|
+
.pipe(Effect.mapError(() => scope === undefined
|
|
124
|
+
? new ActionRejected({ message: "The chat does not exist." })
|
|
125
|
+
: new PermissionDenied({
|
|
126
|
+
message: "The chat is outside the chats agent_chat:import_history covers.",
|
|
127
|
+
permission: "agent_chat:import_history",
|
|
128
|
+
})));
|
|
129
|
+
const kind = record.kind === "private" ? "private" : "group";
|
|
130
|
+
// A private chat's person takes part in its history, as in every message there.
|
|
131
|
+
const people = kind === "private"
|
|
132
|
+
? (yield* backend
|
|
133
|
+
.find({
|
|
134
|
+
entity: "user",
|
|
135
|
+
query: chatPeopleQuery(request.chat),
|
|
136
|
+
})
|
|
137
|
+
.pipe(Effect.mapError((cause) => new ActionRejected({
|
|
138
|
+
message: "The chat's person could not be read.",
|
|
139
|
+
cause,
|
|
140
|
+
})))).items
|
|
141
|
+
: [];
|
|
142
|
+
history = {
|
|
143
|
+
provider: String(record.provider),
|
|
144
|
+
chatId: String(record.external_id),
|
|
145
|
+
chatType: kind,
|
|
146
|
+
threadId: request.thread ?? text(record.thread_id),
|
|
147
|
+
participantIds: people.length === 1 ? [String(people[0].id)] : [],
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
if (scope !== undefined) {
|
|
152
|
+
return yield* new PermissionDenied({
|
|
153
|
+
message: "A scoped agent_chat:import_history covers the chats it names: name the chat by its " +
|
|
154
|
+
"agent_chat record.",
|
|
155
|
+
permission: "agent_chat:import_history",
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const provider = request.channel ?? "";
|
|
159
|
+
if (provider === "" || RESERVED_PROVIDERS.has(provider)) {
|
|
160
|
+
return yield* new ActionRejected({ message: "channel names the chat's channel." });
|
|
161
|
+
}
|
|
162
|
+
if (request.person !== undefined) {
|
|
163
|
+
const person = yield* backend
|
|
164
|
+
.get({ entity: "user", id: request.person })
|
|
165
|
+
.pipe(Effect.mapError(() => new ActionRejected({ message: "person is no user." })));
|
|
166
|
+
if (person.kind === "service") {
|
|
167
|
+
return yield* new ActionRejected({ message: "person is a service user." });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const kind = request.kind ?? "private";
|
|
171
|
+
if (kind === "group" && request.person !== undefined) {
|
|
172
|
+
return yield* new ActionRejected({ message: "A group chat has no one person." });
|
|
173
|
+
}
|
|
174
|
+
history = {
|
|
175
|
+
provider,
|
|
176
|
+
chatId: request.chatId,
|
|
177
|
+
chatType: kind,
|
|
178
|
+
threadId: request.thread ?? null,
|
|
179
|
+
participantIds: request.person === undefined ? [] : [request.person],
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const now = Date.now();
|
|
183
|
+
const ids = new Set(request.messages.map((message) => message.id));
|
|
184
|
+
if (ids.size !== request.messages.length) {
|
|
185
|
+
return yield* new ActionRejected({ message: "Each message's id is unique in the chat." });
|
|
186
|
+
}
|
|
187
|
+
if (request.messages.some((message) => !Number.isFinite(Date.parse(message.at)) || Date.parse(message.at) > now + 5 * 60_000)) {
|
|
188
|
+
return yield* new ActionRejected({
|
|
189
|
+
message: "Each message's at is an ISO 8601 time no later than now.",
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
if (request.messages.some((message) => message.text.trim() === "")) {
|
|
193
|
+
return yield* new ActionRejected({ message: "Each message has text." });
|
|
194
|
+
}
|
|
195
|
+
return yield* input
|
|
196
|
+
.archive({
|
|
197
|
+
history: { ...history, messages: request.messages.map(archived) },
|
|
198
|
+
identityToken: input.identityToken,
|
|
199
|
+
})
|
|
200
|
+
.pipe(Effect.tapError((cause) => Effect.logWarning("ontology.agent_chat.history_import_failed").pipe(Effect.annotateLogs({
|
|
201
|
+
requestId: input.requestId,
|
|
202
|
+
diagnostic: cause.diagnostic,
|
|
203
|
+
error: describeChatError(cause, (value) => input.identityToken ? value.split(input.identityToken).join("[redacted]") : value),
|
|
204
|
+
}))), Effect.mapError((cause) => cause.invalid
|
|
205
|
+
? new ActionRejected({ message: "The chat archive refused the messages." })
|
|
206
|
+
: new ActionRejected({
|
|
207
|
+
message: "The chat archive could not take the messages. Try again.",
|
|
208
|
+
})));
|
|
209
|
+
});
|
|
210
|
+
}
|
package/dist/service/config.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Redacted, Schema } from "effect";
|
|
|
3
3
|
import { BlobStorageBinding } from "../storage/provider.js";
|
|
4
4
|
import { type DeploymentObservation } from "../deployment.js";
|
|
5
5
|
import type { DeliveryArchive } from "./chat/archive.js";
|
|
6
|
+
import type { HistoryArchive } from "./chat/history-import.js";
|
|
6
7
|
import type { Principal } from "../runtime.js";
|
|
7
8
|
import type { PageCatalogV1 } from "../navigation-catalog.js";
|
|
8
9
|
import { type ConnectionTransportFactory, type LocalConnectionContext } from "./connections.js";
|
|
@@ -162,6 +163,8 @@ export interface OntologyServiceConfig extends OntologyEnvironment {
|
|
|
162
163
|
readonly channelAppProviders?: readonly import("./channel/provider.js").ChannelAppProvider[];
|
|
163
164
|
/** Override the archive transport for isolated integrations; defaults to the authenticated platform archive. */
|
|
164
165
|
readonly chatArchive?: DeliveryArchive;
|
|
166
|
+
/** Override where a chat's earlier history is imported, for isolated integrations. */
|
|
167
|
+
readonly chatHistoryArchive?: HistoryArchive;
|
|
165
168
|
/**
|
|
166
169
|
* Reuse the same access token during refresh recovery for this many milliseconds (default 45s).
|
|
167
170
|
* Afterward, recover the unused refresh successor with fresh access. Zero disables recovery.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const FEEDBACK_SUBMIT_TOOL = "app_feedback_submit";
|
|
2
|
+
export declare const FEEDBACK_ATTACH_TOOL = "app_feedback_file_attach";
|
|
3
|
+
/** The workspace's published feedback policy, as far as an agent's guidance depends on it. */
|
|
4
|
+
export interface FeedbackPolicy {
|
|
5
|
+
/** `feedback: { automaticFailureReports: true }`: technical failure reports need no consent. */
|
|
6
|
+
readonly automaticFailureReports: boolean;
|
|
7
|
+
/** Whether the caller can attach files to their feedback. */
|
|
8
|
+
readonly files: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* How an agent handles app feedback, stated once for every client of the ontology: the MCP server's
|
|
12
|
+
* instructions carry it for a caller who may submit, and `app_feedback_submit`'s description carries
|
|
13
|
+
* it for clients that read tools rather than server instructions, such as the workspace agent. It is
|
|
14
|
+
* platform guidance for the model; the agent words what it tells the member in the chat's language.
|
|
15
|
+
*/
|
|
16
|
+
export declare function feedbackGuidance(policy: FeedbackPolicy): string;
|