@segmentstream/agent 0.73.2 → 0.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/dist/_chunks/{build-metadata-3mxf7v2b.mjs → build-metadata-CLenocka.mjs} +1 -1
- package/dist/_chunks/{chat-archive-BCPLSZeK.mjs → chat-archive-CDk-FPLX.mjs} +11 -4
- package/dist/_chunks/{chat-archive-DK7uCgff.mjs → chat-archive-DHYplDT5.mjs} +1 -1
- package/dist/extension/config.d.ts +12 -0
- package/dist/extension/config.mjs +21 -2
- package/dist/extension/extension.d.ts +1 -1
- package/dist/extension/hooks/chat-context.mjs +1 -1
- package/dist/extension/hooks/chat-recorder.mjs +1 -1
- package/dist/extension/lib/build-metadata.mjs +1 -1
- package/dist/extension/lib/chat-archive.d.ts +6 -1
- package/dist/extension/lib/chat-archive.mjs +1 -1
- package/dist/extension/lib/chat-context.mjs +1 -1
- package/dist/extension/lib/chat-messages.d.ts +108 -0
- package/dist/extension/lib/chat-messages.mjs +169 -4
- package/dist/extension/lib/chat-recorder.mjs +44 -13
- package/dist/extension/lib/chat-store.d.ts +23 -1
- package/dist/extension/lib/email/channel.d.ts +3 -0
- package/dist/extension/lib/email/channel.mjs +60 -14
- package/dist/extension/lib/group-routing/routing-context.mjs +1 -1
- package/dist/extension/lib/model-service-alert.mjs +1 -1
- package/dist/extension/lib/review/judge.mjs +1 -1
- package/dist/extension/lib/review/reply.mjs +1 -1
- package/dist/extension/lib/review/revision-channel.mjs +1 -1
- package/dist/extension/lib/slack/adapter.mjs +1 -1
- package/dist/extension/lib/telegram/adapter.mjs +3 -3
- package/dist/extension/lib/telegram/chat-archive.mjs +1 -1
- package/dist/extension/lib/telegram/confirmations.mjs +1 -1
- package/dist/extension/lib/telegram/delivery-diagnostics.mjs +1 -1
- package/dist/extension/lib/telegram/health.mjs +1 -1
- package/dist/extension/lib/telegram/inbound.mjs +1 -1
- package/dist/extension/lib/telegram/lifecycle.mjs +1 -1
- package/dist/extension/lib/telegram/store-capture.mjs +1 -1
- package/dist/extension/lib/telegram/turn-errors.mjs +1 -1
- package/dist/extension/lib/whatsapp/adapter.d.ts +21 -1
- package/dist/extension/lib/whatsapp/adapter.mjs +82 -13
- package/dist/extension/lib/whatsapp/attachments-send.mjs +1 -1
- package/dist/extension/lib/whatsapp/changes.d.ts +46 -0
- package/dist/extension/lib/whatsapp/changes.mjs +212 -0
- package/dist/extension/lib/whatsapp/sent.mjs +1 -1
- package/dist/extension/lib/whatsapp/statuses.d.ts +9 -0
- package/dist/extension/lib/whatsapp/statuses.mjs +9 -0
- package/dist/extension/lib/workspace-channel/channel.d.ts +68 -7
- package/dist/extension/lib/workspace-channel/channel.mjs +149 -33
- package/dist/extension/lib/workspace-chat-archive.d.ts +7 -0
- package/dist/extension/lib/workspace-chat-archive.mjs +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,53 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.75.0
|
|
4
|
+
|
|
5
|
+
An email channel can record its whole mailbox in the workspace chat store, beside the mail the agent
|
|
6
|
+
takes: `"record": "all"` in its settings, `"agent"` (the mail the agent takes) by default.
|
|
7
|
+
|
|
8
|
+
- Every message the mailbox receives joins its sender's chat in its thread, but drafts, spam and
|
|
9
|
+
trash, which are never recorded. Mail the agent takes is recorded with its turn, as before; the
|
|
10
|
+
rest starts no turn and is `skipped` with the reason the agent did not take it: `not_addressed`
|
|
11
|
+
for mail the channel's `query` does not find, `bulk`, `automated` or `bounce`, `identity_unknown`
|
|
12
|
+
for a sender the ontology admits no one for, `refused` and `limited`. Recording admits, signs up
|
|
13
|
+
and answers no one.
|
|
14
|
+
- `recordQuery`, a Gmail search allowed with `"record": "all"` only, narrows what is recorded
|
|
15
|
+
beyond the mail the agent takes.
|
|
16
|
+
- Mail that failed DMARC is recorded `skipped` with the reason `unverified`, as no one's. The
|
|
17
|
+
agent's reads of a chat never return a message skipped as `unverified`, so it reaches no session's
|
|
18
|
+
context and no reply's review.
|
|
19
|
+
|
|
20
|
+
An agent whose ontology keeps no chat store records what it always did, and so does an email
|
|
21
|
+
channel without `record`.
|
|
22
|
+
|
|
23
|
+
## 0.74.0
|
|
24
|
+
|
|
25
|
+
A chat keeps what providers report of its messages beside new ones, on an agent whose ontology
|
|
26
|
+
keeps the workspace chat store. No change starts a turn, and a change to a message the chat does
|
|
27
|
+
not hold changes nothing.
|
|
28
|
+
|
|
29
|
+
- A workspace channel's `parse` returns events (`ChannelEvent`): messages as before, which may now
|
|
30
|
+
carry `replyTo` (the message they quote), `at` and `parts` (`ChatPart`: a location or a contact
|
|
31
|
+
card), and changes to earlier messages, each naming its message by the provider's id in
|
|
32
|
+
`target`: `{ type: "reaction", chatId, sender, target, emoji }` (an empty emoji removes the
|
|
33
|
+
sender's), `{ type: "edit", chatId, target, text, at? }`, `{ type: "deletion", chatId, target,
|
|
34
|
+
at? }` and `{ type: "status", chatId, target, status: "delivered" | "read" | "failed", reason? }`.
|
|
35
|
+
A parse that returns plain messages keeps working. A message's `text` may be empty when it has
|
|
36
|
+
files or parts; one whose only content is a part joins the chat and starts no turn. The 50
|
|
37
|
+
messages a delivery or a read may hold count messages only. Webhooks and polls carry both.
|
|
38
|
+
- The channel records each person's message by the provider's id, with what it quotes, its parts,
|
|
39
|
+
its time and the files it stored, so a later reaction, edit or deletion finds it.
|
|
40
|
+
- WhatsApp keeps Meta's delivery statuses on the messages the number sent (`delivered`, `read`, or
|
|
41
|
+
`failed` with Meta's refusal as a code such as `recipient_unreachable`), people's reactions, the
|
|
42
|
+
message a person quotes, a location's place, and a contact card, which joins the chat without a
|
|
43
|
+
turn. On a number in Coexistence with the WhatsApp Business app it keeps a person's edits and
|
|
44
|
+
deletions, and the business's own from the app. Once it kept a delivery's statuses, the channel
|
|
45
|
+
says so to SegmentStream, which then records the failure nowhere else.
|
|
46
|
+
|
|
47
|
+
An agent whose ontology keeps no chat store records none of these and works as before.
|
|
48
|
+
|
|
49
|
+
Pins @segmentstream/ontology 0.150.2.
|
|
50
|
+
|
|
3
51
|
## 0.73.2
|
|
4
52
|
|
|
5
53
|
Pins @segmentstream/ontology 0.150.1.
|
|
@@ -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.75.0";
|
|
8
8
|
function agentBuild(environment = process.env) {
|
|
9
9
|
return {
|
|
10
10
|
agent: version,
|
|
@@ -95,6 +95,7 @@ function metaOf(event) {
|
|
|
95
95
|
}
|
|
96
96
|
const PRIVATE_KEYS = new Set([
|
|
97
97
|
"attachments",
|
|
98
|
+
"delivery",
|
|
98
99
|
"fences",
|
|
99
100
|
"held",
|
|
100
101
|
"previous"
|
|
@@ -124,6 +125,7 @@ function messageInput(event) {
|
|
|
124
125
|
messageId: storedMessageId(event),
|
|
125
126
|
...replyTo === void 0 ? {} : { replyTo },
|
|
126
127
|
...text === void 0 ? {} : { text },
|
|
128
|
+
...event.parts === void 0 || event.parts.length === 0 ? {} : { parts: event.parts.slice(0, 20) },
|
|
127
129
|
at: event.at,
|
|
128
130
|
...event.kind === "outbound" ? { status: storedStatus(event.status) } : {},
|
|
129
131
|
...event.sessionId === null ? {} : { sessionId: event.sessionId.slice(0, BOUNDS.id) },
|
|
@@ -196,6 +198,10 @@ function eventOf(row) {
|
|
|
196
198
|
};
|
|
197
199
|
}
|
|
198
200
|
const CONTEXT_PAGES = 5;
|
|
201
|
+
const UNVERIFIED = "unverified";
|
|
202
|
+
function unverified(row) {
|
|
203
|
+
return row.direction === "inbound" && (object(row.meta)?.["unverified"] === true || row.disposition === "skipped" && row.reason === "unverified");
|
|
204
|
+
}
|
|
199
205
|
function shown(row) {
|
|
200
206
|
return row.direction === "inbound" || row.direction === "outbound" && (row.status === null || [
|
|
201
207
|
"sent",
|
|
@@ -290,7 +296,7 @@ function workspaceChatArchive(store) {
|
|
|
290
296
|
break;
|
|
291
297
|
}
|
|
292
298
|
examined = row;
|
|
293
|
-
if (row.direction !== "internal" && (includeDecisions === true || shown(row)) && (search.senderId === void 0 || row.senderId === search.senderId) && (from === void 0 || Date.parse(row.at) >= from)) kept.push(row);
|
|
299
|
+
if (row.direction !== "internal" && !unverified(row) && (includeDecisions === true || shown(row)) && (search.senderId === void 0 || row.senderId === search.senderId) && (from === void 0 || Date.parse(row.at) >= from)) kept.push(row);
|
|
294
300
|
}
|
|
295
301
|
more = rest || read.nextCursor !== null;
|
|
296
302
|
before = read.nextCursor ?? void 0;
|
|
@@ -309,7 +315,7 @@ function workspaceChatArchive(store) {
|
|
|
309
315
|
limit: Math.min(limit ?? 100, 200)
|
|
310
316
|
}, signal);
|
|
311
317
|
return {
|
|
312
|
-
events: page.messages.filter((row) => row.direction !== "internal" && (row.replacedBy ?? null) === null).map(eventOf),
|
|
318
|
+
events: page.messages.filter((row) => row.direction !== "internal" && !unverified(row) && (row.replacedBy ?? null) === null).map(eventOf),
|
|
313
319
|
nextCursor: page.nextCursor
|
|
314
320
|
};
|
|
315
321
|
},
|
|
@@ -530,7 +536,8 @@ function cloudChatArchive(input = {}) {
|
|
|
530
536
|
}
|
|
531
537
|
return {
|
|
532
538
|
append: async (event) => {
|
|
533
|
-
|
|
539
|
+
const { parts, ...archived } = event;
|
|
540
|
+
return (await call("events", archived)).stored;
|
|
534
541
|
},
|
|
535
542
|
context: async ({ includeDecisions, participantId, signal, ...search }) => await call("search", {
|
|
536
543
|
search,
|
|
@@ -582,4 +589,4 @@ function summarizeArguments(value) {
|
|
|
582
589
|
};
|
|
583
590
|
return JSON.stringify(workflowValue(visit(value, 0), { boundary: "chat arguments" })).slice(0, 4e3);
|
|
584
591
|
}
|
|
585
|
-
export {
|
|
592
|
+
export { storedFiles as C, sendFingerprint as S, workspaceChatArchive as T, cutText as _, chatEvent as a, metaOf as b, forgetChatStores as c, sessionFailureEventId as d, storeOf as f, boundedMeta as g, UNVERIFIED as h, chatAuthor as i, replyEvent as l, topicContext as m, archiveSequence as n, cloudChatArchive as o, summarizeArguments as p, chatArchive as r, collapseChatEvents as s, AGENT_SENDER as t, replyOperation as u, eventOf as v, storedMessageId as w, reasonCode as x, messageInput as y };
|
|
@@ -7,7 +7,7 @@ __eveCreateRequire(import.meta.url);
|
|
|
7
7
|
import { parseArchivedMessage } from "../extension/lib/telegram/archive-message.mjs";
|
|
8
8
|
import { currentChannel, currentKey } from "../extension/lib/channel-scope.mjs";
|
|
9
9
|
import { n as contentText, t as CONTENT_CAPS } from "./content-cDtVnr8Y.mjs";
|
|
10
|
-
import {
|
|
10
|
+
import { C as storedFiles, S as sendFingerprint, _ as cutText, a as chatEvent, b as metaOf, d as sessionFailureEventId, f as storeOf, g as boundedMeta, x as reasonCode, y as messageInput } from "./chat-archive-CDk-FPLX.mjs";
|
|
11
11
|
import { ChatStoreError } from "../extension/lib/chat-store.mjs";
|
|
12
12
|
import { c as currentInboundRequest, ht as currentThreadId, xt as reportTelegramError } from "./principal-Zf42Yf4r.mjs";
|
|
13
13
|
import { currentConfirmationPrompt } from "../extension/lib/group-routing/messages.mjs";
|
|
@@ -308,6 +308,14 @@ export interface EmailChannelInput {
|
|
|
308
308
|
readonly push?: {
|
|
309
309
|
readonly topic: string;
|
|
310
310
|
};
|
|
311
|
+
/**
|
|
312
|
+
* What the workspace chat store keeps of the mailbox: `agent`, the mail the agent takes, when
|
|
313
|
+
* absent; or `all`, every message it receives but drafts, spam and trash, with what the agent did
|
|
314
|
+
* with each.
|
|
315
|
+
*/
|
|
316
|
+
readonly record?: "agent" | "all";
|
|
317
|
+
/** With `record: "all"`, which of that mail is recorded, as Gmail's search; all of it when absent. */
|
|
318
|
+
readonly recordQuery?: string;
|
|
311
319
|
}
|
|
312
320
|
/**
|
|
313
321
|
* A channel the workspace defines in its own code, `defineChannel` in `agent/agent/channels/`,
|
|
@@ -603,6 +611,10 @@ export interface EmailSettings {
|
|
|
603
611
|
readonly push: {
|
|
604
612
|
readonly topic: string;
|
|
605
613
|
} | null;
|
|
614
|
+
/** `all` records every message the mailbox receives; absent, only the mail the agent takes. */
|
|
615
|
+
readonly record?: "agent" | "all";
|
|
616
|
+
/** With `record: "all"`, the Gmail search that narrows what is recorded. */
|
|
617
|
+
readonly recordQuery?: string;
|
|
606
618
|
}
|
|
607
619
|
/**
|
|
608
620
|
* A channel the workspace defines in its own code (`defineChannel`): the connection its code calls
|
|
@@ -401,7 +401,9 @@ function emailSettings(value, issues, path = ["channels", "email"]) {
|
|
|
401
401
|
"kind",
|
|
402
402
|
"query",
|
|
403
403
|
"checkAfterMinutes",
|
|
404
|
-
"push"
|
|
404
|
+
"push",
|
|
405
|
+
"record",
|
|
406
|
+
"recordQuery"
|
|
405
407
|
],
|
|
406
408
|
issues
|
|
407
409
|
});
|
|
@@ -435,12 +437,29 @@ function emailSettings(value, issues, path = ["channels", "email"]) {
|
|
|
435
437
|
"topic"
|
|
436
438
|
]
|
|
437
439
|
});
|
|
440
|
+
const record = email.record ?? "agent";
|
|
441
|
+
if (record !== "agent" && record !== "all") issues.push({
|
|
442
|
+
message: "must be \"agent\" or \"all\"",
|
|
443
|
+
path: [...path, "record"]
|
|
444
|
+
});
|
|
445
|
+
if (email.recordQuery !== void 0 && record !== "all") issues.push({
|
|
446
|
+
message: "applies only with record \"all\"",
|
|
447
|
+
path: [...path, "recordQuery"]
|
|
448
|
+
});
|
|
449
|
+
else if (email.recordQuery !== void 0 && (typeof email.recordQuery !== "string" || email.recordQuery.trim() === "" || email.recordQuery.length > 1e3)) issues.push({
|
|
450
|
+
message: "must be a Gmail search of at most 1000 characters",
|
|
451
|
+
path: [...path, "recordQuery"]
|
|
452
|
+
});
|
|
438
453
|
if (issues.length > before) return null;
|
|
439
454
|
return {
|
|
440
455
|
connection: email.connection,
|
|
441
456
|
query: query.trim(),
|
|
442
457
|
checkAfterMinutes: minutes,
|
|
443
|
-
push: push === void 0 ? null : { topic: push.topic }
|
|
458
|
+
push: push === void 0 ? null : { topic: push.topic },
|
|
459
|
+
...record === "all" ? {
|
|
460
|
+
record: "all",
|
|
461
|
+
...email.recordQuery === void 0 ? {} : { recordQuery: email.recordQuery.trim() }
|
|
462
|
+
} : {}
|
|
444
463
|
};
|
|
445
464
|
}
|
|
446
465
|
function telegramSettings(value, issues, path = ["channels", "telegram"]) {
|
|
@@ -30,7 +30,7 @@ export { DEFAULT_DECISION_MODEL, DEFAULT_STT_MODEL, DEFAULT_ADMISSION_LIMITS, Se
|
|
|
30
30
|
* Defines a channel in the workspace's own code, `agent/agent/channels/<name>.ts`, for a provider
|
|
31
31
|
* the platform has no channel for: the code receives, reads and sends, the platform does the rest.
|
|
32
32
|
*/
|
|
33
|
-
export { defineChannel, type ChannelChecks, type ChannelContext, type ChannelDefinition, type ChannelFile, type ChannelMessage, type ChannelRead, type ChannelReply, type ChannelReplyFile, type ChannelRequest, type ChannelSender, } from "./lib/workspace-channel/channel.js";
|
|
33
|
+
export { defineChannel, type ChannelChecks, type ChannelContext, type ChannelDefinition, type ChannelEvent, type ChannelFile, type ChannelMessage, type ChannelMessageEvent, type ChannelRead, type ChannelReply, type ChannelReplyFile, type ChannelRequest, type ChannelSender, type ChatPart, } from "./lib/workspace-channel/channel.js";
|
|
34
34
|
export type { ChannelConnection, ChannelConnectionRequest, ChannelConnectionRequestOptions, ChannelConnectionResponse, } from "./lib/workspace-channel/connection.js";
|
|
35
35
|
export { chooseModel, model, type DynamicModel, type ModelChoice } from "./model.js";
|
|
36
36
|
export { forRoles } from "./lib/role-skill.js";
|
|
@@ -5,7 +5,7 @@ const __filename = __eveFileURLToPath(import.meta.url);
|
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import segmentstream from "../extension.mjs";
|
|
8
|
-
import { r as chatArchive } from "../../_chunks/chat-archive-
|
|
8
|
+
import { r as chatArchive } from "../../_chunks/chat-archive-CDk-FPLX.mjs";
|
|
9
9
|
import { chatContextHook } from "../lib/chat-context.mjs";
|
|
10
10
|
import { appOrigin } from "../lib/environment.mjs";
|
|
11
11
|
import { agentSessionReviews } from "../lib/review/client.mjs";
|
|
@@ -6,7 +6,7 @@ __eveDirname(__filename);
|
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import segmentstream from "../extension.mjs";
|
|
8
8
|
import { chatRecorder } from "../lib/chat-recorder.mjs";
|
|
9
|
-
import { r as chatArchive } from "../../_chunks/chat-archive-
|
|
9
|
+
import { r as chatArchive } from "../../_chunks/chat-archive-CDk-FPLX.mjs";
|
|
10
10
|
import { repliesReviewed } from "../lib/review/hold.mjs";
|
|
11
11
|
import { defineHook } from "eve/hooks";
|
|
12
12
|
var chat_recorder_default = defineHook({ events: { "*": chatRecorder(chatArchive(), (principal) => repliesReviewed(segmentstream.config.review, principal)) } });
|
|
@@ -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-CLenocka.mjs";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ChatStore } from "./chat-store.js";
|
|
1
|
+
import { type ChatPart, type ChatStore } from "./chat-store.js";
|
|
2
2
|
import { type Environment } from "./environment.js";
|
|
3
3
|
import { type RuntimeToken } from "./runtime-token.js";
|
|
4
4
|
/** Provider-neutral platform archive. Content uses the shared redaction and byte-limit policy. */
|
|
@@ -57,6 +57,11 @@ export interface ChatEvent {
|
|
|
57
57
|
/** The message was edited, or deleted, since it was written: the store keeps its latest state. */
|
|
58
58
|
readonly edited?: boolean;
|
|
59
59
|
readonly deleted?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* What the message carries beside its text, such as a location: kept by the workspace chat store
|
|
62
|
+
* alone, never sent to the chat archive in Cloud.
|
|
63
|
+
*/
|
|
64
|
+
readonly parts?: ReadonlyArray<ChatPart>;
|
|
60
65
|
}
|
|
61
66
|
export interface ChatPage {
|
|
62
67
|
readonly events: ReadonlyArray<ChatEvent & {
|
|
@@ -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 AGENT_SENDER, n as archiveSequence, r as chatArchive, i as chatAuthor, a as chatEvent, o as cloudChatArchive, s as collapseChatEvents, c as forgetChatStores, l as replyEvent, u as replyOperation, d as sessionFailureEventId, f as storeOf, p as summarizeArguments, m as topicContext } from "../../_chunks/chat-archive-
|
|
7
|
+
export { t as AGENT_SENDER, n as archiveSequence, r as chatArchive, i as chatAuthor, a as chatEvent, o as cloudChatArchive, s as collapseChatEvents, c as forgetChatStores, l as replyEvent, u as replyOperation, d as sessionFailureEventId, f as storeOf, p as summarizeArguments, m as topicContext } from "../../_chunks/chat-archive-CDk-FPLX.mjs";
|
|
@@ -6,7 +6,7 @@ __eveDirname(__filename);
|
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import { agentTaskIdOf } from "./agent-task.mjs";
|
|
8
8
|
import { chatCoordinates, sessionKind } from "./chat-recorder.mjs";
|
|
9
|
-
import { f as storeOf, i as chatAuthor, m as topicContext, n as archiveSequence, r as chatArchive, s as collapseChatEvents } from "../../_chunks/chat-archive-
|
|
9
|
+
import { f as storeOf, i as chatAuthor, m as topicContext, n as archiveSequence, r as chatArchive, s as collapseChatEvents } from "../../_chunks/chat-archive-CDk-FPLX.mjs";
|
|
10
10
|
import { xt as reportTelegramError } from "../../_chunks/principal-Zf42Yf4r.mjs";
|
|
11
11
|
import { createHash } from "node:crypto";
|
|
12
12
|
import { deliveries, runs, turns } from "eve/events";
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { AdmittedChat } from "./admission.js";
|
|
2
2
|
import { type ChatArchive, type ChatAuthor } from "./chat-archive.js";
|
|
3
|
+
import type { ChatPart, StoredFile } from "./chat-store.js";
|
|
4
|
+
import type { StagedFile } from "./inbound-media/files.js";
|
|
5
|
+
export type { ChatPart } from "./chat-store.js";
|
|
3
6
|
/**
|
|
4
7
|
* Messages in a chat that start no turn (docs/agent-chat-storage.md, "Messages without a turn").
|
|
5
8
|
* Every channel records them here, in one shape: the archive keeps them under the chat's
|
|
@@ -12,6 +15,91 @@ export interface RecordedFile {
|
|
|
12
15
|
readonly mimeType?: string;
|
|
13
16
|
readonly size?: number;
|
|
14
17
|
}
|
|
18
|
+
/** The most parts one message keeps, as the store takes them. */
|
|
19
|
+
export declare const MAXIMUM_PARTS = 20;
|
|
20
|
+
/** One part as the store keeps it, or undefined for a value that is none. */
|
|
21
|
+
export declare function chatPart(value: unknown): ChatPart | undefined;
|
|
22
|
+
/** The parts of a message as the store keeps them; undefined when any of them is none. */
|
|
23
|
+
export declare function chatParts(value: unknown): ReadonlyArray<ChatPart> | undefined;
|
|
24
|
+
/** The files of a message the inbound media pipeline stored, by their attachments' ids. */
|
|
25
|
+
export declare function stagedFiles(files: ReadonlyMap<string, StagedFile>): ReadonlyArray<StoredFile>;
|
|
26
|
+
/**
|
|
27
|
+
* A person's message a channel recorded itself, by the provider's id of it, in the workspace chat
|
|
28
|
+
* store: with what the provider says of it beside its text (the message it quotes, its parts, its
|
|
29
|
+
* time), so that a later change to it, a reaction, an edit or a deletion, finds it.
|
|
30
|
+
*/
|
|
31
|
+
export interface ReceivedMessage {
|
|
32
|
+
readonly channel: string;
|
|
33
|
+
readonly chatId: string;
|
|
34
|
+
/** The member who wrote it, who reads it. */
|
|
35
|
+
readonly memberId: string;
|
|
36
|
+
/** The person's account at the provider. */
|
|
37
|
+
readonly senderId: string;
|
|
38
|
+
readonly senderName?: string | null;
|
|
39
|
+
readonly messageId: string;
|
|
40
|
+
readonly replyTo?: string;
|
|
41
|
+
/** The text the agent read of it: its text, and what was transcribed of its speech. */
|
|
42
|
+
readonly text: string;
|
|
43
|
+
readonly parts?: ReadonlyArray<ChatPart>;
|
|
44
|
+
/** Its files as they were stored, by their attachments' ids. */
|
|
45
|
+
readonly files?: ReadonlyArray<StoredFile>;
|
|
46
|
+
/** When it was written, if not now. */
|
|
47
|
+
readonly at?: string;
|
|
48
|
+
/**
|
|
49
|
+
* What the agent did with it: the turn eve's inbox took it into, by the delivery the session
|
|
50
|
+
* received, or the reason it started none.
|
|
51
|
+
*/
|
|
52
|
+
readonly outcome: {
|
|
53
|
+
readonly turn: {
|
|
54
|
+
readonly sessionId: string;
|
|
55
|
+
readonly deliveryId: string;
|
|
56
|
+
};
|
|
57
|
+
} | {
|
|
58
|
+
readonly skipped: string;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Records a person's message by its provider id in the workspace chat store, with what the agent
|
|
63
|
+
* did with it. A message that started a turn keeps the delivery eve took it in (`meta.delivery`),
|
|
64
|
+
* by which the recorder links it to the turn. Recording it again changes nothing. On the chat
|
|
65
|
+
* archive in Cloud nothing is recorded here: the recorder keeps the message eve delivered there,
|
|
66
|
+
* as before, and answers false.
|
|
67
|
+
*/
|
|
68
|
+
export declare function recordReceivedMessage(archive: ChatArchive, message: ReceivedMessage): Promise<boolean>;
|
|
69
|
+
/**
|
|
70
|
+
* One change a provider reports to a message of a chat, found by the provider's id of it, the
|
|
71
|
+
* target: a reaction (an empty emoji removes the sender's), an edit, a deletion, or a delivery
|
|
72
|
+
* status of a message the workspace's side sent. None starts a turn.
|
|
73
|
+
*/
|
|
74
|
+
export type ChatChange = {
|
|
75
|
+
readonly channel: string;
|
|
76
|
+
readonly chatId: string;
|
|
77
|
+
readonly target: string;
|
|
78
|
+
} & ({
|
|
79
|
+
readonly change: "reaction";
|
|
80
|
+
readonly senderId: string;
|
|
81
|
+
readonly emoji: string;
|
|
82
|
+
} | {
|
|
83
|
+
readonly change: "edit";
|
|
84
|
+
readonly text: string;
|
|
85
|
+
readonly at?: string;
|
|
86
|
+
} | {
|
|
87
|
+
readonly change: "delete";
|
|
88
|
+
readonly at?: string;
|
|
89
|
+
} | {
|
|
90
|
+
readonly change: "status";
|
|
91
|
+
readonly status: "sent" | "delivered" | "read" | "failed";
|
|
92
|
+
/** The provider's refusal, as a code: lower case, digits and underscores. */
|
|
93
|
+
readonly failure?: string;
|
|
94
|
+
});
|
|
95
|
+
/** What became of a change: `dropped` on the chat archive in Cloud, which keeps no changes. */
|
|
96
|
+
export type ChatChangeOutcome = "applied" | "unchanged" | "dropped";
|
|
97
|
+
/**
|
|
98
|
+
* Applies one change to its message in the workspace chat store: `unchanged` when the store holds
|
|
99
|
+
* no such message, or the change changes nothing, such as a status older than the message's. On
|
|
100
|
+
* the chat archive in Cloud a change is dropped: it writes nothing new there.
|
|
101
|
+
*/
|
|
102
|
+
export declare function applyChatChange(archive: ChatArchive, change: ChatChange): Promise<ChatChangeOutcome>;
|
|
15
103
|
/** A message the archive keeps without a turn. */
|
|
16
104
|
export interface RecordedMessage {
|
|
17
105
|
/** The channel's name, as the archive keys its chats. */
|
|
@@ -36,10 +124,20 @@ export interface RecordedMessage {
|
|
|
36
124
|
readonly messageId: string;
|
|
37
125
|
readonly text: string;
|
|
38
126
|
readonly files?: ReadonlyArray<RecordedFile>;
|
|
127
|
+
/** The provider's id of the message it quotes. */
|
|
128
|
+
readonly replyTo?: string;
|
|
129
|
+
/** What it carries beside its text: kept by the workspace chat store alone. */
|
|
130
|
+
readonly parts?: ReadonlyArray<ChatPart>;
|
|
39
131
|
/** A group's chat, such as a thread in a Slack channel; by default a person's private chat. */
|
|
40
132
|
readonly chatType?: "private" | "group";
|
|
41
133
|
/** When the message was written, if not now. */
|
|
42
134
|
readonly at?: string;
|
|
135
|
+
/**
|
|
136
|
+
* A person's message nothing proved the sender of, such as mail that failed DMARC: marked so in
|
|
137
|
+
* the same write that records it, so the agent's reads of the chat never return it, even before
|
|
138
|
+
* what the channel did with it is recorded. Only the workspace chat store keeps the mark.
|
|
139
|
+
*/
|
|
140
|
+
readonly unverified?: boolean;
|
|
43
141
|
}
|
|
44
142
|
/** Records one message that starts no turn; true for its first record. */
|
|
45
143
|
export declare function recordChatMessage(archive: ChatArchive, message: RecordedMessage): Promise<boolean | void>;
|
|
@@ -59,6 +157,13 @@ export type SentOutsideOutcome =
|
|
|
59
157
|
* the archive. A message the archive holds by its provider id is skipped.
|
|
60
158
|
*/
|
|
61
159
|
export declare function recordSentOutside(archive: ChatArchive, message: Omit<RecordedMessage, "participantIds" | "direction">): Promise<SentOutsideOutcome>;
|
|
160
|
+
/**
|
|
161
|
+
* Records a person's message the agent cannot read, such as a contact card on a channel that
|
|
162
|
+
* reads only text and files, in the chat the agent has with them, by the same rule as
|
|
163
|
+
* {@link recordSentOutside}, with `reason` as what the agent did with it: it starts no turn. Only
|
|
164
|
+
* the workspace chat store keeps it; nothing is written to the chat archive in Cloud.
|
|
165
|
+
*/
|
|
166
|
+
export declare function recordUnreadMessage(archive: ChatArchive, message: Omit<RecordedMessage, "participantIds" | "direction" | "by">, reason: string): Promise<SentOutsideOutcome | "dropped">;
|
|
62
167
|
/**
|
|
63
168
|
* Records a person's message in a private chat a member of the workspace took over, which starts
|
|
64
169
|
* no turn: the inbox shows it, and the agent reads it in its context once the chat is handed back.
|
|
@@ -74,4 +179,7 @@ export declare function recordHandledMessage(archive: ChatArchive, input: {
|
|
|
74
179
|
readonly messageId: string;
|
|
75
180
|
readonly text: string;
|
|
76
181
|
readonly files?: ReadonlyArray<RecordedFile>;
|
|
182
|
+
readonly replyTo?: string;
|
|
183
|
+
readonly parts?: ReadonlyArray<ChatPart>;
|
|
184
|
+
readonly at?: string;
|
|
77
185
|
}): Promise<void>;
|
|
@@ -4,7 +4,143 @@ 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 { a as chatEvent, f as storeOf } from "../../_chunks/chat-archive-
|
|
7
|
+
import { C as storedFiles, a as chatEvent, f as storeOf, h as UNVERIFIED } from "../../_chunks/chat-archive-CDk-FPLX.mjs";
|
|
8
|
+
const MAXIMUM_PARTS = 20;
|
|
9
|
+
const PART_BOUNDS = {
|
|
10
|
+
text: 1024,
|
|
11
|
+
values: 20,
|
|
12
|
+
value: 320
|
|
13
|
+
};
|
|
14
|
+
function partRecord(value) {
|
|
15
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
16
|
+
}
|
|
17
|
+
function partText(value) {
|
|
18
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim().slice(0, PART_BOUNDS.text) : void 0;
|
|
19
|
+
}
|
|
20
|
+
function partValues(value) {
|
|
21
|
+
const values = (Array.isArray(value) ? value : []).flatMap((item) => typeof item === "string" && item.trim() !== "" ? [item.trim().slice(0, PART_BOUNDS.value)] : []).slice(0, PART_BOUNDS.values);
|
|
22
|
+
return values.length === 0 ? void 0 : values;
|
|
23
|
+
}
|
|
24
|
+
function degrees(value, bound) {
|
|
25
|
+
const number = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
|
|
26
|
+
return typeof number === "number" && Number.isFinite(number) && Math.abs(number) <= bound ? number : void 0;
|
|
27
|
+
}
|
|
28
|
+
function chatPart(value) {
|
|
29
|
+
const part = partRecord(value);
|
|
30
|
+
if (part?.type === "location") {
|
|
31
|
+
const latitude = degrees(part.latitude, 90);
|
|
32
|
+
const longitude = degrees(part.longitude, 180);
|
|
33
|
+
const name = partText(part.name);
|
|
34
|
+
const address = partText(part.address);
|
|
35
|
+
return latitude === void 0 || longitude === void 0 ? void 0 : {
|
|
36
|
+
type: "location",
|
|
37
|
+
latitude,
|
|
38
|
+
longitude,
|
|
39
|
+
...name === void 0 ? {} : { name },
|
|
40
|
+
...address === void 0 ? {} : { address }
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (part?.type === "contact") {
|
|
44
|
+
const name = partText(part.name);
|
|
45
|
+
const phones = partValues(part.phones);
|
|
46
|
+
const emails = partValues(part.emails);
|
|
47
|
+
const organization = partText(part.organization);
|
|
48
|
+
return name === void 0 ? void 0 : {
|
|
49
|
+
type: "contact",
|
|
50
|
+
name,
|
|
51
|
+
...phones === void 0 ? {} : { phones },
|
|
52
|
+
...emails === void 0 ? {} : { emails },
|
|
53
|
+
...organization === void 0 ? {} : { organization }
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function chatParts(value) {
|
|
58
|
+
if (!Array.isArray(value) || value.length > 20) return;
|
|
59
|
+
const parts = value.map(chatPart);
|
|
60
|
+
return parts.every((part) => part !== void 0) ? parts : void 0;
|
|
61
|
+
}
|
|
62
|
+
function stagedFiles(files) {
|
|
63
|
+
return storedFiles([...files.values()].flatMap((file) => file.status === "stored" && typeof file.attachment?.id === "string" ? [{
|
|
64
|
+
id: file.attachment.id,
|
|
65
|
+
filename: file.attachment.filename,
|
|
66
|
+
contentType: file.attachment.contentType,
|
|
67
|
+
size: file.attachment.size
|
|
68
|
+
}] : []));
|
|
69
|
+
}
|
|
70
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;
|
|
71
|
+
async function recordReceivedMessage(archive, message) {
|
|
72
|
+
const store = await storeOf(archive);
|
|
73
|
+
if (store === void 0) return false;
|
|
74
|
+
const target = {
|
|
75
|
+
channel: message.channel,
|
|
76
|
+
chatId: message.chatId.slice(0, 256),
|
|
77
|
+
messageId: message.messageId.slice(0, 512)
|
|
78
|
+
};
|
|
79
|
+
const senderName = message.senderName?.trim().slice(0, 200);
|
|
80
|
+
const turn = "turn" in message.outcome ? message.outcome.turn : void 0;
|
|
81
|
+
await store.record({
|
|
82
|
+
...target,
|
|
83
|
+
chatType: "private",
|
|
84
|
+
participants: UUID.test(message.memberId) ? [message.memberId] : [],
|
|
85
|
+
direction: "inbound",
|
|
86
|
+
author: "human",
|
|
87
|
+
senderId: message.senderId.slice(0, 256),
|
|
88
|
+
...senderName === void 0 || senderName === "" ? {} : { senderName },
|
|
89
|
+
...UUID.test(message.memberId) ? { userId: message.memberId } : {},
|
|
90
|
+
...message.replyTo === void 0 ? {} : { replyTo: message.replyTo.slice(0, 512) },
|
|
91
|
+
...message.text === "" ? {} : { text: message.text.slice(0, 1e5) },
|
|
92
|
+
...message.parts === void 0 || message.parts.length === 0 ? {} : { parts: message.parts.slice(0, 20) },
|
|
93
|
+
...message.files === void 0 || message.files.length === 0 ? {} : { files: message.files.slice(0, 50) },
|
|
94
|
+
...message.at === void 0 ? {} : { at: message.at },
|
|
95
|
+
...turn === void 0 ? {} : {
|
|
96
|
+
sessionId: turn.sessionId.slice(0, 512),
|
|
97
|
+
meta: { delivery: turn.deliveryId }
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
await store.change({
|
|
101
|
+
...target,
|
|
102
|
+
change: "disposition",
|
|
103
|
+
...turn === void 0 ? {
|
|
104
|
+
disposition: "skipped",
|
|
105
|
+
reason: message.outcome.skipped
|
|
106
|
+
} : {
|
|
107
|
+
disposition: "turn",
|
|
108
|
+
sessionId: turn.sessionId.slice(0, 512)
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
const CODE = /^[a-z][a-z0-9_]{0,63}$/u;
|
|
114
|
+
async function applyChatChange(archive, change) {
|
|
115
|
+
const store = await storeOf(archive);
|
|
116
|
+
if (store === void 0) return "dropped";
|
|
117
|
+
const target = {
|
|
118
|
+
channel: change.channel,
|
|
119
|
+
chatId: change.chatId.slice(0, 256),
|
|
120
|
+
messageId: change.target.slice(0, 512)
|
|
121
|
+
};
|
|
122
|
+
const { applied } = await store.change(change.change === "reaction" ? {
|
|
123
|
+
...target,
|
|
124
|
+
change: "reaction",
|
|
125
|
+
senderId: change.senderId.slice(0, 256),
|
|
126
|
+
emoji: change.emoji.slice(0, 64)
|
|
127
|
+
} : change.change === "edit" ? {
|
|
128
|
+
...target,
|
|
129
|
+
change: "edit",
|
|
130
|
+
text: change.text.slice(0, 1e5),
|
|
131
|
+
...change.at === void 0 ? {} : { at: change.at }
|
|
132
|
+
} : change.change === "delete" ? {
|
|
133
|
+
...target,
|
|
134
|
+
change: "delete",
|
|
135
|
+
...change.at === void 0 ? {} : { at: change.at }
|
|
136
|
+
} : {
|
|
137
|
+
...target,
|
|
138
|
+
change: "status",
|
|
139
|
+
status: change.status,
|
|
140
|
+
...change.failure !== void 0 && CODE.test(change.failure) ? { failure: change.failure } : {}
|
|
141
|
+
});
|
|
142
|
+
return applied ? "applied" : "unchanged";
|
|
143
|
+
}
|
|
8
144
|
async function recordChatMessage(archive, message) {
|
|
9
145
|
const { eventId } = message;
|
|
10
146
|
return archive.append(chatEvent({
|
|
@@ -19,9 +155,12 @@ async function recordChatMessage(archive, message) {
|
|
|
19
155
|
senderIsBot: message.by === "bot",
|
|
20
156
|
kind: message.direction,
|
|
21
157
|
messageId: message.messageId,
|
|
158
|
+
...message.replyTo === void 0 ? {} : { replyToMessageId: message.replyTo },
|
|
22
159
|
text: message.text,
|
|
160
|
+
...message.parts === void 0 || message.parts.length === 0 ? {} : { parts: message.parts },
|
|
23
161
|
...message.direction === "outbound" ? { status: "completed" } : {},
|
|
24
162
|
...message.at === void 0 ? {} : { at: message.at },
|
|
163
|
+
...message.unverified === true ? { argsSummary: JSON.stringify({ [UNVERIFIED]: true }) } : {},
|
|
25
164
|
attachments: (message.files ?? []).map((file, index) => ({
|
|
26
165
|
id: `${eventId}:file:${index}`,
|
|
27
166
|
kind: "file",
|
|
@@ -32,6 +171,30 @@ async function recordChatMessage(archive, message) {
|
|
|
32
171
|
}));
|
|
33
172
|
}
|
|
34
173
|
async function recordSentOutside(archive, message) {
|
|
174
|
+
return recordInChat(archive, {
|
|
175
|
+
...message,
|
|
176
|
+
direction: "outbound"
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
async function recordUnreadMessage(archive, message, reason) {
|
|
180
|
+
const store = await storeOf(archive);
|
|
181
|
+
if (store === void 0) return "dropped";
|
|
182
|
+
const outcome = await recordInChat(archive, {
|
|
183
|
+
...message,
|
|
184
|
+
direction: "inbound",
|
|
185
|
+
by: "human"
|
|
186
|
+
});
|
|
187
|
+
if (outcome === "recorded") await store.change({
|
|
188
|
+
channel: message.provider,
|
|
189
|
+
chatId: message.chatId,
|
|
190
|
+
messageId: message.messageId,
|
|
191
|
+
change: "disposition",
|
|
192
|
+
disposition: "skipped",
|
|
193
|
+
reason
|
|
194
|
+
});
|
|
195
|
+
return outcome;
|
|
196
|
+
}
|
|
197
|
+
async function recordInChat(archive, message) {
|
|
35
198
|
const chat = {
|
|
36
199
|
provider: message.provider,
|
|
37
200
|
chatId: message.chatId,
|
|
@@ -50,7 +213,6 @@ async function recordSentOutside(archive, message) {
|
|
|
50
213
|
if (known !== void 0) return "known";
|
|
51
214
|
await recordChatMessage(archive, {
|
|
52
215
|
...message,
|
|
53
|
-
direction: "outbound",
|
|
54
216
|
participantIds: latest.participantIds
|
|
55
217
|
});
|
|
56
218
|
return "recorded";
|
|
@@ -70,7 +232,10 @@ async function recordHandledMessage(archive, input) {
|
|
|
70
232
|
senderName: input.senderName ?? null,
|
|
71
233
|
messageId: input.messageId,
|
|
72
234
|
text: input.text,
|
|
73
|
-
...input.files === void 0 ? {} : { files: input.files }
|
|
235
|
+
...input.files === void 0 ? {} : { files: input.files },
|
|
236
|
+
...input.replyTo === void 0 ? {} : { replyTo: input.replyTo },
|
|
237
|
+
...input.parts === void 0 ? {} : { parts: input.parts },
|
|
238
|
+
...input.at === void 0 ? {} : { at: input.at }
|
|
74
239
|
});
|
|
75
240
|
const store = await storeOf(archive);
|
|
76
241
|
if (store !== void 0) {
|
|
@@ -99,4 +264,4 @@ async function recordHandledMessage(archive, input) {
|
|
|
99
264
|
status: "completed"
|
|
100
265
|
}));
|
|
101
266
|
}
|
|
102
|
-
export { recordChatMessage, recordHandledMessage, recordSentOutside };
|
|
267
|
+
export { MAXIMUM_PARTS, applyChatChange, chatPart, chatParts, recordChatMessage, recordHandledMessage, recordReceivedMessage, recordSentOutside, recordUnreadMessage, stagedFiles };
|