@agentshouse/kit 0.2.1-c3e4a20bb3183 → 0.2.1-c3fc787e93f0e
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 +36 -36
- package/dist/acp.js +16 -93
- package/dist/agents.js +103 -42
- package/dist/api.js +36 -3
- package/dist/app-git.js +64 -0
- package/dist/bootstrap.json +10 -17
- package/dist/bridge.js +36 -52
- package/dist/chatgpt.js +26 -0
- package/dist/clis.js +127 -65
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +288 -236
- package/dist/copies.js +172 -144
- package/dist/end.js +22 -0
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +521 -0
- package/dist/files.js +23 -2
- package/dist/git.js +11 -8
- package/dist/home.js +90 -6
- package/dist/house-main.js +165 -63
- package/dist/instructions.js +7 -5
- package/dist/kit-main.js +55 -4
- package/dist/login.js +96 -106
- package/dist/logout.js +21 -0
- package/dist/mcp.js +2 -1
- package/dist/parts.js +82 -0
- package/dist/placement.js +48 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +20 -0
- package/dist/resident.js +62 -32
- package/dist/scope.js +203 -0
- package/dist/secret-input.js +4 -1
- package/dist/service-main.js +9 -0
- package/dist/service.js +33 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +34 -9
- package/dist/skills.js +12 -2
- package/dist/stream.js +37 -6
- package/dist/transcription.js +54 -6
- package/dist/update.js +30 -6
- package/dist/wire.js +6 -0
- package/dist/work.js +348 -0
- package/package.json +2 -2
- package/dist/blocks.js +0 -51
package/dist/scope.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { mkdirSync, readdirSync, readFileSync, rmdirSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
const LISTING_BYTES = 1 << 28;
|
|
5
|
+
const CGROUPS = '/sys/fs/cgroup';
|
|
6
|
+
const GONE = new Set(['ENOENT', 'ESRCH']);
|
|
7
|
+
function failure(error) {
|
|
8
|
+
return error.code ?? '';
|
|
9
|
+
}
|
|
10
|
+
function listed(environment) {
|
|
11
|
+
const found = new Map();
|
|
12
|
+
const output = execFileSync('ps', [environment ? '-AEww' : '-Aww', '-o', 'pid=,ppid=,sess=,command='], {
|
|
13
|
+
encoding: 'utf8',
|
|
14
|
+
maxBuffer: LISTING_BYTES,
|
|
15
|
+
});
|
|
16
|
+
for (const line of output.split('\n')) {
|
|
17
|
+
const fields = /^\s*(\d+)\s+(\d+)\s+(\S+)\s?(.*)$/.exec(line);
|
|
18
|
+
if (fields !== null)
|
|
19
|
+
found.set(Number(fields[1]), { parent: Number(fields[2]), session: fields[3], command: fields[4] });
|
|
20
|
+
}
|
|
21
|
+
return found;
|
|
22
|
+
}
|
|
23
|
+
function read(pid) {
|
|
24
|
+
try {
|
|
25
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
26
|
+
const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
|
|
27
|
+
const command = readFileSync(`/proc/${pid}/cmdline`, 'utf8').replaceAll('\0', ' ');
|
|
28
|
+
return { parent: Number(fields[1]), session: fields[3], command };
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (GONE.has(failure(error)))
|
|
32
|
+
return null;
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function processes() {
|
|
37
|
+
if (process.platform === 'darwin')
|
|
38
|
+
return listed(false);
|
|
39
|
+
const found = new Map();
|
|
40
|
+
for (const entry of readdirSync('/proc')) {
|
|
41
|
+
const running = /^\d+$/.test(entry) ? read(entry) : null;
|
|
42
|
+
if (running !== null)
|
|
43
|
+
found.set(Number(entry), running);
|
|
44
|
+
}
|
|
45
|
+
return found;
|
|
46
|
+
}
|
|
47
|
+
function descendants(roots, table = processes()) {
|
|
48
|
+
const children = new Map();
|
|
49
|
+
for (const [pid, { parent }] of table)
|
|
50
|
+
children.set(parent, [...(children.get(parent) ?? []), pid]);
|
|
51
|
+
const tree = [];
|
|
52
|
+
const pending = roots.flatMap((root) => children.get(root) ?? []);
|
|
53
|
+
while (pending.length > 0) {
|
|
54
|
+
const pid = pending.pop();
|
|
55
|
+
tree.push(pid);
|
|
56
|
+
pending.push(...(children.get(pid) ?? []));
|
|
57
|
+
}
|
|
58
|
+
return tree;
|
|
59
|
+
}
|
|
60
|
+
function marked(marker, doubted) {
|
|
61
|
+
if (process.platform === 'darwin') {
|
|
62
|
+
return [...listed(true)].filter(([, { command }]) => ` ${command} `.includes(` ${marker} `)).map(([pid]) => pid);
|
|
63
|
+
}
|
|
64
|
+
return readdirSync('/proc')
|
|
65
|
+
.filter((entry) => /^\d+$/.test(entry) && carries(entry, marker, doubted))
|
|
66
|
+
.map(Number);
|
|
67
|
+
}
|
|
68
|
+
function carries(pid, marker, doubted) {
|
|
69
|
+
try {
|
|
70
|
+
return readFileSync(`/proc/${pid}/environ`, 'utf8').split('\0').includes(marker);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (GONE.has(failure(error)))
|
|
74
|
+
return false;
|
|
75
|
+
if (failure(error) !== 'EACCES')
|
|
76
|
+
throw error;
|
|
77
|
+
return doubted && statSync(`/proc/${pid}`, { throwIfNoEntry: false })?.uid === process.getuid();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function killAll(pids) {
|
|
81
|
+
for (const pid of pids) {
|
|
82
|
+
for (const target of [-pid, pid]) {
|
|
83
|
+
try {
|
|
84
|
+
process.kill(target, 'SIGKILL');
|
|
85
|
+
}
|
|
86
|
+
catch { }
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export function killTree(child) {
|
|
91
|
+
killAll([child.pid, ...descendants([child.pid])]);
|
|
92
|
+
}
|
|
93
|
+
export function killMarked(marker) {
|
|
94
|
+
const pids = marked(marker, false);
|
|
95
|
+
killAll([...pids, ...descendants(pids)]);
|
|
96
|
+
}
|
|
97
|
+
export function killDescendants() {
|
|
98
|
+
const pids = marked(`HOUSE_KIT_RESIDENT=${process.pid}`, false);
|
|
99
|
+
killAll([...pids, ...descendants([process.pid, ...pids])]);
|
|
100
|
+
}
|
|
101
|
+
function ownGroup() {
|
|
102
|
+
if (process.platform !== 'linux')
|
|
103
|
+
return null;
|
|
104
|
+
try {
|
|
105
|
+
const line = readFileSync('/proc/self/cgroup', 'utf8')
|
|
106
|
+
.split('\n')
|
|
107
|
+
.find((entry) => entry.startsWith('0::'));
|
|
108
|
+
return line === undefined ? null : join(CGROUPS, line.slice(3));
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function members(group) {
|
|
115
|
+
const pids = readFileSync(join(group, 'cgroup.procs'), 'utf8').split('\n').filter(Boolean).map(Number);
|
|
116
|
+
for (const entry of readdirSync(group, { withFileTypes: true })) {
|
|
117
|
+
if (entry.isDirectory())
|
|
118
|
+
pids.push(...members(join(group, entry.name)));
|
|
119
|
+
}
|
|
120
|
+
return pids;
|
|
121
|
+
}
|
|
122
|
+
export class Scope {
|
|
123
|
+
marker;
|
|
124
|
+
cli;
|
|
125
|
+
group;
|
|
126
|
+
session = null;
|
|
127
|
+
recorded = null;
|
|
128
|
+
constructor(marker, name, cli) {
|
|
129
|
+
this.marker = marker;
|
|
130
|
+
this.cli = cli;
|
|
131
|
+
const own = ownGroup();
|
|
132
|
+
this.group = own === null ? null : join(own, name);
|
|
133
|
+
try {
|
|
134
|
+
if (this.group !== null)
|
|
135
|
+
mkdirSync(this.group);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
this.group = null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
command(command, args) {
|
|
142
|
+
if (this.group === null)
|
|
143
|
+
return [command, args];
|
|
144
|
+
return ['/bin/sh', ['-c', 'echo $$ > "$0/cgroup.procs" 2>/dev/null; exec "$@"', this.group, command, ...args]];
|
|
145
|
+
}
|
|
146
|
+
entered(pid) {
|
|
147
|
+
this.session = process.platform === 'darwin' ? (listed(false).get(pid)?.session ?? null) : String(pid);
|
|
148
|
+
if (this.group === null)
|
|
149
|
+
return;
|
|
150
|
+
const joined = readFileSync(`/proc/${pid}/cgroup`, 'utf8').split('\n').find((entry) => entry.startsWith('0::'));
|
|
151
|
+
if (joined !== undefined && join(CGROUPS, joined.slice(3)) === this.group)
|
|
152
|
+
return;
|
|
153
|
+
this.close();
|
|
154
|
+
this.group = null;
|
|
155
|
+
}
|
|
156
|
+
record() {
|
|
157
|
+
const found = this.found();
|
|
158
|
+
if (found === null)
|
|
159
|
+
return;
|
|
160
|
+
this.recorded = new Set([...found].filter(([, { session }]) => session === this.session).map(([pid]) => pid));
|
|
161
|
+
}
|
|
162
|
+
working() {
|
|
163
|
+
const found = this.found();
|
|
164
|
+
if (found === null)
|
|
165
|
+
return true;
|
|
166
|
+
if (this.recorded === null)
|
|
167
|
+
return found.size > 0;
|
|
168
|
+
return [...found].some(([pid, { session, command }]) => session !== this.session || !(this.recorded.has(pid) || command.startsWith(`${this.cli}-`)));
|
|
169
|
+
}
|
|
170
|
+
empty() {
|
|
171
|
+
return this.found()?.size === 0;
|
|
172
|
+
}
|
|
173
|
+
kill() {
|
|
174
|
+
killMarked(this.marker);
|
|
175
|
+
if (this.group === null)
|
|
176
|
+
return;
|
|
177
|
+
try {
|
|
178
|
+
writeFileSync(join(this.group, 'cgroup.kill'), '1');
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
killAll([...(this.found()?.keys() ?? [])]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
close() {
|
|
185
|
+
if (this.group === null)
|
|
186
|
+
return;
|
|
187
|
+
try {
|
|
188
|
+
rmdirSync(this.group);
|
|
189
|
+
}
|
|
190
|
+
catch { }
|
|
191
|
+
}
|
|
192
|
+
found() {
|
|
193
|
+
try {
|
|
194
|
+
const table = processes();
|
|
195
|
+
const roots = this.group === null ? marked(this.marker, true) : members(this.group);
|
|
196
|
+
const pids = this.group === null ? [...roots, ...descendants(roots, table)] : roots;
|
|
197
|
+
return new Map(pids.flatMap((pid) => (table.has(pid) ? [[pid, table.get(pid)]] : [])));
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
package/dist/secret-input.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { retryDelay } from './api.js';
|
|
1
|
+
import { finalRefusal, retryDelay } from './api.js';
|
|
2
2
|
export async function holdSecretInput(house, subject, steps, waiting) {
|
|
3
3
|
for (let failures = 0; waiting();) {
|
|
4
4
|
try {
|
|
@@ -10,7 +10,10 @@ export async function holdSecretInput(house, subject, steps, waiting) {
|
|
|
10
10
|
failures = 0;
|
|
11
11
|
}
|
|
12
12
|
catch (error) {
|
|
13
|
+
if (finalRefusal(error))
|
|
14
|
+
return null;
|
|
13
15
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
16
|
+
// A wait that did not reach House is held again on the jittered curve for as long as its asker still waits.
|
|
14
17
|
await new Promise((resolve) => setTimeout(resolve, retryDelay(failures++)));
|
|
15
18
|
}
|
|
16
19
|
}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
const UNIT = 'house-kit.service';
|
|
3
|
+
// launchctl and systemctl answer at once, so ten seconds only bounds a stuck service manager.
|
|
4
|
+
const MANAGER_MS = 10_000;
|
|
5
|
+
function manage(command, args) {
|
|
6
|
+
return spawnSync(command, args, { encoding: 'utf8', timeout: MANAGER_MS });
|
|
7
|
+
}
|
|
8
|
+
function start(command, args) {
|
|
9
|
+
const started = manage(command, args);
|
|
10
|
+
if (started.status === 0)
|
|
11
|
+
return;
|
|
12
|
+
const cause = started.error?.message ?? (started.stderr.trim() || `${command} exited with ${started.status ?? 'a signal'}`);
|
|
13
|
+
throw new Error(`the House Kit service could not be started: ${cause}`);
|
|
14
|
+
}
|
|
15
|
+
export function startService() {
|
|
16
|
+
if (process.platform === 'darwin') {
|
|
17
|
+
const service = `gui/${process.getuid()}/house-kit`;
|
|
18
|
+
const printed = manage('launchctl', ['print', service]);
|
|
19
|
+
if (printed.status !== 0 || /^\tstate = running$/m.test(printed.stdout))
|
|
20
|
+
return false;
|
|
21
|
+
start('launchctl', ['kickstart', service]);
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
for (const scope of [['--user'], []]) {
|
|
25
|
+
if (manage('systemctl', [...scope, 'is-enabled', '--quiet', UNIT]).status !== 0)
|
|
26
|
+
continue;
|
|
27
|
+
if (manage('systemctl', [...scope, 'is-active', '--quiet', UNIT]).status === 0)
|
|
28
|
+
return false;
|
|
29
|
+
start('systemctl', [...scope, 'start', UNIT]);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
package/dist/shell.js
ADDED
package/dist/sign-in.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { createInterface } from 'node:readline';
|
|
3
|
-
import { killTree } from './acp.js';
|
|
4
3
|
import { CLIS, withoutProxy } from './clis.js';
|
|
4
|
+
import { killTree } from './scope.js';
|
|
5
5
|
import { holdSecretInput } from './secret-input.js';
|
|
6
6
|
const ESCAPE = /\u001b\[[\d;]*m/g;
|
|
7
7
|
const LINK = /https:\/\/\S+/;
|
|
@@ -12,6 +12,9 @@ function causeOf(error) {
|
|
|
12
12
|
function logged(error) {
|
|
13
13
|
process.stderr.write(`kit: ${causeOf(error)}\n`);
|
|
14
14
|
}
|
|
15
|
+
function endOf(signal) {
|
|
16
|
+
return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
|
|
17
|
+
}
|
|
15
18
|
export class SignIns {
|
|
16
19
|
house;
|
|
17
20
|
agents;
|
|
@@ -30,24 +33,43 @@ export class SignIns {
|
|
|
30
33
|
return;
|
|
31
34
|
this.running.add(input.input_id);
|
|
32
35
|
this.changed();
|
|
33
|
-
this.
|
|
34
|
-
.then(() => this.agents.reread(input.cli))
|
|
35
|
-
.then(() => this.house.deliver(`/kit/inputs/${input.input_id}/ack`, {}))
|
|
36
|
+
this.signIn(input)
|
|
36
37
|
.catch(logged)
|
|
37
38
|
.finally(() => {
|
|
38
39
|
this.running.delete(input.input_id);
|
|
39
40
|
this.changed();
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
|
-
async
|
|
43
|
+
async signIn(input) {
|
|
44
|
+
const ending = new AbortController();
|
|
45
|
+
// House stops waiting for a sign-in ten minutes after it wrote it and sends the seconds left, so its install and login end with the page.
|
|
46
|
+
const timer = setTimeout(() => ending.abort(), input.ends_in * 1000);
|
|
47
|
+
const ended = await Promise.race([this.signedIn(input, ending.signal), endOf(ending.signal)]);
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
if (ended)
|
|
50
|
+
process.stderr.write(`kit: the ${input.cli} sign-in reached the end House set for it\n`);
|
|
51
|
+
await this.house.deliver(`/kit/inputs/${input.input_id}/ack`, {});
|
|
52
|
+
if (!ended)
|
|
53
|
+
await this.agents.reread(input.cli);
|
|
54
|
+
}
|
|
55
|
+
async signedIn(input, signal) {
|
|
56
|
+
await this.login(input, signal);
|
|
57
|
+
if (!signal.aborted)
|
|
58
|
+
await this.agents.recheck(input.cli, signal);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
async login(input, signal) {
|
|
43
62
|
let cli;
|
|
44
63
|
try {
|
|
45
|
-
cli = await this.agents.located(input.cli);
|
|
64
|
+
cli = await this.agents.located(input.cli, signal);
|
|
46
65
|
}
|
|
47
66
|
catch (error) {
|
|
48
|
-
|
|
67
|
+
if (!signal.aborted)
|
|
68
|
+
process.stderr.write(`kit: the ${input.cli} sign-in failed: ${causeOf(error)}\n`);
|
|
49
69
|
return;
|
|
50
70
|
}
|
|
71
|
+
if (signal.aborted)
|
|
72
|
+
return;
|
|
51
73
|
const login = CLIS[input.cli].login;
|
|
52
74
|
const child = spawn(cli, login.args, {
|
|
53
75
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
@@ -55,6 +77,8 @@ export class SignIns {
|
|
|
55
77
|
env: withoutProxy(),
|
|
56
78
|
});
|
|
57
79
|
const running = () => child.exitCode === null && child.signalCode === null;
|
|
80
|
+
const stop = () => killTree(child);
|
|
81
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
58
82
|
let link;
|
|
59
83
|
let code;
|
|
60
84
|
let last = '';
|
|
@@ -91,9 +115,10 @@ export class SignIns {
|
|
|
91
115
|
createInterface({ input: output }).on('line', read);
|
|
92
116
|
const failed = await new Promise((resolve) => {
|
|
93
117
|
child.on('error', (error) => resolve(error.message));
|
|
94
|
-
child.on('close', (status,
|
|
118
|
+
child.on('close', (status, killed) => resolve(status === 0 ? null : `exited with ${status ?? killed}${last ? `: ${last}` : ''}`));
|
|
95
119
|
});
|
|
96
|
-
|
|
120
|
+
signal.removeEventListener('abort', stop);
|
|
121
|
+
if (failed !== null && !signal.aborted)
|
|
97
122
|
process.stderr.write(`kit: the ${input.cli} sign-in failed: ${failed}\n`);
|
|
98
123
|
}
|
|
99
124
|
}
|
package/dist/skills.js
CHANGED
|
@@ -6,7 +6,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
6
6
|
import { KIT_VERSION } from './clis.js';
|
|
7
7
|
import { kitHome, readConfiguration, readHome, writeHome } from './home.js';
|
|
8
8
|
import { refusalOf } from './refusals.js';
|
|
9
|
-
export const HOW_WE_WORK = '/private/how-we-work.md';
|
|
9
|
+
export const HOW_WE_WORK = '/rooms/private/how-we-work.md';
|
|
10
10
|
const SKILL_SET = fileURLToPath(new URL('../skills/', import.meta.url));
|
|
11
11
|
const HOW_WE_WORK_TEXT = readFileSync(new URL('../how-we-work.md', import.meta.url), 'utf8');
|
|
12
12
|
const RECORD = 'skill-set.json';
|
|
@@ -49,6 +49,14 @@ async function install() {
|
|
|
49
49
|
await writeHome(RECORD, { version: KIT_VERSION, names });
|
|
50
50
|
}
|
|
51
51
|
export async function placeSkillSet() {
|
|
52
|
+
try {
|
|
53
|
+
await replaceSkillSet();
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
process.stderr.write(`kit: the skills could not be placed: ${error.message}\n`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async function replaceSkillSet() {
|
|
52
60
|
const accepted = (await readConfiguration()).skills;
|
|
53
61
|
const installed = await readHome(RECORD);
|
|
54
62
|
if (accepted && installed?.version === KIT_VERSION)
|
|
@@ -63,7 +71,9 @@ export async function placeSkillSet() {
|
|
|
63
71
|
await rm(join(kitHome(), RECORD), { force: true });
|
|
64
72
|
}
|
|
65
73
|
export async function createHowWeWork(bridge) {
|
|
66
|
-
if ((await
|
|
74
|
+
if (!(await readConfiguration()).skills || (await readHome(RECORD))?.version === undefined)
|
|
75
|
+
return;
|
|
76
|
+
if (await present(join(kitHome(), CREATED)))
|
|
67
77
|
return;
|
|
68
78
|
const created = await bridge.tool('edit', { changes: [{ op: 'create', path: HOW_WE_WORK, content: HOW_WE_WORK_TEXT }] });
|
|
69
79
|
if (created.isError === true) {
|
package/dist/stream.js
CHANGED
|
@@ -1,17 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { subscribe } from 'node:diagnostics_channel';
|
|
2
|
+
import { finalRefusal, HouseRefusal, retryDelay } from './api.js';
|
|
3
|
+
import { end, REPLACED, signInAgain, updateRequired } from './end.js';
|
|
2
4
|
import { readEnrolment } from './home.js';
|
|
3
|
-
|
|
5
|
+
import { relayed } from './relay.js';
|
|
6
|
+
import { KIT_WIRE, tooOld } from './wire.js';
|
|
7
|
+
const PATH = '/kit/stream';
|
|
8
|
+
const SUBPROTOCOL = `house.kit.stream.${KIT_WIRE}`;
|
|
9
|
+
const REFUSAL_HEADER = 'x-house-refusal';
|
|
4
10
|
function streamUrl(house) {
|
|
5
|
-
const url = new URL(
|
|
11
|
+
const url = new URL(PATH, house);
|
|
6
12
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
7
13
|
return url;
|
|
8
14
|
}
|
|
15
|
+
function header(headers, name) {
|
|
16
|
+
for (let index = 0; index < headers.length; index += 2) {
|
|
17
|
+
if (headers[index].toString('latin1').toLowerCase() === name)
|
|
18
|
+
return headers[index + 1].toString('latin1');
|
|
19
|
+
}
|
|
20
|
+
return '';
|
|
21
|
+
}
|
|
22
|
+
let refused = null;
|
|
23
|
+
subscribe('undici:request:headers', (message) => {
|
|
24
|
+
const { request, response } = message;
|
|
25
|
+
if (!request.path.endsWith(PATH))
|
|
26
|
+
return;
|
|
27
|
+
refused = new HouseRefusal(PATH, response.statusCode, header(response.headers, REFUSAL_HEADER) || response.statusText);
|
|
28
|
+
});
|
|
9
29
|
export function holdStream(handlers) {
|
|
10
30
|
let socket = null;
|
|
11
31
|
let failures = 0;
|
|
12
32
|
const connect = async () => {
|
|
13
33
|
const { house, credential } = (await readEnrolment());
|
|
14
|
-
|
|
34
|
+
refused = null;
|
|
35
|
+
const opening = new WebSocket(relayed(streamUrl(house)), {
|
|
15
36
|
protocols: [SUBPROTOCOL],
|
|
16
37
|
headers: { authorization: `Bearer ${credential}` },
|
|
17
38
|
});
|
|
@@ -30,8 +51,18 @@ export function holdStream(handlers) {
|
|
|
30
51
|
if (socket === opening)
|
|
31
52
|
socket = null;
|
|
32
53
|
process.stderr.write(`kit: control stream closed ${event.code} ${event.reason}\n`);
|
|
33
|
-
|
|
34
|
-
|
|
54
|
+
if (event.reason === 'replaced')
|
|
55
|
+
end(REPLACED);
|
|
56
|
+
if (event.reason === 'credential_rejected' || refused?.status === 401) {
|
|
57
|
+
void signInAgain(credential);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (refused !== null && tooOld(refused.text))
|
|
61
|
+
updateRequired(house);
|
|
62
|
+
if (finalRefusal(refused))
|
|
63
|
+
end(refused.message);
|
|
64
|
+
// A closed socket reopens in one to three seconds, spread so Kits one restart closed return apart; an opening House refused as retryable or left unanswered backs off on the jittered curve.
|
|
65
|
+
setTimeout(() => void connect(), opened ? 1000 + Math.random() * 2000 : retryDelay(failures++));
|
|
35
66
|
};
|
|
36
67
|
};
|
|
37
68
|
void connect();
|
package/dist/transcription.js
CHANGED
|
@@ -29,6 +29,10 @@ export const ENGINES = {
|
|
|
29
29
|
};
|
|
30
30
|
const CHUNK_SECONDS = 20;
|
|
31
31
|
let held = Promise.resolve();
|
|
32
|
+
let holding = Promise.resolve();
|
|
33
|
+
let setting = null;
|
|
34
|
+
let downloading = null;
|
|
35
|
+
let readiness = { installed: false, failure: null };
|
|
32
36
|
function directory() {
|
|
33
37
|
return join(kitHome(), 'transcription');
|
|
34
38
|
}
|
|
@@ -36,6 +40,7 @@ function installed(artifact) {
|
|
|
36
40
|
return join(directory(), artifact.sha256);
|
|
37
41
|
}
|
|
38
42
|
async function executed(command, args, signal) {
|
|
43
|
+
// A download or a transcription runs as long as its size needs; the setting turning off or the turn's interrupt or end aborts it instead.
|
|
39
44
|
const ran = await run(command, args, 0, false, signal);
|
|
40
45
|
if (ran.status !== 0)
|
|
41
46
|
throw new Error(failureOf(ran, basename(command)));
|
|
@@ -54,6 +59,12 @@ async function sha256(path) {
|
|
|
54
59
|
await pipeline(createReadStream(path), hash);
|
|
55
60
|
return hash.digest('hex');
|
|
56
61
|
}
|
|
62
|
+
function causeOf(error) {
|
|
63
|
+
return error instanceof Error ? error.message : String(error);
|
|
64
|
+
}
|
|
65
|
+
function pinnedHere() {
|
|
66
|
+
return ENGINES[`${process.platform}-${process.arch}`];
|
|
67
|
+
}
|
|
57
68
|
async function install(artifacts, signal) {
|
|
58
69
|
await mkdir(directory(), { recursive: true });
|
|
59
70
|
const pinned = new Set(artifacts.map((artifact) => artifact.sha256));
|
|
@@ -68,6 +79,7 @@ async function install(artifacts, signal) {
|
|
|
68
79
|
try {
|
|
69
80
|
for (const artifact of missing) {
|
|
70
81
|
const part = `${installed(artifact)}.part`;
|
|
82
|
+
// Three retries carry a large download past a dropped connection before the download fails.
|
|
71
83
|
await executed('curl', ['-fsSL', '--retry', '3', '-o', part, artifact.url], signal);
|
|
72
84
|
if ((await sha256(part)) !== artifact.sha256)
|
|
73
85
|
throw new Error(`${artifact.url} does not match its sha256`);
|
|
@@ -83,12 +95,46 @@ async function install(artifacts, signal) {
|
|
|
83
95
|
await rm(`${installed(artifact)}.part`, { force: true });
|
|
84
96
|
}
|
|
85
97
|
}
|
|
98
|
+
function reported(next) {
|
|
99
|
+
const changed = next.installed !== readiness.installed || next.failure !== readiness.failure;
|
|
100
|
+
readiness = next;
|
|
101
|
+
return changed;
|
|
102
|
+
}
|
|
103
|
+
async function settle(transcribe) {
|
|
104
|
+
if (transcribe === setting)
|
|
105
|
+
return false;
|
|
106
|
+
setting = transcribe;
|
|
107
|
+
if (!transcribe) {
|
|
108
|
+
await rm(directory(), { recursive: true, force: true });
|
|
109
|
+
return reported({ installed: false, failure: null });
|
|
110
|
+
}
|
|
111
|
+
const download = new AbortController();
|
|
112
|
+
downloading = download;
|
|
113
|
+
try {
|
|
114
|
+
const pinned = pinnedHere();
|
|
115
|
+
await install([pinned.decoder, pinned.engine, MODEL], download.signal);
|
|
116
|
+
return reported({ installed: true, failure: null });
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
await rm(directory(), { recursive: true, force: true });
|
|
120
|
+
return reported({ installed: false, failure: download.signal.aborted ? null : causeOf(error) });
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
downloading = null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export function holdTranscription(transcribe) {
|
|
127
|
+
if (!transcribe)
|
|
128
|
+
downloading?.abort();
|
|
129
|
+
const settled = holding.then(() => settle(transcribe));
|
|
130
|
+
holding = settled.catch(() => undefined);
|
|
131
|
+
return settled;
|
|
132
|
+
}
|
|
133
|
+
export function transcriptionReport() {
|
|
134
|
+
return readiness;
|
|
135
|
+
}
|
|
86
136
|
async function transcribed(path, signal) {
|
|
87
|
-
const
|
|
88
|
-
const pinned = ENGINES[platform];
|
|
89
|
-
if (pinned === undefined)
|
|
90
|
-
throw new Error(`no transcription engine is pinned for ${platform}`);
|
|
91
|
-
await install([pinned.decoder, pinned.engine, MODEL], signal);
|
|
137
|
+
const pinned = pinnedHere();
|
|
92
138
|
const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
|
|
93
139
|
try {
|
|
94
140
|
await executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', path, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
|
|
@@ -106,6 +152,8 @@ async function transcribed(path, signal) {
|
|
|
106
152
|
}
|
|
107
153
|
}
|
|
108
154
|
export async function transcribe(path, signal) {
|
|
155
|
+
if (!readiness.installed)
|
|
156
|
+
return null;
|
|
109
157
|
const before = held;
|
|
110
158
|
let release;
|
|
111
159
|
const mine = new Promise((resolve) => {
|
|
@@ -114,7 +162,7 @@ export async function transcribe(path, signal) {
|
|
|
114
162
|
held = Promise.all([before, mine]);
|
|
115
163
|
try {
|
|
116
164
|
await unlessAborted(before, signal);
|
|
117
|
-
return await transcribed(path, signal);
|
|
165
|
+
return readiness.installed ? await transcribed(path, signal) : null;
|
|
118
166
|
}
|
|
119
167
|
finally {
|
|
120
168
|
release();
|
package/dist/update.js
CHANGED
|
@@ -4,8 +4,8 @@ import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
|
4
4
|
import { homedir } from 'node:os';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
6
|
import { run } from './clis.js';
|
|
7
|
-
import { kitHome } from './home.js';
|
|
8
|
-
|
|
7
|
+
import { kitHome, workspaceRoot } from './home.js';
|
|
8
|
+
import { service, throughSudo } from './placement.js';
|
|
9
9
|
function logged(error) {
|
|
10
10
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
11
11
|
}
|
|
@@ -21,6 +21,21 @@ function outcome() {
|
|
|
21
21
|
function unit(inputId) {
|
|
22
22
|
return `house-kit-update-${inputId}`;
|
|
23
23
|
}
|
|
24
|
+
function manager() {
|
|
25
|
+
return service() === 'user' ? ['--user'] : [];
|
|
26
|
+
}
|
|
27
|
+
function elevated() {
|
|
28
|
+
return throughSudo() ? ['sudo', '-n'] : [];
|
|
29
|
+
}
|
|
30
|
+
function account() {
|
|
31
|
+
return throughSudo() ? [`--uid=${process.getuid()}`, `--gid=${process.getgid()}`] : [];
|
|
32
|
+
}
|
|
33
|
+
function placement() {
|
|
34
|
+
return service() === 'user' ? ['--workspace', workspaceRoot()] : ['--linux'];
|
|
35
|
+
}
|
|
36
|
+
function record() {
|
|
37
|
+
return `outcome=$1; shift; bash "$0" "$@"; printf %s "$?" > "$outcome"; ${[...elevated(), 'systemctl', ...manager(), 'start', 'house-kit.service'].join(' ')}`;
|
|
38
|
+
}
|
|
24
39
|
async function pendingInput() {
|
|
25
40
|
try {
|
|
26
41
|
return JSON.parse(await readFile(pending(), 'utf8')).input_id;
|
|
@@ -40,27 +55,36 @@ async function finished(inputId) {
|
|
|
40
55
|
const ack = await recorded();
|
|
41
56
|
if (ack !== null)
|
|
42
57
|
return ack;
|
|
43
|
-
|
|
58
|
+
// systemctl answers at once, so ten seconds only bounds a stuck service manager.
|
|
59
|
+
if ((await run('systemctl', [...manager(), 'is-active', '--quiet', unit(inputId)], 10_000)).status !== 0) {
|
|
44
60
|
return (await recorded()) ?? { refused: 'the Kit bootstrap did not finish' };
|
|
45
61
|
}
|
|
62
|
+
// The bootstrap runs for minutes, so checking each second sees its outcome within a second at little cost.
|
|
46
63
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
47
64
|
}
|
|
48
65
|
}
|
|
49
66
|
function rerun(script, inputId, flags) {
|
|
50
67
|
return new Promise((resolve) => {
|
|
51
|
-
const
|
|
68
|
+
const [command, ...args] = [
|
|
69
|
+
...elevated(),
|
|
70
|
+
'systemd-run',
|
|
71
|
+
...manager(),
|
|
72
|
+
...account(),
|
|
52
73
|
'--wait',
|
|
53
74
|
'--collect',
|
|
54
75
|
'--quiet',
|
|
55
76
|
`--unit=${unit(inputId)}`,
|
|
56
77
|
`--setenv=HOME=${homedir()}`,
|
|
78
|
+
`--setenv=HOUSE_KIT_HOME=${kitHome()}`,
|
|
57
79
|
'/bin/sh',
|
|
58
80
|
'-c',
|
|
59
|
-
|
|
81
|
+
record(),
|
|
60
82
|
script,
|
|
61
83
|
outcome(),
|
|
84
|
+
...placement(),
|
|
62
85
|
...flags,
|
|
63
|
-
]
|
|
86
|
+
];
|
|
87
|
+
const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
64
88
|
child.on('error', () => resolve({ refused: 'no service manager runs this Kit' }));
|
|
65
89
|
child.on('close', () => resolve(null));
|
|
66
90
|
});
|