@multi-agent-protocol/sdk 0.0.5 → 0.0.7
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 +164 -5
- package/dist/{index-DMcRbXOS.d.cts → index-BQXp4_rd.d.cts} +543 -12
- package/dist/{index-DMcRbXOS.d.ts → index-BQXp4_rd.d.ts} +543 -12
- package/dist/index.cjs +774 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +520 -12
- package/dist/index.d.ts +520 -12
- package/dist/index.js +773 -5
- package/dist/index.js.map +1 -1
- package/dist/testing.cjs +442 -8
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +442 -8
- package/dist/testing.js.map +1 -1
- package/package.json +7 -4
package/dist/testing.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as ParticipantCapabilities,
|
|
1
|
+
import { P as ParticipantCapabilities, fE as SystemExposure, d as FederationRoutingConfig, S as Stream, cw as BaseConnection, f as SessionId, A as AgentId, m as Agent, l as ScopeId, n as Scope, c as Message, q as Event, j as ParticipantId, L as SubscriptionId, cD as ClientConnectionOptions, cC as ClientConnection, c1 as AgentsListRequestParams, eq as ScopesListRequestParams, p as SubscriptionFilter, eQ as Subscription, e as EventType, bL as AgentConnectionOptions, W as AgentConnection } from './index-BQXp4_rd.cjs';
|
|
2
2
|
import 'events';
|
|
3
3
|
|
|
4
4
|
/**
|
package/dist/testing.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as ParticipantCapabilities,
|
|
1
|
+
import { P as ParticipantCapabilities, fE as SystemExposure, d as FederationRoutingConfig, S as Stream, cw as BaseConnection, f as SessionId, A as AgentId, m as Agent, l as ScopeId, n as Scope, c as Message, q as Event, j as ParticipantId, L as SubscriptionId, cD as ClientConnectionOptions, cC as ClientConnection, c1 as AgentsListRequestParams, eq as ScopesListRequestParams, p as SubscriptionFilter, eQ as Subscription, e as EventType, bL as AgentConnectionOptions, W as AgentConnection } from './index-BQXp4_rd.js';
|
|
2
2
|
import 'events';
|
|
3
3
|
|
|
4
4
|
/**
|
package/dist/testing.js
CHANGED
|
@@ -1,6 +1,86 @@
|
|
|
1
|
+
import { TunnelStream } from 'agentic-mesh';
|
|
1
2
|
import { ulid } from 'ulid';
|
|
2
3
|
import { EventEmitter } from 'events';
|
|
3
4
|
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __esm = (fn, res) => function __init() {
|
|
8
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
9
|
+
};
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all)
|
|
12
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/stream/agentic-mesh.ts
|
|
16
|
+
var agentic_mesh_exports = {};
|
|
17
|
+
__export(agentic_mesh_exports, {
|
|
18
|
+
agenticMeshStream: () => agenticMeshStream
|
|
19
|
+
});
|
|
20
|
+
async function agenticMeshStream(config) {
|
|
21
|
+
if (!config.transport.active) {
|
|
22
|
+
await config.transport.start();
|
|
23
|
+
}
|
|
24
|
+
const connected = await config.transport.connect(config.peer);
|
|
25
|
+
if (!connected) {
|
|
26
|
+
throw new Error(`Failed to connect to peer: ${config.peer.peerId}`);
|
|
27
|
+
}
|
|
28
|
+
const streamId = `map-${config.localPeerId}-${Date.now()}`;
|
|
29
|
+
const tunnelStream = new TunnelStream({
|
|
30
|
+
transport: config.transport,
|
|
31
|
+
peerId: config.peer.peerId,
|
|
32
|
+
streamId
|
|
33
|
+
});
|
|
34
|
+
await tunnelStream.open();
|
|
35
|
+
return tunnelStreamToMapStream(tunnelStream);
|
|
36
|
+
}
|
|
37
|
+
function tunnelStreamToMapStream(tunnel) {
|
|
38
|
+
let readingAborted = false;
|
|
39
|
+
const readable = new ReadableStream({
|
|
40
|
+
async start(controller) {
|
|
41
|
+
try {
|
|
42
|
+
for await (const frame of tunnel) {
|
|
43
|
+
if (readingAborted) break;
|
|
44
|
+
controller.enqueue(frame);
|
|
45
|
+
}
|
|
46
|
+
if (!readingAborted) {
|
|
47
|
+
controller.close();
|
|
48
|
+
}
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (!readingAborted) {
|
|
51
|
+
controller.error(error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
cancel() {
|
|
56
|
+
readingAborted = true;
|
|
57
|
+
tunnel.close().catch(() => {
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
const writable = new WritableStream({
|
|
62
|
+
async write(message) {
|
|
63
|
+
if (!tunnel.isOpen) {
|
|
64
|
+
throw new Error("Stream is not open");
|
|
65
|
+
}
|
|
66
|
+
await tunnel.write(message);
|
|
67
|
+
},
|
|
68
|
+
async close() {
|
|
69
|
+
await tunnel.close();
|
|
70
|
+
},
|
|
71
|
+
abort() {
|
|
72
|
+
readingAborted = true;
|
|
73
|
+
tunnel.close().catch(() => {
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return { readable, writable };
|
|
78
|
+
}
|
|
79
|
+
var init_agentic_mesh = __esm({
|
|
80
|
+
"src/stream/agentic-mesh.ts"() {
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
4
84
|
// src/jsonrpc/index.ts
|
|
5
85
|
function isRequest(message) {
|
|
6
86
|
return typeof message === "object" && message !== null && "jsonrpc" in message && message.jsonrpc === "2.0" && "id" in message && "method" in message;
|
|
@@ -110,6 +190,10 @@ var SESSION_METHODS = {
|
|
|
110
190
|
SESSION_LOAD: "map/session/load",
|
|
111
191
|
SESSION_CLOSE: "map/session/close"
|
|
112
192
|
};
|
|
193
|
+
var AUTH_METHODS = {
|
|
194
|
+
AUTHENTICATE: "map/authenticate",
|
|
195
|
+
AUTH_REFRESH: "map/auth/refresh"
|
|
196
|
+
};
|
|
113
197
|
var PERMISSION_METHODS = {
|
|
114
198
|
PERMISSIONS_UPDATE: "map/permissions/update"
|
|
115
199
|
};
|
|
@@ -120,8 +204,7 @@ var NOTIFICATION_METHODS = {
|
|
|
120
204
|
EVENT: "map/event",
|
|
121
205
|
MESSAGE: "map/message",
|
|
122
206
|
/** Client acknowledges received events (for backpressure) */
|
|
123
|
-
SUBSCRIBE_ACK: "map/subscribe.ack"
|
|
124
|
-
};
|
|
207
|
+
SUBSCRIBE_ACK: "map/subscribe.ack"};
|
|
125
208
|
var PROTOCOL_ERROR_CODES = {
|
|
126
209
|
PARSE_ERROR: -32700,
|
|
127
210
|
INVALID_REQUEST: -32600,
|
|
@@ -133,7 +216,10 @@ var AUTH_ERROR_CODES = {
|
|
|
133
216
|
AUTH_REQUIRED: 1e3,
|
|
134
217
|
AUTH_FAILED: 1001,
|
|
135
218
|
TOKEN_EXPIRED: 1002,
|
|
136
|
-
PERMISSION_DENIED: 1003
|
|
219
|
+
PERMISSION_DENIED: 1003,
|
|
220
|
+
INSUFFICIENT_SCOPE: 1004,
|
|
221
|
+
METHOD_NOT_SUPPORTED: 1005,
|
|
222
|
+
INVALID_CREDENTIALS: 1006
|
|
137
223
|
};
|
|
138
224
|
var ROUTING_ERROR_CODES = {
|
|
139
225
|
ADDRESS_NOT_FOUND: 2e3,
|
|
@@ -272,6 +358,27 @@ var MAPRequestError = class _MAPRequestError extends Error {
|
|
|
272
358
|
{ category: "auth" }
|
|
273
359
|
);
|
|
274
360
|
}
|
|
361
|
+
static insufficientScope(required) {
|
|
362
|
+
return new _MAPRequestError(
|
|
363
|
+
AUTH_ERROR_CODES.INSUFFICIENT_SCOPE,
|
|
364
|
+
required ? `Insufficient scope: ${required}` : "Insufficient scope",
|
|
365
|
+
{ category: "auth" }
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
static methodNotSupported(method) {
|
|
369
|
+
return new _MAPRequestError(
|
|
370
|
+
AUTH_ERROR_CODES.METHOD_NOT_SUPPORTED,
|
|
371
|
+
`Authentication method not supported: ${method}`,
|
|
372
|
+
{ category: "auth" }
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
static invalidCredentials(details) {
|
|
376
|
+
return new _MAPRequestError(
|
|
377
|
+
AUTH_ERROR_CODES.INVALID_CREDENTIALS,
|
|
378
|
+
details ?? "Invalid credentials",
|
|
379
|
+
{ category: "auth" }
|
|
380
|
+
);
|
|
381
|
+
}
|
|
275
382
|
// ==========================================================================
|
|
276
383
|
// Routing Errors (2xxx)
|
|
277
384
|
// ==========================================================================
|
|
@@ -1851,11 +1958,6 @@ var TestServer = class {
|
|
|
1851
1958
|
return false;
|
|
1852
1959
|
}
|
|
1853
1960
|
}
|
|
1854
|
-
if (filter.agents?.length && event.source) {
|
|
1855
|
-
if (!filter.agents.includes(event.source)) {
|
|
1856
|
-
return false;
|
|
1857
|
-
}
|
|
1858
|
-
}
|
|
1859
1961
|
if (filter.fromAgents?.length && event.source) {
|
|
1860
1962
|
if (!filter.fromAgents.includes(event.source)) {
|
|
1861
1963
|
return false;
|
|
@@ -2043,6 +2145,7 @@ var TestServer = class {
|
|
|
2043
2145
|
};
|
|
2044
2146
|
|
|
2045
2147
|
// src/stream/index.ts
|
|
2148
|
+
init_agentic_mesh();
|
|
2046
2149
|
function websocketStream(ws) {
|
|
2047
2150
|
const messageQueue = [];
|
|
2048
2151
|
let messageResolver = null;
|
|
@@ -3113,6 +3216,33 @@ var ACPStreamConnection = class extends EventEmitter {
|
|
|
3113
3216
|
});
|
|
3114
3217
|
}
|
|
3115
3218
|
// ===========================================================================
|
|
3219
|
+
// Extension Methods
|
|
3220
|
+
// ===========================================================================
|
|
3221
|
+
/**
|
|
3222
|
+
* Call an ACP extension method on the target agent.
|
|
3223
|
+
*
|
|
3224
|
+
* Extension methods are prefixed with "_" (e.g., "_macro/spawnAgent").
|
|
3225
|
+
* The agent must support the extension for this to succeed.
|
|
3226
|
+
*
|
|
3227
|
+
* @param method - The extension method name (e.g., "_macro/spawnAgent")
|
|
3228
|
+
* @param params - Parameters to pass to the extension method
|
|
3229
|
+
* @returns The result from the extension method
|
|
3230
|
+
*
|
|
3231
|
+
* @example
|
|
3232
|
+
* ```typescript
|
|
3233
|
+
* const result = await acp.callExtension("_macro/spawnAgent", {
|
|
3234
|
+
* task: "Implement feature X",
|
|
3235
|
+
* cwd: "/project"
|
|
3236
|
+
* });
|
|
3237
|
+
* ```
|
|
3238
|
+
*/
|
|
3239
|
+
async callExtension(method, params) {
|
|
3240
|
+
if (!this.#initialized) {
|
|
3241
|
+
throw new Error("Must call initialize() before callExtension()");
|
|
3242
|
+
}
|
|
3243
|
+
return this.#sendRequest(method, params);
|
|
3244
|
+
}
|
|
3245
|
+
// ===========================================================================
|
|
3116
3246
|
// Lifecycle
|
|
3117
3247
|
// ===========================================================================
|
|
3118
3248
|
/**
|
|
@@ -3151,6 +3281,8 @@ var ClientConnection = class _ClientConnection {
|
|
|
3151
3281
|
#connected = false;
|
|
3152
3282
|
#lastConnectOptions;
|
|
3153
3283
|
#isReconnecting = false;
|
|
3284
|
+
#lastResumeToken;
|
|
3285
|
+
#onTokenExpiring;
|
|
3154
3286
|
constructor(stream, options = {}) {
|
|
3155
3287
|
this.#connection = new BaseConnection(stream, options);
|
|
3156
3288
|
this.#options = options;
|
|
@@ -3216,11 +3348,70 @@ var ClientConnection = class _ClientConnection {
|
|
|
3216
3348
|
await client.connect({ auth: options?.auth });
|
|
3217
3349
|
return client;
|
|
3218
3350
|
}
|
|
3351
|
+
/**
|
|
3352
|
+
* Connect to a MAP server via agentic-mesh transport.
|
|
3353
|
+
*
|
|
3354
|
+
* Handles:
|
|
3355
|
+
* - Dynamic import of agentic-mesh (optional peer dependency)
|
|
3356
|
+
* - Stream creation over encrypted mesh tunnel
|
|
3357
|
+
* - Auto-configuration of createStream for reconnection
|
|
3358
|
+
* - Initial MAP protocol connect handshake
|
|
3359
|
+
*
|
|
3360
|
+
* Requires `agentic-mesh` to be installed as a peer dependency.
|
|
3361
|
+
*
|
|
3362
|
+
* @param options - Mesh connection options
|
|
3363
|
+
* @returns Connected ClientConnection instance
|
|
3364
|
+
*
|
|
3365
|
+
* @example
|
|
3366
|
+
* ```typescript
|
|
3367
|
+
* import { createNebulaTransport } from 'agentic-mesh';
|
|
3368
|
+
*
|
|
3369
|
+
* const transport = createNebulaTransport({
|
|
3370
|
+
* configPath: '/etc/nebula/config.yml',
|
|
3371
|
+
* });
|
|
3372
|
+
*
|
|
3373
|
+
* const client = await ClientConnection.connectMesh({
|
|
3374
|
+
* transport,
|
|
3375
|
+
* peer: { peerId: 'server', address: '10.0.0.1', port: 4242 },
|
|
3376
|
+
* localPeerId: 'my-client',
|
|
3377
|
+
* name: 'MeshClient',
|
|
3378
|
+
* reconnection: true
|
|
3379
|
+
* });
|
|
3380
|
+
*
|
|
3381
|
+
* const agents = await client.listAgents();
|
|
3382
|
+
* ```
|
|
3383
|
+
*/
|
|
3384
|
+
static async connectMesh(options) {
|
|
3385
|
+
const { agenticMeshStream: agenticMeshStream2 } = await Promise.resolve().then(() => (init_agentic_mesh(), agentic_mesh_exports));
|
|
3386
|
+
const streamConfig = {
|
|
3387
|
+
transport: options.transport,
|
|
3388
|
+
peer: options.peer,
|
|
3389
|
+
localPeerId: options.localPeerId,
|
|
3390
|
+
timeout: options.timeout
|
|
3391
|
+
};
|
|
3392
|
+
const stream = await agenticMeshStream2(streamConfig);
|
|
3393
|
+
const createStream = async () => agenticMeshStream2(streamConfig);
|
|
3394
|
+
const reconnection = options.reconnection === true ? { enabled: true } : typeof options.reconnection === "object" ? options.reconnection : void 0;
|
|
3395
|
+
const client = new _ClientConnection(stream, {
|
|
3396
|
+
name: options.name,
|
|
3397
|
+
capabilities: options.capabilities,
|
|
3398
|
+
createStream,
|
|
3399
|
+
reconnection
|
|
3400
|
+
});
|
|
3401
|
+
await client.connect({ auth: options.auth });
|
|
3402
|
+
return client;
|
|
3403
|
+
}
|
|
3219
3404
|
// ===========================================================================
|
|
3220
3405
|
// Connection Lifecycle
|
|
3221
3406
|
// ===========================================================================
|
|
3222
3407
|
/**
|
|
3223
3408
|
* Connect to the MAP system
|
|
3409
|
+
*
|
|
3410
|
+
* @param options - Connection options
|
|
3411
|
+
* @param options.sessionId - Specific session ID to use
|
|
3412
|
+
* @param options.resumeToken - Token to resume a previously disconnected session
|
|
3413
|
+
* @param options.auth - Authentication credentials
|
|
3414
|
+
* @param options.onTokenExpiring - Callback invoked before token expires for proactive refresh
|
|
3224
3415
|
*/
|
|
3225
3416
|
async connect(options) {
|
|
3226
3417
|
const params = {
|
|
@@ -3236,10 +3427,134 @@ var ClientConnection = class _ClientConnection {
|
|
|
3236
3427
|
this.#sessionId = result.sessionId;
|
|
3237
3428
|
this.#serverCapabilities = result.capabilities;
|
|
3238
3429
|
this.#connected = true;
|
|
3430
|
+
if (result.resumeToken) {
|
|
3431
|
+
this.#lastResumeToken = result.resumeToken;
|
|
3432
|
+
}
|
|
3433
|
+
if (options?.onTokenExpiring) {
|
|
3434
|
+
this.#onTokenExpiring = options.onTokenExpiring;
|
|
3435
|
+
this.#setupTokenExpiryMonitoring(result);
|
|
3436
|
+
}
|
|
3239
3437
|
this.#connection._transitionTo("connected");
|
|
3240
3438
|
this.#lastConnectOptions = options;
|
|
3241
3439
|
return result;
|
|
3242
3440
|
}
|
|
3441
|
+
/**
|
|
3442
|
+
* Get the resume token for this session.
|
|
3443
|
+
* Can be used to reconnect and restore session state after disconnection.
|
|
3444
|
+
*
|
|
3445
|
+
* @returns The resume token, or undefined if not available
|
|
3446
|
+
*/
|
|
3447
|
+
getResumeToken() {
|
|
3448
|
+
return this.#lastResumeToken;
|
|
3449
|
+
}
|
|
3450
|
+
/**
|
|
3451
|
+
* Reconnect to the server, optionally using a resume token to restore session.
|
|
3452
|
+
*
|
|
3453
|
+
* @param resumeToken - Token to resume previous session. If not provided, uses the last known token.
|
|
3454
|
+
* @returns Connect response result
|
|
3455
|
+
*
|
|
3456
|
+
* @example
|
|
3457
|
+
* ```typescript
|
|
3458
|
+
* // Save token before disconnect
|
|
3459
|
+
* const token = await client.disconnect();
|
|
3460
|
+
*
|
|
3461
|
+
* // Later, reconnect with the token
|
|
3462
|
+
* const result = await client.reconnect(token);
|
|
3463
|
+
* console.log('Reconnected:', result.reconnected);
|
|
3464
|
+
* ```
|
|
3465
|
+
*/
|
|
3466
|
+
async reconnect(resumeToken) {
|
|
3467
|
+
const tokenToUse = resumeToken ?? this.#lastResumeToken;
|
|
3468
|
+
return this.connect({
|
|
3469
|
+
...this.#lastConnectOptions,
|
|
3470
|
+
resumeToken: tokenToUse
|
|
3471
|
+
});
|
|
3472
|
+
}
|
|
3473
|
+
/**
|
|
3474
|
+
* Set up monitoring for token expiration
|
|
3475
|
+
*/
|
|
3476
|
+
#setupTokenExpiryMonitoring(connectResult) {
|
|
3477
|
+
const principal = connectResult.principal;
|
|
3478
|
+
if (!principal?.expiresAt || !this.#onTokenExpiring) {
|
|
3479
|
+
return;
|
|
3480
|
+
}
|
|
3481
|
+
const expiresAt = principal.expiresAt;
|
|
3482
|
+
const now = Date.now();
|
|
3483
|
+
const warningTime = expiresAt - 6e4;
|
|
3484
|
+
const delay = warningTime - now;
|
|
3485
|
+
if (delay > 0) {
|
|
3486
|
+
setTimeout(async () => {
|
|
3487
|
+
if (!this.#connected || !this.#onTokenExpiring) return;
|
|
3488
|
+
try {
|
|
3489
|
+
const newCredentials = await this.#onTokenExpiring(expiresAt);
|
|
3490
|
+
if (newCredentials) {
|
|
3491
|
+
const refreshResult = await this.refreshAuth({
|
|
3492
|
+
method: newCredentials.method,
|
|
3493
|
+
credential: newCredentials.credential
|
|
3494
|
+
});
|
|
3495
|
+
if (refreshResult.success && refreshResult.principal?.expiresAt) {
|
|
3496
|
+
this.#setupTokenExpiryMonitoring({
|
|
3497
|
+
...connectResult,
|
|
3498
|
+
principal: refreshResult.principal
|
|
3499
|
+
});
|
|
3500
|
+
}
|
|
3501
|
+
}
|
|
3502
|
+
} catch {
|
|
3503
|
+
}
|
|
3504
|
+
}, delay);
|
|
3505
|
+
}
|
|
3506
|
+
}
|
|
3507
|
+
/**
|
|
3508
|
+
* Authenticate with the server after connection.
|
|
3509
|
+
*
|
|
3510
|
+
* Use this when the server returns `authRequired` in the connect response,
|
|
3511
|
+
* indicating that authentication is needed before accessing protected resources.
|
|
3512
|
+
*
|
|
3513
|
+
* @param auth - Authentication credentials
|
|
3514
|
+
* @returns Authentication result with principal if successful
|
|
3515
|
+
*
|
|
3516
|
+
* @example
|
|
3517
|
+
* ```typescript
|
|
3518
|
+
* const connectResult = await client.connect();
|
|
3519
|
+
*
|
|
3520
|
+
* if (connectResult.authRequired) {
|
|
3521
|
+
* const authResult = await client.authenticate({
|
|
3522
|
+
* method: 'api-key',
|
|
3523
|
+
* credential: process.env.API_KEY,
|
|
3524
|
+
* });
|
|
3525
|
+
*
|
|
3526
|
+
* if (authResult.success) {
|
|
3527
|
+
* console.log('Authenticated as:', authResult.principal?.id);
|
|
3528
|
+
* }
|
|
3529
|
+
* }
|
|
3530
|
+
* ```
|
|
3531
|
+
*/
|
|
3532
|
+
async authenticate(auth) {
|
|
3533
|
+
const params = {
|
|
3534
|
+
method: auth.method,
|
|
3535
|
+
credential: auth.credential
|
|
3536
|
+
};
|
|
3537
|
+
const result = await this.#connection.sendRequest(AUTH_METHODS.AUTHENTICATE, params);
|
|
3538
|
+
if (result.success && result.sessionId) {
|
|
3539
|
+
this.#sessionId = result.sessionId;
|
|
3540
|
+
}
|
|
3541
|
+
return result;
|
|
3542
|
+
}
|
|
3543
|
+
/**
|
|
3544
|
+
* Refresh authentication credentials.
|
|
3545
|
+
*
|
|
3546
|
+
* Use this to update credentials before they expire for long-lived connections.
|
|
3547
|
+
*
|
|
3548
|
+
* @param auth - New authentication credentials
|
|
3549
|
+
* @returns Updated principal information
|
|
3550
|
+
*/
|
|
3551
|
+
async refreshAuth(auth) {
|
|
3552
|
+
const params = {
|
|
3553
|
+
method: auth.method,
|
|
3554
|
+
credential: auth.credential
|
|
3555
|
+
};
|
|
3556
|
+
return this.#connection.sendRequest(AUTH_METHODS.AUTH_REFRESH, params);
|
|
3557
|
+
}
|
|
3243
3558
|
/**
|
|
3244
3559
|
* Disconnect from the MAP system
|
|
3245
3560
|
* @param reason - Optional reason for disconnecting
|
|
@@ -3254,6 +3569,9 @@ var ClientConnection = class _ClientConnection {
|
|
|
3254
3569
|
reason ? { reason } : void 0
|
|
3255
3570
|
);
|
|
3256
3571
|
resumeToken = result.resumeToken;
|
|
3572
|
+
if (resumeToken) {
|
|
3573
|
+
this.#lastResumeToken = resumeToken;
|
|
3574
|
+
}
|
|
3257
3575
|
} finally {
|
|
3258
3576
|
for (const stream of this.#acpStreams.values()) {
|
|
3259
3577
|
await stream.close();
|
|
@@ -4138,6 +4456,66 @@ var AgentConnection = class _AgentConnection {
|
|
|
4138
4456
|
await agent.connect({ auth: options?.auth });
|
|
4139
4457
|
return agent;
|
|
4140
4458
|
}
|
|
4459
|
+
/**
|
|
4460
|
+
* Connect and register an agent via agentic-mesh transport.
|
|
4461
|
+
*
|
|
4462
|
+
* Handles:
|
|
4463
|
+
* - Dynamic import of agentic-mesh (optional peer dependency)
|
|
4464
|
+
* - Stream creation over encrypted mesh tunnel
|
|
4465
|
+
* - Auto-configuration of createStream for reconnection
|
|
4466
|
+
* - Initial MAP protocol connect handshake
|
|
4467
|
+
* - Agent registration
|
|
4468
|
+
*
|
|
4469
|
+
* Requires `agentic-mesh` to be installed as a peer dependency.
|
|
4470
|
+
*
|
|
4471
|
+
* @param options - Mesh connection and agent options
|
|
4472
|
+
* @returns Connected and registered AgentConnection instance
|
|
4473
|
+
*
|
|
4474
|
+
* @example
|
|
4475
|
+
* ```typescript
|
|
4476
|
+
* import { createNebulaTransport } from 'agentic-mesh';
|
|
4477
|
+
*
|
|
4478
|
+
* const transport = createNebulaTransport({
|
|
4479
|
+
* configPath: '/etc/nebula/config.yml',
|
|
4480
|
+
* });
|
|
4481
|
+
*
|
|
4482
|
+
* const agent = await AgentConnection.connectMesh({
|
|
4483
|
+
* transport,
|
|
4484
|
+
* peer: { peerId: 'server', address: '10.0.0.1', port: 4242 },
|
|
4485
|
+
* localPeerId: 'my-agent',
|
|
4486
|
+
* name: 'MeshWorker',
|
|
4487
|
+
* role: 'processor',
|
|
4488
|
+
* reconnection: true
|
|
4489
|
+
* });
|
|
4490
|
+
*
|
|
4491
|
+
* agent.onMessage(handleMessage);
|
|
4492
|
+
* await agent.busy();
|
|
4493
|
+
* ```
|
|
4494
|
+
*/
|
|
4495
|
+
static async connectMesh(options) {
|
|
4496
|
+
const { agenticMeshStream: agenticMeshStream2 } = await Promise.resolve().then(() => (init_agentic_mesh(), agentic_mesh_exports));
|
|
4497
|
+
const streamConfig = {
|
|
4498
|
+
transport: options.transport,
|
|
4499
|
+
peer: options.peer,
|
|
4500
|
+
localPeerId: options.localPeerId,
|
|
4501
|
+
timeout: options.timeout
|
|
4502
|
+
};
|
|
4503
|
+
const stream = await agenticMeshStream2(streamConfig);
|
|
4504
|
+
const createStream = async () => agenticMeshStream2(streamConfig);
|
|
4505
|
+
const reconnection = options.reconnection === true ? { enabled: true } : typeof options.reconnection === "object" ? options.reconnection : void 0;
|
|
4506
|
+
const agent = new _AgentConnection(stream, {
|
|
4507
|
+
name: options.name,
|
|
4508
|
+
role: options.role,
|
|
4509
|
+
capabilities: options.capabilities,
|
|
4510
|
+
visibility: options.visibility,
|
|
4511
|
+
parent: options.parent,
|
|
4512
|
+
scopes: options.scopes,
|
|
4513
|
+
createStream,
|
|
4514
|
+
reconnection
|
|
4515
|
+
});
|
|
4516
|
+
await agent.connect({ auth: options.auth });
|
|
4517
|
+
return agent;
|
|
4518
|
+
}
|
|
4141
4519
|
// ===========================================================================
|
|
4142
4520
|
// Connection Lifecycle
|
|
4143
4521
|
// ===========================================================================
|
|
@@ -4174,6 +4552,62 @@ var AgentConnection = class _AgentConnection {
|
|
|
4174
4552
|
this.#connection._transitionTo("connected");
|
|
4175
4553
|
return { connection: connectResult, agent: registerResult.agent };
|
|
4176
4554
|
}
|
|
4555
|
+
/**
|
|
4556
|
+
* Authenticate with the server after connection.
|
|
4557
|
+
*
|
|
4558
|
+
* Use this when the server returns `authRequired` in the connect response,
|
|
4559
|
+
* indicating that authentication is needed before registering or accessing
|
|
4560
|
+
* protected resources.
|
|
4561
|
+
*
|
|
4562
|
+
* @param auth - Authentication credentials
|
|
4563
|
+
* @returns Authentication result with principal if successful
|
|
4564
|
+
*
|
|
4565
|
+
* @example
|
|
4566
|
+
* ```typescript
|
|
4567
|
+
* const agent = new AgentConnection(stream, { name: 'MyAgent' });
|
|
4568
|
+
*
|
|
4569
|
+
* // First connect to get auth requirements
|
|
4570
|
+
* const connectResult = await agent.connectOnly();
|
|
4571
|
+
*
|
|
4572
|
+
* if (connectResult.authRequired) {
|
|
4573
|
+
* const authResult = await agent.authenticate({
|
|
4574
|
+
* method: 'api-key',
|
|
4575
|
+
* token: process.env.AGENT_API_KEY,
|
|
4576
|
+
* });
|
|
4577
|
+
*
|
|
4578
|
+
* if (authResult.success) {
|
|
4579
|
+
* // Now register the agent
|
|
4580
|
+
* await agent.register({ name: 'MyAgent', role: 'worker' });
|
|
4581
|
+
* }
|
|
4582
|
+
* }
|
|
4583
|
+
* ```
|
|
4584
|
+
*/
|
|
4585
|
+
async authenticate(auth) {
|
|
4586
|
+
const params = {
|
|
4587
|
+
method: auth.method,
|
|
4588
|
+
credential: auth.token
|
|
4589
|
+
};
|
|
4590
|
+
const result = await this.#connection.sendRequest(AUTH_METHODS.AUTHENTICATE, params);
|
|
4591
|
+
if (result.success && result.sessionId) {
|
|
4592
|
+
this.#sessionId = result.sessionId;
|
|
4593
|
+
}
|
|
4594
|
+
return result;
|
|
4595
|
+
}
|
|
4596
|
+
/**
|
|
4597
|
+
* Refresh authentication credentials.
|
|
4598
|
+
*
|
|
4599
|
+
* Use this to update credentials before they expire for long-lived connections.
|
|
4600
|
+
*
|
|
4601
|
+
* @param auth - New authentication credentials
|
|
4602
|
+
* @returns Updated principal information
|
|
4603
|
+
*/
|
|
4604
|
+
async refreshAuth(auth) {
|
|
4605
|
+
const params = {
|
|
4606
|
+
method: auth.method,
|
|
4607
|
+
credential: auth.token
|
|
4608
|
+
};
|
|
4609
|
+
return this.#connection.sendRequest(AUTH_METHODS.AUTH_REFRESH, params);
|
|
4610
|
+
}
|
|
4177
4611
|
/**
|
|
4178
4612
|
* Disconnect from the MAP system
|
|
4179
4613
|
* @param reason - Optional reason for disconnecting
|