@segmentstream/ontology 0.163.0 → 0.165.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 +28 -0
- package/dist/agent-chat.d.ts +10 -0
- package/dist/agent-draft.d.ts +18 -6
- package/dist/agent-draft.js +29 -0
- package/dist/chat.d.ts +18 -0
- package/dist/chat.js +10 -0
- package/dist/email.d.ts +1 -1
- package/dist/email.js +1 -1
- package/dist/gmail-send.d.ts +10 -0
- package/dist/gmail-send.js +31 -9
- package/dist/manifest.d.ts +469 -469
- package/dist/migrate/model.d.ts +351 -351
- package/dist/service/chat/delivery.js +79 -3
- package/dist/service/chat/draft-files.d.ts +7 -0
- package/dist/service/chat/draft-files.js +33 -4
- package/dist/service/chat/envelope.d.ts +6 -6
- package/dist/service/chat/local-reads.d.ts +1 -1
- package/dist/service/chat/providers/custom.js +2 -2
- package/dist/service/chat/providers/email.js +13 -11
- package/dist/service/chat/providers/files-send.d.ts +36 -0
- package/dist/service/chat/providers/files-send.js +182 -0
- package/dist/service/chat/providers/provider.d.ts +22 -3
- package/dist/service/chat/providers/provider.js +43 -0
- package/dist/service/chat/providers/slack.js +4 -1
- package/dist/service/chat/providers/telegram.js +23 -3
- package/dist/service/chat/providers/whatsapp.js +4 -1
- package/dist/service/chat/review-delivery.d.ts +4 -2
- package/dist/service/chat/review-delivery.js +22 -2
- package/dist/service/chat/schemas.d.ts +5 -0
- package/dist/service/discovery-search.d.ts +1 -1
- package/dist/slack-files.d.ts +2 -0
- package/dist/slack-files.js +1 -1
- package/dist/telegram-files.d.ts +4 -0
- package/dist/telegram-files.js +6 -2
- package/dist/user-identity.d.ts +1 -1
- package/package.json +1 -1
|
@@ -9,6 +9,7 @@ import { describeChatError } from "./diagnostics.js";
|
|
|
9
9
|
import { deliveryRequester } from "./attribution.js";
|
|
10
10
|
import { deliveryFingerprint, deliveryKeyDigest, deliveryScopeDigest } from "./idempotency.js";
|
|
11
11
|
import { deliveryArchiveOf } from "./store-delivery.js";
|
|
12
|
+
import { readDraftFiles, snapshotDraftFiles } from "./draft-files.js";
|
|
12
13
|
import { channelProvider, chatConnection, chatKind, listedChatProviders, } from "./providers/index.js";
|
|
13
14
|
import { chatResolver } from "./resolve.js";
|
|
14
15
|
import { buttonLines } from "./providers/provider.js";
|
|
@@ -509,7 +510,7 @@ chats) {
|
|
|
509
510
|
function deliver(input, asAgent) {
|
|
510
511
|
return Effect.gen(function* () {
|
|
511
512
|
const checked = yield* check(input, true, asAgent);
|
|
512
|
-
const { target, requester, connection, provider, inbox,
|
|
513
|
+
const { target, requester, connection, provider, inbox, compose } = checked;
|
|
513
514
|
const buttons = "buttons" in checked ? checked.buttons : undefined;
|
|
514
515
|
const scope = deliveryScopeDigest({
|
|
515
516
|
via: requester.via,
|
|
@@ -548,9 +549,25 @@ chats) {
|
|
|
548
549
|
history_recorded: receipt.historyRecorded,
|
|
549
550
|
};
|
|
550
551
|
});
|
|
552
|
+
// A post's stored files, as the caller names them. The key binds those names: a retry after
|
|
553
|
+
// a file changed or went recovers the first attempt's receipt, read before the files are.
|
|
554
|
+
const named = "files" in input && input.files !== undefined ? input.files : [];
|
|
555
|
+
const references = named.map((file) => [file.entity, file.id.toLowerCase(), file.property ?? "content", ""]);
|
|
556
|
+
const fingerprint = references.length === 0 || !("preview" in checked) || checked.preview === undefined
|
|
557
|
+
? checked.fingerprint
|
|
558
|
+
: deliveryFingerprint(checked.preview, requester, undefined, buttons, undefined, references);
|
|
559
|
+
if (named.length > 0 &&
|
|
560
|
+
(("template" in input && input.template !== undefined) || archive === cloud)) {
|
|
561
|
+
// @legacy agent.cloud-chat-archive Files go only through the workspace chat store.
|
|
562
|
+
return yield* new ActionRejected({
|
|
563
|
+
message: "The post's files cannot be sent (files_unsupported); nothing was sent.",
|
|
564
|
+
refusal: "files_unsupported",
|
|
565
|
+
});
|
|
566
|
+
}
|
|
551
567
|
// A message read when it is sent, a template, may read differently by the time its key
|
|
552
568
|
// is replayed, or not at all: a replay recovers the first attempt's receipt before it.
|
|
553
|
-
if ("template" in input && input.template !== undefined
|
|
569
|
+
if ((("template" in input && input.template !== undefined) || named.length > 0) &&
|
|
570
|
+
archive.find !== undefined) {
|
|
554
571
|
const found = yield* archive
|
|
555
572
|
.find({ delivery: { scope, key, fingerprint }, identityToken: context.identityToken })
|
|
556
573
|
.pipe(Effect.tapError((error) => report("find", error)), Effect.mapError(unreserved));
|
|
@@ -558,6 +575,41 @@ chats) {
|
|
|
558
575
|
return yield* replayed(found);
|
|
559
576
|
}
|
|
560
577
|
}
|
|
578
|
+
// Each file read as the caller now, at the revision it holds, which the delivery records:
|
|
579
|
+
// every refusal comes before the key is reserved.
|
|
580
|
+
let heldFiles = [];
|
|
581
|
+
if (named.length > 0) {
|
|
582
|
+
const snapshot = yield* snapshotDraftFiles({
|
|
583
|
+
manifest: ontology.manifest,
|
|
584
|
+
backend: context.backend,
|
|
585
|
+
principal: context.principal,
|
|
586
|
+
channel: target.provider,
|
|
587
|
+
files: named,
|
|
588
|
+
});
|
|
589
|
+
if ("refusal" in snapshot) {
|
|
590
|
+
return yield* new ActionRejected({
|
|
591
|
+
message: `The post's files cannot be sent (${snapshot.refusal}); nothing was sent.`,
|
|
592
|
+
refusal: snapshot.refusal,
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
heldFiles = snapshot.files;
|
|
596
|
+
}
|
|
597
|
+
const bindings = heldFiles.map((file) => [file.entity, file.id, file.property, file.revisionId]);
|
|
598
|
+
// The files' bytes, or links to them, read as the caller once more before anything is sent.
|
|
599
|
+
const sendFiles = yield* readDraftFiles({
|
|
600
|
+
backend: context.backend,
|
|
601
|
+
person: null,
|
|
602
|
+
principal: context.principal,
|
|
603
|
+
files: heldFiles,
|
|
604
|
+
fetch,
|
|
605
|
+
...(provider.files?.transfer === undefined ? {} : { transfer: provider.files.transfer }),
|
|
606
|
+
});
|
|
607
|
+
if (sendFiles === undefined) {
|
|
608
|
+
return yield* new ActionRejected({
|
|
609
|
+
message: "A file of the post is gone, or you may no longer read it; nothing was sent.",
|
|
610
|
+
refusal: "file_unavailable",
|
|
611
|
+
});
|
|
612
|
+
}
|
|
561
613
|
const { preview, payload, summary } = yield* compose;
|
|
562
614
|
// The message as sent without formatting: the text, then any attribution.
|
|
563
615
|
const plainText = preview.attribution === ""
|
|
@@ -566,6 +618,17 @@ chats) {
|
|
|
566
618
|
// A member who answers in the inbox writes as themselves, not as the agent.
|
|
567
619
|
const staff = inbox !== undefined && context.caller.kind === "member" && !asAgent;
|
|
568
620
|
const place = inbox ?? (yield* personalChat(target));
|
|
621
|
+
// Slack shares files only in a chat, never to a member's account: a post with files to a
|
|
622
|
+
// person needs the direct message they wrote in, refused before the key is reserved.
|
|
623
|
+
if (sendFiles.length > 0 &&
|
|
624
|
+
provider.provider === "slack" &&
|
|
625
|
+
target.kind === "private" &&
|
|
626
|
+
place === undefined) {
|
|
627
|
+
return yield* new ActionRejected({
|
|
628
|
+
message: "No direct message with this person is known to share files in; nothing was sent.",
|
|
629
|
+
refusal: "files_unsupported",
|
|
630
|
+
});
|
|
631
|
+
}
|
|
569
632
|
const operationId = context.envelope?.operation_id ?? randomUUID();
|
|
570
633
|
const event = {
|
|
571
634
|
eventId: `chat-delivery:${operationId}:reserve`,
|
|
@@ -593,7 +656,15 @@ chats) {
|
|
|
593
656
|
toolName: null,
|
|
594
657
|
argsSummary: JSON.stringify(summary ?? payload ?? provider.payload(preview.finalText, buttons)),
|
|
595
658
|
status: "requested",
|
|
596
|
-
|
|
659
|
+
// The files as the chat's history shows them, as the agent's own sends record them.
|
|
660
|
+
attachments: heldFiles.map((file, index) => ({
|
|
661
|
+
id: `chat-delivery:${operationId}:file:${index}`,
|
|
662
|
+
kind: "file",
|
|
663
|
+
filename: file.filename,
|
|
664
|
+
contentType: file.contentType,
|
|
665
|
+
size: file.size,
|
|
666
|
+
})),
|
|
667
|
+
...(bindings.length === 0 ? {} : { files: bindings }),
|
|
597
668
|
requestedBy: requester,
|
|
598
669
|
url: null,
|
|
599
670
|
at: new Date().toISOString(),
|
|
@@ -632,7 +703,12 @@ chats) {
|
|
|
632
703
|
operationId,
|
|
633
704
|
...(payload === undefined ? {} : { payload }),
|
|
634
705
|
...(buttons === undefined ? {} : { buttons }),
|
|
706
|
+
...(sendFiles.length === 0 ? {} : { files: sendFiles }),
|
|
635
707
|
...(inbox === undefined ? {} : { threadId: inbox.threadId, chatId: inbox.chatId }),
|
|
708
|
+
// Files go in the person's chat, which their account is not on Slack.
|
|
709
|
+
...(inbox === undefined && sendFiles.length > 0 && place !== undefined
|
|
710
|
+
? { chatId: place.chatId }
|
|
711
|
+
: {}),
|
|
636
712
|
transport: connections({
|
|
637
713
|
identityToken: context.identityToken,
|
|
638
714
|
audit: {
|
|
@@ -51,6 +51,13 @@ export declare function snapshotDraftFiles(input: {
|
|
|
51
51
|
export declare function readDraftFiles(input: {
|
|
52
52
|
readonly backend: OntologyBackend["Service"];
|
|
53
53
|
readonly person: string | null;
|
|
54
|
+
/** Reads the files as this principal in place of `person`, as a member's own post does. */
|
|
55
|
+
readonly principal?: Principal;
|
|
54
56
|
readonly files: ReadonlyArray<AgentDraftFile>;
|
|
55
57
|
readonly fetch: typeof fetch;
|
|
58
|
+
/**
|
|
59
|
+
* `link` for a provider that fetches the files itself: each is granted to the person now, which
|
|
60
|
+
* checks they may still read it, and its link is granted again just before its message.
|
|
61
|
+
*/
|
|
62
|
+
readonly transfer?: "bytes" | "link";
|
|
56
63
|
}): Effect.Effect<ReadonlyArray<ChatSendFile> | undefined>;
|
|
@@ -87,13 +87,37 @@ export function readDraftFiles(input) {
|
|
|
87
87
|
return [];
|
|
88
88
|
}
|
|
89
89
|
const { blobs, agents } = input.backend;
|
|
90
|
-
const member = input.
|
|
91
|
-
?
|
|
92
|
-
:
|
|
90
|
+
const member = input.principal !== undefined
|
|
91
|
+
? { principal: input.principal }
|
|
92
|
+
: input.person === null || agents === undefined
|
|
93
|
+
? undefined
|
|
94
|
+
: yield* agents.member(input.person);
|
|
93
95
|
if (blobs === undefined || member === undefined) {
|
|
94
96
|
return undefined;
|
|
95
97
|
}
|
|
96
98
|
const read = [];
|
|
99
|
+
if (input.transfer === "link") {
|
|
100
|
+
for (const file of input.files) {
|
|
101
|
+
const grant = (principal) => blobs.download(file.entity, file.id, file.property, { revisionId: file.revisionId }, principal);
|
|
102
|
+
const granted = yield* grant(member.principal).pipe(Effect.map(() => true), Effect.catch((cause) => Effect.logWarning("ontology.agent_draft.file_unavailable").pipe(Effect.annotateLogs({
|
|
103
|
+
phase: "approve",
|
|
104
|
+
entity: file.entity,
|
|
105
|
+
id: file.id,
|
|
106
|
+
revisionId: file.revisionId,
|
|
107
|
+
error: describeChatError(cause, (text) => text),
|
|
108
|
+
}), Effect.as(false))));
|
|
109
|
+
if (!granted) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
read.push({
|
|
113
|
+
filename: file.filename,
|
|
114
|
+
contentType: file.contentType,
|
|
115
|
+
size: file.size,
|
|
116
|
+
link: () => Effect.runPromise(grant(member.principal)).then(({ url }) => url),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return read;
|
|
120
|
+
}
|
|
97
121
|
for (const file of input.files) {
|
|
98
122
|
const bytes = yield* blobs
|
|
99
123
|
.download(file.entity, file.id, file.property, { revisionId: file.revisionId }, member.principal)
|
|
@@ -130,7 +154,12 @@ export function readDraftFiles(input) {
|
|
|
130
154
|
}
|
|
131
155
|
return undefined;
|
|
132
156
|
}
|
|
133
|
-
read.push({
|
|
157
|
+
read.push({
|
|
158
|
+
filename: file.filename,
|
|
159
|
+
contentType: file.contentType,
|
|
160
|
+
size: file.size,
|
|
161
|
+
bytes,
|
|
162
|
+
});
|
|
134
163
|
}
|
|
135
164
|
return read;
|
|
136
165
|
});
|
|
@@ -52,14 +52,14 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
|
|
|
52
52
|
readonly provider: "telegram";
|
|
53
53
|
readonly chat_id: string;
|
|
54
54
|
readonly thread_id: string;
|
|
55
|
-
readonly message_id?: string | undefined;
|
|
56
|
-
readonly channel?: string | undefined;
|
|
57
55
|
readonly location?: {
|
|
58
56
|
readonly latitude: number;
|
|
59
57
|
readonly longitude: number;
|
|
60
58
|
readonly horizontal_accuracy?: number | undefined;
|
|
61
59
|
readonly live_period?: number | undefined;
|
|
62
60
|
} | undefined;
|
|
61
|
+
readonly channel?: string | undefined;
|
|
62
|
+
readonly message_id?: string | undefined;
|
|
63
63
|
readonly attachments?: readonly {
|
|
64
64
|
readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
|
|
65
65
|
readonly file_id: string;
|
|
@@ -75,8 +75,8 @@ declare const CurrentChatCall_base: Context.ServiceClass<CurrentChatCall, "@segm
|
|
|
75
75
|
readonly provider: "slack" | "whatsapp";
|
|
76
76
|
readonly chat_id: string;
|
|
77
77
|
readonly thread_id: string;
|
|
78
|
-
readonly message_id?: string | undefined;
|
|
79
78
|
readonly channel?: string | undefined;
|
|
79
|
+
readonly message_id?: string | undefined;
|
|
80
80
|
readonly sender?: string | undefined;
|
|
81
81
|
readonly peer?: string | undefined;
|
|
82
82
|
} | undefined;
|
|
@@ -106,14 +106,14 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
|
|
|
106
106
|
readonly provider: "telegram";
|
|
107
107
|
readonly chat_id: string;
|
|
108
108
|
readonly thread_id: string;
|
|
109
|
-
readonly message_id?: string | undefined;
|
|
110
|
-
readonly channel?: string | undefined;
|
|
111
109
|
readonly location?: {
|
|
112
110
|
readonly latitude: number;
|
|
113
111
|
readonly longitude: number;
|
|
114
112
|
readonly horizontal_accuracy?: number | undefined;
|
|
115
113
|
readonly live_period?: number | undefined;
|
|
116
114
|
} | undefined;
|
|
115
|
+
readonly channel?: string | undefined;
|
|
116
|
+
readonly message_id?: string | undefined;
|
|
117
117
|
readonly attachments?: readonly {
|
|
118
118
|
readonly kind: "photo" | "document" | "voice" | "audio" | "video" | "video_note" | "sticker" | "animation";
|
|
119
119
|
readonly file_id: string;
|
|
@@ -129,8 +129,8 @@ export declare function consumeChatEnvelope(args: Readonly<Record<string, unknow
|
|
|
129
129
|
readonly provider: "slack" | "whatsapp";
|
|
130
130
|
readonly chat_id: string;
|
|
131
131
|
readonly thread_id: string;
|
|
132
|
-
readonly message_id?: string | undefined;
|
|
133
132
|
readonly channel?: string | undefined;
|
|
133
|
+
readonly message_id?: string | undefined;
|
|
134
134
|
readonly sender?: string | undefined;
|
|
135
135
|
readonly peer?: string | undefined;
|
|
136
136
|
} | undefined;
|
|
@@ -58,7 +58,7 @@ export declare function chatMessageEvent(message: ChatMessage): {
|
|
|
58
58
|
}[];
|
|
59
59
|
edited: boolean;
|
|
60
60
|
deleted: boolean;
|
|
61
|
-
delivery: "pending" | "read" | "sent" | "
|
|
61
|
+
delivery: "pending" | "read" | "sent" | "failed" | "uncertain" | "delivered" | null;
|
|
62
62
|
disposition: "skipped" | "claimed" | "turn" | null;
|
|
63
63
|
replaced_by: string | null;
|
|
64
64
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
|
|
3
3
|
import { renderWorkspaceChannelText, WORKSPACE_CHANNEL_ID, WORKSPACE_CHANNEL_FILES_BYTES, WORKSPACE_CHANNEL_TEXT_LIMIT, workspaceChannelFiles, workspaceChannelSendRoute, } from "../../../workspace-channel.js";
|
|
4
|
-
import { record } from "./provider.js";
|
|
4
|
+
import { filesWithBytes, record, } from "./provider.js";
|
|
5
5
|
/** How long the agent has to send one message through a workspace channel. */
|
|
6
6
|
const AGENT_SEND_TIMEOUT_MS = 30_000;
|
|
7
7
|
/**
|
|
@@ -42,7 +42,7 @@ export function sendThroughAgent(input) {
|
|
|
42
42
|
text: input.finalText,
|
|
43
43
|
// Every agent with this route reads `files`, and refuses them with `files_unsupported`
|
|
44
44
|
// when the channel's code takes none: nothing is sent then.
|
|
45
|
-
...(files.length === 0 ? {} : { files: workspaceChannelFiles(files) }),
|
|
45
|
+
...(files.length === 0 ? {} : { files: workspaceChannelFiles(filesWithBytes(files)) }),
|
|
46
46
|
};
|
|
47
47
|
const answered = yield* Effect.tryPromise(async () => {
|
|
48
48
|
const response = await agent.fetch(new Request(new URL(workspaceChannelSendRoute(input.destination.provider), agent.origin), {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
|
|
3
|
-
import { base64Url, EMAIL_FILES_BYTES, EMAIL_MESSAGE_LIMIT, emailReplyMessage, GmailError, renderEmailHtml, sendGmailMessage, } from "../../../email.js";
|
|
3
|
+
import { base64Url, EMAIL_FILES_BYTES, EMAIL_MESSAGE_LIMIT, emailReplyMessage, GmailError, GmailMaybeSent, renderEmailHtml, sendGmailMessage, } from "../../../email.js";
|
|
4
4
|
import { EMAIL_PATTERN } from "../../../property.js";
|
|
5
|
-
import { channelRequest, definiteTransportFailure, record, transportFailure, } from "./provider.js";
|
|
5
|
+
import { channelRequest, definiteTransportFailure, filesWithBytes, record, transportFailure, } from "./provider.js";
|
|
6
6
|
/** A Gmail id, of a thread or a message. */
|
|
7
7
|
const GMAIL_ID = /^[A-Za-z0-9_-]{1,512}$/;
|
|
8
8
|
const decoder = new TextDecoder();
|
|
@@ -88,7 +88,7 @@ export function sendEmail(input) {
|
|
|
88
88
|
html: input.finalText,
|
|
89
89
|
autoSubmitted: true,
|
|
90
90
|
...(/^[A-Za-z0-9_-]{1,128}$/.test(key) ? { replyKey: key } : {}),
|
|
91
|
-
...(files.length === 0 ? {} : { attachments: files }),
|
|
91
|
+
...(files.length === 0 ? {} : { attachments: filesWithBytes(files) }),
|
|
92
92
|
});
|
|
93
93
|
const payload = {
|
|
94
94
|
threadId,
|
|
@@ -116,14 +116,16 @@ export function sendEmail(input) {
|
|
|
116
116
|
payload,
|
|
117
117
|
// Gmail's refusal, or a proxy that never reached Gmail, sent nothing; anything else
|
|
118
118
|
// may have, such as an upload whose last answer was lost.
|
|
119
|
-
status:
|
|
120
|
-
|
|
121
|
-
cause
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
119
|
+
status: cause instanceof GmailMaybeSent
|
|
120
|
+
? "uncertain"
|
|
121
|
+
: (cause instanceof GmailError &&
|
|
122
|
+
cause.status >= 400 &&
|
|
123
|
+
cause.status < 500 &&
|
|
124
|
+
cause.status !== 408 &&
|
|
125
|
+
cause.status !== 429) ||
|
|
126
|
+
definiteTransportFailure(cause)
|
|
127
|
+
? "failed"
|
|
128
|
+
: "uncertain",
|
|
127
129
|
messageId: null,
|
|
128
130
|
url: null,
|
|
129
131
|
cause,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type ChatFileLimits, type ChatFilesRefusal, type ChatSend, type ProviderOutcome } from "./provider.js";
|
|
3
|
+
/** Telegram: up to ten files, each one request of the connection, by Telegram's type bounds. */
|
|
4
|
+
export declare const TELEGRAM_FILE_LIMITS: ChatFileLimits;
|
|
5
|
+
export declare function refuseTelegramFiles(files: ReadonlyArray<{
|
|
6
|
+
readonly contentType: string;
|
|
7
|
+
readonly size: number;
|
|
8
|
+
}>): ChatFilesRefusal | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Telegram: one message when the text fits a caption and the files go as one file or one album of
|
|
11
|
+
* one type; otherwise the text, then the files. The text keeps its MarkdownV2, sent again plain when
|
|
12
|
+
* Telegram refuses its formatting, as a text reply is.
|
|
13
|
+
*/
|
|
14
|
+
export declare function sendTelegramWithFiles(input: ChatSend, sendText: (input: ChatSend) => Effect.Effect<ProviderOutcome>): Effect.Effect<ProviderOutcome>;
|
|
15
|
+
/** Slack: up to ten files, each one request of the connection. */
|
|
16
|
+
export declare const SLACK_FILE_LIMITS: ChatFileLimits;
|
|
17
|
+
export declare function refuseSlackFiles(files: ReadonlyArray<{
|
|
18
|
+
readonly contentType: string;
|
|
19
|
+
readonly size: number;
|
|
20
|
+
}>): ChatFilesRefusal | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Slack: every file uploaded, which nobody sees, then one share of them all with the text: one
|
|
23
|
+
* visible message. A file that does not upload shares nothing, so the reply is definitely unsent.
|
|
24
|
+
*/
|
|
25
|
+
export declare function sendSlackWithFiles(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
26
|
+
/** WhatsApp: up to ten files, one message each, by WhatsApp's bound of each type. */
|
|
27
|
+
export declare const WHATSAPP_FILE_LIMITS: ChatFileLimits;
|
|
28
|
+
export declare function refuseWhatsAppFiles(files: ReadonlyArray<{
|
|
29
|
+
readonly contentType: string;
|
|
30
|
+
readonly size: number;
|
|
31
|
+
}>): ChatFilesRefusal | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* WhatsApp: one message per file, which WhatsApp fetches by a link granted just before it, the text
|
|
34
|
+
* as the first's caption, or first on its own when no caption carries it.
|
|
35
|
+
*/
|
|
36
|
+
export declare function sendWhatsAppWithFiles(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { MAXIMUM_CONNECTION_BODY_BYTES } from "../../../connection.js";
|
|
3
|
+
import { sendSlackFiles, SLACK_FILES_PER_MESSAGE, slackFileRefusal } from "../../../slack.js";
|
|
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";
|
|
6
|
+
import { channelRequest, definiteTransportFailure, filesOutcome, filesWithBytes, } from "./provider.js";
|
|
7
|
+
/**
|
|
8
|
+
* An approved reply with files on Telegram, Slack and WhatsApp, by each channel's sender of the
|
|
9
|
+
* ontology SDK, the same one the agent's `attachments_send` calls. Everything that can be refused
|
|
10
|
+
* was refused before the reply was reserved; here each visible part is sent once, and what it
|
|
11
|
+
* became is kept per file.
|
|
12
|
+
*/
|
|
13
|
+
/** A file a provider refuses on its own, as the delivery's refusal names it. */
|
|
14
|
+
function refusalOf(code) {
|
|
15
|
+
return code === "too_large"
|
|
16
|
+
? "file_too_large"
|
|
17
|
+
: code === "empty"
|
|
18
|
+
? "files_unsupported"
|
|
19
|
+
: undefined;
|
|
20
|
+
}
|
|
21
|
+
/** Telegram: up to ten files, each one request of the connection, by Telegram's type bounds. */
|
|
22
|
+
export const TELEGRAM_FILE_LIMITS = {
|
|
23
|
+
count: TELEGRAM_FILES_PER_MESSAGE,
|
|
24
|
+
bytes: TELEGRAM_FILES_PER_MESSAGE * MAXIMUM_CONNECTION_BODY_BYTES,
|
|
25
|
+
};
|
|
26
|
+
export function refuseTelegramFiles(files) {
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
const refusal = refusalOf(telegramFileRefusal(file)?.code);
|
|
29
|
+
if (refusal !== undefined) {
|
|
30
|
+
return refusal;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Telegram: one message when the text fits a caption and the files go as one file or one album of
|
|
37
|
+
* one type; otherwise the text, then the files. The text keeps its MarkdownV2, sent again plain when
|
|
38
|
+
* Telegram refuses its formatting, as a text reply is.
|
|
39
|
+
*/
|
|
40
|
+
export function sendTelegramWithFiles(input, sendText) {
|
|
41
|
+
return Effect.gen(function* () {
|
|
42
|
+
const files = input.files ?? [];
|
|
43
|
+
const thread = typeof input.threadId === "string" && /^[1-9][0-9]*$/u.test(input.threadId)
|
|
44
|
+
? input.threadId
|
|
45
|
+
: undefined;
|
|
46
|
+
const target = {
|
|
47
|
+
chatId: input.destination.external_id,
|
|
48
|
+
...(thread === undefined ? {} : { messageThreadId: thread }),
|
|
49
|
+
};
|
|
50
|
+
const payload = { text: input.finalText, parse_mode: "MarkdownV2" };
|
|
51
|
+
const send = (caption, replyTo) => Effect.promise(() => sendTelegramFiles({
|
|
52
|
+
request: channelRequest(input.transport, input.connection),
|
|
53
|
+
target: { ...target, ...(replyTo === null ? {} : { replyTo }) },
|
|
54
|
+
files: filesWithBytes(files),
|
|
55
|
+
...(caption
|
|
56
|
+
? {
|
|
57
|
+
caption: { text: input.finalText, entities: [], parseMode: "MarkdownV2" },
|
|
58
|
+
plainCaption: { text: input.plainText, entities: [] },
|
|
59
|
+
}
|
|
60
|
+
: {}),
|
|
61
|
+
definite: definiteTransportFailure,
|
|
62
|
+
}));
|
|
63
|
+
const types = new Set(files.map((file) => telegramFileType(file.contentType)));
|
|
64
|
+
const oneMessage = files.length === 1 || (types.size === 1 && files.length <= 10);
|
|
65
|
+
if (input.plainText.trim() === "" ||
|
|
66
|
+
(oneMessage && input.finalText.length <= TELEGRAM_CAPTION_LIMIT)) {
|
|
67
|
+
return filesOutcome(payload, files, yield* send(input.plainText.trim() !== "", input.replyTo));
|
|
68
|
+
}
|
|
69
|
+
const text = yield* sendText({ ...input, files: [] });
|
|
70
|
+
// Files never follow a text that did not certainly reach the person: it is never sent again.
|
|
71
|
+
if (text.status !== "sent") {
|
|
72
|
+
return {
|
|
73
|
+
...text,
|
|
74
|
+
payload: {
|
|
75
|
+
...text.payload,
|
|
76
|
+
files: files.map((file, index) => ({
|
|
77
|
+
index,
|
|
78
|
+
filename: file.filename,
|
|
79
|
+
status: "failed",
|
|
80
|
+
messageId: null,
|
|
81
|
+
})),
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const textPart = {
|
|
86
|
+
files: [],
|
|
87
|
+
text: true,
|
|
88
|
+
status: "sent",
|
|
89
|
+
messageIds: text.messageId === null ? [] : [text.messageId],
|
|
90
|
+
};
|
|
91
|
+
return filesOutcome(payload, files, [textPart, ...(yield* send(false, null))]);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/** Slack: up to ten files, each one request of the connection. */
|
|
95
|
+
export const SLACK_FILE_LIMITS = {
|
|
96
|
+
count: SLACK_FILES_PER_MESSAGE,
|
|
97
|
+
bytes: SLACK_FILES_PER_MESSAGE * MAXIMUM_CONNECTION_BODY_BYTES,
|
|
98
|
+
};
|
|
99
|
+
export function refuseSlackFiles(files) {
|
|
100
|
+
for (const file of files) {
|
|
101
|
+
const refusal = refusalOf(slackFileRefusal(file)?.code);
|
|
102
|
+
if (refusal !== undefined) {
|
|
103
|
+
return refusal;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Slack: every file uploaded, which nobody sees, then one share of them all with the text: one
|
|
110
|
+
* visible message. A file that does not upload shares nothing, so the reply is definitely unsent.
|
|
111
|
+
*/
|
|
112
|
+
export function sendSlackWithFiles(input) {
|
|
113
|
+
return Effect.gen(function* () {
|
|
114
|
+
const files = input.files ?? [];
|
|
115
|
+
const payload = { initial_comment: input.finalText };
|
|
116
|
+
const thread = input.replyTo ?? (typeof input.threadId === "string" ? input.threadId : null);
|
|
117
|
+
const result = yield* Effect.promise(() => sendSlackFiles({
|
|
118
|
+
request: channelRequest(input.transport, input.connection),
|
|
119
|
+
// A file is shared in the chat itself: a direct message's channel, not its member's id.
|
|
120
|
+
channel: input.chatId ?? input.destination.external_id,
|
|
121
|
+
...(thread === null || thread === "" ? {} : { threadTs: thread }),
|
|
122
|
+
files: filesWithBytes(files),
|
|
123
|
+
...(input.plainText.trim() === "" ? {} : { text: input.finalText }),
|
|
124
|
+
requireAll: true,
|
|
125
|
+
definite: definiteTransportFailure,
|
|
126
|
+
}));
|
|
127
|
+
if (result.share === undefined) {
|
|
128
|
+
const failed = result.uploads.flatMap((upload) => ("code" in upload ? [upload.cause] : []));
|
|
129
|
+
return filesOutcome(payload, files, [
|
|
130
|
+
{
|
|
131
|
+
files: files.map((_file, index) => index),
|
|
132
|
+
text: true,
|
|
133
|
+
status: "failed",
|
|
134
|
+
messageIds: [],
|
|
135
|
+
code: "send_failed",
|
|
136
|
+
cause: failed.length === 1
|
|
137
|
+
? failed[0]
|
|
138
|
+
: new AggregateError(failed, "Slack took not every file."),
|
|
139
|
+
},
|
|
140
|
+
]);
|
|
141
|
+
}
|
|
142
|
+
return filesOutcome(payload, files, [result.share]);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** WhatsApp: up to ten files, one message each, by WhatsApp's bound of each type. */
|
|
146
|
+
export const WHATSAPP_FILE_LIMITS = {
|
|
147
|
+
count: 10,
|
|
148
|
+
bytes: 10 * WHATSAPP_FILE_BYTES.document,
|
|
149
|
+
transfer: "link",
|
|
150
|
+
};
|
|
151
|
+
export function refuseWhatsAppFiles(files) {
|
|
152
|
+
for (const file of files) {
|
|
153
|
+
const refusal = refusalOf(whatsappFileRefusal(file)?.code);
|
|
154
|
+
if (refusal !== undefined) {
|
|
155
|
+
return refusal;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* WhatsApp: one message per file, which WhatsApp fetches by a link granted just before it, the text
|
|
162
|
+
* as the first's caption, or first on its own when no caption carries it.
|
|
163
|
+
*/
|
|
164
|
+
export function sendWhatsAppWithFiles(input) {
|
|
165
|
+
return Effect.gen(function* () {
|
|
166
|
+
const files = input.files ?? [];
|
|
167
|
+
const payload = { caption: input.finalText };
|
|
168
|
+
const parts = yield* Effect.promise(() => sendWhatsAppFiles({
|
|
169
|
+
request: channelRequest(input.transport, input.connection),
|
|
170
|
+
recipient: { to: input.destination.external_id },
|
|
171
|
+
files: files.map((file, index) => ({
|
|
172
|
+
index,
|
|
173
|
+
link: file.link ?? (async () => Promise.reject(new Error("The file has no link."))),
|
|
174
|
+
filename: file.filename,
|
|
175
|
+
contentType: file.contentType,
|
|
176
|
+
})),
|
|
177
|
+
...(input.plainText.trim() === "" ? {} : { caption: input.finalText }),
|
|
178
|
+
definite: definiteTransportFailure,
|
|
179
|
+
}));
|
|
180
|
+
return filesOutcome(payload, files, parts);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect, type Exit } from "effect";
|
|
2
|
-
import type { ChannelRequest } from "../../../channel-files.js";
|
|
2
|
+
import type { 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 ConnectionReply, type ConnectionTransport } from "../../../connection.js";
|
|
@@ -26,17 +26,36 @@ export interface ProviderOutcome {
|
|
|
26
26
|
readonly messageId: string | null;
|
|
27
27
|
}>;
|
|
28
28
|
}
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* A record file a send carries after its text, read before anything is sent: its bytes, or for a
|
|
31
|
+
* provider that fetches files itself a fresh link to them, asked for just before its message.
|
|
32
|
+
*/
|
|
30
33
|
export interface ChatSendFile {
|
|
31
34
|
readonly filename: string;
|
|
32
35
|
readonly contentType: string;
|
|
33
|
-
readonly
|
|
36
|
+
readonly size: number;
|
|
37
|
+
readonly bytes?: Uint8Array;
|
|
38
|
+
readonly link?: () => Promise<string>;
|
|
34
39
|
}
|
|
35
40
|
/** How many files one message of a provider carries, and how many bytes of them together. */
|
|
36
41
|
export interface ChatFileLimits {
|
|
37
42
|
readonly count: number;
|
|
38
43
|
readonly bytes: number;
|
|
44
|
+
/** `link` for a provider that fetches each file by a link; its bytes are sent otherwise. */
|
|
45
|
+
readonly transfer?: "bytes" | "link";
|
|
39
46
|
}
|
|
47
|
+
/** A send's files with their bytes, as a provider that uploads them reads them. */
|
|
48
|
+
export declare function filesWithBytes(files: ReadonlyArray<ChatSendFile>): ReadonlyArray<{
|
|
49
|
+
readonly filename: string;
|
|
50
|
+
readonly contentType: string;
|
|
51
|
+
readonly bytes: Uint8Array;
|
|
52
|
+
}>;
|
|
53
|
+
/**
|
|
54
|
+
* What a send of text and files became, from each visible part's result: `sent` when every part
|
|
55
|
+
* was, `failed` when none reached the person, `uncertain` otherwise. Each file's result is kept,
|
|
56
|
+
* so a partial delivery is never sent again but staff see what reached the person.
|
|
57
|
+
*/
|
|
58
|
+
export declare function filesOutcome(payload: Readonly<Record<string, unknown>>, files: ReadonlyArray<ChatSendFile>, parts: ReadonlyArray<ChannelPart>): ProviderOutcome;
|
|
40
59
|
/** Why a provider cannot send a message's files, before its key is reserved. */
|
|
41
60
|
export type ChatFilesRefusal = "files_unsupported" | "too_many_files" | "file_too_large" | "file_unavailable";
|
|
42
61
|
/** Why a chat's provider refused a message, for code to act on. */
|
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
import { Cause, Effect } from "effect";
|
|
2
2
|
import { ConnectionError, } from "../../../connection.js";
|
|
3
|
+
/** A send's files with their bytes, as a provider that uploads them reads them. */
|
|
4
|
+
export function filesWithBytes(files) {
|
|
5
|
+
return files.map((file) => ({
|
|
6
|
+
filename: file.filename,
|
|
7
|
+
contentType: file.contentType,
|
|
8
|
+
bytes: file.bytes ?? new Uint8Array(),
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* What a send of text and files became, from each visible part's result: `sent` when every part
|
|
13
|
+
* was, `failed` when none reached the person, `uncertain` otherwise. Each file's result is kept,
|
|
14
|
+
* so a partial delivery is never sent again but staff see what reached the person.
|
|
15
|
+
*/
|
|
16
|
+
export function filesOutcome(payload, files, parts) {
|
|
17
|
+
const results = files.map((file, index) => {
|
|
18
|
+
const part = parts.find((candidate) => candidate.files.includes(index));
|
|
19
|
+
const position = part?.files.indexOf(index) ?? -1;
|
|
20
|
+
return {
|
|
21
|
+
index,
|
|
22
|
+
filename: file.filename,
|
|
23
|
+
status: part?.status ?? "failed",
|
|
24
|
+
messageId: part?.status === "sent" ? (part.messageIds[position] ?? part.messageIds[0] ?? null) : null,
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
const sent = parts.length > 0 && parts.every((part) => part.status === "sent");
|
|
28
|
+
const none = parts.every((part) => part.status === "failed");
|
|
29
|
+
const first = parts.find((part) => part.status === "sent")?.messageIds[0] ?? null;
|
|
30
|
+
const causes = parts.flatMap((part) => (part.cause === undefined ? [] : [part.cause]));
|
|
31
|
+
return {
|
|
32
|
+
payload: { ...payload, files: results },
|
|
33
|
+
status: sent ? "sent" : none ? "failed" : "uncertain",
|
|
34
|
+
messageId: sent || !none ? first : null,
|
|
35
|
+
url: null,
|
|
36
|
+
files: results.map(({ index, status, messageId }) => ({ index, status, messageId })),
|
|
37
|
+
...(causes.length === 0
|
|
38
|
+
? {}
|
|
39
|
+
: {
|
|
40
|
+
cause: causes.length === 1
|
|
41
|
+
? causes[0]
|
|
42
|
+
: new AggregateError(causes, "Some parts of the message were not sent."),
|
|
43
|
+
}),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
3
46
|
/** The connection failures that prove the request never reached the provider. */
|
|
4
47
|
const DEFINITE_CONNECTION_FAILURES = new Set([
|
|
5
48
|
"authorization_required",
|