@nekzus/liop 2.5.0 → 2.6.1
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/NOTICE +19 -0
- package/README.md +67 -34
- package/dist/bin/agent.js +673 -82
- package/dist/bin/agent.js.map +1 -1
- package/dist/bridge.d.ts +3 -3
- package/dist/bridge.js +3 -3
- package/dist/{chunk-PYKRQAER.js → chunk-2KSO3W4A.js} +2 -2
- package/dist/chunk-2KSO3W4A.js.map +1 -0
- package/dist/{chunk-32ADSAJS.js → chunk-2STZHJ24.js} +14 -4
- package/dist/chunk-2STZHJ24.js.map +1 -0
- package/dist/chunk-3QH44NKZ.js +115 -0
- package/dist/chunk-3QH44NKZ.js.map +1 -0
- package/dist/{chunk-WCCGE4QR.js → chunk-5SWHE62F.js} +196 -33
- package/dist/chunk-5SWHE62F.js.map +1 -0
- package/dist/{chunk-SONUORBT.js → chunk-7BSCUYUE.js} +2 -2
- package/dist/chunk-7BSCUYUE.js.map +1 -0
- package/dist/{chunk-QLCOEP5J.js → chunk-7PITYWOV.js} +2 -2
- package/dist/chunk-7PITYWOV.js.map +1 -0
- package/dist/{chunk-T5IUZF47.js → chunk-EJ426URA.js} +109 -7
- package/dist/chunk-EJ426URA.js.map +1 -0
- package/dist/{chunk-CRZBVFDV.js → chunk-FCFTYOAS.js} +243 -66
- package/dist/chunk-FCFTYOAS.js.map +1 -0
- package/dist/chunk-FTF6AMF6.js +3 -0
- package/dist/{chunk-RWRRBYG4.js.map → chunk-FTF6AMF6.js.map} +1 -1
- package/dist/{chunk-OUUTDSOW.js → chunk-IK7CGWAZ.js} +3 -3
- package/dist/chunk-IK7CGWAZ.js.map +1 -0
- package/dist/chunk-K3DGJLUY.js +106 -0
- package/dist/chunk-K3DGJLUY.js.map +1 -0
- package/dist/{chunk-U5BZ66VX.js → chunk-PAICJCKJ.js} +60 -175
- package/dist/chunk-PAICJCKJ.js.map +1 -0
- package/dist/chunk-PUDETPL4.js +279 -0
- package/dist/chunk-PUDETPL4.js.map +1 -0
- package/dist/{chunk-547TYL27.js → chunk-UJFWPRDM.js} +164 -1389
- package/dist/chunk-UJFWPRDM.js.map +1 -0
- package/dist/chunk-VRB5HUYU.js +1492 -0
- package/dist/chunk-VRB5HUYU.js.map +1 -0
- package/dist/{chunk-GA3Q2S7O.js → chunk-VRDZ5IKE.js} +5 -5
- package/dist/chunk-VRDZ5IKE.js.map +1 -0
- package/dist/chunk-WPTOJYN5.js +181 -0
- package/dist/chunk-WPTOJYN5.js.map +1 -0
- package/dist/{chunk-74CISO2I.js → chunk-XRDN232O.js} +28 -4
- package/dist/chunk-XRDN232O.js.map +1 -0
- package/dist/{chunk-JIJPZFR2.js → chunk-Y62GORQF.js} +2 -2
- package/dist/chunk-Y62GORQF.js.map +1 -0
- package/dist/chunk-ZP3VVXOI.js +66 -0
- package/dist/chunk-ZP3VVXOI.js.map +1 -0
- package/dist/client.d.ts +4 -3
- package/dist/client.js +10 -8
- package/dist/gateway.d.ts +4 -4
- package/dist/gateway.js +10 -9
- package/dist/{hybrid-CF__0YnC.d.ts → hybrid-DWe1bMoV.d.ts} +5 -4
- package/dist/{index-cMV0_yD9.d.ts → index-BuEQtBHD.d.ts} +39 -34
- package/dist/{index-BW95kAL4.d.ts → index-CdW8j8Bn.d.ts} +9 -72
- package/dist/index.d.ts +172 -48
- package/dist/index.js +20 -15
- package/dist/index.js.map +1 -1
- package/dist/kyber-CGOA7Q6A.js +3 -0
- package/dist/{kyber-3ULIJSE3.js.map → kyber-CGOA7Q6A.js.map} +1 -1
- package/dist/mesh.d.ts +27 -143
- package/dist/mesh.js +4 -3
- package/dist/node-B0b2vUFS.d.ts +164 -0
- package/dist/protocol/liop_core.proto +3 -0
- package/dist/server.d.ts +3 -3
- package/dist/server.js +7 -6
- package/dist/types-CzRSOlOo.d.ts +452 -0
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -1
- package/dist/verifier-XFU7ZB2W.js +6 -0
- package/dist/{verifier-3FAKCFNN.js.map → verifier-XFU7ZB2W.js.map} +1 -1
- package/dist/workers/logic-execution.js +3 -3
- package/dist/workers/logic-execution.js.map +1 -1
- package/dist/workers/zk-verifier.js +1 -1
- package/dist/workers/zk-verifier.js.map +1 -1
- package/package.json +50 -44
- package/dist/chunk-32ADSAJS.js.map +0 -1
- package/dist/chunk-547TYL27.js.map +0 -1
- package/dist/chunk-72MNYFR6.js +0 -64
- package/dist/chunk-72MNYFR6.js.map +0 -1
- package/dist/chunk-74CISO2I.js.map +0 -1
- package/dist/chunk-CRZBVFDV.js.map +0 -1
- package/dist/chunk-GA3Q2S7O.js.map +0 -1
- package/dist/chunk-JIJPZFR2.js.map +0 -1
- package/dist/chunk-OUUTDSOW.js.map +0 -1
- package/dist/chunk-PYKRQAER.js.map +0 -1
- package/dist/chunk-QLCOEP5J.js.map +0 -1
- package/dist/chunk-RWRRBYG4.js +0 -3
- package/dist/chunk-SONUORBT.js.map +0 -1
- package/dist/chunk-T5IUZF47.js.map +0 -1
- package/dist/chunk-U5BZ66VX.js.map +0 -1
- package/dist/chunk-WCCGE4QR.js.map +0 -1
- package/dist/chunk-WSW6YJNY.js +0 -168
- package/dist/chunk-WSW6YJNY.js.map +0 -1
- package/dist/kyber-3ULIJSE3.js +0 -3
- package/dist/types-lJIxBlWF.d.ts +0 -256
- package/dist/verifier-3FAKCFNN.js +0 -5
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { log } from './chunk-K3DGJLUY.js';
|
|
2
|
+
|
|
3
|
+
// src/runtime/routing-table.ts
|
|
4
|
+
var RoutingTable = class _RoutingTable {
|
|
5
|
+
routes = /* @__PURE__ */ new Map();
|
|
6
|
+
static MAX_FAILURES = 5;
|
|
7
|
+
/**
|
|
8
|
+
* Registers or updates a collection of tools exposed via an HTTP Gateway.
|
|
9
|
+
*/
|
|
10
|
+
registerGatewayTools(tools, mcpEndpoint, authRequired = true) {
|
|
11
|
+
for (const tool of tools) {
|
|
12
|
+
const existing = this.routes.get(tool.name);
|
|
13
|
+
this.routes.set(tool.name, {
|
|
14
|
+
name: tool.name,
|
|
15
|
+
provider: "http-gateway",
|
|
16
|
+
endpoint: mcpEndpoint,
|
|
17
|
+
description: tool.description ?? existing?.description,
|
|
18
|
+
inputSchema: tool.inputSchema ?? existing?.inputSchema,
|
|
19
|
+
latencyMs: existing?.latencyMs ?? 0,
|
|
20
|
+
failCount: 0,
|
|
21
|
+
lastSuccess: existing?.lastSuccess ?? Date.now(),
|
|
22
|
+
authRequired
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
log.info(
|
|
26
|
+
`[RoutingTable] Registered ${tools.length} tool(s) via HTTP Gateway (${mcpEndpoint})`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Registers tools discovered directly over the P2P Mesh (gRPC).
|
|
31
|
+
*/
|
|
32
|
+
registerMeshTools(tools, grpcTarget, authRequired = false) {
|
|
33
|
+
for (const tool of tools) {
|
|
34
|
+
const existing = this.routes.get(tool.name);
|
|
35
|
+
if (existing && existing.provider === "http-gateway" && existing.failCount === 0) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
this.routes.set(tool.name, {
|
|
39
|
+
name: tool.name,
|
|
40
|
+
provider: "p2p-grpc",
|
|
41
|
+
endpoint: grpcTarget,
|
|
42
|
+
description: tool.description ?? existing?.description,
|
|
43
|
+
inputSchema: tool.inputSchema ?? existing?.inputSchema,
|
|
44
|
+
latencyMs: existing?.latencyMs ?? 0,
|
|
45
|
+
failCount: 0,
|
|
46
|
+
lastSuccess: existing?.lastSuccess ?? Date.now(),
|
|
47
|
+
authRequired
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
log.info(
|
|
51
|
+
`[RoutingTable] Registered ${tools.length} tool(s) via P2P Mesh gRPC (${grpcTarget})`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Registers an in-process local tool (e.g. LiopMeshStatus).
|
|
56
|
+
*/
|
|
57
|
+
registerLocalTool(tool) {
|
|
58
|
+
this.routes.set(tool.name, {
|
|
59
|
+
name: tool.name,
|
|
60
|
+
provider: "local",
|
|
61
|
+
endpoint: "in-process",
|
|
62
|
+
description: tool.description,
|
|
63
|
+
inputSchema: tool.inputSchema,
|
|
64
|
+
latencyMs: 0,
|
|
65
|
+
failCount: 0,
|
|
66
|
+
lastSuccess: Date.now(),
|
|
67
|
+
authRequired: false
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Resolves the optimal route for a specific tool.
|
|
72
|
+
*/
|
|
73
|
+
resolve(toolName) {
|
|
74
|
+
const route = this.routes.get(toolName);
|
|
75
|
+
if (!route) return void 0;
|
|
76
|
+
if (route.failCount >= _RoutingTable.MAX_FAILURES) {
|
|
77
|
+
log.warn(
|
|
78
|
+
`[RoutingTable] Tool '${toolName}' route has tripped circuit breaker (${route.failCount} consecutive failures).`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return route;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Records a successful invocation on a route, resetting failures and updating latency.
|
|
85
|
+
*/
|
|
86
|
+
recordSuccess(toolName, latencyMs) {
|
|
87
|
+
const route = this.routes.get(toolName);
|
|
88
|
+
if (route) {
|
|
89
|
+
route.failCount = 0;
|
|
90
|
+
route.latencyMs = latencyMs;
|
|
91
|
+
route.lastSuccess = Date.now();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Records a failed invocation on a route.
|
|
96
|
+
*/
|
|
97
|
+
recordFailure(toolName) {
|
|
98
|
+
const route = this.routes.get(toolName);
|
|
99
|
+
if (route) {
|
|
100
|
+
route.failCount += 1;
|
|
101
|
+
log.warn(
|
|
102
|
+
`[RoutingTable] Failure recorded for tool '${toolName}' (failCount: ${route.failCount})`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Lists all registered tools in sorted format for tools/list.
|
|
108
|
+
*/
|
|
109
|
+
getAllToolDefinitions() {
|
|
110
|
+
const list = [];
|
|
111
|
+
for (const route of this.routes.values()) {
|
|
112
|
+
list.push({
|
|
113
|
+
name: route.name,
|
|
114
|
+
description: route.description,
|
|
115
|
+
inputSchema: route.inputSchema
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return list.sort((a, b) => a.name.localeCompare(b.name));
|
|
119
|
+
}
|
|
120
|
+
getRoute(toolName) {
|
|
121
|
+
return this.routes.get(toolName);
|
|
122
|
+
}
|
|
123
|
+
size() {
|
|
124
|
+
return this.routes.size;
|
|
125
|
+
}
|
|
126
|
+
clear() {
|
|
127
|
+
this.routes.clear();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// src/runtime/topology-probe.ts
|
|
132
|
+
async function probeGatewayUrl(candidateUrl, options) {
|
|
133
|
+
const trimmedUrl = candidateUrl.replace(/\/+$/, "");
|
|
134
|
+
const healthUrl = `${trimmedUrl}/health`;
|
|
135
|
+
try {
|
|
136
|
+
log.info(`[TopologyProbe] Probing candidate Gateway at: ${healthUrl}`);
|
|
137
|
+
const healthRes = await fetch(healthUrl, {
|
|
138
|
+
headers: { Accept: "application/json" },
|
|
139
|
+
signal: AbortSignal.timeout(3e3)
|
|
140
|
+
});
|
|
141
|
+
if (!healthRes.ok) {
|
|
142
|
+
log.info(
|
|
143
|
+
`[TopologyProbe] Probe failed at ${healthUrl} [HTTP ${healthRes.status}]`
|
|
144
|
+
);
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
const healthData = await healthRes.json();
|
|
148
|
+
if (healthData.status !== "healthy") {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
let issuer = healthData.auth?.issuer || "";
|
|
152
|
+
let tokenEndpoint = healthData.auth?.token_endpoint || "";
|
|
153
|
+
let audience = options.audience || "urn:liop:mesh:api";
|
|
154
|
+
let scopes = [
|
|
155
|
+
"liop:tools:list",
|
|
156
|
+
"liop:tools:call",
|
|
157
|
+
"liop:resources:read",
|
|
158
|
+
"liop:schema:read",
|
|
159
|
+
"liop:mesh:query"
|
|
160
|
+
];
|
|
161
|
+
try {
|
|
162
|
+
const prmUrl = `${trimmedUrl}/.well-known/oauth-protected-resource`;
|
|
163
|
+
const prmRes = await fetch(prmUrl, {
|
|
164
|
+
headers: { Accept: "application/json" },
|
|
165
|
+
signal: AbortSignal.timeout(2e3)
|
|
166
|
+
});
|
|
167
|
+
if (prmRes.ok) {
|
|
168
|
+
const prmData = await prmRes.json();
|
|
169
|
+
if (prmData.resource) audience = prmData.resource;
|
|
170
|
+
if (prmData.authorization_servers?.length) {
|
|
171
|
+
issuer = prmData.authorization_servers[0];
|
|
172
|
+
}
|
|
173
|
+
if (prmData.scopes_supported?.length) {
|
|
174
|
+
scopes = prmData.scopes_supported;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
if (!tokenEndpoint && issuer) {
|
|
180
|
+
const baseIssuer = issuer.endsWith("/oidc") ? issuer : `${issuer}/oidc`;
|
|
181
|
+
tokenEndpoint = `${baseIssuer}/token`;
|
|
182
|
+
}
|
|
183
|
+
if (tokenEndpoint) {
|
|
184
|
+
try {
|
|
185
|
+
const parsedEndpoint = new URL(tokenEndpoint);
|
|
186
|
+
if (options.nexusUrl) {
|
|
187
|
+
const nexusBase = options.nexusUrl.replace(/\/+$/, "");
|
|
188
|
+
const nexusHost = new URL(nexusBase).host;
|
|
189
|
+
parsedEndpoint.host = nexusHost;
|
|
190
|
+
tokenEndpoint = parsedEndpoint.toString();
|
|
191
|
+
} else if ((parsedEndpoint.hostname === "nexus" || parsedEndpoint.hostname === "nexus-prod" || parsedEndpoint.hostname.startsWith("172.")) && trimmedUrl.includes("127.0.0.1")) {
|
|
192
|
+
parsedEndpoint.host = "127.0.0.1:15000";
|
|
193
|
+
tokenEndpoint = parsedEndpoint.toString();
|
|
194
|
+
}
|
|
195
|
+
} catch {
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
log.info(
|
|
199
|
+
`[TopologyProbe] \u2705 Verified Gateway at ${trimmedUrl} (Tools: ${healthData.tools?.length ?? 0}, Tier: ${healthData.topology?.tier ?? 2})`
|
|
200
|
+
);
|
|
201
|
+
return {
|
|
202
|
+
baseUrl: trimmedUrl,
|
|
203
|
+
mcpEndpoint: `${trimmedUrl}/mcp`,
|
|
204
|
+
healthEndpoint: `${trimmedUrl}/health`,
|
|
205
|
+
tokenEndpoint,
|
|
206
|
+
issuer,
|
|
207
|
+
audience,
|
|
208
|
+
scopes,
|
|
209
|
+
tools: healthData.tools || [],
|
|
210
|
+
tier: healthData.topology?.tier ?? 2
|
|
211
|
+
};
|
|
212
|
+
} catch (err) {
|
|
213
|
+
log.info(
|
|
214
|
+
`[TopologyProbe] Gateway probe unreachable at ${trimmedUrl}: ${err instanceof Error ? err.message : String(err)}`
|
|
215
|
+
);
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async function probeTopology(options = {}) {
|
|
220
|
+
log.info("[TopologyProbe] Starting adaptive network topology discovery...");
|
|
221
|
+
let gatewayInfo = null;
|
|
222
|
+
if (options.blgUrl) {
|
|
223
|
+
gatewayInfo = await probeGatewayUrl(options.blgUrl, options);
|
|
224
|
+
}
|
|
225
|
+
if (!gatewayInfo && (options.autoProbeLocal ?? true)) {
|
|
226
|
+
const candidateUrls = [
|
|
227
|
+
"http://127.0.0.1:15018",
|
|
228
|
+
// Production BLG port
|
|
229
|
+
"http://127.0.0.1:15000",
|
|
230
|
+
// Production Nexus port
|
|
231
|
+
"http://127.0.0.1:13000"
|
|
232
|
+
// Demo Nexus port
|
|
233
|
+
];
|
|
234
|
+
for (const url of candidateUrls) {
|
|
235
|
+
if (options.blgUrl && url.includes(new URL(options.blgUrl).port))
|
|
236
|
+
continue;
|
|
237
|
+
gatewayInfo = await probeGatewayUrl(url, options);
|
|
238
|
+
if (gatewayInfo) break;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const bootstrapNodes = options.bootstrapNodes || [];
|
|
242
|
+
const hasMeshBootstraps = bootstrapNodes.length > 0;
|
|
243
|
+
if (gatewayInfo && hasMeshBootstraps) {
|
|
244
|
+
log.info(
|
|
245
|
+
`[TopologyProbe] \u26A1 Mode Determined: HYBRID (Gateway: ${gatewayInfo.baseUrl} + ${bootstrapNodes.length} P2P bootstraps)`
|
|
246
|
+
);
|
|
247
|
+
return {
|
|
248
|
+
mode: "hybrid",
|
|
249
|
+
gateway: gatewayInfo,
|
|
250
|
+
mesh: {
|
|
251
|
+
bootstrapNodes,
|
|
252
|
+
identityPath: options.identityPath
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
if (gatewayInfo) {
|
|
257
|
+
log.info(
|
|
258
|
+
`[TopologyProbe] \u{1F680} Mode Determined: GATEWAY (Zero-P2P overhead, direct to ${gatewayInfo.baseUrl})`
|
|
259
|
+
);
|
|
260
|
+
return {
|
|
261
|
+
mode: "gateway",
|
|
262
|
+
gateway: gatewayInfo
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
log.info(
|
|
266
|
+
`[TopologyProbe] \u{1F310} Mode Determined: MESH (Standard P2P DHT, Bootstraps: ${bootstrapNodes.length})`
|
|
267
|
+
);
|
|
268
|
+
return {
|
|
269
|
+
mode: "mesh",
|
|
270
|
+
mesh: {
|
|
271
|
+
bootstrapNodes,
|
|
272
|
+
identityPath: options.identityPath
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export { RoutingTable, probeTopology };
|
|
278
|
+
//# sourceMappingURL=chunk-PUDETPL4.js.map
|
|
279
|
+
//# sourceMappingURL=chunk-PUDETPL4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/runtime/routing-table.ts","../src/runtime/topology-probe.ts"],"names":[],"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AAM,cAAA,GAAiB,OAAA,CAAQ,cAAA,IAAkB,EAAC;AAClD,EAAA,MAAM,iBAAA,GAAoB,eAAe,MAAA,GAAS,CAAA;AAGlD,EAAA,IAAI,eAAe,iBAAA,EAAmB;AACrC,IAAA,GAAA,CAAI,IAAA;AAAA,MACH,CAAA,yDAAA,EAAuD,WAAA,CAAY,OAAO,CAAA,GAAA,EAAM,eAAe,MAAM,CAAA,gBAAA;AAAA,KACtG;AACA,IAAA,OAAO;AAAA,MACN,IAAA,EAAM,QAAA;AAAA,MACN,OAAA,EAAS,WAAA;AAAA,MACT,IAAA,EAAM;AAAA,QACL,cAAA;AAAA,QACA,cAAc,OAAA,CAAQ;AAAA;AACvB,KACD;AAAA,EACD;AAEA,EAAA,IAAI,WAAA,EAAa;AAChB,IAAA,GAAA,CAAI,IAAA;AAAA,MACH,CAAA,iFAAA,EAA6E,YAAY,OAAO,CAAA,CAAA;AAAA,KACjG;AACA,IAAA,OAAO;AAAA,MACN,IAAA,EAAM,SAAA;AAAA,MACN,OAAA,EAAS;AAAA,KACV;AAAA,EACD;AAEA,EAAA,GAAA,CAAI,IAAA;AAAA,IACH,CAAA,+EAAA,EAA2E,eAAe,MAAM,CAAA,CAAA;AAAA,GACjG;AACA,EAAA,OAAO;AAAA,IACN,IAAA,EAAM,MAAA;AAAA,IACN,IAAA,EAAM;AAAA,MACL,cAAA;AAAA,MACA,cAAc,OAAA,CAAQ;AAAA;AACvB,GACD;AACD","file":"chunk-PUDETPL4.js","sourcesContent":["// Copyright 2026 Nekzus Solutions and contributors\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * LIOP Routing Table — Per-Tool Hybrid Routing & Resilience\n *\n * Maintains deterministic per-tool routes across heterogeneous transports:\n * - \"http-gateway\": Route through Border LIO Gateway or reverse proxy via HTTP/JSON-RPC.\n * - \"p2p-grpc\": Route directly to sovereign node via libp2p + gRPC (Kyber768/ZK-Receipt).\n * - \"local\": Handled in-process (e.g. LiopMeshStatus, diagnostic probes).\n *\n * Implements per-route latency tracking, failure backoff, and circuit breaking.\n */\n\nimport { log } from \"../utils/logger.js\";\n\nexport type TransportProvider = \"http-gateway\" | \"p2p-grpc\" | \"local\";\n\nexport interface ToolDefinition {\n\tname: string;\n\tdescription?: string;\n\tinputSchema?: Record<string, unknown>;\n}\n\nexport interface ToolRoute {\n\tname: string;\n\tprovider: TransportProvider;\n\tendpoint: string;\n\tdescription?: string;\n\tinputSchema?: Record<string, unknown>;\n\tlatencyMs: number;\n\tfailCount: number;\n\tlastSuccess: number;\n\tauthRequired?: boolean;\n}\n\nexport class RoutingTable {\n\tprivate routes = new Map<string, ToolRoute>();\n\tprivate static readonly MAX_FAILURES = 5;\n\n\t/**\n\t * Registers or updates a collection of tools exposed via an HTTP Gateway.\n\t */\n\tpublic registerGatewayTools(\n\t\ttools: ToolDefinition[],\n\t\tmcpEndpoint: string,\n\t\tauthRequired = true,\n\t): void {\n\t\tfor (const tool of tools) {\n\t\t\tconst existing = this.routes.get(tool.name);\n\t\t\tthis.routes.set(tool.name, {\n\t\t\t\tname: tool.name,\n\t\t\t\tprovider: \"http-gateway\",\n\t\t\t\tendpoint: mcpEndpoint,\n\t\t\t\tdescription: tool.description ?? existing?.description,\n\t\t\t\tinputSchema: tool.inputSchema ?? existing?.inputSchema,\n\t\t\t\tlatencyMs: existing?.latencyMs ?? 0,\n\t\t\t\tfailCount: 0,\n\t\t\t\tlastSuccess: existing?.lastSuccess ?? Date.now(),\n\t\t\t\tauthRequired,\n\t\t\t});\n\t\t}\n\t\tlog.info(\n\t\t\t`[RoutingTable] Registered ${tools.length} tool(s) via HTTP Gateway (${mcpEndpoint})`,\n\t\t);\n\t}\n\n\t/**\n\t * Registers tools discovered directly over the P2P Mesh (gRPC).\n\t */\n\tpublic registerMeshTools(\n\t\ttools: ToolDefinition[],\n\t\tgrpcTarget: string,\n\t\tauthRequired = false,\n\t): void {\n\t\tfor (const tool of tools) {\n\t\t\tconst existing = this.routes.get(tool.name);\n\t\t\t// Do not overwrite healthy gateway routes unless explicitly preferred\n\t\t\tif (\n\t\t\t\texisting &&\n\t\t\t\texisting.provider === \"http-gateway\" &&\n\t\t\t\texisting.failCount === 0\n\t\t\t) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.routes.set(tool.name, {\n\t\t\t\tname: tool.name,\n\t\t\t\tprovider: \"p2p-grpc\",\n\t\t\t\tendpoint: grpcTarget,\n\t\t\t\tdescription: tool.description ?? existing?.description,\n\t\t\t\tinputSchema: tool.inputSchema ?? existing?.inputSchema,\n\t\t\t\tlatencyMs: existing?.latencyMs ?? 0,\n\t\t\t\tfailCount: 0,\n\t\t\t\tlastSuccess: existing?.lastSuccess ?? Date.now(),\n\t\t\t\tauthRequired,\n\t\t\t});\n\t\t}\n\t\tlog.info(\n\t\t\t`[RoutingTable] Registered ${tools.length} tool(s) via P2P Mesh gRPC (${grpcTarget})`,\n\t\t);\n\t}\n\n\t/**\n\t * Registers an in-process local tool (e.g. LiopMeshStatus).\n\t */\n\tpublic registerLocalTool(tool: ToolDefinition): void {\n\t\tthis.routes.set(tool.name, {\n\t\t\tname: tool.name,\n\t\t\tprovider: \"local\",\n\t\t\tendpoint: \"in-process\",\n\t\t\tdescription: tool.description,\n\t\t\tinputSchema: tool.inputSchema,\n\t\t\tlatencyMs: 0,\n\t\t\tfailCount: 0,\n\t\t\tlastSuccess: Date.now(),\n\t\t\tauthRequired: false,\n\t\t});\n\t}\n\n\t/**\n\t * Resolves the optimal route for a specific tool.\n\t */\n\tpublic resolve(toolName: string): ToolRoute | undefined {\n\t\tconst route = this.routes.get(toolName);\n\t\tif (!route) return undefined;\n\n\t\t// If circuit is tripped (excessive failures), still return route but log warning\n\t\tif (route.failCount >= RoutingTable.MAX_FAILURES) {\n\t\t\tlog.warn(\n\t\t\t\t`[RoutingTable] Tool '${toolName}' route has tripped circuit breaker (${route.failCount} consecutive failures).`,\n\t\t\t);\n\t\t}\n\n\t\treturn route;\n\t}\n\n\t/**\n\t * Records a successful invocation on a route, resetting failures and updating latency.\n\t */\n\tpublic recordSuccess(toolName: string, latencyMs: number): void {\n\t\tconst route = this.routes.get(toolName);\n\t\tif (route) {\n\t\t\troute.failCount = 0;\n\t\t\troute.latencyMs = latencyMs;\n\t\t\troute.lastSuccess = Date.now();\n\t\t}\n\t}\n\n\t/**\n\t * Records a failed invocation on a route.\n\t */\n\tpublic recordFailure(toolName: string): void {\n\t\tconst route = this.routes.get(toolName);\n\t\tif (route) {\n\t\t\troute.failCount += 1;\n\t\t\tlog.warn(\n\t\t\t\t`[RoutingTable] Failure recorded for tool '${toolName}' (failCount: ${route.failCount})`,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Lists all registered tools in sorted format for tools/list.\n\t */\n\tpublic getAllToolDefinitions(): ToolDefinition[] {\n\t\tconst list: ToolDefinition[] = [];\n\t\tfor (const route of this.routes.values()) {\n\t\t\tlist.push({\n\t\t\t\tname: route.name,\n\t\t\t\tdescription: route.description,\n\t\t\t\tinputSchema: route.inputSchema,\n\t\t\t});\n\t\t}\n\t\treturn list.sort((a, b) => a.name.localeCompare(b.name));\n\t}\n\n\tpublic getRoute(toolName: string): ToolRoute | undefined {\n\t\treturn this.routes.get(toolName);\n\t}\n\n\tpublic size(): number {\n\t\treturn this.routes.size;\n\t}\n\n\tpublic clear(): void {\n\t\tthis.routes.clear();\n\t}\n}\n","// Copyright 2026 Nekzus Solutions and contributors\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * LIOP Topology Probe — RFC 9728 PRM Chain & Adaptive Network Detection\n *\n * Implements single-URL auto-discovery of multi-tier mesh environments:\n * 1. Probes RFC 9728 Protected Resource Metadata (PRM) or /health endpoint.\n * 2. Chains discovery to obtain OAuth 2.1 authorization server endpoints.\n * 3. Identifies whether the node should operate in \"gateway\", \"mesh\", or \"hybrid\" mode.\n *\n * Standards: RFC 9728, RFC 8414, RFC 8707, NIST SP 800-207\n */\n\nimport { log } from \"../utils/logger.js\";\n\nexport interface GatewayTopologyInfo {\n\tbaseUrl: string;\n\tmcpEndpoint: string;\n\thealthEndpoint: string;\n\ttokenEndpoint: string;\n\tissuer: string;\n\taudience: string;\n\tscopes: string[];\n\ttools: string[];\n\ttier: number;\n}\n\nexport interface MeshTopologyInfo {\n\tbootstrapNodes: string[];\n\tidentityPath?: string;\n}\n\nexport interface TopologyProbeResult {\n\tmode: \"gateway\" | \"mesh\" | \"hybrid\";\n\tgateway?: GatewayTopologyInfo;\n\tmesh?: MeshTopologyInfo;\n}\n\nexport interface TopologyProbeOptions {\n\tblgUrl?: string;\n\tnexusUrl?: string;\n\tclientId?: string;\n\tclientSecret?: string;\n\taudience?: string;\n\tstaticToken?: string;\n\tbootstrapNodes?: string[];\n\tidentityPath?: string;\n\tautoProbeLocal?: boolean;\n}\n\n/**\n * Executes the RFC 9728 PRM chain and health probe on a candidate Gateway URL.\n */\nasync function probeGatewayUrl(\n\tcandidateUrl: string,\n\toptions: TopologyProbeOptions,\n): Promise<GatewayTopologyInfo | null> {\n\tconst trimmedUrl = candidateUrl.replace(/\\/+$/, \"\");\n\tconst healthUrl = `${trimmedUrl}/health`;\n\n\ttry {\n\t\tlog.info(`[TopologyProbe] Probing candidate Gateway at: ${healthUrl}`);\n\t\tconst healthRes = await fetch(healthUrl, {\n\t\t\theaders: { Accept: \"application/json\" },\n\t\t\tsignal: AbortSignal.timeout(3000),\n\t\t});\n\n\t\tif (!healthRes.ok) {\n\t\t\tlog.info(\n\t\t\t\t`[TopologyProbe] Probe failed at ${healthUrl} [HTTP ${healthRes.status}]`,\n\t\t\t);\n\t\t\treturn null;\n\t\t}\n\n\t\tconst healthData = (await healthRes.json()) as {\n\t\t\tstatus?: string;\n\t\t\ttools?: string[];\n\t\t\tauth?: {\n\t\t\t\tissuer?: string;\n\t\t\t\ttoken_endpoint?: string;\n\t\t\t\tjwks_uri?: string;\n\t\t\t};\n\t\t\ttopology?: {\n\t\t\t\ttier?: number;\n\t\t\t};\n\t\t};\n\n\t\tif (healthData.status !== \"healthy\") {\n\t\t\treturn null;\n\t\t}\n\n\t\t// Discover OAuth metadata via PRM (RFC 9728)\n\t\tlet issuer = healthData.auth?.issuer || \"\";\n\t\tlet tokenEndpoint = healthData.auth?.token_endpoint || \"\";\n\t\tlet audience = options.audience || \"urn:liop:mesh:api\";\n\t\tlet scopes: string[] = [\n\t\t\t\"liop:tools:list\",\n\t\t\t\"liop:tools:call\",\n\t\t\t\"liop:resources:read\",\n\t\t\t\"liop:schema:read\",\n\t\t\t\"liop:mesh:query\",\n\t\t];\n\n\t\ttry {\n\t\t\tconst prmUrl = `${trimmedUrl}/.well-known/oauth-protected-resource`;\n\t\t\tconst prmRes = await fetch(prmUrl, {\n\t\t\t\theaders: { Accept: \"application/json\" },\n\t\t\t\tsignal: AbortSignal.timeout(2000),\n\t\t\t});\n\t\t\tif (prmRes.ok) {\n\t\t\t\tconst prmData = (await prmRes.json()) as {\n\t\t\t\t\tresource?: string;\n\t\t\t\t\tauthorization_servers?: string[];\n\t\t\t\t\tscopes_supported?: string[];\n\t\t\t\t};\n\t\t\t\tif (prmData.resource) audience = prmData.resource;\n\t\t\t\tif (prmData.authorization_servers?.length) {\n\t\t\t\t\tissuer = prmData.authorization_servers[0];\n\t\t\t\t}\n\t\t\t\tif (prmData.scopes_supported?.length) {\n\t\t\t\t\tscopes = prmData.scopes_supported;\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Non-fatal if PRM endpoint is not present\n\t\t}\n\n\t\t// If token endpoint is not explicitly given, resolve from issuer\n\t\tif (!tokenEndpoint && issuer) {\n\t\t\tconst baseIssuer = issuer.endsWith(\"/oidc\") ? issuer : `${issuer}/oidc`;\n\t\t\ttokenEndpoint = `${baseIssuer}/token`;\n\t\t}\n\n\t\t// Normalize tokenEndpoint for Docker-to-Host bridge environments\n\t\tif (tokenEndpoint) {\n\t\t\ttry {\n\t\t\t\tconst parsedEndpoint = new URL(tokenEndpoint);\n\t\t\t\tif (options.nexusUrl) {\n\t\t\t\t\tconst nexusBase = options.nexusUrl.replace(/\\/+$/, \"\");\n\t\t\t\t\tconst nexusHost = new URL(nexusBase).host;\n\t\t\t\t\tparsedEndpoint.host = nexusHost;\n\t\t\t\t\ttokenEndpoint = parsedEndpoint.toString();\n\t\t\t\t} else if (\n\t\t\t\t\t(parsedEndpoint.hostname === \"nexus\" ||\n\t\t\t\t\t\tparsedEndpoint.hostname === \"nexus-prod\" ||\n\t\t\t\t\t\tparsedEndpoint.hostname.startsWith(\"172.\")) &&\n\t\t\t\t\ttrimmedUrl.includes(\"127.0.0.1\")\n\t\t\t\t) {\n\t\t\t\t\tparsedEndpoint.host = \"127.0.0.1:15000\";\n\t\t\t\t\ttokenEndpoint = parsedEndpoint.toString();\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t/* keep original tokenEndpoint */\n\t\t\t}\n\t\t}\n\n\t\tlog.info(\n\t\t\t`[TopologyProbe] ✅ Verified Gateway at ${trimmedUrl} (Tools: ${healthData.tools?.length ?? 0}, Tier: ${healthData.topology?.tier ?? 2})`,\n\t\t);\n\n\t\treturn {\n\t\t\tbaseUrl: trimmedUrl,\n\t\t\tmcpEndpoint: `${trimmedUrl}/mcp`,\n\t\t\thealthEndpoint: `${trimmedUrl}/health`,\n\t\t\ttokenEndpoint,\n\t\t\tissuer,\n\t\t\taudience,\n\t\t\tscopes,\n\t\t\ttools: healthData.tools || [],\n\t\t\ttier: healthData.topology?.tier ?? 2,\n\t\t};\n\t} catch (err) {\n\t\tlog.info(\n\t\t\t`[TopologyProbe] Gateway probe unreachable at ${trimmedUrl}: ${err instanceof Error ? err.message : String(err)}`,\n\t\t);\n\t\treturn null;\n\t}\n}\n\n/**\n * Probes the runtime environment and determines the optimal architecture mode.\n */\nexport async function probeTopology(\n\toptions: TopologyProbeOptions = {},\n): Promise<TopologyProbeResult> {\n\tlog.info(\"[TopologyProbe] Starting adaptive network topology discovery...\");\n\n\tlet gatewayInfo: GatewayTopologyInfo | null = null;\n\n\t// 1. Check explicit Border LIO Gateway URL (Highest priority)\n\tif (options.blgUrl) {\n\t\tgatewayInfo = await probeGatewayUrl(options.blgUrl, options);\n\t}\n\n\t// 2. Auto-probing known local infrastructure ports if enabled/unspecified\n\tif (!gatewayInfo && (options.autoProbeLocal ?? true)) {\n\t\tconst candidateUrls = [\n\t\t\t\"http://127.0.0.1:15018\", // Production BLG port\n\t\t\t\"http://127.0.0.1:15000\", // Production Nexus port\n\t\t\t\"http://127.0.0.1:13000\", // Demo Nexus port\n\t\t];\n\n\t\tfor (const url of candidateUrls) {\n\t\t\t// Skip if already probed\n\t\t\tif (options.blgUrl && url.includes(new URL(options.blgUrl).port))\n\t\t\t\tcontinue;\n\n\t\t\tgatewayInfo = await probeGatewayUrl(url, options);\n\t\t\tif (gatewayInfo) break;\n\t\t}\n\t}\n\n\t// 3. Evaluate P2P Mesh connectivity\n\tconst bootstrapNodes = options.bootstrapNodes || [];\n\tconst hasMeshBootstraps = bootstrapNodes.length > 0;\n\n\t// 4. Decide runtime mode\n\tif (gatewayInfo && hasMeshBootstraps) {\n\t\tlog.info(\n\t\t\t`[TopologyProbe] ⚡ Mode Determined: HYBRID (Gateway: ${gatewayInfo.baseUrl} + ${bootstrapNodes.length} P2P bootstraps)`,\n\t\t);\n\t\treturn {\n\t\t\tmode: \"hybrid\",\n\t\t\tgateway: gatewayInfo,\n\t\t\tmesh: {\n\t\t\t\tbootstrapNodes,\n\t\t\t\tidentityPath: options.identityPath,\n\t\t\t},\n\t\t};\n\t}\n\n\tif (gatewayInfo) {\n\t\tlog.info(\n\t\t\t`[TopologyProbe] 🚀 Mode Determined: GATEWAY (Zero-P2P overhead, direct to ${gatewayInfo.baseUrl})`,\n\t\t);\n\t\treturn {\n\t\t\tmode: \"gateway\",\n\t\t\tgateway: gatewayInfo,\n\t\t};\n\t}\n\n\tlog.info(\n\t\t`[TopologyProbe] 🌐 Mode Determined: MESH (Standard P2P DHT, Bootstraps: ${bootstrapNodes.length})`,\n\t);\n\treturn {\n\t\tmode: \"mesh\",\n\t\tmesh: {\n\t\t\tbootstrapNodes,\n\t\t\tidentityPath: options.identityPath,\n\t\t},\n\t};\n}\n"]}
|