@segmentstream/ontology 0.111.0 → 0.113.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 +34 -0
- package/README.md +9 -3
- package/dist/analyze.d.ts +8 -4
- package/dist/analyze.js +72 -27
- package/dist/chat-channel.d.ts +27 -2
- package/dist/chat-channel.js +36 -2
- package/dist/email-html.d.ts +23 -0
- package/dist/email-html.js +249 -0
- package/dist/emails.d.ts +38 -6
- package/dist/emails.js +71 -2
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -1
- package/dist/manifest.d.ts +208 -52
- package/dist/manifest.js +32 -6
- package/dist/migrate/model.d.ts +98 -34
- package/dist/runtime/resolve-agent-identity.js +4 -1
- package/dist/service/agent/protocol.d.ts +4 -1
- package/dist/service/agent/protocol.js +7 -3
- package/dist/service/agent/server.js +8 -2
- package/dist/service/authentication.d.ts +2 -1
- package/dist/service/authentication.js +13 -3
- package/dist/service/chat/delivery.d.ts +10 -1
- package/dist/service/chat/delivery.js +21 -3
- package/dist/service/chat/envelope.d.ts +2 -2
- package/dist/service/chat/providers/custom.d.ts +13 -0
- package/dist/service/chat/providers/custom.js +103 -0
- package/dist/service/chat/providers/index.d.ts +1 -0
- package/dist/service/chat/providers/index.js +12 -3
- package/dist/service/chat/providers/provider.d.ts +19 -0
- package/dist/service/chat/reads.d.ts +1 -1
- package/dist/service/chat/reads.js +2 -2
- package/dist/service/chat/resolve.d.ts +2 -1
- package/dist/service/chat/resolve.js +6 -2
- package/dist/service/chat/review-delivery.d.ts +3 -0
- package/dist/service/chat/review-delivery.js +9 -3
- package/dist/service/chat/schemas.d.ts +2 -2
- package/dist/service/chat/schemas.js +2 -2
- package/dist/service/config.d.ts +6 -2
- package/dist/service/schemas.d.ts +30 -8
- package/dist/service/service.js +6 -1
- package/dist/sign-in.d.ts +22 -1
- package/dist/sign-in.js +3 -1
- package/dist/storage/tables.d.ts +4 -3
- package/dist/storage/tables.js +11 -3
- package/dist/workspace-channel.d.ts +54 -0
- package/dist/workspace-channel.js +36 -0
- package/package.json +5 -1
|
@@ -10,6 +10,7 @@ import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./i
|
|
|
10
10
|
import { channelProvider, chatConnection, chatKind, listedChatProviders, } from "./providers/index.js";
|
|
11
11
|
import { chatResolver } from "./resolve.js";
|
|
12
12
|
import { reviewSender } from "./review-delivery.js";
|
|
13
|
+
import { WORKSPACE_CHANNEL_KIND } from "../../chat-channel.js";
|
|
13
14
|
/** The channels a runtime source can name, which a verified pairing's provider is one of. */
|
|
14
15
|
const CHANNEL_PROVIDERS = new Set(["telegram", "slack", "whatsapp"]);
|
|
15
16
|
/**
|
|
@@ -30,7 +31,12 @@ export function sameChat(source, target) {
|
|
|
30
31
|
target.kind === "private" &&
|
|
31
32
|
source.peer === target.external_id);
|
|
32
33
|
}
|
|
33
|
-
export function makeChatDelivery(ontology, connections, archive, previews = {}
|
|
34
|
+
export function makeChatDelivery(ontology, connections, archive, previews = {},
|
|
35
|
+
/**
|
|
36
|
+
* The app's agent at the deployment's production origin, which sends through a workspace
|
|
37
|
+
* channel; `fetch` by default. Without an origin nothing goes through one.
|
|
38
|
+
*/
|
|
39
|
+
agent = {}) {
|
|
34
40
|
if (previews.settings) {
|
|
35
41
|
validateTelegramPreviews(previews.settings);
|
|
36
42
|
}
|
|
@@ -67,7 +73,10 @@ export function makeChatDelivery(ontology, connections, archive, previews = {})
|
|
|
67
73
|
const provider = channelProvider(ontology.manifest.chat, target.provider);
|
|
68
74
|
const connection = chatConnection(ontology.manifest.chat, target.provider);
|
|
69
75
|
const replyTo = input.replyTo ?? null;
|
|
70
|
-
|
|
76
|
+
// A workspace channel needs no connection of the ontology's: the agent sends through it.
|
|
77
|
+
if (provider === undefined ||
|
|
78
|
+
(!connection &&
|
|
79
|
+
chatKind(ontology.manifest.chat, target.provider) !== WORKSPACE_CHANNEL_KIND)) {
|
|
71
80
|
return yield* new ActionRejected({
|
|
72
81
|
message: "Chat posting requires a configured connection for the chat's provider.",
|
|
73
82
|
});
|
|
@@ -117,17 +126,24 @@ export function makeChatDelivery(ontology, connections, archive, previews = {})
|
|
|
117
126
|
});
|
|
118
127
|
}
|
|
119
128
|
}
|
|
120
|
-
return { target, requester, connection, provider, preview };
|
|
129
|
+
return { target, requester, connection: connection ?? "", provider, preview };
|
|
121
130
|
});
|
|
122
131
|
const report = (phase, cause) => Effect.logWarning("ontology.chat.delivery_diagnostic").pipe(Effect.annotateLogs({
|
|
123
132
|
phase,
|
|
124
133
|
requestId: context.requestId,
|
|
125
134
|
error: describeChatError(cause, (text) => context.identityToken ? text.split(context.identityToken).join("[redacted]") : text),
|
|
126
135
|
}));
|
|
136
|
+
// The agent, which sends through a workspace channel, with this request's identity token.
|
|
137
|
+
const agentTransport = {
|
|
138
|
+
origin: agent.origin,
|
|
139
|
+
identityToken: context.identityToken,
|
|
140
|
+
fetch: agent.fetch ?? ((request) => fetch(request)),
|
|
141
|
+
};
|
|
127
142
|
return {
|
|
128
143
|
sendReview: reviewSender({
|
|
129
144
|
ontology,
|
|
130
145
|
connections,
|
|
146
|
+
agent: agentTransport,
|
|
131
147
|
archive,
|
|
132
148
|
previews: destinationPreviews,
|
|
133
149
|
backend: context.backend,
|
|
@@ -229,6 +245,8 @@ export function makeChatDelivery(ontology, connections, archive, previews = {})
|
|
|
229
245
|
previews: destinationPreviews,
|
|
230
246
|
plainText: event.text,
|
|
231
247
|
replyTo: preview.replyTo,
|
|
248
|
+
agent: agentTransport,
|
|
249
|
+
operationId,
|
|
232
250
|
transport: connections({
|
|
233
251
|
identityToken: context.identityToken,
|
|
234
252
|
audit: {
|
|
@@ -72,7 +72,7 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
|
|
|
72
72
|
} | undefined;
|
|
73
73
|
} | {
|
|
74
74
|
readonly kind: "private" | "group";
|
|
75
|
-
readonly provider: "
|
|
75
|
+
readonly provider: "slack" | "whatsapp";
|
|
76
76
|
readonly chat_id: string;
|
|
77
77
|
readonly thread_id: string;
|
|
78
78
|
readonly message_id?: string | undefined;
|
|
@@ -126,7 +126,7 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
|
|
|
126
126
|
} | undefined;
|
|
127
127
|
} | {
|
|
128
128
|
readonly kind: "private" | "group";
|
|
129
|
-
readonly provider: "
|
|
129
|
+
readonly provider: "slack" | "whatsapp";
|
|
130
130
|
readonly chat_id: string;
|
|
131
131
|
readonly thread_id: string;
|
|
132
132
|
readonly message_id?: string | undefined;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type ChatProvider, type ChatSend, type ProviderOutcome } from "./provider.js";
|
|
3
|
+
/**
|
|
4
|
+
* One message through a workspace channel, by the agent's own code: `POST` the channel's send route
|
|
5
|
+
* on the deployment's production origin with the service's Vercel OIDC token, never retried. The
|
|
6
|
+
* agent's `4xx` sent nothing; a timeout, a `5xx` or an answer without its shape may have.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sendThroughAgent(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
9
|
+
/**
|
|
10
|
+
* A channel the workspace defines in its own code: a person's chat on it, by the id the channel's
|
|
11
|
+
* code keyed it with. Its chats are private, and the text is sent as written; the agent sends it.
|
|
12
|
+
*/
|
|
13
|
+
export declare const customProvider: ChatProvider;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { renderWorkspaceChannelText, WORKSPACE_CHANNEL_ID, WORKSPACE_CHANNEL_TEXT_LIMIT, workspaceChannelSendRoute, } from "../../../workspace-channel.js";
|
|
3
|
+
import { record } from "./provider.js";
|
|
4
|
+
/** How long the agent has to send one message through a workspace channel. */
|
|
5
|
+
const AGENT_SEND_TIMEOUT_MS = 30_000;
|
|
6
|
+
/**
|
|
7
|
+
* A chat, person or message id on a workspace channel, as the agent's channel takes them from the
|
|
8
|
+
* workspace's code: letters, digits and `:_.@+-`, as a chat's provenance carries them.
|
|
9
|
+
*/
|
|
10
|
+
const ID = WORKSPACE_CHANNEL_ID;
|
|
11
|
+
/**
|
|
12
|
+
* One message through a workspace channel, by the agent's own code: `POST` the channel's send route
|
|
13
|
+
* on the deployment's production origin with the service's Vercel OIDC token, never retried. The
|
|
14
|
+
* agent's `4xx` sent nothing; a timeout, a `5xx` or an answer without its shape may have.
|
|
15
|
+
*/
|
|
16
|
+
export function sendThroughAgent(input) {
|
|
17
|
+
return Effect.gen(function* () {
|
|
18
|
+
const payload = { text: input.finalText };
|
|
19
|
+
const agent = input.agent;
|
|
20
|
+
if (agent?.origin === undefined || agent.identityToken === undefined) {
|
|
21
|
+
return {
|
|
22
|
+
payload,
|
|
23
|
+
status: "failed",
|
|
24
|
+
messageId: null,
|
|
25
|
+
url: null,
|
|
26
|
+
cause: new Error("The agent's workspace channel is reachable only from the production deployment."),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const body = {
|
|
30
|
+
operation_id: input.operationId ?? input.destination.id,
|
|
31
|
+
// A reviewed reply names its chat. A chat post names only the person: the ontology knows
|
|
32
|
+
// their account, not their chat with the channel, which the agent finds.
|
|
33
|
+
chat_id: input.chatId ?? null,
|
|
34
|
+
recipient: input.destination.external_id,
|
|
35
|
+
thread_id: input.threadId ?? null,
|
|
36
|
+
reply_to: input.replyTo,
|
|
37
|
+
text: input.finalText,
|
|
38
|
+
};
|
|
39
|
+
const answered = yield* Effect.tryPromise(async () => {
|
|
40
|
+
const response = await agent.fetch(new Request(new URL(workspaceChannelSendRoute(input.destination.provider), agent.origin), {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: {
|
|
43
|
+
authorization: `Bearer ${agent.identityToken}`,
|
|
44
|
+
"content-type": "application/json",
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify(body),
|
|
47
|
+
signal: AbortSignal.timeout(AGENT_SEND_TIMEOUT_MS),
|
|
48
|
+
}));
|
|
49
|
+
const text = await response.text();
|
|
50
|
+
return { status: response.status, text };
|
|
51
|
+
}).pipe(Effect.exit);
|
|
52
|
+
if (answered._tag === "Failure") {
|
|
53
|
+
return { payload, status: "uncertain", messageId: null, url: null, cause: answered.cause };
|
|
54
|
+
}
|
|
55
|
+
const { status, text } = answered.value;
|
|
56
|
+
let answer = {};
|
|
57
|
+
try {
|
|
58
|
+
answer = record(JSON.parse(text));
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// The status still says what happened; the body only adds the message's id.
|
|
62
|
+
}
|
|
63
|
+
if (status >= 400 && status < 500) {
|
|
64
|
+
return {
|
|
65
|
+
payload,
|
|
66
|
+
status: "failed",
|
|
67
|
+
messageId: null,
|
|
68
|
+
url: null,
|
|
69
|
+
cause: new Error(`The agent's workspace channel refused the message (${typeof answer.error === "string" ? answer.error : `status ${status}`}).`),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const id = answer.message_id;
|
|
73
|
+
if (status < 200 ||
|
|
74
|
+
status >= 300 ||
|
|
75
|
+
!("message_id" in answer) ||
|
|
76
|
+
(id !== null && (typeof id !== "string" || !ID.test(id)))) {
|
|
77
|
+
return {
|
|
78
|
+
payload,
|
|
79
|
+
status: "uncertain",
|
|
80
|
+
messageId: null,
|
|
81
|
+
url: null,
|
|
82
|
+
cause: new Error(`The agent's workspace channel answered with status ${status}.`),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return { payload, status: "sent", messageId: id, url: null };
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A channel the workspace defines in its own code: a person's chat on it, by the id the channel's
|
|
90
|
+
* code keyed it with. Its chats are private, and the text is sent as written; the agent sends it.
|
|
91
|
+
*/
|
|
92
|
+
export const customProvider = {
|
|
93
|
+
provider: "custom",
|
|
94
|
+
limit: WORKSPACE_CHANNEL_TEXT_LIMIT,
|
|
95
|
+
isTargetId: (externalId, kind) => kind === "private" && ID.test(externalId),
|
|
96
|
+
isReplyId: (replyTo) => ID.test(replyTo),
|
|
97
|
+
render: (text, attribution, separator) => {
|
|
98
|
+
const finalText = renderWorkspaceChannelText(text, attribution, separator);
|
|
99
|
+
return { finalText, plainText: finalText };
|
|
100
|
+
},
|
|
101
|
+
payload: (finalText) => ({ text: finalText }),
|
|
102
|
+
send: sendThroughAgent,
|
|
103
|
+
};
|
|
@@ -10,6 +10,7 @@ export declare const CHAT_PROVIDERS: {
|
|
|
10
10
|
readonly slack: ChatProvider;
|
|
11
11
|
readonly whatsapp: ChatProvider;
|
|
12
12
|
readonly email: ChatProvider;
|
|
13
|
+
readonly custom: ChatProvider;
|
|
13
14
|
};
|
|
14
15
|
export type ChatProviderName = keyof typeof CHAT_PROVIDERS;
|
|
15
16
|
/** The provider of a kind of channel, when the platform posts to it. */
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isChatChannelKind, CHAT_CHANNEL_KINDS, WORKSPACE_CHANNEL_KIND, } from "../../../chat-channel.js";
|
|
2
|
+
import { customProvider } from "./custom.js";
|
|
1
3
|
import { emailProvider } from "./email.js";
|
|
2
4
|
import { slackProvider } from "./slack.js";
|
|
3
5
|
import { telegramProvider } from "./telegram.js";
|
|
@@ -11,8 +13,11 @@ export const CHAT_PROVIDERS = {
|
|
|
11
13
|
slack: slackProvider,
|
|
12
14
|
whatsapp: whatsappProvider,
|
|
13
15
|
email: emailProvider,
|
|
16
|
+
// A channel the workspace defines in its own code: the agent sends through it.
|
|
17
|
+
custom: customProvider,
|
|
14
18
|
};
|
|
15
|
-
|
|
19
|
+
/** The kinds whose channel is named after them; a workspace channel always has its own name. */
|
|
20
|
+
const NAMES = CHAT_CHANNEL_KINDS;
|
|
16
21
|
const isChatProvider = (name) => Object.hasOwn(CHAT_PROVIDERS, name);
|
|
17
22
|
/** The provider of a kind of channel, when the platform posts to it. */
|
|
18
23
|
export function chatProvider(kind) {
|
|
@@ -23,7 +28,11 @@ export function chatProvider(kind) {
|
|
|
23
28
|
* channel named after its kind, which a chat on it before channels had names also is.
|
|
24
29
|
*/
|
|
25
30
|
export function chatKind(chat, name) {
|
|
26
|
-
return
|
|
31
|
+
return isChatChannelKind(name)
|
|
32
|
+
? name
|
|
33
|
+
: name === WORKSPACE_CHANNEL_KIND
|
|
34
|
+
? undefined
|
|
35
|
+
: chat?.channels?.[name]?.kind;
|
|
27
36
|
}
|
|
28
37
|
/** The provider of the agent's channel `name`, by its kind, when the platform posts to it. */
|
|
29
38
|
export function channelProvider(chat, name) {
|
|
@@ -32,7 +41,7 @@ export function channelProvider(chat, name) {
|
|
|
32
41
|
}
|
|
33
42
|
/** The connection the ontology declares for the agent's channel `name`, if it declares one. */
|
|
34
43
|
export function chatConnection(chat, name) {
|
|
35
|
-
return
|
|
44
|
+
return isChatChannelKind(name) ? chat?.[name]?.connection : chat?.channels?.[name]?.connection;
|
|
36
45
|
}
|
|
37
46
|
/**
|
|
38
47
|
* The channels the ontology declares a posting connection for, by name: the destinations it posts
|
|
@@ -11,6 +11,16 @@ export interface ProviderOutcome {
|
|
|
11
11
|
readonly url: string | null;
|
|
12
12
|
readonly cause?: unknown;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* How the service reaches the app's agent, which sends through a workspace channel: the
|
|
16
|
+
* deployment's production origin and the service's Vercel OIDC token, each undefined outside
|
|
17
|
+
* production, and the transport, `fetch` by default.
|
|
18
|
+
*/
|
|
19
|
+
export interface AgentChannelTransport {
|
|
20
|
+
readonly origin: string | undefined;
|
|
21
|
+
readonly identityToken: string | undefined;
|
|
22
|
+
readonly fetch: (request: Request) => Promise<Response>;
|
|
23
|
+
}
|
|
14
24
|
/** What a provider sends: the exact text its `render` gave, to one resolved destination. */
|
|
15
25
|
export interface ChatSend {
|
|
16
26
|
readonly transport: ConnectionTransport["Service"];
|
|
@@ -27,6 +37,15 @@ export interface ChatSend {
|
|
|
27
37
|
readonly threadId?: string | null;
|
|
28
38
|
/** Telegram's link preview settings for its destination connection; others ignore them. */
|
|
29
39
|
readonly previews?: TelegramPreviewContext;
|
|
40
|
+
/** The app's agent, which sends through a workspace channel; others ignore it. */
|
|
41
|
+
readonly agent?: AgentChannelTransport;
|
|
42
|
+
/** The delivery's operation, which a workspace channel's send is named by; others ignore it. */
|
|
43
|
+
readonly operationId?: string;
|
|
44
|
+
/**
|
|
45
|
+
* The chat's own id where it differs from the destination's, as a reviewed reply's chat does;
|
|
46
|
+
* a workspace channel's send receives it, others ignore it.
|
|
47
|
+
*/
|
|
48
|
+
readonly chatId?: string;
|
|
30
49
|
}
|
|
31
50
|
/**
|
|
32
51
|
* A channel the platform posts exact chat messages to. Each provider names its own ids, renders
|
|
@@ -17,7 +17,7 @@ export declare function chatReads(backend: OntologyBackend["Service"], options:
|
|
|
17
17
|
items: {
|
|
18
18
|
external_id: string;
|
|
19
19
|
attribution: string | null;
|
|
20
|
-
kind?: "telegram" | "
|
|
20
|
+
kind?: "telegram" | "slack" | "whatsapp" | "email" | "custom" | undefined;
|
|
21
21
|
id: string;
|
|
22
22
|
title: string;
|
|
23
23
|
provider: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isChatKind } from "../../chat-channel.js";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
3
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
4
4
|
import { isAllowed } from "../../permissions.js";
|
|
@@ -38,7 +38,7 @@ export function chatReads(backend, options) {
|
|
|
38
38
|
id: item.id,
|
|
39
39
|
title: item.title,
|
|
40
40
|
provider: item.provider,
|
|
41
|
-
...(
|
|
41
|
+
...(isChatKind(kind) ? { kind } : {}),
|
|
42
42
|
external_id: item.external_id,
|
|
43
43
|
attribution: chatAttribution(caller, String(user.name), attribution),
|
|
44
44
|
};
|
|
@@ -29,7 +29,8 @@ export declare function withChatMembership(transport: ChatArchiveTransport, back
|
|
|
29
29
|
* Resolves the chats of `providers`, the agent's channels by name: their group chats in the
|
|
30
30
|
* registry, and each member's private chat on each channel whose kind they have a live identity
|
|
31
31
|
* on, `<channel>:private:<member id>`. People are not per channel: one identity of a kind reaches
|
|
32
|
-
* the member on every channel of that kind.
|
|
32
|
+
* the member on every channel of that kind. A workspace channel's accounts are its own
|
|
33
|
+
* (`channel:<name>`).
|
|
33
34
|
*/
|
|
34
35
|
export declare function chatResolver(backend: OntologyBackend["Service"], principal: Principal, providers: ReadonlyArray<string>, options?: {
|
|
35
36
|
/** Also resolve the group chats a member is in without chat read or post permission. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
+
import { channelIdentityProvider } from "../../chat-channel.js";
|
|
2
3
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
3
4
|
import { isAllowed } from "../../permissions.js";
|
|
4
5
|
import { ChatArchiveUnavailable } from "../agent/chat-archive.js";
|
|
@@ -69,7 +70,8 @@ export function withChatMembership(transport, backend) {
|
|
|
69
70
|
* Resolves the chats of `providers`, the agent's channels by name: their group chats in the
|
|
70
71
|
* registry, and each member's private chat on each channel whose kind they have a live identity
|
|
71
72
|
* on, `<channel>:private:<member id>`. People are not per channel: one identity of a kind reaches
|
|
72
|
-
* the member on every channel of that kind.
|
|
73
|
+
* the member on every channel of that kind. A workspace channel's accounts are its own
|
|
74
|
+
* (`channel:<name>`).
|
|
73
75
|
*/
|
|
74
76
|
export function chatResolver(backend, principal, providers, options = {}) {
|
|
75
77
|
const kindOf = options.kindOf ?? ((channel) => channel);
|
|
@@ -129,7 +131,9 @@ export function chatResolver(backend, principal, providers, options = {}) {
|
|
|
129
131
|
const identities = yield* backend.agents.listIdentities(member.principal.id);
|
|
130
132
|
for (const provider of named === undefined ? providers : [named]) {
|
|
131
133
|
const kind = kindOf(provider);
|
|
132
|
-
|
|
134
|
+
// The accounts of the channel's kind, or of a workspace channel its own.
|
|
135
|
+
const account = kind === undefined ? undefined : channelIdentityProvider(kind, provider);
|
|
136
|
+
const identity = identities.find((item) => account !== undefined && item.provider === account && item.revokedAt === null);
|
|
133
137
|
if (identity !== undefined) {
|
|
134
138
|
result.push({
|
|
135
139
|
id: `${provider}:private:${member.principal.id}`,
|
|
@@ -7,6 +7,7 @@ import type { Principal } from "../../runtime.js";
|
|
|
7
7
|
import type { ConnectionTransportFactory } from "../connections.js";
|
|
8
8
|
import type { OntologyBackend } from "../runtime-contract.js";
|
|
9
9
|
import type { DeliveryArchive } from "./archive.js";
|
|
10
|
+
import type { AgentChannelTransport } from "./providers/provider.js";
|
|
10
11
|
/**
|
|
11
12
|
* Sends a reply the agent held for review, once a member approved it: the draft or the member's
|
|
12
13
|
* edit, to the person's chat through the ontology's chat connection of the channel the chat is on,
|
|
@@ -18,6 +19,8 @@ import type { DeliveryArchive } from "./archive.js";
|
|
|
18
19
|
export declare function reviewSender(input: {
|
|
19
20
|
readonly ontology: Ontology;
|
|
20
21
|
readonly connections: ConnectionTransportFactory;
|
|
22
|
+
/** The app's agent, which sends through a workspace channel. */
|
|
23
|
+
readonly agent?: AgentChannelTransport;
|
|
21
24
|
readonly archive: DeliveryArchive;
|
|
22
25
|
/** Link previews for the destination connection, as a chat post gets them. */
|
|
23
26
|
readonly previews?: TelegramPreviewContext;
|
|
@@ -4,6 +4,7 @@ import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
|
4
4
|
import { effectiveScope } from "../../permissions.js";
|
|
5
5
|
import { describeChatError } from "./diagnostics.js";
|
|
6
6
|
import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./idempotency.js";
|
|
7
|
+
import { WORKSPACE_CHANNEL_KIND } from "../../chat-channel.js";
|
|
7
8
|
import { channelProvider, chatConnection, chatKind } from "./providers/index.js";
|
|
8
9
|
/**
|
|
9
10
|
* Sends a reply the agent held for review, once a member approved it: the draft or the member's
|
|
@@ -69,10 +70,12 @@ export function reviewSender(input) {
|
|
|
69
70
|
? review.channel
|
|
70
71
|
: String(review.provider);
|
|
71
72
|
const provider = channelProvider(input.ontology.manifest.chat, providerName);
|
|
72
|
-
const connection = chatConnection(input.ontology.manifest.chat, providerName);
|
|
73
|
+
const connection = chatConnection(input.ontology.manifest.chat, providerName) ?? "";
|
|
74
|
+
const channelKind = chatKind(input.ontology.manifest.chat, providerName);
|
|
75
|
+
// A workspace channel needs no connection of the ontology's: the agent sends through it.
|
|
73
76
|
if (provider === undefined ||
|
|
74
|
-
!connection ||
|
|
75
|
-
|
|
77
|
+
(!connection && channelKind !== WORKSPACE_CHANNEL_KIND) ||
|
|
78
|
+
channelKind !== String(review.provider)) {
|
|
76
79
|
return yield* new ActionRejected({
|
|
77
80
|
message: `Sending a reviewed reply needs chat.${providerName}.connection in defineOntology, ` +
|
|
78
81
|
`of kind ${String(review.provider)}: the connection of the bot, number or mailbox ` +
|
|
@@ -230,6 +233,9 @@ export function reviewSender(input) {
|
|
|
230
233
|
plainText,
|
|
231
234
|
replyTo,
|
|
232
235
|
threadId,
|
|
236
|
+
operationId,
|
|
237
|
+
chatId,
|
|
238
|
+
...(input.agent === undefined ? {} : { agent: input.agent }),
|
|
233
239
|
...(input.previews === undefined ? {} : { previews: input.previews }),
|
|
234
240
|
transport: input.connections({
|
|
235
241
|
identityToken: input.identityToken,
|
|
@@ -28,7 +28,7 @@ export declare const ChatDestination: Schema.Struct<{
|
|
|
28
28
|
readonly id: Schema.String;
|
|
29
29
|
readonly title: Schema.String;
|
|
30
30
|
readonly provider: Schema.String;
|
|
31
|
-
readonly kind: Schema.optionalKey<Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>>;
|
|
31
|
+
readonly kind: Schema.optionalKey<Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email", "custom"]>>;
|
|
32
32
|
readonly external_id: Schema.String;
|
|
33
33
|
readonly attribution: Schema.NullOr<Schema.String>;
|
|
34
34
|
}>;
|
|
@@ -37,7 +37,7 @@ export declare const ChatListResult: Schema.Struct<{
|
|
|
37
37
|
readonly id: Schema.String;
|
|
38
38
|
readonly title: Schema.String;
|
|
39
39
|
readonly provider: Schema.String;
|
|
40
|
-
readonly kind: Schema.optionalKey<Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>>;
|
|
40
|
+
readonly kind: Schema.optionalKey<Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email", "custom"]>>;
|
|
41
41
|
readonly external_id: Schema.String;
|
|
42
42
|
readonly attribution: Schema.NullOr<Schema.String>;
|
|
43
43
|
}>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
2
|
import { ChatPostInput } from "../../chat.js";
|
|
3
|
-
import {
|
|
3
|
+
import { CHAT_KINDS } from "../../chat-channel.js";
|
|
4
4
|
const Id = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512));
|
|
5
5
|
export const ChatPostWire = ChatPostInput.pipe(Schema.encodeKeys({ replyTo: "reply_to", idempotencyKey: "idempotency_key" }));
|
|
6
6
|
export const RuntimeChatPostWire = Schema.Struct({
|
|
@@ -26,7 +26,7 @@ export const ChatDestination = Schema.Struct({
|
|
|
26
26
|
description: "The agent's channel the chat is on, by its name: its kind for the channel named after " +
|
|
27
27
|
"its kind, such as telegram.",
|
|
28
28
|
}),
|
|
29
|
-
kind: Schema.optionalKey(Schema.Literals(
|
|
29
|
+
kind: Schema.optionalKey(Schema.Literals(CHAT_KINDS).annotate({
|
|
30
30
|
description: "The kind of that channel, which decides how a message to the chat renders.",
|
|
31
31
|
})),
|
|
32
32
|
external_id: Id,
|
package/dist/service/config.d.ts
CHANGED
|
@@ -122,7 +122,8 @@ export interface OntologyEnvironment {
|
|
|
122
122
|
readonly localConnections?: LocalConnectionContext;
|
|
123
123
|
/**
|
|
124
124
|
* The deployment's production origin, `https://` and `VERCEL_PROJECT_PRODUCTION_URL`, where
|
|
125
|
-
* `agentTask` hands the app's agent its tasks
|
|
125
|
+
* `agentTask` hands the app's agent its tasks, a workspace channel's approved replies and chat
|
|
126
|
+
* posts reach the agent, and sign-in links sent from actions open.
|
|
126
127
|
* Production only: without it, and without `runtimeIdentity`, `agentTask` answers
|
|
127
128
|
* `agent_unavailable`, and `User.sendSignInLink` marks its emails failed.
|
|
128
129
|
*/
|
|
@@ -204,7 +205,10 @@ export interface OntologyServiceConfig extends OntologyEnvironment {
|
|
|
204
205
|
* replace it.
|
|
205
206
|
*/
|
|
206
207
|
readonly emailCodes?: import("./agent/guest-sign-up.js").EmailCodes;
|
|
207
|
-
/**
|
|
208
|
+
/**
|
|
209
|
+
* How `agentTask`, and a workspace channel's approved reply or chat post, call the app's agent at
|
|
210
|
+
* `agentOrigin`: `fetch` by default. Tests replace it.
|
|
211
|
+
*/
|
|
208
212
|
readonly agentTaskTransport?: import("./agent/task-start.js").AgentTransport;
|
|
209
213
|
/**
|
|
210
214
|
* How `discover` asks the decision model of `defineOntology({ discovery })`: AI Gateway, with
|
|
@@ -470,14 +470,25 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
470
470
|
readonly botCheck: "bot_check";
|
|
471
471
|
}>>;
|
|
472
472
|
readonly invitationEmail: Schema.optionalKey<Schema.String>;
|
|
473
|
+
readonly linkEmail: Schema.optionalKey<Schema.String>;
|
|
474
|
+
readonly codeEmail: Schema.optionalKey<Schema.String>;
|
|
473
475
|
}>, {
|
|
474
476
|
readonly onNewUser: "on_new_user";
|
|
475
477
|
readonly codeLink: "code_link";
|
|
476
478
|
readonly signUp: "sign_up";
|
|
477
479
|
readonly onSignUp: "on_sign_up";
|
|
478
480
|
readonly invitationEmail: "invitation_email";
|
|
481
|
+
readonly linkEmail: "link_email";
|
|
482
|
+
readonly codeEmail: "code_email";
|
|
479
483
|
}>>;
|
|
480
|
-
readonly emails: Schema.optionalKey<Schema.$Record<Schema.String, Schema.encodeKeys<Schema.Struct<{
|
|
484
|
+
readonly emails: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Union<readonly [Schema.encodeKeys<Schema.Struct<{
|
|
485
|
+
readonly subject: Schema.String;
|
|
486
|
+
readonly html: Schema.String;
|
|
487
|
+
readonly text: Schema.String;
|
|
488
|
+
readonly replyTo: Schema.optionalKey<Schema.String>;
|
|
489
|
+
}>, {
|
|
490
|
+
readonly replyTo: "reply_to";
|
|
491
|
+
}>, Schema.encodeKeys<Schema.Struct<{
|
|
481
492
|
readonly subject: Schema.String;
|
|
482
493
|
readonly eyebrow: Schema.optionalKey<Schema.String>;
|
|
483
494
|
readonly heading: Schema.String;
|
|
@@ -506,7 +517,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
506
517
|
readonly replyTo: Schema.optionalKey<Schema.String>;
|
|
507
518
|
}>, {
|
|
508
519
|
readonly replyTo: "reply_to";
|
|
509
|
-
}>>>;
|
|
520
|
+
}>]>>>;
|
|
510
521
|
readonly connections: Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
511
522
|
readonly key: Schema.String;
|
|
512
523
|
readonly description: Schema.String;
|
|
@@ -526,8 +537,8 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
526
537
|
readonly connection: Schema.String;
|
|
527
538
|
}>>;
|
|
528
539
|
readonly channels: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Struct<{
|
|
529
|
-
readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>;
|
|
530
|
-
readonly connection: Schema.String
|
|
540
|
+
readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email", "custom"]>;
|
|
541
|
+
readonly connection: Schema.optionalKey<Schema.String>;
|
|
531
542
|
}>>>;
|
|
532
543
|
readonly attribution: Schema.optionalKey<Schema.Struct<{
|
|
533
544
|
readonly template: Schema.optionalKey<Schema.String>;
|
|
@@ -777,14 +788,25 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
777
788
|
readonly botCheck: "bot_check";
|
|
778
789
|
}>>;
|
|
779
790
|
readonly invitationEmail: Schema.optionalKey<Schema.String>;
|
|
791
|
+
readonly linkEmail: Schema.optionalKey<Schema.String>;
|
|
792
|
+
readonly codeEmail: Schema.optionalKey<Schema.String>;
|
|
780
793
|
}>, {
|
|
781
794
|
readonly onNewUser: "on_new_user";
|
|
782
795
|
readonly codeLink: "code_link";
|
|
783
796
|
readonly signUp: "sign_up";
|
|
784
797
|
readonly onSignUp: "on_sign_up";
|
|
785
798
|
readonly invitationEmail: "invitation_email";
|
|
799
|
+
readonly linkEmail: "link_email";
|
|
800
|
+
readonly codeEmail: "code_email";
|
|
786
801
|
}>>>;
|
|
787
|
-
readonly emails: Schema.toEncoded<Schema.optionalKey<Schema.$Record<Schema.String, Schema.encodeKeys<Schema.Struct<{
|
|
802
|
+
readonly emails: Schema.toEncoded<Schema.optionalKey<Schema.$Record<Schema.String, Schema.Union<readonly [Schema.encodeKeys<Schema.Struct<{
|
|
803
|
+
readonly subject: Schema.String;
|
|
804
|
+
readonly html: Schema.String;
|
|
805
|
+
readonly text: Schema.String;
|
|
806
|
+
readonly replyTo: Schema.optionalKey<Schema.String>;
|
|
807
|
+
}>, {
|
|
808
|
+
readonly replyTo: "reply_to";
|
|
809
|
+
}>, Schema.encodeKeys<Schema.Struct<{
|
|
788
810
|
readonly subject: Schema.String;
|
|
789
811
|
readonly eyebrow: Schema.optionalKey<Schema.String>;
|
|
790
812
|
readonly heading: Schema.String;
|
|
@@ -813,7 +835,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
813
835
|
readonly replyTo: Schema.optionalKey<Schema.String>;
|
|
814
836
|
}>, {
|
|
815
837
|
readonly replyTo: "reply_to";
|
|
816
|
-
}>>>>;
|
|
838
|
+
}>]>>>>;
|
|
817
839
|
readonly connections: Schema.toEncoded<Schema.optionalKey<Schema.$Array<Schema.Struct<{
|
|
818
840
|
readonly key: Schema.String;
|
|
819
841
|
readonly description: Schema.String;
|
|
@@ -833,8 +855,8 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
|
|
|
833
855
|
readonly connection: Schema.String;
|
|
834
856
|
}>>;
|
|
835
857
|
readonly channels: Schema.optionalKey<Schema.$Record<Schema.String, Schema.Struct<{
|
|
836
|
-
readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email"]>;
|
|
837
|
-
readonly connection: Schema.String
|
|
858
|
+
readonly kind: Schema.Literals<readonly ["telegram", "slack", "whatsapp", "email", "custom"]>;
|
|
859
|
+
readonly connection: Schema.optionalKey<Schema.String>;
|
|
838
860
|
}>>>;
|
|
839
861
|
readonly attribution: Schema.optionalKey<Schema.Struct<{
|
|
840
862
|
readonly template: Schema.optionalKey<Schema.String>;
|
package/dist/service/service.js
CHANGED
|
@@ -342,7 +342,12 @@ export function createOntologyService(ontology, config) {
|
|
|
342
342
|
? deployedConnections(config)
|
|
343
343
|
: localConnections(config.localConnections)));
|
|
344
344
|
const replicate = deployedReplication(config);
|
|
345
|
-
const chatDelivery = makeChatDelivery(ontology, connections, config.chatArchive ?? platformDeliveryArchive(config), { settings: config.channels?.telegram, ownOrigins: config.appOrigins }
|
|
345
|
+
const chatDelivery = makeChatDelivery(ontology, connections, config.chatArchive ?? platformDeliveryArchive(config), { settings: config.channels?.telegram, ownOrigins: config.appOrigins },
|
|
346
|
+
// The app's agent sends through a workspace channel, at the origin it takes its tasks at.
|
|
347
|
+
{
|
|
348
|
+
origin: config.agentOrigin,
|
|
349
|
+
...(config.agentTaskTransport === undefined ? {} : { fetch: config.agentTaskTransport }),
|
|
350
|
+
});
|
|
346
351
|
const chatHistory = config.runtimeIdentity === undefined ? undefined : platformChatArchive(config);
|
|
347
352
|
const metricQueries = makeMetricQueries({
|
|
348
353
|
ontology,
|
package/dist/sign-in.d.ts
CHANGED
|
@@ -17,7 +17,12 @@ export type ChannelProvider = (typeof CHANNEL_PROVIDERS)[number];
|
|
|
17
17
|
* Telegram Mini App.
|
|
18
18
|
*/
|
|
19
19
|
export declare const SIGN_UP_CHANNELS: readonly ["email", "telegram", "whatsapp", "web"];
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The people of a channel the workspace defines in its own code, `channel:<name>`, such as
|
|
22
|
+
* `channel:zernio`: the accounts they write from there, which are that channel's alone.
|
|
23
|
+
*/
|
|
24
|
+
export type WorkspaceSignUpChannel = `channel:${string}`;
|
|
25
|
+
export type SignUpChannel = (typeof SIGN_UP_CHANNELS)[number] | WorkspaceSignUpChannel;
|
|
21
26
|
/** The sign-up channels a person reaches through a chat, which need a matching `chat` adapter. */
|
|
22
27
|
export declare const SIGN_UP_CHAT_CHANNELS: ReadonlyArray<SignUpChannel>;
|
|
23
28
|
export declare function isSignUpChannel(value: unknown): value is SignUpChannel;
|
|
@@ -148,6 +153,22 @@ export interface SignInOptions {
|
|
|
148
153
|
* invitation when omitted. A checked ontology holds the email's wire name.
|
|
149
154
|
*/
|
|
150
155
|
readonly invitationEmail?: string;
|
|
156
|
+
/**
|
|
157
|
+
* One of the ontology's `emails`, by its camelCase name, that the platform sends instead of its
|
|
158
|
+
* own sign-in link: to anyone who asks the app for one, and when `User.sendSignInLink` names no
|
|
159
|
+
* template for a member who has signed in before. Like the invitation, it may use only the
|
|
160
|
+
* built-in values. The platform's own sign-in email when omitted.
|
|
161
|
+
*/
|
|
162
|
+
readonly linkEmail?: string;
|
|
163
|
+
/**
|
|
164
|
+
* One of the ontology's `emails`, by its camelCase name, that the platform sends instead of its
|
|
165
|
+
* own sign-in code, to a member who asks for one; a verification or reset code keeps the
|
|
166
|
+
* platform's. Besides the built-in values it shows `{{code}}`,
|
|
167
|
+
* the code, for instance as a section's value to copy. Its button opens the app's sign-in page
|
|
168
|
+
* with the code filled in when `codeLink` is on and is left out otherwise. The platform's own
|
|
169
|
+
* code email when omitted.
|
|
170
|
+
*/
|
|
171
|
+
readonly codeEmail?: string;
|
|
151
172
|
}
|
|
152
173
|
/** The sign-in options of a checked ontology, with the sign-up its definition declares. */
|
|
153
174
|
export interface ResolvedSignInOptions extends SignInOptions {
|
package/dist/sign-in.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { WORKSPACE_IDENTITY_PROVIDER } from "./chat-channel.js";
|
|
1
2
|
/**
|
|
2
3
|
* Who may become a member by signing in. `invited`: only a person whose email a member row already
|
|
3
4
|
* holds, created by an invitation or a role grant; anyone else is refused. `open`: anyone who signs
|
|
@@ -19,7 +20,8 @@ export const SIGN_UP_CHANNELS = ["email", "telegram", "whatsapp", "web"];
|
|
|
19
20
|
/** The sign-up channels a person reaches through a chat, which need a matching `chat` adapter. */
|
|
20
21
|
export const SIGN_UP_CHAT_CHANNELS = ["telegram", "whatsapp"];
|
|
21
22
|
export function isSignUpChannel(value) {
|
|
22
|
-
return SIGN_UP_CHANNELS.includes(value)
|
|
23
|
+
return (SIGN_UP_CHANNELS.includes(value) ||
|
|
24
|
+
(typeof value === "string" && WORKSPACE_IDENTITY_PROVIDER.test(value)));
|
|
23
25
|
}
|
|
24
26
|
/** The channels a guest talks to the agent through: only `web`, a website's chat, so far. */
|
|
25
27
|
export const GUEST_CHANNELS = ["web"];
|
package/dist/storage/tables.d.ts
CHANGED
|
@@ -3,10 +3,11 @@ export declare const BLOB_TABLES_DDL: ReadonlyArray<string>;
|
|
|
3
3
|
/** The agent's temporary attachments, beside the blob tables whose storage they share. */
|
|
4
4
|
export declare const ATTACHMENTS_TABLE = "_agent_attachments";
|
|
5
5
|
/**
|
|
6
|
-
* Where an attachment comes from: a chat message on a channel,
|
|
7
|
-
*
|
|
6
|
+
* Where an attachment comes from: a chat message on a channel, `channel` for a channel the
|
|
7
|
+
* workspace defines in its own code, or a file the agent produced in its sandbox and stores so
|
|
8
|
+
* that an action can keep it.
|
|
8
9
|
*/
|
|
9
|
-
export declare const ATTACHMENT_SOURCES: readonly ["telegram", "slack", "whatsapp", "email", "sandbox"];
|
|
10
|
+
export declare const ATTACHMENT_SOURCES: readonly ["telegram", "slack", "whatsapp", "email", "sandbox", "channel"];
|
|
10
11
|
/** The check that limits `source` to {@link ATTACHMENT_SOURCES}; its name is PostgreSQL's default. */
|
|
11
12
|
export declare const ATTACHMENT_SOURCE_CHECK = "_agent_attachments_source_check";
|
|
12
13
|
/**
|