@agentshouse/kit 0.2.1-ccb257fa8a318 → 0.2.1-ccffb3f637815
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 +27 -31
- package/dist/acp.js +17 -10
- package/dist/afk.js +17 -0
- package/dist/agents.js +121 -52
- package/dist/api.js +11 -6
- package/dist/binaries.js +89 -0
- package/dist/bridge.js +28 -27
- package/dist/building.js +7 -3
- package/dist/calls.js +18 -1
- package/dist/chunks.js +49 -0
- package/dist/clis.js +52 -63
- package/dist/connect.js +108 -0
- package/dist/connection.js +24 -20
- package/dist/content.js +106 -0
- package/dist/conversations.js +255 -134
- package/dist/copies.js +748 -248
- package/dist/earlier.js +263 -0
- package/dist/end.js +9 -6
- package/dist/events.js +549 -281
- package/dist/files.js +77 -11
- package/dist/git-main.js +24 -29
- package/dist/git.js +87 -12
- package/dist/grok.js +39 -0
- package/dist/home.js +80 -29
- package/dist/house-cli.js +106 -123
- package/dist/instructions.js +2 -4
- package/dist/keys.js +238 -0
- package/dist/kit-cli.js +14 -4
- package/dist/login.js +34 -20
- package/dist/logout.js +2 -2
- package/dist/mcp.js +38 -3
- package/dist/old-references.js +27 -0
- package/dist/owner.js +46 -0
- package/dist/placement.js +9 -23
- package/dist/refusals.js +37 -21
- package/dist/resident.js +107 -72
- package/dist/scope.js +12 -4
- package/dist/scripts.js +59 -11
- package/dist/secret-input.js +1 -1
- package/dist/settings.js +188 -0
- package/dist/sign-in.js +49 -4
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +5 -2
- package/dist/translate.js +18 -11
- package/dist/update.js +115 -160
- package/dist/wire.js +19 -9
- package/dist/work.js +27 -2
- package/dist/workspace.js +103 -0
- package/package.json +13 -8
- package/dist/authority-main.js +0 -22
- package/dist/chatgpt.js +0 -32
- package/dist/clis-main.js +0 -23
- package/dist/configure-main.js +0 -6
- package/dist/enrol-main.js +0 -10
- package/dist/parts.js +0 -82
- package/dist/service-main.js +0 -9
- package/dist/service.js +0 -55
package/dist/earlier.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
7
|
+
import { KIT_RELEASE } from './clis.js';
|
|
8
|
+
import { restart } from './end.js';
|
|
9
|
+
import { awaitsTakeover, desktopPrefix, earlierHome, earlierPrefix, oneFolder } from './home.js';
|
|
10
|
+
import { defineSupervisor, launchAgent, linger, manage, required, systemd, userUnit } from './supervisor.js';
|
|
11
|
+
import { placeServedRelease } from './update.js';
|
|
12
|
+
import { followRoot, merged, removeFolder } from './workspace.js';
|
|
13
|
+
const SERVER_PREFIX = '/opt/house-kit';
|
|
14
|
+
const SERVER_BIN = '/usr/local/bin';
|
|
15
|
+
const SERVER_UNIT = '/etc/systemd/system/house-kit.service';
|
|
16
|
+
const SERVER_WORKSPACE = '/agents/house';
|
|
17
|
+
const CONTAINER = 'house-kit';
|
|
18
|
+
const CONTAINER_HOME = '/kit-home';
|
|
19
|
+
const CONTAINER_WORKSPACE = '/agents/house';
|
|
20
|
+
const WINDOWS_COPY = '.house-kit-home';
|
|
21
|
+
const WINDOWS_WORKSPACE = 'workspace';
|
|
22
|
+
const PLACEMENT = 'placement';
|
|
23
|
+
const RELEASES = 'kit';
|
|
24
|
+
const WORKSPACE_FOLDERS = ['agents', 'local-copies'];
|
|
25
|
+
const RECORDS = join('local-copies', 'rooms');
|
|
26
|
+
const COMMANDS = ['kit', 'house'];
|
|
27
|
+
const DROPPED_FLAGS = / --(?:linux|container)$/u;
|
|
28
|
+
// A Mac's LaunchAgent is reloaded by a process of its own, which ends this one within moments; a minute bounds a reload that never comes.
|
|
29
|
+
const RELOAD_MS = 60_000;
|
|
30
|
+
function root() {
|
|
31
|
+
return process.getuid() === 0;
|
|
32
|
+
}
|
|
33
|
+
function desktopWorkspace() {
|
|
34
|
+
return join(homedir(), 'AgentsHouse');
|
|
35
|
+
}
|
|
36
|
+
function desktopBin() {
|
|
37
|
+
return join(homedir(), '.local', 'bin');
|
|
38
|
+
}
|
|
39
|
+
function unitValue(text, name) {
|
|
40
|
+
const quoted = new RegExp(`^Environment="${name}=((?:[^"\\\\]|\\\\.)*)"$`, 'mu').exec(text)?.[1];
|
|
41
|
+
return quoted?.replace(/\\(.)/gu, '$1').replaceAll('%%', '%');
|
|
42
|
+
}
|
|
43
|
+
function plistValue(text, name) {
|
|
44
|
+
const value = new RegExp(`<key>${name}</key>\\s*<string>([^<]*)</string>`, 'u').exec(text)?.[1];
|
|
45
|
+
return value?.replaceAll('<', '<').replaceAll('>', '>').replaceAll('&', '&');
|
|
46
|
+
}
|
|
47
|
+
function readText(path) {
|
|
48
|
+
try {
|
|
49
|
+
return readFileSync(path, 'utf8');
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error.code === 'ENOENT')
|
|
53
|
+
return null;
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function desktopKit() {
|
|
58
|
+
const darwin = process.platform === 'darwin';
|
|
59
|
+
const definition = readText(darwin ? launchAgent() : userUnit()) ?? '';
|
|
60
|
+
const value = (name) => (darwin ? plistValue(definition, name) : unitValue(definition, name));
|
|
61
|
+
const named = value('HOUSE_KIT_HOME');
|
|
62
|
+
if (named === undefined && !existsSync(desktopPrefix()))
|
|
63
|
+
return null;
|
|
64
|
+
const workspace = value('HOUSE_KIT_WORKSPACE') ?? desktopWorkspace();
|
|
65
|
+
return { kind: 'desktop', home: named ?? join(homedir(), '.house-kit'), workspace, seen: workspace, unit: named !== undefined };
|
|
66
|
+
}
|
|
67
|
+
function serverKit() {
|
|
68
|
+
const unit = readText(SERVER_UNIT);
|
|
69
|
+
const account = spawnSync('id', ['-un'], { encoding: 'utf8' }).stdout.trim();
|
|
70
|
+
if (unit !== null && !new RegExp(`^User=${account}$`, 'mu').test(unit))
|
|
71
|
+
return null;
|
|
72
|
+
if (unit === null && !(root() && existsSync(SERVER_PREFIX)))
|
|
73
|
+
return null;
|
|
74
|
+
const home = (unit === null ? undefined : unitValue(unit, 'HOUSE_KIT_HOME')) ?? join(homedir(), '.house-kit');
|
|
75
|
+
return { kind: 'server', home, workspace: SERVER_WORKSPACE, seen: SERVER_WORKSPACE, unit: unit !== null };
|
|
76
|
+
}
|
|
77
|
+
function docker(args) {
|
|
78
|
+
return spawnSync('docker', args, { encoding: 'utf8' });
|
|
79
|
+
}
|
|
80
|
+
function containerKit() {
|
|
81
|
+
const inspected = docker(['container', 'inspect', '--format', '{{json .Mounts}}', CONTAINER]);
|
|
82
|
+
if (inspected.error !== undefined)
|
|
83
|
+
return null;
|
|
84
|
+
if (inspected.status !== 0) {
|
|
85
|
+
if (readText(join(homedir(), '.house-kit', 'placement'))?.trim() !== 'container' || docker(['info']).status === 0)
|
|
86
|
+
return null;
|
|
87
|
+
throw new Error(`Kit runs in the container ${CONTAINER} on this computer and Docker does not answer; start Docker, then run this command again`);
|
|
88
|
+
}
|
|
89
|
+
const mounts = JSON.parse(inspected.stdout);
|
|
90
|
+
const source = (destination) => mounts.find((mount) => mount.Destination === destination)?.Source;
|
|
91
|
+
const home = source(CONTAINER_HOME);
|
|
92
|
+
const workspace = source(CONTAINER_WORKSPACE);
|
|
93
|
+
if (home === undefined || workspace === undefined)
|
|
94
|
+
throw new Error(`the container ${CONTAINER} mounts no Kit home or no workspace`);
|
|
95
|
+
return { kind: 'container', home, workspace, seen: CONTAINER_WORKSPACE, unit: false };
|
|
96
|
+
}
|
|
97
|
+
function windowsKit() {
|
|
98
|
+
const home = join(homedir(), WINDOWS_COPY);
|
|
99
|
+
const workspace = readText(join(home, WINDOWS_WORKSPACE))?.trim();
|
|
100
|
+
if (workspace === undefined)
|
|
101
|
+
return null;
|
|
102
|
+
return { kind: 'windows', home, workspace, seen: CONTAINER_WORKSPACE, unit: false };
|
|
103
|
+
}
|
|
104
|
+
function defaulted(earlier) {
|
|
105
|
+
return earlier.kind === 'server' || earlier.workspace === desktopWorkspace();
|
|
106
|
+
}
|
|
107
|
+
async function relocate(earlier) {
|
|
108
|
+
const folder = oneFolder();
|
|
109
|
+
const copied = earlier.kind === 'windows';
|
|
110
|
+
const kept = (copied ? [RELEASES, PLACEMENT, WINDOWS_WORKSPACE] : [RELEASES]).map((name) => join(earlier.home, name));
|
|
111
|
+
await merged(earlier.home, folder, (source) => (kept.includes(source) ? 'stays' : 'moves'));
|
|
112
|
+
if (earlier.kind === 'container')
|
|
113
|
+
await rm(join(earlier.home, RELEASES), { recursive: true, force: true });
|
|
114
|
+
if (!copied)
|
|
115
|
+
await removeFolder(earlier.home);
|
|
116
|
+
const command = join(folder, 'install-command');
|
|
117
|
+
const recorded = await readFile(command, 'utf8').catch(() => null);
|
|
118
|
+
if (recorded !== null && DROPPED_FLAGS.test(recorded.trim()))
|
|
119
|
+
await writeFile(command, `${recorded.trim().replace(DROPPED_FLAGS, '')}\n`);
|
|
120
|
+
const workspace = defaulted(earlier) ? folder : earlier.workspace;
|
|
121
|
+
if (defaulted(earlier)) {
|
|
122
|
+
for (const name of WORKSPACE_FOLDERS)
|
|
123
|
+
await merged(join(earlier.workspace, name), join(folder, name));
|
|
124
|
+
if (earlier.kind !== 'server')
|
|
125
|
+
await removeFolder(earlier.workspace);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const settings = join(folder, 'kit.json');
|
|
129
|
+
const stored = JSON.parse((await readFile(settings, 'utf8').catch(() => null)) ?? '{}');
|
|
130
|
+
if (stored.workspace === undefined) {
|
|
131
|
+
await writeFile(`${settings}.new`, `${JSON.stringify({ ...stored, workspace })}\n`, { mode: 0o600 });
|
|
132
|
+
await rename(`${settings}.new`, settings);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
await followRoot(join(folder, RECORDS), earlier.seen, workspace);
|
|
136
|
+
if (copied)
|
|
137
|
+
await rm(earlier.home, { recursive: true, force: true });
|
|
138
|
+
}
|
|
139
|
+
function elevated(args, failed) {
|
|
140
|
+
if (root())
|
|
141
|
+
required(args[0], args.slice(1), failed);
|
|
142
|
+
else
|
|
143
|
+
required('sudo', args, failed);
|
|
144
|
+
}
|
|
145
|
+
function stopServer(earlier, now) {
|
|
146
|
+
if (!earlier.unit)
|
|
147
|
+
return;
|
|
148
|
+
elevated(['systemctl', 'disable', ...(now ? ['--now'] : []), 'house-kit.service'], 'the earlier Kit service could not be stopped');
|
|
149
|
+
}
|
|
150
|
+
function removeServer(earlier) {
|
|
151
|
+
if (earlier.unit) {
|
|
152
|
+
elevated(['rm', '-f', SERVER_UNIT], `${SERVER_UNIT} could not be removed`);
|
|
153
|
+
elevated(['systemctl', 'daemon-reload'], 'systemd could not forget the earlier Kit service');
|
|
154
|
+
}
|
|
155
|
+
elevated(['rm', '-rf', SERVER_PREFIX, ...COMMANDS.map((command) => join(SERVER_BIN, command))], `${SERVER_PREFIX} could not be removed`);
|
|
156
|
+
const emptied = [SERVER_WORKSPACE, '/agents'].filter((path) => existsSync(path));
|
|
157
|
+
if (emptied.length > 0)
|
|
158
|
+
elevated(['rmdir', '--ignore-fail-on-non-empty', ...emptied], `${SERVER_WORKSPACE} could not be removed`);
|
|
159
|
+
}
|
|
160
|
+
async function removeDesktop() {
|
|
161
|
+
await rm(desktopPrefix(), { recursive: true, force: true });
|
|
162
|
+
for (const command of COMMANDS)
|
|
163
|
+
await rm(join(desktopBin(), command), { force: true });
|
|
164
|
+
}
|
|
165
|
+
function stopDesktop() {
|
|
166
|
+
if (process.platform === 'darwin') {
|
|
167
|
+
manage('launchctl', ['bootout', `gui/${process.getuid()}/house-kit`]);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
manage('systemctl', ['--user', 'disable', '--now', 'house-kit.service']);
|
|
171
|
+
}
|
|
172
|
+
export async function takeOverEarlier() {
|
|
173
|
+
const earlier = windowsKit() ?? containerKit() ?? serverKit() ?? desktopKit();
|
|
174
|
+
if (earlier === null)
|
|
175
|
+
return;
|
|
176
|
+
process.stdout.write(`Moving the Kit installed before into ${oneFolder()}.\n`);
|
|
177
|
+
if (earlier.kind === 'container')
|
|
178
|
+
required('docker', ['rm', '-f', CONTAINER], `the container ${CONTAINER} could not be removed`);
|
|
179
|
+
if (earlier.kind === 'server' && earlier.unit && !root())
|
|
180
|
+
process.stdout.write('Removing the earlier Kit service; sudo may ask for your password.\n');
|
|
181
|
+
if (earlier.kind === 'server')
|
|
182
|
+
stopServer(earlier, true);
|
|
183
|
+
if (earlier.kind === 'desktop')
|
|
184
|
+
stopDesktop();
|
|
185
|
+
await relocate(earlier);
|
|
186
|
+
if (earlier.kind === 'server')
|
|
187
|
+
removeServer(earlier);
|
|
188
|
+
if (earlier.kind === 'desktop')
|
|
189
|
+
await removeDesktop();
|
|
190
|
+
}
|
|
191
|
+
function movable() {
|
|
192
|
+
if (awaitsTakeover())
|
|
193
|
+
return null;
|
|
194
|
+
if (root())
|
|
195
|
+
return existsSync(SERVER_PREFIX) ? serverKit() : null;
|
|
196
|
+
if (!existsSync(desktopPrefix()))
|
|
197
|
+
return null;
|
|
198
|
+
const desktop = desktopKit();
|
|
199
|
+
if (earlierPrefix() === null)
|
|
200
|
+
return desktop;
|
|
201
|
+
return { ...desktop, home: earlierHome(), workspace: process.env.HOUSE_KIT_WORKSPACE ?? desktop.workspace };
|
|
202
|
+
}
|
|
203
|
+
function supervised() {
|
|
204
|
+
return process.platform === 'darwin' || (systemd() && process.env.HOUSE_KIT_SANDBOX !== '1');
|
|
205
|
+
}
|
|
206
|
+
async function ready() {
|
|
207
|
+
try {
|
|
208
|
+
if (supervised() && process.platform !== 'darwin')
|
|
209
|
+
await linger();
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
process.stderr.write(`kit: Kit stays where it was installed and moves at its next start: ${error.message}\n`);
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
const failure = await placeServedRelease(join(oneFolder(), RELEASES));
|
|
216
|
+
if (failure === null)
|
|
217
|
+
return true;
|
|
218
|
+
process.stderr.write(`kit: Kit stays where it was installed and moves at its next start: ${failure}\n`);
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
async function reloadLaunchAgent() {
|
|
222
|
+
const target = `gui/${process.getuid()}`;
|
|
223
|
+
spawn('/bin/sh', ['-c', `launchctl bootout ${target}/house-kit; launchctl bootstrap ${target} "$0"`, launchAgent()], {
|
|
224
|
+
detached: true,
|
|
225
|
+
stdio: 'ignore',
|
|
226
|
+
}).unref();
|
|
227
|
+
await delay(RELOAD_MS);
|
|
228
|
+
restart('the LaunchAgent was not reloaded; its next start runs Kit from its one folder');
|
|
229
|
+
}
|
|
230
|
+
export async function moveEarlier() {
|
|
231
|
+
const earlier = movable();
|
|
232
|
+
if (earlier === null)
|
|
233
|
+
return;
|
|
234
|
+
const within = earlierPrefix() !== null;
|
|
235
|
+
if (within && !(await ready()))
|
|
236
|
+
return;
|
|
237
|
+
process.stderr.write(`kit: moving Kit home ${earlier.home} into ${oneFolder()}\n`);
|
|
238
|
+
await relocate(earlier);
|
|
239
|
+
if (within && supervised())
|
|
240
|
+
await defineSupervisor();
|
|
241
|
+
if (within && supervised() && process.platform !== 'darwin') {
|
|
242
|
+
required('systemctl', ['--user', 'daemon-reload'], 'systemd could not read the Kit service');
|
|
243
|
+
required('systemctl', ['--user', '--quiet', 'reenable', 'house-kit.service'], 'the Kit service could not be enabled');
|
|
244
|
+
}
|
|
245
|
+
if (earlier.kind === 'server') {
|
|
246
|
+
if (within && earlier.unit && supervised())
|
|
247
|
+
required('systemctl', ['--user', '--no-block', 'start', 'house-kit.service'], 'the Kit service could not be started');
|
|
248
|
+
stopServer(earlier, false);
|
|
249
|
+
removeServer(earlier);
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
await removeDesktop();
|
|
253
|
+
}
|
|
254
|
+
if (!within)
|
|
255
|
+
return;
|
|
256
|
+
if (process.platform === 'darwin')
|
|
257
|
+
await reloadLaunchAgent();
|
|
258
|
+
if (earlier.kind === 'server' && earlier.unit) {
|
|
259
|
+
manage('systemctl', ['--no-block', 'stop', 'house-kit.service']);
|
|
260
|
+
await delay(RELOAD_MS);
|
|
261
|
+
}
|
|
262
|
+
restart(`Kit ${KIT_RELEASE} moved into ${oneFolder()}; its supervisor starts it there`);
|
|
263
|
+
}
|
package/dist/end.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { forgetRejected } from './home.js';
|
|
1
|
+
import { forgetRejected, recordPlannedRestart } from './home.js';
|
|
3
2
|
import { installCommand } from './placement.js';
|
|
4
3
|
import { killDescendants } from './scope.js';
|
|
5
4
|
export const REPLACED = 'another Kit process started for this Environment';
|
|
@@ -14,12 +13,16 @@ export function end(reason) {
|
|
|
14
13
|
export function restart(reason) {
|
|
15
14
|
exit(reason, 1);
|
|
16
15
|
}
|
|
17
|
-
export function
|
|
16
|
+
export function restartPlaced(release) {
|
|
17
|
+
recordPlannedRestart();
|
|
18
|
+
restart(`Kit ${release} is placed; its supervisor starts it`);
|
|
19
|
+
}
|
|
20
|
+
export function updateLine(lead) {
|
|
18
21
|
const command = installCommand();
|
|
19
|
-
|
|
22
|
+
return command === null ? `${lead}; install it again.` : `${lead}. Update it: ${command}`;
|
|
20
23
|
}
|
|
21
|
-
export function
|
|
22
|
-
end(
|
|
24
|
+
export function updateRequired() {
|
|
25
|
+
end(updateLine("House does not serve this Kit's wire"));
|
|
23
26
|
}
|
|
24
27
|
export async function signInAgain(rejected) {
|
|
25
28
|
await forgetRejected(rejected);
|