libp2p-mesh 2026.5.21 → 2026.5.23
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/src/agent-tools.d.ts +1 -0
- package/dist/src/agent-tools.js +5 -5
- package/dist/src/inbound.js +23 -0
- package/package.json +1 -1
- package/src/agent-tools.ts +5 -5
- package/src/inbound.ts +23 -0
package/dist/src/agent-tools.js
CHANGED
|
@@ -171,14 +171,15 @@ export function buildP2PTools(mesh) {
|
|
|
171
171
|
const ch = identity.channel ? ` (${identity.channel})` : "";
|
|
172
172
|
return ` - ${identity.instanceId}${ch}`;
|
|
173
173
|
}
|
|
174
|
-
return ` - ${p
|
|
174
|
+
return ` - ${identity?.instanceId || p} (identity unknown)`;
|
|
175
175
|
})
|
|
176
176
|
.join("\n");
|
|
177
177
|
const text = `在线用户 (${peers.length}):\n${userList}`;
|
|
178
178
|
const safePeers = peers.map((p) => {
|
|
179
179
|
const identity = peerIdentityMap?.resolve(p);
|
|
180
180
|
return {
|
|
181
|
-
|
|
181
|
+
peerId: p,
|
|
182
|
+
instanceId: identity?.instanceId || p,
|
|
182
183
|
channel: identity?.channel || "unknown",
|
|
183
184
|
};
|
|
184
185
|
});
|
|
@@ -306,7 +307,7 @@ export function buildP2PTools(mesh) {
|
|
|
306
307
|
// Format output
|
|
307
308
|
const lines = [];
|
|
308
309
|
lines.push(`P2P Mesh Identity Table`);
|
|
309
|
-
lines.push(`Local: ${localPeerId
|
|
310
|
+
lines.push(`Local: ${localPeerId} | Instance: ${localInstanceId}`);
|
|
310
311
|
lines.push(`Connected: ${connectedPeers.length} | Known total: ${allPeers.length}`);
|
|
311
312
|
lines.push("");
|
|
312
313
|
if (allPeers.length === 0) {
|
|
@@ -314,9 +315,8 @@ export function buildP2PTools(mesh) {
|
|
|
314
315
|
}
|
|
315
316
|
else {
|
|
316
317
|
for (const p of allPeers) {
|
|
317
|
-
const shortId = p.peerId.slice(0, 16) + "...";
|
|
318
318
|
const time = p.lastSeen ? new Date(p.lastSeen).toLocaleTimeString() : "unknown";
|
|
319
|
-
lines.push(` [${p.status}] ${
|
|
319
|
+
lines.push(` [${p.status}] ${p.peerId}`);
|
|
320
320
|
lines.push(` Agent: ${p.agentId} | Channel: ${p.channel}`);
|
|
321
321
|
lines.push(` Instance: ${p.instanceId ?? "unknown"}`);
|
|
322
322
|
lines.push(` Last seen: ${time}`);
|
package/dist/src/inbound.js
CHANGED
|
@@ -58,6 +58,29 @@ export async function handleP2PInbound(msg, deps) {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
else if (msg.type === "direct") {
|
|
61
|
+
// onPeerConnect 通过 sendToPeer 发送 identity 时会被包装成
|
|
62
|
+
// type=direct 的外壳,真正的 identity 消息藏在 payload 里。
|
|
63
|
+
// 先检测并处理这种情况,确保双向身份交换能完成。
|
|
64
|
+
try {
|
|
65
|
+
const raw = JSON.parse(msg.payload);
|
|
66
|
+
if (raw && raw.type === "identity") {
|
|
67
|
+
await handleIdentityMessage(msg, {
|
|
68
|
+
peerIdentityMap: deps.peerIdentityMap,
|
|
69
|
+
localPeerId: deps.peerIdentityMap.getLocalIdentity()?.sessionKey ?? "",
|
|
70
|
+
localAgentId: "",
|
|
71
|
+
localChannel: "",
|
|
72
|
+
localAccountId: "",
|
|
73
|
+
localInstanceId: deps.peerIdentityMap.getLocalIdentity()?.instanceId,
|
|
74
|
+
send: async () => { }, // no-op; 双方都通过 onPeerConnect 主动发送身份
|
|
75
|
+
logger,
|
|
76
|
+
});
|
|
77
|
+
// identity 处理完毕,不再走 direct 消息路由
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// payload 不是 JSON,正常走 direct 消息逻辑
|
|
83
|
+
}
|
|
61
84
|
if (!msg.to) {
|
|
62
85
|
logger?.warn?.("[libp2p-mesh] Direct message missing 'to' field");
|
|
63
86
|
}
|
package/package.json
CHANGED
package/src/agent-tools.ts
CHANGED
|
@@ -172,14 +172,15 @@ export function buildP2PTools(mesh: MeshNetwork) {
|
|
|
172
172
|
const ch = identity.channel ? ` (${identity.channel})` : "";
|
|
173
173
|
return ` - ${identity.instanceId}${ch}`;
|
|
174
174
|
}
|
|
175
|
-
return ` - ${p
|
|
175
|
+
return ` - ${identity?.instanceId || p} (identity unknown)`;
|
|
176
176
|
})
|
|
177
177
|
.join("\n");
|
|
178
178
|
const text = `在线用户 (${peers.length}):\n${userList}`;
|
|
179
179
|
const safePeers = peers.map((p) => {
|
|
180
180
|
const identity = peerIdentityMap?.resolve(p);
|
|
181
181
|
return {
|
|
182
|
-
|
|
182
|
+
peerId: p,
|
|
183
|
+
instanceId: identity?.instanceId || p,
|
|
183
184
|
channel: identity?.channel || "unknown",
|
|
184
185
|
};
|
|
185
186
|
});
|
|
@@ -308,7 +309,7 @@ export function buildP2PTools(mesh: MeshNetwork) {
|
|
|
308
309
|
// Format output
|
|
309
310
|
const lines: string[] = [];
|
|
310
311
|
lines.push(`P2P Mesh Identity Table`);
|
|
311
|
-
lines.push(`Local: ${localPeerId
|
|
312
|
+
lines.push(`Local: ${localPeerId} | Instance: ${localInstanceId}`);
|
|
312
313
|
lines.push(`Connected: ${connectedPeers.length} | Known total: ${allPeers.length}`);
|
|
313
314
|
lines.push("");
|
|
314
315
|
|
|
@@ -316,9 +317,8 @@ export function buildP2PTools(mesh: MeshNetwork) {
|
|
|
316
317
|
lines.push("(no known peers yet)");
|
|
317
318
|
} else {
|
|
318
319
|
for (const p of allPeers) {
|
|
319
|
-
const shortId = p.peerId.slice(0, 16) + "...";
|
|
320
320
|
const time = p.lastSeen ? new Date(p.lastSeen).toLocaleTimeString() : "unknown";
|
|
321
|
-
lines.push(` [${p.status}] ${
|
|
321
|
+
lines.push(` [${p.status}] ${p.peerId}`);
|
|
322
322
|
lines.push(` Agent: ${p.agentId} | Channel: ${p.channel}`);
|
|
323
323
|
lines.push(` Instance: ${p.instanceId ?? "unknown"}`);
|
|
324
324
|
lines.push(` Last seen: ${time}`);
|
package/src/inbound.ts
CHANGED
|
@@ -75,6 +75,29 @@ export async function handleP2PInbound(msg: P2PMessage, deps?: InboundHandlerDep
|
|
|
75
75
|
logger?.error?.(`[libp2p-mesh] Identity message handler error: ${String(err)}`);
|
|
76
76
|
}
|
|
77
77
|
} else if (msg.type === "direct") {
|
|
78
|
+
// onPeerConnect 通过 sendToPeer 发送 identity 时会被包装成
|
|
79
|
+
// type=direct 的外壳,真正的 identity 消息藏在 payload 里。
|
|
80
|
+
// 先检测并处理这种情况,确保双向身份交换能完成。
|
|
81
|
+
try {
|
|
82
|
+
const raw = JSON.parse(msg.payload);
|
|
83
|
+
if (raw && raw.type === "identity") {
|
|
84
|
+
await handleIdentityMessage(msg, {
|
|
85
|
+
peerIdentityMap: deps.peerIdentityMap,
|
|
86
|
+
localPeerId: deps.peerIdentityMap.getLocalIdentity()?.sessionKey ?? "",
|
|
87
|
+
localAgentId: "",
|
|
88
|
+
localChannel: "",
|
|
89
|
+
localAccountId: "",
|
|
90
|
+
localInstanceId: deps.peerIdentityMap.getLocalIdentity()?.instanceId,
|
|
91
|
+
send: async () => {}, // no-op; 双方都通过 onPeerConnect 主动发送身份
|
|
92
|
+
logger,
|
|
93
|
+
});
|
|
94
|
+
// identity 处理完毕,不再走 direct 消息路由
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
} catch {
|
|
98
|
+
// payload 不是 JSON,正常走 direct 消息逻辑
|
|
99
|
+
}
|
|
100
|
+
|
|
78
101
|
if (!msg.to) {
|
|
79
102
|
logger?.warn?.("[libp2p-mesh] Direct message missing 'to' field");
|
|
80
103
|
} else {
|