@agentshouse/kit 0.2.1-c2fb1c24bcdd5 → 0.2.1-c3304109525e3
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 +2 -2
- package/dist/acp.js +54 -4
- package/dist/agents.js +12 -21
- package/dist/calls.js +11 -3
- package/dist/clis.js +7 -3
- package/dist/connection.js +19 -29
- package/dist/conversations.js +140 -40
- package/dist/end.js +8 -2
- package/dist/events.js +29 -8
- package/dist/instructions.js +4 -4
- package/dist/login.js +3 -1
- package/dist/mcp.js +2 -0
- package/dist/resident.js +17 -12
- package/dist/scripts.js +255 -0
- package/dist/silence.js +28 -0
- package/dist/skills.js +38 -12
- package/dist/update.js +21 -4
- package/package.json +8 -8
- package/dist/web-wizard.js +0 -101
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ install Kit natively, with its workspace at `/agents/house`; `kit` and
|
|
|
23
23
|
`house` are then on the server's `PATH`.
|
|
24
24
|
|
|
25
25
|
Kit runs the agent CLIs your login shell finds and installs a missing one
|
|
26
|
-
through that CLI's own installer. Running the command again offers to update
|
|
26
|
+
through that CLI's own installer. A CLI older than the oldest release Kit is built for still runs, though its conversations may fail: Kit tells House its version, that oldest release and the CLI's own update command, such as `claude update`. Running the command again offers to update
|
|
27
27
|
each CLI older than Kit needs; add `--update-clis` to update every one to
|
|
28
28
|
its latest release without asking. Kit also gives every Agent on the
|
|
29
29
|
Environment the agent skills of `@agentshouse/skills`, at the version each Kit
|
|
@@ -53,7 +53,7 @@ the container. An Agent runs both, without `--owner`. `house upload` and `house
|
|
|
53
53
|
|
|
54
54
|
Kit turns each audio file your message to an Agent links into text on this computer before the Agent reads it, and keeps a message whose audio it cannot turn into text from starting. While transcription is on, the speech engine and model use about 425 MB beside the Environment's credential; turning it off deletes them.
|
|
55
55
|
|
|
56
|
-
When you press Run on a Web Wizard's page, Kit runs its script with `bash` in a terminal on this computer, as you, in the home and directory it runs agent CLIs from, with your login shell's `PATH`; the page shows that terminal and takes your typing, and closing the page, or Kit losing its connection to House, ends the script and its child processes. Kit keeps nothing the script prints or you type.
|
|
56
|
+
When you press Run on a Web Wizard's page, Kit runs its script with `bash` in a terminal on this computer, as you, in the home and directory it runs agent CLIs from, with your login shell's `PATH`; the page shows that terminal and takes your typing, and closing the page, or Kit losing its connection to House, ends the script and its child processes. Kit keeps nothing the script prints or you type. A script you keep as a function from a Web Wizard runs as you, from the same home, directory and `PATH`, when a Trigger of your Private Room or your agent runs it, with no terminal and nothing on its standard input, and when you press Run now on its page, in a terminal you can type into. Kit writes the run's input as JSON to a file only you can read, whose path is in `HOUSE_INPUT`, and deletes it when the run ends; it ends the script and its child processes at the run's time limit or when it loses its connection to House, and sends House the end of what the script printed, up to the run's output limit, without any line that holds a line you typed.
|
|
57
57
|
|
|
58
58
|
## License
|
|
59
59
|
|
package/dist/acp.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import { ndJsonStream, PROTOCOL_VERSION, } from '@agentclientprotocol/sdk';
|
|
1
|
+
import { ndJsonStream, PROTOCOL_VERSION, RequestError, } from '@agentclientprotocol/sdk';
|
|
2
2
|
import { spawn } from 'node:child_process';
|
|
3
3
|
import { Readable, Writable } from 'node:stream';
|
|
4
4
|
import { CLIS, KIT_RELEASE, adapterCommand } from './clis.js';
|
|
5
5
|
import { childEnvironment } from './relay.js';
|
|
6
6
|
import { killTree } from './scope.js';
|
|
7
|
+
import { Silence } from './silence.js';
|
|
7
8
|
const RETIRED = 'the CLI was retired';
|
|
9
|
+
const SIGNED_OUT = RequestError.authRequired().code;
|
|
10
|
+
export function signedOut(error) {
|
|
11
|
+
return typeof error === 'object' && error !== null && error.code === SIGNED_OUT;
|
|
12
|
+
}
|
|
13
|
+
// Claude Code ends a shell command or a model request after ten minutes, so a CLI that owes the Kit an answer and sends nothing for half an hour has hung.
|
|
14
|
+
export const CLI_SILENCE_MS = 30 * 60_000;
|
|
15
|
+
const SILENT = `the CLI sent nothing for ${CLI_SILENCE_MS / 60_000} minutes`;
|
|
8
16
|
export async function startAdapter(kind, cli, cwd, app, events = () => undefined, added = {}, signal, scope = null) {
|
|
9
17
|
const adapter = CLIS[kind].adapter;
|
|
10
18
|
const started = [adapter === null ? cli : adapterCommand(kind), CLIS[kind].args];
|
|
@@ -17,16 +25,50 @@ export async function startAdapter(kind, cli, cwd, app, events = () => undefined
|
|
|
17
25
|
});
|
|
18
26
|
let owned = true;
|
|
19
27
|
let retired = false;
|
|
28
|
+
let silent = false;
|
|
29
|
+
let over = false;
|
|
30
|
+
let open = false;
|
|
20
31
|
let stderr = '';
|
|
32
|
+
const owed = new Set();
|
|
33
|
+
const asked = new Set();
|
|
34
|
+
const silence = new Silence(CLI_SILENCE_MS, () => {
|
|
35
|
+
silent = true;
|
|
36
|
+
connection.close(new Error(SILENT));
|
|
37
|
+
if (child.pid !== undefined)
|
|
38
|
+
killTree(child);
|
|
39
|
+
});
|
|
40
|
+
const watch = () => (!over && (owed.size > 0 || open) && asked.size === 0 ? silence.heard() : silence.stop());
|
|
21
41
|
child.stderr.on('data', (chunk) => {
|
|
22
42
|
stderr = (stderr + chunk.toString('utf8')).slice(-4000);
|
|
23
43
|
});
|
|
24
44
|
const stream = ndJsonStream(Writable.toWeb(child.stdin), Readable.toWeb(child.stdout));
|
|
45
|
+
const output = stream.writable.getWriter();
|
|
25
46
|
const connection = app.connect({
|
|
26
|
-
writable:
|
|
47
|
+
writable: new WritableStream({
|
|
48
|
+
write(message) {
|
|
49
|
+
const sent = message;
|
|
50
|
+
if (sent.id !== undefined) {
|
|
51
|
+
if (sent.method === undefined)
|
|
52
|
+
asked.delete(sent.id);
|
|
53
|
+
else
|
|
54
|
+
owed.add(sent.id);
|
|
55
|
+
watch();
|
|
56
|
+
}
|
|
57
|
+
return output.write(message);
|
|
58
|
+
},
|
|
59
|
+
close: () => output.close(),
|
|
60
|
+
abort: (reason) => output.abort(reason),
|
|
61
|
+
}),
|
|
27
62
|
readable: stream.readable.pipeThrough(new TransformStream({
|
|
28
63
|
transform(message, controller) {
|
|
29
64
|
const read = message;
|
|
65
|
+
if (read.id !== undefined) {
|
|
66
|
+
if (read.method === undefined)
|
|
67
|
+
owed.delete(read.id);
|
|
68
|
+
else
|
|
69
|
+
asked.add(read.id);
|
|
70
|
+
}
|
|
71
|
+
watch();
|
|
30
72
|
if (owned)
|
|
31
73
|
events(read);
|
|
32
74
|
if (read.id !== undefined || (read.method !== 'session/update' && !read.method.startsWith('_'))) {
|
|
@@ -38,6 +80,8 @@ export async function startAdapter(kind, cli, cwd, app, events = () => undefined
|
|
|
38
80
|
const { promise: lost, resolve: lose } = Promise.withResolvers();
|
|
39
81
|
const exited = new Promise((resolve) => {
|
|
40
82
|
const end = (cause) => {
|
|
83
|
+
over = true;
|
|
84
|
+
silence.stop();
|
|
41
85
|
if (owned)
|
|
42
86
|
lose(cause);
|
|
43
87
|
owned = false;
|
|
@@ -47,11 +91,13 @@ export async function startAdapter(kind, cli, cwd, app, events = () => undefined
|
|
|
47
91
|
child.on('error', (error) => end(`the CLI did not start: ${error.message}`));
|
|
48
92
|
child.on('exit', (code, signal) => {
|
|
49
93
|
const last = stderr.trim().split('\n').at(-1);
|
|
50
|
-
end(`the CLI exited with ${code ?? signal}${last ? `: ${last}` : ''}`);
|
|
94
|
+
end(silent ? SILENT : `the CLI exited with ${code ?? signal}${last ? `: ${last}` : ''}`);
|
|
51
95
|
});
|
|
52
96
|
});
|
|
53
97
|
const retire = () => {
|
|
54
98
|
owned = false;
|
|
99
|
+
over = true;
|
|
100
|
+
silence.stop();
|
|
55
101
|
if (retired)
|
|
56
102
|
return;
|
|
57
103
|
retired = true;
|
|
@@ -75,7 +121,11 @@ export async function startAdapter(kind, cli, cwd, app, events = () => undefined
|
|
|
75
121
|
clientInfo: { name: '@agentshouse/kit', version: KIT_RELEASE },
|
|
76
122
|
});
|
|
77
123
|
scope?.entered(child.pid);
|
|
78
|
-
|
|
124
|
+
const turn = (opened) => {
|
|
125
|
+
open = opened;
|
|
126
|
+
watch();
|
|
127
|
+
};
|
|
128
|
+
return { connection, initialized, exited, lost, silent: () => silent, turn, retire };
|
|
79
129
|
}
|
|
80
130
|
catch (error) {
|
|
81
131
|
retire();
|
package/dist/agents.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { client } from '@agentclientprotocol/sdk';
|
|
2
2
|
import { readFile, realpath } from 'node:fs/promises';
|
|
3
3
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
-
import { startAdapter } from './acp.js';
|
|
4
|
+
import { signedOut, startAdapter } from './acp.js';
|
|
5
5
|
import { CLIS, KIT_RELEASE, below, installAdapter, installCli, locate, run, versionOf } from './clis.js';
|
|
6
6
|
import { agentBase, kitHome } from './home.js';
|
|
7
7
|
import { updatesItself } from './placement.js';
|
|
@@ -9,6 +9,8 @@ import { holdTranscription, transcriptionReport } from './transcription.js';
|
|
|
9
9
|
import { KIT_WIRE } from './wire.js';
|
|
10
10
|
// Two quiet seconds let a fresh conversation start before background model discovery competes for its CPU.
|
|
11
11
|
const MODEL_PROBE_QUIET_MS = 2_000;
|
|
12
|
+
// A sign-in's model read that has not answered in ten seconds leaves its sign-in to the status check, so a hung CLI never holds the sign-in's result.
|
|
13
|
+
const SIGNED_IN_READ_MS = 10_000;
|
|
12
14
|
const SIGNED_OUT = { signed_in: false, models: [], modes: [] };
|
|
13
15
|
const INSTALLING = { release: null, failure: null, installing: true, ...SIGNED_OUT };
|
|
14
16
|
function select(options, category) {
|
|
@@ -69,13 +71,6 @@ async function models(kind, adapter, known) {
|
|
|
69
71
|
}
|
|
70
72
|
return { models: offered, modes };
|
|
71
73
|
}
|
|
72
|
-
async function signedIn(kind, cli, known, signal) {
|
|
73
|
-
const probe = CLIS[kind].signedIn;
|
|
74
|
-
// A sign-in check that has not answered in half a minute counts as signed out rather than stalling the report.
|
|
75
|
-
if ('command' in probe)
|
|
76
|
-
return (await run(cli, probe.command, 30_000, false, signal)).status === 0;
|
|
77
|
-
return (await offer(kind, cli, known, signal)).signed_in;
|
|
78
|
-
}
|
|
79
74
|
function ended(signal) {
|
|
80
75
|
return new Promise((_, reject) => {
|
|
81
76
|
if (signal.aborted)
|
|
@@ -102,6 +97,8 @@ async function offer(kind, cli, known, signal) {
|
|
|
102
97
|
catch (error) {
|
|
103
98
|
if (!signal?.aborted)
|
|
104
99
|
process.stderr.write(`kit: ${kind} did not offer its models: ${causeOf(error)}\n`);
|
|
100
|
+
if (signedOut(error))
|
|
101
|
+
return SIGNED_OUT;
|
|
105
102
|
return {
|
|
106
103
|
signed_in: adapter === undefined && 'initializeMeta' in probe ? known.signed_in : true,
|
|
107
104
|
models: known.models,
|
|
@@ -113,10 +110,11 @@ async function offer(kind, cli, known, signal) {
|
|
|
113
110
|
}
|
|
114
111
|
}
|
|
115
112
|
function reported(kind, reading) {
|
|
113
|
+
const { minimum, update } = CLIS[kind];
|
|
116
114
|
return {
|
|
117
115
|
kind,
|
|
118
116
|
release: reading.release,
|
|
119
|
-
...(reading.
|
|
117
|
+
...(reading.release !== null && below(reading.release, minimum) ? { minimum, update } : {}),
|
|
120
118
|
failure: reading.failure,
|
|
121
119
|
...(reading.installing === undefined ? {} : { installing: reading.installing }),
|
|
122
120
|
signed_in: reading.signed_in,
|
|
@@ -126,10 +124,6 @@ function reported(kind, reading) {
|
|
|
126
124
|
};
|
|
127
125
|
}
|
|
128
126
|
const NOT_READY = "the Agent's CLI is not ready on this computer";
|
|
129
|
-
function tooOld(kind, release) {
|
|
130
|
-
const cli = CLIS[kind];
|
|
131
|
-
return `${cli.bin} ${release} is older than ${cli.minimum}, the oldest this Kit runs`;
|
|
132
|
-
}
|
|
133
127
|
async function sshHostKey() {
|
|
134
128
|
// Listing a local host key takes a moment, so ten seconds only bounds a stuck ssh-keygen.
|
|
135
129
|
const listed = await run('ssh-keygen', ['-l', '-E', 'sha256', '-f', '/etc/ssh/ssh_host_ed25519_key.pub'], 10_000);
|
|
@@ -215,12 +209,12 @@ export class Agents {
|
|
|
215
209
|
const resolved = await this.resolve(kind);
|
|
216
210
|
if (!('cli' in resolved)) {
|
|
217
211
|
this.update(kind, resolved);
|
|
218
|
-
throw new Error(
|
|
212
|
+
throw new Error(NOT_READY);
|
|
219
213
|
}
|
|
220
214
|
const { cli, release } = resolved;
|
|
221
215
|
const known = this.readings.get(kind);
|
|
222
|
-
const kept = known?.release === null
|
|
223
|
-
if (known?.release !== release ||
|
|
216
|
+
const kept = known?.release === null ? SIGNED_OUT : (known ?? SIGNED_OUT);
|
|
217
|
+
if (known?.release !== release || this.installs.has(kind)) {
|
|
224
218
|
this.update(kind, { release, failure: null, signed_in: kept.signed_in, models: kept.models, modes: kept.modes });
|
|
225
219
|
}
|
|
226
220
|
const read = this.nextRead(kind);
|
|
@@ -246,7 +240,7 @@ export class Agents {
|
|
|
246
240
|
const known = this.readings.get(kind) ?? SIGNED_OUT;
|
|
247
241
|
const owner = {};
|
|
248
242
|
try {
|
|
249
|
-
await this.examine(kind, known,
|
|
243
|
+
await this.examine(kind, known, (kind, cli) => offer(kind, cli, known, AbortSignal.any([signal, AbortSignal.timeout(SIGNED_IN_READ_MS)])), signal, owner);
|
|
250
244
|
}
|
|
251
245
|
finally {
|
|
252
246
|
this.settle(kind, owner);
|
|
@@ -318,9 +312,6 @@ export class Agents {
|
|
|
318
312
|
const release = await versionOf(cli, signal);
|
|
319
313
|
if (release === null)
|
|
320
314
|
return { release: null, failure: `${cli} --version names no version`, ...SIGNED_OUT };
|
|
321
|
-
const minimum = CLIS[kind].minimum;
|
|
322
|
-
if (below(release, minimum))
|
|
323
|
-
return { release, minimum, failure: null, ...SIGNED_OUT };
|
|
324
315
|
return { cli, release };
|
|
325
316
|
}
|
|
326
317
|
async examine(kind, known, read, signal, owner) {
|
|
@@ -339,7 +330,7 @@ export class Agents {
|
|
|
339
330
|
}
|
|
340
331
|
async load(kind) {
|
|
341
332
|
const known = this.readings.get(kind);
|
|
342
|
-
if (known !== undefined && known.failure === null
|
|
333
|
+
if (known !== undefined && known.failure === null)
|
|
343
334
|
return;
|
|
344
335
|
await this.examine(kind, SIGNED_OUT, (kind, cli, known) => this.quietOffer(kind, cli, known));
|
|
345
336
|
await this.report();
|
package/dist/calls.js
CHANGED
|
@@ -11,6 +11,7 @@ export class Calls {
|
|
|
11
11
|
path = join(kitHome(), 'call-results.json');
|
|
12
12
|
results;
|
|
13
13
|
returned = new Set();
|
|
14
|
+
released = new Set();
|
|
14
15
|
open = 0;
|
|
15
16
|
constructor(house, changed) {
|
|
16
17
|
this.house = house;
|
|
@@ -27,6 +28,8 @@ export class Calls {
|
|
|
27
28
|
return this.open === 0;
|
|
28
29
|
}
|
|
29
30
|
carry(callId, work) {
|
|
31
|
+
if (this.released.has(callId))
|
|
32
|
+
return Promise.resolve();
|
|
30
33
|
const known = this.carried.get(callId);
|
|
31
34
|
const result = this.results.get(callId);
|
|
32
35
|
const carried = known ?? (result === undefined ? work() : Promise.resolve({ result }));
|
|
@@ -37,7 +40,7 @@ export class Calls {
|
|
|
37
40
|
this.open++;
|
|
38
41
|
this.changed();
|
|
39
42
|
return carried
|
|
40
|
-
.then(async ({ result, returned }) => {
|
|
43
|
+
.then(async ({ result, returned, release }) => {
|
|
41
44
|
try {
|
|
42
45
|
await this.house.deliver(`/kit/inputs/${callId}/ack`, result);
|
|
43
46
|
}
|
|
@@ -46,8 +49,13 @@ export class Calls {
|
|
|
46
49
|
if (this.results.delete(callId))
|
|
47
50
|
this.save();
|
|
48
51
|
}
|
|
49
|
-
if (known
|
|
50
|
-
|
|
52
|
+
if (known !== undefined)
|
|
53
|
+
return;
|
|
54
|
+
if (release) {
|
|
55
|
+
this.carried.delete(callId);
|
|
56
|
+
this.released.add(callId);
|
|
57
|
+
}
|
|
58
|
+
await returned?.();
|
|
51
59
|
})
|
|
52
60
|
.catch(logged)
|
|
53
61
|
.finally(() => {
|
package/dist/clis.js
CHANGED
|
@@ -16,6 +16,7 @@ const RAW_SUBTYPES = [
|
|
|
16
16
|
'task_started',
|
|
17
17
|
'task_updated',
|
|
18
18
|
'task_notification',
|
|
19
|
+
'task_progress',
|
|
19
20
|
'session_state_changed',
|
|
20
21
|
'hook_started',
|
|
21
22
|
'hook_response',
|
|
@@ -71,8 +72,9 @@ function bridgeInClaudeSandbox(bridge) {
|
|
|
71
72
|
export const CLIS = {
|
|
72
73
|
'codex-acp': {
|
|
73
74
|
bin: 'codex',
|
|
74
|
-
minimum: '0.
|
|
75
|
+
minimum: '0.160.1',
|
|
75
76
|
install: 'curl -fsSL https://chatgpt.com/codex/install.sh | sh',
|
|
77
|
+
update: 'codex update',
|
|
76
78
|
adapter: {
|
|
77
79
|
package: '@agentclientprotocol/codex-acp',
|
|
78
80
|
bin: 'codex-acp',
|
|
@@ -94,8 +96,9 @@ export const CLIS = {
|
|
|
94
96
|
},
|
|
95
97
|
'claude-agent-acp': {
|
|
96
98
|
bin: 'claude',
|
|
97
|
-
minimum: '2.1.
|
|
99
|
+
minimum: '2.1.303',
|
|
98
100
|
install: 'curl -fsSL https://claude.ai/install.sh | bash',
|
|
101
|
+
update: 'claude update',
|
|
99
102
|
adapter: {
|
|
100
103
|
package: '@agentclientprotocol/claude-agent-acp',
|
|
101
104
|
bin: 'claude-agent-acp',
|
|
@@ -112,7 +115,7 @@ export const CLIS = {
|
|
|
112
115
|
title,
|
|
113
116
|
settings: { sandbox: { network: bridgeInClaudeSandbox(bridge) } },
|
|
114
117
|
},
|
|
115
|
-
emitRawSDKMessages: RAW_SUBTYPES.map((subtype) => ({ type: 'system', subtype })),
|
|
118
|
+
emitRawSDKMessages: [...RAW_SUBTYPES.map((subtype) => ({ type: 'system', subtype })), { type: 'result' }],
|
|
116
119
|
},
|
|
117
120
|
}),
|
|
118
121
|
unknownSession: (error) => answered(error).code === -32002,
|
|
@@ -126,6 +129,7 @@ export const CLIS = {
|
|
|
126
129
|
bin: 'grok',
|
|
127
130
|
minimum: '1.0.46',
|
|
128
131
|
install: 'curl -fsSL https://x.ai/cli/install.sh | bash',
|
|
132
|
+
update: 'grok update',
|
|
129
133
|
adapter: null,
|
|
130
134
|
args: ['--sandbox', 'off', 'agent', '--always-approve', '--no-leader', 'stdio'],
|
|
131
135
|
fullAccess: null,
|
package/dist/connection.js
CHANGED
|
@@ -3,40 +3,13 @@ import { HouseRefusal, retryDelay } from './api.js';
|
|
|
3
3
|
import { end, REPLACED, signInAgain } from './end.js';
|
|
4
4
|
import { readEnrolment } from './home.js';
|
|
5
5
|
import { relayed } from './relay.js';
|
|
6
|
+
import { Silence } from './silence.js';
|
|
6
7
|
import { endUnserved, KIT_WIRE } from './wire.js';
|
|
7
8
|
const PATH = '/kit/stream';
|
|
8
9
|
const SUBPROTOCOL = `house.kit.stream.${KIT_WIRE}`;
|
|
9
10
|
const REFUSAL_HEADER = 'x-house-refusal';
|
|
10
11
|
// House beats each Kit connection every thirty seconds, so two silent beats mean this Kit process cannot hear House.
|
|
11
12
|
export const CONNECTION_SILENCE_MS = 60_000;
|
|
12
|
-
const SYSTEM_CONNECTION_CLOCK = {
|
|
13
|
-
later: (delay, callback) => setTimeout(callback, delay),
|
|
14
|
-
clear: (timer) => clearTimeout(timer),
|
|
15
|
-
};
|
|
16
|
-
export class ConnectionSilence {
|
|
17
|
-
timer = null;
|
|
18
|
-
silent;
|
|
19
|
-
clock;
|
|
20
|
-
interval;
|
|
21
|
-
constructor(silent, clock = SYSTEM_CONNECTION_CLOCK, interval = CONNECTION_SILENCE_MS) {
|
|
22
|
-
this.silent = silent;
|
|
23
|
-
this.clock = clock;
|
|
24
|
-
this.interval = interval;
|
|
25
|
-
}
|
|
26
|
-
heard() {
|
|
27
|
-
this.stop();
|
|
28
|
-
this.timer = this.clock.later(this.interval, () => {
|
|
29
|
-
this.timer = null;
|
|
30
|
-
this.silent();
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
stop() {
|
|
34
|
-
if (this.timer === null)
|
|
35
|
-
return;
|
|
36
|
-
this.clock.clear(this.timer);
|
|
37
|
-
this.timer = null;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
13
|
function connectionUrl(house) {
|
|
41
14
|
const url = new URL(PATH, house);
|
|
42
15
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
@@ -58,19 +31,26 @@ subscribe('undici:request:headers', (message) => {
|
|
|
58
31
|
});
|
|
59
32
|
export function holdConnection(handlers) {
|
|
60
33
|
let connection = null;
|
|
34
|
+
let socket = null;
|
|
35
|
+
let paused = false;
|
|
61
36
|
let failures = 0;
|
|
62
37
|
const connect = async () => {
|
|
63
38
|
const { house, credential } = (await readEnrolment());
|
|
39
|
+
if (paused || socket !== null)
|
|
40
|
+
return;
|
|
64
41
|
refused = null;
|
|
65
42
|
const opening = new WebSocket(relayed(connectionUrl(house)), {
|
|
66
43
|
protocols: [SUBPROTOCOL],
|
|
67
44
|
headers: { authorization: `Bearer ${credential}` },
|
|
68
45
|
});
|
|
46
|
+
socket = opening;
|
|
69
47
|
let opened = false;
|
|
70
48
|
let silence = null;
|
|
71
49
|
const closed = (event) => {
|
|
72
50
|
silence?.stop();
|
|
73
51
|
silence = null;
|
|
52
|
+
if (socket === opening)
|
|
53
|
+
socket = null;
|
|
74
54
|
if (connection === opening)
|
|
75
55
|
connection = null;
|
|
76
56
|
handlers.closed();
|
|
@@ -85,6 +65,8 @@ export function holdConnection(handlers) {
|
|
|
85
65
|
endUnserved(refused.text);
|
|
86
66
|
if (!opened && refused !== null)
|
|
87
67
|
process.stderr.write(`kit: ${refused.message}\n`);
|
|
68
|
+
if (paused)
|
|
69
|
+
return;
|
|
88
70
|
// 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.
|
|
89
71
|
setTimeout(() => void connect(), opened ? 1000 + Math.random() * 2000 : retryDelay(failures++));
|
|
90
72
|
};
|
|
@@ -92,7 +74,7 @@ export function holdConnection(handlers) {
|
|
|
92
74
|
opened = true;
|
|
93
75
|
failures = 0;
|
|
94
76
|
connection = opening;
|
|
95
|
-
silence = new
|
|
77
|
+
silence = new Silence(CONNECTION_SILENCE_MS, () => {
|
|
96
78
|
if (connection !== opening)
|
|
97
79
|
return;
|
|
98
80
|
connection = null;
|
|
@@ -122,5 +104,13 @@ export function holdConnection(handlers) {
|
|
|
122
104
|
connection.send(JSON.stringify(notification));
|
|
123
105
|
return true;
|
|
124
106
|
},
|
|
107
|
+
pause() {
|
|
108
|
+
paused = true;
|
|
109
|
+
socket?.close();
|
|
110
|
+
return () => {
|
|
111
|
+
paused = false;
|
|
112
|
+
void connect();
|
|
113
|
+
};
|
|
114
|
+
},
|
|
125
115
|
};
|
|
126
116
|
}
|