@segmentstream/agent 0.100.0 → 0.102.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 +43 -0
- package/dist/_chunks/{build-metadata-fPSw1vHg.mjs → build-metadata-BtB38zQ0.mjs} +1 -1
- package/dist/extension/lib/build-metadata.mjs +1 -1
- package/dist/extension/lib/connection-proxy.d.ts +12 -1
- package/dist/extension/lib/connection-proxy.mjs +24 -3
- package/dist/extension/lib/email/channel.d.ts +2 -0
- package/dist/extension/lib/email/channel.mjs +45 -11
- package/dist/extension/lib/review/client.d.ts +75 -0
- package/dist/extension/lib/review/client.mjs +103 -1
- package/dist/extension/lib/review/hold.d.ts +24 -0
- package/dist/extension/lib/review/hold.mjs +31 -1
- package/dist/extension/lib/review/reply.d.ts +54 -1
- package/dist/extension/lib/review/reply.mjs +150 -78
- package/dist/extension/lib/sent-files.d.ts +24 -8
- package/dist/extension/lib/sent-files.mjs +39 -2
- package/dist/extension/lib/slack/attachments-send.d.ts +5 -4
- package/dist/extension/lib/slack/attachments-send.mjs +24 -24
- package/dist/extension/lib/telegram/adapter.mjs +1 -1
- package/dist/extension/lib/telegram/attachments-send.mjs +36 -31
- package/dist/extension/lib/telegram/health.mjs +1 -1
- package/dist/extension/lib/telegram/lifecycle.mjs +1 -1
- package/dist/extension/lib/whatsapp/attachments-send.d.ts +12 -0
- package/dist/extension/lib/whatsapp/attachments-send.mjs +10 -17
- package/dist/extension/lib/workspace-channel/channel.d.ts +6 -6
- package/dist/extension/lib/workspace-channel/channel.mjs +14 -20
- package/dist/extension/review.d.ts +20 -23
- package/dist/extension/review.mjs +14 -15
- package/package.json +2 -2
|
@@ -4,13 +4,14 @@ 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 { reviewPolicy } from "../../review.mjs";
|
|
7
8
|
import { Ft as chatAddressOf, It as chatChannel, i as memberIdOf } from "../../../_chunks/principal-DL9NdRRP.mjs";
|
|
8
9
|
import { a as chatEvent, r as chatArchive } from "../../../_chunks/chat-archive-_IBU0CXG.mjs";
|
|
9
10
|
import { stampedChat } from "../chat-recorder.mjs";
|
|
10
11
|
import { appOrigin } from "../environment.mjs";
|
|
11
|
-
import { agentReviews } from "./client.mjs";
|
|
12
|
+
import { agentReplies, agentReviews } from "./client.mjs";
|
|
12
13
|
import { _ as withCaptions, h as partFiles, m as filesAfterReply, n as completionKind, p as fileGroups } from "../../../_chunks/silent-xJcJ8SNJ.mjs";
|
|
13
|
-
import { heldReply, repliesReviewed, reviewOf, reviewOutcome } from "./hold.mjs";
|
|
14
|
+
import { heldReply, proposedReply, repliesReviewed, reviewOf, reviewOutcome } from "./hold.mjs";
|
|
14
15
|
import { judgeReply, readChat } from "./judge.mjs";
|
|
15
16
|
const VERDICTS_REMEMBERED = 512;
|
|
16
17
|
function reviewedChat(principal) {
|
|
@@ -39,8 +40,150 @@ function replyReview(options) {
|
|
|
39
40
|
}
|
|
40
41
|
return outcome;
|
|
41
42
|
};
|
|
43
|
+
const recorded = (principal) => ({
|
|
44
|
+
coordinates: reviewedChat(principal),
|
|
45
|
+
memberId: memberIdOf(principal)
|
|
46
|
+
});
|
|
47
|
+
const record = async (input, reason, summary) => {
|
|
48
|
+
const { coordinates, memberId } = recorded(input.principal);
|
|
49
|
+
if (coordinates === void 0) return;
|
|
50
|
+
await options.archive.append(chatEvent({
|
|
51
|
+
...coordinates,
|
|
52
|
+
eventId: `review:${input.sessionId}:${input.partId}`,
|
|
53
|
+
participantIds: memberId === void 0 ? [] : [memberId],
|
|
54
|
+
sessionId: input.sessionId,
|
|
55
|
+
turnId: input.turnId,
|
|
56
|
+
kind: "decision",
|
|
57
|
+
decision: reason === "review_passed" ? "reply" : "silent",
|
|
58
|
+
reason,
|
|
59
|
+
argsSummary: JSON.stringify(summary)
|
|
60
|
+
}));
|
|
61
|
+
};
|
|
62
|
+
const verdictOf = async (input, review, text) => {
|
|
63
|
+
const key = `${input.sessionId}:${input.partId}`;
|
|
64
|
+
const files = input.files?.files ?? [];
|
|
65
|
+
const coordinates = reviewedChat(input.principal);
|
|
66
|
+
return verdicts.get(key) ?? remember(key, await reviewOutcome(review, async (criteria) => judgeReply({
|
|
67
|
+
decide: options.decide,
|
|
68
|
+
model: options.model(),
|
|
69
|
+
chat: coordinates === void 0 ? [] : await readChat(options.archive, coordinates),
|
|
70
|
+
reply: text,
|
|
71
|
+
...files.length === 0 ? {} : { files: files.map((file) => file.filename) },
|
|
72
|
+
...criteria === void 0 ? {} : { criteria }
|
|
73
|
+
}), (error) => console.error("[segmentstream] reply review judge failed; the reply is held", {
|
|
74
|
+
sessionId: input.sessionId,
|
|
75
|
+
error
|
|
76
|
+
})));
|
|
77
|
+
};
|
|
78
|
+
const sendOrHold = async (input) => {
|
|
79
|
+
const review = reviewOf(options.settings(), input.principal);
|
|
80
|
+
if (!repliesReviewed(options.settings(), input.principal) || review.mode === "never") return true;
|
|
81
|
+
const files = input.files?.files ?? [];
|
|
82
|
+
const text = withCaptions(input.text, input.files?.captions ?? []);
|
|
83
|
+
const outcome = await verdictOf(input, review, text);
|
|
84
|
+
const captions = (input.files?.captions ?? []).join("\n\n");
|
|
85
|
+
const verdict = outcome.send && files.length > 0 && (options.sendFiles === void 0 || captions.length > 1024) ? {
|
|
86
|
+
send: false,
|
|
87
|
+
mode: review.mode === "always" ? "always" : "whenUnsure",
|
|
88
|
+
confidence: outcome.confidence ?? null
|
|
89
|
+
} : outcome;
|
|
90
|
+
if (verdict.send) {
|
|
91
|
+
await record(input, "review_passed", {
|
|
92
|
+
mode: review.mode,
|
|
93
|
+
confidence: outcome.confidence ?? null,
|
|
94
|
+
...review.mode === "whenUnsure" ? { minConfidence: review.minConfidence } : {}
|
|
95
|
+
});
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
const [first, ...rest] = fileGroups({
|
|
99
|
+
files,
|
|
100
|
+
captions: []
|
|
101
|
+
});
|
|
102
|
+
const held = heldReply({
|
|
103
|
+
...input,
|
|
104
|
+
text,
|
|
105
|
+
files: first?.files ?? [],
|
|
106
|
+
outcome: verdict
|
|
107
|
+
});
|
|
108
|
+
if (held === void 0) throw new Error("A reply under review has no chat or member to be held for.");
|
|
109
|
+
await options.hold(held);
|
|
110
|
+
for (const [index, group] of rest.entries()) {
|
|
111
|
+
const more = heldReply({
|
|
112
|
+
...input,
|
|
113
|
+
partId: `${input.partId}:files:${index + 1}`,
|
|
114
|
+
text: "",
|
|
115
|
+
files: group.files,
|
|
116
|
+
outcome: verdict
|
|
117
|
+
});
|
|
118
|
+
if (more !== void 0) await options.hold(more);
|
|
119
|
+
}
|
|
120
|
+
await record(input, "review_held", {
|
|
121
|
+
mode: verdict.mode,
|
|
122
|
+
confidence: verdict.confidence,
|
|
123
|
+
...review.mode === "whenUnsure" ? { minConfidence: review.minConfidence } : {}
|
|
124
|
+
});
|
|
125
|
+
return false;
|
|
126
|
+
};
|
|
42
127
|
return {
|
|
43
128
|
reviewed: (principal) => repliesReviewed(options.settings(), principal),
|
|
129
|
+
proposes: (principal) => {
|
|
130
|
+
const provider = chatAddressOf(principal)?.provider;
|
|
131
|
+
return options.propose !== void 0 && options.settings() !== void 0 && (provider === "email" || provider === "custom") && memberIdOf(principal) !== void 0;
|
|
132
|
+
},
|
|
133
|
+
async propose(input) {
|
|
134
|
+
const settings = options.settings();
|
|
135
|
+
const propose = options.propose;
|
|
136
|
+
if (settings === void 0 || propose === void 0) return {
|
|
137
|
+
by: "agent",
|
|
138
|
+
send: await sendOrHold(input)
|
|
139
|
+
};
|
|
140
|
+
const review = reviewOf(settings, input.principal);
|
|
141
|
+
const files = input.files?.files ?? [];
|
|
142
|
+
const captions = (input.files?.captions ?? []).map((caption) => caption.trim()).filter((caption) => caption !== "");
|
|
143
|
+
const confidence = review.mode === "whenUnsure" ? (await verdictOf(input, review, withCaptions(input.text, captions))).confidence ?? null : null;
|
|
144
|
+
const policy = reviewPolicy(settings);
|
|
145
|
+
const [first, ...rest] = fileGroups({
|
|
146
|
+
files,
|
|
147
|
+
captions: []
|
|
148
|
+
});
|
|
149
|
+
const proposal = (partId, text, group) => proposedReply({
|
|
150
|
+
principal: input.principal,
|
|
151
|
+
sessionId: input.sessionId,
|
|
152
|
+
partId,
|
|
153
|
+
turnId: input.turnId,
|
|
154
|
+
text,
|
|
155
|
+
...group !== void 0 && group === first && captions.length > 0 ? { caption: captions.join("\n\n") } : {},
|
|
156
|
+
...group === void 0 ? {} : { files: group.files },
|
|
157
|
+
...input.recipients === void 0 ? {} : { recipients: input.recipients },
|
|
158
|
+
review: policy,
|
|
159
|
+
confidence,
|
|
160
|
+
...input.maxLength === void 0 ? {} : { maxLength: input.maxLength }
|
|
161
|
+
});
|
|
162
|
+
const main = proposal(input.partId, input.text, first);
|
|
163
|
+
if (main === void 0) throw new Error("A reply under review has no chat or member to be proposed for.");
|
|
164
|
+
const answer = await propose(main);
|
|
165
|
+
if (answer.outcome === "unsupported") return {
|
|
166
|
+
by: "agent",
|
|
167
|
+
send: await sendOrHold(input)
|
|
168
|
+
};
|
|
169
|
+
await record(input, answer.outcome === "held" ? "review_held" : "review_passed", {
|
|
170
|
+
mode: answer.review.mode,
|
|
171
|
+
confidence,
|
|
172
|
+
...answer.review.mode === "whenUnsure" ? { minConfidence: answer.review.minConfidence } : {}
|
|
173
|
+
});
|
|
174
|
+
if (answer.outcome === "sent" || answer.outcome === "held") for (const [index, group] of rest.entries()) {
|
|
175
|
+
const more = proposal(`${input.partId}:files:${index + 1}`, "", group);
|
|
176
|
+
const outcome = more === void 0 ? void 0 : await propose(more);
|
|
177
|
+
if (outcome !== void 0 && outcome.outcome !== "sent" && outcome.outcome !== "held") console.error("[segmentstream] a reply's further files were neither sent nor held", {
|
|
178
|
+
sessionId: input.sessionId,
|
|
179
|
+
outcome: outcome.outcome
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
...answer,
|
|
184
|
+
by: "ontology"
|
|
185
|
+
};
|
|
186
|
+
},
|
|
44
187
|
async sendFiles(input) {
|
|
45
188
|
for (const [index, files] of fileGroups(input.files).entries()) await options.sendFiles({
|
|
46
189
|
...input,
|
|
@@ -70,82 +213,7 @@ function replyReview(options) {
|
|
|
70
213
|
await options.hold(held);
|
|
71
214
|
}
|
|
72
215
|
},
|
|
73
|
-
|
|
74
|
-
const review = reviewOf(options.settings(), input.principal);
|
|
75
|
-
if (!repliesReviewed(options.settings(), input.principal) || review.mode === "never") return true;
|
|
76
|
-
const files = input.files?.files ?? [];
|
|
77
|
-
const text = withCaptions(input.text, input.files?.captions ?? []);
|
|
78
|
-
const coordinates = reviewedChat(input.principal);
|
|
79
|
-
const key = `${input.sessionId}:${input.partId}`;
|
|
80
|
-
const outcome = verdicts.get(key) ?? remember(key, await reviewOutcome(review, async (criteria) => judgeReply({
|
|
81
|
-
decide: options.decide,
|
|
82
|
-
model: options.model(),
|
|
83
|
-
chat: coordinates === void 0 ? [] : await readChat(options.archive, coordinates),
|
|
84
|
-
reply: text,
|
|
85
|
-
...files.length === 0 ? {} : { files: files.map((file) => file.filename) },
|
|
86
|
-
...criteria === void 0 ? {} : { criteria }
|
|
87
|
-
}), (error) => console.error("[segmentstream] reply review judge failed; the reply is held", {
|
|
88
|
-
sessionId: input.sessionId,
|
|
89
|
-
error
|
|
90
|
-
})));
|
|
91
|
-
const record = async (reason, summary) => {
|
|
92
|
-
if (coordinates === void 0) return;
|
|
93
|
-
const memberId = memberIdOf(input.principal);
|
|
94
|
-
await options.archive.append(chatEvent({
|
|
95
|
-
...coordinates,
|
|
96
|
-
eventId: `review:${input.sessionId}:${input.partId}`,
|
|
97
|
-
participantIds: memberId === void 0 ? [] : [memberId],
|
|
98
|
-
sessionId: input.sessionId,
|
|
99
|
-
turnId: input.turnId,
|
|
100
|
-
kind: "decision",
|
|
101
|
-
decision: reason === "review_passed" ? "reply" : "silent",
|
|
102
|
-
reason,
|
|
103
|
-
argsSummary: JSON.stringify(summary)
|
|
104
|
-
}));
|
|
105
|
-
};
|
|
106
|
-
const captions = (input.files?.captions ?? []).join("\n\n");
|
|
107
|
-
const verdict = outcome.send && files.length > 0 && (options.sendFiles === void 0 || captions.length > 1024) ? {
|
|
108
|
-
send: false,
|
|
109
|
-
mode: review.mode === "always" ? "always" : "whenUnsure",
|
|
110
|
-
confidence: outcome.confidence ?? null
|
|
111
|
-
} : outcome;
|
|
112
|
-
if (verdict.send) {
|
|
113
|
-
await record("review_passed", {
|
|
114
|
-
mode: review.mode,
|
|
115
|
-
confidence: outcome.confidence ?? null,
|
|
116
|
-
...review.mode === "whenUnsure" ? { minConfidence: review.minConfidence } : {}
|
|
117
|
-
});
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
const [first, ...rest] = fileGroups({
|
|
121
|
-
files,
|
|
122
|
-
captions: []
|
|
123
|
-
});
|
|
124
|
-
const held = heldReply({
|
|
125
|
-
...input,
|
|
126
|
-
text,
|
|
127
|
-
files: first?.files ?? [],
|
|
128
|
-
outcome: verdict
|
|
129
|
-
});
|
|
130
|
-
if (held === void 0) throw new Error("A reply under review has no chat or member to be held for.");
|
|
131
|
-
await options.hold(held);
|
|
132
|
-
for (const [index, group] of rest.entries()) {
|
|
133
|
-
const more = heldReply({
|
|
134
|
-
...input,
|
|
135
|
-
partId: `${input.partId}:files:${index + 1}`,
|
|
136
|
-
text: "",
|
|
137
|
-
files: group.files,
|
|
138
|
-
outcome: verdict
|
|
139
|
-
});
|
|
140
|
-
if (more !== void 0) await options.hold(more);
|
|
141
|
-
}
|
|
142
|
-
await record("review_held", {
|
|
143
|
-
mode: verdict.mode,
|
|
144
|
-
confidence: verdict.confidence,
|
|
145
|
-
...review.mode === "whenUnsure" ? { minConfidence: review.minConfidence } : {}
|
|
146
|
-
});
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
216
|
+
sendOrHold
|
|
149
217
|
};
|
|
150
218
|
}
|
|
151
219
|
function withReviewedReplies(channel, review, archive) {
|
|
@@ -240,6 +308,10 @@ function channelReview(options, runtime) {
|
|
|
240
308
|
hold: options.hold ?? ((reply) => agentReviews({
|
|
241
309
|
origin: appOrigin(runtime.environment),
|
|
242
310
|
...transport
|
|
311
|
+
})(reply)),
|
|
312
|
+
propose: options.propose ?? ((reply) => agentReplies({
|
|
313
|
+
origin: appOrigin(runtime.environment),
|
|
314
|
+
...transport
|
|
243
315
|
})(reply))
|
|
244
316
|
})
|
|
245
317
|
};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import type { ChannelStoredFile } from "@segmentstream/ontology/telegram";
|
|
1
2
|
import { AttachmentError } from "./attachments.js";
|
|
2
3
|
import type { AttachmentOwner } from "./attachments.js";
|
|
3
4
|
/**
|
|
4
5
|
* A stored ontology file the agent sends into a chat: its record, the ontology's file grant and
|
|
5
6
|
* 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.
|
|
7
|
-
*
|
|
7
|
+
* it; each says how many bytes its destination takes. A file sent by link, as WhatsApp fetches
|
|
8
|
+
* it, or by its signed read, which the connection proxy or a provider's upload follows, is read
|
|
9
|
+
* only by its grant.
|
|
8
10
|
*/
|
|
9
11
|
/** A file record: a document by its id, or any entity's record and file property. */
|
|
10
12
|
export interface DocumentReference {
|
|
@@ -45,7 +47,7 @@ export interface StoredFileGrant {
|
|
|
45
47
|
readonly headers: Readonly<Record<string, string>>;
|
|
46
48
|
}
|
|
47
49
|
/** What reading a stored file needs: the member, the chat and the deployment's credentials. */
|
|
48
|
-
interface StoredFileRead {
|
|
50
|
+
export interface StoredFileRead {
|
|
49
51
|
readonly reference: DocumentReference;
|
|
50
52
|
readonly owner: AttachmentOwner;
|
|
51
53
|
/** The deployment's own origin, where the ontology service answers. */
|
|
@@ -61,16 +63,30 @@ interface StoredFileRead {
|
|
|
61
63
|
*/
|
|
62
64
|
export declare function storedFileGrant(input: StoredFileRead): Promise<StoredFileGrant>;
|
|
63
65
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
66
|
+
* A file sent by its signed read, which the connection proxy or a provider's upload follows just
|
|
67
|
+
* before its request: the grant read first, renewed once it is older than a few seconds, so its
|
|
68
|
+
* link is still valid however long the files before it took. A renewal that names a file of
|
|
69
|
+
* another size or type, as when the record's file was replaced meanwhile, rejects.
|
|
67
70
|
*/
|
|
68
|
-
export declare function
|
|
71
|
+
export declare function storedChannelFile(grant: StoredFileGrant, input: StoredFileRead): ChannelStoredFile & {
|
|
72
|
+
readonly link: () => Promise<StoredFileGrant>;
|
|
73
|
+
};
|
|
74
|
+
/** What a stored file's download needs once its grant is read. */
|
|
75
|
+
interface StoredFileDownload {
|
|
76
|
+
readonly fetch: typeof fetch;
|
|
69
77
|
readonly limit: (file: {
|
|
70
78
|
readonly contentType: string;
|
|
71
79
|
readonly size: number;
|
|
72
80
|
}) => number;
|
|
73
81
|
/** The error a download over the limit throws. */
|
|
74
82
|
readonly tooLarge: () => Error;
|
|
75
|
-
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Reads one stored file: its grant from the ontology, then its bytes. `limit` sees the file's type
|
|
86
|
+
* and size before anything is downloaded and answers the most bytes the destination takes, or
|
|
87
|
+
* throws to refuse it.
|
|
88
|
+
*/
|
|
89
|
+
export declare function fetchStoredFile(input: StoredFileRead & Omit<StoredFileDownload, "fetch">): Promise<StoredFile>;
|
|
90
|
+
/** Reads the bytes of a stored file whose grant is read, as {@link fetchStoredFile} does. */
|
|
91
|
+
export declare function downloadStoredFile(grant: StoredFileGrant, input: StoredFileDownload): Promise<StoredFile>;
|
|
76
92
|
export {};
|
|
@@ -110,8 +110,45 @@ async function storedFileGrant(input) {
|
|
|
110
110
|
});
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
+
const FRESH_GRANT_MS = 15e3;
|
|
114
|
+
function storedChannelFile(grant, input) {
|
|
115
|
+
let current = {
|
|
116
|
+
grant,
|
|
117
|
+
at: Date.now()
|
|
118
|
+
};
|
|
119
|
+
const link = async () => {
|
|
120
|
+
if (Date.now() - current.at > FRESH_GRANT_MS) {
|
|
121
|
+
const renewed = await storedFileGrant(input);
|
|
122
|
+
if (renewed.size !== grant.size || renewed.contentType !== grant.contentType) throw new StoredFileError({
|
|
123
|
+
code: "unavailable",
|
|
124
|
+
message: "The stored file changed while it was being sent.",
|
|
125
|
+
filename: grant.filename
|
|
126
|
+
});
|
|
127
|
+
current = {
|
|
128
|
+
grant: renewed,
|
|
129
|
+
at: Date.now()
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
return current.grant;
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
filename: grant.filename,
|
|
136
|
+
contentType: grant.contentType,
|
|
137
|
+
size: grant.size,
|
|
138
|
+
link,
|
|
139
|
+
read: async () => {
|
|
140
|
+
const fresh = await link();
|
|
141
|
+
return {
|
|
142
|
+
url: fresh.url,
|
|
143
|
+
headers: Object.entries(fresh.headers)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
113
148
|
async function fetchStoredFile(input) {
|
|
114
|
-
|
|
149
|
+
return downloadStoredFile(await storedFileGrant(input), input);
|
|
150
|
+
}
|
|
151
|
+
async function downloadStoredFile(grant, input) {
|
|
115
152
|
const { filename, contentType } = grant;
|
|
116
153
|
try {
|
|
117
154
|
const maximum = input.limit({
|
|
@@ -145,4 +182,4 @@ async function fetchStoredFile(input) {
|
|
|
145
182
|
});
|
|
146
183
|
}
|
|
147
184
|
}
|
|
148
|
-
export { StoredFileError, boundedDownload, fetchStoredFile, storedFileGrant };
|
|
185
|
+
export { StoredFileError, boundedDownload, downloadStoredFile, fetchStoredFile, storedChannelFile, storedFileGrant };
|
|
@@ -8,10 +8,11 @@ import type { SendAttachmentsInput } from "../telegram/attachments-send.js";
|
|
|
8
8
|
* in its thread, with Slack's external upload: `files.getUploadURLExternal` for each file, its
|
|
9
9
|
* bytes posted to the address Slack answers, then one `files.completeUploadExternal` that shares
|
|
10
10
|
* them all in the chat with the caption, as the ontology's Slack sender (`sendSlackFiles`) does it.
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* The Web API calls go through the Slack connection, which adds the bot token and needs
|
|
12
|
+
* `files:write`. Each file's bytes go from the workspace's file storage straight to its upload
|
|
13
|
+
* address, which carries its own authorization and needs no credential, streamed, so a file is
|
|
14
|
+
* held to Slack's 1 GB rather than to the connection proxy's request body. The checks and failures
|
|
15
|
+
* are every channel's (`../attachments-send.ts`).
|
|
15
16
|
*/
|
|
16
17
|
/** What became of one requested file. */
|
|
17
18
|
export interface SlackSentFile extends Partial<AttachmentSendFailure> {
|
|
@@ -9,16 +9,9 @@ import { sendStoredAttachments } from "../attachments-send.mjs";
|
|
|
9
9
|
import { connectionProxy } from "../connection-proxy.mjs";
|
|
10
10
|
import { redactDiagnostic } from "../diagnostics.mjs";
|
|
11
11
|
import { proxyEnvironment } from "../environment.mjs";
|
|
12
|
-
import { StoredFileError,
|
|
12
|
+
import { StoredFileError, storedChannelFile, storedFileGrant } from "../sent-files.mjs";
|
|
13
13
|
import { randomUUID } from "node:crypto";
|
|
14
|
-
import {
|
|
15
|
-
const PROXY_LIMIT = "The connection proxy takes at most 2 MiB per upload.";
|
|
16
|
-
function refused(code, message) {
|
|
17
|
-
return new AttachmentError({
|
|
18
|
-
code,
|
|
19
|
-
message
|
|
20
|
-
});
|
|
21
|
-
}
|
|
14
|
+
import { sendSlackFiles, slackFileRefusal } from "@segmentstream/ontology/slack";
|
|
22
15
|
function reportSlackError(operation, error, secrets) {
|
|
23
16
|
const reference = randomUUID();
|
|
24
17
|
console.error("[segmentstream] Slack delivery diagnostic", {
|
|
@@ -49,26 +42,33 @@ async function sendSlackAttachments(options) {
|
|
|
49
42
|
}));
|
|
50
43
|
const read = [];
|
|
51
44
|
for (const [index, reference] of input.documentIds.entries()) try {
|
|
52
|
-
const
|
|
45
|
+
const grant = await storedFileGrant({
|
|
53
46
|
reference,
|
|
54
47
|
owner,
|
|
55
48
|
origin,
|
|
56
49
|
token,
|
|
57
50
|
fetch,
|
|
58
|
-
secrets
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
51
|
+
secrets
|
|
52
|
+
});
|
|
53
|
+
const refusal = slackFileRefusal(grant, { direct: true });
|
|
54
|
+
if (refusal !== void 0) throw new StoredFileError({
|
|
55
|
+
code: refusal.code === "empty" ? "unsupported" : refusal.code,
|
|
56
|
+
message: refusal.message,
|
|
57
|
+
filename: grant.filename
|
|
65
58
|
});
|
|
66
59
|
results[index] = {
|
|
67
60
|
id: reference.id,
|
|
68
|
-
filename:
|
|
61
|
+
filename: grant.filename
|
|
69
62
|
};
|
|
70
63
|
read.push({
|
|
71
|
-
...
|
|
64
|
+
...storedChannelFile(grant, {
|
|
65
|
+
reference,
|
|
66
|
+
owner,
|
|
67
|
+
origin,
|
|
68
|
+
token,
|
|
69
|
+
fetch,
|
|
70
|
+
secrets
|
|
71
|
+
}),
|
|
72
72
|
index
|
|
73
73
|
});
|
|
74
74
|
} catch (error) {
|
|
@@ -80,11 +80,11 @@ async function sendSlackAttachments(options) {
|
|
|
80
80
|
}
|
|
81
81
|
if (read.length === 0) return { files: results };
|
|
82
82
|
const sent = await sendSlackFiles({
|
|
83
|
-
request: (call) =>
|
|
84
|
-
|
|
85
|
-
secrets.add(
|
|
86
|
-
return
|
|
87
|
-
},
|
|
83
|
+
request: (call) => proxy.request(call),
|
|
84
|
+
fetch: ((address, init) => {
|
|
85
|
+
secrets.add(String(address));
|
|
86
|
+
return fetch(address, init);
|
|
87
|
+
}),
|
|
88
88
|
channel: chat.chatId,
|
|
89
89
|
...chat.threadId === "" ? {} : { threadTs: chat.threadId },
|
|
90
90
|
files: read,
|
|
@@ -13,7 +13,7 @@ import { dispatchStructuredMessage, structuredMessage } from "./structured.mjs";
|
|
|
13
13
|
import { handleWorkspaceCallback } from "./callbacks.mjs";
|
|
14
14
|
import { workspaceTriage } from "../triage.mjs";
|
|
15
15
|
import { telegramText, withTelegramTexts } from "./texts.mjs";
|
|
16
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
16
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BtB38zQ0.mjs";
|
|
17
17
|
import { attachmentLifecycle } from "./lifecycle.mjs";
|
|
18
18
|
import { manifestOf } from "../../config.mjs";
|
|
19
19
|
import { gatewayAttachmentDescriber } from "./attachment-description.mjs";
|
|
@@ -8,14 +8,14 @@ import { attachmentOwner } from "../attachment-tools.mjs";
|
|
|
8
8
|
import { _ as AttachmentError, _t as customerMessage, xt as reportTelegramError, yt as failureText } from "../../../_chunks/principal-DL9NdRRP.mjs";
|
|
9
9
|
import { attachmentFailure } from "../attachments-send.mjs";
|
|
10
10
|
import { connectionProxy } from "../connection-proxy.mjs";
|
|
11
|
-
import { StoredFileError,
|
|
11
|
+
import { StoredFileError, downloadStoredFile, storedChannelFile, storedFileGrant } from "../sent-files.mjs";
|
|
12
12
|
import { appOrigin, channelsEnabled, proxyEnvironment } from "../environment.mjs";
|
|
13
13
|
import { runtimeToken } from "../runtime-token.mjs";
|
|
14
14
|
import { confirmationLocale } from "./confirmation-text.mjs";
|
|
15
15
|
import { inTelegramSendQueue, telegramSendScope } from "./send-queue.mjs";
|
|
16
16
|
import { miniAppLinks, telegramAppLinkContext, withMiniAppLinks } from "./app-links.mjs";
|
|
17
17
|
import { proxiedBotRequest, runtimeBotIdentity } from "./bot.mjs";
|
|
18
|
-
import {
|
|
18
|
+
import { TELEGRAM_INLINE_FILE_BYTES, sendTelegramFiles, telegramFileRefusal } from "@segmentstream/ontology/telegram";
|
|
19
19
|
const RECORD_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
20
20
|
const NAME = /^[a-z][a-z0-9_]{0,63}$/;
|
|
21
21
|
function isRecord(value) {
|
|
@@ -49,7 +49,7 @@ function refused(code, message) {
|
|
|
49
49
|
message
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
|
-
const
|
|
52
|
+
const UNDERSTATED = "The stored file is larger than its record says.";
|
|
53
53
|
function sendAttachments(options) {
|
|
54
54
|
const chatId = options.principal?.attributes.chatId;
|
|
55
55
|
return typeof chatId === "string" && options.connection !== void 0 ? inTelegramSendQueue(telegramSendScope(options.environment ?? process.env, options.connection), chatId, () => sendAttachmentsInOrder(options)) : sendAttachmentsInOrder(options);
|
|
@@ -107,40 +107,45 @@ async function sendAttachmentsInOrder(options) {
|
|
|
107
107
|
filename: ""
|
|
108
108
|
}));
|
|
109
109
|
const uploads = [];
|
|
110
|
-
for (const [index, reference] of options.input.documentIds.entries())
|
|
111
|
-
const
|
|
110
|
+
for (const [index, reference] of options.input.documentIds.entries()) {
|
|
111
|
+
const read = {
|
|
112
112
|
reference,
|
|
113
113
|
owner,
|
|
114
114
|
origin,
|
|
115
115
|
token,
|
|
116
116
|
fetch: fetchImpl,
|
|
117
|
-
secrets
|
|
118
|
-
tooLarge: () => refused("too_large", PROXY_LIMIT),
|
|
119
|
-
limit: ({ contentType, size }) => {
|
|
120
|
-
const refusal = telegramFileRefusal({
|
|
121
|
-
contentType,
|
|
122
|
-
size
|
|
123
|
-
});
|
|
124
|
-
if (refusal !== void 0) throw refused(refusal.code === "empty" ? "unsupported" : refusal.code, refusal.message);
|
|
125
|
-
return telegramFileBytes(contentType);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
uploads.push({
|
|
129
|
-
index,
|
|
130
|
-
file
|
|
131
|
-
});
|
|
132
|
-
} catch (error) {
|
|
133
|
-
const errorId = reportTelegramError("stored_attachment_fetch_failed", error, { id: reference.id }, secrets);
|
|
134
|
-
results[index] = {
|
|
135
|
-
id: reference.id,
|
|
136
|
-
filename: error instanceof StoredFileError ? error.filename : "",
|
|
137
|
-
...attachmentFailure({
|
|
138
|
-
error,
|
|
139
|
-
reference: errorId,
|
|
140
|
-
locale,
|
|
141
|
-
fallback: "unavailable"
|
|
142
|
-
})
|
|
117
|
+
secrets
|
|
143
118
|
};
|
|
119
|
+
try {
|
|
120
|
+
const grant = await storedFileGrant(read);
|
|
121
|
+
const refusal = telegramFileRefusal(grant, { stored: true });
|
|
122
|
+
if (refusal !== void 0) throw new StoredFileError({
|
|
123
|
+
code: refusal.code === "empty" ? "unsupported" : refusal.code,
|
|
124
|
+
message: refusal.message,
|
|
125
|
+
filename: grant.filename
|
|
126
|
+
});
|
|
127
|
+
const file = grant.size <= TELEGRAM_INLINE_FILE_BYTES ? await downloadStoredFile(grant, {
|
|
128
|
+
fetch: fetchImpl,
|
|
129
|
+
limit: () => TELEGRAM_INLINE_FILE_BYTES,
|
|
130
|
+
tooLarge: () => refused("too_large", UNDERSTATED)
|
|
131
|
+
}) : storedChannelFile(grant, read);
|
|
132
|
+
uploads.push({
|
|
133
|
+
index,
|
|
134
|
+
file
|
|
135
|
+
});
|
|
136
|
+
} catch (error) {
|
|
137
|
+
const errorId = reportTelegramError("stored_attachment_fetch_failed", error, { id: reference.id }, secrets);
|
|
138
|
+
results[index] = {
|
|
139
|
+
id: reference.id,
|
|
140
|
+
filename: error instanceof StoredFileError ? error.filename : "",
|
|
141
|
+
...attachmentFailure({
|
|
142
|
+
error,
|
|
143
|
+
reference: errorId,
|
|
144
|
+
locale,
|
|
145
|
+
fallback: "unavailable"
|
|
146
|
+
})
|
|
147
|
+
};
|
|
148
|
+
}
|
|
144
149
|
}
|
|
145
150
|
let caption = options.input.caption === void 0 ? void 0 : customerMessage(options.input.caption, locale, options.identifiers);
|
|
146
151
|
const appLinks = caption === void 0 ? void 0 : telegramAppLinkContext(options.appLinks, environment, async () => (await runtimeBotIdentity(scope)({ request: (method, body) => proxiedBotRequest(proxy, method, body) }))?.username);
|
|
@@ -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-BtB38zQ0.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 { E as OntologyActionNotPermitted, Et as principalRuntimeSource, L as needsConfirmation, Ot as runtimeEnvelope, W as member, c as currentInboundRequest, j as ontologyTools, k as actionToolName, kt as telegramRuntimeSource } from "../../../_chunks/principal-DL9NdRRP.mjs";
|
|
8
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
8
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BtB38zQ0.mjs";
|
|
9
9
|
import { object } from "../confirmation-description.mjs";
|
|
10
10
|
var AttachmentLifecycleNotPermitted = class extends Error {
|
|
11
11
|
code = "attachment_lifecycle_not_permitted";
|
|
@@ -4,6 +4,18 @@ import { type ChatArchive } from "../chat-archive.js";
|
|
|
4
4
|
import { type Environment } from "../environment.js";
|
|
5
5
|
import type { RuntimeToken } from "../runtime-token.js";
|
|
6
6
|
import type { SendAttachmentsInput } from "../telegram/attachments-send.js";
|
|
7
|
+
/**
|
|
8
|
+
* `attachments_send` in a WhatsApp chat: each stored file is a message to the person, sent through
|
|
9
|
+
* the WhatsApp channel's connection by the ontology's WhatsApp sender (`sendWhatsAppFiles`), to
|
|
10
|
+
* whom the channel's adapter replies, the caption on the first as the adapter writes it; a caption
|
|
11
|
+
* no file can carry goes first on its own, and the call answers its failure as `caption`. The
|
|
12
|
+
* connection admits no media uploads, so WhatsApp fetches each file by the ontology's download
|
|
13
|
+
* grant, a link valid for about a minute: JPEG and PNG images go as images, other files as
|
|
14
|
+
* WhatsApp's types for them, within WhatsApp's size for each. Each message sent is
|
|
15
|
+
* archived in the person's chat with WhatsApp's id of it, so a file WhatsApp reports later it could
|
|
16
|
+
* not fetch, in the message's status, reads failed in the chat's history. The checks and failures
|
|
17
|
+
* are every channel's (`../attachments-send.ts`).
|
|
18
|
+
*/
|
|
7
19
|
/** What became of one requested file. */
|
|
8
20
|
export interface WhatsAppSentFile extends Partial<AttachmentSendFailure> {
|
|
9
21
|
readonly id: string;
|
|
@@ -10,12 +10,11 @@ import { r as chatArchive } from "../../../_chunks/chat-archive-_IBU0CXG.mjs";
|
|
|
10
10
|
import { connectionProxy } from "../connection-proxy.mjs";
|
|
11
11
|
import { redactDiagnostic } from "../diagnostics.mjs";
|
|
12
12
|
import { proxyEnvironment } from "../environment.mjs";
|
|
13
|
-
import { StoredFileError, storedFileGrant } from "../sent-files.mjs";
|
|
13
|
+
import { StoredFileError, storedChannelFile, storedFileGrant } from "../sent-files.mjs";
|
|
14
14
|
import { ProxiedWhatsAppAdapter } from "./adapter.mjs";
|
|
15
15
|
import { recordSent } from "./sent.mjs";
|
|
16
16
|
import { randomUUID } from "node:crypto";
|
|
17
17
|
import { sendWhatsAppFiles, whatsappFileRefusal } from "@segmentstream/ontology/whatsapp";
|
|
18
|
-
const FRESH_LINK_MS = 15e3;
|
|
19
18
|
function reportWhatsAppError(operation, error, secrets) {
|
|
20
19
|
const reference = randomUUID();
|
|
21
20
|
console.error("[segmentstream] WhatsApp delivery diagnostic", {
|
|
@@ -86,23 +85,17 @@ async function sendWhatsAppAttachments(options) {
|
|
|
86
85
|
id: reference.id,
|
|
87
86
|
filename: grant.filename
|
|
88
87
|
};
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
const stored = storedChannelFile(grant, {
|
|
89
|
+
reference,
|
|
90
|
+
owner,
|
|
91
|
+
origin,
|
|
92
|
+
token,
|
|
93
|
+
fetch,
|
|
94
|
+
secrets
|
|
95
|
+
});
|
|
93
96
|
linked.push({
|
|
94
97
|
index,
|
|
95
|
-
link: async () =>
|
|
96
|
-
if (Date.now() - granted.at <= FRESH_LINK_MS) return granted.url;
|
|
97
|
-
return (await storedFileGrant({
|
|
98
|
-
reference,
|
|
99
|
-
owner,
|
|
100
|
-
origin,
|
|
101
|
-
token,
|
|
102
|
-
fetch,
|
|
103
|
-
secrets
|
|
104
|
-
})).url;
|
|
105
|
-
},
|
|
98
|
+
link: async () => (await stored.link()).url,
|
|
106
99
|
filename: grant.filename,
|
|
107
100
|
contentType: grant.contentType,
|
|
108
101
|
size: grant.size
|