@leavewhite9088/harness-bus 0.2.0 → 0.2.1
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/ARCHITECTURE.md +81 -81
- package/LICENSE +21 -21
- package/README.md +86 -86
- package/SETUP_FOR_AI.md +79 -79
- package/package.json +4 -2
- package/packages/agentd/agentd.config.json +3 -1
- package/packages/agentd/cli/commands.mjs +854 -810
- package/packages/agentd/cli/format.mjs +294 -238
- package/packages/agentd/cli/hbus.mjs +14 -14
- package/packages/agentd/cli/index.mjs +4 -4
- package/packages/agentd/cli/skill/SKILL.md +66 -61
- package/packages/agentd/cli/skill/references/custom.md +12 -0
- package/packages/agentd/cli/skill/references/playbooks.md +89 -86
- package/packages/agentd/cli/skill/references/routing.md +43 -35
- package/packages/agentd/cli/skill-install.mjs +157 -140
- package/packages/agentd/cli/update.mjs +221 -221
- package/packages/agentd/client/index.mjs +3 -3
- package/packages/agentd/contexts/executor.md +13 -12
- package/packages/agentd/contexts/templates/architect.md +12 -12
- package/packages/agentd/contexts/templates/coder.md +12 -12
- package/packages/agentd/contexts/templates/debugger.md +12 -12
- package/packages/agentd/contexts/templates/researcher.md +12 -12
- package/packages/agentd/contexts/templates/reviewer.md +12 -12
- package/packages/agentd/contexts/templates/tester.md +12 -12
- package/packages/agentd/core/base/data-dir.mjs +39 -39
- package/packages/agentd/core/base/index.mjs +5 -5
- package/packages/agentd/core/base/protocol.mjs +13 -13
- package/packages/agentd/core/env/config.mjs +52 -1
- package/packages/agentd/core/env/index.mjs +6 -1
- package/packages/agentd/core/env/install.mjs +82 -4
- package/packages/agentd/core/pi/index.mjs +1 -1
- package/packages/agentd/core/pi/rpc.mjs +206 -206
- package/packages/agentd/core/pool/acp/client.mjs +7 -9
- package/packages/agentd/core/pool/acp/process.mjs +27 -18
- package/packages/agentd/core/pool/acp/run-tracker.mjs +293 -293
- package/packages/agentd/core/pool/acp/sessions.mjs +465 -465
- package/packages/agentd/core/pool/acp/turn-runner.mjs +5 -3
- package/packages/agentd/core/pool/index.mjs +2 -2
- package/packages/agentd/core/pool/instances.mjs +3 -5
- package/packages/agentd/core/pool/probe.mjs +91 -85
- package/packages/agentd/core/pool/registry.mjs +45 -1
- package/packages/agentd/core/pool/support/compaction.mjs +2 -43
- package/packages/agentd/core/pool/support/index.mjs +1 -1
- package/packages/agentd/core/prompts/index.mjs +12 -12
- package/packages/agentd/core/prompts/rules.mjs +22 -22
- package/packages/agentd/core/prompts/templates.mjs +172 -172
- package/packages/agentd/core/providers/index.mjs +14 -14
- package/packages/agentd/core/providers/store.mjs +206 -206
- package/packages/agentd/core/records/cleanup.mjs +51 -51
- package/packages/agentd/core/records/index.mjs +8 -8
- package/packages/agentd/core/records/metrics.mjs +73 -73
- package/packages/agentd/core/records/recovery.mjs +39 -39
- package/packages/agentd/core/records/transcripts.mjs +122 -122
- package/packages/agentd/core/threads/index.mjs +18 -17
- package/packages/agentd/core/threads/manager.mjs +498 -476
- package/packages/agentd/core/threads/paths.mjs +78 -78
- package/packages/agentd/core/threads/store.mjs +146 -125
- package/packages/agentd/daemon/agentd.mjs +58 -58
- package/packages/agentd/daemon/events.mjs +126 -126
- package/packages/agentd/daemon/guard.mjs +127 -127
- package/packages/agentd/daemon/lifecycle.mjs +61 -61
- package/packages/agentd/daemon/ops/index.mjs +15 -15
- package/packages/agentd/daemon/ops/providers.mjs +46 -46
- package/packages/agentd/daemon/ops/run.mjs +413 -400
- package/packages/agentd/daemon/ops/system.mjs +76 -69
- package/packages/agentd/daemon/ops/threads.mjs +156 -153
- package/packages/agentd/daemon/server.mjs +130 -130
- package/packages/agentd/package.json +1 -1
- package/packages/agentd/pi-adapter/acp-adapter.mjs +501 -501
- package/packages/agentd/scripts/restart-daemon.mjs +74 -74
- package/packages/agentd/scripts/run-isolated.mjs +42 -42
- package/packages/agentd/scripts/run-unit.mjs +28 -28
- package/packages/backend/src/daemon.mjs +69 -69
- package/packages/backend/src/server.mjs +270 -270
- package/packages/drivers/{agy.mjs → agy/index.mjs} +2 -1
- package/packages/drivers/{claude.mjs → claude/index.mjs} +23 -1
- package/packages/drivers/{codex.mjs → codex/index.mjs} +2 -1
- package/packages/drivers/{dsh.mjs → dsh/index.mjs} +3 -2
- package/packages/drivers/grok/index.mjs +96 -0
- package/packages/drivers/index.mjs +25 -12
- package/packages/drivers/{kimi.mjs → kimi/index.mjs} +2 -1
- package/packages/drivers/{mcode.mjs → mcode/index.mjs} +2 -1
- package/packages/drivers/{opencode.mjs → opencode/index.mjs} +2 -1
- package/packages/drivers/{pi.mjs → pi/index.mjs} +3 -2
- package/packages/drivers/probes/acp.mjs +109 -109
- package/packages/drivers/probes/index.mjs +131 -129
- package/packages/drivers/probes/provider.mjs +222 -222
- package/packages/drivers/qoder/index.mjs +32 -0
- package/packages/drivers/{bin.mjs → shared/bin.mjs} +2 -0
- package/packages/drivers/shared/index.mjs +2 -0
- package/packages/preflight/bin/preflight.mjs +116 -116
- package/packages/preflight/src/index.mjs +5 -5
- package/packages/preflight/src/report.mjs +18 -1
- package/packages/web/dist/apple-touch-icon.png +0 -0
- package/packages/web/dist/assets/index-CT8dIwav.css +2 -0
- package/packages/web/dist/assets/index-CypTUMFB.js +79 -0
- package/packages/web/dist/favicon-32.png +0 -0
- package/packages/web/dist/favicon.svg +19 -15
- package/packages/web/dist/index.html +5 -5
- package/packages/web/dist/assets/index-B2kdkHNw.js +0 -79
- package/packages/web/dist/assets/index-DQTyjC4x.css +0 -2
- /package/packages/drivers/{dsh-sessions.mjs → dsh/sessions.mjs} +0 -0
- /package/packages/drivers/{pi-catalog.mjs → pi/catalog.mjs} +0 -0
- /package/packages/drivers/{lib.mjs → shared/lib.mjs} +0 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { binaryPath, openCodeHeaders, RULES_IN_PROMPT } from '../shared/index.mjs';
|
|
2
|
+
|
|
3
|
+
const BIN = 'grok';
|
|
4
|
+
|
|
5
|
+
function normalizeAnthropicBaseUrl(endpoint) {
|
|
6
|
+
const clean = String(endpoint ?? '').trim().replace(/\/+$/, '');
|
|
7
|
+
return /\/v1$/i.test(clean) ? clean : `${clean}/v1`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
type: 'grok',
|
|
12
|
+
label: 'Grok Build',
|
|
13
|
+
description: 'xAI Grok Build CLI; native ACP, supports custom OpenAI and Anthropic providers.',
|
|
14
|
+
authHint: '运行 "grok login" 登录、设置 XAI_API_KEY,或运行 "hbus ui" 配置第三方兼容渠道',
|
|
15
|
+
|
|
16
|
+
packages: [],
|
|
17
|
+
binaries: [{ name: BIN, version: '1.0.50' }],
|
|
18
|
+
verified: {
|
|
19
|
+
on: '2026-12-04',
|
|
20
|
+
note: 'ACP v1 over stdio, --always-approve, --no-leader, /compact, MCP servers, session/resume and GROK_HOME custom provider checked on 1.0.50',
|
|
21
|
+
},
|
|
22
|
+
capabilities: { compact: '/compact', rules: 'prompt', mcp: 'acp', mcpStart: 'eager' },
|
|
23
|
+
|
|
24
|
+
launch(install) {
|
|
25
|
+
const missing = [];
|
|
26
|
+
const cmd = binaryPath(install, BIN, missing);
|
|
27
|
+
return {
|
|
28
|
+
cmd,
|
|
29
|
+
args: ['agent', '--always-approve', '--no-leader', 'stdio'],
|
|
30
|
+
env: {
|
|
31
|
+
GROK_DISABLE_AUTOUPDATER: '1',
|
|
32
|
+
GROK_TELEMETRY_ENABLED: '0',
|
|
33
|
+
GROK_PROMPT_SUGGESTIONS: '0',
|
|
34
|
+
GROK_CLAUDE_MCPS_ENABLED: '0',
|
|
35
|
+
GROK_CURSOR_MCPS_ENABLED: '0',
|
|
36
|
+
},
|
|
37
|
+
missing,
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
provider: {
|
|
42
|
+
protocol: 'openai',
|
|
43
|
+
protocols: ['openai', 'anthropic'],
|
|
44
|
+
scope: 'process',
|
|
45
|
+
apply({ provider, endpoint, model, dir, protocol = 'openai' }) {
|
|
46
|
+
const models = [...new Set([...(provider.models?.length ? provider.models : []), ...(model ? [model] : [])].filter(Boolean))];
|
|
47
|
+
const chosen = model || models[0] || null;
|
|
48
|
+
const isAnthropic = protocol === 'anthropic';
|
|
49
|
+
const baseUrl = isAnthropic ? normalizeAnthropicBaseUrl(endpoint) : String(endpoint ?? '').trim().replace(/\/+$/, '');
|
|
50
|
+
const extraHeaders = Object.entries(openCodeHeaders(endpoint, `${provider.id}:${dir || ''}`));
|
|
51
|
+
const q = (s) => JSON.stringify(String(s));
|
|
52
|
+
const lines = [
|
|
53
|
+
'[models]',
|
|
54
|
+
...(chosen
|
|
55
|
+
? [
|
|
56
|
+
`default = ${q(chosen)}`,
|
|
57
|
+
`web_search = ${q(chosen)}`,
|
|
58
|
+
`session_summary = ${q(chosen)}`,
|
|
59
|
+
`prompt_suggestion = ${q(chosen)}`,
|
|
60
|
+
]
|
|
61
|
+
: []),
|
|
62
|
+
'',
|
|
63
|
+
...models.flatMap((m) => {
|
|
64
|
+
const ctx = Number(provider.contexts?.[m]);
|
|
65
|
+
return [
|
|
66
|
+
`[model.${q(m)}]`,
|
|
67
|
+
`model = ${q(m)}`,
|
|
68
|
+
`base_url = ${q(baseUrl)}`,
|
|
69
|
+
'env_key = "SCAFFOLD_PROVIDER_KEY"',
|
|
70
|
+
`api_backend = ${q(isAnthropic ? 'messages' : 'chat_completions')}`,
|
|
71
|
+
`context_window = ${Number.isFinite(ctx) && ctx >= 1000 ? Math.round(ctx) : 200000}`,
|
|
72
|
+
...(extraHeaders.length
|
|
73
|
+
? [`extra_headers = { ${extraHeaders.map(([k, v]) => `${q(k)} = ${q(v)}`).join(', ')} }`]
|
|
74
|
+
: []),
|
|
75
|
+
'',
|
|
76
|
+
];
|
|
77
|
+
}),
|
|
78
|
+
];
|
|
79
|
+
return {
|
|
80
|
+
env: {
|
|
81
|
+
GROK_HOME: dir,
|
|
82
|
+
SCAFFOLD_PROVIDER_KEY: provider.apiKey,
|
|
83
|
+
XAI_API_KEY: provider.apiKey,
|
|
84
|
+
...(chosen ? { GROK_DEFAULT_MODEL: chosen } : {}),
|
|
85
|
+
},
|
|
86
|
+
args: [],
|
|
87
|
+
files: [{ name: 'config.toml', content: lines.join('\n') }],
|
|
88
|
+
model: chosen,
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
rules() {
|
|
94
|
+
return RULES_IN_PROMPT;
|
|
95
|
+
},
|
|
96
|
+
};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import agy from './agy.mjs';
|
|
2
|
-
import claude from './claude.mjs';
|
|
3
|
-
import codex from './codex.mjs';
|
|
4
|
-
import dsh from './dsh.mjs';
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
1
|
+
import agy from './agy/index.mjs';
|
|
2
|
+
import claude from './claude/index.mjs';
|
|
3
|
+
import codex from './codex/index.mjs';
|
|
4
|
+
import dsh from './dsh/index.mjs';
|
|
5
|
+
import grok from './grok/index.mjs';
|
|
6
|
+
import kimi from './kimi/index.mjs';
|
|
7
|
+
import mcode from './mcode/index.mjs';
|
|
8
|
+
import opencode from './opencode/index.mjs';
|
|
9
|
+
import pi from './pi/index.mjs';
|
|
10
|
+
import qoder from './qoder/index.mjs';
|
|
9
11
|
|
|
10
|
-
export const drivers = { mcode, dsh, opencode, claude, codex, pi, kimi, agy };
|
|
12
|
+
export const drivers = { mcode, dsh, opencode, claude, codex, pi, kimi, agy, qoder, grok };
|
|
11
13
|
export const driverTypes = Object.keys(drivers);
|
|
12
14
|
export const getDriver = (type) => drivers[type] ?? null;
|
|
13
15
|
|
|
@@ -37,6 +39,17 @@ export function wantedBinaries() {
|
|
|
37
39
|
return [...byName.values()];
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
export { binaryEnvKey, defaultBinDirs, findBinary, findBinaryInDirs } from './
|
|
41
|
-
export {
|
|
42
|
-
|
|
42
|
+
export { binaryEnvKey, defaultBinDirs, findBinary, findBinaryInDirs } from './shared/index.mjs';
|
|
43
|
+
export {
|
|
44
|
+
FAKE_MCP_PATH,
|
|
45
|
+
PROBE_IDS,
|
|
46
|
+
applyProvider,
|
|
47
|
+
checkEndpoint,
|
|
48
|
+
discoverModels,
|
|
49
|
+
probeDriver,
|
|
50
|
+
providerProtocolOf,
|
|
51
|
+
removeGlobalProvider,
|
|
52
|
+
scrub,
|
|
53
|
+
syncGlobalProvider,
|
|
54
|
+
testProvider,
|
|
55
|
+
} from './probes/index.mjs';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
-
import { openCodeHeaders, RULES_IN_PROMPT, packageDir } from '
|
|
4
|
+
import { openCodeHeaders, RULES_IN_PROMPT, packageDir } from '../shared/index.mjs';
|
|
5
5
|
|
|
6
6
|
const PKG = '@moonshot-ai/kimi-code';
|
|
7
7
|
|
|
@@ -37,6 +37,7 @@ export default {
|
|
|
37
37
|
type: 'kimi',
|
|
38
38
|
label: 'Kimi Code',
|
|
39
39
|
description: 'Moonshot AI coding agent; native ACP.',
|
|
40
|
+
authHint: '运行 "kimi" 登录 Kimi 账号,或运行 "hbus ui" 配置第三方兼容渠道',
|
|
40
41
|
|
|
41
42
|
packages: [{ name: PKG, version: '2.6.3' }],
|
|
42
43
|
verified: { on: '2026-11-18', note: 'ACP v1 over stdio, rules in first prompt, MCP servers, /compact and custom provider checked on 2.6.3 (earlier 2.4.1)' },
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
-
import { NO_RULES, RULES_IN_PROMPT, packageDir } from '
|
|
4
|
+
import { NO_RULES, RULES_IN_PROMPT, packageDir } from '../shared/index.mjs';
|
|
5
5
|
|
|
6
6
|
const PKG = '@minimax-ai/code';
|
|
7
7
|
|
|
@@ -59,6 +59,7 @@ export default {
|
|
|
59
59
|
type: 'mcode',
|
|
60
60
|
label: 'MiniMax Code',
|
|
61
61
|
description: 'MiniMax Code agent. Slower to start, strong at multi-step coding.',
|
|
62
|
+
authHint: '运行 "mcode" 登录 MiniMax 账号,或运行 "hbus ui" 配置第三方兼容渠道',
|
|
62
63
|
|
|
63
64
|
packages: [{ name: PKG, version: '0.7.7' }],
|
|
64
65
|
verified: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { exe, NO_RULES, RULES_IN_PROMPT, packageDir } from '
|
|
1
|
+
import { exe, NO_RULES, RULES_IN_PROMPT, packageDir } from '../shared/index.mjs';
|
|
2
2
|
|
|
3
3
|
const PKG = 'opencode-ai';
|
|
4
4
|
|
|
@@ -6,6 +6,7 @@ export default {
|
|
|
6
6
|
type: 'opencode',
|
|
7
7
|
label: 'OpenCode',
|
|
8
8
|
description: 'OpenCode. Speaks ACP natively; many providers.',
|
|
9
|
+
authHint: '运行 "opencode" 完成模型登录授权,或运行 "hbus ui" 配置第三方兼容渠道',
|
|
9
10
|
|
|
10
11
|
packages: [{ name: PKG, version: '1.19.6' }],
|
|
11
12
|
verified: { on: '2026-11-18', note: 'rules, MCP servers and /compact checked on 1.19.6 (earlier 1.19.1)' },
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { NO_RULES, RULES_IN_PROMPT, packageDir } from '
|
|
2
|
-
import { modelProfileFor } from './
|
|
1
|
+
import { NO_RULES, RULES_IN_PROMPT, packageDir } from '../shared/index.mjs';
|
|
2
|
+
import { modelProfileFor } from './catalog.mjs';
|
|
3
3
|
|
|
4
4
|
const PKG = '@earendil-works/pi-coding-agent';
|
|
5
5
|
|
|
@@ -19,6 +19,7 @@ export default {
|
|
|
19
19
|
description:
|
|
20
20
|
'A separate pi instance, exposed through an ACP adapter over pi RPC mode. ' +
|
|
21
21
|
'Runs with --no-mcp so it cannot call back into this server.',
|
|
22
|
+
authHint: '运行 "hbus ui" 配置第三方兼容渠道,或在 ~/.pi/agent 中配置凭证',
|
|
22
23
|
|
|
23
24
|
packages: [{ name: PKG, version: '1.5.2' }],
|
|
24
25
|
verified: { on: '2026-11-18', note: 'rpc mode, -e builtin:mcp with registerMcpServer, rules, /compact and project dispatcher checked on 1.5.2 (earlier 1.3.0)' },
|
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
-
|
|
3
|
-
export class AcpProcess {
|
|
4
|
-
constructor(child) {
|
|
5
|
-
this.child = child;
|
|
6
|
-
this.pending = new Map();
|
|
7
|
-
this.nextId = 1;
|
|
8
|
-
this.buf = '';
|
|
9
|
-
this.text = '';
|
|
10
|
-
this.stderr = [];
|
|
11
|
-
this.exited = false;
|
|
12
|
-
child.stdout.setEncoding('utf8');
|
|
13
|
-
child.stdout.on('data', (c) => this._onData(c));
|
|
14
|
-
child.stderr.setEncoding('utf8');
|
|
15
|
-
child.stderr.on('data', (d) => {
|
|
16
|
-
this.stderr.push(...d.split('\n').map((l) => l.replace(/\x1b\[[0-9;]*m/g, '').trim()).filter(Boolean));
|
|
17
|
-
this.stderr = this.stderr.slice(-6);
|
|
18
|
-
});
|
|
19
|
-
child.on('close', () => {
|
|
20
|
-
this.exited = true;
|
|
21
|
-
for (const p of this.pending.values()) p.resolve({ error: { message: 'the process ended' } });
|
|
22
|
-
this.pending.clear();
|
|
23
|
-
});
|
|
24
|
-
child.on('error', (e) => this.stderr.push(`spawn error: ${e.message}`));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
static start({ cmd, args, env, cwd }) {
|
|
28
|
-
return new AcpProcess(spawn(cmd, args, { stdio: ['pipe', 'pipe', 'pipe'], detached: process.platform !== 'win32', windowsHide: true, env, cwd }));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
_onData(chunk) {
|
|
32
|
-
this.buf += chunk;
|
|
33
|
-
let i;
|
|
34
|
-
while ((i = this.buf.indexOf('\n')) !== -1) {
|
|
35
|
-
const line = this.buf.slice(0, i).trim();
|
|
36
|
-
this.buf = this.buf.slice(i + 1);
|
|
37
|
-
if (!line) continue;
|
|
38
|
-
let msg;
|
|
39
|
-
try {
|
|
40
|
-
msg = JSON.parse(line);
|
|
41
|
-
} catch {
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
// Method-first routing prevents bidirectional JSON-RPC id collisions (server requests may reuse id=1).
|
|
45
|
-
if (typeof msg.method === 'string') {
|
|
46
|
-
if (msg.method === 'session/update') {
|
|
47
|
-
const u = msg.params?.update;
|
|
48
|
-
if (u?.sessionUpdate === 'agent_message_chunk' && u.content?.text) this.text += u.content.text;
|
|
49
|
-
} else if (msg.method === 'session/request_permission' && msg.id != null) {
|
|
50
|
-
const opts = msg.params?.options ?? [];
|
|
51
|
-
const pick = opts.find((o) => /allow/i.test(o.kind ?? o.optionId ?? '')) ?? opts[0];
|
|
52
|
-
this._write({ jsonrpc: '2.0', id: msg.id, result: { outcome: pick ? { outcome: 'selected', optionId: pick.optionId } : { outcome: 'cancelled' } } });
|
|
53
|
-
}
|
|
54
|
-
} else if (msg.id != null && this.pending.has(msg.id)) {
|
|
55
|
-
const p = this.pending.get(msg.id);
|
|
56
|
-
this.pending.delete(msg.id);
|
|
57
|
-
p.resolve(msg);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
_write(obj) {
|
|
63
|
-
try {
|
|
64
|
-
this.child.stdin.write(JSON.stringify(obj) + '\n');
|
|
65
|
-
} catch {
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
request(method, params, ms = 60_000) {
|
|
70
|
-
return new Promise((resolve) => {
|
|
71
|
-
const id = this.nextId++;
|
|
72
|
-
const timer = setTimeout(() => {
|
|
73
|
-
this.pending.delete(id);
|
|
74
|
-
resolve({ error: { message: `no answer to ${method} within ${Math.round(ms / 1000)}s` } });
|
|
75
|
-
}, ms);
|
|
76
|
-
this.pending.set(id, { resolve: (m) => (clearTimeout(timer), resolve(m)) });
|
|
77
|
-
this._write({ jsonrpc: '2.0', id, method, params });
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async prompt(sessionId, text, ms = 240_000) {
|
|
82
|
-
this.text = '';
|
|
83
|
-
const r = await this.request('session/prompt', { sessionId, prompt: [{ type: 'text', text }] }, ms);
|
|
84
|
-
return { text: this.text, stop: r.result?.stopReason ?? null, error: r.error?.message ?? null };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async stop() {
|
|
88
|
-
if (this.exited) return;
|
|
89
|
-
try {
|
|
90
|
-
this.child.stdin.end();
|
|
91
|
-
} catch {
|
|
92
|
-
}
|
|
93
|
-
if (process.platform === 'win32' && this.child.pid) {
|
|
94
|
-
spawnSync('taskkill', ['/PID', String(this.child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true });
|
|
95
|
-
} else if (this.child.pid) {
|
|
96
|
-
try {
|
|
97
|
-
process.kill(-this.child.pid, 'SIGKILL');
|
|
98
|
-
} catch {
|
|
99
|
-
try {
|
|
100
|
-
this.child.kill('SIGKILL');
|
|
101
|
-
} catch {
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
} else {
|
|
105
|
-
this.child.kill('SIGKILL');
|
|
106
|
-
}
|
|
107
|
-
await new Promise((r) => setTimeout(r, 800));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
export class AcpProcess {
|
|
4
|
+
constructor(child) {
|
|
5
|
+
this.child = child;
|
|
6
|
+
this.pending = new Map();
|
|
7
|
+
this.nextId = 1;
|
|
8
|
+
this.buf = '';
|
|
9
|
+
this.text = '';
|
|
10
|
+
this.stderr = [];
|
|
11
|
+
this.exited = false;
|
|
12
|
+
child.stdout.setEncoding('utf8');
|
|
13
|
+
child.stdout.on('data', (c) => this._onData(c));
|
|
14
|
+
child.stderr.setEncoding('utf8');
|
|
15
|
+
child.stderr.on('data', (d) => {
|
|
16
|
+
this.stderr.push(...d.split('\n').map((l) => l.replace(/\x1b\[[0-9;]*m/g, '').trim()).filter(Boolean));
|
|
17
|
+
this.stderr = this.stderr.slice(-6);
|
|
18
|
+
});
|
|
19
|
+
child.on('close', () => {
|
|
20
|
+
this.exited = true;
|
|
21
|
+
for (const p of this.pending.values()) p.resolve({ error: { message: 'the process ended' } });
|
|
22
|
+
this.pending.clear();
|
|
23
|
+
});
|
|
24
|
+
child.on('error', (e) => this.stderr.push(`spawn error: ${e.message}`));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static start({ cmd, args, env, cwd }) {
|
|
28
|
+
return new AcpProcess(spawn(cmd, args, { stdio: ['pipe', 'pipe', 'pipe'], detached: process.platform !== 'win32', windowsHide: true, env, cwd }));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_onData(chunk) {
|
|
32
|
+
this.buf += chunk;
|
|
33
|
+
let i;
|
|
34
|
+
while ((i = this.buf.indexOf('\n')) !== -1) {
|
|
35
|
+
const line = this.buf.slice(0, i).trim();
|
|
36
|
+
this.buf = this.buf.slice(i + 1);
|
|
37
|
+
if (!line) continue;
|
|
38
|
+
let msg;
|
|
39
|
+
try {
|
|
40
|
+
msg = JSON.parse(line);
|
|
41
|
+
} catch {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// Method-first routing prevents bidirectional JSON-RPC id collisions (server requests may reuse id=1).
|
|
45
|
+
if (typeof msg.method === 'string') {
|
|
46
|
+
if (msg.method === 'session/update') {
|
|
47
|
+
const u = msg.params?.update;
|
|
48
|
+
if (u?.sessionUpdate === 'agent_message_chunk' && u.content?.text) this.text += u.content.text;
|
|
49
|
+
} else if (msg.method === 'session/request_permission' && msg.id != null) {
|
|
50
|
+
const opts = msg.params?.options ?? [];
|
|
51
|
+
const pick = opts.find((o) => /allow/i.test(o.kind ?? o.optionId ?? '')) ?? opts[0];
|
|
52
|
+
this._write({ jsonrpc: '2.0', id: msg.id, result: { outcome: pick ? { outcome: 'selected', optionId: pick.optionId } : { outcome: 'cancelled' } } });
|
|
53
|
+
}
|
|
54
|
+
} else if (msg.id != null && this.pending.has(msg.id)) {
|
|
55
|
+
const p = this.pending.get(msg.id);
|
|
56
|
+
this.pending.delete(msg.id);
|
|
57
|
+
p.resolve(msg);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_write(obj) {
|
|
63
|
+
try {
|
|
64
|
+
this.child.stdin.write(JSON.stringify(obj) + '\n');
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
request(method, params, ms = 60_000) {
|
|
70
|
+
return new Promise((resolve) => {
|
|
71
|
+
const id = this.nextId++;
|
|
72
|
+
const timer = setTimeout(() => {
|
|
73
|
+
this.pending.delete(id);
|
|
74
|
+
resolve({ error: { message: `no answer to ${method} within ${Math.round(ms / 1000)}s` } });
|
|
75
|
+
}, ms);
|
|
76
|
+
this.pending.set(id, { resolve: (m) => (clearTimeout(timer), resolve(m)) });
|
|
77
|
+
this._write({ jsonrpc: '2.0', id, method, params });
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async prompt(sessionId, text, ms = 240_000) {
|
|
82
|
+
this.text = '';
|
|
83
|
+
const r = await this.request('session/prompt', { sessionId, prompt: [{ type: 'text', text }] }, ms);
|
|
84
|
+
return { text: this.text, stop: r.result?.stopReason ?? null, error: r.error?.message ?? null };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async stop() {
|
|
88
|
+
if (this.exited) return;
|
|
89
|
+
try {
|
|
90
|
+
this.child.stdin.end();
|
|
91
|
+
} catch {
|
|
92
|
+
}
|
|
93
|
+
if (process.platform === 'win32' && this.child.pid) {
|
|
94
|
+
spawnSync('taskkill', ['/PID', String(this.child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true });
|
|
95
|
+
} else if (this.child.pid) {
|
|
96
|
+
try {
|
|
97
|
+
process.kill(-this.child.pid, 'SIGKILL');
|
|
98
|
+
} catch {
|
|
99
|
+
try {
|
|
100
|
+
this.child.kill('SIGKILL');
|
|
101
|
+
} catch {
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
this.child.kill('SIGKILL');
|
|
106
|
+
}
|
|
107
|
+
await new Promise((r) => setTimeout(r, 800));
|
|
108
|
+
}
|
|
109
|
+
}
|