@agentshouse/kit 0.2.1-c5262a8cc063d → 0.2.1-c5987c9cb6553
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 +103 -42
- package/dist/api.js +36 -3
- package/dist/app-git.js +64 -0
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +36 -52
- package/dist/chatgpt.js +25 -0
- package/dist/clis.js +127 -65
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +288 -236
- package/dist/copies.js +167 -139
- 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 +41 -3
- package/dist/house-main.js +158 -55
- 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/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 +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/skills.js +1 -1
- package/dist/stream.js +37 -6
- 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,62 +1,105 @@
|
|
|
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";
|
|
26
|
+
const UNCONFIRMED = 'with no result; a write may or may not have taken effect, so read before you repeat it';
|
|
17
27
|
const socketPath = process.env.HOUSE_BRIDGE;
|
|
18
|
-
|
|
28
|
+
const MEDIA_TYPES = new Map([
|
|
29
|
+
['avif', 'image/avif'],
|
|
30
|
+
['csv', 'text/csv'],
|
|
31
|
+
['docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
|
|
32
|
+
['gif', 'image/gif'],
|
|
33
|
+
['heic', 'image/heic'],
|
|
34
|
+
['html', 'text/html'],
|
|
35
|
+
['jpeg', 'image/jpeg'],
|
|
36
|
+
['jpg', 'image/jpeg'],
|
|
37
|
+
['json', 'application/json'],
|
|
38
|
+
['m4a', 'audio/mp4'],
|
|
39
|
+
['md', 'text/markdown'],
|
|
40
|
+
['mov', 'video/quicktime'],
|
|
41
|
+
['mp3', 'audio/mpeg'],
|
|
42
|
+
['mp4', 'video/mp4'],
|
|
43
|
+
['ogg', 'audio/ogg'],
|
|
44
|
+
['pdf', 'application/pdf'],
|
|
45
|
+
['png', 'image/png'],
|
|
46
|
+
['pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'],
|
|
47
|
+
['svg', 'image/svg+xml'],
|
|
48
|
+
['txt', 'text/plain'],
|
|
49
|
+
['wav', 'audio/wav'],
|
|
50
|
+
['webm', 'video/webm'],
|
|
51
|
+
['webp', 'image/webp'],
|
|
52
|
+
['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
|
|
53
|
+
['zip', 'application/zip'],
|
|
54
|
+
]);
|
|
55
|
+
function refused(path, method, status, text, local) {
|
|
19
56
|
const refusal = refusalOf(text);
|
|
20
57
|
if (refusal !== null)
|
|
21
58
|
return refusal.text;
|
|
22
59
|
if (status === 401)
|
|
23
60
|
return REVOKED;
|
|
61
|
+
const act = method === 'GET' ? 'download' : 'upload';
|
|
24
62
|
if (URL.canParse(path))
|
|
25
|
-
return `the
|
|
26
|
-
if (status === 502 &&
|
|
63
|
+
return `the ${act} answered ${status}; ${act} the file again`;
|
|
64
|
+
if (status === 502 && local)
|
|
27
65
|
return text;
|
|
28
|
-
return `${path} answered ${status}`;
|
|
66
|
+
return path === '/' ? unconfirmed(status) : `${path} answered ${status}`;
|
|
29
67
|
}
|
|
30
|
-
function
|
|
68
|
+
function unconfirmed(status) {
|
|
69
|
+
return `House answered ${status} ${UNCONFIRMED}`;
|
|
70
|
+
}
|
|
71
|
+
function exchanged(path, method, body, headers = { 'content-type': 'application/json' }, to = socketPath) {
|
|
31
72
|
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
|
-
});
|
|
73
|
+
const sent = request({ socketPath: to, path, method, headers }, (answer) => {
|
|
74
|
+
const chunks = [];
|
|
75
|
+
answer.on('data', (chunk) => chunks.push(chunk));
|
|
38
76
|
answer.on('end', () => {
|
|
77
|
+
const bytes = Buffer.concat(chunks);
|
|
39
78
|
if (answer.statusCode === 200)
|
|
40
|
-
resolve(
|
|
79
|
+
resolve(bytes);
|
|
41
80
|
else
|
|
42
|
-
reject(new Error(refused(path, answer.statusCode,
|
|
81
|
+
reject(new Error(refused(path, method, answer.statusCode, bytes.toString('utf8'), answer.headers['content-type'] === undefined)));
|
|
43
82
|
});
|
|
44
83
|
});
|
|
45
|
-
sent.on('error', () => reject(new Error(CLOSED)));
|
|
46
|
-
sent.end(body instanceof Buffer ? body : JSON.stringify(body));
|
|
84
|
+
sent.on('error', (error) => reject(new Error(error.code === 'EPERM' || error.code === 'EACCES' ? SANDBOXED : CLOSED)));
|
|
85
|
+
sent.end(body === undefined ? undefined : body instanceof Buffer ? body : JSON.stringify(body));
|
|
47
86
|
});
|
|
48
87
|
}
|
|
88
|
+
async function bridged(path, body, headers, to = socketPath) {
|
|
89
|
+
return (await exchanged(path, 'POST', body, headers, to)).toString('utf8');
|
|
90
|
+
}
|
|
49
91
|
async function direct(message) {
|
|
50
92
|
const ownAgent = await readOwnAgent();
|
|
51
93
|
if (ownAgent === null)
|
|
52
94
|
throw new Error(UNCONNECTED);
|
|
95
|
+
await readEnrolment();
|
|
53
96
|
setGlobalProxyFromEnv();
|
|
54
97
|
const answer = await callHouse(ownAgent.house, ownAgent.credential, message).catch(() => {
|
|
55
98
|
throw new Error(UNREACHABLE);
|
|
56
99
|
});
|
|
57
100
|
if (answer.status === 200)
|
|
58
101
|
return answer.text;
|
|
59
|
-
throw new Error(answer.status === 401 ? DISCONNECTED : (refusalOf(answer.text)?.text ??
|
|
102
|
+
throw new Error(answer.status === 401 ? DISCONNECTED : (refusalOf(answer.text)?.text ?? unconfirmed(answer.status)));
|
|
60
103
|
}
|
|
61
104
|
async function mcp(method, params) {
|
|
62
105
|
const message = rpc(method, params);
|
|
@@ -78,31 +121,75 @@ async function localFile(path) {
|
|
|
78
121
|
: `${path} is not a readable file`);
|
|
79
122
|
}
|
|
80
123
|
}
|
|
81
|
-
async function
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
124
|
+
async function transferred(url, method, bytes, headers) {
|
|
125
|
+
if (socketPath !== undefined)
|
|
126
|
+
return exchanged(url, method, bytes, headers);
|
|
127
|
+
setGlobalProxyFromEnv();
|
|
128
|
+
const answer = await fetch(relayed(url), { method, ...(headers === undefined ? {} : { headers }), ...(bytes === undefined ? {} : { body: bytes }) }).catch(() => {
|
|
129
|
+
throw new Error(UNREACHABLE);
|
|
130
|
+
});
|
|
131
|
+
const received = Buffer.from(await answer.arrayBuffer());
|
|
132
|
+
if (answer.status === 200)
|
|
133
|
+
return received;
|
|
134
|
+
throw new Error(refused(url, method, answer.status, received.toString('utf8'), false));
|
|
135
|
+
}
|
|
136
|
+
async function sent(url, bytes, operation) {
|
|
137
|
+
const headers = { 'content-type': 'application/octet-stream', 'x-house-byte-operation': operation };
|
|
138
|
+
return (await transferred(url, 'POST', bytes, headers)).toString('utf8');
|
|
139
|
+
}
|
|
140
|
+
async function download(args) {
|
|
141
|
+
const { flags, positionals: [argument] } = accepted(args, ['--help'], 1, DOWNLOAD_USAGE);
|
|
142
|
+
if (flags.has('--help'))
|
|
143
|
+
return DOWNLOAD_USAGE;
|
|
144
|
+
const { file, ...selected } = argumentsOf(argument);
|
|
145
|
+
if (typeof file !== 'string' || file === '')
|
|
146
|
+
throw new Error('download needs "file", the local file to write');
|
|
147
|
+
const inspected = parse(await called('inspect', selected));
|
|
148
|
+
if (typeof inspected?.download !== 'string')
|
|
149
|
+
throw new Error(`${String(selected.path)} holds no binary file; shell reads its text`);
|
|
150
|
+
const bytes = verified(await transferred(inspected.download, 'GET'), String(inspected.sha256), String(selected.path));
|
|
151
|
+
const written = resolve(file);
|
|
152
|
+
await mkdir(dirname(written), { recursive: true });
|
|
153
|
+
await writeFile(written, bytes);
|
|
154
|
+
return written;
|
|
155
|
+
}
|
|
156
|
+
async function upload({ file, ...destination }) {
|
|
157
|
+
if (typeof file !== 'string' || file === '')
|
|
158
|
+
throw new Error('upload needs "file", a local file');
|
|
159
|
+
const bytes = await localFile(file);
|
|
160
|
+
const mediaType = MEDIA_TYPES.get(extname(file).slice(1).toLowerCase());
|
|
161
|
+
const answer = await called('upload', {
|
|
162
|
+
...destination,
|
|
88
163
|
bytes: bytes.length,
|
|
89
164
|
sha256: createHash('sha256').update(bytes).digest('hex'),
|
|
90
|
-
|
|
165
|
+
...(mediaType === undefined ? {} : { media_type: mediaType }),
|
|
166
|
+
});
|
|
167
|
+
const declared = parse(answer);
|
|
91
168
|
if (declared.upload === undefined)
|
|
92
|
-
return
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
169
|
+
return answer;
|
|
170
|
+
const written = JSON.parse(await sent(declared.upload.url, bytes, declared.upload.operation));
|
|
171
|
+
return stringify(written, { lineWidth: 0 }).trimEnd();
|
|
172
|
+
}
|
|
173
|
+
function unexpected(argument, usage) {
|
|
174
|
+
return new Error(`unexpected argument ${argument}; ${usage}`);
|
|
175
|
+
}
|
|
176
|
+
function accepted(args, flags, positionals, usage) {
|
|
177
|
+
const given = { flags: new Set(), positionals: [] };
|
|
178
|
+
for (const arg of args) {
|
|
179
|
+
if (flags.includes(arg))
|
|
180
|
+
given.flags.add(arg);
|
|
181
|
+
else if (!arg.startsWith('-') && given.positionals.length < positionals)
|
|
182
|
+
given.positionals.push(arg);
|
|
183
|
+
else
|
|
184
|
+
throw unexpected(arg, usage);
|
|
185
|
+
}
|
|
186
|
+
return given;
|
|
100
187
|
}
|
|
101
188
|
async function push(args) {
|
|
102
|
-
|
|
189
|
+
const { flags, positionals: [commit = 'HEAD'] } = accepted(args, ['--help', '--owner'], 1, PUSH_USAGE);
|
|
190
|
+
if (flags.has('--help'))
|
|
103
191
|
return PUSH_USAGE;
|
|
104
|
-
const owner =
|
|
105
|
-
const commit = args.find((arg) => arg !== '--owner') ?? 'HEAD';
|
|
192
|
+
const owner = flags.has('--owner');
|
|
106
193
|
if (owner && socketPath !== undefined)
|
|
107
194
|
throw new Error('--owner is not for Agent conversations; run house push without it');
|
|
108
195
|
if (!owner && socketPath === undefined)
|
|
@@ -112,6 +199,14 @@ async function push(args) {
|
|
|
112
199
|
throw new Error(pushed.text);
|
|
113
200
|
return pushed.text;
|
|
114
201
|
}
|
|
202
|
+
async function fetched(args) {
|
|
203
|
+
if (accepted(args, ['--help'], 0, FETCH_USAGE).flags.has('--help'))
|
|
204
|
+
return FETCH_USAGE;
|
|
205
|
+
const answer = JSON.parse(await bridged('/git/fetch', {}, undefined, socketPath ?? join(kitHome(), 'owner.sock')));
|
|
206
|
+
if (answer.refused)
|
|
207
|
+
throw new Error(answer.text);
|
|
208
|
+
return answer.text;
|
|
209
|
+
}
|
|
115
210
|
function argumentsOf(argument) {
|
|
116
211
|
if (argument === undefined)
|
|
117
212
|
return {};
|
|
@@ -126,36 +221,44 @@ function argumentsOf(argument) {
|
|
|
126
221
|
throw new Error(ARGUMENTS);
|
|
127
222
|
return parsed;
|
|
128
223
|
}
|
|
129
|
-
async function house(
|
|
130
|
-
|
|
131
|
-
|
|
224
|
+
async function house(args) {
|
|
225
|
+
const { flags, positionals: [verb, argument] } = accepted(args, ['--help'], 2, USAGE);
|
|
226
|
+
if (verb === 'help' && argument !== undefined)
|
|
227
|
+
throw unexpected(argument, USAGE);
|
|
228
|
+
if (flags.has('--help') && argument !== undefined)
|
|
229
|
+
throw unexpected('--help', USAGE);
|
|
230
|
+
if (verb === undefined || verb === 'help') {
|
|
231
|
+
return [USAGE, 'help', PUSH, FETCH, DOWNLOAD, ...(await tools()).map((tool) => (tool.description === undefined ? tool.name : `${tool.name}: ${tool.description}`))].join('\n');
|
|
132
232
|
}
|
|
133
|
-
if (
|
|
233
|
+
if (flags.has('--help')) {
|
|
134
234
|
const tool = (await tools()).find((listed) => listed.name === verb);
|
|
135
235
|
if (tool === undefined)
|
|
136
236
|
throw new Error(`${verb} is no Tool; house find_command '{"query":"${verb}"}' finds Commands`);
|
|
137
237
|
return [...(tool.description === undefined ? [] : [tool.description]), JSON.stringify(tool.inputSchema)].join('\n');
|
|
138
238
|
}
|
|
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 });
|
|
239
|
+
const parsed = argumentsOf(argument);
|
|
240
|
+
return verb === 'upload' ? upload(parsed) : called(verb, parsed);
|
|
241
|
+
}
|
|
242
|
+
async function called(name, args) {
|
|
243
|
+
const result = await mcp('tools/call', { name, arguments: args });
|
|
151
244
|
const text = result.content.map((content) => content.text).join('\n');
|
|
152
245
|
if (result.isError === true)
|
|
153
246
|
throw new Error(text.replace(/\n+$/, ''));
|
|
154
247
|
return text;
|
|
155
248
|
}
|
|
156
249
|
const argv = process.argv.slice(2);
|
|
157
|
-
|
|
250
|
+
const named = argv.findIndex((arg) => !arg.startsWith('-'));
|
|
251
|
+
const rest = argv.filter((_arg, index) => index !== named);
|
|
252
|
+
(argv[named] === 'push'
|
|
253
|
+
? push(rest)
|
|
254
|
+
: argv[named] === 'fetch'
|
|
255
|
+
? fetched(rest)
|
|
256
|
+
: argv[named] === 'download'
|
|
257
|
+
? download(rest)
|
|
258
|
+
: house(argv)).then((text) => {
|
|
158
259
|
process.stdout.write(`${text}\n`);
|
|
260
|
+
if (argv[0] === 'shell')
|
|
261
|
+
process.exitCode = Number(/^exit: (\d+)(?:\n|$)/.exec(text)?.[1] ?? 0);
|
|
159
262
|
}, (error) => {
|
|
160
263
|
process.stderr.write(`house: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
161
264
|
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
|
}
|