@agentshouse/kit 0.2.1-cbabe8ad337ff → 0.2.1-cbff58027f078
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 +47 -46
- package/dist/acp.js +90 -19
- package/dist/afk.js +17 -0
- package/dist/agents.js +154 -44
- package/dist/api.js +7 -3
- package/dist/app-git.js +10 -26
- package/dist/authority-main.js +2 -2
- package/dist/bridge.js +11 -25
- package/dist/building.js +50 -0
- package/dist/calls.js +71 -0
- package/dist/chatgpt.js +32 -0
- package/dist/chunks.js +49 -0
- package/dist/clis.js +95 -48
- package/dist/connection.js +105 -0
- package/dist/conversations.js +574 -179
- package/dist/copies.js +431 -218
- package/dist/end.js +29 -4
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +70 -8
- package/dist/files.js +139 -31
- package/dist/git.js +57 -21
- package/dist/home.js +89 -7
- package/dist/house-cli.js +299 -0
- package/dist/house-main.js +7 -177
- package/dist/instructions.js +8 -10
- package/dist/kit-cli.js +68 -0
- package/dist/kit-main.js +8 -22
- package/dist/login.js +113 -23
- package/dist/logout.js +0 -2
- package/dist/mcp.js +6 -2
- package/dist/parts.js +44 -16
- package/dist/placement.js +31 -0
- package/dist/relay.js +33 -2
- package/dist/resident.js +111 -44
- package/dist/scope.js +15 -4
- package/dist/scripts.js +255 -0
- package/dist/service-main.js +9 -0
- package/dist/service.js +55 -0
- package/dist/sign-in.js +22 -39
- package/dist/silence.js +28 -0
- package/dist/skills.js +49 -35
- package/dist/transcription.js +85 -45
- package/dist/translate.js +2 -8
- package/dist/update.js +113 -105
- package/dist/wire.js +40 -0
- package/dist/work.js +3 -5
- package/package.json +20 -20
- package/dist/bootstrap.json +0 -24
- package/dist/clis-main.js +0 -23
- package/dist/stream.js +0 -72
- package/how-we-work.md +0 -23
- package/skills/ask-matt/LICENSE +0 -21
- package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
- package/skills/ask-matt/SKILL.md +0 -91
- package/skills/ask-matt/agents/openai.yaml +0 -4
- package/skills/code-review/LICENSE +0 -21
- package/skills/code-review/SKILL.md +0 -89
- package/skills/code-review/agents/openai.yaml +0 -3
- package/skills/codebase-design/DEEPENING.md +0 -37
- package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
- package/skills/codebase-design/LICENSE +0 -21
- package/skills/codebase-design/SKILL.md +0 -116
- package/skills/codebase-design/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/LICENSE +0 -21
- package/skills/diagnosing-bugs/SKILL.md +0 -140
- package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
- package/skills/domain-modeling/ADR-FORMAT.md +0 -47
- package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
- package/skills/domain-modeling/LICENSE +0 -21
- package/skills/domain-modeling/SKILL.md +0 -76
- package/skills/domain-modeling/agents/openai.yaml +0 -3
- package/skills/grill-me/LICENSE +0 -21
- package/skills/grill-me/SKILL.md +0 -8
- package/skills/grill-me/agents/openai.yaml +0 -4
- package/skills/grill-with-docs/LICENSE +0 -21
- package/skills/grill-with-docs/SKILL.md +0 -8
- package/skills/grill-with-docs/agents/openai.yaml +0 -4
- package/skills/grilling/LICENSE +0 -21
- package/skills/grilling/SKILL.md +0 -30
- package/skills/grilling/agents/openai.yaml +0 -3
- package/skills/handoff/LICENSE +0 -21
- package/skills/handoff/SKILL.md +0 -17
- package/skills/handoff/agents/openai.yaml +0 -4
- package/skills/implement/LICENSE +0 -21
- package/skills/implement/SKILL.md +0 -16
- package/skills/implement/agents/openai.yaml +0 -4
- package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
- package/skills/improve-codebase-architecture/LICENSE +0 -21
- package/skills/improve-codebase-architecture/SKILL.md +0 -72
- package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
- package/skills/prototype/LICENSE +0 -21
- package/skills/prototype/LOGIC.md +0 -67
- package/skills/prototype/SKILL.md +0 -28
- package/skills/prototype/UI.md +0 -112
- package/skills/prototype/agents/openai.yaml +0 -3
- package/skills/research/LICENSE +0 -21
- package/skills/research/SKILL.md +0 -14
- package/skills/research/agents/openai.yaml +0 -3
- package/skills/resolving-merge-conflicts/LICENSE +0 -21
- package/skills/resolving-merge-conflicts/SKILL.md +0 -16
- package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
- package/skills/tdd/LICENSE +0 -21
- package/skills/tdd/SKILL.md +0 -40
- package/skills/tdd/agents/openai.yaml +0 -3
- package/skills/tdd/mocking.md +0 -59
- package/skills/tdd/tests.md +0 -77
- package/skills/teach/GLOSSARY-FORMAT.md +0 -35
- package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
- package/skills/teach/LICENSE +0 -21
- package/skills/teach/MISSION-FORMAT.md +0 -31
- package/skills/teach/RESOURCES-FORMAT.md +0 -32
- package/skills/teach/SKILL.md +0 -141
- package/skills/teach/agents/openai.yaml +0 -4
- package/skills/to-questionnaire/LICENSE +0 -21
- package/skills/to-questionnaire/SKILL.md +0 -55
- package/skills/to-questionnaire/agents/openai.yaml +0 -4
- package/skills/to-spec/LICENSE +0 -21
- package/skills/to-spec/SKILL.md +0 -76
- package/skills/to-spec/agents/openai.yaml +0 -4
- package/skills/to-tickets/LICENSE +0 -21
- package/skills/to-tickets/SKILL.md +0 -106
- package/skills/to-tickets/agents/openai.yaml +0 -4
- package/skills/wait-what/LICENSE +0 -21
- package/skills/wait-what/SKILL.md +0 -8
- package/skills/wait-what/agents/openai.yaml +0 -4
- package/skills/wayfinder/LICENSE +0 -21
- package/skills/wayfinder/SKILL.md +0 -129
- package/skills/wayfinder/agents/openai.yaml +0 -4
- package/skills/writing-for-agents/LICENSE +0 -21
- package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
- package/skills/writing-for-agents/SKILL.md +0 -83
- package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/clis.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
2
3
|
import { constants, readFileSync } from 'node:fs';
|
|
3
|
-
import { access, mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { access, mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
4
5
|
import { homedir, userInfo } from 'node:os';
|
|
5
|
-
import { delimiter, isAbsolute, join } from 'node:path';
|
|
6
|
-
import {
|
|
6
|
+
import { delimiter, dirname, isAbsolute, join } from 'node:path';
|
|
7
|
+
import { releaseDirectory } from './placement.js';
|
|
8
|
+
import { childEnvironment } from './relay.js';
|
|
9
|
+
const HOUSE_EXECUTABLES = /^host_executable\(name = "house", paths = (\[.*\])\)$/m;
|
|
10
|
+
function houseRule(houses) {
|
|
11
|
+
return `prefix_rule(pattern = ["house"], decision = "allow")\nhost_executable(name = "house", paths = ${JSON.stringify(houses)})\n`;
|
|
12
|
+
}
|
|
7
13
|
const RAW_SUBTYPES = [
|
|
8
14
|
'background_tasks_changed',
|
|
9
15
|
'task_started',
|
|
10
16
|
'task_updated',
|
|
11
17
|
'task_notification',
|
|
18
|
+
'task_progress',
|
|
12
19
|
'session_state_changed',
|
|
13
20
|
'hook_started',
|
|
14
21
|
'hook_response',
|
|
@@ -22,25 +29,59 @@ async function executable(path) {
|
|
|
22
29
|
return false;
|
|
23
30
|
}
|
|
24
31
|
}
|
|
25
|
-
async function
|
|
32
|
+
async function listedHouses(file) {
|
|
33
|
+
let text;
|
|
34
|
+
try {
|
|
35
|
+
text = await readFile(file, 'utf8');
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (error.code === 'ENOENT')
|
|
39
|
+
return [];
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const listed = JSON.parse(HOUSE_EXECUTABLES.exec(text)?.[1] ?? '[]');
|
|
44
|
+
return Array.isArray(listed) ? listed.filter((house) => typeof house === 'string') : [];
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export async function allowHouseInCodex(path) {
|
|
26
51
|
const candidates = [...new Set(path.split(delimiter).filter((directory) => isAbsolute(directory)))].map((directory) => join(directory, 'house'));
|
|
27
|
-
const houses = [];
|
|
28
|
-
for (const candidate of candidates)
|
|
29
|
-
if (await executable(candidate))
|
|
30
|
-
houses.push(candidate);
|
|
31
52
|
const rules = join(process.env.CODEX_HOME || join(homedir(), '.codex'), 'rules');
|
|
53
|
+
const file = join(rules, 'house.rules');
|
|
54
|
+
const houses = [];
|
|
55
|
+
for (const house of [...(await listedHouses(file)), ...candidates]) {
|
|
56
|
+
if (!houses.includes(house) && (await executable(house)))
|
|
57
|
+
houses.push(house);
|
|
58
|
+
}
|
|
32
59
|
await mkdir(rules, { recursive: true });
|
|
33
|
-
|
|
60
|
+
const temporary = `${file}.${randomUUID()}`;
|
|
61
|
+
await writeFile(temporary, houseRule(houses));
|
|
62
|
+
await rename(temporary, file);
|
|
34
63
|
}
|
|
35
64
|
async function nothing() { }
|
|
65
|
+
function claudeQuestionFields(request) {
|
|
66
|
+
const properties = request.requestedSchema?.properties ?? {};
|
|
67
|
+
return Object.keys(properties).filter((name) => properties[name]._meta?.jetbrains?.air?.customAnswer !== undefined);
|
|
68
|
+
}
|
|
69
|
+
function codexQuestionFields(request) {
|
|
70
|
+
const { _meta, requestedSchema } = request;
|
|
71
|
+
return _meta?.codex === undefined ? [] : (requestedSchema?.required ?? []);
|
|
72
|
+
}
|
|
73
|
+
function answered(error) {
|
|
74
|
+
return typeof error === 'object' && error !== null ? error : {};
|
|
75
|
+
}
|
|
36
76
|
function bridgeInClaudeSandbox(bridge) {
|
|
37
77
|
return process.platform === 'darwin' ? { allowUnixSockets: [bridge] } : { allowAllUnixSockets: true };
|
|
38
78
|
}
|
|
39
79
|
export const CLIS = {
|
|
40
80
|
'codex-acp': {
|
|
41
81
|
bin: 'codex',
|
|
42
|
-
minimum: '0.
|
|
82
|
+
minimum: '0.160.1',
|
|
43
83
|
install: 'curl -fsSL https://chatgpt.com/codex/install.sh | sh',
|
|
84
|
+
update: 'codex update',
|
|
44
85
|
adapter: {
|
|
45
86
|
package: '@agentclientprotocol/codex-acp',
|
|
46
87
|
bin: 'codex-acp',
|
|
@@ -50,16 +91,22 @@ export const CLIS = {
|
|
|
50
91
|
args: [],
|
|
51
92
|
fullAccess: 'agent-full-access',
|
|
52
93
|
sessionMeta: () => ({}),
|
|
94
|
+
unknownSession: (error) => {
|
|
95
|
+
const { code, data } = answered(error);
|
|
96
|
+
return code === -32603 && typeof data?.details === 'string' && data.details.startsWith('no rollout found');
|
|
97
|
+
},
|
|
53
98
|
renames: true,
|
|
54
99
|
allowHouse: allowHouseInCodex,
|
|
100
|
+
questionFields: codexQuestionFields,
|
|
55
101
|
signedIn: { command: ['login', 'status'] },
|
|
56
102
|
login: { args: ['login', '--device-auth'], code: 'show' },
|
|
57
103
|
air: ['asyncTasks', 'nativeSubagentSessions'],
|
|
58
104
|
},
|
|
59
105
|
'claude-agent-acp': {
|
|
60
106
|
bin: 'claude',
|
|
61
|
-
minimum: '2.1.
|
|
107
|
+
minimum: '2.1.303',
|
|
62
108
|
install: 'curl -fsSL https://claude.ai/install.sh | bash',
|
|
109
|
+
update: 'claude update',
|
|
63
110
|
adapter: {
|
|
64
111
|
package: '@agentclientprotocol/claude-agent-acp',
|
|
65
112
|
bin: 'claude-agent-acp',
|
|
@@ -76,11 +123,13 @@ export const CLIS = {
|
|
|
76
123
|
title,
|
|
77
124
|
settings: { sandbox: { network: bridgeInClaudeSandbox(bridge) } },
|
|
78
125
|
},
|
|
79
|
-
emitRawSDKMessages: RAW_SUBTYPES.map((subtype) => ({ type: 'system', subtype })),
|
|
126
|
+
emitRawSDKMessages: [...RAW_SUBTYPES.map((subtype) => ({ type: 'system', subtype })), { type: 'result' }],
|
|
80
127
|
},
|
|
81
128
|
}),
|
|
129
|
+
unknownSession: (error) => answered(error).code === -32002,
|
|
82
130
|
renames: false,
|
|
83
131
|
allowHouse: nothing,
|
|
132
|
+
questionFields: claudeQuestionFields,
|
|
84
133
|
signedIn: { command: ['auth', 'status'] },
|
|
85
134
|
login: { args: ['auth', 'login', '--claudeai'], code: 'collect', rejected: 'Invalid code' },
|
|
86
135
|
air: ['asyncTasks', 'sessionFailure'],
|
|
@@ -89,50 +138,37 @@ export const CLIS = {
|
|
|
89
138
|
bin: 'grok',
|
|
90
139
|
minimum: '1.0.46',
|
|
91
140
|
install: 'curl -fsSL https://x.ai/cli/install.sh | bash',
|
|
141
|
+
update: 'grok update',
|
|
92
142
|
adapter: null,
|
|
93
143
|
args: ['--sandbox', 'off', 'agent', '--always-approve', '--no-leader', 'stdio'],
|
|
94
144
|
fullAccess: null,
|
|
95
145
|
sessionMeta: () => ({}),
|
|
146
|
+
unknownSession: (error) => answered(error).data?.code === 'FS_NOT_FOUND',
|
|
96
147
|
renames: false,
|
|
97
148
|
allowHouse: nothing,
|
|
149
|
+
questionFields: () => [],
|
|
98
150
|
signedIn: { initializeMeta: 'defaultAuthMethodId' },
|
|
99
151
|
login: { args: ['login', '--device-auth'], code: 'show' },
|
|
100
152
|
air: ['asyncTasks'],
|
|
101
153
|
},
|
|
102
154
|
};
|
|
103
155
|
const KIT_PACKAGE = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
104
|
-
export const
|
|
105
|
-
const PROXY = /^https?_proxy$/i;
|
|
156
|
+
export const KIT_RELEASE = KIT_PACKAGE.version;
|
|
106
157
|
const VERSION = /\d+\.\d+\.\d+(?:-[0-9A-Za-z.]+)?/;
|
|
107
|
-
// An install still running after fifteen minutes is ended
|
|
158
|
+
// An install or update still running after fifteen minutes is ended as failed, so a hung download can neither hold its CLI's report nor keep the Kit busy.
|
|
108
159
|
const INSTALL_MS = 15 * 60_000;
|
|
109
160
|
// A login shell or a version read is ended after half a minute, so a profile that waits for input cannot stall the report.
|
|
110
161
|
const READ_MS = 30_000;
|
|
111
162
|
const CAUSE_CHARACTERS = 4000;
|
|
112
|
-
function prefix(kind) {
|
|
113
|
-
return join(kitHome(), 'agents', kind);
|
|
114
|
-
}
|
|
115
163
|
export function adapterCommand(kind) {
|
|
116
|
-
return join(
|
|
117
|
-
}
|
|
118
|
-
async function installedAdapter(kind) {
|
|
119
|
-
try {
|
|
120
|
-
const manifest = join(prefix(kind), 'node_modules', CLIS[kind].adapter.package, 'package.json');
|
|
121
|
-
return JSON.parse(await readFile(manifest, 'utf8')).version;
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
export function withoutProxy() {
|
|
128
|
-
return Object.fromEntries(Object.entries(process.env).filter(([name]) => !PROXY.test(name)));
|
|
164
|
+
return join(releaseDirectory(KIT_RELEASE), 'adapters', kind, 'node_modules', '.bin', CLIS[kind].adapter.bin);
|
|
129
165
|
}
|
|
130
166
|
export function run(command, args, timeoutMs, detached = false, signal) {
|
|
131
167
|
return new Promise((resolve) => {
|
|
132
168
|
const child = spawn(command, args, {
|
|
133
169
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
134
170
|
timeout: timeoutMs,
|
|
135
|
-
env:
|
|
171
|
+
env: childEnvironment(),
|
|
136
172
|
detached,
|
|
137
173
|
signal: detached ? undefined : signal,
|
|
138
174
|
});
|
|
@@ -166,19 +202,18 @@ export function run(command, args, timeoutMs, detached = false, signal) {
|
|
|
166
202
|
export function failureOf(ran, program) {
|
|
167
203
|
return ran.output.trim().slice(-CAUSE_CHARACTERS) || `${program} exited with ${ran.status ?? 'a signal'}`;
|
|
168
204
|
}
|
|
169
|
-
export
|
|
170
|
-
|
|
171
|
-
if (adapter === null)
|
|
172
|
-
return null;
|
|
173
|
-
const release = KIT_PACKAGE.peerDependencies[adapter.package];
|
|
174
|
-
if ((await installedAdapter(kind)) === release)
|
|
175
|
-
return null;
|
|
176
|
-
const ran = await run('npm', ['install', '--prefix', prefix(kind), '--no-save', '--no-audit', '--no-fund', '--omit=optional', `${adapter.package}@${release}`], INSTALL_MS, true, signal);
|
|
177
|
-
return ran.status === 0 ? null : failureOf(ran, 'npm');
|
|
205
|
+
export function adapterArchive(kind) {
|
|
206
|
+
return `${kind}-${KIT_PACKAGE.peerDependencies[CLIS[kind].adapter.package]}.tar.gz`;
|
|
178
207
|
}
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
208
|
+
async function bounded(program, work, signal) {
|
|
209
|
+
const deadline = AbortSignal.timeout(INSTALL_MS);
|
|
210
|
+
const ran = await work(signal === undefined ? deadline : AbortSignal.any([signal, deadline]));
|
|
211
|
+
if (deadline.aborted && signal?.aborted !== true)
|
|
212
|
+
return `${program} did not end within ${INSTALL_MS / 1000} seconds`;
|
|
213
|
+
return ran.status === 0 ? null : failureOf(ran, program);
|
|
214
|
+
}
|
|
215
|
+
export function installCli(kind, signal) {
|
|
216
|
+
return bounded('the install', (within) => run('bash', ['-o', 'pipefail', '-c', CLIS[kind].install], INSTALL_MS, true, within), signal);
|
|
182
217
|
}
|
|
183
218
|
function loginShell() {
|
|
184
219
|
return process.env.SHELL || userInfo().shell;
|
|
@@ -186,18 +221,30 @@ function loginShell() {
|
|
|
186
221
|
const FOUND = 'house-kit-cli ';
|
|
187
222
|
const LOOKUP = `found=$(command -v "$1") || exit 0; case $found in /*) ;; *) found=$(pwd -P)/$found ;; esac; printf "${FOUND}%s\\n" "$found"`;
|
|
188
223
|
const USER_BIN = 'case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) PATH="$PATH:$HOME/.local/bin" ;; esac; export PATH';
|
|
189
|
-
function inLoginShell(script, argument = '', signal) {
|
|
190
|
-
return run(loginShell(), ['-l', '-i', '-c', `exec /bin/sh -c '${USER_BIN}; ${script}' sh ${argument}`],
|
|
224
|
+
function inLoginShell(script, argument = '', signal, timeoutMs = READ_MS) {
|
|
225
|
+
return run(loginShell(), ['-l', '-i', '-c', `exec /bin/sh -c '${USER_BIN}; ${script}' sh ${argument}`], timeoutMs, true, signal);
|
|
191
226
|
}
|
|
192
|
-
export async function
|
|
193
|
-
const ran = await inLoginShell(LOOKUP,
|
|
227
|
+
export async function onLoginPath(program, signal) {
|
|
228
|
+
const ran = await inLoginShell(LOOKUP, program, signal);
|
|
194
229
|
return ran.stdout.split('\n').findLast((line) => line.startsWith(FOUND))?.slice(FOUND.length) ?? null;
|
|
195
230
|
}
|
|
231
|
+
export function locate(kind, signal) {
|
|
232
|
+
return onLoginPath(CLIS[kind].bin, signal);
|
|
233
|
+
}
|
|
234
|
+
export function updateCli(kind) {
|
|
235
|
+
return bounded('the update', (within) => inLoginShell(CLIS[kind].update, '', within, INSTALL_MS));
|
|
236
|
+
}
|
|
196
237
|
export async function loginPath() {
|
|
197
238
|
const marker = 'house-kit-path ';
|
|
198
239
|
const ran = await inLoginShell(`printf "${marker}%s\\n" "$PATH"`);
|
|
199
240
|
return ran.stdout.split('\n').findLast((line) => line.startsWith(marker)).slice(marker.length);
|
|
200
241
|
}
|
|
242
|
+
export async function writeCodexRule() {
|
|
243
|
+
const shell = await loginPath().catch(() => '');
|
|
244
|
+
await allowHouseInCodex([dirname(process.execPath), process.env.PATH ?? '', shell].join(delimiter)).catch((error) => {
|
|
245
|
+
throw new Error(`Codex's rule for house could not be written: ${error.message}`);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
201
248
|
export async function versionOf(path, signal) {
|
|
202
249
|
return VERSION.exec((await run(path, ['--version'], READ_MS, false, signal)).stdout)?.[0] ?? null;
|
|
203
250
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { subscribe } from 'node:diagnostics_channel';
|
|
2
|
+
import { HouseRefusal, retryDelay } from './api.js';
|
|
3
|
+
import { end, REPLACED, signInAgain } from './end.js';
|
|
4
|
+
import { readEnrolment } from './home.js';
|
|
5
|
+
import { relayed } from './relay.js';
|
|
6
|
+
import { Silence } from './silence.js';
|
|
7
|
+
import { endUnserved, KIT_WIRE, unserved } from './wire.js';
|
|
8
|
+
const PATH = '/kit/stream';
|
|
9
|
+
const SUBPROTOCOL = `house.kit.stream.${KIT_WIRE}`;
|
|
10
|
+
const REFUSAL_HEADER = 'x-house-refusal';
|
|
11
|
+
// House beats each Kit connection every thirty seconds, so two silent beats mean this Kit process cannot hear House.
|
|
12
|
+
export const CONNECTION_SILENCE_MS = 60_000;
|
|
13
|
+
function connectionUrl(house) {
|
|
14
|
+
const url = new URL(PATH, house);
|
|
15
|
+
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
16
|
+
return url;
|
|
17
|
+
}
|
|
18
|
+
function header(headers, name) {
|
|
19
|
+
for (let index = 0; index < headers.length; index += 2) {
|
|
20
|
+
if (headers[index].toString('latin1').toLowerCase() === name)
|
|
21
|
+
return headers[index + 1].toString('latin1');
|
|
22
|
+
}
|
|
23
|
+
return '';
|
|
24
|
+
}
|
|
25
|
+
let refused = null;
|
|
26
|
+
subscribe('undici:request:headers', (message) => {
|
|
27
|
+
const { request, response } = message;
|
|
28
|
+
if (!request.path.endsWith(PATH))
|
|
29
|
+
return;
|
|
30
|
+
refused = new HouseRefusal(PATH, response.statusCode, header(response.headers, REFUSAL_HEADER) || response.statusText);
|
|
31
|
+
});
|
|
32
|
+
export function holdConnection(handlers) {
|
|
33
|
+
let connection = null;
|
|
34
|
+
let socket = null;
|
|
35
|
+
let failures = 0;
|
|
36
|
+
const connect = async () => {
|
|
37
|
+
const { house, credential } = (await readEnrolment());
|
|
38
|
+
if (socket !== null || unserved())
|
|
39
|
+
return;
|
|
40
|
+
refused = null;
|
|
41
|
+
const opening = new WebSocket(relayed(connectionUrl(house)), {
|
|
42
|
+
protocols: [SUBPROTOCOL],
|
|
43
|
+
headers: { authorization: `Bearer ${credential}` },
|
|
44
|
+
});
|
|
45
|
+
socket = opening;
|
|
46
|
+
let opened = false;
|
|
47
|
+
let silence = null;
|
|
48
|
+
const closed = (event) => {
|
|
49
|
+
silence?.stop();
|
|
50
|
+
silence = null;
|
|
51
|
+
if (socket === opening)
|
|
52
|
+
socket = null;
|
|
53
|
+
if (connection === opening)
|
|
54
|
+
connection = null;
|
|
55
|
+
handlers.closed();
|
|
56
|
+
process.stderr.write(`kit: connection closed ${event.code} ${event.reason}\n`);
|
|
57
|
+
if (event.reason === 'replaced')
|
|
58
|
+
end(REPLACED);
|
|
59
|
+
if (event.reason === 'credential_rejected' || refused?.status === 401) {
|
|
60
|
+
void signInAgain(credential);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (refused !== null)
|
|
64
|
+
endUnserved(refused.text);
|
|
65
|
+
if (!opened && refused !== null)
|
|
66
|
+
process.stderr.write(`kit: ${refused.message}\n`);
|
|
67
|
+
// A closed connection reopens in one to three seconds, spread so Kit processes one restart closed return apart; an opening House refused or left unanswered backs off on the jittered curve, retryable or not, since a resident outlives a House deploy.
|
|
68
|
+
setTimeout(() => void connect(), opened ? 1000 + Math.random() * 2000 : retryDelay(failures++));
|
|
69
|
+
};
|
|
70
|
+
opening.onopen = () => {
|
|
71
|
+
opened = true;
|
|
72
|
+
failures = 0;
|
|
73
|
+
connection = opening;
|
|
74
|
+
silence = new Silence(CONNECTION_SILENCE_MS, () => {
|
|
75
|
+
if (connection !== opening)
|
|
76
|
+
return;
|
|
77
|
+
connection = null;
|
|
78
|
+
opening.onclose = null;
|
|
79
|
+
opening.close();
|
|
80
|
+
closed(new CloseEvent('close', { code: 1006, reason: 'heartbeat_missed' }));
|
|
81
|
+
});
|
|
82
|
+
silence.heard();
|
|
83
|
+
handlers.opened();
|
|
84
|
+
};
|
|
85
|
+
opening.onmessage = (event) => {
|
|
86
|
+
silence?.heard();
|
|
87
|
+
const arrived = JSON.parse(String(event.data));
|
|
88
|
+
if (arrived.type === 'input')
|
|
89
|
+
handlers.call(arrived);
|
|
90
|
+
else if (arrived.type !== 'beat')
|
|
91
|
+
handlers.notification(arrived);
|
|
92
|
+
};
|
|
93
|
+
opening.onerror = () => undefined;
|
|
94
|
+
opening.onclose = closed;
|
|
95
|
+
};
|
|
96
|
+
void connect();
|
|
97
|
+
return {
|
|
98
|
+
send(notification) {
|
|
99
|
+
if (connection === null || connection.readyState !== WebSocket.OPEN)
|
|
100
|
+
return false;
|
|
101
|
+
connection.send(JSON.stringify(notification));
|
|
102
|
+
return true;
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|