@1agents/acp-service 0.10.8 → 0.10.9
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1agents/acp-service",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.9",
|
|
4
4
|
"description": "agent-workflow ACP runtime, CLI, HTTP/WebSocket service and DeepSeek Harness ACP/A2A plugins in one package. Part of 一芥天地 (DreamMate World).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dreammate",
|
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
- id: oneagents-acp
|
|
5
5
|
name: '@1agents/acp-service'
|
|
6
6
|
config:
|
|
7
|
-
# The ACP daemon is a
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# `acp-service serve --host 127.0.0.1 --port 36812 --no-report`
|
|
11
|
-
|
|
7
|
+
# The ACP daemon is a shared local service. `launchd` wakes the registered
|
|
8
|
+
# service when nothing answers, then leaves its lifecycle to the service
|
|
9
|
+
# manager, so no DSH host owns it. Install the LaunchAgent once:
|
|
10
|
+
# `acp-service serve --host 127.0.0.1 --port 36812 --no-report` under
|
|
11
|
+
# label com.1agents.acp-service.
|
|
12
|
+
serviceMode: launchd
|
|
13
|
+
serviceManagerLabel: com.1agents.acp-service
|
|
12
14
|
serviceUrl: http://127.0.0.1:36812
|
|
13
15
|
reconnectAttempts: 5
|
|
14
16
|
reconnectDelayMs: 1000
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
export interface ServiceOptions {
|
|
2
2
|
serviceUrl: string;
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* `external`: connect only; the daemon has its own owner.
|
|
5
|
+
* `auto`: connect, or fork a child that this host owns and stops on dispose.
|
|
6
|
+
* `launchd`: connect, or wake a registered service manager that keeps the process
|
|
7
|
+
* alive on its own; this host never owns or stops it.
|
|
8
|
+
*/
|
|
9
|
+
serviceMode: 'auto' | 'external' | 'launchd';
|
|
4
10
|
serviceStartupTimeoutMs: number;
|
|
5
11
|
serviceShutdownTimeoutMs: number;
|
|
12
|
+
/** Service manager label woken in `launchd` mode. */
|
|
13
|
+
serviceManagerLabel: string;
|
|
6
14
|
}
|
|
7
15
|
/** Validate lifecycle options before any process or connection is created. */
|
|
8
16
|
export declare function resolveServiceOptions(input: Partial<ServiceOptions>): ServiceOptions;
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
/** Own local ACP service processes; existing and remote servers keep their external owner. */
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
2
3
|
import { fork } from 'node:child_process';
|
|
4
|
+
import { access } from 'node:fs/promises';
|
|
3
5
|
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { promisify } from 'node:util';
|
|
7
|
+
const execFileAsync = promisify(execFile);
|
|
4
8
|
const leases = new Map();
|
|
9
|
+
const MODES = ['auto', 'external', 'launchd'];
|
|
10
|
+
/** launchd labels are reverse-DNS-ish; keep this strict because it reaches launchctl. */
|
|
11
|
+
const LABEL = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
5
12
|
/** Validate lifecycle options before any process or connection is created. */
|
|
6
13
|
export function resolveServiceOptions(input) {
|
|
7
14
|
const result = {
|
|
@@ -11,12 +18,15 @@ export function resolveServiceOptions(input) {
|
|
|
11
18
|
serviceMode: input.serviceMode ?? 'external',
|
|
12
19
|
serviceStartupTimeoutMs: input.serviceStartupTimeoutMs ?? 15_000,
|
|
13
20
|
serviceShutdownTimeoutMs: input.serviceShutdownTimeoutMs ?? 5_000,
|
|
21
|
+
serviceManagerLabel: input.serviceManagerLabel ?? 'com.1agents.acp-service',
|
|
14
22
|
};
|
|
15
23
|
const url = new URL(result.serviceUrl);
|
|
16
24
|
if (!['http:', 'https:'].includes(url.protocol))
|
|
17
25
|
throw new Error('serviceUrl must use http or https');
|
|
18
|
-
if (!
|
|
19
|
-
throw new Error(
|
|
26
|
+
if (!MODES.includes(result.serviceMode))
|
|
27
|
+
throw new Error(`serviceMode must be one of ${MODES.join(', ')}`);
|
|
28
|
+
if (!LABEL.test(result.serviceManagerLabel))
|
|
29
|
+
throw new Error('serviceManagerLabel must be a plain launchd label');
|
|
20
30
|
for (const value of [result.serviceStartupTimeoutMs, result.serviceShutdownTimeoutMs]) {
|
|
21
31
|
if (!Number.isSafeInteger(value) || value < 1 || value > 2_147_483_647)
|
|
22
32
|
throw new Error('ACP service timeouts must be integers between 1 and 2147483647');
|
|
@@ -52,6 +62,60 @@ async function running(url, timeout) {
|
|
|
52
62
|
}
|
|
53
63
|
return true;
|
|
54
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Wake a service manager that owns the process, then wait for it to answer.
|
|
67
|
+
*
|
|
68
|
+
* The host only asks for the service to exist; the manager keeps it alive and
|
|
69
|
+
* restarts it, so this host's exit never stops it. Nothing here is recorded for
|
|
70
|
+
* shutdown, which is what separates this from `auto`.
|
|
71
|
+
*/
|
|
72
|
+
async function wake(options, url) {
|
|
73
|
+
if (await running(url, options.serviceStartupTimeoutMs))
|
|
74
|
+
return async () => { };
|
|
75
|
+
const domain = `gui/${process.getuid?.() ?? ''}`;
|
|
76
|
+
const target = `${domain}/${options.serviceManagerLabel}`;
|
|
77
|
+
// `kickstart` only reaches a service the domain already knows. An installed but
|
|
78
|
+
// unloaded agent still has its plist, so bootstrap it first and kickstart after.
|
|
79
|
+
const started = await execFileAsync('launchctl', ['kickstart', '-p', target], { timeout: options.serviceStartupTimeoutMs })
|
|
80
|
+
.then(() => true, () => false);
|
|
81
|
+
if (!started && !await bootstrap(options, domain)) {
|
|
82
|
+
throw new Error(`ACP service manager ${options.serviceManagerLabel} is neither running nor loadable; install its launchd plist first`);
|
|
83
|
+
}
|
|
84
|
+
// A manager may report success before the port is accepting; poll until healthy.
|
|
85
|
+
const deadline = Date.now() + options.serviceStartupTimeoutMs;
|
|
86
|
+
for (;;) {
|
|
87
|
+
try {
|
|
88
|
+
if (await running(url, Math.max(1_000, deadline - Date.now())))
|
|
89
|
+
return async () => { };
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (Date.now() >= deadline)
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
if (Date.now() >= deadline)
|
|
96
|
+
throw new Error(`ACP service at ${url.origin} did not become healthy after the service manager started`);
|
|
97
|
+
await new Promise(resolve => setTimeout(resolve, 250));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Load the agent from the user's LaunchAgents directory; false when no plist is installed. */
|
|
101
|
+
async function bootstrap(options, domain) {
|
|
102
|
+
const home = process.env.HOME;
|
|
103
|
+
if (!home)
|
|
104
|
+
return false;
|
|
105
|
+
const plist = `${home}/Library/LaunchAgents/${options.serviceManagerLabel}.plist`;
|
|
106
|
+
try {
|
|
107
|
+
await access(plist);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const loaded = await execFileAsync('launchctl', ['bootstrap', domain, plist], { timeout: options.serviceStartupTimeoutMs })
|
|
113
|
+
.then(() => true, () => false);
|
|
114
|
+
// Bootstrap can report failure for an agent that is already loaded; kickstart decides.
|
|
115
|
+
await execFileAsync('launchctl', ['kickstart', '-p', `${domain}/${options.serviceManagerLabel}`], { timeout: options.serviceStartupTimeoutMs })
|
|
116
|
+
.catch(() => { });
|
|
117
|
+
return loaded || await running(new URL(options.serviceUrl), 2_000).catch(() => false);
|
|
118
|
+
}
|
|
55
119
|
async function start(options, url) {
|
|
56
120
|
if (await running(url, options.serviceStartupTimeoutMs))
|
|
57
121
|
return async () => { };
|
|
@@ -120,7 +184,9 @@ export async function acquireService(options) {
|
|
|
120
184
|
return acquireService(options);
|
|
121
185
|
}
|
|
122
186
|
if (!lease) {
|
|
123
|
-
|
|
187
|
+
// `launchd` wakes the manager once per origin too; its release is a no-op, so
|
|
188
|
+
// sharing one lease never hands the process to this host.
|
|
189
|
+
lease = { users: 0, ready: options.serviceMode === 'launchd' ? wake(options, url) : start(options, url) };
|
|
124
190
|
leases.set(key, lease);
|
|
125
191
|
}
|
|
126
192
|
lease.users++;
|