@segmentstream/agent 0.52.0 → 0.53.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 +36 -0
- package/README.md +6 -2
- package/dist/_chunks/{build-metadata-D1C8gnfm.mjs → build-metadata-Bc9qfC4D.mjs} +1 -1
- package/dist/extension/instructions.md +8 -5
- package/dist/extension/lib/attachments-send.d.ts +84 -0
- package/dist/extension/lib/attachments-send.mjs +88 -0
- package/dist/extension/lib/build-metadata.mjs +1 -1
- package/dist/extension/lib/chat-archive.d.ts +35 -0
- package/dist/extension/lib/chat-archive.mjs +37 -1
- package/dist/extension/lib/chat-context.d.ts +1 -1
- package/dist/extension/lib/chat-context.mjs +3 -2
- package/dist/extension/lib/chat-recorder.mjs +2 -1
- package/dist/extension/lib/email/attachments-send.d.ts +43 -0
- package/dist/extension/lib/email/attachments-send.mjs +130 -0
- package/dist/extension/lib/email/channel.d.ts +7 -1
- package/dist/extension/lib/email/channel.mjs +221 -52
- package/dist/extension/lib/email/gmail.d.ts +22 -6
- package/dist/extension/lib/email/gmail.mjs +101 -5
- package/dist/extension/lib/email/message.d.ts +19 -0
- package/dist/extension/lib/email/message.mjs +22 -1
- package/dist/extension/lib/review/judge.mjs +2 -2
- package/dist/extension/lib/sent-files.d.ts +29 -9
- package/dist/extension/lib/sent-files.mjs +35 -13
- package/dist/extension/lib/slack/attachments-send.d.ts +6 -14
- package/dist/extension/lib/slack/attachments-send.mjs +128 -170
- package/dist/extension/lib/telegram/adapter.mjs +1 -1
- package/dist/extension/lib/telegram/attachments-send.mjs +8 -11
- package/dist/extension/lib/telegram/health.mjs +1 -1
- package/dist/extension/lib/telegram/lifecycle.mjs +1 -1
- package/dist/extension/lib/web/channel.d.ts +10 -3
- package/dist/extension/lib/web/channel.mjs +9 -2
- package/dist/extension/lib/whatsapp/attachments-send.d.ts +35 -0
- package/dist/extension/lib/whatsapp/attachments-send.mjs +117 -0
- package/dist/extension/tools/attachments_send.mjs +17 -6
- package/package.json +2 -2
|
@@ -2,8 +2,9 @@ import { AttachmentError } from "./attachments.js";
|
|
|
2
2
|
import type { AttachmentOwner } from "./attachments.js";
|
|
3
3
|
/**
|
|
4
4
|
* A stored ontology file the agent sends into a chat: its record, the ontology's file grant and
|
|
5
|
-
* the file's bytes, read as the turn's member in the turn's chat.
|
|
6
|
-
*
|
|
5
|
+
* the file's bytes, read as the turn's member in the turn's chat. Every channel's sender shares
|
|
6
|
+
* it; each says how many bytes its destination takes. WhatsApp, which sends files by link, reads
|
|
7
|
+
* only the grant.
|
|
7
8
|
*/
|
|
8
9
|
/** A file record: a document by its id, or any entity's record and file property. */
|
|
9
10
|
export interface DocumentReference {
|
|
@@ -33,13 +34,18 @@ export declare class StoredFileError extends AttachmentError {
|
|
|
33
34
|
}
|
|
34
35
|
/** Do not trust storage metadata or Content-Length to bound the downloaded bytes. */
|
|
35
36
|
export declare function boundedDownload(response: Response, maximum: number, tooLarge: () => Error): Promise<Uint8Array>;
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
/** A stored file's record and the ontology's short-lived grant to download it. */
|
|
38
|
+
export interface StoredFileGrant {
|
|
39
|
+
readonly filename: string;
|
|
40
|
+
/** Lower case. */
|
|
41
|
+
readonly contentType: string;
|
|
42
|
+
readonly size: number;
|
|
43
|
+
readonly url: string;
|
|
44
|
+
/** Headers the download must send; a grant a third party can follow has none. */
|
|
45
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
46
|
+
}
|
|
47
|
+
/** What reading a stored file needs: the member, the chat and the deployment's credentials. */
|
|
48
|
+
interface StoredFileRead {
|
|
43
49
|
readonly reference: DocumentReference;
|
|
44
50
|
readonly owner: AttachmentOwner;
|
|
45
51
|
/** The deployment's own origin, where the ontology service answers. */
|
|
@@ -47,6 +53,19 @@ export declare function fetchStoredFile(input: {
|
|
|
47
53
|
readonly token: () => Promise<string>;
|
|
48
54
|
readonly fetch: typeof fetch;
|
|
49
55
|
readonly secrets: Set<string>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Reads one stored file's grant from the ontology, as the turn's member in the turn's chat. The
|
|
59
|
+
* grant's URL, headers and signed parameters are added to `secrets`, so a diagnostic never shows
|
|
60
|
+
* them.
|
|
61
|
+
*/
|
|
62
|
+
export declare function storedFileGrant(input: StoredFileRead): Promise<StoredFileGrant>;
|
|
63
|
+
/**
|
|
64
|
+
* Reads one stored file: its grant from the ontology, then its bytes. `limit` sees the file's type
|
|
65
|
+
* and size before anything is downloaded and answers the most bytes the destination takes, or
|
|
66
|
+
* throws to refuse it.
|
|
67
|
+
*/
|
|
68
|
+
export declare function fetchStoredFile(input: StoredFileRead & {
|
|
50
69
|
readonly limit: (file: {
|
|
51
70
|
readonly contentType: string;
|
|
52
71
|
readonly size: number;
|
|
@@ -54,3 +73,4 @@ export declare function fetchStoredFile(input: {
|
|
|
54
73
|
/** The error a download over the limit throws. */
|
|
55
74
|
readonly tooLarge: () => Error;
|
|
56
75
|
}): Promise<StoredFile>;
|
|
76
|
+
export {};
|
|
@@ -39,9 +39,19 @@ async function boundedDownload(response, maximum, tooLarge) {
|
|
|
39
39
|
}
|
|
40
40
|
return new Uint8Array(Buffer.concat(chunks));
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
function storedFileError(error, known) {
|
|
43
|
+
if (error instanceof StoredFileError) return error;
|
|
44
|
+
return new StoredFileError({
|
|
45
|
+
code: error instanceof AttachmentError ? error.code : "unavailable",
|
|
46
|
+
message: error instanceof Error ? error.message : "The stored file could not be read.",
|
|
47
|
+
...error instanceof AttachmentError && error.status !== void 0 ? { status: error.status } : {},
|
|
48
|
+
filename: known.filename,
|
|
49
|
+
...known.requestId === void 0 ? {} : { requestId: known.requestId },
|
|
50
|
+
cause: error
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async function storedFileGrant(input) {
|
|
43
54
|
const { reference, owner } = input;
|
|
44
|
-
let filename = "";
|
|
45
55
|
let requestId;
|
|
46
56
|
try {
|
|
47
57
|
const path = [
|
|
@@ -79,16 +89,32 @@ async function fetchStoredFile(input) {
|
|
|
79
89
|
code: "invalid_response",
|
|
80
90
|
message: "The ontology returned invalid file metadata."
|
|
81
91
|
});
|
|
82
|
-
filename = file.filename;
|
|
83
92
|
input.secrets.add(grant.url);
|
|
84
93
|
Object.values(grant.headers).forEach((header) => input.secrets.add(header));
|
|
85
94
|
new URL(grant.url).searchParams.forEach((value, name) => {
|
|
86
95
|
if (/signature|credential|token|key/i.test(name)) input.secrets.add(value);
|
|
87
96
|
});
|
|
88
|
-
|
|
97
|
+
return {
|
|
98
|
+
filename: file.filename,
|
|
99
|
+
contentType: file.content_type.toLowerCase(),
|
|
100
|
+
size: file.size,
|
|
101
|
+
url: grant.url,
|
|
102
|
+
headers: grant.headers
|
|
103
|
+
};
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw storedFileError(error, {
|
|
106
|
+
filename: "",
|
|
107
|
+
requestId
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function fetchStoredFile(input) {
|
|
112
|
+
const grant = await storedFileGrant(input);
|
|
113
|
+
const { filename, contentType } = grant;
|
|
114
|
+
try {
|
|
89
115
|
const maximum = input.limit({
|
|
90
116
|
contentType,
|
|
91
|
-
size:
|
|
117
|
+
size: grant.size
|
|
92
118
|
});
|
|
93
119
|
const download = await input.fetch(grant.url, {
|
|
94
120
|
method: "GET",
|
|
@@ -101,7 +127,7 @@ async function fetchStoredFile(input) {
|
|
|
101
127
|
message: "The stored file could not be downloaded."
|
|
102
128
|
});
|
|
103
129
|
const bytes = await boundedDownload(download, maximum, input.tooLarge);
|
|
104
|
-
if (bytes.byteLength !==
|
|
130
|
+
if (bytes.byteLength !== grant.size) throw new AttachmentError({
|
|
105
131
|
code: "invalid_response",
|
|
106
132
|
message: "The downloaded size differs from the stored file metadata."
|
|
107
133
|
});
|
|
@@ -111,14 +137,10 @@ async function fetchStoredFile(input) {
|
|
|
111
137
|
bytes
|
|
112
138
|
};
|
|
113
139
|
} catch (error) {
|
|
114
|
-
throw
|
|
115
|
-
code: error instanceof AttachmentError ? error.code : "unavailable",
|
|
116
|
-
message: error instanceof Error ? error.message : "The stored file could not be read.",
|
|
117
|
-
...error instanceof AttachmentError && error.status !== void 0 ? { status: error.status } : {},
|
|
140
|
+
throw storedFileError(error, {
|
|
118
141
|
filename,
|
|
119
|
-
|
|
120
|
-
cause: error
|
|
142
|
+
requestId: void 0
|
|
121
143
|
});
|
|
122
144
|
}
|
|
123
145
|
}
|
|
124
|
-
export { StoredFileError, boundedDownload, fetchStoredFile };
|
|
146
|
+
export { StoredFileError, boundedDownload, fetchStoredFile, storedFileGrant };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { SessionAuthContext } from "eve/context";
|
|
2
|
+
import { type AttachmentSendFailure, type AttachmentSendRefusal } from "../attachments-send.js";
|
|
2
3
|
import { type Environment } from "../environment.js";
|
|
3
|
-
import {
|
|
4
|
+
import type { RuntimeToken } from "../runtime-token.js";
|
|
4
5
|
import type { SendAttachmentsInput } from "../telegram/attachments-send.js";
|
|
5
6
|
/**
|
|
6
7
|
* `attachments_send` in Slack: stored ontology files posted into the turn's Slack chat,
|
|
@@ -8,28 +9,19 @@ import type { SendAttachmentsInput } from "../telegram/attachments-send.js";
|
|
|
8
9
|
* bytes posted to the address Slack answers, then one `files.completeUploadExternal` that shares
|
|
9
10
|
* them all in the chat with the caption. Every call goes through the Slack connection,
|
|
10
11
|
* which adds the bot token and needs `files:write`; each upload carries at most the connection
|
|
11
|
-
* proxy's 2 MiB, as Telegram's do.
|
|
12
|
+
* proxy's 2 MiB, as Telegram's do. The checks and failures are every channel's
|
|
13
|
+
* (`../attachments-send.ts`).
|
|
12
14
|
*/
|
|
13
15
|
/** What became of one requested file. */
|
|
14
|
-
export interface SlackSentFile {
|
|
16
|
+
export interface SlackSentFile extends Partial<AttachmentSendFailure> {
|
|
15
17
|
readonly id: string;
|
|
16
18
|
readonly filename: string;
|
|
17
19
|
/** Slack's id of the shared file. */
|
|
18
20
|
readonly slackFileId?: string;
|
|
19
|
-
readonly error?: string;
|
|
20
|
-
readonly detail?: string;
|
|
21
|
-
readonly reference?: string;
|
|
22
|
-
readonly status?: number;
|
|
23
|
-
readonly retryable?: boolean;
|
|
24
|
-
readonly requestId?: string;
|
|
25
21
|
}
|
|
26
22
|
type SlackSendResult = {
|
|
27
23
|
readonly files: ReadonlyArray<SlackSentFile>;
|
|
28
|
-
} |
|
|
29
|
-
readonly error: string;
|
|
30
|
-
readonly detail: string;
|
|
31
|
-
readonly reference: string;
|
|
32
|
-
};
|
|
24
|
+
} | AttachmentSendRefusal;
|
|
33
25
|
export declare function sendSlackAttachments(options: {
|
|
34
26
|
readonly input: SendAttachmentsInput;
|
|
35
27
|
readonly principal: SessionAuthContext | null;
|
|
@@ -4,15 +4,12 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { _ as AttachmentError } from "../../../_chunks/principal-JoaOmdFL.mjs";
|
|
8
|
+
import { sendStoredAttachments } from "../attachments-send.mjs";
|
|
9
9
|
import { MAXIMUM_PROXY_BODY_BYTES, connectionProxy } from "../connection-proxy.mjs";
|
|
10
10
|
import { redactDiagnostic } from "../diagnostics.mjs";
|
|
11
|
-
import {
|
|
12
|
-
import { transientFileStatus } from "../file-grant.mjs";
|
|
13
|
-
import { runtimeToken } from "../runtime-token.mjs";
|
|
11
|
+
import { proxyEnvironment } from "../environment.mjs";
|
|
14
12
|
import { StoredFileError, fetchStoredFile } from "../sent-files.mjs";
|
|
15
|
-
import { confirmationLocale } from "../telegram/confirmation-text.mjs";
|
|
16
13
|
import { proxiedSlackFetch } from "./adapter.mjs";
|
|
17
14
|
import { randomUUID } from "node:crypto";
|
|
18
15
|
const SLACK_API = "https://slack.com/api/";
|
|
@@ -36,172 +33,133 @@ function reportSlackError(operation, error, secrets) {
|
|
|
36
33
|
return reference;
|
|
37
34
|
}
|
|
38
35
|
async function sendSlackAttachments(options) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
detail: failureText(reference, "attachment", locale),
|
|
51
|
-
reference
|
|
52
|
-
};
|
|
53
|
-
};
|
|
54
|
-
const chat = chatAddressOf(principal);
|
|
55
|
-
if (!channelsEnabled(environment)) return unavailable("channels_disabled", "Attachments can be sent only in production.");
|
|
56
|
-
if (options.connection === void 0) return unavailable("unsupported", "The Slack channel is not configured.");
|
|
57
|
-
if (chat?.provider !== "slack") return unavailable("no_slack_chat", "This turn has no Slack chat.");
|
|
58
|
-
let owner;
|
|
59
|
-
try {
|
|
60
|
-
owner = attachmentOwner({
|
|
61
|
-
principal,
|
|
62
|
-
sessionId: options.sessionId
|
|
63
|
-
});
|
|
64
|
-
} catch (error) {
|
|
65
|
-
const reference = reportSlackError("attachment_owner_refused", error, secrets);
|
|
66
|
-
return {
|
|
67
|
-
error: "forbidden",
|
|
68
|
-
detail: failureText(reference, "attachment", locale),
|
|
69
|
-
reference
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
const runtimeToken$1 = options.token ?? runtimeToken;
|
|
73
|
-
const token = async () => {
|
|
74
|
-
const value = await runtimeToken$1();
|
|
75
|
-
secrets.add(value);
|
|
76
|
-
return value;
|
|
77
|
-
};
|
|
78
|
-
const fetchImpl = options.fetch ?? fetch;
|
|
79
|
-
const connection = options.connection;
|
|
80
|
-
const slack = proxiedSlackFetch(() => connectionProxy({
|
|
81
|
-
environment: proxyEnvironment(environment),
|
|
82
|
-
connection,
|
|
83
|
-
token,
|
|
84
|
-
fetch: fetchImpl
|
|
85
|
-
}), fetchImpl);
|
|
86
|
-
const call = async (method, init) => {
|
|
87
|
-
const response = await slack(`${SLACK_API}${method}`, {
|
|
88
|
-
method: "POST",
|
|
89
|
-
...init
|
|
90
|
-
});
|
|
91
|
-
const answer = await response.json().catch(() => void 0);
|
|
92
|
-
if (!response.ok || !isRecord(answer) || answer.ok !== true) {
|
|
93
|
-
const code = isRecord(answer) && typeof answer.error === "string" ? answer.error : "";
|
|
94
|
-
throw new AttachmentError({
|
|
95
|
-
code: code === "missing_scope" || code === "not_in_channel" ? "forbidden" : "send_failed",
|
|
96
|
-
message: `Slack refused ${method}${code === "" ? "" : `: ${code}`}.`,
|
|
97
|
-
status: response.status
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
return answer;
|
|
101
|
-
};
|
|
102
|
-
const results = options.input.documentIds.map(({ id }) => ({
|
|
103
|
-
id,
|
|
104
|
-
filename: ""
|
|
105
|
-
}));
|
|
106
|
-
const uploaded = [];
|
|
107
|
-
for (const [index, reference] of options.input.documentIds.entries()) {
|
|
108
|
-
const failed = (error, operation) => {
|
|
109
|
-
const errorReference = reportSlackError(operation, error, secrets);
|
|
110
|
-
const stored = error instanceof StoredFileError ? error : void 0;
|
|
111
|
-
results[index] = {
|
|
112
|
-
id: reference.id,
|
|
113
|
-
filename: stored?.filename ?? results[index].filename,
|
|
114
|
-
error: error instanceof AttachmentError ? error.code : "send_failed",
|
|
115
|
-
detail: failureText(errorReference, "attachment", locale),
|
|
116
|
-
reference: errorReference,
|
|
117
|
-
...error instanceof AttachmentError && error.status !== void 0 ? {
|
|
118
|
-
status: error.status,
|
|
119
|
-
retryable: transientFileStatus(error.status)
|
|
120
|
-
} : {},
|
|
121
|
-
...stored?.requestId === void 0 ? {} : { requestId: stored.requestId }
|
|
122
|
-
};
|
|
123
|
-
};
|
|
124
|
-
let file;
|
|
125
|
-
try {
|
|
126
|
-
file = await fetchStoredFile({
|
|
127
|
-
reference,
|
|
128
|
-
owner,
|
|
129
|
-
origin: appOrigin(environment),
|
|
36
|
+
return sendStoredAttachments({
|
|
37
|
+
...options,
|
|
38
|
+
provider: "slack",
|
|
39
|
+
channel: "Slack",
|
|
40
|
+
report: (operation, error, _metadata, secrets) => reportSlackError(operation, error, secrets),
|
|
41
|
+
async send(context) {
|
|
42
|
+
const { input, chat, connection, owner, origin, environment, fetch, token, secrets } = context;
|
|
43
|
+
const failed = context.failed;
|
|
44
|
+
const slack = proxiedSlackFetch(() => connectionProxy({
|
|
45
|
+
environment: proxyEnvironment(environment),
|
|
46
|
+
connection,
|
|
130
47
|
token,
|
|
131
|
-
fetch
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
48
|
+
fetch
|
|
49
|
+
}), fetch);
|
|
50
|
+
const call = async (method, init) => {
|
|
51
|
+
const response = await slack(`${SLACK_API}${method}`, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
...init
|
|
54
|
+
});
|
|
55
|
+
const answer = await response.json().catch(() => void 0);
|
|
56
|
+
if (!response.ok || !isRecord(answer) || answer.ok !== true) {
|
|
57
|
+
const code = isRecord(answer) && typeof answer.error === "string" ? answer.error : "";
|
|
58
|
+
throw new AttachmentError({
|
|
59
|
+
code: code === "missing_scope" || code === "not_in_channel" ? "forbidden" : "send_failed",
|
|
60
|
+
message: `Slack refused ${method}${code === "" ? "" : `: ${code}`}.`,
|
|
61
|
+
status: response.status
|
|
62
|
+
});
|
|
138
63
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
64
|
+
return answer;
|
|
65
|
+
};
|
|
66
|
+
const results = input.documentIds.map(({ id }) => ({
|
|
67
|
+
id,
|
|
68
|
+
filename: ""
|
|
69
|
+
}));
|
|
70
|
+
const uploaded = [];
|
|
71
|
+
for (const [index, reference] of input.documentIds.entries()) {
|
|
72
|
+
const fail = (error, operation) => {
|
|
73
|
+
const stored = error instanceof StoredFileError ? error : void 0;
|
|
74
|
+
results[index] = {
|
|
75
|
+
id: reference.id,
|
|
76
|
+
filename: stored?.filename ?? results[index].filename,
|
|
77
|
+
...failed(error, operation)
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
let file;
|
|
81
|
+
try {
|
|
82
|
+
file = await fetchStoredFile({
|
|
83
|
+
reference,
|
|
84
|
+
owner,
|
|
85
|
+
origin,
|
|
86
|
+
token,
|
|
87
|
+
fetch,
|
|
88
|
+
secrets,
|
|
89
|
+
tooLarge: () => refused("too_large", PROXY_LIMIT),
|
|
90
|
+
limit: ({ size }) => {
|
|
91
|
+
if (size > 2097152) throw refused("too_large", PROXY_LIMIT);
|
|
92
|
+
if (size === 0) throw refused("unsupported", "Slack cannot share an empty file.");
|
|
93
|
+
return MAXIMUM_PROXY_BODY_BYTES;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
} catch (error) {
|
|
97
|
+
fail(error, "stored_attachment_fetch_failed");
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
results[index] = {
|
|
101
|
+
id: reference.id,
|
|
102
|
+
filename: file.filename
|
|
103
|
+
};
|
|
104
|
+
try {
|
|
105
|
+
const ticket = await call("files.getUploadURLExternal", {
|
|
106
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
107
|
+
body: new URLSearchParams({
|
|
108
|
+
filename: file.filename,
|
|
109
|
+
length: String(file.bytes.byteLength)
|
|
110
|
+
}).toString()
|
|
111
|
+
});
|
|
112
|
+
if (typeof ticket.upload_url !== "string" || typeof ticket.file_id !== "string") throw refused("invalid_response", "Slack answered no upload address.");
|
|
113
|
+
secrets.add(ticket.upload_url);
|
|
114
|
+
const upload = await slack(ticket.upload_url, {
|
|
115
|
+
method: "POST",
|
|
116
|
+
headers: { "content-type": file.contentType },
|
|
117
|
+
body: Buffer.from(file.bytes)
|
|
118
|
+
});
|
|
119
|
+
await upload.body?.cancel();
|
|
120
|
+
if (!upload.ok) throw new AttachmentError({
|
|
121
|
+
code: "send_failed",
|
|
122
|
+
message: "Slack refused the file's bytes.",
|
|
123
|
+
status: upload.status
|
|
124
|
+
});
|
|
125
|
+
uploaded.push({
|
|
126
|
+
index,
|
|
127
|
+
slackId: ticket.file_id
|
|
128
|
+
});
|
|
129
|
+
} catch (error) {
|
|
130
|
+
fail(error, "attachment_upload_failed");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (uploaded.length === 0) return { files: results };
|
|
134
|
+
try {
|
|
135
|
+
const shared = await call("files.completeUploadExternal", {
|
|
136
|
+
headers: { "content-type": "application/json; charset=utf-8" },
|
|
137
|
+
body: JSON.stringify({
|
|
138
|
+
files: uploaded.map(({ index, slackId }) => ({
|
|
139
|
+
id: slackId,
|
|
140
|
+
title: results[index].filename
|
|
141
|
+
})),
|
|
142
|
+
channel_id: chat.chatId,
|
|
143
|
+
...chat.threadId === "" ? {} : { thread_ts: chat.threadId },
|
|
144
|
+
...input.caption === void 0 ? {} : { initial_comment: input.caption }
|
|
145
|
+
})
|
|
146
|
+
});
|
|
147
|
+
if (!Array.isArray(shared.files)) throw refused("invalid_response", "Slack did not answer the shared files.");
|
|
148
|
+
for (const { index, slackId } of uploaded) results[index] = {
|
|
149
|
+
...results[index],
|
|
150
|
+
slackFileId: slackId
|
|
151
|
+
};
|
|
152
|
+
} catch (error) {
|
|
153
|
+
const failure = failed(error, "attachment_share_failed");
|
|
154
|
+
for (const { index } of uploaded) results[index] = {
|
|
155
|
+
...results[index],
|
|
156
|
+
error: failure.error,
|
|
157
|
+
detail: failure.detail,
|
|
158
|
+
reference: failure.reference
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return { files: results };
|
|
175
162
|
}
|
|
176
|
-
}
|
|
177
|
-
if (uploaded.length === 0) return { files: results };
|
|
178
|
-
try {
|
|
179
|
-
const shared = await call("files.completeUploadExternal", {
|
|
180
|
-
headers: { "content-type": "application/json; charset=utf-8" },
|
|
181
|
-
body: JSON.stringify({
|
|
182
|
-
files: uploaded.map(({ index, slackId }) => ({
|
|
183
|
-
id: slackId,
|
|
184
|
-
title: results[index].filename
|
|
185
|
-
})),
|
|
186
|
-
channel_id: chat.chatId,
|
|
187
|
-
...chat.threadId === "" ? {} : { thread_ts: chat.threadId },
|
|
188
|
-
...options.input.caption === void 0 ? {} : { initial_comment: options.input.caption }
|
|
189
|
-
})
|
|
190
|
-
});
|
|
191
|
-
if (!Array.isArray(shared.files)) throw refused("invalid_response", "Slack did not answer the shared files.");
|
|
192
|
-
for (const { index, slackId } of uploaded) results[index] = {
|
|
193
|
-
...results[index],
|
|
194
|
-
slackFileId: slackId
|
|
195
|
-
};
|
|
196
|
-
} catch (error) {
|
|
197
|
-
const reference = reportSlackError("attachment_share_failed", error, secrets);
|
|
198
|
-
for (const { index } of uploaded) results[index] = {
|
|
199
|
-
...results[index],
|
|
200
|
-
error: error instanceof AttachmentError ? error.code : "send_failed",
|
|
201
|
-
detail: failureText(reference, "attachment", locale),
|
|
202
|
-
reference
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
return { files: results };
|
|
163
|
+
});
|
|
206
164
|
}
|
|
207
165
|
export { sendSlackAttachments };
|
|
@@ -10,7 +10,7 @@ import { groupRoutingHealth } from "../group-routing/health.mjs";
|
|
|
10
10
|
import { dispatchStructuredMessage, structuredMessage } from "./structured.mjs";
|
|
11
11
|
import { handleWorkspaceCallback, isWorkspaceCallback } from "./callbacks.mjs";
|
|
12
12
|
import { telegramText, withTelegramTexts } from "./texts.mjs";
|
|
13
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
13
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-Bc9qfC4D.mjs";
|
|
14
14
|
import { attachmentLifecycle } from "./lifecycle.mjs";
|
|
15
15
|
import { manifestOf } from "../../config.mjs";
|
|
16
16
|
import { gatewayAttachmentDescriber } from "./attachment-description.mjs";
|
|
@@ -6,9 +6,9 @@ __eveDirname(__filename);
|
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import { attachmentOwner } from "../attachment-tools.mjs";
|
|
8
8
|
import { Ct as failureText, Tt as reportTelegramError, _ as AttachmentError, wt as formattingRefused, xt as customerMessage } from "../../../_chunks/principal-JoaOmdFL.mjs";
|
|
9
|
+
import { attachmentFailure } from "../attachments-send.mjs";
|
|
9
10
|
import { MAXIMUM_PROXY_BODY_BYTES, connectionProxy } from "../connection-proxy.mjs";
|
|
10
11
|
import { redactDiagnostic } from "../diagnostics.mjs";
|
|
11
|
-
import { transientFileStatus } from "../file-grant.mjs";
|
|
12
12
|
import { StoredFileError, fetchStoredFile } from "../sent-files.mjs";
|
|
13
13
|
import { appOrigin, channelsEnabled, proxyEnvironment } from "../environment.mjs";
|
|
14
14
|
import { runtimeToken } from "../runtime-token.mjs";
|
|
@@ -144,18 +144,15 @@ async function sendAttachmentsInOrder(options) {
|
|
|
144
144
|
});
|
|
145
145
|
} catch (error) {
|
|
146
146
|
const errorId = reportTelegramError("stored_attachment_fetch_failed", error, { id: reference.id }, secrets);
|
|
147
|
-
const failed = error instanceof StoredFileError ? error : void 0;
|
|
148
147
|
results[index] = {
|
|
149
148
|
id: reference.id,
|
|
150
|
-
filename:
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
} : {},
|
|
158
|
-
...failed?.requestId === void 0 ? {} : { requestId: failed.requestId }
|
|
149
|
+
filename: error instanceof StoredFileError ? error.filename : "",
|
|
150
|
+
...attachmentFailure({
|
|
151
|
+
error,
|
|
152
|
+
reference: errorId,
|
|
153
|
+
locale,
|
|
154
|
+
fallback: "unavailable"
|
|
155
|
+
})
|
|
159
156
|
};
|
|
160
157
|
}
|
|
161
158
|
function multipart(files, caption) {
|
|
@@ -4,7 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
7
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-Bc9qfC4D.mjs";
|
|
8
8
|
import { runtimeRequestFailure } from "./runtime-request.mjs";
|
|
9
9
|
import { GET } from "eve/channels";
|
|
10
10
|
const HEALTH_ROUTE = "/eve/v1/telegram/health";
|
|
@@ -5,7 +5,7 @@ const __filename = __eveFileURLToPath(import.meta.url);
|
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import { At as principalRuntimeSource, K as member, Mt as telegramRuntimeSource, N as ontologyTools, O as OntologyActionNotPermitted, c as currentInboundRequest, j as actionToolName, jt as runtimeEnvelope, z as needsConfirmation } from "../../../_chunks/principal-JoaOmdFL.mjs";
|
|
8
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
8
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-Bc9qfC4D.mjs";
|
|
9
9
|
import { object } from "../confirmation-description.mjs";
|
|
10
10
|
var AttachmentLifecycleNotPermitted = class extends Error {
|
|
11
11
|
code = "attachment_lifecycle_not_permitted";
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { type WebAuthOptions } from "./auth.js";
|
|
2
|
+
/**
|
|
3
|
+
* The response headers a browser on another origin must read: a refusal's `Retry-After`, and the
|
|
4
|
+
* session and stream headers the chat client reads from every stream response, without which it
|
|
5
|
+
* stops at the first reply. The runtime fixes their names; it exports no constants for them.
|
|
6
|
+
*/
|
|
7
|
+
export declare const WEB_EXPOSED_HEADERS: readonly ["retry-after", "x-eve-session-id", "x-eve-stream-format", "x-eve-stream-tail-index", "x-eve-stream-version"];
|
|
2
8
|
/**
|
|
3
9
|
* Browsers on any origin may call the agent's routes: every request a browser makes carries an
|
|
4
10
|
* agent token as its bearer, never a cookie, and a website's visitors get theirs only from the
|
|
5
|
-
* ontology, on the websites its `guest` or `signUp` names.
|
|
11
|
+
* ontology, on the websites its `guest` or `signUp` names. {@link WEB_EXPOSED_HEADERS} stay
|
|
12
|
+
* readable.
|
|
6
13
|
*/
|
|
7
14
|
export declare const WEB_CORS: {
|
|
8
|
-
|
|
9
|
-
|
|
15
|
+
origin: string;
|
|
16
|
+
exposeHeaders: ("retry-after" | "x-eve-session-id" | "x-eve-stream-format" | "x-eve-stream-tail-index" | "x-eve-stream-version")[];
|
|
10
17
|
};
|
|
11
18
|
/**
|
|
12
19
|
* The agent's own HTTP channel, which browsers talk to: a member, or a guest of a declared website,
|
|
@@ -7,9 +7,16 @@ __eveCreateRequire(import.meta.url);
|
|
|
7
7
|
import { at as WEB_PROVIDER, lt as webAuth } from "../../../_chunks/principal-JoaOmdFL.mjs";
|
|
8
8
|
import { localDev, placeholderAuth, vercelOidc } from "eve/channels/auth";
|
|
9
9
|
import { eveChannel } from "eve/channels/eve";
|
|
10
|
+
const WEB_EXPOSED_HEADERS = [
|
|
11
|
+
"retry-after",
|
|
12
|
+
"x-eve-session-id",
|
|
13
|
+
"x-eve-stream-format",
|
|
14
|
+
"x-eve-stream-tail-index",
|
|
15
|
+
"x-eve-stream-version"
|
|
16
|
+
];
|
|
10
17
|
const WEB_CORS = {
|
|
11
18
|
origin: "*",
|
|
12
|
-
exposeHeaders: [
|
|
19
|
+
exposeHeaders: [...WEB_EXPOSED_HEADERS]
|
|
13
20
|
};
|
|
14
21
|
function webChannel(options = {}) {
|
|
15
22
|
return eveChannel({
|
|
@@ -22,4 +29,4 @@ function webChannel(options = {}) {
|
|
|
22
29
|
cors: WEB_CORS
|
|
23
30
|
});
|
|
24
31
|
}
|
|
25
|
-
export { WEB_CORS, WEB_PROVIDER, webAuth, webChannel };
|
|
32
|
+
export { WEB_CORS, WEB_EXPOSED_HEADERS, WEB_PROVIDER, webAuth, webChannel };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { SessionAuthContext } from "eve/context";
|
|
2
|
+
import { type AttachmentSendFailure, type AttachmentSendRefusal } from "../attachments-send.js";
|
|
3
|
+
import { type Environment } from "../environment.js";
|
|
4
|
+
import type { RuntimeToken } from "../runtime-token.js";
|
|
5
|
+
import type { SendAttachmentsInput } from "../telegram/attachments-send.js";
|
|
6
|
+
/**
|
|
7
|
+
* `attachments_send` in a WhatsApp chat: each stored file is a message to the person, sent through
|
|
8
|
+
* the WhatsApp channel's connection by the Chat SDK adapter the channel replies with, the caption
|
|
9
|
+
* on the first. The connection admits no media uploads, so WhatsApp fetches each file by the
|
|
10
|
+
* ontology's download grant, a link valid for about a minute: JPEG and PNG images go as images,
|
|
11
|
+
* other files as WhatsApp's types for them, within WhatsApp's size for each. WhatsApp reports a
|
|
12
|
+
* file it could not fetch later, in the message's status, which the channel does not read. The
|
|
13
|
+
* checks and failures are every channel's (`../attachments-send.ts`).
|
|
14
|
+
*/
|
|
15
|
+
/** What became of one requested file. */
|
|
16
|
+
export interface WhatsAppSentFile extends Partial<AttachmentSendFailure> {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly filename: string;
|
|
19
|
+
/** WhatsApp's id of the message that carries it. */
|
|
20
|
+
readonly whatsappMessageId?: string;
|
|
21
|
+
}
|
|
22
|
+
type WhatsAppSendResult = {
|
|
23
|
+
readonly files: ReadonlyArray<WhatsAppSentFile>;
|
|
24
|
+
} | AttachmentSendRefusal;
|
|
25
|
+
export declare function sendWhatsAppAttachments(options: {
|
|
26
|
+
readonly input: SendAttachmentsInput;
|
|
27
|
+
readonly principal: SessionAuthContext | null;
|
|
28
|
+
readonly sessionId: string;
|
|
29
|
+
/** The workspace's `channels.whatsapp.connection`. */
|
|
30
|
+
readonly connection: string | undefined;
|
|
31
|
+
readonly environment?: Environment;
|
|
32
|
+
readonly token?: RuntimeToken;
|
|
33
|
+
readonly fetch?: typeof fetch;
|
|
34
|
+
}): Promise<WhatsAppSendResult>;
|
|
35
|
+
export {};
|