@skillcdn/console 0.6.1 → 0.6.2
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/README.md +2 -2
- package/dist/agent-page.d.ts.map +1 -1
- package/dist/agent-page.js +38 -28
- package/dist/agent-page.js.map +1 -1
- package/dist/agent-usage.d.ts +9 -6
- package/dist/agent-usage.d.ts.map +1 -1
- package/dist/agent-usage.js +38 -8
- package/dist/agent-usage.js.map +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +1 -1
- package/dist/api.js.map +1 -1
- package/dist/cli/cli.d.ts.map +1 -1
- package/dist/cli/cli.js +65 -0
- package/dist/cli/cli.js.map +1 -1
- package/dist/cli/help.js +3 -3
- package/dist/cli/help.js.map +1 -1
- package/dist/client.d.ts +7 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -1
- package/dist/client.js.map +1 -1
- package/dist/components/agent-page.d.ts +33 -8
- package/dist/components/agent-page.d.ts.map +1 -1
- package/dist/components/agent-page.js +98 -32
- package/dist/components/agent-page.js.map +1 -1
- package/dist/components/calendar.d.ts +24 -4
- package/dist/components/calendar.d.ts.map +1 -1
- package/dist/components/calendar.js +78 -35
- package/dist/components/calendar.js.map +1 -1
- package/dist/console.d.ts.map +1 -1
- package/dist/console.js +39 -11
- package/dist/console.js.map +1 -1
- package/dist/data.d.ts +2 -0
- package/dist/data.d.ts.map +1 -1
- package/dist/data.js +4 -0
- package/dist/data.js.map +1 -1
- package/dist/desk-page.js +5 -5
- package/dist/desk-page.js.map +1 -1
- package/dist/feed-words.js +6 -0
- package/dist/feed-words.js.map +1 -1
- package/dist/i18n/en.d.ts +6 -0
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +8 -1
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/ko.d.ts.map +1 -1
- package/dist/i18n/ko.js +24 -17
- package/dist/i18n/ko.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/plugin/capabilities.d.ts +1 -1
- package/dist/plugin/capabilities.d.ts.map +1 -1
- package/dist/plugin/capabilities.js +6 -1
- package/dist/plugin/capabilities.js.map +1 -1
- package/dist/plugin/context.d.ts +62 -1
- package/dist/plugin/context.d.ts.map +1 -1
- package/dist/plugin/limits.d.ts +7 -0
- package/dist/plugin/limits.d.ts.map +1 -1
- package/dist/plugin/limits.js +7 -0
- package/dist/plugin/limits.js.map +1 -1
- package/dist/plugin/testing.d.ts +2 -0
- package/dist/plugin/testing.d.ts.map +1 -1
- package/dist/plugin/testing.js +2 -0
- package/dist/plugin/testing.js.map +1 -1
- package/dist/rules.js +1 -1
- package/dist/rules.js.map +1 -1
- package/dist/runner/claude.d.ts.map +1 -1
- package/dist/runner/claude.js +138 -9
- package/dist/runner/claude.js.map +1 -1
- package/dist/runner/desk.d.ts.map +1 -1
- package/dist/runner/desk.js +11 -4
- package/dist/runner/desk.js.map +1 -1
- package/dist/runner/message.d.ts +32 -2
- package/dist/runner/message.d.ts.map +1 -1
- package/dist/runner/message.js +163 -27
- package/dist/runner/message.js.map +1 -1
- package/dist/runner/runner.d.ts +15 -1
- package/dist/runner/runner.d.ts.map +1 -1
- package/dist/runner/runner.js +131 -15
- package/dist/runner/runner.js.map +1 -1
- package/dist/runner/slack.d.ts +17 -11
- package/dist/runner/slack.d.ts.map +1 -1
- package/dist/runner/slack.js +1007 -105
- package/dist/runner/slack.js.map +1 -1
- package/dist/runner/types.d.ts +110 -5
- package/dist/runner/types.d.ts.map +1 -1
- package/dist/runner/types.js.map +1 -1
- package/dist/runner.d.ts +4 -4
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +3 -3
- package/dist/runner.js.map +1 -1
- package/dist/schemas.d.ts +103 -0
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +55 -0
- package/dist/schemas.js.map +1 -1
- package/docs/specs/cli.md +2 -1
- package/docs/specs/plugins.md +7 -2
- package/docs/specs/rest.md +4 -2
- package/docs/specs/runner.md +50 -19
- package/package.json +1 -1
- package/skills/building-a-plugin/SKILL.md +3 -3
- package/skills/planning-the-board/references/setting-up.md +1 -1
- package/skills/running-an-agent/SKILL.md +10 -67
- package/skills/running-an-agent/references/preparing-the-machine.md +17 -0
- package/skills/running-an-agent/references/several-tasks.md +1 -1
- package/skills/running-an-agent/references/slack-app.md +36 -6
- package/skills/running-an-agent/references/the-account-and-the-invitation.md +5 -0
- package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +3 -0
- package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +16 -0
- package/skills/running-an-agent/references/your-first-task.md +14 -0
- package/skills/working-the-board/SKILL.md +9 -13
- package/skills/working-the-board/references/a-pack-skills-questions.md +3 -0
- package/skills/working-the-board/references/a-plugins-commands.md +3 -0
- package/skills/working-the-board/references/a-review.md +3 -0
- package/skills/working-the-board/references/an-open-run.md +3 -0
- package/skills/working-the-board/references/requirements.md +7 -0
- package/skills/working-the-board/references/what-they-were-told.md +3 -0
- package/skills/working-the-board/references/work-given-in-chat.md +5 -0
- package/src/agent-page.tsx +41 -40
- package/src/agent-usage.ts +55 -12
- package/src/api.ts +7 -0
- package/src/cli/cli.ts +84 -0
- package/src/cli/help.ts +3 -3
- package/src/client.ts +11 -0
- package/src/components/agent-page.tsx +114 -36
- package/src/components/calendar.tsx +92 -36
- package/src/console.tsx +46 -10
- package/src/data.ts +6 -0
- package/src/desk-page.tsx +5 -5
- package/src/feed-words.ts +6 -0
- package/src/i18n/en.ts +11 -1
- package/src/i18n/ko.ts +30 -17
- package/src/index.ts +2 -0
- package/src/plugin/capabilities.ts +8 -1
- package/src/plugin/context.ts +63 -0
- package/src/plugin/limits.ts +7 -0
- package/src/plugin/testing.ts +4 -0
- package/src/rules.ts +1 -1
- package/src/runner/claude.ts +154 -9
- package/src/runner/desk.ts +13 -4
- package/src/runner/message.ts +176 -29
- package/src/runner/runner.ts +173 -15
- package/src/runner/slack.ts +1273 -124
- package/src/runner/types.ts +102 -2
- package/src/runner.ts +16 -1
- package/src/schemas.ts +64 -0
package/dist/runner/slack.js
CHANGED
|
@@ -1,20 +1,74 @@
|
|
|
1
1
|
import * as z from "zod/mini";
|
|
2
|
+
import { MAX_SIGNAL_LENGTH } from "../limits.js";
|
|
2
3
|
import { withoutForbiddenCodePoints } from "../text.js";
|
|
4
|
+
import { wakeTextOf } from "./message.js";
|
|
3
5
|
// The `slack` wake source (ADR-0085): one app for one agent account, which receives over its
|
|
4
6
|
// Socket Mode connection the events the agent's own member of the workspace can see, and raises
|
|
5
7
|
// a wake for what is said to the agent: a direct message, a message of a group conversation, a
|
|
6
8
|
// message that mentions it, and any message of a channel `runner.json` names. The wake names the
|
|
7
9
|
// thread, so that what is said in one thread is one thing to the agent, and
|
|
8
10
|
// carries the words as someone else's: what the message shows, a table, a list and code among
|
|
9
|
-
// it, read from its blocks where it has them.
|
|
10
|
-
//
|
|
11
|
-
//
|
|
11
|
+
// it, read from its blocks where it has them. Its first line is the source's own, and says of
|
|
12
|
+
// the account that wrote what the event says of it: its id, its workspace where that is
|
|
13
|
+
// another than the agent's (ADR-0160), that it wrote through an app where it did, and, where the
|
|
14
|
+
// console knows which member the account is (ADR-0164), that member and whether they supervise
|
|
15
|
+
// the agent. A wake is
|
|
16
|
+
// raised in parts (ADR-0163): the source's own lines, which nothing a message holds reaches,
|
|
17
|
+
// and what was written, a message someone passed on apart from the words of whoever passed it
|
|
18
|
+
// on. With the member's own token, where the account keeps it, the source reads what was said
|
|
19
|
+
// in the conversation before the message, and the wake carries what the agent missed of it,
|
|
20
|
+
// who else there writes as an agent account does, and what to judge by whether to answer.
|
|
21
|
+
// Written from the messenger's own documentation of Socket Mode, of the Events API, of its
|
|
22
|
+
// blocks and of the two methods that read a conversation; what an event or an answer holds is
|
|
23
|
+
// read by schema, bounded, and never run.
|
|
12
24
|
/** The secret of the account's store that holds the app's token for the socket (`xapp-...`). */
|
|
13
25
|
export const SLACK_APP_TOKEN = "slack.app_token";
|
|
26
|
+
/**
|
|
27
|
+
* The secret that holds the member's own token (`xoxp-...`), which the agent sends with: with it
|
|
28
|
+
* the source reads the conversation a wake is raised in, as the scopes the events ask for allow.
|
|
29
|
+
*/
|
|
30
|
+
export const SLACK_USER_TOKEN = "slack.user_token";
|
|
14
31
|
/** Where a connection is asked for. */
|
|
15
32
|
const OPEN_URL = "https://slack.com/api/apps.connections.open";
|
|
16
|
-
/**
|
|
33
|
+
/** Where a conversation is read outside its threads, the newest first, and where a thread is, the oldest first. */
|
|
34
|
+
const HISTORY_URL = "https://slack.com/api/conversations.history";
|
|
35
|
+
const REPLIES_URL = "https://slack.com/api/conversations.replies";
|
|
36
|
+
/** The most of a message a wake carries: the agent reads the rest with the messenger's own tool. */
|
|
17
37
|
const MAX_WAKE_TEXT = 4000;
|
|
38
|
+
/**
|
|
39
|
+
* The most the message itself takes of the text a signal carries, its lines begun as that text
|
|
40
|
+
* begins them: what is left is for what was said before it and for who else is there. A message of a
|
|
41
|
+
* great many short lines comes to more than this within `MAX_WAKE_TEXT`, and is cut shorter.
|
|
42
|
+
*/
|
|
43
|
+
const MAX_LEAD_LENGTH = MAX_SIGNAL_LENGTH - 2500;
|
|
44
|
+
/** How many of the things that hang under a message a wake carries apart, as it names so many files. */
|
|
45
|
+
const MAX_UNDER = 5;
|
|
46
|
+
/**
|
|
47
|
+
* What a wake carries of what was said before the message that raised it: so many messages at
|
|
48
|
+
* the most, so much of each, and so much of them together as the text a signal carries holds.
|
|
49
|
+
*/
|
|
50
|
+
const MAX_BEFORE = 10;
|
|
51
|
+
const MAX_BEFORE_TEXT = 500;
|
|
52
|
+
const MAX_BEFORE_LENGTH = 3000;
|
|
53
|
+
/** How many messages a wake reads of a conversation, and of a thread: a page of so many, so many pages. */
|
|
54
|
+
const READ_MESSAGES = 50;
|
|
55
|
+
const READ_THREAD_PAGE = 200;
|
|
56
|
+
const READ_THREAD_PAGES = 5;
|
|
57
|
+
/** How long all that one wake reads is waited for, however many requests it takes, before the wake goes without. */
|
|
58
|
+
const READ_WAIT_MS = 5000;
|
|
59
|
+
/** How many wakes may wait behind one whose conversation is read: with more, it goes with nothing read. */
|
|
60
|
+
const MAX_WAITING = 5;
|
|
61
|
+
/** How long the source waits before it opens the connection the messenger asked for in place of the one it ended. */
|
|
62
|
+
const REOPEN_WAIT_MS = 1000;
|
|
63
|
+
/** How many others that write through an app a wake names. */
|
|
64
|
+
const MAX_OTHERS = 5;
|
|
65
|
+
/** Of so many accounts the console is asked at once who they are: what one asking takes. */
|
|
66
|
+
const MAX_WRITERS_ASKED = 100;
|
|
67
|
+
/** What the command reads of a conversation where nothing says how much, and at the most. */
|
|
68
|
+
export const SLACK_READ_DEFAULT = 20;
|
|
69
|
+
export const SLACK_READ_MOST = 100;
|
|
70
|
+
/** How long what the command reads is at the most, every message of it together: the newest are kept. */
|
|
71
|
+
const MAX_READ_LENGTH = 12_000;
|
|
18
72
|
/** The deepest a list of a message is indented in a wake. */
|
|
19
73
|
const MAX_LIST_DEPTH = 8;
|
|
20
74
|
/** How many files of a message a wake names, and how much of a name. */
|
|
@@ -55,11 +109,57 @@ const eventSchema = z.object({
|
|
|
55
109
|
files: z.optional(z.unknown()),
|
|
56
110
|
ts: z.optional(stamp),
|
|
57
111
|
thread_ts: z.optional(stamp),
|
|
112
|
+
/**
|
|
113
|
+
* The workspace the message came from, and in a conversation shared between workspaces the
|
|
114
|
+
* one its writer is a member of. Read loosely (`workspaceOf`): one that is not spelled as the
|
|
115
|
+
* messenger spells an id says nothing, and never lets go of the wake.
|
|
116
|
+
*/
|
|
117
|
+
team: z.optional(z.unknown()),
|
|
118
|
+
user_team: z.optional(z.unknown()),
|
|
58
119
|
});
|
|
59
120
|
const payloadSchema = z.object({
|
|
60
121
|
event: z.optional(eventSchema),
|
|
61
|
-
/**
|
|
62
|
-
|
|
122
|
+
/**
|
|
123
|
+
* The installation the event was delivered for: the member the app acts for, with the
|
|
124
|
+
* workspace that member is of and, where the workspace is one of several of an organization,
|
|
125
|
+
* the organization. The two are read loosely, as an event's own workspace is.
|
|
126
|
+
*/
|
|
127
|
+
authorizations: z.optional(z
|
|
128
|
+
.array(z.object({
|
|
129
|
+
user_id: z.optional(z.nullable(id)),
|
|
130
|
+
team_id: z.optional(z.unknown()),
|
|
131
|
+
enterprise_id: z.optional(z.unknown()),
|
|
132
|
+
}))
|
|
133
|
+
.check(z.maxLength(20))),
|
|
134
|
+
/** Whether the conversation is shared with another organization, as the messenger says it. */
|
|
135
|
+
is_ext_shared_channel: z.optional(z.unknown()),
|
|
136
|
+
});
|
|
137
|
+
/** A workspace's id, or an organization's, as the messenger spells one; nothing for anything else. */
|
|
138
|
+
const workspaceOf = (value) => typeof value === "string" && /^[A-Z0-9]{1,24}$/.test(value) ? value : undefined;
|
|
139
|
+
/**
|
|
140
|
+
* A message as a read of a conversation answers one: what the source reads of it, and nothing
|
|
141
|
+
* else. Its blocks and what hangs under it are read loosely, as an event's are.
|
|
142
|
+
*/
|
|
143
|
+
const heardSchema = z.object({
|
|
144
|
+
subtype: z.optional(z.string().check(z.maxLength(60))),
|
|
145
|
+
user: z.optional(id),
|
|
146
|
+
bot_id: z.optional(z.string().check(z.maxLength(40))),
|
|
147
|
+
text: z.optional(z.string().check(z.maxLength(100_000))),
|
|
148
|
+
blocks: z.optional(z.unknown()),
|
|
149
|
+
attachments: z.optional(z.unknown()),
|
|
150
|
+
files: z.optional(z.unknown()),
|
|
151
|
+
ts: stamp,
|
|
152
|
+
thread_ts: z.optional(stamp),
|
|
153
|
+
team: z.optional(z.unknown()),
|
|
154
|
+
user_team: z.optional(z.unknown()),
|
|
155
|
+
});
|
|
156
|
+
/** What a read of a conversation answers: its messages, each read by itself, and whether there are more. */
|
|
157
|
+
const readSchema = z.object({
|
|
158
|
+
ok: z.boolean(),
|
|
159
|
+
error: z.optional(z.string().check(z.maxLength(200))),
|
|
160
|
+
messages: z.optional(z.array(z.unknown()).check(z.maxLength(1000))),
|
|
161
|
+
has_more: z.optional(z.unknown()),
|
|
162
|
+
response_metadata: z.optional(z.unknown()),
|
|
63
163
|
});
|
|
64
164
|
const envelopeSchema = z.object({
|
|
65
165
|
type: z.optional(z.string().check(z.maxLength(40))),
|
|
@@ -232,33 +332,6 @@ function blockOf(block) {
|
|
|
232
332
|
return [leftOut(block.type)];
|
|
233
333
|
}
|
|
234
334
|
}
|
|
235
|
-
/**
|
|
236
|
-
* A message's words as people read them. Where it has blocks they are what the messenger shows,
|
|
237
|
-
* and its `text` is what a notification says in their place, which leaves a table out: the
|
|
238
|
-
* words are read from the blocks, and from the `text` only where no block says anything. A
|
|
239
|
-
* table may hang under the message instead, so what hangs under it is read after its own: its
|
|
240
|
-
* blocks, or the words it has in their place, a message someone passed on say. A file of the
|
|
241
|
-
* message is named and marked as not carried. Each is walked once and to the depth the
|
|
242
|
-
* messenger gives a block, whatever it is nested with.
|
|
243
|
-
*/
|
|
244
|
-
function wordsOf(event) {
|
|
245
|
-
const shownBy = (blocks) => blocks
|
|
246
|
-
.flatMap((block) => (isObject(block) ? blockOf(block) : []))
|
|
247
|
-
.filter((part) => part.trim() !== "")
|
|
248
|
-
.join("\n");
|
|
249
|
-
const shown = shownBy(listOf(event.blocks));
|
|
250
|
-
const under = listOf(event.attachments).map((one) => {
|
|
251
|
-
if (!isObject(one)) {
|
|
252
|
-
return "";
|
|
253
|
-
}
|
|
254
|
-
const blocks = shownBy(listOf(one.blocks));
|
|
255
|
-
const words = blocks === "" ? (textOf(one.text) ?? textOf(one.fallback) ?? "") : blocks;
|
|
256
|
-
return words.trim() === "" ? leftOut("attachment") : words;
|
|
257
|
-
});
|
|
258
|
-
return [shown === "" ? (event.text ?? "") : shown, ...under, ...filesOf(event.files)]
|
|
259
|
-
.filter((part) => part !== "")
|
|
260
|
-
.join("\n");
|
|
261
|
-
}
|
|
262
335
|
/** A mark for each file of a message, by its name on one line: the first few, and how many more. */
|
|
263
336
|
function filesOf(files) {
|
|
264
337
|
const all = listOf(files).filter(isObject);
|
|
@@ -272,32 +345,277 @@ function filesOf(files) {
|
|
|
272
345
|
const more = all.length - marks.length;
|
|
273
346
|
return more > 0 ? [...marks, `[${more} more files, not carried in this wake]`] : marks;
|
|
274
347
|
}
|
|
348
|
+
/** What a member's id begins with, as the messenger spells one. */
|
|
349
|
+
const MEMBER_ID = /^[UW][A-Z0-9]{1,23}$/;
|
|
350
|
+
/**
|
|
351
|
+
* What the blocks of a message show, the parts one under another. Each is walked once and to the
|
|
352
|
+
* depth the messenger gives a block, whatever it is nested with.
|
|
353
|
+
*/
|
|
354
|
+
const shownBy = (blocks) => blocks
|
|
355
|
+
.flatMap((block) => (isObject(block) ? blockOf(block) : []))
|
|
356
|
+
.filter((part) => part.trim() !== "")
|
|
357
|
+
.join("\n");
|
|
358
|
+
/**
|
|
359
|
+
* A message's words as people read them, and what hangs under it apart from them. Where it has
|
|
360
|
+
* blocks they are what the messenger shows, and its `text` is what a notification says in their
|
|
361
|
+
* place, which leaves a table out: the words are read from the blocks, and from the `text` only
|
|
362
|
+
* where no block says anything. A file of the message is named and marked as not carried.
|
|
363
|
+
*
|
|
364
|
+
* What hangs under a message is not its writer's own words: a message someone passed on, one a
|
|
365
|
+
* link to it brought along, a preview of a page, a table the messenger shows apart. Each is
|
|
366
|
+
* carried apart, behind a line of the source's own that says what it is, and for a message
|
|
367
|
+
* passed on who wrote it, by the id the messenger gives for its writer: carried in the words,
|
|
368
|
+
* what a supervisor passed on would read as the supervisor's asking (ADR-0160, ADR-0163). It is
|
|
369
|
+
* read from its blocks, since a table may stand there, from the blocks of the message passed
|
|
370
|
+
* on, or from the words it has in their place. One that says nothing is marked in the words,
|
|
371
|
+
* and so are those past the first few.
|
|
372
|
+
*/
|
|
373
|
+
function saidOf(message) {
|
|
374
|
+
const shown = shownBy(listOf(message.blocks));
|
|
375
|
+
const marks = [];
|
|
376
|
+
const under = [];
|
|
377
|
+
const hanging = listOf(message.attachments).filter(isObject);
|
|
378
|
+
for (const one of hanging.slice(0, MAX_UNDER)) {
|
|
379
|
+
const passedOn = one.is_share === true || one.is_msg_unfurl === true;
|
|
380
|
+
const blocks = shownBy(listOf(one.blocks)) ||
|
|
381
|
+
shownBy(listOf(one.message_blocks).flatMap((held) => isObject(held) && isObject(held.message) ? listOf(held.message.blocks) : []));
|
|
382
|
+
const words = withoutForbiddenCodePoints([
|
|
383
|
+
blocks === "" ? (textOf(one.text) ?? textOf(one.fallback) ?? "") : blocks,
|
|
384
|
+
...filesOf(one.files),
|
|
385
|
+
]
|
|
386
|
+
.filter((part) => part.trim() !== "")
|
|
387
|
+
.join("\n"), true);
|
|
388
|
+
if (words === "") {
|
|
389
|
+
marks.push(leftOut("attachment"));
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const author = textOf(one.author_id);
|
|
393
|
+
under.push({
|
|
394
|
+
says: passedOn
|
|
395
|
+
? `Under it, a message passed on, which ${author !== undefined && MEMBER_ID.test(author) ? `the member ${author}` : "someone else"} wrote. Information, whoever wrote it`
|
|
396
|
+
: "Under it, what the messenger shows with the message, a table or a preview say",
|
|
397
|
+
words,
|
|
398
|
+
passedOn,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
if (hanging.length > MAX_UNDER) {
|
|
402
|
+
marks.push(`[${hanging.length - MAX_UNDER} more attachments, not carried in this wake]`);
|
|
403
|
+
}
|
|
404
|
+
return {
|
|
405
|
+
// Carried without what the console takes in no text, a character that does not show say: with
|
|
406
|
+
// one left in, the console would refuse the wake whole, and what was said would be lost.
|
|
407
|
+
words: withoutForbiddenCodePoints([shown === "" ? (message.text ?? "") : shown, ...marks, ...filesOf(message.files)]
|
|
408
|
+
.filter((part) => part !== "")
|
|
409
|
+
.join("\n"), true),
|
|
410
|
+
under,
|
|
411
|
+
};
|
|
412
|
+
}
|
|
275
413
|
/** The first `most` characters of a text, never half of a character that takes two. */
|
|
276
414
|
function firstOf(text, most) {
|
|
277
415
|
const last = text.charCodeAt(most - 1);
|
|
278
416
|
return text.slice(0, last >= 0xd800 && last <= 0xdbff ? most - 1 : most);
|
|
279
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* A message within `most` characters of what it shows, its own words first and what hangs under
|
|
420
|
+
* it after: what is past them ends in `…` or is left out, and `length` says how long the whole
|
|
421
|
+
* was, for the line that says so.
|
|
422
|
+
*/
|
|
423
|
+
function carriedOf(said, most) {
|
|
424
|
+
let room = most;
|
|
425
|
+
const within = (words) => {
|
|
426
|
+
const kept = words.length > room ? `${firstOf(words, room)}…` : words;
|
|
427
|
+
room -= Math.min(words.length, room);
|
|
428
|
+
return kept;
|
|
429
|
+
};
|
|
430
|
+
const words = within(said.words);
|
|
431
|
+
const under = [];
|
|
432
|
+
for (const one of said.under) {
|
|
433
|
+
if (room <= 0) {
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
under.push({ ...one, words: within(one.words) });
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
words,
|
|
440
|
+
under,
|
|
441
|
+
length: said.under.reduce((sum, one) => sum + one.words.length, said.words.length),
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
/** A message's parts as a wake carries them, after the line of the source's own that leads them. */
|
|
445
|
+
const partsOf = (said) => [
|
|
446
|
+
...(said.words === "" ? [] : [{ words: said.words }]),
|
|
447
|
+
...said.under.flatMap((one) => [{ says: one.says }, { words: one.words }]),
|
|
448
|
+
];
|
|
280
449
|
/**
|
|
281
450
|
* How the messenger writes a call of everyone in a message's `text`: `<!here>`, `<!channel>`
|
|
282
451
|
* and `<!everyone>`, with what it shows for it after a bar where it says so. What a person
|
|
283
452
|
* typed that only looks like one arrives escaped, and is none.
|
|
284
453
|
*/
|
|
285
454
|
const EVERYONE_CALLED = /<!(here|channel|everyone)(?:\|[^<>]{0,40})?>/;
|
|
455
|
+
/**
|
|
456
|
+
* Who wrote a message, as a line of the source's says it: the account by the id the messenger
|
|
457
|
+
* gives it, its workspace where the message says that it is another than the agent's own, and
|
|
458
|
+
* that it wrote through an app where the message says so, which is how an agent account writes
|
|
459
|
+
* with its member's token, and an app that posts in a member's name. An app that posted in
|
|
460
|
+
* nobody's name is said as an app. The agent's own is the workspace the event was delivered
|
|
461
|
+
* for, and the organization that workspace is one of several of, either of which an
|
|
462
|
+
* installation may leave out. Where the message names no workspace for its writer, or nothing
|
|
463
|
+
* is known of the agent's own, nothing is said of it: a line that names no workspace is no
|
|
464
|
+
* proof of a colleague.
|
|
465
|
+
*/
|
|
466
|
+
function writerOf(message, own, who = NOBODY) {
|
|
467
|
+
if (message.user === undefined) {
|
|
468
|
+
const app = workspaceOf(message.bot_id);
|
|
469
|
+
return `an app${app === undefined ? "" : ` (${app})`}`;
|
|
470
|
+
}
|
|
471
|
+
const theirs = workspaceOf(message.user_team) ?? workspaceOf(message.team);
|
|
472
|
+
const elsewhere = theirs === undefined || own.includes(theirs) || own.every((one) => one === undefined)
|
|
473
|
+
? undefined
|
|
474
|
+
: theirs;
|
|
475
|
+
return `the member ${message.user}${elsewhere === undefined ? "" : ` of another workspace (${elsewhere})`}${message.bot_id === undefined ? "" : " through an app"}${who(message)}`;
|
|
476
|
+
}
|
|
477
|
+
const NOBODY = () => "";
|
|
478
|
+
/** Why what was asked came to nothing as the signal ended: by the word it was ended with, or because the wait for it was over. */
|
|
479
|
+
const endedBy = (until) => typeof until.reason === "string" ? until.reason : "no_answer";
|
|
480
|
+
/** What is answered before the signal ends, or that it ended: an answer that comes or fails after that is nobody's to hear. */
|
|
481
|
+
const ENDED = Symbol("ended");
|
|
482
|
+
async function unlessEnded(until, answer) {
|
|
483
|
+
answer.catch(() => undefined);
|
|
484
|
+
let stop = () => undefined;
|
|
485
|
+
const ended = new Promise((resolve) => {
|
|
486
|
+
stop = () => resolve(ENDED);
|
|
487
|
+
until.addEventListener("abort", stop, { once: true });
|
|
488
|
+
});
|
|
489
|
+
try {
|
|
490
|
+
return await Promise.race([answer, ended]);
|
|
491
|
+
}
|
|
492
|
+
finally {
|
|
493
|
+
until.removeEventListener("abort", stop);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/** A login as a line of the source's carries one: a name of the console's, and never anything a line could be made of. */
|
|
497
|
+
const LOGIN = /^[A-Za-z0-9][A-Za-z0-9._@+-]{0,99}$/;
|
|
498
|
+
/**
|
|
499
|
+
* The account a message names as its writer, as the console is asked of it: with the workspace
|
|
500
|
+
* the message itself says the account is of, and none where it says none. The agent's own
|
|
501
|
+
* workspace is never taken for it: the same id at another workspace is another account, and an
|
|
502
|
+
* account whose workspace nothing says is nobody the console knows.
|
|
503
|
+
*/
|
|
504
|
+
function accountOf(message) {
|
|
505
|
+
const workspace = workspaceOf(message.user_team) ?? workspaceOf(message.team);
|
|
506
|
+
return message.user === undefined || workspace === undefined
|
|
507
|
+
? undefined
|
|
508
|
+
: { workspace, account: message.user };
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* How a member stands to the agent, as a line says it after who they are: a supervisor of its
|
|
512
|
+
* own or none, and where the console says more, how: its manager, anyone above its manager or
|
|
513
|
+
* an administrator, who supervises from outside the line and may stand in it too; and of
|
|
514
|
+
* anyone below the agent in its line, that they report to it, themselves or through others.
|
|
515
|
+
* A console that says only whether a member supervises is said as it was.
|
|
516
|
+
*/
|
|
517
|
+
function standsOf(member) {
|
|
518
|
+
const reports = member.line === "report"
|
|
519
|
+
? "reports to you"
|
|
520
|
+
: member.line === "below"
|
|
521
|
+
? "reports to you through others"
|
|
522
|
+
: undefined;
|
|
523
|
+
// A supervisor only where the console says so in so many words: how it is one says more of
|
|
524
|
+
// it, and never makes one.
|
|
525
|
+
if (member.supervisor !== true) {
|
|
526
|
+
return `no supervisor of yours${reports === undefined ? "" : `, ${reports}`}`;
|
|
527
|
+
}
|
|
528
|
+
const how = [
|
|
529
|
+
member.line === "manager"
|
|
530
|
+
? "your manager"
|
|
531
|
+
: member.line === "above"
|
|
532
|
+
? "above your manager"
|
|
533
|
+
: undefined,
|
|
534
|
+
member.administrator === true ? "an administrator" : undefined,
|
|
535
|
+
].filter((one) => one !== undefined);
|
|
536
|
+
return `a supervisor of yours${how.length === 0 ? "" : `: ${how.join(" and ")}`}${reports === undefined ? "" : `, who ${reports}`}`;
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Asks the console who wrote (ADR-0164), of so many messages at once, and answers what a line
|
|
540
|
+
* says of each writer after the account:
|
|
541
|
+
*
|
|
542
|
+
* - the member it is, by their login in the console, an agent account said as one, and whether
|
|
543
|
+
* they supervise the agent: `(@kim in the console, a supervisor of yours: your manager)`,
|
|
544
|
+
* with how, its manager, anyone above its manager or an administrator, and of anyone below
|
|
545
|
+
* the agent in its line that they report to it, themselves or through others (`standsOf`);
|
|
546
|
+
* - that it is no member the console knows, where the console is told the accounts of that
|
|
547
|
+
* workspace and not this one: what it is told may be short of one, so a supervisor's tell that
|
|
548
|
+
* names the account still tells whose it is;
|
|
549
|
+
* - nothing, where the console is told no account of that workspace, where it does not answer
|
|
550
|
+
* before the signal ends, or where it is older than the question: the line is then as the
|
|
551
|
+
* messenger alone has it. Where the signal has ended already, the console is not asked.
|
|
552
|
+
*
|
|
553
|
+
* It is the console's answer and nothing else's: no word of a message, no name the messenger
|
|
554
|
+
* shows and no id alone makes an account a supervisor's, and a login that is not shaped as one
|
|
555
|
+
* is left out. An agent takes a supervisor's own word at the messenger as an instruction
|
|
556
|
+
* (ADR-0160), and this is how a wake says that one wrote.
|
|
557
|
+
*/
|
|
558
|
+
async function whoOf(host, messages, until) {
|
|
559
|
+
const keyOf = (one) => `${one.workspace} ${one.account}`;
|
|
560
|
+
const asked = new Map();
|
|
561
|
+
for (const message of messages) {
|
|
562
|
+
const one = accountOf(message);
|
|
563
|
+
if (one !== undefined && asked.size < MAX_WRITERS_ASKED) {
|
|
564
|
+
asked.set(keyOf(one), one);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (host.writers === undefined || asked.size === 0 || until.aborted) {
|
|
568
|
+
return NOBODY;
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
const answered = await unlessEnded(until, host.writers([...asked.values()], until));
|
|
572
|
+
if (answered === ENDED) {
|
|
573
|
+
return NOBODY;
|
|
574
|
+
}
|
|
575
|
+
const known = new Map();
|
|
576
|
+
for (const one of answered) {
|
|
577
|
+
// Only what was asked of is heard, and only an answer of the shape one has.
|
|
578
|
+
if (!asked.has(keyOf(one)) || one.told !== true) {
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
const member = one.member;
|
|
582
|
+
// A login that is not shaped as one is not written into a line: the member is said, unnamed.
|
|
583
|
+
const named = member !== null && typeof member.login === "string" && LOGIN.test(member.login)
|
|
584
|
+
? `@${member.login} in the console`
|
|
585
|
+
: "a member of the console";
|
|
586
|
+
known.set(keyOf(one), member === null
|
|
587
|
+
? " (no member the console knows)"
|
|
588
|
+
: ` (${named}${member.kind === "agent" ? ", an agent account" : ""}, ${standsOf(member)})`);
|
|
589
|
+
}
|
|
590
|
+
return (message) => {
|
|
591
|
+
const one = accountOf(message);
|
|
592
|
+
return one === undefined ? "" : (known.get(keyOf(one)) ?? "");
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
return NOBODY;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
286
599
|
/**
|
|
287
600
|
* The wake an event of the messenger means, or nothing: a message in a direct conversation, or
|
|
288
601
|
* one that mentions the member the app acts for, from anyone but that member; and, mentioned or
|
|
289
602
|
* not, a person's message in a group conversation the member is in or in one of the channels
|
|
290
603
|
* `runner.json` names, and a call of everyone (`@here`, `@channel`, `@everyone`) in any
|
|
291
604
|
* channel the member is in, whoever wrote it. A message that is an edit, a join or anything else
|
|
292
|
-
* than someone's words wakes nobody.
|
|
605
|
+
* than someone's words wakes nobody. The wake's first line names the account that wrote by its
|
|
606
|
+
* id, and its workspace where the event says it is another than the agent's own; and it says
|
|
607
|
+
* that a conversation is shared with another organization where the messenger says so: an
|
|
608
|
+
* agent goes by the account and never by a name (ADR-0160), and the same name at another
|
|
609
|
+
* organization is another account.
|
|
293
610
|
*/
|
|
294
|
-
|
|
611
|
+
function heardOf(payload, channels) {
|
|
295
612
|
const parsed = payloadSchema.safeParse(payload);
|
|
296
613
|
if (!parsed.success) {
|
|
297
614
|
return undefined;
|
|
298
615
|
}
|
|
299
616
|
const event = parsed.data.event;
|
|
300
|
-
const
|
|
617
|
+
const installed = parsed.data.authorizations?.[0];
|
|
618
|
+
const member = installed?.user_id ?? undefined;
|
|
301
619
|
if (event === undefined ||
|
|
302
620
|
(event.type !== "message" && event.type !== "app_mention") ||
|
|
303
621
|
!WORDS_OF_SOMEONE.includes(event.subtype) ||
|
|
@@ -332,31 +650,644 @@ export function slackWakeOf(payload, channels = []) {
|
|
|
332
650
|
return undefined;
|
|
333
651
|
}
|
|
334
652
|
const thread = event.thread_ts ?? event.ts;
|
|
335
|
-
// Carried without what the console takes in no text, a character that does not show say: with
|
|
336
|
-
// one left in, the console would refuse the wake whole, and what was said would be lost.
|
|
337
|
-
const words = withoutForbiddenCodePoints(wordsOf(event), true);
|
|
338
|
-
// A cut is said in the source's own line, with the message to read whole by its moment: said
|
|
339
|
-
// after the words, it would read as theirs.
|
|
340
|
-
const cut = words.length > MAX_WAKE_TEXT
|
|
341
|
-
? ` (cut in this wake after ${MAX_WAKE_TEXT} of its ${words.length} characters: read the message ${event.ts} whole with the messenger's own tool)`
|
|
342
|
-
: "";
|
|
343
653
|
// A message of a conversation outside any thread is said as one: named a thread, it is answered
|
|
344
654
|
// in one, where whoever wrote it reads the conversation itself.
|
|
345
|
-
const
|
|
655
|
+
const where = direct
|
|
346
656
|
? "A direct message at Slack"
|
|
347
657
|
: group
|
|
348
658
|
? "A message in a group conversation at Slack"
|
|
349
659
|
: undefined;
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
660
|
+
const own = [installed?.team_id, installed?.enterprise_id].map(workspaceOf);
|
|
661
|
+
// A conversation shared with another organization is said to be one, whoever wrote in it:
|
|
662
|
+
// what is said there, people outside read.
|
|
663
|
+
const shared = parsed.data.is_ext_shared_channel === true ? "externally shared " : "";
|
|
664
|
+
const whole = saidOf(event);
|
|
665
|
+
const saying = (who) => {
|
|
666
|
+
const from = `from ${writerOf(event, own, who)}`;
|
|
667
|
+
const first = where === undefined
|
|
668
|
+
? `${mentioned ? "A mention" : called !== undefined ? `A call of everyone (@${called})` : "A message"} in the ${shared}channel ${event.channel} at Slack, ${from}, in the thread ${thread}`
|
|
669
|
+
: event.thread_ts === undefined
|
|
670
|
+
? `${where}, ${from}, in the ${shared}conversation ${event.channel}`
|
|
671
|
+
: `${where}, ${from}, in the thread ${thread} of the ${shared}conversation ${event.channel}`;
|
|
672
|
+
// A cut is said in the source's own line, with the message to read whole by its moment: said
|
|
673
|
+
// after the words, it would read as theirs.
|
|
674
|
+
const carried = (most) => {
|
|
675
|
+
const said = carriedOf(whole, most);
|
|
676
|
+
const cut = said.length > most
|
|
677
|
+
? ` (cut in this wake after ${most} of its ${said.length} characters: read the message ${event.ts} whole with the messenger's own tool)`
|
|
678
|
+
: "";
|
|
679
|
+
return [{ says: `${first}${cut}` }, ...partsOf(said)];
|
|
680
|
+
};
|
|
681
|
+
// So much of the message as a wake carries, and less where its lines are so many that, begun
|
|
682
|
+
// as a signal's text begins them, they would leave no room for anything else.
|
|
683
|
+
let most = MAX_WAKE_TEXT;
|
|
684
|
+
let parts = carried(most);
|
|
685
|
+
while (most > 250 && wakeTextOf(parts, Number.MAX_SAFE_INTEGER).length > MAX_LEAD_LENGTH) {
|
|
686
|
+
most = Math.floor(most / 2);
|
|
687
|
+
parts = carried(most);
|
|
688
|
+
}
|
|
689
|
+
return parts;
|
|
690
|
+
};
|
|
355
691
|
return {
|
|
356
692
|
key: `slack:${event.channel}:${thread}`,
|
|
357
|
-
|
|
693
|
+
saying,
|
|
694
|
+
channel: event.channel,
|
|
695
|
+
ts: event.ts,
|
|
696
|
+
thread: event.thread_ts,
|
|
697
|
+
user: event.user,
|
|
698
|
+
member,
|
|
699
|
+
direct,
|
|
700
|
+
group,
|
|
701
|
+
own,
|
|
702
|
+
from: {
|
|
703
|
+
user: event.user,
|
|
704
|
+
bot_id: event.bot_id,
|
|
705
|
+
team: event.team,
|
|
706
|
+
user_team: event.user_team,
|
|
707
|
+
},
|
|
358
708
|
};
|
|
359
709
|
}
|
|
710
|
+
/**
|
|
711
|
+
* The wake an event of the messenger means, with nothing read of the conversation it was said
|
|
712
|
+
* in, or nothing where the event wakes nobody (`heardOf`): its key, and its parts, the line of
|
|
713
|
+
* the source's own first, then the message's words, then what hangs under the message, each
|
|
714
|
+
* behind a line that says what it is.
|
|
715
|
+
*/
|
|
716
|
+
export function slackWakeOf(payload, channels = []) {
|
|
717
|
+
const heard = heardOf(payload, channels);
|
|
718
|
+
return heard === undefined ? undefined : { key: heard.key, parts: heard.saying(NOBODY) };
|
|
719
|
+
}
|
|
720
|
+
/** Whether one moment of the messenger's is before another: by the seconds, then by the counter. */
|
|
721
|
+
function before(one, other) {
|
|
722
|
+
const [seconds = "", counter = ""] = one.split(".");
|
|
723
|
+
const [otherSeconds = "", otherCounter = ""] = other.split(".");
|
|
724
|
+
return Number(seconds) === Number(otherSeconds)
|
|
725
|
+
? counter.padEnd(9, "0") < otherCounter.padEnd(9, "0")
|
|
726
|
+
: Number(seconds) < Number(otherSeconds);
|
|
727
|
+
}
|
|
728
|
+
/** A stretch of time as a line says it: whole seconds, minutes, hours or days, one at the least. */
|
|
729
|
+
function stretchOf(seconds) {
|
|
730
|
+
const counted = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
731
|
+
if (seconds < 60) {
|
|
732
|
+
return counted(Math.max(1, Math.floor(seconds)), "second");
|
|
733
|
+
}
|
|
734
|
+
if (seconds < 3600) {
|
|
735
|
+
return counted(Math.floor(seconds / 60), "minute");
|
|
736
|
+
}
|
|
737
|
+
return seconds < 86_400
|
|
738
|
+
? counted(Math.floor(seconds / 3600), "hour")
|
|
739
|
+
: counted(Math.floor(seconds / 86_400), "day");
|
|
740
|
+
}
|
|
741
|
+
/** The seconds of a moment of the messenger's. */
|
|
742
|
+
const secondsOf = (moment) => Number(moment.split(".")[0]);
|
|
743
|
+
/** A short name of why a read came to nothing, as the messenger spells its errors; `refused` for anything else. */
|
|
744
|
+
const whyOf = (error) => error !== undefined && /^[a-z_]{1,60}$/.test(error) ? error : "refused";
|
|
745
|
+
/**
|
|
746
|
+
* Whether a message a read answered is someone's words: a plain one, one with a file, a reply
|
|
747
|
+
* sent to its conversation too, each with the member that wrote it, and what an app posted in
|
|
748
|
+
* nobody's name. A join, an edit and a message that does not read are none.
|
|
749
|
+
*/
|
|
750
|
+
function wordsOfSomeone(message) {
|
|
751
|
+
const parsed = heardSchema.safeParse(message);
|
|
752
|
+
if (!parsed.success) {
|
|
753
|
+
return undefined;
|
|
754
|
+
}
|
|
755
|
+
const one = parsed.data;
|
|
756
|
+
return (WORDS_OF_SOMEONE.includes(one.subtype) && one.user !== undefined) ||
|
|
757
|
+
(one.subtype === "bot_message" && one.bot_id !== undefined)
|
|
758
|
+
? one
|
|
759
|
+
: undefined;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Does what one wake reads, all of it within one wait: the signal handed to the work ends once
|
|
763
|
+
* the wait is over (`no_answer`), or before that with the one given, and what is asked under it
|
|
764
|
+
* comes to nothing then. Where the one given has ended already, nothing is asked at all.
|
|
765
|
+
*/
|
|
766
|
+
async function within(host, signal, read) {
|
|
767
|
+
const control = new AbortController();
|
|
768
|
+
const stop = () => control.abort(signal.reason);
|
|
769
|
+
if (signal.aborted) {
|
|
770
|
+
stop();
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
774
|
+
void host.wait(READ_WAIT_MS, control.signal).then(() => control.abort("no_answer"));
|
|
775
|
+
}
|
|
776
|
+
try {
|
|
777
|
+
return await read(control.signal);
|
|
778
|
+
}
|
|
779
|
+
finally {
|
|
780
|
+
control.abort();
|
|
781
|
+
signal.removeEventListener("abort", stop);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Asks the messenger for a page of a conversation, with the member's own token in the
|
|
786
|
+
* `authorization` header and what is asked in the address, where no token ever is. Waited for
|
|
787
|
+
* until the signal ends and no longer: a wake is not held back for a messenger that does not
|
|
788
|
+
* answer.
|
|
789
|
+
*/
|
|
790
|
+
async function pageOf(host, token, url, asked, until) {
|
|
791
|
+
if (until.aborted) {
|
|
792
|
+
return { ok: false, why: endedBy(until) };
|
|
793
|
+
}
|
|
794
|
+
try {
|
|
795
|
+
const answered = await unlessEnded(until, host
|
|
796
|
+
.fetch(`${url}?${new URLSearchParams(asked).toString()}`, {
|
|
797
|
+
method: "GET",
|
|
798
|
+
headers: { authorization: `Bearer ${token}` },
|
|
799
|
+
signal: until,
|
|
800
|
+
})
|
|
801
|
+
.then((answered) => answered.json()));
|
|
802
|
+
if (answered === ENDED) {
|
|
803
|
+
return { ok: false, why: endedBy(until) };
|
|
804
|
+
}
|
|
805
|
+
const parsed = readSchema.safeParse(answered);
|
|
806
|
+
if (!parsed.success) {
|
|
807
|
+
return { ok: false, why: "unreadable" };
|
|
808
|
+
}
|
|
809
|
+
if (!parsed.data.ok) {
|
|
810
|
+
return { ok: false, why: whyOf(parsed.data.error) };
|
|
811
|
+
}
|
|
812
|
+
const meta = parsed.data.response_metadata;
|
|
813
|
+
const cursor = isObject(meta) ? textOf(meta.next_cursor) : undefined;
|
|
814
|
+
return {
|
|
815
|
+
ok: true,
|
|
816
|
+
messages: parsed.data.messages ?? [],
|
|
817
|
+
more: parsed.data.has_more === true,
|
|
818
|
+
cursor: cursor === undefined || cursor === "" || cursor.length > 1000 ? undefined : cursor,
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
catch {
|
|
822
|
+
return { ok: false, why: until.aborted ? endedBy(until) : "failed" };
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
/** The messages of a read that are someone's words, the oldest first, each once. */
|
|
826
|
+
function inOrder(messages) {
|
|
827
|
+
const read = new Map();
|
|
828
|
+
for (const message of messages) {
|
|
829
|
+
const one = wordsOfSomeone(message);
|
|
830
|
+
if (one !== undefined) {
|
|
831
|
+
read.set(one.ts, one);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
return [...read.values()].sort((one, other) => before(one.ts, other.ts) ? -1 : before(other.ts, one.ts) ? 1 : 0);
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* The latest of a conversation outside its threads: so many messages, before a moment where one
|
|
838
|
+
* is given. The messenger answers them the newest first.
|
|
839
|
+
*/
|
|
840
|
+
async function conversationOf(host, token, channel, asked, until) {
|
|
841
|
+
const page = await pageOf(host, token, HISTORY_URL, {
|
|
842
|
+
channel,
|
|
843
|
+
limit: String(asked.limit),
|
|
844
|
+
...(asked.before === undefined ? {} : { latest: asked.before, inclusive: "false" }),
|
|
845
|
+
}, until);
|
|
846
|
+
return page.ok ? { ok: true, messages: inOrder(page.messages), more: page.more } : page;
|
|
847
|
+
}
|
|
848
|
+
/**
|
|
849
|
+
* A thread: the messenger answers it the oldest first, the message that began it ahead of all, a
|
|
850
|
+
* page at a time, so it is read to its end, or to as many pages as a read takes. Where a moment
|
|
851
|
+
* is given, what is asked for is what was written after it: from the first page on, or past the
|
|
852
|
+
* first page alone (`beginning`), which a long thread is read by without being read through,
|
|
853
|
+
* and its first page says who writes there. `more` says that the thread goes on past what was
|
|
854
|
+
* read, and so that its latest messages are not among them.
|
|
855
|
+
*/
|
|
856
|
+
async function threadOf(host, token, channel, thread, from, until) {
|
|
857
|
+
const messages = [];
|
|
858
|
+
const asked = {
|
|
859
|
+
channel,
|
|
860
|
+
ts: thread,
|
|
861
|
+
limit: String(READ_THREAD_PAGE),
|
|
862
|
+
...(from.after === undefined || from.beginning === true ? {} : { oldest: from.after }),
|
|
863
|
+
};
|
|
864
|
+
for (let pages = 0; pages < READ_THREAD_PAGES; pages += 1) {
|
|
865
|
+
const page = await pageOf(host, token, REPLIES_URL, asked, until);
|
|
866
|
+
if (!page.ok) {
|
|
867
|
+
return page;
|
|
868
|
+
}
|
|
869
|
+
messages.push(...page.messages);
|
|
870
|
+
if (!page.more) {
|
|
871
|
+
return { ok: true, messages: inOrder(messages), more: false };
|
|
872
|
+
}
|
|
873
|
+
if (from.after !== undefined && asked.oldest === undefined) {
|
|
874
|
+
asked.oldest = from.after;
|
|
875
|
+
}
|
|
876
|
+
else if (page.cursor === undefined) {
|
|
877
|
+
break;
|
|
878
|
+
}
|
|
879
|
+
else {
|
|
880
|
+
asked.cursor = page.cursor;
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return { ok: true, messages: inOrder(messages), more: true };
|
|
884
|
+
}
|
|
885
|
+
/** The command that reads on where a wake was raised: the conversation, or the thread in it. */
|
|
886
|
+
const readWith = (channel, thread, after) => `console agent messages slack:${channel}${thread === undefined ? "" : `:${thread}`}${after === undefined ? "" : ` --after ${after}`}`;
|
|
887
|
+
/**
|
|
888
|
+
* A message said before the one that raised a wake, as the wake carries it: who wrote it and how
|
|
889
|
+
* long before, in a line of the source's own, and its writer's own words, so much of them. What
|
|
890
|
+
* hangs under it is marked in the words and not carried: the agent reads it with the command.
|
|
891
|
+
*/
|
|
892
|
+
function earlierOf(message, heard, who = NOBODY) {
|
|
893
|
+
const said = saidOf(message);
|
|
894
|
+
const words = [
|
|
895
|
+
said.words,
|
|
896
|
+
...said.under.map((one) => one.passedOn ? "[a message passed on, not carried in this wake]" : leftOut("attachment")),
|
|
897
|
+
]
|
|
898
|
+
.filter((part) => part !== "")
|
|
899
|
+
.join("\n");
|
|
900
|
+
const cut = words.length > MAX_BEFORE_TEXT
|
|
901
|
+
? `, cut after ${MAX_BEFORE_TEXT} of its ${words.length} characters`
|
|
902
|
+
: "";
|
|
903
|
+
return [
|
|
904
|
+
{
|
|
905
|
+
says: `${stretchOf(secondsOf(heard.ts) - secondsOf(message.ts))} before it, from ${writerOf(message, heard.own, who)}${cut}`,
|
|
906
|
+
},
|
|
907
|
+
{ words: cut === "" ? words : `${firstOf(words, MAX_BEFORE_TEXT)}…` },
|
|
908
|
+
];
|
|
909
|
+
}
|
|
910
|
+
/** How many names the source keeps, of the messages its wakes carried and of where they were raised: the latest. */
|
|
911
|
+
const MAX_KEPT = 2000;
|
|
912
|
+
/** The name the source keeps the moment of the last message a wake brought from a conversation, or from a thread, under. */
|
|
913
|
+
const broughtOf = (heard) => `brought:${heard.channel}:${heard.thread ?? ""}`;
|
|
914
|
+
/** The name the source keeps a message a wake carried under: `woke`, or `said` among what was said before another. */
|
|
915
|
+
const carriedAs = (channel, ts) => `carried:${channel}:${ts}`;
|
|
916
|
+
/** Keeps a name as the latest, and lets go of the oldest past the most. */
|
|
917
|
+
function keep(kept, name, value) {
|
|
918
|
+
kept.delete(name);
|
|
919
|
+
kept.set(name, value);
|
|
920
|
+
for (const oldest of kept.keys()) {
|
|
921
|
+
if (kept.size <= MAX_KEPT) {
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
kept.delete(oldest);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* What a wake carries of the conversation it was raised in, after the message itself (ADR-0163),
|
|
929
|
+
* read with the member's own token where the account keeps it:
|
|
930
|
+
*
|
|
931
|
+
* - what was said there before the message and since the later of two: the agent's own last
|
|
932
|
+
* message there, and the last message a wake already brought it from there, and of that what
|
|
933
|
+
* no wake carried, in a thread the message that began it say; so that an agent that stays
|
|
934
|
+
* silent is handed each message once. The oldest first, each behind a line that says who wrote
|
|
935
|
+
* it and how long before: the last few, so much of each, and where there was more, how many
|
|
936
|
+
* and the command that reads them. An agent account's message wakes no other unless it names
|
|
937
|
+
* it, so this is where an agent reads what another answered. It is information, whoever wrote
|
|
938
|
+
* it;
|
|
939
|
+
* - who else there writes through an app, as an agent account does, the last to write first,
|
|
940
|
+
* and with them what the agent judges by whether to answer: said in every conversation but
|
|
941
|
+
* a direct one where such an account was read, since one message wakes each of them and
|
|
942
|
+
* none sees what another is about to say.
|
|
943
|
+
*
|
|
944
|
+
* Where the account keeps no token to read with, nothing is added, and the wake is as it was;
|
|
945
|
+
* where the messenger refuses the read or does not answer, a line says so, with why in a short
|
|
946
|
+
* name of the messenger's. In a thread it is the thread that is read, and elsewhere the
|
|
947
|
+
* conversation outside its threads.
|
|
948
|
+
*/
|
|
949
|
+
async function beforeOf(host, heard, writer, until) {
|
|
950
|
+
const token = await host.secret(SLACK_USER_TOKEN);
|
|
951
|
+
if (token === undefined) {
|
|
952
|
+
return { parts: [], read: false, carried: [] };
|
|
953
|
+
}
|
|
954
|
+
const upTo = host.kept.get(broughtOf(heard));
|
|
955
|
+
const read = heard.thread === undefined
|
|
956
|
+
? await conversationOf(host, token, heard.channel, { limit: READ_MESSAGES, before: heard.ts }, until)
|
|
957
|
+
: await threadOf(host, token, heard.channel, heard.thread, { after: upTo, beginning: true }, until);
|
|
958
|
+
const command = readWith(heard.channel, heard.thread);
|
|
959
|
+
if (!read.ok) {
|
|
960
|
+
return {
|
|
961
|
+
parts: [{ says: `What was said there before it could not be read (${read.why})` }],
|
|
962
|
+
read: false,
|
|
963
|
+
carried: [],
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
if (heard.thread !== undefined && read.more) {
|
|
967
|
+
// Its latest messages are past what was read. The line says so, and the next wake there is
|
|
968
|
+
// carried what was said after this message.
|
|
969
|
+
return {
|
|
970
|
+
parts: [
|
|
971
|
+
{
|
|
972
|
+
says: `The thread is longer than a wake reads, so what was said there before it is not carried: ${command}`,
|
|
973
|
+
},
|
|
974
|
+
],
|
|
975
|
+
read: true,
|
|
976
|
+
carried: [],
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
const earlier = read.messages.filter((message) => before(message.ts, heard.ts));
|
|
980
|
+
// Who else writes there as an agent account does, the last to write first, the agent itself
|
|
981
|
+
// in its place among them: the account that wrote the message is not woken by it.
|
|
982
|
+
const writers = [];
|
|
983
|
+
for (const message of earlier.toReversed()) {
|
|
984
|
+
if (message.user !== undefined &&
|
|
985
|
+
message.bot_id !== undefined &&
|
|
986
|
+
message.user !== heard.user &&
|
|
987
|
+
!writers.includes(message.user)) {
|
|
988
|
+
writers.push(message.user);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
const others = writers.filter((one) => one !== heard.member);
|
|
992
|
+
// Where the event does not say which member it was delivered for, the agent's own account
|
|
993
|
+
// cannot be told from another's, and nobody is named: it would be told to leave it to itself.
|
|
994
|
+
const shown = heard.direct || heard.member === undefined || others.length === 0
|
|
995
|
+
? []
|
|
996
|
+
: writers.slice(0, MAX_OTHERS + 1);
|
|
997
|
+
const wrote = heard.member === undefined
|
|
998
|
+
? -1
|
|
999
|
+
: earlier.findLastIndex((message) => message.user === heard.member);
|
|
1000
|
+
// What a wake already brought of this conversation, as the message that woke or among what
|
|
1001
|
+
// was said before it, is not carried again: the agent has it, whether or not it answered. Nor
|
|
1002
|
+
// is what a wake brought from elsewhere, the message that began this thread from its channel.
|
|
1003
|
+
const brought = upTo === undefined ? -1 : earlier.findLastIndex((message) => !before(upTo, message.ts));
|
|
1004
|
+
const last = Math.max(wrote, brought);
|
|
1005
|
+
const since = earlier
|
|
1006
|
+
.slice(last + 1)
|
|
1007
|
+
.filter((message) => !host.kept.has(carriedAs(heard.channel, message.ts)));
|
|
1008
|
+
const latest = since.toReversed().slice(0, MAX_BEFORE);
|
|
1009
|
+
// Who wrote each, as the console says it (ADR-0164), asked at once for all whose account is
|
|
1010
|
+
// another than that of the message that woke, which is asked of beside the read: said of an
|
|
1011
|
+
// earlier message as of the one that woke, and information all the same, whoever wrote it;
|
|
1012
|
+
// and said of each of the others named, by a message of theirs, since how one stands to the
|
|
1013
|
+
// agent, a supervisor of its own say, is among what it judges by whether to answer.
|
|
1014
|
+
const theirs = new Map();
|
|
1015
|
+
for (const one of shown) {
|
|
1016
|
+
const written = earlier.findLast((message) => message.user === one && message.bot_id !== undefined);
|
|
1017
|
+
if (one !== heard.member && written !== undefined) {
|
|
1018
|
+
theirs.set(one, written);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
const first = await writer;
|
|
1022
|
+
const known = accountOf(heard.from);
|
|
1023
|
+
const rest = await whoOf(host, [...latest, ...theirs.values()].filter((message) => {
|
|
1024
|
+
const one = accountOf(message);
|
|
1025
|
+
return one?.account !== known?.account || one?.workspace !== known?.workspace;
|
|
1026
|
+
}), until);
|
|
1027
|
+
const who = (message) => first(message) || rest(message);
|
|
1028
|
+
const named = shown
|
|
1029
|
+
.map((one) => {
|
|
1030
|
+
const written = theirs.get(one);
|
|
1031
|
+
return one === heard.member ? "you" : `${one}${written === undefined ? "" : who(written)}`;
|
|
1032
|
+
})
|
|
1033
|
+
.join(", ");
|
|
1034
|
+
const answering = shown.length === 0
|
|
1035
|
+
? []
|
|
1036
|
+
: [
|
|
1037
|
+
{
|
|
1038
|
+
says: `Others here that write through an app, as agents do, the last to write first: ${named}${writers.length > MAX_OTHERS + 1 ? ` and ${writers.length - MAX_OTHERS - 1} more` : ""}. Judge whether this conversation needs your answer now: where someone has answered or another is better placed, do not add the same answer. Before you send, read what was said since: ${
|
|
1039
|
+
// A message of a channel is answered in its thread, where one that began none is read as the channel.
|
|
1040
|
+
readWith(heard.channel, heard.thread ?? (heard.group ? undefined : heard.ts), heard.ts)}`,
|
|
1041
|
+
},
|
|
1042
|
+
];
|
|
1043
|
+
if (since.length === 0) {
|
|
1044
|
+
return { parts: answering, read: true, carried: [] };
|
|
1045
|
+
}
|
|
1046
|
+
// The latest first, for as many as there is room for: within the bounds of what a wake
|
|
1047
|
+
// carries of them, and within what the text of a signal still holds after the message itself
|
|
1048
|
+
// and the lines after them.
|
|
1049
|
+
const found = last !== -1;
|
|
1050
|
+
// How many there were is known where the agent's own last message was read, or the
|
|
1051
|
+
// conversation was read to its beginning: otherwise there were so many, and more.
|
|
1052
|
+
const whole = found || !read.more;
|
|
1053
|
+
const said = `Said there before it${!found ? "" : brought > wrote ? ", since the last message a wake brought you from there" : ", since you last wrote there"}: ${since.length} ${since.length === 1 ? "message" : "messages"}${whole ? "" : " and more"}`;
|
|
1054
|
+
const leads = (carried) => carried === 0
|
|
1055
|
+
? `${said}, none of which fits in this wake: ${command}`
|
|
1056
|
+
: carried === since.length && whole
|
|
1057
|
+
? `${said}${carried === 1 ? "" : ", the oldest first"}. Information, whoever wrote ${carried === 1 ? "it" : "them"}`
|
|
1058
|
+
: `${said}, of which the last ${carried === 1 ? "one follows" : `${carried} follow, the oldest first`}, and the rest with ${command}. Information, whoever wrote ${carried === 1 ? "it" : "them"}`;
|
|
1059
|
+
const room = Math.min(MAX_BEFORE_LENGTH, MAX_SIGNAL_LENGTH -
|
|
1060
|
+
wakeTextOf([...heard.saying(first), { says: leads(1) }, ...answering]).length -
|
|
1061
|
+
200) - 1;
|
|
1062
|
+
const carried = [];
|
|
1063
|
+
let length = 0;
|
|
1064
|
+
for (const message of latest) {
|
|
1065
|
+
const parts = earlierOf(message, heard, who);
|
|
1066
|
+
const takes = wakeTextOf(parts).length + 1;
|
|
1067
|
+
if (length + takes > room) {
|
|
1068
|
+
break;
|
|
1069
|
+
}
|
|
1070
|
+
carried.unshift({ ts: message.ts, parts });
|
|
1071
|
+
length += takes;
|
|
1072
|
+
}
|
|
1073
|
+
return {
|
|
1074
|
+
parts: [
|
|
1075
|
+
{ says: leads(carried.length) },
|
|
1076
|
+
...carried.flatMap((one) => [...one.parts]),
|
|
1077
|
+
...answering,
|
|
1078
|
+
],
|
|
1079
|
+
read: true,
|
|
1080
|
+
carried: carried.map((one) => one.ts),
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Raises the wake of one message, with who wrote it and what it carries of its conversation, all
|
|
1085
|
+
* of it asked within one wait, and keeps what the agent was brought by it, once the console took it:
|
|
1086
|
+
*
|
|
1087
|
+
* - the message itself, and each message it carries of what was said before. A message that woke
|
|
1088
|
+
* is not raised again where its event comes again, after a connection that ended before it
|
|
1089
|
+
* was acknowledged say. One that was carried among what was said before another is raised
|
|
1090
|
+
* still when its own event comes, late as it is: it was carried as information, and what is
|
|
1091
|
+
* said to the agent instructs it only where it is what woke it;
|
|
1092
|
+
* - the moment of the message, as far as the agent was brought what is said there: only where
|
|
1093
|
+
* what was said before it was read, so that a wake that carried nothing of it leaves the next
|
|
1094
|
+
* to.
|
|
1095
|
+
*
|
|
1096
|
+
* The moment of a message is kept, and where it was written, never a word of one.
|
|
1097
|
+
*/
|
|
1098
|
+
async function raiseOf(host, heard, reading) {
|
|
1099
|
+
const message = carriedAs(heard.channel, heard.ts);
|
|
1100
|
+
if (host.kept.get(message) === "woke") {
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
const { parts, prior } = await within(host, reading, async (until) => {
|
|
1104
|
+
// Who wrote, as the console says it (ADR-0164), is asked beside the read of the conversation
|
|
1105
|
+
// and not after it: a messenger that does not answer costs the wake's first line nothing.
|
|
1106
|
+
const writer = whoOf(host, [heard.from], until);
|
|
1107
|
+
// A read that fails in a way of its own costs the wake nothing: it goes as it stands.
|
|
1108
|
+
const prior = await beforeOf(host, heard, writer, until).catch(() => ({
|
|
1109
|
+
parts: [{ says: "What was said there before it could not be read (failed)" }],
|
|
1110
|
+
read: false,
|
|
1111
|
+
carried: [],
|
|
1112
|
+
}));
|
|
1113
|
+
return { parts: heard.saying(await writer), prior };
|
|
1114
|
+
});
|
|
1115
|
+
const taken = await host
|
|
1116
|
+
.raise({ key: heard.key, parts: [...parts, ...prior.parts] })
|
|
1117
|
+
.catch((error) => {
|
|
1118
|
+
host.log(`a wake from slack was not raised (${error instanceof Error ? error.name : "error"})`);
|
|
1119
|
+
return false;
|
|
1120
|
+
});
|
|
1121
|
+
if (!taken) {
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
for (const ts of prior.carried) {
|
|
1125
|
+
keep(host.kept, carriedAs(heard.channel, ts), "said");
|
|
1126
|
+
}
|
|
1127
|
+
keep(host.kept, message, "woke");
|
|
1128
|
+
if (prior.read) {
|
|
1129
|
+
const place = broughtOf(heard);
|
|
1130
|
+
const was = host.kept.get(place);
|
|
1131
|
+
keep(host.kept, place, was !== undefined && before(heard.ts, was) ? was : heard.ts);
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
/** Where a read is asked for, as a wake's key names it after the source's name: the conversation, and the thread in it. */
|
|
1135
|
+
const NAMED = /^([A-Z0-9]{1,24})(?::([0-9]{1,12}\.[0-9]{1,9}))?$/;
|
|
1136
|
+
/**
|
|
1137
|
+
* The latest of what was said in a conversation or in a thread of one, for the account's own
|
|
1138
|
+
* command: each message behind a line of the source's own that says how long ago it was
|
|
1139
|
+
* written, by whom and under which moment, its words as a wake carries them and what hangs
|
|
1140
|
+
* under it apart. After a moment alone, where one is given. Named as a wake's key names it,
|
|
1141
|
+
* `<conversation>` or `<conversation>:<thread>`; a thread that is none, the message of a wake
|
|
1142
|
+
* that was written in no thread, is read as its conversation. So much of it all together, the
|
|
1143
|
+
* newest kept, and the first line says how many were left out.
|
|
1144
|
+
*/
|
|
1145
|
+
async function readOf(host, where, options) {
|
|
1146
|
+
const named = NAMED.exec(where);
|
|
1147
|
+
const channel = named?.[1];
|
|
1148
|
+
if (channel === undefined) {
|
|
1149
|
+
return { ok: false, why: "not_a_conversation" };
|
|
1150
|
+
}
|
|
1151
|
+
const after = options.after;
|
|
1152
|
+
if (after !== undefined && !stamp.safeParse(after).success) {
|
|
1153
|
+
return { ok: false, why: "not_a_moment" };
|
|
1154
|
+
}
|
|
1155
|
+
const token = await host.secret(SLACK_USER_TOKEN);
|
|
1156
|
+
if (token === undefined) {
|
|
1157
|
+
return { ok: false, why: "no_token" };
|
|
1158
|
+
}
|
|
1159
|
+
const limit = Math.min(Math.max(Math.trunc(options.limit ?? SLACK_READ_DEFAULT), 1), SLACK_READ_MOST);
|
|
1160
|
+
let thread = named?.[2];
|
|
1161
|
+
let read = thread === undefined
|
|
1162
|
+
? await conversationOf(host, token, channel, { limit }, options.signal)
|
|
1163
|
+
: await threadOf(host, token, channel, thread, { after }, options.signal);
|
|
1164
|
+
// A message that began no thread is all a read of its thread answers: what is asked for is
|
|
1165
|
+
// then what was said around it, in its conversation.
|
|
1166
|
+
if (thread !== undefined &&
|
|
1167
|
+
read.ok &&
|
|
1168
|
+
read.messages.length <= 1 &&
|
|
1169
|
+
read.messages[0]?.thread_ts === undefined) {
|
|
1170
|
+
thread = undefined;
|
|
1171
|
+
read = await conversationOf(host, token, channel, { limit }, options.signal);
|
|
1172
|
+
}
|
|
1173
|
+
if (!read.ok) {
|
|
1174
|
+
return read;
|
|
1175
|
+
}
|
|
1176
|
+
const said = after === undefined
|
|
1177
|
+
? read.messages
|
|
1178
|
+
: read.messages.filter((message) => before(after, message.ts));
|
|
1179
|
+
const latest = said.slice(-limit);
|
|
1180
|
+
const now = Math.floor(options.now / 1000);
|
|
1181
|
+
const who = await whoOf(host, latest, options.signal);
|
|
1182
|
+
// The newest first, for as long as there is room: the oldest are what is left out.
|
|
1183
|
+
const shown = [];
|
|
1184
|
+
let length = 0;
|
|
1185
|
+
for (const message of latest.toReversed()) {
|
|
1186
|
+
const carried = carriedOf(saidOf(message), MAX_WAKE_TEXT);
|
|
1187
|
+
const parts = [
|
|
1188
|
+
{
|
|
1189
|
+
says: `${stretchOf(Math.max(0, now - secondsOf(message.ts)))} ago, from ${writerOf(message, [], who)}, the message ${message.ts}${carried.length > MAX_WAKE_TEXT
|
|
1190
|
+
? ` (cut after ${MAX_WAKE_TEXT} of its ${carried.length} characters)`
|
|
1191
|
+
: ""}`,
|
|
1192
|
+
},
|
|
1193
|
+
...partsOf(carried),
|
|
1194
|
+
];
|
|
1195
|
+
length += wakeTextOf(parts, Number.MAX_SAFE_INTEGER).length + 1;
|
|
1196
|
+
if (shown.length > 0 && length > MAX_READ_LENGTH) {
|
|
1197
|
+
break;
|
|
1198
|
+
}
|
|
1199
|
+
shown.unshift(parts);
|
|
1200
|
+
}
|
|
1201
|
+
const left = latest.length - shown.length;
|
|
1202
|
+
// A conversation is answered from its latest back: where all of it was said after the moment,
|
|
1203
|
+
// more may have been.
|
|
1204
|
+
const more = latest.length < said.length ||
|
|
1205
|
+
(thread === undefined && read.more && said.length === read.messages.length);
|
|
1206
|
+
const rest = thread !== undefined && read.more
|
|
1207
|
+
? // A thread that goes on past what was read is said so: its latest are not these.
|
|
1208
|
+
"; the thread is longer than a read takes, and goes on after these"
|
|
1209
|
+
: more
|
|
1210
|
+
? "; more was said before"
|
|
1211
|
+
: "";
|
|
1212
|
+
return {
|
|
1213
|
+
ok: true,
|
|
1214
|
+
parts: [
|
|
1215
|
+
{
|
|
1216
|
+
says: `${shown.length === 0
|
|
1217
|
+
? "Nothing is said"
|
|
1218
|
+
: `The last ${shown.length === 1 ? "message" : `${shown.length} messages`}`}${after === undefined ? "" : ` after ${after}`} ${thread === undefined ? `in the conversation ${channel}` : `in the thread ${thread} of the conversation ${channel}`} at Slack${shown.length < 2 ? "" : ", the oldest first"}${rest}${left === 0
|
|
1219
|
+
? ""
|
|
1220
|
+
: `; ${left} before ${shown.length === 1 ? "it" : "these"} left out, past what one reading holds: read ${left === 1 ? "it" : "them"} with the messenger's own tool`}. A line that begins with > is what someone wrote`,
|
|
1221
|
+
},
|
|
1222
|
+
...shown.flat(),
|
|
1223
|
+
],
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* One connection of the socket, until it ends: every envelope is acknowledged as it arrives, and
|
|
1228
|
+
* what is said to the agent handed on. Answers whether the messenger asked for a new one in its
|
|
1229
|
+
* place, which it does now and then.
|
|
1230
|
+
*/
|
|
1231
|
+
async function listen(host, token, hear, signal) {
|
|
1232
|
+
const answered = await host.fetch(OPEN_URL, {
|
|
1233
|
+
method: "POST",
|
|
1234
|
+
headers: {
|
|
1235
|
+
authorization: `Bearer ${token}`,
|
|
1236
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
1237
|
+
},
|
|
1238
|
+
signal,
|
|
1239
|
+
});
|
|
1240
|
+
const opened = openedSchema.safeParse(await answered.json().catch(() => undefined));
|
|
1241
|
+
if (!opened.success || !opened.data.ok || opened.data.url === undefined) {
|
|
1242
|
+
throw new SlackError(opened.success ? (opened.data.error ?? "no_address") : "unreadable");
|
|
1243
|
+
}
|
|
1244
|
+
// The address is the messenger's own socket, or it is not opened.
|
|
1245
|
+
const address = URL.canParse(opened.data.url) ? new URL(opened.data.url) : undefined;
|
|
1246
|
+
if (address === undefined ||
|
|
1247
|
+
address.protocol !== "wss:" ||
|
|
1248
|
+
!/(^|\.)slack\.com$/.test(address.hostname)) {
|
|
1249
|
+
throw new SlackError("foreign_address");
|
|
1250
|
+
}
|
|
1251
|
+
const socket = host.socket(address.href);
|
|
1252
|
+
const closing = () => socket.close();
|
|
1253
|
+
signal.addEventListener("abort", closing, { once: true });
|
|
1254
|
+
try {
|
|
1255
|
+
host.log("the slack wake source is connected");
|
|
1256
|
+
host.stands("connected");
|
|
1257
|
+
for await (const text of socket.messages) {
|
|
1258
|
+
let json;
|
|
1259
|
+
try {
|
|
1260
|
+
json = JSON.parse(text);
|
|
1261
|
+
}
|
|
1262
|
+
catch {
|
|
1263
|
+
continue;
|
|
1264
|
+
}
|
|
1265
|
+
const envelope = envelopeSchema.safeParse(json);
|
|
1266
|
+
if (!envelope.success) {
|
|
1267
|
+
continue;
|
|
1268
|
+
}
|
|
1269
|
+
if (envelope.data.type === "disconnect") {
|
|
1270
|
+
return true;
|
|
1271
|
+
}
|
|
1272
|
+
// Acknowledged first, whatever it holds: an event nobody acknowledged is sent again.
|
|
1273
|
+
if (envelope.data.envelope_id !== undefined) {
|
|
1274
|
+
socket.send(JSON.stringify({ envelope_id: envelope.data.envelope_id }));
|
|
1275
|
+
}
|
|
1276
|
+
if (envelope.data.type !== "events_api") {
|
|
1277
|
+
continue;
|
|
1278
|
+
}
|
|
1279
|
+
const heard = heardOf(envelope.data.payload, host.channels);
|
|
1280
|
+
if (heard !== undefined) {
|
|
1281
|
+
hear(heard);
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
return false;
|
|
1285
|
+
}
|
|
1286
|
+
finally {
|
|
1287
|
+
signal.removeEventListener("abort", closing);
|
|
1288
|
+
socket.close();
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
360
1291
|
export const slack = {
|
|
361
1292
|
name: "slack",
|
|
362
1293
|
async run(host, signal) {
|
|
@@ -367,67 +1298,38 @@ export const slack = {
|
|
|
367
1298
|
await host.wait(NO_TOKEN_WAIT_MS, signal);
|
|
368
1299
|
return;
|
|
369
1300
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
if (address === undefined ||
|
|
385
|
-
address.protocol !== "wss:" ||
|
|
386
|
-
!/(^|\.)slack\.com$/.test(address.hostname)) {
|
|
387
|
-
throw new SlackError("foreign_address");
|
|
388
|
-
}
|
|
389
|
-
const socket = host.socket(address.href);
|
|
390
|
-
const closing = () => socket.close();
|
|
391
|
-
signal.addEventListener("abort", closing, { once: true });
|
|
1301
|
+
// What is raised, one wake after another in the order the events arrived: the conversation
|
|
1302
|
+
// is read for each before it is raised, and the socket is not kept waiting for that, since
|
|
1303
|
+
// an event that is not acknowledged within a few seconds is sent again. Where too many
|
|
1304
|
+
// wait, the one whose turn it is goes with nothing read: the latest say what was said before.
|
|
1305
|
+
const reading = new AbortController();
|
|
1306
|
+
let raising = Promise.resolve();
|
|
1307
|
+
let waiting = 0;
|
|
1308
|
+
const hear = (heard) => {
|
|
1309
|
+
waiting += 1;
|
|
1310
|
+
raising = raising.then(() => {
|
|
1311
|
+
waiting -= 1;
|
|
1312
|
+
return raiseOf(host, heard, waiting > MAX_WAITING ? AbortSignal.abort("too_many_waiting") : reading.signal);
|
|
1313
|
+
});
|
|
1314
|
+
};
|
|
392
1315
|
try {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
}
|
|
400
|
-
catch {
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
const envelope = envelopeSchema.safeParse(json);
|
|
404
|
-
if (!envelope.success) {
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
// The messenger asks for a new connection now and then: this one ends, and the runner
|
|
408
|
-
// starts the source again.
|
|
409
|
-
if (envelope.data.type === "disconnect") {
|
|
1316
|
+
// The messenger asks for a new connection now and then: it is opened with what is still
|
|
1317
|
+
// being raised carried over, which nothing of it waits for.
|
|
1318
|
+
while (await listen(host, token, hear, signal)) {
|
|
1319
|
+
host.stands("waiting");
|
|
1320
|
+
await host.wait(REOPEN_WAIT_MS, signal);
|
|
1321
|
+
if (signal.aborted) {
|
|
410
1322
|
return;
|
|
411
1323
|
}
|
|
412
|
-
// Acknowledged first, whatever it holds: an event nobody acknowledged is sent again.
|
|
413
|
-
if (envelope.data.envelope_id !== undefined) {
|
|
414
|
-
socket.send(JSON.stringify({ envelope_id: envelope.data.envelope_id }));
|
|
415
|
-
}
|
|
416
|
-
if (envelope.data.type !== "events_api") {
|
|
417
|
-
continue;
|
|
418
|
-
}
|
|
419
|
-
const wake = slackWakeOf(envelope.data.payload, host.channels);
|
|
420
|
-
if (wake !== undefined) {
|
|
421
|
-
await host.raise(wake).catch((error) => {
|
|
422
|
-
host.log(`a wake from slack was not raised (${error instanceof Error ? error.name : "error"})`);
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
1324
|
}
|
|
426
1325
|
}
|
|
427
1326
|
finally {
|
|
428
|
-
|
|
429
|
-
|
|
1327
|
+
// What was heard before the source ended is raised still, with nothing more read for it:
|
|
1328
|
+
// neither the next connection nor a stop of the runner waits for a messenger to answer.
|
|
1329
|
+
reading.abort("stopped");
|
|
1330
|
+
await raising;
|
|
430
1331
|
}
|
|
431
1332
|
},
|
|
1333
|
+
read: (host, where, options) => readOf(host, where, options),
|
|
432
1334
|
};
|
|
433
1335
|
//# sourceMappingURL=slack.js.map
|