@segmentstream/ontology 0.160.0 → 0.162.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 +48 -0
- package/dist/agent-draft.d.ts +71 -4
- package/dist/agent-draft.js +177 -10
- package/dist/chat.d.ts +14 -0
- package/dist/errors.d.ts +4 -1
- package/dist/errors.js +4 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/runtime/agents.js +126 -3
- package/dist/runtime/chat-messages.d.ts +32 -0
- package/dist/runtime/chat-messages.js +39 -4
- package/dist/service/agent/protocol.d.ts +21 -5
- package/dist/service/agent/protocol.js +28 -3
- package/dist/service/agent/server.d.ts +3 -0
- package/dist/service/agent/server.js +104 -4
- package/dist/service/chat/archive.d.ts +15 -1
- package/dist/service/chat/delivery.js +27 -0
- package/dist/service/chat/draft-files.d.ts +56 -0
- package/dist/service/chat/draft-files.js +137 -0
- package/dist/service/chat/idempotency.d.ts +6 -1
- package/dist/service/chat/idempotency.js +7 -1
- package/dist/service/chat/providers/custom.js +12 -2
- package/dist/service/chat/providers/email.d.ts +2 -1
- package/dist/service/chat/providers/email.js +48 -4
- package/dist/service/chat/providers/provider.d.ts +49 -1
- package/dist/service/chat/providers/provider.js +18 -1
- package/dist/service/chat/review-delivery.d.ts +4 -0
- package/dist/service/chat/review-delivery.js +150 -18
- package/dist/service/chat/store-delivery.js +13 -0
- package/dist/service/runtime-contract.d.ts +45 -0
- package/dist/service/schemas.js +1 -1
- package/dist/service/service.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { AgentDraftFile } from "../../agent-draft.js";
|
|
3
|
+
import type { OntologyManifest } from "../../manifest.js";
|
|
4
|
+
import type { Principal } from "../../runtime.js";
|
|
5
|
+
import type { OntologyBackend } from "../runtime-contract.js";
|
|
6
|
+
import type { ChatFilesRefusal, ChatProvider, ChatSendFile } from "./providers/provider.js";
|
|
7
|
+
/**
|
|
8
|
+
* Record files a draft carries with its reply (`agent_draft.files`). The agent names them when it
|
|
9
|
+
* holds the reply; the ontology reads each as the person the reply is for and keeps the revision
|
|
10
|
+
* it read, which approval sends again only after reading it as that person once more. Nothing of a
|
|
11
|
+
* file's bytes is kept with the draft: the record keeps them, as long as it keeps that revision.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Why `provider` cannot send these files in one reply, checked before anything is reserved or
|
|
15
|
+
* sent: `files_unsupported` for a provider that sends none, `too_many_files` past its count,
|
|
16
|
+
* `file_too_large` past its bytes together, else what its own check says.
|
|
17
|
+
*/
|
|
18
|
+
export declare function refuseChatFiles(provider: ChatProvider, files: ReadonlyArray<{
|
|
19
|
+
readonly contentType: string;
|
|
20
|
+
readonly size: number;
|
|
21
|
+
}>): ChatFilesRefusal | undefined;
|
|
22
|
+
/** A file a reply names, by its record and file property. */
|
|
23
|
+
export interface DraftFileReference {
|
|
24
|
+
readonly entity: string;
|
|
25
|
+
readonly id: string;
|
|
26
|
+
readonly property?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The files a reply on `channel` names, read as `principal`, the person it is for: each record's
|
|
30
|
+
* revision of the file now, with its name, type and size. A file the person may not read, or that
|
|
31
|
+
* is gone, refuses them all as `file_unavailable` with its index; files the channel cannot send
|
|
32
|
+
* as its refusal.
|
|
33
|
+
*/
|
|
34
|
+
export declare function snapshotDraftFiles(input: {
|
|
35
|
+
readonly manifest: OntologyManifest;
|
|
36
|
+
readonly backend: OntologyBackend["Service"];
|
|
37
|
+
readonly principal: Principal;
|
|
38
|
+
readonly channel: string;
|
|
39
|
+
readonly files: ReadonlyArray<DraftFileReference>;
|
|
40
|
+
}): Effect.Effect<{
|
|
41
|
+
readonly files: ReadonlyArray<AgentDraftFile>;
|
|
42
|
+
} | {
|
|
43
|
+
readonly refusal: ChatFilesRefusal;
|
|
44
|
+
readonly index?: number;
|
|
45
|
+
}>;
|
|
46
|
+
/**
|
|
47
|
+
* The bytes of a draft's files for its delivery, each read as `person`, the member the reply is
|
|
48
|
+
* for, at the revision the draft holds; undefined when any of them is gone, no longer retained, or
|
|
49
|
+
* no longer theirs to read, or reads other than the revision held.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readDraftFiles(input: {
|
|
52
|
+
readonly backend: OntologyBackend["Service"];
|
|
53
|
+
readonly person: string | null;
|
|
54
|
+
readonly files: ReadonlyArray<AgentDraftFile>;
|
|
55
|
+
readonly fetch: typeof fetch;
|
|
56
|
+
}): Effect.Effect<ReadonlyArray<ChatSendFile> | undefined>;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { resolveFileRecord } from "../../runtime/files.js";
|
|
3
|
+
import { describeChatError } from "./diagnostics.js";
|
|
4
|
+
import { channelProvider } from "./providers/index.js";
|
|
5
|
+
/**
|
|
6
|
+
* Record files a draft carries with its reply (`agent_draft.files`). The agent names them when it
|
|
7
|
+
* holds the reply; the ontology reads each as the person the reply is for and keeps the revision
|
|
8
|
+
* it read, which approval sends again only after reading it as that person once more. Nothing of a
|
|
9
|
+
* file's bytes is kept with the draft: the record keeps them, as long as it keeps that revision.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Why `provider` cannot send these files in one reply, checked before anything is reserved or
|
|
13
|
+
* sent: `files_unsupported` for a provider that sends none, `too_many_files` past its count,
|
|
14
|
+
* `file_too_large` past its bytes together, else what its own check says.
|
|
15
|
+
*/
|
|
16
|
+
export function refuseChatFiles(provider, files) {
|
|
17
|
+
if (files.length === 0) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
if (provider.files === undefined) {
|
|
21
|
+
return "files_unsupported";
|
|
22
|
+
}
|
|
23
|
+
if (files.length > provider.files.count) {
|
|
24
|
+
return "too_many_files";
|
|
25
|
+
}
|
|
26
|
+
if (files.reduce((total, file) => total + file.size, 0) > provider.files.bytes) {
|
|
27
|
+
return "file_too_large";
|
|
28
|
+
}
|
|
29
|
+
return provider.refuseFiles?.(files);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The files a reply on `channel` names, read as `principal`, the person it is for: each record's
|
|
33
|
+
* revision of the file now, with its name, type and size. A file the person may not read, or that
|
|
34
|
+
* is gone, refuses them all as `file_unavailable` with its index; files the channel cannot send
|
|
35
|
+
* as its refusal.
|
|
36
|
+
*/
|
|
37
|
+
export function snapshotDraftFiles(input) {
|
|
38
|
+
return Effect.gen(function* () {
|
|
39
|
+
const { backend, principal } = input;
|
|
40
|
+
const blobs = backend.blobs;
|
|
41
|
+
const files = [];
|
|
42
|
+
if (blobs === undefined) {
|
|
43
|
+
return { refusal: "file_unavailable", index: 0 };
|
|
44
|
+
}
|
|
45
|
+
for (const [index, reference] of input.files.entries()) {
|
|
46
|
+
const file = yield* resolveFileRecord({
|
|
47
|
+
manifest: input.manifest,
|
|
48
|
+
reads: backend,
|
|
49
|
+
principal,
|
|
50
|
+
reference,
|
|
51
|
+
}).pipe(
|
|
52
|
+
// The revision read, granted to the person: a scoped read they may not make refuses it.
|
|
53
|
+
Effect.tap((resolved) => blobs.download(resolved.entity, resolved.id, resolved.property, { revisionId: resolved.file.id }, principal)), Effect.map((resolved) => ({
|
|
54
|
+
entity: resolved.entity,
|
|
55
|
+
id: resolved.id.toLowerCase(),
|
|
56
|
+
property: resolved.property,
|
|
57
|
+
revisionId: resolved.file.id,
|
|
58
|
+
filename: resolved.file.filename,
|
|
59
|
+
contentType: resolved.file.contentType,
|
|
60
|
+
size: resolved.file.size,
|
|
61
|
+
})),
|
|
62
|
+
// A refusal or a missing record; a defect of the store still fails the request. The
|
|
63
|
+
// original reason stays in the operator log.
|
|
64
|
+
Effect.catch((cause) => Effect.logWarning("ontology.agent_draft.file_unavailable").pipe(Effect.annotateLogs({
|
|
65
|
+
phase: "hold",
|
|
66
|
+
index,
|
|
67
|
+
error: describeChatError(cause, (text) => text),
|
|
68
|
+
}), Effect.as(undefined))));
|
|
69
|
+
if (file === undefined) {
|
|
70
|
+
return { refusal: "file_unavailable", index };
|
|
71
|
+
}
|
|
72
|
+
files.push(file);
|
|
73
|
+
}
|
|
74
|
+
const provider = channelProvider(input.manifest.chat, input.channel);
|
|
75
|
+
const refusal = provider === undefined ? "files_unsupported" : refuseChatFiles(provider, files);
|
|
76
|
+
return refusal === undefined ? { files } : { refusal };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The bytes of a draft's files for its delivery, each read as `person`, the member the reply is
|
|
81
|
+
* for, at the revision the draft holds; undefined when any of them is gone, no longer retained, or
|
|
82
|
+
* no longer theirs to read, or reads other than the revision held.
|
|
83
|
+
*/
|
|
84
|
+
export function readDraftFiles(input) {
|
|
85
|
+
return Effect.gen(function* () {
|
|
86
|
+
if (input.files.length === 0) {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
const { blobs, agents } = input.backend;
|
|
90
|
+
const member = input.person === null || agents === undefined
|
|
91
|
+
? undefined
|
|
92
|
+
: yield* agents.member(input.person);
|
|
93
|
+
if (blobs === undefined || member === undefined) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
const read = [];
|
|
97
|
+
for (const file of input.files) {
|
|
98
|
+
const bytes = yield* blobs
|
|
99
|
+
.download(file.entity, file.id, file.property, { revisionId: file.revisionId }, member.principal)
|
|
100
|
+
.pipe(Effect.flatMap((grant) => Effect.tryPromise(async () => {
|
|
101
|
+
const response = await input.fetch(grant.url, {
|
|
102
|
+
method: grant.method,
|
|
103
|
+
headers: grant.headers,
|
|
104
|
+
});
|
|
105
|
+
if (!response.ok) {
|
|
106
|
+
await response.body?.cancel();
|
|
107
|
+
throw new Error(`Storage answered the file's read with status ${response.status}.`);
|
|
108
|
+
}
|
|
109
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
110
|
+
}).pipe(
|
|
111
|
+
// The grant's link is a credential: no diagnostic shows it.
|
|
112
|
+
Effect.mapError((cause) => ({ cause, secret: grant.url })))), Effect.catch((failure) => Effect.logWarning("ontology.agent_draft.file_unavailable").pipe(Effect.annotateLogs({
|
|
113
|
+
phase: "approve",
|
|
114
|
+
entity: file.entity,
|
|
115
|
+
id: file.id,
|
|
116
|
+
revisionId: file.revisionId,
|
|
117
|
+
error: typeof failure === "object" && failure !== null && "secret" in failure
|
|
118
|
+
? describeChatError(failure.cause, (text) => text.split(failure.secret).join("[redacted]"))
|
|
119
|
+
: describeChatError(failure, (text) => text),
|
|
120
|
+
}), Effect.as(undefined))));
|
|
121
|
+
if (bytes === undefined || bytes.byteLength !== file.size) {
|
|
122
|
+
if (bytes !== undefined) {
|
|
123
|
+
yield* Effect.logWarning("ontology.agent_draft.file_unavailable").pipe(Effect.annotateLogs({
|
|
124
|
+
phase: "approve",
|
|
125
|
+
entity: file.entity,
|
|
126
|
+
id: file.id,
|
|
127
|
+
revisionId: file.revisionId,
|
|
128
|
+
error: `The revision read has ${bytes.byteLength} bytes, not the ${file.size} held.`,
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
read.push({ filename: file.filename, contentType: file.contentType, bytes });
|
|
134
|
+
}
|
|
135
|
+
return read;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
@@ -46,5 +46,10 @@ buttons?: ChatButtonRows,
|
|
|
46
46
|
recipients?: {
|
|
47
47
|
readonly to: ReadonlyArray<string>;
|
|
48
48
|
readonly cc: ReadonlyArray<string>;
|
|
49
|
-
}
|
|
49
|
+
},
|
|
50
|
+
/**
|
|
51
|
+
* The record files a message carries, each `[entity, id, property, revisionId]` in order, bound
|
|
52
|
+
* when given so a key of text alone keeps its binding.
|
|
53
|
+
*/
|
|
54
|
+
files?: ReadonlyArray<readonly [string, string, string, string]>): string;
|
|
50
55
|
export {};
|
|
@@ -27,7 +27,12 @@ buttons,
|
|
|
27
27
|
* Whom a reply to mail goes to beside its destination, bound when given so a key that names no
|
|
28
28
|
* one else keeps its binding.
|
|
29
29
|
*/
|
|
30
|
-
recipients
|
|
30
|
+
recipients,
|
|
31
|
+
/**
|
|
32
|
+
* The record files a message carries, each `[entity, id, property, revisionId]` in order, bound
|
|
33
|
+
* when given so a key of text alone keeps its binding.
|
|
34
|
+
*/
|
|
35
|
+
files) {
|
|
31
36
|
const sorted = (values) => Object.entries(values).sort(([one], [other]) => (one < other ? -1 : one > other ? 1 : 0));
|
|
32
37
|
const media = template?.header?.media;
|
|
33
38
|
return digest(JSON.stringify([
|
|
@@ -40,6 +45,7 @@ recipients) {
|
|
|
40
45
|
preview.text,
|
|
41
46
|
...(buttons === undefined ? [] : [{ buttons }]),
|
|
42
47
|
...(recipients === undefined ? [] : [{ to: recipients.to, cc: recipients.cc }]),
|
|
48
|
+
...(files === undefined || files.length === 0 ? [] : [{ files }]),
|
|
43
49
|
]
|
|
44
50
|
: [
|
|
45
51
|
{
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
|
|
3
|
+
import { renderWorkspaceChannelText, WORKSPACE_CHANNEL_ID, WORKSPACE_CHANNEL_FILES_BYTES, WORKSPACE_CHANNEL_TEXT_LIMIT, workspaceChannelFiles, workspaceChannelSendRoute, } from "../../../workspace-channel.js";
|
|
3
4
|
import { record } from "./provider.js";
|
|
4
5
|
/** How long the agent has to send one message through a workspace channel. */
|
|
5
6
|
const AGENT_SEND_TIMEOUT_MS = 30_000;
|
|
@@ -15,7 +16,11 @@ const ID = WORKSPACE_CHANNEL_ID;
|
|
|
15
16
|
*/
|
|
16
17
|
export function sendThroughAgent(input) {
|
|
17
18
|
return Effect.gen(function* () {
|
|
18
|
-
const
|
|
19
|
+
const files = input.files ?? [];
|
|
20
|
+
const payload = {
|
|
21
|
+
text: input.finalText,
|
|
22
|
+
...(files.length === 0 ? {} : { files: files.map((file) => file.filename) }),
|
|
23
|
+
};
|
|
19
24
|
const agent = input.agent;
|
|
20
25
|
if (agent?.origin === undefined || agent.identityToken === undefined) {
|
|
21
26
|
return {
|
|
@@ -35,6 +40,9 @@ export function sendThroughAgent(input) {
|
|
|
35
40
|
thread_id: input.threadId ?? null,
|
|
36
41
|
reply_to: input.replyTo,
|
|
37
42
|
text: input.finalText,
|
|
43
|
+
// Every agent with this route reads `files`, and refuses them with `files_unsupported`
|
|
44
|
+
// when the channel's code takes none: nothing is sent then.
|
|
45
|
+
...(files.length === 0 ? {} : { files: workspaceChannelFiles(files) }),
|
|
38
46
|
};
|
|
39
47
|
const answered = yield* Effect.tryPromise(async () => {
|
|
40
48
|
const response = await agent.fetch(new Request(new URL(workspaceChannelSendRoute(input.destination.provider), agent.origin), {
|
|
@@ -99,5 +107,7 @@ export const customProvider = {
|
|
|
99
107
|
return { finalText, plainText: finalText };
|
|
100
108
|
},
|
|
101
109
|
payload: (finalText) => ({ text: finalText }),
|
|
110
|
+
// The files go in the one message the channel's code sends, within the contract's bound.
|
|
111
|
+
files: { count: AGENT_DRAFT_FILES_LIMIT, bytes: WORKSPACE_CHANNEL_FILES_BYTES },
|
|
102
112
|
send: sendThroughAgent,
|
|
103
113
|
};
|
|
@@ -8,7 +8,8 @@ import { type ChatProvider, type ChatSend, type ProviderOutcome } from "./provid
|
|
|
8
8
|
* with the thread id, never retried. A reply sends no attribution and names no sender, so it
|
|
9
9
|
* leaves from the mailbox's own address. It carries `Auto-Submitted: auto-replied` and
|
|
10
10
|
* `X-SegmentStream-Reply: review-<review id>`, as the email channel's replies carry their own
|
|
11
|
-
* keys.
|
|
11
|
+
* keys. Files go after the text in the same message, uploaded in chunks when one request cannot
|
|
12
|
+
* carry it (`sendGmailMessage`). `sent` means Gmail accepted it; the payload keeps whom it went to.
|
|
12
13
|
*/
|
|
13
14
|
export declare function sendEmail(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
14
15
|
/**
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import {
|
|
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
4
|
import { EMAIL_PATTERN } from "../../../property.js";
|
|
4
|
-
import { record, transportFailure, } from "./provider.js";
|
|
5
|
+
import { channelRequest, definiteTransportFailure, record, transportFailure, } from "./provider.js";
|
|
5
6
|
/** A Gmail id, of a thread or a message. */
|
|
6
7
|
const GMAIL_ID = /^[A-Za-z0-9_-]{1,512}$/;
|
|
7
8
|
const decoder = new TextDecoder();
|
|
@@ -33,7 +34,8 @@ function headersOf(message) {
|
|
|
33
34
|
* with the thread id, never retried. A reply sends no attribution and names no sender, so it
|
|
34
35
|
* leaves from the mailbox's own address. It carries `Auto-Submitted: auto-replied` and
|
|
35
36
|
* `X-SegmentStream-Reply: review-<review id>`, as the email channel's replies carry their own
|
|
36
|
-
* keys.
|
|
37
|
+
* keys. Files go after the text in the same message, uploaded in chunks when one request cannot
|
|
38
|
+
* carry it (`sendGmailMessage`). `sent` means Gmail accepted it; the payload keeps whom it went to.
|
|
37
39
|
*/
|
|
38
40
|
export function sendEmail(input) {
|
|
39
41
|
return Effect.gen(function* () {
|
|
@@ -75,6 +77,7 @@ export function sendEmail(input) {
|
|
|
75
77
|
const key = `review-${input.destination.id}`;
|
|
76
78
|
const to = input.recipients?.to ?? [input.destination.external_id];
|
|
77
79
|
const cc = input.recipients?.cc ?? [];
|
|
80
|
+
const files = input.files ?? [];
|
|
78
81
|
const raw = emailReplyMessage({
|
|
79
82
|
to,
|
|
80
83
|
...(cc.length === 0 ? {} : { cc }),
|
|
@@ -85,8 +88,47 @@ export function sendEmail(input) {
|
|
|
85
88
|
html: input.finalText,
|
|
86
89
|
autoSubmitted: true,
|
|
87
90
|
...(/^[A-Za-z0-9_-]{1,128}$/.test(key) ? { replyKey: key } : {}),
|
|
91
|
+
...(files.length === 0 ? {} : { attachments: files }),
|
|
88
92
|
});
|
|
89
|
-
const payload = {
|
|
93
|
+
const payload = {
|
|
94
|
+
threadId,
|
|
95
|
+
to,
|
|
96
|
+
cc,
|
|
97
|
+
html: input.finalText,
|
|
98
|
+
text: input.plainText,
|
|
99
|
+
...(files.length === 0 ? {} : { files: files.map((file) => file.filename) }),
|
|
100
|
+
};
|
|
101
|
+
// A reply with files is one message too, uploaded when one request cannot carry it.
|
|
102
|
+
if (files.length > 0) {
|
|
103
|
+
return yield* Effect.promise(() => sendGmailMessage({
|
|
104
|
+
request: channelRequest(input.transport, input.connection),
|
|
105
|
+
message: new TextEncoder().encode(raw),
|
|
106
|
+
threadId,
|
|
107
|
+
}).then((answer) => typeof answer.id === "string" && GMAIL_ID.test(answer.id)
|
|
108
|
+
? { payload, status: "sent", messageId: answer.id, url: null }
|
|
109
|
+
: {
|
|
110
|
+
payload,
|
|
111
|
+
status: "uncertain",
|
|
112
|
+
messageId: null,
|
|
113
|
+
url: null,
|
|
114
|
+
cause: new Error("Gmail accepted the reply without its id."),
|
|
115
|
+
}, (cause) => ({
|
|
116
|
+
payload,
|
|
117
|
+
// Gmail's refusal, or a proxy that never reached Gmail, sent nothing; anything else
|
|
118
|
+
// may have, such as an upload whose last answer was lost.
|
|
119
|
+
status: (cause instanceof GmailError &&
|
|
120
|
+
cause.status >= 400 &&
|
|
121
|
+
cause.status < 500 &&
|
|
122
|
+
cause.status !== 408 &&
|
|
123
|
+
cause.status !== 429) ||
|
|
124
|
+
definiteTransportFailure(cause)
|
|
125
|
+
? "failed"
|
|
126
|
+
: "uncertain",
|
|
127
|
+
messageId: null,
|
|
128
|
+
url: null,
|
|
129
|
+
cause,
|
|
130
|
+
})));
|
|
131
|
+
}
|
|
90
132
|
const result = yield* input.transport
|
|
91
133
|
.request({
|
|
92
134
|
connection: input.connection,
|
|
@@ -140,5 +182,7 @@ export const emailProvider = {
|
|
|
140
182
|
return { finalText: renderEmailHtml(text), plainText: text };
|
|
141
183
|
},
|
|
142
184
|
payload: (finalText) => ({ html: finalText }),
|
|
185
|
+
// The files go in the reply's one message, under the bound every email sender keeps.
|
|
186
|
+
files: { count: AGENT_DRAFT_FILES_LIMIT, bytes: EMAIL_FILES_BYTES },
|
|
143
187
|
send: sendEmail,
|
|
144
188
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect, type Exit } from "effect";
|
|
2
|
+
import type { ChannelRequest } from "../../../channel-files.js";
|
|
2
3
|
import type { ChatButtonRows } from "../../../chat.js";
|
|
3
4
|
import type { TelegramPreviewContext } from "../../../telegram.js";
|
|
4
5
|
import { ConnectionError, type ConnectionReply, type ConnectionTransport } from "../../../connection.js";
|
|
@@ -15,7 +16,29 @@ export interface ProviderOutcome {
|
|
|
15
16
|
readonly cause?: unknown;
|
|
16
17
|
/** Why the provider refused a `failed` message, when it says: see `ActionRejected.refusal`. */
|
|
17
18
|
readonly refusal?: ChatRefusal;
|
|
19
|
+
/**
|
|
20
|
+
* What became of each file of a send with files, by its index, when the provider sent them in
|
|
21
|
+
* several messages: one that reached the person, one that did not, or one that may have.
|
|
22
|
+
*/
|
|
23
|
+
readonly files?: ReadonlyArray<{
|
|
24
|
+
readonly index: number;
|
|
25
|
+
readonly status: "sent" | "uncertain" | "failed";
|
|
26
|
+
readonly messageId: string | null;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
/** A record file a send carries after its text, read before anything is sent. */
|
|
30
|
+
export interface ChatSendFile {
|
|
31
|
+
readonly filename: string;
|
|
32
|
+
readonly contentType: string;
|
|
33
|
+
readonly bytes: Uint8Array;
|
|
18
34
|
}
|
|
35
|
+
/** How many files one message of a provider carries, and how many bytes of them together. */
|
|
36
|
+
export interface ChatFileLimits {
|
|
37
|
+
readonly count: number;
|
|
38
|
+
readonly bytes: number;
|
|
39
|
+
}
|
|
40
|
+
/** Why a provider cannot send a message's files, before its key is reserved. */
|
|
41
|
+
export type ChatFilesRefusal = "files_unsupported" | "too_many_files" | "file_too_large" | "file_unavailable";
|
|
19
42
|
/** Why a chat's provider refused a message, for code to act on. */
|
|
20
43
|
export type ChatRefusal = "outside_reply_window" | "recipient_unreachable" | "recipient_opted_out";
|
|
21
44
|
/**
|
|
@@ -60,6 +83,11 @@ export interface ChatSend {
|
|
|
60
83
|
* a workspace channel's send receives it, others ignore it.
|
|
61
84
|
*/
|
|
62
85
|
readonly chatId?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Files the message carries after its text, in order, for a provider that declares `files`;
|
|
88
|
+
* the text may be empty with them.
|
|
89
|
+
*/
|
|
90
|
+
readonly files?: ReadonlyArray<ChatSendFile>;
|
|
63
91
|
/**
|
|
64
92
|
* Whom a reply to mail goes to, in `To` and `Cc`, in place of the destination alone; others
|
|
65
93
|
* ignore it.
|
|
@@ -111,6 +139,19 @@ export interface ChatProvider {
|
|
|
111
139
|
* provider that shows no buttons, which refuses them all.
|
|
112
140
|
*/
|
|
113
141
|
readonly refuseButtons?: (rows: ChatButtonRows, finalText: string) => string | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* The files one message carries: absent for a provider that sends none, which refuses every
|
|
144
|
+
* file with `files_unsupported`.
|
|
145
|
+
*/
|
|
146
|
+
readonly files?: ChatFileLimits;
|
|
147
|
+
/**
|
|
148
|
+
* Why the provider cannot send files of these types and sizes beyond its `files` limits, or
|
|
149
|
+
* undefined when it can. It is checked before the message is reserved, as `refuseButtons` is.
|
|
150
|
+
*/
|
|
151
|
+
readonly refuseFiles?: (files: ReadonlyArray<{
|
|
152
|
+
readonly contentType: string;
|
|
153
|
+
readonly size: number;
|
|
154
|
+
}>) => ChatFilesRefusal | undefined;
|
|
114
155
|
/** One attempt: `sent` only when the provider's answer proves it. */
|
|
115
156
|
readonly send: (input: ChatSend) => Effect.Effect<ProviderOutcome>;
|
|
116
157
|
/**
|
|
@@ -130,6 +171,13 @@ export interface ChatProvider {
|
|
|
130
171
|
* before the provider saw it, otherwise `uncertain`, since the provider may have acted on it.
|
|
131
172
|
*/
|
|
132
173
|
export declare function transportFailure(payload: ProviderOutcome["payload"], result: Exit.Failure<ConnectionReply, ConnectionError>): ProviderOutcome;
|
|
174
|
+
/** Whether an error of the connection transport proves the request never reached the provider. */
|
|
175
|
+
export declare function definiteTransportFailure(error: unknown): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* The connection transport as the channels' shared senders call it (`ChannelRequest`): one call on
|
|
178
|
+
* `connection`, which rejects with the transport's own error when it did not relay the call.
|
|
179
|
+
*/
|
|
180
|
+
export declare function channelRequest(transport: ConnectionTransport["Service"], connection: string): ChannelRequest;
|
|
133
181
|
export declare const record: (value: unknown) => Record<string, unknown>;
|
|
134
182
|
/**
|
|
135
183
|
* A message's buttons as its history keeps them, one a line, as the person reads them: a link where
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Cause } from "effect";
|
|
1
|
+
import { Cause, Effect } from "effect";
|
|
2
2
|
import { ConnectionError, } from "../../../connection.js";
|
|
3
3
|
/** The connection failures that prove the request never reached the provider. */
|
|
4
4
|
const DEFINITE_CONNECTION_FAILURES = new Set([
|
|
@@ -23,6 +23,23 @@ export function transportFailure(payload, result) {
|
|
|
23
23
|
cause,
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
+
/** Whether an error of the connection transport proves the request never reached the provider. */
|
|
27
|
+
export function definiteTransportFailure(error) {
|
|
28
|
+
return error instanceof ConnectionError && DEFINITE_CONNECTION_FAILURES.has(error.reason);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The connection transport as the channels' shared senders call it (`ChannelRequest`): one call on
|
|
32
|
+
* `connection`, which rejects with the transport's own error when it did not relay the call.
|
|
33
|
+
*/
|
|
34
|
+
export function channelRequest(transport, connection) {
|
|
35
|
+
return async (call) => {
|
|
36
|
+
const exit = await Effect.runPromiseExit(transport.request({ ...call, connection }));
|
|
37
|
+
if (exit._tag === "Failure") {
|
|
38
|
+
throw Cause.squash(exit.cause);
|
|
39
|
+
}
|
|
40
|
+
return exit.value;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
26
43
|
export const record = (value) => value !== null && typeof value === "object" && !Array.isArray(value)
|
|
27
44
|
? value
|
|
28
45
|
: {};
|
|
@@ -36,14 +36,18 @@ export declare function reviewSender(input: {
|
|
|
36
36
|
readonly principal: Principal;
|
|
37
37
|
readonly identityToken: string | undefined;
|
|
38
38
|
readonly requestId: string;
|
|
39
|
+
/** Reads a file's bytes by its download grant; `fetch` by default. */
|
|
40
|
+
readonly fetch?: typeof fetch;
|
|
39
41
|
}): (send: ReviewSendInput) => Effect.Effect<{
|
|
40
42
|
recipients?: EmailRecipients | undefined;
|
|
43
|
+
message?: string | undefined;
|
|
41
44
|
status: "sent" | "uncertain" | "failed";
|
|
42
45
|
text: string;
|
|
43
46
|
messageId: string | null;
|
|
44
47
|
duplicate: true;
|
|
45
48
|
} | {
|
|
46
49
|
recipients?: EmailRecipients | undefined;
|
|
50
|
+
message?: string | undefined;
|
|
47
51
|
status: "sent" | "uncertain" | "failed";
|
|
48
52
|
text: string;
|
|
49
53
|
messageId: string | null;
|