@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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.102.0
|
|
4
|
+
|
|
5
|
+
On email and workspace channels, an agent with `review` proposes each turn's reply to the
|
|
6
|
+
ontology service instead of sending or holding it itself, so the review applies with the roles the
|
|
7
|
+
person holds when the reply is written, never the ones their session started with. The agent still
|
|
8
|
+
judges a reply its session's roles review `whenUnsure` and sends the confidence; the criteria stay
|
|
9
|
+
in the agent. The ontology sends or holds the reply and records it in the chat. Without `review`,
|
|
10
|
+
replies are sent as before.
|
|
11
|
+
|
|
12
|
+
Action required: upgrade `@segmentstream/ontology` with the agent SDK. An ontology service released
|
|
13
|
+
before proposals answers 404, and the agent then reviews the reply itself by the session's roles,
|
|
14
|
+
as before; that fallback is deprecated and stops on 2027-05-28 (`agent.reply-proposals-fallback`),
|
|
15
|
+
after which the agent requires an ontology SDK that serves `POST /api/ontology/agent/replies`.
|
|
16
|
+
|
|
17
|
+
Pins @segmentstream/ontology 0.181.0.
|
|
18
|
+
|
|
19
|
+
## 0.101.0
|
|
20
|
+
|
|
21
|
+
`segmentstream__attachments_send` sends files in Telegram and Slack up to the providers' own
|
|
22
|
+
limits, not the connection proxy's 2 MiB request body.
|
|
23
|
+
|
|
24
|
+
- **Telegram.** Photos go up to 10 MB and other files up to 50 MB, Telegram's limits for a bot's
|
|
25
|
+
uploads. A file of up to 1,984 KiB is sent in its request's body and may share an album, as
|
|
26
|
+
before. A larger one goes alone in its message: the connection proxy reads it from the
|
|
27
|
+
workspace's file storage and sends it inside the multipart form the agent builds around it, so
|
|
28
|
+
its bytes never pass through the agent.
|
|
29
|
+
- **Slack.** A file goes up to Slack's 1 GB. Its bytes stream from the workspace's file storage
|
|
30
|
+
straight to the upload address `files.getUploadURLExternal` answers, which needs no credential,
|
|
31
|
+
without passing through the connection proxy; each file must reach Slack within two minutes.
|
|
32
|
+
- Files a member sends to the agent are still stored up to 20 MB in every channel. In a chat whose
|
|
33
|
+
replies are reviewed, files still go with the reply at most 2 MiB each in Telegram and Slack,
|
|
34
|
+
since approval may send the reply through the ontology.
|
|
35
|
+
|
|
36
|
+
The ontology SDK's channel senders take the files this needs: `sendTelegramFiles` and
|
|
37
|
+
`sendSlackFiles` accept a `ChannelStoredFile` (`{ filename, contentType, size, read() }`), which
|
|
38
|
+
Telegram's sender sends as a `ChannelCall` with a `file` (a signed read with `prefix` and `suffix`
|
|
39
|
+
bytes around it) and Slack's uploads with the new `fetch` option. `telegramFileRefusal` takes
|
|
40
|
+
`{ stored: true }` and `slackFileRefusal` `{ direct: true }` to check a file against the
|
|
41
|
+
provider's limit alone. `TELEGRAM_INLINE_FILE_BYTES`, `SLACK_UPLOAD_BYTES` and
|
|
42
|
+
`CHANNEL_FILE_FRAME_BYTES` name the bounds. Without these options both senders behave as before.
|
|
43
|
+
|
|
44
|
+
Pins @segmentstream/ontology 0.180.0.
|
|
45
|
+
|
|
3
46
|
## 0.100.0
|
|
4
47
|
|
|
5
48
|
Opened groups can use a workspace-authored response handler and one explicitly granted ontology action. The action acts for a sender scoped to that group.
|
|
@@ -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
|
-
var version = "0.
|
|
7
|
+
var version = "0.102.0";
|
|
8
8
|
function agentBuild(environment = process.env) {
|
|
9
9
|
return {
|
|
10
10
|
agent: version,
|
|
@@ -4,4 +4,4 @@ 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
|
-
export { t as agentBuild } from "../../_chunks/build-metadata-
|
|
7
|
+
export { t as agentBuild } from "../../_chunks/build-metadata-BtB38zQ0.mjs";
|
|
@@ -9,6 +9,11 @@ import type { RuntimeToken } from "./runtime-token.js";
|
|
|
9
9
|
*/
|
|
10
10
|
/** The largest request or buffered response body the proxy forwards. */
|
|
11
11
|
export declare const MAXIMUM_PROXY_BODY_BYTES: number;
|
|
12
|
+
/**
|
|
13
|
+
* The most bytes a call sends before, and the most after, a stored file it sends as its body
|
|
14
|
+
* ({@link ProxyFile}'s `prefix` and `suffix`).
|
|
15
|
+
*/
|
|
16
|
+
export declare const MAXIMUM_PROXY_FRAME_BYTES: number;
|
|
12
17
|
/**
|
|
13
18
|
* The largest file a streamed download takes: what Telegram's Bot API lets a bot download. A
|
|
14
19
|
* streamed response is relayed in frames rather than buffered, so it is not bound by
|
|
@@ -32,12 +37,18 @@ export interface ProxyCall {
|
|
|
32
37
|
*/
|
|
33
38
|
readonly file?: ProxyFile;
|
|
34
39
|
}
|
|
35
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* The signed read of a byte range of a stored file, as the proxy reads it, and the bytes it sends
|
|
42
|
+
* before and after the file's, such as a multipart form around the file's part: each at most
|
|
43
|
+
* {@link MAXIMUM_PROXY_FRAME_BYTES}, and the whole body at most 64 MiB.
|
|
44
|
+
*/
|
|
36
45
|
export interface ProxyFile {
|
|
37
46
|
readonly url: string;
|
|
38
47
|
readonly headers: ReadonlyArray<readonly [string, string]>;
|
|
39
48
|
readonly start: number;
|
|
40
49
|
readonly end: number;
|
|
50
|
+
readonly prefix?: Uint8Array;
|
|
51
|
+
readonly suffix?: Uint8Array;
|
|
41
52
|
}
|
|
42
53
|
/** What the provider answered, buffered by the proxy. */
|
|
43
54
|
export interface ProxyReply {
|
|
@@ -5,9 +5,24 @@ const __filename = __eveFileURLToPath(import.meta.url);
|
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
const MAXIMUM_PROXY_BODY_BYTES = 2097152;
|
|
8
|
+
const MAXIMUM_PROXY_FRAME_BYTES = 65536;
|
|
8
9
|
const MAXIMUM_DOWNLOAD_BYTES = 20971520;
|
|
9
10
|
const PROXY_STREAM_MEDIA_TYPE = "application/vnd.segmentstream.proxy-stream.v1+ndjson";
|
|
10
11
|
const CALL_TIMEOUT_MS = 4e4;
|
|
12
|
+
const STORED_FILE_READ_MS = 15e3;
|
|
13
|
+
function wireFile(file) {
|
|
14
|
+
const base64 = (bytes) => bytes === void 0 || bytes.byteLength === 0 ? void 0 : Buffer.from(bytes).toString("base64");
|
|
15
|
+
const prefix = base64(file.prefix);
|
|
16
|
+
const suffix = base64(file.suffix);
|
|
17
|
+
return {
|
|
18
|
+
url: file.url,
|
|
19
|
+
headers: file.headers,
|
|
20
|
+
start: file.start,
|
|
21
|
+
end: file.end,
|
|
22
|
+
...prefix === void 0 ? {} : { prefix },
|
|
23
|
+
...suffix === void 0 ? {} : { suffix }
|
|
24
|
+
};
|
|
25
|
+
}
|
|
11
26
|
var ConnectionProxyError = class extends Error {
|
|
12
27
|
name = "ConnectionProxyError";
|
|
13
28
|
connection;
|
|
@@ -63,6 +78,12 @@ function connectionProxy(input) {
|
|
|
63
78
|
code: "invalid_request",
|
|
64
79
|
message: "The request body exceeds the proxy's 2 MiB limit."
|
|
65
80
|
});
|
|
81
|
+
if ((call.file?.prefix?.byteLength ?? 0) > 65536 || (call.file?.suffix?.byteLength ?? 0) > 65536) throw new ConnectionProxyError({
|
|
82
|
+
connection,
|
|
83
|
+
code: "invalid_request",
|
|
84
|
+
message: "The bytes around a stored file exceed the proxy's 64 KiB limit."
|
|
85
|
+
});
|
|
86
|
+
const timeout = CALL_TIMEOUT_MS + (call.file === void 0 ? 0 : STORED_FILE_READ_MS);
|
|
66
87
|
const token = await input.token();
|
|
67
88
|
try {
|
|
68
89
|
return await fetchImpl(url, {
|
|
@@ -78,10 +99,10 @@ function connectionProxy(input) {
|
|
|
78
99
|
method: call.method,
|
|
79
100
|
path: call.path,
|
|
80
101
|
headers: call.headers,
|
|
81
|
-
...call.file === void 0 ? { body: Buffer.from(call.body).toString("base64") } : { file: call.file }
|
|
102
|
+
...call.file === void 0 ? { body: Buffer.from(call.body).toString("base64") } : { file: wireFile(call.file) }
|
|
82
103
|
}),
|
|
83
104
|
redirect: "error",
|
|
84
|
-
signal: abortSignal === void 0 ? AbortSignal.timeout(
|
|
105
|
+
signal: abortSignal === void 0 ? AbortSignal.timeout(timeout) : AbortSignal.any([abortSignal, AbortSignal.timeout(timeout)])
|
|
85
106
|
});
|
|
86
107
|
} catch (error) {
|
|
87
108
|
throw new ConnectionProxyError({
|
|
@@ -206,4 +227,4 @@ async function framedReply(connection, body, maxBytes) {
|
|
|
206
227
|
body: new Uint8Array(Buffer.concat(chunks))
|
|
207
228
|
};
|
|
208
229
|
}
|
|
209
|
-
export { ConnectionProxyError, MAXIMUM_DOWNLOAD_BYTES, MAXIMUM_PROXY_BODY_BYTES, PROXY_STREAM_MEDIA_TYPE, connectionProxy };
|
|
230
|
+
export { ConnectionProxyError, MAXIMUM_DOWNLOAD_BYTES, MAXIMUM_PROXY_BODY_BYTES, MAXIMUM_PROXY_FRAME_BYTES, PROXY_STREAM_MEDIA_TYPE, connectionProxy };
|
|
@@ -66,6 +66,8 @@ export interface EmailChannelOptions {
|
|
|
66
66
|
/** Tests pass an archive and a hold in place of the ontology's. */
|
|
67
67
|
readonly archive?: ChatArchive;
|
|
68
68
|
readonly hold?: Parameters<typeof channelReview>[0]["hold"];
|
|
69
|
+
/** Tests pass a proposal in place of the ontology's. */
|
|
70
|
+
readonly propose?: Parameters<typeof channelReview>[0]["propose"];
|
|
69
71
|
/** Sends a passed reply's files; without it such a reply is held. */
|
|
70
72
|
readonly sendFiles?: Parameters<typeof channelReview>[0]["sendFiles"];
|
|
71
73
|
};
|
|
@@ -50,6 +50,7 @@ var MessageTimeout = class extends Error {
|
|
|
50
50
|
};
|
|
51
51
|
const REPLY_ATTEMPTS = 4;
|
|
52
52
|
const REPLY_RETRY_MS = 2e3;
|
|
53
|
+
const DELIVER_ATTEMPTS = 3;
|
|
53
54
|
const REPLY_WAKE_AFTER_MS = 6e5;
|
|
54
55
|
const REPLY_EXPIRY_MS = 2592e5;
|
|
55
56
|
const REPLIES_PER_WAKE = 10;
|
|
@@ -247,6 +248,7 @@ function emailChannelDefinition(options) {
|
|
|
247
248
|
model: options.review.model,
|
|
248
249
|
...options.review.archive === void 0 ? {} : { archive: options.review.archive },
|
|
249
250
|
...options.review.hold === void 0 ? {} : { hold: options.review.hold },
|
|
251
|
+
...options.review.propose === void 0 ? {} : { propose: options.review.propose },
|
|
250
252
|
...options.review.sendFiles === void 0 ? {} : { sendFiles: options.review.sendFiles }
|
|
251
253
|
}, {
|
|
252
254
|
environment,
|
|
@@ -1016,7 +1018,7 @@ function emailChannelDefinition(options) {
|
|
|
1016
1018
|
});
|
|
1017
1019
|
return;
|
|
1018
1020
|
}
|
|
1019
|
-
|
|
1021
|
+
const written = {
|
|
1020
1022
|
principal,
|
|
1021
1023
|
sessionId: ctx.session.id,
|
|
1022
1024
|
turnId,
|
|
@@ -1027,7 +1029,32 @@ function emailChannelDefinition(options) {
|
|
|
1027
1029
|
cc
|
|
1028
1030
|
},
|
|
1029
1031
|
...files === void 0 || files.files.length === 0 ? {} : { files }
|
|
1030
|
-
}
|
|
1032
|
+
};
|
|
1033
|
+
const proposed = reviewing !== void 0 && reviewing.proposes(principal) ? await reviewing.propose(written) : void 0;
|
|
1034
|
+
if (proposed?.by === "ontology") {
|
|
1035
|
+
if (proposed.outcome !== "sent" && proposed.outcome !== "held") {
|
|
1036
|
+
log("[segmentstream] email reply failed", {
|
|
1037
|
+
sessionId: ctx.session.id,
|
|
1038
|
+
outcome: proposed.outcome,
|
|
1039
|
+
...proposed.refusal === void 0 ? {} : { refusal: proposed.refusal }
|
|
1040
|
+
});
|
|
1041
|
+
if (files !== void 0 && files.files.length > 0) await reviewing.holdFiles({
|
|
1042
|
+
principal,
|
|
1043
|
+
sessionId: ctx.session.id,
|
|
1044
|
+
turnId,
|
|
1045
|
+
files,
|
|
1046
|
+
recipients: {
|
|
1047
|
+
to: [chat.peer],
|
|
1048
|
+
cc
|
|
1049
|
+
}
|
|
1050
|
+
});
|
|
1051
|
+
} else if (proposed.outcome === "sent" && proposed.files !== void 0 && proposed.files !== "sent") log("[segmentstream] a passed reply's files were not all sent", {
|
|
1052
|
+
sessionId: ctx.session.id,
|
|
1053
|
+
outcome: proposed.files
|
|
1054
|
+
});
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
if (!(proposed !== void 0 ? proposed.send : reviewing === void 0 ? true : await reviewing.sendOrHold(written))) return;
|
|
1031
1058
|
const memberId = memberIdOf(principal);
|
|
1032
1059
|
const participantIds = participantsOf(principal) ?? (memberId === void 0 ? [] : [memberId]);
|
|
1033
1060
|
const reply = {
|
|
@@ -1126,15 +1153,22 @@ function emailChannelDefinition(options) {
|
|
|
1126
1153
|
return Response.json({ error: "file_unavailable" }, { status: 422 });
|
|
1127
1154
|
}
|
|
1128
1155
|
try {
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1156
|
+
let id;
|
|
1157
|
+
for (let attempt = 1;; attempt++) try {
|
|
1158
|
+
id = await mailbox().reply({
|
|
1159
|
+
threadId,
|
|
1160
|
+
to,
|
|
1161
|
+
...cc.length === 0 ? {} : { cc },
|
|
1162
|
+
text,
|
|
1163
|
+
key,
|
|
1164
|
+
...attachments.length === 0 ? {} : { attachments },
|
|
1165
|
+
...withSignature()
|
|
1166
|
+
});
|
|
1167
|
+
break;
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
if (!retryable(error) || attempt >= DELIVER_ATTEMPTS) throw error;
|
|
1170
|
+
await sleep(REPLY_RETRY_MS * 4 ** (attempt - 1));
|
|
1171
|
+
}
|
|
1138
1172
|
return Response.json({ message_id: id === "" ? null : id });
|
|
1139
1173
|
} catch (cause) {
|
|
1140
1174
|
const reference = report("deliver_failed", cause);
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { Review as PolicyReview, ReviewSettings as PolicySettings } from "@segmentstream/ontology/review";
|
|
1
2
|
import type { RuntimeToken } from "../runtime-token.js";
|
|
3
|
+
import type { ReplyFile } from "./files.js";
|
|
2
4
|
import type { HoldReply } from "./hold.js";
|
|
3
5
|
/**
|
|
4
6
|
* Reports a reply kept for review to the ontology service, which records it as a pending
|
|
@@ -11,6 +13,79 @@ export declare function agentReviews(input: {
|
|
|
11
13
|
readonly token: RuntimeToken;
|
|
12
14
|
readonly fetch?: typeof fetch;
|
|
13
15
|
}): HoldReply;
|
|
16
|
+
/** The review the ontology applied, with the roles the person held when it decided. */
|
|
17
|
+
export type AppliedReview = PolicyReview;
|
|
18
|
+
/** What the ontology service did with a turn's reply the agent proposed. */
|
|
19
|
+
export type ProposalOutcome = {
|
|
20
|
+
readonly outcome: "held";
|
|
21
|
+
readonly review: AppliedReview;
|
|
22
|
+
readonly draft: string;
|
|
23
|
+
} | {
|
|
24
|
+
/** The reply's text, or its files for a reply of files alone. */
|
|
25
|
+
readonly outcome: "sent" | "uncertain" | "failed";
|
|
26
|
+
readonly review: AppliedReview;
|
|
27
|
+
readonly messageId: string | null;
|
|
28
|
+
/** Why the provider refused a `failed` reply, when it says. */
|
|
29
|
+
readonly refusal?: string;
|
|
30
|
+
/** What became of the files' message after the text: `unsent` when the text did not go. */
|
|
31
|
+
readonly files?: "sent" | "uncertain" | "failed" | "unsent";
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @legacy agent.reply-proposals-fallback
|
|
35
|
+
* An ontology service from before proposals answered 404: the channel reviews the reply itself.
|
|
36
|
+
*/
|
|
37
|
+
| {
|
|
38
|
+
readonly outcome: "unsupported";
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The reply of a turn, proposed to the ontology service (`POST /api/ontology/agent/replies`) in
|
|
42
|
+
* place of sending it: the ontology applies `review` with the roles the person holds now, and sends
|
|
43
|
+
* or holds it. Its criteria stay here: only the modes and thresholds go.
|
|
44
|
+
*/
|
|
45
|
+
export interface ProposedReply {
|
|
46
|
+
/** Names the reply, so a proposal repeated after a failure finds what became of it. */
|
|
47
|
+
readonly key: string;
|
|
48
|
+
readonly provider: "email" | "custom";
|
|
49
|
+
/** The channel the chat is on, by name, when it is not the one named after its kind. */
|
|
50
|
+
readonly channel?: string;
|
|
51
|
+
readonly chatId: string;
|
|
52
|
+
readonly recipient: string;
|
|
53
|
+
readonly threadId: string;
|
|
54
|
+
readonly replyTo: string | null;
|
|
55
|
+
/** A reply to mail: whom it goes to and copies. */
|
|
56
|
+
readonly to?: ReadonlyArray<string>;
|
|
57
|
+
readonly cc?: ReadonlyArray<string>;
|
|
58
|
+
/** The members who read it, in a chat several people share, its member among them. */
|
|
59
|
+
readonly participants?: ReadonlyArray<string>;
|
|
60
|
+
readonly memberId: string;
|
|
61
|
+
readonly sessionId: string;
|
|
62
|
+
readonly turnId: string;
|
|
63
|
+
readonly text: string;
|
|
64
|
+
/** The captions the reply's files were attached with. */
|
|
65
|
+
readonly caption?: string;
|
|
66
|
+
readonly files?: ReadonlyArray<ReplyFile>;
|
|
67
|
+
/** The agent's review for the channel, without its criteria. */
|
|
68
|
+
readonly review: PolicySettings;
|
|
69
|
+
/** The decision model's probability that staff would send it as written; null when unjudged. */
|
|
70
|
+
readonly confidence: number | null;
|
|
71
|
+
/** Where the chat stood when the turn read it, `<time>~<id>`; null when unknown. */
|
|
72
|
+
readonly basis: string | null;
|
|
73
|
+
/** A workspace channel's `limits.maxLength`, in whose parts a longer reply is sent. */
|
|
74
|
+
readonly maxLength?: number;
|
|
75
|
+
}
|
|
76
|
+
/** Proposes a turn's reply; resolves what became of it. */
|
|
77
|
+
export type ProposeReply = (reply: ProposedReply) => Promise<ProposalOutcome>;
|
|
78
|
+
/**
|
|
79
|
+
* Proposes a turn's reply to the ontology service, which sends or holds it by `review` and the
|
|
80
|
+
* roles the person holds now. A refusal, or an answer the agent does not know, rejects, so the
|
|
81
|
+
* reply is neither sent nor lost silently; an ontology service that predates proposals answers
|
|
82
|
+
* 404, which resolves `unsupported`.
|
|
83
|
+
*/
|
|
84
|
+
export declare function agentReplies(input: {
|
|
85
|
+
readonly origin: string;
|
|
86
|
+
readonly token: RuntimeToken;
|
|
87
|
+
readonly fetch?: typeof fetch;
|
|
88
|
+
}): ProposeReply;
|
|
14
89
|
/** What became of a reply an agent session held for review, as the ontology service answers. */
|
|
15
90
|
export interface SessionReview {
|
|
16
91
|
readonly id: string;
|
|
@@ -99,6 +99,108 @@ function agentReviews(input) {
|
|
|
99
99
|
}
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
|
+
const PROPOSAL_TIMEOUT_MS = 2e5;
|
|
103
|
+
const DELIVERY_OUTCOMES = new Set([
|
|
104
|
+
"sent",
|
|
105
|
+
"uncertain",
|
|
106
|
+
"failed"
|
|
107
|
+
]);
|
|
108
|
+
function appliedReview(value) {
|
|
109
|
+
if (!isRecord(value)) return;
|
|
110
|
+
if (value.mode === "never" || value.mode === "always") return { mode: value.mode };
|
|
111
|
+
return value.mode === "whenUnsure" && typeof value.min_confidence === "number" && Number.isFinite(value.min_confidence) ? {
|
|
112
|
+
mode: "whenUnsure",
|
|
113
|
+
minConfidence: value.min_confidence
|
|
114
|
+
} : void 0;
|
|
115
|
+
}
|
|
116
|
+
function agentReplies(input) {
|
|
117
|
+
const fetchImpl = input.fetch ?? fetch;
|
|
118
|
+
return async (reply) => {
|
|
119
|
+
const token = await input.token();
|
|
120
|
+
let response;
|
|
121
|
+
try {
|
|
122
|
+
response = await fetchImpl(`${input.origin}/api/ontology/agent/replies`, {
|
|
123
|
+
method: "POST",
|
|
124
|
+
headers: {
|
|
125
|
+
authorization: `Bearer ${token}`,
|
|
126
|
+
...ontologyDeploymentHeaders(),
|
|
127
|
+
accept: "application/json",
|
|
128
|
+
"content-type": "application/json"
|
|
129
|
+
},
|
|
130
|
+
body: JSON.stringify({
|
|
131
|
+
key: reply.key,
|
|
132
|
+
provider: reply.provider,
|
|
133
|
+
...reply.channel === void 0 ? {} : { channel: reply.channel },
|
|
134
|
+
chat_id: reply.chatId,
|
|
135
|
+
recipient: reply.recipient,
|
|
136
|
+
...reply.to === void 0 ? {} : {
|
|
137
|
+
to: reply.to,
|
|
138
|
+
cc: reply.cc ?? []
|
|
139
|
+
},
|
|
140
|
+
...reply.participants === void 0 ? {} : { participants: reply.participants },
|
|
141
|
+
thread_id: reply.threadId,
|
|
142
|
+
reply_to: reply.replyTo,
|
|
143
|
+
member_id: reply.memberId,
|
|
144
|
+
session_id: reply.sessionId,
|
|
145
|
+
text: reply.text,
|
|
146
|
+
...reply.caption === void 0 ? {} : { caption: reply.caption },
|
|
147
|
+
...reply.files === void 0 || reply.files.length === 0 ? {} : { files: reply.files.map((file) => ({
|
|
148
|
+
entity: file.entity,
|
|
149
|
+
id: file.id,
|
|
150
|
+
property: file.property
|
|
151
|
+
})) },
|
|
152
|
+
turn_id: reply.turnId,
|
|
153
|
+
review: reply.review,
|
|
154
|
+
confidence: reply.confidence,
|
|
155
|
+
basis: reply.basis,
|
|
156
|
+
...reply.maxLength === void 0 ? {} : { max_length: reply.maxLength }
|
|
157
|
+
}),
|
|
158
|
+
redirect: "error",
|
|
159
|
+
signal: AbortSignal.timeout(PROPOSAL_TIMEOUT_MS)
|
|
160
|
+
});
|
|
161
|
+
} catch (error) {
|
|
162
|
+
throw new AgentIdentityError({
|
|
163
|
+
code: "unavailable",
|
|
164
|
+
message: "The ontology service could not be reached to propose a reply.",
|
|
165
|
+
cause: redactDiagnostic(error, new Set([token]))
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
if (response.status === 404) {
|
|
169
|
+
await response.body?.cancel();
|
|
170
|
+
return { outcome: "unsupported" };
|
|
171
|
+
}
|
|
172
|
+
if (!response.ok) {
|
|
173
|
+
const error = await refusal(response, "reply proposal");
|
|
174
|
+
throw new AgentIdentityError({
|
|
175
|
+
code: error.code,
|
|
176
|
+
...error.status === void 0 ? {} : { status: error.status },
|
|
177
|
+
...error.requestId === void 0 ? {} : { requestId: error.requestId },
|
|
178
|
+
message: redactDiagnostic(error.message, new Set([token])),
|
|
179
|
+
cause: redactDiagnostic(error, new Set([token]))
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
const body = await response.json().catch(() => void 0);
|
|
183
|
+
const review = isRecord(body) ? appliedReview(body.review) : void 0;
|
|
184
|
+
if (isRecord(body) && review !== void 0) {
|
|
185
|
+
if (body.outcome === "held" && typeof body.draft === "string") return {
|
|
186
|
+
outcome: "held",
|
|
187
|
+
review,
|
|
188
|
+
draft: body.draft
|
|
189
|
+
};
|
|
190
|
+
if (typeof body.outcome === "string" && DELIVERY_OUTCOMES.has(body.outcome) && (body.message_id === null || typeof body.message_id === "string")) return {
|
|
191
|
+
outcome: body.outcome,
|
|
192
|
+
review,
|
|
193
|
+
messageId: body.message_id,
|
|
194
|
+
...typeof body.refusal === "string" ? { refusal: body.refusal } : {},
|
|
195
|
+
...body.files === "sent" || body.files === "uncertain" || body.files === "failed" || body.files === "unsent" ? { files: body.files } : {}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
throw new AgentIdentityError({
|
|
199
|
+
code: "unavailable",
|
|
200
|
+
message: "The ontology service answered a reply proposal it did not describe."
|
|
201
|
+
});
|
|
202
|
+
};
|
|
203
|
+
}
|
|
102
204
|
async function refusedFields(response) {
|
|
103
205
|
try {
|
|
104
206
|
const body = await response.clone().json();
|
|
@@ -159,4 +261,4 @@ function agentSessionReviews(input) {
|
|
|
159
261
|
}] : []);
|
|
160
262
|
};
|
|
161
263
|
}
|
|
162
|
-
export { agentReviews, agentSessionReviews };
|
|
264
|
+
export { agentReplies, agentReviews, agentSessionReviews };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { SessionAuthContext } from "eve/context";
|
|
2
|
+
import type { ReviewSettings as PolicySettings } from "@segmentstream/ontology/review";
|
|
2
3
|
import { type ReviewSettings, type Review } from "../../review.js";
|
|
4
|
+
import type { ProposedReply } from "./client.js";
|
|
3
5
|
import type { ReplyFile } from "./files.js";
|
|
4
6
|
/**
|
|
5
7
|
* Whether the agent sends a reply itself or holds it for a member to review, by the review of the
|
|
@@ -93,6 +95,28 @@ export declare function repliesReviewed(settings: ReviewSettings | undefined, pr
|
|
|
93
95
|
* gives no probability holds it, so a model outage never sends an unreviewed reply.
|
|
94
96
|
*/
|
|
95
97
|
export declare function reviewOutcome(review: Review, judge: (criteria: string | undefined) => Promise<number | undefined>, onJudgeFailure?: (error: unknown) => void): Promise<ReviewOutcome>;
|
|
98
|
+
/**
|
|
99
|
+
* The proposal of a turn's reply to `principal` on a channel whose replies the ontology sends,
|
|
100
|
+
* an email or a workspace channel; undefined on another channel, or when the session has no chat
|
|
101
|
+
* or member to propose it for.
|
|
102
|
+
*/
|
|
103
|
+
export declare function proposedReply(input: {
|
|
104
|
+
readonly principal: SessionAuthContext | null | undefined;
|
|
105
|
+
readonly sessionId: string;
|
|
106
|
+
readonly partId: string;
|
|
107
|
+
readonly turnId: string;
|
|
108
|
+
readonly text: string;
|
|
109
|
+
readonly caption?: string;
|
|
110
|
+
readonly files?: ReadonlyArray<ReplyFile>;
|
|
111
|
+
/** A reply to mail: whom it goes to and copies. */
|
|
112
|
+
readonly recipients?: {
|
|
113
|
+
readonly to: ReadonlyArray<string>;
|
|
114
|
+
readonly cc: ReadonlyArray<string>;
|
|
115
|
+
};
|
|
116
|
+
readonly review: PolicySettings;
|
|
117
|
+
readonly confidence: number | null;
|
|
118
|
+
readonly maxLength?: number;
|
|
119
|
+
}): ProposedReply | undefined;
|
|
96
120
|
/**
|
|
97
121
|
* The held reply for a final reply to `principal`, or undefined when the session has no
|
|
98
122
|
* chat or member to hold it for, as a workflow task's session does.
|
|
@@ -38,6 +38,36 @@ async function reviewOutcome(review, judge, onJudgeFailure = () => void 0) {
|
|
|
38
38
|
confidence: confidence ?? null
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
|
+
function proposedReply(input) {
|
|
42
|
+
const chat = chatAddressOf(input.principal);
|
|
43
|
+
const memberId = memberIdOf(input.principal);
|
|
44
|
+
const participants = participantsOf(input.principal);
|
|
45
|
+
if (chat === void 0 || memberId === void 0 || chat.provider !== "email" && chat.provider !== "custom") return;
|
|
46
|
+
return {
|
|
47
|
+
key: `${input.sessionId}:${input.partId}`,
|
|
48
|
+
provider: chat.provider,
|
|
49
|
+
...chat.channel === void 0 ? {} : { channel: chat.channel },
|
|
50
|
+
chatId: chat.chatId,
|
|
51
|
+
recipient: chat.kind === "private" && chat.peer !== void 0 ? chat.peer : chat.chatId,
|
|
52
|
+
threadId: chat.threadId,
|
|
53
|
+
replyTo: null,
|
|
54
|
+
...chat.provider === "email" && input.recipients !== void 0 ? {
|
|
55
|
+
to: input.recipients.to,
|
|
56
|
+
cc: input.recipients.cc
|
|
57
|
+
} : {},
|
|
58
|
+
...participants === void 0 ? {} : { participants },
|
|
59
|
+
memberId,
|
|
60
|
+
sessionId: input.sessionId,
|
|
61
|
+
turnId: input.turnId,
|
|
62
|
+
text: input.text,
|
|
63
|
+
...input.caption === void 0 || input.caption === "" ? {} : { caption: input.caption },
|
|
64
|
+
...input.files === void 0 || input.files.length === 0 ? {} : { files: input.files },
|
|
65
|
+
review: input.review,
|
|
66
|
+
confidence: input.confidence,
|
|
67
|
+
basis: null,
|
|
68
|
+
...input.maxLength === void 0 ? {} : { maxLength: input.maxLength }
|
|
69
|
+
};
|
|
70
|
+
}
|
|
41
71
|
function heldReply(input) {
|
|
42
72
|
const chat = chatAddressOf(input.principal);
|
|
43
73
|
const memberId = memberIdOf(input.principal);
|
|
@@ -66,4 +96,4 @@ function heldReply(input) {
|
|
|
66
96
|
...input.turnId === void 0 || input.turnId === null || input.turnId === "" ? {} : { turnId: input.turnId }
|
|
67
97
|
};
|
|
68
98
|
}
|
|
69
|
-
export { heldReply, repliesReviewed, reviewOf, reviewOutcome };
|
|
99
|
+
export { heldReply, proposedReply, repliesReviewed, reviewOf, reviewOutcome };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { SessionAuthContext } from "eve/context";
|
|
2
2
|
import type { SessionView } from "eve/events";
|
|
3
|
-
import type
|
|
3
|
+
import { type ReviewSettings } from "../../review.js";
|
|
4
4
|
import { type ChatArchive } from "../chat-archive.js";
|
|
5
5
|
import { type Environment } from "../environment.js";
|
|
6
6
|
import type { RuntimeToken } from "../runtime-token.js";
|
|
7
|
+
import { type ProposalOutcome, type ProposeReply } from "./client.js";
|
|
7
8
|
import { type HoldReply } from "./hold.js";
|
|
8
9
|
import { type ReviewDecideFunction } from "./judge.js";
|
|
9
10
|
import { type ReplyFiles } from "./files.js";
|
|
@@ -15,6 +16,37 @@ import { type ReplyFiles } from "./files.js";
|
|
|
15
16
|
export interface ReplyReview {
|
|
16
17
|
/** Whether replies to `principal` are reviewed: their review is not `never`. */
|
|
17
18
|
reviewed(principal: SessionAuthContext | null | undefined): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Whether a turn's reply to `principal` is proposed to the ontology rather than sent or held
|
|
21
|
+
* here: the agent has `review`, and the chat is on an email or a workspace channel, whose replies
|
|
22
|
+
* the ontology sends.
|
|
23
|
+
*/
|
|
24
|
+
proposes(principal: SessionAuthContext | null | undefined): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Proposes a turn's reply to the ontology, which applies `review` with the roles the person
|
|
27
|
+
* holds now and sends or holds it: the judge still reads a reply whose review by the session's
|
|
28
|
+
* roles is `whenUnsure`, and its confidence goes with the proposal. Files past a draft's ten go in
|
|
29
|
+
* proposals of their own once the reply was sent or held. Answers what the ontology did, or, from
|
|
30
|
+
* an ontology that predates proposals, whether the channel may send the reply itself after
|
|
31
|
+
* {@link ReplyReview.sendOrHold}. Throws when the ontology refused it, so the reply is neither
|
|
32
|
+
* sent nor lost silently.
|
|
33
|
+
*/
|
|
34
|
+
propose(input: {
|
|
35
|
+
readonly principal: SessionAuthContext | null | undefined;
|
|
36
|
+
readonly sessionId: string;
|
|
37
|
+
readonly turnId: string;
|
|
38
|
+
readonly partId: string;
|
|
39
|
+
readonly text: string;
|
|
40
|
+
/** A reply to mail: whom it goes to and copies. */
|
|
41
|
+
readonly recipients?: {
|
|
42
|
+
readonly to: ReadonlyArray<string>;
|
|
43
|
+
readonly cc: ReadonlyArray<string>;
|
|
44
|
+
};
|
|
45
|
+
/** The files `attachments_send` attached to the turn's reply, and their captions. */
|
|
46
|
+
readonly files?: ReplyFiles;
|
|
47
|
+
/** A workspace channel's `limits.maxLength`: the ontology sends a longer reply in parts. */
|
|
48
|
+
readonly maxLength?: number;
|
|
49
|
+
}): Promise<ProposedOutcome>;
|
|
18
50
|
/**
|
|
19
51
|
* Whether the channel may send `text` now: true for a chat that is not reviewed, or a
|
|
20
52
|
* reply the judge passed; false once the reply is held for review. Throws when it could not be
|
|
@@ -64,6 +96,20 @@ export interface ReplyReview {
|
|
|
64
96
|
};
|
|
65
97
|
}): Promise<void>;
|
|
66
98
|
}
|
|
99
|
+
/** What became of a turn's reply a channel proposed. */
|
|
100
|
+
export type ProposedOutcome = (Exclude<ProposalOutcome, {
|
|
101
|
+
readonly outcome: "unsupported";
|
|
102
|
+
}> & {
|
|
103
|
+
readonly by: "ontology";
|
|
104
|
+
}) | {
|
|
105
|
+
/**
|
|
106
|
+
* @legacy agent.reply-proposals-fallback
|
|
107
|
+
* The ontology predates proposals: the reply was reviewed here, by the session's roles, and
|
|
108
|
+
* the channel sends it itself when `send` is true, as before.
|
|
109
|
+
*/
|
|
110
|
+
readonly by: "agent";
|
|
111
|
+
readonly send: boolean;
|
|
112
|
+
};
|
|
67
113
|
export declare function replyReview(options: {
|
|
68
114
|
readonly settings: () => ReviewSettings | undefined;
|
|
69
115
|
readonly decide: ReviewDecideFunction;
|
|
@@ -71,6 +117,11 @@ export declare function replyReview(options: {
|
|
|
71
117
|
readonly model: () => string;
|
|
72
118
|
readonly archive: ChatArchive;
|
|
73
119
|
readonly hold: HoldReply;
|
|
120
|
+
/**
|
|
121
|
+
* Proposes a turn's reply to the ontology, which sends or holds it; without it, the channels
|
|
122
|
+
* review their replies here, as before proposals.
|
|
123
|
+
*/
|
|
124
|
+
readonly propose?: ProposeReply;
|
|
74
125
|
/**
|
|
75
126
|
* Sends a passed reply's files in the chat; without it a reply with files is always held, so
|
|
76
127
|
* they are never lost or sent unreviewed.
|
|
@@ -121,6 +172,8 @@ export interface ChannelReviewOptions {
|
|
|
121
172
|
readonly model: () => string;
|
|
122
173
|
/** Holds a reply for review; tests pass a fake. */
|
|
123
174
|
readonly hold?: HoldReply;
|
|
175
|
+
/** Proposes a turn's reply to the ontology; tests pass a fake. */
|
|
176
|
+
readonly propose?: ProposeReply;
|
|
124
177
|
/** The chat archive; tests pass a fake. */
|
|
125
178
|
readonly archive?: ChatArchive;
|
|
126
179
|
/** Sends a passed reply's files; without it such a reply is held. */
|