@segmentstream/ontology 0.161.0 → 0.163.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 +41 -0
- package/dist/agent-draft.d.ts +21 -0
- package/dist/agent-draft.js +57 -1
- package/dist/deployment.d.ts +13 -1
- package/dist/deployment.js +8 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.js +7 -6
- package/dist/manifest.js +1 -0
- package/dist/runtime/agent-web.js +42 -3
- package/dist/runtime/agents.js +6 -1
- package/dist/service/agent/authentication.js +9 -2
- package/dist/service/agent/eval-tokens.d.ts +25 -0
- package/dist/service/agent/eval-tokens.js +130 -0
- package/dist/service/agent/protocol.d.ts +20 -5
- package/dist/service/agent/protocol.js +25 -3
- package/dist/service/agent/server.d.ts +11 -0
- package/dist/service/agent/server.js +114 -5
- package/dist/service/agent/web-tokens.d.ts +13 -2
- package/dist/service/agent/web-tokens.js +20 -5
- package/dist/service/authentication.js +3 -2
- package/dist/service/chat/archive.d.ts +15 -1
- package/dist/service/chat/draft-files.d.ts +56 -0
- package/dist/service/chat/draft-files.js +137 -0
- package/dist/service/chat/idempotency.d.ts +6 -1
- package/dist/service/chat/idempotency.js +7 -1
- package/dist/service/chat/providers/custom.js +12 -2
- package/dist/service/chat/providers/email.d.ts +2 -1
- package/dist/service/chat/providers/email.js +48 -4
- package/dist/service/chat/providers/provider.d.ts +49 -1
- package/dist/service/chat/providers/provider.js +18 -1
- package/dist/service/chat/review-delivery.d.ts +2 -0
- package/dist/service/chat/review-delivery.js +73 -4
- package/dist/service/chat/store-delivery.js +13 -0
- package/dist/service/config.js +8 -2
- package/dist/service/runtime-contract.d.ts +33 -0
- package/dist/service/runtime-identity.d.ts +17 -1
- package/dist/service/runtime-identity.js +15 -2
- package/dist/service/service.js +64 -11
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
|
|
3
|
+
import { renderWorkspaceChannelText, WORKSPACE_CHANNEL_ID, WORKSPACE_CHANNEL_FILES_BYTES, WORKSPACE_CHANNEL_TEXT_LIMIT, workspaceChannelFiles, workspaceChannelSendRoute, } from "../../../workspace-channel.js";
|
|
3
4
|
import { record } from "./provider.js";
|
|
4
5
|
/** How long the agent has to send one message through a workspace channel. */
|
|
5
6
|
const AGENT_SEND_TIMEOUT_MS = 30_000;
|
|
@@ -15,7 +16,11 @@ const ID = WORKSPACE_CHANNEL_ID;
|
|
|
15
16
|
*/
|
|
16
17
|
export function sendThroughAgent(input) {
|
|
17
18
|
return Effect.gen(function* () {
|
|
18
|
-
const
|
|
19
|
+
const files = input.files ?? [];
|
|
20
|
+
const payload = {
|
|
21
|
+
text: input.finalText,
|
|
22
|
+
...(files.length === 0 ? {} : { files: files.map((file) => file.filename) }),
|
|
23
|
+
};
|
|
19
24
|
const agent = input.agent;
|
|
20
25
|
if (agent?.origin === undefined || agent.identityToken === undefined) {
|
|
21
26
|
return {
|
|
@@ -35,6 +40,9 @@ export function sendThroughAgent(input) {
|
|
|
35
40
|
thread_id: input.threadId ?? null,
|
|
36
41
|
reply_to: input.replyTo,
|
|
37
42
|
text: input.finalText,
|
|
43
|
+
// Every agent with this route reads `files`, and refuses them with `files_unsupported`
|
|
44
|
+
// when the channel's code takes none: nothing is sent then.
|
|
45
|
+
...(files.length === 0 ? {} : { files: workspaceChannelFiles(files) }),
|
|
38
46
|
};
|
|
39
47
|
const answered = yield* Effect.tryPromise(async () => {
|
|
40
48
|
const response = await agent.fetch(new Request(new URL(workspaceChannelSendRoute(input.destination.provider), agent.origin), {
|
|
@@ -99,5 +107,7 @@ export const customProvider = {
|
|
|
99
107
|
return { finalText, plainText: finalText };
|
|
100
108
|
},
|
|
101
109
|
payload: (finalText) => ({ text: finalText }),
|
|
110
|
+
// The files go in the one message the channel's code sends, within the contract's bound.
|
|
111
|
+
files: { count: AGENT_DRAFT_FILES_LIMIT, bytes: WORKSPACE_CHANNEL_FILES_BYTES },
|
|
102
112
|
send: sendThroughAgent,
|
|
103
113
|
};
|
|
@@ -8,7 +8,8 @@ import { type ChatProvider, type ChatSend, type ProviderOutcome } from "./provid
|
|
|
8
8
|
* with the thread id, never retried. A reply sends no attribution and names no sender, so it
|
|
9
9
|
* leaves from the mailbox's own address. It carries `Auto-Submitted: auto-replied` and
|
|
10
10
|
* `X-SegmentStream-Reply: review-<review id>`, as the email channel's replies carry their own
|
|
11
|
-
* keys.
|
|
11
|
+
* keys. Files go after the text in the same message, uploaded in chunks when one request cannot
|
|
12
|
+
* carry it (`sendGmailMessage`). `sent` means Gmail accepted it; the payload keeps whom it went to.
|
|
12
13
|
*/
|
|
13
14
|
export declare function sendEmail(input: ChatSend): Effect.Effect<ProviderOutcome>;
|
|
14
15
|
/**
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import { AGENT_DRAFT_FILES_LIMIT } from "../../../agent-draft.js";
|
|
3
|
+
import { base64Url, EMAIL_FILES_BYTES, EMAIL_MESSAGE_LIMIT, emailReplyMessage, GmailError, renderEmailHtml, sendGmailMessage, } from "../../../email.js";
|
|
3
4
|
import { EMAIL_PATTERN } from "../../../property.js";
|
|
4
|
-
import { record, transportFailure, } from "./provider.js";
|
|
5
|
+
import { channelRequest, definiteTransportFailure, record, transportFailure, } from "./provider.js";
|
|
5
6
|
/** A Gmail id, of a thread or a message. */
|
|
6
7
|
const GMAIL_ID = /^[A-Za-z0-9_-]{1,512}$/;
|
|
7
8
|
const decoder = new TextDecoder();
|
|
@@ -33,7 +34,8 @@ function headersOf(message) {
|
|
|
33
34
|
* with the thread id, never retried. A reply sends no attribution and names no sender, so it
|
|
34
35
|
* leaves from the mailbox's own address. It carries `Auto-Submitted: auto-replied` and
|
|
35
36
|
* `X-SegmentStream-Reply: review-<review id>`, as the email channel's replies carry their own
|
|
36
|
-
* keys.
|
|
37
|
+
* keys. Files go after the text in the same message, uploaded in chunks when one request cannot
|
|
38
|
+
* carry it (`sendGmailMessage`). `sent` means Gmail accepted it; the payload keeps whom it went to.
|
|
37
39
|
*/
|
|
38
40
|
export function sendEmail(input) {
|
|
39
41
|
return Effect.gen(function* () {
|
|
@@ -75,6 +77,7 @@ export function sendEmail(input) {
|
|
|
75
77
|
const key = `review-${input.destination.id}`;
|
|
76
78
|
const to = input.recipients?.to ?? [input.destination.external_id];
|
|
77
79
|
const cc = input.recipients?.cc ?? [];
|
|
80
|
+
const files = input.files ?? [];
|
|
78
81
|
const raw = emailReplyMessage({
|
|
79
82
|
to,
|
|
80
83
|
...(cc.length === 0 ? {} : { cc }),
|
|
@@ -85,8 +88,47 @@ export function sendEmail(input) {
|
|
|
85
88
|
html: input.finalText,
|
|
86
89
|
autoSubmitted: true,
|
|
87
90
|
...(/^[A-Za-z0-9_-]{1,128}$/.test(key) ? { replyKey: key } : {}),
|
|
91
|
+
...(files.length === 0 ? {} : { attachments: files }),
|
|
88
92
|
});
|
|
89
|
-
const payload = {
|
|
93
|
+
const payload = {
|
|
94
|
+
threadId,
|
|
95
|
+
to,
|
|
96
|
+
cc,
|
|
97
|
+
html: input.finalText,
|
|
98
|
+
text: input.plainText,
|
|
99
|
+
...(files.length === 0 ? {} : { files: files.map((file) => file.filename) }),
|
|
100
|
+
};
|
|
101
|
+
// A reply with files is one message too, uploaded when one request cannot carry it.
|
|
102
|
+
if (files.length > 0) {
|
|
103
|
+
return yield* Effect.promise(() => sendGmailMessage({
|
|
104
|
+
request: channelRequest(input.transport, input.connection),
|
|
105
|
+
message: new TextEncoder().encode(raw),
|
|
106
|
+
threadId,
|
|
107
|
+
}).then((answer) => typeof answer.id === "string" && GMAIL_ID.test(answer.id)
|
|
108
|
+
? { payload, status: "sent", messageId: answer.id, url: null }
|
|
109
|
+
: {
|
|
110
|
+
payload,
|
|
111
|
+
status: "uncertain",
|
|
112
|
+
messageId: null,
|
|
113
|
+
url: null,
|
|
114
|
+
cause: new Error("Gmail accepted the reply without its id."),
|
|
115
|
+
}, (cause) => ({
|
|
116
|
+
payload,
|
|
117
|
+
// Gmail's refusal, or a proxy that never reached Gmail, sent nothing; anything else
|
|
118
|
+
// may have, such as an upload whose last answer was lost.
|
|
119
|
+
status: (cause instanceof GmailError &&
|
|
120
|
+
cause.status >= 400 &&
|
|
121
|
+
cause.status < 500 &&
|
|
122
|
+
cause.status !== 408 &&
|
|
123
|
+
cause.status !== 429) ||
|
|
124
|
+
definiteTransportFailure(cause)
|
|
125
|
+
? "failed"
|
|
126
|
+
: "uncertain",
|
|
127
|
+
messageId: null,
|
|
128
|
+
url: null,
|
|
129
|
+
cause,
|
|
130
|
+
})));
|
|
131
|
+
}
|
|
90
132
|
const result = yield* input.transport
|
|
91
133
|
.request({
|
|
92
134
|
connection: input.connection,
|
|
@@ -140,5 +182,7 @@ export const emailProvider = {
|
|
|
140
182
|
return { finalText: renderEmailHtml(text), plainText: text };
|
|
141
183
|
},
|
|
142
184
|
payload: (finalText) => ({ html: finalText }),
|
|
185
|
+
// The files go in the reply's one message, under the bound every email sender keeps.
|
|
186
|
+
files: { count: AGENT_DRAFT_FILES_LIMIT, bytes: EMAIL_FILES_BYTES },
|
|
143
187
|
send: sendEmail,
|
|
144
188
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Effect, type Exit } from "effect";
|
|
2
|
+
import type { ChannelRequest } from "../../../channel-files.js";
|
|
2
3
|
import type { ChatButtonRows } from "../../../chat.js";
|
|
3
4
|
import type { TelegramPreviewContext } from "../../../telegram.js";
|
|
4
5
|
import { ConnectionError, type ConnectionReply, type ConnectionTransport } from "../../../connection.js";
|
|
@@ -15,7 +16,29 @@ export interface ProviderOutcome {
|
|
|
15
16
|
readonly cause?: unknown;
|
|
16
17
|
/** Why the provider refused a `failed` message, when it says: see `ActionRejected.refusal`. */
|
|
17
18
|
readonly refusal?: ChatRefusal;
|
|
19
|
+
/**
|
|
20
|
+
* What became of each file of a send with files, by its index, when the provider sent them in
|
|
21
|
+
* several messages: one that reached the person, one that did not, or one that may have.
|
|
22
|
+
*/
|
|
23
|
+
readonly files?: ReadonlyArray<{
|
|
24
|
+
readonly index: number;
|
|
25
|
+
readonly status: "sent" | "uncertain" | "failed";
|
|
26
|
+
readonly messageId: string | null;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
/** A record file a send carries after its text, read before anything is sent. */
|
|
30
|
+
export interface ChatSendFile {
|
|
31
|
+
readonly filename: string;
|
|
32
|
+
readonly contentType: string;
|
|
33
|
+
readonly bytes: Uint8Array;
|
|
18
34
|
}
|
|
35
|
+
/** How many files one message of a provider carries, and how many bytes of them together. */
|
|
36
|
+
export interface ChatFileLimits {
|
|
37
|
+
readonly count: number;
|
|
38
|
+
readonly bytes: number;
|
|
39
|
+
}
|
|
40
|
+
/** Why a provider cannot send a message's files, before its key is reserved. */
|
|
41
|
+
export type ChatFilesRefusal = "files_unsupported" | "too_many_files" | "file_too_large" | "file_unavailable";
|
|
19
42
|
/** Why a chat's provider refused a message, for code to act on. */
|
|
20
43
|
export type ChatRefusal = "outside_reply_window" | "recipient_unreachable" | "recipient_opted_out";
|
|
21
44
|
/**
|
|
@@ -60,6 +83,11 @@ export interface ChatSend {
|
|
|
60
83
|
* a workspace channel's send receives it, others ignore it.
|
|
61
84
|
*/
|
|
62
85
|
readonly chatId?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Files the message carries after its text, in order, for a provider that declares `files`;
|
|
88
|
+
* the text may be empty with them.
|
|
89
|
+
*/
|
|
90
|
+
readonly files?: ReadonlyArray<ChatSendFile>;
|
|
63
91
|
/**
|
|
64
92
|
* Whom a reply to mail goes to, in `To` and `Cc`, in place of the destination alone; others
|
|
65
93
|
* ignore it.
|
|
@@ -111,6 +139,19 @@ export interface ChatProvider {
|
|
|
111
139
|
* provider that shows no buttons, which refuses them all.
|
|
112
140
|
*/
|
|
113
141
|
readonly refuseButtons?: (rows: ChatButtonRows, finalText: string) => string | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* The files one message carries: absent for a provider that sends none, which refuses every
|
|
144
|
+
* file with `files_unsupported`.
|
|
145
|
+
*/
|
|
146
|
+
readonly files?: ChatFileLimits;
|
|
147
|
+
/**
|
|
148
|
+
* Why the provider cannot send files of these types and sizes beyond its `files` limits, or
|
|
149
|
+
* undefined when it can. It is checked before the message is reserved, as `refuseButtons` is.
|
|
150
|
+
*/
|
|
151
|
+
readonly refuseFiles?: (files: ReadonlyArray<{
|
|
152
|
+
readonly contentType: string;
|
|
153
|
+
readonly size: number;
|
|
154
|
+
}>) => ChatFilesRefusal | undefined;
|
|
114
155
|
/** One attempt: `sent` only when the provider's answer proves it. */
|
|
115
156
|
readonly send: (input: ChatSend) => Effect.Effect<ProviderOutcome>;
|
|
116
157
|
/**
|
|
@@ -130,6 +171,13 @@ export interface ChatProvider {
|
|
|
130
171
|
* before the provider saw it, otherwise `uncertain`, since the provider may have acted on it.
|
|
131
172
|
*/
|
|
132
173
|
export declare function transportFailure(payload: ProviderOutcome["payload"], result: Exit.Failure<ConnectionReply, ConnectionError>): ProviderOutcome;
|
|
174
|
+
/** Whether an error of the connection transport proves the request never reached the provider. */
|
|
175
|
+
export declare function definiteTransportFailure(error: unknown): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* The connection transport as the channels' shared senders call it (`ChannelRequest`): one call on
|
|
178
|
+
* `connection`, which rejects with the transport's own error when it did not relay the call.
|
|
179
|
+
*/
|
|
180
|
+
export declare function channelRequest(transport: ConnectionTransport["Service"], connection: string): ChannelRequest;
|
|
133
181
|
export declare const record: (value: unknown) => Record<string, unknown>;
|
|
134
182
|
/**
|
|
135
183
|
* A message's buttons as its history keeps them, one a line, as the person reads them: a link where
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Cause } from "effect";
|
|
1
|
+
import { Cause, Effect } from "effect";
|
|
2
2
|
import { ConnectionError, } from "../../../connection.js";
|
|
3
3
|
/** The connection failures that prove the request never reached the provider. */
|
|
4
4
|
const DEFINITE_CONNECTION_FAILURES = new Set([
|
|
@@ -23,6 +23,23 @@ export function transportFailure(payload, result) {
|
|
|
23
23
|
cause,
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
+
/** Whether an error of the connection transport proves the request never reached the provider. */
|
|
27
|
+
export function definiteTransportFailure(error) {
|
|
28
|
+
return error instanceof ConnectionError && DEFINITE_CONNECTION_FAILURES.has(error.reason);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The connection transport as the channels' shared senders call it (`ChannelRequest`): one call on
|
|
32
|
+
* `connection`, which rejects with the transport's own error when it did not relay the call.
|
|
33
|
+
*/
|
|
34
|
+
export function channelRequest(transport, connection) {
|
|
35
|
+
return async (call) => {
|
|
36
|
+
const exit = await Effect.runPromiseExit(transport.request({ ...call, connection }));
|
|
37
|
+
if (exit._tag === "Failure") {
|
|
38
|
+
throw Cause.squash(exit.cause);
|
|
39
|
+
}
|
|
40
|
+
return exit.value;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
26
43
|
export const record = (value) => value !== null && typeof value === "object" && !Array.isArray(value)
|
|
27
44
|
? value
|
|
28
45
|
: {};
|
|
@@ -36,6 +36,8 @@ export declare function reviewSender(input: {
|
|
|
36
36
|
readonly principal: Principal;
|
|
37
37
|
readonly identityToken: string | undefined;
|
|
38
38
|
readonly requestId: string;
|
|
39
|
+
/** Reads a file's bytes by its download grant; `fetch` by default. */
|
|
40
|
+
readonly fetch?: typeof fetch;
|
|
39
41
|
}): (send: ReviewSendInput) => Effect.Effect<{
|
|
40
42
|
recipients?: EmailRecipients | undefined;
|
|
41
43
|
message?: string | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
-
import { addressesOfDraft, AGENT_DRAFT_TEXT_LIMIT, participantsOfDraft, } from "../../agent-draft.js";
|
|
2
|
+
import { addressesOfDraft, AGENT_DRAFT_TEXT_LIMIT, draftFilesOf, participantsOfDraft, } from "../../agent-draft.js";
|
|
3
3
|
import { EMAIL_RECIPIENTS_LIMIT, emailRecipients } from "../../email.js";
|
|
4
4
|
import { ActionRejected, PermissionDenied } from "../../errors.js";
|
|
5
5
|
import { effectiveScope } from "../../permissions.js";
|
|
@@ -9,6 +9,7 @@ import { WORKSPACE_CHANNEL_KIND } from "../../chat-channel.js";
|
|
|
9
9
|
import { chatRetentionDaysOf } from "../../manifest.js";
|
|
10
10
|
import { channelProvider, chatConnection, chatKind } from "./providers/index.js";
|
|
11
11
|
import { storeDeliveryArchive } from "./store-delivery.js";
|
|
12
|
+
import { readDraftFiles, refuseChatFiles } from "./draft-files.js";
|
|
12
13
|
/**
|
|
13
14
|
* Whether a reply to mail goes to the people a review records it went to: its `to`, else its
|
|
14
15
|
* recipient, and its `cc`, whatever their order and case.
|
|
@@ -19,6 +20,20 @@ function sameRecipients(recipients, review) {
|
|
|
19
20
|
return (set(recipients.to) ===
|
|
20
21
|
set(held.length > 0 ? held : typeof review.recipient === "string" ? [review.recipient] : []) && set(recipients.cc) === set(addressesOfDraft(review.cc)));
|
|
21
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* A draft's files the approval cannot send, refused before anything is reserved: the code names
|
|
25
|
+
* why, and the draft still waits.
|
|
26
|
+
*/
|
|
27
|
+
function refused(code, channel) {
|
|
28
|
+
return Effect.fail(new ActionRejected({
|
|
29
|
+
message: code === "files_unsupported"
|
|
30
|
+
? `The ${channel} channel sends no files with an approved reply; nothing was sent.`
|
|
31
|
+
: code === "file_unavailable"
|
|
32
|
+
? "A file of the reply is gone, or its person may no longer read it; nothing was sent."
|
|
33
|
+
: `The reply's files exceed what ${channel} sends in one reply; nothing was sent.`,
|
|
34
|
+
refusal: code,
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
22
37
|
/**
|
|
23
38
|
* Sends a reply the agent held for review, once a member approved it: the draft or the member's
|
|
24
39
|
* edit, to the person's chat through the ontology's chat connection of the channel the chat is on,
|
|
@@ -148,7 +163,8 @@ export function reviewSender(input) {
|
|
|
148
163
|
try: () => provider.render(text, "", ""),
|
|
149
164
|
catch: (cause) => new ActionRejected({ message: "The Markdown could not be rendered.", cause }),
|
|
150
165
|
});
|
|
151
|
-
|
|
166
|
+
const files = draftFilesOf(review.files);
|
|
167
|
+
if ((text.trim() === "" && files.length === 0) ||
|
|
152
168
|
text.length > AGENT_DRAFT_TEXT_LIMIT ||
|
|
153
169
|
finalText.length > provider.limit ||
|
|
154
170
|
plainText.length > provider.limit ||
|
|
@@ -160,6 +176,11 @@ export function reviewSender(input) {
|
|
|
160
176
|
"sent or truncated.",
|
|
161
177
|
});
|
|
162
178
|
}
|
|
179
|
+
// The files the channel cannot send are refused before anything is reserved: no claim spent.
|
|
180
|
+
const filesRefusal = refuseChatFiles(provider, files);
|
|
181
|
+
if (filesRefusal !== undefined) {
|
|
182
|
+
return yield* refused(filesRefusal, providerName);
|
|
183
|
+
}
|
|
163
184
|
// A guest is no user; `agent_draft` is not theirs to approve.
|
|
164
185
|
if (principal.kind === "guest") {
|
|
165
186
|
return yield* new PermissionDenied({ message: "The approver is unknown." });
|
|
@@ -223,7 +244,8 @@ export function reviewSender(input) {
|
|
|
223
244
|
recipients.to[0] === externalId.toLowerCase())
|
|
224
245
|
? recipients
|
|
225
246
|
: undefined;
|
|
226
|
-
const
|
|
247
|
+
const bindings = files.map((file) => [file.entity, file.id, file.property, file.revisionId]);
|
|
248
|
+
const fingerprint = deliveryFingerprint(preview, requester, undefined, undefined, bound, bindings.length === 0 ? undefined : bindings);
|
|
227
249
|
const event = {
|
|
228
250
|
eventId: `chat-delivery:${operationId}:reserve`,
|
|
229
251
|
operationId,
|
|
@@ -253,7 +275,15 @@ export function reviewSender(input) {
|
|
|
253
275
|
toolName: null,
|
|
254
276
|
argsSummary: JSON.stringify(provider.payload(finalText)),
|
|
255
277
|
status: "requested",
|
|
256
|
-
|
|
278
|
+
// The files as the chat's history shows them, as the agent's own sends record them.
|
|
279
|
+
attachments: files.map((file, index) => ({
|
|
280
|
+
id: `chat-delivery:${operationId}:file:${index}`,
|
|
281
|
+
kind: "file",
|
|
282
|
+
filename: file.filename,
|
|
283
|
+
contentType: file.contentType,
|
|
284
|
+
size: file.size,
|
|
285
|
+
})),
|
|
286
|
+
...(bindings.length === 0 ? {} : { files: bindings }),
|
|
257
287
|
requestedBy: requester,
|
|
258
288
|
url: null,
|
|
259
289
|
at: new Date().toISOString(),
|
|
@@ -265,6 +295,44 @@ export function reviewSender(input) {
|
|
|
265
295
|
const archive = onStore
|
|
266
296
|
? storeDeliveryArchive(store, chatRetentionDaysOf(input.ontology.manifest))
|
|
267
297
|
: input.cloud;
|
|
298
|
+
// Files go only through the workspace chat store: the archive in Cloud is not extended.
|
|
299
|
+
// @legacy agent.cloud-chat-archive
|
|
300
|
+
if (files.length > 0 && !onStore) {
|
|
301
|
+
return yield* refused("files_unsupported", providerName);
|
|
302
|
+
}
|
|
303
|
+
// A replay of an approval that sent its files answers that delivery without reading them
|
|
304
|
+
// again: they may since be gone, which must not refuse what was already sent.
|
|
305
|
+
const earlier = files.length === 0 || archive.find === undefined
|
|
306
|
+
? undefined
|
|
307
|
+
: yield* archive
|
|
308
|
+
.find({ delivery: { scope, key, fingerprint }, identityToken: input.identityToken })
|
|
309
|
+
.pipe(Effect.tapError((error) => report("find", error)), Effect.mapError((cause) => new ActionRejected({
|
|
310
|
+
cause,
|
|
311
|
+
message: cause.diagnostic === "chat_archive_conflict"
|
|
312
|
+
? "The reply was already approved by another member, with another " +
|
|
313
|
+
"text or to other people; it is never sent again."
|
|
314
|
+
: "The delivery could not be read, and nothing was sent. Approve again.",
|
|
315
|
+
})));
|
|
316
|
+
if (earlier !== undefined) {
|
|
317
|
+
return {
|
|
318
|
+
status: earlier.status,
|
|
319
|
+
text,
|
|
320
|
+
messageId: earlier.messageId,
|
|
321
|
+
duplicate: true,
|
|
322
|
+
...(recipients === undefined ? {} : { recipients }),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
// Each file as its person may read it now, the revision held: everything that can fail is
|
|
326
|
+
// read before the claim, so a refusal spends none.
|
|
327
|
+
const sendFiles = yield* readDraftFiles({
|
|
328
|
+
backend,
|
|
329
|
+
person,
|
|
330
|
+
files,
|
|
331
|
+
fetch: input.fetch ?? fetch,
|
|
332
|
+
});
|
|
333
|
+
if (sendFiles === undefined) {
|
|
334
|
+
return yield* refused("file_unavailable", providerName);
|
|
335
|
+
}
|
|
268
336
|
const reservation = archive({
|
|
269
337
|
event,
|
|
270
338
|
delivery: { phase: "reserve", scope, key, fingerprint },
|
|
@@ -376,6 +444,7 @@ export function reviewSender(input) {
|
|
|
376
444
|
threadId,
|
|
377
445
|
operationId,
|
|
378
446
|
chatId,
|
|
447
|
+
...(sendFiles.length === 0 ? {} : { files: sendFiles }),
|
|
379
448
|
...(recipients === undefined ? {} : { recipients }),
|
|
380
449
|
...(input.agent === undefined ? {} : { agent: input.agent }),
|
|
381
450
|
...(input.previews === undefined ? {} : { previews: input.previews }),
|
|
@@ -52,6 +52,8 @@ function payloadFingerprint(event) {
|
|
|
52
52
|
requester.clientName,
|
|
53
53
|
requester.attribution,
|
|
54
54
|
],
|
|
55
|
+
// A message's files bind it only when it has some, so a text's binding stays.
|
|
56
|
+
...(event.files === undefined || event.files.length === 0 ? [] : [{ files: event.files }]),
|
|
55
57
|
]);
|
|
56
58
|
}
|
|
57
59
|
function object(value) {
|
|
@@ -169,6 +171,17 @@ export function storeDeliveryArchive(store, retentionDays) {
|
|
|
169
171
|
: {}),
|
|
170
172
|
...(event.replyToMessageId === null ? {} : { replyTo: event.replyToMessageId }),
|
|
171
173
|
text: event.text,
|
|
174
|
+
// A message's files as its history shows them, as the agent records the files it sends.
|
|
175
|
+
...(event.attachments.length === 0
|
|
176
|
+
? {}
|
|
177
|
+
: {
|
|
178
|
+
files: event.attachments.map((file) => ({
|
|
179
|
+
id: file.id,
|
|
180
|
+
name: file.filename,
|
|
181
|
+
type: file.contentType,
|
|
182
|
+
size: file.size,
|
|
183
|
+
})),
|
|
184
|
+
}),
|
|
172
185
|
at: event.at,
|
|
173
186
|
// Before the provider is called: the receipt settles it, a lost one leaves it unsent.
|
|
174
187
|
status: "pending",
|
package/dist/service/config.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { telegramPreviewEnvironment } from "../telegram.js";
|
|
2
2
|
import { Redacted, Schema } from "effect";
|
|
3
3
|
import { BlobStorageBinding } from "../storage/provider.js";
|
|
4
|
-
import { deploymentFromEnv } from "../deployment.js";
|
|
4
|
+
import { deploymentFromEnv, previewRunsAgent } from "../deployment.js";
|
|
5
5
|
import { LocalConnectionContextError, readLocalConnectionContext, RUNTIME_CONTEXT_FD, } from "./connections.js";
|
|
6
6
|
/** The environment of the workspace deployment's ontology service. */
|
|
7
7
|
export const ENVIRONMENT_VARIABLES = {
|
|
@@ -170,7 +170,13 @@ export function configFromEnv(environment = process.env) {
|
|
|
170
170
|
message: "OIDC requires both a public client ID and an HTTPS issuer.",
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
|
-
|
|
173
|
+
// A hosted preview trusts its own functions' tokens, which name `preview`, only when the platform
|
|
174
|
+
// runs the agent there; a local session has no functions.
|
|
175
|
+
const runtimeIdentity = deployment.target === "preview"
|
|
176
|
+
? previewRunsAgent(deployment)
|
|
177
|
+
? { ...runtimeIdentityFromEnv(read), environment: "preview" }
|
|
178
|
+
: undefined
|
|
179
|
+
: runtimeIdentityFromEnv(read);
|
|
174
180
|
const localConnections = deployment.target === "preview" ? undefined : localConnectionsFromEnv(environment);
|
|
175
181
|
let blobStorage;
|
|
176
182
|
if (read("SEGMENTSTREAM_ONTOLOGY_STORAGE")) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AgentDraftFile } from "../agent-draft.js";
|
|
1
2
|
import type { ActionRuntime } from "../runtime-source.js";
|
|
2
3
|
import type { ConfirmationStore } from "../runtime/confirmations.js";
|
|
3
4
|
import { Context, Schema, type Effect, type Layer, type Redacted } from "effect";
|
|
@@ -264,7 +265,22 @@ export interface AgentWebStore {
|
|
|
264
265
|
readonly grantMember: (input: {
|
|
265
266
|
readonly grantId: string;
|
|
266
267
|
readonly userId: string;
|
|
268
|
+
/**
|
|
269
|
+
* Whether an `agent-eval` grant counts too: only in a preview that runs the agent, where evals
|
|
270
|
+
* act as a test member. Never in production.
|
|
271
|
+
*/
|
|
272
|
+
readonly evalGrants?: boolean;
|
|
267
273
|
}) => Effect.Effect<Principal | undefined>;
|
|
274
|
+
/**
|
|
275
|
+
* In a preview that runs the agent: an access token of a new `agent-eval` grant to the agent at
|
|
276
|
+
* `resource`, for the active member with `email`, revoking the member's earlier `agent-eval`
|
|
277
|
+
* grants. No refresh token. Absent where the store cannot issue them.
|
|
278
|
+
*/
|
|
279
|
+
readonly issueEval?: (input: {
|
|
280
|
+
readonly email: string;
|
|
281
|
+
readonly resource: string;
|
|
282
|
+
readonly expiresAt: Date;
|
|
283
|
+
}) => Effect.Effect<EvalIssue>;
|
|
268
284
|
/**
|
|
269
285
|
* Counts one turn a member starts on the web against the agent's limits when `signUp` created
|
|
270
286
|
* them, as their turns on a messenger are counted, on the same counters; invited and paired
|
|
@@ -384,6 +400,21 @@ export type VisitorSignUp = {
|
|
|
384
400
|
} | {
|
|
385
401
|
readonly status: "refused";
|
|
386
402
|
};
|
|
403
|
+
/** An eval session's credential, as {@link AgentWebStore.issueEval} answers it. */
|
|
404
|
+
export type EvalIssue = {
|
|
405
|
+
readonly status: "issued";
|
|
406
|
+
readonly token: string;
|
|
407
|
+
readonly grantId: string;
|
|
408
|
+
readonly memberId: string;
|
|
409
|
+
}
|
|
410
|
+
/** No member has the address. */
|
|
411
|
+
| {
|
|
412
|
+
readonly status: "not_member";
|
|
413
|
+
}
|
|
414
|
+
/** The member is deactivated or blocked. */
|
|
415
|
+
| {
|
|
416
|
+
readonly status: "inactive";
|
|
417
|
+
};
|
|
387
418
|
/** A guest's credential, as {@link AgentWebStore.guest} answers it. */
|
|
388
419
|
export type GuestIssue = {
|
|
389
420
|
readonly status: "issued";
|
|
@@ -809,6 +840,8 @@ export interface HeldReplyInput {
|
|
|
809
840
|
readonly memberId: string;
|
|
810
841
|
readonly sessionId: string;
|
|
811
842
|
readonly text: string;
|
|
843
|
+
/** Record files the reply carries, as the person could read them when it was held. */
|
|
844
|
+
readonly files?: ReadonlyArray<AgentDraftFile>;
|
|
812
845
|
readonly mode: "always" | "whenUnsure";
|
|
813
846
|
readonly confidence: number | null;
|
|
814
847
|
/** The agent's turn that wrote the reply; absent from agents that predate it. */
|
|
@@ -30,6 +30,14 @@ export declare const CHAT: RegExp;
|
|
|
30
30
|
export declare const SESSION: RegExp;
|
|
31
31
|
/** The chat and session a request names, or `undefined` without a well-formed chat. */
|
|
32
32
|
export declare function requestChat(request: HttpServerRequest.HttpServerRequest): ChatProvenance | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* The header the agent of a preview names its own deployment in, as the ontology's own calls to
|
|
35
|
+
* the platform do. Vercel's function tokens name a project and its environment, never a
|
|
36
|
+
* deployment, and every preview of a workspace shares one hosting project, so a preview's ontology
|
|
37
|
+
* checks the header against its own deployment before it trusts a runtime token: an agent of
|
|
38
|
+
* another preview of the same workspace is refused rather than served another branch's data.
|
|
39
|
+
*/
|
|
40
|
+
export declare const ONTOLOGY_DEPLOYMENT_HEADER = "x-segmentstream-ontology-deployment-id";
|
|
33
41
|
/**
|
|
34
42
|
* Whom the service trusts as its runtime caller: Vercel OIDC tokens of the functions of this
|
|
35
43
|
* deployment's own project. The app's agent is a service of the same Vercel project as the ontology
|
|
@@ -42,6 +50,11 @@ export interface RuntimeIdentityConfig {
|
|
|
42
50
|
readonly audience: string;
|
|
43
51
|
/** The project the token must name, from `VERCEL_PROJECT_ID`. */
|
|
44
52
|
readonly projectId: string;
|
|
53
|
+
/**
|
|
54
|
+
* The environment the token must name: `production`, unless the platform runs the agent in a
|
|
55
|
+
* hosted preview, whose functions' tokens name `preview`.
|
|
56
|
+
*/
|
|
57
|
+
readonly environment?: "production" | "preview";
|
|
45
58
|
}
|
|
46
59
|
export type RuntimeVerification = {
|
|
47
60
|
readonly status: "valid";
|
|
@@ -61,10 +74,13 @@ export type RuntimeVerification = {
|
|
|
61
74
|
export type RuntimeTokenVerifier = (token: string) => Promise<RuntimeVerification>;
|
|
62
75
|
/** The issuer's key set, at `<issuer>/.well-known/jwks` as Vercel publishes it. */
|
|
63
76
|
export declare function runtimeKeySet(issuer: string): JWTVerifyGetKey;
|
|
77
|
+
/** Whether a bearer token claims the runtime issuer, read without verifying it. */
|
|
78
|
+
export declare function claimsRuntimeIssuer(config: RuntimeIdentityConfig, token: string): boolean;
|
|
64
79
|
/**
|
|
65
80
|
* Verifies the Vercel OIDC token of a function of this deployment's project, as backend-next
|
|
66
81
|
* verifies the ontology service's own: RS256 from the team's issuer, for the team's audience,
|
|
67
|
-
* unexpired, with `project_id` equal to the configured project and `environment`
|
|
82
|
+
* unexpired, with `project_id` equal to the configured project and `environment` the configured
|
|
83
|
+
* one, `production` unless set.
|
|
68
84
|
* The issuer and the audience name the team, so no further owner check is needed. A token that
|
|
69
85
|
* claims another issuer is `other`, without a key-set fetch.
|
|
70
86
|
*/
|
|
@@ -44,6 +44,14 @@ const CLOCK_TOLERANCE_SECONDS = 5;
|
|
|
44
44
|
const JWKS_TIMEOUT_MS = 5_000;
|
|
45
45
|
/** The environment that a Vercel function token must name: channels run only in production. */
|
|
46
46
|
const PRODUCTION = "production";
|
|
47
|
+
/**
|
|
48
|
+
* The header the agent of a preview names its own deployment in, as the ontology's own calls to
|
|
49
|
+
* the platform do. Vercel's function tokens name a project and its environment, never a
|
|
50
|
+
* deployment, and every preview of a workspace shares one hosting project, so a preview's ontology
|
|
51
|
+
* checks the header against its own deployment before it trusts a runtime token: an agent of
|
|
52
|
+
* another preview of the same workspace is refused rather than served another branch's data.
|
|
53
|
+
*/
|
|
54
|
+
export const ONTOLOGY_DEPLOYMENT_HEADER = "x-segmentstream-ontology-deployment-id";
|
|
47
55
|
/** The issuer without a trailing slash, as Vercel writes it in `iss` and as its JWKS URL starts. */
|
|
48
56
|
function normalizedIssuer(issuer) {
|
|
49
57
|
let end = issuer.length;
|
|
@@ -83,10 +91,15 @@ function claimedIssuer(token) {
|
|
|
83
91
|
return undefined;
|
|
84
92
|
}
|
|
85
93
|
}
|
|
94
|
+
/** Whether a bearer token claims the runtime issuer, read without verifying it. */
|
|
95
|
+
export function claimsRuntimeIssuer(config, token) {
|
|
96
|
+
return claimedIssuer(token) === normalizedIssuer(config.issuer);
|
|
97
|
+
}
|
|
86
98
|
/**
|
|
87
99
|
* Verifies the Vercel OIDC token of a function of this deployment's project, as backend-next
|
|
88
100
|
* verifies the ontology service's own: RS256 from the team's issuer, for the team's audience,
|
|
89
|
-
* unexpired, with `project_id` equal to the configured project and `environment`
|
|
101
|
+
* unexpired, with `project_id` equal to the configured project and `environment` the configured
|
|
102
|
+
* one, `production` unless set.
|
|
90
103
|
* The issuer and the audience name the team, so no further owner check is needed. A token that
|
|
91
104
|
* claims another issuer is `other`, without a key-set fetch.
|
|
92
105
|
*/
|
|
@@ -107,7 +120,7 @@ export function makeRuntimeTokenVerifier(config, keySet) {
|
|
|
107
120
|
if (payload.project_id !== config.projectId) {
|
|
108
121
|
return { status: "invalid", reason: "claim:project_id" };
|
|
109
122
|
}
|
|
110
|
-
if (payload.environment !== PRODUCTION) {
|
|
123
|
+
if (payload.environment !== (config.environment ?? PRODUCTION)) {
|
|
111
124
|
return { status: "invalid", reason: "claim:environment" };
|
|
112
125
|
}
|
|
113
126
|
return { status: "valid" };
|