@agent-relay/sdk 3.2.21 → 4.0.0
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 +10 -3
- package/bin/agent-relay-broker-darwin-arm64 +0 -0
- package/bin/agent-relay-broker-darwin-x64 +0 -0
- package/bin/agent-relay-broker-linux-arm64 +0 -0
- package/bin/agent-relay-broker-linux-x64 +0 -0
- package/dist/client.d.ts +108 -196
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +336 -824
- package/dist/client.js.map +1 -1
- package/dist/examples/example.js +2 -5
- package/dist/examples/example.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/relay-adapter.d.ts +9 -26
- package/dist/relay-adapter.d.ts.map +1 -1
- package/dist/relay-adapter.js +75 -47
- package/dist/relay-adapter.js.map +1 -1
- package/dist/relay.d.ts +24 -5
- package/dist/relay.d.ts.map +1 -1
- package/dist/relay.js +213 -43
- package/dist/relay.js.map +1 -1
- package/dist/transport.d.ts +58 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +184 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +69 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/workflows/cli.js +46 -2
- package/dist/workflows/cli.js.map +1 -1
- package/dist/workflows/file-db.d.ts +2 -0
- package/dist/workflows/file-db.d.ts.map +1 -1
- package/dist/workflows/file-db.js +20 -3
- package/dist/workflows/file-db.js.map +1 -1
- package/dist/workflows/runner.d.ts +6 -1
- package/dist/workflows/runner.d.ts.map +1 -1
- package/dist/workflows/runner.js +157 -11
- package/dist/workflows/runner.js.map +1 -1
- package/dist/workflows/validator.d.ts.map +1 -1
- package/dist/workflows/validator.js +17 -2
- package/dist/workflows/validator.js.map +1 -1
- package/package.json +2 -2
package/dist/client.js
CHANGED
|
@@ -1,211 +1,223 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* AgentRelayClient — single client for communicating with an agent-relay broker
|
|
3
|
+
* over HTTP/WS. Works identically for local and remote brokers.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* // Remote broker (Daytona sandbox, cloud, etc.)
|
|
7
|
+
* const client = new AgentRelayClient({ baseUrl, apiKey });
|
|
8
|
+
*
|
|
9
|
+
* // Local broker (spawn and connect)
|
|
10
|
+
* const client = await AgentRelayClient.spawn({ cwd: '/my/project' });
|
|
11
|
+
*/
|
|
12
|
+
import { spawn } from 'node:child_process';
|
|
13
|
+
import { randomBytes } from 'node:crypto';
|
|
14
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
6
15
|
import path from 'node:path';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { PROTOCOL_VERSION, } from './protocol.js';
|
|
10
|
-
export class AgentRelayProtocolError extends Error {
|
|
11
|
-
code;
|
|
12
|
-
retryable;
|
|
13
|
-
data;
|
|
14
|
-
constructor(payload) {
|
|
15
|
-
super(payload.message);
|
|
16
|
-
this.name = 'AgentRelayProtocolError';
|
|
17
|
-
this.code = payload.code;
|
|
18
|
-
this.retryable = payload.retryable;
|
|
19
|
-
this.data = payload.data;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export class AgentRelayProcessError extends Error {
|
|
23
|
-
constructor(message) {
|
|
24
|
-
super(message);
|
|
25
|
-
this.name = 'AgentRelayProcessError';
|
|
26
|
-
}
|
|
27
|
-
}
|
|
16
|
+
import { BrokerTransport, AgentRelayProtocolError } from './transport.js';
|
|
17
|
+
import { getBrokerBinaryPath } from './broker-path.js';
|
|
28
18
|
function isHeadlessProvider(value) {
|
|
29
19
|
return value === 'claude' || value === 'opencode';
|
|
30
20
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
requestSeq = 0;
|
|
38
|
-
pending = new Map();
|
|
39
|
-
startingPromise;
|
|
40
|
-
eventListeners = new Set();
|
|
41
|
-
stderrListeners = new Set();
|
|
42
|
-
eventBuffer = [];
|
|
43
|
-
maxBufferSize = 1000;
|
|
44
|
-
exitPromise;
|
|
45
|
-
/** The workspace key returned by the broker in its hello_ack response. */
|
|
46
|
-
workspaceKey;
|
|
47
|
-
constructor(options = {}) {
|
|
48
|
-
this.options = {
|
|
49
|
-
binaryPath: options.binaryPath ?? resolveDefaultBinaryPath(),
|
|
50
|
-
binaryArgs: options.binaryArgs ?? [],
|
|
51
|
-
brokerName: options.brokerName ?? (path.basename(options.cwd ?? process.cwd()) || 'project'),
|
|
52
|
-
channels: options.channels ?? ['general'],
|
|
53
|
-
cwd: options.cwd ?? process.cwd(),
|
|
54
|
-
env: options.env ?? process.env,
|
|
55
|
-
requestTimeoutMs: options.requestTimeoutMs ?? 10_000,
|
|
56
|
-
shutdownTimeoutMs: options.shutdownTimeoutMs ?? 3_000,
|
|
57
|
-
clientName: options.clientName ?? '@agent-relay/sdk',
|
|
58
|
-
clientVersion: options.clientVersion ?? '0.1.0',
|
|
59
|
-
};
|
|
21
|
+
function resolveSpawnTransport(input) {
|
|
22
|
+
return input.transport ?? (input.provider === 'opencode' ? 'headless' : 'pty');
|
|
23
|
+
}
|
|
24
|
+
function isProcessRunning(pid) {
|
|
25
|
+
if (!Number.isInteger(pid) || pid <= 0) {
|
|
26
|
+
return false;
|
|
60
27
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return client;
|
|
28
|
+
try {
|
|
29
|
+
process.kill(pid, 0);
|
|
30
|
+
return true;
|
|
65
31
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return () => {
|
|
69
|
-
this.eventListeners.delete(listener);
|
|
70
|
-
};
|
|
32
|
+
catch (error) {
|
|
33
|
+
return error.code === 'EPERM';
|
|
71
34
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
35
|
+
}
|
|
36
|
+
// ── Client ─────────────────────────────────────────────────────────────
|
|
37
|
+
export class AgentRelayClient {
|
|
38
|
+
transport;
|
|
39
|
+
/** Set after spawn() — the managed child process. */
|
|
40
|
+
child = null;
|
|
41
|
+
/** Lease renewal timer (only for spawned brokers). */
|
|
42
|
+
leaseTimer = null;
|
|
43
|
+
workspaceKey;
|
|
44
|
+
constructor(options) {
|
|
45
|
+
this.transport = new BrokerTransport({
|
|
46
|
+
baseUrl: options.baseUrl,
|
|
47
|
+
apiKey: options.apiKey,
|
|
48
|
+
requestTimeoutMs: options.requestTimeoutMs,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Connect to an already-running broker by reading its connection file.
|
|
53
|
+
*
|
|
54
|
+
* The broker writes `connection.json` to its data directory ({cwd}/.agent-relay/
|
|
55
|
+
* in persist mode). This method reads that file to get the URL and API key.
|
|
56
|
+
*
|
|
57
|
+
* @param cwd — project directory (default: process.cwd())
|
|
58
|
+
* @param connectionPath — explicit path to connection.json (overrides cwd)
|
|
59
|
+
*/
|
|
60
|
+
static connect(options) {
|
|
61
|
+
const cwd = options?.cwd ?? process.cwd();
|
|
62
|
+
const stateDir = process.env.AGENT_RELAY_STATE_DIR;
|
|
63
|
+
const connPath = options?.connectionPath ?? path.join(stateDir ?? path.join(cwd, '.agent-relay'), 'connection.json');
|
|
64
|
+
if (!existsSync(connPath)) {
|
|
65
|
+
throw new Error(`No running broker found (${connPath} does not exist). Start one with 'agent-relay up' or use AgentRelayClient.spawn().`);
|
|
76
66
|
}
|
|
77
|
-
|
|
78
|
-
|
|
67
|
+
const raw = readFileSync(connPath, 'utf-8');
|
|
68
|
+
let conn;
|
|
69
|
+
try {
|
|
70
|
+
conn = JSON.parse(raw);
|
|
79
71
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
events = events.filter((event) => 'timestamp' in event && typeof event.timestamp === 'number' && event.timestamp >= since);
|
|
72
|
+
catch {
|
|
73
|
+
throw new Error(`Corrupt broker connection file (${connPath}). Remove it and start the broker again.`);
|
|
83
74
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
events = events.slice(-limit);
|
|
75
|
+
if (typeof conn.url !== 'string' || typeof conn.api_key !== 'string' || typeof conn.pid !== 'number') {
|
|
76
|
+
throw new Error(`Invalid broker connection metadata in ${connPath}. Remove it and start the broker again.`);
|
|
87
77
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
getLastEvent(kind, name) {
|
|
91
|
-
for (let i = this.eventBuffer.length - 1; i >= 0; i -= 1) {
|
|
92
|
-
const event = this.eventBuffer[i];
|
|
93
|
-
if (event.kind === kind && (!name || ('name' in event && event.name === name))) {
|
|
94
|
-
return event;
|
|
95
|
-
}
|
|
78
|
+
if (!isProcessRunning(conn.pid)) {
|
|
79
|
+
throw new Error(`Stale broker connection file (${connPath}) points to dead pid ${conn.pid}. Start the broker with 'agent-relay up' or use AgentRelayClient.spawn().`);
|
|
96
80
|
}
|
|
97
|
-
return
|
|
81
|
+
return new AgentRelayClient({ baseUrl: conn.url, apiKey: conn.api_key });
|
|
98
82
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Spawn a local broker process and return a connected client.
|
|
85
|
+
*
|
|
86
|
+
* 1. Generates a random API key
|
|
87
|
+
* 2. Spawns the broker binary (attached)
|
|
88
|
+
* 3. Parses the API port from stdout
|
|
89
|
+
* 4. Connects HTTP/WS transport
|
|
90
|
+
* 5. Fetches session metadata
|
|
91
|
+
* 6. Starts event stream + lease renewal
|
|
92
|
+
*/
|
|
93
|
+
static async spawn(options) {
|
|
94
|
+
const binaryPath = options?.binaryPath ?? getBrokerBinaryPath() ?? 'agent-relay-broker';
|
|
95
|
+
const cwd = options?.cwd ?? process.cwd();
|
|
96
|
+
const brokerName = options?.brokerName ?? (path.basename(cwd) || 'project');
|
|
97
|
+
const channels = options?.channels ?? ['general'];
|
|
98
|
+
const timeoutMs = options?.startupTimeoutMs ?? 15_000;
|
|
99
|
+
const userArgs = options?.binaryArgs ?? [];
|
|
100
|
+
const apiKey = `br_${randomBytes(16).toString('hex')}`;
|
|
101
|
+
const env = {
|
|
102
|
+
...process.env,
|
|
103
|
+
...options?.env,
|
|
104
|
+
RELAY_BROKER_API_KEY: apiKey,
|
|
103
105
|
};
|
|
106
|
+
const args = ['init', '--name', brokerName, '--channels', channels.join(','), ...userArgs];
|
|
107
|
+
const child = spawn(binaryPath, args, {
|
|
108
|
+
cwd,
|
|
109
|
+
env,
|
|
110
|
+
stdio: ['ignore', 'pipe', options?.onStderr ? 'pipe' : 'ignore'],
|
|
111
|
+
});
|
|
112
|
+
// Forward stderr if requested
|
|
113
|
+
if (options?.onStderr && child.stderr) {
|
|
114
|
+
const { createInterface } = await import('node:readline');
|
|
115
|
+
const rl = createInterface({ input: child.stderr });
|
|
116
|
+
rl.on('line', (line) => options.onStderr(line));
|
|
117
|
+
}
|
|
118
|
+
// Parse the API URL from stdout (the broker prints it after binding)
|
|
119
|
+
const baseUrl = await waitForApiUrl(child, timeoutMs);
|
|
120
|
+
const client = new AgentRelayClient({
|
|
121
|
+
baseUrl,
|
|
122
|
+
apiKey,
|
|
123
|
+
requestTimeoutMs: options?.requestTimeoutMs,
|
|
124
|
+
});
|
|
125
|
+
client.child = child;
|
|
126
|
+
await client.getSession();
|
|
127
|
+
client.connectEvents();
|
|
128
|
+
// Renew the owner lease so the broker doesn't auto-shutdown
|
|
129
|
+
client.leaseTimer = setInterval(() => {
|
|
130
|
+
client.renewLease().catch(() => { });
|
|
131
|
+
}, 60_000);
|
|
132
|
+
child.on('exit', () => {
|
|
133
|
+
client.disconnectEvents();
|
|
134
|
+
if (client.leaseTimer) {
|
|
135
|
+
clearInterval(client.leaseTimer);
|
|
136
|
+
client.leaseTimer = null;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return client;
|
|
104
140
|
}
|
|
141
|
+
/** PID of the managed broker process, if spawned locally. */
|
|
105
142
|
get brokerPid() {
|
|
106
143
|
return this.child?.pid;
|
|
107
144
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return this.startingPromise;
|
|
114
|
-
}
|
|
115
|
-
this.startingPromise = this.startInternal();
|
|
116
|
-
try {
|
|
117
|
-
await this.startingPromise;
|
|
118
|
-
}
|
|
119
|
-
finally {
|
|
120
|
-
this.startingPromise = undefined;
|
|
121
|
-
}
|
|
145
|
+
// ── Session ────────────────────────────────────────────────────────
|
|
146
|
+
async getSession() {
|
|
147
|
+
const session = await this.transport.request('/api/session');
|
|
148
|
+
this.workspaceKey = session.workspace_key;
|
|
149
|
+
return session;
|
|
122
150
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
* The broker warms its token cache in parallel; subsequent spawn_agent calls
|
|
126
|
-
* hit the cache rather than waiting on individual HTTP registrations.
|
|
127
|
-
* Fire-and-forget from the caller's perspective — broker responds immediately
|
|
128
|
-
* and registers in the background.
|
|
129
|
-
*/
|
|
130
|
-
async preflightAgents(agents) {
|
|
131
|
-
if (agents.length === 0)
|
|
132
|
-
return;
|
|
133
|
-
await this.start();
|
|
134
|
-
await this.requestOk('preflight_agents', { agents });
|
|
151
|
+
async healthCheck() {
|
|
152
|
+
return this.transport.request('/health');
|
|
135
153
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const agent = {
|
|
140
|
-
name: input.name,
|
|
141
|
-
runtime: 'pty',
|
|
142
|
-
cli: input.cli,
|
|
143
|
-
args,
|
|
144
|
-
channels: input.channels ?? [],
|
|
145
|
-
model: input.model,
|
|
146
|
-
cwd: input.cwd ?? this.options.cwd,
|
|
147
|
-
team: input.team,
|
|
148
|
-
shadow_of: input.shadowOf,
|
|
149
|
-
shadow_mode: input.shadowMode,
|
|
150
|
-
restart_policy: input.restartPolicy,
|
|
151
|
-
};
|
|
152
|
-
const result = await this.requestOk('spawn_agent', {
|
|
153
|
-
agent,
|
|
154
|
-
...(input.task != null ? { initial_task: input.task } : {}),
|
|
155
|
-
...(input.idleThresholdSecs != null ? { idle_threshold_secs: input.idleThresholdSecs } : {}),
|
|
156
|
-
...(input.continueFrom != null ? { continue_from: input.continueFrom } : {}),
|
|
157
|
-
...(input.skipRelayPrompt != null ? { skip_relay_prompt: input.skipRelayPrompt } : {}),
|
|
158
|
-
});
|
|
159
|
-
return result;
|
|
154
|
+
// ── Events ─────────────────────────────────────────────────────────
|
|
155
|
+
connectEvents(sinceSeq) {
|
|
156
|
+
this.transport.connect(sinceSeq);
|
|
160
157
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
158
|
+
disconnectEvents() {
|
|
159
|
+
this.transport.disconnect();
|
|
160
|
+
}
|
|
161
|
+
onEvent(listener) {
|
|
162
|
+
return this.transport.onEvent(listener);
|
|
163
|
+
}
|
|
164
|
+
queryEvents(filter) {
|
|
165
|
+
return this.transport.queryEvents(filter);
|
|
166
|
+
}
|
|
167
|
+
getLastEvent(kind, name) {
|
|
168
|
+
return this.transport.getLastEvent(kind, name);
|
|
169
|
+
}
|
|
170
|
+
// ── Agent lifecycle ────────────────────────────────────────────────
|
|
171
|
+
async spawnPty(input) {
|
|
172
|
+
return this.transport.request('/api/spawn', {
|
|
173
|
+
method: 'POST',
|
|
174
|
+
body: JSON.stringify({
|
|
175
|
+
name: input.name,
|
|
176
|
+
cli: input.cli,
|
|
177
|
+
model: input.model,
|
|
178
|
+
args: input.args ?? [],
|
|
179
|
+
task: input.task,
|
|
180
|
+
channels: input.channels ?? [],
|
|
181
|
+
cwd: input.cwd,
|
|
182
|
+
team: input.team,
|
|
183
|
+
shadowOf: input.shadowOf,
|
|
184
|
+
shadowMode: input.shadowMode,
|
|
185
|
+
continueFrom: input.continueFrom,
|
|
186
|
+
idleThresholdSecs: input.idleThresholdSecs,
|
|
187
|
+
restartPolicy: input.restartPolicy,
|
|
188
|
+
skipRelayPrompt: input.skipRelayPrompt,
|
|
189
|
+
}),
|
|
174
190
|
});
|
|
175
|
-
return result;
|
|
176
191
|
}
|
|
177
192
|
async spawnProvider(input) {
|
|
178
|
-
const transport =
|
|
179
|
-
if (transport === 'headless') {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
193
|
+
const transport = resolveSpawnTransport(input);
|
|
194
|
+
if (transport === 'headless' && !isHeadlessProvider(input.provider)) {
|
|
195
|
+
throw new Error(`provider '${input.provider}' does not support headless transport (supported: claude, opencode)`);
|
|
196
|
+
}
|
|
197
|
+
return this.transport.request('/api/spawn', {
|
|
198
|
+
method: 'POST',
|
|
199
|
+
body: JSON.stringify({
|
|
184
200
|
name: input.name,
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
201
|
+
cli: input.provider,
|
|
202
|
+
model: input.model,
|
|
203
|
+
args: input.args ?? [],
|
|
188
204
|
task: input.task,
|
|
205
|
+
channels: input.channels ?? [],
|
|
206
|
+
cwd: input.cwd,
|
|
207
|
+
team: input.team,
|
|
208
|
+
shadowOf: input.shadowOf,
|
|
209
|
+
shadowMode: input.shadowMode,
|
|
210
|
+
continueFrom: input.continueFrom,
|
|
211
|
+
idleThresholdSecs: input.idleThresholdSecs,
|
|
212
|
+
restartPolicy: input.restartPolicy,
|
|
189
213
|
skipRelayPrompt: input.skipRelayPrompt,
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return this.spawnPty({
|
|
193
|
-
name: input.name,
|
|
194
|
-
cli: input.provider,
|
|
195
|
-
args: input.args,
|
|
196
|
-
channels: input.channels,
|
|
197
|
-
task: input.task,
|
|
198
|
-
model: input.model,
|
|
199
|
-
cwd: input.cwd,
|
|
200
|
-
team: input.team,
|
|
201
|
-
shadowOf: input.shadowOf,
|
|
202
|
-
shadowMode: input.shadowMode,
|
|
203
|
-
idleThresholdSecs: input.idleThresholdSecs,
|
|
204
|
-
restartPolicy: input.restartPolicy,
|
|
205
|
-
continueFrom: input.continueFrom,
|
|
206
|
-
skipRelayPrompt: input.skipRelayPrompt,
|
|
214
|
+
transport,
|
|
215
|
+
}),
|
|
207
216
|
});
|
|
208
217
|
}
|
|
218
|
+
async spawnHeadless(input) {
|
|
219
|
+
return this.spawnProvider({ ...input, transport: 'headless' });
|
|
220
|
+
}
|
|
209
221
|
async spawnClaude(input) {
|
|
210
222
|
return this.spawnProvider({ ...input, provider: 'claude' });
|
|
211
223
|
}
|
|
@@ -213,58 +225,44 @@ export class AgentRelayClient {
|
|
|
213
225
|
return this.spawnProvider({ ...input, provider: 'opencode' });
|
|
214
226
|
}
|
|
215
227
|
async release(name, reason) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
await this.start();
|
|
221
|
-
return this.requestOk('send_input', { name, data });
|
|
222
|
-
}
|
|
223
|
-
async subscribeChannels(name, channels) {
|
|
224
|
-
await this.start();
|
|
225
|
-
await this.requestOk('subscribe_channels', { name, channels });
|
|
228
|
+
return this.transport.request(`/api/spawned/${encodeURIComponent(name)}`, {
|
|
229
|
+
method: 'DELETE',
|
|
230
|
+
...(reason ? { body: JSON.stringify({ reason }) } : {}),
|
|
231
|
+
});
|
|
226
232
|
}
|
|
227
|
-
async
|
|
228
|
-
await this.
|
|
229
|
-
|
|
233
|
+
async listAgents() {
|
|
234
|
+
const result = await this.transport.request('/api/spawned');
|
|
235
|
+
return result.agents;
|
|
230
236
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return this.
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
cols,
|
|
237
|
+
// ── PTY control ────────────────────────────────────────────────────
|
|
238
|
+
async sendInput(name, data) {
|
|
239
|
+
return this.transport.request(`/api/input/${encodeURIComponent(name)}`, {
|
|
240
|
+
method: 'POST',
|
|
241
|
+
body: JSON.stringify({ data }),
|
|
237
242
|
});
|
|
238
243
|
}
|
|
239
|
-
async
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
model,
|
|
244
|
-
timeout_ms: opts?.timeoutMs,
|
|
244
|
+
async resizePty(name, rows, cols) {
|
|
245
|
+
return this.transport.request(`/api/resize/${encodeURIComponent(name)}`, {
|
|
246
|
+
method: 'POST',
|
|
247
|
+
body: JSON.stringify({ rows, cols }),
|
|
245
248
|
});
|
|
246
249
|
}
|
|
247
|
-
|
|
248
|
-
await this.start();
|
|
249
|
-
return this.requestOk('get_metrics', { agent });
|
|
250
|
-
}
|
|
251
|
-
async getCrashInsights() {
|
|
252
|
-
await this.start();
|
|
253
|
-
return this.requestOk('get_crash_insights', {});
|
|
254
|
-
}
|
|
250
|
+
// ── Messaging ──────────────────────────────────────────────────────
|
|
255
251
|
async sendMessage(input) {
|
|
256
|
-
await this.start();
|
|
257
252
|
try {
|
|
258
|
-
return await this.
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
253
|
+
return await this.transport.request('/api/send', {
|
|
254
|
+
method: 'POST',
|
|
255
|
+
body: JSON.stringify({
|
|
256
|
+
to: input.to,
|
|
257
|
+
text: input.text,
|
|
258
|
+
from: input.from,
|
|
259
|
+
threadId: input.threadId,
|
|
260
|
+
workspaceId: input.workspaceId,
|
|
261
|
+
workspaceAlias: input.workspaceAlias,
|
|
262
|
+
priority: input.priority,
|
|
263
|
+
data: input.data,
|
|
264
|
+
mode: input.mode,
|
|
265
|
+
}),
|
|
268
266
|
});
|
|
269
267
|
}
|
|
270
268
|
catch (error) {
|
|
@@ -274,636 +272,150 @@ export class AgentRelayClient {
|
|
|
274
272
|
throw error;
|
|
275
273
|
}
|
|
276
274
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
async getStatus() {
|
|
283
|
-
await this.start();
|
|
284
|
-
return this.requestOk('get_status', {});
|
|
285
|
-
}
|
|
286
|
-
async shutdown() {
|
|
287
|
-
if (!this.child) {
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
void this.requestOk('shutdown', {}).catch(() => {
|
|
291
|
-
// Continue shutdown path if broker is already unhealthy or exits before replying.
|
|
275
|
+
// ── Model control ──────────────────────────────────────────────────
|
|
276
|
+
async setModel(name, model, opts) {
|
|
277
|
+
return this.transport.request(`/api/spawned/${encodeURIComponent(name)}/model`, {
|
|
278
|
+
method: 'POST',
|
|
279
|
+
body: JSON.stringify({ model, timeout_ms: opts?.timeoutMs }),
|
|
292
280
|
});
|
|
293
|
-
const child = this.child;
|
|
294
|
-
const wait = this.exitPromise ?? Promise.resolve();
|
|
295
|
-
const waitForExit = async (timeoutMs) => {
|
|
296
|
-
let timer;
|
|
297
|
-
const result = await Promise.race([
|
|
298
|
-
wait.then(() => true),
|
|
299
|
-
new Promise((resolve) => {
|
|
300
|
-
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
301
|
-
}),
|
|
302
|
-
]);
|
|
303
|
-
if (timer !== undefined)
|
|
304
|
-
clearTimeout(timer);
|
|
305
|
-
return result;
|
|
306
|
-
};
|
|
307
|
-
if (await waitForExit(this.options.shutdownTimeoutMs)) {
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
if (child.exitCode === null && child.signalCode === null) {
|
|
311
|
-
child.kill('SIGTERM');
|
|
312
|
-
}
|
|
313
|
-
if (await waitForExit(1_000)) {
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
if (child.exitCode === null && child.signalCode === null) {
|
|
317
|
-
child.kill('SIGKILL');
|
|
318
|
-
}
|
|
319
|
-
await waitForExit(1_000);
|
|
320
|
-
}
|
|
321
|
-
async waitForExit() {
|
|
322
|
-
if (!this.child) {
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
await this.exitPromise;
|
|
326
281
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
this.lastStderrLine = undefined;
|
|
333
|
-
const args = [
|
|
334
|
-
'init',
|
|
335
|
-
'--name',
|
|
336
|
-
this.options.brokerName,
|
|
337
|
-
...(this.options.channels.length > 0 ? ['--channels', this.options.channels.join(',')] : []),
|
|
338
|
-
...this.options.binaryArgs,
|
|
339
|
-
];
|
|
340
|
-
// Ensure the SDK bin directory (containing agent-relay-broker) is on
|
|
341
|
-
// PATH so spawned workers can find it without any user setup.
|
|
342
|
-
const env = { ...this.options.env };
|
|
343
|
-
if (isExplicitPath(this.options.binaryPath)) {
|
|
344
|
-
const binDir = path.dirname(path.resolve(resolvedBinary));
|
|
345
|
-
const currentPath = env.PATH ?? env.Path ?? '';
|
|
346
|
-
if (!currentPath.split(path.delimiter).includes(binDir)) {
|
|
347
|
-
env.PATH = `${binDir}${path.delimiter}${currentPath}`;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
console.error(`[broker] Starting: ${resolvedBinary} ${args.join(' ')}`);
|
|
351
|
-
const child = spawn(resolvedBinary, args, {
|
|
352
|
-
cwd: this.options.cwd,
|
|
353
|
-
env,
|
|
354
|
-
stdio: 'pipe',
|
|
355
|
-
});
|
|
356
|
-
this.child = child;
|
|
357
|
-
this.stdoutRl = createInterface({ input: child.stdout, crlfDelay: Infinity });
|
|
358
|
-
this.stderrRl = createInterface({ input: child.stderr, crlfDelay: Infinity });
|
|
359
|
-
this.stdoutRl.on('line', (line) => {
|
|
360
|
-
this.handleStdoutLine(line);
|
|
361
|
-
});
|
|
362
|
-
this.stderrRl.on('line', (line) => {
|
|
363
|
-
const trimmed = line.trim();
|
|
364
|
-
if (trimmed) {
|
|
365
|
-
this.lastStderrLine = trimmed;
|
|
366
|
-
}
|
|
367
|
-
for (const listener of this.stderrListeners) {
|
|
368
|
-
listener(line);
|
|
369
|
-
}
|
|
282
|
+
// ── Channels ───────────────────────────────────────────────────────
|
|
283
|
+
async subscribeChannels(name, channels) {
|
|
284
|
+
await this.transport.request(`/api/spawned/${encodeURIComponent(name)}/subscribe`, {
|
|
285
|
+
method: 'POST',
|
|
286
|
+
body: JSON.stringify({ channels }),
|
|
370
287
|
});
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
// exits AND all stdio streams have ended.
|
|
377
|
-
child.once('close', (code, signal) => {
|
|
378
|
-
const detail = this.lastStderrLine ? `: ${this.lastStderrLine}` : '';
|
|
379
|
-
const error = new AgentRelayProcessError(`broker exited (code=${code ?? 'null'}, signal=${signal ?? 'null'})${detail}`);
|
|
380
|
-
this.failAllPending(error);
|
|
381
|
-
this.disposeProcessHandles();
|
|
382
|
-
resolve();
|
|
383
|
-
});
|
|
384
|
-
child.once('error', (error) => {
|
|
385
|
-
this.failAllPending(error);
|
|
386
|
-
this.disposeProcessHandles();
|
|
387
|
-
resolve();
|
|
388
|
-
});
|
|
288
|
+
}
|
|
289
|
+
async unsubscribeChannels(name, channels) {
|
|
290
|
+
await this.transport.request(`/api/spawned/${encodeURIComponent(name)}/unsubscribe`, {
|
|
291
|
+
method: 'POST',
|
|
292
|
+
body: JSON.stringify({ channels }),
|
|
389
293
|
});
|
|
390
|
-
const helloAck = await this.requestHello();
|
|
391
|
-
console.error('[broker] Broker ready (hello handshake complete)');
|
|
392
|
-
if (helloAck.workspace_key) {
|
|
393
|
-
this.workspaceKey = helloAck.workspace_key;
|
|
394
|
-
}
|
|
395
294
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
this.
|
|
400
|
-
this.stderrRl = undefined;
|
|
401
|
-
this.lastStderrLine = undefined;
|
|
402
|
-
this.child = undefined;
|
|
403
|
-
this.exitPromise = undefined;
|
|
404
|
-
}
|
|
405
|
-
failAllPending(error) {
|
|
406
|
-
for (const pending of this.pending.values()) {
|
|
407
|
-
clearTimeout(pending.timeout);
|
|
408
|
-
pending.reject(error);
|
|
409
|
-
}
|
|
410
|
-
this.pending.clear();
|
|
295
|
+
// ── Observability ──────────────────────────────────────────────────
|
|
296
|
+
async getMetrics(agent) {
|
|
297
|
+
const query = agent ? `?agent=${encodeURIComponent(agent)}` : '';
|
|
298
|
+
return this.transport.request(`/api/metrics${query}`);
|
|
411
299
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
try {
|
|
415
|
-
parsed = JSON.parse(line);
|
|
416
|
-
}
|
|
417
|
-
catch {
|
|
418
|
-
// Non-protocol output should not crash the SDK.
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
if (!parsed || typeof parsed !== 'object') {
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
if (parsed.v !== PROTOCOL_VERSION || typeof parsed.type !== 'string') {
|
|
425
|
-
return;
|
|
426
|
-
}
|
|
427
|
-
const envelope = {
|
|
428
|
-
v: parsed.v,
|
|
429
|
-
type: parsed.type,
|
|
430
|
-
request_id: parsed.request_id,
|
|
431
|
-
payload: parsed.payload,
|
|
432
|
-
};
|
|
433
|
-
if (envelope.type === 'event') {
|
|
434
|
-
const payload = envelope.payload;
|
|
435
|
-
this.eventBuffer.push(payload);
|
|
436
|
-
if (this.eventBuffer.length > this.maxBufferSize) {
|
|
437
|
-
this.eventBuffer.shift();
|
|
438
|
-
}
|
|
439
|
-
for (const listener of this.eventListeners) {
|
|
440
|
-
listener(payload);
|
|
441
|
-
}
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
if (!envelope.request_id) {
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
const pending = this.pending.get(envelope.request_id);
|
|
448
|
-
if (!pending) {
|
|
449
|
-
return;
|
|
450
|
-
}
|
|
451
|
-
if (envelope.type === 'error') {
|
|
452
|
-
clearTimeout(pending.timeout);
|
|
453
|
-
this.pending.delete(envelope.request_id);
|
|
454
|
-
pending.reject(new AgentRelayProtocolError(envelope.payload));
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
if (envelope.type !== pending.expectedType) {
|
|
458
|
-
clearTimeout(pending.timeout);
|
|
459
|
-
this.pending.delete(envelope.request_id);
|
|
460
|
-
pending.reject(new AgentRelayProcessError(`unexpected response type '${envelope.type}' for request '${envelope.request_id}' (expected '${pending.expectedType}')`));
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
clearTimeout(pending.timeout);
|
|
464
|
-
this.pending.delete(envelope.request_id);
|
|
465
|
-
pending.resolve(envelope);
|
|
466
|
-
}
|
|
467
|
-
async requestHello() {
|
|
468
|
-
const payload = {
|
|
469
|
-
client_name: this.options.clientName,
|
|
470
|
-
client_version: this.options.clientVersion,
|
|
471
|
-
};
|
|
472
|
-
const frame = await this.sendRequest('hello', payload, 'hello_ack');
|
|
473
|
-
return frame.payload;
|
|
300
|
+
async getStatus() {
|
|
301
|
+
return this.transport.request('/api/status');
|
|
474
302
|
}
|
|
475
|
-
async
|
|
476
|
-
|
|
477
|
-
const result = frame.payload;
|
|
478
|
-
return result.result;
|
|
303
|
+
async getCrashInsights() {
|
|
304
|
+
return this.transport.request('/api/crash-insights');
|
|
479
305
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
const message = {
|
|
486
|
-
v: PROTOCOL_VERSION,
|
|
487
|
-
type,
|
|
488
|
-
request_id: requestId,
|
|
489
|
-
payload,
|
|
490
|
-
};
|
|
491
|
-
const responsePromise = new Promise((resolve, reject) => {
|
|
492
|
-
const timeout = setTimeout(() => {
|
|
493
|
-
this.pending.delete(requestId);
|
|
494
|
-
reject(new AgentRelayProcessError(`request timed out after ${this.options.requestTimeoutMs}ms (type='${type}', request_id='${requestId}')`));
|
|
495
|
-
}, this.options.requestTimeoutMs);
|
|
496
|
-
this.pending.set(requestId, {
|
|
497
|
-
expectedType,
|
|
498
|
-
resolve,
|
|
499
|
-
reject,
|
|
500
|
-
timeout,
|
|
501
|
-
});
|
|
306
|
+
// ── Lifecycle ──────────────────────────────────────────────────────
|
|
307
|
+
async preflight(agents) {
|
|
308
|
+
return this.transport.request('/api/preflight', {
|
|
309
|
+
method: 'POST',
|
|
310
|
+
body: JSON.stringify({ agents }),
|
|
502
311
|
});
|
|
503
|
-
const line = `${JSON.stringify(message)}\n`;
|
|
504
|
-
if (!this.child.stdin.write(line)) {
|
|
505
|
-
await once(this.child.stdin, 'drain');
|
|
506
|
-
}
|
|
507
|
-
return responsePromise;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
const CLI_MODEL_FLAG_CLIS = new Set(['claude', 'codex', 'gemini', 'goose', 'aider']);
|
|
511
|
-
const CLI_DEFAULT_ARGS = {
|
|
512
|
-
codex: ['-c', 'check_for_update_on_startup=false'],
|
|
513
|
-
};
|
|
514
|
-
function buildPtyArgsWithModel(cli, args, model) {
|
|
515
|
-
const cliName = cli.split(':')[0].trim().toLowerCase();
|
|
516
|
-
const defaultArgs = CLI_DEFAULT_ARGS[cliName] ?? [];
|
|
517
|
-
const baseArgs = [...defaultArgs, ...args];
|
|
518
|
-
if (!model) {
|
|
519
|
-
return baseArgs;
|
|
520
|
-
}
|
|
521
|
-
if (!CLI_MODEL_FLAG_CLIS.has(cliName)) {
|
|
522
|
-
return baseArgs;
|
|
523
|
-
}
|
|
524
|
-
if (hasModelArg(baseArgs)) {
|
|
525
|
-
return baseArgs;
|
|
526
|
-
}
|
|
527
|
-
return ['--model', model, ...baseArgs];
|
|
528
|
-
}
|
|
529
|
-
function hasModelArg(args) {
|
|
530
|
-
for (let i = 0; i < args.length; i += 1) {
|
|
531
|
-
const arg = args[i];
|
|
532
|
-
if (arg === '--model') {
|
|
533
|
-
return true;
|
|
534
|
-
}
|
|
535
|
-
if (arg.startsWith('--model=')) {
|
|
536
|
-
return true;
|
|
537
|
-
}
|
|
538
312
|
}
|
|
539
|
-
|
|
540
|
-
}
|
|
541
|
-
function expandTilde(p) {
|
|
542
|
-
if (p === '~' || p.startsWith('~/') || p.startsWith('~\\')) {
|
|
543
|
-
const home = os.homedir();
|
|
544
|
-
return path.join(home, p.slice(2));
|
|
313
|
+
async renewLease() {
|
|
314
|
+
return this.transport.request('/api/session/renew', { method: 'POST' });
|
|
545
315
|
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
};
|
|
560
|
-
return platformMap[process.platform]?.[process.arch] ?? null;
|
|
561
|
-
}
|
|
562
|
-
function getLatestVersionSync() {
|
|
563
|
-
try {
|
|
564
|
-
const result = execSync('curl -fsSL https://api.github.com/repos/AgentWorkforce/relay/releases/latest', {
|
|
565
|
-
timeout: 15_000,
|
|
566
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
567
|
-
}).toString();
|
|
568
|
-
const match = result.match(/"tag_name"\s*:\s*"([^"]+)"/);
|
|
569
|
-
if (!match?.[1])
|
|
570
|
-
return null;
|
|
571
|
-
// Strip tag prefixes: "openclaw-v3.1.18" -> "3.1.18", "v3.1.18" -> "3.1.18"
|
|
572
|
-
return match[1].replace(/^openclaw-/, '').replace(/^v/, '');
|
|
573
|
-
}
|
|
574
|
-
catch {
|
|
575
|
-
return null;
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
function installBrokerBinary() {
|
|
579
|
-
const suffix = detectPlatformSuffix();
|
|
580
|
-
if (!suffix) {
|
|
581
|
-
throw new AgentRelayProcessError(`Unsupported platform: ${process.platform}-${process.arch}`);
|
|
582
|
-
}
|
|
583
|
-
const homeDir = process.env.HOME || process.env.USERPROFILE || '';
|
|
584
|
-
const installDir = path.join(homeDir, '.agent-relay', 'bin');
|
|
585
|
-
const brokerExe = process.platform === 'win32' ? 'agent-relay-broker.exe' : 'agent-relay-broker';
|
|
586
|
-
const targetPath = path.join(installDir, brokerExe);
|
|
587
|
-
console.log(`[agent-relay] Broker binary not found, installing for ${suffix}...`);
|
|
588
|
-
const version = getLatestVersionSync();
|
|
589
|
-
if (!version) {
|
|
590
|
-
throw new AgentRelayProcessError('Failed to fetch latest agent-relay version from GitHub.\n' +
|
|
591
|
-
'Install manually: curl -fsSL https://raw.githubusercontent.com/AgentWorkforce/relay/main/install.sh | bash');
|
|
592
|
-
}
|
|
593
|
-
const binaryName = `agent-relay-broker-${suffix}`;
|
|
594
|
-
const downloadUrl = `https://github.com/AgentWorkforce/relay/releases/download/v${version}/${binaryName}`;
|
|
595
|
-
console.log(`[agent-relay] Downloading v${version} from ${downloadUrl}`);
|
|
596
|
-
try {
|
|
597
|
-
fs.mkdirSync(installDir, { recursive: true });
|
|
598
|
-
execSync(`curl -fsSL "${downloadUrl}" -o "${targetPath}"`, {
|
|
599
|
-
timeout: 60_000,
|
|
600
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
601
|
-
});
|
|
602
|
-
fs.chmodSync(targetPath, 0o755);
|
|
603
|
-
// macOS: strip quarantine attribute and re-sign to avoid Gatekeeper issues
|
|
604
|
-
if (process.platform === 'darwin') {
|
|
605
|
-
try {
|
|
606
|
-
execSync(`xattr -d com.apple.quarantine "${targetPath}" 2>/dev/null || true`, {
|
|
607
|
-
timeout: 10_000,
|
|
608
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
catch {
|
|
612
|
-
// Non-fatal
|
|
613
|
-
}
|
|
316
|
+
/**
|
|
317
|
+
* Shut down and clean up.
|
|
318
|
+
* - For spawned brokers (via .spawn()): sends POST /api/shutdown to kill the broker, waits for exit.
|
|
319
|
+
* - For connected brokers (via .connect() or constructor): just disconnects the transport.
|
|
320
|
+
* Does NOT kill the broker — the caller doesn't own it.
|
|
321
|
+
*/
|
|
322
|
+
async shutdown() {
|
|
323
|
+
if (this.leaseTimer) {
|
|
324
|
+
clearInterval(this.leaseTimer);
|
|
325
|
+
this.leaseTimer = null;
|
|
326
|
+
}
|
|
327
|
+
// Only send the shutdown command if we own the broker process
|
|
328
|
+
if (this.child) {
|
|
614
329
|
try {
|
|
615
|
-
|
|
616
|
-
timeout: 10_000,
|
|
617
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
618
|
-
});
|
|
330
|
+
await this.transport.request('/api/shutdown', { method: 'POST' });
|
|
619
331
|
}
|
|
620
332
|
catch {
|
|
621
|
-
//
|
|
333
|
+
// Broker may already be dead
|
|
622
334
|
}
|
|
623
335
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
try {
|
|
629
|
-
fs.unlinkSync(targetPath);
|
|
630
|
-
}
|
|
631
|
-
catch {
|
|
632
|
-
/* ignore */
|
|
336
|
+
this.transport.disconnect();
|
|
337
|
+
if (this.child) {
|
|
338
|
+
await waitForExit(this.child, 5000);
|
|
339
|
+
this.child = null;
|
|
633
340
|
}
|
|
634
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
635
|
-
throw new AgentRelayProcessError(`Failed to install broker binary: ${message}\n` +
|
|
636
|
-
'Install manually: curl -fsSL https://raw.githubusercontent.com/AgentWorkforce/relay/main/install.sh | bash');
|
|
637
341
|
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
|
644
|
-
// 1. In a source checkout, prefer Cargo's release binary to avoid stale bundled
|
|
645
|
-
// copies when local dev rebuilds happen while broker processes are running.
|
|
646
|
-
const workspaceRelease = path.resolve(moduleDir, '..', '..', '..', 'target', 'release', brokerExe);
|
|
647
|
-
if (fs.existsSync(workspaceRelease)) {
|
|
648
|
-
return workspaceRelease;
|
|
649
|
-
}
|
|
650
|
-
// 2. Check for bundled platform-specific broker binary in SDK package (npm install).
|
|
651
|
-
// Only use binaries that match the current platform to avoid running
|
|
652
|
-
// e.g. a macOS binary on Linux (or vice-versa).
|
|
653
|
-
const binDir = path.resolve(moduleDir, '..', 'bin');
|
|
654
|
-
const suffix = detectPlatformSuffix();
|
|
655
|
-
if (suffix) {
|
|
656
|
-
const ext = process.platform === 'win32' ? '.exe' : '';
|
|
657
|
-
const platformBinary = path.join(binDir, `agent-relay-broker-${suffix}${ext}`);
|
|
658
|
-
if (fs.existsSync(platformBinary)) {
|
|
659
|
-
return platformBinary;
|
|
342
|
+
/** Disconnect without shutting down the broker. Alias for cases where the intent is clear. */
|
|
343
|
+
disconnect() {
|
|
344
|
+
if (this.leaseTimer) {
|
|
345
|
+
clearInterval(this.leaseTimer);
|
|
346
|
+
this.leaseTimer = null;
|
|
660
347
|
}
|
|
348
|
+
this.transport.disconnect();
|
|
661
349
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
const standaloneBroker = path.join(homeDir, '.agent-relay', 'bin', brokerExe);
|
|
665
|
-
if (fs.existsSync(standaloneBroker)) {
|
|
666
|
-
return standaloneBroker;
|
|
350
|
+
async getConfig() {
|
|
351
|
+
return this.transport.request('/api/config');
|
|
667
352
|
}
|
|
668
|
-
// 4. Auto-install from GitHub releases
|
|
669
|
-
return installBrokerBinary();
|
|
670
|
-
}
|
|
671
|
-
const DEFAULT_DASHBOARD_PORT = (() => {
|
|
672
|
-
const envPort = typeof process !== 'undefined' ? process.env.AGENT_RELAY_DASHBOARD_PORT : undefined;
|
|
673
|
-
if (envPort) {
|
|
674
|
-
const parsed = Number.parseInt(envPort, 10);
|
|
675
|
-
if (Number.isFinite(parsed) && parsed > 0)
|
|
676
|
-
return parsed;
|
|
677
|
-
}
|
|
678
|
-
return 3888;
|
|
679
|
-
})();
|
|
680
|
-
const HTTP_MAX_PORT_SCAN = 25;
|
|
681
|
-
const HTTP_AUTOSTART_TIMEOUT_MS = 10_000;
|
|
682
|
-
const HTTP_AUTOSTART_POLL_MS = 250;
|
|
683
|
-
function sanitizeBrokerName(name) {
|
|
684
|
-
return name.replace(/[^\p{L}\p{N}-]/gu, '-');
|
|
685
353
|
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
* Connect to an already-running broker on the given port.
|
|
699
|
-
*/
|
|
700
|
-
static async connectHttp(port, options) {
|
|
701
|
-
const client = new HttpAgentRelayClient({ port, apiKey: options?.apiKey });
|
|
702
|
-
// Verify connectivity
|
|
703
|
-
await client.healthCheck();
|
|
704
|
-
return client;
|
|
705
|
-
}
|
|
706
|
-
/**
|
|
707
|
-
* Discover a running broker for the current project and connect to it.
|
|
708
|
-
* Reads the broker PID file, verifies the process is alive, scans ports
|
|
709
|
-
* for the HTTP API, and returns a connected client.
|
|
710
|
-
*/
|
|
711
|
-
static async discoverAndConnect(options) {
|
|
712
|
-
const cwd = options?.cwd ?? process.cwd();
|
|
713
|
-
const apiKey = options?.apiKey ?? process.env.RELAY_BROKER_API_KEY?.trim();
|
|
714
|
-
const autoStart = options?.autoStart ?? false;
|
|
715
|
-
const paths = getProjectPaths(cwd);
|
|
716
|
-
const preferredApiPort = DEFAULT_DASHBOARD_PORT + 1;
|
|
717
|
-
// Try to find a running broker via PID file
|
|
718
|
-
const pidFilePath = path.join(paths.dataDir, brokerPidFilename(paths.projectRoot));
|
|
719
|
-
const legacyPidPath = path.join(paths.dataDir, 'broker.pid');
|
|
720
|
-
let brokerRunning = false;
|
|
721
|
-
for (const pidPath of [pidFilePath, legacyPidPath]) {
|
|
722
|
-
if (fs.existsSync(pidPath)) {
|
|
723
|
-
const pidStr = fs.readFileSync(pidPath, 'utf-8').trim();
|
|
724
|
-
const pid = Number.parseInt(pidStr, 10);
|
|
725
|
-
if (Number.isFinite(pid) && pid > 0) {
|
|
726
|
-
try {
|
|
727
|
-
process.kill(pid, 0);
|
|
728
|
-
brokerRunning = true;
|
|
729
|
-
break;
|
|
730
|
-
}
|
|
731
|
-
catch {
|
|
732
|
-
// Process not running
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
}
|
|
354
|
+
// ── Helpers ──────────────────────────────────────────────────────────────
|
|
355
|
+
/**
|
|
356
|
+
* Parse the API URL from the broker's stdout. The broker prints:
|
|
357
|
+
* [agent-relay] API listening on http://{bind}:{port}
|
|
358
|
+
* Returns the full URL (e.g. "http://127.0.0.1:3889").
|
|
359
|
+
*/
|
|
360
|
+
async function waitForApiUrl(child, timeoutMs) {
|
|
361
|
+
const { createInterface } = await import('node:readline');
|
|
362
|
+
return new Promise((resolve, reject) => {
|
|
363
|
+
if (!child.stdout) {
|
|
364
|
+
reject(new Error('Broker stdout not available'));
|
|
365
|
+
return;
|
|
736
366
|
}
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
367
|
+
let resolved = false;
|
|
368
|
+
const rl = createInterface({ input: child.stdout });
|
|
369
|
+
const timer = setTimeout(() => {
|
|
370
|
+
if (!resolved) {
|
|
371
|
+
resolved = true;
|
|
372
|
+
rl.close();
|
|
373
|
+
reject(new Error(`Broker did not report API port within ${timeoutMs}ms`));
|
|
741
374
|
}
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
// The broker binary requires the `init` subcommand with `--api-port` and
|
|
750
|
-
// `--persist` so it writes PID files for subsequent discovery.
|
|
751
|
-
const brokerBinary = options?.brokerBinaryPath ?? resolveDefaultBinaryPath();
|
|
752
|
-
const child = spawn(brokerBinary, ['init', '--persist', '--api-port', String(preferredApiPort)], {
|
|
753
|
-
cwd: paths.projectRoot,
|
|
754
|
-
env: process.env,
|
|
755
|
-
detached: true,
|
|
756
|
-
stdio: 'ignore',
|
|
757
|
-
});
|
|
758
|
-
child.unref();
|
|
759
|
-
const startedAt = Date.now();
|
|
760
|
-
while (Date.now() - startedAt < HTTP_AUTOSTART_TIMEOUT_MS) {
|
|
761
|
-
const port = await HttpAgentRelayClient.scanForBrokerPort(preferredApiPort);
|
|
762
|
-
if (port !== null) {
|
|
763
|
-
return new HttpAgentRelayClient({ port, apiKey });
|
|
375
|
+
}, timeoutMs);
|
|
376
|
+
child.on('exit', (code) => {
|
|
377
|
+
if (!resolved) {
|
|
378
|
+
resolved = true;
|
|
379
|
+
clearTimeout(timer);
|
|
380
|
+
rl.close();
|
|
381
|
+
reject(new Error(`Broker process exited with code ${code} before becoming ready`));
|
|
764
382
|
}
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
try {
|
|
773
|
-
const res = await fetch(`http://127.0.0.1:${port}/health`);
|
|
774
|
-
if (!res.ok)
|
|
775
|
-
continue;
|
|
776
|
-
const payload = (await res.json().catch(() => null));
|
|
777
|
-
if (payload?.service === 'agent-relay-listen') {
|
|
778
|
-
return port;
|
|
779
|
-
}
|
|
383
|
+
});
|
|
384
|
+
child.on('error', (err) => {
|
|
385
|
+
if (!resolved) {
|
|
386
|
+
resolved = true;
|
|
387
|
+
clearTimeout(timer);
|
|
388
|
+
rl.close();
|
|
389
|
+
reject(new Error(`Failed to start broker: ${err.message}`));
|
|
780
390
|
}
|
|
781
|
-
|
|
782
|
-
|
|
391
|
+
});
|
|
392
|
+
rl.on('line', (line) => {
|
|
393
|
+
if (resolved)
|
|
394
|
+
return;
|
|
395
|
+
const match = line.match(/API listening on (https?:\/\/[^\s]+)/);
|
|
396
|
+
if (match) {
|
|
397
|
+
resolved = true;
|
|
398
|
+
clearTimeout(timer);
|
|
399
|
+
rl.close();
|
|
400
|
+
resolve(match[1]);
|
|
783
401
|
}
|
|
784
|
-
}
|
|
785
|
-
return null;
|
|
786
|
-
}
|
|
787
|
-
async request(pathname, init) {
|
|
788
|
-
const headers = new Headers(init?.headers);
|
|
789
|
-
if (this.apiKey && !headers.has('x-api-key') && !headers.has('authorization')) {
|
|
790
|
-
headers.set('x-api-key', this.apiKey);
|
|
791
|
-
}
|
|
792
|
-
const response = await fetch(`http://127.0.0.1:${this.port}${pathname}`, {
|
|
793
|
-
...init,
|
|
794
|
-
headers,
|
|
795
402
|
});
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
}
|
|
804
|
-
if (!response.ok) {
|
|
805
|
-
const msg = HttpAgentRelayClient.extractErrorMessage(response, payload);
|
|
806
|
-
throw new AgentRelayProcessError(msg);
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
function waitForExit(child, timeoutMs) {
|
|
406
|
+
return new Promise((resolve) => {
|
|
407
|
+
if (child.exitCode !== null) {
|
|
408
|
+
resolve();
|
|
409
|
+
return;
|
|
807
410
|
}
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
return p.error;
|
|
816
|
-
if (typeof p?.error?.message === 'string')
|
|
817
|
-
return p.error.message;
|
|
818
|
-
if (typeof p?.message === 'string' && p.message.trim())
|
|
819
|
-
return p.message.trim();
|
|
820
|
-
return `${response.status} ${response.statusText}`.trim();
|
|
821
|
-
}
|
|
822
|
-
async healthCheck() {
|
|
823
|
-
return this.request('/health');
|
|
824
|
-
}
|
|
825
|
-
/** No-op — broker is already running. */
|
|
826
|
-
async start() { }
|
|
827
|
-
/** No-op — don't kill an externally-managed broker. */
|
|
828
|
-
async shutdown() { }
|
|
829
|
-
async spawnPty(input) {
|
|
830
|
-
const payload = await this.request('/api/spawn', {
|
|
831
|
-
method: 'POST',
|
|
832
|
-
headers: { 'content-type': 'application/json' },
|
|
833
|
-
body: JSON.stringify({
|
|
834
|
-
name: input.name,
|
|
835
|
-
cli: input.cli,
|
|
836
|
-
model: input.model,
|
|
837
|
-
args: input.args ?? [],
|
|
838
|
-
task: input.task,
|
|
839
|
-
channels: input.channels ?? [],
|
|
840
|
-
cwd: input.cwd,
|
|
841
|
-
team: input.team,
|
|
842
|
-
shadowOf: input.shadowOf,
|
|
843
|
-
shadowMode: input.shadowMode,
|
|
844
|
-
continueFrom: input.continueFrom,
|
|
845
|
-
idleThresholdSecs: input.idleThresholdSecs,
|
|
846
|
-
restartPolicy: input.restartPolicy,
|
|
847
|
-
skipRelayPrompt: input.skipRelayPrompt,
|
|
848
|
-
}),
|
|
849
|
-
});
|
|
850
|
-
return {
|
|
851
|
-
name: typeof payload?.name === 'string' ? payload.name : input.name,
|
|
852
|
-
runtime: 'pty',
|
|
853
|
-
};
|
|
854
|
-
}
|
|
855
|
-
async sendMessage(input) {
|
|
856
|
-
return this.request('/api/send', {
|
|
857
|
-
method: 'POST',
|
|
858
|
-
headers: { 'content-type': 'application/json' },
|
|
859
|
-
body: JSON.stringify({
|
|
860
|
-
to: input.to,
|
|
861
|
-
text: input.text,
|
|
862
|
-
from: input.from,
|
|
863
|
-
threadId: input.threadId,
|
|
864
|
-
workspaceId: input.workspaceId,
|
|
865
|
-
workspaceAlias: input.workspaceAlias,
|
|
866
|
-
priority: input.priority,
|
|
867
|
-
data: input.data,
|
|
868
|
-
mode: input.mode,
|
|
869
|
-
}),
|
|
870
|
-
});
|
|
871
|
-
}
|
|
872
|
-
async listAgents() {
|
|
873
|
-
const payload = await this.request('/api/spawned', { method: 'GET' });
|
|
874
|
-
return Array.isArray(payload?.agents) ? payload.agents : [];
|
|
875
|
-
}
|
|
876
|
-
async release(name, reason) {
|
|
877
|
-
const payload = await this.request(`/api/spawned/${encodeURIComponent(name)}`, {
|
|
878
|
-
method: 'DELETE',
|
|
879
|
-
...(reason
|
|
880
|
-
? { headers: { 'content-type': 'application/json' }, body: JSON.stringify({ reason }) }
|
|
881
|
-
: {}),
|
|
882
|
-
});
|
|
883
|
-
return { name: typeof payload?.name === 'string' ? payload.name : name };
|
|
884
|
-
}
|
|
885
|
-
async subscribeChannels(_name, _channels) {
|
|
886
|
-
throw new Error('subscribeChannels is only available via the broker protocol (BrokerAgentRelayClient). ' +
|
|
887
|
-
'The HTTP API does not support dynamic channel subscription.');
|
|
888
|
-
}
|
|
889
|
-
async unsubscribeChannels(_name, _channels) {
|
|
890
|
-
throw new Error('unsubscribeChannels is only available via the broker protocol (BrokerAgentRelayClient). ' +
|
|
891
|
-
'The HTTP API does not support dynamic channel unsubscription.');
|
|
892
|
-
}
|
|
893
|
-
async setModel(name, model, opts) {
|
|
894
|
-
const payload = await this.request(`/api/spawned/${encodeURIComponent(name)}/model`, {
|
|
895
|
-
method: 'POST',
|
|
896
|
-
headers: { 'content-type': 'application/json' },
|
|
897
|
-
body: JSON.stringify({ model, timeoutMs: opts?.timeoutMs }),
|
|
411
|
+
const timer = setTimeout(() => {
|
|
412
|
+
child.kill('SIGKILL');
|
|
413
|
+
resolve();
|
|
414
|
+
}, timeoutMs);
|
|
415
|
+
child.on('exit', () => {
|
|
416
|
+
clearTimeout(timer);
|
|
417
|
+
resolve();
|
|
898
418
|
});
|
|
899
|
-
|
|
900
|
-
name,
|
|
901
|
-
model: typeof payload?.model === 'string' ? payload.model : model,
|
|
902
|
-
success: payload?.success !== false,
|
|
903
|
-
};
|
|
904
|
-
}
|
|
905
|
-
async getConfig() {
|
|
906
|
-
return this.request('/api/config');
|
|
907
|
-
}
|
|
419
|
+
});
|
|
908
420
|
}
|
|
909
421
|
//# sourceMappingURL=client.js.map
|