@kana-ai/edgar-ext-slack 0.1.4 → 0.1.6
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 +36 -25
- package/dist/src/api/client.d.ts +1 -1
- package/dist/src/api/client.js +4 -2
- package/dist/src/api/tags.d.ts +5 -0
- package/dist/src/api/tags.js +100 -0
- package/dist/src/inbound/route.d.ts +3 -1
- package/dist/src/inbound/route.js +100 -16
- package/dist/src/outbound/layout.d.ts +38 -12
- package/dist/src/outbound/layout.js +330 -63
- package/dist/src/outbound/messages.d.ts +17 -3
- package/dist/src/outbound/messages.js +42 -2
- package/dist/src/outbound/places.d.ts +11 -6
- package/dist/src/outbound/places.js +94 -26
- package/dist/src/outbound/progress.js +346 -84
- package/dist/src/outbound/tools.js +2 -3
- package/dist/src/outbound/words.d.ts +56 -8
- package/dist/src/outbound/words.js +126 -13
- package/dist/src/schemas/config.d.ts +1 -0
- package/dist/src/schemas/config.js +5 -1
- package/dist/src/schemas/events.d.ts +66 -3
- package/dist/src/schemas/events.js +65 -5
- package/dist/src/schemas/slack.d.ts +19 -2
- package/dist/src/schemas/slack.js +19 -4
- package/edgar-extension.json +1 -1
- package/package.json +3 -3
- package/schemas/config.json +9 -1
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
import { cardLine, defineEventHook, done, endNote, noteCard, notePull, ok, runCard, } from "@kana-ai/edgar-sdk";
|
|
2
2
|
import { apiAccess } from "#api/client";
|
|
3
|
-
import { askLayout, noteLayout, planLayout, questionLayout,
|
|
4
|
-
import { findMarked, listed, mark, newestMarked, post, status, threadStatus, update, waitingRelays, } from "#outbound/messages";
|
|
5
|
-
import { cardThread, directOf, homeOf, mentioned, oneMore,
|
|
3
|
+
import { answerLayout, behalfLine, askLayout, noteLayout, planLayout, questionLayout, stageStatus, style, usedLayout, } from "#outbound/layout";
|
|
4
|
+
import { findMarked, listed, mark, newestMarked, post, reacted, remove, status, threadStatus, update, waitingRelays, } from "#outbound/messages";
|
|
5
|
+
import { cardThread, directOf, homeOf, joinedRoot, mentioned, oneMore, rootOf, stopperOf, taggedOf, threadOf, } from "#outbound/places";
|
|
6
6
|
import { cutEscaped, escape } from "#outbound/tools";
|
|
7
|
-
import { ANSWER, ANSWER_FLOWS,
|
|
8
|
-
import { settingsOf } from "#schemas/config";
|
|
9
|
-
import { answerOutcome, approvalRequested, failedOutcome, openedOutcome, outcomeRecord, planState, postOutcome, pullOutcome, questionOutcome, reviewOutcome, runRead, runTrigger, slackOrigin, waitingForSlot, } from "#schemas/events";
|
|
10
|
-
/** The most characters of a call's input an approval question shows; a section's text holds 3,000. */
|
|
11
|
-
const INPUT_CHARS = 2400;
|
|
12
|
-
/** A call's input as the text of a code block: markup made plain, no two backticks side by side to end the block, and at most `INPUT_CHARS` characters, cut between two characters and never inside an entity. `cut` says that a part is left out. */
|
|
13
|
-
function inputBlock(shown) {
|
|
14
|
-
return cutEscaped(escape(shown).replace(/`(?=`)/g, "`"), INPUT_CHARS);
|
|
15
|
-
}
|
|
7
|
+
import { ACTED, ANSWER, ANSWER_FLOWS, REPLY, REVIEW_WAIT, SAYS, reviewedLabel, } from "#outbound/words";
|
|
8
|
+
import { settingsOf, userId } from "#schemas/config";
|
|
9
|
+
import { actorsOutcome, answerOutcome, approvalRequested, endActors, endPaid, failedOutcome, openedOutcome, outcomeRecord, outputOutcome, planState, postOutcome, pullOutcome, questionOutcome, reviewOutcome, runRead, runRequester, runTrigger, slackOrigin, waitingForSlot, } from "#schemas/events";
|
|
16
10
|
/** The card events of a person who writes to EDGAR. */
|
|
17
11
|
const written = new Set(["comment", "mention"]);
|
|
18
12
|
/** `Began` of each run that has not ended. It is a cache and no more: the hook's process ends after `idle_seconds` with no work, so a run that is not here is read again from its first event. */
|
|
@@ -24,11 +18,33 @@ function beganFrom(payload) {
|
|
|
24
18
|
return {
|
|
25
19
|
card: noteCard(card),
|
|
26
20
|
assignee: trigger?.assignee,
|
|
21
|
+
requester: trigger?.requested_by === undefined
|
|
22
|
+
? undefined
|
|
23
|
+
: { name: trigger.requested_by, id: trigger.requested_by_id },
|
|
24
|
+
ask: trigger?.ask,
|
|
27
25
|
quiet: part || written.has(card?.event ?? ""),
|
|
26
|
+
part,
|
|
28
27
|
workflow: trigger?.workflow,
|
|
29
28
|
relayed: slackOrigin.safeParse(trigger?.relayed).data,
|
|
29
|
+
requestedBy: requestedByOf(payload),
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
|
+
function requestedByOf(payload) {
|
|
33
|
+
const asked = runRequester.safeParse(payload).data;
|
|
34
|
+
if (asked?.requester !== undefined)
|
|
35
|
+
return asked.requester;
|
|
36
|
+
const id = asked?.requested_by_id;
|
|
37
|
+
if (id !== undefined && userId.safeParse(id).success)
|
|
38
|
+
return { slack: id };
|
|
39
|
+
return asked?.requested_by === undefined ? undefined : { name: asked.requested_by };
|
|
40
|
+
}
|
|
41
|
+
/** The people a question or a decision of the run tags: those of `named`, then the run's requester, then the card's assignee when `mentioned` finds their Slack user, so EDGAR's own tracker user is never written. */
|
|
42
|
+
async function whoCanAct(ctx, run, message, named, first, settings) {
|
|
43
|
+
const { assignee, requestedBy } = first;
|
|
44
|
+
const user = assignee === undefined ? undefined : await mentioned(ctx, run, message, assignee, settings);
|
|
45
|
+
const entries = requestedBy === undefined ? named : [...named, requestedBy];
|
|
46
|
+
return await taggedOf(ctx, run, message, entries, settings, user === undefined ? [] : [`<@${user}>`]);
|
|
47
|
+
}
|
|
32
48
|
/** What the run of `event` began with: from the event when it is the `run_created`, else from memory, else from the run's first event, which `runs.read` gives. A run that does not read is one log line, and its messages name no card and are not quiet. */
|
|
33
49
|
async function beganOf(ctx, event) {
|
|
34
50
|
const known = event.kind === "run_created" ? beganFrom(event.payload) : began.get(event.run);
|
|
@@ -46,16 +62,22 @@ async function beganOf(ctx, event) {
|
|
|
46
62
|
return {
|
|
47
63
|
card: undefined,
|
|
48
64
|
assignee: undefined,
|
|
65
|
+
requester: undefined,
|
|
66
|
+
ask: undefined,
|
|
49
67
|
quiet: false,
|
|
68
|
+
part: false,
|
|
50
69
|
workflow: undefined,
|
|
51
70
|
relayed: undefined,
|
|
71
|
+
requestedBy: undefined,
|
|
52
72
|
};
|
|
53
73
|
}
|
|
54
74
|
const found = beganFrom(first.payload);
|
|
55
75
|
began.set(event.run, found);
|
|
56
76
|
return found;
|
|
57
77
|
}
|
|
58
|
-
/**
|
|
78
|
+
/** The plan of each run whose card's thread does not list it yet: the thread and its status message open at the run's second stage, or at the first one that asks a person, so a run that ends after one stage with nothing to tell leaves no line. A cache and no more: after a restart, the plan starts at the run's next stage. */
|
|
79
|
+
const held = new Map();
|
|
80
|
+
/** Writes the card's one status message in its thread after `change`, and answers the plan. The message is the newest plan of the thread, of whichever run wrote it: a run changes it while the message is its own, and with `first` a run takes it over once the run that wrote it is over, so each run of a card updates the same message in place and the thread never gets a second one; a run that starts while another run of the card still works shows in it when that run ends. With `first`, a thread with no status message gets one, whose first line links the card that the run began with in a thread that a person's message opened, as the card's own thread has a first message that links it; without it, a run whose message is not its own changes nothing and the answer is `undefined`, so the end of a run that showed no plan posts no empty one. */
|
|
59
81
|
async function replan(ctx, event, place, settings, change, first) {
|
|
60
82
|
const all = await listed(ctx, place.channel, place.thread);
|
|
61
83
|
return all.ok ? await replanIn(ctx, event, place, settings, change, all.value, first) : all;
|
|
@@ -63,13 +85,20 @@ async function replan(ctx, event, place, settings, change, first) {
|
|
|
63
85
|
/** `replan` on the messages `all` of the thread, read once by the caller. */
|
|
64
86
|
async function replanIn(ctx, event, place, settings, change, all, first) {
|
|
65
87
|
const { channel, thread } = place;
|
|
66
|
-
const found = newestMarked(all, "edgar_plan"
|
|
88
|
+
const found = newestMarked(all, "edgar_plan");
|
|
67
89
|
const ts = found?.ts;
|
|
68
|
-
|
|
90
|
+
const payload = found?.metadata?.event_payload;
|
|
91
|
+
const own = payload?.["run"] === event.run;
|
|
92
|
+
const free = found === undefined || planState.safeParse(payload).data?.ended !== undefined;
|
|
93
|
+
if (!own && (first === undefined || !free))
|
|
69
94
|
return ok(undefined);
|
|
70
|
-
const kept = planState.safeParse(
|
|
71
|
-
const card = first?.card === undefined
|
|
72
|
-
|
|
95
|
+
const kept = planState.safeParse(own ? payload : undefined);
|
|
96
|
+
const card = first?.card === undefined || place.origin === undefined
|
|
97
|
+
? {}
|
|
98
|
+
: { card: cardLine(first.card, style) };
|
|
99
|
+
const fresh = held.get(event.run) ?? { run: event.run, tasks: [], pulls: [], ...card };
|
|
100
|
+
held.delete(event.run);
|
|
101
|
+
const plan = change(kept.success ? kept.data : fresh);
|
|
73
102
|
const { text, blocks } = planLayout(plan, settings);
|
|
74
103
|
const fields = { blocks, metadata: mark("edgar_plan", plan) };
|
|
75
104
|
const sent = ts === undefined
|
|
@@ -115,7 +144,7 @@ async function ask(ctx, event, place, settings, first) {
|
|
|
115
144
|
const asked = approvalRequested.safeParse(event.payload);
|
|
116
145
|
if (!asked.success)
|
|
117
146
|
return ok(undefined);
|
|
118
|
-
const { operation_id, tool, input, assignee } = asked.data;
|
|
147
|
+
const { operation_id, tool, policy, input, assignee } = asked.data;
|
|
119
148
|
const { channel, thread } = place;
|
|
120
149
|
const same = (p) => p["operation"] === operation_id;
|
|
121
150
|
const found = await findMarked(ctx, channel, thread, "edgar_approval", same);
|
|
@@ -130,9 +159,8 @@ async function ask(ctx, event, place, settings, first) {
|
|
|
130
159
|
run: event.run,
|
|
131
160
|
tool,
|
|
132
161
|
operation: operation_id,
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
: inputBlock(JSON.stringify(input, null, 2)),
|
|
162
|
+
policy,
|
|
163
|
+
input,
|
|
136
164
|
card: first.card,
|
|
137
165
|
user,
|
|
138
166
|
consoleUrl: settings.console_url,
|
|
@@ -147,7 +175,7 @@ async function ask(ctx, event, place, settings, first) {
|
|
|
147
175
|
await status(ctx, place.origin, "suspended");
|
|
148
176
|
return ok(undefined);
|
|
149
177
|
}
|
|
150
|
-
/** Posts once in the card's thread the question that the done stage `stage` left open, to the
|
|
178
|
+
/** Posts once in the card's thread the question that the done stage `stage` left open, to the people of `whoCanAct`. EDGAR goes on with its work, and a reply in the thread goes on the card as the person's comment, which starts the run that answers it. */
|
|
151
179
|
async function question(ctx, event, stage, place, settings, first) {
|
|
152
180
|
const asked = questionOutcome.safeParse(event.payload).data?.outcome.output.question;
|
|
153
181
|
if (asked === undefined)
|
|
@@ -160,26 +188,28 @@ async function question(ctx, event, stage, place, settings, first) {
|
|
|
160
188
|
return found;
|
|
161
189
|
if (found.value !== undefined)
|
|
162
190
|
return ok(undefined);
|
|
163
|
-
const
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
: await mentioned(ctx, run, "the question", assignee, settings);
|
|
167
|
-
const { text, blocks } = questionLayout({
|
|
168
|
-
run,
|
|
169
|
-
card: first.card,
|
|
170
|
-
user,
|
|
171
|
-
question: asked,
|
|
172
|
-
});
|
|
191
|
+
const named = actorsOutcome.safeParse(event.payload).data?.outcome.output.who_can_act ?? [];
|
|
192
|
+
const users = await whoCanAct(ctx, run, "the question", named, first, settings);
|
|
193
|
+
const { text, blocks } = questionLayout({ run, card: first.card, users, question: asked });
|
|
173
194
|
const sent = await post(ctx, channel, thread, text, {
|
|
174
195
|
blocks,
|
|
175
196
|
metadata: mark("edgar_question", { run, stage }),
|
|
176
197
|
});
|
|
177
198
|
return sent.ok ? ok(undefined) : sent;
|
|
178
199
|
}
|
|
200
|
+
/** How long after it happened an event counts as late: a backlog of events, or a restart, hands it long after the run moved on. A late event shows no progress and opens no card thread, so a run long over never posts a new message at the top of the channel. */
|
|
201
|
+
const LATE_MS = 15 * 60 * 1000;
|
|
202
|
+
/** Whether `event` reaches the hook `LATE_MS` or more after it happened. */
|
|
203
|
+
function late(event) {
|
|
204
|
+
return Date.now() - event.at >= LATE_MS;
|
|
205
|
+
}
|
|
206
|
+
/** The events that only show a run's progress, which a late one no longer has to show. */
|
|
207
|
+
const progressOnly = new Set(["run_created", "attempt_started", "waiting_for_slot"]);
|
|
179
208
|
/** The kinds of end note that a person must see also when the run showed no plan. */
|
|
180
209
|
const asks = new Set([
|
|
181
210
|
"recovered",
|
|
182
211
|
"decision",
|
|
212
|
+
"handed",
|
|
183
213
|
"opened",
|
|
184
214
|
"merged",
|
|
185
215
|
"approved",
|
|
@@ -191,18 +221,30 @@ const asks = new Set([
|
|
|
191
221
|
function markOf(note) {
|
|
192
222
|
return note?.what === "opened" ? "edgar_ready" : "edgar_end";
|
|
193
223
|
}
|
|
194
|
-
/** Posts the note at `place` once: a note with the same mark for the run is not posted again.
|
|
195
|
-
async function noteAt(ctx, place, run,
|
|
224
|
+
/** Posts the note at `place` once: a note with the same mark for the run is not posted again. A note in a thread stays in the thread: nothing is sent to the top of the conversation. With `used`, a thread that already holds a question of the run gets `used` under that question, one time, in place of the note, so the question is not asked twice and the run's end is no message of its own. */
|
|
225
|
+
async function noteAt(ctx, place, run, layout, kind, used) {
|
|
196
226
|
const { channel, thread } = place;
|
|
197
|
-
const
|
|
198
|
-
if (!
|
|
199
|
-
return
|
|
200
|
-
if (
|
|
227
|
+
const all = await listed(ctx, channel, thread);
|
|
228
|
+
if (!all.ok)
|
|
229
|
+
return all;
|
|
230
|
+
if (newestMarked(all.value, kind, (p) => p["run"] === run) !== undefined)
|
|
201
231
|
return ok(undefined);
|
|
202
|
-
const
|
|
203
|
-
|
|
232
|
+
const asked = used === undefined || thread === undefined
|
|
233
|
+
? undefined
|
|
234
|
+
: newestMarked(all.value, "edgar_question", (p) => p["run"] === run);
|
|
235
|
+
if (asked !== undefined && used !== undefined) {
|
|
236
|
+
const payload = asked.metadata?.event_payload ?? {};
|
|
237
|
+
if (payload["ended"] === true || used.text === "")
|
|
238
|
+
return ok(undefined);
|
|
239
|
+
const blocks = asked.blocks === undefined ? [] : [...asked.blocks, ...used.blocks];
|
|
240
|
+
const metadata = mark("edgar_question", { ...payload, ended: true });
|
|
241
|
+
const text = `${asked.text ?? ""}\n${used.text}`;
|
|
242
|
+
const changed = await update(ctx, channel, asked.ts, text, { blocks, metadata });
|
|
243
|
+
return changed.ok ? ok(undefined) : changed;
|
|
244
|
+
}
|
|
245
|
+
const sent = await post(ctx, channel, thread, layout.text, {
|
|
246
|
+
blocks: layout.blocks,
|
|
204
247
|
metadata: mark(kind, { run }),
|
|
205
|
-
...(shown && thread !== undefined ? { reply_broadcast: true } : {}),
|
|
206
248
|
});
|
|
207
249
|
if (!sent.ok)
|
|
208
250
|
return sent;
|
|
@@ -210,13 +252,70 @@ async function noteAt(ctx, place, run, note, shown, kind) {
|
|
|
210
252
|
ctx.log(`run ${run}: ${what} went to ${sent.value.channel} at ${sent.value.ts}`);
|
|
211
253
|
return ok(undefined);
|
|
212
254
|
}
|
|
213
|
-
/**
|
|
214
|
-
async function
|
|
255
|
+
/** Who asked for the work, as Slack writes them: a mention of the Slack user that opened the card, a mention of the user that `mentioned` finds for the name, else the name as text. None when the card names nobody. */
|
|
256
|
+
async function behalfOf(ctx, event, first, settings) {
|
|
257
|
+
const { requester } = first;
|
|
258
|
+
if (requester === undefined)
|
|
259
|
+
return undefined;
|
|
260
|
+
const fromSlack = slackOrigin.safeParse(event.origin).success;
|
|
261
|
+
if (fromSlack && userId.safeParse(requester.id).success)
|
|
262
|
+
return `<@${requester.id ?? ""}>`;
|
|
263
|
+
const user = await mentioned(ctx, event.run, "the card's first message", requester.name, settings);
|
|
264
|
+
return user === undefined ? escape(requester.name) : `<@${user}>`;
|
|
265
|
+
}
|
|
266
|
+
/** Whose own key or subscription the run of `event` ran on, as Slack writes them: a mention of the Slack id that the engine found in `[people]`, else of the user that `mentioned` finds for their email or name, else their name; and what it was, such as "Claude subscription". None when the run ran on EDGAR's own. */
|
|
267
|
+
async function paidOf(ctx, event, settings) {
|
|
268
|
+
const paid = endPaid.safeParse(event.payload).data?.end.paid_by;
|
|
269
|
+
if (paid === undefined)
|
|
270
|
+
return undefined;
|
|
271
|
+
const vendor = paid.provider === undefined
|
|
272
|
+
? ""
|
|
273
|
+
: `${paid.provider.charAt(0).toUpperCase()}${paid.provider.slice(1)} `;
|
|
274
|
+
const what = `${vendor}${paid.credential === "oauth" ? "subscription" : "API key"}`;
|
|
275
|
+
if (userId.safeParse(paid.slack).success)
|
|
276
|
+
return { who: `<@${paid.slack ?? ""}>`, what };
|
|
277
|
+
for (const given of [paid.email, paid.name]) {
|
|
278
|
+
const user = given === undefined
|
|
279
|
+
? undefined
|
|
280
|
+
: await mentioned(ctx, event.run, "the card's first message", given, settings);
|
|
281
|
+
if (user !== undefined)
|
|
282
|
+
return { who: `<@${user}>`, what };
|
|
283
|
+
}
|
|
284
|
+
return { who: escape(paid.name), what };
|
|
285
|
+
}
|
|
286
|
+
/** What the card's first message shows for the run of `event` at `status`: the card's ask, who asked for the work, the card's holder when Slack knows them, whose own key or subscription the run ran on, set again at each change so an earlier run's never stays, and the pull requests of `pulls` that the run opened. */
|
|
287
|
+
async function viewOf(ctx, event, first, settings, status, pulls = []) {
|
|
288
|
+
const own = pulls.flatMap(({ url, label, referenced }) => (referenced ? [] : [{ url, label }]));
|
|
289
|
+
const by = await behalfOf(ctx, event, first, settings);
|
|
290
|
+
const paid = await paidOf(ctx, event, settings);
|
|
291
|
+
const holder = first.assignee === undefined
|
|
292
|
+
? undefined
|
|
293
|
+
: await mentioned(ctx, event.run, "the card's first message", first.assignee, settings);
|
|
294
|
+
return {
|
|
295
|
+
status,
|
|
296
|
+
...(first.ask === undefined ? {} : { ask: first.ask }),
|
|
297
|
+
...(by === undefined ? {} : { by }),
|
|
298
|
+
...(holder === undefined ? {} : { holder: `<@${holder}>` }),
|
|
299
|
+
paid: paid?.who,
|
|
300
|
+
paid_with: paid?.what,
|
|
301
|
+
...(own.length === 0 ? {} : { pulls: own }),
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
/** Shows `view` on the card's first message at `home`, for a card that did not come from Slack: there the first message is EDGAR's own, while a person wrote the first message of a Slack card. A first message that Slack does not change is one log line. */
|
|
305
|
+
async function restatus(ctx, event, home, card, view) {
|
|
306
|
+
if (home.origin !== undefined || home.direct || card === undefined)
|
|
307
|
+
return;
|
|
308
|
+
const changed = await rootOf(ctx, home.channel, card, event.run, view);
|
|
309
|
+
if (!changed.ok)
|
|
310
|
+
ctx.log(`run ${event.run}: the card's first message keeps its status (${changed.error.message})`);
|
|
311
|
+
}
|
|
312
|
+
/** Posts `note` once in the card's thread at `home` and in each `dm`: `found` is the card's thread at home when the run has one, and a note whose run is in no thread goes in the thread of its card. The note greets the card's assignee with a mention when `mentioned` finds their Slack user, and a note that asks that person for a decision also goes in the card's thread of their own DM; a DM that Slack refuses is one log line, and the other notes are still posted. The note of a pull request that is ready for people also goes in the card's thread in `channel`, as a reply there and nothing more. */
|
|
313
|
+
async function tell(ctx, event, settings, home, found, note, view, tagged = []) {
|
|
215
314
|
const places = [];
|
|
216
315
|
if (home !== undefined && !home.direct) {
|
|
217
316
|
const under = found !== undefined || note?.card === undefined
|
|
218
317
|
? ok(found)
|
|
219
|
-
: await rootOf(ctx, home.channel, note.card, event.run);
|
|
318
|
+
: await rootOf(ctx, home.channel, note.card, event.run, view);
|
|
220
319
|
if (!under.ok)
|
|
221
320
|
return under;
|
|
222
321
|
places.push({ channel: home.channel, thread: under.value });
|
|
@@ -227,14 +326,14 @@ async function tell(ctx, event, settings, home, found, note) {
|
|
|
227
326
|
return channel;
|
|
228
327
|
const thread = note?.card === undefined
|
|
229
328
|
? ok(undefined)
|
|
230
|
-
: await rootOf(ctx, channel.value, note.card, event.run);
|
|
329
|
+
: await rootOf(ctx, channel.value, note.card, event.run, view);
|
|
231
330
|
if (!thread.ok)
|
|
232
331
|
return thread;
|
|
233
332
|
places.push({ channel: channel.value, thread: thread.value });
|
|
234
333
|
}
|
|
235
334
|
const shown = note?.what === "opened" ? settings.channel : undefined;
|
|
236
335
|
if (shown !== undefined) {
|
|
237
|
-
const team = await oneMore(ctx, shown, note?.card, event.run, places);
|
|
336
|
+
const team = await oneMore(ctx, shown, note?.card, event.run, places, view);
|
|
238
337
|
if (!team.ok)
|
|
239
338
|
return team;
|
|
240
339
|
if (team.value !== undefined)
|
|
@@ -245,20 +344,41 @@ async function tell(ctx, event, settings, home, found, note) {
|
|
|
245
344
|
: await mentioned(ctx, event.run, "the end note", note.assignee, settings);
|
|
246
345
|
if (user !== undefined && note?.what === "decision") {
|
|
247
346
|
const direct = await directOf(ctx, user);
|
|
248
|
-
const own = direct.ok
|
|
347
|
+
const own = direct.ok
|
|
348
|
+
? await oneMore(ctx, direct.value, note.card, event.run, places, view)
|
|
349
|
+
: direct;
|
|
249
350
|
if (!own.ok)
|
|
250
351
|
ctx.log(`run ${event.run}: the decision note did not go to the DM of ${note.assignee ?? user}, because Slack refused it (${own.error.kind})`);
|
|
251
352
|
else if (own.value !== undefined)
|
|
252
353
|
places.push(own.value);
|
|
253
354
|
}
|
|
254
|
-
const
|
|
355
|
+
const by = note?.facts.stoppedBy;
|
|
356
|
+
const stopper = by === undefined ? undefined : await stopperOf(ctx, event.run, by, settings);
|
|
357
|
+
const layout = noteLayout(event.run, note, user, behalfLine(view), stopper, tagged);
|
|
358
|
+
const asked = endActors.safeParse(event.payload).data?.end.question?.trim();
|
|
359
|
+
const used = note?.what === "decision" && asked !== undefined && note.facts.summary === asked
|
|
360
|
+
? usedLayout(note)
|
|
361
|
+
: undefined;
|
|
255
362
|
for (const place of places) {
|
|
256
|
-
const sent = await noteAt(ctx, place, event.run, layout,
|
|
363
|
+
const sent = await noteAt(ctx, place, event.run, layout, markOf(note), used);
|
|
257
364
|
if (!sent.ok)
|
|
258
365
|
return sent;
|
|
259
366
|
}
|
|
260
367
|
return ok(undefined);
|
|
261
368
|
}
|
|
369
|
+
/** Whether another run, such as a step of this one, already asked `asked` in the card's thread `all` while the run of `event` worked, in its decision note or its question: a chain of runs asks a person one time. */
|
|
370
|
+
function askedInChain(all, event, asked) {
|
|
371
|
+
const since = endActors.safeParse(event.payload).data?.end.started;
|
|
372
|
+
if (asked === undefined || since === undefined)
|
|
373
|
+
return false;
|
|
374
|
+
const words = escape(asked.trim()).slice(0, 100);
|
|
375
|
+
return all.some((message) => {
|
|
376
|
+
const kind = message.metadata?.event_type;
|
|
377
|
+
const other = message.metadata?.event_payload["run"] !== event.run;
|
|
378
|
+
const asks = kind === "edgar_end" || kind === "edgar_question";
|
|
379
|
+
return (asks && other && Number(message.ts) * 1000 >= since && (message.text ?? "").includes(words));
|
|
380
|
+
});
|
|
381
|
+
}
|
|
262
382
|
/** Posts once in the card's thread that EDGAR will post the result in the channel that a person asked for, when a done stage's output names one in `post_in`. The message's metadata `edgar_post_to` keeps the channel and the run that heard it, so the end of a later run that is done keeps the promise. */
|
|
263
383
|
async function promise(ctx, event, at) {
|
|
264
384
|
const channel = postOutcome.safeParse(event.payload).data?.outcome.output.post_in;
|
|
@@ -342,8 +462,8 @@ function readyNote(run, first, plan, settings) {
|
|
|
342
462
|
}
|
|
343
463
|
/** The most characters of an answer in a thread; Slack refuses a text over 40,000. */
|
|
344
464
|
const ANSWER_CHARS = 39_000;
|
|
345
|
-
/** Marks the oldest reply of `all` that waits for an answer as answered by `run`, unless `run` answered one already
|
|
346
|
-
async function closeRelay(ctx, at, run, all) {
|
|
465
|
+
/** Marks the oldest reply of `all` that waits for an answer as answered by `run`, unless `run` answered one already, and swaps the reaction on the person's message for the sign of `worked`. A reply that is not marked is one log line: a later reply then counts as one more while EDGAR is busy, for a while. */
|
|
466
|
+
async function closeRelay(ctx, at, run, all, worked) {
|
|
347
467
|
if (newestMarked(all, "edgar_relay_done", (p) => p["run"] === run) !== undefined)
|
|
348
468
|
return;
|
|
349
469
|
const [oldest] = waitingRelays(all);
|
|
@@ -353,36 +473,97 @@ async function closeRelay(ctx, at, run, all) {
|
|
|
353
473
|
const closed = await update(ctx, at.channel, oldest.ts, oldest.text ?? "", { metadata });
|
|
354
474
|
if (!closed.ok)
|
|
355
475
|
ctx.log(`run ${run}: the relayed reply stays open (${closed.error.message})`);
|
|
476
|
+
const asked = oldest.metadata?.event_payload["asked"];
|
|
477
|
+
if (typeof asked === "string")
|
|
478
|
+
await reacted(ctx, at.channel, asked, worked);
|
|
479
|
+
}
|
|
480
|
+
/** The words of `ACTED` for `stage`, if any. */
|
|
481
|
+
function actedOn(stage) {
|
|
482
|
+
return Object.hasOwn(ACTED, stage) ? ACTED[stage] : undefined;
|
|
483
|
+
}
|
|
484
|
+
/** Posts `text` once in the thread `at` for the stage `stage` of `run` at `when`, to the person of the oldest reply that waits there. The reply stays open: only an answer or the run's end closes it. */
|
|
485
|
+
async function confirm(ctx, run, stage, when, at, text) {
|
|
486
|
+
const all = await listed(ctx, at.channel, at.thread);
|
|
487
|
+
if (!all.ok)
|
|
488
|
+
return all;
|
|
489
|
+
const same = (p) => p["run"] === run && p["stage"] === stage && p["when"] === when;
|
|
490
|
+
if (newestMarked(all.value, "edgar_said", same) !== undefined)
|
|
491
|
+
return ok(undefined);
|
|
492
|
+
const user = waitingRelays(all.value)[0]?.metadata?.event_payload["user"];
|
|
493
|
+
const said = ANSWER.said(typeof user === "string" ? user : undefined, text);
|
|
494
|
+
const sent = await post(ctx, at.channel, at.thread, said, {
|
|
495
|
+
metadata: mark("edgar_said", { run, stage, when }),
|
|
496
|
+
});
|
|
497
|
+
return sent.ok ? ok(undefined) : sent;
|
|
498
|
+
}
|
|
499
|
+
/** A stage of a run that a person's words in Slack started begins: the thread says what EDGAR starts for them, when `ACTED` has words for the stage, and the moving line under the thread names the stage, so a reply that takes a while is never silence. */
|
|
500
|
+
async function relayedStart(ctx, event, at, settings) {
|
|
501
|
+
const { stage } = event;
|
|
502
|
+
if (stage === null)
|
|
503
|
+
return ok(undefined);
|
|
504
|
+
const words = actedOn(stage)?.started;
|
|
505
|
+
if (words !== undefined) {
|
|
506
|
+
const said = await confirm(ctx, event.run, stage, "started", at, words);
|
|
507
|
+
if (!said.ok)
|
|
508
|
+
return said;
|
|
509
|
+
}
|
|
510
|
+
await threadStatus(ctx, at.channel, at.thread, stageStatus(stage, settings));
|
|
511
|
+
return ok(undefined);
|
|
356
512
|
}
|
|
357
513
|
/** Posts one message of `run` in the thread `at` that a person wrote in, once for the run, with the words of `said` for the person of the oldest reply that waits there, and marks that reply answered. */
|
|
358
|
-
async function answerIn(ctx, run, at,
|
|
514
|
+
async function answerIn(ctx, run, at, say) {
|
|
359
515
|
const all = await listed(ctx, at.channel, at.thread);
|
|
360
516
|
if (!all.ok)
|
|
361
517
|
return all;
|
|
362
518
|
if (newestMarked(all.value, "edgar_answer", (p) => p["run"] === run) !== undefined)
|
|
363
519
|
return ok(undefined);
|
|
364
520
|
const user = waitingRelays(all.value)[0]?.metadata?.event_payload["user"];
|
|
365
|
-
const text =
|
|
521
|
+
const { text, blocks } = say(typeof user === "string" ? user : undefined);
|
|
366
522
|
const sent = await post(ctx, at.channel, at.thread, text, {
|
|
523
|
+
...(blocks.length === 0 ? {} : { blocks }),
|
|
367
524
|
metadata: mark("edgar_answer", { run }),
|
|
368
525
|
});
|
|
369
526
|
if (!sent.ok)
|
|
370
527
|
return sent;
|
|
371
|
-
await closeRelay(ctx, at, run, all.value);
|
|
528
|
+
await closeRelay(ctx, at, run, all.value, true);
|
|
372
529
|
return ok(undefined);
|
|
373
530
|
}
|
|
374
|
-
/** A done stage of a run that a person's words in Slack started:
|
|
375
|
-
async function relayedOutcome(ctx, event, first, at) {
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
531
|
+
/** A done stage of a run that a person's words in Slack started: what it did for them, when `ACTED` has words for it, with the text of its output's `says` field; its answer; and a card that a chat opened for work, so they know where to follow it, each go in their thread. */
|
|
532
|
+
async function relayedOutcome(ctx, event, first, at, settings) {
|
|
533
|
+
const { stage } = event;
|
|
534
|
+
const words = stage === null ? undefined : actedOn(stage);
|
|
535
|
+
const word = outcomeRecord.safeParse(event.payload).data?.outcome.outcome;
|
|
536
|
+
if (stage !== null && words?.done !== undefined && word === "done") {
|
|
537
|
+
const output = outputOutcome.safeParse(event.payload).data?.outcome.output;
|
|
538
|
+
const extra = words.says === undefined ? undefined : output?.[words.says];
|
|
539
|
+
const text = typeof extra === "string" && extra.trim() !== ""
|
|
540
|
+
? `${words.done}\n${cutEscaped(escape(extra.trim()), ANSWER_CHARS).text}`
|
|
541
|
+
: words.done;
|
|
542
|
+
const said = await confirm(ctx, event.run, stage, "done", at, text);
|
|
543
|
+
if (!said.ok)
|
|
544
|
+
return said;
|
|
545
|
+
}
|
|
546
|
+
const answer = answerOutcome.safeParse(event.payload).data?.outcome.output;
|
|
547
|
+
if (answer !== undefined) {
|
|
548
|
+
const base = settings.console_url?.replace(/\/$/, "");
|
|
549
|
+
const page = base === undefined ? undefined : `${base}/runs/${encodeURIComponent(event.run)}`;
|
|
550
|
+
const answered = {
|
|
551
|
+
text: answer.text,
|
|
552
|
+
sources: answer.sources ?? [],
|
|
553
|
+
confident: answer.confident,
|
|
554
|
+
card: first.card,
|
|
555
|
+
page,
|
|
556
|
+
};
|
|
557
|
+
return await answerIn(ctx, event.run, at, (user) => answerLayout({ ...answered, user }, ANSWER_CHARS));
|
|
380
558
|
}
|
|
381
559
|
const card = first.card === undefined ? openedOutcome.safeParse(event.payload).data : undefined;
|
|
382
560
|
const id = card?.outcome.output.id;
|
|
383
561
|
if (id === undefined)
|
|
384
562
|
return ok(undefined);
|
|
385
|
-
return await answerIn(ctx, event.run, at, () =>
|
|
563
|
+
return await answerIn(ctx, event.run, at, () => ({
|
|
564
|
+
text: ANSWER.opened(escape(id)),
|
|
565
|
+
blocks: [],
|
|
566
|
+
}));
|
|
386
567
|
}
|
|
387
568
|
/** The end of a run that a person's words in Slack started: the reply it answered is closed and the thread's status ends. An answer run that did not end done says so in one plain line there, unless it already answered, and the answer is `true`, as nothing else needs saying. */
|
|
388
569
|
async function relayedEnd(ctx, event, first, at, word) {
|
|
@@ -399,11 +580,22 @@ async function relayedEnd(ctx, event, first, at, word) {
|
|
|
399
580
|
if (!sent.ok)
|
|
400
581
|
return sent;
|
|
401
582
|
}
|
|
402
|
-
await closeRelay(ctx, at, event.run, all.value);
|
|
583
|
+
await closeRelay(ctx, at, event.run, all.value, word === "done" && !failed);
|
|
403
584
|
await threadStatus(ctx, at.channel, at.thread, "");
|
|
404
585
|
return ok(failed);
|
|
405
586
|
}
|
|
406
|
-
/**
|
|
587
|
+
/** Whether the run of `run` did no work: none of its stages ended other than skipped, as when no stage applies to the card. A run that does not read did work, with one log line, so its end still shows. */
|
|
588
|
+
async function idle(ctx, run) {
|
|
589
|
+
const read = await ctx.runs.read(run);
|
|
590
|
+
const events = read.ok ? runRead.safeParse(read.value).data?.events : undefined;
|
|
591
|
+
if (events === undefined) {
|
|
592
|
+
ctx.log(`run ${run}: the run did not read, so its end shows as the end of a run that worked`);
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
595
|
+
return events.every(({ kind, payload }) => kind !== "attempt_outcome" ||
|
|
596
|
+
outcomeRecord.safeParse(payload).data?.outcome.outcome === "skipped");
|
|
597
|
+
}
|
|
598
|
+
/** The end of a run: a done run that did no work, as `idle` says, deletes its own status message from the card's thread and says nothing more, so the card's first message keeps the status of the run that worked; else the plan marked over when the run showed one, the session status of a Slack thread, and the end note of `noteLayout` at each place of `tell`. A run that already said that its pull request is ready for people, when its review wait started, does not say it again. A run with no card that ends done sends no note, except the first run of a schedule that works again; a done run that showed no plan sends none either, except a note that asks for a decision, is about a pull request or says what the run waits on. A done run with no summary sends none: the plan's first line already says that it is done, and a note with nothing to tell is noise. A decision that a step of the run, or another run, already asked in the card's thread while the run worked is not asked again. A quiet run that ends done, and a run that a stage of another run started whatever its end except a decision, marks its own plan over and removes the moving line under the card's thread, but sends no note, sets no session status and keeps no promise, since the top run of the chain says the end; one that a person's words in Slack started ends with `relayedEnd` alone. A note whose run is in no thread goes in the thread of its card. A failed run of a schedule whose run just before it failed the same way sends nothing. An event whose outcome `endNote` cannot read still ends the plan and the session, and its note says only that the run is over. A run that a person stopped and that showed no status message of its own ends with no note: the person knows, and a stop of many such runs at once must not fill the channel. A late note goes only in a thread that the card already has. */
|
|
407
599
|
async function ended(ctx, event, settings, first) {
|
|
408
600
|
const note = endNote(event, style, settings.console_url);
|
|
409
601
|
if (note === undefined)
|
|
@@ -416,18 +608,16 @@ async function ended(ctx, event, settings, first) {
|
|
|
416
608
|
if (!said.ok || said.value)
|
|
417
609
|
return said.ok ? ok(undefined) : said;
|
|
418
610
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
if (origin !== undefined)
|
|
422
|
-
await threadStatus(ctx, origin.channel, origin.thread, "");
|
|
611
|
+
const quiet = first.part ? note?.what !== "decision" : first.quiet && note?.what === "done";
|
|
612
|
+
if (quiet && first.relayed !== undefined)
|
|
423
613
|
return ok(undefined);
|
|
424
|
-
}
|
|
425
614
|
const home = await homeOf(ctx, event, settings);
|
|
426
615
|
if (!home.ok)
|
|
427
616
|
return home;
|
|
428
617
|
let planned = false;
|
|
429
618
|
let found;
|
|
430
619
|
let promised;
|
|
620
|
+
let chained = false;
|
|
431
621
|
if (home.value !== undefined) {
|
|
432
622
|
const thread = await threadOf(ctx, event, home.value);
|
|
433
623
|
if (!thread.ok)
|
|
@@ -438,36 +628,63 @@ async function ended(ctx, event, settings, first) {
|
|
|
438
628
|
const all = await listed(ctx, at.channel, found);
|
|
439
629
|
if (!all.ok)
|
|
440
630
|
return all;
|
|
441
|
-
|
|
631
|
+
if (word === "done" && (await idle(ctx, event.run))) {
|
|
632
|
+
const own = newestMarked(all.value, "edgar_plan", (p) => p["run"] === event.run);
|
|
633
|
+
return own === undefined ? ok(undefined) : await remove(ctx, at.channel, own.ts);
|
|
634
|
+
}
|
|
635
|
+
const over = (plan) => ({ ...plan, ended: note?.what ?? word });
|
|
442
636
|
const changed = await replanIn(ctx, event, at, settings, over, all.value);
|
|
443
637
|
if (!changed.ok)
|
|
444
638
|
return changed;
|
|
445
639
|
planned = changed.value !== undefined;
|
|
446
640
|
await closeQuestions(ctx, at, event.run, note?.what === "stopped", all.value);
|
|
447
|
-
const marker = word === "done"
|
|
641
|
+
const marker = word === "done" && !quiet
|
|
448
642
|
? newestMarked(all.value, "edgar_post_to", (p) => p["run"] !== event.run)
|
|
449
643
|
: undefined;
|
|
450
644
|
if (marker !== undefined)
|
|
451
645
|
promised = { at, marker };
|
|
646
|
+
if (quiet)
|
|
647
|
+
await threadStatus(ctx, at.channel, found, "");
|
|
648
|
+
chained = note?.what === "decision" && askedInChain(all.value, event, note.facts.summary);
|
|
452
649
|
}
|
|
453
|
-
if (home.value.origin !== undefined) {
|
|
650
|
+
if (home.value.origin !== undefined && !quiet) {
|
|
454
651
|
const close = settings.close_on_done && word === "done";
|
|
455
652
|
await status(ctx, home.value.origin, close ? "closed" : "active");
|
|
456
653
|
}
|
|
457
654
|
}
|
|
655
|
+
if (quiet)
|
|
656
|
+
return ok(undefined);
|
|
458
657
|
if (promised !== undefined) {
|
|
459
658
|
const layout = noteLayout(event.run, note, undefined);
|
|
460
659
|
const kept = await keepPromise(ctx, promised.at, promised.marker, layout);
|
|
461
660
|
if (!kept.ok)
|
|
462
661
|
return kept;
|
|
463
662
|
}
|
|
663
|
+
if (note?.what === "stopped" && !planned)
|
|
664
|
+
return ok(undefined);
|
|
665
|
+
const view = note === undefined
|
|
666
|
+
? {}
|
|
667
|
+
: await viewOf(ctx, event, first, settings, note.what, note.facts.pulls);
|
|
668
|
+
if (home.value !== undefined && found !== undefined && note !== undefined)
|
|
669
|
+
await restatus(ctx, event, home.value, first.card, view);
|
|
670
|
+
if (chained)
|
|
671
|
+
return ok(undefined);
|
|
464
672
|
const told = note !== undefined && asks.has(note.what);
|
|
465
673
|
const bare = note?.what === "done" && note.facts.summary === undefined;
|
|
466
674
|
if (word === "done" && !told && (!planned || bare))
|
|
467
675
|
return ok(undefined);
|
|
468
|
-
|
|
676
|
+
if (found === undefined && late(event)) {
|
|
677
|
+
ctx.log(`run ${event.run}: the end note came late and the card has no thread here, so it opens none`);
|
|
678
|
+
return ok(undefined);
|
|
679
|
+
}
|
|
680
|
+
const asking = note?.what === "decision" || note?.what === "handed";
|
|
681
|
+
const named = endActors.safeParse(event.payload).data?.end.who_can_act ?? [];
|
|
682
|
+
const tagged = asking
|
|
683
|
+
? await whoCanAct(ctx, event.run, "the end note", named, first, settings)
|
|
684
|
+
: [];
|
|
685
|
+
return await tell(ctx, event, settings, home.value, found, note, view, tagged);
|
|
469
686
|
}
|
|
470
|
-
/** What each event the hook takes before the end does in the card's thread; a run with no card has no thread and no plan. The start of a run
|
|
687
|
+
/** What each event the hook takes before the end does in the card's thread; a run with no card has no thread and no plan. The start of a run posts nothing in a channel: a run joins the card's thread from its first stage when the card already has one; else the thread and its one status message open when the run starts its second stage, or when a person must read something, as `opening` says, so a run that ends after one stage with nothing to tell leaves no line. A run of the card that starts while another still works takes the status message over once that run is over. A stage is in the plan from the moment it starts, so a stage that the run skips or never reaches is never shown. When the stage `REVIEW_WAIT` of a top run starts to wait, the note that the pull request is ready for people is posted, at the moment they can act and not at the end of the run. A done stage whose output has a `question` posts it once, as `question` says. A stage that starts also sets the moving line under the thread to its phrase of `stageStatus`, which Slack removes at EDGAR's next message there. A quiet run shows its progress the same way, so the work that a person's reply or another run starts is never silence. */
|
|
471
688
|
const steps = {
|
|
472
689
|
run_created: async (event, ctx, place) => {
|
|
473
690
|
if (place.origin !== undefined)
|
|
@@ -482,10 +699,10 @@ const steps = {
|
|
|
482
699
|
const at = { ...place, thread };
|
|
483
700
|
const working = (p) => withTask(p, stage, "working");
|
|
484
701
|
const changed = await replan(ctx, event, at, settings, working, first);
|
|
485
|
-
if (place.origin !== undefined)
|
|
702
|
+
if (place.origin !== undefined && !first.quiet)
|
|
486
703
|
await status(ctx, place.origin, "processing");
|
|
487
704
|
if (changed.ok)
|
|
488
|
-
await threadStatus(ctx, place.channel, thread,
|
|
705
|
+
await threadStatus(ctx, place.channel, thread, stageStatus(stage, settings));
|
|
489
706
|
return changed;
|
|
490
707
|
},
|
|
491
708
|
attempt_outcome: async (event, ctx, place, settings, first) => {
|
|
@@ -496,7 +713,7 @@ const steps = {
|
|
|
496
713
|
return ok(undefined);
|
|
497
714
|
const word = record.data.outcome.outcome;
|
|
498
715
|
const at = { ...place, thread };
|
|
499
|
-
const changed = await replan(ctx, event, at, settings, (p) => afterOutcome(p, stage, event.payload, word), first);
|
|
716
|
+
const changed = await replan(ctx, event, at, settings, (p) => afterOutcome(p, stage, event.payload, word), word === "skipped" ? undefined : first);
|
|
500
717
|
if (!changed.ok)
|
|
501
718
|
return changed;
|
|
502
719
|
const asked = await question(ctx, event, stage, at, settings, first);
|
|
@@ -506,12 +723,15 @@ const steps = {
|
|
|
506
723
|
if (!promised.ok)
|
|
507
724
|
return promised;
|
|
508
725
|
const plan = changed.value;
|
|
509
|
-
if (
|
|
726
|
+
if (first.part ||
|
|
727
|
+
stage !== REVIEW_WAIT ||
|
|
510
728
|
word !== "waiting" ||
|
|
511
729
|
plan === undefined ||
|
|
512
730
|
plan.pulls.length === 0)
|
|
513
731
|
return changed;
|
|
514
|
-
|
|
732
|
+
const view = await viewOf(ctx, event, first, settings, "opened", plan.pulls);
|
|
733
|
+
await restatus(ctx, event, place, first.card, view);
|
|
734
|
+
return await tell(ctx, event, settings, place, thread, readyNote(event.run, first, plan, settings), view);
|
|
515
735
|
},
|
|
516
736
|
approval_requested: async (event, ctx, place, settings, first) => await ask(ctx, event, place, settings, first),
|
|
517
737
|
waiting_for_slot: async (event, ctx, place, settings, first) => {
|
|
@@ -532,26 +752,68 @@ async function take(event, ctx, settings) {
|
|
|
532
752
|
const first = await beganOf(ctx, event);
|
|
533
753
|
if (event.kind === "run_outcome") {
|
|
534
754
|
began.delete(event.run);
|
|
755
|
+
held.delete(event.run);
|
|
535
756
|
return await ended(ctx, event, settings, first);
|
|
536
757
|
}
|
|
537
|
-
if (!isTaken(event.kind))
|
|
758
|
+
if (!isTaken(event.kind) || (progressOnly.has(event.kind) && late(event)))
|
|
538
759
|
return ok(undefined);
|
|
539
760
|
if (first.relayed !== undefined && event.kind === "attempt_outcome")
|
|
540
|
-
return await relayedOutcome(ctx, event, first, first.relayed);
|
|
541
|
-
if (first.
|
|
761
|
+
return await relayedOutcome(ctx, event, first, first.relayed, settings);
|
|
762
|
+
if (first.relayed !== undefined && event.kind === "attempt_started")
|
|
763
|
+
return await relayedStart(ctx, event, first.relayed, settings);
|
|
764
|
+
const relayedOnly = first.relayed !== undefined && event.kind !== "approval_requested";
|
|
765
|
+
if (first.quiet && (event.kind === "run_created" || relayedOnly))
|
|
542
766
|
return ok(undefined);
|
|
543
767
|
const home = await homeOf(ctx, event, settings);
|
|
544
768
|
if (!home.ok || home.value === undefined)
|
|
545
769
|
return home;
|
|
546
770
|
const thread = event.kind === "run_created"
|
|
547
|
-
?
|
|
771
|
+
? ok(home.value.origin?.thread)
|
|
548
772
|
: event.kind === "approval_requested"
|
|
549
773
|
? await cardThread(ctx, event, home.value, first.card)
|
|
550
774
|
: await threadOf(ctx, event, home.value);
|
|
551
775
|
if (!thread.ok)
|
|
552
776
|
return thread;
|
|
777
|
+
const word = outcomeRecord.safeParse(event.payload).data?.outcome.outcome;
|
|
778
|
+
if (thread.value === undefined && event.kind === "attempt_outcome" && word === "skipped") {
|
|
779
|
+
const kept = held.get(event.run);
|
|
780
|
+
if (kept !== undefined && event.stage !== null)
|
|
781
|
+
held.set(event.run, withoutTask(kept, event.stage));
|
|
782
|
+
return ok(undefined);
|
|
783
|
+
}
|
|
784
|
+
const opened = thread.value === undefined &&
|
|
785
|
+
first.card !== undefined &&
|
|
786
|
+
event.kind !== "run_created" &&
|
|
787
|
+
!late(event)
|
|
788
|
+
? await opening(ctx, event, home.value.channel, first.card, async () => await viewOf(ctx, event, first, settings, "working"))
|
|
789
|
+
: thread;
|
|
790
|
+
if (!opened.ok || (opened.value === undefined && event.kind !== "run_created"))
|
|
791
|
+
return opened;
|
|
553
792
|
const step = steps[event.kind];
|
|
554
|
-
return await step(event, ctx, { ...home.value, thread:
|
|
793
|
+
return await step(event, ctx, { ...home.value, thread: opened.value }, settings, first);
|
|
794
|
+
}
|
|
795
|
+
/** The card's thread for a step of a run that its thread does not list yet: the card's thread when the card already has one, which `joinedRoot` gives the run, so the run shows from its first stage; else none while `held` keeps the run's plan, and then the thread that `rootOf` opens and gives the run. With no thread yet, the plan is held until the run starts its second stage or a done stage asks a person a question, so a run that ends after one stage with nothing to tell posts no new message at the top of the channel. */
|
|
796
|
+
async function opening(ctx, event, channel, card, view) {
|
|
797
|
+
const base = held.get(event.run) ?? { run: event.run, tasks: [], pulls: [] };
|
|
798
|
+
const { stage } = event;
|
|
799
|
+
const word = outcomeRecord.safeParse(event.payload).data?.outcome.outcome;
|
|
800
|
+
const wait = waitingForSlot.safeParse(event.payload).data;
|
|
801
|
+
const next = event.kind === "attempt_started" && stage !== null
|
|
802
|
+
? withTask(base, stage, "working")
|
|
803
|
+
: event.kind === "attempt_outcome" && stage !== null && word !== undefined
|
|
804
|
+
? afterOutcome(base, stage, event.payload, word)
|
|
805
|
+
: event.kind === "waiting_for_slot" && wait !== undefined
|
|
806
|
+
? { ...base, queued: wait }
|
|
807
|
+
: base;
|
|
808
|
+
const asked = event.kind === "attempt_outcome" && questionOutcome.safeParse(event.payload).success;
|
|
809
|
+
if (!asked && next.tasks.length < 2) {
|
|
810
|
+
const joined = await joinedRoot(ctx, channel, card, event.run, await view());
|
|
811
|
+
if (!joined.ok || joined.value !== undefined)
|
|
812
|
+
return joined;
|
|
813
|
+
held.set(event.run, next);
|
|
814
|
+
return ok(undefined);
|
|
815
|
+
}
|
|
816
|
+
return await rootOf(ctx, channel, card, event.run, await view());
|
|
555
817
|
}
|
|
556
818
|
async function deliver(event, ctx) {
|
|
557
819
|
const settings = settingsOf(ctx.config);
|