@kana-ai/edgar-ext-slack 0.1.3 → 0.1.5

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.
@@ -2,6 +2,7 @@ import type { Origin } from "@kana-ai/edgar-contracts/rpc";
2
2
  import { type Failure, type NoteCard, type Result, type RunEvent } from "@kana-ai/edgar-sdk";
3
3
  import { type SlackCtx } from "#api/client";
4
4
  import { type Config } from "#schemas/config";
5
+ import { type RootView } from "#schemas/events";
5
6
  /** The conversation of a run's messages: the Slack thread its card came from, else `[extensions.slack] channel`, else the DM of the first `dm` entry; `direct` marks the DM. */
6
7
  export interface Home {
7
8
  readonly channel: string;
@@ -18,18 +19,22 @@ export interface Spot {
18
19
  readonly thread: string | undefined;
19
20
  }
20
21
  /** The Slack user id of a card's assignee, from the first of these that names one: the `people` entry of that name, the operator of that name, the assignee itself when it is an email. An entry that is an email is the user that `users.lookupByEmail` finds, kept for the life of the process. None when nothing names one. An email that finds no user, or an answer that is not a user id, leaves the name as it is and writes one line to the log, which names `message`, so the message is still posted and never holds a mention that Slack cannot read. */
21
- export declare function mentioned(ctx: SlackCtx, run: string, message: "the end note" | "the approval question" | "the question", assignee: string, settings: Config): Promise<string | undefined>;
22
+ export declare function mentioned(ctx: SlackCtx, run: string, message: "the end note" | "the approval question" | "the question" | "the card's first message", assignee: string, settings: Config): Promise<string | undefined>;
23
+ /** Who stopped a run, as its end note writes them: a mention of the Slack person that the move's `<person>@slack` names by Slack id, by `people` or as an operator, else the name as the engine recorded it. */
24
+ export declare function stopperOf(ctx: SlackCtx, run: string, by: string, settings: Config): Promise<string>;
25
+ /** The people of `entries` as a message tags them, after the mentions of `known`, each once and at most `MAX_TAGS`: a mention of the Slack id that the engine found for them in `[people]`, else of the user that `mentioned` finds for their email or name, else their name or login as text with one log line, so nobody is dropped. An email is never written. */
26
+ export declare function taggedOf(ctx: SlackCtx, run: string, message: "the end note" | "the question", entries: readonly Readonly<Record<string, string>>[], settings: Config, known?: readonly string[]): Promise<readonly string[]>;
22
27
  /** The DM conversation of a `dm` entry, which a card's thread needs to be found again. */
23
28
  export declare function directOf(ctx: SlackCtx, entry: string): Promise<Result<string, Failure>>;
24
29
  /** The home of a run, or none when the card did not come from Slack and neither `channel` nor `dm` is set. */
25
30
  export declare function homeOf(ctx: SlackCtx, event: RunEvent, settings: Config): Promise<Result<Home | undefined, Failure>>;
26
- /** The first message of the card's thread in `channel`, found by the card's link, posted when the card has none, and given `run` when it does not list it; answers its `ts`. */
27
- export declare function rootOf(ctx: SlackCtx, channel: string, card: NoteCard, run: string): Promise<Result<string, Failure>>;
31
+ /** The first message of the card's thread in `channel`, found by the card id or link among the channel's newest 1,000 messages, posted when the card has none there, given `run` and the card's newest link and title when it lacks them, and changed in place to show each part of `view` over what it showed; answers its `ts`. */
32
+ export declare function rootOf(ctx: SlackCtx, channel: string, card: NoteCard, run: string, view?: RootView): Promise<Result<string, Failure>>;
33
+ /** `rootOf` that never posts: the card's first message in `channel`, given `run` as `rootOf` gives it, or none when the card has no thread there. */
34
+ export declare function joinedRoot(ctx: SlackCtx, channel: string, card: NoteCard, run: string, view?: RootView): Promise<Result<string | undefined, Failure>>;
28
35
  /** The card's thread of a run: the Slack thread its card came from, else the thread whose first message lists the run. */
29
36
  export declare function threadOf(ctx: SlackCtx, event: RunEvent, home: Home): Promise<Result<string | undefined, Failure>>;
30
- /** The thread a new run's messages go in: the Slack thread its card came from, else the card's thread, opened by the run's first message about the card; a run with no card has none. */
31
- export declare function openThread(ctx: SlackCtx, event: RunEvent, home: Home): Promise<Result<string | undefined, Failure>>;
32
37
  /** The thread of a message that a person must see, for a run that can have no plan: the thread of `threadOf`, else the thread of `card`, which gets the run, so a run that a comment started still writes under its card. */
33
38
  export declare function cardThread(ctx: SlackCtx, event: RunEvent, home: Home, card: NoteCard | undefined): Promise<Result<string | undefined, Failure>>;
34
39
  /** The card's thread in `channel`, opened by the card's line when the conversation has none, or none when `places` already holds that conversation. */
35
- export declare function oneMore(ctx: SlackCtx, channel: string, card: NoteCard | undefined, run: string, places: readonly Spot[]): Promise<Result<Spot | undefined, Failure>>;
40
+ export declare function oneMore(ctx: SlackCtx, channel: string, card: NoteCard | undefined, run: string, places: readonly Spot[], view: RootView): Promise<Result<Spot | undefined, Failure>>;
@@ -1,9 +1,9 @@
1
- import { cardLine, err, noteCard, ok, permanentFailure, runCard, } from "@kana-ai/edgar-sdk";
1
+ import { err, ok, permanentFailure, } from "@kana-ai/edgar-sdk";
2
2
  import { slack } from "#api/client";
3
- import { style } from "#outbound/layout";
4
- import { findMarked, mark, post, update } from "#outbound/messages";
3
+ import { rootLayout, style } from "#outbound/layout";
4
+ import { findInChannel, mark, post, update } from "#outbound/messages";
5
5
  import { email, userId } from "#schemas/config";
6
- import { cardRoot, slackOrigin } from "#schemas/events";
6
+ import { cardRoot, rootView, slackOrigin } from "#schemas/events";
7
7
  import { opened, userInfo } from "#schemas/slack";
8
8
  /** The runs a card's first message keeps; an older run of the card finds no thread. */
9
9
  const MAX_RUNS = 20;
@@ -36,9 +36,46 @@ export async function mentioned(ctx, run, message, assignee, settings) {
36
36
  const user = await recipient(ctx, entry);
37
37
  if (user.ok && userId.safeParse(user.value).success)
38
38
  return user.value;
39
- ctx.log(`run ${run}: ${message} names ${assignee} with no mention, because users.lookupByEmail gave no user for ${entry} (${user.ok ? "not a user id" : user.error.kind})`);
39
+ ctx.log(`run ${run}: ${message} greets nobody for ${assignee}, because users.lookupByEmail gave no user for ${entry} (${user.ok ? "not a user id" : user.error.kind})`);
40
40
  return undefined;
41
41
  }
42
+ /** Who stopped a run, as its end note writes them: a mention of the Slack person that the move's `<person>@slack` names by Slack id, by `people` or as an operator, else the name as the engine recorded it. */
43
+ export async function stopperOf(ctx, run, by, settings) {
44
+ const person = by.endsWith("@slack") ? by.slice(0, -"@slack".length) : undefined;
45
+ if (person === undefined)
46
+ return style.text(by);
47
+ if (userId.safeParse(person).success)
48
+ return `<@${person}>`;
49
+ const user = await mentioned(ctx, run, "the end note", person, settings);
50
+ return user === undefined ? style.text(person) : `<@${user}>`;
51
+ }
52
+ /** The most people one message tags. */
53
+ const MAX_TAGS = 5;
54
+ /** The people of `entries` as a message tags them, after the mentions of `known`, each once and at most `MAX_TAGS`: a mention of the Slack id that the engine found for them in `[people]`, else of the user that `mentioned` finds for their email or name, else their name or login as text with one log line, so nobody is dropped. An email is never written. */
55
+ export async function taggedOf(ctx, run, message, entries, settings, known = []) {
56
+ const out = [];
57
+ for (const entry of entries) {
58
+ if (out.length >= MAX_TAGS)
59
+ break;
60
+ const tag = await tagOf(ctx, run, message, entry, settings);
61
+ if (tag !== undefined && !out.includes(tag))
62
+ out.push(tag);
63
+ }
64
+ return [...out, ...known.filter((tag) => !out.includes(tag))].slice(0, MAX_TAGS);
65
+ }
66
+ async function tagOf(ctx, run, message, entry, settings) {
67
+ const own = entry["slack"];
68
+ if (own !== undefined && userId.safeParse(own).success)
69
+ return `<@${own}>`;
70
+ for (const given of [entry["email"], entry["name"]]) {
71
+ const user = given === undefined ? undefined : await mentioned(ctx, run, message, given, settings);
72
+ if (user !== undefined)
73
+ return `<@${user}>`;
74
+ }
75
+ const text = entry["name"] ?? entry["github"];
76
+ ctx.log(`run ${run}: ${message} tags nobody for ${text ?? "a person"}, as no person of [people] with a Slack id has that id`);
77
+ return text === undefined ? undefined : style.text(text);
78
+ }
42
79
  /** The DM conversation of a `dm` entry, which a card's thread needs to be found again. */
43
80
  export async function directOf(ctx, entry) {
44
81
  const user = await recipient(ctx, entry);
@@ -66,21 +103,55 @@ export async function homeOf(ctx, event, settings) {
66
103
  const direct = await directOf(ctx, first);
67
104
  return direct.ok ? ok({ channel: direct.value, origin: undefined, direct: true }) : direct;
68
105
  }
69
- /** The first message of the card's thread in `channel`, found by the card's link, posted when the card has none, and given `run` when it does not list it; answers its `ts`. */
70
- export async function rootOf(ctx, channel, card, run) {
71
- const found = await findMarked(ctx, channel, undefined, "edgar_card", (p) => p["card"] === card.url);
72
- if (!found.ok)
73
- return found;
74
- const kept = cardRoot.safeParse(found.value?.metadata?.event_payload);
106
+ /** The part of a card's link up to its id: a tracker such as Linear puts the title after the id, so the link changes when the card is retitled. */
107
+ function upTo(url, id) {
108
+ const at = url.indexOf(`/${id}/`);
109
+ return at === -1 ? url : url.slice(0, at + id.length + 1);
110
+ }
111
+ /** Whether the metadata of a card's first message names `card`: by the card id, else, for a message that keeps no id, by the link up to the card id. */
112
+ function sameCard(payload, card) {
113
+ const { id, card: link } = payload;
114
+ if (typeof id === "string" && card.id !== undefined)
115
+ return id === card.id;
116
+ if (typeof link !== "string")
117
+ return false;
118
+ return (link === card.url || (card.id !== undefined && upTo(link, card.id) === upTo(card.url, card.id)));
119
+ }
120
+ /** The first message of the card's thread in `channel`, found by the card id or link among the channel's newest 1,000 messages, posted when the card has none there, given `run` and the card's newest link and title when it lacks them, and changed in place to show each part of `view` over what it showed; answers its `ts`. */
121
+ export async function rootOf(ctx, channel, card, run, view = {}) {
122
+ const found = await findInChannel(ctx, channel, "edgar_card", (p) => sameCard(p, card));
123
+ return found.ok ? await keepRoot(ctx, channel, card, run, view, found.value) : found;
124
+ }
125
+ /** `rootOf` that never posts: the card's first message in `channel`, given `run` as `rootOf` gives it, or none when the card has no thread there. */
126
+ export async function joinedRoot(ctx, channel, card, run, view = {}) {
127
+ const found = await findInChannel(ctx, channel, "edgar_card", (p) => sameCard(p, card));
128
+ if (!found.ok || found.value === undefined)
129
+ return found.ok ? ok(undefined) : found;
130
+ return await keepRoot(ctx, channel, card, run, view, found.value);
131
+ }
132
+ /** The card's first message `found` in `channel` written as `rootOf` says, or posted when there is none. */
133
+ async function keepRoot(ctx, channel, card, run, view, found) {
134
+ const kept = cardRoot.safeParse(found?.metadata?.event_payload);
75
135
  const runs = kept.success ? kept.data.runs : [];
76
- const ts = found.value?.ts;
77
- if (ts !== undefined && runs.includes(run))
136
+ const before = rootView.safeParse(kept.data?.["view"]).data ?? {};
137
+ const shown = { ...before, ...view };
138
+ const ts = found?.ts;
139
+ const same = JSON.stringify(shown) === JSON.stringify(before);
140
+ const current = kept.data?.card === card.url && (kept.data.title ?? card.title) === card.title;
141
+ if (ts !== undefined && runs.includes(run) && same && current)
78
142
  return ok(ts);
79
- const metadata = mark("edgar_card", { card: card.url, runs: [...runs, run].slice(-MAX_RUNS) });
80
- const text = cardLine(card, style);
143
+ const listed = runs.includes(run) ? runs : [...runs, run].slice(-MAX_RUNS);
144
+ const metadata = mark("edgar_card", {
145
+ card: card.url,
146
+ ...(card.id === undefined ? {} : { id: card.id }),
147
+ title: card.title,
148
+ runs: listed,
149
+ ...(Object.keys(shown).length === 0 ? {} : { view: shown }),
150
+ });
151
+ const { text, blocks } = rootLayout(card, shown);
81
152
  const written = ts === undefined
82
- ? await post(ctx, channel, undefined, text, { metadata })
83
- : await update(ctx, channel, ts, text, { metadata });
153
+ ? await post(ctx, channel, undefined, text, { metadata, blocks })
154
+ : await update(ctx, channel, ts, text, { metadata, blocks });
84
155
  return written.ok ? ok(written.value.ts) : written;
85
156
  }
86
157
  /** The card's thread of a run: the Slack thread its card came from, else the thread whose first message lists the run. */
@@ -88,16 +159,9 @@ export async function threadOf(ctx, event, home) {
88
159
  if (home.origin !== undefined)
89
160
  return ok(home.origin.thread);
90
161
  const lists = (p) => cardRoot.safeParse(p).data?.runs.includes(event.run) === true;
91
- const found = await findMarked(ctx, home.channel, undefined, "edgar_card", lists);
162
+ const found = await findInChannel(ctx, home.channel, "edgar_card", lists);
92
163
  return found.ok ? ok(found.value?.ts) : found;
93
164
  }
94
- /** The thread a new run's messages go in: the Slack thread its card came from, else the card's thread, opened by the run's first message about the card; a run with no card has none. */
95
- export async function openThread(ctx, event, home) {
96
- if (home.origin !== undefined)
97
- return ok(home.origin.thread);
98
- const card = noteCard(runCard.safeParse(event.payload).data);
99
- return card === undefined ? ok(undefined) : await rootOf(ctx, home.channel, card, event.run);
100
- }
101
165
  /** The thread of a message that a person must see, for a run that can have no plan: the thread of `threadOf`, else the thread of `card`, which gets the run, so a run that a comment started still writes under its card. */
102
166
  export async function cardThread(ctx, event, home, card) {
103
167
  const thread = await threadOf(ctx, event, home);
@@ -106,9 +170,9 @@ export async function cardThread(ctx, event, home, card) {
106
170
  return await rootOf(ctx, home.channel, card, event.run);
107
171
  }
108
172
  /** The card's thread in `channel`, opened by the card's line when the conversation has none, or none when `places` already holds that conversation. */
109
- export async function oneMore(ctx, channel, card, run, places) {
173
+ export async function oneMore(ctx, channel, card, run, places, view) {
110
174
  if (places.some((place) => place.channel === channel))
111
175
  return ok(undefined);
112
- const thread = card === undefined ? ok(undefined) : await rootOf(ctx, channel, card, run);
176
+ const thread = card === undefined ? ok(undefined) : await rootOf(ctx, channel, card, run, view);
113
177
  return thread.ok ? ok({ channel, thread: thread.value }) : thread;
114
178
  }