@agentshouse/kit 0.2.1-c3895fcff473b → 0.2.1-c39de3ed26c1b
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 +26 -26
- package/dist/acp.js +16 -93
- package/dist/agents.js +85 -35
- package/dist/api.js +34 -2
- package/dist/app-git.js +64 -0
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +33 -51
- package/dist/chatgpt.js +25 -0
- package/dist/clis.js +117 -70
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +285 -236
- package/dist/copies.js +148 -115
- package/dist/end.js +22 -0
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +521 -0
- package/dist/files.js +21 -1
- package/dist/git.js +11 -8
- package/dist/home.js +41 -3
- package/dist/house-main.js +151 -52
- package/dist/instructions.js +7 -5
- package/dist/kit-main.js +54 -6
- package/dist/login.js +95 -104
- package/dist/logout.js +16 -13
- package/dist/parts.js +82 -0
- package/dist/placement.js +48 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +12 -2
- package/dist/resident.js +38 -27
- 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/stream.js +35 -5
- package/dist/transcription.js +54 -6
- package/dist/update.js +29 -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/house-main.js
CHANGED
|
@@ -1,55 +1,94 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
|
-
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
4
|
import { request, setGlobalProxyFromEnv } from 'node:http';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { dirname, extname, join, resolve } from 'node:path';
|
|
6
|
+
import { verified } from './files.js';
|
|
7
|
+
import { HOUSE_REF } from './git.js';
|
|
8
|
+
import { kitHome, readEnrolment, readOwnAgent } from './home.js';
|
|
7
9
|
import { callHouse, rpc, UNREACHABLE } from './mcp.js';
|
|
8
10
|
import { refusalOf } from './refusals.js';
|
|
11
|
+
import { relayed } from './relay.js';
|
|
12
|
+
import { parse, stringify } from 'yaml';
|
|
9
13
|
const USAGE = "usage: house <tool> ['<arguments as JSON>']";
|
|
10
14
|
const PUSH_USAGE = 'usage: house push [commit] [--owner]';
|
|
11
15
|
const PUSH = "push [commit]: sends the Local copy's committed changes to House";
|
|
16
|
+
const FETCH_USAGE = 'usage: house fetch';
|
|
17
|
+
const FETCH = `fetch: records House's changes on each Local copy's ${HOUSE_REF}`;
|
|
18
|
+
const DOWNLOAD_USAGE = `usage: house download '{"path":"/rooms/<handle>/<path>","file":"<local file>"}', adding "ref" for a revision`;
|
|
19
|
+
const DOWNLOAD = 'download: writes a binary file of a Room to the local file named by file';
|
|
12
20
|
const ARGUMENTS = `the arguments are one JSON object in single quotes, like house search '{"query":"invoice"}'`;
|
|
13
21
|
const CLOSED = "this conversation's House connection is closed; nothing to do from here";
|
|
22
|
+
const SANDBOXED = "your CLI's sandbox kept house from House; run house directly, not through another program";
|
|
14
23
|
const REVOKED = 'this conversation no longer has House access; nothing to do from here';
|
|
15
24
|
const UNCONNECTED = "this computer's own agent has no House connection; run kit login";
|
|
16
25
|
const DISCONNECTED = "House no longer accepts this computer's own agent; run kit login";
|
|
17
26
|
const socketPath = process.env.HOUSE_BRIDGE;
|
|
18
|
-
|
|
27
|
+
const MEDIA_TYPES = new Map([
|
|
28
|
+
['avif', 'image/avif'],
|
|
29
|
+
['csv', 'text/csv'],
|
|
30
|
+
['docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
|
|
31
|
+
['gif', 'image/gif'],
|
|
32
|
+
['heic', 'image/heic'],
|
|
33
|
+
['html', 'text/html'],
|
|
34
|
+
['jpeg', 'image/jpeg'],
|
|
35
|
+
['jpg', 'image/jpeg'],
|
|
36
|
+
['json', 'application/json'],
|
|
37
|
+
['m4a', 'audio/mp4'],
|
|
38
|
+
['md', 'text/markdown'],
|
|
39
|
+
['mov', 'video/quicktime'],
|
|
40
|
+
['mp3', 'audio/mpeg'],
|
|
41
|
+
['mp4', 'video/mp4'],
|
|
42
|
+
['ogg', 'audio/ogg'],
|
|
43
|
+
['pdf', 'application/pdf'],
|
|
44
|
+
['png', 'image/png'],
|
|
45
|
+
['pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
|
|
46
|
+
['svg', 'image/svg+xml'],
|
|
47
|
+
['txt', 'text/plain'],
|
|
48
|
+
['wav', 'audio/wav'],
|
|
49
|
+
['webm', 'video/webm'],
|
|
50
|
+
['webp', 'image/webp'],
|
|
51
|
+
['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
|
|
52
|
+
['zip', 'application/zip'],
|
|
53
|
+
]);
|
|
54
|
+
function refused(path, method, status, text) {
|
|
19
55
|
const refusal = refusalOf(text);
|
|
20
56
|
if (refusal !== null)
|
|
21
57
|
return refusal.text;
|
|
22
58
|
if (status === 401)
|
|
23
59
|
return REVOKED;
|
|
60
|
+
const act = method === 'GET' ? 'download' : 'upload';
|
|
24
61
|
if (URL.canParse(path))
|
|
25
|
-
return `the
|
|
62
|
+
return `the ${act} answered ${status}; ${act} the file again`;
|
|
26
63
|
if (status === 502 && /^[A-Za-z]/.test(text))
|
|
27
64
|
return text;
|
|
28
65
|
return `${path} answered ${status}`;
|
|
29
66
|
}
|
|
30
|
-
function
|
|
67
|
+
function exchanged(path, method, body, headers = { 'content-type': 'application/json' }, to = socketPath) {
|
|
31
68
|
return new Promise((resolve, reject) => {
|
|
32
|
-
const sent = request({ socketPath: to, path, method
|
|
33
|
-
|
|
34
|
-
answer.
|
|
35
|
-
answer.on('data', (chunk) => {
|
|
36
|
-
text += chunk;
|
|
37
|
-
});
|
|
69
|
+
const sent = request({ socketPath: to, path, method, headers }, (answer) => {
|
|
70
|
+
const chunks = [];
|
|
71
|
+
answer.on('data', (chunk) => chunks.push(chunk));
|
|
38
72
|
answer.on('end', () => {
|
|
73
|
+
const bytes = Buffer.concat(chunks);
|
|
39
74
|
if (answer.statusCode === 200)
|
|
40
|
-
resolve(
|
|
75
|
+
resolve(bytes);
|
|
41
76
|
else
|
|
42
|
-
reject(new Error(refused(path, answer.statusCode,
|
|
77
|
+
reject(new Error(refused(path, method, answer.statusCode, bytes.toString('utf8'))));
|
|
43
78
|
});
|
|
44
79
|
});
|
|
45
|
-
sent.on('error', () => reject(new Error(CLOSED)));
|
|
46
|
-
sent.end(body instanceof Buffer ? body : JSON.stringify(body));
|
|
80
|
+
sent.on('error', (error) => reject(new Error(error.code === 'EPERM' || error.code === 'EACCES' ? SANDBOXED : CLOSED)));
|
|
81
|
+
sent.end(body === undefined ? undefined : body instanceof Buffer ? body : JSON.stringify(body));
|
|
47
82
|
});
|
|
48
83
|
}
|
|
84
|
+
async function bridged(path, body, headers, to = socketPath) {
|
|
85
|
+
return (await exchanged(path, 'POST', body, headers, to)).toString('utf8');
|
|
86
|
+
}
|
|
49
87
|
async function direct(message) {
|
|
50
88
|
const ownAgent = await readOwnAgent();
|
|
51
89
|
if (ownAgent === null)
|
|
52
90
|
throw new Error(UNCONNECTED);
|
|
91
|
+
await readEnrolment();
|
|
53
92
|
setGlobalProxyFromEnv();
|
|
54
93
|
const answer = await callHouse(ownAgent.house, ownAgent.credential, message).catch(() => {
|
|
55
94
|
throw new Error(UNREACHABLE);
|
|
@@ -78,31 +117,75 @@ async function localFile(path) {
|
|
|
78
117
|
: `${path} is not a readable file`);
|
|
79
118
|
}
|
|
80
119
|
}
|
|
81
|
-
async function
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
120
|
+
async function transferred(url, method, bytes, headers) {
|
|
121
|
+
if (socketPath !== undefined)
|
|
122
|
+
return exchanged(url, method, bytes, headers);
|
|
123
|
+
setGlobalProxyFromEnv();
|
|
124
|
+
const answer = await fetch(relayed(url), { method, ...(headers === undefined ? {} : { headers }), ...(bytes === undefined ? {} : { body: bytes }) }).catch(() => {
|
|
125
|
+
throw new Error(UNREACHABLE);
|
|
126
|
+
});
|
|
127
|
+
const received = Buffer.from(await answer.arrayBuffer());
|
|
128
|
+
if (answer.status === 200)
|
|
129
|
+
return received;
|
|
130
|
+
throw new Error(refused(url, method, answer.status, received.toString('utf8')));
|
|
131
|
+
}
|
|
132
|
+
async function sent(url, bytes, operation) {
|
|
133
|
+
const headers = { 'content-type': 'application/octet-stream', 'x-house-byte-operation': operation };
|
|
134
|
+
return (await transferred(url, 'POST', bytes, headers)).toString('utf8');
|
|
135
|
+
}
|
|
136
|
+
async function download(args) {
|
|
137
|
+
const { flags, positionals: [argument] } = accepted(args, ['--help'], 1, DOWNLOAD_USAGE);
|
|
138
|
+
if (flags.has('--help'))
|
|
139
|
+
return DOWNLOAD_USAGE;
|
|
140
|
+
const { file, ...selected } = argumentsOf(argument);
|
|
141
|
+
if (typeof file !== 'string' || file === '')
|
|
142
|
+
throw new Error('download needs "file", the local file to write');
|
|
143
|
+
const inspected = parse(await called('inspect', selected));
|
|
144
|
+
if (typeof inspected?.download !== 'string')
|
|
145
|
+
throw new Error(`${String(selected.path)} holds no binary file; shell reads its text`);
|
|
146
|
+
const bytes = verified(await transferred(inspected.download, 'GET'), String(inspected.sha256), String(selected.path));
|
|
147
|
+
const written = resolve(file);
|
|
148
|
+
await mkdir(dirname(written), { recursive: true });
|
|
149
|
+
await writeFile(written, bytes);
|
|
150
|
+
return written;
|
|
151
|
+
}
|
|
152
|
+
async function upload({ file, ...destination }) {
|
|
153
|
+
if (typeof file !== 'string' || file === '')
|
|
154
|
+
throw new Error('upload needs "file", a local file');
|
|
155
|
+
const bytes = await localFile(file);
|
|
156
|
+
const mediaType = MEDIA_TYPES.get(extname(file).slice(1).toLowerCase());
|
|
157
|
+
const answer = await called('upload', {
|
|
158
|
+
...destination,
|
|
88
159
|
bytes: bytes.length,
|
|
89
160
|
sha256: createHash('sha256').update(bytes).digest('hex'),
|
|
90
|
-
|
|
161
|
+
...(mediaType === undefined ? {} : { media_type: mediaType }),
|
|
162
|
+
});
|
|
163
|
+
const declared = parse(answer);
|
|
91
164
|
if (declared.upload === undefined)
|
|
92
|
-
return
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
165
|
+
return answer;
|
|
166
|
+
const written = JSON.parse(await sent(declared.upload.url, bytes, declared.upload.operation));
|
|
167
|
+
return stringify(written, { lineWidth: 0 }).trimEnd();
|
|
168
|
+
}
|
|
169
|
+
function unexpected(argument, usage) {
|
|
170
|
+
return new Error(`unexpected argument ${argument}; ${usage}`);
|
|
171
|
+
}
|
|
172
|
+
function accepted(args, flags, positionals, usage) {
|
|
173
|
+
const given = { flags: new Set(), positionals: [] };
|
|
174
|
+
for (const arg of args) {
|
|
175
|
+
if (flags.includes(arg))
|
|
176
|
+
given.flags.add(arg);
|
|
177
|
+
else if (!arg.startsWith('-') && given.positionals.length < positionals)
|
|
178
|
+
given.positionals.push(arg);
|
|
179
|
+
else
|
|
180
|
+
throw unexpected(arg, usage);
|
|
181
|
+
}
|
|
182
|
+
return given;
|
|
100
183
|
}
|
|
101
184
|
async function push(args) {
|
|
102
|
-
|
|
185
|
+
const { flags, positionals: [commit = 'HEAD'] } = accepted(args, ['--help', '--owner'], 1, PUSH_USAGE);
|
|
186
|
+
if (flags.has('--help'))
|
|
103
187
|
return PUSH_USAGE;
|
|
104
|
-
const owner =
|
|
105
|
-
const commit = args.find((arg) => arg !== '--owner') ?? 'HEAD';
|
|
188
|
+
const owner = flags.has('--owner');
|
|
106
189
|
if (owner && socketPath !== undefined)
|
|
107
190
|
throw new Error('--owner is not for Agent conversations; run house push without it');
|
|
108
191
|
if (!owner && socketPath === undefined)
|
|
@@ -112,6 +195,14 @@ async function push(args) {
|
|
|
112
195
|
throw new Error(pushed.text);
|
|
113
196
|
return pushed.text;
|
|
114
197
|
}
|
|
198
|
+
async function fetched(args) {
|
|
199
|
+
if (accepted(args, ['--help'], 0, FETCH_USAGE).flags.has('--help'))
|
|
200
|
+
return FETCH_USAGE;
|
|
201
|
+
const answer = JSON.parse(await bridged('/git/fetch', {}, undefined, socketPath ?? join(kitHome(), 'owner.sock')));
|
|
202
|
+
if (answer.refused)
|
|
203
|
+
throw new Error(answer.text);
|
|
204
|
+
return answer.text;
|
|
205
|
+
}
|
|
115
206
|
function argumentsOf(argument) {
|
|
116
207
|
if (argument === undefined)
|
|
117
208
|
return {};
|
|
@@ -126,36 +217,44 @@ function argumentsOf(argument) {
|
|
|
126
217
|
throw new Error(ARGUMENTS);
|
|
127
218
|
return parsed;
|
|
128
219
|
}
|
|
129
|
-
async function house(
|
|
130
|
-
|
|
131
|
-
|
|
220
|
+
async function house(args) {
|
|
221
|
+
const { flags, positionals: [verb, argument] } = accepted(args, ['--help'], 2, USAGE);
|
|
222
|
+
if (verb === 'help' && argument !== undefined)
|
|
223
|
+
throw unexpected(argument, USAGE);
|
|
224
|
+
if (flags.has('--help') && argument !== undefined)
|
|
225
|
+
throw unexpected('--help', USAGE);
|
|
226
|
+
if (verb === undefined || verb === 'help') {
|
|
227
|
+
return [USAGE, 'help', PUSH, FETCH, DOWNLOAD, ...(await tools()).map((tool) => (tool.description === undefined ? tool.name : `${tool.name}: ${tool.description}`))].join('\n');
|
|
132
228
|
}
|
|
133
|
-
if (
|
|
229
|
+
if (flags.has('--help')) {
|
|
134
230
|
const tool = (await tools()).find((listed) => listed.name === verb);
|
|
135
231
|
if (tool === undefined)
|
|
136
232
|
throw new Error(`${verb} is no Tool; house find_command '{"query":"${verb}"}' finds Commands`);
|
|
137
233
|
return [...(tool.description === undefined ? [] : [tool.description]), JSON.stringify(tool.inputSchema)].join('\n');
|
|
138
234
|
}
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const attachments = [];
|
|
145
|
-
for (const path of args.attachments) {
|
|
146
|
-
attachments.push(await upload(path, args.room_ref, 'each attachments entry is the path of a local file'));
|
|
147
|
-
}
|
|
148
|
-
args.attachments = attachments;
|
|
149
|
-
}
|
|
150
|
-
const result = await mcp('tools/call', { name: verb, arguments: args });
|
|
235
|
+
const parsed = argumentsOf(argument);
|
|
236
|
+
return verb === 'upload' ? upload(parsed) : called(verb, parsed);
|
|
237
|
+
}
|
|
238
|
+
async function called(name, args) {
|
|
239
|
+
const result = await mcp('tools/call', { name, arguments: args });
|
|
151
240
|
const text = result.content.map((content) => content.text).join('\n');
|
|
152
241
|
if (result.isError === true)
|
|
153
242
|
throw new Error(text.replace(/\n+$/, ''));
|
|
154
243
|
return text;
|
|
155
244
|
}
|
|
156
245
|
const argv = process.argv.slice(2);
|
|
157
|
-
|
|
246
|
+
const named = argv.findIndex((arg) => !arg.startsWith('-'));
|
|
247
|
+
const rest = argv.filter((_arg, index) => index !== named);
|
|
248
|
+
(argv[named] === 'push'
|
|
249
|
+
? push(rest)
|
|
250
|
+
: argv[named] === 'fetch'
|
|
251
|
+
? fetched(rest)
|
|
252
|
+
: argv[named] === 'download'
|
|
253
|
+
? download(rest)
|
|
254
|
+
: house(argv)).then((text) => {
|
|
158
255
|
process.stdout.write(`${text}\n`);
|
|
256
|
+
if (argv[0] === 'shell')
|
|
257
|
+
process.exitCode = Number(/^exit: (\d+)(?:\n|$)/.exec(text)?.[1] ?? 0);
|
|
159
258
|
}, (error) => {
|
|
160
259
|
process.stderr.write(`house: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
161
260
|
process.exitCode = 1;
|
package/dist/instructions.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { HOUSE_REF } from './git.js';
|
|
1
2
|
import { HOW_WE_WORK } from './skills.js';
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
3
|
+
const SOURCE_LINE = "From House:";
|
|
4
|
+
const HOUSE_LINE = "Work with House through the `house` CLI: run `house --help`, and `house find_command` for House's guides and formats.";
|
|
5
|
+
const FILE_LINE = 'To give the User a file, upload it with `house upload` and link it; for an App, a Site or a website, follow `house find_command \'{"query":"app"}\'`. Never start a server.';
|
|
6
|
+
const READ_LINE = "Optional per-Room instructions: `/rooms/private/rooms/<handle>/AGENTS.md`.";
|
|
7
|
+
const COPIES_LINE = `House Local copies; before work run \`house fetch\` and rebase onto \`${HOUSE_REF}\`; after it commit, then \`house push\`:`;
|
|
6
8
|
const UNREADABLE = /^(?:\S+: )?(?:path_not_found|room_not_found|operation_denied)\b/;
|
|
7
9
|
const FAILED = /^exit: [1-9]\d*$/;
|
|
8
10
|
const CUT = /^stderr: shell: output_cut (\d+) of \d+ bytes; continue with: (.+)$/;
|
|
@@ -29,6 +31,6 @@ async function howWeWork(bridge) {
|
|
|
29
31
|
return document.replace(/\n$/, '');
|
|
30
32
|
}
|
|
31
33
|
export async function instructions(bridge, base, copies) {
|
|
32
|
-
const lines = [HOUSE_LINE, FILE_LINE, READ_LINE, ...(copies.length === 0 ? [] : [COPIES_LINE, ...copies])];
|
|
34
|
+
const lines = [SOURCE_LINE, HOUSE_LINE, FILE_LINE, READ_LINE, ...(copies.length === 0 ? [] : [COPIES_LINE, ...copies])];
|
|
33
35
|
return [lines.join('\n'), base, await howWeWork(bridge)].filter((part) => part !== '').join('\n\n');
|
|
34
36
|
}
|
package/dist/kit-main.js
CHANGED
|
@@ -1,19 +1,67 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { setGlobalProxyFromEnv } from 'node:http';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { answerGitCredential } from './app-git.js';
|
|
5
|
+
import { chatgpt } from './chatgpt.js';
|
|
3
6
|
import { login } from './login.js';
|
|
4
7
|
import { logout } from './logout.js';
|
|
5
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
|
+
]);
|
|
6
39
|
const [command, ...argv] = process.argv.slice(2);
|
|
7
40
|
setGlobalProxyFromEnv();
|
|
8
41
|
async function run() {
|
|
9
42
|
if (command === 'login')
|
|
10
43
|
return login(argv);
|
|
11
|
-
if (command === '
|
|
12
|
-
return
|
|
13
|
-
if (command === '
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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);
|
|
17
65
|
}
|
|
18
66
|
run().catch((error) => {
|
|
19
67
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
package/dist/login.js
CHANGED
|
@@ -1,55 +1,25 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
1
2
|
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
-
import { createServer } from 'node:http';
|
|
3
3
|
import { hostname } from 'node:os';
|
|
4
|
-
import { createInterface } from 'node:readline/promises';
|
|
5
4
|
import { parseArgs } from 'node:util';
|
|
6
|
-
import { post } from './api.js';
|
|
5
|
+
import { post, retrying } from './api.js';
|
|
6
|
+
import { placeAppGit } from './app-git.js';
|
|
7
|
+
import { writeCodexRule } from './clis.js';
|
|
7
8
|
import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
|
|
8
9
|
import { callHouse, rpc } from './mcp.js';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
response.writeHead(404).end();
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
held = response;
|
|
24
|
-
deliver(received);
|
|
25
|
-
});
|
|
26
|
-
const close = () => {
|
|
27
|
-
server.close();
|
|
28
|
-
server.closeAllConnections();
|
|
29
|
-
};
|
|
30
|
-
const port = Number(process.env.HOUSE_KIT_LOGIN_PORT ?? 0);
|
|
31
|
-
server.once('error', reject);
|
|
32
|
-
server.listen(port, port === 0 ? '127.0.0.1' : '0.0.0.0', () => {
|
|
33
|
-
const address = server.address();
|
|
34
|
-
if (address === null || typeof address === 'string') {
|
|
35
|
-
reject(new Error('the login callback has no port'));
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
resolve({
|
|
39
|
-
uri: `http://127.0.0.1:${address.port}/callback`,
|
|
40
|
-
code,
|
|
41
|
-
answer: (text) => new Promise((written) => {
|
|
42
|
-
if (held === null || held.destroyed || !held.socket || held.socket.destroyed)
|
|
43
|
-
return written();
|
|
44
|
-
held.once('close', () => written());
|
|
45
|
-
held.writeHead(200, { 'content-type': 'text/plain; charset=utf-8', connection: 'close' }).end(`${text}\n`);
|
|
46
|
-
}),
|
|
47
|
-
close,
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
10
|
+
import { selectRelay } from './relay.js';
|
|
11
|
+
import { startService } from './service.js';
|
|
12
|
+
import { placeSkillSet } from './skills.js';
|
|
13
|
+
export const DEFAULT_HOUSE = 'https://agents.house';
|
|
14
|
+
const OPENER = process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
15
|
+
// Polling every two seconds brings the User's confirmation to the waiting Kit moments after they give it.
|
|
16
|
+
const POLL_INTERVAL_MS = 2000;
|
|
17
|
+
function open(link) {
|
|
18
|
+
spawn(OPENER, [link], { detached: true, stdio: 'ignore' })
|
|
19
|
+
.on('error', () => undefined)
|
|
20
|
+
.unref();
|
|
51
21
|
}
|
|
52
|
-
async function accepted(house, credential) {
|
|
22
|
+
export async function accepted(house, credential) {
|
|
53
23
|
const checked = await callHouse(house, credential, rpc('tools/list', {}));
|
|
54
24
|
if (checked.status === 200)
|
|
55
25
|
return true;
|
|
@@ -57,17 +27,20 @@ async function accepted(house, credential) {
|
|
|
57
27
|
return false;
|
|
58
28
|
throw new Error(`House answered ${checked.status} when Kit checked this computer's own agent's connection: ${checked.text}`);
|
|
59
29
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
30
|
+
const USAGE = `usage: kit login [--house <origin>] [--environment <id>] [--rooms <ref>[,<ref>...] | --no-private]
|
|
31
|
+
kit login [--house <origin>] --code <code>
|
|
32
|
+
--house <origin> the House to connect to, ${DEFAULT_HOUSE} unless one is stored
|
|
33
|
+
--environment <id> reconnect this Environment
|
|
34
|
+
--rooms <ref>[,<ref>...] give this Environment only these Rooms
|
|
35
|
+
--no-private give this Environment every Room but your Private Room
|
|
36
|
+
--code <code> connect this machine with a login code
|
|
37
|
+
`;
|
|
38
|
+
function chosenRooms(rooms, noPrivate) {
|
|
39
|
+
if (rooms !== undefined && noPrivate)
|
|
40
|
+
throw new Error('--rooms and --no-private are two choices of Rooms; pass one');
|
|
41
|
+
if (rooms !== undefined)
|
|
42
|
+
return { rooms: { reach: 'selected', refs: rooms.split(',') } };
|
|
43
|
+
return noPrivate ? { rooms: { reach: 'all_but_private' } } : {};
|
|
71
44
|
}
|
|
72
45
|
export async function login(argv) {
|
|
73
46
|
const { values } = parseArgs({
|
|
@@ -75,62 +48,80 @@ export async function login(argv) {
|
|
|
75
48
|
options: {
|
|
76
49
|
house: { type: 'string' },
|
|
77
50
|
environment: { type: 'string' },
|
|
78
|
-
|
|
51
|
+
rooms: { type: 'string' },
|
|
52
|
+
'no-private': { type: 'boolean', default: false },
|
|
53
|
+
code: { type: 'string' },
|
|
54
|
+
help: { type: 'boolean', default: false },
|
|
79
55
|
},
|
|
80
56
|
});
|
|
57
|
+
if (values.help) {
|
|
58
|
+
process.stdout.write(USAGE);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (values.code !== undefined && values.rooms !== undefined)
|
|
62
|
+
throw new Error('--code cannot be used with --rooms');
|
|
63
|
+
if (values.code !== undefined && values['no-private'])
|
|
64
|
+
throw new Error('--code cannot be used with --no-private');
|
|
65
|
+
if (values.code !== undefined && values.environment !== undefined)
|
|
66
|
+
throw new Error('--code cannot be used with --environment');
|
|
67
|
+
const rooms = chosenRooms(values.rooms, values['no-private']);
|
|
81
68
|
const enrolled = (await readEnrolment()) ?? (await readDisconnected());
|
|
69
|
+
if (values.code !== undefined && enrolled !== null) {
|
|
70
|
+
throw new Error(`--code connects a new machine; this Kit home records Environment ${enrolled.environment}, so run kit login to reconnect it`);
|
|
71
|
+
}
|
|
72
|
+
const relay = process.env.HOUSE_KIT_RELAY || undefined;
|
|
73
|
+
selectRelay(relay);
|
|
82
74
|
const house = values.house ?? enrolled?.house ?? DEFAULT_HOUSE;
|
|
83
75
|
const replaces = process.env.HOUSE_KIT_REPLACES;
|
|
84
76
|
const environment = replaces === undefined ? (values.environment ?? (enrolled?.house === house ? enrolled.environment : undefined)) : undefined;
|
|
85
77
|
const ownAgent = await readOwnAgent();
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
78
|
+
let held = false;
|
|
79
|
+
let exchanged;
|
|
80
|
+
if (values.code !== undefined) {
|
|
81
|
+
exchanged = await post(house, '/kit/token', { code: values.code, label: hostname() });
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
held = ownAgent !== null && ownAgent.house === house && (await accepted(house, ownAgent.credential));
|
|
85
|
+
const verifier = randomBytes(32).toString('base64url');
|
|
86
|
+
const started = await retrying(() => post(house, '/kit', {
|
|
87
|
+
label: hostname(),
|
|
88
|
+
...(environment === undefined
|
|
89
|
+
? { act: 'new_environment', ...(replaces === undefined ? {} : { replaces }) }
|
|
90
|
+
: { act: 'existing_environment', environment_id: environment }),
|
|
91
|
+
...rooms,
|
|
92
|
+
code_challenge: createHash('sha256').update(verifier).digest('base64url'),
|
|
93
|
+
...(held ? {} : { headless: true }),
|
|
94
|
+
}));
|
|
95
|
+
process.stdout.write(`Open this link and confirm: ${started.start_url}\n`);
|
|
96
|
+
open(started.start_url);
|
|
97
|
+
exchanged = await retrying(() => post(house, '/kit/token', { login: started.login, code_verifier: verifier }), undefined, () => POLL_INTERVAL_MS);
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
await placeSkillSet();
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
process.stderr.write(`kit: the skills could not be placed: ${error.message}\n`);
|
|
104
|
+
}
|
|
105
|
+
await writeCodexRule().catch((error) => process.stderr.write(`kit: ${error.message}\n`));
|
|
106
|
+
await writeEnrolment({
|
|
107
|
+
house,
|
|
108
|
+
...(relay === undefined ? {} : { relay }),
|
|
109
|
+
environment: exchanged.environment,
|
|
110
|
+
credential: exchanged.credential,
|
|
111
|
+
});
|
|
112
|
+
process.stdout.write(`Environment ${exchanged.environment} is connected to ${house}.\n`);
|
|
113
|
+
if (exchanged.headless_credential !== undefined) {
|
|
114
|
+
await writeOwnAgent({ house, user: exchanged.user, credential: exchanged.headless_credential });
|
|
115
|
+
}
|
|
116
|
+
else if (held && ownAgent !== null && ownAgent.user !== exchanged.user) {
|
|
117
|
+
await forgetOwnAgent();
|
|
118
|
+
process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
|
|
115
119
|
}
|
|
116
|
-
|
|
120
|
+
await placeAppGit(ownAgent, await readOwnAgent());
|
|
117
121
|
try {
|
|
118
|
-
|
|
119
|
-
...started,
|
|
120
|
-
presentation: 'loopback',
|
|
121
|
-
loopback_uri: callback.uri,
|
|
122
|
-
});
|
|
123
|
-
process.stdout.write(`Open this link and confirm: ${start_url}\n`);
|
|
124
|
-
try {
|
|
125
|
-
await exchange(await callback.code);
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
await callback.answer(`This Environment was not connected: ${error.message}`);
|
|
129
|
-
throw error;
|
|
130
|
-
}
|
|
131
|
-
await callback.answer('This Environment is connected. You can close this tab.');
|
|
122
|
+
startService();
|
|
132
123
|
}
|
|
133
|
-
|
|
134
|
-
|
|
124
|
+
catch (error) {
|
|
125
|
+
process.stderr.write(`kit: ${error.message}\n`);
|
|
135
126
|
}
|
|
136
127
|
}
|