@agentshouse/kit 0.2.1-cd646adba3f64 → 0.2.1-cdc381ebf021f

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 (141) hide show
  1. package/README.md +42 -48
  2. package/dist/acp.js +102 -108
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +227 -68
  5. package/dist/api.js +19 -10
  6. package/dist/app-git.js +10 -26
  7. package/dist/bridge.js +18 -47
  8. package/dist/building.js +50 -0
  9. package/dist/calls.js +88 -0
  10. package/dist/chunks.js +49 -0
  11. package/dist/clis.js +157 -100
  12. package/dist/codex-main.js +58 -0
  13. package/dist/connect.js +108 -0
  14. package/dist/connection.js +120 -0
  15. package/dist/content.js +106 -0
  16. package/dist/conversations.js +815 -335
  17. package/dist/copies.js +439 -232
  18. package/dist/earlier.js +263 -0
  19. package/dist/end.js +23 -5
  20. package/dist/events.js +793 -0
  21. package/dist/files.js +139 -17
  22. package/dist/git.js +57 -21
  23. package/dist/grok.js +39 -0
  24. package/dist/home.js +122 -8
  25. package/dist/house-cli.js +359 -0
  26. package/dist/house-main.js +7 -159
  27. package/dist/instructions.js +7 -11
  28. package/dist/kit-cli.js +78 -0
  29. package/dist/kit-main.js +8 -22
  30. package/dist/login.js +132 -107
  31. package/dist/logout.js +15 -17
  32. package/dist/mcp.js +6 -2
  33. package/dist/owner.js +46 -0
  34. package/dist/placement.js +25 -0
  35. package/dist/relay.js +33 -2
  36. package/dist/resident.js +160 -58
  37. package/dist/scope.js +214 -0
  38. package/dist/scripts.js +303 -0
  39. package/dist/secret-input.js +1 -1
  40. package/dist/settings.js +188 -0
  41. package/dist/sign-in.js +40 -33
  42. package/dist/silence.js +28 -0
  43. package/dist/skills.js +49 -35
  44. package/dist/supervisor.js +163 -0
  45. package/dist/transcription.js +90 -47
  46. package/dist/translate.js +19 -16
  47. package/dist/update.js +119 -102
  48. package/dist/wire.js +32 -0
  49. package/dist/work.js +353 -0
  50. package/dist/workspace.js +103 -0
  51. package/package.json +25 -21
  52. package/dist/authority-main.js +0 -22
  53. package/dist/blocks.js +0 -51
  54. package/dist/bootstrap.json +0 -24
  55. package/dist/clis-main.js +0 -23
  56. package/dist/configure-main.js +0 -6
  57. package/dist/enrol-main.js +0 -4
  58. package/dist/stream.js +0 -72
  59. package/how-we-work.md +0 -23
  60. package/skills/ask-matt/LICENSE +0 -21
  61. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  62. package/skills/ask-matt/SKILL.md +0 -91
  63. package/skills/ask-matt/agents/openai.yaml +0 -4
  64. package/skills/code-review/LICENSE +0 -21
  65. package/skills/code-review/SKILL.md +0 -89
  66. package/skills/code-review/agents/openai.yaml +0 -3
  67. package/skills/codebase-design/DEEPENING.md +0 -37
  68. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  69. package/skills/codebase-design/LICENSE +0 -21
  70. package/skills/codebase-design/SKILL.md +0 -116
  71. package/skills/codebase-design/agents/openai.yaml +0 -3
  72. package/skills/diagnosing-bugs/LICENSE +0 -21
  73. package/skills/diagnosing-bugs/SKILL.md +0 -140
  74. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  75. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  76. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  77. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  78. package/skills/domain-modeling/LICENSE +0 -21
  79. package/skills/domain-modeling/SKILL.md +0 -76
  80. package/skills/domain-modeling/agents/openai.yaml +0 -3
  81. package/skills/grill-me/LICENSE +0 -21
  82. package/skills/grill-me/SKILL.md +0 -8
  83. package/skills/grill-me/agents/openai.yaml +0 -4
  84. package/skills/grill-with-docs/LICENSE +0 -21
  85. package/skills/grill-with-docs/SKILL.md +0 -8
  86. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  87. package/skills/grilling/LICENSE +0 -21
  88. package/skills/grilling/SKILL.md +0 -30
  89. package/skills/grilling/agents/openai.yaml +0 -3
  90. package/skills/handoff/LICENSE +0 -21
  91. package/skills/handoff/SKILL.md +0 -17
  92. package/skills/handoff/agents/openai.yaml +0 -4
  93. package/skills/implement/LICENSE +0 -21
  94. package/skills/implement/SKILL.md +0 -16
  95. package/skills/implement/agents/openai.yaml +0 -4
  96. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  97. package/skills/improve-codebase-architecture/LICENSE +0 -21
  98. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  99. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  100. package/skills/prototype/LICENSE +0 -21
  101. package/skills/prototype/LOGIC.md +0 -67
  102. package/skills/prototype/SKILL.md +0 -28
  103. package/skills/prototype/UI.md +0 -112
  104. package/skills/prototype/agents/openai.yaml +0 -3
  105. package/skills/research/LICENSE +0 -21
  106. package/skills/research/SKILL.md +0 -14
  107. package/skills/research/agents/openai.yaml +0 -3
  108. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  109. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  110. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  111. package/skills/tdd/LICENSE +0 -21
  112. package/skills/tdd/SKILL.md +0 -40
  113. package/skills/tdd/agents/openai.yaml +0 -3
  114. package/skills/tdd/mocking.md +0 -59
  115. package/skills/tdd/tests.md +0 -77
  116. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  117. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  118. package/skills/teach/LICENSE +0 -21
  119. package/skills/teach/MISSION-FORMAT.md +0 -31
  120. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  121. package/skills/teach/SKILL.md +0 -141
  122. package/skills/teach/agents/openai.yaml +0 -4
  123. package/skills/to-questionnaire/LICENSE +0 -21
  124. package/skills/to-questionnaire/SKILL.md +0 -55
  125. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  126. package/skills/to-spec/LICENSE +0 -21
  127. package/skills/to-spec/SKILL.md +0 -76
  128. package/skills/to-spec/agents/openai.yaml +0 -4
  129. package/skills/to-tickets/LICENSE +0 -21
  130. package/skills/to-tickets/SKILL.md +0 -106
  131. package/skills/to-tickets/agents/openai.yaml +0 -4
  132. package/skills/wait-what/LICENSE +0 -21
  133. package/skills/wait-what/SKILL.md +0 -8
  134. package/skills/wait-what/agents/openai.yaml +0 -4
  135. package/skills/wayfinder/LICENSE +0 -21
  136. package/skills/wayfinder/SKILL.md +0 -129
  137. package/skills/wayfinder/agents/openai.yaml +0 -4
  138. package/skills/writing-for-agents/LICENSE +0 -21
  139. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  140. package/skills/writing-for-agents/SKILL.md +0 -83
  141. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/skills.js CHANGED
@@ -1,17 +1,14 @@
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, writeFile } 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 { KIT_VERSION } from './clis.js';
4
+ import { GUIDE, listSkills } from '@agentshouse/skills';
5
+ import { parse } from 'yaml';
6
+ import { KIT_RELEASE } from './clis.js';
7
7
  import { kitHome, readConfiguration, readHome, writeHome } from './home.js';
8
- import { refusalOf } from './refusals.js';
9
- export const HOW_WE_WORK = '/rooms/private/how-we-work.md';
10
- const SKILL_SET = fileURLToPath(new URL('../skills/', import.meta.url));
11
- const HOW_WE_WORK_TEXT = readFileSync(new URL('../how-we-work.md', import.meta.url), 'utf8');
12
8
  const RECORD = 'skill-set.json';
13
- const CREATED = 'how-we-work-created';
14
- const UNWRITABLE = new Set(['room_not_found', 'operation_denied']);
9
+ const ENTRY = 'SKILL.md';
10
+ const FRONTMATTER = /^---\n([\s\S]*?)\n---\n/u;
11
+ const ORIGIN = '<origin>';
15
12
  function skillsDirectory() {
16
13
  return join(homedir(), '.agents', 'skills');
17
14
  }
@@ -29,50 +26,67 @@ async function present(path) {
29
26
  throw error;
30
27
  }
31
28
  }
32
- async function install() {
33
- const names = [];
34
- for (const name of (await readdir(SKILL_SET)).sort()) {
35
- if (!(await present(join(skillsDirectory(), name))) && !(await present(join(claudeDirectory(), name)))) {
36
- names.push(name);
29
+ function skillName(text) {
30
+ const front = FRONTMATTER.exec(text);
31
+ const fields = (front === null ? null : parse(front[1]));
32
+ return typeof fields?.name === 'string' && typeof fields.description === 'string' ? fields.name : undefined;
33
+ }
34
+ function skillSet(origin) {
35
+ const upstream = listSkills().map(({ name, directory }) => ({
36
+ name,
37
+ place: (destination) => cp(directory, destination, { recursive: true }),
38
+ }));
39
+ const pages = GUIDE.flatMap(({ text }) => {
40
+ const name = skillName(text);
41
+ if (name === undefined)
42
+ return [];
43
+ const place = async (destination) => {
44
+ await mkdir(destination);
45
+ await writeFile(join(destination, ENTRY), text.replaceAll(ORIGIN, origin));
46
+ };
47
+ return [{ name, place }];
48
+ });
49
+ return [...upstream, ...pages].sort((left, right) => (left.name < right.name ? -1 : 1));
50
+ }
51
+ async function install(origin) {
52
+ const placed = [];
53
+ for (const skill of skillSet(origin)) {
54
+ if (!(await present(join(skillsDirectory(), skill.name))) && !(await present(join(claudeDirectory(), skill.name)))) {
55
+ placed.push(skill);
37
56
  }
38
57
  }
58
+ const names = placed.map((skill) => skill.name);
39
59
  await writeHome(RECORD, { names });
40
60
  await mkdir(skillsDirectory(), { recursive: true });
41
61
  await mkdir(claudeDirectory(), { recursive: true });
42
62
  const skills = await realpath(skillsDirectory());
43
63
  const claude = await realpath(claudeDirectory());
44
- for (const name of names) {
45
- await cp(join(SKILL_SET, name), join(skillsDirectory(), name), { recursive: true });
64
+ for (const { name, place } of placed) {
65
+ await place(join(skillsDirectory(), name));
46
66
  if (claude !== skills)
47
67
  await symlink(relative(claude, join(skills, name)), join(claudeDirectory(), name));
48
68
  }
49
- await writeHome(RECORD, { version: KIT_VERSION, names });
69
+ await writeHome(RECORD, { version: KIT_RELEASE, origin, names });
70
+ }
71
+ export async function placeSkillSet(origin) {
72
+ try {
73
+ await replaceSkillSet(origin);
74
+ }
75
+ catch (error) {
76
+ process.stderr.write(`kit: the skills could not be placed: ${error.message}\n`);
77
+ }
50
78
  }
51
- export async function placeSkillSet() {
79
+ async function replaceSkillSet(origin) {
52
80
  const accepted = (await readConfiguration()).skills;
53
81
  const installed = await readHome(RECORD);
54
- if (accepted && installed?.version === KIT_VERSION)
82
+ if (accepted && installed?.version === KIT_RELEASE && installed.origin === origin)
55
83
  return;
56
84
  for (const name of installed?.names ?? []) {
57
85
  await rm(join(claudeDirectory(), name), { recursive: true, force: true });
58
86
  await rm(join(skillsDirectory(), name), { recursive: true, force: true });
59
87
  }
60
88
  if (accepted)
61
- await install();
89
+ await install(origin);
62
90
  else
63
91
  await rm(join(kitHome(), RECORD), { force: true });
64
92
  }
65
- export async function createHowWeWork(bridge) {
66
- if ((await readHome(RECORD)) === null || (await present(join(kitHome(), CREATED))))
67
- return;
68
- const created = await bridge.tool('edit', { changes: [{ op: 'create', path: HOW_WE_WORK, content: HOW_WE_WORK_TEXT }] });
69
- if (created.isError === true) {
70
- const text = created.content.map((content) => content.text).join('\n');
71
- const refusal = refusalOf(text);
72
- if (UNWRITABLE.has(refusal?.code ?? ''))
73
- return;
74
- if (refusal?.code !== 'edit_conflict' || !refusal.conflicts.includes('path_exists'))
75
- throw new Error(text);
76
- }
77
- await writeFile(join(kitHome(), CREATED), '');
78
- }
@@ -0,0 +1,163 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { existsSync } from 'node:fs';
3
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
4
+ import { homedir } from 'node:os';
5
+ import { dirname, join } from 'node:path';
6
+ import { setTimeout as delay } from 'node:timers/promises';
7
+ import { oneFolder } from './home.js';
8
+ const SERVICE = 'house-kit';
9
+ const UNIT = `${SERVICE}.service`;
10
+ // launchctl, loginctl and systemctl answer at once and lingering starts the account's service manager within a second, so ten seconds only bound a stuck one.
11
+ const MANAGER_MS = 10_000;
12
+ // The service manager lingering starts answers within a second, so a check every tenth of a second starts Kit as soon as it does.
13
+ const MANAGER_CHECK_MS = 100;
14
+ // A Kit that failed restarts after five seconds, back soon without spinning on a failure that repeats; a Kit that ended itself stays stopped.
15
+ const RESTART_SECONDS = 5;
16
+ export function userUnit() {
17
+ return join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'systemd', 'user', UNIT);
18
+ }
19
+ export function launchAgent() {
20
+ return join(homedir(), 'Library', 'LaunchAgents', `${SERVICE}.plist`);
21
+ }
22
+ export function launcher() {
23
+ return join(oneFolder(), 'bin', 'kit');
24
+ }
25
+ export function systemd() {
26
+ return existsSync('/run/systemd/system');
27
+ }
28
+ function userBus() {
29
+ if (process.env.XDG_RUNTIME_DIR)
30
+ return process.env;
31
+ return { ...process.env, XDG_RUNTIME_DIR: `/run/user/${process.getuid()}` };
32
+ }
33
+ export function manage(command, args) {
34
+ return spawnSync(command, args, { encoding: 'utf8', timeout: MANAGER_MS, env: command === 'systemctl' ? userBus() : process.env });
35
+ }
36
+ export function required(command, args, failed) {
37
+ const ran = manage(command, args);
38
+ if (ran.status === 0)
39
+ return;
40
+ const cause = ran.error?.message ?? (ran.stderr.trim() || `${command} exited with ${ran.status ?? 'a signal'}`);
41
+ throw new Error(`${failed}: ${cause}`);
42
+ }
43
+ function unitWord(text) {
44
+ return `"${text.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('%', '%%')}"`;
45
+ }
46
+ function xmlText(text) {
47
+ return text.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
48
+ }
49
+ function unitText() {
50
+ return `[Unit]
51
+ Description=Kit
52
+
53
+ [Service]
54
+ ExecStart=${unitWord(launcher())} resident
55
+ Restart=on-failure
56
+ RestartSec=${RESTART_SECONDS}
57
+ Delegate=yes
58
+ OOMPolicy=continue
59
+
60
+ [Install]
61
+ WantedBy=default.target
62
+ `;
63
+ }
64
+ function plistText() {
65
+ return `<?xml version="1.0" encoding="UTF-8"?>
66
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
67
+ <plist version="1.0">
68
+ <dict>
69
+ \t<key>Label</key>
70
+ \t<string>${SERVICE}</string>
71
+ \t<key>ProgramArguments</key>
72
+ \t<array>
73
+ \t\t<string>${xmlText(launcher())}</string>
74
+ \t\t<string>resident</string>
75
+ \t</array>
76
+ \t<key>RunAtLoad</key>
77
+ \t<true/>
78
+ \t<key>KeepAlive</key>
79
+ \t<dict>
80
+ \t\t<key>SuccessfulExit</key>
81
+ \t\t<false/>
82
+ \t</dict>
83
+ \t<key>ProcessType</key>
84
+ \t<string>Interactive</string>
85
+ \t<key>StandardErrorPath</key>
86
+ \t<string>${xmlText(join(oneFolder(), 'kit.log'))}</string>
87
+ </dict>
88
+ </plist>
89
+ `;
90
+ }
91
+ async function define(path, text) {
92
+ if ((await readFile(path, 'utf8').catch(() => null)) === text)
93
+ return false;
94
+ await mkdir(dirname(path), { recursive: true });
95
+ await writeFile(`${path}.new`, text);
96
+ await rename(`${path}.new`, path);
97
+ return true;
98
+ }
99
+ export function defineSupervisor() {
100
+ return process.platform === 'darwin' ? define(launchAgent(), plistText()) : define(userUnit(), unitText());
101
+ }
102
+ export async function linger() {
103
+ required('loginctl', ['enable-linger', String(process.getuid())], 'lingering could not be turned on for this account');
104
+ if (process.getuid() === 0)
105
+ required('systemctl', ['start', 'user@0.service'], "root's service manager could not be started");
106
+ const stuck = Date.now() + MANAGER_MS;
107
+ while (manage('systemctl', ['--user', 'show-environment']).status !== 0) {
108
+ if (Date.now() >= stuck) {
109
+ throw new Error(`this account's service manager did not start within ${MANAGER_MS / 1000} seconds; restart this computer, then run this command again`);
110
+ }
111
+ await delay(MANAGER_CHECK_MS);
112
+ }
113
+ }
114
+ function macos() {
115
+ const target = `gui/${process.getuid()}`;
116
+ return { target, service: `${target}/${SERVICE}` };
117
+ }
118
+ export async function supervise(changed, restart) {
119
+ if (process.platform === 'darwin') {
120
+ const { target, service } = macos();
121
+ const printed = manage('launchctl', ['print', service]);
122
+ const loaded = printed.status === 0;
123
+ if (loaded && changed)
124
+ required('launchctl', ['bootout', service], 'the Kit service could not be stopped');
125
+ if (!loaded || changed) {
126
+ manage('launchctl', ['enable', service]);
127
+ required('launchctl', ['bootstrap', target, launchAgent()], 'the Kit service could not be started');
128
+ }
129
+ else if (restart) {
130
+ required('launchctl', ['kickstart', '-k', service], 'the Kit service could not be restarted');
131
+ }
132
+ else if (!/^\tstate = running$/m.test(printed.stdout)) {
133
+ required('launchctl', ['kickstart', service], 'the Kit service could not be started');
134
+ }
135
+ return;
136
+ }
137
+ await linger();
138
+ if (changed)
139
+ required('systemctl', ['--user', 'daemon-reload'], 'systemd could not read the Kit service');
140
+ required('systemctl', ['--user', '--quiet', 'enable', UNIT], 'the Kit service could not be enabled');
141
+ if (manage('systemctl', ['--user', '--quiet', 'is-active', UNIT]).status !== 0) {
142
+ required('systemctl', ['--user', 'start', UNIT], 'the Kit service could not be started');
143
+ }
144
+ else if (changed || restart) {
145
+ required('systemctl', ['--user', 'restart', UNIT], 'the Kit service could not be restarted');
146
+ }
147
+ }
148
+ export function startService() {
149
+ if (process.platform === 'darwin') {
150
+ const { service } = macos();
151
+ const printed = manage('launchctl', ['print', service]);
152
+ if (printed.status !== 0 || /^\tstate = running$/m.test(printed.stdout))
153
+ return false;
154
+ required('launchctl', ['kickstart', service], 'the Kit service could not be started');
155
+ return true;
156
+ }
157
+ if (manage('systemctl', ['--user', '--quiet', 'is-enabled', UNIT]).status !== 0)
158
+ return false;
159
+ if (manage('systemctl', ['--user', '--quiet', 'is-active', UNIT]).status === 0)
160
+ return false;
161
+ required('systemctl', ['--user', 'start', UNIT], 'the Kit service could not be started');
162
+ return true;
163
+ }
@@ -1,33 +1,40 @@
1
- import { createHash } from 'node:crypto';
2
- import { createReadStream } from 'node:fs';
3
1
  import { chmod, mkdir, mkdtemp, readdir, readFile, rename, rm, stat } from 'node:fs/promises';
4
2
  import { availableParallelism, tmpdir } from 'node:os';
5
3
  import { basename, join } from 'node:path';
6
- import { pipeline } from 'node:stream/promises';
4
+ import { isDeepStrictEqual } from 'node:util';
7
5
  import { unlessAborted } from './abort.js';
6
+ import { downloadServed } from './building.js';
8
7
  import { failureOf, run } from './clis.js';
8
+ import { downloaded, fetched } from './files.js';
9
9
  import { kitHome } from './home.js';
10
10
  const RELEASE = 'https://github.com/agentshouse/kit/releases/download/engine-2';
11
11
  export const MODEL = {
12
- url: 'https://huggingface.co/ggml-org/parakeet-GGUF/resolve/35156454d1a39de06863303dd209fd2bed6ee079/ggml-parakeet-tdt-0.6b-v3-q4_k.bin',
12
+ name: 'ggml-parakeet-tdt-0.6b-v3-q4_k.bin',
13
+ from: 'https://huggingface.co/ggml-org/parakeet-GGUF/resolve/35156454d1a39de06863303dd209fd2bed6ee079/ggml-parakeet-tdt-0.6b-v3-q4_k.bin',
13
14
  sha256: '8b205b8b39c6535e153de6fb11c51db46125d45c4f16ba496fe41a0fe71b885e',
15
+ bytes: 415_611_879,
14
16
  executable: false,
15
17
  };
16
18
  export const ENGINES = {
17
19
  'linux-x64': {
18
- decoder: { url: `${RELEASE}/ffmpeg-linux-x86_64`, sha256: '1f8235d206428305afb89c6e8ab99d100425a0ba61e22bb4c7aff017db8c8de5', executable: true },
19
- engine: { url: `${RELEASE}/parakeet-cli-linux-x86_64`, sha256: 'bb2585695992fe1927a33960002cdf51b8a90a96ad65593c3d0a9f1fbc0865c2', executable: true },
20
+ decoder: { name: 'ffmpeg-linux-x86_64', from: `${RELEASE}/ffmpeg-linux-x86_64`, sha256: '1f8235d206428305afb89c6e8ab99d100425a0ba61e22bb4c7aff017db8c8de5', bytes: 5_175_832, executable: true },
21
+ engine: { name: 'parakeet-cli-linux-x86_64', from: `${RELEASE}/parakeet-cli-linux-x86_64`, sha256: 'bb2585695992fe1927a33960002cdf51b8a90a96ad65593c3d0a9f1fbc0865c2', bytes: 3_801_616, executable: true },
20
22
  },
21
23
  'linux-arm64': {
22
- decoder: { url: `${RELEASE}/ffmpeg-linux-aarch64`, sha256: 'a7e08a0fae0179d1078312f6bb13d2a9aa4d4c71a1d82a022953f25dee452f5e', executable: true },
23
- engine: { url: `${RELEASE}/parakeet-cli-linux-aarch64`, sha256: 'cbc0bedc8daae97f1ef9dcee4200c06226218e300e0fd62ead1306d22742b7ed', executable: true },
24
+ decoder: { name: 'ffmpeg-linux-aarch64', from: `${RELEASE}/ffmpeg-linux-aarch64`, sha256: 'a7e08a0fae0179d1078312f6bb13d2a9aa4d4c71a1d82a022953f25dee452f5e', bytes: 4_590_208, executable: true },
25
+ engine: { name: 'parakeet-cli-linux-aarch64', from: `${RELEASE}/parakeet-cli-linux-aarch64`, sha256: 'cbc0bedc8daae97f1ef9dcee4200c06226218e300e0fd62ead1306d22742b7ed', bytes: 3_477_624, executable: true },
24
26
  },
25
27
  'darwin-arm64': {
26
- decoder: { url: `${RELEASE}/ffmpeg-darwin-arm64`, sha256: '7abe154aaed3d6b49f5167ca7221bb66bed51388925d99a7e4261a41fe52fba6', executable: true },
27
- engine: { url: `${RELEASE}/parakeet-cli-darwin-arm64`, sha256: '9a2aafc0922ad7c0b6db6eff1641e96572ba32ac40bf995a1df242ed2febf297', executable: true },
28
+ decoder: { name: 'ffmpeg-darwin-arm64', from: `${RELEASE}/ffmpeg-darwin-arm64`, sha256: '7abe154aaed3d6b49f5167ca7221bb66bed51388925d99a7e4261a41fe52fba6', bytes: 3_696_768, executable: true },
29
+ engine: { name: 'parakeet-cli-darwin-arm64', from: `${RELEASE}/parakeet-cli-darwin-arm64`, sha256: '9a2aafc0922ad7c0b6db6eff1641e96572ba32ac40bf995a1df242ed2febf297', bytes: 1_939_728, executable: true },
28
30
  },
29
31
  };
30
32
  const CHUNK_SECONDS = 20;
33
+ const OFF = "transcription is off on the Agent's Environment, so this message's audio has no text; turn it on or write instead";
34
+ const NOT_INSTALLED = "the speech engine is not installed on the Agent's Environment, so this message's audio has no text";
35
+ const NOT_DOWNLOADED = 'an audio file this message links could not be downloaded; send the message again';
36
+ const UNDECODABLE = 'an audio file this message links could not be decoded';
37
+ const UNTRANSCRIBED = 'an audio file this message links could not be transcribed';
31
38
  let held = Promise.resolve();
32
39
  let holding = Promise.resolve();
33
40
  let setting = null;
@@ -40,7 +47,7 @@ function installed(artifact) {
40
47
  return join(directory(), artifact.sha256);
41
48
  }
42
49
  async function executed(command, args, signal) {
43
- // 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
+ // A decode or a transcription runs as long as its audio needs; the setting turning off or the turn's interrupt or end aborts it instead.
44
51
  const ran = await run(command, args, 0, false, signal);
45
52
  if (ran.status !== 0)
46
53
  throw new Error(failureOf(ran, basename(command)));
@@ -49,22 +56,50 @@ async function executed(command, args, signal) {
49
56
  function present(path) {
50
57
  return stat(path).then(() => true, () => false);
51
58
  }
52
- async function cpus() {
59
+ async function threads() {
53
60
  const [quota, period] = (await readFile('/sys/fs/cgroup/cpu.max', 'utf8').catch(() => 'max')).split(' ');
54
61
  const allowed = quota === 'max' ? Infinity : Math.ceil(Number(quota) / Number(period));
55
- return Math.min(availableParallelism(), allowed);
56
- }
57
- async function sha256(path) {
58
- const hash = createHash('sha256');
59
- await pipeline(createReadStream(path), hash);
60
- return hash.digest('hex');
62
+ return Math.max(1, Math.floor(Math.min(availableParallelism(), allowed) / 2));
61
63
  }
62
64
  function causeOf(error) {
63
65
  return error instanceof Error ? error.message : String(error);
64
66
  }
67
+ async function failing(reason, work) {
68
+ try {
69
+ return await work;
70
+ }
71
+ catch (error) {
72
+ throw new Error(reason, { cause: error });
73
+ }
74
+ }
75
+ function ready() {
76
+ if (setting !== true)
77
+ throw new Error(OFF);
78
+ if (!readiness.installed)
79
+ throw new Error(NOT_INSTALLED);
80
+ }
65
81
  function pinnedHere() {
66
82
  return ENGINES[`${process.platform}-${process.arch}`];
67
83
  }
84
+ const DOWNLOADS = 3;
85
+ async function downloadedAgain(artifact, signal) {
86
+ // An artifact arrives at a hundred kilobytes a second or its download ends, so a stalled Building cannot hold the install open.
87
+ const deadlineMs = Math.ceil(artifact.bytes / 100);
88
+ const deadline = AbortSignal.timeout(deadlineMs);
89
+ for (let attempt = 1;; attempt++) {
90
+ try {
91
+ return await downloadServed(artifact.name, artifact.sha256, `${installed(artifact)}.part`, AbortSignal.any([signal, deadline]), artifact.bytes);
92
+ }
93
+ catch (error) {
94
+ if (signal.aborted)
95
+ throw error;
96
+ if (deadline.aborted)
97
+ throw new Error(`${artifact.name} did not arrive within ${Math.ceil(deadlineMs / 1000)} seconds`);
98
+ if (attempt === DOWNLOADS)
99
+ throw error;
100
+ }
101
+ }
102
+ }
68
103
  async function install(artifacts, signal) {
69
104
  await mkdir(directory(), { recursive: true });
70
105
  const pinned = new Set(artifacts.map((artifact) => artifact.sha256));
@@ -77,13 +112,8 @@ async function install(artifacts, signal) {
77
112
  if (!(await present(installed(artifact))))
78
113
  missing.push(artifact);
79
114
  try {
80
- for (const artifact of missing) {
81
- const part = `${installed(artifact)}.part`;
82
- // Three retries carry a large download past a dropped connection before the download fails.
83
- await executed('curl', ['-fsSL', '--retry', '3', '-o', part, artifact.url], signal);
84
- if ((await sha256(part)) !== artifact.sha256)
85
- throw new Error(`${artifact.url} does not match its sha256`);
86
- }
115
+ for (const artifact of missing)
116
+ await downloadedAgain(artifact, signal);
87
117
  for (const artifact of missing) {
88
118
  if (artifact.executable)
89
119
  await chmod(`${installed(artifact)}.part`, 0o755);
@@ -96,7 +126,7 @@ async function install(artifacts, signal) {
96
126
  }
97
127
  }
98
128
  function reported(next) {
99
- const changed = next.installed !== readiness.installed || next.failure !== readiness.failure;
129
+ const changed = !isDeepStrictEqual(next, readiness);
100
130
  readiness = next;
101
131
  return changed;
102
132
  }
@@ -108,10 +138,12 @@ async function settle(transcribe) {
108
138
  await rm(directory(), { recursive: true, force: true });
109
139
  return reported({ installed: false, failure: null });
110
140
  }
141
+ const pinned = pinnedHere();
142
+ if (pinned === undefined)
143
+ return reported({ installed: false, failure: null, no_engine: true });
111
144
  const download = new AbortController();
112
145
  downloading = download;
113
146
  try {
114
- const pinned = pinnedHere();
115
147
  await install([pinned.decoder, pinned.engine, MODEL], download.signal);
116
148
  return reported({ installed: true, failure: null });
117
149
  }
@@ -133,27 +165,23 @@ export function holdTranscription(transcribe) {
133
165
  export function transcriptionReport() {
134
166
  return readiness;
135
167
  }
136
- async function transcribed(path, signal) {
168
+ async function transcribed(audio, parts, signal) {
137
169
  const pinned = pinnedHere();
138
- const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
139
- try {
140
- await executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', path, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
141
- '-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(work, '%05d.wav')], signal);
142
- const parts = (await readdir(work)).sort().flatMap((part) => ['-f', join(work, part)]);
143
- const text = await executed(installed(pinned.engine), ['-t', String(await cpus()), '-np', '-m', installed(MODEL), ...parts], signal);
144
- return text
145
- .split('\n')
146
- .map((line) => line.trim())
147
- .filter((line) => line !== '')
148
- .join(' ');
149
- }
150
- finally {
151
- await rm(work, { recursive: true, force: true });
152
- }
170
+ await mkdir(parts);
171
+ await failing(UNDECODABLE, executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', audio, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
172
+ '-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(parts, '%05d.wav')], signal));
173
+ const segments = (await readdir(parts)).sort().flatMap((part) => ['-f', join(parts, part)]);
174
+ const said = await failing(UNTRANSCRIBED, executed(installed(pinned.engine), ['-t', String(await threads()), '-np', '-m', installed(MODEL), ...segments], signal));
175
+ const text = said
176
+ .split('\n')
177
+ .map((line) => line.trim())
178
+ .filter((line) => line !== '')
179
+ .join(' ');
180
+ if (text === '')
181
+ throw new Error(UNTRANSCRIBED);
182
+ return text;
153
183
  }
154
- export async function transcribe(path, signal) {
155
- if (!readiness.installed)
156
- return null;
184
+ async function alone(work, signal) {
157
185
  const before = held;
158
186
  let release;
159
187
  const mine = new Promise((resolve) => {
@@ -162,9 +190,24 @@ export async function transcribe(path, signal) {
162
190
  held = Promise.all([before, mine]);
163
191
  try {
164
192
  await unlessAborted(before, signal);
165
- return readiness.installed ? await transcribed(path, signal) : null;
193
+ return await work();
166
194
  }
167
195
  finally {
168
196
  release();
169
197
  }
170
198
  }
199
+ export async function transcribe(address, signal) {
200
+ ready();
201
+ const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
202
+ try {
203
+ const audio = join(work, 'audio');
204
+ await failing(NOT_DOWNLOADED, downloaded(address, (url) => fetched(url, signal), audio, 'audio'));
205
+ return await alone(() => {
206
+ ready();
207
+ return transcribed(audio, join(work, 'parts'), signal);
208
+ }, signal);
209
+ }
210
+ finally {
211
+ await rm(work, { recursive: true, force: true });
212
+ }
213
+ }
package/dist/translate.js CHANGED
@@ -1,18 +1,21 @@
1
- import { apply, parse, serialize } from '@agentshouse/mdmodel';
1
+ import { apply, definitionAt, HOUSE_TYPED_PATHS, parse, serialize, } from '@agentshouse/mdmodel';
2
2
  function same(left, right) {
3
3
  return JSON.stringify(left) === JSON.stringify(right);
4
4
  }
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);
5
+ function applied(document, changes, definition) {
6
+ const result = apply(document, changes, definition);
7
+ return result.outcome === 'applied' ? serialize(result.document) : null;
8
+ }
9
+ export function readDocument(content, file, model) {
10
+ const definitions = definitionAt(model, file, HOUSE_TYPED_PATHS);
11
+ const parsed = parse(content, file, definitions);
12
+ if (!parsed.ok)
13
+ return null;
14
+ const { document } = parsed;
15
+ const definition = definitions(document.type);
16
+ return definition !== undefined || document.type === undefined ? { document, definition } : null;
14
17
  }
15
- function components(path, before, after, text) {
18
+ function components(path, before, after, text, definition) {
16
19
  const edits = [];
17
20
  const changes = [];
18
21
  if (before.preamble !== after.preamble) {
@@ -39,13 +42,13 @@ function components(path, before, after, text) {
39
42
  edits.push({ op: 'replace_section', path, section: name, base: prior, content: value });
40
43
  changes.push({ kind: 'section', name, value, prior });
41
44
  }
42
- return edits.length > 0 && applied(before, changes) === text ? edits : null;
45
+ return edits.length > 0 && applied(before, changes, definition) === text ? edits : null;
43
46
  }
44
- export function changed(path, file, base, before, after) {
47
+ export function changed(path, file, base, before, after, model) {
45
48
  if (!file.endsWith('.md'))
46
49
  return [{ op: 'replace', path, base, content: after }];
47
- const parsedBefore = parse(before, file);
48
- const parsedAfter = parse(after, file);
49
- const structured = parsedBefore.ok && parsedAfter.ok ? components(path, parsedBefore.document, parsedAfter.document, after) : null;
50
+ const prior = readDocument(before, file, model);
51
+ const next = readDocument(after, file, model);
52
+ const structured = prior !== null && next !== null ? components(path, prior.document, next.document, after, prior.definition) : null;
50
53
  return structured ?? [{ op: 'replace', path, base, content: after }];
51
54
  }