@segmentstream/ontology 0.183.0 → 0.185.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 +5 -2
- package/dist/channel-files.d.ts +17 -0
- package/dist/email.d.ts +3 -7
- package/dist/service/chat/providers/email.js +13 -37
- package/dist/service/chat/providers/files-send.d.ts +4 -0
- package/dist/service/chat/providers/files-send.js +7 -3
- package/dist/service/chat/providers/provider.d.ts +21 -1
- package/dist/service/chat/providers/provider.js +82 -0
- package/dist/service/chat/providers/telegram.d.ts +21 -8
- package/dist/service/chat/providers/telegram.js +195 -48
- package/dist/service/chat/providers/whatsapp.d.ts +5 -1
- package/dist/service/chat/providers/whatsapp.js +152 -9
- package/dist/telegram.d.ts +84 -1
- package/dist/telegram.js +14 -0
- package/dist/whatsapp-files.d.ts +25 -1
- package/dist/whatsapp-files.js +68 -41
- package/dist/whatsapp.d.ts +83 -3
- package/dist/whatsapp.js +14 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,67 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.185.0
|
|
4
|
+
|
|
5
|
+
The ontology's WhatsApp messages, a post in either voice (`agent_chat.send`), a draft a member
|
|
6
|
+
approves and a reply its review lets go, are rendered and checked as before and handed to the
|
|
7
|
+
agent's WhatsApp channel of the chat's name at `POST /eve/segmentstream/v1/whatsapp/<name>/deliver`,
|
|
8
|
+
which sends them from its number: the text with its attribution, the text with its buttons, or an
|
|
9
|
+
approved template with its header's, body's and buttons' values, and the files after the text, each
|
|
10
|
+
by a short-lived link granted just before. What the channel answers is recorded as the ontology's
|
|
11
|
+
own send was: `sent`, `failed` (with `outside_reply_window`, `recipient_unreachable` or
|
|
12
|
+
`recipient_opted_out` where Meta's code names one) or `uncertain`, with each file's result and the
|
|
13
|
+
same payload in the delivery's history.
|
|
14
|
+
|
|
15
|
+
`@segmentstream/ontology/whatsapp` exports the route and its wire types: `whatsappDeliverRoute`,
|
|
16
|
+
`WhatsAppDeliver`, `WhatsAppDeliverMessage`, `WhatsAppDelivered`, `WhatsAppDeliverRefused` and
|
|
17
|
+
`WHATSAPP_DELIVER_FILES`, with `ChannelDeliverFile` and `ChannelDeliveredFile`. It also exports
|
|
18
|
+
`sendWhatsAppMessage`, the one-message sender `sendWhatsAppFiles` sends each of its messages with,
|
|
19
|
+
and `WhatsAppMessageType` and `WhatsAppRefusal`; a part `sendWhatsAppFiles` answers as `failed`
|
|
20
|
+
now names Meta's `refusal` where its code names one.
|
|
21
|
+
|
|
22
|
+
An agent from before the route answers 404, and one without a WhatsApp channel of the chat's name
|
|
23
|
+
on the connection `chat` names answers `channel_unknown`: the ontology then sends the message
|
|
24
|
+
through that connection itself, as before. A workspace channel that reaches WhatsApp through
|
|
25
|
+
another provider is unaffected. This release works with the agent SDK's previous release, and the
|
|
26
|
+
agent SDK's next release works with this ontology SDK's previous one, which never calls the route.
|
|
27
|
+
|
|
28
|
+
Action required: upgrade `@segmentstream/agent` together with `@segmentstream/ontology`, and give
|
|
29
|
+
the agent a WhatsApp channel of the name of each WhatsApp channel in the ontology's `chat`, on the
|
|
30
|
+
same connection (`channels.whatsapp.connection` in `agent/segmentstream.json` naming the connection
|
|
31
|
+
of `chat.whatsapp`, and so for a channel of another name). The ontology's own WhatsApp sender, which
|
|
32
|
+
sends for an agent without such a channel, is deprecated and stops on 2027-05-28
|
|
33
|
+
(`ontology.whatsapp-own-sender`).
|
|
34
|
+
|
|
35
|
+
## 0.184.0
|
|
36
|
+
|
|
37
|
+
The ontology's Telegram messages, a post in either voice (`agent_chat.send`), a draft a member
|
|
38
|
+
approves and a reply its review lets go, are rendered as before and handed to the agent's Telegram
|
|
39
|
+
channel of the chat's name at `POST /eve/segmentstream/v1/telegram/<name>/deliver`, which sends them
|
|
40
|
+
through its bot: the exact text in MarkdownV2 with its attribution, links to the app and link
|
|
41
|
+
preview, its text messages in order, the buttons under the last, and the files after the text or
|
|
42
|
+
under it as their caption, each by a short-lived link granted just before. What the channel answers
|
|
43
|
+
is recorded as the ontology's own send was: `sent`, `failed` (with `recipient_unreachable` for a
|
|
44
|
+
person who blocked the bot) or `uncertain`, with each file's result.
|
|
45
|
+
|
|
46
|
+
`@segmentstream/ontology/telegram` exports the route and its wire types: `telegramDeliverRoute`,
|
|
47
|
+
`TelegramDeliver`, `TelegramDeliverPart`, `TelegramDeliverText`, `TelegramDelivered`,
|
|
48
|
+
`TelegramDeliverRefused`, `TELEGRAM_DELIVER_PARTS` and `TELEGRAM_MESSAGE_LIMIT`, and
|
|
49
|
+
`ChannelDeliverFile` and `ChannelDeliveredFile` for a file handed over by its link and what became
|
|
50
|
+
of it.
|
|
51
|
+
|
|
52
|
+
An agent from before the route answers 404, and one without a Telegram channel of the chat's name
|
|
53
|
+
on the connection `chat` names answers `channel_unknown`: the ontology then sends the message
|
|
54
|
+
through that connection itself, as before. This release works with the agent SDK's previous
|
|
55
|
+
release, and the agent SDK's next release works with this ontology SDK's previous one, which never
|
|
56
|
+
calls the route.
|
|
57
|
+
|
|
58
|
+
Action required: upgrade `@segmentstream/agent` together with `@segmentstream/ontology`, and give
|
|
59
|
+
the agent a Telegram channel of the name of each Telegram channel in the ontology's `chat`, on the
|
|
60
|
+
same connection (`channels.telegram.connection` in `agent/segmentstream.json` naming the connection
|
|
61
|
+
of `chat.telegram`, and so for a channel of another name). The ontology's own Telegram sender, which
|
|
62
|
+
sends for an agent without such a channel, is deprecated and stops on 2027-05-28
|
|
63
|
+
(`ontology.telegram-own-sender`).
|
|
64
|
+
|
|
3
65
|
## 0.183.0
|
|
4
66
|
|
|
5
67
|
`agent_chat.send` is the one action that sends a message to a chat, in the voice its new `as`
|
package/README.md
CHANGED
|
@@ -1591,8 +1591,11 @@ the separator and attribution with Markdown escaped and the attribution in itali
|
|
|
1591
1591
|
Members' WhatsApp chats with the workspace's number are destinations only when `chat.whatsapp`
|
|
1592
1592
|
declares the `WhatsAppConnection`, under the same rules. Each member with a live WhatsApp pairing
|
|
1593
1593
|
is `whatsapp:private:<member UUID>`, posted to their WhatsApp id; WhatsApp has no group
|
|
1594
|
-
destinations. A WhatsApp post is one text message,
|
|
1595
|
-
|
|
1594
|
+
destinations. A WhatsApp post is one text message, which the agent's WhatsApp channel of the
|
|
1595
|
+
chat's name sends from its number (`POST /eve/segmentstream/v1/whatsapp/<name>/deliver`,
|
|
1596
|
+
`whatsappDeliverRoute`), or the ontology itself for an agent without one, `POST /phone/messages` on
|
|
1597
|
+
the connection's `graph` API with link previews off: the member's text as written, then the
|
|
1598
|
+
separator and the
|
|
1596
1599
|
attribution in italics, whose WhatsApp formatting markers (`*`, `_`, `~`, `` ` ``) become
|
|
1597
1600
|
look-alikes WhatsApp never formats, as `renderWhatsApp` from `@segmentstream/ontology/whatsapp`
|
|
1598
1601
|
renders it. The body is at most 4096 characters, and `reply_to` is refused. Meta delivers
|
package/dist/channel-files.d.ts
CHANGED
|
@@ -88,6 +88,23 @@ export interface ChannelPart {
|
|
|
88
88
|
readonly code?: string;
|
|
89
89
|
readonly cause?: unknown;
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* A file the ontology hands an agent's channel to deliver with a message: by a short-lived link to
|
|
93
|
+
* the workspace's file storage, granted just before the message is handed over, which the channel
|
|
94
|
+
* reads it by. `size` is the revision's the ontology checked: bytes of another size are not it.
|
|
95
|
+
*/
|
|
96
|
+
export interface ChannelDeliverFile {
|
|
97
|
+
readonly filename: string;
|
|
98
|
+
readonly content_type: string;
|
|
99
|
+
readonly size: number;
|
|
100
|
+
readonly url: string;
|
|
101
|
+
}
|
|
102
|
+
/** What became of one file of a delivery, by its index among the files the request named. */
|
|
103
|
+
export interface ChannelDeliveredFile {
|
|
104
|
+
readonly index: number;
|
|
105
|
+
readonly status: "sent" | "uncertain" | "failed";
|
|
106
|
+
readonly message_id: string | null;
|
|
107
|
+
}
|
|
91
108
|
/**
|
|
92
109
|
* The provider's answer to a call that did not prove its message sent, kept whole for operators:
|
|
93
110
|
* its status and its body, such as Telegram's `description` or Meta's error. Whoever logs it
|
package/dist/email.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ChannelDeliverFile } from "./channel-files.js";
|
|
1
2
|
/** The most characters of a reply the mail channel sends in one message. */
|
|
2
3
|
export declare const EMAIL_MESSAGE_LIMIT = 100000;
|
|
3
4
|
/** The most addresses one reply goes to, in `To` and `Cc` together. */
|
|
@@ -115,12 +116,7 @@ export interface EmailDeliver {
|
|
|
115
116
|
/** The reply's key (`X-SegmentStream-Reply`), so a send after a lost answer is found, not repeated. */
|
|
116
117
|
readonly key: string;
|
|
117
118
|
/** The reply's files, each by a short-lived link to the workspace's file storage. */
|
|
118
|
-
readonly files?: ReadonlyArray<
|
|
119
|
-
readonly filename: string;
|
|
120
|
-
readonly content_type: string;
|
|
121
|
-
readonly size: number;
|
|
122
|
-
readonly url: string;
|
|
123
|
-
}>;
|
|
119
|
+
readonly files?: ReadonlyArray<ChannelDeliverFile>;
|
|
124
120
|
}
|
|
125
121
|
/**
|
|
126
122
|
* The agent's answer: `200 { message_id }` once Gmail took the reply. A `4xx` with `{ error }` sent
|
|
@@ -136,5 +132,5 @@ export interface EmailDelivered {
|
|
|
136
132
|
export declare function emailReplyMessage(reply: EmailReply): string;
|
|
137
133
|
/** Bytes as URL-safe base64 without padding, as the Gmail API takes a raw message. */
|
|
138
134
|
export declare function base64Url(text: string): string;
|
|
139
|
-
export type { ChannelCall, ChannelReply, ChannelRequest } from "./channel-files.js";
|
|
135
|
+
export type { ChannelCall, ChannelDeliverFile, ChannelReply, ChannelRequest, } from "./channel-files.js";
|
|
140
136
|
export { EMAIL_FILES_BYTES, GmailError, gmailExplanation, GmailMaybeSent, gmailJson, mailboxSignature, mailboxSignatures, MAILBOX_SIGNATURE_LIFETIME_MS, sendGmailMessage, } from "./gmail-send.js";
|
|
@@ -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. */
|
|
@@ -32,5 +34,7 @@ export declare function refuseWhatsAppFiles(files: ReadonlyArray<{
|
|
|
32
34
|
/**
|
|
33
35
|
* WhatsApp: one message per file, which WhatsApp fetches by a link granted just before it, the text
|
|
34
36
|
* as the first's caption, or first on its own when no caption carries it.
|
|
37
|
+
* @legacy ontology.whatsapp-own-sender The ontology's own send of a message with files, which the
|
|
38
|
+
* agent's WhatsApp channel's deliver route does in its place.
|
|
35
39
|
*/
|
|
36
40
|
export declare function sendWhatsAppWithFiles(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
@@ -2,7 +2,7 @@ import { Effect } from "effect";
|
|
|
2
2
|
import { MAXIMUM_CONNECTION_BODY_BYTES } from "../../../connection.js";
|
|
3
3
|
import { sendSlackFiles, SLACK_FILES_PER_MESSAGE, slackFileRefusal } from "../../../slack.js";
|
|
4
4
|
import { sendTelegramFiles, TELEGRAM_CAPTION_LIMIT, TELEGRAM_FILES_PER_MESSAGE, telegramFileRefusal, telegramFileType, } from "../../../telegram.js";
|
|
5
|
-
import { sendWhatsAppFiles, WHATSAPP_FILE_BYTES, whatsappFileRefusal } from "../../../whatsapp.js";
|
|
5
|
+
import { sendWhatsAppFiles, WHATSAPP_DELIVER_FILES, WHATSAPP_FILE_BYTES, whatsappFileRefusal, } from "../../../whatsapp.js";
|
|
6
6
|
import { channelRequest, definiteTransportFailure, filesOutcome, filesWithBytes, } from "./provider.js";
|
|
7
7
|
/**
|
|
8
8
|
* An approved reply with files on Telegram, Slack and WhatsApp, by each channel's sender of the
|
|
@@ -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* () {
|
|
@@ -144,8 +146,8 @@ export function sendSlackWithFiles(input) {
|
|
|
144
146
|
}
|
|
145
147
|
/** WhatsApp: up to ten files, one message each, by WhatsApp's bound of each type. */
|
|
146
148
|
export const WHATSAPP_FILE_LIMITS = {
|
|
147
|
-
count:
|
|
148
|
-
bytes:
|
|
149
|
+
count: WHATSAPP_DELIVER_FILES,
|
|
150
|
+
bytes: WHATSAPP_DELIVER_FILES * WHATSAPP_FILE_BYTES.document,
|
|
149
151
|
transfer: "link",
|
|
150
152
|
};
|
|
151
153
|
export function refuseWhatsAppFiles(files) {
|
|
@@ -160,6 +162,8 @@ export function refuseWhatsAppFiles(files) {
|
|
|
160
162
|
/**
|
|
161
163
|
* WhatsApp: one message per file, which WhatsApp fetches by a link granted just before it, the text
|
|
162
164
|
* as the first's caption, or first on its own when no caption carries it.
|
|
165
|
+
* @legacy ontology.whatsapp-own-sender The ontology's own send of a message with files, which the
|
|
166
|
+
* agent's WhatsApp channel's deliver route does in its place.
|
|
163
167
|
*/
|
|
164
168
|
export function sendWhatsAppWithFiles(input) {
|
|
165
169
|
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,26 @@ 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>;
|
|
93
|
+
/**
|
|
94
|
+
* What became of each file of a delivery, as an agent's channel answers it (`ChannelDeliveredFile`):
|
|
95
|
+
* one result per file in order, each message named by an id `messageId` matches, or undefined for
|
|
96
|
+
* an answer without that shape.
|
|
97
|
+
*/
|
|
98
|
+
export declare function deliveredFiles(value: unknown, count: number, messageId: RegExp): ReadonlyArray<NonNullable<ProviderOutcome["files"]>[number]> | undefined;
|
|
79
99
|
/** What a provider sends: the exact text its `render` gave, to one resolved destination. */
|
|
80
100
|
export interface ChatSend {
|
|
81
101
|
readonly transport: ConnectionTransport["Service"];
|
|
@@ -43,6 +43,88 @@ 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
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* What became of each file of a delivery, as an agent's channel answers it (`ChannelDeliveredFile`):
|
|
101
|
+
* one result per file in order, each message named by an id `messageId` matches, or undefined for
|
|
102
|
+
* an answer without that shape.
|
|
103
|
+
*/
|
|
104
|
+
export function deliveredFiles(value, count, messageId) {
|
|
105
|
+
if (count === 0) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
if (!Array.isArray(value) || value.length !== count) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
const results = value.map((item, index) => {
|
|
112
|
+
const result = record(item);
|
|
113
|
+
const id = result.message_id;
|
|
114
|
+
return result.index === index &&
|
|
115
|
+
(result.status === "sent" || result.status === "uncertain" || result.status === "failed") &&
|
|
116
|
+
(id === null || (typeof id === "string" && messageId.test(id)))
|
|
117
|
+
? {
|
|
118
|
+
index,
|
|
119
|
+
status: result.status,
|
|
120
|
+
messageId: result.status === "sent" ? id : null,
|
|
121
|
+
}
|
|
122
|
+
: undefined;
|
|
123
|
+
});
|
|
124
|
+
return results.every((result) => result !== undefined)
|
|
125
|
+
? results
|
|
126
|
+
: undefined;
|
|
127
|
+
}
|
|
46
128
|
/** The connection failures that prove the request never reached the provider. */
|
|
47
129
|
const DEFINITE_CONNECTION_FAILURES = new Set([
|
|
48
130
|
"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 {};
|