@1agents/acp-service 0.1.0 → 0.1.2
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/dist/bin/acp-service.js +128 -19
- package/package.json +1 -1
package/dist/bin/acp-service.js
CHANGED
|
@@ -1,7 +1,81 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import fs from 'node:fs';
|
|
2
5
|
import { serveAcpService } from '../src/server.js';
|
|
3
6
|
import { attachProcessSignalHandlers } from '../src/bridge.js';
|
|
4
|
-
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const rawArgs = process.argv.slice(2);
|
|
9
|
+
const invokedBin = 'acp-service';
|
|
10
|
+
function getAcpxCliPath() {
|
|
11
|
+
return require.resolve('@scottzx/1acp/dist/cli.js');
|
|
12
|
+
}
|
|
13
|
+
function getPackageVersion() {
|
|
14
|
+
try {
|
|
15
|
+
const pkgPath = new URL('../package.json', import.meta.url);
|
|
16
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
17
|
+
return pkg.version || '0.1.1';
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return '0.1.1';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function printServeHelp(binName) {
|
|
24
|
+
console.log(`用法: ${binName} serve [选项]
|
|
25
|
+
|
|
26
|
+
启动 DreamMate Network ACP Agent 运行时常驻服务(HTTP + WebSocket)
|
|
27
|
+
|
|
28
|
+
选项:
|
|
29
|
+
--port, -p <n> 指定监听端口(默认: 36812)
|
|
30
|
+
--host <ip> 指定绑定 IP(默认: 0.0.0.0)
|
|
31
|
+
--no-report 不向本机 node agent (36908) 报备
|
|
32
|
+
-h, --help 显示帮助信息
|
|
33
|
+
`);
|
|
34
|
+
}
|
|
35
|
+
function printUnifiedHelp(binName) {
|
|
36
|
+
let baseHelp = '';
|
|
37
|
+
try {
|
|
38
|
+
const cliPath = getAcpxCliPath();
|
|
39
|
+
baseHelp = execFileSync(process.execPath, [cliPath, '--help'], { encoding: 'utf-8' });
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
baseHelp = String(err);
|
|
43
|
+
}
|
|
44
|
+
let help = baseHelp
|
|
45
|
+
.replace(/^Usage:\s+acpx/m, `Usage: ${binName}`)
|
|
46
|
+
.replace(/Headless CLI client for the Agent Client Protocol/, 'Headless CLI client & DreamMate Network runtime service for the Agent Client Protocol (ACP)');
|
|
47
|
+
const serverCommands = `Commands:
|
|
48
|
+
serve [options] Start DreamMate Network ACP runtime daemon (HTTP + WS)`;
|
|
49
|
+
help = help.replace(/^Commands:/m, serverCommands);
|
|
50
|
+
const serverOptions = `Options:
|
|
51
|
+
--port, -p <n> Server listen port (for serve, default: 36812)
|
|
52
|
+
--host <ip> Server bind IP (for serve, default: 0.0.0.0)
|
|
53
|
+
--no-report Do not report to local dreammate-node (36908)`;
|
|
54
|
+
help = help.replace(/^Options:/m, serverOptions);
|
|
55
|
+
help = help.replaceAll(/acpx /g, `${binName} `);
|
|
56
|
+
help = help.replace(/^Examples:\n/m, `Examples:\n ${binName} serve --port 36812\n`);
|
|
57
|
+
console.log(help);
|
|
58
|
+
}
|
|
59
|
+
function forwardToCli(args) {
|
|
60
|
+
const cliPath = getAcpxCliPath();
|
|
61
|
+
const child = spawn(process.execPath, [cliPath, ...args], {
|
|
62
|
+
stdio: 'inherit',
|
|
63
|
+
env: process.env,
|
|
64
|
+
});
|
|
65
|
+
child.on('exit', (code, signal) => {
|
|
66
|
+
if (signal) {
|
|
67
|
+
process.kill(process.pid, signal);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
process.exit(code ?? 0);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
child.on('error', (err) => {
|
|
74
|
+
console.error(`[${invokedBin}] 进程启动失败:`, err.message);
|
|
75
|
+
process.exit(1);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function parseServerArgs(args) {
|
|
5
79
|
const options = {};
|
|
6
80
|
for (let i = 0; i < args.length; i++) {
|
|
7
81
|
const arg = args[i];
|
|
@@ -10,32 +84,67 @@ function parseArgs(args) {
|
|
|
10
84
|
if (val)
|
|
11
85
|
options.port = Number.parseInt(val, 10);
|
|
12
86
|
}
|
|
13
|
-
else if (arg === '--host'
|
|
87
|
+
else if (arg === '--host') {
|
|
14
88
|
options.host = args[++i];
|
|
15
89
|
}
|
|
16
90
|
else if (arg === '--no-report') {
|
|
17
91
|
options.report = false;
|
|
18
92
|
}
|
|
19
|
-
else if (arg === '--help') {
|
|
20
|
-
console.log(`用法: acp-service [选项]
|
|
21
|
-
|
|
22
|
-
选项:
|
|
23
|
-
--port, -p <n> 指定监听端口(默认: 36812)
|
|
24
|
-
--host, -h <ip> 指定绑定 IP(默认: 0.0.0.0)
|
|
25
|
-
--no-report 不向本机 node agent (36908) 报备
|
|
26
|
-
--help 显示帮助信息
|
|
27
|
-
`);
|
|
28
|
-
process.exit(0);
|
|
29
|
-
}
|
|
30
93
|
}
|
|
31
94
|
return options;
|
|
32
95
|
}
|
|
33
|
-
|
|
34
|
-
try {
|
|
96
|
+
async function runServer(options) {
|
|
35
97
|
attachProcessSignalHandlers();
|
|
36
|
-
|
|
98
|
+
try {
|
|
99
|
+
await serveAcpService(options);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error(`[${invokedBin}] 启动失败:`, error instanceof Error ? error.message : error);
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
37
105
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
106
|
+
async function main() {
|
|
107
|
+
const isServe = rawArgs[0] === 'serve' || rawArgs[0] === 'run';
|
|
108
|
+
const hasServerExplicitFlag = rawArgs[0] === '--port' ||
|
|
109
|
+
rawArgs[0] === '-p' ||
|
|
110
|
+
rawArgs[0] === '--host' ||
|
|
111
|
+
rawArgs[0] === '--no-report';
|
|
112
|
+
// 1. Version
|
|
113
|
+
if (rawArgs[0] === '-V' || rawArgs[0] === '--version') {
|
|
114
|
+
const version = getPackageVersion();
|
|
115
|
+
console.log(`@1agents/acp-service v${version}`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
// 2. Help
|
|
119
|
+
if (rawArgs[0] === '--help' ||
|
|
120
|
+
(rawArgs[0] === '-h' && rawArgs.length === 1) ||
|
|
121
|
+
rawArgs[0] === 'help') {
|
|
122
|
+
printUnifiedHelp(invokedBin);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// 3. Serve command explicit
|
|
126
|
+
if (isServe) {
|
|
127
|
+
const serveArgs = rawArgs.slice(1);
|
|
128
|
+
if (serveArgs.includes('--help') || serveArgs.includes('-h')) {
|
|
129
|
+
printServeHelp(invokedBin);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const options = parseServerArgs(serveArgs);
|
|
133
|
+
await runServer(options);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
// 4. Server explicit flags e.g. `acp-service --port 36812`
|
|
137
|
+
if (hasServerExplicitFlag) {
|
|
138
|
+
const options = parseServerArgs(rawArgs);
|
|
139
|
+
await runServer(options);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
// 5. No arguments: default to running the server!
|
|
143
|
+
if (rawArgs.length === 0) {
|
|
144
|
+
await runServer({});
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
// 6. Any other subcommand or arguments -> forward directly to @scottzx/1acp CLI
|
|
148
|
+
forwardToCli(rawArgs);
|
|
41
149
|
}
|
|
150
|
+
await main();
|
package/package.json
CHANGED