@agent-relay/utils 2.1.5 → 2.1.6
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/cjs/discovery.js +328 -0
- package/dist/cjs/errors.js +81 -0
- package/dist/discovery.d.ts +123 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +439 -0
- package/dist/discovery.js.map +1 -0
- package/dist/errors.d.ts +29 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +50 -0
- package/dist/errors.js.map +1 -0
- package/package.json +15 -2
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var discovery_exports = {};
|
|
20
|
+
__export(discovery_exports, {
|
|
21
|
+
cloudApiRequest: () => cloudApiRequest,
|
|
22
|
+
detectCloudWorkspace: () => detectCloudWorkspace,
|
|
23
|
+
discoverAgentName: () => discoverAgentName,
|
|
24
|
+
discoverSocket: () => discoverSocket,
|
|
25
|
+
getCloudEnvironmentSummary: () => getCloudEnvironmentSummary,
|
|
26
|
+
getCloudOutboxPath: () => getCloudOutboxPath,
|
|
27
|
+
getCloudSocketPath: () => getCloudSocketPath,
|
|
28
|
+
getConnectionInfo: () => getConnectionInfo,
|
|
29
|
+
getWorkspaceStatus: () => getWorkspaceStatus,
|
|
30
|
+
isCloudWorkspace: () => isCloudWorkspace
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(discovery_exports);
|
|
33
|
+
var import_node_fs = require("node:fs");
|
|
34
|
+
var import_node_path = require("node:path");
|
|
35
|
+
var import_node_os = require("node:os");
|
|
36
|
+
var import_config = require("@agent-relay/config");
|
|
37
|
+
function detectCloudWorkspace() {
|
|
38
|
+
const workspaceId = process.env.WORKSPACE_ID;
|
|
39
|
+
const cloudApiUrl = process.env.CLOUD_API_URL;
|
|
40
|
+
if (!workspaceId || !cloudApiUrl) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
workspaceId,
|
|
45
|
+
cloudApiUrl,
|
|
46
|
+
workspaceToken: process.env.WORKSPACE_TOKEN,
|
|
47
|
+
ownerUserId: process.env.WORKSPACE_OWNER_USER_ID
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function isCloudWorkspace() {
|
|
51
|
+
return detectCloudWorkspace() !== null;
|
|
52
|
+
}
|
|
53
|
+
function getCloudSocketPath(workspaceId) {
|
|
54
|
+
return `/tmp/relay/${workspaceId}/sockets/daemon.sock`;
|
|
55
|
+
}
|
|
56
|
+
function getCloudOutboxPath(workspaceId, agentName) {
|
|
57
|
+
return `/tmp/relay/${workspaceId}/outbox/${agentName}`;
|
|
58
|
+
}
|
|
59
|
+
function getDataDir() {
|
|
60
|
+
const platform = process.platform;
|
|
61
|
+
if (platform === "darwin") {
|
|
62
|
+
return (0, import_node_path.join)((0, import_node_os.homedir)(), "Library", "Application Support", "agent-relay");
|
|
63
|
+
} else if (platform === "win32") {
|
|
64
|
+
return (0, import_node_path.join)(process.env.APPDATA || (0, import_node_os.homedir)(), "agent-relay");
|
|
65
|
+
} else {
|
|
66
|
+
return (0, import_node_path.join)(
|
|
67
|
+
process.env.XDG_DATA_HOME || (0, import_node_path.join)((0, import_node_os.homedir)(), ".local", "share"),
|
|
68
|
+
"agent-relay"
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function discoverSocket(options = {}) {
|
|
73
|
+
if (options.socketPath) {
|
|
74
|
+
const workspace2 = options.workspace ? {
|
|
75
|
+
workspaceId: options.workspace.workspaceId || "override",
|
|
76
|
+
cloudApiUrl: options.workspace.cloudApiUrl || ""
|
|
77
|
+
} : void 0;
|
|
78
|
+
return {
|
|
79
|
+
socketPath: options.socketPath,
|
|
80
|
+
project: workspace2?.workspaceId || "override",
|
|
81
|
+
source: "env",
|
|
82
|
+
isCloud: !!workspace2,
|
|
83
|
+
workspace: workspace2
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const socketEnv = process.env.RELAY_SOCKET;
|
|
87
|
+
if (socketEnv) {
|
|
88
|
+
const workspace2 = detectCloudWorkspace();
|
|
89
|
+
return {
|
|
90
|
+
socketPath: socketEnv,
|
|
91
|
+
project: process.env.RELAY_PROJECT || workspace2?.workspaceId || "unknown",
|
|
92
|
+
source: "env",
|
|
93
|
+
isCloud: !!workspace2,
|
|
94
|
+
workspace: workspace2 || void 0
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const workspace = detectCloudWorkspace();
|
|
98
|
+
if (workspace) {
|
|
99
|
+
const cloudSocket = getCloudSocketPath(workspace.workspaceId);
|
|
100
|
+
return {
|
|
101
|
+
socketPath: cloudSocket,
|
|
102
|
+
project: workspace.workspaceId,
|
|
103
|
+
source: "cloud",
|
|
104
|
+
isCloud: true,
|
|
105
|
+
workspace
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const projectEnv = process.env.RELAY_PROJECT;
|
|
109
|
+
if (projectEnv) {
|
|
110
|
+
const dataDir2 = getDataDir();
|
|
111
|
+
const projectSocket = (0, import_node_path.join)(dataDir2, "projects", projectEnv, "daemon.sock");
|
|
112
|
+
return {
|
|
113
|
+
socketPath: projectSocket,
|
|
114
|
+
project: projectEnv,
|
|
115
|
+
source: "env",
|
|
116
|
+
isCloud: false
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const projectRoot = (0, import_config.findProjectRoot)(process.cwd());
|
|
120
|
+
const searchDirs = [process.cwd()];
|
|
121
|
+
if (projectRoot && projectRoot !== process.cwd()) {
|
|
122
|
+
searchDirs.push(projectRoot);
|
|
123
|
+
}
|
|
124
|
+
for (const dir of searchDirs) {
|
|
125
|
+
const projectLocalSocket = (0, import_node_path.join)(dir, ".agent-relay", "relay.sock");
|
|
126
|
+
if ((0, import_node_fs.existsSync)(projectLocalSocket)) {
|
|
127
|
+
let projectId = "local";
|
|
128
|
+
const markerPath = (0, import_node_path.join)(dir, ".agent-relay", ".project");
|
|
129
|
+
if ((0, import_node_fs.existsSync)(markerPath)) {
|
|
130
|
+
try {
|
|
131
|
+
const marker = JSON.parse((0, import_node_fs.readFileSync)(markerPath, "utf-8"));
|
|
132
|
+
projectId = marker.projectId || "local";
|
|
133
|
+
} catch {
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
socketPath: projectLocalSocket,
|
|
138
|
+
project: projectId,
|
|
139
|
+
source: "cwd",
|
|
140
|
+
isCloud: false
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const cwdConfig = (0, import_node_path.join)(process.cwd(), ".relay", "config.json");
|
|
145
|
+
if ((0, import_node_fs.existsSync)(cwdConfig)) {
|
|
146
|
+
try {
|
|
147
|
+
const config = JSON.parse((0, import_node_fs.readFileSync)(cwdConfig, "utf-8"));
|
|
148
|
+
if (config.socketPath) {
|
|
149
|
+
return {
|
|
150
|
+
socketPath: config.socketPath,
|
|
151
|
+
project: config.project || "local",
|
|
152
|
+
source: "cwd",
|
|
153
|
+
isCloud: false
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
} catch (err) {
|
|
157
|
+
if (process.env.DEBUG || process.env.RELAY_DEBUG) {
|
|
158
|
+
console.debug("[discovery] Failed to read cwd config:", cwdConfig, err);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const dataDir = getDataDir();
|
|
163
|
+
const projectsDir = (0, import_node_path.join)(dataDir, "projects");
|
|
164
|
+
if ((0, import_node_fs.existsSync)(projectsDir)) {
|
|
165
|
+
try {
|
|
166
|
+
const projects = (0, import_node_fs.readdirSync)(projectsDir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
|
|
167
|
+
for (const project of projects) {
|
|
168
|
+
const socketPath = (0, import_node_path.join)(projectsDir, project, "daemon.sock");
|
|
169
|
+
if ((0, import_node_fs.existsSync)(socketPath)) {
|
|
170
|
+
return {
|
|
171
|
+
socketPath,
|
|
172
|
+
project,
|
|
173
|
+
source: "scan",
|
|
174
|
+
isCloud: false
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
} catch (err) {
|
|
179
|
+
if (process.env.DEBUG || process.env.RELAY_DEBUG) {
|
|
180
|
+
console.debug("[discovery] Failed to scan projects directory:", projectsDir, err);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
async function cloudApiRequest(workspace, path, options = {}) {
|
|
187
|
+
const url = `${workspace.cloudApiUrl}${path}`;
|
|
188
|
+
const headers = {
|
|
189
|
+
"Content-Type": "application/json",
|
|
190
|
+
...options.headers
|
|
191
|
+
};
|
|
192
|
+
if (workspace.workspaceToken) {
|
|
193
|
+
headers["Authorization"] = `Bearer ${workspace.workspaceToken}`;
|
|
194
|
+
}
|
|
195
|
+
return fetch(url, {
|
|
196
|
+
...options,
|
|
197
|
+
headers
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
async function getWorkspaceStatus(workspace) {
|
|
201
|
+
try {
|
|
202
|
+
const response = await cloudApiRequest(
|
|
203
|
+
workspace,
|
|
204
|
+
`/api/workspaces/${workspace.workspaceId}/status`
|
|
205
|
+
);
|
|
206
|
+
if (!response.ok) {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
return await response.json();
|
|
210
|
+
} catch {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function getConnectionInfo(options = {}) {
|
|
215
|
+
const discovery = discoverSocket(options);
|
|
216
|
+
if (!discovery) {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
const info = {
|
|
220
|
+
socketPath: discovery.socketPath,
|
|
221
|
+
project: discovery.project,
|
|
222
|
+
isCloud: discovery.isCloud,
|
|
223
|
+
workspace: discovery.workspace
|
|
224
|
+
};
|
|
225
|
+
if (discovery.workspace?.cloudApiUrl) {
|
|
226
|
+
info.daemonUrl = discovery.workspace.cloudApiUrl;
|
|
227
|
+
}
|
|
228
|
+
return info;
|
|
229
|
+
}
|
|
230
|
+
function getCloudEnvironmentSummary() {
|
|
231
|
+
return {
|
|
232
|
+
WORKSPACE_ID: process.env.WORKSPACE_ID,
|
|
233
|
+
CLOUD_API_URL: process.env.CLOUD_API_URL,
|
|
234
|
+
WORKSPACE_TOKEN: process.env.WORKSPACE_TOKEN ? "[set]" : void 0,
|
|
235
|
+
WORKSPACE_OWNER_USER_ID: process.env.WORKSPACE_OWNER_USER_ID,
|
|
236
|
+
RELAY_SOCKET: process.env.RELAY_SOCKET,
|
|
237
|
+
RELAY_PROJECT: process.env.RELAY_PROJECT,
|
|
238
|
+
RELAY_AGENT_NAME: process.env.RELAY_AGENT_NAME
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function discoverAgentName(_discovery) {
|
|
242
|
+
const envName = process.env.RELAY_AGENT_NAME;
|
|
243
|
+
if (envName) {
|
|
244
|
+
return envName;
|
|
245
|
+
}
|
|
246
|
+
const projectRoot = (0, import_config.findProjectRoot)(process.cwd());
|
|
247
|
+
const searchDirs = [process.cwd()];
|
|
248
|
+
if (projectRoot && projectRoot !== process.cwd()) {
|
|
249
|
+
searchDirs.push(projectRoot);
|
|
250
|
+
}
|
|
251
|
+
for (const dir of searchDirs) {
|
|
252
|
+
const relayDir = (0, import_node_path.join)(dir, ".agent-relay");
|
|
253
|
+
if (!(0, import_node_fs.existsSync)(relayDir)) continue;
|
|
254
|
+
const pidIdentityPath = (0, import_node_path.join)(relayDir, `mcp-identity-${process.ppid}`);
|
|
255
|
+
if ((0, import_node_fs.existsSync)(pidIdentityPath)) {
|
|
256
|
+
try {
|
|
257
|
+
const content = (0, import_node_fs.readFileSync)(pidIdentityPath, "utf-8").trim();
|
|
258
|
+
if (content) {
|
|
259
|
+
return content;
|
|
260
|
+
}
|
|
261
|
+
} catch {
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
const files = (0, import_node_fs.readdirSync)(relayDir, { withFileTypes: true }).filter((d) => d.isFile() && d.name.startsWith("mcp-identity-")).map((d) => ({
|
|
266
|
+
path: (0, import_node_path.join)(relayDir, d.name),
|
|
267
|
+
name: d.name
|
|
268
|
+
}));
|
|
269
|
+
if (files.length > 0) {
|
|
270
|
+
const sorted = files.map((f) => {
|
|
271
|
+
try {
|
|
272
|
+
const stat = (0, import_node_fs.statSync)(f.path);
|
|
273
|
+
return { ...f, mtime: stat.mtimeMs };
|
|
274
|
+
} catch {
|
|
275
|
+
return { ...f, mtime: 0 };
|
|
276
|
+
}
|
|
277
|
+
}).sort((a, b) => b.mtime - a.mtime);
|
|
278
|
+
const latest = sorted[0];
|
|
279
|
+
if (latest) {
|
|
280
|
+
try {
|
|
281
|
+
const content = (0, import_node_fs.readFileSync)(latest.path, "utf-8").trim();
|
|
282
|
+
if (content) {
|
|
283
|
+
return content;
|
|
284
|
+
}
|
|
285
|
+
} catch {
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
} catch {
|
|
290
|
+
}
|
|
291
|
+
const identityPath = (0, import_node_path.join)(relayDir, "mcp-identity");
|
|
292
|
+
if ((0, import_node_fs.existsSync)(identityPath)) {
|
|
293
|
+
try {
|
|
294
|
+
const content = (0, import_node_fs.readFileSync)(identityPath, "utf-8").trim();
|
|
295
|
+
if (content) {
|
|
296
|
+
return content;
|
|
297
|
+
}
|
|
298
|
+
} catch {
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
for (const dir of searchDirs) {
|
|
303
|
+
const outboxDir = (0, import_node_path.join)(dir, ".agent-relay", "outbox");
|
|
304
|
+
if ((0, import_node_fs.existsSync)(outboxDir)) {
|
|
305
|
+
try {
|
|
306
|
+
const agents = (0, import_node_fs.readdirSync)(outboxDir, { withFileTypes: true }).filter((d) => d.isDirectory()).map((d) => d.name);
|
|
307
|
+
if (agents.length === 1) {
|
|
308
|
+
return agents[0];
|
|
309
|
+
}
|
|
310
|
+
} catch {
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
317
|
+
0 && (module.exports = {
|
|
318
|
+
cloudApiRequest,
|
|
319
|
+
detectCloudWorkspace,
|
|
320
|
+
discoverAgentName,
|
|
321
|
+
discoverSocket,
|
|
322
|
+
getCloudEnvironmentSummary,
|
|
323
|
+
getCloudOutboxPath,
|
|
324
|
+
getCloudSocketPath,
|
|
325
|
+
getConnectionInfo,
|
|
326
|
+
getWorkspaceStatus,
|
|
327
|
+
isCloudWorkspace
|
|
328
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var errors_exports = {};
|
|
20
|
+
__export(errors_exports, {
|
|
21
|
+
AgentNotFoundError: () => AgentNotFoundError,
|
|
22
|
+
ChannelNotFoundError: () => ChannelNotFoundError,
|
|
23
|
+
ConnectionError: () => ConnectionError,
|
|
24
|
+
DaemonNotRunningError: () => DaemonNotRunningError,
|
|
25
|
+
RelayError: () => RelayError,
|
|
26
|
+
SpawnError: () => SpawnError,
|
|
27
|
+
TimeoutError: () => TimeoutError
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(errors_exports);
|
|
30
|
+
class RelayError extends Error {
|
|
31
|
+
constructor(message) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = "RelayError";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
class DaemonNotRunningError extends RelayError {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super(message || "Relay daemon is not running. Start with: agent-relay up");
|
|
39
|
+
this.name = "DaemonNotRunningError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
class AgentNotFoundError extends RelayError {
|
|
43
|
+
constructor(agentName) {
|
|
44
|
+
super(`Agent not found: ${agentName}`);
|
|
45
|
+
this.name = "AgentNotFoundError";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
class TimeoutError extends RelayError {
|
|
49
|
+
constructor(operation, timeoutMs) {
|
|
50
|
+
super(`Timeout after ${timeoutMs}ms: ${operation}`);
|
|
51
|
+
this.name = "TimeoutError";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
class ConnectionError extends RelayError {
|
|
55
|
+
constructor(message) {
|
|
56
|
+
super(`Connection error: ${message}`);
|
|
57
|
+
this.name = "ConnectionError";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
class ChannelNotFoundError extends RelayError {
|
|
61
|
+
constructor(channel) {
|
|
62
|
+
super(`Channel not found: ${channel}`);
|
|
63
|
+
this.name = "ChannelNotFoundError";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
class SpawnError extends RelayError {
|
|
67
|
+
constructor(workerName, reason) {
|
|
68
|
+
super(`Failed to spawn worker "${workerName}": ${reason}`);
|
|
69
|
+
this.name = "SpawnError";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
+
0 && (module.exports = {
|
|
74
|
+
AgentNotFoundError,
|
|
75
|
+
ChannelNotFoundError,
|
|
76
|
+
ConnectionError,
|
|
77
|
+
DaemonNotRunningError,
|
|
78
|
+
RelayError,
|
|
79
|
+
SpawnError,
|
|
80
|
+
TimeoutError
|
|
81
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Socket Discovery & Cloud Workspace Detection
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for discovering relay daemon sockets,
|
|
5
|
+
* cloud workspace environments, and agent identity.
|
|
6
|
+
*
|
|
7
|
+
* Previously duplicated in @agent-relay/mcp (cloud.ts). Now consolidated
|
|
8
|
+
* here in the SDK so both SDK and MCP use the same discovery logic.
|
|
9
|
+
*/
|
|
10
|
+
export interface CloudWorkspace {
|
|
11
|
+
workspaceId: string;
|
|
12
|
+
cloudApiUrl: string;
|
|
13
|
+
workspaceToken?: string;
|
|
14
|
+
ownerUserId?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface DiscoveryResult {
|
|
17
|
+
socketPath: string;
|
|
18
|
+
project: string;
|
|
19
|
+
source: 'env' | 'cloud' | 'cwd' | 'scan';
|
|
20
|
+
isCloud: boolean;
|
|
21
|
+
workspace?: CloudWorkspace;
|
|
22
|
+
}
|
|
23
|
+
export interface CloudConnectionOptions {
|
|
24
|
+
/** Override socket path (for testing) */
|
|
25
|
+
socketPath?: string;
|
|
26
|
+
/** Override workspace detection */
|
|
27
|
+
workspace?: Partial<CloudWorkspace>;
|
|
28
|
+
}
|
|
29
|
+
export interface CloudConnectionInfo {
|
|
30
|
+
socketPath: string;
|
|
31
|
+
project: string;
|
|
32
|
+
isCloud: boolean;
|
|
33
|
+
workspace?: CloudWorkspace;
|
|
34
|
+
daemonUrl?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Detect if running in a cloud workspace environment.
|
|
38
|
+
*
|
|
39
|
+
* Cloud workspaces set these environment variables:
|
|
40
|
+
* - WORKSPACE_ID: The unique workspace identifier
|
|
41
|
+
* - CLOUD_API_URL: The cloud API endpoint
|
|
42
|
+
* - WORKSPACE_TOKEN: Bearer token for API auth (optional)
|
|
43
|
+
* - WORKSPACE_OWNER_USER_ID: The workspace owner's user ID (optional)
|
|
44
|
+
*/
|
|
45
|
+
export declare function detectCloudWorkspace(): CloudWorkspace | null;
|
|
46
|
+
/**
|
|
47
|
+
* Check if we're running in a cloud workspace.
|
|
48
|
+
*/
|
|
49
|
+
export declare function isCloudWorkspace(): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get the workspace-namespaced socket path.
|
|
52
|
+
*
|
|
53
|
+
* In cloud workspaces, sockets are stored at:
|
|
54
|
+
* /tmp/relay/{WORKSPACE_ID}/sockets/daemon.sock
|
|
55
|
+
*
|
|
56
|
+
* This provides multi-tenant isolation on shared infrastructure.
|
|
57
|
+
*/
|
|
58
|
+
export declare function getCloudSocketPath(workspaceId: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Get the workspace-namespaced outbox path.
|
|
61
|
+
*
|
|
62
|
+
* In cloud workspaces, outbox directories are at:
|
|
63
|
+
* /tmp/relay/{WORKSPACE_ID}/outbox/{agentName}/
|
|
64
|
+
*/
|
|
65
|
+
export declare function getCloudOutboxPath(workspaceId: string, agentName: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Discover relay daemon socket with cloud-awareness.
|
|
68
|
+
*
|
|
69
|
+
* Priority order:
|
|
70
|
+
* 1. RELAY_SOCKET environment variable (explicit path)
|
|
71
|
+
* 2. Cloud workspace socket (if WORKSPACE_ID is set)
|
|
72
|
+
* 3. RELAY_PROJECT environment variable (project name -> data dir)
|
|
73
|
+
* 4. Current working directory .relay/config.json
|
|
74
|
+
* 5. Scan data directory for active sockets
|
|
75
|
+
*
|
|
76
|
+
* @param options - Optional configuration overrides
|
|
77
|
+
* @returns Discovery result with socket path, project info, and cloud status
|
|
78
|
+
*/
|
|
79
|
+
export declare function discoverSocket(options?: CloudConnectionOptions): DiscoveryResult | null;
|
|
80
|
+
/**
|
|
81
|
+
* Make an authenticated request to the cloud API.
|
|
82
|
+
*
|
|
83
|
+
* @param workspace - Cloud workspace configuration
|
|
84
|
+
* @param path - API path (e.g., '/api/status')
|
|
85
|
+
* @param options - Fetch options
|
|
86
|
+
* @returns Response from the API
|
|
87
|
+
*/
|
|
88
|
+
export declare function cloudApiRequest(workspace: CloudWorkspace, path: string, options?: RequestInit): Promise<Response>;
|
|
89
|
+
/**
|
|
90
|
+
* Get the workspace status from the cloud API.
|
|
91
|
+
*/
|
|
92
|
+
export declare function getWorkspaceStatus(workspace: CloudWorkspace): Promise<{
|
|
93
|
+
status: string;
|
|
94
|
+
agents?: string[];
|
|
95
|
+
} | null>;
|
|
96
|
+
/**
|
|
97
|
+
* Get connection info for the relay daemon.
|
|
98
|
+
*
|
|
99
|
+
* This function determines the best way to connect to the daemon:
|
|
100
|
+
* - In cloud environments: Uses workspace-namespaced socket
|
|
101
|
+
* - In local environments: Uses standard socket discovery
|
|
102
|
+
*
|
|
103
|
+
* @param options - Optional configuration overrides
|
|
104
|
+
* @returns Connection info or null if daemon not found
|
|
105
|
+
*/
|
|
106
|
+
export declare function getConnectionInfo(options?: CloudConnectionOptions): CloudConnectionInfo | null;
|
|
107
|
+
/**
|
|
108
|
+
* Environment variable summary for debugging.
|
|
109
|
+
*/
|
|
110
|
+
export declare function getCloudEnvironmentSummary(): Record<string, string | undefined>;
|
|
111
|
+
/**
|
|
112
|
+
* Discover the agent name for the MCP server.
|
|
113
|
+
*
|
|
114
|
+
* Priority order:
|
|
115
|
+
* 1. RELAY_AGENT_NAME environment variable (explicit)
|
|
116
|
+
* 2. Identity file in .agent-relay directory (written by wrapper)
|
|
117
|
+
* 3. Scan outbox directories to find agent's outbox
|
|
118
|
+
*
|
|
119
|
+
* @param _discovery - Optional discovery result (reserved for future use)
|
|
120
|
+
* @returns Agent name or null if not found
|
|
121
|
+
*/
|
|
122
|
+
export declare function discoverAgentName(_discovery?: DiscoveryResult | null): string | null;
|
|
123
|
+
//# sourceMappingURL=discovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAWH,MAAM,WAAW,cAAc;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,KAAK,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;IACzC,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,cAAc,CAAC;CAC5B;AA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AAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAU/E;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,CAAC,EAAE,eAAe,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAgHpF"}
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Socket Discovery & Cloud Workspace Detection
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for discovering relay daemon sockets,
|
|
5
|
+
* cloud workspace environments, and agent identity.
|
|
6
|
+
*
|
|
7
|
+
* Previously duplicated in @agent-relay/mcp (cloud.ts). Now consolidated
|
|
8
|
+
* here in the SDK so both SDK and MCP use the same discovery logic.
|
|
9
|
+
*/
|
|
10
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { homedir } from 'node:os';
|
|
13
|
+
import { findProjectRoot } from '@agent-relay/config';
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Cloud Workspace Detection
|
|
16
|
+
// ============================================================================
|
|
17
|
+
/**
|
|
18
|
+
* Detect if running in a cloud workspace environment.
|
|
19
|
+
*
|
|
20
|
+
* Cloud workspaces set these environment variables:
|
|
21
|
+
* - WORKSPACE_ID: The unique workspace identifier
|
|
22
|
+
* - CLOUD_API_URL: The cloud API endpoint
|
|
23
|
+
* - WORKSPACE_TOKEN: Bearer token for API auth (optional)
|
|
24
|
+
* - WORKSPACE_OWNER_USER_ID: The workspace owner's user ID (optional)
|
|
25
|
+
*/
|
|
26
|
+
export function detectCloudWorkspace() {
|
|
27
|
+
const workspaceId = process.env.WORKSPACE_ID;
|
|
28
|
+
const cloudApiUrl = process.env.CLOUD_API_URL;
|
|
29
|
+
if (!workspaceId || !cloudApiUrl) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
workspaceId,
|
|
34
|
+
cloudApiUrl,
|
|
35
|
+
workspaceToken: process.env.WORKSPACE_TOKEN,
|
|
36
|
+
ownerUserId: process.env.WORKSPACE_OWNER_USER_ID,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Check if we're running in a cloud workspace.
|
|
41
|
+
*/
|
|
42
|
+
export function isCloudWorkspace() {
|
|
43
|
+
return detectCloudWorkspace() !== null;
|
|
44
|
+
}
|
|
45
|
+
// ============================================================================
|
|
46
|
+
// Workspace-Aware Socket Discovery
|
|
47
|
+
// ============================================================================
|
|
48
|
+
/**
|
|
49
|
+
* Get the workspace-namespaced socket path.
|
|
50
|
+
*
|
|
51
|
+
* In cloud workspaces, sockets are stored at:
|
|
52
|
+
* /tmp/relay/{WORKSPACE_ID}/sockets/daemon.sock
|
|
53
|
+
*
|
|
54
|
+
* This provides multi-tenant isolation on shared infrastructure.
|
|
55
|
+
*/
|
|
56
|
+
export function getCloudSocketPath(workspaceId) {
|
|
57
|
+
return `/tmp/relay/${workspaceId}/sockets/daemon.sock`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get the workspace-namespaced outbox path.
|
|
61
|
+
*
|
|
62
|
+
* In cloud workspaces, outbox directories are at:
|
|
63
|
+
* /tmp/relay/{WORKSPACE_ID}/outbox/{agentName}/
|
|
64
|
+
*/
|
|
65
|
+
export function getCloudOutboxPath(workspaceId, agentName) {
|
|
66
|
+
return `/tmp/relay/${workspaceId}/outbox/${agentName}`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get platform-specific data directory.
|
|
70
|
+
*/
|
|
71
|
+
function getDataDir() {
|
|
72
|
+
const platform = process.platform;
|
|
73
|
+
if (platform === 'darwin') {
|
|
74
|
+
return join(homedir(), 'Library', 'Application Support', 'agent-relay');
|
|
75
|
+
}
|
|
76
|
+
else if (platform === 'win32') {
|
|
77
|
+
return join(process.env.APPDATA || homedir(), 'agent-relay');
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
return join(process.env.XDG_DATA_HOME || join(homedir(), '.local', 'share'), 'agent-relay');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Discover relay daemon socket with cloud-awareness.
|
|
85
|
+
*
|
|
86
|
+
* Priority order:
|
|
87
|
+
* 1. RELAY_SOCKET environment variable (explicit path)
|
|
88
|
+
* 2. Cloud workspace socket (if WORKSPACE_ID is set)
|
|
89
|
+
* 3. RELAY_PROJECT environment variable (project name -> data dir)
|
|
90
|
+
* 4. Current working directory .relay/config.json
|
|
91
|
+
* 5. Scan data directory for active sockets
|
|
92
|
+
*
|
|
93
|
+
* @param options - Optional configuration overrides
|
|
94
|
+
* @returns Discovery result with socket path, project info, and cloud status
|
|
95
|
+
*/
|
|
96
|
+
export function discoverSocket(options = {}) {
|
|
97
|
+
// 0. Use override if provided
|
|
98
|
+
if (options.socketPath) {
|
|
99
|
+
const workspace = options.workspace
|
|
100
|
+
? {
|
|
101
|
+
workspaceId: options.workspace.workspaceId || 'override',
|
|
102
|
+
cloudApiUrl: options.workspace.cloudApiUrl || '',
|
|
103
|
+
}
|
|
104
|
+
: undefined;
|
|
105
|
+
return {
|
|
106
|
+
socketPath: options.socketPath,
|
|
107
|
+
project: workspace?.workspaceId || 'override',
|
|
108
|
+
source: 'env',
|
|
109
|
+
isCloud: !!workspace,
|
|
110
|
+
workspace,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
// 1. Explicit socket path from environment
|
|
114
|
+
const socketEnv = process.env.RELAY_SOCKET;
|
|
115
|
+
if (socketEnv) {
|
|
116
|
+
const workspace = detectCloudWorkspace();
|
|
117
|
+
return {
|
|
118
|
+
socketPath: socketEnv,
|
|
119
|
+
project: process.env.RELAY_PROJECT || workspace?.workspaceId || 'unknown',
|
|
120
|
+
source: 'env',
|
|
121
|
+
isCloud: !!workspace,
|
|
122
|
+
workspace: workspace || undefined,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// 2. Cloud workspace socket (highest priority for cloud environments)
|
|
126
|
+
// Return the determined path even if the socket file doesn't exist yet
|
|
127
|
+
// (daemon may not have started)
|
|
128
|
+
const workspace = detectCloudWorkspace();
|
|
129
|
+
if (workspace) {
|
|
130
|
+
const cloudSocket = getCloudSocketPath(workspace.workspaceId);
|
|
131
|
+
return {
|
|
132
|
+
socketPath: cloudSocket,
|
|
133
|
+
project: workspace.workspaceId,
|
|
134
|
+
source: 'cloud',
|
|
135
|
+
isCloud: true,
|
|
136
|
+
workspace,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
// 3. Project name -> data dir lookup
|
|
140
|
+
const projectEnv = process.env.RELAY_PROJECT;
|
|
141
|
+
if (projectEnv) {
|
|
142
|
+
const dataDir = getDataDir();
|
|
143
|
+
const projectSocket = join(dataDir, 'projects', projectEnv, 'daemon.sock');
|
|
144
|
+
return {
|
|
145
|
+
socketPath: projectSocket,
|
|
146
|
+
project: projectEnv,
|
|
147
|
+
source: 'env',
|
|
148
|
+
isCloud: false,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// 4. Project-local socket (created by daemon in project's .agent-relay directory)
|
|
152
|
+
// This is the primary path for local development
|
|
153
|
+
// First try cwd, then scan up to find project root
|
|
154
|
+
const projectRoot = findProjectRoot(process.cwd());
|
|
155
|
+
const searchDirs = [process.cwd()];
|
|
156
|
+
if (projectRoot && projectRoot !== process.cwd()) {
|
|
157
|
+
searchDirs.push(projectRoot);
|
|
158
|
+
}
|
|
159
|
+
for (const dir of searchDirs) {
|
|
160
|
+
const projectLocalSocket = join(dir, '.agent-relay', 'relay.sock');
|
|
161
|
+
if (existsSync(projectLocalSocket)) {
|
|
162
|
+
// Read project ID from marker file if available
|
|
163
|
+
let projectId = 'local';
|
|
164
|
+
const markerPath = join(dir, '.agent-relay', '.project');
|
|
165
|
+
if (existsSync(markerPath)) {
|
|
166
|
+
try {
|
|
167
|
+
const marker = JSON.parse(readFileSync(markerPath, 'utf-8'));
|
|
168
|
+
projectId = marker.projectId || 'local';
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
// Ignore marker read errors
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
socketPath: projectLocalSocket,
|
|
176
|
+
project: projectId,
|
|
177
|
+
source: 'cwd',
|
|
178
|
+
isCloud: false,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// 4b. Legacy .relay/config.json support
|
|
183
|
+
const cwdConfig = join(process.cwd(), '.relay', 'config.json');
|
|
184
|
+
if (existsSync(cwdConfig)) {
|
|
185
|
+
try {
|
|
186
|
+
const config = JSON.parse(readFileSync(cwdConfig, 'utf-8'));
|
|
187
|
+
if (config.socketPath) {
|
|
188
|
+
return {
|
|
189
|
+
socketPath: config.socketPath,
|
|
190
|
+
project: config.project || 'local',
|
|
191
|
+
source: 'cwd',
|
|
192
|
+
isCloud: false,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
// Invalid config (malformed JSON, permission error, etc.), continue to next method
|
|
198
|
+
if (process.env.DEBUG || process.env.RELAY_DEBUG) {
|
|
199
|
+
console.debug('[discovery] Failed to read cwd config:', cwdConfig, err);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// 5. Scan data directory for active sockets
|
|
204
|
+
const dataDir = getDataDir();
|
|
205
|
+
const projectsDir = join(dataDir, 'projects');
|
|
206
|
+
if (existsSync(projectsDir)) {
|
|
207
|
+
try {
|
|
208
|
+
const projects = readdirSync(projectsDir, { withFileTypes: true })
|
|
209
|
+
.filter((d) => d.isDirectory())
|
|
210
|
+
.map((d) => d.name);
|
|
211
|
+
for (const project of projects) {
|
|
212
|
+
const socketPath = join(projectsDir, project, 'daemon.sock');
|
|
213
|
+
if (existsSync(socketPath)) {
|
|
214
|
+
return {
|
|
215
|
+
socketPath,
|
|
216
|
+
project,
|
|
217
|
+
source: 'scan',
|
|
218
|
+
isCloud: false,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
// Directory read failed (permission error, etc.), return null
|
|
225
|
+
if (process.env.DEBUG || process.env.RELAY_DEBUG) {
|
|
226
|
+
console.debug('[discovery] Failed to scan projects directory:', projectsDir, err);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
// ============================================================================
|
|
233
|
+
// Cloud API Helpers
|
|
234
|
+
// ============================================================================
|
|
235
|
+
/**
|
|
236
|
+
* Make an authenticated request to the cloud API.
|
|
237
|
+
*
|
|
238
|
+
* @param workspace - Cloud workspace configuration
|
|
239
|
+
* @param path - API path (e.g., '/api/status')
|
|
240
|
+
* @param options - Fetch options
|
|
241
|
+
* @returns Response from the API
|
|
242
|
+
*/
|
|
243
|
+
export async function cloudApiRequest(workspace, path, options = {}) {
|
|
244
|
+
const url = `${workspace.cloudApiUrl}${path}`;
|
|
245
|
+
const headers = {
|
|
246
|
+
'Content-Type': 'application/json',
|
|
247
|
+
...options.headers,
|
|
248
|
+
};
|
|
249
|
+
if (workspace.workspaceToken) {
|
|
250
|
+
headers['Authorization'] = `Bearer ${workspace.workspaceToken}`;
|
|
251
|
+
}
|
|
252
|
+
return fetch(url, {
|
|
253
|
+
...options,
|
|
254
|
+
headers,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Get the workspace status from the cloud API.
|
|
259
|
+
*/
|
|
260
|
+
export async function getWorkspaceStatus(workspace) {
|
|
261
|
+
try {
|
|
262
|
+
const response = await cloudApiRequest(workspace, `/api/workspaces/${workspace.workspaceId}/status`);
|
|
263
|
+
if (!response.ok) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
return (await response.json());
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// ============================================================================
|
|
273
|
+
// Cloud Connection Factory
|
|
274
|
+
// ============================================================================
|
|
275
|
+
/**
|
|
276
|
+
* Get connection info for the relay daemon.
|
|
277
|
+
*
|
|
278
|
+
* This function determines the best way to connect to the daemon:
|
|
279
|
+
* - In cloud environments: Uses workspace-namespaced socket
|
|
280
|
+
* - In local environments: Uses standard socket discovery
|
|
281
|
+
*
|
|
282
|
+
* @param options - Optional configuration overrides
|
|
283
|
+
* @returns Connection info or null if daemon not found
|
|
284
|
+
*/
|
|
285
|
+
export function getConnectionInfo(options = {}) {
|
|
286
|
+
const discovery = discoverSocket(options);
|
|
287
|
+
if (!discovery) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
const info = {
|
|
291
|
+
socketPath: discovery.socketPath,
|
|
292
|
+
project: discovery.project,
|
|
293
|
+
isCloud: discovery.isCloud,
|
|
294
|
+
workspace: discovery.workspace,
|
|
295
|
+
};
|
|
296
|
+
// In cloud environments, we may also have a daemon URL for HTTP API access
|
|
297
|
+
if (discovery.workspace?.cloudApiUrl) {
|
|
298
|
+
info.daemonUrl = discovery.workspace.cloudApiUrl;
|
|
299
|
+
}
|
|
300
|
+
return info;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Environment variable summary for debugging.
|
|
304
|
+
*/
|
|
305
|
+
export function getCloudEnvironmentSummary() {
|
|
306
|
+
return {
|
|
307
|
+
WORKSPACE_ID: process.env.WORKSPACE_ID,
|
|
308
|
+
CLOUD_API_URL: process.env.CLOUD_API_URL,
|
|
309
|
+
WORKSPACE_TOKEN: process.env.WORKSPACE_TOKEN ? '[set]' : undefined,
|
|
310
|
+
WORKSPACE_OWNER_USER_ID: process.env.WORKSPACE_OWNER_USER_ID,
|
|
311
|
+
RELAY_SOCKET: process.env.RELAY_SOCKET,
|
|
312
|
+
RELAY_PROJECT: process.env.RELAY_PROJECT,
|
|
313
|
+
RELAY_AGENT_NAME: process.env.RELAY_AGENT_NAME,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
// ============================================================================
|
|
317
|
+
// Agent Identity Discovery
|
|
318
|
+
// ============================================================================
|
|
319
|
+
/**
|
|
320
|
+
* Discover the agent name for the MCP server.
|
|
321
|
+
*
|
|
322
|
+
* Priority order:
|
|
323
|
+
* 1. RELAY_AGENT_NAME environment variable (explicit)
|
|
324
|
+
* 2. Identity file in .agent-relay directory (written by wrapper)
|
|
325
|
+
* 3. Scan outbox directories to find agent's outbox
|
|
326
|
+
*
|
|
327
|
+
* @param _discovery - Optional discovery result (reserved for future use)
|
|
328
|
+
* @returns Agent name or null if not found
|
|
329
|
+
*/
|
|
330
|
+
export function discoverAgentName(_discovery) {
|
|
331
|
+
// 1. Explicit environment variable
|
|
332
|
+
const envName = process.env.RELAY_AGENT_NAME;
|
|
333
|
+
if (envName) {
|
|
334
|
+
return envName;
|
|
335
|
+
}
|
|
336
|
+
// 2. Identity file in .agent-relay directory
|
|
337
|
+
// The wrapper creates this file with the agent name
|
|
338
|
+
const projectRoot = findProjectRoot(process.cwd());
|
|
339
|
+
const searchDirs = [process.cwd()];
|
|
340
|
+
if (projectRoot && projectRoot !== process.cwd()) {
|
|
341
|
+
searchDirs.push(projectRoot);
|
|
342
|
+
}
|
|
343
|
+
for (const dir of searchDirs) {
|
|
344
|
+
const relayDir = join(dir, '.agent-relay');
|
|
345
|
+
if (!existsSync(relayDir))
|
|
346
|
+
continue;
|
|
347
|
+
// First check for per-process identity files
|
|
348
|
+
// The orchestrator writes mcp-identity-{orchestrator.pid}
|
|
349
|
+
// Try to find one by checking process.ppid and its ancestors
|
|
350
|
+
const pidIdentityPath = join(relayDir, `mcp-identity-${process.ppid}`);
|
|
351
|
+
if (existsSync(pidIdentityPath)) {
|
|
352
|
+
try {
|
|
353
|
+
const content = readFileSync(pidIdentityPath, 'utf-8').trim();
|
|
354
|
+
if (content) {
|
|
355
|
+
return content;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
// Ignore read errors
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
// Scan all mcp-identity-* files and return the most recently modified one
|
|
363
|
+
// This handles the case where MCP server's ppid doesn't match the orchestrator
|
|
364
|
+
try {
|
|
365
|
+
const files = readdirSync(relayDir, { withFileTypes: true })
|
|
366
|
+
.filter((d) => d.isFile() && d.name.startsWith('mcp-identity-'))
|
|
367
|
+
.map((d) => ({
|
|
368
|
+
path: join(relayDir, d.name),
|
|
369
|
+
name: d.name,
|
|
370
|
+
}));
|
|
371
|
+
if (files.length > 0) {
|
|
372
|
+
// Sort by mtime (most recent first) to get the latest identity
|
|
373
|
+
const sorted = files
|
|
374
|
+
.map((f) => {
|
|
375
|
+
try {
|
|
376
|
+
const stat = statSync(f.path);
|
|
377
|
+
return { ...f, mtime: stat.mtimeMs };
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
return { ...f, mtime: 0 };
|
|
381
|
+
}
|
|
382
|
+
})
|
|
383
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
384
|
+
// Return the most recently modified identity file
|
|
385
|
+
const latest = sorted[0];
|
|
386
|
+
if (latest) {
|
|
387
|
+
try {
|
|
388
|
+
const content = readFileSync(latest.path, 'utf-8').trim();
|
|
389
|
+
if (content) {
|
|
390
|
+
return content;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
// Ignore
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
catch {
|
|
400
|
+
// Ignore scan errors
|
|
401
|
+
}
|
|
402
|
+
// Fallback to simple identity file (for single-agent scenarios)
|
|
403
|
+
const identityPath = join(relayDir, 'mcp-identity');
|
|
404
|
+
if (existsSync(identityPath)) {
|
|
405
|
+
try {
|
|
406
|
+
const content = readFileSync(identityPath, 'utf-8').trim();
|
|
407
|
+
if (content) {
|
|
408
|
+
return content;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
// Ignore read errors
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
// 3. Check outbox directories for a match
|
|
417
|
+
// If only one agent's outbox exists, assume we're that agent
|
|
418
|
+
for (const dir of searchDirs) {
|
|
419
|
+
const outboxDir = join(dir, '.agent-relay', 'outbox');
|
|
420
|
+
if (existsSync(outboxDir)) {
|
|
421
|
+
try {
|
|
422
|
+
const agents = readdirSync(outboxDir, { withFileTypes: true })
|
|
423
|
+
.filter((d) => d.isDirectory())
|
|
424
|
+
.map((d) => d.name);
|
|
425
|
+
// If there's exactly one outbox, use that agent name
|
|
426
|
+
if (agents.length === 1) {
|
|
427
|
+
return agents[0];
|
|
428
|
+
}
|
|
429
|
+
// If there are multiple, we can't determine which one we are
|
|
430
|
+
// The wrapper should have created an identity file
|
|
431
|
+
}
|
|
432
|
+
catch {
|
|
433
|
+
// Ignore read errors
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
//# sourceMappingURL=discovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAoCtD,+EAA+E;AAC/E,4BAA4B;AAC5B,+EAA+E;AAE/E;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB;IAClC,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;IAC7C,MAAM,WAAW,GAAG,OA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CAAC,IAAI,CAAC,CAAC;gBAEtB,qDAAqD;gBACrD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;oBACxB,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC;gBACnB,CAAC;gBAED,6DAA6D;gBAC7D,mDAAmD;YACrD,CAAC;YAAC,MAAM,CAAC;gBACP,qBAAqB;YACvB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Types for Agent Relay
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for typed error classes.
|
|
5
|
+
* Previously duplicated in @agent-relay/mcp (errors.ts).
|
|
6
|
+
* Now consolidated here in the SDK for shared use.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RelayError extends Error {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
export declare class DaemonNotRunningError extends RelayError {
|
|
12
|
+
constructor(message?: string);
|
|
13
|
+
}
|
|
14
|
+
export declare class AgentNotFoundError extends RelayError {
|
|
15
|
+
constructor(agentName: string);
|
|
16
|
+
}
|
|
17
|
+
export declare class TimeoutError extends RelayError {
|
|
18
|
+
constructor(operation: string, timeoutMs: number);
|
|
19
|
+
}
|
|
20
|
+
export declare class ConnectionError extends RelayError {
|
|
21
|
+
constructor(message: string);
|
|
22
|
+
}
|
|
23
|
+
export declare class ChannelNotFoundError extends RelayError {
|
|
24
|
+
constructor(channel: string);
|
|
25
|
+
}
|
|
26
|
+
export declare class SpawnError extends RelayError {
|
|
27
|
+
constructor(workerName: string, reason: string);
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,qBAAsB,SAAQ,UAAU;gBACvC,OAAO,CAAC,EAAE,MAAM;CAI7B;AAED,qBAAa,kBAAmB,SAAQ,UAAU;gBACpC,SAAS,EAAE,MAAM;CAI9B;AAED,qBAAa,YAAa,SAAQ,UAAU;gBAC9B,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;CAIjD;AAED,qBAAa,eAAgB,SAAQ,UAAU;gBACjC,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,oBAAqB,SAAQ,UAAU;gBACtC,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,UAAW,SAAQ,UAAU;gBAC5B,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAI/C"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Types for Agent Relay
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for typed error classes.
|
|
5
|
+
* Previously duplicated in @agent-relay/mcp (errors.ts).
|
|
6
|
+
* Now consolidated here in the SDK for shared use.
|
|
7
|
+
*/
|
|
8
|
+
export class RelayError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'RelayError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class DaemonNotRunningError extends RelayError {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message || 'Relay daemon is not running. Start with: agent-relay up');
|
|
17
|
+
this.name = 'DaemonNotRunningError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class AgentNotFoundError extends RelayError {
|
|
21
|
+
constructor(agentName) {
|
|
22
|
+
super(`Agent not found: ${agentName}`);
|
|
23
|
+
this.name = 'AgentNotFoundError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class TimeoutError extends RelayError {
|
|
27
|
+
constructor(operation, timeoutMs) {
|
|
28
|
+
super(`Timeout after ${timeoutMs}ms: ${operation}`);
|
|
29
|
+
this.name = 'TimeoutError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class ConnectionError extends RelayError {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super(`Connection error: ${message}`);
|
|
35
|
+
this.name = 'ConnectionError';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export class ChannelNotFoundError extends RelayError {
|
|
39
|
+
constructor(channel) {
|
|
40
|
+
super(`Channel not found: ${channel}`);
|
|
41
|
+
this.name = 'ChannelNotFoundError';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export class SpawnError extends RelayError {
|
|
45
|
+
constructor(workerName, reason) {
|
|
46
|
+
super(`Failed to spawn worker "${workerName}": ${reason}`);
|
|
47
|
+
this.name = 'SpawnError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC3B,CAAC;CACF;AAED,MAAM,OAAO,qBAAsB,SAAQ,UAAU;IACnD,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,IAAI,yDAAyD,CAAC,CAAC;QAC5E,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;IACtC,CAAC;CACF;AAED,MAAM,OAAO,kBAAmB,SAAQ,UAAU;IAChD,YAAY,SAAiB;QAC3B,KAAK,CAAC,oBAAoB,SAAS,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,MAAM,OAAO,YAAa,SAAQ,UAAU;IAC1C,YAAY,SAAiB,EAAE,SAAiB;QAC9C,KAAK,CAAC,iBAAiB,SAAS,OAAO,SAAS,EAAE,CAAC,CAAC;QACpD,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAED,MAAM,OAAO,eAAgB,SAAQ,UAAU;IAC7C,YAAY,OAAe;QACzB,KAAK,CAAC,qBAAqB,OAAO,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED,MAAM,OAAO,oBAAqB,SAAQ,UAAU;IAClD,YAAY,OAAe;QACzB,KAAK,CAAC,sBAAsB,OAAO,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAED,MAAM,OAAO,UAAW,SAAQ,UAAU;IACxC,YAAY,UAAkB,EAAE,MAAc;QAC5C,KAAK,CAAC,2BAA2B,UAAU,MAAM,MAAM,EAAE,CAAC,CAAC;QAC3D,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;IAC3B,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-relay/utils",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"description": "Shared utilities for agent-relay: logging, name generation, command resolution, update checking",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -72,6 +72,18 @@
|
|
|
72
72
|
"import": "./dist/client-helpers.js",
|
|
73
73
|
"default": "./dist/cjs/client-helpers.js"
|
|
74
74
|
},
|
|
75
|
+
"./discovery": {
|
|
76
|
+
"types": "./dist/discovery.d.ts",
|
|
77
|
+
"require": "./dist/cjs/discovery.js",
|
|
78
|
+
"import": "./dist/discovery.js",
|
|
79
|
+
"default": "./dist/cjs/discovery.js"
|
|
80
|
+
},
|
|
81
|
+
"./errors": {
|
|
82
|
+
"types": "./dist/errors.d.ts",
|
|
83
|
+
"require": "./dist/cjs/errors.js",
|
|
84
|
+
"import": "./dist/errors.js",
|
|
85
|
+
"default": "./dist/cjs/errors.js"
|
|
86
|
+
},
|
|
75
87
|
"./package.json": "./package.json"
|
|
76
88
|
},
|
|
77
89
|
"files": [
|
|
@@ -94,7 +106,8 @@
|
|
|
94
106
|
"vitest": "^3.2.4"
|
|
95
107
|
},
|
|
96
108
|
"dependencies": {
|
|
97
|
-
"@agent-relay/
|
|
109
|
+
"@agent-relay/config": "2.1.6",
|
|
110
|
+
"@agent-relay/protocol": "2.1.6",
|
|
98
111
|
"compare-versions": "^6.1.1"
|
|
99
112
|
},
|
|
100
113
|
"publishConfig": {
|