@agentshouse/kit 0.2.1-alpha.9 → 0.2.1-c031c7457a661

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.
Files changed (136) hide show
  1. package/README.md +48 -29
  2. package/dist/abort.js +9 -0
  3. package/dist/acp.js +49 -86
  4. package/dist/agents.js +333 -52
  5. package/dist/api.js +40 -4
  6. package/dist/app-git.js +48 -0
  7. package/dist/bootstrap.json +10 -14
  8. package/dist/bridge.js +93 -78
  9. package/dist/calls.js +63 -0
  10. package/dist/chatgpt.js +32 -0
  11. package/dist/clis-main.js +23 -0
  12. package/dist/clis.js +217 -72
  13. package/dist/codex-main.js +58 -0
  14. package/dist/connection.js +126 -0
  15. package/dist/conversations.js +642 -263
  16. package/dist/copies.js +237 -173
  17. package/dist/end.js +27 -0
  18. package/dist/enrol-main.js +7 -1
  19. package/dist/events.js +525 -0
  20. package/dist/files.js +140 -17
  21. package/dist/git-main.js +2 -2
  22. package/dist/git.js +16 -10
  23. package/dist/home.js +127 -9
  24. package/dist/house-cli.js +300 -0
  25. package/dist/house-main.js +5 -102
  26. package/dist/instructions.js +16 -17
  27. package/dist/kit-cli.js +68 -0
  28. package/dist/kit-main.js +8 -16
  29. package/dist/login.js +128 -95
  30. package/dist/logout.js +19 -0
  31. package/dist/mcp.js +41 -0
  32. package/dist/operation.js +7 -4
  33. package/dist/parts.js +82 -0
  34. package/dist/placement.js +39 -0
  35. package/dist/refusals.js +53 -0
  36. package/dist/relay.js +20 -0
  37. package/dist/resident.js +139 -52
  38. package/dist/scope.js +206 -0
  39. package/dist/secret-input.js +7 -2
  40. package/dist/service-main.js +9 -0
  41. package/dist/service.js +55 -0
  42. package/dist/shell.js +3 -0
  43. package/dist/sign-in.js +50 -31
  44. package/dist/skills.js +20 -37
  45. package/dist/transcription.js +201 -0
  46. package/dist/translate.js +2 -8
  47. package/dist/update.js +138 -0
  48. package/dist/web-wizard.js +100 -0
  49. package/dist/wire.js +22 -0
  50. package/dist/work.js +346 -0
  51. package/package.json +9 -11
  52. package/dist/blocks.js +0 -51
  53. package/dist/stream.js +0 -47
  54. package/how-we-work.md +0 -25
  55. package/skills/ask-matt/LICENSE +0 -21
  56. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  57. package/skills/ask-matt/SKILL.md +0 -91
  58. package/skills/ask-matt/agents/openai.yaml +0 -4
  59. package/skills/code-review/LICENSE +0 -21
  60. package/skills/code-review/SKILL.md +0 -89
  61. package/skills/code-review/agents/openai.yaml +0 -3
  62. package/skills/codebase-design/DEEPENING.md +0 -37
  63. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  64. package/skills/codebase-design/LICENSE +0 -21
  65. package/skills/codebase-design/SKILL.md +0 -116
  66. package/skills/codebase-design/agents/openai.yaml +0 -3
  67. package/skills/diagnosing-bugs/LICENSE +0 -21
  68. package/skills/diagnosing-bugs/SKILL.md +0 -140
  69. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  70. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  71. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  72. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  73. package/skills/domain-modeling/LICENSE +0 -21
  74. package/skills/domain-modeling/SKILL.md +0 -76
  75. package/skills/domain-modeling/agents/openai.yaml +0 -3
  76. package/skills/grill-me/LICENSE +0 -21
  77. package/skills/grill-me/SKILL.md +0 -8
  78. package/skills/grill-me/agents/openai.yaml +0 -4
  79. package/skills/grill-with-docs/LICENSE +0 -21
  80. package/skills/grill-with-docs/SKILL.md +0 -8
  81. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  82. package/skills/grilling/LICENSE +0 -21
  83. package/skills/grilling/SKILL.md +0 -30
  84. package/skills/grilling/agents/openai.yaml +0 -3
  85. package/skills/handoff/LICENSE +0 -21
  86. package/skills/handoff/SKILL.md +0 -17
  87. package/skills/handoff/agents/openai.yaml +0 -4
  88. package/skills/implement/LICENSE +0 -21
  89. package/skills/implement/SKILL.md +0 -16
  90. package/skills/implement/agents/openai.yaml +0 -4
  91. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  92. package/skills/improve-codebase-architecture/LICENSE +0 -21
  93. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  94. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  95. package/skills/prototype/LICENSE +0 -21
  96. package/skills/prototype/LOGIC.md +0 -67
  97. package/skills/prototype/SKILL.md +0 -28
  98. package/skills/prototype/UI.md +0 -112
  99. package/skills/prototype/agents/openai.yaml +0 -3
  100. package/skills/research/LICENSE +0 -21
  101. package/skills/research/SKILL.md +0 -14
  102. package/skills/research/agents/openai.yaml +0 -3
  103. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  104. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  105. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  106. package/skills/tdd/LICENSE +0 -21
  107. package/skills/tdd/SKILL.md +0 -40
  108. package/skills/tdd/agents/openai.yaml +0 -3
  109. package/skills/tdd/mocking.md +0 -59
  110. package/skills/tdd/tests.md +0 -77
  111. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  112. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  113. package/skills/teach/LICENSE +0 -21
  114. package/skills/teach/MISSION-FORMAT.md +0 -31
  115. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  116. package/skills/teach/SKILL.md +0 -141
  117. package/skills/teach/agents/openai.yaml +0 -4
  118. package/skills/to-questionnaire/LICENSE +0 -21
  119. package/skills/to-questionnaire/SKILL.md +0 -55
  120. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  121. package/skills/to-spec/LICENSE +0 -21
  122. package/skills/to-spec/SKILL.md +0 -76
  123. package/skills/to-spec/agents/openai.yaml +0 -4
  124. package/skills/to-tickets/LICENSE +0 -21
  125. package/skills/to-tickets/SKILL.md +0 -106
  126. package/skills/to-tickets/agents/openai.yaml +0 -4
  127. package/skills/wait-what/LICENSE +0 -21
  128. package/skills/wait-what/SKILL.md +0 -8
  129. package/skills/wait-what/agents/openai.yaml +0 -4
  130. package/skills/wayfinder/LICENSE +0 -21
  131. package/skills/wayfinder/SKILL.md +0 -129
  132. package/skills/wayfinder/agents/openai.yaml +0 -4
  133. package/skills/writing-for-agents/LICENSE +0 -21
  134. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  135. package/skills/writing-for-agents/SKILL.md +0 -83
  136. package/skills/writing-for-agents/agents/openai.yaml +0 -3
@@ -0,0 +1,9 @@
1
+ import { startService } from './service.js';
2
+ try {
3
+ if (startService())
4
+ process.stdout.write('started\n');
5
+ }
6
+ catch (error) {
7
+ process.stderr.write(`kit: ${error.message}\n`);
8
+ process.exitCode = 1;
9
+ }
@@ -0,0 +1,55 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { existsSync, realpathSync } from 'node:fs';
3
+ import { resolve } from 'node:path';
4
+ import { kitHome } from './home.js';
5
+ const UNIT = 'house-kit.service';
6
+ // launchctl and systemctl answer at once, so ten seconds only bounds a stuck service manager.
7
+ const MANAGER_MS = 10_000;
8
+ function manage(command, args) {
9
+ return spawnSync(command, args, { encoding: 'utf8', timeout: MANAGER_MS });
10
+ }
11
+ function start(command, args) {
12
+ const started = manage(command, args);
13
+ if (started.status === 0)
14
+ return;
15
+ const cause = started.error?.message ?? (started.stderr.trim() || `${command} exited with ${started.status ?? 'a signal'}`);
16
+ throw new Error(`the Kit service could not be started: ${cause}`);
17
+ }
18
+ function physical(path) {
19
+ return existsSync(path) ? realpathSync(path) : resolve(path);
20
+ }
21
+ function systemdHome(environment) {
22
+ for (const [, quoted, bare] of environment.matchAll(/"((?:[^"\\]|\\.)*)"|(\S+)/g)) {
23
+ const assignment = bare ?? quoted.replace(/\\(.)/g, '$1');
24
+ if (assignment.startsWith('HOUSE_KIT_HOME='))
25
+ return assignment.slice('HOUSE_KIT_HOME='.length);
26
+ }
27
+ return undefined;
28
+ }
29
+ function runsThisHome(served) {
30
+ return served !== undefined && physical(served) === physical(kitHome());
31
+ }
32
+ export function startService() {
33
+ if (process.platform === 'darwin') {
34
+ const service = `gui/${process.getuid()}/house-kit`;
35
+ const printed = manage('launchctl', ['print', service]);
36
+ if (printed.status !== 0 || /^\tstate = running$/m.test(printed.stdout))
37
+ return false;
38
+ if (!runsThisHome(/^\t+HOUSE_KIT_HOME => (.+)$/m.exec(printed.stdout)?.[1]))
39
+ return false;
40
+ start('launchctl', ['kickstart', service]);
41
+ return true;
42
+ }
43
+ for (const scope of [['--user'], []]) {
44
+ if (manage('systemctl', [...scope, 'is-enabled', '--quiet', UNIT]).status !== 0)
45
+ continue;
46
+ const environment = manage('systemctl', [...scope, 'show', '--property=Environment', '--value', UNIT]).stdout;
47
+ if (!runsThisHome(systemdHome(environment)))
48
+ continue;
49
+ if (manage('systemctl', [...scope, 'is-active', '--quiet', UNIT]).status === 0)
50
+ return false;
51
+ start('systemctl', [...scope, 'start', UNIT]);
52
+ return true;
53
+ }
54
+ return false;
55
+ }
package/dist/shell.js ADDED
@@ -0,0 +1,3 @@
1
+ export function quoted(value) {
2
+ return `'${value.replaceAll("'", `'\\''`)}'`;
3
+ }
package/dist/sign-in.js CHANGED
@@ -1,50 +1,66 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { createInterface } from 'node:readline';
3
- import { killTree } from './acp.js';
4
- import { CLIS, cliCommand, withoutProxy } from './clis.js';
3
+ import { CLIS, withoutProxy } from './clis.js';
4
+ import { killTree } from './scope.js';
5
5
  import { holdSecretInput } from './secret-input.js';
6
6
  const ESCAPE = /\u001b\[[\d;]*m/g;
7
7
  const LINK = /https:\/\/\S+/;
8
8
  const CODE = /^[A-Z0-9]+-[A-Z0-9]+$/;
9
- function logged(error) {
10
- process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
9
+ function causeOf(error) {
10
+ return error instanceof Error ? error.message : String(error);
11
+ }
12
+ function endOf(signal) {
13
+ return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
11
14
  }
12
15
  export class SignIns {
13
16
  house;
14
17
  agents;
15
- changed;
16
- running = new Set();
17
- constructor(house, agents, changed) {
18
+ calls;
19
+ constructor(house, agents, calls) {
18
20
  this.house = house;
19
21
  this.agents = agents;
20
- this.changed = changed;
22
+ this.calls = calls;
21
23
  }
22
- idle() {
23
- return this.running.size === 0;
24
+ start(call) {
25
+ void this.calls.carry(call.input_id, () => this.signIn(call));
24
26
  }
25
- start(input) {
26
- if (this.running.has(input.input_id))
27
- return;
28
- this.running.add(input.input_id);
29
- this.changed();
30
- this.login(input)
31
- .then(() => this.agents.report())
32
- .then(() => this.house.deliver(`/kit/inputs/${input.input_id}/ack`, {}))
33
- .catch(logged)
34
- .finally(() => {
35
- this.running.delete(input.input_id);
36
- this.changed();
37
- });
27
+ async signIn(call) {
28
+ const ending = new AbortController();
29
+ // House stops waiting for a sign-in ten minutes after it wrote it and sends the seconds left, so its install and login end with the page.
30
+ const timer = setTimeout(() => ending.abort(), call.ends_in * 1000);
31
+ const ended = await Promise.race([this.signedIn(call, ending.signal), endOf(ending.signal)]);
32
+ clearTimeout(timer);
33
+ if (ended)
34
+ process.stderr.write(`kit: the ${call.cli} sign-in reached the end House set for it\n`);
35
+ return ended ? { result: {} } : { result: {}, returned: () => this.agents.reread(call.cli) };
38
36
  }
39
- async login(input) {
40
- await this.agents.installed();
41
- const login = CLIS[input.cli].login;
42
- const child = spawn(cliCommand(input.cli), login.args, {
37
+ async signedIn(call, signal) {
38
+ await this.login(call, signal);
39
+ if (!signal.aborted)
40
+ await this.agents.recheck(call.cli, signal);
41
+ return false;
42
+ }
43
+ async login(call, signal) {
44
+ let cli;
45
+ try {
46
+ cli = await this.agents.located(call.cli, signal);
47
+ }
48
+ catch (error) {
49
+ if (!signal.aborted)
50
+ process.stderr.write(`kit: the ${call.cli} sign-in failed: ${causeOf(error)}\n`);
51
+ return;
52
+ }
53
+ if (signal.aborted)
54
+ return;
55
+ const login = CLIS[call.cli].login;
56
+ const child = spawn(cli, login.args, {
43
57
  stdio: ['pipe', 'pipe', 'pipe'],
44
58
  detached: true,
45
59
  env: withoutProxy(),
46
60
  });
47
61
  const running = () => child.exitCode === null && child.signalCode === null;
62
+ const stop = () => killTree(child);
63
+ signal.addEventListener('abort', stop, { once: true });
48
64
  let link;
49
65
  let code;
50
66
  let last = '';
@@ -54,6 +70,8 @@ export class SignIns {
54
70
  if (plain === '')
55
71
  return;
56
72
  last = plain;
73
+ if (login.code === 'collect' && plain.includes(login.rejected))
74
+ killTree(child);
57
75
  link ??= LINK.exec(plain)?.[0];
58
76
  if (CODE.test(plain))
59
77
  code ??= plain;
@@ -66,7 +84,7 @@ export class SignIns {
66
84
  return;
67
85
  held = true;
68
86
  const steps = [{ kind: 'visit', label: 'Open this link and sign in', url: link }, step];
69
- void holdSecretInput(this.house, input.input_id, steps, running).then((content) => {
87
+ void holdSecretInput(this.house, call.input_id, steps, running).then((content) => {
70
88
  if (!running())
71
89
  return;
72
90
  if (content === null)
@@ -79,9 +97,10 @@ export class SignIns {
79
97
  createInterface({ input: output }).on('line', read);
80
98
  const failed = await new Promise((resolve) => {
81
99
  child.on('error', (error) => resolve(error.message));
82
- child.on('close', (status, signal) => resolve(status === 0 ? null : `exited with ${status ?? signal}${last ? `: ${last}` : ''}`));
100
+ child.on('close', (status, killed) => resolve(status === 0 ? null : `exited with ${status ?? killed}${last ? `: ${last}` : ''}`));
83
101
  });
84
- if (failed !== null)
85
- process.stderr.write(`kit: the ${input.cli} sign-in failed: ${failed}\n`);
102
+ signal.removeEventListener('abort', stop);
103
+ if (failed !== null && !signal.aborted)
104
+ process.stderr.write(`kit: the ${call.cli} sign-in failed: ${failed}\n`);
86
105
  }
87
106
  }
package/dist/skills.js CHANGED
@@ -1,18 +1,10 @@
1
- import { readFileSync } from 'node:fs';
2
- import { cp, lstat, mkdir, readdir, realpath, rm, symlink, writeFile } from 'node:fs/promises';
1
+ import { cp, lstat, mkdir, realpath, rm, symlink } from 'node:fs/promises';
3
2
  import { homedir } from 'node:os';
4
3
  import { join, relative } from 'node:path';
5
- import { fileURLToPath } from 'node:url';
6
- import { parse } from 'yaml';
7
- import { KIT_VERSION } from './clis.js';
8
- import { refusalCode } from './copies.js';
4
+ import { listSkills } from '@agentshouse/skills';
5
+ import { KIT_RELEASE } from './clis.js';
9
6
  import { kitHome, readConfiguration, readHome, writeHome } from './home.js';
10
- export const HOW_WE_WORK = '/private/library/how-we-work.md';
11
- const SKILL_SET = fileURLToPath(new URL('../skills/', import.meta.url));
12
- const HOW_WE_WORK_TEXT = readFileSync(new URL('../how-we-work.md', import.meta.url), 'utf8');
13
7
  const RECORD = 'skill-set.json';
14
- const CREATED = 'how-we-work-created';
15
- const UNWRITABLE = new Set(['room_not_found', 'operation_denied']);
16
8
  function skillsDirectory() {
17
9
  return join(homedir(), '.agents', 'skills');
18
10
  }
@@ -31,28 +23,37 @@ async function present(path) {
31
23
  }
32
24
  }
33
25
  async function install() {
34
- const names = [];
35
- for (const name of (await readdir(SKILL_SET)).sort()) {
36
- if (!(await present(join(skillsDirectory(), name))) && !(await present(join(claudeDirectory(), name)))) {
37
- names.push(name);
26
+ const placed = [];
27
+ for (const skill of listSkills()) {
28
+ if (!(await present(join(skillsDirectory(), skill.name))) && !(await present(join(claudeDirectory(), skill.name)))) {
29
+ placed.push(skill);
38
30
  }
39
31
  }
32
+ const names = placed.map((skill) => skill.name);
40
33
  await writeHome(RECORD, { names });
41
34
  await mkdir(skillsDirectory(), { recursive: true });
42
35
  await mkdir(claudeDirectory(), { recursive: true });
43
36
  const skills = await realpath(skillsDirectory());
44
37
  const claude = await realpath(claudeDirectory());
45
- for (const name of names) {
46
- await cp(join(SKILL_SET, name), join(skillsDirectory(), name), { recursive: true });
38
+ for (const { name, directory } of placed) {
39
+ await cp(directory, join(skillsDirectory(), name), { recursive: true });
47
40
  if (claude !== skills)
48
41
  await symlink(relative(claude, join(skills, name)), join(claudeDirectory(), name));
49
42
  }
50
- await writeHome(RECORD, { version: KIT_VERSION, names });
43
+ await writeHome(RECORD, { version: KIT_RELEASE, names });
51
44
  }
52
45
  export async function placeSkillSet() {
46
+ try {
47
+ await replaceSkillSet();
48
+ }
49
+ catch (error) {
50
+ process.stderr.write(`kit: the skills could not be placed: ${error.message}\n`);
51
+ }
52
+ }
53
+ async function replaceSkillSet() {
53
54
  const accepted = (await readConfiguration()).skills;
54
55
  const installed = await readHome(RECORD);
55
- if (accepted && installed?.version === KIT_VERSION)
56
+ if (accepted && installed?.version === KIT_RELEASE)
56
57
  return;
57
58
  for (const name of installed?.names ?? []) {
58
59
  await rm(join(claudeDirectory(), name), { recursive: true, force: true });
@@ -63,21 +64,3 @@ export async function placeSkillSet() {
63
64
  else
64
65
  await rm(join(kitHome(), RECORD), { force: true });
65
66
  }
66
- function documentPresent(refusal) {
67
- const { conflicts } = parse(refusal.slice(refusal.indexOf('\n') + 1));
68
- return conflicts.some((conflict) => conflict.code === 'path_exists');
69
- }
70
- export async function createHowWeWork(bridge) {
71
- if ((await readHome(RECORD)) === null || (await present(join(kitHome(), CREATED))))
72
- return;
73
- const created = await bridge.tool('edit', { changes: [{ op: 'create', path: HOW_WE_WORK, content: HOW_WE_WORK_TEXT }] });
74
- if (created.isError === true) {
75
- const text = created.content.map((content) => content.text).join('\n');
76
- const code = refusalCode(text);
77
- if (UNWRITABLE.has(code))
78
- return;
79
- if (code !== 'edit_conflict' || !documentPresent(text))
80
- throw new Error(text);
81
- }
82
- await writeFile(join(kitHome(), CREATED), '');
83
- }
@@ -0,0 +1,201 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { createReadStream } from 'node:fs';
3
+ import { chmod, mkdir, mkdtemp, readdir, readFile, rename, rm, stat } from 'node:fs/promises';
4
+ import { availableParallelism, tmpdir } from 'node:os';
5
+ import { basename, join } from 'node:path';
6
+ import { pipeline } from 'node:stream/promises';
7
+ import { unlessAborted } from './abort.js';
8
+ import { failureOf, run } from './clis.js';
9
+ import { downloaded, fetched } from './files.js';
10
+ import { kitHome } from './home.js';
11
+ const RELEASE = 'https://github.com/agentshouse/kit/releases/download/engine-2';
12
+ export const MODEL = {
13
+ url: 'https://huggingface.co/ggml-org/parakeet-GGUF/resolve/35156454d1a39de06863303dd209fd2bed6ee079/ggml-parakeet-tdt-0.6b-v3-q4_k.bin',
14
+ sha256: '8b205b8b39c6535e153de6fb11c51db46125d45c4f16ba496fe41a0fe71b885e',
15
+ executable: false,
16
+ };
17
+ export const ENGINES = {
18
+ 'linux-x64': {
19
+ decoder: { url: `${RELEASE}/ffmpeg-linux-x86_64`, sha256: '1f8235d206428305afb89c6e8ab99d100425a0ba61e22bb4c7aff017db8c8de5', executable: true },
20
+ engine: { url: `${RELEASE}/parakeet-cli-linux-x86_64`, sha256: 'bb2585695992fe1927a33960002cdf51b8a90a96ad65593c3d0a9f1fbc0865c2', executable: true },
21
+ },
22
+ 'linux-arm64': {
23
+ decoder: { url: `${RELEASE}/ffmpeg-linux-aarch64`, sha256: 'a7e08a0fae0179d1078312f6bb13d2a9aa4d4c71a1d82a022953f25dee452f5e', executable: true },
24
+ engine: { url: `${RELEASE}/parakeet-cli-linux-aarch64`, sha256: 'cbc0bedc8daae97f1ef9dcee4200c06226218e300e0fd62ead1306d22742b7ed', executable: true },
25
+ },
26
+ 'darwin-arm64': {
27
+ decoder: { url: `${RELEASE}/ffmpeg-darwin-arm64`, sha256: '7abe154aaed3d6b49f5167ca7221bb66bed51388925d99a7e4261a41fe52fba6', executable: true },
28
+ engine: { url: `${RELEASE}/parakeet-cli-darwin-arm64`, sha256: '9a2aafc0922ad7c0b6db6eff1641e96572ba32ac40bf995a1df242ed2febf297', executable: true },
29
+ },
30
+ };
31
+ const CHUNK_SECONDS = 20;
32
+ const OFF = "transcription is off on the Agent's Environment, so this message's audio has no text; turn it on or write instead";
33
+ const NOT_INSTALLED = "the speech engine is not installed on the Agent's Environment, so this message's audio has no text";
34
+ const NOT_DOWNLOADED = 'an audio file this message links could not be downloaded; send the message again';
35
+ const UNDECODABLE = 'an audio file this message links could not be decoded';
36
+ const UNTRANSCRIBED = 'an audio file this message links could not be transcribed';
37
+ let held = Promise.resolve();
38
+ let holding = Promise.resolve();
39
+ let setting = null;
40
+ let downloading = null;
41
+ let readiness = { installed: false, failure: null };
42
+ function directory() {
43
+ return join(kitHome(), 'transcription');
44
+ }
45
+ function installed(artifact) {
46
+ return join(directory(), artifact.sha256);
47
+ }
48
+ async function executed(command, args, signal) {
49
+ // A download or a transcription runs as long as its size needs; the setting turning off or the turn's interrupt or end aborts it instead.
50
+ const ran = await run(command, args, 0, false, signal);
51
+ if (ran.status !== 0)
52
+ throw new Error(failureOf(ran, basename(command)));
53
+ return ran.stdout;
54
+ }
55
+ function present(path) {
56
+ return stat(path).then(() => true, () => false);
57
+ }
58
+ async function threads() {
59
+ const [quota, period] = (await readFile('/sys/fs/cgroup/cpu.max', 'utf8').catch(() => 'max')).split(' ');
60
+ const allowed = quota === 'max' ? Infinity : Math.ceil(Number(quota) / Number(period));
61
+ return Math.max(1, Math.floor(Math.min(availableParallelism(), allowed) / 2));
62
+ }
63
+ async function sha256(path) {
64
+ const hash = createHash('sha256');
65
+ await pipeline(createReadStream(path), hash);
66
+ return hash.digest('hex');
67
+ }
68
+ function causeOf(error) {
69
+ return error instanceof Error ? error.message : String(error);
70
+ }
71
+ async function failing(reason, work) {
72
+ try {
73
+ return await work;
74
+ }
75
+ catch (error) {
76
+ throw new Error(reason, { cause: error });
77
+ }
78
+ }
79
+ function ready() {
80
+ if (setting !== true)
81
+ throw new Error(OFF);
82
+ if (!readiness.installed)
83
+ throw new Error(NOT_INSTALLED);
84
+ }
85
+ function pinnedHere() {
86
+ return ENGINES[`${process.platform}-${process.arch}`];
87
+ }
88
+ async function install(artifacts, signal) {
89
+ await mkdir(directory(), { recursive: true });
90
+ const pinned = new Set(artifacts.map((artifact) => artifact.sha256));
91
+ for (const name of await readdir(directory())) {
92
+ if (!pinned.has(name))
93
+ await rm(join(directory(), name), { recursive: true, force: true });
94
+ }
95
+ const missing = [];
96
+ for (const artifact of artifacts)
97
+ if (!(await present(installed(artifact))))
98
+ missing.push(artifact);
99
+ try {
100
+ for (const artifact of missing) {
101
+ const part = `${installed(artifact)}.part`;
102
+ // Three retries carry a large download past a dropped connection before the download fails.
103
+ await executed('curl', ['-fsSL', '--retry', '3', '-o', part, artifact.url], signal);
104
+ if ((await sha256(part)) !== artifact.sha256)
105
+ throw new Error(`${artifact.url} does not match its sha256`);
106
+ }
107
+ for (const artifact of missing) {
108
+ if (artifact.executable)
109
+ await chmod(`${installed(artifact)}.part`, 0o755);
110
+ await rename(`${installed(artifact)}.part`, installed(artifact));
111
+ }
112
+ }
113
+ finally {
114
+ for (const artifact of missing)
115
+ await rm(`${installed(artifact)}.part`, { force: true });
116
+ }
117
+ }
118
+ function reported(next) {
119
+ const changed = next.installed !== readiness.installed || next.failure !== readiness.failure;
120
+ readiness = next;
121
+ return changed;
122
+ }
123
+ async function settle(transcribe) {
124
+ if (transcribe === setting)
125
+ return false;
126
+ setting = transcribe;
127
+ if (!transcribe) {
128
+ await rm(directory(), { recursive: true, force: true });
129
+ return reported({ installed: false, failure: null });
130
+ }
131
+ const download = new AbortController();
132
+ downloading = download;
133
+ try {
134
+ const pinned = pinnedHere();
135
+ await install([pinned.decoder, pinned.engine, MODEL], download.signal);
136
+ return reported({ installed: true, failure: null });
137
+ }
138
+ catch (error) {
139
+ await rm(directory(), { recursive: true, force: true });
140
+ return reported({ installed: false, failure: download.signal.aborted ? null : causeOf(error) });
141
+ }
142
+ finally {
143
+ downloading = null;
144
+ }
145
+ }
146
+ export function holdTranscription(transcribe) {
147
+ if (!transcribe)
148
+ downloading?.abort();
149
+ const settled = holding.then(() => settle(transcribe));
150
+ holding = settled.catch(() => undefined);
151
+ return settled;
152
+ }
153
+ export function transcriptionReport() {
154
+ return readiness;
155
+ }
156
+ async function transcribed(audio, parts, signal) {
157
+ const pinned = pinnedHere();
158
+ await mkdir(parts);
159
+ await failing(UNDECODABLE, executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', audio, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
160
+ '-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(parts, '%05d.wav')], signal));
161
+ const segments = (await readdir(parts)).sort().flatMap((part) => ['-f', join(parts, part)]);
162
+ const said = await failing(UNTRANSCRIBED, executed(installed(pinned.engine), ['-t', String(await threads()), '-np', '-m', installed(MODEL), ...segments], signal));
163
+ const text = said
164
+ .split('\n')
165
+ .map((line) => line.trim())
166
+ .filter((line) => line !== '')
167
+ .join(' ');
168
+ if (text === '')
169
+ throw new Error(UNTRANSCRIBED);
170
+ return text;
171
+ }
172
+ async function alone(work, signal) {
173
+ const before = held;
174
+ let release;
175
+ const mine = new Promise((resolve) => {
176
+ release = resolve;
177
+ });
178
+ held = Promise.all([before, mine]);
179
+ try {
180
+ await unlessAborted(before, signal);
181
+ return await work();
182
+ }
183
+ finally {
184
+ release();
185
+ }
186
+ }
187
+ export async function transcribe(address, signal) {
188
+ ready();
189
+ const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
190
+ try {
191
+ const audio = join(work, 'audio');
192
+ await failing(NOT_DOWNLOADED, downloaded(address, (url) => fetched(url, signal), audio, 'audio'));
193
+ return await alone(() => {
194
+ ready();
195
+ return transcribed(audio, join(work, 'parts'), signal);
196
+ }, signal);
197
+ }
198
+ finally {
199
+ await rm(work, { recursive: true, force: true });
200
+ }
201
+ }
package/dist/translate.js CHANGED
@@ -3,14 +3,8 @@ function same(left, right) {
3
3
  return JSON.stringify(left) === JSON.stringify(right);
4
4
  }
5
5
  function applied(document, changes) {
6
- let current = document;
7
- for (const change of changes) {
8
- const result = apply(current, change);
9
- if (result.outcome !== 'applied')
10
- return null;
11
- current = result.document;
12
- }
13
- return serialize(current);
6
+ const result = apply(document, changes);
7
+ return result.outcome === 'applied' ? serialize(result.document) : null;
14
8
  }
15
9
  function components(path, before, after, text) {
16
10
  const edits = [];
package/dist/update.js ADDED
@@ -0,0 +1,138 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { createHash } from 'node:crypto';
3
+ import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
4
+ import { homedir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { run } from './clis.js';
7
+ import { kitHome, workspaceRoot } from './home.js';
8
+ import { service, throughSudo } from './placement.js';
9
+ function directory() {
10
+ return join(kitHome(), 'update');
11
+ }
12
+ function pending() {
13
+ return join(directory(), 'pending.json');
14
+ }
15
+ function outcome() {
16
+ return join(directory(), 'outcome');
17
+ }
18
+ function unit(callId) {
19
+ return `house-kit-update-${callId}`;
20
+ }
21
+ function manager() {
22
+ return service() === 'user' ? ['--user'] : [];
23
+ }
24
+ function elevated() {
25
+ return throughSudo() ? ['sudo', '-n'] : [];
26
+ }
27
+ function account() {
28
+ return throughSudo() ? [`--uid=${process.getuid()}`, `--gid=${process.getgid()}`] : [];
29
+ }
30
+ function placement() {
31
+ return service() === 'user' ? ['--workspace', workspaceRoot()] : ['--linux'];
32
+ }
33
+ function record() {
34
+ return `outcome=$1; shift; bash "$0" "$@"; printf %s "$?" > "$outcome"; ${[...elevated(), 'systemctl', ...manager(), 'start', 'house-kit.service'].join(' ')}`;
35
+ }
36
+ async function pendingCall() {
37
+ try {
38
+ return JSON.parse(await readFile(pending(), 'utf8')).input_id;
39
+ }
40
+ catch {
41
+ return null;
42
+ }
43
+ }
44
+ async function recorded() {
45
+ const status = await readFile(outcome(), 'utf8').catch(() => null);
46
+ if (status === null)
47
+ return null;
48
+ return status.trim() === '0' ? {} : { refused: `the Kit bootstrap exited with ${status.trim()}` };
49
+ }
50
+ async function forget(callId) {
51
+ if ((await pendingCall()) !== callId)
52
+ return;
53
+ await rm(pending(), { force: true });
54
+ await rm(outcome(), { force: true });
55
+ }
56
+ async function finished(callId) {
57
+ for (;;) {
58
+ const result = await recorded();
59
+ if (result !== null)
60
+ return result;
61
+ // systemctl answers at once, so ten seconds only bounds a stuck service manager.
62
+ if ((await run('systemctl', [...manager(), 'is-active', '--quiet', unit(callId)], 10_000)).status !== 0) {
63
+ return (await recorded()) ?? { refused: 'the Kit bootstrap did not finish' };
64
+ }
65
+ // The bootstrap runs for minutes, so checking each second sees its outcome within a second at little cost.
66
+ await new Promise((resolve) => setTimeout(resolve, 1000));
67
+ }
68
+ }
69
+ function rerun(script, callId, flags) {
70
+ return new Promise((resolve) => {
71
+ const [command, ...args] = [
72
+ ...elevated(),
73
+ 'systemd-run',
74
+ ...manager(),
75
+ ...account(),
76
+ '--wait',
77
+ '--collect',
78
+ '--quiet',
79
+ `--unit=${unit(callId)}`,
80
+ `--setenv=HOME=${homedir()}`,
81
+ `--setenv=HOUSE_KIT_HOME=${kitHome()}`,
82
+ '/bin/sh',
83
+ '-c',
84
+ record(),
85
+ script,
86
+ outcome(),
87
+ ...placement(),
88
+ ...flags,
89
+ ];
90
+ const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'inherit'] });
91
+ child.on('error', () => resolve({ refused: 'no service manager runs this Kit process' }));
92
+ child.on('close', () => resolve(null));
93
+ });
94
+ }
95
+ export class KitUpdate {
96
+ calls;
97
+ queue = Promise.resolve();
98
+ constructor(calls) {
99
+ this.calls = calls;
100
+ }
101
+ async resumed() {
102
+ const callId = await pendingCall();
103
+ if (callId === null)
104
+ return;
105
+ await this.enqueue(callId, () => finished(callId));
106
+ }
107
+ start(call) {
108
+ void this.enqueue(call.input_id, () => this.carry(call));
109
+ }
110
+ enqueue(callId, work) {
111
+ const before = this.queue;
112
+ const carried = this.calls.carry(callId, async () => {
113
+ await before;
114
+ return { result: await work(), returned: () => forget(callId) };
115
+ });
116
+ this.queue = before.then(() => carried);
117
+ return carried;
118
+ }
119
+ async carry(call) {
120
+ if ((await pendingCall()) === call.input_id)
121
+ return finished(call.input_id);
122
+ const answer = await fetch(call.bootstrap);
123
+ if (!answer.ok)
124
+ return { refused: `the Kit bootstrap could not be downloaded: ${answer.status}` };
125
+ const script = Buffer.from(await answer.arrayBuffer());
126
+ if (createHash('sha256').update(script).digest('hex') !== call.sha256) {
127
+ return { refused: 'the Kit bootstrap does not match its sha256' };
128
+ }
129
+ await mkdir(directory(), { recursive: true, mode: 0o700 });
130
+ const path = join(directory(), 'connect-linux.sh');
131
+ await writeFile(path, script, { mode: 0o700 });
132
+ await rm(outcome(), { force: true });
133
+ await writeFile(pending(), `${JSON.stringify({ input_id: call.input_id })}\n`, { mode: 0o600 });
134
+ return (await rerun(path, call.input_id, call.update_clis ? ['--update-clis'] : [])) ?? (await recorded()) ?? {
135
+ refused: 'the Kit bootstrap did not finish',
136
+ };
137
+ }
138
+ }