@segmentstream/agent 0.90.1 → 0.91.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/_chunks/{build-metadata-BDZ3bQVF.mjs → build-metadata-BisjjIh6.mjs} +1 -1
- package/dist/extension/config.d.ts +8 -2
- package/dist/extension/conventions.mjs +6 -1
- package/dist/extension/instructions/persona.d.ts +1 -6
- package/dist/extension/instructions/persona.mjs +6 -3
- package/dist/extension/lib/build-metadata.mjs +1 -1
- package/dist/extension/lib/chat-persona.d.ts +15 -0
- package/dist/extension/lib/chat-persona.mjs +75 -0
- package/dist/extension/lib/slack/adapter.mjs +8 -2
- package/dist/extension/lib/telegram/adapter.mjs +1 -1
- package/dist/extension/lib/telegram/health.mjs +1 -1
- package/dist/extension/lib/telegram/inbound.mjs +21 -1
- package/dist/extension/lib/telegram/lifecycle.mjs +1 -1
- package/dist/extension/lib/whatsapp/adapter.mjs +3 -2
- package/dist/extension/personas.d.ts +9 -8
- package/dist/extension/personas.mjs +79 -3
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.91.0
|
|
4
|
+
|
|
5
|
+
Route the agent's persona by chat assignment, channel rules and workspace default.
|
|
6
|
+
|
|
7
|
+
Pins @segmentstream/ontology 0.172.0.
|
|
8
|
+
|
|
3
9
|
## 0.90.1
|
|
4
10
|
|
|
5
11
|
Slack action confirmations show the full confirmation facts, document links and workspace button labels. The clicked message shows the result when the action finishes.
|
|
@@ -4,7 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
-
var version = "0.
|
|
7
|
+
var version = "0.91.0";
|
|
8
8
|
function agentBuild(environment = process.env) {
|
|
9
9
|
return {
|
|
10
10
|
agent: version,
|
|
@@ -75,6 +75,7 @@ export interface FirstContactSettings {
|
|
|
75
75
|
export declare const ADMISSION_REPLY_KEYS: readonly ["pairingRequired", "pairingButton", "groupPairingButton", "limited", "perSenderLimited", "workspaceLimited", "registrationsLimited", "groupPairingRequired", "refused"];
|
|
76
76
|
export type AdmissionReplies = Partial<Record<(typeof ADMISSION_REPLY_KEYS)[number], LocalizedText>>;
|
|
77
77
|
export interface AdmissionSettings {
|
|
78
|
+
readonly personas?: PersonaSettings;
|
|
78
79
|
readonly firstContact: FirstContactSettings | null;
|
|
79
80
|
readonly limits: AdmissionLimits;
|
|
80
81
|
readonly replies: AdmissionReplies;
|
|
@@ -205,13 +206,18 @@ export interface SegmentStreamAgentConfigInput {
|
|
|
205
206
|
readonly idleResetHours?: unknown;
|
|
206
207
|
};
|
|
207
208
|
/**
|
|
208
|
-
*
|
|
209
|
-
* who started the session holds winning in the order written, and a `default` for everyone else.
|
|
209
|
+
* Choose by chat assignment and ordered rules, or by the session initiator’s roles.
|
|
210
210
|
* Each persona's text is `agent/personas/<name>.md`; the SDK and the platform put it in
|
|
211
211
|
* `texts`, which is not written by hand.
|
|
212
212
|
*/
|
|
213
213
|
readonly personas?: {
|
|
214
214
|
readonly byRole?: Readonly<Record<string, string>>;
|
|
215
|
+
readonly rules?: readonly {
|
|
216
|
+
readonly when: Readonly<Partial<Record<string, string>>>;
|
|
217
|
+
readonly use: string;
|
|
218
|
+
}[];
|
|
219
|
+
readonly available?: readonly string[];
|
|
220
|
+
readonly notice?: LocalizedText;
|
|
215
221
|
readonly default?: string;
|
|
216
222
|
readonly texts?: Readonly<Record<string, string>>;
|
|
217
223
|
};
|
|
@@ -30,7 +30,12 @@ function inlineConventionFiles(input, read) {
|
|
|
30
30
|
}
|
|
31
31
|
if (isRecord(input.personas)) {
|
|
32
32
|
const personas = input.personas;
|
|
33
|
-
const named = [
|
|
33
|
+
const named = [
|
|
34
|
+
...isRecord(personas.byRole) ? Object.values(personas.byRole) : [],
|
|
35
|
+
...Array.isArray(personas.available) ? personas.available : [],
|
|
36
|
+
...Array.isArray(personas.rules) ? personas.rules.map((rule) => isRecord(rule) ? rule.use : void 0) : [],
|
|
37
|
+
personas.default
|
|
38
|
+
].filter(isPersonaName);
|
|
34
39
|
const texts = isRecord(personas.texts) ? { ...personas.texts } : {};
|
|
35
40
|
for (const name of new Set(named)) {
|
|
36
41
|
const text = read(personaFile(name));
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The persona of the session: a system instruction after the workspace's own, chosen from the
|
|
3
|
-
* roles of the person who started the session (the workflow's user for a workflow task), so it
|
|
4
|
-
* stays the same for the whole session and keeps the prompt prefix cached. Nothing without
|
|
5
|
-
* `personas` in `agent/segmentstream.json`, or when no role matches and there is no `default`.
|
|
6
|
-
*/
|
|
1
|
+
/** Resolve the active chat's persona for this turn, or the initiator's in role-based mode. */
|
|
7
2
|
declare const _default: import("eve").DynamicSentinel<import("eve/instructions").DynamicInstructionsResult, import("eve").DynamicContext>;
|
|
8
3
|
export default _default;
|
|
@@ -6,13 +6,16 @@ __eveDirname(__filename);
|
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import segmentstream from "../extension.mjs";
|
|
8
8
|
import { a as rolesOf } from "../../_chunks/principal-CY72xYFi.mjs";
|
|
9
|
+
import { resolveChatPersona } from "../lib/chat-persona.mjs";
|
|
9
10
|
import { personaFor } from "../personas.mjs";
|
|
10
11
|
import { defineDynamic, defineInstructions } from "eve/instructions";
|
|
11
|
-
var persona_default = defineDynamic((context) => {
|
|
12
|
+
var persona_default = defineDynamic(async (context) => {
|
|
12
13
|
const { personas } = segmentstream.config;
|
|
13
14
|
if (personas === void 0) return null;
|
|
14
|
-
const
|
|
15
|
-
const
|
|
15
|
+
const current = context.session.auth.current;
|
|
16
|
+
const routed = current?.attributes.segmentstreamChatPersona;
|
|
17
|
+
const persona = personas.rules !== void 0 ? typeof routed === "string" && Object.hasOwn(personas.texts ?? {}, routed) ? routed : await context.memo([context.turn?.id ?? null, current?.principalId ?? null], async () => (await resolveChatPersona(personas, current)).name ?? null) : personaFor(personas, rolesOf(context.session.auth.initiator ?? context.session.auth.current));
|
|
18
|
+
const content = persona == null ? void 0 : personas.texts?.[persona];
|
|
16
19
|
return content === void 0 ? null : defineInstructions({
|
|
17
20
|
role: "system",
|
|
18
21
|
content
|
|
@@ -4,4 +4,4 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
-
export { t as agentBuild } from "../../_chunks/build-metadata-
|
|
7
|
+
export { t as agentBuild } from "../../_chunks/build-metadata-BisjjIh6.mjs";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SessionAuthContext } from "eve/context";
|
|
2
|
+
import { chatPersona, type PersonaSettings } from "../personas.js";
|
|
3
|
+
export declare function resolveChatPersona(settings: PersonaSettings, principal: SessionAuthContext | null | undefined, read?: (key: string) => Promise<string | null>): Promise<{
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly source: "chat";
|
|
6
|
+
} | {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly source: "rule";
|
|
9
|
+
} | {
|
|
10
|
+
readonly name: string | undefined;
|
|
11
|
+
readonly source: "default";
|
|
12
|
+
}>;
|
|
13
|
+
/** A provider-specific private delivery, claimed once by the ontology as this member. */
|
|
14
|
+
export declare function offerPersona(settings: PersonaSettings | undefined, selection: ReturnType<typeof chatPersona> | undefined, principal: SessionAuthContext, deliver: (text: string) => Promise<unknown>, locale?: string): Promise<void>;
|
|
15
|
+
export declare function personaPrincipal(settings: PersonaSettings | undefined, principal: SessionAuthContext, deliver?: (text: string) => Promise<unknown>): Promise<SessionAuthContext>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { fileURLToPath as __eveFileURLToPath } from "node:url";
|
|
2
|
+
import { dirname as __eveDirname } from "node:path";
|
|
3
|
+
import { createRequire as __eveCreateRequire } from "node:module";
|
|
4
|
+
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
|
+
__eveDirname(__filename);
|
|
6
|
+
__eveCreateRequire(import.meta.url);
|
|
7
|
+
import { chatPersona } from "../personas.mjs";
|
|
8
|
+
import { Ft as chatAddressOf, It as chatChannel, i as memberIdOf, ot as guestIdOf } from "../../_chunks/principal-CY72xYFi.mjs";
|
|
9
|
+
import { appOrigin, ontologyDeploymentHeaders } from "./environment.mjs";
|
|
10
|
+
import { localizedText } from "./admission.mjs";
|
|
11
|
+
import { runtimeToken } from "./runtime-token.mjs";
|
|
12
|
+
async function request(operation, data) {
|
|
13
|
+
const response = await fetch(`${appOrigin()}/api/ontology/agent/chats/${operation}`, {
|
|
14
|
+
method: "POST",
|
|
15
|
+
redirect: "error",
|
|
16
|
+
headers: {
|
|
17
|
+
...ontologyDeploymentHeaders(),
|
|
18
|
+
authorization: `Bearer ${await runtimeToken()}`,
|
|
19
|
+
"content-type": "application/json"
|
|
20
|
+
},
|
|
21
|
+
body: JSON.stringify(data),
|
|
22
|
+
signal: AbortSignal.timeout(5e3)
|
|
23
|
+
});
|
|
24
|
+
if (!response.ok) throw new Error(`Reading the chat persona failed (HTTP ${response.status}).`);
|
|
25
|
+
return response.json();
|
|
26
|
+
}
|
|
27
|
+
async function readAssignment(key) {
|
|
28
|
+
const body = await request("persona", { key });
|
|
29
|
+
if (body?.persona !== null && typeof body?.persona !== "string") throw new Error("Invalid chat persona response.");
|
|
30
|
+
return body.persona;
|
|
31
|
+
}
|
|
32
|
+
async function resolveChatPersona(settings, principal, read = readAssignment) {
|
|
33
|
+
const chat = chatAddressOf(principal);
|
|
34
|
+
if (!chat) return chatPersona(settings, void 0);
|
|
35
|
+
const channel = chatChannel(chat);
|
|
36
|
+
return chatPersona(settings, {
|
|
37
|
+
provider: chat.provider,
|
|
38
|
+
channel,
|
|
39
|
+
kind: chat.kind,
|
|
40
|
+
audience: guestIdOf(principal) ? "guest" : "member"
|
|
41
|
+
}, await read(`${channel}:${chat.chatId}`));
|
|
42
|
+
}
|
|
43
|
+
async function offerPersona(settings, selection, principal, deliver, locale) {
|
|
44
|
+
const language = locale ?? principal.attributes.locale;
|
|
45
|
+
const text = localizedText(settings?.notice, typeof language === "string" ? language : void 0);
|
|
46
|
+
const chat = chatAddressOf(principal);
|
|
47
|
+
const memberId = memberIdOf(principal);
|
|
48
|
+
if (!text || !chat || !memberId || selection?.source !== "default" || !selection.name) return;
|
|
49
|
+
try {
|
|
50
|
+
const receipt = await request("persona-notice", {
|
|
51
|
+
key: `${chatChannel(chat)}:${chat.chatId}`,
|
|
52
|
+
memberId,
|
|
53
|
+
provider: chat.provider
|
|
54
|
+
});
|
|
55
|
+
if (typeof receipt?.chat !== "string") return;
|
|
56
|
+
const url = new URL("/agent/persona", appOrigin());
|
|
57
|
+
url.searchParams.set("chat", receipt.chat);
|
|
58
|
+
await deliver(text.replaceAll("{persona}", selection.name).replaceAll("{url}", url.href));
|
|
59
|
+
} catch {
|
|
60
|
+
console.warn("[segmentstream] Private persona notice unavailable");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function personaPrincipal(settings, principal, deliver) {
|
|
64
|
+
if (settings?.rules === void 0) return principal;
|
|
65
|
+
const selection = await resolveChatPersona(settings, principal);
|
|
66
|
+
if (deliver) await offerPersona(settings, selection, principal, deliver);
|
|
67
|
+
return selection.name ? {
|
|
68
|
+
...principal,
|
|
69
|
+
attributes: {
|
|
70
|
+
...principal.attributes,
|
|
71
|
+
segmentstreamChatPersona: selection.name
|
|
72
|
+
}
|
|
73
|
+
} : principal;
|
|
74
|
+
}
|
|
75
|
+
export { offerPersona, personaPrincipal, resolveChatPersona };
|
|
@@ -8,6 +8,7 @@ import { admitMessage, localizedText } from "../admission.mjs";
|
|
|
8
8
|
import { agentToolsAttribute } from "../agent-tools.mjs";
|
|
9
9
|
import { agentChats, channelChats } from "../chats.mjs";
|
|
10
10
|
import { withButtonDecision } from "../confirmation-buttons.mjs";
|
|
11
|
+
import { personaPrincipal } from "../chat-persona.mjs";
|
|
11
12
|
import { Vt as withChatAddress, b as agentAttachments, d as prepareInboundMedia, m as turnContent } from "../../../_chunks/principal-CY72xYFi.mjs";
|
|
12
13
|
import { channelAttributes, currentChannel, withNamedRoutes, withSessionChannels } from "../channel-scope.mjs";
|
|
13
14
|
import { redactDiagnostic } from "../diagnostics.mjs";
|
|
@@ -500,7 +501,7 @@ async function receive(input) {
|
|
|
500
501
|
title: () => slackChannelTitle((method, body) => ctx.slack.request(method, body), channelId),
|
|
501
502
|
memberId: resolution.member.id
|
|
502
503
|
});
|
|
503
|
-
if (input.waitUntil === void 0) await reported;
|
|
504
|
+
if (input.waitUntil === void 0 || admission.personas?.rules !== void 0) await reported;
|
|
504
505
|
else input.waitUntil(reported);
|
|
505
506
|
}
|
|
506
507
|
if (!turn) return null;
|
|
@@ -513,7 +514,7 @@ async function receive(input) {
|
|
|
513
514
|
messageTs: message.ts,
|
|
514
515
|
memberId: resolution.member.id
|
|
515
516
|
}).catch((error) => report("thread_history_failed", error));
|
|
516
|
-
const
|
|
517
|
+
const incoming = withChatAddress(slackPrincipal(resolution.member, user), {
|
|
517
518
|
provider: "slack",
|
|
518
519
|
...currentChannel("slack") === "slack" ? {} : { channel: currentChannel("slack") },
|
|
519
520
|
chatId: ctx.slack.channelId,
|
|
@@ -524,6 +525,11 @@ async function receive(input) {
|
|
|
524
525
|
id: message.ts,
|
|
525
526
|
sender: user
|
|
526
527
|
});
|
|
528
|
+
const auth = await personaPrincipal(admission.personas, incoming, ctx.edit === void 0 ? (text) => ctx.slack.request("chat.postEphemeral", {
|
|
529
|
+
channel: ctx.slack.channelId,
|
|
530
|
+
user,
|
|
531
|
+
text
|
|
532
|
+
}) : void 0);
|
|
527
533
|
const received = (media) => {
|
|
528
534
|
if (ctx.edit === void 0) {
|
|
529
535
|
const at = slackTime(message.ts);
|
|
@@ -11,7 +11,7 @@ import { groupRoutingHealth } from "../group-routing/health.mjs";
|
|
|
11
11
|
import { dispatchStructuredMessage, structuredMessage } from "./structured.mjs";
|
|
12
12
|
import { handleWorkspaceCallback } from "./callbacks.mjs";
|
|
13
13
|
import { telegramText, withTelegramTexts } from "./texts.mjs";
|
|
14
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
14
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BisjjIh6.mjs";
|
|
15
15
|
import { attachmentLifecycle } from "./lifecycle.mjs";
|
|
16
16
|
import { manifestOf } from "../../config.mjs";
|
|
17
17
|
import { gatewayAttachmentDescriber } from "./attachment-description.mjs";
|
|
@@ -4,7 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
7
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BisjjIh6.mjs";
|
|
8
8
|
import { runtimeRequestFailure } from "./runtime-request.mjs";
|
|
9
9
|
import { GET } from "eve/channels";
|
|
10
10
|
const HEALTH_ROUTE = "/eve/v1/telegram/health";
|
|
@@ -15,6 +15,7 @@ import { effectiveGroupPolicy, groupPolicy } from "../group-routing/policy.mjs";
|
|
|
15
15
|
import { questionInvitation } from "../group-routing/engagement.mjs";
|
|
16
16
|
import { AttachmentLifecycleNotPermitted } from "./lifecycle.mjs";
|
|
17
17
|
import { admitMessage, localizedText } from "../admission.mjs";
|
|
18
|
+
import { offerPersona, resolveChatPersona } from "../chat-persona.mjs";
|
|
18
19
|
import { chatContext } from "../chats.mjs";
|
|
19
20
|
import { openUsageLimit, questionAddress, questionFromHistory, usageLimitAnswer } from "./confirmations.mjs";
|
|
20
21
|
import { answerMarker } from "../questions.mjs";
|
|
@@ -618,6 +619,12 @@ async function handleInboundMessage(input) {
|
|
|
618
619
|
text = "[attachment]";
|
|
619
620
|
if (input.request !== void 0) input.request.text = text;
|
|
620
621
|
}
|
|
622
|
+
const personas = dependencies.admission().personas;
|
|
623
|
+
const selection = personas?.rules === void 0 ? void 0 : await resolveChatPersona(personas, telegramPrincipal({
|
|
624
|
+
message: sent,
|
|
625
|
+
member
|
|
626
|
+
}));
|
|
627
|
+
const persona = selection?.name;
|
|
621
628
|
let routingBytes;
|
|
622
629
|
const routingFile = sent.attachments.find((file) => file.fileId !== voice?.fileId);
|
|
623
630
|
const attachmentMetadata = sent.attachments.length === 0 ? [] : [JSON.stringify({ attachment_metadata: sent.attachments.map((file) => ({
|
|
@@ -666,7 +673,7 @@ async function handleInboundMessage(input) {
|
|
|
666
673
|
member
|
|
667
674
|
}
|
|
668
675
|
},
|
|
669
|
-
agent: await input.request?.agentDescription() ?? "",
|
|
676
|
+
agent: persona ?? await input.request?.agentDescription() ?? "",
|
|
670
677
|
agentIdentity: {
|
|
671
678
|
...bot,
|
|
672
679
|
displayName: bot?.displayName ?? name,
|
|
@@ -1084,6 +1091,19 @@ async function handleInboundMessage(input) {
|
|
|
1084
1091
|
member,
|
|
1085
1092
|
chatLanguage: language
|
|
1086
1093
|
});
|
|
1094
|
+
if (persona !== void 0) {
|
|
1095
|
+
await offerPersona(personas, selection, principal, (text) => context.telegram.request("sendMessage", {
|
|
1096
|
+
chat_id: sent.from.id,
|
|
1097
|
+
text
|
|
1098
|
+
}), language);
|
|
1099
|
+
principal = {
|
|
1100
|
+
...principal,
|
|
1101
|
+
attributes: {
|
|
1102
|
+
...principal.attributes,
|
|
1103
|
+
segmentstreamChatPersona: persona
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1087
1107
|
const [guidance] = group ? chatContext(dependencies.chats.chat(sent.chat.id)) : [];
|
|
1088
1108
|
if (guidance !== void 0) principal = {
|
|
1089
1109
|
...principal,
|
|
@@ -5,7 +5,7 @@ const __filename = __eveFileURLToPath(import.meta.url);
|
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import { E as OntologyActionNotPermitted, Et as principalRuntimeSource, L as needsConfirmation, Ot as runtimeEnvelope, W as member, c as currentInboundRequest, j as ontologyTools, k as actionToolName, kt as telegramRuntimeSource } from "../../../_chunks/principal-CY72xYFi.mjs";
|
|
8
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
8
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BisjjIh6.mjs";
|
|
9
9
|
import { object } from "../confirmation-description.mjs";
|
|
10
10
|
var AttachmentLifecycleNotPermitted = class extends Error {
|
|
11
11
|
code = "attachment_lifecycle_not_permitted";
|
|
@@ -13,6 +13,7 @@ import { gatewayMediaCapabilities, turnMediaPlan } from "../inbound-media/plan.m
|
|
|
13
13
|
import { gatewayTranscriber } from "../inbound-media/speech.mjs";
|
|
14
14
|
import { whatsappMedia, whatsappTurnContent } from "./files.mjs";
|
|
15
15
|
import { chatSdkButtonAnswer, withButtonDecision } from "../confirmation-buttons.mjs";
|
|
16
|
+
import { personaPrincipal } from "../chat-persona.mjs";
|
|
16
17
|
import { channelAttributes, currentChannel, currentKey, withNamedRoutes, withSessionChannels } from "../channel-scope.mjs";
|
|
17
18
|
import { channelReview, withReviewedReplies } from "../review/reply.mjs";
|
|
18
19
|
import { redactDiagnostic } from "../diagnostics.mjs";
|
|
@@ -563,7 +564,7 @@ async function receive(input) {
|
|
|
563
564
|
}).catch((cause) => report("message_record_failed", cause));
|
|
564
565
|
return;
|
|
565
566
|
}
|
|
566
|
-
return turn ? withChatAddress(whatsappPrincipal(resolution.member, sender), {
|
|
567
|
+
return turn ? personaPrincipal(admission.personas, withChatAddress(whatsappPrincipal(resolution.member, sender), {
|
|
567
568
|
provider: "whatsapp",
|
|
568
569
|
...currentChannel("whatsapp") === "whatsapp" ? {} : { channel: currentChannel("whatsapp") },
|
|
569
570
|
chatId: thread.id,
|
|
@@ -573,7 +574,7 @@ async function receive(input) {
|
|
|
573
574
|
}, {
|
|
574
575
|
id: message.id,
|
|
575
576
|
sender
|
|
576
|
-
}) : void 0;
|
|
577
|
+
}), (text) => thread.post(text)) : void 0;
|
|
577
578
|
case "pairing_required": {
|
|
578
579
|
await unadmitted("identity_unknown");
|
|
579
580
|
const text = localizedText(admission.replies.pairingRequired);
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* SDK reads when the agent is built and the platform puts inline (`personas.texts`) when it
|
|
5
|
-
* deploys, so the running agent, which has no files, reads the same text. Selection reads only the
|
|
6
|
-
* roles of the person who started the session, so a session keeps one persona.
|
|
7
|
-
*/
|
|
1
|
+
import { type PersonaRule } from "@segmentstream/ontology/chat-personas";
|
|
2
|
+
export { chatPersona } from "@segmentstream/ontology/chat-personas";
|
|
3
|
+
export type { PersonaContext, PersonaRule } from "@segmentstream/ontology/chat-personas";
|
|
8
4
|
/** The directory beside `segmentstream.json` that holds the persona texts. */
|
|
9
5
|
export declare const PERSONAS_DIRECTORY = "personas";
|
|
10
6
|
export interface PersonaSettings {
|
|
7
|
+
/** Ordered chat rules. Presence opts into chat routing, including an empty list. */
|
|
8
|
+
readonly rules?: readonly PersonaRule[];
|
|
9
|
+
/** Additional names that may be assigned directly to a chat. */
|
|
10
|
+
readonly available?: readonly string[];
|
|
11
|
+
/** Localized private setup prompt with {persona} and {url} for the workspace's chat settings page. */
|
|
12
|
+
readonly notice?: Readonly<Record<string, string>>;
|
|
11
13
|
/** A persona per role; the first role the person holds wins, in the order written. */
|
|
12
14
|
readonly byRole: Readonly<Record<string, string>>;
|
|
13
15
|
/** The persona of anyone no role matches, including a person who holds no roles. */
|
|
@@ -36,4 +38,3 @@ export declare function isPersonaName(value: unknown): value is string;
|
|
|
36
38
|
* when the agent is built or deployed, every persona it names needs its text.
|
|
37
39
|
*/
|
|
38
40
|
export declare function personaSettings(value: unknown, issues: Issue[], requireTexts?: boolean): PersonaSettings | null;
|
|
39
|
-
export {};
|
|
@@ -4,6 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
+
import { chatPersona, chatPersonaNames } from "@segmentstream/ontology/chat-personas";
|
|
7
8
|
const PERSONAS_DIRECTORY = "personas";
|
|
8
9
|
const NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
9
10
|
const MAXIMUM_NAME_LENGTH = 64;
|
|
@@ -13,13 +14,56 @@ function personaFile(name) {
|
|
|
13
14
|
}
|
|
14
15
|
function personaNames(settings) {
|
|
15
16
|
if (settings === null) return [];
|
|
16
|
-
return [...new Set([...Object.values(settings.byRole), ...settings
|
|
17
|
+
return [...new Set([...Object.values(settings.byRole), ...chatPersonaNames(settings)])];
|
|
17
18
|
}
|
|
18
19
|
function personaFor(settings, roles) {
|
|
19
20
|
if (settings === null) return;
|
|
20
21
|
for (const [role, persona] of Object.entries(settings.byRole)) if (roles.includes(role)) return persona;
|
|
21
22
|
return settings.default;
|
|
22
23
|
}
|
|
24
|
+
const RULE_FIELDS = {
|
|
25
|
+
provider: /^(telegram|slack|whatsapp|email|web|custom)$/,
|
|
26
|
+
channel: /^[a-z][a-z0-9_]{0,31}$/,
|
|
27
|
+
kind: /^(group|private)$/,
|
|
28
|
+
audience: /^(guest|member)$/
|
|
29
|
+
};
|
|
30
|
+
function readRules(value, issues) {
|
|
31
|
+
const rules = [];
|
|
32
|
+
if (!Array.isArray(value)) {
|
|
33
|
+
issues.push({
|
|
34
|
+
message: "must be an array",
|
|
35
|
+
path: ["personas", "rules"]
|
|
36
|
+
});
|
|
37
|
+
return rules;
|
|
38
|
+
}
|
|
39
|
+
for (const [index, rule] of value.entries()) {
|
|
40
|
+
const path = [
|
|
41
|
+
"personas",
|
|
42
|
+
"rules",
|
|
43
|
+
String(index)
|
|
44
|
+
];
|
|
45
|
+
if (!isRecord(rule) || !validName(rule.use) || !isRecord(rule.when) || Object.keys(rule).some((key) => key !== "when" && key !== "use") || !Object.keys(rule.when).length) {
|
|
46
|
+
issues.push({
|
|
47
|
+
message: "needs when with at least one chat condition, and use with a persona name",
|
|
48
|
+
path
|
|
49
|
+
});
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
for (const [key, condition] of Object.entries(rule.when)) if (!Object.hasOwn(RULE_FIELDS, key) || typeof condition !== "string" || !RULE_FIELDS[key].test(condition)) issues.push({
|
|
53
|
+
message: "unknown chat condition or invalid value",
|
|
54
|
+
path: [
|
|
55
|
+
...path,
|
|
56
|
+
"when",
|
|
57
|
+
key
|
|
58
|
+
]
|
|
59
|
+
});
|
|
60
|
+
rules.push({
|
|
61
|
+
when: rule.when,
|
|
62
|
+
use: rule.use
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return rules;
|
|
66
|
+
}
|
|
23
67
|
function isRecord(value) {
|
|
24
68
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
25
69
|
}
|
|
@@ -42,7 +86,7 @@ function personaSettings(value, issues, requireTexts = false) {
|
|
|
42
86
|
});
|
|
43
87
|
return null;
|
|
44
88
|
}
|
|
45
|
-
for (const key of Object.keys(value)) if (key !== "byRole" && key !== "default" && key !== "texts") issues.push({
|
|
89
|
+
for (const key of Object.keys(value)) if (key !== "byRole" && key !== "default" && key !== "texts" && key !== "notice" && key !== "available" && key !== "rules") issues.push({
|
|
46
90
|
message: "is not a known setting",
|
|
47
91
|
path: [...path, key]
|
|
48
92
|
});
|
|
@@ -78,7 +122,39 @@ function personaSettings(value, issues, requireTexts = false) {
|
|
|
78
122
|
message: "needs byRole, default or both",
|
|
79
123
|
path
|
|
80
124
|
});
|
|
125
|
+
const rules = value.rules === void 0 ? void 0 : readRules(value.rules, issues);
|
|
126
|
+
if (rules !== void 0 && (value.byRole !== void 0 && Object.keys(byRole).length > 0 || !validName(value.default))) issues.push({
|
|
127
|
+
message: "chat routing requires a default and cannot select by role",
|
|
128
|
+
path
|
|
129
|
+
});
|
|
130
|
+
let notice;
|
|
131
|
+
if (value.notice !== void 0) {
|
|
132
|
+
if (rules === void 0 || !isRecord(value.notice) || !Object.keys(value.notice).length) issues.push({
|
|
133
|
+
message: "notice requires chat routing and a locale-to-text map",
|
|
134
|
+
path: [...path, "notice"]
|
|
135
|
+
});
|
|
136
|
+
else {
|
|
137
|
+
notice = {};
|
|
138
|
+
for (const [locale, text] of Object.entries(value.notice)) if (typeof text !== "string" || text.length > 2e3 || !text.includes("{persona}") || !text.includes("{url}")) issues.push({
|
|
139
|
+
message: "needs {persona} and {url} within 2000 characters",
|
|
140
|
+
path: [
|
|
141
|
+
...path,
|
|
142
|
+
"notice",
|
|
143
|
+
locale
|
|
144
|
+
]
|
|
145
|
+
});
|
|
146
|
+
else notice[locale] = text;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const available = value.available;
|
|
150
|
+
if (available !== void 0 && (rules === void 0 || !Array.isArray(available) || !available.every(validName))) issues.push({
|
|
151
|
+
message: "available requires chat rules and an array of persona names",
|
|
152
|
+
path: [...path, "available"]
|
|
153
|
+
});
|
|
81
154
|
const settings = {
|
|
155
|
+
...notice ? { notice } : {},
|
|
156
|
+
...rules === void 0 ? {} : { rules },
|
|
157
|
+
...Array.isArray(available) && available.every(validName) ? { available } : {},
|
|
82
158
|
byRole,
|
|
83
159
|
...typeof value.default === "string" && validName(value.default) ? { default: value.default } : {}
|
|
84
160
|
};
|
|
@@ -100,4 +176,4 @@ function personaSettings(value, issues, requireTexts = false) {
|
|
|
100
176
|
texts
|
|
101
177
|
};
|
|
102
178
|
}
|
|
103
|
-
export { PERSONAS_DIRECTORY, isPersonaName, personaFile, personaFor, personaNames, personaSettings };
|
|
179
|
+
export { PERSONAS_DIRECTORY, chatPersona, isPersonaName, personaFile, personaFor, personaNames, personaSettings };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.91.0",
|
|
4
4
|
"description": "The SegmentStream workspace agent: the ontology as tools, acting as the member who writes, and the browser, Telegram, WhatsApp and Slack through the workspace's connections.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "SegmentStream",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"@chat-adapter/state-memory": "4.42.0",
|
|
89
89
|
"@chat-adapter/telegram": "4.41.0",
|
|
90
90
|
"@chat-adapter/whatsapp": "4.42.0",
|
|
91
|
-
"@segmentstream/ontology": "0.
|
|
91
|
+
"@segmentstream/ontology": "0.172.0",
|
|
92
92
|
"@vercel/oidc": "^3.8.4",
|
|
93
93
|
"ai": "^7.0.172",
|
|
94
94
|
"chat": "4.42.0",
|