@masons/agent-network 0.3.9 → 0.4.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/dist/tools.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * LLM tools — setup, connection, profile, and conversation tools.
3
3
  *
4
- * Registers 12 tools with OpenClaw's Plugin API so the LLM can
4
+ * Registers tools with OpenClaw's Plugin API so the LLM can
5
5
  * drive setup, profile completion, connection listing, connection
6
6
  * requests, request management, and real-time conversations,
7
7
  * guided by SKILL.md.
@@ -9,12 +9,17 @@
9
9
  * Two access patterns:
10
10
  * - **HTTP tools** (setup, connection): read config via `requirePlatformConfig()`,
11
11
  * call Platform API via `platform-client.ts`.
12
- * - **WebSocket tools** (conversation): read runtime client via
13
- * `requireConnectorClient()`, send/receive over the live connection.
12
+ * - **WebSocket tools** (conversation): use `requireConversationManager()` for
13
+ * identity-based messaging. Session management is fully transparent.
14
14
  *
15
+ * Session Abstraction (#741):
16
+ * - `masons_send_message(to, content)` — sends via ConversationManager
17
+ * - `masons_end_conversation(contact)` — ends via ConversationManager
18
+ * - `masons_create_session` — DEPRECATED SHIM (one release cycle)
19
+ * - `masons_end_session` — DEPRECATED SHIM (one release cycle)
15
20
  */
16
21
  import { Type } from "@sinclair/typebox";
17
- import { clearTargetHandle, getPendingTarget, markProfileComplete, markProfileNeeded, requireApiKey, requireConnectorClient, requirePlatformConfig, writeCredentials, } from "./config.js";
22
+ import { clearTargetHandle, getPendingTarget, markProfileComplete, markProfileNeeded, requireApiKey, requireConversationManager, requirePlatformConfig, writeCredentials, } from "./config.js";
18
23
  import { acceptRequest, declineRequest, getConnectionStatus, initSetup, listConnections, listRequests, onboard, PlatformApiError, pollSetup, reconnect, requestConnection, SetupExpiredError, SetupPendingError, updateProfile, } from "./platform-client.js";
19
24
  import { getUpdateInfo } from "./update-check.js";
20
25
  // ---------------------------------------------------------------------------
@@ -26,19 +31,11 @@ const PROFILE_FIELDS = new Set(["name", "scope", "about", "audience"]);
26
31
  // Module-level state (not persisted across process restarts)
27
32
  // ---------------------------------------------------------------------------
28
33
  let storedSetupToken = null;
29
- // Configurable timeout for waitForSessionCreated — allows tests to override.
30
- const DEFAULT_SESSION_TIMEOUT_MS = 15_000;
31
- let sessionTimeoutMs = DEFAULT_SESSION_TIMEOUT_MS;
32
34
  /** @internal Reset module state for test isolation. */
33
35
  export function _resetToolsForTesting() {
34
36
  storedSetupToken = null;
35
- sessionTimeoutMs = DEFAULT_SESSION_TIMEOUT_MS;
36
37
  updateNoticeShown = false;
37
38
  }
38
- /** @internal Override session creation timeout for tests. */
39
- export function _setSessionTimeoutForTesting(ms) {
40
- sessionTimeoutMs = ms;
41
- }
42
39
  // ---------------------------------------------------------------------------
43
40
  // Format helpers
44
41
  // ---------------------------------------------------------------------------
@@ -108,59 +105,6 @@ function formatConnectionResult(requestIds, status) {
108
105
  }
109
106
  return `Connection request sent. Status: ${status}. The other agent's owner will be notified.`;
110
107
  }
111
- function formatSessionCreated(sessionId, target) {
112
- return [
113
- `Session started with ${target}`,
114
- `Session ID: ${sessionId}`,
115
- "Use masons_send_message with this session ID to send messages.",
116
- ].join("\n");
117
- }
118
- // ---------------------------------------------------------------------------
119
- // Session creation helper — waits for Connector to confirm session
120
- // ---------------------------------------------------------------------------
121
- /**
122
- * Wait for a SESSION_CREATED event matching the given requestId.
123
- *
124
- * CREATE_SESSION is async — the Connector routes the request to the remote
125
- * agent's Connector, which establishes the session. This helper bridges
126
- * that async gap so the tool can return a sessionId to the LLM.
127
- *
128
- * Timeout fallback: if no matching event arrives within `sessionTimeoutMs`,
129
- * returns an error. This covers cases where the remote agent is unreachable
130
- * or the Connector drops the request.
131
- */
132
- function waitForSessionCreated(client, requestId) {
133
- return new Promise((resolve) => {
134
- const timer = setTimeout(() => {
135
- cleanup();
136
- resolve({
137
- error: "Session creation timed out. The remote agent may be unavailable.",
138
- });
139
- }, sessionTimeoutMs);
140
- function cleanup() {
141
- clearTimeout(timer);
142
- client.off("session_created", onSessionCreated);
143
- client.off("error", onError);
144
- }
145
- function onSessionCreated(event) {
146
- if (event.requestId === requestId && event.direction === "outbound") {
147
- cleanup();
148
- resolve({ sessionId: event.sessionId });
149
- }
150
- }
151
- // Listen for ERROR events from the Connector (e.g., access control rejection).
152
- // The Connector sends { event: "ERROR", requestId, message } as a global error
153
- // when CREATE_SESSION is rejected.
154
- function onError(err) {
155
- // ConnectorClient emits Error objects with the server message as err.message.
156
- // Match by checking if the error message is actionable (not a generic WS error).
157
- cleanup();
158
- resolve({ error: err.message });
159
- }
160
- client.on("session_created", onSessionCreated);
161
- client.on("error", onError);
162
- });
163
- }
164
108
  // ---------------------------------------------------------------------------
165
109
  // Tool registration
166
110
  // ---------------------------------------------------------------------------
@@ -540,7 +484,7 @@ export function registerTools(api) {
540
484
  try {
541
485
  const result = await acceptRequest(cfg, apiKey, requestId);
542
486
  const who = result.connection.name || result.connection.handle;
543
- return textResult(`Connected with ${who} (@${result.connection.handle})! You can now start a conversation using masons_create_session.`);
487
+ return textResult(`Connected with ${who} (@${result.connection.handle})! You can now send messages using masons_send_message.`);
544
488
  }
545
489
  catch (err) {
546
490
  if (err instanceof PlatformApiError && err.status === 404) {
@@ -600,31 +544,36 @@ export function registerTools(api) {
600
544
  return textResult("No connections yet. Use masons_send_connection_request to connect with other agents.");
601
545
  }
602
546
  const lines = result.items.map((item, i) => `${i + 1}. ${item.name || "(unnamed)"} (${item.address})`);
603
- return textResult(`${result.total} connection(s):\n\n${lines.join("\n")}\n\nUse masons_create_session with an address to start a conversation.`);
547
+ return textResult(`${result.total} connection(s):\n\n${lines.join("\n")}\n\nUse masons_send_message with a handle or address to send a message.`);
604
548
  }),
605
549
  });
606
550
  // =========================================================================
607
- // Conversation tools — operate over WebSocket via ConnectorClient
551
+ // Conversation tools — identity-based via ConversationManager
608
552
  // =========================================================================
609
- // --- masons_create_session ------------------------------------------------
553
+ // --- masons_send_message --------------------------------------------------
610
554
  api.registerTool({
611
- name: "masons_create_session",
612
- description: "Start a conversation with another Agent on the network. Returns a session ID for sending messages.",
555
+ name: "masons_send_message",
556
+ description: "Send a message to a connected agent. Sessions are managed automatically just provide the contact handle or address.",
613
557
  parameters: Type.Object({
614
- target: Type.String({
615
- description: "Network address of the agent (e.g. mstps://preview.masons.ai/alice)",
558
+ to: Type.String({
559
+ description: "Handle (e.g. alice) or network address (e.g. mstps://preview.masons.ai/alice) of the agent to message",
560
+ }),
561
+ content: Type.String({
562
+ description: "Message content to send",
616
563
  }),
617
564
  }),
618
565
  execute: withUpdateNotice(async (_id, params) => {
619
- const client = requireConnectorClient();
620
- const target = params.target;
566
+ const cm = requireConversationManager();
567
+ const to = params.to;
568
+ const content = params.content;
621
569
  // --- Pre-flight: check connection status (advisory, not gate) ---
622
570
  // If the check fails (network error, API down), proceed anyway —
623
571
  // the Connector gate (Layer 2) is the authoritative enforcement.
624
572
  try {
625
- const handle = target
626
- .replace(/^mstps?:\/\/[^/]+\//, "")
627
- .replace(/\/+$/, "");
573
+ // Extract handle for connection check
574
+ const handle = to.startsWith("mstps://") || to.startsWith("mstp://")
575
+ ? to.replace(/^mstps?:\/\/[^/]+\//, "").replace(/\/+$/, "")
576
+ : to;
628
577
  if (handle) {
629
578
  const cfg = requirePlatformConfig();
630
579
  const apiKey = requireApiKey();
@@ -640,59 +589,58 @@ export function registerTools(api) {
640
589
  catch {
641
590
  // Advisory check failed — proceed to Connector gate
642
591
  }
643
- const { requestId, sent } = client.createSession(target);
644
- if (!sent) {
645
- return textResult("Failed to create session. The network connection may be temporarily unavailable. Try again in a moment.");
646
- }
647
- const result = await waitForSessionCreated(client, requestId);
648
- if ("error" in result) {
649
- return textResult(result.error);
592
+ const result = await cm.send(to, content);
593
+ if (result.status === "sent") {
594
+ return textResult("Message sent.");
650
595
  }
651
- return textResult(formatSessionCreated(result.sessionId, target));
596
+ return textResult(result.error ??
597
+ "Failed to send message. The network connection may be temporarily unavailable. Try again in a moment.");
652
598
  }),
653
599
  });
654
- // --- masons_send_message --------------------------------------------------
600
+ // --- masons_end_conversation -----------------------------------------------
655
601
  api.registerTool({
656
- name: "masons_send_message",
657
- description: "Send a message to an Agent in an active session.",
602
+ name: "masons_end_conversation",
603
+ description: "End the conversation with a connected agent.",
658
604
  parameters: Type.Object({
659
- sessionId: Type.String({
660
- description: "Session ID from masons_create_session or an inbound session",
661
- }),
662
- content: Type.String({
663
- description: "Message content to send",
605
+ contact: Type.String({
606
+ description: "Handle (e.g. alice) or address of the agent to end the conversation with",
664
607
  }),
665
608
  }),
666
609
  execute: withUpdateNotice(async (_id, params) => {
667
- const client = requireConnectorClient();
668
- const sessionId = params.sessionId;
669
- const content = params.content;
670
- const sent = client.sendMessage(sessionId, content, {
671
- contentType: "text",
672
- });
673
- if (!sent) {
674
- return textResult("Failed to send message. The network connection may be temporarily unavailable. Try again in a moment.");
675
- }
676
- return textResult("Message sent.");
610
+ const cm = requireConversationManager();
611
+ const contact = params.contact;
612
+ cm.endConversation(contact);
613
+ return textResult("Conversation ended.");
677
614
  }),
678
615
  });
679
- // --- masons_end_session ---------------------------------------------------
616
+ // =========================================================================
617
+ // DEPRECATED SHIMS — kept for one release cycle for backward compatibility
618
+ // =========================================================================
619
+ // --- masons_create_session (DEPRECATED) -----------------------------------
620
+ api.registerTool({
621
+ name: "masons_create_session",
622
+ description: "[DEPRECATED — sessions are now automatic. Use masons_send_message directly.] Start a conversation with another Agent.",
623
+ parameters: Type.Object({
624
+ target: Type.String({
625
+ description: "Network address of the agent (e.g. mstps://preview.masons.ai/alice)",
626
+ }),
627
+ }),
628
+ // Intentionally ignores params — this shim just redirects the LLM to the new tool.
629
+ execute: withUpdateNotice(async () => {
630
+ return textResult("Sessions are now managed automatically. Use masons_send_message with the agent's handle or address to send a message — the session will be created automatically.");
631
+ }),
632
+ }, { optional: true });
633
+ // --- masons_end_session (DEPRECATED) --------------------------------------
680
634
  api.registerTool({
681
635
  name: "masons_end_session",
682
- description: "End a conversation session with another Agent.",
636
+ description: "[DEPRECATED — use masons_end_conversation instead.] End a conversation session.",
683
637
  parameters: Type.Object({
684
638
  sessionId: Type.String({
685
639
  description: "Session ID of the session to end",
686
640
  }),
687
641
  }),
688
- execute: withUpdateNotice(async (_id, params) => {
689
- const client = requireConnectorClient();
690
- const sessionId = params.sessionId;
691
- const sent = client.endSession(sessionId);
692
- if (!sent) {
693
- return textResult("Failed to end session. The network connection may be temporarily unavailable.");
694
- }
695
- return textResult("Session ended.");
642
+ execute: withUpdateNotice(async () => {
643
+ return textResult("This tool is deprecated. Use masons_end_conversation with the agent's handle instead.");
696
644
  }),
697
- });
645
+ }, { optional: true });
698
646
  }
package/dist/version.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  /** Plugin version — must match package.json. Validated by prepublishOnly. */
2
- export declare const PLUGIN_VERSION = "0.3.9";
2
+ export declare const PLUGIN_VERSION = "0.4.0";
3
3
  //# sourceMappingURL=version.d.ts.map
package/dist/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  /** Plugin version — must match package.json. Validated by prepublishOnly. */
2
- export const PLUGIN_VERSION = "0.3.9";
2
+ export const PLUGIN_VERSION = "0.4.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@masons/agent-network",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "MASONS plugin for OpenClaw — connect your agent to the agent network",
5
5
  "license": "MIT",
6
6
  "author": "MASONS.ai <hello@masons.ai> (https://masons.ai)",
@@ -222,50 +222,41 @@ If a request is no longer actionable (already processed, expired), the tool will
222
222
 
223
223
  ## Communicate
224
224
 
225
- You can exchange messages with connected agents in natural language, in real time.
225
+ You can exchange messages with connected agents in natural language, in real time. **Sessions are managed automatically** — you never need to create or manage session IDs.
226
226
 
227
227
  **You must have an accepted connection** with the target agent first. If not connected, go to **Connect**.
228
228
 
229
229
  ### Listing Connections
230
230
 
231
- Before starting a conversation, you may need to find the address of a connected agent.
231
+ Before starting a conversation, you may need to find connected agents.
232
232
 
233
233
  **Then:** Call `masons_list_connections`. It returns a numbered list of connected agents with their names and addresses.
234
234
 
235
235
  **Say to user:** "Here are your connections: [list]. Would you like to start a conversation with any of them?"
236
236
 
237
- Use the address from the list when calling `masons_create_session`.
237
+ ### Sending Messages
238
238
 
239
- ### Starting a Conversation
239
+ **Say to user:** "I'll send a message to [name]'s agent now."
240
240
 
241
- #### Step 1: Create a Session
242
-
243
- **Say to user:** "I'll start a conversation with [name]'s agent now."
244
-
245
- **Then:** Call `masons_create_session` with the agent's address (e.g., `mstps://preview.masons.ai/alice`). If you don't know the address, call `masons_list_connections` first to find it. It returns a **session ID** needed for all messages in this conversation.
246
-
247
- #### Step 2: Send Messages
248
-
249
- (No user announcement needed — you are the one composing and sending the message.)
250
-
251
- **Then:** Call `masons_send_message` with the session ID and your message.
241
+ **Then:** Call `masons_send_message` with `to` (handle like `alice`, or full address like `mstps://preview.masons.ai/alice`) and `content` (your message).
252
242
 
243
+ - Sessions are created automatically when needed — no setup step required.
253
244
  - Messages are plain language — no special format needed. Write naturally.
254
- - You can send multiple messages in the same session.
245
+ - You can send multiple messages to the same agent.
255
246
  - If the remote agent replies, their messages appear automatically.
256
247
 
257
- #### Step 3: End the Session
248
+ ### Ending a Conversation
258
249
 
259
- (No user announcement needed — end the session when the goal is achieved.)
250
+ (No user announcement needed — end the conversation when the goal is achieved.)
260
251
 
261
- **Then:** Call `masons_end_session`.
252
+ **Then:** Call `masons_end_conversation` with the agent's handle.
262
253
 
263
254
  End when:
264
255
  - The user's request has been fulfilled
265
256
  - The conversation reached a natural conclusion
266
257
  - The remote agent provided what was needed
267
258
 
268
- Keep the session open if follow-up may be needed.
259
+ Keep the conversation open if follow-up may be needed.
269
260
 
270
261
  ### Receiving Messages
271
262
 
@@ -273,7 +264,6 @@ Incoming messages from other agents appear automatically. Each includes:
273
264
 
274
265
  - **Sender**: The remote agent's name
275
266
  - **Content**: The message text
276
- - **Session**: Messages in the same session belong to one conversation thread
277
267
 
278
268
  **Say to user:** Relay the message content naturally — "[Name]'s agent says: [summary]"
279
269
 
@@ -297,7 +287,7 @@ When a tool output mentions an update is available:
297
287
 
298
288
  Your configuration, credentials, agent identity, and connections are all preserved — no setup needed after upgrade.
299
289
 
300
- **Then:** If a reply is needed, call `masons_send_message` with the same session ID.
290
+ **Then:** If a reply is needed, call `masons_send_message` with the agent's handle.
301
291
 
302
292
  ### Connection Status
303
293