@multi-agent-protocol/sdk 0.1.4 → 0.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/testing.cjs CHANGED
@@ -231,6 +231,7 @@ var TASK_METHODS = {
231
231
  var NOTIFICATION_METHODS = {
232
232
  EVENT: "map/event",
233
233
  MESSAGE: "map/message",
234
+ SEND: "map/send",
234
235
  /** Client acknowledges received events (for backpressure) */
235
236
  SUBSCRIBE_ACK: "map/subscribe.ack"};
236
237
  var PROTOCOL_ERROR_CODES = {
@@ -4482,7 +4483,8 @@ var ClientConnection = class _ClientConnection {
4482
4483
  }
4483
4484
  break;
4484
4485
  }
4485
- case NOTIFICATION_METHODS.MESSAGE: {
4486
+ case NOTIFICATION_METHODS.MESSAGE:
4487
+ case "map/send": {
4486
4488
  break;
4487
4489
  }
4488
4490
  default:
@@ -4826,7 +4828,9 @@ var AgentConnection = class _AgentConnection {
4826
4828
  constructor(stream, options = {}) {
4827
4829
  this.#connection = new BaseConnection(stream, options);
4828
4830
  this.#options = options;
4829
- this.#connection.setNotificationHandler(this.#handleNotification.bind(this));
4831
+ this.#connection.setNotificationHandler(
4832
+ this.#handleNotification.bind(this)
4833
+ );
4830
4834
  if (options.reconnection?.enabled && options.createStream) {
4831
4835
  this.#connection.onStateChange((newState) => {
4832
4836
  if (newState === "closed" && this.#connected && !this.#isReconnecting) {
@@ -4895,6 +4899,52 @@ var AgentConnection = class _AgentConnection {
4895
4899
  await agent.connect({ auth: options?.auth });
4896
4900
  return agent;
4897
4901
  }
4902
+ /**
4903
+ * Create an AgentConnection over WebSocket without performing the MAP handshake.
4904
+ *
4905
+ * Use this when you need to control the connection flow — e.g., to handle
4906
+ * server-driven auth negotiation before registration.
4907
+ *
4908
+ * @example
4909
+ * ```typescript
4910
+ * const agent = await AgentConnection.createConnection('ws://localhost:8080', {
4911
+ * name: 'Worker',
4912
+ * role: 'processor',
4913
+ * });
4914
+ *
4915
+ * const result = await agent.connectOnly();
4916
+ * if (result.authRequired) {
4917
+ * await agent.authenticate({ method: result.authRequired.methods[0], token: cred });
4918
+ * }
4919
+ * await agent.register();
4920
+ * ```
4921
+ */
4922
+ static async createConnection(url, options) {
4923
+ const parsedUrl = new URL(url);
4924
+ if (!["ws:", "wss:"].includes(parsedUrl.protocol)) {
4925
+ throw new Error(`Unsupported protocol: ${parsedUrl.protocol}. Use ws: or wss:`);
4926
+ }
4927
+ const timeout = options?.connectTimeout ?? 1e4;
4928
+ const ws = new WebSocket(url);
4929
+ await waitForOpen(ws, timeout);
4930
+ const stream = websocketStream(ws);
4931
+ const createStream = async () => {
4932
+ const newWs = new WebSocket(url);
4933
+ await waitForOpen(newWs, timeout);
4934
+ return websocketStream(newWs);
4935
+ };
4936
+ const reconnection = options?.reconnection === true ? { enabled: true } : typeof options?.reconnection === "object" ? options.reconnection : void 0;
4937
+ return new _AgentConnection(stream, {
4938
+ name: options?.name,
4939
+ role: options?.role,
4940
+ capabilities: options?.capabilities,
4941
+ visibility: options?.visibility,
4942
+ parent: options?.parent,
4943
+ scopes: options?.scopes,
4944
+ createStream,
4945
+ reconnection
4946
+ });
4947
+ }
4898
4948
  /**
4899
4949
  * Connect and register an agent via agentic-mesh transport.
4900
4950
  *
@@ -4991,6 +5041,65 @@ var AgentConnection = class _AgentConnection {
4991
5041
  this.#connection._transitionTo("connected");
4992
5042
  return { connection: connectResult, agent: registerResult.agent };
4993
5043
  }
5044
+ /**
5045
+ * Connect to the MAP system without registering an agent.
5046
+ *
5047
+ * Use this when the server may require authentication before registration.
5048
+ * After connecting, check `authRequired` in the response and call
5049
+ * `authenticate()` if needed, then `register()` to complete the handshake.
5050
+ *
5051
+ * @example
5052
+ * ```typescript
5053
+ * const result = await agent.connectOnly();
5054
+ *
5055
+ * if (result.authRequired) {
5056
+ * const method = result.authRequired.methods[0];
5057
+ * await agent.authenticate({ method, token: myCredential });
5058
+ * }
5059
+ *
5060
+ * await agent.register();
5061
+ * ```
5062
+ */
5063
+ async connectOnly(options) {
5064
+ const connectParams = {
5065
+ protocolVersion: PROTOCOL_VERSION,
5066
+ participantType: "agent",
5067
+ participantId: options?.agentId,
5068
+ name: this.#options.name,
5069
+ capabilities: this.#options.capabilities,
5070
+ resumeToken: options?.resumeToken,
5071
+ auth: options?.auth
5072
+ };
5073
+ const connectResult = await this.#connection.sendRequest(CORE_METHODS.CONNECT, connectParams);
5074
+ this.#sessionId = connectResult.sessionId;
5075
+ this.#serverCapabilities = connectResult.capabilities;
5076
+ this.#connected = true;
5077
+ this.#lastConnectOptions = options;
5078
+ return connectResult;
5079
+ }
5080
+ /**
5081
+ * Register as an agent after connecting.
5082
+ *
5083
+ * Call this after `connectOnly()` and optional `authenticate()` to complete
5084
+ * the connection handshake. Uses the options provided during construction
5085
+ * (name, role, scopes, etc.) unless overridden.
5086
+ */
5087
+ async register(overrides) {
5088
+ const registerParams = {
5089
+ agentId: overrides?.agentId ?? this.#lastConnectOptions?.agentId,
5090
+ name: overrides?.name ?? this.#options.name,
5091
+ role: overrides?.role ?? this.#options.role,
5092
+ parent: this.#options.parent,
5093
+ scopes: this.#options.scopes,
5094
+ visibility: this.#options.visibility,
5095
+ capabilities: this.#options.capabilities
5096
+ };
5097
+ const registerResult = await this.#connection.sendRequest(LIFECYCLE_METHODS.AGENTS_REGISTER, registerParams);
5098
+ this.#agentId = registerResult.agent.id;
5099
+ this.#currentState = registerResult.agent.state;
5100
+ this.#connection._transitionTo("connected");
5101
+ return registerResult.agent;
5102
+ }
4994
5103
  /**
4995
5104
  * Authenticate with the server after connection.
4996
5105
  *
@@ -5003,20 +5112,15 @@ var AgentConnection = class _AgentConnection {
5003
5112
  *
5004
5113
  * @example
5005
5114
  * ```typescript
5006
- * const agent = new AgentConnection(stream, { name: 'MyAgent' });
5007
- *
5008
- * // First connect to get auth requirements
5009
- * const connectResult = await agent.connectOnly();
5115
+ * const result = await agent.connectOnly();
5010
5116
  *
5011
- * if (connectResult.authRequired) {
5117
+ * if (result.authRequired) {
5012
5118
  * const authResult = await agent.authenticate({
5013
- * method: 'api-key',
5014
- * token: process.env.AGENT_API_KEY,
5119
+ * method: result.authRequired.methods[0],
5120
+ * token: myCredential,
5015
5121
  * });
5016
- *
5017
5122
  * if (authResult.success) {
5018
- * // Now register the agent
5019
- * await agent.register({ name: 'MyAgent', role: 'worker' });
5123
+ * await agent.register();
5020
5124
  * }
5021
5125
  * }
5022
5126
  * ```
@@ -5062,10 +5166,7 @@ var AgentConnection = class _AgentConnection {
5062
5166
  reason
5063
5167
  });
5064
5168
  }
5065
- const result = await this.#connection.sendRequest(
5066
- CORE_METHODS.DISCONNECT,
5067
- reason ? { reason } : void 0
5068
- );
5169
+ const result = await this.#connection.sendRequest(CORE_METHODS.DISCONNECT, reason ? { reason } : void 0);
5069
5170
  resumeToken = result.resumeToken;
5070
5171
  } finally {
5071
5172
  for (const subscription of this.#subscriptions.values()) {
@@ -5381,10 +5482,7 @@ var AgentConnection = class _AgentConnection {
5381
5482
  * @returns Created conversation and participant info
5382
5483
  */
5383
5484
  async createConversation(params) {
5384
- return this.#connection.sendRequest(
5385
- MAIL_METHODS.MAIL_CREATE,
5386
- params ?? {}
5387
- );
5485
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_CREATE, params ?? {});
5388
5486
  }
5389
5487
  /**
5390
5488
  * Get a conversation by ID with optional includes.
@@ -5394,10 +5492,7 @@ var AgentConnection = class _AgentConnection {
5394
5492
  * @returns Conversation details with requested includes
5395
5493
  */
5396
5494
  async getConversation(conversationId, include) {
5397
- return this.#connection.sendRequest(
5398
- MAIL_METHODS.MAIL_GET,
5399
- { conversationId, include }
5400
- );
5495
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_GET, { conversationId, include });
5401
5496
  }
5402
5497
  /**
5403
5498
  * List conversations with optional filters.
@@ -5406,10 +5501,7 @@ var AgentConnection = class _AgentConnection {
5406
5501
  * @returns Paginated list of conversations
5407
5502
  */
5408
5503
  async listConversations(params) {
5409
- return this.#connection.sendRequest(
5410
- MAIL_METHODS.MAIL_LIST,
5411
- params ?? {}
5412
- );
5504
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_LIST, params ?? {});
5413
5505
  }
5414
5506
  /**
5415
5507
  * Close a conversation.
@@ -5419,10 +5511,7 @@ var AgentConnection = class _AgentConnection {
5419
5511
  * @returns The closed conversation
5420
5512
  */
5421
5513
  async closeConversation(conversationId, reason) {
5422
- return this.#connection.sendRequest(
5423
- MAIL_METHODS.MAIL_CLOSE,
5424
- { conversationId, reason }
5425
- );
5514
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_CLOSE, { conversationId, reason });
5426
5515
  }
5427
5516
  /**
5428
5517
  * Join an existing conversation.
@@ -5431,10 +5520,7 @@ var AgentConnection = class _AgentConnection {
5431
5520
  * @returns Conversation, participant, and optional history
5432
5521
  */
5433
5522
  async joinConversation(params) {
5434
- return this.#connection.sendRequest(
5435
- MAIL_METHODS.MAIL_JOIN,
5436
- params
5437
- );
5523
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_JOIN, params);
5438
5524
  }
5439
5525
  /**
5440
5526
  * Leave a conversation.
@@ -5444,10 +5530,7 @@ var AgentConnection = class _AgentConnection {
5444
5530
  * @returns Leave confirmation with timestamp
5445
5531
  */
5446
5532
  async leaveConversation(conversationId, reason) {
5447
- return this.#connection.sendRequest(
5448
- MAIL_METHODS.MAIL_LEAVE,
5449
- { conversationId, reason }
5450
- );
5533
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_LEAVE, { conversationId, reason });
5451
5534
  }
5452
5535
  /**
5453
5536
  * Invite a participant to a conversation.
@@ -5456,10 +5539,7 @@ var AgentConnection = class _AgentConnection {
5456
5539
  * @returns Invite result
5457
5540
  */
5458
5541
  async inviteToConversation(params) {
5459
- return this.#connection.sendRequest(
5460
- MAIL_METHODS.MAIL_INVITE,
5461
- params
5462
- );
5542
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_INVITE, params);
5463
5543
  }
5464
5544
  /**
5465
5545
  * Record a turn (message) in a conversation.
@@ -5468,10 +5548,7 @@ var AgentConnection = class _AgentConnection {
5468
5548
  * @returns The created turn
5469
5549
  */
5470
5550
  async recordTurn(params) {
5471
- return this.#connection.sendRequest(
5472
- MAIL_METHODS.MAIL_TURN,
5473
- params
5474
- );
5551
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_TURN, params);
5475
5552
  }
5476
5553
  /**
5477
5554
  * List turns in a conversation with optional filters.
@@ -5480,10 +5557,7 @@ var AgentConnection = class _AgentConnection {
5480
5557
  * @returns Paginated list of turns
5481
5558
  */
5482
5559
  async listTurns(params) {
5483
- return this.#connection.sendRequest(
5484
- MAIL_METHODS.MAIL_TURNS_LIST,
5485
- params
5486
- );
5560
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_TURNS_LIST, params);
5487
5561
  }
5488
5562
  /**
5489
5563
  * Create a thread in a conversation.
@@ -5492,10 +5566,7 @@ var AgentConnection = class _AgentConnection {
5492
5566
  * @returns The created thread
5493
5567
  */
5494
5568
  async createThread(params) {
5495
- return this.#connection.sendRequest(
5496
- MAIL_METHODS.MAIL_THREAD_CREATE,
5497
- params
5498
- );
5569
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_THREAD_CREATE, params);
5499
5570
  }
5500
5571
  /**
5501
5572
  * List threads in a conversation.
@@ -5504,10 +5575,7 @@ var AgentConnection = class _AgentConnection {
5504
5575
  * @returns Paginated list of threads
5505
5576
  */
5506
5577
  async listThreads(params) {
5507
- return this.#connection.sendRequest(
5508
- MAIL_METHODS.MAIL_THREAD_LIST,
5509
- params
5510
- );
5578
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_THREAD_LIST, params);
5511
5579
  }
5512
5580
  /**
5513
5581
  * Get a summary of a conversation.
@@ -5516,10 +5584,7 @@ var AgentConnection = class _AgentConnection {
5516
5584
  * @returns Generated summary with optional key points, decisions, and questions
5517
5585
  */
5518
5586
  async getConversationSummary(params) {
5519
- return this.#connection.sendRequest(
5520
- MAIL_METHODS.MAIL_SUMMARY,
5521
- params
5522
- );
5587
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_SUMMARY, params);
5523
5588
  }
5524
5589
  /**
5525
5590
  * Replay turns from a conversation, optionally from a specific point.
@@ -5528,10 +5593,7 @@ var AgentConnection = class _AgentConnection {
5528
5593
  * @returns Replayed turns with pagination info
5529
5594
  */
5530
5595
  async replayConversation(params) {
5531
- return this.#connection.sendRequest(
5532
- MAIL_METHODS.MAIL_REPLAY,
5533
- params
5534
- );
5596
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_REPLAY, params);
5535
5597
  }
5536
5598
  /**
5537
5599
  * Send a message to an agent with mail context attached.
@@ -5578,10 +5640,7 @@ var AgentConnection = class _AgentConnection {
5578
5640
  * @returns The created task
5579
5641
  */
5580
5642
  async createTask(params) {
5581
- return this.#connection.sendRequest(
5582
- TASK_METHODS.TASKS_CREATE,
5583
- params
5584
- );
5643
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_CREATE, params);
5585
5644
  }
5586
5645
  /**
5587
5646
  * Assign a task to an agent.
@@ -5591,10 +5650,7 @@ var AgentConnection = class _AgentConnection {
5591
5650
  * @returns The updated task
5592
5651
  */
5593
5652
  async assignTask(taskId, agentId) {
5594
- return this.#connection.sendRequest(
5595
- TASK_METHODS.TASKS_ASSIGN,
5596
- { taskId, agentId }
5597
- );
5653
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_ASSIGN, { taskId, agentId });
5598
5654
  }
5599
5655
  /**
5600
5656
  * Update a task's status or fields.
@@ -5603,10 +5659,7 @@ var AgentConnection = class _AgentConnection {
5603
5659
  * @returns The updated task
5604
5660
  */
5605
5661
  async updateTask(params) {
5606
- return this.#connection.sendRequest(
5607
- TASK_METHODS.TASKS_UPDATE,
5608
- params
5609
- );
5662
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_UPDATE, params);
5610
5663
  }
5611
5664
  /**
5612
5665
  * List tasks with optional filters.
@@ -5615,10 +5668,7 @@ var AgentConnection = class _AgentConnection {
5615
5668
  * @returns Paginated list of tasks
5616
5669
  */
5617
5670
  async listTasks(params) {
5618
- return this.#connection.sendRequest(
5619
- TASK_METHODS.TASKS_LIST,
5620
- params ?? {}
5621
- );
5671
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_LIST, params ?? {});
5622
5672
  }
5623
5673
  // ===========================================================================
5624
5674
  // Internal
@@ -5630,17 +5680,21 @@ var AgentConnection = class _AgentConnection {
5630
5680
  switch (method) {
5631
5681
  case NOTIFICATION_METHODS.EVENT: {
5632
5682
  const eventParams = params;
5633
- const subscription = this.#subscriptions.get(eventParams.subscriptionId);
5683
+ const subscription = this.#subscriptions.get(
5684
+ eventParams.subscriptionId
5685
+ );
5634
5686
  if (subscription) {
5635
5687
  subscription._pushEvent(eventParams);
5636
5688
  }
5637
5689
  break;
5638
5690
  }
5639
- case NOTIFICATION_METHODS.MESSAGE: {
5691
+ case NOTIFICATION_METHODS.MESSAGE:
5692
+ case NOTIFICATION_METHODS.SEND: {
5640
5693
  const messageParams = params;
5694
+ const message = messageParams.message ?? messageParams;
5641
5695
  for (const handler of this.#messageHandlers) {
5642
5696
  try {
5643
- await handler(messageParams.message);
5697
+ await handler(message);
5644
5698
  } catch (error) {
5645
5699
  console.error("MAP: Message handler error:", error);
5646
5700
  }
@@ -5733,7 +5787,11 @@ var AgentConnection = class _AgentConnection {
5733
5787
  try {
5734
5788
  await this.joinScope(scopeId);
5735
5789
  } catch (error) {
5736
- console.warn("MAP: Failed to restore scope membership:", scopeId, error);
5790
+ console.warn(
5791
+ "MAP: Failed to restore scope membership:",
5792
+ scopeId,
5793
+ error
5794
+ );
5737
5795
  }
5738
5796
  }
5739
5797
  }