@agentshouse/kit 0.2.1-alpha.9 → 0.2.1-c14f8446ba284
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 +37 -20
- package/dist/abort.js +9 -0
- package/dist/acp.js +18 -75
- package/dist/agents.js +246 -52
- package/dist/api.js +36 -4
- package/dist/app-git.js +64 -0
- package/dist/bootstrap.json +10 -14
- package/dist/bridge.js +99 -70
- package/dist/clis-main.js +23 -0
- package/dist/clis.js +171 -71
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +324 -199
- package/dist/copies.js +70 -68
- package/dist/end.js +8 -0
- package/dist/events.js +521 -0
- package/dist/files.js +16 -1
- package/dist/git-main.js +2 -2
- package/dist/git.js +6 -2
- package/dist/home.js +32 -3
- package/dist/house-main.js +117 -44
- package/dist/instructions.js +13 -12
- package/dist/kit-main.js +7 -1
- package/dist/login.js +39 -94
- package/dist/logout.js +21 -0
- package/dist/mcp.js +39 -0
- package/dist/operation.js +7 -4
- package/dist/parts.js +64 -0
- package/dist/refusals.js +53 -0
- package/dist/relay.js +10 -0
- package/dist/resident.js +46 -21
- package/dist/scope.js +203 -0
- package/dist/secret-input.js +7 -2
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +48 -11
- package/dist/skills.js +5 -10
- package/dist/stream.js +31 -6
- package/dist/transcription.js +170 -0
- package/dist/update.js +119 -0
- package/dist/work.js +348 -0
- package/how-we-work.md +10 -12
- package/package.json +4 -7
- package/dist/blocks.js +0 -51
package/dist/house-main.js
CHANGED
|
@@ -1,13 +1,35 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { createHash
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
3
|
import { readFile } from 'node:fs/promises';
|
|
4
|
-
import { request } from 'node:http';
|
|
4
|
+
import { request, setGlobalProxyFromEnv } from 'node:http';
|
|
5
5
|
import { basename, join } from 'node:path';
|
|
6
|
-
import { kitHome } from './home.js';
|
|
6
|
+
import { kitHome, readOwnAgent } from './home.js';
|
|
7
|
+
import { callHouse, rpc, UNREACHABLE } from './mcp.js';
|
|
8
|
+
import { refusalOf } from './refusals.js';
|
|
9
|
+
import { relayed } from './relay.js';
|
|
10
|
+
import { parse } from 'yaml';
|
|
7
11
|
const USAGE = "usage: house <tool> ['<arguments as JSON>']";
|
|
8
|
-
const
|
|
9
|
-
const
|
|
12
|
+
const PUSH_USAGE = 'usage: house push [commit] [--owner]';
|
|
13
|
+
const PUSH = "push [commit]: sends the Local copy's committed changes to House";
|
|
14
|
+
const ARGUMENTS = `the arguments are one JSON object in single quotes, like house search '{"query":"invoice"}'`;
|
|
15
|
+
const CLOSED = "this conversation's House connection is closed; nothing to do from here";
|
|
16
|
+
const SANDBOXED = "your CLI's sandbox kept house from House; run house directly, not through another program";
|
|
17
|
+
const REVOKED = 'this conversation no longer has House access; nothing to do from here';
|
|
18
|
+
const UNCONNECTED = "this computer's own agent has no House connection; run kit login";
|
|
19
|
+
const DISCONNECTED = "House no longer accepts this computer's own agent; run kit login";
|
|
10
20
|
const socketPath = process.env.HOUSE_BRIDGE;
|
|
21
|
+
function refused(path, status, text) {
|
|
22
|
+
const refusal = refusalOf(text);
|
|
23
|
+
if (refusal !== null)
|
|
24
|
+
return refusal.text;
|
|
25
|
+
if (status === 401)
|
|
26
|
+
return REVOKED;
|
|
27
|
+
if (URL.canParse(path))
|
|
28
|
+
return `the upload answered ${status}; upload the file again`;
|
|
29
|
+
if (status === 502 && /^[A-Za-z]/.test(text))
|
|
30
|
+
return text;
|
|
31
|
+
return `${path} answered ${status}`;
|
|
32
|
+
}
|
|
11
33
|
function bridged(path, body, headers = { 'content-type': 'application/json' }, to = socketPath) {
|
|
12
34
|
return new Promise((resolve, reject) => {
|
|
13
35
|
const sent = request({ socketPath: to, path, method: 'POST', headers }, (answer) => {
|
|
@@ -20,15 +42,28 @@ function bridged(path, body, headers = { 'content-type': 'application/json' }, t
|
|
|
20
42
|
if (answer.statusCode === 200)
|
|
21
43
|
resolve(text);
|
|
22
44
|
else
|
|
23
|
-
reject(new Error(
|
|
45
|
+
reject(new Error(refused(path, answer.statusCode, text)));
|
|
24
46
|
});
|
|
25
47
|
});
|
|
26
|
-
sent.on('error', reject);
|
|
48
|
+
sent.on('error', (error) => reject(new Error(error.code === 'EPERM' || error.code === 'EACCES' ? SANDBOXED : CLOSED)));
|
|
27
49
|
sent.end(body instanceof Buffer ? body : JSON.stringify(body));
|
|
28
50
|
});
|
|
29
51
|
}
|
|
52
|
+
async function direct(message) {
|
|
53
|
+
const ownAgent = await readOwnAgent();
|
|
54
|
+
if (ownAgent === null)
|
|
55
|
+
throw new Error(UNCONNECTED);
|
|
56
|
+
setGlobalProxyFromEnv();
|
|
57
|
+
const answer = await callHouse(ownAgent.house, ownAgent.credential, message).catch(() => {
|
|
58
|
+
throw new Error(UNREACHABLE);
|
|
59
|
+
});
|
|
60
|
+
if (answer.status === 200)
|
|
61
|
+
return answer.text;
|
|
62
|
+
throw new Error(answer.status === 401 ? DISCONNECTED : (refusalOf(answer.text)?.text ?? `House answered ${answer.status}`));
|
|
63
|
+
}
|
|
30
64
|
async function mcp(method, params) {
|
|
31
|
-
const
|
|
65
|
+
const message = rpc(method, params);
|
|
66
|
+
const answer = JSON.parse(socketPath === undefined ? await direct(message) : await bridged('/', message));
|
|
32
67
|
if (answer.error !== undefined)
|
|
33
68
|
throw new Error(answer.error.message);
|
|
34
69
|
return answer.result;
|
|
@@ -36,72 +71,110 @@ async function mcp(method, params) {
|
|
|
36
71
|
async function tools() {
|
|
37
72
|
return (await mcp('tools/list', {})).tools;
|
|
38
73
|
}
|
|
39
|
-
async function
|
|
40
|
-
|
|
41
|
-
|
|
74
|
+
async function localFile(path) {
|
|
75
|
+
try {
|
|
76
|
+
return await readFile(path);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
throw new Error(error.code === 'EISDIR'
|
|
80
|
+
? `${path} is a folder; upload one file at a time`
|
|
81
|
+
: `${path} is not a readable file`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function sent(url, bytes, operation) {
|
|
85
|
+
const headers = { 'content-type': 'application/octet-stream', 'x-house-byte-operation': operation };
|
|
86
|
+
if (socketPath !== undefined)
|
|
87
|
+
return bridged(url, bytes, headers);
|
|
88
|
+
setGlobalProxyFromEnv();
|
|
89
|
+
const answer = await fetch(relayed(url), { method: 'POST', headers, body: bytes }).catch(() => {
|
|
90
|
+
throw new Error(UNREACHABLE);
|
|
91
|
+
});
|
|
92
|
+
const text = await answer.text();
|
|
93
|
+
if (answer.status === 200)
|
|
94
|
+
return text;
|
|
95
|
+
throw new Error(refused(url, answer.status, text));
|
|
96
|
+
}
|
|
97
|
+
async function upload(path, roomRef, missing) {
|
|
98
|
+
if (typeof path !== 'string' || path === '')
|
|
99
|
+
throw new Error(missing);
|
|
100
|
+
const bytes = await localFile(path);
|
|
101
|
+
const declared = parse(await called('upload', {
|
|
42
102
|
...(roomRef === undefined ? {} : { room_ref: roomRef }),
|
|
43
|
-
version: randomUUID(),
|
|
44
103
|
name: basename(path),
|
|
45
104
|
bytes: bytes.length,
|
|
46
105
|
sha256: createHash('sha256').update(bytes).digest('hex'),
|
|
47
106
|
}));
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
107
|
+
if (declared.upload === undefined)
|
|
108
|
+
return declared.attachment;
|
|
109
|
+
const saved = JSON.parse(await sent(declared.upload.url, bytes, declared.upload.operation));
|
|
110
|
+
if (saved.upload_failure !== undefined)
|
|
111
|
+
throw new Error(`${path} was not saved: ${saved.upload_failure}; call again later`);
|
|
112
|
+
return saved.attachment;
|
|
52
113
|
}
|
|
53
|
-
async function
|
|
114
|
+
async function push(args) {
|
|
54
115
|
if (args.includes('--help'))
|
|
55
|
-
return
|
|
56
|
-
if (args[0] !== 'push')
|
|
57
|
-
throw new Error(GIT_USAGE);
|
|
116
|
+
return PUSH_USAGE;
|
|
58
117
|
const owner = args.includes('--owner');
|
|
59
|
-
const commit = args.
|
|
118
|
+
const commit = args.find((arg) => arg !== '--owner') ?? 'HEAD';
|
|
60
119
|
if (owner && socketPath !== undefined)
|
|
61
|
-
throw new Error('
|
|
120
|
+
throw new Error('--owner is not for Agent conversations; run house push without it');
|
|
62
121
|
if (!owner && socketPath === undefined)
|
|
63
|
-
throw new Error('outside an Agent conversation, house
|
|
122
|
+
throw new Error('outside an Agent conversation, house push needs --owner');
|
|
64
123
|
const pushed = JSON.parse(await bridged('/git/push', { cwd: process.cwd(), commit }, undefined, owner ? join(kitHome(), 'owner.sock') : socketPath));
|
|
65
124
|
if (pushed.refused)
|
|
66
125
|
throw new Error(pushed.text);
|
|
67
126
|
return pushed.text;
|
|
68
127
|
}
|
|
128
|
+
function argumentsOf(argument) {
|
|
129
|
+
if (argument === undefined)
|
|
130
|
+
return {};
|
|
131
|
+
let parsed;
|
|
132
|
+
try {
|
|
133
|
+
parsed = JSON.parse(argument);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
throw new Error(ARGUMENTS);
|
|
137
|
+
}
|
|
138
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed))
|
|
139
|
+
throw new Error(ARGUMENTS);
|
|
140
|
+
return parsed;
|
|
141
|
+
}
|
|
69
142
|
async function house([verb, argument]) {
|
|
70
|
-
if (verb === undefined || verb === '--help') {
|
|
71
|
-
return [USAGE,
|
|
143
|
+
if (verb === undefined || verb === '--help' || verb === 'help') {
|
|
144
|
+
return [USAGE, 'help', PUSH, ...(await tools()).map((tool) => (tool.description === undefined ? tool.name : `${tool.name}: ${tool.description}`))].join('\n');
|
|
72
145
|
}
|
|
73
146
|
if (argument === '--help') {
|
|
74
147
|
const tool = (await tools()).find((listed) => listed.name === verb);
|
|
75
148
|
if (tool === undefined)
|
|
76
|
-
throw new Error(
|
|
77
|
-
return
|
|
149
|
+
throw new Error(`${verb} is no Tool; house find_command '{"query":"${verb}"}' finds Commands`);
|
|
150
|
+
return [...(tool.description === undefined ? [] : [tool.description]), JSON.stringify(tool.inputSchema)].join('\n');
|
|
151
|
+
}
|
|
152
|
+
const args = argumentsOf(argument);
|
|
153
|
+
if (verb === 'upload') {
|
|
154
|
+
return JSON.stringify({ attachment: await upload(args.path, args.room_ref, 'upload needs "path", a local file') });
|
|
78
155
|
}
|
|
79
|
-
const args = (argument === undefined ? {} : JSON.parse(argument));
|
|
80
|
-
if (verb === 'upload_attachment')
|
|
81
|
-
return upload(String(args.path), args.room_ref);
|
|
82
156
|
if (verb === 'append_record' && Array.isArray(args.attachments)) {
|
|
83
157
|
const attachments = [];
|
|
84
158
|
for (const path of args.attachments) {
|
|
85
|
-
attachments.push(
|
|
159
|
+
attachments.push(await upload(path, args.room_ref, 'each attachments entry is the path of a local file'));
|
|
86
160
|
}
|
|
87
161
|
args.attachments = attachments;
|
|
88
162
|
}
|
|
89
|
-
|
|
163
|
+
return called(verb, args);
|
|
164
|
+
}
|
|
165
|
+
async function called(name, args) {
|
|
166
|
+
const result = await mcp('tools/call', { name, arguments: args });
|
|
90
167
|
const text = result.content.map((content) => content.text).join('\n');
|
|
91
168
|
if (result.isError === true)
|
|
92
|
-
throw new Error(text);
|
|
169
|
+
throw new Error(text.replace(/\n+$/, ''));
|
|
93
170
|
return text;
|
|
94
171
|
}
|
|
95
172
|
const argv = process.argv.slice(2);
|
|
96
|
-
|
|
97
|
-
process.
|
|
173
|
+
(argv[0] === 'push' ? push(argv.slice(1)) : house(argv)).then((text) => {
|
|
174
|
+
process.stdout.write(`${text}\n`);
|
|
175
|
+
if (argv[0] === 'shell')
|
|
176
|
+
process.exitCode = Number(/^exit: (\d+)(?:\n|$)/.exec(text)?.[1] ?? 0);
|
|
177
|
+
}, (error) => {
|
|
178
|
+
process.stderr.write(`house: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
98
179
|
process.exitCode = 1;
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
(argv[0] === 'git' ? gitPush(argv.slice(1)) : house(argv)).then((text) => {
|
|
102
|
-
process.stdout.write(`${text}\n`);
|
|
103
|
-
}, (error) => {
|
|
104
|
-
process.stderr.write(`house: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
105
|
-
process.exitCode = 1;
|
|
106
|
-
});
|
|
107
|
-
}
|
|
180
|
+
});
|
package/dist/instructions.js
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
1
|
-
import { readEnrolment } from './home.js';
|
|
2
1
|
import { HOW_WE_WORK } from './skills.js';
|
|
3
2
|
const HOUSE_LINE = 'Work with House through the `house` CLI: run `house --help`.';
|
|
4
|
-
const FILE_LINE = 'To give the User a file, upload it with `house
|
|
5
|
-
const
|
|
3
|
+
const FILE_LINE = 'To give the User a file or a page, upload it with `house upload` and link it; never start a server.';
|
|
4
|
+
const READ_LINE = 'Before working in a Room, read `/rooms/private/rooms/<handle>/AGENTS.md`.';
|
|
5
|
+
const COPIES_LINE = 'House Local copies; commit, then `house push`:';
|
|
6
6
|
const UNREADABLE = /^(?:\S+: )?(?:path_not_found|room_not_found|operation_denied)\b/;
|
|
7
|
+
const FAILED = /^exit: [1-9]\d*$/;
|
|
7
8
|
const CUT = /^stderr: shell: output_cut (\d+) of \d+ bytes; continue with: (.+)$/;
|
|
9
|
+
function cutOf(lines) {
|
|
10
|
+
const cut = CUT.exec(lines[0] ?? '');
|
|
11
|
+
return cut !== null && Buffer.byteLength(lines.slice(1).join('\n')) === Number(cut[1]) ? cut : null;
|
|
12
|
+
}
|
|
8
13
|
async function howWeWork(bridge) {
|
|
9
14
|
let document = '';
|
|
10
15
|
for (let command = `cat ${HOW_WE_WORK}`; command !== undefined;) {
|
|
11
16
|
const read = await bridge.tool('shell', { command });
|
|
12
17
|
const text = read.content.map((content) => content.text).join('\n');
|
|
13
18
|
const lines = text.split('\n');
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const refusal = read.isError === true ? text : lines.slice(at).join('\n').replace(/^stderr: /gm, '');
|
|
19
|
+
if (read.isError === true || (FAILED.test(lines[0] ?? '') && lines[1]?.startsWith('stderr: '))) {
|
|
20
|
+
const refusal = read.isError === true ? text : lines.slice(1).join('\n').replace(/^stderr: /gm, '');
|
|
17
21
|
if (UNREADABLE.test(refusal))
|
|
18
22
|
return '';
|
|
19
23
|
throw new Error(refusal);
|
|
20
24
|
}
|
|
21
|
-
const cut =
|
|
22
|
-
|
|
23
|
-
document += (cut === null ? shown : shown.subarray(0, Number(cut[1]))).toString();
|
|
25
|
+
const cut = cutOf(lines);
|
|
26
|
+
document += lines.slice(cut === null ? 0 : 1).join('\n');
|
|
24
27
|
command = cut?.[2];
|
|
25
28
|
}
|
|
26
29
|
return document.replace(/\n$/, '');
|
|
27
30
|
}
|
|
28
31
|
export async function instructions(bridge, base, copies) {
|
|
29
|
-
const
|
|
30
|
-
const appLine = `To show the User a page, upload it or push it to an App instead of starting a server; \`git clone ${app}\` starts a new App.`;
|
|
31
|
-
const lines = [HOUSE_LINE, FILE_LINE, appLine, ...(copies.length === 0 ? [] : [COPIES_LINE, ...copies])];
|
|
32
|
+
const lines = [HOUSE_LINE, FILE_LINE, READ_LINE, ...(copies.length === 0 ? [] : [COPIES_LINE, ...copies])];
|
|
32
33
|
return [lines.join('\n'), base, await howWeWork(bridge)].filter((part) => part !== '').join('\n\n');
|
|
33
34
|
}
|
package/dist/kit-main.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { setGlobalProxyFromEnv } from 'node:http';
|
|
3
|
+
import { answerGitCredential } from './app-git.js';
|
|
3
4
|
import { login } from './login.js';
|
|
5
|
+
import { logout } from './logout.js';
|
|
4
6
|
import { resident } from './resident.js';
|
|
5
7
|
const [command, ...argv] = process.argv.slice(2);
|
|
6
8
|
setGlobalProxyFromEnv();
|
|
7
9
|
async function run() {
|
|
8
10
|
if (command === 'login')
|
|
9
11
|
return login(argv);
|
|
12
|
+
if (command === 'logout')
|
|
13
|
+
return logout();
|
|
10
14
|
if (command === 'resident')
|
|
11
15
|
return resident();
|
|
12
|
-
|
|
16
|
+
if (command === 'git-credential')
|
|
17
|
+
return answerGitCredential(argv[0]);
|
|
18
|
+
process.stderr.write('usage: kit <login|logout|resident>\n');
|
|
13
19
|
process.exitCode = 2;
|
|
14
20
|
}
|
|
15
21
|
run().catch((error) => {
|
package/dist/login.js
CHANGED
|
@@ -1,64 +1,27 @@
|
|
|
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';
|
|
7
|
-
import {
|
|
5
|
+
import { post, retrying } from './api.js';
|
|
6
|
+
import { placeAppGit } from './app-git.js';
|
|
7
|
+
import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
|
|
8
|
+
import { callHouse, rpc } from './mcp.js';
|
|
8
9
|
const DEFAULT_HOUSE = 'https://agents.house';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const server = createServer((request, response) => {
|
|
17
|
-
const received = new URL(request.url ?? '/', 'http://127.0.0.1').searchParams.get('code');
|
|
18
|
-
if (received === null || held !== null) {
|
|
19
|
-
response.writeHead(404).end();
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
held = response;
|
|
23
|
-
deliver(received);
|
|
24
|
-
});
|
|
25
|
-
const close = () => {
|
|
26
|
-
server.close();
|
|
27
|
-
server.closeAllConnections();
|
|
28
|
-
};
|
|
29
|
-
const port = Number(process.env.HOUSE_KIT_LOGIN_PORT ?? 0);
|
|
30
|
-
server.once('error', reject);
|
|
31
|
-
server.listen(port, port === 0 ? '127.0.0.1' : '0.0.0.0', () => {
|
|
32
|
-
const address = server.address();
|
|
33
|
-
if (address === null || typeof address === 'string') {
|
|
34
|
-
reject(new Error('the login callback has no port'));
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
resolve({
|
|
38
|
-
uri: `http://127.0.0.1:${address.port}/callback`,
|
|
39
|
-
code,
|
|
40
|
-
answer: (text) => new Promise((written) => {
|
|
41
|
-
if (held === null || held.destroyed || !held.socket || held.socket.destroyed)
|
|
42
|
-
return written();
|
|
43
|
-
held.once('close', () => written());
|
|
44
|
-
held.writeHead(200, { 'content-type': 'text/plain; charset=utf-8', connection: 'close' }).end(`${text}\n`);
|
|
45
|
-
}),
|
|
46
|
-
close,
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
10
|
+
const OPENER = process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
11
|
+
// Polling every two seconds brings the User's confirmation to the waiting Kit moments after they give it.
|
|
12
|
+
const POLL_INTERVAL_MS = 2000;
|
|
13
|
+
function open(link) {
|
|
14
|
+
spawn(OPENER, [link], { detached: true, stdio: 'ignore' })
|
|
15
|
+
.on('error', () => undefined)
|
|
16
|
+
.unref();
|
|
50
17
|
}
|
|
51
|
-
async function
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
finally {
|
|
60
|
-
reader.close();
|
|
61
|
-
}
|
|
18
|
+
async function accepted(house, credential) {
|
|
19
|
+
const checked = await callHouse(house, credential, rpc('tools/list', {}));
|
|
20
|
+
if (checked.status === 200)
|
|
21
|
+
return true;
|
|
22
|
+
if (checked.status === 401)
|
|
23
|
+
return false;
|
|
24
|
+
throw new Error(`House answered ${checked.status} when Kit checked this computer's own agent's connection: ${checked.text}`);
|
|
62
25
|
}
|
|
63
26
|
export async function login(argv) {
|
|
64
27
|
const { values } = parseArgs({
|
|
@@ -66,51 +29,33 @@ export async function login(argv) {
|
|
|
66
29
|
options: {
|
|
67
30
|
house: { type: 'string' },
|
|
68
31
|
environment: { type: 'string' },
|
|
69
|
-
manual: { type: 'boolean', default: false },
|
|
70
32
|
},
|
|
71
33
|
});
|
|
72
|
-
const enrolled = await readEnrolment();
|
|
34
|
+
const enrolled = (await readEnrolment()) ?? (await readDisconnected());
|
|
73
35
|
const house = values.house ?? enrolled?.house ?? DEFAULT_HOUSE;
|
|
74
|
-
const
|
|
36
|
+
const replaces = process.env.HOUSE_KIT_REPLACES;
|
|
37
|
+
const environment = replaces === undefined ? (values.environment ?? (enrolled?.house === house ? enrolled.environment : undefined)) : undefined;
|
|
38
|
+
const ownAgent = await readOwnAgent();
|
|
39
|
+
const held = ownAgent !== null && ownAgent.house === house && (await accepted(house, ownAgent.credential));
|
|
75
40
|
const verifier = randomBytes(32).toString('base64url');
|
|
76
|
-
const started = {
|
|
41
|
+
const started = await retrying(() => post(house, '/kit', {
|
|
77
42
|
...(environment === undefined
|
|
78
|
-
? { act: 'new_environment', label: hostname() }
|
|
43
|
+
? { act: 'new_environment', label: hostname(), ...(replaces === undefined ? {} : { replaces }) }
|
|
79
44
|
: { act: 'existing_environment', environment_id: environment }),
|
|
80
45
|
code_challenge: createHash('sha256').update(verifier).digest('base64url'),
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
presentation: 'manual',
|
|
91
|
-
});
|
|
92
|
-
process.stdout.write(`Open this link and confirm: ${start_url}\n`);
|
|
93
|
-
await exchange(await typed('Code: '));
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
const callback = await listen();
|
|
97
|
-
try {
|
|
98
|
-
const { start_url } = await post(house, '/kit', {
|
|
99
|
-
...started,
|
|
100
|
-
presentation: 'loopback',
|
|
101
|
-
loopback_uri: callback.uri,
|
|
102
|
-
});
|
|
103
|
-
process.stdout.write(`Open this link and confirm: ${start_url}\n`);
|
|
104
|
-
try {
|
|
105
|
-
await exchange(await callback.code);
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
await callback.answer(`This Environment was not connected: ${error.message}`);
|
|
109
|
-
throw error;
|
|
110
|
-
}
|
|
111
|
-
await callback.answer('This Environment is connected. You can close this tab.');
|
|
46
|
+
...(held ? {} : { headless: true }),
|
|
47
|
+
}));
|
|
48
|
+
process.stdout.write(`Open this link and confirm: ${started.start_url}\n`);
|
|
49
|
+
open(started.start_url);
|
|
50
|
+
const exchanged = await retrying(() => post(house, '/kit/token', { login: started.login, code_verifier: verifier }), undefined, () => POLL_INTERVAL_MS);
|
|
51
|
+
await writeEnrolment({ house, environment: exchanged.environment, credential: exchanged.credential });
|
|
52
|
+
process.stdout.write(`Environment ${exchanged.environment} is connected to ${house}.\n`);
|
|
53
|
+
if (exchanged.headless_credential !== undefined) {
|
|
54
|
+
await writeOwnAgent({ house, user: exchanged.user, credential: exchanged.headless_credential });
|
|
112
55
|
}
|
|
113
|
-
|
|
114
|
-
|
|
56
|
+
else if (held && ownAgent.user !== exchanged.user) {
|
|
57
|
+
await forgetOwnAgent();
|
|
58
|
+
process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
|
|
115
59
|
}
|
|
60
|
+
await placeAppGit(ownAgent, await readOwnAgent());
|
|
116
61
|
}
|
package/dist/logout.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { HouseRefusal, post } from './api.js';
|
|
2
|
+
import { forgetAppGit } from './app-git.js';
|
|
3
|
+
import { forgetEnrolment, readEnrolment } from './home.js';
|
|
4
|
+
export async function logout() {
|
|
5
|
+
const enrolled = await readEnrolment();
|
|
6
|
+
if (enrolled === null) {
|
|
7
|
+
await forgetAppGit();
|
|
8
|
+
process.stdout.write('This computer is not connected to House.\n');
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
await post(enrolled.house, '/kit/logout', {}, enrolled.credential);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (!(error instanceof HouseRefusal && error.status === 401))
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
await forgetEnrolment(enrolled);
|
|
19
|
+
await forgetAppGit();
|
|
20
|
+
process.stdout.write(`Environment ${enrolled.environment} is disconnected; kit login reconnects it.\n`);
|
|
21
|
+
}
|
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { KIT_VERSION } from './clis.js';
|
|
3
|
+
import { relayed } from './relay.js';
|
|
4
|
+
const MCP_PROTOCOL_VERSION = '2026-07-28';
|
|
5
|
+
export const UNREACHABLE = 'House is unreachable; call again in a minute';
|
|
6
|
+
export function rpc(method, params) {
|
|
7
|
+
return { jsonrpc: '2.0', id: randomUUID(), method, params };
|
|
8
|
+
}
|
|
9
|
+
export function mcpBody(message, operation) {
|
|
10
|
+
return JSON.stringify({
|
|
11
|
+
...message,
|
|
12
|
+
params: {
|
|
13
|
+
...message.params,
|
|
14
|
+
_meta: {
|
|
15
|
+
'io.modelcontextprotocol/protocolVersion': MCP_PROTOCOL_VERSION,
|
|
16
|
+
'io.modelcontextprotocol/clientInfo': { name: '@agentshouse/kit', version: KIT_VERSION },
|
|
17
|
+
'io.modelcontextprotocol/clientCapabilities': {},
|
|
18
|
+
...(operation === null ? {} : { 'agents.house/agent-operation': operation }),
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export async function callHouse(origin, credential, message, operation = null, signal) {
|
|
24
|
+
const tool = message.method === 'tools/call' ? String(message.params.name) : null;
|
|
25
|
+
const answer = await fetch(relayed(new URL('/', origin)), {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
headers: {
|
|
28
|
+
'content-type': 'application/json',
|
|
29
|
+
authorization: `Bearer ${credential}`,
|
|
30
|
+
accept: 'application/json, text/event-stream',
|
|
31
|
+
'mcp-protocol-version': MCP_PROTOCOL_VERSION,
|
|
32
|
+
'mcp-method': message.method,
|
|
33
|
+
...(tool === null ? {} : { 'mcp-name': tool }),
|
|
34
|
+
},
|
|
35
|
+
body: mcpBody(message, operation),
|
|
36
|
+
signal,
|
|
37
|
+
});
|
|
38
|
+
return { status: answer.status, text: await answer.text() };
|
|
39
|
+
}
|
package/dist/operation.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
|
+
export function uuidOf(bytes, version) {
|
|
3
|
+
bytes[6] = (version << 4) | (bytes[6] & 0x0f);
|
|
4
|
+
bytes[8] = 0x80 | (bytes[8] & 0x3f);
|
|
5
|
+
const hex = bytes.toString('hex', 0, 16);
|
|
6
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
7
|
+
}
|
|
2
8
|
export function operationId() {
|
|
3
9
|
const bytes = randomBytes(16);
|
|
4
10
|
bytes.writeUIntBE(Date.now(), 0, 6);
|
|
5
|
-
|
|
6
|
-
bytes[8] = 0x80 | (bytes[8] & 0x3f);
|
|
7
|
-
const hex = bytes.toString('hex');
|
|
8
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
11
|
+
return uuidOf(bytes, 7);
|
|
9
12
|
}
|
package/dist/parts.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export class Parts {
|
|
2
|
+
parts = [];
|
|
3
|
+
context = null;
|
|
4
|
+
emit;
|
|
5
|
+
writing = null;
|
|
6
|
+
planned = null;
|
|
7
|
+
constructor(emit) {
|
|
8
|
+
this.emit = emit;
|
|
9
|
+
}
|
|
10
|
+
write(type, id, text) {
|
|
11
|
+
if (text === '')
|
|
12
|
+
return;
|
|
13
|
+
const last = this.parts.length - 1;
|
|
14
|
+
const part = this.parts[last];
|
|
15
|
+
if (this.writing?.type === type && this.writing.id === id && part?.type === type) {
|
|
16
|
+
part.text += text;
|
|
17
|
+
this.emit({ op: 'append', index: last, text });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.start({ type, text });
|
|
21
|
+
this.writing = { type, id };
|
|
22
|
+
}
|
|
23
|
+
command() {
|
|
24
|
+
const last = this.parts.length - 1;
|
|
25
|
+
const part = this.parts[last];
|
|
26
|
+
if (part?.type !== 'commands') {
|
|
27
|
+
this.start({ type: 'commands', count: 1 });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
part.count++;
|
|
31
|
+
this.emit({ op: 'count', index: last, count: part.count });
|
|
32
|
+
}
|
|
33
|
+
start(part) {
|
|
34
|
+
this.writing = null;
|
|
35
|
+
this.parts.push(part);
|
|
36
|
+
const index = this.parts.length - 1;
|
|
37
|
+
this.emit({ op: 'start', index, part });
|
|
38
|
+
return index;
|
|
39
|
+
}
|
|
40
|
+
done(index, result) {
|
|
41
|
+
const part = this.parts[index];
|
|
42
|
+
part.status = 'done';
|
|
43
|
+
part.result = result;
|
|
44
|
+
this.emit({ op: 'status', index, status: 'done', result });
|
|
45
|
+
}
|
|
46
|
+
plan(entries) {
|
|
47
|
+
if (this.planned === null) {
|
|
48
|
+
this.planned = this.start({ type: 'plan', entries });
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.parts[this.planned].entries = entries;
|
|
52
|
+
this.emit({ op: 'plan', index: this.planned, entries });
|
|
53
|
+
}
|
|
54
|
+
measure(context) {
|
|
55
|
+
this.context = context;
|
|
56
|
+
this.emit({ op: 'context', ...context });
|
|
57
|
+
}
|
|
58
|
+
operations() {
|
|
59
|
+
return [
|
|
60
|
+
...this.parts.map((part, index) => ({ op: 'start', index, part })),
|
|
61
|
+
...(this.context === null ? [] : [{ op: 'context', ...this.context }]),
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
}
|
package/dist/refusals.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { parse } from 'yaml';
|
|
2
|
+
const HEAD = /^([a-z][a-z0-9_]*): \S/;
|
|
3
|
+
function conflictCodes(detail) {
|
|
4
|
+
let parsed;
|
|
5
|
+
try {
|
|
6
|
+
parsed = parse(detail);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
const conflicts = parsed?.conflicts;
|
|
12
|
+
if (!Array.isArray(conflicts))
|
|
13
|
+
return [];
|
|
14
|
+
return conflicts.flatMap((conflict) => typeof conflict?.code === 'string' ? [conflict.code] : []);
|
|
15
|
+
}
|
|
16
|
+
function spoken(text) {
|
|
17
|
+
const head = HEAD.exec(text);
|
|
18
|
+
if (head === null)
|
|
19
|
+
return null;
|
|
20
|
+
const said = text.replace(/\n+$/, '');
|
|
21
|
+
const detail = said.indexOf('\n');
|
|
22
|
+
return { code: head[1], text: said, conflicts: detail === -1 ? [] : conflictCodes(said.slice(detail + 1)) };
|
|
23
|
+
}
|
|
24
|
+
export function retryable(text) {
|
|
25
|
+
let body;
|
|
26
|
+
try {
|
|
27
|
+
body = JSON.parse(text);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const error = body?.error;
|
|
33
|
+
const verdict = error?.retryable ?? error?.data?.retryable;
|
|
34
|
+
return typeof verdict === 'boolean' ? verdict : null;
|
|
35
|
+
}
|
|
36
|
+
export function refusalOf(text) {
|
|
37
|
+
let body;
|
|
38
|
+
try {
|
|
39
|
+
body = JSON.parse(text);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return spoken(text);
|
|
43
|
+
}
|
|
44
|
+
const error = body?.error;
|
|
45
|
+
if (typeof error !== 'object' || error === null)
|
|
46
|
+
return null;
|
|
47
|
+
const { code, message, data } = error;
|
|
48
|
+
const said = typeof message === 'string' ? spoken(message) : null;
|
|
49
|
+
if (said !== null)
|
|
50
|
+
return said;
|
|
51
|
+
const named = typeof code === 'string' ? code : typeof data?.code === 'string' ? data.code : null;
|
|
52
|
+
return named === null ? null : { code: named, text: named, conflicts: [] };
|
|
53
|
+
}
|
package/dist/relay.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function relayed(target) {
|
|
2
|
+
const url = new URL(target);
|
|
3
|
+
const relay = process.env.HOUSE_KIT_RELAY;
|
|
4
|
+
if (relay === undefined || relay === '')
|
|
5
|
+
return url;
|
|
6
|
+
const through = new URL(`${url.host}${url.pathname}${url.search}`, `${relay}/`);
|
|
7
|
+
if (url.protocol === 'wss:' || url.protocol === 'ws:')
|
|
8
|
+
through.protocol = 'wss:';
|
|
9
|
+
return through;
|
|
10
|
+
}
|