@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/channel.d.ts.map +1 -1
- package/dist/channel.js +65 -73
- package/dist/config.d.ts +19 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +32 -0
- package/dist/conversation-manager.d.ts +91 -0
- package/dist/conversation-manager.d.ts.map +1 -0
- package/dist/conversation-manager.js +189 -0
- package/dist/plugin.js +2 -2
- package/dist/session-lifecycle.d.ts +89 -0
- package/dist/session-lifecycle.d.ts.map +1 -0
- package/dist/session-lifecycle.js +348 -0
- package/dist/tools.d.ts +8 -5
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +63 -115
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/skills/agent-network/SKILL.md +12 -22
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
|
|
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):
|
|
13
|
-
*
|
|
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,
|
|
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
|
|
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
|
|
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 —
|
|
551
|
+
// Conversation tools — identity-based via ConversationManager
|
|
608
552
|
// =========================================================================
|
|
609
|
-
// ---
|
|
553
|
+
// --- masons_send_message --------------------------------------------------
|
|
610
554
|
api.registerTool({
|
|
611
|
-
name: "
|
|
612
|
-
description: "
|
|
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
|
-
|
|
615
|
-
description: "
|
|
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
|
|
620
|
-
const
|
|
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
|
-
|
|
626
|
-
|
|
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
|
|
644
|
-
if (
|
|
645
|
-
return textResult("
|
|
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(
|
|
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
|
-
// ---
|
|
600
|
+
// --- masons_end_conversation -----------------------------------------------
|
|
655
601
|
api.registerTool({
|
|
656
|
-
name: "
|
|
657
|
-
description: "
|
|
602
|
+
name: "masons_end_conversation",
|
|
603
|
+
description: "End the conversation with a connected agent.",
|
|
658
604
|
parameters: Type.Object({
|
|
659
|
-
|
|
660
|
-
description: "
|
|
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
|
|
668
|
-
const
|
|
669
|
-
|
|
670
|
-
|
|
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
|
-
//
|
|
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
|
|
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 (
|
|
689
|
-
|
|
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
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.
|
|
2
|
+
export const PLUGIN_VERSION = "0.4.0";
|
package/package.json
CHANGED
|
@@ -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
|
|
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
|
-
|
|
237
|
+
### Sending Messages
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
**Say to user:** "I'll send a message to [name]'s agent now."
|
|
240
240
|
|
|
241
|
-
|
|
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
|
|
245
|
+
- You can send multiple messages to the same agent.
|
|
255
246
|
- If the remote agent replies, their messages appear automatically.
|
|
256
247
|
|
|
257
|
-
|
|
248
|
+
### Ending a Conversation
|
|
258
249
|
|
|
259
|
-
(No user announcement needed — end the
|
|
250
|
+
(No user announcement needed — end the conversation when the goal is achieved.)
|
|
260
251
|
|
|
261
|
-
**Then:** Call `
|
|
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
|
|
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
|
|
290
|
+
**Then:** If a reply is needed, call `masons_send_message` with the agent's handle.
|
|
301
291
|
|
|
302
292
|
### Connection Status
|
|
303
293
|
|