@skillcdn/console 0.6.1 → 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.
- package/README.md +14 -7
- 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 +77 -30
- 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/agent-usage.d.ts +9 -6
- package/dist/agent-usage.d.ts.map +1 -1
- package/dist/agent-usage.js +38 -8
- package/dist/agent-usage.js.map +1 -1
- package/dist/api.d.ts +4 -3
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +4 -3
- 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 +369 -130
- package/dist/cli/cli.js.map +1 -1
- package/dist/cli/format.d.ts +13 -4
- package/dist/cli/format.d.ts.map +1 -1
- package/dist/cli/format.js +43 -9
- 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 +780 -318
- package/dist/cli/help.js.map +1 -1
- package/dist/cli/pack.d.ts +2 -2
- package/dist/cli/pack.d.ts.map +1 -1
- package/dist/cli/pack.js +6 -4
- package/dist/cli/pack.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 +110 -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/client.d.ts +22 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +7 -1
- package/dist/client.js.map +1 -1
- package/dist/components/agent-page.d.ts +61 -11
- package/dist/components/agent-page.d.ts.map +1 -1
- package/dist/components/agent-page.js +146 -35
- package/dist/components/agent-page.js.map +1 -1
- package/dist/components/agents.d.ts +3 -2
- package/dist/components/agents.d.ts.map +1 -1
- package/dist/components/agents.js +9 -3
- package/dist/components/agents.js.map +1 -1
- package/dist/components/calendar.d.ts +24 -4
- package/dist/components/calendar.d.ts.map +1 -1
- package/dist/components/calendar.js +78 -35
- package/dist/components/calendar.js.map +1 -1
- package/dist/console.d.ts.map +1 -1
- package/dist/console.js +40 -12
- package/dist/console.js.map +1 -1
- package/dist/data.d.ts +2 -0
- package/dist/data.d.ts.map +1 -1
- package/dist/data.js +4 -0
- package/dist/data.js.map +1 -1
- package/dist/desk-page.js +5 -5
- package/dist/desk-page.js.map +1 -1
- package/dist/feed-words.d.ts.map +1 -1
- package/dist/feed-words.js +18 -1
- package/dist/feed-words.js.map +1 -1
- package/dist/i18n/en.d.ts +51 -1
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +44 -2
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/ko.d.ts.map +1 -1
- package/dist/i18n/ko.js +52 -17
- package/dist/i18n/ko.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/limits.d.ts +10 -0
- package/dist/limits.d.ts.map +1 -1
- package/dist/limits.js +10 -0
- package/dist/limits.js.map +1 -1
- package/dist/plugin/capabilities.d.ts +1 -1
- package/dist/plugin/capabilities.d.ts.map +1 -1
- package/dist/plugin/capabilities.js +6 -1
- package/dist/plugin/capabilities.js.map +1 -1
- package/dist/plugin/context.d.ts +101 -8
- package/dist/plugin/context.d.ts.map +1 -1
- package/dist/plugin/limits.d.ts +7 -0
- package/dist/plugin/limits.d.ts.map +1 -1
- package/dist/plugin/limits.js +7 -0
- package/dist/plugin/limits.js.map +1 -1
- package/dist/plugin/testing.d.ts +2 -0
- package/dist/plugin/testing.d.ts.map +1 -1
- package/dist/plugin/testing.js +2 -0
- package/dist/plugin/testing.js.map +1 -1
- package/dist/rules.js +1 -1
- package/dist/rules.js.map +1 -1
- package/dist/runner/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 +143 -10
- package/dist/runner/claude.js.map +1 -1
- package/dist/runner/codex.d.ts.map +1 -1
- package/dist/runner/codex.js +14 -2
- 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 +20 -4
- package/dist/runner/desk.js.map +1 -1
- package/dist/runner/message.d.ts +26 -2
- package/dist/runner/message.d.ts.map +1 -1
- package/dist/runner/message.js +120 -32
- package/dist/runner/message.js.map +1 -1
- package/dist/runner/runner.d.ts +40 -2
- package/dist/runner/runner.d.ts.map +1 -1
- package/dist/runner/runner.js +533 -32
- 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 +17 -11
- package/dist/runner/slack.d.ts.map +1 -1
- package/dist/runner/slack.js +1199 -109
- package/dist/runner/slack.js.map +1 -1
- package/dist/runner/types.d.ts +205 -6
- 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 +4 -4
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +3 -3
- package/dist/runner.js.map +1 -1
- package/dist/schemas.d.ts +263 -1
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +137 -5
- package/dist/schemas.js.map +1 -1
- package/dist/vocabulary.d.ts +7 -0
- package/dist/vocabulary.d.ts.map +1 -1
- package/dist/vocabulary.js +6 -0
- 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 +21 -12
- package/docs/specs/plugins.md +12 -7
- package/docs/specs/rest.md +14 -5
- package/docs/specs/runner.md +133 -26
- package/docs/start.md +26 -5
- package/package.json +3 -3
- package/skills/building-a-plugin/SKILL.md +3 -3
- 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/planning-the-board/references/setting-up.md +1 -1
- package/skills/running-an-agent/SKILL.md +18 -72
- package/skills/running-an-agent/references/preparing-the-machine.md +17 -0
- package/skills/running-an-agent/references/several-tasks.md +1 -1
- package/skills/running-an-agent/references/slack-app.md +55 -8
- package/skills/running-an-agent/references/the-account-and-the-invitation.md +5 -0
- package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +3 -0
- package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +16 -0
- package/skills/running-an-agent/references/your-first-task.md +19 -0
- 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 +11 -15
- package/skills/working-the-board/references/a-pack-skills-questions.md +3 -0
- package/skills/working-the-board/references/a-plugins-commands.md +3 -0
- package/skills/working-the-board/references/a-review.md +3 -0
- package/skills/working-the-board/references/an-open-run.md +3 -0
- package/skills/working-the-board/references/requirements.md +7 -0
- package/skills/working-the-board/references/the-pack.md +3 -3
- package/skills/working-the-board/references/what-they-were-told.md +3 -0
- package/skills/working-the-board/references/work-given-in-chat.md +5 -0
- package/src/agent-page.tsx +133 -41
- package/src/agent-state.ts +33 -0
- package/src/agent-usage.ts +55 -12
- package/src/api.ts +23 -0
- package/src/cli/cli.ts +443 -137
- package/src/cli/format.ts +49 -9
- package/src/cli/help.ts +904 -359
- package/src/cli/pack.ts +6 -4
- package/src/cli/runner-files.ts +120 -0
- package/src/cli/runner-host.ts +48 -2
- package/src/client.ts +64 -0
- package/src/components/agent-page.tsx +264 -39
- package/src/components/agents.tsx +20 -5
- package/src/components/calendar.tsx +92 -36
- package/src/console.tsx +47 -10
- package/src/data.ts +6 -0
- package/src/desk-page.tsx +5 -5
- package/src/feed-words.ts +20 -1
- package/src/i18n/en.ts +50 -2
- package/src/i18n/ko.ts +62 -17
- package/src/index.ts +2 -0
- package/src/limits.ts +10 -0
- package/src/plugin/capabilities.ts +8 -1
- package/src/plugin/context.ts +103 -7
- package/src/plugin/limits.ts +7 -0
- package/src/plugin/testing.ts +4 -0
- package/src/rules.ts +1 -1
- package/src/runner/acp.ts +9 -3
- package/src/runner/claude.ts +159 -10
- package/src/runner/codex.ts +14 -2
- package/src/runner/config.ts +114 -1
- package/src/runner/desk.ts +23 -4
- package/src/runner/message.ts +144 -35
- package/src/runner/runner.ts +658 -35
- package/src/runner/slack-status.ts +423 -0
- package/src/runner/slack.ts +1509 -128
- package/src/runner/types.ts +200 -5
- package/src/runner/usage.ts +16 -3
- package/src/runner.ts +20 -1
- package/src/schemas.ts +161 -3
- package/src/vocabulary.ts +9 -0
- package/src/wake-text.ts +133 -0
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
// Registers the GitHub App a console signs people in with, from a manifest (ADR-0171): a person
|
|
2
|
+
// presses "Create GitHub App" once, and what GitHub hands back, the client id, the client secret
|
|
3
|
+
// and, for an organization, the app's private key, goes where the installation keeps it without
|
|
4
|
+
// passing through a command line, a chat or a terminal.
|
|
5
|
+
//
|
|
6
|
+
// node github-app.mjs --url <the console's origin> [--org <login>] [--name <name>]
|
|
7
|
+
// [--port <port>] [--host <address>] [--wait <seconds>]
|
|
8
|
+
// [--web-url <origin>] [--api-url <url>] [--secrets <file>]
|
|
9
|
+
//
|
|
10
|
+
// It listens on this machine, at http://127.0.0.1:11196/ unless --port says otherwise. That page
|
|
11
|
+
// sends the browser to GitHub with the app's settings; GitHub sends it back here with a code,
|
|
12
|
+
// which is exchanged once. Then:
|
|
13
|
+
// - by default, lines of an env file on standard output, for a script to take: install.sh of
|
|
14
|
+
// the smallest install does (deploy/README.md, "Installing with the script");
|
|
15
|
+
// - with --secrets <file>, the secrets merged into that JSON file (the secrets file of the
|
|
16
|
+
// skill deploying-a-console, readable by its owner alone) and only what is no secret printed.
|
|
17
|
+
// Everything said to a person is on standard error. --org registers the app under an
|
|
18
|
+
// organization, with the one permission that lets the console read who is a member. --web-url
|
|
19
|
+
// and --api-url name a GitHub Enterprise Server. Exit: 0 registered, 1 not, 2 not understood.
|
|
20
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
21
|
+
import { chmodSync, existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
22
|
+
import { createServer } from "node:http";
|
|
23
|
+
import { resolve } from "node:path";
|
|
24
|
+
import process from "node:process";
|
|
25
|
+
import { fileURLToPath } from "node:url";
|
|
26
|
+
|
|
27
|
+
/** The port the page is served at unless another is named: beside the console's own 11199. */
|
|
28
|
+
export const DEFAULT_PORT = 11196;
|
|
29
|
+
/** How long the press is waited for, in seconds, unless said. */
|
|
30
|
+
export const DEFAULT_WAIT_SECONDS = 600;
|
|
31
|
+
/** What GitHub may answer the exchange with: an app's settings and one key are a few kilobytes. */
|
|
32
|
+
export const MAX_ANSWER_BYTES = 64 * 1024;
|
|
33
|
+
/** The longest name GitHub takes for an app. */
|
|
34
|
+
export const MAX_NAME_LENGTH = 34;
|
|
35
|
+
const EXCHANGE_TIMEOUT_MS = 15_000;
|
|
36
|
+
const API_VERSION = "2022-11-28";
|
|
37
|
+
|
|
38
|
+
/** What went wrong, in a sentence for whoever ran the script; `usage` where the arguments were. */
|
|
39
|
+
export class RegistrarError extends Error {
|
|
40
|
+
constructor(message, usage = false) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = "RegistrarError";
|
|
43
|
+
this.usage = usage;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const LOGIN = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
|
|
48
|
+
/** A character that does not show, or ends a line: none belongs in a name or a value. */
|
|
49
|
+
const hasControl = (value) =>
|
|
50
|
+
[...value].some((character) => {
|
|
51
|
+
const code = character.codePointAt(0) ?? 0;
|
|
52
|
+
return code < 0x20 || (code >= 0x7f && code <= 0x9f) || code === 0x2028 || code === 0x2029;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/** An origin as the console's own configuration reads one: a scheme and a host, nothing after. */
|
|
56
|
+
function originOf(value, name) {
|
|
57
|
+
let url;
|
|
58
|
+
try {
|
|
59
|
+
url = new URL(value);
|
|
60
|
+
} catch {
|
|
61
|
+
throw new RegistrarError(`${name} is an origin such as https://console.example.com`, true);
|
|
62
|
+
}
|
|
63
|
+
if (
|
|
64
|
+
(url.protocol !== "https:" && url.protocol !== "http:") ||
|
|
65
|
+
url.pathname !== "/" ||
|
|
66
|
+
url.search !== "" ||
|
|
67
|
+
url.hash !== "" ||
|
|
68
|
+
url.username !== "" ||
|
|
69
|
+
url.password !== ""
|
|
70
|
+
) {
|
|
71
|
+
throw new RegistrarError(
|
|
72
|
+
`${name} is an origin such as https://console.example.com, without a path`,
|
|
73
|
+
true,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return url.origin;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The address of an API, which may carry a path (`https://<host>/api/v3`), without its last slash. */
|
|
80
|
+
function apiOf(value) {
|
|
81
|
+
let url;
|
|
82
|
+
try {
|
|
83
|
+
url = new URL(value);
|
|
84
|
+
} catch {
|
|
85
|
+
throw new RegistrarError("--api-url is an address such as https://api.github.com", true);
|
|
86
|
+
}
|
|
87
|
+
if (
|
|
88
|
+
(url.protocol !== "https:" && url.protocol !== "http:") ||
|
|
89
|
+
url.search !== "" ||
|
|
90
|
+
url.hash !== "" ||
|
|
91
|
+
url.username !== "" ||
|
|
92
|
+
url.password !== ""
|
|
93
|
+
) {
|
|
94
|
+
throw new RegistrarError("--api-url is an address such as https://api.github.com", true);
|
|
95
|
+
}
|
|
96
|
+
return `${url.origin}${url.pathname}`.replace(/\/+$/, "");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function integerOf(value, name, min, max) {
|
|
100
|
+
if (!/^\d{1,6}$/.test(value)) {
|
|
101
|
+
throw new RegistrarError(`${name} takes a number between ${min} and ${max}`, true);
|
|
102
|
+
}
|
|
103
|
+
const number = Number(value);
|
|
104
|
+
if (number < min || number > max) {
|
|
105
|
+
throw new RegistrarError(`${name} takes a number between ${min} and ${max}`, true);
|
|
106
|
+
}
|
|
107
|
+
return number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const USAGE =
|
|
111
|
+
"usage: node github-app.mjs --url <the console's origin> [--org <login>] [--name <name>] [--port <port>] [--host <address>] [--wait <seconds>] [--web-url <origin>] [--api-url <url>] [--secrets <file>]";
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The arguments as options, each held to its form. No secret is among them, and none is ever
|
|
115
|
+
* taken: what is secret is GitHub's to make and this script's to put away.
|
|
116
|
+
*/
|
|
117
|
+
export function parseArguments(argv) {
|
|
118
|
+
const given = {};
|
|
119
|
+
const names = new Set([
|
|
120
|
+
"url",
|
|
121
|
+
"org",
|
|
122
|
+
"name",
|
|
123
|
+
"port",
|
|
124
|
+
"host",
|
|
125
|
+
"wait",
|
|
126
|
+
"web-url",
|
|
127
|
+
"api-url",
|
|
128
|
+
"secrets",
|
|
129
|
+
]);
|
|
130
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
131
|
+
const word = argv[index] ?? "";
|
|
132
|
+
const name = word.startsWith("--") ? word.slice(2) : undefined;
|
|
133
|
+
if (name === undefined || !names.has(name)) {
|
|
134
|
+
throw new RegistrarError(`not an option: ${word}\n${USAGE}`, true);
|
|
135
|
+
}
|
|
136
|
+
const value = argv[index + 1];
|
|
137
|
+
if (value === undefined || value.startsWith("--")) {
|
|
138
|
+
throw new RegistrarError(`--${name} takes a value\n${USAGE}`, true);
|
|
139
|
+
}
|
|
140
|
+
if (name in given) {
|
|
141
|
+
throw new RegistrarError(`--${name} is said once`, true);
|
|
142
|
+
}
|
|
143
|
+
given[name] = value;
|
|
144
|
+
index += 1;
|
|
145
|
+
}
|
|
146
|
+
if (given.url === undefined) {
|
|
147
|
+
throw new RegistrarError(`--url names the console's origin\n${USAGE}`, true);
|
|
148
|
+
}
|
|
149
|
+
const org = given.org;
|
|
150
|
+
if (org !== undefined && !LOGIN.test(org)) {
|
|
151
|
+
throw new RegistrarError("--org is the organization's login at GitHub", true);
|
|
152
|
+
}
|
|
153
|
+
const name = given.name;
|
|
154
|
+
if (name !== undefined) {
|
|
155
|
+
if (name.trim().length === 0 || name.length > MAX_NAME_LENGTH || hasControl(name)) {
|
|
156
|
+
throw new RegistrarError(
|
|
157
|
+
`--name is the app's name, ${MAX_NAME_LENGTH} characters at the most`,
|
|
158
|
+
true,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const host = given.host ?? "127.0.0.1";
|
|
163
|
+
if (!/^[0-9a-fA-F.:]{2,45}$/.test(host)) {
|
|
164
|
+
throw new RegistrarError("--host is an address of this machine, 127.0.0.1 unless said", true);
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
url: originOf(given.url, "--url"),
|
|
168
|
+
org,
|
|
169
|
+
name,
|
|
170
|
+
port: given.port === undefined ? DEFAULT_PORT : integerOf(given.port, "--port", 0, 65_535),
|
|
171
|
+
host,
|
|
172
|
+
waitSeconds:
|
|
173
|
+
given.wait === undefined ? DEFAULT_WAIT_SECONDS : integerOf(given.wait, "--wait", 1, 3600),
|
|
174
|
+
webUrl: originOf(given["web-url"] ?? "https://github.com", "--web-url"),
|
|
175
|
+
apiUrl: apiOf(given["api-url"] ?? "https://api.github.com"),
|
|
176
|
+
secrets: given.secrets,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The app's settings as GitHub reads a manifest: a homepage, where GitHub sends the browser
|
|
182
|
+
* back with the code, the console's callback for signing in, no webhook and no event, and no
|
|
183
|
+
* permission but, for an organization, reading who is a member. `redirect` is this script's own
|
|
184
|
+
* address for the code.
|
|
185
|
+
*/
|
|
186
|
+
export function manifestOf({ url, org, name, redirect }) {
|
|
187
|
+
return {
|
|
188
|
+
name,
|
|
189
|
+
url,
|
|
190
|
+
description: `Signs people in to the SkillCDN Console at ${url}.`,
|
|
191
|
+
redirect_url: redirect,
|
|
192
|
+
callback_urls: [`${url}/auth/gh/callback`],
|
|
193
|
+
public: false,
|
|
194
|
+
default_permissions: org === undefined ? {} : { members: "read" },
|
|
195
|
+
default_events: [],
|
|
196
|
+
request_oauth_on_install: false,
|
|
197
|
+
setup_on_update: false,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Where the browser posts the manifest: the account's own apps, or an organization's. */
|
|
202
|
+
export function registrationUrl({ webUrl, org, state }) {
|
|
203
|
+
const path = org === undefined ? "/settings/apps/new" : `/organizations/${org}/settings/apps/new`;
|
|
204
|
+
return `${webUrl}${path}?state=${encodeURIComponent(state)}`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const escapeHtml = (value) =>
|
|
208
|
+
value
|
|
209
|
+
.replaceAll("&", "&")
|
|
210
|
+
.replaceAll("<", "<")
|
|
211
|
+
.replaceAll(">", ">")
|
|
212
|
+
.replaceAll('"', """)
|
|
213
|
+
.replaceAll("'", "'");
|
|
214
|
+
|
|
215
|
+
const page = (title, body) =>
|
|
216
|
+
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>${escapeHtml(title)}</title><style>body{font:16px/1.5 system-ui,sans-serif;max-width:40rem;margin:3rem auto;padding:0 1rem}button{font:inherit;padding:.5rem 1rem}</style></head><body><h1>${escapeHtml(title)}</h1>${body}</body></html>`;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* What GitHub answered the exchange with, held to the fields that are read and to the form each
|
|
220
|
+
* must have to be written into a settings file: anything else is refused as a whole. A value
|
|
221
|
+
* that held a line break, a quote or a `$` would be read as something else there.
|
|
222
|
+
*/
|
|
223
|
+
export function parseConversion(body, { webUrl }) {
|
|
224
|
+
const refuse = () =>
|
|
225
|
+
new RegistrarError(
|
|
226
|
+
"GitHub's answer was not an app's settings as this script reads them; nothing was written.",
|
|
227
|
+
);
|
|
228
|
+
let value;
|
|
229
|
+
try {
|
|
230
|
+
value = JSON.parse(body);
|
|
231
|
+
} catch {
|
|
232
|
+
throw refuse();
|
|
233
|
+
}
|
|
234
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
235
|
+
throw refuse();
|
|
236
|
+
}
|
|
237
|
+
const text = (field, pattern) =>
|
|
238
|
+
typeof field === "string" && pattern.test(field) ? field : undefined;
|
|
239
|
+
const id = Number.isSafeInteger(value.id) && value.id > 0 ? String(value.id) : undefined;
|
|
240
|
+
const clientId = text(value.client_id, /^[A-Za-z0-9._-]{1,64}$/);
|
|
241
|
+
const clientSecret = text(value.client_secret, /^[A-Za-z0-9_-]{16,200}$/);
|
|
242
|
+
const pem =
|
|
243
|
+
typeof value.pem === "string" && value.pem.length <= 16_384
|
|
244
|
+
? text(
|
|
245
|
+
value.pem.replaceAll("\r\n", "\n"),
|
|
246
|
+
/^-----BEGIN (?:RSA )?PRIVATE KEY-----\n[A-Za-z0-9+/=\n]+-----END (?:RSA )?PRIVATE KEY-----\n?$/,
|
|
247
|
+
)
|
|
248
|
+
: undefined;
|
|
249
|
+
if (
|
|
250
|
+
id === undefined ||
|
|
251
|
+
clientId === undefined ||
|
|
252
|
+
clientSecret === undefined ||
|
|
253
|
+
pem === undefined
|
|
254
|
+
) {
|
|
255
|
+
throw refuse();
|
|
256
|
+
}
|
|
257
|
+
const owner = typeof value.owner === "object" && value.owner !== null ? value.owner : {};
|
|
258
|
+
const name =
|
|
259
|
+
typeof value.name === "string" && value.name.length <= 100 && !hasControl(value.name)
|
|
260
|
+
? value.name
|
|
261
|
+
: undefined;
|
|
262
|
+
// The app's page is linked and printed only where it is at the host that was asked.
|
|
263
|
+
const appUrl =
|
|
264
|
+
typeof value.html_url === "string" &&
|
|
265
|
+
value.html_url.length <= 300 &&
|
|
266
|
+
!hasControl(value.html_url) &&
|
|
267
|
+
/^[\x21-\x7e]+$/.test(value.html_url) &&
|
|
268
|
+
!/["'<>`$\\]/.test(value.html_url) &&
|
|
269
|
+
value.html_url.startsWith(`${webUrl}/`)
|
|
270
|
+
? value.html_url
|
|
271
|
+
: undefined;
|
|
272
|
+
return {
|
|
273
|
+
id,
|
|
274
|
+
clientId,
|
|
275
|
+
clientSecret,
|
|
276
|
+
pem: pem.endsWith("\n") ? pem : `${pem}\n`,
|
|
277
|
+
name,
|
|
278
|
+
url: appUrl,
|
|
279
|
+
owner: text(owner.login, LOGIN),
|
|
280
|
+
ownerType:
|
|
281
|
+
owner.type === "User" ? "User" : owner.type === "Organization" ? "Organization" : undefined,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** A key of several lines as one line of an env file: in double quotes, its breaks as `\n`. */
|
|
286
|
+
export const envKey = (pem) => `"${pem.replaceAll("\n", "\\n")}"`;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* The app as lines of an env file, in the names the console reads. The private key, which only
|
|
290
|
+
* reading an organization needs, is among them only where the app was registered for one.
|
|
291
|
+
*/
|
|
292
|
+
export function envLines(app, { org, secrets = true }) {
|
|
293
|
+
const lines = [`GITHUB_CLIENT_ID=${app.clientId}`];
|
|
294
|
+
if (secrets) {
|
|
295
|
+
lines.push(`GITHUB_CLIENT_SECRET=${app.clientSecret}`);
|
|
296
|
+
}
|
|
297
|
+
lines.push(`GITHUB_APP_ID=${app.id}`);
|
|
298
|
+
if (org !== undefined) {
|
|
299
|
+
if (secrets) {
|
|
300
|
+
lines.push(`GITHUB_APP_PRIVATE_KEY=${envKey(app.pem)}`);
|
|
301
|
+
}
|
|
302
|
+
lines.push(`GITHUB_ORG=${org}`);
|
|
303
|
+
}
|
|
304
|
+
if (app.owner !== undefined) {
|
|
305
|
+
lines.push(`GITHUB_APP_OWNER=${app.owner}`);
|
|
306
|
+
}
|
|
307
|
+
if (app.ownerType !== undefined) {
|
|
308
|
+
lines.push(`GITHUB_APP_OWNER_TYPE=${app.ownerType}`);
|
|
309
|
+
}
|
|
310
|
+
if (app.url !== undefined) {
|
|
311
|
+
lines.push(`GITHUB_APP_URL=${app.url}`);
|
|
312
|
+
}
|
|
313
|
+
return lines;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Reads a body up to a bound: a length a server names is a hint, not a promise. */
|
|
317
|
+
async function bounded(response, most) {
|
|
318
|
+
if (response.body === null) {
|
|
319
|
+
return "";
|
|
320
|
+
}
|
|
321
|
+
const chunks = [];
|
|
322
|
+
let total = 0;
|
|
323
|
+
const reader = response.body.getReader();
|
|
324
|
+
while (true) {
|
|
325
|
+
const { done, value } = await reader.read();
|
|
326
|
+
if (done) {
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
total += value.byteLength;
|
|
330
|
+
if (total > most) {
|
|
331
|
+
await reader.cancel();
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
chunks.push(value);
|
|
335
|
+
}
|
|
336
|
+
return new TextDecoder().decode(Buffer.concat(chunks));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** The code exchanged for the app's settings, once. */
|
|
340
|
+
async function exchange(code, { apiUrl, webUrl, fetch: send }) {
|
|
341
|
+
let response;
|
|
342
|
+
try {
|
|
343
|
+
response = await send(`${apiUrl}/app-manifests/${encodeURIComponent(code)}/conversions`, {
|
|
344
|
+
method: "POST",
|
|
345
|
+
headers: {
|
|
346
|
+
accept: "application/vnd.github+json",
|
|
347
|
+
"x-github-api-version": API_VERSION,
|
|
348
|
+
"user-agent": "skillcdn-console-github-app",
|
|
349
|
+
},
|
|
350
|
+
redirect: "manual",
|
|
351
|
+
signal: AbortSignal.timeout(EXCHANGE_TIMEOUT_MS),
|
|
352
|
+
});
|
|
353
|
+
} catch {
|
|
354
|
+
throw new RegistrarError(
|
|
355
|
+
"GitHub could not be reached to take the app's settings. The app may stand there with nothing taken from it: delete it on its page, and run this again.",
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
if (response.status !== 201 && response.status !== 200) {
|
|
359
|
+
await response.body?.cancel();
|
|
360
|
+
throw new RegistrarError(
|
|
361
|
+
`GitHub refused the exchange (${response.status}): the code was used already, or an hour has passed. An app that stands at GitHub with nothing taken from it is deleted on its page; run this again.`,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
const body = await bounded(response, MAX_ANSWER_BYTES);
|
|
365
|
+
if (body === undefined) {
|
|
366
|
+
throw new RegistrarError(
|
|
367
|
+
"GitHub's answer was longer than an app's settings are; nothing was written.",
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
return parseConversion(body, { webUrl });
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const sameText = (one, other) => {
|
|
374
|
+
const a = Buffer.from(one);
|
|
375
|
+
const b = Buffer.from(other);
|
|
376
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Serves the page, waits for GitHub to send the browser back, and answers with the app. `started`
|
|
381
|
+
* is told where the page is once it listens. The listener is closed whichever way it ends.
|
|
382
|
+
*/
|
|
383
|
+
export function register(options, { fetch: send = fetch, started = () => {} } = {}) {
|
|
384
|
+
const state = randomBytes(24).toString("hex");
|
|
385
|
+
const name = options.name ?? `SkillCDN Console ${randomBytes(3).toString("hex")}`;
|
|
386
|
+
const hostnames = ["127.0.0.1", "localhost", "[::1]"];
|
|
387
|
+
let used = false;
|
|
388
|
+
let settle;
|
|
389
|
+
const done = new Promise((resolveDone, rejectDone) => {
|
|
390
|
+
settle = { resolve: resolveDone, reject: rejectDone };
|
|
391
|
+
});
|
|
392
|
+
const server = createServer((request, response) => {
|
|
393
|
+
const port = server.address()?.port;
|
|
394
|
+
const send_ = (status, title, body) => {
|
|
395
|
+
response.writeHead(status, {
|
|
396
|
+
"content-type": "text/html; charset=utf-8",
|
|
397
|
+
"cache-control": "no-store",
|
|
398
|
+
"referrer-policy": "no-referrer",
|
|
399
|
+
"x-content-type-options": "nosniff",
|
|
400
|
+
"content-security-policy": `default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; form-action ${options.webUrl}; base-uri 'none'; frame-ancestors 'none'`,
|
|
401
|
+
});
|
|
402
|
+
response.end(page(title, body));
|
|
403
|
+
};
|
|
404
|
+
// Only a browser that came by this machine's own name: a page elsewhere that names this
|
|
405
|
+
// port under a name of its own is not answered.
|
|
406
|
+
if (!hostnames.some((hostname) => request.headers.host === `${hostname}:${port}`)) {
|
|
407
|
+
send_(421, "Not this address", "<p>Open the address the script printed.</p>");
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const asked = new URL(request.url ?? "/", `http://127.0.0.1:${port}`);
|
|
411
|
+
if (request.method !== "GET") {
|
|
412
|
+
send_(405, "Not understood", "<p>Open the address the script printed.</p>");
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (asked.pathname === "/") {
|
|
416
|
+
const manifest = manifestOf({
|
|
417
|
+
url: options.url,
|
|
418
|
+
org: options.org,
|
|
419
|
+
name,
|
|
420
|
+
redirect: `http://localhost:${port}/callback`,
|
|
421
|
+
});
|
|
422
|
+
const action = registrationUrl({ webUrl: options.webUrl, org: options.org, state });
|
|
423
|
+
send_(
|
|
424
|
+
200,
|
|
425
|
+
"Register the console at GitHub",
|
|
426
|
+
`<p>GitHub asks you to confirm an app named <strong>${escapeHtml(name)}</strong>, which signs people in to the console at ${escapeHtml(options.url)}. Press <strong>Create GitHub App</strong> there, once. It reads nothing of your repositories${options.org === undefined ? "" : `, and of ${escapeHtml(options.org)} only who is a member`}.</p><form method="post" action="${escapeHtml(action)}"><input type="hidden" name="manifest" value="${escapeHtml(JSON.stringify(manifest))}"><button type="submit">Continue to GitHub</button></form><script>document.forms[0].submit()</script>`,
|
|
427
|
+
);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (asked.pathname !== "/callback") {
|
|
431
|
+
send_(404, "Nothing here", "<p>Open the address the script printed.</p>");
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
const code = asked.searchParams.get("code") ?? "";
|
|
435
|
+
// A callback that does not carry this run's state is somebody else's, and ends nothing.
|
|
436
|
+
if (!sameText(asked.searchParams.get("state") ?? "", state)) {
|
|
437
|
+
send_(
|
|
438
|
+
400,
|
|
439
|
+
"Not this registration",
|
|
440
|
+
"<p>Open the address the script printed, and begin there.</p>",
|
|
441
|
+
);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
if (!/^[A-Za-z0-9_-]{1,200}$/.test(code)) {
|
|
445
|
+
send_(
|
|
446
|
+
400,
|
|
447
|
+
"Not understood",
|
|
448
|
+
"<p>GitHub sent no code. Open the address the script printed, and begin again.</p>",
|
|
449
|
+
);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (used) {
|
|
453
|
+
send_(
|
|
454
|
+
409,
|
|
455
|
+
"Done already",
|
|
456
|
+
"<p>This registration was taken already. Go back to the terminal.</p>",
|
|
457
|
+
);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
used = true;
|
|
461
|
+
exchange(code, { apiUrl: options.apiUrl, webUrl: options.webUrl, fetch: send }).then(
|
|
462
|
+
(app) => {
|
|
463
|
+
const install =
|
|
464
|
+
options.org !== undefined && app.url !== undefined
|
|
465
|
+
? `<p>One step is left at GitHub: <a href="${escapeHtml(app.url)}/installations/new">install the app on ${escapeHtml(options.org)}</a>, so that the console reads who is a member.</p>`
|
|
466
|
+
: "";
|
|
467
|
+
send_(
|
|
468
|
+
200,
|
|
469
|
+
"The console is registered",
|
|
470
|
+
`<p>${escapeHtml(app.name ?? name)} is registered. Go back to the terminal: the rest goes on there.</p>${install}`,
|
|
471
|
+
);
|
|
472
|
+
settle.resolve({ ...app, name: app.name ?? name });
|
|
473
|
+
},
|
|
474
|
+
(error) => {
|
|
475
|
+
send_(
|
|
476
|
+
502,
|
|
477
|
+
"The registration was not taken",
|
|
478
|
+
"<p>Go back to the terminal, which says what happened.</p>",
|
|
479
|
+
);
|
|
480
|
+
settle.reject(error);
|
|
481
|
+
},
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
const timer = setTimeout(() => {
|
|
485
|
+
settle.reject(
|
|
486
|
+
new RegistrarError(
|
|
487
|
+
`Nobody pressed "Create GitHub App" within ${options.waitSeconds} seconds: nothing was registered. Run this again.`,
|
|
488
|
+
),
|
|
489
|
+
);
|
|
490
|
+
}, options.waitSeconds * 1000);
|
|
491
|
+
server.on("error", (error) => {
|
|
492
|
+
settle.reject(
|
|
493
|
+
new RegistrarError(
|
|
494
|
+
error?.code === "EADDRINUSE"
|
|
495
|
+
? `Port ${options.port} of this machine is taken: free it, or name another with --port.`
|
|
496
|
+
: "The page could not be served on this machine.",
|
|
497
|
+
),
|
|
498
|
+
);
|
|
499
|
+
});
|
|
500
|
+
server.listen(options.port, options.host, () => {
|
|
501
|
+
const port = server.address()?.port;
|
|
502
|
+
started({ address: `http://127.0.0.1:${port}/`, port, name, state });
|
|
503
|
+
});
|
|
504
|
+
const closed = () => {
|
|
505
|
+
clearTimeout(timer);
|
|
506
|
+
server.close();
|
|
507
|
+
server.closeAllConnections?.();
|
|
508
|
+
};
|
|
509
|
+
return done.then(
|
|
510
|
+
(app) => {
|
|
511
|
+
closed();
|
|
512
|
+
return app;
|
|
513
|
+
},
|
|
514
|
+
(error) => {
|
|
515
|
+
closed();
|
|
516
|
+
throw error;
|
|
517
|
+
},
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/** The two secrets merged into the JSON secrets file, which its owner alone may read. */
|
|
522
|
+
export function writeSecrets(file, app, { org }) {
|
|
523
|
+
let kept = {};
|
|
524
|
+
if (existsSync(file)) {
|
|
525
|
+
try {
|
|
526
|
+
kept = JSON.parse(readFileSync(file, "utf8"));
|
|
527
|
+
} catch {
|
|
528
|
+
throw new RegistrarError(`${file} is there and does not read as JSON; nothing was written.`);
|
|
529
|
+
}
|
|
530
|
+
if (typeof kept !== "object" || kept === null || Array.isArray(kept)) {
|
|
531
|
+
throw new RegistrarError(`${file} is there and is no secrets file; nothing was written.`);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const next = {
|
|
535
|
+
...kept,
|
|
536
|
+
GITHUB_CLIENT_SECRET: app.clientSecret,
|
|
537
|
+
...(org === undefined ? {} : { GITHUB_APP_PRIVATE_KEY: app.pem }),
|
|
538
|
+
};
|
|
539
|
+
writeFileSync(file, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
540
|
+
chmodSync(file, 0o600);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** Runs the script: the arguments, the page, the press, and what is taken put where it was asked. */
|
|
544
|
+
export async function main(argv, io) {
|
|
545
|
+
let options;
|
|
546
|
+
try {
|
|
547
|
+
options = parseArguments(argv);
|
|
548
|
+
} catch (error) {
|
|
549
|
+
io.stderr(`${error instanceof RegistrarError ? error.message : USAGE}\n`);
|
|
550
|
+
return 2;
|
|
551
|
+
}
|
|
552
|
+
try {
|
|
553
|
+
const app = await register(options, {
|
|
554
|
+
fetch: io.fetch,
|
|
555
|
+
started: ({ address, name }) => {
|
|
556
|
+
io.stderr(
|
|
557
|
+
`Registering a GitHub App named "${name}" for the console at ${options.url}.\nOpen ${address} in a browser signed in to GitHub${options.org === undefined ? "" : ` as an owner of ${options.org}`}, and press "Create GitHub App" there, once. Where GitHub asks you to sign in on the way, sign in and open the address again.\nWaiting for that, up to ${options.waitSeconds} seconds.\n`,
|
|
558
|
+
);
|
|
559
|
+
io.started?.({ address });
|
|
560
|
+
},
|
|
561
|
+
});
|
|
562
|
+
if (options.secrets === undefined) {
|
|
563
|
+
io.stdout(`${envLines(app, { org: options.org }).join("\n")}\n`);
|
|
564
|
+
} else {
|
|
565
|
+
writeSecrets(options.secrets, app, { org: options.org });
|
|
566
|
+
io.stdout(`${envLines(app, { org: options.org, secrets: false }).join("\n")}\n`);
|
|
567
|
+
io.stderr(
|
|
568
|
+
`GITHUB_CLIENT_SECRET${options.org === undefined ? " was" : " and GITHUB_APP_PRIVATE_KEY were"} written to ${options.secrets}.\n`,
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
io.stderr(
|
|
572
|
+
`Registered "${app.name}"${app.url === undefined ? "" : ` (${app.url})`}${app.owner === undefined ? "" : `, owned by ${app.owner}`}.\n${
|
|
573
|
+
options.org === undefined
|
|
574
|
+
? ""
|
|
575
|
+
: `One step is left at GitHub: install it on ${options.org}${app.url === undefined ? ", from the app's page" : `, at ${app.url}/installations/new`}, so that the console reads who is a member.\n`
|
|
576
|
+
}`,
|
|
577
|
+
);
|
|
578
|
+
return 0;
|
|
579
|
+
} catch (error) {
|
|
580
|
+
io.stderr(
|
|
581
|
+
`${error instanceof RegistrarError ? error.message : "The registration failed; nothing was written."}\n`,
|
|
582
|
+
);
|
|
583
|
+
return 1;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
if (process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
588
|
+
process.exitCode = await main(process.argv.slice(2), {
|
|
589
|
+
stdout: (text) => process.stdout.write(text),
|
|
590
|
+
stderr: (text) => process.stderr.write(text),
|
|
591
|
+
fetch,
|
|
592
|
+
});
|
|
593
|
+
}
|
|
@@ -37,7 +37,7 @@ A plugin the deployment installed ([building-a-plugin](/skills/building-a-plugin
|
|
|
37
37
|
|
|
38
38
|
An agent account is an agent that is a member of its own: it signs in as itself, is assigned tasks, is asked and answers, and reports to a manager, never as an administrator. It is not the person's own agent, which acts as them with a token: ask which is meant when "add an agent" could be either.
|
|
39
39
|
|
|
40
|
-
- Before one is made, read
|
|
40
|
+
- Before one is made, read [the account and the invitation](/skills/running-an-agent/references/the-account-and-the-invitation.md) of running-an-agent: the account it signs in with has to be there first at a git host, and no password of it is ever written in a task, a comment or a command. Its invitation, its machine, its secrets and what a supervisor tells it are that skill's.
|
|
41
41
|
- `console agent new <handle> [--login <login>] [--name "<name>"] [--title "<title>"] [--manager <login|me>] [--project <key>]` makes one: the handle it is called by (`@mira`), which never changes; the account it will sign in with, a login at the git host or an address at Google, made of the workspace's short name, the handle and its domain where the workspace names its identity service and `--login` is left out; its manager, the person asking when none is said; and the project it starts in, the directory's unless `--project` says. The member's row is written at once, so that tasks are assigned to it before anyone signed in with it (`--worker @mira`). The pages make one from proposals instead: Agents, "Add an agent account".
|
|
42
42
|
- `console agents` lists the agent accounts, `console agent <handle>` reads one. Its role, its manager and whether it is in are a member's, said with `console person`, by its login or its handle: `console person manager @mira <login>`, `console person deactivate @mira`. An agent account is never made an administrator (`person.agent_admin`), and its manager is replaced, never taken away (`person.manager_required`).
|
|
43
43
|
|