@segmentstream/ontology 0.182.0 → 0.184.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 +62 -0
- package/README.md +23 -15
- package/dist/action.d.ts +11 -4
- package/dist/agent-chat.d.ts +6 -0
- package/dist/agent-chat.js +43 -28
- package/dist/analyze.d.ts +6 -1
- package/dist/analyze.js +51 -19
- package/dist/authoring.js +7 -1
- package/dist/channel-files.d.ts +17 -0
- package/dist/chat.d.ts +32 -3
- package/dist/chat.js +33 -12
- package/dist/email.d.ts +3 -7
- package/dist/manifest.d.ts +64 -0
- package/dist/manifest.js +9 -0
- package/dist/migrate/model.d.ts +16 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +17 -0
- package/dist/render.d.ts +6 -0
- package/dist/render.js +25 -10
- package/dist/runtime/pipeline.d.ts +3 -3
- package/dist/runtime/pipeline.js +11 -4
- package/dist/service/catalog.js +3 -3
- package/dist/service/chat/chat-draft.d.ts +1 -1
- package/dist/service/chat/delivery.js +43 -22
- package/dist/service/chat/providers/email.js +13 -37
- package/dist/service/chat/providers/files-send.d.ts +2 -0
- package/dist/service/chat/providers/files-send.js +2 -0
- package/dist/service/chat/providers/provider.d.ts +15 -1
- package/dist/service/chat/providers/provider.js +53 -0
- package/dist/service/chat/providers/telegram.d.ts +21 -8
- package/dist/service/chat/providers/telegram.js +220 -48
- package/dist/service/chat/routes.js +2 -0
- package/dist/service/chat/schemas.d.ts +40 -0
- package/dist/service/chat/schemas.js +3 -1
- package/dist/service/mcp/server.js +2 -0
- package/dist/service/mcp/tools.d.ts +10 -2
- package/dist/service/mcp/tools.js +11 -4
- package/dist/service/operations.js +8 -4
- package/dist/service/plan.d.ts +3 -1
- package/dist/service/plan.js +4 -2
- package/dist/service/schemas.d.ts +4 -0
- package/dist/service/service.js +2 -2
- package/dist/telegram.d.ts +84 -1
- package/dist/telegram.js +14 -0
- package/package.json +1 -1
package/dist/runtime/pipeline.js
CHANGED
|
@@ -16,7 +16,7 @@ import { beginReplicationWrite } from "../replication/transaction.js";
|
|
|
16
16
|
import { RuntimeDeployment } from "./deployment.js";
|
|
17
17
|
import { requireDeploymentCapability } from "../deployment.js";
|
|
18
18
|
import { authorizeBlobOperation } from "../storage/authorization.js";
|
|
19
|
-
import { effectiveScope, generatedActionPermissions,
|
|
19
|
+
import { effectiveScope, generatedActionPermissions, missingActionPermission, parsePermission, } from "../permissions.js";
|
|
20
20
|
import { OntologyReads, OntologyRuntime } from "../runtime.js";
|
|
21
21
|
import { UnknownAction, ValidationIssue, WorkflowRecoveryUnavailable, } from "./errors.js";
|
|
22
22
|
import { generatedActions, writesOnlyThroughActions } from "./generated-actions.js";
|
|
@@ -72,7 +72,13 @@ export function resolveAction(ontology, entityName, actionName) {
|
|
|
72
72
|
}
|
|
73
73
|
const authored = entity.actions.find((candidate) => candidate.name === actionName);
|
|
74
74
|
if (authored !== undefined) {
|
|
75
|
-
return {
|
|
75
|
+
return {
|
|
76
|
+
action: authored.action,
|
|
77
|
+
...(authored.anyOf === undefined
|
|
78
|
+
? { permissions: [authored.permission] }
|
|
79
|
+
: { permissions: authored.anyOf, anyPermission: true }),
|
|
80
|
+
generated: false,
|
|
81
|
+
};
|
|
76
82
|
}
|
|
77
83
|
const generated = generatedActions(ontology, entityName)[actionName];
|
|
78
84
|
return generated === undefined
|
|
@@ -205,10 +211,11 @@ export function runAction(ontology, request) {
|
|
|
205
211
|
if (!decoded.ok) {
|
|
206
212
|
return { result: null, validation: { ok: false, issues: decoded.issues }, edits: [] };
|
|
207
213
|
}
|
|
208
|
-
const missing =
|
|
214
|
+
const missing = missingActionPermission(found, request.principal);
|
|
209
215
|
if (missing !== undefined) {
|
|
210
216
|
return yield* new PermissionDenied({
|
|
211
|
-
|
|
217
|
+
// Of an action several permissions open, the first it names.
|
|
218
|
+
permission: found.anyPermission === true ? found.permissions[0] : missing,
|
|
212
219
|
message: `${qualified} requires ${missing}, which the principal's roles do not grant.`,
|
|
213
220
|
});
|
|
214
221
|
}
|
package/dist/service/catalog.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isValueMatched } from "../manifest.js";
|
|
2
|
-
import { isAllowed } from "../permissions.js";
|
|
3
|
-
import {
|
|
2
|
+
import { allowsAction, isAllowed } from "../permissions.js";
|
|
3
|
+
import { actionRequirement, modelMetricsFor } from "../render.js";
|
|
4
4
|
/**
|
|
5
5
|
* The catalog for a reader: the manifest, with each entity's actions narrowed to the ones the given
|
|
6
6
|
* permissions may run, and only the metrics of the model whose every input entity they may read.
|
|
@@ -29,7 +29,7 @@ export function catalogFor(manifest, permissions) {
|
|
|
29
29
|
isAllowed(entity.permissions?.read ?? `${entity.name}:read`, { permissions }))
|
|
30
30
|
.map((entity) => ({
|
|
31
31
|
...entity,
|
|
32
|
-
actions: entity.actions.filter((action) =>
|
|
32
|
+
actions: entity.actions.filter((action) => allowsAction(actionRequirement(entity.name, action), { permissions })),
|
|
33
33
|
})),
|
|
34
34
|
};
|
|
35
35
|
}
|
|
@@ -10,7 +10,7 @@ import type { Destination } from "./resolve.js";
|
|
|
10
10
|
export declare const CHAT_DRAFT_PERMISSION = "agent_chat:draft";
|
|
11
11
|
/** The key prefix of a draft the agent writes on request: `request:<request>`. */
|
|
12
12
|
export declare const REQUESTED_DRAFT_KEY = "request:";
|
|
13
|
-
/** A private chat the inbox registered, as `agent_chat.send`
|
|
13
|
+
/** A private chat the inbox registered, as `agent_chat.send` finds it in either voice. */
|
|
14
14
|
export interface DraftChat {
|
|
15
15
|
readonly target: Destination;
|
|
16
16
|
readonly chatId: string;
|
|
@@ -69,22 +69,26 @@ chats) {
|
|
|
69
69
|
const destinationPreviews = previews.settings?.connection === destination ? previews : { ownOrigins: previews.ownOrigins };
|
|
70
70
|
return (context) => {
|
|
71
71
|
/**
|
|
72
|
-
* A
|
|
73
|
-
* sender attribution, under `agent_chat:send
|
|
72
|
+
* A message's destination, requester and exact text, in the voice `as` names: as the agent, by
|
|
73
|
+
* default, without the sender attribution, under `agent_chat:send`, the requester still
|
|
74
|
+
* recorded with it; or, with `as: "me"`, signed as the requester, under
|
|
75
|
+
* `agent_chat:post_message`. The voice's own permission is checked here, whichever of the two
|
|
76
|
+
* opened the action.
|
|
74
77
|
*/
|
|
75
|
-
const check = (input, approved = true
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const check = (input, approved = true) => Effect.gen(function* () {
|
|
79
|
+
const asAgent = input.as !== "me";
|
|
80
|
+
const template = input.template;
|
|
81
|
+
const buttons = input.buttons;
|
|
82
|
+
// A message is text, or on WhatsApp an approved template.
|
|
83
|
+
if ((input.text === undefined) === (template === undefined)) {
|
|
80
84
|
return yield* new ActionRejected({
|
|
81
85
|
message: "Give exactly one of text and template.",
|
|
82
86
|
});
|
|
83
87
|
}
|
|
84
88
|
// An approved template has its own buttons, given by their values.
|
|
85
|
-
if (buttons !== undefined &&
|
|
89
|
+
if (buttons !== undefined && template !== undefined) {
|
|
86
90
|
return yield* new ActionRejected({
|
|
87
|
-
message: "Buttons go under a text
|
|
91
|
+
message: "Buttons go under a text; a template's are its own.",
|
|
88
92
|
});
|
|
89
93
|
}
|
|
90
94
|
const permission = asAgent ? "agent_chat:send" : "agent_chat:post_message";
|
|
@@ -101,6 +105,14 @@ chats) {
|
|
|
101
105
|
message: "The agent's sessions do not send as the agent: they answer in their chats.",
|
|
102
106
|
});
|
|
103
107
|
}
|
|
108
|
+
// A session posts for its member only the exact text they approved in their chat, whose
|
|
109
|
+
// frozen preview binds no template, buttons or files.
|
|
110
|
+
if (context.caller.kind === "runtime" &&
|
|
111
|
+
(template !== undefined || buttons !== undefined || input.files !== undefined)) {
|
|
112
|
+
return yield* new ActionRejected({
|
|
113
|
+
message: "The agent's sessions post only the text a member approved, as written.",
|
|
114
|
+
});
|
|
115
|
+
}
|
|
104
116
|
const user = yield* context.backend.get({ entity: "user", id: context.principal.id });
|
|
105
117
|
if (user.active !== true || user.kind !== context.principal.kind) {
|
|
106
118
|
return yield* new PermissionDenied({ message: "The requester is inactive." });
|
|
@@ -205,6 +217,16 @@ chats) {
|
|
|
205
217
|
message: "Templates are sent on WhatsApp; this chat's channel takes text.",
|
|
206
218
|
});
|
|
207
219
|
}
|
|
220
|
+
// WhatsApp sends an approved template's text as it is: nothing can sign it. In the
|
|
221
|
+
// caller's voice it goes only where their post carries no name, as a member's own reply
|
|
222
|
+
// in the inbox does where the inbox's `attribution` is `none`.
|
|
223
|
+
if (signature !== "") {
|
|
224
|
+
return yield* new ActionRejected({
|
|
225
|
+
message: "A template cannot carry the sender attribution; nothing was sent. Send it as " +
|
|
226
|
+
"the agent, or as text.",
|
|
227
|
+
refusal: "attribution_unsupported",
|
|
228
|
+
});
|
|
229
|
+
}
|
|
208
230
|
const read = provider.template;
|
|
209
231
|
const request = {
|
|
210
232
|
name: template.name,
|
|
@@ -574,15 +596,17 @@ chats) {
|
|
|
574
596
|
identityToken: context.identityToken,
|
|
575
597
|
requestId: context.requestId,
|
|
576
598
|
}),
|
|
577
|
-
preview: (input) => check(input, false).pipe(Effect.flatMap(({ compose }) => compose), Effect.map(({ preview }) => preview)),
|
|
599
|
+
preview: (input) => check({ ...input, as: "me" }, false).pipe(Effect.flatMap(({ compose }) => compose), Effect.map(({ preview }) => preview)),
|
|
578
600
|
validate: (input) => check(input).pipe(Effect.asVoid),
|
|
579
|
-
|
|
580
|
-
|
|
601
|
+
// @legacy ontology.chat-post-message
|
|
602
|
+
post: (input) => deliver({ ...input, as: "me" }),
|
|
603
|
+
send: (input) => deliver(input),
|
|
581
604
|
};
|
|
582
|
-
/** One delivery:
|
|
583
|
-
function deliver(input
|
|
605
|
+
/** One delivery: as the agent without attribution, or with `as: "me"` signed as the caller. */
|
|
606
|
+
function deliver(input) {
|
|
584
607
|
return Effect.gen(function* () {
|
|
585
|
-
const
|
|
608
|
+
const asAgent = input.as !== "me";
|
|
609
|
+
const checked = yield* check(input);
|
|
586
610
|
const { target, requester, connection, provider, inbox, compose } = checked;
|
|
587
611
|
const buttons = "buttons" in checked ? checked.buttons : undefined;
|
|
588
612
|
const scope = deliveryScopeDigest({
|
|
@@ -626,13 +650,12 @@ chats) {
|
|
|
626
650
|
});
|
|
627
651
|
// A post's stored files, as the caller names them. The key binds those names: a retry after
|
|
628
652
|
// a file changed or went recovers the first attempt's receipt, read before the files are.
|
|
629
|
-
const named =
|
|
653
|
+
const named = input.files ?? [];
|
|
630
654
|
const references = named.map((file) => [file.entity, file.id.toLowerCase(), file.property ?? "content", ""]);
|
|
631
655
|
const fingerprint = references.length === 0 || !("preview" in checked) || checked.preview === undefined
|
|
632
656
|
? checked.fingerprint
|
|
633
657
|
: deliveryFingerprint(checked.preview, requester, undefined, buttons, undefined, references);
|
|
634
|
-
if (named.length > 0 &&
|
|
635
|
-
(("template" in input && input.template !== undefined) || archive === cloud)) {
|
|
658
|
+
if (named.length > 0 && (input.template !== undefined || archive === cloud)) {
|
|
636
659
|
// @legacy agent.cloud-chat-archive Files go only through the workspace chat store.
|
|
637
660
|
return yield* new ActionRejected({
|
|
638
661
|
message: "The post's files cannot be sent (files_unsupported); nothing was sent.",
|
|
@@ -642,9 +665,7 @@ chats) {
|
|
|
642
665
|
// A message read when it is sent, a template, its files, or the people of a shared
|
|
643
666
|
// thread's reply, may read differently by the time its key is replayed, or not at all: a
|
|
644
667
|
// replay recovers the first attempt's receipt before it.
|
|
645
|
-
if ((
|
|
646
|
-
named.length > 0 ||
|
|
647
|
-
inbox?.shared === true) &&
|
|
668
|
+
if ((input.template !== undefined || named.length > 0 || inbox?.shared === true) &&
|
|
648
669
|
archive.find !== undefined) {
|
|
649
670
|
const found = yield* archive
|
|
650
671
|
.find({ delivery: { scope, key, fingerprint }, identityToken: context.identityToken })
|
|
@@ -692,7 +713,7 @@ chats) {
|
|
|
692
713
|
const transport = connections({
|
|
693
714
|
identityToken: context.identityToken,
|
|
694
715
|
audit: {
|
|
695
|
-
action:
|
|
716
|
+
action: "agent_chat.send",
|
|
696
717
|
principalId: context.principal.id,
|
|
697
718
|
requestId: context.requestId,
|
|
698
719
|
},
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Effect } from "effect";
|
|
1
|
+
import { Cause, Effect } from "effect";
|
|
2
2
|
import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
|
|
3
3
|
import { base64Url, emailDeliverRoute, EMAIL_FILES_BYTES, EMAIL_MESSAGE_LIMIT, emailReplyMessage, GmailError, gmailExplanation, GmailMaybeSent, renderEmailHtml, replyAllRecipients, sendGmailMessage, withEmailSignature, } from "../../../email.js";
|
|
4
4
|
import { mailboxSignature } from "../../../gmail-send.js";
|
|
5
5
|
import { ActionRejected } from "../../../errors.js";
|
|
6
6
|
import { EMAIL_PATTERN } from "../../../property.js";
|
|
7
7
|
import { describeChatError } from "../diagnostics.js";
|
|
8
|
-
import { channelRequest, definiteTransportFailure, filesWithBytes, record, transportCall, transportFailure, } from "./provider.js";
|
|
8
|
+
import { agentFileLinks, channelRequest, definiteTransportFailure, filesWithBytes, postToAgent, record, transportCall, transportFailure, } from "./provider.js";
|
|
9
9
|
/** A Gmail id, of a thread or a message. */
|
|
10
10
|
const GMAIL_ID = /^[A-Za-z0-9_-]{1,512}$/;
|
|
11
11
|
const decoder = new TextDecoder();
|
|
@@ -117,7 +117,6 @@ const AGENT_DELIVER_TIMEOUT_MS = 90_000;
|
|
|
117
117
|
*/
|
|
118
118
|
function deliverThroughAgent(input, reply) {
|
|
119
119
|
return Effect.gen(function* () {
|
|
120
|
-
const agent = input.agent;
|
|
121
120
|
const payload = {
|
|
122
121
|
threadId: reply.threadId,
|
|
123
122
|
to: reply.to,
|
|
@@ -132,20 +131,9 @@ function deliverThroughAgent(input, reply) {
|
|
|
132
131
|
url: null,
|
|
133
132
|
cause,
|
|
134
133
|
});
|
|
135
|
-
|
|
136
|
-
const links = yield* Effect.tryPromise(() => Promise.all(reply.files.map(async (file) => {
|
|
137
|
-
if (file.link === undefined) {
|
|
138
|
-
throw new Error("A file of the reply has no link to read it by.");
|
|
139
|
-
}
|
|
140
|
-
return {
|
|
141
|
-
filename: file.filename,
|
|
142
|
-
content_type: file.contentType,
|
|
143
|
-
size: file.size,
|
|
144
|
-
url: await file.link(),
|
|
145
|
-
};
|
|
146
|
-
}))).pipe(Effect.exit);
|
|
134
|
+
const links = yield* agentFileLinks(reply.files).pipe(Effect.exit);
|
|
147
135
|
if (links._tag === "Failure") {
|
|
148
|
-
return failed(links.cause);
|
|
136
|
+
return failed(Cause.squash(links.cause));
|
|
149
137
|
}
|
|
150
138
|
const body = {
|
|
151
139
|
thread_id: reply.threadId,
|
|
@@ -155,32 +143,20 @@ function deliverThroughAgent(input, reply) {
|
|
|
155
143
|
key: reply.key,
|
|
156
144
|
...(links.value.length === 0 ? {} : { files: links.value }),
|
|
157
145
|
};
|
|
158
|
-
const answered = yield*
|
|
159
|
-
const response = await agent.fetch(new Request(new URL(emailDeliverRoute(input.destination.provider), agent.origin), {
|
|
160
|
-
method: "POST",
|
|
161
|
-
headers: {
|
|
162
|
-
authorization: `Bearer ${agent.identityToken}`,
|
|
163
|
-
"content-type": "application/json",
|
|
164
|
-
},
|
|
165
|
-
body: JSON.stringify(body),
|
|
166
|
-
signal: AbortSignal.timeout(AGENT_DELIVER_TIMEOUT_MS),
|
|
167
|
-
}));
|
|
168
|
-
return { status: response.status, text: await response.text() };
|
|
169
|
-
}).pipe(Effect.exit);
|
|
146
|
+
const answered = yield* postToAgent(input.agent, emailDeliverRoute(input.destination.provider), body, AGENT_DELIVER_TIMEOUT_MS).pipe(Effect.exit);
|
|
170
147
|
if (answered._tag === "Failure") {
|
|
171
|
-
return {
|
|
148
|
+
return {
|
|
149
|
+
payload,
|
|
150
|
+
status: "uncertain",
|
|
151
|
+
messageId: null,
|
|
152
|
+
url: null,
|
|
153
|
+
cause: Cause.squash(answered.cause),
|
|
154
|
+
};
|
|
172
155
|
}
|
|
173
|
-
const { status,
|
|
156
|
+
const { status, answer } = answered.value;
|
|
174
157
|
if (status === 404) {
|
|
175
158
|
return undefined;
|
|
176
159
|
}
|
|
177
|
-
let answer = {};
|
|
178
|
-
try {
|
|
179
|
-
answer = record(JSON.parse(text));
|
|
180
|
-
}
|
|
181
|
-
catch {
|
|
182
|
-
// The status still says what happened; the body only adds the message's id.
|
|
183
|
-
}
|
|
184
160
|
if (status >= 400 && status < 500) {
|
|
185
161
|
return failed(new Error(`The agent's email channel refused the reply (${typeof answer.error === "string" ? answer.error : `status ${status}`}).`));
|
|
186
162
|
}
|
|
@@ -10,6 +10,8 @@ export declare function refuseTelegramFiles(files: ReadonlyArray<{
|
|
|
10
10
|
* Telegram: one message when the text fits a caption and the files go as one file or one album of
|
|
11
11
|
* one type; otherwise the text, then the files. The text keeps its MarkdownV2, sent again plain when
|
|
12
12
|
* Telegram refuses its formatting, as a text reply is.
|
|
13
|
+
* @legacy ontology.telegram-own-sender The ontology's own send of a message with files, which the
|
|
14
|
+
* agent's Telegram channel's deliver route does in its place.
|
|
13
15
|
*/
|
|
14
16
|
export declare function sendTelegramWithFiles(input: ChatSend, sendText: (input: ChatSend) => Effect.Effect<ProviderOutcome>): Effect.Effect<ProviderOutcome>;
|
|
15
17
|
/** Slack: up to ten files, each one request of the connection. */
|
|
@@ -36,6 +36,8 @@ export function refuseTelegramFiles(files) {
|
|
|
36
36
|
* Telegram: one message when the text fits a caption and the files go as one file or one album of
|
|
37
37
|
* one type; otherwise the text, then the files. The text keeps its MarkdownV2, sent again plain when
|
|
38
38
|
* Telegram refuses its formatting, as a text reply is.
|
|
39
|
+
* @legacy ontology.telegram-own-sender The ontology's own send of a message with files, which the
|
|
40
|
+
* agent's Telegram channel's deliver route does in its place.
|
|
39
41
|
*/
|
|
40
42
|
export function sendTelegramWithFiles(input, sendText) {
|
|
41
43
|
return Effect.gen(function* () {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, type Exit } from "effect";
|
|
2
|
-
import type { ChannelCall, ChannelPart, ChannelRequest } from "../../../channel-files.js";
|
|
2
|
+
import type { ChannelCall, ChannelDeliverFile, ChannelPart, ChannelRequest } from "../../../channel-files.js";
|
|
3
3
|
import type { ChatButtonRows } from "../../../chat.js";
|
|
4
4
|
import type { TelegramPreviewContext } from "../../../telegram.js";
|
|
5
5
|
import { ConnectionError, type ConnectionCall, type ConnectionReply, type ConnectionTransport } from "../../../connection.js";
|
|
@@ -76,6 +76,20 @@ export interface AgentChannelTransport {
|
|
|
76
76
|
readonly identityToken: string | undefined;
|
|
77
77
|
readonly fetch: (request: Request) => Promise<Response>;
|
|
78
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* A send's files as an agent's channel reads them: each by a link to the workspace's file storage,
|
|
81
|
+
* granted now, just before the message is handed over, as the person may still read its file.
|
|
82
|
+
*/
|
|
83
|
+
export declare function agentFileLinks(files: ReadonlyArray<ChatSendFile>): Effect.Effect<ReadonlyArray<ChannelDeliverFile>, unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* One `POST` of `body` to an agent's channel route on the deployment's production origin, with
|
|
86
|
+
* the service's Vercel OIDC token, never retried: the agent's status and its JSON answer, `{}` when
|
|
87
|
+
* it answered none. It fails when no answer came, as on a timeout: the agent may have acted on it.
|
|
88
|
+
*/
|
|
89
|
+
export declare function postToAgent(agent: AgentChannelTransport, route: string, body: unknown, timeoutMs: number): Effect.Effect<{
|
|
90
|
+
readonly status: number;
|
|
91
|
+
readonly answer: Record<string, unknown>;
|
|
92
|
+
}, unknown>;
|
|
79
93
|
/** What a provider sends: the exact text its `render` gave, to one resolved destination. */
|
|
80
94
|
export interface ChatSend {
|
|
81
95
|
readonly transport: ConnectionTransport["Service"];
|
|
@@ -43,6 +43,59 @@ export function filesOutcome(payload, files, parts) {
|
|
|
43
43
|
}),
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* A send's files as an agent's channel reads them: each by a link to the workspace's file storage,
|
|
48
|
+
* granted now, just before the message is handed over, as the person may still read its file.
|
|
49
|
+
*/
|
|
50
|
+
export function agentFileLinks(files) {
|
|
51
|
+
return Effect.tryPromise({
|
|
52
|
+
try: () => Promise.all(files.map(async (file) => {
|
|
53
|
+
if (file.link === undefined) {
|
|
54
|
+
throw new Error("A file of the message has no link to read it by.");
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
filename: file.filename,
|
|
58
|
+
content_type: file.contentType,
|
|
59
|
+
size: file.size,
|
|
60
|
+
url: await file.link(),
|
|
61
|
+
};
|
|
62
|
+
})),
|
|
63
|
+
catch: (cause) => cause,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* One `POST` of `body` to an agent's channel route on the deployment's production origin, with
|
|
68
|
+
* the service's Vercel OIDC token, never retried: the agent's status and its JSON answer, `{}` when
|
|
69
|
+
* it answered none. It fails when no answer came, as on a timeout: the agent may have acted on it.
|
|
70
|
+
*/
|
|
71
|
+
export function postToAgent(agent, route, body, timeoutMs) {
|
|
72
|
+
return Effect.tryPromise({
|
|
73
|
+
try: async () => {
|
|
74
|
+
if (agent.origin === undefined || agent.identityToken === undefined) {
|
|
75
|
+
throw new Error("The agent's channels are reachable only from the production deployment.");
|
|
76
|
+
}
|
|
77
|
+
const response = await agent.fetch(new Request(new URL(route, agent.origin), {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: {
|
|
80
|
+
authorization: `Bearer ${agent.identityToken}`,
|
|
81
|
+
"content-type": "application/json",
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify(body),
|
|
84
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
85
|
+
}));
|
|
86
|
+
const text = await response.text();
|
|
87
|
+
let answer = {};
|
|
88
|
+
try {
|
|
89
|
+
answer = record(JSON.parse(text));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// The status still says what happened; the body only adds to it.
|
|
93
|
+
}
|
|
94
|
+
return { status: response.status, answer };
|
|
95
|
+
},
|
|
96
|
+
catch: (cause) => cause,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
46
99
|
/** The connection failures that prove the request never reached the provider. */
|
|
47
100
|
const DEFINITE_CONNECTION_FAILURES = new Set([
|
|
48
101
|
"authorization_required",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { type TelegramPreviewContext } from "../../../telegram.js";
|
|
1
|
+
import { TELEGRAM_MESSAGE_LIMIT, type TelegramDeliverPart, type TelegramPreviewContext } from "../../../telegram.js";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
3
|
import type { ChatButtonRows } from "../../../chat.js";
|
|
4
4
|
import type { ConnectionTransport } from "../../../connection.js";
|
|
5
5
|
import type { Destination } from "../resolve.js";
|
|
6
6
|
import { type ChatProvider, type ProviderOutcome } from "./provider.js";
|
|
7
|
-
export
|
|
7
|
+
export { TELEGRAM_MESSAGE_LIMIT };
|
|
8
8
|
/**
|
|
9
9
|
* Render source independently so user Markdown cannot open an entity around the footer. Without an
|
|
10
10
|
* attribution, as for a reply the agent sends after a member approved it, there is no footer.
|
|
@@ -12,27 +12,40 @@ export declare const TELEGRAM_MESSAGE_LIMIT = 4096;
|
|
|
12
12
|
export declare function renderTelegram(markdown: string, attribution: string, separator?: string, links?: ReadonlyMap<string, string>): string;
|
|
13
13
|
/** Buttons as Telegram shows them: an inline keyboard, a workspace button's data `ws:<token>`. */
|
|
14
14
|
export declare function telegramKeyboard(rows: ChatButtonRows, links?: ReadonlyMap<string, string>): Readonly<Record<string, unknown>>;
|
|
15
|
-
|
|
15
|
+
/** Where a text goes: the chat, its forum topic and the message it answers. */
|
|
16
|
+
interface TelegramTarget {
|
|
16
17
|
readonly transport: ConnectionTransport["Service"];
|
|
17
18
|
readonly connection: string;
|
|
18
19
|
readonly destination: Destination;
|
|
20
|
+
readonly replyTo: string | null;
|
|
21
|
+
/** The forum topic to send in, for a group with topics. */
|
|
22
|
+
readonly threadId?: string | null;
|
|
23
|
+
}
|
|
24
|
+
interface TelegramSend extends TelegramTarget {
|
|
19
25
|
/** Links parsed before MarkdownV2 escaping, with the literal attribution added separately. */
|
|
20
26
|
readonly formattedLinks?: readonly string[];
|
|
21
27
|
readonly previews?: TelegramPreviewContext;
|
|
22
28
|
readonly finalText: string;
|
|
23
29
|
readonly plainText: string;
|
|
24
|
-
readonly replyTo: string | null;
|
|
25
|
-
/** The forum topic to send in, for a group with topics. */
|
|
26
|
-
readonly threadId?: string | null;
|
|
27
30
|
readonly link_preview_options?: Readonly<Record<string, unknown>>;
|
|
28
31
|
readonly disable_web_page_preview?: boolean;
|
|
29
32
|
/** The message's buttons, kept on the plain retry. */
|
|
30
33
|
readonly replyMarkup?: Readonly<Record<string, unknown>>;
|
|
31
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* One text's `sendMessage` fields as Telegram is sent them: the MarkdownV2 text and, for the one
|
|
37
|
+
* retry a refusal of its formatting allows, the plain text, each with the link preview its own
|
|
38
|
+
* links choose.
|
|
39
|
+
*/
|
|
40
|
+
export declare function telegramPart(input: Pick<TelegramSend, "finalText" | "plainText" | "formattedLinks" | "previews" | "link_preview_options" | "disable_web_page_preview">): TelegramDeliverPart;
|
|
32
41
|
/** One proxy POST, except one plain retry after a definite 400 entity parse refusal.
|
|
33
42
|
* Never retry an unknown outcome; never chunk, truncate or invoke an agent.
|
|
43
|
+
* @legacy ontology.telegram-own-sender The ontology's own send of a text, which the agent's
|
|
44
|
+
* Telegram channel's deliver route does in its place.
|
|
34
45
|
*/
|
|
35
46
|
export declare function sendTelegram(input: TelegramSend): Effect.Effect<ProviderOutcome>;
|
|
36
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* Telegram: the ontology renders the message, its MarkdownV2 with one plain retry after a definite
|
|
49
|
+
* entity parse refusal, and the agent's Telegram channel of the chat's name sends it.
|
|
50
|
+
*/
|
|
37
51
|
export declare const telegramProvider: ChatProvider;
|
|
38
|
-
export {};
|