@skillcdn/console 0.6.2 → 0.6.4
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 +16 -9
- package/SKILLCDN.md +3 -2
- package/dist/agent-page.d.ts +2 -0
- package/dist/agent-page.d.ts.map +1 -1
- package/dist/agent-page.js +46 -5
- package/dist/agent-page.js.map +1 -1
- package/dist/agent-state.d.ts +9 -0
- package/dist/agent-state.d.ts.map +1 -0
- package/dist/agent-state.js +28 -0
- package/dist/agent-state.js.map +1 -0
- package/dist/api.d.ts +6 -4
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +6 -4
- package/dist/api.js.map +1 -1
- package/dist/cli/cli.d.ts +2 -0
- package/dist/cli/cli.d.ts.map +1 -1
- package/dist/cli/cli.js +948 -252
- package/dist/cli/cli.js.map +1 -1
- package/dist/cli/format.d.ts +44 -7
- package/dist/cli/format.d.ts.map +1 -1
- package/dist/cli/format.js +168 -13
- package/dist/cli/format.js.map +1 -1
- package/dist/cli/help.d.ts +82 -9
- package/dist/cli/help.d.ts.map +1 -1
- package/dist/cli/help.js +855 -331
- package/dist/cli/help.js.map +1 -1
- package/dist/cli/pack.d.ts +13 -12
- package/dist/cli/pack.d.ts.map +1 -1
- package/dist/cli/pack.js +33 -45
- package/dist/cli/pack.js.map +1 -1
- package/dist/cli/plugin-cli.d.ts.map +1 -1
- package/dist/cli/plugin-cli.js +8 -5
- package/dist/cli/plugin-cli.js.map +1 -1
- package/dist/cli/runner-files.d.ts +16 -0
- package/dist/cli/runner-files.d.ts.map +1 -0
- package/dist/cli/runner-files.js +125 -0
- package/dist/cli/runner-files.js.map +1 -0
- package/dist/cli/runner-host.d.ts +7 -0
- package/dist/cli/runner-host.d.ts.map +1 -1
- package/dist/cli/runner-host.js +40 -2
- package/dist/cli/runner-host.js.map +1 -1
- package/dist/cli/skill-path.d.ts +11 -0
- package/dist/cli/skill-path.d.ts.map +1 -0
- package/dist/cli/skill-path.js +44 -0
- package/dist/cli/skill-path.js.map +1 -0
- package/dist/client.d.ts +32 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +15 -1
- package/dist/client.js.map +1 -1
- package/dist/components/agent-page.d.ts +93 -6
- package/dist/components/agent-page.d.ts.map +1 -1
- package/dist/components/agent-page.js +202 -10
- package/dist/components/agent-page.js.map +1 -1
- package/dist/components/agents.d.ts +6 -6
- package/dist/components/agents.d.ts.map +1 -1
- package/dist/components/agents.js +16 -9
- package/dist/components/agents.js.map +1 -1
- package/dist/components/people.d.ts.map +1 -1
- package/dist/components/people.js +7 -2
- package/dist/components/people.js.map +1 -1
- package/dist/components/picker.d.ts +16 -0
- package/dist/components/picker.d.ts.map +1 -1
- package/dist/components/picker.js +69 -4
- package/dist/components/picker.js.map +1 -1
- package/dist/console.css +40 -0
- package/dist/console.js +2 -2
- package/dist/console.js.map +1 -1
- package/dist/feed-words.d.ts.map +1 -1
- package/dist/feed-words.js +12 -1
- package/dist/feed-words.js.map +1 -1
- package/dist/i18n/en.d.ts +64 -6
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +59 -10
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/ko.d.ts.map +1 -1
- package/dist/i18n/ko.js +49 -9
- package/dist/i18n/ko.js.map +1 -1
- package/dist/limits.d.ts +26 -0
- package/dist/limits.d.ts.map +1 -1
- package/dist/limits.js +26 -0
- package/dist/limits.js.map +1 -1
- package/dist/onboarding.d.ts +18 -6
- package/dist/onboarding.d.ts.map +1 -1
- package/dist/onboarding.js +42 -7
- package/dist/onboarding.js.map +1 -1
- package/dist/plugin/args.d.ts +24 -2
- package/dist/plugin/args.d.ts.map +1 -1
- package/dist/plugin/args.js +20 -2
- package/dist/plugin/args.js.map +1 -1
- package/dist/plugin/context.d.ts +39 -7
- package/dist/plugin/context.d.ts.map +1 -1
- package/dist/plugin/define.d.ts.map +1 -1
- package/dist/plugin/define.js +12 -3
- package/dist/plugin/define.js.map +1 -1
- package/dist/plugin/rest-schemas.d.ts +2 -0
- package/dist/plugin/rest-schemas.d.ts.map +1 -1
- package/dist/plugin/rest-schemas.js +1 -0
- package/dist/plugin/rest-schemas.js.map +1 -1
- package/dist/routes.d.ts +2 -0
- package/dist/routes.d.ts.map +1 -1
- package/dist/routes.js +2 -0
- package/dist/routes.js.map +1 -1
- package/dist/rules.d.ts +4 -1
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +5 -2
- package/dist/rules.js.map +1 -1
- package/dist/runner/acp.d.ts.map +1 -1
- package/dist/runner/acp.js +9 -3
- package/dist/runner/acp.js.map +1 -1
- package/dist/runner/claude.d.ts.map +1 -1
- package/dist/runner/claude.js +53 -1
- package/dist/runner/claude.js.map +1 -1
- package/dist/runner/codex.d.ts.map +1 -1
- package/dist/runner/codex.js +18 -3
- package/dist/runner/codex.js.map +1 -1
- package/dist/runner/config.d.ts +18 -0
- package/dist/runner/config.d.ts.map +1 -1
- package/dist/runner/config.js +77 -1
- package/dist/runner/config.js.map +1 -1
- package/dist/runner/desk.d.ts +7 -0
- package/dist/runner/desk.d.ts.map +1 -1
- package/dist/runner/desk.js +14 -4
- package/dist/runner/desk.js.map +1 -1
- package/dist/runner/message.d.ts +16 -22
- package/dist/runner/message.d.ts.map +1 -1
- package/dist/runner/message.js +72 -120
- package/dist/runner/message.js.map +1 -1
- package/dist/runner/runner.d.ts +41 -1
- package/dist/runner/runner.d.ts.map +1 -1
- package/dist/runner/runner.js +557 -23
- package/dist/runner/runner.js.map +1 -1
- package/dist/runner/slack-status.d.ts +19 -0
- package/dist/runner/slack-status.d.ts.map +1 -0
- package/dist/runner/slack-status.js +332 -0
- package/dist/runner/slack-status.js.map +1 -0
- package/dist/runner/slack.d.ts.map +1 -1
- package/dist/runner/slack.js +258 -59
- package/dist/runner/slack.js.map +1 -1
- package/dist/runner/types.d.ts +128 -15
- package/dist/runner/types.d.ts.map +1 -1
- package/dist/runner/types.js.map +1 -1
- package/dist/runner/usage.d.ts +6 -1
- package/dist/runner/usage.d.ts.map +1 -1
- package/dist/runner/usage.js +9 -2
- package/dist/runner/usage.js.map +1 -1
- package/dist/runner.d.ts +2 -2
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js.map +1 -1
- package/dist/schemas.d.ts +635 -6
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +197 -15
- package/dist/schemas.js.map +1 -1
- package/dist/secret-file.d.ts +21 -0
- package/dist/secret-file.d.ts.map +1 -0
- package/dist/secret-file.js +59 -0
- package/dist/secret-file.js.map +1 -0
- package/dist/services.d.ts.map +1 -1
- package/dist/services.js +3 -2
- package/dist/services.js.map +1 -1
- package/dist/setup-guide.d.ts +2 -2
- package/dist/setup-guide.d.ts.map +1 -1
- package/dist/setup-guide.js +6 -5
- package/dist/setup-guide.js.map +1 -1
- package/dist/vocabulary.d.ts +17 -2
- package/dist/vocabulary.d.ts.map +1 -1
- package/dist/vocabulary.js +15 -2
- package/dist/vocabulary.js.map +1 -1
- package/dist/wake-text.d.ts +43 -0
- package/dist/wake-text.d.ts.map +1 -0
- package/dist/wake-text.js +115 -0
- package/dist/wake-text.js.map +1 -0
- package/docs/specs/cli.md +39 -25
- package/docs/specs/plugins.md +10 -9
- package/docs/specs/rest.md +34 -15
- package/docs/specs/runner.md +108 -25
- package/docs/start.md +28 -6
- package/package.json +3 -3
- package/skills/building-a-plugin/SKILL.md +2 -2
- package/skills/deploying-a-console/SKILL.md +3 -3
- package/skills/deploying-a-console/references/access.md +15 -1
- package/skills/deploying-a-console/references/resources.md +2 -2
- package/skills/deploying-a-console/scripts/github-app.mjs +593 -0
- package/skills/keeping-the-pack/SKILL.md +87 -0
- package/skills/planning-the-board/SKILL.md +6 -6
- package/skills/planning-the-board/references/a-task.md +1 -1
- package/skills/planning-the-board/references/setting-up.md +9 -5
- package/skills/running-an-agent/SKILL.md +21 -17
- package/skills/running-an-agent/references/preparing-the-machine.md +4 -3
- package/skills/running-an-agent/references/several-tasks.md +8 -2
- package/skills/running-an-agent/references/slack-app.md +31 -8
- package/skills/running-an-agent/references/the-account-and-the-invitation.md +1 -1
- package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +3 -1
- package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +4 -4
- package/skills/running-an-agent/references/your-first-task.md +11 -5
- package/skills/trying-a-console/SKILL.md +136 -0
- package/skills/trying-a-console/references/when-it-does-not-go.md +30 -0
- package/skills/working-the-board/SKILL.md +6 -6
- package/skills/working-the-board/references/terms.md +3 -1
- package/skills/working-the-board/references/the-pack.md +14 -7
- package/skills/working-the-board/references/what-they-were-told.md +1 -1
- package/src/agent-page.tsx +111 -4
- package/src/agent-state.ts +33 -0
- package/src/api.ts +43 -0
- package/src/cli/cli.ts +1190 -313
- package/src/cli/format.ts +207 -13
- package/src/cli/help.ts +991 -372
- package/src/cli/pack.ts +55 -62
- package/src/cli/plugin-cli.ts +8 -5
- package/src/cli/runner-files.ts +137 -0
- package/src/cli/runner-host.ts +48 -2
- package/src/cli/skill-path.ts +51 -0
- package/src/client.ts +103 -0
- package/src/components/agent-page.tsx +446 -20
- package/src/components/agents.tsx +27 -28
- package/src/components/people.tsx +7 -3
- package/src/components/picker.tsx +79 -5
- package/src/console.tsx +1 -1
- package/src/feed-words.ts +14 -1
- package/src/i18n/en.ts +64 -11
- package/src/i18n/ko.ts +55 -9
- package/src/limits.ts +26 -0
- package/src/onboarding.ts +60 -21
- package/src/plugin/args.ts +29 -3
- package/src/plugin/context.ts +40 -7
- package/src/plugin/define.ts +18 -3
- package/src/plugin/rest-schemas.ts +3 -0
- package/src/routes.ts +2 -0
- package/src/rules.ts +5 -2
- package/src/runner/acp.ts +9 -3
- package/src/runner/claude.ts +58 -1
- package/src/runner/codex.ts +18 -3
- package/src/runner/config.ts +114 -1
- package/src/runner/desk.ts +15 -4
- package/src/runner/message.ts +88 -126
- package/src/runner/runner.ts +682 -27
- package/src/runner/slack-status.ts +423 -0
- package/src/runner/slack.ts +310 -67
- package/src/runner/types.ts +124 -13
- package/src/runner/usage.ts +16 -3
- package/src/runner.ts +4 -0
- package/src/schemas.ts +245 -13
- package/src/secret-file.ts +76 -0
- package/src/services.ts +3 -2
- package/src/setup-guide.ts +6 -5
- package/src/styles/README.md +1 -1
- package/src/styles/pages/agents.css +40 -0
- package/src/vocabulary.ts +21 -2
- package/src/wake-text.ts +133 -0
- package/dist/cli/command-words.d.ts +0 -20
- package/dist/cli/command-words.d.ts.map +0 -1
- package/dist/cli/command-words.js +0 -51
- package/dist/cli/command-words.js.map +0 -1
- package/src/cli/command-words.ts +0 -56
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import * as z from "zod/mini";
|
|
2
|
+
import { en } from "../i18n/en.js";
|
|
3
|
+
import { ko } from "../i18n/ko.js";
|
|
4
|
+
import { withoutForbiddenCodePoints } from "../text.js";
|
|
5
|
+
import type { AgentStatus, WakeSource } from "./types.js";
|
|
6
|
+
|
|
7
|
+
// How the `slack` wake source shows an agent account's status (ADR-0170): the member's own
|
|
8
|
+
// status at the messenger, set with the member's own token, and taken back by the runner that
|
|
9
|
+
// set it and by no other. Written from the messenger's documentation of `users.profile.set`
|
|
10
|
+
// and from what a trial found of how often it may be called; whether a workspace takes it is
|
|
11
|
+
// seen in one.
|
|
12
|
+
|
|
13
|
+
const PROFILE_URL = "https://slack.com/api/users.profile.set";
|
|
14
|
+
/** The scope of the member's token that a status takes. */
|
|
15
|
+
const SLACK_STATUS_SCOPE = "users.profile:write";
|
|
16
|
+
|
|
17
|
+
type Host = Parameters<NonNullable<WakeSource["show"]>>[0];
|
|
18
|
+
type Options = Parameters<NonNullable<WakeSource["show"]>>[2];
|
|
19
|
+
|
|
20
|
+
/** The emoji of each state where the desk names none. */
|
|
21
|
+
const EMOJI = {
|
|
22
|
+
thinking: ":thought_balloon:",
|
|
23
|
+
working: ":hammer_and_wrench:",
|
|
24
|
+
limited: ":low_battery:",
|
|
25
|
+
} as const;
|
|
26
|
+
/** The most a status says at the messenger, in characters. */
|
|
27
|
+
const MAX_TEXT = 100;
|
|
28
|
+
/**
|
|
29
|
+
* How long a status stands at the messenger by itself, in seconds: a runner that is gone, with
|
|
30
|
+
* its machine say, leaves nothing there for longer. The runner says how the agent stands again
|
|
31
|
+
* every half minute, and a status with less than `RENEW_S` left is set again then: that is one
|
|
32
|
+
* call in a quarter of an hour for an agent at work that long.
|
|
33
|
+
*/
|
|
34
|
+
const THINKING_S = 2 * 60;
|
|
35
|
+
const WORKING_S = 15 * 60;
|
|
36
|
+
const LIMITED_S = 30 * 60;
|
|
37
|
+
const RENEW_S = 60;
|
|
38
|
+
/**
|
|
39
|
+
* How often the profile is written: so many calls at once, and one more every so long after
|
|
40
|
+
* that. The messenger takes ten a minute of one member's profile, and refused the fourth in a
|
|
41
|
+
* row where it was tried. What changed meanwhile is not sent: the latest is, when its turn comes.
|
|
42
|
+
*/
|
|
43
|
+
const BURST = 3;
|
|
44
|
+
const REFILL_MS = 8000;
|
|
45
|
+
/** How long the source waits where the messenger asked it to and said no number that reads, and the longest it waits for one that does. */
|
|
46
|
+
const RETRY_MS = 30_000;
|
|
47
|
+
const MAX_RETRY_MS = 10 * 60_000;
|
|
48
|
+
/** How often one sender looks again at what is to be shown before it leaves the rest to the next say. */
|
|
49
|
+
const MAX_TURNS = 20;
|
|
50
|
+
/** The name the source keeps under, from one runner to the next, until when a status of its own stands at the messenger: seconds since the epoch. */
|
|
51
|
+
export const KEPT_STATUS = "shown";
|
|
52
|
+
const KEPT_UNTIL = /^[0-9]{1,12}$/;
|
|
53
|
+
/** Whether what stands under that name is what the source keeps there. */
|
|
54
|
+
export const keptStatusFits = (value: string): boolean => KEPT_UNTIL.test(value);
|
|
55
|
+
/** What the messenger answers where nothing of the request is wrong: tried again when the runner says again. */
|
|
56
|
+
const PASSING = new Set([
|
|
57
|
+
"internal_error",
|
|
58
|
+
"fatal_error",
|
|
59
|
+
"service_unavailable",
|
|
60
|
+
"request_timeout",
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
const answerSchema = z.object({ ok: z.boolean(), error: z.optional(z.string()) });
|
|
64
|
+
|
|
65
|
+
/** A status as the messenger takes one: its emoji, its words, and when it ends by itself. */
|
|
66
|
+
interface Look {
|
|
67
|
+
readonly emoji: string;
|
|
68
|
+
readonly text: string;
|
|
69
|
+
/** Seconds since the epoch. */
|
|
70
|
+
readonly expires: number;
|
|
71
|
+
/** Whether that moment is one the status says, and not only how long it stands unless renewed. */
|
|
72
|
+
readonly fixed: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What the source holds of one host's status. */
|
|
76
|
+
interface Standing {
|
|
77
|
+
status: AgentStatus;
|
|
78
|
+
options: Options;
|
|
79
|
+
/** The status of the runner's own that stands at the messenger, as far as the source knows: nothing where it set none or took it back. */
|
|
80
|
+
set: Look | undefined;
|
|
81
|
+
/** How many times the runner said how the agent stands. */
|
|
82
|
+
asked: number;
|
|
83
|
+
sending: Promise<void> | undefined;
|
|
84
|
+
/** Ends the wait of the sender under way: something was said meanwhile. */
|
|
85
|
+
wake: AbortController | undefined;
|
|
86
|
+
/** The calls it may make at once, and since when the next is counted. */
|
|
87
|
+
calls: number;
|
|
88
|
+
countedFrom: number;
|
|
89
|
+
/** The moment the messenger asked not to be called before. */
|
|
90
|
+
notBefore: number;
|
|
91
|
+
/** The token the messenger refused a status with, and its word for why: not tried again with that token. */
|
|
92
|
+
refused: { readonly token: string; readonly why: string } | undefined;
|
|
93
|
+
/** Why no status is shown, for the agent: a line for the next wake, until it was carried. */
|
|
94
|
+
note: string | undefined;
|
|
95
|
+
/** What the runner's lines last said of it, so that each thing is said once. */
|
|
96
|
+
said: string | undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const standings = new WeakMap<object, Standing>();
|
|
100
|
+
|
|
101
|
+
function standingOf(host: Host, status: AgentStatus, options: Options): Standing {
|
|
102
|
+
const before = standings.get(host);
|
|
103
|
+
if (before !== undefined) {
|
|
104
|
+
before.status = status;
|
|
105
|
+
before.options = options;
|
|
106
|
+
return before;
|
|
107
|
+
}
|
|
108
|
+
// What a runner before this one showed and never took back, ended as it was by its machine or
|
|
109
|
+
// by force: the source's own still, until the moment it ends by itself.
|
|
110
|
+
const kept = host.kept.get(KEPT_STATUS);
|
|
111
|
+
const standing: Standing = {
|
|
112
|
+
status,
|
|
113
|
+
options,
|
|
114
|
+
set:
|
|
115
|
+
kept !== undefined && keptStatusFits(kept)
|
|
116
|
+
? { emoji: "", text: "", expires: Number(kept), fixed: false }
|
|
117
|
+
: undefined,
|
|
118
|
+
asked: 0,
|
|
119
|
+
sending: undefined,
|
|
120
|
+
wake: undefined,
|
|
121
|
+
calls: BURST,
|
|
122
|
+
countedFrom: options.now(),
|
|
123
|
+
notBefore: 0,
|
|
124
|
+
refused: undefined,
|
|
125
|
+
note: undefined,
|
|
126
|
+
said: undefined,
|
|
127
|
+
};
|
|
128
|
+
standings.set(host, standing);
|
|
129
|
+
return standing;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The source's own words for each state, in the workspace's language where the package speaks it. */
|
|
133
|
+
const wordsOf = (language: string | undefined) =>
|
|
134
|
+
(language ?? "").toLowerCase().split("-")[0] === "ko" ? ko.agents.status : en.agents.status;
|
|
135
|
+
|
|
136
|
+
/** Words as a status holds them: plain, one line, within what the messenger shows. */
|
|
137
|
+
const plain = (text: string): string =>
|
|
138
|
+
[...withoutForbiddenCodePoints(text)].slice(0, MAX_TEXT).join("").trim();
|
|
139
|
+
|
|
140
|
+
/** How a state is shown at the messenger, or nothing for an agent with nothing to show. */
|
|
141
|
+
function lookOf(status: AgentStatus, options: Options, now: number): Look | undefined {
|
|
142
|
+
if (status.state === "idle") {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
const own = wordsOf(options.language);
|
|
146
|
+
const seconds = Math.floor(now / 1000);
|
|
147
|
+
if (status.state !== "limited") {
|
|
148
|
+
const said = options.words[status.state];
|
|
149
|
+
return {
|
|
150
|
+
emoji: said?.emoji ?? EMOJI[status.state],
|
|
151
|
+
text: plain(said?.text ?? own[status.state]),
|
|
152
|
+
expires: seconds + (status.state === "thinking" ? THINKING_S : WORKING_S),
|
|
153
|
+
fixed: false,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const said = options.words.limited;
|
|
157
|
+
const emoji = said?.emoji ?? EMOJI.limited;
|
|
158
|
+
const text = plain(said?.text ?? own.limited);
|
|
159
|
+
if (status.until === undefined || status.until <= now) {
|
|
160
|
+
return { emoji, text, expires: seconds + LIMITED_S, fixed: false };
|
|
161
|
+
}
|
|
162
|
+
// Until the moment it is back, which the status says and ends at by itself. Where the two
|
|
163
|
+
// together are more than a status holds, the moment is left to the messenger to show.
|
|
164
|
+
const back = withoutForbiddenCodePoints(
|
|
165
|
+
(said?.back ?? own.back).replace("{time}", options.time(status.until)),
|
|
166
|
+
).trim();
|
|
167
|
+
const whole = `${text} · ${back}`;
|
|
168
|
+
return {
|
|
169
|
+
emoji,
|
|
170
|
+
text: [...whole].length <= MAX_TEXT ? whole : text,
|
|
171
|
+
expires: Math.ceil(status.until / 1000),
|
|
172
|
+
fixed: true,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Whether what stands at the messenger is what is to be shown, with long enough left of it. */
|
|
177
|
+
const shownAs = (set: Look | undefined, look: Look, seconds: number): boolean =>
|
|
178
|
+
set !== undefined &&
|
|
179
|
+
set.emoji === look.emoji &&
|
|
180
|
+
set.text === look.text &&
|
|
181
|
+
(look.fixed ? set.expires === look.expires : set.expires - seconds > RENEW_S);
|
|
182
|
+
|
|
183
|
+
/** Says a thing of the status in the runner's lines, once. */
|
|
184
|
+
function sayOnce(host: Host, standing: Standing, line: string): void {
|
|
185
|
+
if (standing.said !== line) {
|
|
186
|
+
standing.said = line;
|
|
187
|
+
host.log(line);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* How long the next call waits, in milliseconds: until the messenger takes one again where it
|
|
193
|
+
* asked to be left alone, and until the next of the calls the source allows itself. The last
|
|
194
|
+
* thing a runner says waits for neither count, and is not sent at all where the messenger asked
|
|
195
|
+
* for more time than a runner that ends has: nothing is answered then.
|
|
196
|
+
*/
|
|
197
|
+
function waitOf(standing: Standing, now: number, last: boolean): number | undefined {
|
|
198
|
+
if (now < standing.notBefore) {
|
|
199
|
+
return last ? undefined : standing.notBefore - now;
|
|
200
|
+
}
|
|
201
|
+
if (last) {
|
|
202
|
+
return 0;
|
|
203
|
+
}
|
|
204
|
+
const gained = Math.floor((now - standing.countedFrom) / REFILL_MS);
|
|
205
|
+
if (gained > 0) {
|
|
206
|
+
standing.calls = Math.min(BURST, standing.calls + gained);
|
|
207
|
+
standing.countedFrom =
|
|
208
|
+
standing.calls === BURST ? now : standing.countedFrom + gained * REFILL_MS;
|
|
209
|
+
}
|
|
210
|
+
return standing.calls > 0 ? 0 : standing.countedFrom + REFILL_MS - now;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Waits so long, or until something else is said or the runner ends. */
|
|
214
|
+
async function pause(host: Host, standing: Standing, ms: number): Promise<void> {
|
|
215
|
+
const wake = new AbortController();
|
|
216
|
+
const ended = (): void => wake.abort();
|
|
217
|
+
const { signal } = standing.options;
|
|
218
|
+
signal.addEventListener("abort", ended, { once: true });
|
|
219
|
+
standing.wake = wake;
|
|
220
|
+
try {
|
|
221
|
+
await host.wait(ms, wake.signal);
|
|
222
|
+
} finally {
|
|
223
|
+
signal.removeEventListener("abort", ended);
|
|
224
|
+
if (standing.wake === wake) {
|
|
225
|
+
standing.wake = undefined;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** How long the messenger asks to be left alone, as its header says in seconds. */
|
|
231
|
+
function retryOf(header: string | null | undefined): number {
|
|
232
|
+
const seconds =
|
|
233
|
+
header !== null && header !== undefined && /^[0-9]{1,6}$/.test(header.trim())
|
|
234
|
+
? Number(header.trim())
|
|
235
|
+
: undefined;
|
|
236
|
+
return seconds === undefined ? RETRY_MS : Math.min(MAX_RETRY_MS, Math.max(1000, seconds * 1000));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** A short name of why the messenger refused, as it spells its errors; `refused` for anything else. */
|
|
240
|
+
const whyOf = (error: string | undefined): string =>
|
|
241
|
+
error !== undefined && /^[a-z_]{1,60}$/.test(error) ? error : "refused";
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Writes the member's status, or takes it back where there is none to show: one call, with the
|
|
245
|
+
* member's own token in the `authorization` header. What came of it: taken, to be asked again
|
|
246
|
+
* after a while, refused for a reason that stands, or failed in passing.
|
|
247
|
+
*/
|
|
248
|
+
async function write(
|
|
249
|
+
host: Host,
|
|
250
|
+
token: string,
|
|
251
|
+
look: Look | undefined,
|
|
252
|
+
signal: AbortSignal,
|
|
253
|
+
): Promise<
|
|
254
|
+
| { readonly how: "taken" }
|
|
255
|
+
| { readonly how: "later"; readonly ms: number }
|
|
256
|
+
| { readonly how: "refused"; readonly why: string }
|
|
257
|
+
| { readonly how: "failed" }
|
|
258
|
+
> {
|
|
259
|
+
try {
|
|
260
|
+
const answered = await host.fetch(PROFILE_URL, {
|
|
261
|
+
method: "POST",
|
|
262
|
+
headers: {
|
|
263
|
+
authorization: `Bearer ${token}`,
|
|
264
|
+
"content-type": "application/json; charset=utf-8",
|
|
265
|
+
},
|
|
266
|
+
body: JSON.stringify({
|
|
267
|
+
profile: {
|
|
268
|
+
status_text: look?.text ?? "",
|
|
269
|
+
status_emoji: look?.emoji ?? "",
|
|
270
|
+
status_expiration: look?.expires ?? 0,
|
|
271
|
+
},
|
|
272
|
+
}),
|
|
273
|
+
signal,
|
|
274
|
+
});
|
|
275
|
+
const said = answerSchema.safeParse(await answered.json().catch(() => undefined));
|
|
276
|
+
if (answered.status === 429 || (said.success && said.data.error === "ratelimited")) {
|
|
277
|
+
return { how: "later", ms: retryOf(answered.headers?.get("retry-after")) };
|
|
278
|
+
}
|
|
279
|
+
if (!said.success || (!said.data.ok && PASSING.has(said.data.error ?? ""))) {
|
|
280
|
+
return { how: "failed" };
|
|
281
|
+
}
|
|
282
|
+
return said.data.ok ? { how: "taken" } : { how: "refused", why: whyOf(said.data.error) };
|
|
283
|
+
} catch {
|
|
284
|
+
return { how: "failed" };
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** What the agent is told where the messenger refuses its status, in a line of the source's own. */
|
|
289
|
+
const noteOf = (why: string): string =>
|
|
290
|
+
why === "missing_scope"
|
|
291
|
+
? `Your status at Slack is not shown while you work (missing_scope): your app lacks the user scope ${SLACK_STATUS_SCOPE}. The skill running-an-agent says how to add it (references/slack-app.md); "status": false in runner.json shows none`
|
|
292
|
+
: `Your status at Slack is not shown while you work (${why}); "status": false in runner.json shows none`;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Brings what stands at the messenger to what the runner said last, a call at a time, until
|
|
296
|
+
* the two are the same or nothing more can be done about it now.
|
|
297
|
+
*/
|
|
298
|
+
async function send(host: Host, secret: string, standing: Standing): Promise<void> {
|
|
299
|
+
for (let turn = 0; turn < MAX_TURNS; turn += 1) {
|
|
300
|
+
const { status, options } = standing;
|
|
301
|
+
if (options.signal.aborted) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const now = options.now();
|
|
305
|
+
const seconds = Math.floor(now / 1000);
|
|
306
|
+
// What stood until a moment that is past is there no more: the messenger took it back.
|
|
307
|
+
if (standing.set !== undefined && standing.set.expires <= seconds) {
|
|
308
|
+
standing.set = undefined;
|
|
309
|
+
host.kept.delete(KEPT_STATUS);
|
|
310
|
+
}
|
|
311
|
+
const look = lookOf(status, options, now);
|
|
312
|
+
// Nothing to show and nothing of its own there: whatever stands is somebody else's, and
|
|
313
|
+
// is left as it is. No profile is read to find out.
|
|
314
|
+
if (look === undefined ? standing.set === undefined : shownAs(standing.set, look, seconds)) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const token = await host.secret(secret);
|
|
318
|
+
if (token === undefined) {
|
|
319
|
+
sayOnce(
|
|
320
|
+
host,
|
|
321
|
+
standing,
|
|
322
|
+
`the slack wake source shows no status: no ${secret} in the account's store`,
|
|
323
|
+
);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
if (standing.refused !== undefined) {
|
|
327
|
+
if (standing.refused.token === token) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
// Another token, of an app installed again say: asked once more.
|
|
331
|
+
standing.refused = undefined;
|
|
332
|
+
}
|
|
333
|
+
const last = status.state === "idle" && status.last === true;
|
|
334
|
+
const wait = waitOf(standing, now, last);
|
|
335
|
+
if (wait === undefined) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (wait > 0) {
|
|
339
|
+
await pause(host, standing, wait);
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (standing.calls === BURST) {
|
|
343
|
+
standing.countedFrom = now;
|
|
344
|
+
}
|
|
345
|
+
standing.calls = Math.max(0, standing.calls - 1);
|
|
346
|
+
const answer = await write(host, token, look, options.signal);
|
|
347
|
+
if (answer.how === "taken") {
|
|
348
|
+
standing.set = look;
|
|
349
|
+
host.kept.delete(KEPT_STATUS);
|
|
350
|
+
if (look !== undefined) {
|
|
351
|
+
host.kept.set(KEPT_STATUS, String(look.expires));
|
|
352
|
+
}
|
|
353
|
+
// It is shown: whatever kept it from being so is over, and the agent is told nothing of it.
|
|
354
|
+
standing.note = undefined;
|
|
355
|
+
if (standing.said !== undefined) {
|
|
356
|
+
standing.said = undefined;
|
|
357
|
+
host.log("the slack wake source shows the agent's status again");
|
|
358
|
+
}
|
|
359
|
+
// What is to be shown may have changed while the messenger answered.
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (answer.how === "later") {
|
|
363
|
+
standing.notBefore = options.now() + answer.ms;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (answer.how === "refused") {
|
|
367
|
+
standing.refused = { token, why: answer.why };
|
|
368
|
+
standing.note = noteOf(answer.why);
|
|
369
|
+
sayOnce(
|
|
370
|
+
host,
|
|
371
|
+
standing,
|
|
372
|
+
`the slack wake source shows no status: the messenger refused it (${answer.why})${answer.why === "missing_scope" ? `, the member's token lacks the scope ${SLACK_STATUS_SCOPE}` : ""}; not tried again until the runner starts again or the token changes`,
|
|
373
|
+
);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
// Tried again when the runner next says how the agent stands, half a minute on at the most.
|
|
377
|
+
sayOnce(
|
|
378
|
+
host,
|
|
379
|
+
standing,
|
|
380
|
+
"the slack wake source could not write the agent's status (failed); it tries again",
|
|
381
|
+
);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Shows how the agent stands as the status of its own member at Slack (ADR-0170), with the
|
|
388
|
+
* token the account keeps under `secret`. One sender at a time brings the messenger to the
|
|
389
|
+
* latest thing said, so that what changed twice while it waited is written once.
|
|
390
|
+
*/
|
|
391
|
+
export async function showSlackStatus(
|
|
392
|
+
host: Host,
|
|
393
|
+
secret: string,
|
|
394
|
+
status: AgentStatus,
|
|
395
|
+
options: Options,
|
|
396
|
+
): Promise<void> {
|
|
397
|
+
const standing = standingOf(host, status, options);
|
|
398
|
+
standing.asked += 1;
|
|
399
|
+
standing.wake?.abort();
|
|
400
|
+
standing.sending ??= (async () => {
|
|
401
|
+
try {
|
|
402
|
+
// What was said while the sender was on its way out is sent by it still.
|
|
403
|
+
for (let asked = -1; asked !== standing.asked; ) {
|
|
404
|
+
asked = standing.asked;
|
|
405
|
+
await send(host, secret, standing);
|
|
406
|
+
}
|
|
407
|
+
} finally {
|
|
408
|
+
standing.sending = undefined;
|
|
409
|
+
}
|
|
410
|
+
})();
|
|
411
|
+
await standing.sending;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Why no status is shown, as a line for the agent that no wake carried yet; nothing where there is none to say. */
|
|
415
|
+
export const slackStatusNote = (host: object): string | undefined => standings.get(host)?.note;
|
|
416
|
+
|
|
417
|
+
/** The line was carried by a wake: it is said no more. */
|
|
418
|
+
export function slackStatusNoted(host: object): void {
|
|
419
|
+
const standing = standings.get(host);
|
|
420
|
+
if (standing !== undefined) {
|
|
421
|
+
standing.note = undefined;
|
|
422
|
+
}
|
|
423
|
+
}
|