@skillcdn/console 0.6.2 → 0.6.3

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.
Files changed (179) hide show
  1. package/README.md +13 -6
  2. package/SKILLCDN.md +3 -2
  3. package/dist/agent-page.d.ts +2 -0
  4. package/dist/agent-page.d.ts.map +1 -1
  5. package/dist/agent-page.js +40 -3
  6. package/dist/agent-page.js.map +1 -1
  7. package/dist/agent-state.d.ts +9 -0
  8. package/dist/agent-state.d.ts.map +1 -0
  9. package/dist/agent-state.js +28 -0
  10. package/dist/agent-state.js.map +1 -0
  11. package/dist/api.d.ts +4 -3
  12. package/dist/api.d.ts.map +1 -1
  13. package/dist/api.js +4 -3
  14. package/dist/api.js.map +1 -1
  15. package/dist/cli/cli.d.ts +2 -0
  16. package/dist/cli/cli.d.ts.map +1 -1
  17. package/dist/cli/cli.js +304 -130
  18. package/dist/cli/cli.js.map +1 -1
  19. package/dist/cli/format.d.ts +13 -4
  20. package/dist/cli/format.d.ts.map +1 -1
  21. package/dist/cli/format.js +43 -9
  22. package/dist/cli/format.js.map +1 -1
  23. package/dist/cli/help.d.ts +82 -9
  24. package/dist/cli/help.d.ts.map +1 -1
  25. package/dist/cli/help.js +780 -318
  26. package/dist/cli/help.js.map +1 -1
  27. package/dist/cli/pack.d.ts +2 -2
  28. package/dist/cli/pack.d.ts.map +1 -1
  29. package/dist/cli/pack.js +6 -4
  30. package/dist/cli/pack.js.map +1 -1
  31. package/dist/cli/runner-files.d.ts +16 -0
  32. package/dist/cli/runner-files.d.ts.map +1 -0
  33. package/dist/cli/runner-files.js +110 -0
  34. package/dist/cli/runner-files.js.map +1 -0
  35. package/dist/cli/runner-host.d.ts +7 -0
  36. package/dist/cli/runner-host.d.ts.map +1 -1
  37. package/dist/cli/runner-host.js +40 -2
  38. package/dist/cli/runner-host.js.map +1 -1
  39. package/dist/client.d.ts +16 -1
  40. package/dist/client.d.ts.map +1 -1
  41. package/dist/client.js +6 -1
  42. package/dist/client.js.map +1 -1
  43. package/dist/components/agent-page.d.ts +28 -3
  44. package/dist/components/agent-page.d.ts.map +1 -1
  45. package/dist/components/agent-page.js +48 -3
  46. package/dist/components/agent-page.js.map +1 -1
  47. package/dist/components/agents.d.ts +3 -2
  48. package/dist/components/agents.d.ts.map +1 -1
  49. package/dist/components/agents.js +9 -3
  50. package/dist/components/agents.js.map +1 -1
  51. package/dist/console.d.ts.map +1 -1
  52. package/dist/console.js +1 -1
  53. package/dist/console.js.map +1 -1
  54. package/dist/feed-words.d.ts.map +1 -1
  55. package/dist/feed-words.js +12 -1
  56. package/dist/feed-words.js.map +1 -1
  57. package/dist/i18n/en.d.ts +45 -1
  58. package/dist/i18n/en.d.ts.map +1 -1
  59. package/dist/i18n/en.js +36 -1
  60. package/dist/i18n/en.js.map +1 -1
  61. package/dist/i18n/ko.d.ts.map +1 -1
  62. package/dist/i18n/ko.js +28 -0
  63. package/dist/i18n/ko.js.map +1 -1
  64. package/dist/limits.d.ts +10 -0
  65. package/dist/limits.d.ts.map +1 -1
  66. package/dist/limits.js +10 -0
  67. package/dist/limits.js.map +1 -1
  68. package/dist/plugin/context.d.ts +39 -7
  69. package/dist/plugin/context.d.ts.map +1 -1
  70. package/dist/runner/acp.d.ts.map +1 -1
  71. package/dist/runner/acp.js +9 -3
  72. package/dist/runner/acp.js.map +1 -1
  73. package/dist/runner/claude.d.ts.map +1 -1
  74. package/dist/runner/claude.js +5 -1
  75. package/dist/runner/claude.js.map +1 -1
  76. package/dist/runner/codex.d.ts.map +1 -1
  77. package/dist/runner/codex.js +14 -2
  78. package/dist/runner/codex.js.map +1 -1
  79. package/dist/runner/config.d.ts +18 -0
  80. package/dist/runner/config.d.ts.map +1 -1
  81. package/dist/runner/config.js +77 -1
  82. package/dist/runner/config.js.map +1 -1
  83. package/dist/runner/desk.d.ts +7 -0
  84. package/dist/runner/desk.d.ts.map +1 -1
  85. package/dist/runner/desk.js +9 -0
  86. package/dist/runner/desk.js.map +1 -1
  87. package/dist/runner/message.d.ts +16 -22
  88. package/dist/runner/message.d.ts.map +1 -1
  89. package/dist/runner/message.js +72 -120
  90. package/dist/runner/message.js.map +1 -1
  91. package/dist/runner/runner.d.ts +25 -1
  92. package/dist/runner/runner.d.ts.map +1 -1
  93. package/dist/runner/runner.js +404 -19
  94. package/dist/runner/runner.js.map +1 -1
  95. package/dist/runner/slack-status.d.ts +19 -0
  96. package/dist/runner/slack-status.d.ts.map +1 -0
  97. package/dist/runner/slack-status.js +332 -0
  98. package/dist/runner/slack-status.js.map +1 -0
  99. package/dist/runner/slack.d.ts.map +1 -1
  100. package/dist/runner/slack.js +245 -57
  101. package/dist/runner/slack.js.map +1 -1
  102. package/dist/runner/types.d.ts +109 -15
  103. package/dist/runner/types.d.ts.map +1 -1
  104. package/dist/runner/types.js.map +1 -1
  105. package/dist/runner/usage.d.ts +6 -1
  106. package/dist/runner/usage.d.ts.map +1 -1
  107. package/dist/runner/usage.js +9 -2
  108. package/dist/runner/usage.js.map +1 -1
  109. package/dist/runner.d.ts +2 -2
  110. package/dist/runner.d.ts.map +1 -1
  111. package/dist/runner.js.map +1 -1
  112. package/dist/schemas.d.ts +160 -1
  113. package/dist/schemas.d.ts.map +1 -1
  114. package/dist/schemas.js +82 -5
  115. package/dist/schemas.js.map +1 -1
  116. package/dist/vocabulary.d.ts +7 -0
  117. package/dist/vocabulary.d.ts.map +1 -1
  118. package/dist/vocabulary.js +6 -0
  119. package/dist/vocabulary.js.map +1 -1
  120. package/dist/wake-text.d.ts +43 -0
  121. package/dist/wake-text.d.ts.map +1 -0
  122. package/dist/wake-text.js +115 -0
  123. package/dist/wake-text.js.map +1 -0
  124. package/docs/specs/cli.md +19 -11
  125. package/docs/specs/plugins.md +5 -5
  126. package/docs/specs/rest.md +10 -3
  127. package/docs/specs/runner.md +96 -20
  128. package/docs/start.md +26 -5
  129. package/package.json +3 -3
  130. package/skills/building-a-plugin/SKILL.md +2 -2
  131. package/skills/deploying-a-console/SKILL.md +3 -3
  132. package/skills/deploying-a-console/references/access.md +15 -1
  133. package/skills/deploying-a-console/references/resources.md +2 -2
  134. package/skills/deploying-a-console/scripts/github-app.mjs +593 -0
  135. package/skills/running-an-agent/SKILL.md +13 -10
  136. package/skills/running-an-agent/references/preparing-the-machine.md +1 -1
  137. package/skills/running-an-agent/references/slack-app.md +25 -8
  138. package/skills/running-an-agent/references/the-account-and-the-invitation.md +1 -1
  139. package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +1 -1
  140. package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +1 -1
  141. package/skills/running-an-agent/references/your-first-task.md +7 -2
  142. package/skills/trying-a-console/SKILL.md +136 -0
  143. package/skills/trying-a-console/references/when-it-does-not-go.md +30 -0
  144. package/skills/working-the-board/SKILL.md +3 -3
  145. package/skills/working-the-board/references/the-pack.md +3 -3
  146. package/skills/working-the-board/references/what-they-were-told.md +1 -1
  147. package/src/agent-page.tsx +92 -1
  148. package/src/agent-state.ts +33 -0
  149. package/src/api.ts +16 -0
  150. package/src/cli/cli.ts +359 -137
  151. package/src/cli/format.ts +49 -9
  152. package/src/cli/help.ts +904 -359
  153. package/src/cli/pack.ts +6 -4
  154. package/src/cli/runner-files.ts +120 -0
  155. package/src/cli/runner-host.ts +48 -2
  156. package/src/client.ts +53 -0
  157. package/src/components/agent-page.tsx +150 -3
  158. package/src/components/agents.tsx +20 -5
  159. package/src/console.tsx +1 -0
  160. package/src/feed-words.ts +14 -1
  161. package/src/i18n/en.ts +39 -1
  162. package/src/i18n/ko.ts +32 -0
  163. package/src/limits.ts +10 -0
  164. package/src/plugin/context.ts +40 -7
  165. package/src/runner/acp.ts +9 -3
  166. package/src/runner/claude.ts +5 -1
  167. package/src/runner/codex.ts +14 -2
  168. package/src/runner/config.ts +114 -1
  169. package/src/runner/desk.ts +10 -0
  170. package/src/runner/message.ts +88 -126
  171. package/src/runner/runner.ts +487 -22
  172. package/src/runner/slack-status.ts +423 -0
  173. package/src/runner/slack.ts +293 -61
  174. package/src/runner/types.ts +108 -13
  175. package/src/runner/usage.ts +16 -3
  176. package/src/runner.ts +4 -0
  177. package/src/schemas.ts +97 -3
  178. package/src/vocabulary.ts +9 -0
  179. package/src/wake-text.ts +133 -0
@@ -2,6 +2,13 @@ import * as z from "zod/mini";
2
2
  import { MAX_SIGNAL_LENGTH } from "../limits.js";
3
3
  import { withoutForbiddenCodePoints } from "../text.js";
4
4
  import { wakeTextOf } from "./message.js";
5
+ import {
6
+ KEPT_STATUS,
7
+ keptStatusFits,
8
+ showSlackStatus,
9
+ slackStatusNote,
10
+ slackStatusNoted,
11
+ } from "./slack-status.js";
5
12
  import type { WakeAccount, WakeHost, WakePart, WakeRead, WakeSource, WakeWriter } from "./types.js";
6
13
 
7
14
  // The `slack` wake source (ADR-0085): one app for one agent account, which receives over its
@@ -20,6 +27,9 @@ import type { WakeAccount, WakeHost, WakePart, WakeRead, WakeSource, WakeWriter
20
27
  // on. With the member's own token, where the account keeps it, the source reads what was said
21
28
  // in the conversation before the message, and the wake carries what the agent missed of it,
22
29
  // who else there writes as an agent account does, and what to judge by whether to answer.
30
+ // What the source brought the agent it keeps by ids and moments, from one runner to the next,
31
+ // and counts for as long as the agent's conversation holds it (ADR-0166): a conversation begun
32
+ // since, at a reset say, is carried what led up to the message, the agent's own words among it.
23
33
  // Written from the messenger's own documentation of Socket Mode, of the Events API, of its
24
34
  // blocks and of the two methods that read a conversation; what an event or an answer holds is
25
35
  // read by schema, bounded, and never run.
@@ -53,6 +63,21 @@ const MAX_UNDER = 5;
53
63
  const MAX_BEFORE = 10;
54
64
  const MAX_BEFORE_TEXT = 500;
55
65
  const MAX_BEFORE_LENGTH = 3000;
66
+ /**
67
+ * The most of one earlier message where few are carried (ADR-0166): they share what a wake holds
68
+ * of them, so that one long message is read whole in its wake and not fetched again, and each of
69
+ * many is still cut at `MAX_BEFORE_TEXT`.
70
+ */
71
+ const MAX_BEFORE_MOST = 2000;
72
+ /** What a line that leads an earlier message takes of that share, about. */
73
+ const BEFORE_LINE = 200;
74
+ /**
75
+ * How many earlier messages a wake carries where the agent's conversation holds nothing of the
76
+ * place (ADR-0166): the last few, whoever wrote them, to take up what was being said.
77
+ */
78
+ const MAX_BEFORE_ANEW = 5;
79
+ /** How many members a wake's first line names of those a message names beside the agent. */
80
+ const MAX_NAMED = 5;
56
81
  /** How many messages a wake reads of a conversation, and of a thread: a page of so many, so many pages. */
57
82
  const READ_MESSAGES = 50;
58
83
  const READ_THREAD_PAGE = 200;
@@ -372,7 +397,14 @@ function blockOf(block: Json): string[] {
372
397
  }
373
398
  }
374
399
 
375
- /** A mark for each file of a message, by its name on one line: the first few, and how many more. */
400
+ /** A file's id, as the messenger spells one. */
401
+ const FILE_ID = /^F[A-Z0-9]{1,23}$/;
402
+
403
+ /**
404
+ * A mark for each file of a message, by its name on one line and the id the messenger reads it
405
+ * by (ADR-0166), so that the agent opens it with the messenger's own tool without looking the
406
+ * message up again: the first few, and how many more.
407
+ */
376
408
  function filesOf(files: unknown): string[] {
377
409
  const all = listOf(files).filter(isObject);
378
410
  const marks = all.slice(0, MAX_FILES_NAMED).map((file) => {
@@ -380,7 +412,11 @@ function filesOf(files: unknown): string[] {
380
412
  .replaceAll(/[\r\n\]]+/g, " ")
381
413
  .trim()
382
414
  .slice(0, MAX_FILE_NAME);
383
- return `[file${name === "" ? "" : ` ${name}`}, not carried in this wake]`;
415
+ const id = textOf(file.id);
416
+ const named = [name, id !== undefined && FILE_ID.test(id) ? `(${id})` : ""]
417
+ .filter((part) => part !== "")
418
+ .join(" ");
419
+ return `[file${named === "" ? "" : ` ${named}`}, not carried in this wake]`;
384
420
  });
385
421
  const more = all.length - marks.length;
386
422
  return more > 0 ? [...marks, `[${more} more files, not carried in this wake]`] : marks;
@@ -534,6 +570,35 @@ const partsOf = (said: Said): WakePart[] => [
534
570
  */
535
571
  const EVERYONE_CALLED = /<!(here|channel|everyone)(?:\|[^<>]{0,40})?>/;
536
572
 
573
+ /**
574
+ * How the messenger writes a member's name in a message's `text`, with what it shows for it
575
+ * after a bar where it says so. What a person typed that only looks like one arrives escaped.
576
+ */
577
+ const MEMBER_NAMED = /<@([UW][A-Z0-9]{1,23})(?:\|[^<>]{0,80})?>/g;
578
+
579
+ /**
580
+ * How the messenger writes a call of a group of members in a message's `text`, by the group's
581
+ * id, with what it shows for it after a bar where it says so. Who is in the group, nothing in a
582
+ * message says.
583
+ */
584
+ const GROUP_CALLED = /<!subteam\^[A-Z0-9]{1,23}(?:\|[^<>]{0,80})?>/;
585
+
586
+ /**
587
+ * Whom a message names: the members by their ids, each once, in the order it names them, in
588
+ * whichever of its two ways the messenger wrote a name, and whether it calls a group of members,
589
+ * which may hold anyone. The one reading of who is named, for the agent as for the others.
590
+ */
591
+ function namedIn(text: string): { readonly members: string[]; readonly group: boolean } {
592
+ const members: string[] = [];
593
+ for (const found of text.matchAll(MEMBER_NAMED)) {
594
+ const id = found[1];
595
+ if (id !== undefined && !members.includes(id)) {
596
+ members.push(id);
597
+ }
598
+ }
599
+ return { members, group: GROUP_CALLED.test(text) };
600
+ }
601
+
537
602
  /**
538
603
  * Who wrote a message, as a line of the source's says it: the account by the id the messenger
539
604
  * gives it, its workspace where the message says that it is another than the agent's own, and
@@ -602,12 +667,15 @@ const LOGIN = /^[A-Za-z0-9][A-Za-z0-9._@+-]{0,99}$/;
602
667
 
603
668
  /**
604
669
  * The account a message names as its writer, as the console is asked of it: with the workspace
605
- * the message itself says the account is of, and none where it says none. The agent's own
606
- * workspace is never taken for it: the same id at another workspace is another account, and an
607
- * account whose workspace nothing says is nobody the console knows.
670
+ * the message itself says the account is of. Where it says none, a message with a file say, the
671
+ * workspace is `home`, the agent's own, which is given only for a conversation the messenger
672
+ * says in so many words is shared with no other organization (ADR-0166): everyone who writes
673
+ * there is an account of the agent's own organization, where an id is one account. Anywhere
674
+ * else the agent's own workspace is never taken for it: the same id at another organization is
675
+ * another account, and an account whose workspace nothing says is nobody the console knows.
608
676
  */
609
- function accountOf(message: Written): WakeAccount | undefined {
610
- const workspace = workspaceOf(message.user_team) ?? workspaceOf(message.team);
677
+ function accountOf(message: Written, home?: string): WakeAccount | undefined {
678
+ const workspace = workspaceOf(message.user_team) ?? workspaceOf(message.team) ?? home;
611
679
  return message.user === undefined || workspace === undefined
612
680
  ? undefined
613
681
  : { workspace, account: message.user };
@@ -652,8 +720,9 @@ function standsOf(member: NonNullable<WakeWriter["member"]>): string {
652
720
  * with how, its manager, anyone above its manager or an administrator, and of anyone below
653
721
  * the agent in its line that they report to it, themselves or through others (`standsOf`);
654
722
  * - that it is no member the console knows, where the console is told the accounts of that
655
- * workspace and not this one: what it is told may be short of one, so a supervisor's tell that
656
- * names the account still tells whose it is;
723
+ * workspace and not this one: what it is told may be short of one, so a supervisor's own word
724
+ * that names the account, a tell or a message in an account known to be a supervisor's
725
+ * (ADR-0173), still tells whose it is;
657
726
  * - nothing, where the console is told no account of that workspace, where it does not answer
658
727
  * before the signal ends, or where it is older than the question: the line is then as the
659
728
  * messenger alone has it. Where the signal has ended already, the console is not asked.
@@ -667,11 +736,12 @@ async function whoOf(
667
736
  host: Pick<WakeHost, "writers">,
668
737
  messages: readonly Written[],
669
738
  until: AbortSignal,
739
+ home?: string,
670
740
  ): Promise<Who> {
671
741
  const keyOf = (one: WakeAccount): string => `${one.workspace} ${one.account}`;
672
742
  const asked = new Map<string, WakeAccount>();
673
743
  for (const message of messages) {
674
- const one = accountOf(message);
744
+ const one = accountOf(message, home);
675
745
  if (one !== undefined && asked.size < MAX_WRITERS_ASKED) {
676
746
  asked.set(keyOf(one), one);
677
747
  }
@@ -704,7 +774,7 @@ async function whoOf(
704
774
  );
705
775
  }
706
776
  return (message) => {
707
- const one = accountOf(message);
777
+ const one = accountOf(message, home);
708
778
  return one === undefined ? "" : (known.get(keyOf(one)) ?? "");
709
779
  };
710
780
  } catch {
@@ -731,6 +801,11 @@ interface Heard {
731
801
  readonly own: readonly (string | undefined)[];
732
802
  /** What the event says of whoever wrote, for the console to be asked who that is. */
733
803
  readonly from: Written;
804
+ /**
805
+ * The agent's own workspace, where the messenger says that the conversation is shared with no
806
+ * other organization: the workspace of whoever writes there, where a message names none.
807
+ */
808
+ readonly home: string | undefined;
734
809
  }
735
810
 
736
811
  /**
@@ -765,11 +840,12 @@ function heardOf(payload: unknown, channels: readonly string[]): Heard | undefin
765
840
  return undefined;
766
841
  }
767
842
  const direct = event.channel_type === "im";
768
- // A mention is told by the message's `text`, where the messenger writes one in its own way and
769
- // escapes what a person typed that only looks like one.
843
+ // A mention is told by the message's `text`, where the messenger writes one in its own ways and
844
+ // escapes what a person typed that only looks like one: read once, for the agent and for
845
+ // whoever else the message names.
846
+ const named = namedIn(event.text ?? "");
770
847
  const mentioned =
771
- event.type === "app_mention" ||
772
- (member !== undefined && (event.text ?? "").includes(`<@${member}>`));
848
+ event.type === "app_mention" || (member !== undefined && named.members.includes(member));
773
849
  const group = event.channel_type === "mpim";
774
850
  // Where nobody named the agent, only a person's own words wake it: what an app posts in a
775
851
  // conversation the agent listens to is not said to it, and would wake it at every post.
@@ -802,12 +878,28 @@ function heardOf(payload: unknown, channels: readonly string[]): Heard | undefin
802
878
  // A conversation shared with another organization is said to be one, whoever wrote in it:
803
879
  // what is said there, people outside read.
804
880
  const shared = parsed.data.is_ext_shared_channel === true ? "externally shared " : "";
881
+ // Whom a message names where it does not name the agent (ADR-0166): where several are in a
882
+ // conversation, that is whom it was said to, and the agent reads so before it reads on. Not
883
+ // in a direct conversation, where whatever is said is said to the agent, nor in a call of
884
+ // everyone, which calls the agent with the rest, nor where a group of members is called,
885
+ // which the agent may be in: nobody is said not to be named who may have been.
886
+ const others =
887
+ direct || mentioned || called !== undefined || named.group || member === undefined
888
+ ? []
889
+ : named.members.filter((one) => one !== member && one !== event.user);
890
+ const naming =
891
+ others.length === 0
892
+ ? ""
893
+ : `, naming ${others.slice(0, MAX_NAMED).join(", ")}${others.length > MAX_NAMED ? ` and ${others.length - MAX_NAMED} more` : ""} and not you`;
805
894
  const whole = saidOf(event);
806
895
  const saying = (who: Who): readonly WakePart[] => {
807
- const from = `from ${writerOf(event, own, who)}`;
896
+ const from = `from ${writerOf(event, own, who)}${naming}`;
897
+ // A message of a channel that is in no thread is said to be in none (ADR-0166), with the
898
+ // thread a reply to it goes in, which is its own: named as a thread alone, it read as a
899
+ // reply in one.
808
900
  const first =
809
901
  where === undefined
810
- ? `${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}`
902
+ ? `${mentioned ? "A mention" : called !== undefined ? `A call of everyone (@${called})` : "A message"} in the ${shared}channel ${event.channel} at Slack, ${from}, ${event.thread_ts === undefined ? `outside any thread: a reply begins the thread ${thread}` : `in the thread ${thread}`}`
811
903
  : event.thread_ts === undefined
812
904
  ? `${where}, ${from}, in the ${shared}conversation ${event.channel}`
813
905
  : `${where}, ${from}, in the thread ${thread} of the ${shared}conversation ${event.channel}`;
@@ -848,6 +940,7 @@ function heardOf(payload: unknown, channels: readonly string[]): Heard | undefin
848
940
  team: event.team,
849
941
  user_team: event.user_team,
850
942
  },
943
+ home: parsed.data.is_ext_shared_channel === false ? workspaceOf(installed?.team_id) : undefined,
851
944
  };
852
945
  }
853
946
 
@@ -1096,8 +1189,9 @@ const readWith = (channel: string, thread: string | undefined, after?: string):
1096
1189
  */
1097
1190
  function earlierOf(
1098
1191
  message: HeardMessage,
1099
- heard: Pick<Heard, "ts" | "own">,
1192
+ heard: Pick<Heard, "ts" | "own" | "member">,
1100
1193
  who: Who = NOBODY,
1194
+ most = MAX_BEFORE_TEXT,
1101
1195
  ): readonly [WakePart, WakePart] {
1102
1196
  const said = saidOf(message);
1103
1197
  const words = [
@@ -1108,15 +1202,17 @@ function earlierOf(
1108
1202
  ]
1109
1203
  .filter((part) => part !== "")
1110
1204
  .join("\n");
1111
- const cut =
1112
- words.length > MAX_BEFORE_TEXT
1113
- ? `, cut after ${MAX_BEFORE_TEXT} of its ${words.length} characters`
1114
- : "";
1205
+ const cut = words.length > most ? `, cut after ${most} of its ${words.length} characters` : "";
1206
+ // The agent's own message, carried where its conversation holds it no more, is said as its own.
1207
+ const from =
1208
+ message.user !== undefined && message.user === heard.member
1209
+ ? "you"
1210
+ : writerOf(message, heard.own, who);
1115
1211
  return [
1116
1212
  {
1117
- says: `${stretchOf(secondsOf(heard.ts) - secondsOf(message.ts))} before it, from ${writerOf(message, heard.own, who)}${cut}`,
1213
+ says: `${stretchOf(secondsOf(heard.ts) - secondsOf(message.ts))} before it, from ${from}${cut}`,
1118
1214
  },
1119
- { words: cut === "" ? words : `${firstOf(words, MAX_BEFORE_TEXT)}…` },
1215
+ { words: cut === "" ? words : `${firstOf(words, most)}…` },
1120
1216
  ];
1121
1217
  }
1122
1218
 
@@ -1130,6 +1226,36 @@ const broughtOf = (heard: Pick<Heard, "channel" | "thread">): string =>
1130
1226
  /** The name the source keeps a message a wake carried under: `woke`, or `said` among what was said before another. */
1131
1227
  const carriedAs = (channel: string, ts: string): string => `carried:${channel}:${ts}`;
1132
1228
 
1229
+ /** The name the source keeps the moment under at which a wake last said, of a conversation or a thread, what to judge by whether to answer. */
1230
+ const judgedOf = (heard: Pick<Heard, "channel" | "thread">): string =>
1231
+ `judged:${heard.channel}:${heard.thread ?? ""}`;
1232
+
1233
+ /** A place the source keeps a moment under, and a message it keeps a word under, as it names them. */
1234
+ const KEPT_PLACE = /^(?:brought|judged):[A-Z0-9]{1,24}:(?:[0-9]{1,12}\.[0-9]{1,9})?$/;
1235
+ const KEPT_MESSAGE = /^carried:[A-Z0-9]{1,24}:[0-9]{1,12}\.[0-9]{1,9}$/;
1236
+ const KEPT_MOMENT = /^[0-9]{1,12}\.[0-9]{1,9}$/;
1237
+
1238
+ /**
1239
+ * Holds what the source kept to its own shapes (ADR-0166): a moment of the messenger's under a
1240
+ * place, `woke` or `said` under a message. It is read back from a file of the desk, where
1241
+ * anything may stand, and compared as it is: a moment that is none, a number without end say,
1242
+ * would count everything said in a place as brought, for good. Anything else is nothing kept
1243
+ * for that name. Beside them, until when a status of the source's own stands (ADR-0170).
1244
+ */
1245
+ function ownShapes(kept: Map<string, string>): void {
1246
+ for (const [name, value] of [...kept]) {
1247
+ const fits =
1248
+ name === KEPT_STATUS
1249
+ ? keptStatusFits(value)
1250
+ : KEPT_PLACE.test(name)
1251
+ ? KEPT_MOMENT.test(value)
1252
+ : KEPT_MESSAGE.test(name) && (value === "woke" || value === "said");
1253
+ if (!fits) {
1254
+ kept.delete(name);
1255
+ }
1256
+ }
1257
+ }
1258
+
1133
1259
  /** Keeps a name as the latest, and lets go of the oldest past the most. */
1134
1260
  function keep(kept: Map<string, string>, name: string, value: string): void {
1135
1261
  kept.delete(name);
@@ -1147,24 +1273,32 @@ interface Prior {
1147
1273
  readonly parts: readonly WakePart[];
1148
1274
  readonly read: boolean;
1149
1275
  readonly carried: readonly string[];
1276
+ /** Whether it says in full what the agent judges by whether to answer: kept, so that the next there says it short. */
1277
+ readonly judged?: boolean | undefined;
1150
1278
  }
1151
1279
 
1152
1280
  /**
1153
- * What a wake carries of the conversation it was raised in, after the message itself (ADR-0163),
1154
- * read with the member's own token where the account keeps it:
1281
+ * What a wake carries of the conversation it was raised in, after the message itself (ADR-0163,
1282
+ * ADR-0166), read with the member's own token where the account keeps it:
1155
1283
  *
1156
1284
  * - what was said there before the message and since the later of two: the agent's own last
1157
1285
  * message there, and the last message a wake already brought it from there, and of that what
1158
1286
  * no wake carried, in a thread the message that began it say; so that an agent that stays
1159
1287
  * silent is handed each message once. The oldest first, each behind a line that says who wrote
1160
- * it and how long before: the last few, so much of each, and where there was more, how many
1161
- * and the command that reads them. An agent account's message wakes no other unless it names
1162
- * it, so this is where an agent reads what another answered. It is information, whoever wrote
1163
- * it;
1288
+ * it and how long before: the last few, so much of each as their number leaves room for, and
1289
+ * where there was more, how many and the command that reads them. An agent account's message
1290
+ * wakes no other unless it names it, so this is where an agent reads what another answered.
1291
+ * It is information, whoever wrote it;
1292
+ * - each of the two counts only where the agent's conversation still holds it: one that began
1293
+ * since, at a reset say (`WakeHost.began`), holds nothing of what the agent wrote or was
1294
+ * brought before. The first wake of a place in such a conversation carries the last few
1295
+ * messages before it, whoever wrote them, the agent's own said as its own, so that what was
1296
+ * being said is taken up from the wake and not from a memory of it;
1164
1297
  * - who else there writes through an app, as an agent account does, the last to write first,
1165
1298
  * and with them what the agent judges by whether to answer: said in every conversation but
1166
1299
  * a direct one where such an account was read, since one message wakes each of them and
1167
- * none sees what another is about to say.
1300
+ * none sees what another is about to say. The sentence is said once for a place in a
1301
+ * conversation of the agent's; after that the line names them and the command, and no more.
1168
1302
  *
1169
1303
  * Where the account keeps no token to read with, nothing is added, and the wake is as it was;
1170
1304
  * where the messenger refuses the read or does not answer, a line says so, with why in a short
@@ -1172,7 +1306,7 @@ interface Prior {
1172
1306
  * conversation outside its threads.
1173
1307
  */
1174
1308
  async function beforeOf(
1175
- host: Pick<WakeHost, "secret" | "fetch" | "kept" | "writers">,
1309
+ host: Pick<WakeHost, "secret" | "fetch" | "kept" | "writers" | "began">,
1176
1310
  heard: Heard,
1177
1311
  writer: Promise<Who>,
1178
1312
  until: AbortSignal,
@@ -1181,7 +1315,12 @@ async function beforeOf(
1181
1315
  if (token === undefined) {
1182
1316
  return { parts: [], read: false, carried: [] };
1183
1317
  }
1184
- const upTo = host.kept.get(broughtOf(heard));
1318
+ // What was written or brought before the agent's conversation began, that conversation holds
1319
+ // no more: a moment of the messenger's is held only from then on.
1320
+ const began = host.began?.() ?? 0;
1321
+ const held = (moment: string): boolean => Number(moment) * 1000 >= began;
1322
+ const mark = host.kept.get(broughtOf(heard));
1323
+ const upTo = mark !== undefined && held(mark) ? mark : undefined;
1185
1324
  const read =
1186
1325
  heard.thread === undefined
1187
1326
  ? await conversationOf(
@@ -1241,20 +1380,26 @@ async function beforeOf(
1241
1380
  heard.direct || heard.member === undefined || others.length === 0
1242
1381
  ? []
1243
1382
  : writers.slice(0, MAX_OTHERS + 1);
1244
- const wrote =
1383
+ const own =
1245
1384
  heard.member === undefined
1246
1385
  ? -1
1247
1386
  : earlier.findLastIndex((message) => message.user === heard.member);
1387
+ const wrote = own !== -1 && held(earlier[own]?.ts ?? "0") ? own : -1;
1248
1388
  // What a wake already brought of this conversation, as the message that woke or among what
1249
1389
  // was said before it, is not carried again: the agent has it, whether or not it answered. Nor
1250
1390
  // is what a wake brought from elsewhere, the message that began this thread from its channel.
1251
1391
  const brought =
1252
1392
  upTo === undefined ? -1 : earlier.findLastIndex((message) => !before(upTo, message.ts));
1253
1393
  const last = Math.max(wrote, brought);
1394
+ // Nothing of the place is in the agent's conversation, and something was said there before
1395
+ // that conversation began: what is carried is what leads up to the message, whoever wrote it.
1396
+ const anew = last === -1 && earlier.some((message) => !held(message.ts));
1254
1397
  const since = earlier
1255
1398
  .slice(last + 1)
1256
- .filter((message) => !host.kept.has(carriedAs(heard.channel, message.ts)));
1257
- const latest = since.toReversed().slice(0, MAX_BEFORE);
1399
+ .filter(
1400
+ (message) => !(held(message.ts) && host.kept.has(carriedAs(heard.channel, message.ts))),
1401
+ );
1402
+ const latest = since.toReversed().slice(0, anew ? MAX_BEFORE_ANEW : MAX_BEFORE);
1258
1403
  // Who wrote each, as the console says it (ADR-0164), asked at once for all whose account is
1259
1404
  // another than that of the message that woke, which is asked of beside the read: said of an
1260
1405
  // earlier message as of the one that woke, and information all the same, whoever wrote it;
@@ -1270,14 +1415,19 @@ async function beforeOf(
1270
1415
  }
1271
1416
  }
1272
1417
  const first = await writer;
1273
- const known = accountOf(heard.from);
1418
+ const known = accountOf(heard.from, heard.home);
1274
1419
  const rest = await whoOf(
1275
1420
  host,
1276
1421
  [...latest, ...theirs.values()].filter((message) => {
1277
- const one = accountOf(message);
1278
- return one?.account !== known?.account || one?.workspace !== known?.workspace;
1422
+ const one = accountOf(message, heard.home);
1423
+ // The agent's own account is said as its own, and asked of nobody.
1424
+ return (
1425
+ message.user !== heard.member &&
1426
+ (one?.account !== known?.account || one?.workspace !== known?.workspace)
1427
+ );
1279
1428
  }),
1280
1429
  until,
1430
+ heard.home,
1281
1431
  );
1282
1432
  const who: Who = (message) => first(message) || rest(message);
1283
1433
  const named = shown
@@ -1286,12 +1436,17 @@ async function beforeOf(
1286
1436
  return one === heard.member ? "you" : `${one}${written === undefined ? "" : who(written)}`;
1287
1437
  })
1288
1438
  .join(", ");
1439
+ // What to judge by is said once for a place in a conversation of the agent's (ADR-0166):
1440
+ // where a wake said it there since that conversation began, the line names who else writes
1441
+ // there and the command, which are what changes from one wake to the next.
1442
+ const judged = host.kept.get(judgedOf(heard));
1443
+ const brief = judged !== undefined && held(judged);
1289
1444
  const answering: WakePart[] =
1290
1445
  shown.length === 0
1291
1446
  ? []
1292
1447
  : [
1293
1448
  {
1294
- 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: ${
1449
+ says: `Others here that write through an app, ${brief ? "" : "as agents do, "}the last to write first: ${named}${writers.length > MAX_OTHERS + 1 ? ` and ${writers.length - MAX_OTHERS - 1} more` : ""}. ${brief ? "" : "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: ${
1295
1450
  // A message of a channel is answered in its thread, where one that began none is read as the channel.
1296
1451
  readWith(
1297
1452
  heard.channel,
@@ -1301,8 +1456,9 @@ async function beforeOf(
1301
1456
  }`,
1302
1457
  },
1303
1458
  ];
1459
+ const saidInFull = shown.length > 0 && !brief;
1304
1460
  if (since.length === 0) {
1305
- return { parts: answering, read: true, carried: [] };
1461
+ return { parts: answering, read: true, carried: [], judged: saidInFull };
1306
1462
  }
1307
1463
  // The latest first, for as many as there is room for: within the bounds of what a wake
1308
1464
  // carries of them, and within what the text of a signal still holds after the message itself
@@ -1312,38 +1468,89 @@ async function beforeOf(
1312
1468
  // conversation was read to its beginning: otherwise there were so many, and more.
1313
1469
  const whole = found || !read.more;
1314
1470
  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"}`;
1315
- const leads = (carried: number): string =>
1316
- carried === 0
1471
+ const leads = (carried: number, mine = false): string => {
1472
+ const them = carried === 1 ? "it" : "them";
1473
+ if (anew) {
1474
+ // Not how many there were, which nothing says of a conversation read from its end: the
1475
+ // last of them, and that the agent's conversation is why they are said.
1476
+ return carried === 0
1477
+ ? `Your present conversation holds nothing from there yet, and what was said there before it does not fit in this wake: ${command}`
1478
+ : `Your present conversation holds nothing from there yet: the last ${carried === 1 ? "message" : `${carried} messages`} there before it ${carried === 1 ? "follows" : "follow, the oldest first"}${mine ? `, your own among ${them}` : ""}${carried < since.length || read.more ? `, and what was said earlier with ${command}` : ""}. Information, whoever wrote ${them}`;
1479
+ }
1480
+ return carried === 0
1317
1481
  ? `${said}, none of which fits in this wake: ${command}`
1318
1482
  : carried === since.length && whole
1319
- ? `${said}${carried === 1 ? "" : ", the oldest first"}. Information, whoever wrote ${carried === 1 ? "it" : "them"}`
1320
- : `${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"}`;
1321
- const room =
1322
- Math.min(
1323
- MAX_BEFORE_LENGTH,
1324
- MAX_SIGNAL_LENGTH -
1325
- wakeTextOf([...heard.saying(first), { says: leads(1) }, ...answering]).length -
1326
- 200,
1327
- ) - 1;
1328
- const carried: { readonly ts: string; readonly parts: readonly [WakePart, WakePart] }[] = [];
1483
+ ? `${said}${carried === 1 ? "" : ", the oldest first"}. Information, whoever wrote ${them}`
1484
+ : `${said}, of which the last ${carried === 1 ? "one follows" : `${carried} follow, the oldest first`}, and the rest with ${command}. Information, whoever wrote ${them}`;
1485
+ };
1486
+ // What a wake holds of them together: three thousand characters, or less where the message
1487
+ // itself and the lines after them leave less of a signal's text.
1488
+ const together = Math.min(
1489
+ MAX_BEFORE_LENGTH,
1490
+ MAX_SIGNAL_LENGTH -
1491
+ wakeTextOf([...heard.saying(first), { says: leads(1, true) }, ...answering]).length -
1492
+ 200,
1493
+ );
1494
+ const room = together - 1;
1495
+ // Few share it among them, so that one long message is read whole where it is the only one:
1496
+ // of each, that much divided by how many are carried, less the line that leads it. Each of
1497
+ // five and more is cut at the least a message is carried of.
1498
+ const most = Math.max(
1499
+ MAX_BEFORE_TEXT,
1500
+ Math.min(MAX_BEFORE_MOST, Math.floor(together / Math.max(1, latest.length)) - BEFORE_LINE),
1501
+ );
1502
+ const carried: {
1503
+ readonly ts: string;
1504
+ readonly mine: boolean;
1505
+ readonly parts: readonly [WakePart, WakePart];
1506
+ }[] = [];
1507
+ const one = (message: HeardMessage, parts: readonly [WakePart, WakePart]) => ({
1508
+ ts: message.ts,
1509
+ mine: message.user !== undefined && message.user === heard.member,
1510
+ parts,
1511
+ });
1329
1512
  let length = 0;
1330
1513
  for (const message of latest) {
1331
- const parts = earlierOf(message, heard, who);
1514
+ const parts = earlierOf(message, heard, who, most);
1332
1515
  const takes = wakeTextOf(parts).length + 1;
1333
1516
  if (length + takes > room) {
1334
1517
  break;
1335
1518
  }
1336
- carried.unshift({ ts: message.ts, parts });
1519
+ carried.unshift(one(message, parts));
1337
1520
  length += takes;
1338
1521
  }
1522
+ // A message takes more of a wake than its characters where its lines are many, each begun as
1523
+ // a signal's text begins one. The latest is cut shorter until it fits, down to the least a
1524
+ // message is carried of, rather than left out where it is all there is to carry.
1525
+ const newest = latest[0];
1526
+ for (
1527
+ let less = Math.floor(most / 2);
1528
+ carried.length === 0 && newest !== undefined && most > MAX_BEFORE_TEXT;
1529
+ less = Math.floor(less / 2)
1530
+ ) {
1531
+ const cut = Math.max(MAX_BEFORE_TEXT, less);
1532
+ const parts = earlierOf(newest, heard, who, cut);
1533
+ if (wakeTextOf(parts).length + 1 <= room) {
1534
+ carried.push(one(newest, parts));
1535
+ }
1536
+ if (cut === MAX_BEFORE_TEXT) {
1537
+ break;
1538
+ }
1539
+ }
1339
1540
  return {
1340
1541
  parts: [
1341
- { says: leads(carried.length) },
1542
+ {
1543
+ says: leads(
1544
+ carried.length,
1545
+ carried.some((one) => one.mine),
1546
+ ),
1547
+ },
1342
1548
  ...carried.flatMap((one) => [...one.parts]),
1343
1549
  ...answering,
1344
1550
  ],
1345
1551
  read: true,
1346
1552
  carried: carried.map((one) => one.ts),
1553
+ judged: saidInFull,
1347
1554
  };
1348
1555
  }
1349
1556
 
@@ -1358,9 +1565,13 @@ async function beforeOf(
1358
1565
  * said to the agent instructs it only where it is what woke it;
1359
1566
  * - the moment of the message, as far as the agent was brought what is said there: only where
1360
1567
  * what was said before it was read, so that a wake that carried nothing of it leaves the next
1361
- * to.
1568
+ * to;
1569
+ * - the moment at which a wake said in full, of that place, what the agent judges by whether to
1570
+ * answer.
1362
1571
  *
1363
- * The moment of a message is kept, and where it was written, never a word of one.
1572
+ * The moment of a message is kept, and where it was written, never a word of one. It is kept
1573
+ * from one runner to the next where the machine keeps it, and counts for as long as the
1574
+ * agent's conversation holds what it stands for (ADR-0166).
1364
1575
  */
1365
1576
  async function raiseOf(host: WakeHost, heard: Heard, reading: AbortSignal): Promise<void> {
1366
1577
  const message = carriedAs(heard.channel, heard.ts);
@@ -1370,7 +1581,7 @@ async function raiseOf(host: WakeHost, heard: Heard, reading: AbortSignal): Prom
1370
1581
  const { parts, prior } = await within(host, reading, async (until) => {
1371
1582
  // Who wrote, as the console says it (ADR-0164), is asked beside the read of the conversation
1372
1583
  // and not after it: a messenger that does not answer costs the wake's first line nothing.
1373
- const writer = whoOf(host, [heard.from], until);
1584
+ const writer = whoOf(host, [heard.from], until, heard.home);
1374
1585
  // A read that fails in a way of its own costs the wake nothing: it goes as it stands.
1375
1586
  const prior = await beforeOf(host, heard, writer, until).catch(
1376
1587
  (): Prior => ({
@@ -1381,8 +1592,15 @@ async function raiseOf(host: WakeHost, heard: Heard, reading: AbortSignal): Prom
1381
1592
  );
1382
1593
  return { parts: heard.saying(await writer), prior };
1383
1594
  });
1595
+ // Why the agent's status is not shown, where the messenger refused it (ADR-0170): said to
1596
+ // the agent once, as a line of the source's own in a wake that has room for it.
1597
+ const carried: readonly WakePart[] = [...parts, ...prior.parts];
1598
+ const note = slackStatusNote(host);
1599
+ const withNote: readonly WakePart[] | undefined =
1600
+ note === undefined ? undefined : [...carried, { says: note }];
1601
+ const noted = withNote !== undefined && wakeTextOf(withNote).length <= MAX_SIGNAL_LENGTH;
1384
1602
  const taken = await host
1385
- .raise({ key: heard.key, parts: [...parts, ...prior.parts] })
1603
+ .raise({ key: heard.key, parts: noted ? withNote : carried })
1386
1604
  .catch((error: unknown) => {
1387
1605
  host.log(
1388
1606
  `a wake from slack was not raised (${error instanceof Error ? error.name : "error"})`,
@@ -1392,10 +1610,20 @@ async function raiseOf(host: WakeHost, heard: Heard, reading: AbortSignal): Prom
1392
1610
  if (!taken) {
1393
1611
  return;
1394
1612
  }
1613
+ if (noted) {
1614
+ slackStatusNoted(host);
1615
+ }
1395
1616
  for (const ts of prior.carried) {
1396
- keep(host.kept, carriedAs(heard.channel, ts), "said");
1617
+ // One that woke stays one that woke, though it is carried again among what led up to this
1618
+ // message, in a conversation begun since: its own event, come again, wakes nobody twice.
1619
+ const name = carriedAs(heard.channel, ts);
1620
+ keep(host.kept, name, host.kept.get(name) === "woke" ? "woke" : "said");
1397
1621
  }
1398
1622
  keep(host.kept, message, "woke");
1623
+ if (prior.judged === true) {
1624
+ // What to judge by was said in full: the next wake there says it short (ADR-0166).
1625
+ keep(host.kept, judgedOf(heard), heard.ts);
1626
+ }
1399
1627
  if (prior.read) {
1400
1628
  const place = broughtOf(heard);
1401
1629
  const was = host.kept.get(place);
@@ -1590,6 +1818,9 @@ async function listen(
1590
1818
  export const slack: WakeSource = {
1591
1819
  name: "slack",
1592
1820
  async run(host: WakeHost, signal: AbortSignal): Promise<void> {
1821
+ // What a runner before this one kept comes from a file: held to the source's own shapes
1822
+ // before anything is compared with it.
1823
+ ownShapes(host.kept);
1593
1824
  const token = await host.secret(SLACK_APP_TOKEN);
1594
1825
  if (token === undefined) {
1595
1826
  host.log(`the slack wake source finds no ${SLACK_APP_TOKEN} in the account's store`);
@@ -1633,4 +1864,5 @@ export const slack: WakeSource = {
1633
1864
  }
1634
1865
  },
1635
1866
  read: (host, where, options) => readOf(host, where, options),
1867
+ show: (host, status, options) => showSlackStatus(host, SLACK_USER_TOKEN, status, options),
1636
1868
  };