@segmentstream/agent 0.69.2 → 0.70.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 +16 -0
- package/dist/_chunks/{build-metadata-B1FvLdht.mjs → build-metadata-BXGz_t3T.mjs} +1 -1
- package/dist/extension/channels/slack.mjs +1 -0
- package/dist/extension/channels/telegram.mjs +1 -0
- package/dist/extension/channels/whatsapp.mjs +1 -0
- package/dist/extension/config.d.ts +13 -5
- package/dist/extension/config.mjs +14 -0
- package/dist/extension/lib/build-metadata.mjs +1 -1
- package/dist/extension/lib/slack/adapter.d.ts +6 -0
- package/dist/extension/lib/slack/adapter.mjs +1 -1
- package/dist/extension/lib/slack/callbacks.d.ts +4 -4
- package/dist/extension/lib/slack/callbacks.mjs +17 -34
- package/dist/extension/lib/telegram/adapter.d.ts +6 -0
- package/dist/extension/lib/telegram/adapter.mjs +2 -2
- package/dist/extension/lib/telegram/callbacks.mjs +42 -49
- package/dist/extension/lib/telegram/health.mjs +1 -1
- package/dist/extension/lib/telegram/lifecycle.mjs +1 -1
- package/dist/extension/lib/whatsapp/adapter.d.ts +3 -0
- package/dist/extension/lib/whatsapp/adapter.mjs +44 -0
- package/dist/extension/lib/whatsapp/callbacks.d.ts +55 -0
- package/dist/extension/lib/whatsapp/callbacks.mjs +138 -0
- package/dist/extension/lib/workspace-action.d.ts +30 -0
- package/dist/extension/lib/workspace-action.mjs +35 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.70.0
|
|
4
|
+
|
|
5
|
+
One action answers the workspace's buttons in every channel: `buttons: { entity, action,
|
|
6
|
+
contextVersion: 2 }` in `agent/segmentstream.json` names the routine action a press of a button
|
|
7
|
+
whose data is `ws:<token>` calls, as the member who pressed it, with `{ token }`. A channel's own
|
|
8
|
+
`callbacks` (`channels.telegram.callbacks`, or a Slack channel's) is its earlier name, still read
|
|
9
|
+
where `buttons` is absent until 2027-01-19; beside `buttons` it must name the same action.
|
|
10
|
+
|
|
11
|
+
WhatsApp now answers such presses too: a reply button `agent_chat.send` put under a message, and an
|
|
12
|
+
approved template's quick reply whose payload is `ws:<token>`, which reaches a person outside the 24
|
|
13
|
+
hours. The action's runtime source names the message the button was under. The press, which the
|
|
14
|
+
person sees as their message, and the action's acknowledgement, sent as a message, are kept in the
|
|
15
|
+
chat's history; `context` queues a turn. The press's record claims it: a press WhatsApp delivers
|
|
16
|
+
again runs nothing again, and one the history cannot record runs nothing.
|
|
17
|
+
WhatsApp cannot edit a message, so `edit` is not applied.
|
|
18
|
+
|
|
3
19
|
## 0.69.2
|
|
4
20
|
|
|
5
21
|
Pins @segmentstream/ontology 0.145.0.
|
|
@@ -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.70.0";
|
|
8
8
|
function agentBuild(environment = process.env) {
|
|
9
9
|
return {
|
|
10
10
|
agent: version,
|
|
@@ -10,6 +10,7 @@ import { segmentstreamSlack } from "../lib/slack/adapter.mjs";
|
|
|
10
10
|
import { decide } from "eve/ai";
|
|
11
11
|
var slack_default = segmentstreamSlack({
|
|
12
12
|
settings: (name) => channelOf(segmentstream.config.channels, "slack", name),
|
|
13
|
+
buttons: () => segmentstream.config.buttons,
|
|
13
14
|
admission: () => segmentstream.config,
|
|
14
15
|
groups: () => segmentstream.config.groups,
|
|
15
16
|
models: () => segmentstream.config.models,
|
|
@@ -10,6 +10,7 @@ import { segmentstreamTelegram } from "../lib/telegram/adapter.mjs";
|
|
|
10
10
|
import { decide } from "eve/ai";
|
|
11
11
|
var telegram_default = segmentstreamTelegram({
|
|
12
12
|
settings: (name) => channelOf(segmentstream.config.channels, "telegram", name),
|
|
13
|
+
buttons: () => segmentstream.config.buttons,
|
|
13
14
|
groups: () => segmentstream.config.groups,
|
|
14
15
|
models: () => segmentstream.config.models,
|
|
15
16
|
confirmationMode: () => "native",
|
|
@@ -10,6 +10,7 @@ import { segmentstreamWhatsApp } from "../lib/whatsapp/adapter.mjs";
|
|
|
10
10
|
import { decide } from "eve/ai";
|
|
11
11
|
var whatsapp_default = segmentstreamWhatsApp({
|
|
12
12
|
settings: (name) => channelOf(segmentstream.config.channels, "whatsapp", name),
|
|
13
|
+
buttons: () => segmentstream.config.buttons,
|
|
13
14
|
admission: () => segmentstream.config,
|
|
14
15
|
models: () => segmentstream.config.models,
|
|
15
16
|
review: {
|
|
@@ -143,6 +143,15 @@ export interface SegmentStreamAgentConfigInput {
|
|
|
143
143
|
* Off when absent.
|
|
144
144
|
*/
|
|
145
145
|
readonly handoff?: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* The routine ontology action a press of a workspace button calls, in every channel, as the
|
|
148
|
+
* member who pressed it: a button the workspace sent with `agent_chat.send` and its `buttons`,
|
|
149
|
+
* or one it sent itself whose data is `ws:<token>`. The action receives `{ token }`. See
|
|
150
|
+
* `docs/agent.md`, "Workspace callback actions". Without it such a press calls nothing.
|
|
151
|
+
*/
|
|
152
|
+
readonly buttons?: Omit<WorkspaceActionHook, "contextVersion"> & {
|
|
153
|
+
readonly contextVersion: number;
|
|
154
|
+
};
|
|
146
155
|
/**
|
|
147
156
|
* The web the agent reaches: with `domains`, `segmentstream__web_fetch` fetches `https://` pages
|
|
148
157
|
* of these domains, each also covering its subdomains, for callers who hold `agent_tool:web`;
|
|
@@ -253,6 +262,7 @@ export interface TelegramChannelInput {
|
|
|
253
262
|
readonly kinds: ReadonlyArray<string>;
|
|
254
263
|
readonly contextVersion: number;
|
|
255
264
|
};
|
|
265
|
+
/** The earlier name of the top-level `buttons`, for Telegram's buttons alone. */
|
|
256
266
|
readonly callbacks?: Omit<WorkspaceActionHook, "contextVersion"> & {
|
|
257
267
|
readonly contextVersion: number;
|
|
258
268
|
};
|
|
@@ -272,10 +282,7 @@ export interface WhatsAppChannelInput {
|
|
|
272
282
|
export interface SlackChannelInput {
|
|
273
283
|
/** The key of the workspace's `SlackConnection`, such as `slack`. */
|
|
274
284
|
readonly connection: string;
|
|
275
|
-
/**
|
|
276
|
-
* The routine action a press of a button the workspace sent calls, as the member who pressed
|
|
277
|
-
* it: a button whose `value` is `ws:<token>`. See `docs/agent.md`, "Workspace callback actions".
|
|
278
|
-
*/
|
|
285
|
+
/** The earlier name of the top-level `buttons`, for this Slack channel's buttons alone. */
|
|
279
286
|
readonly callbacks?: Omit<WorkspaceActionHook, "contextVersion"> & {
|
|
280
287
|
readonly contextVersion: number;
|
|
281
288
|
};
|
|
@@ -508,6 +515,8 @@ export interface SegmentStreamAgentConfig extends AdmissionSettings {
|
|
|
508
515
|
* {@link SegmentStreamAgentConfigInput.handoff}.
|
|
509
516
|
*/
|
|
510
517
|
readonly handoff?: boolean;
|
|
518
|
+
/** The action a press of a workspace button calls, in every channel; absent without one. */
|
|
519
|
+
readonly buttons?: WorkspaceActionHook;
|
|
511
520
|
/** The web tools' settings; absent without web tools. See {@link WebSettings}. */
|
|
512
521
|
readonly web?: WebSettings;
|
|
513
522
|
/** The agent's browser; absent without the browser tool. See {@link BrowserSettings}. */
|
|
@@ -559,7 +568,6 @@ export interface WhatsAppSettings {
|
|
|
559
568
|
*/
|
|
560
569
|
export interface SlackSettings {
|
|
561
570
|
readonly connection: string;
|
|
562
|
-
/** The action a press of a workspace button calls; without it such a press calls nothing. */
|
|
563
571
|
readonly callbacks?: WorkspaceActionHook;
|
|
564
572
|
}
|
|
565
573
|
/** The fewest and most minutes `checkAfterMinutes` may name, and its default. */
|
|
@@ -1052,6 +1052,7 @@ function validateConfig(input, options = {}) {
|
|
|
1052
1052
|
"workspaceSource",
|
|
1053
1053
|
"unanswered",
|
|
1054
1054
|
"handoff",
|
|
1055
|
+
"buttons",
|
|
1055
1056
|
"web",
|
|
1056
1057
|
"browser",
|
|
1057
1058
|
"sessions",
|
|
@@ -1258,6 +1259,18 @@ function validateConfig(input, options = {}) {
|
|
|
1258
1259
|
message: "must be true or false",
|
|
1259
1260
|
path: ["handoff"]
|
|
1260
1261
|
});
|
|
1262
|
+
const buttons = workspaceHook(root?.buttons, ["buttons"], issues);
|
|
1263
|
+
for (const [name, channel] of Object.entries(declared)) {
|
|
1264
|
+
const own = channel !== null && "callbacks" in channel ? channel.callbacks : void 0;
|
|
1265
|
+
if (buttons !== void 0 && own !== void 0 && (own.entity !== buttons.entity || own.action !== buttons.action)) issues.push({
|
|
1266
|
+
message: "names another action than buttons, which answers every channel's buttons",
|
|
1267
|
+
path: [
|
|
1268
|
+
"channels",
|
|
1269
|
+
name,
|
|
1270
|
+
"callbacks"
|
|
1271
|
+
]
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1261
1274
|
const web = webSettings(root?.web, issues);
|
|
1262
1275
|
const browser = browserSettings(root?.browser, issues);
|
|
1263
1276
|
if (root?.browser !== void 0 && sandbox !== true) issues.push({
|
|
@@ -1289,6 +1302,7 @@ function validateConfig(input, options = {}) {
|
|
|
1289
1302
|
...workspaceSource === true ? { workspaceSource: true } : {},
|
|
1290
1303
|
...unanswered === true ? { unanswered: true } : {},
|
|
1291
1304
|
...handoff === true ? { handoff: true } : {},
|
|
1305
|
+
...buttons === void 0 ? {} : { buttons },
|
|
1292
1306
|
...web === null ? {} : { web },
|
|
1293
1307
|
...browser === null ? {} : { browser },
|
|
1294
1308
|
sessions,
|
|
@@ -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-BXGz_t3T.mjs";
|
|
@@ -14,6 +14,7 @@ import { type ImageDescriber } from "../inbound-media/images.js";
|
|
|
14
14
|
import { type CapabilityLookup, type MediaPlan } from "../inbound-media/plan.js";
|
|
15
15
|
import { type Transcriber } from "../inbound-media/speech.js";
|
|
16
16
|
import { type SlackFileDownload } from "./files.js";
|
|
17
|
+
import type { WorkspaceActionHook } from "../workspace-action.js";
|
|
17
18
|
import { type ChannelReviewOptions } from "../review/reply.js";
|
|
18
19
|
import { type RuntimeToken } from "../runtime-token.js";
|
|
19
20
|
import { type ChatArchive } from "../chat-archive.js";
|
|
@@ -53,6 +54,11 @@ export interface SlackChannelOptions {
|
|
|
53
54
|
/** The Slack channel `name` the mount declares, or `null` when it declares no such channel. */
|
|
54
55
|
readonly settings: (name: string) => SlackSettings | null;
|
|
55
56
|
readonly admission: () => AdmissionSettings;
|
|
57
|
+
/**
|
|
58
|
+
* The action a press of a workspace button calls, from `buttons` in `agent/segmentstream.json`:
|
|
59
|
+
* the same in every channel. Without it, a channel's own earlier `callbacks`.
|
|
60
|
+
*/
|
|
61
|
+
readonly buttons?: () => WorkspaceActionHook | undefined;
|
|
56
62
|
/**
|
|
57
63
|
* The workspace's group routing. In `mention` mode a reply in a followed thread reaches the
|
|
58
64
|
* agent only when it mentions the bot; in `smart` mode the model decides.
|
|
@@ -342,7 +342,7 @@ function segmentstreamSlack(options) {
|
|
|
342
342
|
onInteraction: (action, ctx) => handleWorkspaceCallback({
|
|
343
343
|
action,
|
|
344
344
|
ctx,
|
|
345
|
-
configuration: settings()?.callbacks,
|
|
345
|
+
configuration: options.buttons?.() ?? settings()?.callbacks,
|
|
346
346
|
admission: options.admission(),
|
|
347
347
|
identities: pairing(),
|
|
348
348
|
environment,
|
|
@@ -7,10 +7,10 @@ import type { RuntimeToken } from "../runtime-token.js";
|
|
|
7
7
|
import { type WorkspaceActionHook } from "../workspace-action.js";
|
|
8
8
|
/**
|
|
9
9
|
* A press of a button the workspace sent, whose `value` is `ws:<token>`: the routine action
|
|
10
|
-
* `
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
10
|
+
* `buttons` names runs as the member paired with the Slack user who pressed it, with the verified
|
|
11
|
+
* chat, thread, pressed message and presser as its runtime source. What the action returns can
|
|
12
|
+
* acknowledge the press privately to the presser, edit the pressed message and queue one turn in
|
|
13
|
+
* the thread's session. Anyone not paired gets the workspace's pairing reply, privately.
|
|
14
14
|
*
|
|
15
15
|
* There is no retry or token claim: expiry, audience checks, repeated presses and business
|
|
16
16
|
* idempotency belong to the workspace's action. A button with another value is not the
|
|
@@ -4,16 +4,11 @@ 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 { admitMessage } from "../admission.mjs";
|
|
8
7
|
import { It as runtimeEnvelope, bt as withChatAddress } from "../../../_chunks/principal-DYl7vqOp.mjs";
|
|
9
8
|
import { currentChannel } from "../channel-scope.mjs";
|
|
10
|
-
import {
|
|
11
|
-
import { workspaceAction, workspaceButtonToken } from "../workspace-action.mjs";
|
|
9
|
+
import { pressWorkspaceButton, queueWorkspaceButtonTurn, workspaceAction, workspaceButtonToken } from "../workspace-action.mjs";
|
|
12
10
|
import { pairingNotice } from "./pairing.mjs";
|
|
13
11
|
import { randomUUID } from "node:crypto";
|
|
14
|
-
const DISPATCH_DEADLINE_MS = 1e4;
|
|
15
|
-
const ACKNOWLEDGEMENT_LIMIT = 200;
|
|
16
|
-
const CONTEXT_LIMIT = 16384;
|
|
17
12
|
const EDIT_TEXT_LIMIT = 4e4;
|
|
18
13
|
const EDIT_BLOCKS_LIMIT = 50;
|
|
19
14
|
const EDIT_PAYLOAD_LIMIT = 32768;
|
|
@@ -23,7 +18,7 @@ async function handleWorkspaceCallback(input) {
|
|
|
23
18
|
const messageTs = action.messageTs;
|
|
24
19
|
if (token === void 0 || messageTs === void 0) return;
|
|
25
20
|
if (input.configuration === void 0) {
|
|
26
|
-
report("workspace_callback_unconfigured", new Error("A workspace button was pressed in Slack, but
|
|
21
|
+
report("workspace_callback_unconfigured", new Error("A workspace button was pressed in Slack, but buttons names no action."));
|
|
27
22
|
return;
|
|
28
23
|
}
|
|
29
24
|
const user = action.user.id;
|
|
@@ -65,16 +60,14 @@ async function handleWorkspaceCallback(input) {
|
|
|
65
60
|
};
|
|
66
61
|
const principal = withChatAddress(input.principal(lookup.member, user), address);
|
|
67
62
|
const callId = action.triggerId ?? randomUUID();
|
|
68
|
-
|
|
69
|
-
let timer;
|
|
70
|
-
let result;
|
|
63
|
+
let answer;
|
|
71
64
|
try {
|
|
72
|
-
|
|
65
|
+
answer = await pressWorkspaceButton((signal) => workspaceAction({
|
|
73
66
|
configuration: input.configuration,
|
|
74
67
|
principal,
|
|
75
68
|
memberId: lookup.member.id,
|
|
76
69
|
input: { token },
|
|
77
|
-
signal
|
|
70
|
+
signal,
|
|
78
71
|
envelope: runtimeEnvelope({
|
|
79
72
|
source: {
|
|
80
73
|
provider: "slack",
|
|
@@ -92,23 +85,14 @@ async function handleWorkspaceCallback(input) {
|
|
|
92
85
|
environment: input.environment,
|
|
93
86
|
...input.token === void 0 ? {} : { token: input.token },
|
|
94
87
|
...input.fetch === void 0 ? {} : { fetch: input.fetch }
|
|
95
|
-
}),
|
|
96
|
-
timer = setTimeout(() => {
|
|
97
|
-
controller.abort();
|
|
98
|
-
report("workspace_callback_timeout", new Error("A Slack workspace button's action took more than 10 seconds."));
|
|
99
|
-
resolve(void 0);
|
|
100
|
-
}, DISPATCH_DEADLINE_MS);
|
|
101
|
-
})]);
|
|
88
|
+
}), (error) => report("workspace_callback_timeout", error));
|
|
102
89
|
} catch (error) {
|
|
103
90
|
report("workspace_callback_failed", error);
|
|
104
91
|
return;
|
|
105
|
-
} finally {
|
|
106
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
107
92
|
}
|
|
108
|
-
if (
|
|
109
|
-
const acknowledgement =
|
|
110
|
-
if (
|
|
111
|
-
const edit = object(result.edit);
|
|
93
|
+
if (answer === void 0) return;
|
|
94
|
+
const { acknowledgement, edit, context } = answer;
|
|
95
|
+
if (acknowledgement !== void 0) await privately(acknowledgement).catch((error) => report("workspace_callback_ack_failed", error));
|
|
112
96
|
if (typeof edit.text === "string" || Array.isArray(edit.blocks)) try {
|
|
113
97
|
const text = typeof edit.text === "string" && edit.text.length <= EDIT_TEXT_LIMIT ? edit.text : void 0;
|
|
114
98
|
const blocks = Array.isArray(edit.blocks) && edit.blocks.length <= EDIT_BLOCKS_LIMIT ? edit.blocks : void 0;
|
|
@@ -124,10 +108,9 @@ async function handleWorkspaceCallback(input) {
|
|
|
124
108
|
} catch (error) {
|
|
125
109
|
report("workspace_callback_edit_failed", error);
|
|
126
110
|
}
|
|
127
|
-
|
|
128
|
-
if (typeof context !== "string" || context.trim() === "" || context.length > CONTEXT_LIMIT) return;
|
|
111
|
+
if (context === void 0) return;
|
|
129
112
|
try {
|
|
130
|
-
|
|
113
|
+
await queueWorkspaceButtonTurn({
|
|
131
114
|
identity: {
|
|
132
115
|
provider: "slack",
|
|
133
116
|
externalId: user,
|
|
@@ -142,12 +125,12 @@ async function handleWorkspaceCallback(input) {
|
|
|
142
125
|
channel,
|
|
143
126
|
chatId: channelId,
|
|
144
127
|
recipient: user
|
|
145
|
-
} } : {}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
128
|
+
} } : {},
|
|
129
|
+
memberId: lookup.member.id,
|
|
130
|
+
queue: () => ctx.send(context, {
|
|
131
|
+
auth: principal,
|
|
132
|
+
turnPolicy: "queue"
|
|
133
|
+
})
|
|
151
134
|
});
|
|
152
135
|
} catch (error) {
|
|
153
136
|
report("workspace_callback_turn_failed", error);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { GroupPolicy } from "../group-routing/policy.js";
|
|
2
|
+
import type { WorkspaceActionHook } from "../workspace-action.js";
|
|
2
3
|
import { type AttachmentDescriber } from "./attachment-description.js";
|
|
3
4
|
import { type TelegramChannel } from "eve/channels/telegram";
|
|
4
5
|
import type { ChatModelChoice, AdmissionSettings, SessionSettings, TelegramSettings } from "../../config.js";
|
|
@@ -27,6 +28,11 @@ import { type ChannelReviewOptions } from "../review/reply.js";
|
|
|
27
28
|
export interface TelegramAdapterOptions {
|
|
28
29
|
/** The Telegram channel `name` the mount declares, or `null` when it declares no such channel. */
|
|
29
30
|
readonly settings: (name: string) => TelegramSettings | null;
|
|
31
|
+
/**
|
|
32
|
+
* The action a press of a workspace button calls, from `buttons` in `agent/segmentstream.json`:
|
|
33
|
+
* the same in every channel. Without it, a channel's own earlier `callbacks`.
|
|
34
|
+
*/
|
|
35
|
+
readonly buttons?: () => WorkspaceActionHook | undefined;
|
|
30
36
|
readonly groups?: () => GroupPolicy;
|
|
31
37
|
readonly models: () => {
|
|
32
38
|
readonly stt: string | false;
|
|
@@ -10,7 +10,7 @@ import { groupRoutingHealth } from "../group-routing/health.mjs";
|
|
|
10
10
|
import { dispatchStructuredMessage, structuredMessage } from "./structured.mjs";
|
|
11
11
|
import { handleWorkspaceCallback } from "./callbacks.mjs";
|
|
12
12
|
import { telegramText, withTelegramTexts } from "./texts.mjs";
|
|
13
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
13
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BXGz_t3T.mjs";
|
|
14
14
|
import { attachmentLifecycle } from "./lifecycle.mjs";
|
|
15
15
|
import { manifestOf } from "../../config.mjs";
|
|
16
16
|
import { gatewayAttachmentDescriber } from "./attachment-description.mjs";
|
|
@@ -352,7 +352,7 @@ function segmentstreamTelegram(options) {
|
|
|
352
352
|
await handleWorkspaceCallback({
|
|
353
353
|
context,
|
|
354
354
|
query,
|
|
355
|
-
configuration: settings().callbacks,
|
|
355
|
+
configuration: options.buttons?.() ?? settings().callbacks,
|
|
356
356
|
admission: options.admission?.() ?? manifestOf({}),
|
|
357
357
|
scope: options.sessions().scope,
|
|
358
358
|
identities: pairing(),
|
|
@@ -4,22 +4,18 @@ 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 { object } from "../confirmation-description.mjs";
|
|
8
7
|
import { At as reportTelegramError, It as runtimeEnvelope, Lt as telegramRuntimeSource, c as currentInboundRequest, i as memberIdOf, o as telegramPrincipal } from "../../../_chunks/principal-DYl7vqOp.mjs";
|
|
9
8
|
import { formatAddress, parseAddress, scopedAddress } from "./sessions.mjs";
|
|
10
9
|
import { telegramText } from "./texts.mjs";
|
|
11
|
-
import { isWorkspaceCallback, workspaceAction, workspaceButtonToken } from "../workspace-action.mjs";
|
|
12
|
-
import { admitMessage } from "../admission.mjs";
|
|
10
|
+
import { isWorkspaceCallback, pressWorkspaceButton, queueWorkspaceButtonTurn, workspaceAction, workspaceButtonToken } from "../workspace-action.mjs";
|
|
13
11
|
import { currentChannel } from "../channel-scope.mjs";
|
|
14
12
|
async function handleWorkspaceCallback(input) {
|
|
15
13
|
const { query, context } = input;
|
|
16
14
|
const locale = query.from.languageCode;
|
|
17
15
|
let acknowledgement;
|
|
18
16
|
let showAlert = false;
|
|
19
|
-
let
|
|
17
|
+
let answer;
|
|
20
18
|
let principal;
|
|
21
|
-
const controller = new AbortController();
|
|
22
|
-
let timer;
|
|
23
19
|
try {
|
|
24
20
|
const token = workspaceButtonToken(query.data);
|
|
25
21
|
if (!query.message || query.from.isBot || token === void 0) acknowledgement = telegramText("callback", "unknown", locale);
|
|
@@ -33,7 +29,7 @@ async function handleWorkspaceCallback(input) {
|
|
|
33
29
|
caption: "",
|
|
34
30
|
raw: {}
|
|
35
31
|
};
|
|
36
|
-
|
|
32
|
+
answer = await pressWorkspaceButton(async (signal) => {
|
|
37
33
|
const lookup = await input.identities.lookup({
|
|
38
34
|
provider: "telegram",
|
|
39
35
|
externalId: query.from.id
|
|
@@ -42,7 +38,7 @@ async function handleWorkspaceCallback(input) {
|
|
|
42
38
|
acknowledgement = telegramText("callback", "forbidden", locale);
|
|
43
39
|
return;
|
|
44
40
|
}
|
|
45
|
-
|
|
41
|
+
signal.throwIfAborted();
|
|
46
42
|
principal = telegramPrincipal({
|
|
47
43
|
message,
|
|
48
44
|
member: lookup.member,
|
|
@@ -54,32 +50,23 @@ async function handleWorkspaceCallback(input) {
|
|
|
54
50
|
principal,
|
|
55
51
|
memberId: lookup.member.id,
|
|
56
52
|
input: { token },
|
|
57
|
-
signal
|
|
53
|
+
signal,
|
|
58
54
|
envelope: runtimeEnvelope({
|
|
59
55
|
source: telegramRuntimeSource(message, void 0, query.id),
|
|
60
56
|
sessionId: `telegram:callback:${query.id}`,
|
|
61
57
|
callId: query.id
|
|
62
58
|
})
|
|
63
59
|
});
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
reportTelegramError("workspace_callback_timeout", new Error("Telegram callback dispatch exceeded 10 seconds."), { callbackQueryId: query.id });
|
|
69
|
-
resolve(void 0);
|
|
70
|
-
}, 1e4);
|
|
71
|
-
});
|
|
72
|
-
result = await Promise.race([dispatch(), deadline]);
|
|
73
|
-
if (result !== void 0) {
|
|
74
|
-
acknowledgement = typeof result.acknowledgement === "string" && result.acknowledgement.length <= 200 ? result.acknowledgement : void 0;
|
|
75
|
-
showAlert = result.show_alert === true;
|
|
60
|
+
}, (error) => reportTelegramError("workspace_callback_timeout", error, { callbackQueryId: query.id }));
|
|
61
|
+
if (answer !== void 0) {
|
|
62
|
+
acknowledgement = answer.acknowledgement;
|
|
63
|
+
showAlert = answer.result.show_alert === true;
|
|
76
64
|
}
|
|
77
65
|
}
|
|
78
66
|
} catch (error) {
|
|
79
67
|
reportTelegramError("workspace_callback_failed", error, { callbackQueryId: query.id });
|
|
80
|
-
acknowledgement =
|
|
68
|
+
acknowledgement = telegramText("callback", "error", locale);
|
|
81
69
|
} finally {
|
|
82
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
83
70
|
try {
|
|
84
71
|
await context.telegram.answerCallbackQuery({
|
|
85
72
|
callbackQueryId: query.id,
|
|
@@ -90,8 +77,8 @@ async function handleWorkspaceCallback(input) {
|
|
|
90
77
|
reportTelegramError("workspace_callback_ack_failed", error, { callbackQueryId: query.id });
|
|
91
78
|
}
|
|
92
79
|
}
|
|
93
|
-
if (
|
|
94
|
-
const edit =
|
|
80
|
+
if (answer === void 0 || query.message === void 0 || principal === void 0) return;
|
|
81
|
+
const { edit, context: queued } = answer;
|
|
95
82
|
try {
|
|
96
83
|
if (typeof edit.text === "string" || edit.reply_markup !== void 0) {
|
|
97
84
|
const payload = {
|
|
@@ -113,12 +100,16 @@ async function handleWorkspaceCallback(input) {
|
|
|
113
100
|
} catch (error) {
|
|
114
101
|
reportTelegramError("workspace_callback_edit_failed", error, { callbackQueryId: query.id });
|
|
115
102
|
}
|
|
116
|
-
|
|
117
|
-
|
|
103
|
+
const message = query.message;
|
|
104
|
+
const member = principal;
|
|
105
|
+
const memberId = memberIdOf(member);
|
|
106
|
+
if (queued === void 0 || memberId === void 0) return;
|
|
107
|
+
try {
|
|
108
|
+
await queueWorkspaceButtonTurn({
|
|
118
109
|
identity: {
|
|
119
110
|
provider: "telegram",
|
|
120
111
|
externalId: query.from.id,
|
|
121
|
-
chat:
|
|
112
|
+
chat: message.chat.type === "private" ? "private" : "group",
|
|
122
113
|
turn: true
|
|
123
114
|
},
|
|
124
115
|
identities: input.identities,
|
|
@@ -126,29 +117,31 @@ async function handleWorkspaceCallback(input) {
|
|
|
126
117
|
languageCode: locale,
|
|
127
118
|
sendNotice: (text) => context.telegram.post(text),
|
|
128
119
|
noticeFailed: (error) => reportTelegramError("workspace_callback_notice_failed", error),
|
|
129
|
-
...
|
|
120
|
+
...message.chat.type === "private" ? { chat: {
|
|
130
121
|
channel: currentChannel("telegram"),
|
|
131
122
|
chatId: query.from.id
|
|
132
|
-
} } : {}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
123
|
+
} } : {},
|
|
124
|
+
memberId,
|
|
125
|
+
queue: async () => {
|
|
126
|
+
const sessions = currentInboundRequest()?.sessions;
|
|
127
|
+
if (sessions === void 0) throw new Error("The callback has no session transport.");
|
|
128
|
+
const address = scopedAddress(formatAddress({
|
|
129
|
+
chatId: message.chat.type === "private" ? query.from.id : message.chat.id,
|
|
130
|
+
messageThreadId: String(message.messageThreadId ?? ""),
|
|
131
|
+
conversationId: message.chat.type === "private" ? "" : message.messageId
|
|
132
|
+
}), input.scope);
|
|
133
|
+
const session = sessions.from(address);
|
|
134
|
+
if (session.send === void 0) throw new Error("The callback session cannot enqueue turns.");
|
|
135
|
+
await session.send(queued, {
|
|
136
|
+
auth: member,
|
|
137
|
+
turnPolicy: "queue",
|
|
138
|
+
state: {
|
|
139
|
+
chatId: message.chat.id,
|
|
140
|
+
chatType: message.chat.type,
|
|
141
|
+
messageThreadId: message.messageThreadId ?? null,
|
|
142
|
+
conversationId: parseAddress(address).conversationId || null
|
|
143
|
+
}
|
|
144
|
+
});
|
|
152
145
|
}
|
|
153
146
|
});
|
|
154
147
|
} catch (error) {
|
|
@@ -4,7 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
7
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BXGz_t3T.mjs";
|
|
8
8
|
import { runtimeRequestFailure } from "./runtime-request.mjs";
|
|
9
9
|
import { GET } from "eve/channels";
|
|
10
10
|
const HEALTH_ROUTE = "/eve/v1/telegram/health";
|
|
@@ -5,7 +5,7 @@ const __filename = __eveFileURLToPath(import.meta.url);
|
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
7
|
import { E as OntologyActionNotPermitted, Ft as principalRuntimeSource, It as runtimeEnvelope, L as needsConfirmation, Lt as telegramRuntimeSource, W as member, c as currentInboundRequest, j as ontologyTools, k as actionToolName } from "../../../_chunks/principal-DYl7vqOp.mjs";
|
|
8
|
-
import { t as agentBuild } from "../../../_chunks/build-metadata-
|
|
8
|
+
import { t as agentBuild } from "../../../_chunks/build-metadata-BXGz_t3T.mjs";
|
|
9
9
|
import { object } from "../confirmation-description.mjs";
|
|
10
10
|
var AttachmentLifecycleNotPermitted = class extends Error {
|
|
11
11
|
code = "attachment_lifecycle_not_permitted";
|
|
@@ -13,6 +13,7 @@ import { type Environment } from "../environment.js";
|
|
|
13
13
|
import { type AgentIdentities, type PairedMember } from "../identities.js";
|
|
14
14
|
import { type RuntimeToken } from "../runtime-token.js";
|
|
15
15
|
import { type ChatArchive } from "../chat-archive.js";
|
|
16
|
+
import type { WorkspaceActionHook } from "../workspace-action.js";
|
|
16
17
|
import { type WhatsAppEcho } from "./echoes.js";
|
|
17
18
|
/**
|
|
18
19
|
* The SegmentStream WhatsApp channel (`agent-design.md`, "Channels"): eve's Chat SDK bridge with
|
|
@@ -62,6 +63,8 @@ export interface WhatsAppChannelOptions {
|
|
|
62
63
|
/** The WhatsApp channel `name` the mount declares, or `null` when it declares no such channel. */
|
|
63
64
|
readonly settings: (name: string) => WhatsAppSettings | null;
|
|
64
65
|
readonly admission: () => AdmissionSettings;
|
|
66
|
+
/** The action a press of a workspace button calls, from `buttons` in `agent/segmentstream.json`. */
|
|
67
|
+
readonly buttons?: () => WorkspaceActionHook | undefined;
|
|
65
68
|
readonly environment?: Environment;
|
|
66
69
|
readonly token?: RuntimeToken;
|
|
67
70
|
/** For the proxy and the ontology service; tests pass a fake. */
|
|
@@ -22,6 +22,7 @@ import { agentIdentities } from "../identities.mjs";
|
|
|
22
22
|
import { runtimeToken } from "../runtime-token.mjs";
|
|
23
23
|
import { chatArchive } from "../chat-archive.mjs";
|
|
24
24
|
import { recordHandledMessage } from "../chat-messages.mjs";
|
|
25
|
+
import { handleWorkspaceButton } from "./callbacks.mjs";
|
|
25
26
|
import { recordEchoes, whatsappEchoes } from "./echoes.mjs";
|
|
26
27
|
import { withPlainLinks } from "./links.mjs";
|
|
27
28
|
import { collectSent, noteSent, recordSent } from "./sent.mjs";
|
|
@@ -304,6 +305,49 @@ function segmentstreamWhatsApp(options) {
|
|
|
304
305
|
return principal === null || answer === void 0 ? principal : withButtonDecision(principal, [answer]);
|
|
305
306
|
}
|
|
306
307
|
});
|
|
308
|
+
bridge.bot.onAction(async (event) => {
|
|
309
|
+
const thread = event.thread;
|
|
310
|
+
await handleWorkspaceButton({
|
|
311
|
+
press: {
|
|
312
|
+
actionId: event.actionId,
|
|
313
|
+
messageId: event.messageId,
|
|
314
|
+
...event.value === void 0 ? {} : { value: event.value },
|
|
315
|
+
user: {
|
|
316
|
+
userId: event.user.userId,
|
|
317
|
+
fullName: event.user.fullName
|
|
318
|
+
},
|
|
319
|
+
raw: event.raw,
|
|
320
|
+
thread
|
|
321
|
+
},
|
|
322
|
+
configuration: options.buttons?.(),
|
|
323
|
+
admission: options.admission(),
|
|
324
|
+
identities: pairing(),
|
|
325
|
+
environment,
|
|
326
|
+
archive: chats,
|
|
327
|
+
principal: whatsappPrincipal,
|
|
328
|
+
pair: async (sender, display) => {
|
|
329
|
+
const admission = options.admission();
|
|
330
|
+
const text = localizedText(admission.replies.pairingRequired);
|
|
331
|
+
if (text === void 0 || thread === null) return;
|
|
332
|
+
const code = (await pairing().createPairing({
|
|
333
|
+
provider: "whatsapp",
|
|
334
|
+
externalId: sender,
|
|
335
|
+
...display === void 0 ? {} : { display }
|
|
336
|
+
})).code;
|
|
337
|
+
await thread.post(pairingMessage(text, localizedText(admission.replies.pairingButton), `${appOrigin(environment)}/agent/connect?code=${encodeURIComponent(code)}`));
|
|
338
|
+
},
|
|
339
|
+
turn: async (text, auth) => {
|
|
340
|
+
if (thread !== null) await bridge.send(text, {
|
|
341
|
+
thread: thread.id,
|
|
342
|
+
idempotencyKey: currentKey("whatsapp", `${event.messageId}:button`),
|
|
343
|
+
auth
|
|
344
|
+
});
|
|
345
|
+
},
|
|
346
|
+
report,
|
|
347
|
+
token,
|
|
348
|
+
...options.fetch === void 0 ? {} : { fetch: options.fetch }
|
|
349
|
+
});
|
|
350
|
+
});
|
|
307
351
|
bridge.bot.onDirectMessage(async (thread, message) => {
|
|
308
352
|
const auth = await receive({
|
|
309
353
|
thread,
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { SessionAuthContext } from "eve/context";
|
|
2
|
+
import type { AdmissionSettings } from "../../config.js";
|
|
3
|
+
import type { ChatArchive } from "../chat-archive.js";
|
|
4
|
+
import type { Environment } from "../environment.js";
|
|
5
|
+
import type { AgentIdentities, PairedMember } from "../identities.js";
|
|
6
|
+
import type { RuntimeToken } from "../runtime-token.js";
|
|
7
|
+
import { type WorkspaceActionHook } from "../workspace-action.js";
|
|
8
|
+
/** A press of a button under a WhatsApp message, as the Chat SDK hands it over. */
|
|
9
|
+
export interface WhatsAppPress {
|
|
10
|
+
/** The button's id: a workspace button's is `ws:<token>`, a template quick reply's its payload. */
|
|
11
|
+
readonly actionId: string;
|
|
12
|
+
/** WhatsApp's id of the press, which the person sees as their own message. */
|
|
13
|
+
readonly messageId: string;
|
|
14
|
+
/** The button's label. */
|
|
15
|
+
readonly value?: string;
|
|
16
|
+
readonly user: {
|
|
17
|
+
readonly userId: string;
|
|
18
|
+
readonly fullName?: string;
|
|
19
|
+
};
|
|
20
|
+
/** The inbound message, whose `context.id` names the message the button was under. */
|
|
21
|
+
readonly raw: unknown;
|
|
22
|
+
readonly thread: {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
post(text: string): Promise<unknown>;
|
|
25
|
+
} | null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A press of a workspace button under a WhatsApp message: a reply button the workspace sent with
|
|
29
|
+
* `agent_chat.send`, or a template's quick reply whose payload is `ws:<token>`, which reaches a
|
|
30
|
+
* person outside the 24 hours. The action `buttons` names runs as the member paired with the
|
|
31
|
+
* person, with the verified chat, the message the button was under and the person as its runtime
|
|
32
|
+
* source. A press is a message the person sees as theirs, and reopens the 24 hours: the chat's
|
|
33
|
+
* history keeps it, and the action's `acknowledgement` is sent as a message of the workspace's,
|
|
34
|
+
* kept too. WhatsApp cannot edit a message, so `edit` does nothing; `context` queues one turn in
|
|
35
|
+
* the person's session. Anyone not paired gets the workspace's pairing reply and nothing is called.
|
|
36
|
+
* A redelivered press is recorded once and runs nothing again, and a press the history cannot
|
|
37
|
+
* record runs nothing; business idempotency is the action's.
|
|
38
|
+
*/
|
|
39
|
+
export declare function handleWorkspaceButton(input: {
|
|
40
|
+
readonly press: WhatsAppPress;
|
|
41
|
+
readonly configuration: WorkspaceActionHook | undefined;
|
|
42
|
+
readonly admission: AdmissionSettings;
|
|
43
|
+
readonly identities: AgentIdentities;
|
|
44
|
+
readonly environment: Environment;
|
|
45
|
+
readonly archive: () => ChatArchive;
|
|
46
|
+
/** The session principal of a paired member's WhatsApp account. */
|
|
47
|
+
readonly principal: (member: PairedMember, sender: string) => SessionAuthContext;
|
|
48
|
+
/** Sends the workspace's pairing reply to someone not paired. */
|
|
49
|
+
readonly pair: (sender: string, display: string | undefined) => Promise<void>;
|
|
50
|
+
/** Queues one turn in the person's session, as a message of theirs would. */
|
|
51
|
+
readonly turn: (text: string, auth: SessionAuthContext) => Promise<void>;
|
|
52
|
+
readonly report: (operation: string, error: unknown) => void;
|
|
53
|
+
readonly token?: RuntimeToken;
|
|
54
|
+
readonly fetch?: typeof fetch;
|
|
55
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { fileURLToPath as __eveFileURLToPath } from "node:url";
|
|
2
|
+
import { dirname as __eveDirname } from "node:path";
|
|
3
|
+
import { createRequire as __eveCreateRequire } from "node:module";
|
|
4
|
+
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
|
+
__eveDirname(__filename);
|
|
6
|
+
__eveCreateRequire(import.meta.url);
|
|
7
|
+
import { It as runtimeEnvelope, bt as withChatAddress } from "../../../_chunks/principal-DYl7vqOp.mjs";
|
|
8
|
+
import { currentChannel } from "../channel-scope.mjs";
|
|
9
|
+
import { recordChatMessage } from "../chat-messages.mjs";
|
|
10
|
+
import { object } from "../confirmation-description.mjs";
|
|
11
|
+
import { pressWorkspaceButton, queueWorkspaceButtonTurn, workspaceAction, workspaceButtonToken } from "../workspace-action.mjs";
|
|
12
|
+
const WAMID = /^wamid\.[A-Za-z0-9+/=_-]{8,256}$/;
|
|
13
|
+
async function handleWorkspaceButton(input) {
|
|
14
|
+
const { press, report } = input;
|
|
15
|
+
const token = workspaceButtonToken(press.actionId);
|
|
16
|
+
const thread = press.thread;
|
|
17
|
+
if (token === void 0 || thread === null) return;
|
|
18
|
+
if (input.configuration === void 0) {
|
|
19
|
+
report("workspace_button_unconfigured", new Error("A workspace button was pressed in WhatsApp, but buttons names no action."));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const sender = press.user.userId;
|
|
23
|
+
const display = press.user.fullName?.trim() || void 0;
|
|
24
|
+
const lookup = await input.identities.lookup({
|
|
25
|
+
provider: "whatsapp",
|
|
26
|
+
externalId: sender
|
|
27
|
+
});
|
|
28
|
+
if (lookup.status !== "paired") {
|
|
29
|
+
if (lookup.status === "unknown") await input.pair(sender, display).catch((error) => report("workspace_button_pairing_failed", error));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const channel = currentChannel("whatsapp");
|
|
33
|
+
const archive = input.archive();
|
|
34
|
+
const label = press.value?.trim() || token;
|
|
35
|
+
const chat = {
|
|
36
|
+
provider: channel,
|
|
37
|
+
chatId: thread.id,
|
|
38
|
+
chatType: "private"
|
|
39
|
+
};
|
|
40
|
+
if (await recordChatMessage(archive, {
|
|
41
|
+
...chat,
|
|
42
|
+
participantIds: [lookup.member.id],
|
|
43
|
+
eventId: `whatsapp-press:${channel}:${thread.id}:${press.messageId}`,
|
|
44
|
+
direction: "inbound",
|
|
45
|
+
by: "human",
|
|
46
|
+
senderId: sender,
|
|
47
|
+
senderName: display ?? null,
|
|
48
|
+
messageId: press.messageId,
|
|
49
|
+
text: label
|
|
50
|
+
}).catch((error) => {
|
|
51
|
+
report("workspace_button_record_failed", error);
|
|
52
|
+
return false;
|
|
53
|
+
}) === false) return;
|
|
54
|
+
const context = object(object(press.raw).context);
|
|
55
|
+
const card = typeof context.id === "string" && WAMID.test(context.id) ? context.id : void 0;
|
|
56
|
+
const address = {
|
|
57
|
+
provider: "whatsapp",
|
|
58
|
+
...channel === "whatsapp" ? {} : { channel },
|
|
59
|
+
chatId: thread.id,
|
|
60
|
+
threadId: "",
|
|
61
|
+
kind: "private",
|
|
62
|
+
peer: sender
|
|
63
|
+
};
|
|
64
|
+
const principal = withChatAddress(input.principal(lookup.member, sender), address);
|
|
65
|
+
let answer;
|
|
66
|
+
try {
|
|
67
|
+
answer = await pressWorkspaceButton((signal) => workspaceAction({
|
|
68
|
+
configuration: input.configuration,
|
|
69
|
+
principal,
|
|
70
|
+
memberId: lookup.member.id,
|
|
71
|
+
input: { token },
|
|
72
|
+
signal,
|
|
73
|
+
envelope: runtimeEnvelope({
|
|
74
|
+
source: {
|
|
75
|
+
provider: "whatsapp",
|
|
76
|
+
...channel === "whatsapp" ? {} : { channel },
|
|
77
|
+
chat_id: thread.id,
|
|
78
|
+
thread_id: "",
|
|
79
|
+
kind: "private",
|
|
80
|
+
peer: sender,
|
|
81
|
+
...card === void 0 ? {} : { message_id: card },
|
|
82
|
+
sender
|
|
83
|
+
},
|
|
84
|
+
sessionId: `whatsapp:button:${press.messageId}`,
|
|
85
|
+
callId: press.messageId
|
|
86
|
+
}),
|
|
87
|
+
environment: input.environment,
|
|
88
|
+
...input.token === void 0 ? {} : { token: input.token },
|
|
89
|
+
...input.fetch === void 0 ? {} : { fetch: input.fetch }
|
|
90
|
+
}), (error) => report("workspace_button_timeout", error));
|
|
91
|
+
} catch (error) {
|
|
92
|
+
report("workspace_button_failed", error);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (answer === void 0) return;
|
|
96
|
+
if (answer.result.edit !== void 0) report("workspace_button_edit_unsupported", new Error("WhatsApp cannot edit a message; the action's edit was not applied."));
|
|
97
|
+
const { acknowledgement, context: queued } = answer;
|
|
98
|
+
if (acknowledgement !== void 0) try {
|
|
99
|
+
const sent = object(await thread.post(acknowledgement));
|
|
100
|
+
await recordChatMessage(archive, {
|
|
101
|
+
...chat,
|
|
102
|
+
participantIds: [lookup.member.id],
|
|
103
|
+
eventId: `whatsapp-press:${channel}:${thread.id}:${press.messageId}:acknowledgement`,
|
|
104
|
+
direction: "outbound",
|
|
105
|
+
by: "bot",
|
|
106
|
+
senderId: null,
|
|
107
|
+
messageId: typeof sent.id === "string" && sent.id !== "" ? sent.id : `${press.messageId}:acknowledgement`,
|
|
108
|
+
text: acknowledgement
|
|
109
|
+
});
|
|
110
|
+
} catch (error) {
|
|
111
|
+
report("workspace_button_ack_failed", error);
|
|
112
|
+
}
|
|
113
|
+
if (queued === void 0) return;
|
|
114
|
+
try {
|
|
115
|
+
await queueWorkspaceButtonTurn({
|
|
116
|
+
identity: {
|
|
117
|
+
provider: "whatsapp",
|
|
118
|
+
externalId: sender,
|
|
119
|
+
chat: "private",
|
|
120
|
+
turn: true
|
|
121
|
+
},
|
|
122
|
+
identities: input.identities,
|
|
123
|
+
settings: input.admission,
|
|
124
|
+
sendNotice: (text) => thread.post(text),
|
|
125
|
+
noticeFailed: (error) => report("workspace_button_notice_failed", error),
|
|
126
|
+
chat: {
|
|
127
|
+
channel,
|
|
128
|
+
chatId: thread.id,
|
|
129
|
+
recipient: sender
|
|
130
|
+
},
|
|
131
|
+
memberId: lookup.member.id,
|
|
132
|
+
queue: () => input.turn(queued, principal)
|
|
133
|
+
});
|
|
134
|
+
} catch (error) {
|
|
135
|
+
report("workspace_button_turn_failed", error);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export { handleWorkspaceButton };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SessionAuthContext } from "eve/context";
|
|
2
|
+
import { admitMessage } from "./admission.js";
|
|
2
3
|
import type { RuntimeEnvelope } from "./runtime-envelope.js";
|
|
3
4
|
import type { Environment } from "./environment.js";
|
|
4
5
|
import type { RuntimeToken } from "./runtime-token.js";
|
|
@@ -12,6 +13,35 @@ export declare const WORKSPACE_TOKEN_BYTES = 60;
|
|
|
12
13
|
export declare const isWorkspaceCallback: (data?: string) => boolean;
|
|
13
14
|
/** A workspace button's token, or undefined when it is empty or longer than the limit. */
|
|
14
15
|
export declare function workspaceButtonToken(data: string | undefined): string | undefined;
|
|
16
|
+
/** How long a workspace button's action may take before the press is given up, in every channel. */
|
|
17
|
+
export declare const WORKSPACE_BUTTON_DEADLINE_MS = 10000;
|
|
18
|
+
/** What a workspace button's action answered, within what every channel carries. */
|
|
19
|
+
export interface WorkspaceButtonAnswer {
|
|
20
|
+
/** A short reply to the person who pressed the button. */
|
|
21
|
+
readonly acknowledgement?: string;
|
|
22
|
+
/** The action's edit of the message the button was under, which each channel reads its own way. */
|
|
23
|
+
readonly edit: Record<string, unknown>;
|
|
24
|
+
/** One turn to queue in the chat's session. */
|
|
25
|
+
readonly context?: string;
|
|
26
|
+
/** Everything the action returned, for what only one channel reads. */
|
|
27
|
+
readonly result: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Runs a workspace button's action within the deadline every channel gives it, and reads its
|
|
31
|
+
* answer. Undefined when `dispatch` answers nothing, or when the action took too long: its call is
|
|
32
|
+
* aborted and `timedOut` is told. A failure of the action is thrown.
|
|
33
|
+
*/
|
|
34
|
+
export declare function pressWorkspaceButton(dispatch: (signal: AbortSignal) => Promise<Record<string, unknown> | undefined>, timedOut: (error: Error) => void): Promise<WorkspaceButtonAnswer | undefined>;
|
|
35
|
+
/**
|
|
36
|
+
* Queues the turn a press asked for as a message of the presser's would start one: admitted as
|
|
37
|
+
* theirs first, and none while a member of staff holds the chat or the presser is not the member
|
|
38
|
+
* the action ran as.
|
|
39
|
+
*/
|
|
40
|
+
export declare function queueWorkspaceButtonTurn(input: Parameters<typeof admitMessage>[0] & {
|
|
41
|
+
/** The member the button's action ran as. */
|
|
42
|
+
readonly memberId: string;
|
|
43
|
+
readonly queue: () => Promise<unknown>;
|
|
44
|
+
}): Promise<void>;
|
|
15
45
|
export interface WorkspaceActionHook {
|
|
16
46
|
readonly entity: string;
|
|
17
47
|
readonly action: string;
|
|
@@ -4,6 +4,7 @@ import { createRequire as __eveCreateRequire } from "node:module";
|
|
|
4
4
|
const __filename = __eveFileURLToPath(import.meta.url);
|
|
5
5
|
__eveDirname(__filename);
|
|
6
6
|
__eveCreateRequire(import.meta.url);
|
|
7
|
+
import { admitMessage } from "./admission.mjs";
|
|
7
8
|
import { L as needsConfirmation, W as member, j as ontologyTools, k as actionToolName } from "../../_chunks/principal-DYl7vqOp.mjs";
|
|
8
9
|
import { object } from "./confirmation-description.mjs";
|
|
9
10
|
var WorkspaceActionAlreadyDispatched = class extends Error {};
|
|
@@ -14,6 +15,39 @@ function workspaceButtonToken(data) {
|
|
|
14
15
|
const token = isWorkspaceCallback(data) ? data.slice(3) : "";
|
|
15
16
|
return token === "" || new TextEncoder().encode(token).byteLength > 60 ? void 0 : token;
|
|
16
17
|
}
|
|
18
|
+
const WORKSPACE_BUTTON_DEADLINE_MS = 1e4;
|
|
19
|
+
const ACKNOWLEDGEMENT_LIMIT = 200;
|
|
20
|
+
const CONTEXT_LIMIT = 16384;
|
|
21
|
+
async function pressWorkspaceButton(dispatch, timedOut) {
|
|
22
|
+
const controller = new AbortController();
|
|
23
|
+
let timer;
|
|
24
|
+
let result;
|
|
25
|
+
try {
|
|
26
|
+
result = await Promise.race([dispatch(controller.signal), new Promise((resolve) => {
|
|
27
|
+
timer = setTimeout(() => {
|
|
28
|
+
controller.abort();
|
|
29
|
+
timedOut(new Error("A workspace button's action took more than 10 seconds."));
|
|
30
|
+
resolve(void 0);
|
|
31
|
+
}, WORKSPACE_BUTTON_DEADLINE_MS);
|
|
32
|
+
})]);
|
|
33
|
+
} finally {
|
|
34
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
35
|
+
}
|
|
36
|
+
if (result === void 0) return;
|
|
37
|
+
const { acknowledgement, context } = result;
|
|
38
|
+
return {
|
|
39
|
+
...typeof acknowledgement === "string" && acknowledgement.trim() !== "" && acknowledgement.length <= ACKNOWLEDGEMENT_LIMIT ? { acknowledgement } : {},
|
|
40
|
+
edit: object(result.edit),
|
|
41
|
+
...typeof context === "string" && context.trim() !== "" && context.length <= CONTEXT_LIMIT ? { context } : {},
|
|
42
|
+
result
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async function queueWorkspaceButtonTurn(input) {
|
|
46
|
+
const { memberId, queue, ...admission } = input;
|
|
47
|
+
const admitted = await admitMessage(admission);
|
|
48
|
+
if (admitted.status !== "existing" && admitted.status !== "created" || admitted.member.id !== memberId || admitted.handled === true) return;
|
|
49
|
+
await queue();
|
|
50
|
+
}
|
|
17
51
|
async function workspaceAction(input) {
|
|
18
52
|
const action = actionToolName(input.configuration.entity, input.configuration.action);
|
|
19
53
|
const tools = ontologyTools({
|
|
@@ -32,4 +66,4 @@ async function workspaceAction(input) {
|
|
|
32
66
|
if (object(envelope.validation).ok === false) throw new Error("The workspace hook rejected its event.");
|
|
33
67
|
return object(envelope.result ?? envelope);
|
|
34
68
|
}
|
|
35
|
-
export { WORKSPACE_BUTTON_PREFIX, WORKSPACE_TOKEN_BYTES, WorkspaceActionAlreadyDispatched, isWorkspaceCallback, workspaceAction, workspaceButtonToken };
|
|
69
|
+
export { WORKSPACE_BUTTON_DEADLINE_MS, WORKSPACE_BUTTON_PREFIX, WORKSPACE_TOKEN_BYTES, WorkspaceActionAlreadyDispatched, isWorkspaceCallback, pressWorkspaceButton, queueWorkspaceButtonTurn, workspaceAction, workspaceButtonToken };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.70.0",
|
|
4
4
|
"description": "The SegmentStream workspace agent: the ontology as tools, acting as the member who writes, and the browser, Telegram, WhatsApp and Slack through the workspace's connections.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "SegmentStream",
|