@agentshouse/kit 0.2.1-alpha.9 → 0.2.1-c03721a31074a
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 +48 -29
- package/dist/abort.js +9 -0
- package/dist/acp.js +49 -86
- package/dist/agents.js +333 -52
- package/dist/api.js +40 -4
- package/dist/app-git.js +48 -0
- package/dist/bootstrap.json +10 -14
- package/dist/bridge.js +93 -78
- package/dist/calls.js +63 -0
- package/dist/chatgpt.js +32 -0
- package/dist/clis-main.js +23 -0
- package/dist/clis.js +217 -72
- package/dist/codex-main.js +58 -0
- package/dist/connection.js +126 -0
- package/dist/conversations.js +642 -263
- package/dist/copies.js +237 -173
- package/dist/end.js +27 -0
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +525 -0
- package/dist/files.js +140 -17
- package/dist/git-main.js +2 -2
- package/dist/git.js +16 -10
- package/dist/home.js +127 -9
- package/dist/house-cli.js +300 -0
- package/dist/house-main.js +5 -102
- package/dist/instructions.js +16 -17
- package/dist/kit-cli.js +68 -0
- package/dist/kit-main.js +8 -16
- package/dist/login.js +128 -95
- package/dist/logout.js +19 -0
- package/dist/mcp.js +41 -0
- package/dist/operation.js +7 -4
- package/dist/parts.js +82 -0
- package/dist/placement.js +39 -0
- package/dist/refusals.js +53 -0
- package/dist/relay.js +20 -0
- package/dist/resident.js +139 -52
- package/dist/scope.js +206 -0
- package/dist/secret-input.js +7 -2
- package/dist/service-main.js +9 -0
- package/dist/service.js +55 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +50 -31
- package/dist/skills.js +20 -37
- package/dist/transcription.js +214 -0
- package/dist/translate.js +2 -8
- package/dist/update.js +163 -0
- package/dist/web-wizard.js +100 -0
- package/dist/wire.js +22 -0
- package/dist/work.js +346 -0
- package/package.json +14 -16
- package/dist/blocks.js +0 -51
- package/dist/stream.js +0 -47
- package/how-we-work.md +0 -25
- package/skills/ask-matt/LICENSE +0 -21
- package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
- package/skills/ask-matt/SKILL.md +0 -91
- package/skills/ask-matt/agents/openai.yaml +0 -4
- package/skills/code-review/LICENSE +0 -21
- package/skills/code-review/SKILL.md +0 -89
- package/skills/code-review/agents/openai.yaml +0 -3
- package/skills/codebase-design/DEEPENING.md +0 -37
- package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
- package/skills/codebase-design/LICENSE +0 -21
- package/skills/codebase-design/SKILL.md +0 -116
- package/skills/codebase-design/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/LICENSE +0 -21
- package/skills/diagnosing-bugs/SKILL.md +0 -140
- package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
- package/skills/domain-modeling/ADR-FORMAT.md +0 -47
- package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
- package/skills/domain-modeling/LICENSE +0 -21
- package/skills/domain-modeling/SKILL.md +0 -76
- package/skills/domain-modeling/agents/openai.yaml +0 -3
- package/skills/grill-me/LICENSE +0 -21
- package/skills/grill-me/SKILL.md +0 -8
- package/skills/grill-me/agents/openai.yaml +0 -4
- package/skills/grill-with-docs/LICENSE +0 -21
- package/skills/grill-with-docs/SKILL.md +0 -8
- package/skills/grill-with-docs/agents/openai.yaml +0 -4
- package/skills/grilling/LICENSE +0 -21
- package/skills/grilling/SKILL.md +0 -30
- package/skills/grilling/agents/openai.yaml +0 -3
- package/skills/handoff/LICENSE +0 -21
- package/skills/handoff/SKILL.md +0 -17
- package/skills/handoff/agents/openai.yaml +0 -4
- package/skills/implement/LICENSE +0 -21
- package/skills/implement/SKILL.md +0 -16
- package/skills/implement/agents/openai.yaml +0 -4
- package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
- package/skills/improve-codebase-architecture/LICENSE +0 -21
- package/skills/improve-codebase-architecture/SKILL.md +0 -72
- package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
- package/skills/prototype/LICENSE +0 -21
- package/skills/prototype/LOGIC.md +0 -67
- package/skills/prototype/SKILL.md +0 -28
- package/skills/prototype/UI.md +0 -112
- package/skills/prototype/agents/openai.yaml +0 -3
- package/skills/research/LICENSE +0 -21
- package/skills/research/SKILL.md +0 -14
- package/skills/research/agents/openai.yaml +0 -3
- package/skills/resolving-merge-conflicts/LICENSE +0 -21
- package/skills/resolving-merge-conflicts/SKILL.md +0 -16
- package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
- package/skills/tdd/LICENSE +0 -21
- package/skills/tdd/SKILL.md +0 -40
- package/skills/tdd/agents/openai.yaml +0 -3
- package/skills/tdd/mocking.md +0 -59
- package/skills/tdd/tests.md +0 -77
- package/skills/teach/GLOSSARY-FORMAT.md +0 -35
- package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
- package/skills/teach/LICENSE +0 -21
- package/skills/teach/MISSION-FORMAT.md +0 -31
- package/skills/teach/RESOURCES-FORMAT.md +0 -32
- package/skills/teach/SKILL.md +0 -141
- package/skills/teach/agents/openai.yaml +0 -4
- package/skills/to-questionnaire/LICENSE +0 -21
- package/skills/to-questionnaire/SKILL.md +0 -55
- package/skills/to-questionnaire/agents/openai.yaml +0 -4
- package/skills/to-spec/LICENSE +0 -21
- package/skills/to-spec/SKILL.md +0 -76
- package/skills/to-spec/agents/openai.yaml +0 -4
- package/skills/to-tickets/LICENSE +0 -21
- package/skills/to-tickets/SKILL.md +0 -106
- package/skills/to-tickets/agents/openai.yaml +0 -4
- package/skills/wait-what/LICENSE +0 -21
- package/skills/wait-what/SKILL.md +0 -8
- package/skills/wait-what/agents/openai.yaml +0 -4
- package/skills/wayfinder/LICENSE +0 -21
- package/skills/wayfinder/SKILL.md +0 -129
- package/skills/wayfinder/agents/openai.yaml +0 -4
- package/skills/writing-for-agents/LICENSE +0 -21
- package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
- package/skills/writing-for-agents/SKILL.md +0 -83
- package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/service.js
ADDED
|
@@ -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
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 {
|
|
4
|
-
import {
|
|
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
|
|
10
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
constructor(house, agents, changed) {
|
|
18
|
+
calls;
|
|
19
|
+
constructor(house, agents, calls) {
|
|
18
20
|
this.house = house;
|
|
19
21
|
this.agents = agents;
|
|
20
|
-
this.
|
|
22
|
+
this.calls = calls;
|
|
21
23
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
start(call) {
|
|
25
|
+
void this.calls.carry(call.input_id, () => this.signIn(call));
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
.
|
|
33
|
-
|
|
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
|
|
40
|
-
await this.
|
|
41
|
-
|
|
42
|
-
|
|
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,
|
|
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,
|
|
100
|
+
child.on('close', (status, killed) => resolve(status === 0 ? null : `exited with ${status ?? killed}${last ? `: ${last}` : ''}`));
|
|
83
101
|
});
|
|
84
|
-
|
|
85
|
-
|
|
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 {
|
|
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 {
|
|
6
|
-
import {
|
|
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
|
|
35
|
-
for (const
|
|
36
|
-
if (!(await present(join(skillsDirectory(), name))) && !(await present(join(claudeDirectory(), name)))) {
|
|
37
|
-
|
|
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
|
|
46
|
-
await cp(
|
|
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:
|
|
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 ===
|
|
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,214 @@
|
|
|
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
|
+
bytes: 415_611_879,
|
|
16
|
+
executable: false,
|
|
17
|
+
};
|
|
18
|
+
export const ENGINES = {
|
|
19
|
+
'linux-x64': {
|
|
20
|
+
decoder: { url: `${RELEASE}/ffmpeg-linux-x86_64`, sha256: '1f8235d206428305afb89c6e8ab99d100425a0ba61e22bb4c7aff017db8c8de5', bytes: 5_175_832, executable: true },
|
|
21
|
+
engine: { url: `${RELEASE}/parakeet-cli-linux-x86_64`, sha256: 'bb2585695992fe1927a33960002cdf51b8a90a96ad65593c3d0a9f1fbc0865c2', bytes: 3_801_616, executable: true },
|
|
22
|
+
},
|
|
23
|
+
'linux-arm64': {
|
|
24
|
+
decoder: { url: `${RELEASE}/ffmpeg-linux-aarch64`, sha256: 'a7e08a0fae0179d1078312f6bb13d2a9aa4d4c71a1d82a022953f25dee452f5e', bytes: 4_590_208, executable: true },
|
|
25
|
+
engine: { url: `${RELEASE}/parakeet-cli-linux-aarch64`, sha256: 'cbc0bedc8daae97f1ef9dcee4200c06226218e300e0fd62ead1306d22742b7ed', bytes: 3_477_624, executable: true },
|
|
26
|
+
},
|
|
27
|
+
'darwin-arm64': {
|
|
28
|
+
decoder: { url: `${RELEASE}/ffmpeg-darwin-arm64`, sha256: '7abe154aaed3d6b49f5167ca7221bb66bed51388925d99a7e4261a41fe52fba6', bytes: 3_696_768, executable: true },
|
|
29
|
+
engine: { url: `${RELEASE}/parakeet-cli-darwin-arm64`, sha256: '9a2aafc0922ad7c0b6db6eff1641e96572ba32ac40bf995a1df242ed2febf297', bytes: 1_939_728, executable: true },
|
|
30
|
+
},
|
|
31
|
+
};
|
|
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';
|
|
38
|
+
let held = Promise.resolve();
|
|
39
|
+
let holding = Promise.resolve();
|
|
40
|
+
let setting = null;
|
|
41
|
+
let downloading = null;
|
|
42
|
+
let readiness = { installed: false, failure: null };
|
|
43
|
+
function directory() {
|
|
44
|
+
return join(kitHome(), 'transcription');
|
|
45
|
+
}
|
|
46
|
+
function installed(artifact) {
|
|
47
|
+
return join(directory(), artifact.sha256);
|
|
48
|
+
}
|
|
49
|
+
async function executed(command, args, signal) {
|
|
50
|
+
// A download or a transcription runs as long as its size needs; a download's deadline, the setting turning off or the turn's interrupt or end aborts it instead.
|
|
51
|
+
const ran = await run(command, args, 0, false, signal);
|
|
52
|
+
if (ran.status !== 0)
|
|
53
|
+
throw new Error(failureOf(ran, basename(command)));
|
|
54
|
+
return ran.stdout;
|
|
55
|
+
}
|
|
56
|
+
function present(path) {
|
|
57
|
+
return stat(path).then(() => true, () => false);
|
|
58
|
+
}
|
|
59
|
+
async function threads() {
|
|
60
|
+
const [quota, period] = (await readFile('/sys/fs/cgroup/cpu.max', 'utf8').catch(() => 'max')).split(' ');
|
|
61
|
+
const allowed = quota === 'max' ? Infinity : Math.ceil(Number(quota) / Number(period));
|
|
62
|
+
return Math.max(1, Math.floor(Math.min(availableParallelism(), allowed) / 2));
|
|
63
|
+
}
|
|
64
|
+
async function sha256(path) {
|
|
65
|
+
const hash = createHash('sha256');
|
|
66
|
+
await pipeline(createReadStream(path), hash);
|
|
67
|
+
return hash.digest('hex');
|
|
68
|
+
}
|
|
69
|
+
function causeOf(error) {
|
|
70
|
+
return error instanceof Error ? error.message : String(error);
|
|
71
|
+
}
|
|
72
|
+
async function failing(reason, work) {
|
|
73
|
+
try {
|
|
74
|
+
return await work;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw new Error(reason, { cause: error });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function ready() {
|
|
81
|
+
if (setting !== true)
|
|
82
|
+
throw new Error(OFF);
|
|
83
|
+
if (!readiness.installed)
|
|
84
|
+
throw new Error(NOT_INSTALLED);
|
|
85
|
+
}
|
|
86
|
+
function pinnedHere() {
|
|
87
|
+
return ENGINES[`${process.platform}-${process.arch}`];
|
|
88
|
+
}
|
|
89
|
+
async function install(artifacts, signal) {
|
|
90
|
+
await mkdir(directory(), { recursive: true });
|
|
91
|
+
const pinned = new Set(artifacts.map((artifact) => artifact.sha256));
|
|
92
|
+
for (const name of await readdir(directory())) {
|
|
93
|
+
if (!pinned.has(name))
|
|
94
|
+
await rm(join(directory(), name), { recursive: true, force: true });
|
|
95
|
+
}
|
|
96
|
+
const missing = [];
|
|
97
|
+
for (const artifact of artifacts)
|
|
98
|
+
if (!(await present(installed(artifact))))
|
|
99
|
+
missing.push(artifact);
|
|
100
|
+
try {
|
|
101
|
+
for (const artifact of missing) {
|
|
102
|
+
const part = `${installed(artifact)}.part`;
|
|
103
|
+
// An artifact arrives at a hundred kilobytes a second or its download ends, so a stalled host cannot hold the install open.
|
|
104
|
+
const deadlineMs = Math.ceil(artifact.bytes / 100);
|
|
105
|
+
const deadline = AbortSignal.timeout(deadlineMs);
|
|
106
|
+
// Three retries carry a large download past a dropped connection before the download fails.
|
|
107
|
+
try {
|
|
108
|
+
await executed('bash', ['-c', 'trap "" XFSZ && ulimit -f "$1" && exec curl -fsSL --retry 3 -o "$2" "$3"', 'bash', String(Math.ceil(artifact.bytes / 1024)), part, artifact.url], AbortSignal.any([signal, deadline]));
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
if (((await stat(part).catch(() => null))?.size ?? 0) >= artifact.bytes)
|
|
112
|
+
throw new Error(`${artifact.url} is larger than its pinned ${artifact.bytes} bytes`);
|
|
113
|
+
if (deadline.aborted && !signal.aborted)
|
|
114
|
+
throw new Error(`${artifact.url} did not arrive within ${Math.ceil(deadlineMs / 1000)} seconds`);
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
if ((await sha256(part)) !== artifact.sha256)
|
|
118
|
+
throw new Error(`${artifact.url} does not match its sha256`);
|
|
119
|
+
}
|
|
120
|
+
for (const artifact of missing) {
|
|
121
|
+
if (artifact.executable)
|
|
122
|
+
await chmod(`${installed(artifact)}.part`, 0o755);
|
|
123
|
+
await rename(`${installed(artifact)}.part`, installed(artifact));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
for (const artifact of missing)
|
|
128
|
+
await rm(`${installed(artifact)}.part`, { force: true });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function reported(next) {
|
|
132
|
+
const changed = next.installed !== readiness.installed || next.failure !== readiness.failure;
|
|
133
|
+
readiness = next;
|
|
134
|
+
return changed;
|
|
135
|
+
}
|
|
136
|
+
async function settle(transcribe) {
|
|
137
|
+
if (transcribe === setting)
|
|
138
|
+
return false;
|
|
139
|
+
setting = transcribe;
|
|
140
|
+
if (!transcribe) {
|
|
141
|
+
await rm(directory(), { recursive: true, force: true });
|
|
142
|
+
return reported({ installed: false, failure: null });
|
|
143
|
+
}
|
|
144
|
+
const download = new AbortController();
|
|
145
|
+
downloading = download;
|
|
146
|
+
try {
|
|
147
|
+
const pinned = pinnedHere();
|
|
148
|
+
await install([pinned.decoder, pinned.engine, MODEL], download.signal);
|
|
149
|
+
return reported({ installed: true, failure: null });
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
await rm(directory(), { recursive: true, force: true });
|
|
153
|
+
return reported({ installed: false, failure: download.signal.aborted ? null : causeOf(error) });
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
downloading = null;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export function holdTranscription(transcribe) {
|
|
160
|
+
if (!transcribe)
|
|
161
|
+
downloading?.abort();
|
|
162
|
+
const settled = holding.then(() => settle(transcribe));
|
|
163
|
+
holding = settled.catch(() => undefined);
|
|
164
|
+
return settled;
|
|
165
|
+
}
|
|
166
|
+
export function transcriptionReport() {
|
|
167
|
+
return readiness;
|
|
168
|
+
}
|
|
169
|
+
async function transcribed(audio, parts, signal) {
|
|
170
|
+
const pinned = pinnedHere();
|
|
171
|
+
await mkdir(parts);
|
|
172
|
+
await failing(UNDECODABLE, executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', audio, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
|
|
173
|
+
'-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(parts, '%05d.wav')], signal));
|
|
174
|
+
const segments = (await readdir(parts)).sort().flatMap((part) => ['-f', join(parts, part)]);
|
|
175
|
+
const said = await failing(UNTRANSCRIBED, executed(installed(pinned.engine), ['-t', String(await threads()), '-np', '-m', installed(MODEL), ...segments], signal));
|
|
176
|
+
const text = said
|
|
177
|
+
.split('\n')
|
|
178
|
+
.map((line) => line.trim())
|
|
179
|
+
.filter((line) => line !== '')
|
|
180
|
+
.join(' ');
|
|
181
|
+
if (text === '')
|
|
182
|
+
throw new Error(UNTRANSCRIBED);
|
|
183
|
+
return text;
|
|
184
|
+
}
|
|
185
|
+
async function alone(work, signal) {
|
|
186
|
+
const before = held;
|
|
187
|
+
let release;
|
|
188
|
+
const mine = new Promise((resolve) => {
|
|
189
|
+
release = resolve;
|
|
190
|
+
});
|
|
191
|
+
held = Promise.all([before, mine]);
|
|
192
|
+
try {
|
|
193
|
+
await unlessAborted(before, signal);
|
|
194
|
+
return await work();
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
release();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
export async function transcribe(address, signal) {
|
|
201
|
+
ready();
|
|
202
|
+
const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
|
|
203
|
+
try {
|
|
204
|
+
const audio = join(work, 'audio');
|
|
205
|
+
await failing(NOT_DOWNLOADED, downloaded(address, (url) => fetched(url, signal), audio, 'audio'));
|
|
206
|
+
return await alone(() => {
|
|
207
|
+
ready();
|
|
208
|
+
return transcribed(audio, join(work, 'parts'), signal);
|
|
209
|
+
}, signal);
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
await rm(work, { recursive: true, force: true });
|
|
213
|
+
}
|
|
214
|
+
}
|
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
|
-
|
|
7
|
-
|
|
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 = [];
|