@agentshouse/kit 0.2.1-c681dfca497a8 → 0.2.1-c70284f7a7466
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -15
- package/dist/agents.js +51 -3
- package/dist/api.js +1 -1
- package/dist/app-git.js +10 -26
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +2 -5
- package/dist/chatgpt.js +7 -1
- package/dist/clis.js +11 -1
- package/dist/conversations.js +98 -28
- package/dist/copies.js +50 -25
- package/dist/end.js +3 -2
- package/dist/events.js +4 -0
- package/dist/git.js +2 -3
- package/dist/home.js +31 -13
- package/dist/house-cli.js +291 -0
- package/dist/house-main.js +7 -289
- package/dist/instructions.js +4 -28
- package/dist/kit-cli.js +68 -0
- package/dist/kit-main.js +8 -67
- package/dist/login.js +33 -8
- package/dist/logout.js +0 -2
- package/dist/placement.js +2 -20
- package/dist/resident.js +51 -10
- package/dist/skills.js +71 -12
- package/dist/stream.js +58 -12
- package/dist/transcription.js +3 -3
- package/dist/work.js +3 -5
- package/how-we-work.md +2 -0
- package/package.json +1 -1
package/dist/kit-cli.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { setGlobalProxyFromEnv } from 'node:http';
|
|
2
|
+
import { parseArgs } from 'node:util';
|
|
3
|
+
import { answerGitCredential } from './app-git.js';
|
|
4
|
+
import { chatgpt } from './chatgpt.js';
|
|
5
|
+
import { login } from './login.js';
|
|
6
|
+
import { logout } from './logout.js';
|
|
7
|
+
import { resident } from './resident.js';
|
|
8
|
+
const USAGE = 'usage: kit <login|logout|resident|chatgpt>\n';
|
|
9
|
+
const COMMANDS = new Map([
|
|
10
|
+
[
|
|
11
|
+
'logout',
|
|
12
|
+
{
|
|
13
|
+
usage: 'usage: kit logout',
|
|
14
|
+
about: "disconnects this computer, stops House Kit and ends your own agent's connection",
|
|
15
|
+
operands: 0,
|
|
16
|
+
run: logout,
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
[
|
|
20
|
+
'resident',
|
|
21
|
+
{
|
|
22
|
+
usage: 'usage: kit resident',
|
|
23
|
+
about: "keeps this Environment connected; House Kit's service runs it",
|
|
24
|
+
operands: 0,
|
|
25
|
+
run: resident,
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
[
|
|
29
|
+
'git-credential',
|
|
30
|
+
{
|
|
31
|
+
usage: 'usage: kit git-credential <get|store|erase>',
|
|
32
|
+
about: "answers git with your own agent's House credential; git runs it",
|
|
33
|
+
operands: 1,
|
|
34
|
+
run: ([operation]) => answerGitCredential(operation),
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
]);
|
|
38
|
+
const [command, ...argv] = process.argv.slice(2);
|
|
39
|
+
setGlobalProxyFromEnv();
|
|
40
|
+
async function run() {
|
|
41
|
+
if (command === 'login')
|
|
42
|
+
return login(argv);
|
|
43
|
+
if (command === 'chatgpt')
|
|
44
|
+
return chatgpt(argv);
|
|
45
|
+
if (command === '--help') {
|
|
46
|
+
parseArgs({ args: argv });
|
|
47
|
+
process.stdout.write(USAGE);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const chosen = COMMANDS.get(command ?? '');
|
|
51
|
+
if (chosen === undefined) {
|
|
52
|
+
process.stderr.write(USAGE);
|
|
53
|
+
process.exitCode = 2;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const { values, positionals } = parseArgs({ args: argv, options: { help: { type: 'boolean', default: false } }, allowPositionals: true });
|
|
57
|
+
if (positionals.length > chosen.operands)
|
|
58
|
+
throw new Error(`unexpected argument ${positionals[chosen.operands]}; ${chosen.usage}`);
|
|
59
|
+
if (values.help) {
|
|
60
|
+
process.stdout.write(`${chosen.usage}\n ${chosen.about}\n`);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
return chosen.run(positionals);
|
|
64
|
+
}
|
|
65
|
+
run().catch((error) => {
|
|
66
|
+
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
67
|
+
process.exitCode = 1;
|
|
68
|
+
});
|
package/dist/kit-main.js
CHANGED
|
@@ -1,69 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { chatgpt } from './chatgpt.js';
|
|
6
|
-
import { login } from './login.js';
|
|
7
|
-
import { logout } from './logout.js';
|
|
8
|
-
import { resident } from './resident.js';
|
|
9
|
-
const USAGE = 'usage: kit <login|logout|resident|chatgpt>\n';
|
|
10
|
-
const COMMANDS = new Map([
|
|
11
|
-
[
|
|
12
|
-
'logout',
|
|
13
|
-
{
|
|
14
|
-
usage: 'usage: kit logout',
|
|
15
|
-
about: "disconnects this computer, stops House Kit and ends your own agent's connection",
|
|
16
|
-
operands: 0,
|
|
17
|
-
run: logout,
|
|
18
|
-
},
|
|
19
|
-
],
|
|
20
|
-
[
|
|
21
|
-
'resident',
|
|
22
|
-
{
|
|
23
|
-
usage: 'usage: kit resident',
|
|
24
|
-
about: "keeps this Environment connected; House Kit's service runs it",
|
|
25
|
-
operands: 0,
|
|
26
|
-
run: resident,
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
[
|
|
30
|
-
'git-credential',
|
|
31
|
-
{
|
|
32
|
-
usage: 'usage: kit git-credential <get|store|erase>',
|
|
33
|
-
about: "answers git with your own agent's House credential; git runs it",
|
|
34
|
-
operands: 1,
|
|
35
|
-
run: ([operation]) => answerGitCredential(operation),
|
|
36
|
-
},
|
|
37
|
-
],
|
|
38
|
-
]);
|
|
39
|
-
const [command, ...argv] = process.argv.slice(2);
|
|
40
|
-
setGlobalProxyFromEnv();
|
|
41
|
-
async function run() {
|
|
42
|
-
if (command === 'login')
|
|
43
|
-
return login(argv);
|
|
44
|
-
if (command === 'chatgpt')
|
|
45
|
-
return chatgpt(argv);
|
|
46
|
-
if (command === '--help') {
|
|
47
|
-
parseArgs({ args: argv });
|
|
48
|
-
process.stdout.write(USAGE);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
const chosen = COMMANDS.get(command ?? '');
|
|
52
|
-
if (chosen === undefined) {
|
|
53
|
-
process.stderr.write(USAGE);
|
|
54
|
-
process.exitCode = 2;
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const { values, positionals } = parseArgs({ args: argv, options: { help: { type: 'boolean', default: false } }, allowPositionals: true });
|
|
58
|
-
if (positionals.length > chosen.operands)
|
|
59
|
-
throw new Error(`unexpected argument ${positionals[chosen.operands]}; ${chosen.usage}`);
|
|
60
|
-
if (values.help) {
|
|
61
|
-
process.stdout.write(`${chosen.usage}\n ${chosen.about}\n`);
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
return chosen.run(positionals);
|
|
65
|
-
}
|
|
66
|
-
run().catch((error) => {
|
|
67
|
-
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
2
|
+
const [major = 0, minor = 0] = process.versions.node.split('.').map(Number);
|
|
3
|
+
if (major < 24 || (major === 24 && minor < 14)) {
|
|
4
|
+
process.stderr.write(`kit: the Kit needs Node 24.14.0 or newer; this is ${process.versions.node}\n`);
|
|
68
5
|
process.exitCode = 1;
|
|
69
|
-
}
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
await import('./kit-cli.js');
|
|
9
|
+
}
|
|
10
|
+
export {};
|
package/dist/login.js
CHANGED
|
@@ -2,11 +2,12 @@ import { spawn } from 'node:child_process';
|
|
|
2
2
|
import { createHash, randomBytes } from 'node:crypto';
|
|
3
3
|
import { hostname } from 'node:os';
|
|
4
4
|
import { parseArgs } from 'node:util';
|
|
5
|
-
import { post, retrying } from './api.js';
|
|
5
|
+
import { HouseRefusal, post, retrying } from './api.js';
|
|
6
6
|
import { placeAppGit } from './app-git.js';
|
|
7
7
|
import { writeCodexRule } from './clis.js';
|
|
8
|
-
import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
|
|
8
|
+
import { forgetDisconnected, forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent, } from './home.js';
|
|
9
9
|
import { callHouse, rpc } from './mcp.js';
|
|
10
|
+
import { refusalOf } from './refusals.js';
|
|
10
11
|
import { selectRelay } from './relay.js';
|
|
11
12
|
import { startService } from './service.js';
|
|
12
13
|
import { placeSkillSet } from './skills.js';
|
|
@@ -35,6 +36,16 @@ const USAGE = `usage: kit login [--house <origin>] [--environment <id>] [--rooms
|
|
|
35
36
|
--no-private give this Environment every Room but your Private Room
|
|
36
37
|
--code <code> connect this machine with a login code
|
|
37
38
|
`;
|
|
39
|
+
function connected(house, named, environment) {
|
|
40
|
+
if (named === undefined)
|
|
41
|
+
return `Environment ${environment} is connected to ${house}.\n`;
|
|
42
|
+
if (named === environment)
|
|
43
|
+
return `Environment ${environment} is reconnected to ${house}.\n`;
|
|
44
|
+
return `Environment ${named} stays with its account; this computer is connected to ${house} as new Environment ${environment} of the account that confirmed.\n`;
|
|
45
|
+
}
|
|
46
|
+
function gone(error) {
|
|
47
|
+
return error instanceof HouseRefusal && refusalOf(error.text)?.code === 'kit_environment_not_found';
|
|
48
|
+
}
|
|
38
49
|
function chosenRooms(rooms, noPrivate) {
|
|
39
50
|
if (rooms !== undefined && noPrivate)
|
|
40
51
|
throw new Error('--rooms and --no-private are two choices of Rooms; pass one');
|
|
@@ -73,9 +84,11 @@ export async function login(argv) {
|
|
|
73
84
|
selectRelay(relay);
|
|
74
85
|
const house = values.house ?? enrolled?.house ?? DEFAULT_HOUSE;
|
|
75
86
|
const replaces = process.env.HOUSE_KIT_REPLACES;
|
|
76
|
-
const
|
|
87
|
+
const remembered = replaces === undefined && values.environment === undefined && enrolled?.house === house ? enrolled.environment : undefined;
|
|
88
|
+
const environment = replaces === undefined ? (values.environment ?? remembered) : undefined;
|
|
77
89
|
const ownAgent = await readOwnAgent();
|
|
78
90
|
let held = false;
|
|
91
|
+
let named = environment;
|
|
79
92
|
let exchanged;
|
|
80
93
|
if (values.code !== undefined) {
|
|
81
94
|
exchanged = await post(house, '/kit/token', { code: values.code, label: hostname() });
|
|
@@ -83,15 +96,27 @@ export async function login(argv) {
|
|
|
83
96
|
else {
|
|
84
97
|
held = ownAgent !== null && ownAgent.house === house && (await accepted(house, ownAgent.credential));
|
|
85
98
|
const verifier = randomBytes(32).toString('base64url');
|
|
86
|
-
const
|
|
99
|
+
const start = () => retrying(() => post(house, '/kit', {
|
|
87
100
|
label: hostname(),
|
|
88
|
-
...(
|
|
101
|
+
...(named === undefined
|
|
89
102
|
? { act: 'new_environment', ...(replaces === undefined ? {} : { replaces }) }
|
|
90
|
-
: { act: 'existing_environment', environment_id:
|
|
103
|
+
: { act: 'existing_environment', environment_id: named }),
|
|
91
104
|
...rooms,
|
|
92
105
|
code_challenge: createHash('sha256').update(verifier).digest('base64url'),
|
|
93
106
|
...(held ? {} : { headless: true }),
|
|
94
107
|
}));
|
|
108
|
+
let started;
|
|
109
|
+
try {
|
|
110
|
+
started = await start();
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (remembered === undefined || !gone(error))
|
|
114
|
+
throw error;
|
|
115
|
+
await forgetDisconnected();
|
|
116
|
+
process.stdout.write(`House no longer holds Environment ${remembered}; connecting this computer as a new one.\n`);
|
|
117
|
+
named = undefined;
|
|
118
|
+
started = await start();
|
|
119
|
+
}
|
|
95
120
|
process.stdout.write(`Open this link and confirm: ${started.start_url}\n`);
|
|
96
121
|
open(started.start_url);
|
|
97
122
|
exchanged = await retrying(() => post(house, '/kit/token', { login: started.login, code_verifier: verifier }), undefined, () => POLL_INTERVAL_MS);
|
|
@@ -105,7 +130,7 @@ export async function login(argv) {
|
|
|
105
130
|
environment: exchanged.environment,
|
|
106
131
|
credential: exchanged.credential,
|
|
107
132
|
});
|
|
108
|
-
process.stdout.write(
|
|
133
|
+
process.stdout.write(connected(house, named, exchanged.environment));
|
|
109
134
|
if (exchanged.headless_credential !== undefined) {
|
|
110
135
|
await writeOwnAgent({ house, ...relayRecord, user: exchanged.user, credential: exchanged.headless_credential });
|
|
111
136
|
}
|
|
@@ -116,7 +141,7 @@ export async function login(argv) {
|
|
|
116
141
|
else if (held && ownAgent !== null) {
|
|
117
142
|
await writeOwnAgent({ house, ...relayRecord, user: ownAgent.user, credential: ownAgent.credential });
|
|
118
143
|
}
|
|
119
|
-
await placeAppGit(
|
|
144
|
+
await placeAppGit(await readOwnAgent()).catch((error) => process.stderr.write(`kit: ${error.message}\n`));
|
|
120
145
|
try {
|
|
121
146
|
startService();
|
|
122
147
|
}
|
package/dist/logout.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { HouseRefusal, post } from './api.js';
|
|
2
|
-
import { forgetAppGit } from './app-git.js';
|
|
3
2
|
import { forgetEnrolment, forgetOwnAgent, readEnrolment } from './home.js';
|
|
4
3
|
export async function logout() {
|
|
5
4
|
const enrolled = await readEnrolment();
|
|
@@ -13,7 +12,6 @@ export async function logout() {
|
|
|
13
12
|
}
|
|
14
13
|
await forgetEnrolment(enrolled);
|
|
15
14
|
}
|
|
16
|
-
await forgetAppGit();
|
|
17
15
|
await forgetOwnAgent();
|
|
18
16
|
process.stdout.write(enrolled === null
|
|
19
17
|
? 'This computer is not connected to House.\n'
|
package/dist/placement.js
CHANGED
|
@@ -2,7 +2,6 @@ import { readFileSync } from 'node:fs';
|
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { run } from './clis.js';
|
|
4
4
|
import { kitHome } from './home.js';
|
|
5
|
-
import { relayOrigin } from './relay.js';
|
|
6
5
|
export function service() {
|
|
7
6
|
if (process.platform !== 'linux' || !process.env.INVOCATION_ID)
|
|
8
7
|
return null;
|
|
@@ -19,9 +18,9 @@ export async function updatesItself() {
|
|
|
19
18
|
// sudo answers at once, so ten seconds only bounds a stuck sudo.
|
|
20
19
|
return (await run('sudo', ['-n', 'true'], 10_000)).status === 0;
|
|
21
20
|
}
|
|
22
|
-
function
|
|
21
|
+
export function installCommand() {
|
|
23
22
|
try {
|
|
24
|
-
return readFileSync(join(kitHome(), '
|
|
23
|
+
return readFileSync(join(kitHome(), 'install-command'), 'utf8').trim();
|
|
25
24
|
}
|
|
26
25
|
catch (error) {
|
|
27
26
|
if (error.code === 'ENOENT')
|
|
@@ -29,20 +28,3 @@ function installed() {
|
|
|
29
28
|
throw error;
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
|
-
function entries(house) {
|
|
33
|
-
const host = new URL(house).host;
|
|
34
|
-
const relay = relayOrigin();
|
|
35
|
-
if (relay === undefined)
|
|
36
|
-
return [`curl -fsSL ${host}/kit | sh`, `irm ${host}/kit.ps1 | iex`];
|
|
37
|
-
const entry = `${relay}/${host}/kit`;
|
|
38
|
-
return [`curl -fsSL ${entry} | HOUSE_KIT_RELAY=${relay} sh`, `$env:HOUSE_KIT_RELAY='${relay}'; irm ${entry}.ps1 | iex`];
|
|
39
|
-
}
|
|
40
|
-
export function installCommand(house) {
|
|
41
|
-
const [shell, windows] = entries(house);
|
|
42
|
-
const placement = installed();
|
|
43
|
-
if (placement === 'native')
|
|
44
|
-
return shell;
|
|
45
|
-
if (placement === 'server')
|
|
46
|
-
return `${shell} -s -- --linux`;
|
|
47
|
-
return `${shell} -s -- --container, or ${windows} on Windows`;
|
|
48
|
-
}
|
package/dist/resident.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { once } from 'node:events';
|
|
2
2
|
import { watch } from 'node:fs';
|
|
3
3
|
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
4
|
-
import { createServer } from 'node:http';
|
|
4
|
+
import { createServer, get } from 'node:http';
|
|
5
5
|
import { dirname, extname, join } from 'node:path';
|
|
6
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
6
7
|
import { fileURLToPath } from 'node:url';
|
|
7
8
|
import { Agents } from './agents.js';
|
|
8
9
|
import { house } from './api.js';
|
|
@@ -10,7 +11,7 @@ import { received } from './bridge.js';
|
|
|
10
11
|
import { CONVERSATION_INPUTS, Conversations } from './conversations.js';
|
|
11
12
|
import { LocalCopies } from './copies.js';
|
|
12
13
|
import { restart } from './end.js';
|
|
13
|
-
import { agentBase, enrolled, forgetRestart, kitHome, restartAsked } from './home.js';
|
|
14
|
+
import { agentBase, enrolled, forgetRestart, kitHome, ownerLock, ownerSocket, restartAsked } from './home.js';
|
|
14
15
|
import { killDescendants } from './scope.js';
|
|
15
16
|
import { quoted } from './shell.js';
|
|
16
17
|
import { SignIns } from './sign-in.js';
|
|
@@ -29,13 +30,32 @@ async function installShim(path, module) {
|
|
|
29
30
|
await mkdir(dirname(path), { recursive: true });
|
|
30
31
|
await writeFile(path, `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, { mode: 0o755 });
|
|
31
32
|
}
|
|
33
|
+
function claimed(lock) {
|
|
34
|
+
try {
|
|
35
|
+
lock.exec('BEGIN EXCLUSIVE');
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error.errcode === 5)
|
|
40
|
+
return false;
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
32
44
|
async function serveOwner(copies) {
|
|
33
|
-
const
|
|
34
|
-
|
|
45
|
+
const lock = new DatabaseSync(ownerLock());
|
|
46
|
+
if (!claimed(lock)) {
|
|
47
|
+
lock.close();
|
|
48
|
+
const running = await runningResident();
|
|
49
|
+
throw new Error(`${running === null ? 'another Kit' : `Kit ${running}`} already runs this Environment from Kit home ${kitHome()}; stop it first to run another here`);
|
|
50
|
+
}
|
|
35
51
|
const server = createServer(async (request, response) => {
|
|
36
52
|
try {
|
|
37
53
|
const text = await received(request);
|
|
38
54
|
let answered;
|
|
55
|
+
if (request.url === '/resident') {
|
|
56
|
+
response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ pid: process.pid }));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
39
59
|
if (request.url === '/git/fetch') {
|
|
40
60
|
answered = await copies.fetch();
|
|
41
61
|
}
|
|
@@ -53,9 +73,23 @@ async function serveOwner(copies) {
|
|
|
53
73
|
response.writeHead(502).end(error instanceof Error ? error.message : String(error));
|
|
54
74
|
}
|
|
55
75
|
});
|
|
56
|
-
server.
|
|
76
|
+
server.on('close', () => lock.close());
|
|
77
|
+
await rm(ownerSocket(), { force: true });
|
|
78
|
+
server.listen(ownerSocket());
|
|
57
79
|
await once(server, 'listening');
|
|
58
80
|
}
|
|
81
|
+
function runningResident() {
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
get({ socketPath: ownerSocket(), path: '/resident' }, (answer) => {
|
|
84
|
+
received(answer).then((text) => resolve(JSON.parse(text).pid), reject);
|
|
85
|
+
}).on('error', (error) => {
|
|
86
|
+
if (error.code === 'ENOENT' || error.code === 'ECONNREFUSED')
|
|
87
|
+
resolve(null);
|
|
88
|
+
else
|
|
89
|
+
reject(error);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
59
93
|
function connected() {
|
|
60
94
|
return new Promise((resolve) => {
|
|
61
95
|
const watcher = watch(kitHome(), () => {
|
|
@@ -72,7 +106,7 @@ function connected() {
|
|
|
72
106
|
resolve();
|
|
73
107
|
});
|
|
74
108
|
}
|
|
75
|
-
|
|
109
|
+
async function started() {
|
|
76
110
|
await connected();
|
|
77
111
|
await forgetRestart();
|
|
78
112
|
watch(kitHome(), () => {
|
|
@@ -83,13 +117,20 @@ export async function resident() {
|
|
|
83
117
|
});
|
|
84
118
|
await placeSkillSet();
|
|
85
119
|
await mkdir(agentBase(), { recursive: true });
|
|
86
|
-
process.env.HOUSE_KIT_RESIDENT = String(process.pid);
|
|
87
|
-
const agents = new Agents(house, () => changed());
|
|
88
|
-
const copies = new LocalCopies(house);
|
|
89
|
-
await copies.load();
|
|
90
120
|
await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
|
|
91
121
|
await installShim(codexLauncher(), 'codex-main');
|
|
122
|
+
}
|
|
123
|
+
export async function resident() {
|
|
124
|
+
await mkdir(kitHome(), { recursive: true, mode: 0o700 });
|
|
125
|
+
const copies = new LocalCopies(house);
|
|
126
|
+
await copies.load();
|
|
92
127
|
await serveOwner(copies);
|
|
128
|
+
await started().catch((error) => {
|
|
129
|
+
report(error);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
});
|
|
132
|
+
process.env.HOUSE_KIT_RESIDENT = String(process.pid);
|
|
133
|
+
const agents = new Agents(house, () => changed());
|
|
93
134
|
let stream = null;
|
|
94
135
|
let reported = false;
|
|
95
136
|
const idle = () => conversations.idle() && signIns.idle() && agents.idle();
|
package/dist/skills.js
CHANGED
|
@@ -1,17 +1,31 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
import { readFileSync } from 'node:fs';
|
|
2
|
-
import { cp, lstat, mkdir, readdir, realpath, rm, symlink
|
|
3
|
+
import { cp, lstat, mkdir, readdir, realpath, rm, symlink } from 'node:fs/promises';
|
|
3
4
|
import { homedir } from 'node:os';
|
|
4
5
|
import { join, relative } from 'node:path';
|
|
5
6
|
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { parse } from 'yaml';
|
|
6
8
|
import { KIT_VERSION } from './clis.js';
|
|
7
9
|
import { kitHome, readConfiguration, readHome, writeHome } from './home.js';
|
|
8
10
|
import { refusalOf } from './refusals.js';
|
|
9
|
-
|
|
11
|
+
const HOW_WE_WORK = '/rooms/private/how-we-work.md';
|
|
10
12
|
const SKILL_SET = fileURLToPath(new URL('../skills/', import.meta.url));
|
|
11
13
|
const HOW_WE_WORK_TEXT = readFileSync(new URL('../how-we-work.md', import.meta.url), 'utf8');
|
|
14
|
+
const SHIPPED_HOW_WE_WORK = [
|
|
15
|
+
'ae4dda60da7b174f22dc5fa9bf07fc27c25864932a74230de4f9f509ddd91f41',
|
|
16
|
+
'0bdcd43e1603ba6673a3cae56d70bbf90657cbeefd124e91a47be2ca1cbd6c93',
|
|
17
|
+
'4106a87ec66da6f7b2a10e74d18e6559d68d5f4fe7415dae68f5c7bdb9081968',
|
|
18
|
+
'ac814a2197038f037b657bb719340395f89f319ac9fcdd386629c3fe2cec68f1',
|
|
19
|
+
'29951ee394aad9979eecfed3e5dc32c2c86e2226b6a380db5dbd4f305f4d1156',
|
|
20
|
+
];
|
|
12
21
|
const RECORD = 'skill-set.json';
|
|
13
|
-
const
|
|
22
|
+
const WRITTEN = 'how-we-work.json';
|
|
14
23
|
const UNWRITABLE = new Set(['room_not_found', 'operation_denied']);
|
|
24
|
+
const PRESENT = new Set(['path_exists']);
|
|
25
|
+
const CHANGED = new Set(['base_revision_stale', 'path_absent']);
|
|
26
|
+
const UNREADABLE = /^(?:\S+: )?(?:path_not_found|room_not_found|operation_denied)\b/;
|
|
27
|
+
const FAILED = /^exit: [1-9]\d*$/;
|
|
28
|
+
const CUT = /^stderr: shell: output_cut (\d+) of \d+ bytes; continue with: (.+)$/;
|
|
15
29
|
function skillsDirectory() {
|
|
16
30
|
return join(homedir(), '.agents', 'skills');
|
|
17
31
|
}
|
|
@@ -70,19 +84,64 @@ async function replaceSkillSet() {
|
|
|
70
84
|
else
|
|
71
85
|
await rm(join(kitHome(), RECORD), { force: true });
|
|
72
86
|
}
|
|
87
|
+
function sha256(text) {
|
|
88
|
+
return createHash('sha256').update(text).digest('hex');
|
|
89
|
+
}
|
|
90
|
+
function cutOf(lines) {
|
|
91
|
+
const cut = CUT.exec(lines[0] ?? '');
|
|
92
|
+
return cut !== null && Buffer.byteLength(lines.slice(1).join('\n')) === Number(cut[1]) ? cut : null;
|
|
93
|
+
}
|
|
94
|
+
export async function readHowWeWork(bridge) {
|
|
95
|
+
let document = '';
|
|
96
|
+
for (let command = `cat ${HOW_WE_WORK}`; command !== undefined;) {
|
|
97
|
+
const read = await bridge.tool('shell', { command });
|
|
98
|
+
const text = read.content.map((content) => content.text).join('\n');
|
|
99
|
+
const lines = text.split('\n');
|
|
100
|
+
if (read.isError === true || (FAILED.test(lines[0] ?? '') && lines[1]?.startsWith('stderr: '))) {
|
|
101
|
+
const refusal = read.isError === true ? text : lines.slice(1).join('\n').replace(/^stderr: /gm, '');
|
|
102
|
+
if (UNREADABLE.test(refusal))
|
|
103
|
+
return '';
|
|
104
|
+
throw new Error(refusal);
|
|
105
|
+
}
|
|
106
|
+
const cut = cutOf(lines);
|
|
107
|
+
document += lines.slice(cut === null ? 0 : 1).join('\n');
|
|
108
|
+
command = cut?.[2];
|
|
109
|
+
}
|
|
110
|
+
return document;
|
|
111
|
+
}
|
|
112
|
+
async function currentRevision(bridge) {
|
|
113
|
+
const inspected = await bridge.tool('inspect', { path: HOW_WE_WORK });
|
|
114
|
+
const text = inspected.content.map((content) => content.text).join('\n');
|
|
115
|
+
if (inspected.isError === true)
|
|
116
|
+
throw new Error(text);
|
|
117
|
+
return parse(text).revisions[0].revision;
|
|
118
|
+
}
|
|
119
|
+
async function edit(bridge, change, kept) {
|
|
120
|
+
const edited = await bridge.tool('edit', { changes: [{ ...change, path: HOW_WE_WORK, content: HOW_WE_WORK_TEXT }] });
|
|
121
|
+
if (edited.isError !== true)
|
|
122
|
+
return 'written';
|
|
123
|
+
const text = edited.content.map((content) => content.text).join('\n');
|
|
124
|
+
const refusal = refusalOf(text);
|
|
125
|
+
if (UNWRITABLE.has(refusal?.code ?? ''))
|
|
126
|
+
return 'unwritable';
|
|
127
|
+
if (refusal?.code === 'edit_conflict' && refusal.conflicts.some((conflict) => kept.has(conflict)))
|
|
128
|
+
return 'kept';
|
|
129
|
+
throw new Error(text);
|
|
130
|
+
}
|
|
73
131
|
export async function createHowWeWork(bridge) {
|
|
74
132
|
if (!(await readConfiguration()).skills || (await readHome(RECORD))?.version === undefined)
|
|
75
133
|
return;
|
|
76
|
-
|
|
134
|
+
const current = sha256(HOW_WE_WORK_TEXT);
|
|
135
|
+
if ((await readHome(WRITTEN)) === current)
|
|
136
|
+
return;
|
|
137
|
+
const created = await edit(bridge, { op: 'create' }, PRESENT);
|
|
138
|
+
if (created === 'unwritable')
|
|
77
139
|
return;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
if (UNWRITABLE.has(refusal?.code ?? ''))
|
|
140
|
+
if (created === 'kept') {
|
|
141
|
+
const base = await currentRevision(bridge);
|
|
142
|
+
const held = sha256(await readHowWeWork(bridge));
|
|
143
|
+
if (held !== current && SHIPPED_HOW_WE_WORK.includes(held) && (await edit(bridge, { op: 'replace', base }, CHANGED)) === 'unwritable')
|
|
83
144
|
return;
|
|
84
|
-
if (refusal?.code !== 'edit_conflict' || !refusal.conflicts.includes('path_exists'))
|
|
85
|
-
throw new Error(text);
|
|
86
145
|
}
|
|
87
|
-
await
|
|
146
|
+
await writeHome(WRITTEN, current);
|
|
88
147
|
}
|
package/dist/stream.js
CHANGED
|
@@ -7,6 +7,36 @@ import { KIT_WIRE, tooOld } from './wire.js';
|
|
|
7
7
|
const PATH = '/kit/stream';
|
|
8
8
|
const SUBPROTOCOL = `house.kit.stream.${KIT_WIRE}`;
|
|
9
9
|
const REFUSAL_HEADER = 'x-house-refusal';
|
|
10
|
+
// House beats each stream every thirty seconds, so two silent beats mean this Kit cannot hear House.
|
|
11
|
+
export const STREAM_SILENCE_MS = 60_000;
|
|
12
|
+
const SYSTEM_STREAM_CLOCK = {
|
|
13
|
+
later: (delay, callback) => setTimeout(callback, delay),
|
|
14
|
+
clear: (timer) => clearTimeout(timer),
|
|
15
|
+
};
|
|
16
|
+
export class StreamSilence {
|
|
17
|
+
timer = null;
|
|
18
|
+
silent;
|
|
19
|
+
clock;
|
|
20
|
+
interval;
|
|
21
|
+
constructor(silent, clock = SYSTEM_STREAM_CLOCK, interval = STREAM_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
|
+
}
|
|
10
40
|
function streamUrl(house) {
|
|
11
41
|
const url = new URL(PATH, house);
|
|
12
42
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
@@ -37,17 +67,10 @@ export function holdStream(handlers) {
|
|
|
37
67
|
headers: { authorization: `Bearer ${credential}` },
|
|
38
68
|
});
|
|
39
69
|
let opened = false;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
handlers.opened();
|
|
45
|
-
};
|
|
46
|
-
opening.onmessage = (event) => {
|
|
47
|
-
handlers.frame(JSON.parse(String(event.data)));
|
|
48
|
-
};
|
|
49
|
-
opening.onerror = () => undefined;
|
|
50
|
-
opening.onclose = (event) => {
|
|
70
|
+
let silence = null;
|
|
71
|
+
const closed = (event) => {
|
|
72
|
+
silence?.stop();
|
|
73
|
+
silence = null;
|
|
51
74
|
if (socket === opening)
|
|
52
75
|
socket = null;
|
|
53
76
|
process.stderr.write(`kit: control stream closed ${event.code} ${event.reason}\n`);
|
|
@@ -58,12 +81,35 @@ export function holdStream(handlers) {
|
|
|
58
81
|
return;
|
|
59
82
|
}
|
|
60
83
|
if (refused !== null && tooOld(refused.text))
|
|
61
|
-
updateRequired(
|
|
84
|
+
updateRequired();
|
|
62
85
|
if (finalRefusal(refused))
|
|
63
86
|
end(refused.message);
|
|
64
87
|
// 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
88
|
setTimeout(() => void connect(), opened ? 1000 + Math.random() * 2000 : retryDelay(failures++));
|
|
66
89
|
};
|
|
90
|
+
opening.onopen = () => {
|
|
91
|
+
opened = true;
|
|
92
|
+
failures = 0;
|
|
93
|
+
socket = opening;
|
|
94
|
+
silence = new StreamSilence(() => {
|
|
95
|
+
if (socket !== opening)
|
|
96
|
+
return;
|
|
97
|
+
socket = null;
|
|
98
|
+
opening.onclose = null;
|
|
99
|
+
opening.close();
|
|
100
|
+
closed(new CloseEvent('close', { code: 1006, reason: 'heartbeat_missed' }));
|
|
101
|
+
});
|
|
102
|
+
silence.heard();
|
|
103
|
+
handlers.opened();
|
|
104
|
+
};
|
|
105
|
+
opening.onmessage = (event) => {
|
|
106
|
+
silence?.heard();
|
|
107
|
+
const frame = JSON.parse(String(event.data));
|
|
108
|
+
if (frame.type !== 'beat')
|
|
109
|
+
handlers.frame(frame);
|
|
110
|
+
};
|
|
111
|
+
opening.onerror = () => undefined;
|
|
112
|
+
opening.onclose = closed;
|
|
67
113
|
};
|
|
68
114
|
void connect();
|
|
69
115
|
return {
|
package/dist/transcription.js
CHANGED
|
@@ -49,10 +49,10 @@ async function executed(command, args, signal) {
|
|
|
49
49
|
function present(path) {
|
|
50
50
|
return stat(path).then(() => true, () => false);
|
|
51
51
|
}
|
|
52
|
-
async function
|
|
52
|
+
async function threads() {
|
|
53
53
|
const [quota, period] = (await readFile('/sys/fs/cgroup/cpu.max', 'utf8').catch(() => 'max')).split(' ');
|
|
54
54
|
const allowed = quota === 'max' ? Infinity : Math.ceil(Number(quota) / Number(period));
|
|
55
|
-
return Math.min(availableParallelism(), allowed);
|
|
55
|
+
return Math.max(1, Math.floor(Math.min(availableParallelism(), allowed) / 2));
|
|
56
56
|
}
|
|
57
57
|
async function sha256(path) {
|
|
58
58
|
const hash = createHash('sha256');
|
|
@@ -140,7 +140,7 @@ async function transcribed(path, signal) {
|
|
|
140
140
|
await executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', path, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
|
|
141
141
|
'-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(work, '%05d.wav')], signal);
|
|
142
142
|
const parts = (await readdir(work)).sort().flatMap((part) => ['-f', join(work, part)]);
|
|
143
|
-
const text = await executed(installed(pinned.engine), ['-t', String(await
|
|
143
|
+
const text = await executed(installed(pinned.engine), ['-t', String(await threads()), '-np', '-m', installed(MODEL), ...parts], signal);
|
|
144
144
|
return text
|
|
145
145
|
.split('\n')
|
|
146
146
|
.map((line) => line.trim())
|
package/dist/work.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import { once } from 'node:events';
|
|
3
3
|
import { readFileSync } from 'node:fs';
|
|
4
|
-
import {
|
|
4
|
+
import { rm } from 'node:fs/promises';
|
|
5
5
|
import { createServer } from 'node:net';
|
|
6
6
|
import { join } from 'node:path';
|
|
7
7
|
import { RUNNING_JOB } from './events.js';
|
|
8
|
-
import { kitHome } from './home.js';
|
|
8
|
+
import { kitHome, socketDirectory } from './home.js';
|
|
9
9
|
const ENDED_TASK = new Set(['completed', 'failed', 'killed']);
|
|
10
10
|
const SCHEDULING = new Set(['CronCreate', 'CronDelete', 'ScheduleWakeup']);
|
|
11
11
|
const QUEUE_LIST = 'house-kit-queue-';
|
|
@@ -194,9 +194,7 @@ class CodexWork extends Work {
|
|
|
194
194
|
proving = -1;
|
|
195
195
|
proven = -1;
|
|
196
196
|
async open(cli) {
|
|
197
|
-
|
|
198
|
-
await mkdir(sockets, { recursive: true, mode: 0o700 });
|
|
199
|
-
this.path = join(sockets, `${randomUUID()}.sock`);
|
|
197
|
+
this.path = join(socketDirectory(), `${randomUUID()}.sock`);
|
|
200
198
|
this.server = createServer((socket) => this.connected(socket));
|
|
201
199
|
this.server.listen(this.path);
|
|
202
200
|
await once(this.server, 'listening');
|
package/how-we-work.md
CHANGED
|
@@ -4,6 +4,8 @@ A Room is where information people share is kept; it is not the place for
|
|
|
4
4
|
private information. Work goes into the Room it belongs to, or the First Room
|
|
5
5
|
when none is named.
|
|
6
6
|
|
|
7
|
+
Every House answer carries the `url` of what it names; send the User that link.
|
|
8
|
+
|
|
7
9
|
The skills are already set up; never run `setup-matt-pocock-skills`.
|
|
8
10
|
|
|
9
11
|
## Where skill output goes
|